1 // Copyright 2012 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/x509_certificate.h"
6
7 #include <limits.h>
8 #include <stdlib.h>
9
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include "base/containers/contains.h"
15 #include "base/containers/span.h"
16 #include "base/logging.h"
17 #include "base/notreached.h"
18 #include "base/numerics/safe_conversions.h"
19 #include "base/pickle.h"
20 #include "base/strings/string_piece.h"
21 #include "base/strings/string_util.h"
22 #include "base/time/time.h"
23 #include "crypto/openssl_util.h"
24 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
25 #include "net/base/tracing.h"
26 #include "net/base/url_util.h"
27 #include "net/cert/asn1_util.h"
28 #include "net/cert/time_conversions.h"
29 #include "net/cert/x509_util.h"
30 #include "third_party/boringssl/src/include/openssl/evp.h"
31 #include "third_party/boringssl/src/include/openssl/pool.h"
32 #include "third_party/boringssl/src/include/openssl/sha.h"
33 #include "third_party/boringssl/src/pki/cert_errors.h"
34 #include "third_party/boringssl/src/pki/name_constraints.h"
35 #include "third_party/boringssl/src/pki/parsed_certificate.h"
36 #include "third_party/boringssl/src/pki/parser.h"
37 #include "third_party/boringssl/src/pki/pem.h"
38 #include "third_party/boringssl/src/pki/signature_algorithm.h"
39 #include "third_party/boringssl/src/pki/verify_certificate_chain.h"
40 #include "third_party/boringssl/src/pki/verify_name_match.h"
41 #include "third_party/boringssl/src/pki/verify_signed_data.h"
42 #include "url/url_canon.h"
43
44 namespace net {
45
46 namespace {
47
48 // Indicates the order to use when trying to decode binary data, which is
49 // based on (speculation) as to what will be most common -> least common
50 const X509Certificate::Format kFormatDecodePriority[] = {
51 X509Certificate::FORMAT_SINGLE_CERTIFICATE,
52 X509Certificate::FORMAT_PKCS7
53 };
54
55 // The PEM block header used for DER certificates
56 const char kCertificateHeader[] = "CERTIFICATE";
57 // The PEM block header used for PKCS#7 data
58 const char kPKCS7Header[] = "PKCS7";
59
60 // Utility to split |src| on the first occurrence of |c|, if any. |right| will
61 // either be empty if |c| was not found, or will contain the remainder of the
62 // string including the split character itself.
SplitOnChar(base::StringPiece src,char c,base::StringPiece * left,base::StringPiece * right)63 void SplitOnChar(base::StringPiece src,
64 char c,
65 base::StringPiece* left,
66 base::StringPiece* right) {
67 size_t pos = src.find(c);
68 if (pos == base::StringPiece::npos) {
69 *left = src;
70 *right = base::StringPiece();
71 } else {
72 *left = src.substr(0, pos);
73 *right = src.substr(pos);
74 }
75 }
76
77 // Sets |value| to the Value from a DER Sequence Tag-Length-Value and return
78 // true, or return false if the TLV was not a valid DER Sequence.
ParseSequenceValue(const bssl::der::Input & tlv,bssl::der::Input * value)79 [[nodiscard]] bool ParseSequenceValue(const bssl::der::Input& tlv,
80 bssl::der::Input* value) {
81 bssl::der::Parser parser(tlv);
82 return parser.ReadTag(bssl::der::kSequence, value) && !parser.HasMore();
83 }
84
85 // Normalize |cert|'s Issuer and store it in |out_normalized_issuer|, returning
86 // true on success or false if there was a parsing error.
GetNormalizedCertIssuer(CRYPTO_BUFFER * cert,std::string * out_normalized_issuer)87 bool GetNormalizedCertIssuer(CRYPTO_BUFFER* cert,
88 std::string* out_normalized_issuer) {
89 bssl::der::Input tbs_certificate_tlv;
90 bssl::der::Input signature_algorithm_tlv;
91 bssl::der::BitString signature_value;
92 if (!bssl::ParseCertificate(
93 bssl::der::Input(CRYPTO_BUFFER_data(cert), CRYPTO_BUFFER_len(cert)),
94 &tbs_certificate_tlv, &signature_algorithm_tlv, &signature_value,
95 nullptr)) {
96 return false;
97 }
98 bssl::ParsedTbsCertificate tbs;
99 if (!ParseTbsCertificate(tbs_certificate_tlv,
100 x509_util::DefaultParseCertificateOptions(), &tbs,
101 nullptr))
102 return false;
103
104 bssl::der::Input issuer_value;
105 if (!ParseSequenceValue(tbs.issuer_tlv, &issuer_value))
106 return false;
107
108 bssl::CertErrors errors;
109 return NormalizeName(issuer_value, out_normalized_issuer, &errors);
110 }
111
CreateCertBufferFromBytesWithSanityCheck(base::span<const uint8_t> data)112 bssl::UniquePtr<CRYPTO_BUFFER> CreateCertBufferFromBytesWithSanityCheck(
113 base::span<const uint8_t> data) {
114 bssl::der::Input tbs_certificate_tlv;
115 bssl::der::Input signature_algorithm_tlv;
116 bssl::der::BitString signature_value;
117 // Do a bare minimum of DER parsing here to see if the input looks
118 // certificate-ish.
119 if (!bssl::ParseCertificate(bssl::der::Input(data), &tbs_certificate_tlv,
120 &signature_algorithm_tlv, &signature_value,
121 nullptr)) {
122 return nullptr;
123 }
124 return x509_util::CreateCryptoBuffer(data);
125 }
126
127 } // namespace
128
129 // static
CreateFromBuffer(bssl::UniquePtr<CRYPTO_BUFFER> cert_buffer,std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates)130 scoped_refptr<X509Certificate> X509Certificate::CreateFromBuffer(
131 bssl::UniquePtr<CRYPTO_BUFFER> cert_buffer,
132 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates) {
133 return CreateFromBufferUnsafeOptions(std::move(cert_buffer),
134 std::move(intermediates), {});
135 }
136
137 // static
CreateFromBufferUnsafeOptions(bssl::UniquePtr<CRYPTO_BUFFER> cert_buffer,std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates,UnsafeCreateOptions options)138 scoped_refptr<X509Certificate> X509Certificate::CreateFromBufferUnsafeOptions(
139 bssl::UniquePtr<CRYPTO_BUFFER> cert_buffer,
140 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates,
141 UnsafeCreateOptions options) {
142 DCHECK(cert_buffer);
143 ParsedFields parsed;
144 if (!parsed.Initialize(cert_buffer.get(), options)) {
145 return nullptr;
146 }
147 return base::WrapRefCounted(new X509Certificate(
148 std::move(parsed), std::move(cert_buffer), std::move(intermediates)));
149 }
150
151 // static
CreateFromDERCertChain(const std::vector<base::StringPiece> & der_certs)152 scoped_refptr<X509Certificate> X509Certificate::CreateFromDERCertChain(
153 const std::vector<base::StringPiece>& der_certs) {
154 return CreateFromDERCertChainUnsafeOptions(der_certs, {});
155 }
156
157 // static
158 scoped_refptr<X509Certificate>
CreateFromDERCertChainUnsafeOptions(const std::vector<base::StringPiece> & der_certs,UnsafeCreateOptions options)159 X509Certificate::CreateFromDERCertChainUnsafeOptions(
160 const std::vector<base::StringPiece>& der_certs,
161 UnsafeCreateOptions options) {
162 TRACE_EVENT0("io", "X509Certificate::CreateFromDERCertChain");
163 if (der_certs.empty())
164 return nullptr;
165
166 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediate_ca_certs;
167 intermediate_ca_certs.reserve(der_certs.size() - 1);
168 for (size_t i = 1; i < der_certs.size(); i++) {
169 intermediate_ca_certs.push_back(
170 x509_util::CreateCryptoBuffer(der_certs[i]));
171 }
172
173 return CreateFromBufferUnsafeOptions(
174 x509_util::CreateCryptoBuffer(der_certs[0]),
175 std::move(intermediate_ca_certs), options);
176 }
177
178 // static
CreateFromBytes(base::span<const uint8_t> data)179 scoped_refptr<X509Certificate> X509Certificate::CreateFromBytes(
180 base::span<const uint8_t> data) {
181 return CreateFromBytesUnsafeOptions(data, {});
182 }
183
184 // static
CreateFromBytesUnsafeOptions(base::span<const uint8_t> data,UnsafeCreateOptions options)185 scoped_refptr<X509Certificate> X509Certificate::CreateFromBytesUnsafeOptions(
186 base::span<const uint8_t> data,
187 UnsafeCreateOptions options) {
188 scoped_refptr<X509Certificate> cert = CreateFromBufferUnsafeOptions(
189 x509_util::CreateCryptoBuffer(data), {}, options);
190 return cert;
191 }
192
193 // static
CreateFromPickle(base::PickleIterator * pickle_iter)194 scoped_refptr<X509Certificate> X509Certificate::CreateFromPickle(
195 base::PickleIterator* pickle_iter) {
196 return CreateFromPickleUnsafeOptions(pickle_iter, {});
197 }
198
199 // static
CreateFromPickleUnsafeOptions(base::PickleIterator * pickle_iter,UnsafeCreateOptions options)200 scoped_refptr<X509Certificate> X509Certificate::CreateFromPickleUnsafeOptions(
201 base::PickleIterator* pickle_iter,
202 UnsafeCreateOptions options) {
203 size_t chain_length = 0;
204 if (!pickle_iter->ReadLength(&chain_length))
205 return nullptr;
206
207 std::vector<base::StringPiece> cert_chain;
208 const char* data = nullptr;
209 size_t data_length = 0;
210 for (size_t i = 0; i < chain_length; ++i) {
211 if (!pickle_iter->ReadData(&data, &data_length))
212 return nullptr;
213 cert_chain.emplace_back(data, data_length);
214 }
215 return CreateFromDERCertChainUnsafeOptions(cert_chain, options);
216 }
217
218 // static
CreateCertificateListFromBytes(base::span<const uint8_t> data,int format)219 CertificateList X509Certificate::CreateCertificateListFromBytes(
220 base::span<const uint8_t> data,
221 int format) {
222 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> certificates;
223
224 // Check to see if it is in a PEM-encoded form. This check is performed
225 // first, as both OS X and NSS will both try to convert if they detect
226 // PEM encoding, except they don't do it consistently between the two.
227 base::StringPiece data_string(reinterpret_cast<const char*>(data.data()),
228 data.size());
229 std::vector<std::string> pem_headers;
230
231 // To maintain compatibility with NSS/Firefox, CERTIFICATE is a universally
232 // valid PEM block header for any format.
233 pem_headers.push_back(kCertificateHeader);
234 if (format & FORMAT_PKCS7)
235 pem_headers.push_back(kPKCS7Header);
236
237 bssl::PEMTokenizer pem_tokenizer(data_string, pem_headers);
238 while (pem_tokenizer.GetNext()) {
239 std::string decoded(pem_tokenizer.data());
240
241 bssl::UniquePtr<CRYPTO_BUFFER> handle;
242 if (format & FORMAT_PEM_CERT_SEQUENCE) {
243 handle = CreateCertBufferFromBytesWithSanityCheck(
244 base::as_bytes(base::make_span(decoded)));
245 }
246 if (handle) {
247 // Parsed a DER encoded certificate. All PEM blocks that follow must
248 // also be DER encoded certificates wrapped inside of PEM blocks.
249 format = FORMAT_PEM_CERT_SEQUENCE;
250 certificates.push_back(std::move(handle));
251 continue;
252 }
253
254 // If the first block failed to parse as a DER certificate, and
255 // formats other than PEM are acceptable, check to see if the decoded
256 // data is one of the accepted formats.
257 if (format & ~FORMAT_PEM_CERT_SEQUENCE) {
258 for (size_t i = 0;
259 certificates.empty() && i < std::size(kFormatDecodePriority); ++i) {
260 if (format & kFormatDecodePriority[i]) {
261 certificates = CreateCertBuffersFromBytes(
262 base::as_bytes(base::make_span(decoded)),
263 kFormatDecodePriority[i]);
264 }
265 }
266 }
267
268 // Stop parsing after the first block for any format but a sequence of
269 // PEM-encoded DER certificates. The case of FORMAT_PEM_CERT_SEQUENCE
270 // is handled above, and continues processing until a certificate fails
271 // to parse.
272 break;
273 }
274
275 // Try each of the formats, in order of parse preference, to see if |data|
276 // contains the binary representation of a Format, if it failed to parse
277 // as a PEM certificate/chain.
278 for (size_t i = 0;
279 certificates.empty() && i < std::size(kFormatDecodePriority); ++i) {
280 if (format & kFormatDecodePriority[i])
281 certificates = CreateCertBuffersFromBytes(data, kFormatDecodePriority[i]);
282 }
283
284 CertificateList results;
285 // No certificates parsed.
286 if (certificates.empty())
287 return results;
288
289 for (auto& it : certificates) {
290 scoped_refptr<X509Certificate> cert = CreateFromBuffer(std::move(it), {});
291 if (cert)
292 results.push_back(std::move(cert));
293 }
294
295 return results;
296 }
297
CloneWithDifferentIntermediates(std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates)298 scoped_refptr<X509Certificate> X509Certificate::CloneWithDifferentIntermediates(
299 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates) {
300 // If intermediates are the same, return another reference to the same
301 // object. Note that this only does a pointer equality comparison on the
302 // CRYPTO_BUFFERs, which is generally sufficient, but in some edge cases
303 // buffers have equal contents but with different addresses. This is
304 // acceptable as this is just an optimization.
305 if (intermediates == intermediate_ca_certs_) {
306 return this;
307 }
308
309 return base::WrapRefCounted(
310 new X509Certificate(*this, std::move(intermediates)));
311 }
312
Persist(base::Pickle * pickle) const313 void X509Certificate::Persist(base::Pickle* pickle) const {
314 DCHECK(cert_buffer_);
315 // This would be an absolutely insane number of intermediates.
316 if (intermediate_ca_certs_.size() > static_cast<size_t>(INT_MAX) - 1) {
317 NOTREACHED();
318 return;
319 }
320 pickle->WriteInt(static_cast<int>(intermediate_ca_certs_.size() + 1));
321 pickle->WriteString(x509_util::CryptoBufferAsStringPiece(cert_buffer_.get()));
322 for (const auto& intermediate : intermediate_ca_certs_) {
323 pickle->WriteString(
324 x509_util::CryptoBufferAsStringPiece(intermediate.get()));
325 }
326 }
327
GetSubjectAltName(std::vector<std::string> * dns_names,std::vector<std::string> * ip_addrs) const328 bool X509Certificate::GetSubjectAltName(
329 std::vector<std::string>* dns_names,
330 std::vector<std::string>* ip_addrs) const {
331 if (dns_names)
332 dns_names->clear();
333 if (ip_addrs)
334 ip_addrs->clear();
335
336 bssl::der::Input tbs_certificate_tlv;
337 bssl::der::Input signature_algorithm_tlv;
338 bssl::der::BitString signature_value;
339 if (!bssl::ParseCertificate(
340 bssl::der::Input(CRYPTO_BUFFER_data(cert_buffer_.get()),
341 CRYPTO_BUFFER_len(cert_buffer_.get())),
342 &tbs_certificate_tlv, &signature_algorithm_tlv, &signature_value,
343 nullptr)) {
344 return false;
345 }
346
347 bssl::ParsedTbsCertificate tbs;
348 if (!ParseTbsCertificate(tbs_certificate_tlv,
349 x509_util::DefaultParseCertificateOptions(), &tbs,
350 nullptr))
351 return false;
352 if (!tbs.extensions_tlv)
353 return false;
354
355 std::map<bssl::der::Input, bssl::ParsedExtension> extensions;
356 if (!ParseExtensions(tbs.extensions_tlv.value(), &extensions))
357 return false;
358
359 bssl::ParsedExtension subject_alt_names_extension;
360 if (!ConsumeExtension(bssl::der::Input(bssl::kSubjectAltNameOid), &extensions,
361 &subject_alt_names_extension)) {
362 return false;
363 }
364
365 bssl::CertErrors errors;
366 std::unique_ptr<bssl::GeneralNames> subject_alt_names =
367 bssl::GeneralNames::Create(subject_alt_names_extension.value, &errors);
368 if (!subject_alt_names)
369 return false;
370
371 if (dns_names) {
372 for (const auto& dns_name : subject_alt_names->dns_names)
373 dns_names->push_back(std::string(dns_name));
374 }
375 if (ip_addrs) {
376 for (const auto& addr : subject_alt_names->ip_addresses) {
377 ip_addrs->push_back(std::string(addr.AsStringView()));
378 }
379 }
380
381 return !subject_alt_names->dns_names.empty() ||
382 !subject_alt_names->ip_addresses.empty();
383 }
384
HasExpired() const385 bool X509Certificate::HasExpired() const {
386 return base::Time::Now() > valid_expiry();
387 }
388
EqualsExcludingChain(const X509Certificate * other) const389 bool X509Certificate::EqualsExcludingChain(const X509Certificate* other) const {
390 return x509_util::CryptoBufferEqual(cert_buffer_.get(),
391 other->cert_buffer_.get());
392 }
393
EqualsIncludingChain(const X509Certificate * other) const394 bool X509Certificate::EqualsIncludingChain(const X509Certificate* other) const {
395 if (intermediate_ca_certs_.size() != other->intermediate_ca_certs_.size() ||
396 !EqualsExcludingChain(other)) {
397 return false;
398 }
399 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) {
400 if (!x509_util::CryptoBufferEqual(intermediate_ca_certs_[i].get(),
401 other->intermediate_ca_certs_[i].get())) {
402 return false;
403 }
404 }
405 return true;
406 }
407
IsIssuedByEncoded(const std::vector<std::string> & valid_issuers) const408 bool X509Certificate::IsIssuedByEncoded(
409 const std::vector<std::string>& valid_issuers) const {
410 std::vector<std::string> normalized_issuers;
411 bssl::CertErrors errors;
412 for (const auto& raw_issuer : valid_issuers) {
413 bssl::der::Input issuer_value;
414 std::string normalized_issuer;
415 if (!ParseSequenceValue(bssl::der::Input(raw_issuer), &issuer_value) ||
416 !NormalizeName(issuer_value, &normalized_issuer, &errors)) {
417 continue;
418 }
419 normalized_issuers.push_back(std::move(normalized_issuer));
420 }
421
422 std::string normalized_cert_issuer;
423 if (!GetNormalizedCertIssuer(cert_buffer_.get(), &normalized_cert_issuer))
424 return false;
425 if (base::Contains(normalized_issuers, normalized_cert_issuer))
426 return true;
427
428 for (const auto& intermediate : intermediate_ca_certs_) {
429 if (!GetNormalizedCertIssuer(intermediate.get(), &normalized_cert_issuer))
430 return false;
431 if (base::Contains(normalized_issuers, normalized_cert_issuer))
432 return true;
433 }
434 return false;
435 }
436
437 // static
VerifyHostname(const std::string & hostname,const std::vector<std::string> & cert_san_dns_names,const std::vector<std::string> & cert_san_ip_addrs)438 bool X509Certificate::VerifyHostname(
439 const std::string& hostname,
440 const std::vector<std::string>& cert_san_dns_names,
441 const std::vector<std::string>& cert_san_ip_addrs) {
442 DCHECK(!hostname.empty());
443
444 if (cert_san_dns_names.empty() && cert_san_ip_addrs.empty()) {
445 // Either a dNSName or iPAddress subjectAltName MUST be present in order
446 // to match, so fail quickly if not.
447 return false;
448 }
449
450 // Perform name verification following http://tools.ietf.org/html/rfc6125.
451 // The terminology used in this method is as per that RFC:-
452 // Reference identifier == the host the local user/agent is intending to
453 // access, i.e. the thing displayed in the URL bar.
454 // Presented identifier(s) == name(s) the server knows itself as, in its cert.
455
456 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
457 const std::string host_or_ip = hostname.find(':') != std::string::npos ?
458 "[" + hostname + "]" : hostname;
459 url::CanonHostInfo host_info;
460 std::string reference_name = CanonicalizeHost(host_or_ip, &host_info);
461
462 // If the host cannot be canonicalized, fail fast.
463 if (reference_name.empty())
464 return false;
465
466 // Fully handle all cases where |hostname| contains an IP address.
467 if (host_info.IsIPAddress()) {
468 base::StringPiece ip_addr_string(
469 reinterpret_cast<const char*>(host_info.address),
470 host_info.AddressLength());
471 return base::Contains(cert_san_ip_addrs, ip_addr_string);
472 }
473
474 // The host portion of a URL may support a variety of name resolution formats
475 // and services. However, the only supported name types in this code are IP
476 // addresses, which have been handled above via iPAddress subjectAltNames,
477 // and DNS names, via dNSName subjectAltNames.
478 // Validate that the host conforms to the DNS preferred name syntax, in
479 // either relative or absolute form, and exclude the "root" label for DNS.
480 if (reference_name == "." || !IsCanonicalizedHostCompliant(reference_name))
481 return false;
482
483 // CanonicalizeHost does not normalize absolute vs relative DNS names. If
484 // the input name was absolute (included trailing .), normalize it as if it
485 // was relative.
486 if (reference_name.back() == '.')
487 reference_name.pop_back();
488
489 // |reference_domain| is the remainder of |host| after the leading host
490 // component is stripped off, but includes the leading dot e.g.
491 // "www.f.com" -> ".f.com".
492 // If there is no meaningful domain part to |host| (e.g. it contains no dots)
493 // then |reference_domain| will be empty.
494 base::StringPiece reference_host, reference_domain;
495 SplitOnChar(reference_name, '.', &reference_host, &reference_domain);
496 bool allow_wildcards = false;
497 if (!reference_domain.empty()) {
498 DCHECK(reference_domain.starts_with("."));
499
500 // Do not allow wildcards for public/ICANN registry controlled domains -
501 // that is, prevent *.com or *.co.uk as valid presented names, but do not
502 // prevent *.appspot.com (a private registry controlled domain).
503 // In addition, unknown top-level domains (such as 'intranet' domains or
504 // new TLDs/gTLDs not yet added to the registry controlled domain dataset)
505 // are also implicitly prevented.
506 // Because |reference_domain| must contain at least one name component that
507 // is not registry controlled, this ensures that all reference domains
508 // contain at least three domain components when using wildcards.
509 size_t registry_length =
510 registry_controlled_domains::GetCanonicalHostRegistryLength(
511 reference_name,
512 registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES,
513 registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
514
515 // Because |reference_name| was already canonicalized, the following
516 // should never happen.
517 CHECK_NE(std::string::npos, registry_length);
518
519 // Account for the leading dot in |reference_domain|.
520 bool is_registry_controlled =
521 registry_length != 0 &&
522 registry_length == (reference_domain.size() - 1);
523
524 // Additionally, do not attempt wildcard matching for purely numeric
525 // hostnames.
526 allow_wildcards =
527 !is_registry_controlled &&
528 reference_name.find_first_not_of("0123456789.") != std::string::npos;
529 }
530
531 // Now step through the DNS names doing wild card comparison (if necessary)
532 // on each against the reference name.
533 for (const auto& cert_san_dns_name : cert_san_dns_names) {
534 // Catch badly corrupt cert names up front.
535 if (cert_san_dns_name.empty() ||
536 cert_san_dns_name.find('\0') != std::string::npos) {
537 continue;
538 }
539 std::string presented_name(base::ToLowerASCII(cert_san_dns_name));
540
541 // Remove trailing dot, if any.
542 if (*presented_name.rbegin() == '.')
543 presented_name.resize(presented_name.length() - 1);
544
545 // The hostname must be at least as long as the cert name it is matching,
546 // as we require the wildcard (if present) to match at least one character.
547 if (presented_name.length() > reference_name.length())
548 continue;
549
550 base::StringPiece presented_host, presented_domain;
551 SplitOnChar(presented_name, '.', &presented_host, &presented_domain);
552
553 if (presented_domain != reference_domain)
554 continue;
555
556 if (presented_host != "*") {
557 if (presented_host == reference_host)
558 return true;
559 continue;
560 }
561
562 if (!allow_wildcards)
563 continue;
564
565 return true;
566 }
567 return false;
568 }
569
VerifyNameMatch(const std::string & hostname) const570 bool X509Certificate::VerifyNameMatch(const std::string& hostname) const {
571 std::vector<std::string> dns_names, ip_addrs;
572 GetSubjectAltName(&dns_names, &ip_addrs);
573 return VerifyHostname(hostname, dns_names, ip_addrs);
574 }
575
576 // static
GetPEMEncodedFromDER(base::StringPiece der_encoded,std::string * pem_encoded)577 bool X509Certificate::GetPEMEncodedFromDER(base::StringPiece der_encoded,
578 std::string* pem_encoded) {
579 if (der_encoded.empty())
580 return false;
581
582 *pem_encoded = bssl::PEMEncode(der_encoded, "CERTIFICATE");
583 return true;
584 }
585
586 // static
GetPEMEncoded(const CRYPTO_BUFFER * cert_buffer,std::string * pem_encoded)587 bool X509Certificate::GetPEMEncoded(const CRYPTO_BUFFER* cert_buffer,
588 std::string* pem_encoded) {
589 return GetPEMEncodedFromDER(x509_util::CryptoBufferAsStringPiece(cert_buffer),
590 pem_encoded);
591 }
592
GetPEMEncodedChain(std::vector<std::string> * pem_encoded) const593 bool X509Certificate::GetPEMEncodedChain(
594 std::vector<std::string>* pem_encoded) const {
595 std::vector<std::string> encoded_chain;
596 std::string pem_data;
597 if (!GetPEMEncoded(cert_buffer(), &pem_data))
598 return false;
599 encoded_chain.push_back(pem_data);
600 for (const auto& intermediate_ca_cert : intermediate_ca_certs_) {
601 if (!GetPEMEncoded(intermediate_ca_cert.get(), &pem_data))
602 return false;
603 encoded_chain.push_back(pem_data);
604 }
605 pem_encoded->swap(encoded_chain);
606 return true;
607 }
608
609 // static
GetPublicKeyInfo(const CRYPTO_BUFFER * cert_buffer,size_t * size_bits,PublicKeyType * type)610 void X509Certificate::GetPublicKeyInfo(const CRYPTO_BUFFER* cert_buffer,
611 size_t* size_bits,
612 PublicKeyType* type) {
613 *type = kPublicKeyTypeUnknown;
614 *size_bits = 0;
615
616 base::StringPiece spki;
617 if (!asn1::ExtractSPKIFromDERCert(
618 base::StringPiece(
619 reinterpret_cast<const char*>(CRYPTO_BUFFER_data(cert_buffer)),
620 CRYPTO_BUFFER_len(cert_buffer)),
621 &spki)) {
622 return;
623 }
624
625 bssl::UniquePtr<EVP_PKEY> pkey;
626 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE);
627 CBS cbs;
628 CBS_init(&cbs, reinterpret_cast<const uint8_t*>(spki.data()), spki.size());
629 pkey.reset(EVP_parse_public_key(&cbs));
630 if (!pkey)
631 return;
632
633 switch (EVP_PKEY_id(pkey.get())) {
634 case EVP_PKEY_RSA:
635 *type = kPublicKeyTypeRSA;
636 break;
637 case EVP_PKEY_DSA:
638 *type = kPublicKeyTypeDSA;
639 break;
640 case EVP_PKEY_EC:
641 *type = kPublicKeyTypeECDSA;
642 break;
643 case EVP_PKEY_DH:
644 *type = kPublicKeyTypeDH;
645 break;
646 }
647 *size_bits = base::saturated_cast<size_t>(EVP_PKEY_bits(pkey.get()));
648 }
649
650 // static
651 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>>
CreateCertBuffersFromBytes(base::span<const uint8_t> data,Format format)652 X509Certificate::CreateCertBuffersFromBytes(base::span<const uint8_t> data,
653 Format format) {
654 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> results;
655
656 switch (format) {
657 case FORMAT_SINGLE_CERTIFICATE: {
658 bssl::UniquePtr<CRYPTO_BUFFER> handle =
659 CreateCertBufferFromBytesWithSanityCheck(data);
660 if (handle)
661 results.push_back(std::move(handle));
662 break;
663 }
664 case FORMAT_PKCS7: {
665 x509_util::CreateCertBuffersFromPKCS7Bytes(data, &results);
666 break;
667 }
668 default: {
669 NOTREACHED() << "Certificate format " << format << " unimplemented";
670 break;
671 }
672 }
673
674 return results;
675 }
676
677 // static
CalculateFingerprint256(const CRYPTO_BUFFER * cert)678 SHA256HashValue X509Certificate::CalculateFingerprint256(
679 const CRYPTO_BUFFER* cert) {
680 SHA256HashValue sha256;
681
682 SHA256(CRYPTO_BUFFER_data(cert), CRYPTO_BUFFER_len(cert), sha256.data);
683 return sha256;
684 }
685
CalculateChainFingerprint256() const686 SHA256HashValue X509Certificate::CalculateChainFingerprint256() const {
687 SHA256HashValue sha256;
688 memset(sha256.data, 0, sizeof(sha256.data));
689
690 SHA256_CTX sha256_ctx;
691 SHA256_Init(&sha256_ctx);
692 SHA256_Update(&sha256_ctx, CRYPTO_BUFFER_data(cert_buffer_.get()),
693 CRYPTO_BUFFER_len(cert_buffer_.get()));
694 for (const auto& cert : intermediate_ca_certs_) {
695 SHA256_Update(&sha256_ctx, CRYPTO_BUFFER_data(cert.get()),
696 CRYPTO_BUFFER_len(cert.get()));
697 }
698 SHA256_Final(sha256.data, &sha256_ctx);
699
700 return sha256;
701 }
702
703 // static
IsSelfSigned(CRYPTO_BUFFER * cert_buffer)704 bool X509Certificate::IsSelfSigned(CRYPTO_BUFFER* cert_buffer) {
705 std::shared_ptr<const bssl::ParsedCertificate> parsed_cert =
706 bssl::ParsedCertificate::Create(
707 bssl::UpRef(cert_buffer), x509_util::DefaultParseCertificateOptions(),
708 /*errors=*/nullptr);
709 if (!parsed_cert) {
710 return false;
711 }
712 return VerifyCertificateIsSelfSigned(*parsed_cert, /*cache=*/nullptr,
713 /*errors=*/nullptr);
714 }
715
X509Certificate(ParsedFields parsed,bssl::UniquePtr<CRYPTO_BUFFER> cert_buffer,std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates)716 X509Certificate::X509Certificate(
717 ParsedFields parsed,
718 bssl::UniquePtr<CRYPTO_BUFFER> cert_buffer,
719 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates)
720 : parsed_(std::move(parsed)),
721 cert_buffer_(std::move(cert_buffer)),
722 intermediate_ca_certs_(std::move(intermediates)) {}
723
X509Certificate(const X509Certificate & other,std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates)724 X509Certificate::X509Certificate(
725 const X509Certificate& other,
726 std::vector<bssl::UniquePtr<CRYPTO_BUFFER>> intermediates)
727 : parsed_(other.parsed_),
728 cert_buffer_(bssl::UpRef(other.cert_buffer_)),
729 intermediate_ca_certs_(std::move(intermediates)) {}
730
731 X509Certificate::~X509Certificate() = default;
732
733 X509Certificate::ParsedFields::ParsedFields() = default;
734 X509Certificate::ParsedFields::ParsedFields(const ParsedFields&) = default;
735 X509Certificate::ParsedFields::ParsedFields(ParsedFields&&) = default;
736 X509Certificate::ParsedFields::~ParsedFields() = default;
737
Initialize(const CRYPTO_BUFFER * cert_buffer,X509Certificate::UnsafeCreateOptions options)738 bool X509Certificate::ParsedFields::Initialize(
739 const CRYPTO_BUFFER* cert_buffer,
740 X509Certificate::UnsafeCreateOptions options) {
741 bssl::der::Input tbs_certificate_tlv;
742 bssl::der::Input signature_algorithm_tlv;
743 bssl::der::BitString signature_value;
744
745 if (!bssl::ParseCertificate(bssl::der::Input(CRYPTO_BUFFER_data(cert_buffer),
746 CRYPTO_BUFFER_len(cert_buffer)),
747 &tbs_certificate_tlv, &signature_algorithm_tlv,
748 &signature_value, nullptr)) {
749 return false;
750 }
751
752 bssl::ParsedTbsCertificate tbs;
753 if (!ParseTbsCertificate(tbs_certificate_tlv,
754 x509_util::DefaultParseCertificateOptions(), &tbs,
755 nullptr))
756 return false;
757
758 CertPrincipal::PrintableStringHandling printable_string_handling =
759 options.printable_string_is_utf8
760 ? CertPrincipal::PrintableStringHandling::kAsUTF8Hack
761 : CertPrincipal::PrintableStringHandling::kDefault;
762 if (!subject_.ParseDistinguishedName(tbs.subject_tlv,
763 printable_string_handling) ||
764 !issuer_.ParseDistinguishedName(tbs.issuer_tlv,
765 printable_string_handling)) {
766 return false;
767 }
768
769 if (!GeneralizedTimeToTime(tbs.validity_not_before, &valid_start_) ||
770 !GeneralizedTimeToTime(tbs.validity_not_after, &valid_expiry_)) {
771 return false;
772 }
773 serial_number_ = tbs.serial_number.AsString();
774 return true;
775 }
776
777 } // namespace net
778