blob: 6892c72e9b4385d853f633abc86526e2feda1f6c [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
Ben Chan11c213f2014-09-05 08:21:06 -07009#include <base/files/file_util.h>
Ben Chana0ddf462014-02-06 11:32:42 -080010#include <base/strings/string_util.h>
Darin Petkov823c47e2011-06-27 16:15:35 -070011
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
Paul Stewarta794cd62015-06-16 13:13:10 -070025CryptoDESCBC::CryptoDESCBC(GLib* glib) : glib_(glib) {}
Darin Petkov823c47e2011-06-27 16:15:35 -070026
27string CryptoDESCBC::GetID() {
28 return kID;
29}
30
Paul Stewarta794cd62015-06-16 13:13:10 -070031bool 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
Paul Stewarta794cd62015-06-16 13:13:10 -070037bool CryptoDESCBC::Decrypt(const string& ciphertext, string* plaintext) {
Darin Petkov823c47e2011-06-27 16:15:35 -070038 CHECK_EQ(kBlockSize, key_.size());
39 CHECK_EQ(kBlockSize, iv_.size());
40 int version = 1;
41 string b64_ciphertext = ciphertext;
Alex Vakulenkoccab3f92015-06-15 12:53:22 -070042 if (base::StartsWithASCII(ciphertext, kVersion2Prefix, true)) {
Darin Petkov823c47e2011-06-27 16:15:35 -070043 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) {
Alex Vakulenkoccab3f92015-06-15 12:53:22 -070074 if (!base::EndsWith(text, kSentinel, true)) {
Darin Petkov823c47e2011-06-27 16:15:35 -070075 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
Paul Stewarta794cd62015-06-16 13:13:10 -070084bool CryptoDESCBC::LoadKeyMatter(const FilePath& path) {
Darin Petkov823c47e2011-06-27 16:15:35 -070085 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.
Ben Chana0ddf462014-02-06 11:32:42 -080090 if (!base::ReadFileToString(path, &matter)) {
Darin Petkov823c47e2011-06-27 16:15:35 -070091 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