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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/certificate_provider/thread_safe_certificate_map.h"
7 #include "net/base/hash_value.h"
8 #include "net/cert/x509_certificate.h"
11 namespace certificate_provider
{
14 void BuildFingerprintsMap(
15 const std::map
<std::string
, certificate_provider::CertificateInfoList
>&
16 extension_to_certificates
,
17 ThreadSafeCertificateMap::ExtensionToFingerprintsMap
* ext_to_certs_map
) {
18 for (const auto& entry
: extension_to_certificates
) {
19 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
) {
22 const net::SHA256HashValue fingerprint
=
23 net::X509Certificate::CalculateFingerprint256(
24 cert_info
.certificate
->os_cert_handle());
25 fingerprint_to_cert
[fingerprint
] = cert_info
;
32 ThreadSafeCertificateMap::ThreadSafeCertificateMap() {}
34 ThreadSafeCertificateMap::~ThreadSafeCertificateMap() {}
36 void ThreadSafeCertificateMap::Update(
37 const std::map
<std::string
, certificate_provider::CertificateInfoList
>&
38 extension_to_certificates
) {
39 ExtensionToFingerprintsMap new_ext_to_certs_map
;
40 BuildFingerprintsMap(extension_to_certificates
, &new_ext_to_certs_map
);
42 base::AutoLock
auto_lock(lock_
);
43 extension_to_certificates_
.swap(new_ext_to_certs_map
);
46 bool ThreadSafeCertificateMap::LookUpCertificate(
47 const net::X509Certificate
& cert
,
48 CertificateInfo
* info
,
49 std::string
* extension_id
) {
50 const net::SHA256HashValue fingerprint
=
51 net::X509Certificate::CalculateFingerprint256(cert
.os_cert_handle());
53 base::AutoLock
auto_lock(lock_
);
54 for (const auto& entry
: extension_to_certificates_
) {
55 const FingerprintToCertMap
& certs
= entry
.second
;
56 const auto it
= certs
.find(fingerprint
);
57 if (it
!= certs
.end()) {
59 *extension_id
= entry
.first
;
66 void ThreadSafeCertificateMap::RemoveExtension(
67 const std::string
& extension_id
) {
68 base::AutoLock
auto_lock(lock_
);
69 extension_to_certificates_
.erase(extension_id
);
72 } // namespace certificate_provider
73 } // namespace chromeos