blob: 6dd09fbc582464d18a96938890b654c8c619bc2a [file] [log] [blame]
Darin Petkov7326cd82012-03-30 11:02:01 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov823c47e2011-06-27 16:15:35 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/crypto_des_cbc.h"
6
7#include <rpc/des_crypt.h>
8
9#include <base/file_util.h>
10#include <base/string_util.h>
11
12#include "shill/glib.h"
13
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080014using base::FilePath;
Darin Petkov823c47e2011-06-27 16:15:35 -070015using std::string;
16using std::vector;
17
18namespace shill {
19
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050020const unsigned int CryptoDESCBC::kBlockSize = 8;
Darin Petkov823c47e2011-06-27 16:15:35 -070021const char CryptoDESCBC::kID[] = "des-cbc";
22const char CryptoDESCBC::kSentinel[] = "[ok]";
23const char CryptoDESCBC::kVersion2Prefix[] = "02:";
24
25CryptoDESCBC::CryptoDESCBC(GLib *glib) : glib_(glib) {}
26
27string CryptoDESCBC::GetID() {
28 return kID;
29}
30
31bool CryptoDESCBC::Encrypt(const string &plaintext, string *ciphertext) {
Darin Petkov7326cd82012-03-30 11:02:01 +020032 // Never encrypt. We'll fall back to rot47 which doesn't depend on
33 // the owner key which may change due to rotation.
34 return false;
Darin Petkov823c47e2011-06-27 16:15:35 -070035}
36
37bool CryptoDESCBC::Decrypt(const string &ciphertext, string *plaintext) {
38 CHECK_EQ(kBlockSize, key_.size());
39 CHECK_EQ(kBlockSize, iv_.size());
40 int version = 1;
41 string b64_ciphertext = ciphertext;
42 if (StartsWithASCII(ciphertext, kVersion2Prefix, true)) {
43 version = 2;
44 b64_ciphertext.erase(0, strlen(kVersion2Prefix));
45 }
46
Christopher Wiley0f3eab32013-03-21 11:55:41 -070047 string decoded_data;
48 if (!glib_->B64Decode(b64_ciphertext, &decoded_data)) {
Darin Petkov823c47e2011-06-27 16:15:35 -070049 LOG(ERROR) << "Unable to base64-decode DEC-CBC ciphertext.";
50 return false;
51 }
52
Christopher Wiley0f3eab32013-03-21 11:55:41 -070053 vector<char> data(decoded_data.c_str(),
54 decoded_data.c_str() + decoded_data.length());
Darin Petkov823c47e2011-06-27 16:15:35 -070055 if (data.empty() || (data.size() % kBlockSize != 0)) {
56 LOG(ERROR) << "Invalid DES-CBC ciphertext size: " << data.size();
57 return false;
58 }
59
60 // The IV is modified in place.
61 vector<char> iv = iv_;
62 int rv =
63 cbc_crypt(key_.data(), data.data(), data.size(), DES_DECRYPT, iv.data());
64 if (DES_FAILED(rv)) {
65 LOG(ERROR) << "DES-CBC decryption failed.";
66 return false;
67 }
68 if (data.back() != '\0') {
69 LOG(ERROR) << "DEC-CBC decryption resulted in invalid plain text.";
70 return false;
71 }
72 string text = data.data();
73 if (version == 2) {
74 if (!EndsWith(text, kSentinel, true)) {
75 LOG(ERROR) << "DES-CBC decrypted text missing sentinel -- bad key?";
76 return false;
77 }
78 text.erase(text.size() - strlen(kSentinel), strlen(kSentinel));
79 }
80 *plaintext = text;
81 return true;
82}
83
84bool CryptoDESCBC::LoadKeyMatter(const FilePath &path) {
85 key_.clear();
86 iv_.clear();
87 string matter;
88 // TODO(petkov): This mimics current flimflam behavior. Fix it so that it
89 // doesn't read the whole file.
90 if (!file_util::ReadFileToString(path, &matter)) {
91 LOG(ERROR) << "Unable to load key matter from " << path.value();
92 return false;
93 }
94 if (matter.size() < 2 * kBlockSize) {
95 LOG(ERROR) << "Key matter data not enough " << matter.size() << " < "
96 << 2 * kBlockSize;
97 return false;
98 }
99 string::const_iterator matter_start =
100 matter.begin() + (matter.size() - 2 * kBlockSize);
101 key_.assign(matter_start + kBlockSize, matter_start + 2 * kBlockSize);
102 iv_.assign(matter_start, matter_start + kBlockSize);
103 return true;
104}
105
106} // namespace shill