1 // Copyright 2014 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/platform_keys/platform_keys.h"
10 #include "base/bind_helpers.h"
11 #include "base/location.h"
12 #include "base/threading/worker_pool.h"
13 #include "net/base/hash_value.h"
14 #include "net/cert/x509_certificate.h"
18 namespace platform_keys
{
22 void IntersectOnWorkerThread(const net::CertificateList
& certs1
,
23 const net::CertificateList
& certs2
,
24 net::CertificateList
* intersection
) {
25 std::map
<net::SHA256HashValue
, scoped_refptr
<net::X509Certificate
>,
26 net::SHA256HashValueLessThan
> fingerprints2
;
28 // Fill the map with fingerprints of certs from |certs2|.
29 for (const auto& cert2
: certs2
) {
30 fingerprints2
[net::X509Certificate::CalculateFingerprint256(
31 cert2
->os_cert_handle())] = cert2
;
34 // Compare each cert from |certs1| with the entries of the map.
35 for (const auto& cert1
: certs1
) {
36 const net::SHA256HashValue fingerprint1
=
37 net::X509Certificate::CalculateFingerprint256(cert1
->os_cert_handle());
38 const auto it
= fingerprints2
.find(fingerprint1
);
39 if (it
== fingerprints2
.end())
41 const auto& cert2
= it
->second
;
42 DCHECK(cert1
->Equals(cert2
.get()));
43 intersection
->push_back(cert1
);
49 const char kTokenIdUser
[] = "user";
50 const char kTokenIdSystem
[] = "system";
52 ClientCertificateRequest::ClientCertificateRequest() {
55 ClientCertificateRequest::~ClientCertificateRequest() {
58 void IntersectCertificates(
59 const net::CertificateList
& certs1
,
60 const net::CertificateList
& certs2
,
61 const base::Callback
<void(scoped_ptr
<net::CertificateList
>)>& callback
) {
62 scoped_ptr
<net::CertificateList
> intersection(new net::CertificateList
);
63 net::CertificateList
* const intersection_ptr
= intersection
.get();
64 if (!base::WorkerPool::PostTaskAndReply(
65 FROM_HERE
, base::Bind(&IntersectOnWorkerThread
, certs1
, certs2
,
67 base::Bind(callback
, base::Passed(&intersection
)),
68 false /* task_is_slow */)) {
69 callback
.Run(make_scoped_ptr(new net::CertificateList
));
73 } // namespace platform_keys
75 } // namespace chromeos