Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 ( | |
| 19 // TrustStoreChromeOS which uses net::NSSProfileFilterChromeOS.. similar to | |
| 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(const std::string& der_name, TrustAnchors* out_anchors) { | |
| 33 crypto::EnsureNSSInit(); | |
| 34 | |
| 35 SECItem name; | |
| 36 name.len = der_name.size(); | |
| 37 name.data = | |
| 38 const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>((der_name.data()))); | |
| 39 CERTCertList* found_certs = CERT_CreateSubjectCertList( | |
| 40 nullptr /* certList */, CERT_GetDefaultCertDB(), &name, | |
| 41 PR_Now() /* sorttime */, PR_FALSE /* validOnly */); | |
| 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> cert = | |
| 57 ParsedCertificate::CreateFromCertificateData( | |
| 58 node->cert->derCert.data, node->cert->derCert.len, | |
| 59 ParsedCertificate::DataSource::INTERNAL_COPY, {}); | |
|
eroman
2016/08/23 18:48:54
side-comment: I am thinking we may want to refacto
| |
| 60 if (!cert) { | |
| 61 // TODO(mattm): return errors better. | |
| 62 LOG(ERROR) << "error parsing issuer certificate"; | |
| 63 continue; | |
| 64 } | |
| 65 | |
| 66 out_anchors->push_back( | |
| 67 TrustAnchor::CreateFromCertificateNoConstraints(std::move(cert))); | |
| 68 } | |
| 69 CERT_DestroyCertList(found_certs); | |
| 70 } | |
| 71 | |
| 72 class GetAnchorsRequest : public TrustStore::Request { | |
| 73 public: | |
| 74 explicit GetAnchorsRequest(const TrustStore::TrustAnchorCallback& 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(const der::Input& name, base::TaskRunner* task_runner); | |
| 80 | |
| 81 private: | |
| 82 void HandleGetAnchors(std::unique_ptr<TrustAnchors> anchors); | |
| 83 | |
| 84 TrustStore::TrustAnchorCallback callback_; | |
| 85 base::WeakPtrFactory<GetAnchorsRequest> weak_ptr_factory_; | |
| 86 }; | |
| 87 | |
| 88 GetAnchorsRequest::GetAnchorsRequest( | |
| 89 const TrustStore::TrustAnchorCallback& callback) | |
| 90 : callback_(callback), weak_ptr_factory_(this) {} | |
| 91 | |
| 92 void GetAnchorsRequest::Start(const der::Input& name, | |
| 93 base::TaskRunner* task_runner) { | |
| 94 auto anchors = base::MakeUnique<TrustAnchors>(); | |
| 95 auto* anchors_ptr = anchors.get(); | |
| 96 task_runner->PostTaskAndReply( | |
| 97 FROM_HERE, base::Bind(&GetAnchors, name.AsString(), anchors_ptr), | |
| 98 base::Bind(&GetAnchorsRequest::HandleGetAnchors, | |
| 99 weak_ptr_factory_.GetWeakPtr(), base::Passed(&anchors))); | |
| 100 } | |
| 101 | |
| 102 void GetAnchorsRequest::HandleGetAnchors( | |
| 103 std::unique_ptr<TrustAnchors> anchors) { | |
| 104 base::ResetAndReturn(&callback_).Run(std::move(anchors)); | |
| 105 // |this| may be deleted here. | |
| 106 } | |
| 107 | |
| 108 } // namespace | |
| 109 | |
| 110 TrustStoreNSS::TrustStoreNSS(scoped_refptr<base::TaskRunner> nss_task_runner) | |
| 111 : nss_task_runner_(std::move(nss_task_runner)) {} | |
| 112 | |
| 113 TrustStoreNSS::~TrustStoreNSS() = default; | |
| 114 | |
| 115 void TrustStoreNSS::FindTrustAnchorsForCert( | |
| 116 const ParsedCertificate* cert, | |
| 117 const TrustAnchorCallback& callback, | |
| 118 TrustAnchors* out_matches, | |
| 119 std::unique_ptr<Request>* out_req) const { | |
| 120 if (callback.is_null()) { | |
| 121 *out_req = nullptr; | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 auto req(base::MakeUnique<GetAnchorsRequest>(callback)); | |
| 126 req->Start(cert->tbs().issuer_tlv, nss_task_runner_.get()); | |
|
eroman
2016/08/23 18:48:54
This could be a problem in the case of cancellatio
mattm
2016/08/27 00:37:02
it was making a copy of the name inside Start() be
| |
| 127 *out_req = std::move(req); | |
| 128 } | |
| 129 | |
| 130 } // namespace net | |
| OLD | NEW |