OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cert/internal/trust_store_nss.h" | 5 #include "net/cert/internal/trust_store_nss.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <certdb.h> | 8 #include <certdb.h> |
9 | 9 |
10 #include "base/bind.h" | |
11 #include "base/callback_helpers.h" | |
12 #include "base/memory/ptr_util.h" | 10 #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" | 11 #include "crypto/nss_util.h" |
16 #include "net/cert/internal/cert_errors.h" | 12 #include "net/cert/internal/cert_errors.h" |
17 #include "net/cert/internal/parsed_certificate.h" | 13 #include "net/cert/internal/parsed_certificate.h" |
18 | 14 |
19 // TODO(mattm): structure so that supporting ChromeOS multi-profile stuff is | 15 // TODO(mattm): structure so that supporting ChromeOS multi-profile stuff is |
20 // doable (Have a TrustStoreChromeOS which uses net::NSSProfileFilterChromeOS, | 16 // doable (Have a TrustStoreChromeOS which uses net::NSSProfileFilterChromeOS, |
21 // similar to CertVerifyProcChromeOS.) | 17 // similar to CertVerifyProcChromeOS.) |
22 | 18 |
23 namespace net { | 19 namespace net { |
24 | 20 |
25 namespace { | 21 TrustStoreNSS::TrustStoreNSS(SECTrustType trust_type) |
| 22 : trust_type_(trust_type) {} |
26 | 23 |
27 // Get all certs in NSS which have a subject matching |der_name| and which are | 24 TrustStoreNSS::~TrustStoreNSS() = default; |
28 // marked as a trusted CA. | 25 |
29 void GetAnchors(const scoped_refptr<ParsedCertificate>& cert, | 26 void TrustStoreNSS::FindTrustAnchorsForCert( |
30 SECTrustType trust_type, | 27 const scoped_refptr<ParsedCertificate>& cert, |
31 TrustAnchors* out_anchors) { | 28 TrustAnchors* out_anchors) const { |
32 crypto::EnsureNSSInit(); | 29 crypto::EnsureNSSInit(); |
33 | 30 |
34 SECItem name; | 31 SECItem name; |
35 // Use the original issuer value instead of the normalized version. NSS does a | 32 // Use the original issuer value instead of the normalized version. NSS does a |
36 // less extensive normalization in its Name comparisons, so our normalized | 33 // less extensive normalization in its Name comparisons, so our normalized |
37 // version may not match the unnormalized version. | 34 // version may not match the unnormalized version. |
38 name.len = cert->tbs().issuer_tlv.Length(); | 35 name.len = cert->tbs().issuer_tlv.Length(); |
39 name.data = const_cast<uint8_t*>(cert->tbs().issuer_tlv.UnsafeData()); | 36 name.data = const_cast<uint8_t*>(cert->tbs().issuer_tlv.UnsafeData()); |
40 // |validOnly| in CERT_CreateSubjectCertList controls whether to return only | 37 // |validOnly| in CERT_CreateSubjectCertList controls whether to return only |
41 // certs that are valid at |sorttime|. Expiration isn't meaningful for trust | 38 // certs that are valid at |sorttime|. Expiration isn't meaningful for trust |
42 // anchors, so request all the matches. | 39 // anchors, so request all the matches. |
43 CERTCertList* found_certs = CERT_CreateSubjectCertList( | 40 CERTCertList* found_certs = CERT_CreateSubjectCertList( |
44 nullptr /* certList */, CERT_GetDefaultCertDB(), &name, | 41 nullptr /* certList */, CERT_GetDefaultCertDB(), &name, |
45 PR_Now() /* sorttime */, PR_FALSE /* validOnly */); | 42 PR_Now() /* sorttime */, PR_FALSE /* validOnly */); |
46 if (!found_certs) | 43 if (!found_certs) |
47 return; | 44 return; |
48 | 45 |
49 for (CERTCertListNode* node = CERT_LIST_HEAD(found_certs); | 46 for (CERTCertListNode* node = CERT_LIST_HEAD(found_certs); |
50 !CERT_LIST_END(node, found_certs); node = CERT_LIST_NEXT(node)) { | 47 !CERT_LIST_END(node, found_certs); node = CERT_LIST_NEXT(node)) { |
51 CERTCertTrust trust; | 48 CERTCertTrust trust; |
52 if (CERT_GetCertTrust(node->cert, &trust) != SECSuccess) | 49 if (CERT_GetCertTrust(node->cert, &trust) != SECSuccess) |
53 continue; | 50 continue; |
54 | 51 |
55 // TODO(mattm): handle explicit distrust (blacklisting)? | 52 // TODO(mattm): handle explicit distrust (blacklisting)? |
56 const int ca_trust = CERTDB_TRUSTED_CA; | 53 const int ca_trust = CERTDB_TRUSTED_CA; |
57 if ((SEC_GET_TRUST_FLAGS(&trust, trust_type) & ca_trust) != ca_trust) | 54 if ((SEC_GET_TRUST_FLAGS(&trust, trust_type_) & ca_trust) != ca_trust) |
58 continue; | 55 continue; |
59 | 56 |
60 CertErrors errors; | 57 CertErrors errors; |
61 scoped_refptr<ParsedCertificate> anchor_cert = ParsedCertificate::Create( | 58 scoped_refptr<ParsedCertificate> anchor_cert = ParsedCertificate::Create( |
62 node->cert->derCert.data, node->cert->derCert.len, {}, &errors); | 59 node->cert->derCert.data, node->cert->derCert.len, {}, &errors); |
63 if (!anchor_cert) { | 60 if (!anchor_cert) { |
64 // TODO(crbug.com/634443): return errors better. | 61 // TODO(crbug.com/634443): return errors better. |
65 LOG(ERROR) << "Error parsing issuer certificate:\n" | 62 LOG(ERROR) << "Error parsing issuer certificate:\n" |
66 << errors.ToDebugString(); | 63 << errors.ToDebugString(); |
67 continue; | 64 continue; |
68 } | 65 } |
69 | 66 |
70 out_anchors->push_back(TrustAnchor::CreateFromCertificateNoConstraints( | 67 out_anchors->push_back(TrustAnchor::CreateFromCertificateNoConstraints( |
71 std::move(anchor_cert))); | 68 std::move(anchor_cert))); |
72 } | 69 } |
73 CERT_DestroyCertList(found_certs); | 70 CERT_DestroyCertList(found_certs); |
74 } | 71 } |
75 | 72 |
76 class GetAnchorsRequest : public TrustStore::Request { | |
77 public: | |
78 explicit GetAnchorsRequest(const TrustStore::TrustAnchorsCallback& callback); | |
79 // Destruction of the Request cancels it. GetAnchors will still run, but the | |
80 // callback will not be called since the WeakPtr will be invalidated. | |
81 ~GetAnchorsRequest() override = default; | |
82 | |
83 void Start(const scoped_refptr<ParsedCertificate>& cert, | |
84 SECTrustType trust_type, | |
85 base::TaskRunner* task_runner); | |
86 | |
87 private: | |
88 void HandleGetAnchors(std::unique_ptr<TrustAnchors> anchors); | |
89 | |
90 TrustStore::TrustAnchorsCallback callback_; | |
91 base::WeakPtrFactory<GetAnchorsRequest> weak_ptr_factory_; | |
92 }; | |
93 | |
94 GetAnchorsRequest::GetAnchorsRequest( | |
95 const TrustStore::TrustAnchorsCallback& callback) | |
96 : callback_(callback), weak_ptr_factory_(this) {} | |
97 | |
98 void GetAnchorsRequest::Start(const scoped_refptr<ParsedCertificate>& cert, | |
99 SECTrustType trust_type, | |
100 base::TaskRunner* task_runner) { | |
101 auto anchors = base::MakeUnique<TrustAnchors>(); | |
102 | |
103 auto* anchors_ptr = anchors.get(); | |
104 task_runner->PostTaskAndReply( | |
105 FROM_HERE, base::Bind(&GetAnchors, cert, trust_type, anchors_ptr), | |
106 base::Bind(&GetAnchorsRequest::HandleGetAnchors, | |
107 weak_ptr_factory_.GetWeakPtr(), base::Passed(&anchors))); | |
108 } | |
109 | |
110 void GetAnchorsRequest::HandleGetAnchors( | |
111 std::unique_ptr<TrustAnchors> anchors) { | |
112 base::ResetAndReturn(&callback_).Run(std::move(*anchors)); | |
113 // |this| may be deleted here. | |
114 } | |
115 | |
116 } // namespace | |
117 | |
118 TrustStoreNSS::TrustStoreNSS(SECTrustType trust_type, | |
119 scoped_refptr<base::TaskRunner> nss_task_runner) | |
120 : trust_type_(trust_type), nss_task_runner_(std::move(nss_task_runner)) {} | |
121 | |
122 TrustStoreNSS::~TrustStoreNSS() = default; | |
123 | |
124 void TrustStoreNSS::FindTrustAnchorsForCert( | |
125 const scoped_refptr<ParsedCertificate>& cert, | |
126 const TrustAnchorsCallback& callback, | |
127 TrustAnchors* synchronous_matches, | |
128 std::unique_ptr<Request>* out_req) const { | |
129 if (callback.is_null()) | |
130 return; | |
131 | |
132 auto req = base::MakeUnique<GetAnchorsRequest>(callback); | |
133 req->Start(cert, trust_type_, nss_task_runner_.get()); | |
134 *out_req = std::move(req); | |
135 } | |
136 | |
137 } // namespace net | 73 } // namespace net |
OLD | NEW |