xref: /aosp_15_r20/external/cronet/net/cert/internal/cert_issuer_source_aia.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
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/cert_issuer_source_aia.h"
6 
7 #include <string_view>
8 
9 #include "base/containers/span.h"
10 #include "base/logging.h"
11 #include "net/cert/cert_net_fetcher.h"
12 #include "net/cert/x509_util.h"
13 #include "third_party/boringssl/src/pki/cert_errors.h"
14 #include "third_party/boringssl/src/pki/pem.h"
15 #include "url/gurl.h"
16 
17 namespace net {
18 
19 namespace {
20 
21 // TODO(mattm): These are arbitrary choices. Re-evaluate.
22 const int kTimeoutMilliseconds = 10000;
23 const int kMaxResponseBytes = 65536;
24 const int kMaxFetchesPerCert = 5;
25 
ParseCertFromDer(base::span<const uint8_t> data,bssl::ParsedCertificateList * results)26 bool ParseCertFromDer(base::span<const uint8_t> data,
27                       bssl::ParsedCertificateList* results) {
28   bssl::CertErrors errors;
29   if (!bssl::ParsedCertificate::CreateAndAddToVector(
30           x509_util::CreateCryptoBuffer(data),
31           x509_util::DefaultParseCertificateOptions(), results, &errors)) {
32     // TODO(crbug.com/634443): propagate error info.
33     // TODO(mattm): this creates misleading log spam if one of the other Parse*
34     // methods is actually able to parse the data.
35     LOG(ERROR) << "Error parsing cert retrieved from AIA (as DER):\n"
36                << errors.ToDebugString();
37 
38     return false;
39   }
40 
41   return true;
42 }
43 
ParseCertsFromCms(base::span<const uint8_t> data,bssl::ParsedCertificateList * results)44 bool ParseCertsFromCms(base::span<const uint8_t> data,
45                        bssl::ParsedCertificateList* results) {
46   std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> cert_buffers;
47   // A "certs-only CMS message" is a PKCS#7 SignedData structure with no signed
48   // inner content. See RFC 3851 section 3.2.2 and RFC 2315 section 9.1.
49   // Note: RFC 5280 section 4.2.2.1 says that the data should be a certs-only
50   // CMS message, however this will actually allow a SignedData which
51   // contains CRLs and/or inner content, ignoring them.
52   if (!x509_util::CreateCertBuffersFromPKCS7Bytes(data, &cert_buffers)) {
53     return false;
54   }
55   bool any_succeeded = false;
56   for (auto& cert_buffer : cert_buffers) {
57     bssl::CertErrors errors;
58     if (!bssl::ParsedCertificate::CreateAndAddToVector(
59             std::move(cert_buffer), x509_util::DefaultParseCertificateOptions(),
60             results, &errors)) {
61       // TODO(crbug.com/634443): propagate error info.
62       LOG(ERROR) << "Error parsing cert extracted from AIA PKCS7:\n"
63                  << errors.ToDebugString();
64       continue;
65     }
66     any_succeeded = true;
67   }
68   return any_succeeded;
69 }
70 
ParseCertFromPem(const uint8_t * data,size_t length,bssl::ParsedCertificateList * results)71 bool ParseCertFromPem(const uint8_t* data,
72                       size_t length,
73                       bssl::ParsedCertificateList* results) {
74   std::string_view data_strpiece(reinterpret_cast<const char*>(data), length);
75 
76   bssl::PEMTokenizer pem_tokenizer(data_strpiece, {"CERTIFICATE"});
77   if (!pem_tokenizer.GetNext())
78     return false;
79 
80   return ParseCertFromDer(base::as_byte_span(pem_tokenizer.data()), results);
81 }
82 
83 class AiaRequest : public bssl::CertIssuerSource::Request {
84  public:
85   AiaRequest() = default;
86 
87   AiaRequest(const AiaRequest&) = delete;
88   AiaRequest& operator=(const AiaRequest&) = delete;
89 
90   ~AiaRequest() override;
91 
92   // bssl::CertIssuerSource::Request implementation.
93   void GetNext(bssl::ParsedCertificateList* issuers) override;
94 
95   void AddCertFetcherRequest(
96       std::unique_ptr<CertNetFetcher::Request> cert_fetcher_request);
97 
98   bool AddCompletedFetchToResults(Error error,
99                                   std::vector<uint8_t> fetched_bytes,
100                                   bssl::ParsedCertificateList* results);
101 
102  private:
103   std::vector<std::unique_ptr<CertNetFetcher::Request>> cert_fetcher_requests_;
104   size_t current_request_ = 0;
105 };
106 
107 AiaRequest::~AiaRequest() = default;
108 
GetNext(bssl::ParsedCertificateList * out_certs)109 void AiaRequest::GetNext(bssl::ParsedCertificateList* out_certs) {
110   // TODO(eroman): Rather than blocking in FIFO order, select the one that
111   // completes first.
112   while (current_request_ < cert_fetcher_requests_.size()) {
113     Error error;
114     std::vector<uint8_t> bytes;
115     auto req = std::move(cert_fetcher_requests_[current_request_++]);
116     req->WaitForResult(&error, &bytes);
117 
118     if (AddCompletedFetchToResults(error, std::move(bytes), out_certs)) {
119       return;
120     }
121   }
122 }
123 
AddCertFetcherRequest(std::unique_ptr<CertNetFetcher::Request> cert_fetcher_request)124 void AiaRequest::AddCertFetcherRequest(
125     std::unique_ptr<CertNetFetcher::Request> cert_fetcher_request) {
126   DCHECK(cert_fetcher_request);
127   cert_fetcher_requests_.push_back(std::move(cert_fetcher_request));
128 }
129 
AddCompletedFetchToResults(Error error,std::vector<uint8_t> fetched_bytes,bssl::ParsedCertificateList * results)130 bool AiaRequest::AddCompletedFetchToResults(
131     Error error,
132     std::vector<uint8_t> fetched_bytes,
133     bssl::ParsedCertificateList* results) {
134   if (error != OK) {
135     // TODO(mattm): propagate error info.
136     LOG(ERROR) << "AiaRequest::OnFetchCompleted got error " << error;
137     return false;
138   }
139 
140   // RFC 5280 section 4.2.2.1:
141   //
142   //    Conforming applications that support HTTP or FTP for accessing
143   //    certificates MUST be able to accept individual DER encoded
144   //    certificates and SHOULD be able to accept "certs-only" CMS messages.
145 
146   // TODO(https://crbug.com/870359): Some AIA responses are served as PEM, which
147   // is not part of RFC 5280's profile.
148   return ParseCertFromDer(fetched_bytes, results) ||
149          ParseCertsFromCms(fetched_bytes, results) ||
150          ParseCertFromPem(fetched_bytes.data(), fetched_bytes.size(), results);
151 }
152 
153 }  // namespace
154 
CertIssuerSourceAia(scoped_refptr<CertNetFetcher> cert_fetcher)155 CertIssuerSourceAia::CertIssuerSourceAia(
156     scoped_refptr<CertNetFetcher> cert_fetcher)
157     : cert_fetcher_(std::move(cert_fetcher)) {}
158 
159 CertIssuerSourceAia::~CertIssuerSourceAia() = default;
160 
SyncGetIssuersOf(const bssl::ParsedCertificate * cert,bssl::ParsedCertificateList * issuers)161 void CertIssuerSourceAia::SyncGetIssuersOf(
162     const bssl::ParsedCertificate* cert,
163     bssl::ParsedCertificateList* issuers) {
164   // CertIssuerSourceAia never returns synchronous results.
165 }
166 
AsyncGetIssuersOf(const bssl::ParsedCertificate * cert,std::unique_ptr<Request> * out_req)167 void CertIssuerSourceAia::AsyncGetIssuersOf(const bssl::ParsedCertificate* cert,
168                                             std::unique_ptr<Request>* out_req) {
169   out_req->reset();
170 
171   if (!cert->has_authority_info_access())
172     return;
173 
174   // RFC 5280 section 4.2.2.1:
175   //
176   //    An authorityInfoAccess extension may include multiple instances of
177   //    the id-ad-caIssuers accessMethod.  The different instances may
178   //    specify different methods for accessing the same information or may
179   //    point to different information.
180 
181   std::vector<GURL> urls;
182   for (const auto& uri : cert->ca_issuers_uris()) {
183     GURL url(uri);
184     if (url.is_valid()) {
185       // TODO(mattm): do the kMaxFetchesPerCert check only on the number of
186       // supported URL schemes, not all the URLs.
187       if (urls.size() < kMaxFetchesPerCert) {
188         urls.push_back(url);
189       } else {
190         // TODO(mattm): propagate error info.
191         LOG(ERROR) << "kMaxFetchesPerCert exceeded, skipping";
192       }
193     } else {
194       // TODO(mattm): propagate error info.
195       LOG(ERROR) << "invalid AIA URL: " << uri;
196     }
197   }
198   if (urls.empty())
199     return;
200 
201   auto aia_request = std::make_unique<AiaRequest>();
202 
203   for (const auto& url : urls) {
204     // TODO(mattm): add synchronous failure mode to FetchCaIssuers interface so
205     // that this doesn't need to wait for async callback just to tell that an
206     // URL has an unsupported scheme?
207     aia_request->AddCertFetcherRequest(cert_fetcher_->FetchCaIssuers(
208         url, kTimeoutMilliseconds, kMaxResponseBytes));
209   }
210 
211   *out_req = std::move(aia_request);
212 }
213 
214 }  // namespace net
215