1 // Copyright (c) 2012 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.
5 #include "net/cert/x509_certificate.h"
14 #include "base/base64.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/singleton.h"
19 #include "base/metrics/histogram.h"
20 #include "base/pickle.h"
21 #include "base/sha1.h"
22 #include "base/strings/string_piece.h"
23 #include "base/strings/string_util.h"
24 #include "base/synchronization/lock.h"
25 #include "base/time/time.h"
26 #include "crypto/secure_hash.h"
27 #include "net/base/net_util.h"
28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
29 #include "net/cert/pem_tokenizer.h"
30 #include "url/url_canon.h"
36 // Indicates the order to use when trying to decode binary data, which is
37 // based on (speculation) as to what will be most common -> least common
38 const X509Certificate::Format kFormatDecodePriority
[] = {
39 X509Certificate::FORMAT_SINGLE_CERTIFICATE
,
40 X509Certificate::FORMAT_PKCS7
43 // The PEM block header used for DER certificates
44 const char kCertificateHeader
[] = "CERTIFICATE";
45 // The PEM block header used for PKCS#7 data
46 const char kPKCS7Header
[] = "PKCS7";
49 // A thread-safe cache for OS certificate handles.
51 // Within each of the supported underlying crypto libraries, a certificate
52 // handle is represented as a ref-counted object that contains the parsed
53 // data for the certificate. In addition, the underlying OS handle may also
54 // contain a copy of the original ASN.1 DER used to constructed the handle.
56 // In order to reduce the memory usage when multiple SSL connections exist,
57 // with each connection storing the server's identity certificate plus any
58 // intermediates supplied, the certificate handles are cached. Any two
59 // X509Certificates that were created from the same ASN.1 DER data,
60 // regardless of where that data came from, will share the same underlying
61 // OS certificate handle.
62 class X509CertificateCache
{
64 // Performs a compare-and-swap like operation. If an OS certificate handle
65 // for the same certificate data as |*cert_handle| already exists in the
66 // cache, the original |*cert_handle| will be freed and |cert_handle|
67 // will be updated to point to a duplicated reference to the existing cached
68 // certificate, with the caller taking ownership of this duplicated handle.
69 // If an equivalent OS certificate handle is not found, a duplicated
70 // reference to |*cert_handle| will be added to the cache. In either case,
71 // upon return, the caller fully owns |*cert_handle| and is responsible for
72 // calling FreeOSCertHandle(), after first calling Remove().
73 void InsertOrUpdate(X509Certificate::OSCertHandle
* cert_handle
);
75 // Decrements the cache reference count for |cert_handle|, a handle that was
76 // previously obtained by calling InsertOrUpdate(). If this is the last
77 // cached reference held, this will remove the handle from the cache. The
78 // caller retains ownership of |cert_handle| and remains responsible for
79 // calling FreeOSCertHandle() to release the underlying OS certificate
80 void Remove(X509Certificate::OSCertHandle cert_handle
);
83 // A single entry in the cache. Certificates will be keyed by their SHA1
84 // fingerprints, but will not be considered equivalent unless the entire
85 // certificate data matches.
87 Entry() : cert_handle(NULL
), ref_count(0) {}
89 X509Certificate::OSCertHandle cert_handle
;
91 // Increased by each call to InsertOrUpdate(), and balanced by each call
92 // to Remove(). When it equals 0, all references created by
93 // InsertOrUpdate() have been released, so the cache entry will be removed
94 // the cached OS certificate handle will be freed.
97 typedef std::map
<SHA1HashValue
, Entry
, SHA1HashValueLessThan
> CertMap
;
99 // Obtain an instance of X509CertificateCache via a LazyInstance.
100 X509CertificateCache() {}
101 ~X509CertificateCache() {}
102 friend struct base::DefaultLazyInstanceTraits
<X509CertificateCache
>;
104 // You must acquire this lock before using any private data of this object
105 // You must not block while holding this lock.
108 // The certificate cache. You must acquire |lock_| before using |cache_|.
111 DISALLOW_COPY_AND_ASSIGN(X509CertificateCache
);
114 base::LazyInstance
<X509CertificateCache
>::Leaky
115 g_x509_certificate_cache
= LAZY_INSTANCE_INITIALIZER
;
117 void X509CertificateCache::InsertOrUpdate(
118 X509Certificate::OSCertHandle
* cert_handle
) {
120 SHA1HashValue fingerprint
=
121 X509Certificate::CalculateFingerprint(*cert_handle
);
123 X509Certificate::OSCertHandle old_handle
= NULL
;
125 base::AutoLock
lock(lock_
);
126 CertMap::iterator pos
= cache_
.find(fingerprint
);
127 if (pos
== cache_
.end()) {
128 // A cached entry was not found, so initialize a new entry. The entry
129 // assumes ownership of the current |*cert_handle|.
131 cache_entry
.cert_handle
= *cert_handle
;
132 cache_entry
.ref_count
= 0;
133 CertMap::value_type
cache_value(fingerprint
, cache_entry
);
134 pos
= cache_
.insert(cache_value
).first
;
137 X509Certificate::IsSameOSCert(*cert_handle
, pos
->second
.cert_handle
);
139 // Two certificates don't match, due to a SHA1 hash collision. Given
140 // the low probability, the simplest solution is to not cache the
141 // certificate, which should not affect performance too negatively.
144 // A cached entry was found and will be used instead of the caller's
145 // handle. Ensure the caller's original handle will be freed, since
146 // ownership is assumed.
147 old_handle
= *cert_handle
;
149 // Whether an existing cached handle or a new handle, increment the
150 // cache's reference count and return a handle that the caller can own.
151 ++pos
->second
.ref_count
;
152 *cert_handle
= X509Certificate::DupOSCertHandle(pos
->second
.cert_handle
);
154 // If the caller's handle was replaced with a cached handle, free the
155 // original handle now. This is done outside of the lock because
156 // |old_handle| may be the only handle for this particular certificate, so
157 // freeing it may be complex or resource-intensive and does not need to
158 // be guarded by the lock.
160 X509Certificate::FreeOSCertHandle(old_handle
);
161 DHISTOGRAM_COUNTS("X509CertificateReuseCount", 1);
165 void X509CertificateCache::Remove(X509Certificate::OSCertHandle cert_handle
) {
166 SHA1HashValue fingerprint
=
167 X509Certificate::CalculateFingerprint(cert_handle
);
168 base::AutoLock
lock(lock_
);
170 CertMap::iterator pos
= cache_
.find(fingerprint
);
171 if (pos
== cache_
.end())
172 return; // A hash collision where the winning cert was already freed.
174 bool is_same_cert
= X509Certificate::IsSameOSCert(cert_handle
,
175 pos
->second
.cert_handle
);
177 return; // A hash collision where the winning cert is still around.
179 if (--pos
->second
.ref_count
== 0) {
180 // The last reference to |cert_handle| has been removed, so release the
181 // Entry's OS handle and remove the Entry. The caller still holds a
182 // reference to |cert_handle| and is responsible for freeing it.
183 X509Certificate::FreeOSCertHandle(pos
->second
.cert_handle
);
187 #endif // !defined(USE_NSS)
189 // See X509CertificateCache::InsertOrUpdate. NSS has a built-in cache, so there
190 // is no point in wrapping another cache around it.
191 void InsertOrUpdateCache(X509Certificate::OSCertHandle
* cert_handle
) {
192 #if !defined(USE_NSS)
193 g_x509_certificate_cache
.Pointer()->InsertOrUpdate(cert_handle
);
197 // See X509CertificateCache::Remove.
198 void RemoveFromCache(X509Certificate::OSCertHandle cert_handle
) {
199 #if !defined(USE_NSS)
200 g_x509_certificate_cache
.Pointer()->Remove(cert_handle
);
204 // Utility to split |src| on the first occurrence of |c|, if any. |right| will
205 // either be empty if |c| was not found, or will contain the remainder of the
206 // string including the split character itself.
207 void SplitOnChar(const base::StringPiece
& src
,
209 base::StringPiece
* left
,
210 base::StringPiece
* right
) {
211 size_t pos
= src
.find(c
);
212 if (pos
== base::StringPiece::npos
) {
216 *left
= src
.substr(0, pos
);
217 *right
= src
.substr(pos
);
223 bool X509Certificate::LessThan::operator()(
224 const scoped_refptr
<X509Certificate
>& lhs
,
225 const scoped_refptr
<X509Certificate
>& rhs
) const {
226 if (lhs
.get() == rhs
.get())
229 int rv
= memcmp(lhs
->fingerprint_
.data
, rhs
->fingerprint_
.data
,
230 sizeof(lhs
->fingerprint_
.data
));
234 rv
= memcmp(lhs
->ca_fingerprint_
.data
, rhs
->ca_fingerprint_
.data
,
235 sizeof(lhs
->ca_fingerprint_
.data
));
239 X509Certificate::X509Certificate(const std::string
& subject
,
240 const std::string
& issuer
,
241 base::Time start_date
,
242 base::Time expiration_date
)
245 valid_start_(start_date
),
246 valid_expiry_(expiration_date
),
248 memset(fingerprint_
.data
, 0, sizeof(fingerprint_
.data
));
249 memset(ca_fingerprint_
.data
, 0, sizeof(ca_fingerprint_
.data
));
253 X509Certificate
* X509Certificate::CreateFromHandle(
254 OSCertHandle cert_handle
,
255 const OSCertHandles
& intermediates
) {
257 return new X509Certificate(cert_handle
, intermediates
);
261 X509Certificate
* X509Certificate::CreateFromDERCertChain(
262 const std::vector
<base::StringPiece
>& der_certs
) {
263 if (der_certs
.empty())
266 X509Certificate::OSCertHandles intermediate_ca_certs
;
267 for (size_t i
= 1; i
< der_certs
.size(); i
++) {
268 OSCertHandle handle
= CreateOSCertHandleFromBytes(
269 const_cast<char*>(der_certs
[i
].data()), der_certs
[i
].size());
272 intermediate_ca_certs
.push_back(handle
);
275 OSCertHandle handle
= NULL
;
276 // Return NULL if we failed to parse any of the certs.
277 if (der_certs
.size() - 1 == intermediate_ca_certs
.size()) {
278 handle
= CreateOSCertHandleFromBytes(
279 const_cast<char*>(der_certs
[0].data()), der_certs
[0].size());
282 X509Certificate
* cert
= NULL
;
284 cert
= CreateFromHandle(handle
, intermediate_ca_certs
);
285 FreeOSCertHandle(handle
);
288 for (size_t i
= 0; i
< intermediate_ca_certs
.size(); i
++)
289 FreeOSCertHandle(intermediate_ca_certs
[i
]);
295 X509Certificate
* X509Certificate::CreateFromBytes(const char* data
,
297 OSCertHandle cert_handle
= CreateOSCertHandleFromBytes(data
, length
);
301 X509Certificate
* cert
= CreateFromHandle(cert_handle
, OSCertHandles());
302 FreeOSCertHandle(cert_handle
);
307 X509Certificate
* X509Certificate::CreateFromPickle(const Pickle
& pickle
,
308 PickleIterator
* pickle_iter
,
310 if (type
== PICKLETYPE_CERTIFICATE_CHAIN_V3
) {
311 int chain_length
= 0;
312 if (!pickle_iter
->ReadLength(&chain_length
))
315 std::vector
<base::StringPiece
> cert_chain
;
316 const char* data
= NULL
;
318 for (int i
= 0; i
< chain_length
; ++i
) {
319 if (!pickle_iter
->ReadData(&data
, &data_length
))
321 cert_chain
.push_back(base::StringPiece(data
, data_length
));
323 return CreateFromDERCertChain(cert_chain
);
326 // Legacy / Migration code. This should eventually be removed once
327 // sufficient time has passed that all pickles serialized prior to
328 // PICKLETYPE_CERTIFICATE_CHAIN_V3 have been removed.
329 OSCertHandle cert_handle
= ReadOSCertHandleFromPickle(pickle_iter
);
333 OSCertHandles intermediates
;
334 uint32 num_intermediates
= 0;
335 if (type
!= PICKLETYPE_SINGLE_CERTIFICATE
) {
336 if (!pickle_iter
->ReadUInt32(&num_intermediates
)) {
337 FreeOSCertHandle(cert_handle
);
341 #if defined(OS_POSIX) && !defined(OS_MACOSX) && defined(__x86_64__)
342 // On 64-bit Linux (and any other 64-bit platforms), the intermediate count
343 // might really be a 64-bit field since we used to use Pickle::WriteSize(),
344 // which writes either 32 or 64 bits depending on the architecture. Since
345 // x86-64 is little-endian, if that happens, the next 32 bits will be all
346 // zeroes (the high bits) and the 32 bits we already read above are the
347 // correct value (we assume there are never more than 2^32 - 1 intermediate
348 // certificates in a chain; in practice, more than a dozen or so is
349 // basically unheard of). Since it's invalid for a certificate to start with
350 // 32 bits of zeroes, we check for that here and skip it if we find it. We
351 // save a copy of the pickle iterator to restore in case we don't get 32
352 // bits of zeroes. Now we always write 32 bits, so after a while, these old
353 // cached pickles will all get replaced.
354 // TODO(mdm): remove this compatibility code in April 2013 or so.
355 PickleIterator saved_iter
= *pickle_iter
;
356 uint32 zero_check
= 0;
357 if (!pickle_iter
->ReadUInt32(&zero_check
)) {
358 // This may not be an error. If there are no intermediates, and we're
359 // reading an old 32-bit pickle, and there's nothing else after this in
360 // the pickle, we should report success. Note that it is technically
361 // possible for us to skip over zeroes that should have occurred after
362 // an empty certificate list; to avoid this going forward, only do this
363 // backward-compatibility stuff for PICKLETYPE_CERTIFICATE_CHAIN_V1
364 // which comes from the pickle version number in http_response_info.cc.
365 if (num_intermediates
) {
366 FreeOSCertHandle(cert_handle
);
371 *pickle_iter
= saved_iter
;
372 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && defined(__x86_64__)
374 for (uint32 i
= 0; i
< num_intermediates
; ++i
) {
375 OSCertHandle intermediate
= ReadOSCertHandleFromPickle(pickle_iter
);
378 intermediates
.push_back(intermediate
);
382 X509Certificate
* cert
= NULL
;
383 if (intermediates
.size() == num_intermediates
)
384 cert
= CreateFromHandle(cert_handle
, intermediates
);
385 FreeOSCertHandle(cert_handle
);
386 for (size_t i
= 0; i
< intermediates
.size(); ++i
)
387 FreeOSCertHandle(intermediates
[i
]);
393 CertificateList
X509Certificate::CreateCertificateListFromBytes(
394 const char* data
, int length
, int format
) {
395 OSCertHandles certificates
;
397 // Check to see if it is in a PEM-encoded form. This check is performed
398 // first, as both OS X and NSS will both try to convert if they detect
399 // PEM encoding, except they don't do it consistently between the two.
400 base::StringPiece
data_string(data
, length
);
401 std::vector
<std::string
> pem_headers
;
403 // To maintain compatibility with NSS/Firefox, CERTIFICATE is a universally
404 // valid PEM block header for any format.
405 pem_headers
.push_back(kCertificateHeader
);
406 if (format
& FORMAT_PKCS7
)
407 pem_headers
.push_back(kPKCS7Header
);
409 PEMTokenizer
pem_tok(data_string
, pem_headers
);
410 while (pem_tok
.GetNext()) {
411 std::string
decoded(pem_tok
.data());
413 OSCertHandle handle
= NULL
;
414 if (format
& FORMAT_PEM_CERT_SEQUENCE
)
415 handle
= CreateOSCertHandleFromBytes(decoded
.c_str(), decoded
.size());
416 if (handle
!= NULL
) {
417 // Parsed a DER encoded certificate. All PEM blocks that follow must
418 // also be DER encoded certificates wrapped inside of PEM blocks.
419 format
= FORMAT_PEM_CERT_SEQUENCE
;
420 certificates
.push_back(handle
);
424 // If the first block failed to parse as a DER certificate, and
425 // formats other than PEM are acceptable, check to see if the decoded
426 // data is one of the accepted formats.
427 if (format
& ~FORMAT_PEM_CERT_SEQUENCE
) {
428 for (size_t i
= 0; certificates
.empty() &&
429 i
< arraysize(kFormatDecodePriority
); ++i
) {
430 if (format
& kFormatDecodePriority
[i
]) {
431 certificates
= CreateOSCertHandlesFromBytes(decoded
.c_str(),
432 decoded
.size(), kFormatDecodePriority
[i
]);
437 // Stop parsing after the first block for any format but a sequence of
438 // PEM-encoded DER certificates. The case of FORMAT_PEM_CERT_SEQUENCE
439 // is handled above, and continues processing until a certificate fails
444 // Try each of the formats, in order of parse preference, to see if |data|
445 // contains the binary representation of a Format, if it failed to parse
446 // as a PEM certificate/chain.
447 for (size_t i
= 0; certificates
.empty() &&
448 i
< arraysize(kFormatDecodePriority
); ++i
) {
449 if (format
& kFormatDecodePriority
[i
])
450 certificates
= CreateOSCertHandlesFromBytes(data
, length
,
451 kFormatDecodePriority
[i
]);
454 CertificateList results
;
455 // No certificates parsed.
456 if (certificates
.empty())
459 for (OSCertHandles::iterator it
= certificates
.begin();
460 it
!= certificates
.end(); ++it
) {
461 X509Certificate
* result
= CreateFromHandle(*it
, OSCertHandles());
462 results
.push_back(scoped_refptr
<X509Certificate
>(result
));
463 FreeOSCertHandle(*it
);
469 void X509Certificate::Persist(Pickle
* pickle
) {
470 DCHECK(cert_handle_
);
471 // This would be an absolutely insane number of intermediates.
472 if (intermediate_ca_certs_
.size() > static_cast<size_t>(INT_MAX
) - 1) {
476 if (!pickle
->WriteInt(
477 static_cast<int>(intermediate_ca_certs_
.size() + 1)) ||
478 !WriteOSCertHandleToPickle(cert_handle_
, pickle
)) {
482 for (size_t i
= 0; i
< intermediate_ca_certs_
.size(); ++i
) {
483 if (!WriteOSCertHandleToPickle(intermediate_ca_certs_
[i
], pickle
)) {
490 void X509Certificate::GetDNSNames(std::vector
<std::string
>* dns_names
) const {
491 GetSubjectAltName(dns_names
, NULL
);
492 if (dns_names
->empty())
493 dns_names
->push_back(subject_
.common_name
);
496 bool X509Certificate::HasExpired() const {
497 return base::Time::Now() > valid_expiry();
500 bool X509Certificate::Equals(const X509Certificate
* other
) const {
501 return IsSameOSCert(cert_handle_
, other
->cert_handle_
);
505 bool X509Certificate::VerifyHostname(
506 const std::string
& hostname
,
507 const std::string
& cert_common_name
,
508 const std::vector
<std::string
>& cert_san_dns_names
,
509 const std::vector
<std::string
>& cert_san_ip_addrs
,
510 bool* common_name_fallback_used
) {
511 DCHECK(!hostname
.empty());
512 // Perform name verification following http://tools.ietf.org/html/rfc6125.
513 // The terminology used in this method is as per that RFC:-
514 // Reference identifier == the host the local user/agent is intending to
515 // access, i.e. the thing displayed in the URL bar.
516 // Presented identifier(s) == name(s) the server knows itself as, in its cert.
518 // CanonicalizeHost requires surrounding brackets to parse an IPv6 address.
519 const std::string host_or_ip
= hostname
.find(':') != std::string::npos
?
520 "[" + hostname
+ "]" : hostname
;
521 url::CanonHostInfo host_info
;
522 std::string reference_name
= CanonicalizeHost(host_or_ip
, &host_info
);
523 // CanonicalizeHost does not normalize absolute vs relative DNS names. If
524 // the input name was absolute (included trailing .), normalize it as if it
526 if (!reference_name
.empty() && *reference_name
.rbegin() == '.')
527 reference_name
.resize(reference_name
.size() - 1);
528 if (reference_name
.empty())
531 // Allow fallback to Common name matching?
532 const bool common_name_fallback
= cert_san_dns_names
.empty() &&
533 cert_san_ip_addrs
.empty();
534 *common_name_fallback_used
= common_name_fallback
;
536 // Fully handle all cases where |hostname| contains an IP address.
537 if (host_info
.IsIPAddress()) {
538 if (common_name_fallback
&& host_info
.family
== url::CanonHostInfo::IPV4
) {
539 // Fallback to Common name matching. As this is deprecated and only
540 // supported for compatibility refuse it for IPv6 addresses.
541 return reference_name
== cert_common_name
;
543 base::StringPiece
ip_addr_string(
544 reinterpret_cast<const char*>(host_info
.address
),
545 host_info
.AddressLength());
546 return std::find(cert_san_ip_addrs
.begin(), cert_san_ip_addrs
.end(),
547 ip_addr_string
) != cert_san_ip_addrs
.end();
550 // |reference_domain| is the remainder of |host| after the leading host
551 // component is stripped off, but includes the leading dot e.g.
552 // "www.f.com" -> ".f.com".
553 // If there is no meaningful domain part to |host| (e.g. it contains no dots)
554 // then |reference_domain| will be empty.
555 base::StringPiece reference_host
, reference_domain
;
556 SplitOnChar(reference_name
, '.', &reference_host
, &reference_domain
);
557 bool allow_wildcards
= false;
558 if (!reference_domain
.empty()) {
559 DCHECK(reference_domain
.starts_with("."));
561 // Do not allow wildcards for public/ICANN registry controlled domains -
562 // that is, prevent *.com or *.co.uk as valid presented names, but do not
563 // prevent *.appspot.com (a private registry controlled domain).
564 // In addition, unknown top-level domains (such as 'intranet' domains or
565 // new TLDs/gTLDs not yet added to the registry controlled domain dataset)
566 // are also implicitly prevented.
567 // Because |reference_domain| must contain at least one name component that
568 // is not registry controlled, this ensures that all reference domains
569 // contain at least three domain components when using wildcards.
570 size_t registry_length
=
571 registry_controlled_domains::GetRegistryLength(
573 registry_controlled_domains::INCLUDE_UNKNOWN_REGISTRIES
,
574 registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
576 // Because |reference_name| was already canonicalized, the following
577 // should never happen.
578 CHECK_NE(std::string::npos
, registry_length
);
580 // Account for the leading dot in |reference_domain|.
581 bool is_registry_controlled
=
582 registry_length
!= 0 &&
583 registry_length
== (reference_domain
.size() - 1);
585 // Additionally, do not attempt wildcard matching for purely numeric
588 !is_registry_controlled
&&
589 reference_name
.find_first_not_of("0123456789.") != std::string::npos
;
592 // Now step through the DNS names doing wild card comparison (if necessary)
593 // on each against the reference name. If subjectAltName is empty, then
594 // fallback to use the common name instead.
595 std::vector
<std::string
> common_name_as_vector
;
596 const std::vector
<std::string
>* presented_names
= &cert_san_dns_names
;
597 if (common_name_fallback
) {
598 // Note: there's a small possibility cert_common_name is an international
599 // domain name in non-standard encoding (e.g. UTF8String or BMPString
600 // instead of A-label). As common name fallback is deprecated we're not
601 // doing anything specific to deal with this.
602 common_name_as_vector
.push_back(cert_common_name
);
603 presented_names
= &common_name_as_vector
;
605 for (std::vector
<std::string
>::const_iterator it
=
606 presented_names
->begin();
607 it
!= presented_names
->end(); ++it
) {
608 // Catch badly corrupt cert names up front.
609 if (it
->empty() || it
->find('\0') != std::string::npos
) {
610 DVLOG(1) << "Bad name in cert: " << *it
;
613 std::string
presented_name(base::StringToLowerASCII(*it
));
615 // Remove trailing dot, if any.
616 if (*presented_name
.rbegin() == '.')
617 presented_name
.resize(presented_name
.length() - 1);
619 // The hostname must be at least as long as the cert name it is matching,
620 // as we require the wildcard (if present) to match at least one character.
621 if (presented_name
.length() > reference_name
.length())
624 base::StringPiece presented_host
, presented_domain
;
625 SplitOnChar(presented_name
, '.', &presented_host
, &presented_domain
);
627 if (presented_domain
!= reference_domain
)
630 base::StringPiece pattern_begin
, pattern_end
;
631 SplitOnChar(presented_host
, '*', &pattern_begin
, &pattern_end
);
633 if (pattern_end
.empty()) { // No '*' in the presented_host
634 if (presented_host
== reference_host
)
638 pattern_end
.remove_prefix(1); // move past the *
640 if (!allow_wildcards
)
643 // * must not match a substring of an IDN A label; just a whole fragment.
644 if (reference_host
.starts_with("xn--") &&
645 !(pattern_begin
.empty() && pattern_end
.empty()))
648 if (reference_host
.starts_with(pattern_begin
) &&
649 reference_host
.ends_with(pattern_end
))
655 bool X509Certificate::VerifyNameMatch(const std::string
& hostname
,
656 bool* common_name_fallback_used
) const {
657 std::vector
<std::string
> dns_names
, ip_addrs
;
658 GetSubjectAltName(&dns_names
, &ip_addrs
);
659 return VerifyHostname(hostname
, subject_
.common_name
, dns_names
, ip_addrs
,
660 common_name_fallback_used
);
664 bool X509Certificate::GetPEMEncodedFromDER(const std::string
& der_encoded
,
665 std::string
* pem_encoded
) {
666 if (der_encoded
.empty())
668 std::string b64_encoded
;
669 base::Base64Encode(der_encoded
, &b64_encoded
);
670 *pem_encoded
= "-----BEGIN CERTIFICATE-----\n";
672 // Divide the Base-64 encoded data into 64-character chunks, as per
673 // 4.3.2.4 of RFC 1421.
674 static const size_t kChunkSize
= 64;
675 size_t chunks
= (b64_encoded
.size() + (kChunkSize
- 1)) / kChunkSize
;
676 for (size_t i
= 0, chunk_offset
= 0; i
< chunks
;
677 ++i
, chunk_offset
+= kChunkSize
) {
678 pem_encoded
->append(b64_encoded
, chunk_offset
, kChunkSize
);
679 pem_encoded
->append("\n");
681 pem_encoded
->append("-----END CERTIFICATE-----\n");
686 bool X509Certificate::GetPEMEncoded(OSCertHandle cert_handle
,
687 std::string
* pem_encoded
) {
688 std::string der_encoded
;
689 if (!GetDEREncoded(cert_handle
, &der_encoded
))
691 return GetPEMEncodedFromDER(der_encoded
, pem_encoded
);
694 bool X509Certificate::GetPEMEncodedChain(
695 std::vector
<std::string
>* pem_encoded
) const {
696 std::vector
<std::string
> encoded_chain
;
697 std::string pem_data
;
698 if (!GetPEMEncoded(os_cert_handle(), &pem_data
))
700 encoded_chain
.push_back(pem_data
);
701 for (size_t i
= 0; i
< intermediate_ca_certs_
.size(); ++i
) {
702 if (!GetPEMEncoded(intermediate_ca_certs_
[i
], &pem_data
))
704 encoded_chain
.push_back(pem_data
);
706 pem_encoded
->swap(encoded_chain
);
711 SHA256HashValue
X509Certificate::CalculateCAFingerprint256(
712 const OSCertHandles
& intermediates
) {
713 SHA256HashValue sha256
;
714 memset(sha256
.data
, 0, sizeof(sha256
.data
));
716 scoped_ptr
<crypto::SecureHash
> hash(
717 crypto::SecureHash::Create(crypto::SecureHash::SHA256
));
719 for (size_t i
= 0; i
< intermediates
.size(); ++i
) {
720 std::string der_encoded
;
721 if (!GetDEREncoded(intermediates
[i
], &der_encoded
))
723 hash
->Update(der_encoded
.data(), der_encoded
.length());
725 hash
->Finish(sha256
.data
, sizeof(sha256
.data
));
731 SHA256HashValue
X509Certificate::CalculateChainFingerprint256(
733 const OSCertHandles
& intermediates
) {
735 chain
.push_back(leaf
);
736 chain
.insert(chain
.end(), intermediates
.begin(), intermediates
.end());
738 return CalculateCAFingerprint256(chain
);
741 X509Certificate::X509Certificate(OSCertHandle cert_handle
,
742 const OSCertHandles
& intermediates
)
743 : cert_handle_(DupOSCertHandle(cert_handle
)) {
744 InsertOrUpdateCache(&cert_handle_
);
745 for (size_t i
= 0; i
< intermediates
.size(); ++i
) {
746 // Duplicate the incoming certificate, as the caller retains ownership
747 // of |intermediates|.
748 OSCertHandle intermediate
= DupOSCertHandle(intermediates
[i
]);
749 // Update the cache, which will assume ownership of the duplicated
750 // handle and return a suitable equivalent, potentially from the cache.
751 InsertOrUpdateCache(&intermediate
);
752 intermediate_ca_certs_
.push_back(intermediate
);
754 // Platform-specific initialization.
758 X509Certificate::~X509Certificate() {
760 RemoveFromCache(cert_handle_
);
761 FreeOSCertHandle(cert_handle_
);
763 for (size_t i
= 0; i
< intermediate_ca_certs_
.size(); ++i
) {
764 RemoveFromCache(intermediate_ca_certs_
[i
]);
765 FreeOSCertHandle(intermediate_ca_certs_
[i
]);