Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1374)

Side by Side Diff: net/cert/internal/trust_store_nss.cc

Issue 2272493002: Add TrustStoreNSS and TrustStoreCollection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cert-trust-store-interface3-nss
Patch Set: rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/cert/internal/trust_store_nss.h ('k') | net/cert/internal/trust_store_nss_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "net/cert/internal/trust_store_nss.h"
6
7 #include <cert.h>
8 #include <certdb.h>
9
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/task_runner.h"
15 #include "crypto/nss_util.h"
16 #include "net/cert/internal/parsed_certificate.h"
17
18 // TODO(mattm): structure so that supporting ChromeOS multi-profile stuff is
19 // doable (Have a TrustStoreChromeOS which uses net::NSSProfileFilterChromeOS,
20 // similar to CertVerifyProcChromeOS.)
21
22 namespace net {
23
24 namespace {
25
26 // Get all certs in NSS which have a subject matching |der_name| and which are
27 // marked as a trusted CA.
28 void GetAnchors(const scoped_refptr<ParsedCertificate>& cert,
29 SECTrustType trust_type,
30 TrustAnchors* out_anchors) {
31 crypto::EnsureNSSInit();
32
33 SECItem name;
34 // Use the original issuer value instead of the normalized version. NSS does a
35 // less extensive normalization in its Name comparisons, so our normalized
36 // version may not match the unnormalized version.
37 name.len = cert->tbs().issuer_tlv.Length();
38 name.data = const_cast<uint8_t*>(cert->tbs().issuer_tlv.UnsafeData());
39 // |validOnly| in CERT_CreateSubjectCertList controls whether to return only
40 // certs that are valid at |sorttime|. Expiration isn't meaningful for trust
41 // anchors, so request all the matches.
42 CERTCertList* found_certs = CERT_CreateSubjectCertList(
43 nullptr /* certList */, CERT_GetDefaultCertDB(), &name,
44 PR_Now() /* sorttime */, PR_FALSE /* validOnly */);
45 if (!found_certs)
46 return;
47
48 for (CERTCertListNode* node = CERT_LIST_HEAD(found_certs);
49 !CERT_LIST_END(node, found_certs); node = CERT_LIST_NEXT(node)) {
50 CERTCertTrust trust;
51 if (CERT_GetCertTrust(node->cert, &trust) != SECSuccess)
52 continue;
53
54 // TODO(mattm): handle explicit distrust (blacklisting)?
55 const int ca_trust = CERTDB_TRUSTED_CA;
56 if ((SEC_GET_TRUST_FLAGS(&trust, trust_type) & ca_trust) != ca_trust)
57 continue;
58
59 scoped_refptr<ParsedCertificate> anchor_cert =
60 ParsedCertificate::CreateFromCertificateData(
61 node->cert->derCert.data, node->cert->derCert.len,
62 ParsedCertificate::DataSource::INTERNAL_COPY, {});
63 if (!anchor_cert) {
64 // TODO(mattm): return errors better.
65 LOG(ERROR) << "error parsing issuer certificate";
66 continue;
67 }
68
69 out_anchors->push_back(TrustAnchor::CreateFromCertificateNoConstraints(
70 std::move(anchor_cert)));
71 }
72 CERT_DestroyCertList(found_certs);
73 }
74
75 class GetAnchorsRequest : public TrustStore::Request {
76 public:
77 explicit GetAnchorsRequest(const TrustStore::TrustAnchorsCallback& callback);
78 // Destruction of the Request cancels it. GetAnchors will still run, but the
79 // callback will not be called since the WeakPtr will be invalidated.
80 ~GetAnchorsRequest() override = default;
81
82 void Start(const scoped_refptr<ParsedCertificate>& cert,
83 SECTrustType trust_type,
84 base::TaskRunner* task_runner);
85
86 private:
87 void HandleGetAnchors(std::unique_ptr<TrustAnchors> anchors);
88
89 TrustStore::TrustAnchorsCallback callback_;
90 base::WeakPtrFactory<GetAnchorsRequest> weak_ptr_factory_;
91 };
92
93 GetAnchorsRequest::GetAnchorsRequest(
94 const TrustStore::TrustAnchorsCallback& callback)
95 : callback_(callback), weak_ptr_factory_(this) {}
96
97 void GetAnchorsRequest::Start(const scoped_refptr<ParsedCertificate>& cert,
98 SECTrustType trust_type,
99 base::TaskRunner* task_runner) {
100 auto anchors = base::MakeUnique<TrustAnchors>();
101
102 auto* anchors_ptr = anchors.get();
103 task_runner->PostTaskAndReply(
104 FROM_HERE, base::Bind(&GetAnchors, cert, trust_type, anchors_ptr),
105 base::Bind(&GetAnchorsRequest::HandleGetAnchors,
106 weak_ptr_factory_.GetWeakPtr(), base::Passed(&anchors)));
107 }
108
109 void GetAnchorsRequest::HandleGetAnchors(
110 std::unique_ptr<TrustAnchors> anchors) {
111 base::ResetAndReturn(&callback_).Run(std::move(*anchors));
112 // |this| may be deleted here.
113 }
114
115 } // namespace
116
117 TrustStoreNSS::TrustStoreNSS(SECTrustType trust_type,
118 scoped_refptr<base::TaskRunner> nss_task_runner)
119 : trust_type_(trust_type), nss_task_runner_(std::move(nss_task_runner)) {}
120
121 TrustStoreNSS::~TrustStoreNSS() = default;
122
123 void TrustStoreNSS::FindTrustAnchorsForCert(
124 const scoped_refptr<ParsedCertificate>& cert,
125 const TrustAnchorsCallback& callback,
126 TrustAnchors* synchronous_matches,
127 std::unique_ptr<Request>* out_req) const {
128 if (callback.is_null())
129 return;
130
131 auto req = base::MakeUnique<GetAnchorsRequest>(callback);
132 req->Start(cert, trust_type_, nss_task_runner_.get());
133 *out_req = std::move(req);
134 }
135
136 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/internal/trust_store_nss.h ('k') | net/cert/internal/trust_store_nss_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698