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

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: review changes 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
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 TrustAnchors* out_anchors) {
30 crypto::EnsureNSSInit();
31
32 SECItem name;
33 // Use the original issuer value instead of the normalized version. NSS does a
34 // less extensive normalization in its Name comparisons, so our normalized
35 // version may not match the unnormalized version.
36 name.len = cert->tbs().issuer_tlv.Length();
37 name.data = const_cast<uint8_t*>(cert->tbs().issuer_tlv.UnsafeData());
38 // |validOnly| in CERT_CreateSubjectCertList controls whether to return only
39 // certs that are valid at |sorttime|. Expiration isn't meaningful for trust
40 // anchors, so request all the matches.
41 CERTCertList* found_certs = CERT_CreateSubjectCertList(
42 nullptr /* certList */, CERT_GetDefaultCertDB(), &name,
43 PR_Now() /* sorttime */, PR_FALSE /* validOnly */);
44 if (!found_certs)
45 return;
46
47 for (CERTCertListNode* node = CERT_LIST_HEAD(found_certs);
48 !CERT_LIST_END(node, found_certs); node = CERT_LIST_NEXT(node)) {
49 CERTCertTrust trust;
50 if (CERT_GetCertTrust(node->cert, &trust) != SECSuccess)
51 continue;
52
53 // TODO(mattm): handle explicit distrust (blacklisting)?
54 const int ca_trust = CERTDB_TRUSTED_CA;
55 if ((trust.sslFlags & ca_trust) != ca_trust)
Ryan Sleevi 2016/08/31 20:50:39 int flags = SEC_GET_TRUST_FLAG(&trust, trustSSL) i
mattm 2016/09/01 00:59:02 done.
56 continue;
57
58 scoped_refptr<ParsedCertificate> anchor_cert =
59 ParsedCertificate::CreateFromCertificateData(
60 node->cert->derCert.data, node->cert->derCert.len,
61 ParsedCertificate::DataSource::INTERNAL_COPY, {});
62 if (!anchor_cert) {
63 // TODO(mattm): return errors better.
64 LOG(ERROR) << "error parsing issuer certificate";
65 continue;
66 }
67
68 out_anchors->push_back(TrustAnchor::CreateFromCertificateNoConstraints(
69 std::move(anchor_cert)));
70 }
71 CERT_DestroyCertList(found_certs);
72 }
73
74 class GetAnchorsRequest : public TrustStore::Request {
75 public:
76 explicit GetAnchorsRequest(const TrustStore::TrustAnchorsCallback& callback);
77 // Destruction of the Request cancels it. GetAnchors will still run, but the
78 // callback will not be called since the WeakPtr will be invalidated.
79 ~GetAnchorsRequest() override = default;
80
81 void Start(const scoped_refptr<ParsedCertificate>& cert,
82 base::TaskRunner* task_runner);
83
84 private:
85 void HandleGetAnchors(std::unique_ptr<TrustAnchors> anchors);
86
87 TrustStore::TrustAnchorsCallback callback_;
88 base::WeakPtrFactory<GetAnchorsRequest> weak_ptr_factory_;
89 };
90
91 GetAnchorsRequest::GetAnchorsRequest(
92 const TrustStore::TrustAnchorsCallback& callback)
93 : callback_(callback), weak_ptr_factory_(this) {}
94
95 void GetAnchorsRequest::Start(const scoped_refptr<ParsedCertificate>& cert,
96 base::TaskRunner* task_runner) {
97 auto anchors = base::MakeUnique<TrustAnchors>();
98
99 auto* anchors_ptr = anchors.get();
100 task_runner->PostTaskAndReply(
101 FROM_HERE, base::Bind(&GetAnchors, cert, anchors_ptr),
102 base::Bind(&GetAnchorsRequest::HandleGetAnchors,
103 weak_ptr_factory_.GetWeakPtr(), base::Passed(&anchors)));
104 }
105
106 void GetAnchorsRequest::HandleGetAnchors(
107 std::unique_ptr<TrustAnchors> anchors) {
108 base::ResetAndReturn(&callback_).Run(std::move(*anchors));
109 // |this| may be deleted here.
110 }
111
112 } // namespace
113
114 TrustStoreNSS::TrustStoreNSS(scoped_refptr<base::TaskRunner> nss_task_runner)
115 : nss_task_runner_(std::move(nss_task_runner)) {}
116
117 TrustStoreNSS::~TrustStoreNSS() = default;
118
119 void TrustStoreNSS::FindTrustAnchorsForCert(
120 const scoped_refptr<ParsedCertificate>& cert,
121 const TrustAnchorsCallback& callback,
122 TrustAnchors* synchronous_matches,
123 std::unique_ptr<Request>* out_req) const {
124 if (callback.is_null())
125 return;
126
127 auto req = base::MakeUnique<GetAnchorsRequest>(callback);
128 req->Start(cert, nss_task_runner_.get());
129 *out_req = std::move(req);
130 }
131
132 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698