1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/crypto_des_cbc.h"
18
19 #include <rpc/des_crypt.h>
20
21 #include <base/files/file_util.h>
22 #include <base/strings/string_util.h>
23 #include <brillo/data_encoding.h>
24
25 using base::FilePath;
26 using std::string;
27 using std::vector;
28
29 namespace shill {
30
31 const unsigned int CryptoDESCBC::kBlockSize = 8;
32 const char CryptoDESCBC::kID[] = "des-cbc";
33 const char CryptoDESCBC::kSentinel[] = "[ok]";
34 const char CryptoDESCBC::kVersion2Prefix[] = "02:";
35
CryptoDESCBC()36 CryptoDESCBC::CryptoDESCBC() {}
37
GetID()38 string CryptoDESCBC::GetID() {
39 return kID;
40 }
41
Encrypt(const string & plaintext,string * ciphertext)42 bool CryptoDESCBC::Encrypt(const string& plaintext, string* ciphertext) {
43 // Never encrypt. We'll fall back to rot47 which doesn't depend on
44 // the owner key which may change due to rotation.
45 return false;
46 }
47
Decrypt(const string & ciphertext,string * plaintext)48 bool CryptoDESCBC::Decrypt(const string& ciphertext, string* plaintext) {
49 CHECK_EQ(kBlockSize, key_.size());
50 CHECK_EQ(kBlockSize, iv_.size());
51 int version = 1;
52 string b64_ciphertext = ciphertext;
53 if (base::StartsWith(ciphertext, kVersion2Prefix,
54 base::CompareCase::SENSITIVE)) {
55 version = 2;
56 b64_ciphertext.erase(0, strlen(kVersion2Prefix));
57 }
58
59 string decoded_data;
60 if (!brillo::data_encoding::Base64Decode(b64_ciphertext, &decoded_data)) {
61 LOG(ERROR) << "Unable to base64-decode DEC-CBC ciphertext.";
62 return false;
63 }
64
65 vector<char> data(decoded_data.c_str(),
66 decoded_data.c_str() + decoded_data.length());
67 if (data.empty() || (data.size() % kBlockSize != 0)) {
68 LOG(ERROR) << "Invalid DES-CBC ciphertext size: " << data.size();
69 return false;
70 }
71
72 // The IV is modified in place.
73 vector<char> iv = iv_;
74 int rv =
75 cbc_crypt(key_.data(), data.data(), data.size(), DES_DECRYPT, iv.data());
76 if (DES_FAILED(rv)) {
77 LOG(ERROR) << "DES-CBC decryption failed.";
78 return false;
79 }
80 if (data.back() != '\0') {
81 LOG(ERROR) << "DEC-CBC decryption resulted in invalid plain text.";
82 return false;
83 }
84 string text = data.data();
85 if (version == 2) {
86 if (!base::EndsWith(text, kSentinel, base::CompareCase::SENSITIVE)) {
87 LOG(ERROR) << "DES-CBC decrypted text missing sentinel -- bad key?";
88 return false;
89 }
90 text.erase(text.size() - strlen(kSentinel), strlen(kSentinel));
91 }
92 *plaintext = text;
93 return true;
94 }
95
LoadKeyMatter(const FilePath & path)96 bool CryptoDESCBC::LoadKeyMatter(const FilePath& path) {
97 key_.clear();
98 iv_.clear();
99 string matter;
100 // TODO(petkov): This mimics current flimflam behavior. Fix it so that it
101 // doesn't read the whole file.
102 if (!base::ReadFileToString(path, &matter)) {
103 LOG(ERROR) << "Unable to load key matter from " << path.value();
104 return false;
105 }
106 if (matter.size() < 2 * kBlockSize) {
107 LOG(ERROR) << "Key matter data not enough " << matter.size() << " < "
108 << 2 * kBlockSize;
109 return false;
110 }
111 string::const_iterator matter_start =
112 matter.begin() + (matter.size() - 2 * kBlockSize);
113 key_.assign(matter_start + kBlockSize, matter_start + 2 * kBlockSize);
114 iv_.assign(matter_start, matter_start + kBlockSize);
115 return true;
116 }
117
118 } // namespace shill
119