OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/cros/cert_library.h" | 5 #include "chrome/browser/chromeos/cros/cert_library.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/observer_list_threadsafe.h" | 9 #include "base/observer_list_threadsafe.h" |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/browser_process.h" // g_browser_process | 13 #include "chrome/browser/browser_process.h" // g_browser_process |
| 14 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 15 #include "chrome/browser/chromeos/cros/cryptohome_library.h" |
12 #include "chrome/browser/chromeos/login/user_manager.h" | 16 #include "chrome/browser/chromeos/login/user_manager.h" |
13 #include "chrome/common/net/x509_certificate_model.h" | 17 #include "chrome/common/net/x509_certificate_model.h" |
14 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "crypto/encryptor.h" |
15 #include "crypto/nss_util.h" | 20 #include "crypto/nss_util.h" |
| 21 #include "crypto/sha2.h" |
| 22 #include "crypto/symmetric_key.h" |
16 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
17 #include "net/base/cert_database.h" | 24 #include "net/base/cert_database.h" |
18 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
19 #include "ui/base/l10n/l10n_util_collator.h" | 26 #include "ui/base/l10n/l10n_util_collator.h" |
20 #include "unicode/coll.h" // icu::Collator | 27 #include "unicode/coll.h" // icu::Collator |
21 | 28 |
22 using content::BrowserThread; | 29 using content::BrowserThread; |
23 | 30 |
24 ////////////////////////////////////////////////////////////////////////////// | 31 ////////////////////////////////////////////////////////////////////////////// |
25 | 32 |
26 namespace { | 33 namespace { |
27 | 34 |
28 // Root CA certificates that are built into Chrome use this token name. | 35 // Root CA certificates that are built into Chrome use this token name. |
29 const char kRootCertificateTokenName[] = "Builtin Object Token"; | 36 const char kRootCertificateTokenName[] = "Builtin Object Token"; |
30 | 37 |
31 // Delay between certificate requests while waiting for TPM/PKCS#11 init. | 38 // Delay between certificate requests while waiting for TPM/PKCS#11 init. |
32 const int kRequestDelayMs = 500; | 39 const int kRequestDelayMs = 500; |
33 | 40 |
| 41 const size_t kKeySize = 16; |
| 42 |
| 43 // Decrypts (AES) hex encoded encrypted token given |key| and |salt|. |
| 44 std::string DecryptTokenWithKey( |
| 45 crypto::SymmetricKey* key, |
| 46 const std::string& salt, |
| 47 const std::string& encrypted_token_hex) { |
| 48 std::vector<uint8> encrypted_token_bytes; |
| 49 if (!base::HexStringToBytes(encrypted_token_hex, &encrypted_token_bytes)) |
| 50 return std::string(); |
| 51 |
| 52 std::string encrypted_token( |
| 53 reinterpret_cast<char*>(encrypted_token_bytes.data()), |
| 54 encrypted_token_bytes.size()); |
| 55 crypto::Encryptor encryptor; |
| 56 if (!encryptor.Init(key, crypto::Encryptor::CTR, std::string())) |
| 57 return std::string(); |
| 58 |
| 59 std::string nonce = salt.substr(0, kKeySize); |
| 60 std::string token; |
| 61 CHECK(encryptor.SetCounter(nonce)); |
| 62 if (!encryptor.Decrypt(encrypted_token, &token)) |
| 63 return std::string(); |
| 64 return token; |
| 65 } |
| 66 |
34 string16 GetDisplayString(net::X509Certificate* cert, bool hardware_backed) { | 67 string16 GetDisplayString(net::X509Certificate* cert, bool hardware_backed) { |
35 std::string org; | 68 std::string org; |
36 if (!cert->subject().organization_names.empty()) | 69 if (!cert->subject().organization_names.empty()) |
37 org = cert->subject().organization_names[0]; | 70 org = cert->subject().organization_names[0]; |
38 if (org.empty()) | 71 if (org.empty()) |
39 org = cert->subject().GetDisplayName(); | 72 org = cert->subject().GetDisplayName(); |
40 string16 issued_by = UTF8ToUTF16( | 73 string16 issued_by = UTF8ToUTF16( |
41 x509_certificate_model::GetIssuerCommonName(cert->os_cert_handle(), | 74 x509_certificate_model::GetIssuerCommonName(cert->os_cert_handle(), |
42 org)); // alternative text | 75 org)); // alternative text |
43 string16 issued_to = UTF8ToUTF16( | 76 string16 issued_to = UTF8ToUTF16( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 126 |
94 // CertLibrary implementation. | 127 // CertLibrary implementation. |
95 virtual void RequestCertificates() OVERRIDE { | 128 virtual void RequestCertificates() OVERRIDE { |
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
97 | 130 |
98 if (!UserManager::Get()->user_is_logged_in()) { | 131 if (!UserManager::Get()->user_is_logged_in()) { |
99 // If we are not logged in, we cannot load any certificates. | 132 // If we are not logged in, we cannot load any certificates. |
100 // Set 'loaded' to true for the UI, since we are not waiting on loading. | 133 // Set 'loaded' to true for the UI, since we are not waiting on loading. |
101 LOG(WARNING) << "Requesting certificates before login."; | 134 LOG(WARNING) << "Requesting certificates before login."; |
102 certificates_loaded_ = true; | 135 certificates_loaded_ = true; |
| 136 supplemental_user_key_.reset(NULL); |
103 return; | 137 return; |
104 } | 138 } |
105 | 139 |
106 if (!user_logged_in_) { | 140 if (!user_logged_in_) { |
107 user_logged_in_ = true; | 141 user_logged_in_ = true; |
108 certificates_loaded_ = false; | 142 certificates_loaded_ = false; |
| 143 supplemental_user_key_.reset(NULL); |
109 } | 144 } |
110 | 145 |
111 VLOG(1) << "Requesting Certificates."; | 146 VLOG(1) << "Requesting Certificates."; |
112 | 147 |
113 // Need TPM token name to filter user certificates. | 148 // Need TPM token name to filter user certificates. |
114 // TODO(stevenjb): crypto::EnsureTPMTokenReady() may block if init has | 149 // TODO(stevenjb): crypto::EnsureTPMTokenReady() may block if init has |
115 // not succeeded. It is not clear whether or not TPM / PKCS#11 init can | 150 // not succeeded. It is not clear whether or not TPM / PKCS#11 init can |
116 // be done safely on a non blocking thread. Blocking time is low. | 151 // be done safely on a non blocking thread. Blocking time is low. |
117 if (crypto::EnsureTPMTokenReady()) { | 152 if (crypto::EnsureTPMTokenReady()) { |
118 std::string unused_pin; | 153 std::string unused_pin; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 virtual const CertList& GetServerCertificates() const OVERRIDE { | 207 virtual const CertList& GetServerCertificates() const OVERRIDE { |
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
174 return server_certs_; | 209 return server_certs_; |
175 } | 210 } |
176 | 211 |
177 virtual const CertList& GetCACertificates() const OVERRIDE { | 212 virtual const CertList& GetCACertificates() const OVERRIDE { |
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
179 return server_ca_certs_; | 214 return server_ca_certs_; |
180 } | 215 } |
181 | 216 |
182 virtual crypto::SymmetricKey* GetSupplementalUserKey() const { | 217 virtual std::string EncryptToken(const std::string& token) OVERRIDE { |
183 return crypto::GetSupplementalUserKey(); | 218 if (!LoadSupplementalUserKey()) |
| 219 return std::string(); |
| 220 crypto::Encryptor encryptor; |
| 221 if (!encryptor.Init(supplemental_user_key_.get(), crypto::Encryptor::CTR, |
| 222 std::string())) |
| 223 return std::string(); |
| 224 std::string salt = |
| 225 CrosLibrary::Get()->GetCryptohomeLibrary()->GetSystemSalt(); |
| 226 std::string nonce = salt.substr(0, kKeySize); |
| 227 std::string encoded_token; |
| 228 CHECK(encryptor.SetCounter(nonce)); |
| 229 if (!encryptor.Encrypt(token, &encoded_token)) |
| 230 return std::string(); |
| 231 |
| 232 return StringToLowerASCII(base::HexEncode( |
| 233 reinterpret_cast<const void*>(encoded_token.data()), |
| 234 encoded_token.size())); |
| 235 } |
| 236 |
| 237 virtual std::string DecryptToken( |
| 238 const std::string& encrypted_token_hex) OVERRIDE { |
| 239 if (!LoadSupplementalUserKey()) |
| 240 return std::string(); |
| 241 return DecryptTokenWithKey(supplemental_user_key_.get(), |
| 242 CrosLibrary::Get()->GetCryptohomeLibrary()->GetSystemSalt(), |
| 243 encrypted_token_hex); |
184 } | 244 } |
185 | 245 |
186 // net::CertDatabase::Observer implementation. Observer added on UI thread. | 246 // net::CertDatabase::Observer implementation. Observer added on UI thread. |
187 virtual void OnCertTrustChanged(const net::X509Certificate* cert) OVERRIDE { | 247 virtual void OnCertTrustChanged(const net::X509Certificate* cert) OVERRIDE { |
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
189 } | 249 } |
190 | 250 |
191 virtual void OnUserCertAdded(const net::X509Certificate* cert) OVERRIDE { | 251 virtual void OnUserCertAdded(const net::X509Certificate* cert) OVERRIDE { |
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
193 VLOG(1) << "Certificate Added."; | 253 VLOG(1) << "Certificate Added."; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 | 372 |
313 // Set loaded state and notify observers. | 373 // Set loaded state and notify observers. |
314 if (!certificates_loaded_) { | 374 if (!certificates_loaded_) { |
315 certificates_loaded_ = true; | 375 certificates_loaded_ = true; |
316 NotifyCertificatesLoaded(true); | 376 NotifyCertificatesLoaded(true); |
317 } else { | 377 } else { |
318 NotifyCertificatesLoaded(false); | 378 NotifyCertificatesLoaded(false); |
319 } | 379 } |
320 } | 380 } |
321 | 381 |
| 382 bool LoadSupplementalUserKey() { |
| 383 if (!user_logged_in_) { |
| 384 // If we are not logged in, we cannot load any certificates. |
| 385 // Set 'loaded' to true for the UI, since we are not waiting on loading. |
| 386 LOG(WARNING) << "Requesting supplemental use key before login."; |
| 387 return false; |
| 388 } |
| 389 if (!supplemental_user_key_.get()) { |
| 390 supplemental_user_key_.reset(crypto::GetSupplementalUserKey()); |
| 391 } |
| 392 return supplemental_user_key_.get() != NULL; |
| 393 } |
| 394 |
322 // Observers. | 395 // Observers. |
323 const scoped_refptr<CertLibraryObserverList> observer_list_; | 396 const scoped_refptr<CertLibraryObserverList> observer_list_; |
324 | 397 |
325 // Active request task for re-requests while waiting for TPM init. | 398 // Active request task for re-requests while waiting for TPM init. |
326 base::Closure request_task_; | 399 base::Closure request_task_; |
327 | 400 |
328 // Cached TPM token name. | 401 // Cached TPM token name. |
329 std::string tpm_token_name_; | 402 std::string tpm_token_name_; |
330 | 403 |
| 404 // Supplemental user key. |
| 405 scoped_ptr<crypto::SymmetricKey> supplemental_user_key_; |
| 406 |
331 // Local state. | 407 // Local state. |
332 bool user_logged_in_; | 408 bool user_logged_in_; |
333 bool certificates_loaded_; | 409 bool certificates_loaded_; |
334 | 410 |
335 // Certificates. | 411 // Certificates. |
336 CertList certs_; | 412 CertList certs_; |
337 CertList user_certs_; | 413 CertList user_certs_; |
338 CertList server_certs_; | 414 CertList server_certs_; |
339 CertList server_ca_certs_; | 415 CertList server_ca_certs_; |
340 | 416 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 net::X509Certificate* cert = GetCertificateAt(index); | 479 net::X509Certificate* cert = GetCertificateAt(index); |
404 net::X509Certificate::OSCertHandle cert_handle = cert->os_cert_handle(); | 480 net::X509Certificate::OSCertHandle cert_handle = cert->os_cert_handle(); |
405 std::string id = x509_certificate_model::GetPkcs11Id(cert_handle); | 481 std::string id = x509_certificate_model::GetPkcs11Id(cert_handle); |
406 if (id == pkcs11_id) | 482 if (id == pkcs11_id) |
407 return index; | 483 return index; |
408 } | 484 } |
409 return -1; // Not found. | 485 return -1; // Not found. |
410 } | 486 } |
411 | 487 |
412 } // chromeos | 488 } // chromeos |
OLD | NEW |