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/http/transport_security_state.h"
7 #if defined(USE_OPENSSL)
8 #include <openssl/ecdsa.h>
9 #include <openssl/ssl.h>
10 #else // !defined(USE_OPENSSL)
20 #include "base/base64.h"
21 #include "base/build_time.h"
22 #include "base/logging.h"
23 #include "base/memory/scoped_ptr.h"
24 #include "base/metrics/histogram.h"
25 #include "base/sha1.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_util.h"
28 #include "base/strings/utf_string_conversions.h"
29 #include "base/time/time.h"
30 #include "base/values.h"
31 #include "crypto/sha2.h"
32 #include "net/base/dns_util.h"
33 #include "net/cert/x509_cert_types.h"
34 #include "net/cert/x509_certificate.h"
35 #include "net/http/http_security_headers.h"
36 #include "net/ssl/ssl_info.h"
39 #if defined(USE_OPENSSL)
40 #include "crypto/openssl_util.h"
47 std::string
HashesToBase64String(const HashValueVector
& hashes
) {
49 for (size_t i
= 0; i
!= hashes
.size(); ++i
) {
52 str
+= hashes
[i
].ToString();
57 std::string
HashHost(const std::string
& canonicalized_host
) {
58 char hashed
[crypto::kSHA256Length
];
59 crypto::SHA256HashString(canonicalized_host
, hashed
, sizeof(hashed
));
60 return std::string(hashed
, sizeof(hashed
));
63 // Returns true if the intersection of |a| and |b| is not empty. If either
64 // |a| or |b| is empty, returns false.
65 bool HashesIntersect(const HashValueVector
& a
,
66 const HashValueVector
& b
) {
67 for (HashValueVector::const_iterator i
= a
.begin(); i
!= a
.end(); ++i
) {
68 HashValueVector::const_iterator j
=
69 std::find_if(b
.begin(), b
.end(), HashValuesEqual(*i
));
76 bool AddHash(const char* sha1_hash
,
77 HashValueVector
* out
) {
78 HashValue
hash(HASH_VALUE_SHA1
);
79 memcpy(hash
.data(), sha1_hash
, hash
.size());
86 TransportSecurityState::TransportSecurityState()
88 DCHECK(CalledOnValidThread());
91 TransportSecurityState::Iterator::Iterator(const TransportSecurityState
& state
)
92 : iterator_(state
.enabled_hosts_
.begin()),
93 end_(state
.enabled_hosts_
.end()) {
96 TransportSecurityState::Iterator::~Iterator() {}
98 bool TransportSecurityState::ShouldSSLErrorsBeFatal(const std::string
& host
,
101 if (GetStaticDomainState(host
, sni_enabled
, &state
))
103 return GetDynamicDomainState(host
, &state
);
106 bool TransportSecurityState::ShouldUpgradeToSSL(const std::string
& host
,
108 DomainState dynamic_state
;
109 if (GetDynamicDomainState(host
, &dynamic_state
))
110 return dynamic_state
.ShouldUpgradeToSSL();
112 DomainState static_state
;
113 if (GetStaticDomainState(host
, sni_enabled
, &static_state
) &&
114 static_state
.ShouldUpgradeToSSL()) {
121 bool TransportSecurityState::CheckPublicKeyPins(const std::string
& host
,
123 const HashValueVector
& hashes
,
124 std::string
* failure_log
) {
125 DomainState dynamic_state
;
126 if (GetDynamicDomainState(host
, &dynamic_state
))
127 return dynamic_state
.CheckPublicKeyPins(hashes
, failure_log
);
129 DomainState static_state
;
130 if (GetStaticDomainState(host
, sni_enabled
, &static_state
) &&
131 static_state
.CheckPublicKeyPins(hashes
, failure_log
)) {
138 bool TransportSecurityState::HasPublicKeyPins(const std::string
& host
,
140 DomainState dynamic_state
;
141 if (GetDynamicDomainState(host
, &dynamic_state
))
142 return dynamic_state
.HasPublicKeyPins();
144 DomainState static_state
;
145 if (GetStaticDomainState(host
, sni_enabled
, &static_state
)) {
146 if (static_state
.HasPublicKeyPins())
153 void TransportSecurityState::SetDelegate(
154 TransportSecurityState::Delegate
* delegate
) {
155 DCHECK(CalledOnValidThread());
156 delegate_
= delegate
;
159 void TransportSecurityState::EnableHost(const std::string
& host
,
160 const DomainState
& state
) {
161 DCHECK(CalledOnValidThread());
163 const std::string canonicalized_host
= CanonicalizeHost(host
);
164 if (canonicalized_host
.empty())
167 DomainState
state_copy(state
);
168 // No need to store this value since it is redundant. (|canonicalized_host|
170 state_copy
.domain
.clear();
172 enabled_hosts_
[HashHost(canonicalized_host
)] = state_copy
;
176 bool TransportSecurityState::DeleteDynamicDataForHost(const std::string
& host
) {
177 DCHECK(CalledOnValidThread());
179 const std::string canonicalized_host
= CanonicalizeHost(host
);
180 if (canonicalized_host
.empty())
183 DomainStateMap::iterator i
= enabled_hosts_
.find(
184 HashHost(canonicalized_host
));
185 if (i
!= enabled_hosts_
.end()) {
186 enabled_hosts_
.erase(i
);
193 void TransportSecurityState::ClearDynamicData() {
194 DCHECK(CalledOnValidThread());
195 enabled_hosts_
.clear();
198 void TransportSecurityState::DeleteAllDynamicDataSince(const base::Time
& time
) {
199 DCHECK(CalledOnValidThread());
201 bool dirtied
= false;
202 DomainStateMap::iterator i
= enabled_hosts_
.begin();
203 while (i
!= enabled_hosts_
.end()) {
204 if (i
->second
.sts
.last_observed
>= time
&&
205 i
->second
.pkp
.last_observed
>= time
) {
207 enabled_hosts_
.erase(i
++);
211 if (i
->second
.sts
.last_observed
>= time
) {
213 i
->second
.sts
.upgrade_mode
= DomainState::MODE_DEFAULT
;
214 } else if (i
->second
.pkp
.last_observed
>= time
) {
216 i
->second
.pkp
.spki_hashes
.clear();
217 i
->second
.pkp
.expiry
= base::Time();
226 TransportSecurityState::~TransportSecurityState() {
227 DCHECK(CalledOnValidThread());
230 void TransportSecurityState::DirtyNotify() {
231 DCHECK(CalledOnValidThread());
234 delegate_
->StateIsDirty(this);
238 std::string
TransportSecurityState::CanonicalizeHost(const std::string
& host
) {
239 // We cannot perform the operations as detailed in the spec here as |host|
240 // has already undergone IDN processing before it reached us. Thus, we check
241 // that there are no invalid characters in the host and lowercase the result.
243 std::string new_host
;
244 if (!DNSDomainFromDot(host
, &new_host
)) {
245 // DNSDomainFromDot can fail if any label is > 63 bytes or if the whole
246 // name is >255 bytes. However, search terms can have those properties.
247 return std::string();
250 for (size_t i
= 0; new_host
[i
]; i
+= new_host
[i
] + 1) {
251 const unsigned label_length
= static_cast<unsigned>(new_host
[i
]);
255 for (size_t j
= 0; j
< label_length
; ++j
) {
256 new_host
[i
+ 1 + j
] = tolower(new_host
[i
+ 1 + j
]);
263 // |ReportUMAOnPinFailure| uses these to report which domain was associated
264 // with the public key pinning failure.
266 // DO NOT CHANGE THE ORDERING OF THESE NAMES OR REMOVE ANY OF THEM. Add new
267 // domains at the END of the listing (but before DOMAIN_NUM_EVENTS).
268 enum SecondLevelDomainName
{
273 DOMAIN_GOOGLE_ANALYTICS_COM
,
274 DOMAIN_GOOGLEPLEX_COM
,
276 DOMAIN_GOOGLEUSERCONTENT_COM
,
278 DOMAIN_GOOGLEAPIS_COM
,
279 DOMAIN_GOOGLEADSERVICES_COM
,
280 DOMAIN_GOOGLECODE_COM
,
282 DOMAIN_GOOGLESYNDICATION_COM
,
283 DOMAIN_DOUBLECLICK_NET
,
286 DOMAIN_GOOGLEMAIL_COM
,
287 DOMAIN_GOOGLEGROUPS_COM
,
289 DOMAIN_TORPROJECT_ORG
,
299 DOMAIN_GOOGLECOMMERCE_COM
,
526 DOMAIN_GOOGLETAGMANAGER_COM
,
527 DOMAIN_GOOGLETAGSERVICES_COM
,
531 // Boundary value for UMA_HISTOGRAM_ENUMERATION:
535 // PublicKeyPins contains a number of SubjectPublicKeyInfo hashes for a site.
536 // The validated certificate chain for the site must not include any of
537 // |excluded_hashes| and must include one or more of |required_hashes|.
538 struct PublicKeyPins
{
539 const char* const* required_hashes
;
540 const char* const* excluded_hashes
;
545 bool include_subdomains
;
549 SecondLevelDomainName second_level_domain_name
;
552 static bool HasPreload(const struct HSTSPreload
* entries
, size_t num_entries
,
553 const std::string
& canonicalized_host
, size_t i
,
554 TransportSecurityState::DomainState
* out
, bool* ret
) {
555 for (size_t j
= 0; j
< num_entries
; j
++) {
556 if (entries
[j
].length
== canonicalized_host
.size() - i
&&
557 memcmp(entries
[j
].dns_name
, &canonicalized_host
[i
],
558 entries
[j
].length
) == 0) {
559 if (!entries
[j
].include_subdomains
&& i
!= 0) {
562 out
->sts
.include_subdomains
= entries
[j
].include_subdomains
;
563 out
->sts
.last_observed
= base::GetBuildTime();
564 out
->pkp
.include_subdomains
= entries
[j
].include_subdomains
;
565 out
->pkp
.last_observed
= base::GetBuildTime();
567 out
->sts
.upgrade_mode
=
568 TransportSecurityState::DomainState::MODE_FORCE_HTTPS
;
569 if (!entries
[j
].https_required
)
570 out
->sts
.upgrade_mode
=
571 TransportSecurityState::DomainState::MODE_DEFAULT
;
572 if (entries
[j
].pins
.required_hashes
) {
573 const char* const* sha1_hash
= entries
[j
].pins
.required_hashes
;
575 AddHash(*sha1_hash
, &out
->pkp
.spki_hashes
);
579 if (entries
[j
].pins
.excluded_hashes
) {
580 const char* const* sha1_hash
= entries
[j
].pins
.excluded_hashes
;
582 AddHash(*sha1_hash
, &out
->pkp
.bad_spki_hashes
);
593 #include "net/http/transport_security_state_static.h"
595 // Returns the HSTSPreload entry for the |canonicalized_host| in |entries|,
596 // or NULL if there is none. Prefers exact hostname matches to those that
597 // match only because HSTSPreload.include_subdomains is true.
599 // |canonicalized_host| should be the hostname as canonicalized by
601 static const struct HSTSPreload
* GetHSTSPreload(
602 const std::string
& canonicalized_host
,
603 const struct HSTSPreload
* entries
,
604 size_t num_entries
) {
605 for (size_t i
= 0; canonicalized_host
[i
]; i
+= canonicalized_host
[i
] + 1) {
606 for (size_t j
= 0; j
< num_entries
; j
++) {
607 const struct HSTSPreload
* entry
= entries
+ j
;
609 if (i
!= 0 && !entry
->include_subdomains
)
612 if (entry
->length
== canonicalized_host
.size() - i
&&
613 memcmp(entry
->dns_name
, &canonicalized_host
[i
], entry
->length
) == 0) {
622 bool TransportSecurityState::AddHSTSHeader(const std::string
& host
,
623 const std::string
& value
) {
624 DCHECK(CalledOnValidThread());
626 base::Time now
= base::Time::Now();
627 base::TimeDelta max_age
;
628 TransportSecurityState::DomainState domain_state
;
629 GetDynamicDomainState(host
, &domain_state
);
630 if (ParseHSTSHeader(value
, &max_age
, &domain_state
.sts
.include_subdomains
)) {
631 // Handle max-age == 0.
632 if (max_age
.InSeconds() == 0)
633 domain_state
.sts
.upgrade_mode
= DomainState::MODE_DEFAULT
;
635 domain_state
.sts
.upgrade_mode
= DomainState::MODE_FORCE_HTTPS
;
636 domain_state
.sts
.last_observed
= now
;
637 domain_state
.sts
.expiry
= now
+ max_age
;
638 EnableHost(host
, domain_state
);
644 bool TransportSecurityState::AddHPKPHeader(const std::string
& host
,
645 const std::string
& value
,
646 const SSLInfo
& ssl_info
) {
647 DCHECK(CalledOnValidThread());
649 base::Time now
= base::Time::Now();
650 base::TimeDelta max_age
;
651 TransportSecurityState::DomainState domain_state
;
652 GetDynamicDomainState(host
, &domain_state
);
653 if (ParseHPKPHeader(value
,
654 ssl_info
.public_key_hashes
,
656 &domain_state
.pkp
.include_subdomains
,
657 &domain_state
.pkp
.spki_hashes
)) {
658 // Handle max-age == 0.
659 if (max_age
.InSeconds() == 0)
660 domain_state
.pkp
.spki_hashes
.clear();
661 domain_state
.pkp
.last_observed
= now
;
662 domain_state
.pkp
.expiry
= now
+ max_age
;
663 EnableHost(host
, domain_state
);
669 bool TransportSecurityState::AddHSTS(const std::string
& host
,
670 const base::Time
& expiry
,
671 bool include_subdomains
) {
672 DCHECK(CalledOnValidThread());
674 // Copy-and-modify the existing DomainState for this host (if any).
675 TransportSecurityState::DomainState domain_state
;
676 const std::string canonicalized_host
= CanonicalizeHost(host
);
677 const std::string hashed_host
= HashHost(canonicalized_host
);
678 DomainStateMap::const_iterator i
= enabled_hosts_
.find(
680 if (i
!= enabled_hosts_
.end())
681 domain_state
= i
->second
;
683 domain_state
.sts
.last_observed
= base::Time::Now();
684 domain_state
.sts
.include_subdomains
= include_subdomains
;
685 domain_state
.sts
.expiry
= expiry
;
686 domain_state
.sts
.upgrade_mode
= DomainState::MODE_FORCE_HTTPS
;
687 EnableHost(host
, domain_state
);
691 bool TransportSecurityState::AddHPKP(const std::string
& host
,
692 const base::Time
& expiry
,
693 bool include_subdomains
,
694 const HashValueVector
& hashes
) {
695 DCHECK(CalledOnValidThread());
697 // Copy-and-modify the existing DomainState for this host (if any).
698 TransportSecurityState::DomainState domain_state
;
699 const std::string canonicalized_host
= CanonicalizeHost(host
);
700 const std::string hashed_host
= HashHost(canonicalized_host
);
701 DomainStateMap::const_iterator i
= enabled_hosts_
.find(
703 if (i
!= enabled_hosts_
.end())
704 domain_state
= i
->second
;
706 domain_state
.pkp
.last_observed
= base::Time::Now();
707 domain_state
.pkp
.include_subdomains
= include_subdomains
;
708 domain_state
.pkp
.expiry
= expiry
;
709 domain_state
.pkp
.spki_hashes
= hashes
;
710 EnableHost(host
, domain_state
);
715 bool TransportSecurityState::IsGooglePinnedProperty(const std::string
& host
,
717 std::string canonicalized_host
= CanonicalizeHost(host
);
718 const struct HSTSPreload
* entry
=
719 GetHSTSPreload(canonicalized_host
, kPreloadedSTS
, kNumPreloadedSTS
);
721 if (entry
&& entry
->pins
.required_hashes
== kGoogleAcceptableCerts
)
725 entry
= GetHSTSPreload(canonicalized_host
, kPreloadedSNISTS
,
726 kNumPreloadedSNISTS
);
727 if (entry
&& entry
->pins
.required_hashes
== kGoogleAcceptableCerts
)
735 void TransportSecurityState::ReportUMAOnPinFailure(const std::string
& host
) {
736 std::string canonicalized_host
= CanonicalizeHost(host
);
738 const struct HSTSPreload
* entry
=
739 GetHSTSPreload(canonicalized_host
, kPreloadedSTS
, kNumPreloadedSTS
);
742 entry
= GetHSTSPreload(canonicalized_host
, kPreloadedSNISTS
,
743 kNumPreloadedSNISTS
);
747 // We don't care to report pin failures for dynamic pins.
752 DCHECK(entry
->pins
.required_hashes
);
753 DCHECK(entry
->second_level_domain_name
!= DOMAIN_NOT_PINNED
);
755 UMA_HISTOGRAM_ENUMERATION("Net.PublicKeyPinFailureDomain",
756 entry
->second_level_domain_name
, DOMAIN_NUM_EVENTS
);
760 bool TransportSecurityState::IsBuildTimely() {
761 const base::Time build_time
= base::GetBuildTime();
762 // We consider built-in information to be timely for 10 weeks.
763 return (base::Time::Now() - build_time
).InDays() < 70 /* 10 weeks */;
766 bool TransportSecurityState::GetStaticDomainState(const std::string
& host
,
768 DomainState
* out
) const {
769 DCHECK(CalledOnValidThread());
771 const std::string canonicalized_host
= CanonicalizeHost(host
);
773 out
->sts
.upgrade_mode
= DomainState::MODE_FORCE_HTTPS
;
774 out
->sts
.include_subdomains
= false;
775 out
->pkp
.include_subdomains
= false;
777 const bool is_build_timely
= IsBuildTimely();
779 for (size_t i
= 0; canonicalized_host
[i
]; i
+= canonicalized_host
[i
] + 1) {
780 std::string
host_sub_chunk(&canonicalized_host
[i
],
781 canonicalized_host
.size() - i
);
782 out
->domain
= DNSDomainToString(host_sub_chunk
);
784 if (is_build_timely
&&
785 HasPreload(kPreloadedSTS
, kNumPreloadedSTS
, canonicalized_host
, i
, out
,
791 HasPreload(kPreloadedSNISTS
, kNumPreloadedSNISTS
, canonicalized_host
, i
,
800 bool TransportSecurityState::GetDynamicDomainState(const std::string
& host
,
801 DomainState
* result
) {
802 DCHECK(CalledOnValidThread());
805 const std::string canonicalized_host
= CanonicalizeHost(host
);
806 if (canonicalized_host
.empty())
809 base::Time
current_time(base::Time::Now());
811 for (size_t i
= 0; canonicalized_host
[i
]; i
+= canonicalized_host
[i
] + 1) {
812 std::string
host_sub_chunk(&canonicalized_host
[i
],
813 canonicalized_host
.size() - i
);
814 DomainStateMap::iterator j
=
815 enabled_hosts_
.find(HashHost(host_sub_chunk
));
816 if (j
== enabled_hosts_
.end())
819 if (current_time
> j
->second
.sts
.expiry
&&
820 current_time
> j
->second
.pkp
.expiry
) {
821 enabled_hosts_
.erase(j
);
827 state
.domain
= DNSDomainToString(host_sub_chunk
);
829 // Succeed if we matched the domain exactly or if subdomain matches are
831 if (i
== 0 || j
->second
.sts
.include_subdomains
||
832 j
->second
.pkp
.include_subdomains
) {
843 void TransportSecurityState::AddOrUpdateEnabledHosts(
844 const std::string
& hashed_host
, const DomainState
& state
) {
845 DCHECK(CalledOnValidThread());
846 enabled_hosts_
[hashed_host
] = state
;
849 TransportSecurityState::DomainState::DomainState() {
850 sts
.upgrade_mode
= MODE_DEFAULT
;
851 sts
.include_subdomains
= false;
852 pkp
.include_subdomains
= false;
855 TransportSecurityState::DomainState::~DomainState() {
858 bool TransportSecurityState::DomainState::CheckPublicKeyPins(
859 const HashValueVector
& hashes
, std::string
* failure_log
) const {
860 // Validate that hashes is not empty. By the time this code is called (in
861 // production), that should never happen, but it's good to be defensive.
862 // And, hashes *can* be empty in some test scenarios.
863 if (hashes
.empty()) {
865 "Rejecting empty public key chain for public-key-pinned domains: " +
870 if (HashesIntersect(pkp
.bad_spki_hashes
, hashes
)) {
871 failure_log
->append("Rejecting public key chain for domain " + domain
+
872 ". Validated chain: " + HashesToBase64String(hashes
) +
873 ", matches one or more bad hashes: " +
874 HashesToBase64String(pkp
.bad_spki_hashes
));
878 // If there are no pins, then any valid chain is acceptable.
879 if (pkp
.spki_hashes
.empty())
882 if (HashesIntersect(pkp
.spki_hashes
, hashes
)) {
886 failure_log
->append("Rejecting public key chain for domain " + domain
+
887 ". Validated chain: " + HashesToBase64String(hashes
) +
888 ", expected: " + HashesToBase64String(pkp
.spki_hashes
));
892 bool TransportSecurityState::DomainState::ShouldUpgradeToSSL() const {
893 return sts
.upgrade_mode
== MODE_FORCE_HTTPS
;
896 bool TransportSecurityState::DomainState::ShouldSSLErrorsBeFatal() const {
900 bool TransportSecurityState::DomainState::HasPublicKeyPins() const {
901 return pkp
.spki_hashes
.size() > 0 || pkp
.bad_spki_hashes
.size() > 0;
904 TransportSecurityState::DomainState::PKPState::PKPState() {
907 TransportSecurityState::DomainState::PKPState::~PKPState() {