Darin Petkov | 823c47e | 2011-06-27 16:15:35 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 14 | using std::string; |
| 15 | using std::vector; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | const int CryptoDESCBC::kBlockSize = 8; |
| 20 | const char CryptoDESCBC::kID[] = "des-cbc"; |
| 21 | const char CryptoDESCBC::kSentinel[] = "[ok]"; |
| 22 | const char CryptoDESCBC::kVersion2Prefix[] = "02:"; |
| 23 | |
| 24 | CryptoDESCBC::CryptoDESCBC(GLib *glib) : glib_(glib) {} |
| 25 | |
| 26 | string CryptoDESCBC::GetID() { |
| 27 | return kID; |
| 28 | } |
| 29 | |
| 30 | bool CryptoDESCBC::Encrypt(const string &plaintext, string *ciphertext) { |
| 31 | CHECK_EQ(kBlockSize, key_.size()); |
| 32 | CHECK_EQ(kBlockSize, iv_.size()); |
| 33 | |
| 34 | // Prepare the data to be encrypted by concatenating |plaintext| and |
| 35 | // |kSentinel|, and appending nulls to a size multiple of |kBlockSize|. |
| 36 | vector<char> data(plaintext.begin(), plaintext.end()); |
| 37 | data.insert(data.end(), kSentinel, kSentinel + strlen(kSentinel)); |
| 38 | // +1 to encrypt at least one null terminator. +(kBlockSize - 1) to pad to a |
| 39 | // full block, if necessary. |
| 40 | int blocks = (data.size() + 1 + (kBlockSize - 1)) / kBlockSize; |
| 41 | data.resize(blocks * kBlockSize, '\0'); |
| 42 | |
| 43 | // The IV is modified in place. |
| 44 | vector<char> iv = iv_; |
| 45 | int rv = |
| 46 | cbc_crypt(key_.data(), data.data(), data.size(), DES_ENCRYPT, iv.data()); |
| 47 | if (DES_FAILED(rv)) { |
| 48 | LOG(ERROR) << "DES-CBC encryption failed."; |
| 49 | return false; |
| 50 | } |
| 51 | gchar *b64_ciphertext = |
| 52 | glib_->Base64Encode(reinterpret_cast<guchar *>(data.data()), data.size()); |
| 53 | if (!b64_ciphertext) { |
| 54 | LOG(ERROR) << "Unable to base64-encode DES-CBC ciphertext."; |
| 55 | return false; |
| 56 | } |
| 57 | *ciphertext = string(kVersion2Prefix) + b64_ciphertext; |
| 58 | glib_->Free(b64_ciphertext); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool CryptoDESCBC::Decrypt(const string &ciphertext, string *plaintext) { |
| 63 | CHECK_EQ(kBlockSize, key_.size()); |
| 64 | CHECK_EQ(kBlockSize, iv_.size()); |
| 65 | int version = 1; |
| 66 | string b64_ciphertext = ciphertext; |
| 67 | if (StartsWithASCII(ciphertext, kVersion2Prefix, true)) { |
| 68 | version = 2; |
| 69 | b64_ciphertext.erase(0, strlen(kVersion2Prefix)); |
| 70 | } |
| 71 | |
| 72 | gsize data_len = 0; |
| 73 | guchar *gdata = glib_->Base64Decode(b64_ciphertext.c_str(), &data_len); |
| 74 | if (!gdata) { |
| 75 | LOG(ERROR) << "Unable to base64-decode DEC-CBC ciphertext."; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | vector<char> data(gdata, gdata + data_len); |
| 80 | glib_->Free(gdata); |
| 81 | if (data.empty() || (data.size() % kBlockSize != 0)) { |
| 82 | LOG(ERROR) << "Invalid DES-CBC ciphertext size: " << data.size(); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // The IV is modified in place. |
| 87 | vector<char> iv = iv_; |
| 88 | int rv = |
| 89 | cbc_crypt(key_.data(), data.data(), data.size(), DES_DECRYPT, iv.data()); |
| 90 | if (DES_FAILED(rv)) { |
| 91 | LOG(ERROR) << "DES-CBC decryption failed."; |
| 92 | return false; |
| 93 | } |
| 94 | if (data.back() != '\0') { |
| 95 | LOG(ERROR) << "DEC-CBC decryption resulted in invalid plain text."; |
| 96 | return false; |
| 97 | } |
| 98 | string text = data.data(); |
| 99 | if (version == 2) { |
| 100 | if (!EndsWith(text, kSentinel, true)) { |
| 101 | LOG(ERROR) << "DES-CBC decrypted text missing sentinel -- bad key?"; |
| 102 | return false; |
| 103 | } |
| 104 | text.erase(text.size() - strlen(kSentinel), strlen(kSentinel)); |
| 105 | } |
| 106 | *plaintext = text; |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | bool CryptoDESCBC::LoadKeyMatter(const FilePath &path) { |
| 111 | key_.clear(); |
| 112 | iv_.clear(); |
| 113 | string matter; |
| 114 | // TODO(petkov): This mimics current flimflam behavior. Fix it so that it |
| 115 | // doesn't read the whole file. |
| 116 | if (!file_util::ReadFileToString(path, &matter)) { |
| 117 | LOG(ERROR) << "Unable to load key matter from " << path.value(); |
| 118 | return false; |
| 119 | } |
| 120 | if (matter.size() < 2 * kBlockSize) { |
| 121 | LOG(ERROR) << "Key matter data not enough " << matter.size() << " < " |
| 122 | << 2 * kBlockSize; |
| 123 | return false; |
| 124 | } |
| 125 | string::const_iterator matter_start = |
| 126 | matter.begin() + (matter.size() - 2 * kBlockSize); |
| 127 | key_.assign(matter_start + kBlockSize, matter_start + 2 * kBlockSize); |
| 128 | iv_.assign(matter_start, matter_start + kBlockSize); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | } // namespace shill |