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

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: rebaes 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 stuff is doable (
eroman 2016/08/27 01:53:39 nit on capitalization: ChromeOS ?
mattm 2016/08/29 20:38:17 Done.
19 // TrustStoreChromeOS which uses net::NSSProfileFilterChromeOS.. similar to
eroman 2016/08/27 01:53:38 Not sure I follow this comment. Was it a note-to-s
mattm 2016/08/29 20:38:16 Yeah, I guess it was mostly a note-to-self. I'll r
20 // CertVerifyProcChromeOS )
21 // Could have protected version of FindTrustAnchorsForCert that takes a
22 // cert_filter callback param, which is then passed into GetAnchors, and
23 // subclass of TrustStoreNSS that calls that? Or take the callback in
24 // constructor?
25
26 namespace net {
27
28 namespace {
29
30 // Get all certs in NSS which have a subject matching |der_name| and which are
31 // marked as a trusted CA.
32 void GetAnchors(scoped_refptr<ParsedCertificate> cert,
eroman 2016/08/27 01:53:38 is our Bind() smart enough to avoid copying from s
mattm 2016/08/29 20:38:17 I think it is, but changed to be safe.
33 TrustAnchors* out_anchors) {
34 crypto::EnsureNSSInit();
35
36 SECItem name;
37 name.len = cert->tbs().issuer_tlv.Length();
38 name.data = const_cast<uint8_t*>(cert->tbs().issuer_tlv.UnsafeData());
39 CERTCertList* found_certs = CERT_CreateSubjectCertList(
40 nullptr /* certList */, CERT_GetDefaultCertDB(), &name,
41 PR_Now() /* sorttime */, PR_FALSE /* validOnly */);
eroman 2016/08/27 01:53:38 Is validOnly=false necessary? (i.e. are trust anch
mattm 2016/08/29 20:38:17 validOnly here refers to the time validity checkin
42 if (!found_certs)
43 return;
44
45 for (CERTCertListNode* node = CERT_LIST_HEAD(found_certs);
46 !CERT_LIST_END(node, found_certs); node = CERT_LIST_NEXT(node)) {
47 CERTCertTrust trust;
48 if (CERT_GetCertTrust(node->cert, &trust) != SECSuccess)
49 continue;
50
51 // TODO(mattm): handle explicit distrust (blacklisting)?
52 const int ca_trust = CERTDB_TRUSTED_CA;
53 if ((trust.sslFlags & ca_trust) != ca_trust)
54 continue;
55
56 scoped_refptr<ParsedCertificate> anchor_cert =
57 ParsedCertificate::CreateFromCertificateData(
58 node->cert->derCert.data, node->cert->derCert.len,
59 ParsedCertificate::DataSource::INTERNAL_COPY, {});
60 if (!anchor_cert) {
61 // TODO(mattm): return errors better.
eroman 2016/08/27 01:53:39 In fact we may need to be less strict and just do
mattm 2016/08/29 20:38:17 Acknowledged.
62 LOG(ERROR) << "error parsing issuer certificate";
63 continue;
64 }
65
66 out_anchors->push_back(TrustAnchor::CreateFromCertificateNoConstraints(
eroman 2016/08/27 01:53:38 Does NSS trust store have the notion of attached c
mattm 2016/08/29 20:38:16 I don't think it does. Ryan?
mattm 2016/09/01 00:59:02 I chatted with Ryan about that. The current state
67 std::move(anchor_cert)));
68 }
69 CERT_DestroyCertList(found_certs);
70 }
71
72 class GetAnchorsRequest : public TrustStore::Request {
73 public:
74 explicit GetAnchorsRequest(const TrustStore::TrustAnchorsCallback& callback);
75 // Destruction of the Request cancels it. GetAnchors will still run, but the
76 // callback will not be called since the WeakPtr will be invalidated.
77 ~GetAnchorsRequest() override = default;
78
79 void Start(scoped_refptr<ParsedCertificate> cert,
80 base::TaskRunner* task_runner);
81
82 private:
83 void HandleGetAnchors(std::unique_ptr<TrustAnchors> anchors);
84
85 TrustStore::TrustAnchorsCallback callback_;
86 base::WeakPtrFactory<GetAnchorsRequest> weak_ptr_factory_;
87 };
88
89 GetAnchorsRequest::GetAnchorsRequest(
90 const TrustStore::TrustAnchorsCallback& callback)
91 : callback_(callback), weak_ptr_factory_(this) {}
92
93 void GetAnchorsRequest::Start(scoped_refptr<ParsedCertificate> cert,
94 base::TaskRunner* task_runner) {
95 auto anchors = base::MakeUnique<TrustAnchors>();
96
97 auto* anchors_ptr = anchors.get();
eroman 2016/08/27 01:53:38 heh. gotta love C++'s undefined argument evaluatio
98 task_runner->PostTaskAndReply(
99 FROM_HERE, base::Bind(&GetAnchors, std::move(cert), anchors_ptr),
100 base::Bind(&GetAnchorsRequest::HandleGetAnchors,
101 weak_ptr_factory_.GetWeakPtr(), base::Passed(&anchors)));
102 }
103
104 void GetAnchorsRequest::HandleGetAnchors(
105 std::unique_ptr<TrustAnchors> anchors) {
106 base::ResetAndReturn(&callback_).Run(std::move(*anchors));
107 // |this| may be deleted here.
108 }
109
110 } // namespace
111
112 TrustStoreNSS::TrustStoreNSS(scoped_refptr<base::TaskRunner> nss_task_runner)
113 : nss_task_runner_(std::move(nss_task_runner)) {}
114
115 TrustStoreNSS::~TrustStoreNSS() = default;
116
117 void TrustStoreNSS::FindTrustAnchorsForCert(
118 scoped_refptr<ParsedCertificate> cert,
119 const TrustAnchorsCallback& callback,
120 TrustAnchors* synchronous_matches,
121 std::unique_ptr<Request>* out_req) const {
122 if (callback.is_null())
123 return;
124
125 auto req(base::MakeUnique<GetAnchorsRequest>(callback));
eroman 2016/08/27 01:53:38 style nit: for consistency with higher up, use "au
mattm 2016/08/29 20:38:16 Done.
126 // Use the original issuer value instead of the normalized version. NSS does a
eroman 2016/08/27 01:53:38 How about moving this comment into GetAnchors() si
mattm 2016/08/29 20:38:16 Done.
127 // less extensive normalization in its Name comparisons, so our normalized
128 // version may not match the unnormalized version.
129 req->Start(std::move(cert), nss_task_runner_.get());
130 *out_req = std::move(req);
131 }
132
133 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698