OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/certificate_provider/thread_safe_certificate_m
ap.h" | 5 #include "chrome/browser/chromeos/certificate_provider/thread_safe_certificate_m
ap.h" |
6 | 6 |
7 #include "net/base/hash_value.h" | 7 #include "net/base/hash_value.h" |
8 #include "net/cert/x509_certificate.h" | 8 #include "net/cert/x509_certificate.h" |
9 | 9 |
10 namespace chromeos { | 10 namespace chromeos { |
11 namespace certificate_provider { | 11 namespace certificate_provider { |
12 namespace { | 12 namespace { |
13 | 13 |
14 void BuildFingerprintsMap( | 14 void BuildFingerprintsMap( |
15 const std::map<std::string, certificate_provider::CertificateInfoList>& | 15 const std::map<std::string, certificate_provider::CertificateInfoList>& |
16 extension_to_certificates, | 16 extension_to_certificates, |
17 ThreadSafeCertificateMap::ExtensionToFingerprintsMap* ext_to_certs_map) { | 17 ThreadSafeCertificateMap::FingerprintToCertAndExtensionMap* |
| 18 fingerprint_to_cert) { |
18 for (const auto& entry : extension_to_certificates) { | 19 for (const auto& entry : extension_to_certificates) { |
19 const std::string& extension_id = entry.first; | 20 const std::string& extension_id = entry.first; |
20 auto& fingerprint_to_cert = (*ext_to_certs_map)[extension_id]; | |
21 for (const CertificateInfo& cert_info : entry.second) { | 21 for (const CertificateInfo& cert_info : entry.second) { |
22 const net::SHA256HashValue fingerprint = | 22 const net::SHA256HashValue fingerprint = |
23 net::X509Certificate::CalculateFingerprint256( | 23 net::X509Certificate::CalculateFingerprint256( |
24 cert_info.certificate->os_cert_handle()); | 24 cert_info.certificate->os_cert_handle()); |
25 fingerprint_to_cert[fingerprint] = cert_info; | 25 fingerprint_to_cert->insert( |
| 26 fingerprint, make_scoped_ptr(new ThreadSafeCertificateMap::MapValue( |
| 27 cert_info, extension_id))); |
26 } | 28 } |
27 } | 29 } |
28 } | 30 } |
29 | 31 |
30 } // namespace | 32 } // namespace |
31 | 33 |
| 34 ThreadSafeCertificateMap::MapValue::MapValue(const CertificateInfo& cert_info, |
| 35 const std::string& extension_id) |
| 36 : cert_info(cert_info), extension_id(extension_id) {} |
| 37 |
| 38 ThreadSafeCertificateMap::MapValue::~MapValue() {} |
| 39 |
32 ThreadSafeCertificateMap::ThreadSafeCertificateMap() {} | 40 ThreadSafeCertificateMap::ThreadSafeCertificateMap() {} |
33 | 41 |
34 ThreadSafeCertificateMap::~ThreadSafeCertificateMap() {} | 42 ThreadSafeCertificateMap::~ThreadSafeCertificateMap() {} |
35 | 43 |
36 void ThreadSafeCertificateMap::Update( | 44 void ThreadSafeCertificateMap::Update( |
37 const std::map<std::string, certificate_provider::CertificateInfoList>& | 45 const std::map<std::string, certificate_provider::CertificateInfoList>& |
38 extension_to_certificates) { | 46 extension_to_certificates) { |
39 ExtensionToFingerprintsMap new_ext_to_certs_map; | 47 FingerprintToCertAndExtensionMap new_fingerprint_map; |
40 BuildFingerprintsMap(extension_to_certificates, &new_ext_to_certs_map); | 48 BuildFingerprintsMap(extension_to_certificates, &new_fingerprint_map); |
41 | 49 |
42 base::AutoLock auto_lock(lock_); | 50 base::AutoLock auto_lock(lock_); |
43 extension_to_certificates_.swap(new_ext_to_certs_map); | 51 // Keep all old fingerprints from |fingerprint_to_cert_and_extension_| but |
| 52 // remove the association to any extension. |
| 53 for (const auto& entry : fingerprint_to_cert_and_extension_) { |
| 54 const net::SHA256HashValue& fingerprint = entry.first; |
| 55 // This doesn't modify the map if it already contains the key |fingerprint|. |
| 56 new_fingerprint_map.insert(fingerprint, nullptr); |
| 57 } |
| 58 fingerprint_to_cert_and_extension_.swap(new_fingerprint_map); |
44 } | 59 } |
45 | 60 |
46 bool ThreadSafeCertificateMap::LookUpCertificate( | 61 bool ThreadSafeCertificateMap::LookUpCertificate( |
47 const net::X509Certificate& cert, | 62 const net::X509Certificate& cert, |
| 63 bool* is_currently_provided, |
48 CertificateInfo* info, | 64 CertificateInfo* info, |
49 std::string* extension_id) { | 65 std::string* extension_id) { |
| 66 *is_currently_provided = false; |
50 const net::SHA256HashValue fingerprint = | 67 const net::SHA256HashValue fingerprint = |
51 net::X509Certificate::CalculateFingerprint256(cert.os_cert_handle()); | 68 net::X509Certificate::CalculateFingerprint256(cert.os_cert_handle()); |
52 | 69 |
53 base::AutoLock auto_lock(lock_); | 70 base::AutoLock auto_lock(lock_); |
54 for (const auto& entry : extension_to_certificates_) { | 71 const auto it = fingerprint_to_cert_and_extension_.find(fingerprint); |
55 const FingerprintToCertMap& certs = entry.second; | 72 if (it == fingerprint_to_cert_and_extension_.end()) |
56 const auto it = certs.find(fingerprint); | 73 return false; |
57 if (it != certs.end()) { | 74 |
58 *info = it->second; | 75 MapValue* const value = it->second; |
59 *extension_id = entry.first; | 76 if (value) { |
60 return true; | 77 *is_currently_provided = true; |
61 } | 78 *info = value->cert_info; |
| 79 *extension_id = value->extension_id; |
62 } | 80 } |
63 return false; | 81 return true; |
64 } | 82 } |
65 | 83 |
66 void ThreadSafeCertificateMap::RemoveExtension( | 84 void ThreadSafeCertificateMap::RemoveExtension( |
67 const std::string& extension_id) { | 85 const std::string& extension_id) { |
68 base::AutoLock auto_lock(lock_); | 86 base::AutoLock auto_lock(lock_); |
69 extension_to_certificates_.erase(extension_id); | 87 for (auto& entry : fingerprint_to_cert_and_extension_) { |
| 88 MapValue* const value = entry.second; |
| 89 // Only remove the association of the fingerprint to the extension, but keep |
| 90 // the fingerprint. |
| 91 if (value && value->extension_id == extension_id) |
| 92 fingerprint_to_cert_and_extension_.set(entry.first, nullptr); |
| 93 } |
70 } | 94 } |
71 | 95 |
72 } // namespace certificate_provider | 96 } // namespace certificate_provider |
73 } // namespace chromeos | 97 } // namespace chromeos |
OLD | NEW |