Probably broke Win7 Tests (dbg)(6). http://build.chromium.org/p/chromium.win/builders...
[chromium-blink-merge.git] / net / http / transport_security_state.cc
blob0b209b356e834dc7018bb0d9a743711b3e02cff5
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)
11 #include <cryptohi.h>
12 #include <hasht.h>
13 #include <keyhi.h>
14 #include <nspr.h>
15 #include <pk11pub.h>
16 #endif
18 #include <algorithm>
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"
37 #include "url/gurl.h"
39 #if defined(USE_OPENSSL)
40 #include "crypto/openssl_util.h"
41 #endif
43 namespace net {
45 namespace {
47 std::string HashesToBase64String(const HashValueVector& hashes) {
48 std::string str;
49 for (size_t i = 0; i != hashes.size(); ++i) {
50 if (i != 0)
51 str += ",";
52 str += hashes[i].ToString();
54 return str;
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));
70 if (j != b.end())
71 return true;
73 return false;
76 bool AddHash(const char* sha1_hash,
77 HashValueVector* out) {
78 HashValue hash(HASH_VALUE_SHA1);
79 memcpy(hash.data(), sha1_hash, hash.size());
80 out->push_back(hash);
81 return true;
84 } // namespace
86 TransportSecurityState::TransportSecurityState()
87 : delegate_(NULL) {
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,
99 bool sni_enabled) {
100 DomainState state;
101 if (GetStaticDomainState(host, sni_enabled, &state))
102 return true;
103 return GetDynamicDomainState(host, &state);
106 bool TransportSecurityState::ShouldUpgradeToSSL(const std::string& host,
107 bool sni_enabled) {
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()) {
115 return true;
118 return false;
121 bool TransportSecurityState::CheckPublicKeyPins(const std::string& host,
122 bool sni_enabled,
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)) {
132 return true;
135 return false;
138 bool TransportSecurityState::HasPublicKeyPins(const std::string& host,
139 bool sni_enabled) {
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())
147 return true;
150 return false;
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())
165 return;
167 DomainState state_copy(state);
168 // No need to store this value since it is redundant. (|canonicalized_host|
169 // is the map key.)
170 state_copy.domain.clear();
172 enabled_hosts_[HashHost(canonicalized_host)] = state_copy;
173 DirtyNotify();
176 bool TransportSecurityState::DeleteDynamicDataForHost(const std::string& host) {
177 DCHECK(CalledOnValidThread());
179 const std::string canonicalized_host = CanonicalizeHost(host);
180 if (canonicalized_host.empty())
181 return false;
183 DomainStateMap::iterator i = enabled_hosts_.find(
184 HashHost(canonicalized_host));
185 if (i != enabled_hosts_.end()) {
186 enabled_hosts_.erase(i);
187 DirtyNotify();
188 return true;
190 return false;
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) {
206 dirtied = true;
207 enabled_hosts_.erase(i++);
208 continue;
211 if (i->second.sts.last_observed >= time) {
212 dirtied = true;
213 i->second.sts.upgrade_mode = DomainState::MODE_DEFAULT;
214 } else if (i->second.pkp.last_observed >= time) {
215 dirtied = true;
216 i->second.pkp.spki_hashes.clear();
217 i->second.pkp.expiry = base::Time();
219 ++i;
222 if (dirtied)
223 DirtyNotify();
226 TransportSecurityState::~TransportSecurityState() {
227 DCHECK(CalledOnValidThread());
230 void TransportSecurityState::DirtyNotify() {
231 DCHECK(CalledOnValidThread());
233 if (delegate_)
234 delegate_->StateIsDirty(this);
237 // static
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]);
252 if (!label_length)
253 break;
255 for (size_t j = 0; j < label_length; ++j) {
256 new_host[i + 1 + j] = tolower(new_host[i + 1 + j]);
260 return new_host;
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 {
269 DOMAIN_NOT_PINNED,
271 DOMAIN_GOOGLE_COM,
272 DOMAIN_ANDROID_COM,
273 DOMAIN_GOOGLE_ANALYTICS_COM,
274 DOMAIN_GOOGLEPLEX_COM,
275 DOMAIN_YTIMG_COM,
276 DOMAIN_GOOGLEUSERCONTENT_COM,
277 DOMAIN_YOUTUBE_COM,
278 DOMAIN_GOOGLEAPIS_COM,
279 DOMAIN_GOOGLEADSERVICES_COM,
280 DOMAIN_GOOGLECODE_COM,
281 DOMAIN_APPSPOT_COM,
282 DOMAIN_GOOGLESYNDICATION_COM,
283 DOMAIN_DOUBLECLICK_NET,
284 DOMAIN_GSTATIC_COM,
285 DOMAIN_GMAIL_COM,
286 DOMAIN_GOOGLEMAIL_COM,
287 DOMAIN_GOOGLEGROUPS_COM,
289 DOMAIN_TORPROJECT_ORG,
291 DOMAIN_TWITTER_COM,
292 DOMAIN_TWIMG_COM,
294 DOMAIN_AKAMAIHD_NET,
296 DOMAIN_TOR2WEB_ORG,
298 DOMAIN_YOUTU_BE,
299 DOMAIN_GOOGLECOMMERCE_COM,
300 DOMAIN_URCHIN_COM,
301 DOMAIN_GOO_GL,
302 DOMAIN_G_CO,
303 DOMAIN_GOOGLE_AC,
304 DOMAIN_GOOGLE_AD,
305 DOMAIN_GOOGLE_AE,
306 DOMAIN_GOOGLE_AF,
307 DOMAIN_GOOGLE_AG,
308 DOMAIN_GOOGLE_AM,
309 DOMAIN_GOOGLE_AS,
310 DOMAIN_GOOGLE_AT,
311 DOMAIN_GOOGLE_AZ,
312 DOMAIN_GOOGLE_BA,
313 DOMAIN_GOOGLE_BE,
314 DOMAIN_GOOGLE_BF,
315 DOMAIN_GOOGLE_BG,
316 DOMAIN_GOOGLE_BI,
317 DOMAIN_GOOGLE_BJ,
318 DOMAIN_GOOGLE_BS,
319 DOMAIN_GOOGLE_BY,
320 DOMAIN_GOOGLE_CA,
321 DOMAIN_GOOGLE_CAT,
322 DOMAIN_GOOGLE_CC,
323 DOMAIN_GOOGLE_CD,
324 DOMAIN_GOOGLE_CF,
325 DOMAIN_GOOGLE_CG,
326 DOMAIN_GOOGLE_CH,
327 DOMAIN_GOOGLE_CI,
328 DOMAIN_GOOGLE_CL,
329 DOMAIN_GOOGLE_CM,
330 DOMAIN_GOOGLE_CN,
331 DOMAIN_CO_AO,
332 DOMAIN_CO_BW,
333 DOMAIN_CO_CK,
334 DOMAIN_CO_CR,
335 DOMAIN_CO_HU,
336 DOMAIN_CO_ID,
337 DOMAIN_CO_IL,
338 DOMAIN_CO_IM,
339 DOMAIN_CO_IN,
340 DOMAIN_CO_JE,
341 DOMAIN_CO_JP,
342 DOMAIN_CO_KE,
343 DOMAIN_CO_KR,
344 DOMAIN_CO_LS,
345 DOMAIN_CO_MA,
346 DOMAIN_CO_MZ,
347 DOMAIN_CO_NZ,
348 DOMAIN_CO_TH,
349 DOMAIN_CO_TZ,
350 DOMAIN_CO_UG,
351 DOMAIN_CO_UK,
352 DOMAIN_CO_UZ,
353 DOMAIN_CO_VE,
354 DOMAIN_CO_VI,
355 DOMAIN_CO_ZA,
356 DOMAIN_CO_ZM,
357 DOMAIN_CO_ZW,
358 DOMAIN_COM_AF,
359 DOMAIN_COM_AG,
360 DOMAIN_COM_AI,
361 DOMAIN_COM_AR,
362 DOMAIN_COM_AU,
363 DOMAIN_COM_BD,
364 DOMAIN_COM_BH,
365 DOMAIN_COM_BN,
366 DOMAIN_COM_BO,
367 DOMAIN_COM_BR,
368 DOMAIN_COM_BY,
369 DOMAIN_COM_BZ,
370 DOMAIN_COM_CN,
371 DOMAIN_COM_CO,
372 DOMAIN_COM_CU,
373 DOMAIN_COM_CY,
374 DOMAIN_COM_DO,
375 DOMAIN_COM_EC,
376 DOMAIN_COM_EG,
377 DOMAIN_COM_ET,
378 DOMAIN_COM_FJ,
379 DOMAIN_COM_GE,
380 DOMAIN_COM_GH,
381 DOMAIN_COM_GI,
382 DOMAIN_COM_GR,
383 DOMAIN_COM_GT,
384 DOMAIN_COM_HK,
385 DOMAIN_COM_IQ,
386 DOMAIN_COM_JM,
387 DOMAIN_COM_JO,
388 DOMAIN_COM_KH,
389 DOMAIN_COM_KW,
390 DOMAIN_COM_LB,
391 DOMAIN_COM_LY,
392 DOMAIN_COM_MT,
393 DOMAIN_COM_MX,
394 DOMAIN_COM_MY,
395 DOMAIN_COM_NA,
396 DOMAIN_COM_NF,
397 DOMAIN_COM_NG,
398 DOMAIN_COM_NI,
399 DOMAIN_COM_NP,
400 DOMAIN_COM_NR,
401 DOMAIN_COM_OM,
402 DOMAIN_COM_PA,
403 DOMAIN_COM_PE,
404 DOMAIN_COM_PH,
405 DOMAIN_COM_PK,
406 DOMAIN_COM_PL,
407 DOMAIN_COM_PR,
408 DOMAIN_COM_PY,
409 DOMAIN_COM_QA,
410 DOMAIN_COM_RU,
411 DOMAIN_COM_SA,
412 DOMAIN_COM_SB,
413 DOMAIN_COM_SG,
414 DOMAIN_COM_SL,
415 DOMAIN_COM_SV,
416 DOMAIN_COM_TJ,
417 DOMAIN_COM_TN,
418 DOMAIN_COM_TR,
419 DOMAIN_COM_TW,
420 DOMAIN_COM_UA,
421 DOMAIN_COM_UY,
422 DOMAIN_COM_VC,
423 DOMAIN_COM_VE,
424 DOMAIN_COM_VN,
425 DOMAIN_GOOGLE_CV,
426 DOMAIN_GOOGLE_CZ,
427 DOMAIN_GOOGLE_DE,
428 DOMAIN_GOOGLE_DJ,
429 DOMAIN_GOOGLE_DK,
430 DOMAIN_GOOGLE_DM,
431 DOMAIN_GOOGLE_DZ,
432 DOMAIN_GOOGLE_EE,
433 DOMAIN_GOOGLE_ES,
434 DOMAIN_GOOGLE_FI,
435 DOMAIN_GOOGLE_FM,
436 DOMAIN_GOOGLE_FR,
437 DOMAIN_GOOGLE_GA,
438 DOMAIN_GOOGLE_GE,
439 DOMAIN_GOOGLE_GG,
440 DOMAIN_GOOGLE_GL,
441 DOMAIN_GOOGLE_GM,
442 DOMAIN_GOOGLE_GP,
443 DOMAIN_GOOGLE_GR,
444 DOMAIN_GOOGLE_GY,
445 DOMAIN_GOOGLE_HK,
446 DOMAIN_GOOGLE_HN,
447 DOMAIN_GOOGLE_HR,
448 DOMAIN_GOOGLE_HT,
449 DOMAIN_GOOGLE_HU,
450 DOMAIN_GOOGLE_IE,
451 DOMAIN_GOOGLE_IM,
452 DOMAIN_GOOGLE_INFO,
453 DOMAIN_GOOGLE_IQ,
454 DOMAIN_GOOGLE_IS,
455 DOMAIN_GOOGLE_IT,
456 DOMAIN_IT_AO,
457 DOMAIN_GOOGLE_JE,
458 DOMAIN_GOOGLE_JO,
459 DOMAIN_GOOGLE_JOBS,
460 DOMAIN_GOOGLE_JP,
461 DOMAIN_GOOGLE_KG,
462 DOMAIN_GOOGLE_KI,
463 DOMAIN_GOOGLE_KZ,
464 DOMAIN_GOOGLE_LA,
465 DOMAIN_GOOGLE_LI,
466 DOMAIN_GOOGLE_LK,
467 DOMAIN_GOOGLE_LT,
468 DOMAIN_GOOGLE_LU,
469 DOMAIN_GOOGLE_LV,
470 DOMAIN_GOOGLE_MD,
471 DOMAIN_GOOGLE_ME,
472 DOMAIN_GOOGLE_MG,
473 DOMAIN_GOOGLE_MK,
474 DOMAIN_GOOGLE_ML,
475 DOMAIN_GOOGLE_MN,
476 DOMAIN_GOOGLE_MS,
477 DOMAIN_GOOGLE_MU,
478 DOMAIN_GOOGLE_MV,
479 DOMAIN_GOOGLE_MW,
480 DOMAIN_GOOGLE_NE,
481 DOMAIN_NE_JP,
482 DOMAIN_GOOGLE_NET,
483 DOMAIN_GOOGLE_NL,
484 DOMAIN_GOOGLE_NO,
485 DOMAIN_GOOGLE_NR,
486 DOMAIN_GOOGLE_NU,
487 DOMAIN_OFF_AI,
488 DOMAIN_GOOGLE_PK,
489 DOMAIN_GOOGLE_PL,
490 DOMAIN_GOOGLE_PN,
491 DOMAIN_GOOGLE_PS,
492 DOMAIN_GOOGLE_PT,
493 DOMAIN_GOOGLE_RO,
494 DOMAIN_GOOGLE_RS,
495 DOMAIN_GOOGLE_RU,
496 DOMAIN_GOOGLE_RW,
497 DOMAIN_GOOGLE_SC,
498 DOMAIN_GOOGLE_SE,
499 DOMAIN_GOOGLE_SH,
500 DOMAIN_GOOGLE_SI,
501 DOMAIN_GOOGLE_SK,
502 DOMAIN_GOOGLE_SM,
503 DOMAIN_GOOGLE_SN,
504 DOMAIN_GOOGLE_SO,
505 DOMAIN_GOOGLE_ST,
506 DOMAIN_GOOGLE_TD,
507 DOMAIN_GOOGLE_TG,
508 DOMAIN_GOOGLE_TK,
509 DOMAIN_GOOGLE_TL,
510 DOMAIN_GOOGLE_TM,
511 DOMAIN_GOOGLE_TN,
512 DOMAIN_GOOGLE_TO,
513 DOMAIN_GOOGLE_TP,
514 DOMAIN_GOOGLE_TT,
515 DOMAIN_GOOGLE_US,
516 DOMAIN_GOOGLE_UZ,
517 DOMAIN_GOOGLE_VG,
518 DOMAIN_GOOGLE_VU,
519 DOMAIN_GOOGLE_WS,
521 DOMAIN_CHROMIUM_ORG,
523 DOMAIN_CRYPTO_CAT,
524 DOMAIN_LAVABIT_COM,
526 DOMAIN_GOOGLETAGMANAGER_COM,
527 DOMAIN_GOOGLETAGSERVICES_COM,
529 DOMAIN_DROPBOX_COM,
531 // Boundary value for UMA_HISTOGRAM_ENUMERATION:
532 DOMAIN_NUM_EVENTS
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;
543 struct HSTSPreload {
544 uint8 length;
545 bool include_subdomains;
546 char dns_name[38];
547 bool https_required;
548 PublicKeyPins pins;
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) {
560 *ret = false;
561 } else {
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();
566 *ret = true;
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;
574 while (*sha1_hash) {
575 AddHash(*sha1_hash, &out->pkp.spki_hashes);
576 sha1_hash++;
579 if (entries[j].pins.excluded_hashes) {
580 const char* const* sha1_hash = entries[j].pins.excluded_hashes;
581 while (*sha1_hash) {
582 AddHash(*sha1_hash, &out->pkp.bad_spki_hashes);
583 sha1_hash++;
587 return true;
590 return false;
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
600 // CanonicalizeHost.
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)
610 continue;
612 if (entry->length == canonicalized_host.size() - i &&
613 memcmp(entry->dns_name, &canonicalized_host[i], entry->length) == 0) {
614 return entry;
619 return NULL;
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;
634 else
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);
639 return true;
641 return false;
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,
655 &max_age,
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);
664 return true;
666 return false;
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(
679 hashed_host);
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);
688 return true;
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(
702 hashed_host);
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);
711 return true;
714 // static
715 bool TransportSecurityState::IsGooglePinnedProperty(const std::string& host,
716 bool sni_enabled) {
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)
722 return true;
724 if (sni_enabled) {
725 entry = GetHSTSPreload(canonicalized_host, kPreloadedSNISTS,
726 kNumPreloadedSNISTS);
727 if (entry && entry->pins.required_hashes == kGoogleAcceptableCerts)
728 return true;
731 return false;
734 // static
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);
741 if (!entry) {
742 entry = GetHSTSPreload(canonicalized_host, kPreloadedSNISTS,
743 kNumPreloadedSNISTS);
746 if (!entry) {
747 // We don't care to report pin failures for dynamic pins.
748 return;
751 DCHECK(entry);
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);
759 // static
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,
767 bool sni_enabled,
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);
783 bool ret;
784 if (is_build_timely &&
785 HasPreload(kPreloadedSTS, kNumPreloadedSTS, canonicalized_host, i, out,
786 &ret)) {
787 return ret;
789 if (sni_enabled &&
790 is_build_timely &&
791 HasPreload(kPreloadedSNISTS, kNumPreloadedSNISTS, canonicalized_host, i,
792 out, &ret)) {
793 return ret;
797 return false;
800 bool TransportSecurityState::GetDynamicDomainState(const std::string& host,
801 DomainState* result) {
802 DCHECK(CalledOnValidThread());
804 DomainState state;
805 const std::string canonicalized_host = CanonicalizeHost(host);
806 if (canonicalized_host.empty())
807 return false;
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())
817 continue;
819 if (current_time > j->second.sts.expiry &&
820 current_time > j->second.pkp.expiry) {
821 enabled_hosts_.erase(j);
822 DirtyNotify();
823 continue;
826 state = j->second;
827 state.domain = DNSDomainToString(host_sub_chunk);
829 // Succeed if we matched the domain exactly or if subdomain matches are
830 // allowed.
831 if (i == 0 || j->second.sts.include_subdomains ||
832 j->second.pkp.include_subdomains) {
833 *result = state;
834 return true;
837 return false;
840 return false;
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()) {
864 failure_log->append(
865 "Rejecting empty public key chain for public-key-pinned domains: " +
866 domain);
867 return false;
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));
875 return false;
878 // If there are no pins, then any valid chain is acceptable.
879 if (pkp.spki_hashes.empty())
880 return true;
882 if (HashesIntersect(pkp.spki_hashes, hashes)) {
883 return true;
886 failure_log->append("Rejecting public key chain for domain " + domain +
887 ". Validated chain: " + HashesToBase64String(hashes) +
888 ", expected: " + HashesToBase64String(pkp.spki_hashes));
889 return false;
892 bool TransportSecurityState::DomainState::ShouldUpgradeToSSL() const {
893 return sts.upgrade_mode == MODE_FORCE_HTTPS;
896 bool TransportSecurityState::DomainState::ShouldSSLErrorsBeFatal() const {
897 return true;
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() {
910 } // namespace