Darin Petkov | 7326cd8 | 2012-03-30 11:02:01 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 2 | // 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 Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 14 | using base::FilePath; |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 15 | using std::string; |
| 16 | using std::vector; |
| 17 | |
| 18 | namespace shill { |
| 19 | |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 20 | const unsigned int CryptoDESCBC::kBlockSize = 8; |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 21 | const char CryptoDESCBC::kID[] = "des-cbc"; |
| 22 | const char CryptoDESCBC::kSentinel[] = "[ok]"; |
| 23 | const char CryptoDESCBC::kVersion2Prefix[] = "02:"; |
| 24 | |
| 25 | CryptoDESCBC::CryptoDESCBC(GLib *glib) : glib_(glib) {} |
| 26 | |
| 27 | string CryptoDESCBC::GetID() { |
| 28 | return kID; |
| 29 | } |
| 30 | |
| 31 | bool CryptoDESCBC::Encrypt(const string &plaintext, string *ciphertext) { |
Darin Petkov | 7326cd8 | 2012-03-30 11:02:01 +0200 | [diff] [blame] | 32 | // 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 Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool 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 Wiley | 0f3eab3 | 2013-03-21 11:55:41 -0700 | [diff] [blame] | 47 | string decoded_data; |
| 48 | if (!glib_->B64Decode(b64_ciphertext, &decoded_data)) { |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 49 | LOG(ERROR) << "Unable to base64-decode DEC-CBC ciphertext."; |
| 50 | return false; |
| 51 | } |
| 52 | |
Christopher Wiley | 0f3eab3 | 2013-03-21 11:55:41 -0700 | [diff] [blame] | 53 | vector<char> data(decoded_data.c_str(), |
| 54 | decoded_data.c_str() + decoded_data.length()); |
Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 55 | 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 | |
| 84 | bool 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 |