1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/cryptohome/cryptohome_library.h"
#include <map>
#include "base/bind.h"
#include "base/chromeos/chromeos_version.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "chromeos/dbus/cryptohome_client.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "crypto/encryptor.h"
#include "crypto/nss_util.h"
#include "crypto/sha2.h"
#include "crypto/symmetric_key.h"
namespace chromeos {
namespace {
const char kStubSystemSalt[] = "stub_system_salt";
const size_t kNonceSize = 16;
// Does nothing. Used as a Cryptohome::VoidMethodCallback.
void DoNothing(DBusMethodCallStatus call_status) {}
} // namespace
// This class handles the interaction with the ChromeOS cryptohome library APIs.
class CryptohomeLibraryImpl : public CryptohomeLibrary {
public:
CryptohomeLibraryImpl() : weak_ptr_factory_(this) {
}
virtual ~CryptohomeLibraryImpl() {
}
virtual bool TpmIsEnabled() OVERRIDE {
bool result = false;
DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsEnabledAndBlock(
&result);
return result;
}
virtual bool TpmIsOwned() OVERRIDE {
bool result = false;
DBusThreadManager::Get()->GetCryptohomeClient()->CallTpmIsOwnedAndBlock(
&result);
return result;
}
virtual bool TpmIsBeingOwned() OVERRIDE {
bool result = false;
DBusThreadManager::Get()->GetCryptohomeClient()->
CallTpmIsBeingOwnedAndBlock(&result);
return result;
}
virtual void TpmCanAttemptOwnership() OVERRIDE {
DBusThreadManager::Get()->GetCryptohomeClient()->TpmCanAttemptOwnership(
base::Bind(&DoNothing));
}
virtual void TpmClearStoredPassword() OVERRIDE {
DBusThreadManager::Get()->GetCryptohomeClient()->
CallTpmClearStoredPasswordAndBlock();
}
virtual bool InstallAttributesGet(
const std::string& name, std::string* value) OVERRIDE {
std::vector<uint8> buf;
bool success = false;
DBusThreadManager::Get()->GetCryptohomeClient()->
InstallAttributesGet(name, &buf, &success);
if (success) {
// Cryptohome returns 'buf' with a terminating '\0' character.
DCHECK(!buf.empty());
DCHECK_EQ(buf.back(), 0);
value->assign(reinterpret_cast<char*>(buf.data()), buf.size() - 1);
}
return success;
}
virtual bool InstallAttributesSet(
const std::string& name, const std::string& value) OVERRIDE {
std::vector<uint8> buf(value.c_str(), value.c_str() + value.size() + 1);
bool success = false;
DBusThreadManager::Get()->GetCryptohomeClient()->
InstallAttributesSet(name, buf, &success);
return success;
}
virtual bool InstallAttributesFinalize() OVERRIDE {
bool success = false;
DBusThreadManager::Get()->GetCryptohomeClient()->
InstallAttributesFinalize(&success);
return success;
}
virtual bool InstallAttributesIsInvalid() OVERRIDE {
bool result = false;
DBusThreadManager::Get()->GetCryptohomeClient()->
InstallAttributesIsInvalid(&result);
return result;
}
virtual bool InstallAttributesIsFirstInstall() OVERRIDE {
bool result = false;
DBusThreadManager::Get()->GetCryptohomeClient()->
InstallAttributesIsFirstInstall(&result);
return result;
}
virtual std::string GetSystemSalt() OVERRIDE {
LoadSystemSalt(); // no-op if it's already loaded.
return StringToLowerASCII(base::HexEncode(
reinterpret_cast<const void*>(system_salt_.data()),
system_salt_.size()));
}
virtual std::string EncryptWithSystemSalt(const std::string& token) OVERRIDE {
// Don't care about token encryption while debugging.
if (!base::chromeos::IsRunningOnChromeOS())
return token;
if (!LoadSystemSaltKey()) {
LOG(WARNING) << "System salt key is not available for encrypt.";
return std::string();
}
return EncryptTokenWithKey(system_salt_key_.get(),
GetSystemSalt(),
token);
}
virtual std::string DecryptWithSystemSalt(
const std::string& encrypted_token_hex) OVERRIDE {
// Don't care about token encryption while debugging.
if (!base::chromeos::IsRunningOnChromeOS())
return encrypted_token_hex;
if (!LoadSystemSaltKey()) {
LOG(WARNING) << "System salt key is not available for decrypt.";
return std::string();
}
return DecryptTokenWithKey(system_salt_key_.get(),
GetSystemSalt(),
encrypted_token_hex);
}
private:
void LoadSystemSalt() {
if (!system_salt_.empty())
return;
DBusThreadManager::Get()->GetCryptohomeClient()->
GetSystemSalt(&system_salt_);
CHECK(!system_salt_.empty());
CHECK_EQ(system_salt_.size() % 2, 0U);
}
// TODO: should this use the system salt for both the password and the salt
// value, or should this use a separate salt value?
bool LoadSystemSaltKey() {
if (!system_salt_key_.get())
system_salt_key_.reset(PassphraseToKey(GetSystemSalt(), GetSystemSalt()));
return system_salt_key_.get();
}
crypto::SymmetricKey* PassphraseToKey(const std::string& passphrase,
const std::string& salt) {
return crypto::SymmetricKey::DeriveKeyFromPassword(
crypto::SymmetricKey::AES, passphrase, salt, 1000, 256);
}
// Encrypts (AES) the token given |key| and |salt|.
std::string EncryptTokenWithKey(crypto::SymmetricKey* key,
const std::string& salt,
const std::string& token) {
crypto::Encryptor encryptor;
if (!encryptor.Init(key, crypto::Encryptor::CTR, std::string())) {
LOG(WARNING) << "Failed to initialize Encryptor.";
return std::string();
}
std::string nonce = salt.substr(0, kNonceSize);
std::string encoded_token;
CHECK(encryptor.SetCounter(nonce));
if (!encryptor.Encrypt(token, &encoded_token)) {
LOG(WARNING) << "Failed to encrypt token.";
return std::string();
}
return StringToLowerASCII(base::HexEncode(
reinterpret_cast<const void*>(encoded_token.data()),
encoded_token.size()));
}
// Decrypts (AES) hex encoded encrypted token given |key| and |salt|.
std::string DecryptTokenWithKey(crypto::SymmetricKey* key,
const std::string& salt,
const std::string& encrypted_token_hex) {
std::vector<uint8> encrypted_token_bytes;
if (!base::HexStringToBytes(encrypted_token_hex, &encrypted_token_bytes)) {
LOG(WARNING) << "Corrupt encrypted token found.";
return std::string();
}
std::string encrypted_token(
reinterpret_cast<char*>(encrypted_token_bytes.data()),
encrypted_token_bytes.size());
crypto::Encryptor encryptor;
if (!encryptor.Init(key, crypto::Encryptor::CTR, std::string())) {
LOG(WARNING) << "Failed to initialize Encryptor.";
return std::string();
}
std::string nonce = salt.substr(0, kNonceSize);
std::string token;
CHECK(encryptor.SetCounter(nonce));
if (!encryptor.Decrypt(encrypted_token, &token)) {
LOG(WARNING) << "Failed to decrypt token.";
return std::string();
}
return token;
}
base::WeakPtrFactory<CryptohomeLibraryImpl> weak_ptr_factory_;
std::vector<uint8> system_salt_;
// A key based on the system salt. Useful for encrypting device-level
// data for which we have no additional credentials.
scoped_ptr<crypto::SymmetricKey> system_salt_key_;
DISALLOW_COPY_AND_ASSIGN(CryptohomeLibraryImpl);
};
class CryptohomeLibraryStubImpl : public CryptohomeLibrary {
public:
CryptohomeLibraryStubImpl()
: locked_(false) {}
virtual ~CryptohomeLibraryStubImpl() {}
virtual bool TpmIsEnabled() OVERRIDE {
return true;
}
virtual bool TpmIsOwned() OVERRIDE {
return true;
}
virtual bool TpmIsBeingOwned() OVERRIDE {
return true;
}
virtual void TpmCanAttemptOwnership() OVERRIDE {}
virtual void TpmClearStoredPassword() OVERRIDE {}
virtual bool InstallAttributesGet(
const std::string& name, std::string* value) OVERRIDE {
if (install_attrs_.find(name) != install_attrs_.end()) {
*value = install_attrs_[name];
return true;
}
return false;
}
virtual bool InstallAttributesSet(
const std::string& name, const std::string& value) OVERRIDE {
install_attrs_[name] = value;
return true;
}
virtual bool InstallAttributesFinalize() OVERRIDE {
locked_ = true;
return true;
}
virtual bool InstallAttributesIsInvalid() OVERRIDE {
return false;
}
virtual bool InstallAttributesIsFirstInstall() OVERRIDE {
return !locked_;
}
virtual std::string GetSystemSalt() OVERRIDE {
return kStubSystemSalt;
}
virtual std::string EncryptWithSystemSalt(const std::string& token) OVERRIDE {
return token;
}
virtual std::string DecryptWithSystemSalt(
const std::string& encrypted_token_hex) OVERRIDE {
return encrypted_token_hex;
}
private:
std::map<std::string, std::string> install_attrs_;
bool locked_;
DISALLOW_COPY_AND_ASSIGN(CryptohomeLibraryStubImpl);
};
CryptohomeLibrary::CryptohomeLibrary() {}
CryptohomeLibrary::~CryptohomeLibrary() {}
static CryptohomeLibrary* g_cryptohome_library = NULL;
static CryptohomeLibrary* g_test_cryptohome_library = NULL;
// static
void CryptohomeLibrary::Initialize() {
CHECK(!g_cryptohome_library);
if (base::chromeos::IsRunningOnChromeOS())
g_cryptohome_library = new CryptohomeLibraryImpl();
else
g_cryptohome_library = new CryptohomeLibraryStubImpl();
}
// static
bool CryptohomeLibrary::IsInitialized() {
return g_cryptohome_library;
}
// static
void CryptohomeLibrary::Shutdown() {
CHECK(g_cryptohome_library);
delete g_cryptohome_library;
g_cryptohome_library = NULL;
}
// static
CryptohomeLibrary* CryptohomeLibrary::Get() {
CHECK(g_cryptohome_library || g_test_cryptohome_library)
<< "CryptohomeLibrary::Get() called before Initialize()";
if (g_test_cryptohome_library)
return g_test_cryptohome_library;
return g_cryptohome_library;
}
// static
void CryptohomeLibrary::SetForTest(CryptohomeLibrary* impl) {
CHECK(!g_test_cryptohome_library || !impl);
g_test_cryptohome_library = impl;
}
// static
CryptohomeLibrary* CryptohomeLibrary::GetTestImpl() {
return new CryptohomeLibraryStubImpl();
}
} // namespace chromeos
|