summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/platform_keys/platform_keys.cc
blob: 40cb72ccc6f3de8c4f1afb7f44aa791a1e140951 (plain)
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
// Copyright 2014 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 "chrome/browser/chromeos/platform_keys/platform_keys.h"

#include <map>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/threading/worker_pool.h"
#include "net/base/hash_value.h"
#include "net/cert/x509_certificate.h"

namespace chromeos {

namespace platform_keys {

namespace {

void IntersectOnWorkerThread(const net::CertificateList& certs1,
                             const net::CertificateList& certs2,
                             net::CertificateList* intersection) {
  std::map<net::SHA256HashValue, scoped_refptr<net::X509Certificate>,
           net::SHA256HashValueLessThan> fingerprints2;

  // Fill the map with fingerprints of certs from |certs2|.
  for (const auto& cert2 : certs2) {
    fingerprints2[net::X509Certificate::CalculateFingerprint256(
        cert2->os_cert_handle())] = cert2;
  }

  // Compare each cert from |certs1| with the entries of the map.
  for (const auto& cert1 : certs1) {
    const net::SHA256HashValue fingerprint1 =
        net::X509Certificate::CalculateFingerprint256(cert1->os_cert_handle());
    const auto it = fingerprints2.find(fingerprint1);
    if (it == fingerprints2.end())
      continue;
    const auto& cert2 = it->second;
    DCHECK(cert1->Equals(cert2.get()));
    intersection->push_back(cert1);
  }
}

}  // namespace

const char kTokenIdUser[] = "user";
const char kTokenIdSystem[] = "system";

ClientCertificateRequest::ClientCertificateRequest() {
}

ClientCertificateRequest::~ClientCertificateRequest() {
}

void IntersectCertificates(
    const net::CertificateList& certs1,
    const net::CertificateList& certs2,
    const base::Callback<void(scoped_ptr<net::CertificateList>)>& callback) {
  scoped_ptr<net::CertificateList> intersection(new net::CertificateList);
  net::CertificateList* const intersection_ptr = intersection.get();
  if (!base::WorkerPool::PostTaskAndReply(
          FROM_HERE, base::Bind(&IntersectOnWorkerThread, certs1, certs2,
                                intersection_ptr),
          base::Bind(callback, base::Passed(&intersection)),
          false /* task_is_slow */)) {
    callback.Run(make_scoped_ptr(new net::CertificateList));
  }
}

}  // namespace platform_keys

}  // namespace chromeos