blob: 8c12a44ef9cb6fa11f6be0123a18e0fa0e09f099 [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
14using std::string;
15using std::vector;
16
17namespace shill {
18
Eric Shienbroodc74cf9c2012-03-02 15:00:35 -050019const unsigned int CryptoDESCBC::kBlockSize = 8;
Darin Petkov823c47e2011-06-27 16:15:35 -070020const char CryptoDESCBC::kID[] = "des-cbc";
21const char CryptoDESCBC::kSentinel[] = "[ok]";
22const char CryptoDESCBC::kVersion2Prefix[] = "02:";
23
24CryptoDESCBC::CryptoDESCBC(GLib *glib) : glib_(glib) {}
25
26string CryptoDESCBC::GetID() {
27 return kID;
28}
29
30bool CryptoDESCBC::Encrypt(const string &plaintext, string *ciphertext) {
Darin Petkov7326cd82012-03-30 11:02:01 +020031 // Never encrypt. We'll fall back to rot47 which doesn't depend on
32 // the owner key which may change due to rotation.
33 return false;
Darin Petkov823c47e2011-06-27 16:15:35 -070034}
35
36bool CryptoDESCBC::Decrypt(const string &ciphertext, string *plaintext) {
37 CHECK_EQ(kBlockSize, key_.size());
38 CHECK_EQ(kBlockSize, iv_.size());
39 int version = 1;
40 string b64_ciphertext = ciphertext;
41 if (StartsWithASCII(ciphertext, kVersion2Prefix, true)) {
42 version = 2;
43 b64_ciphertext.erase(0, strlen(kVersion2Prefix));
44 }
45
46 gsize data_len = 0;
47 guchar *gdata = glib_->Base64Decode(b64_ciphertext.c_str(), &data_len);
48 if (!gdata) {
49 LOG(ERROR) << "Unable to base64-decode DEC-CBC ciphertext.";
50 return false;
51 }
52
53 vector<char> data(gdata, gdata + data_len);
54 glib_->Free(gdata);
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
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