OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/transport_security_state.h" | 5 #include "net/base/transport_security_state.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
12 #include "base/sha2.h" | 12 #include "base/sha2.h" |
13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "base/string_tokenizer.h" | 14 #include "base/string_tokenizer.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
19 #include "net/base/dns_util.h" | 19 #include "net/base/dns_util.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 | 22 |
| 23 const long int TransportSecurityState::kMaxHSTSAgeSecs = 86400 * 365; // 1 year |
| 24 |
23 TransportSecurityState::TransportSecurityState() | 25 TransportSecurityState::TransportSecurityState() |
24 : delegate_(NULL) { | 26 : delegate_(NULL) { |
25 } | 27 } |
26 | 28 |
27 void TransportSecurityState::EnableHost(const std::string& host, | 29 void TransportSecurityState::EnableHost(const std::string& host, |
28 const DomainState& state) { | 30 const DomainState& state) { |
29 const std::string canonicalised_host = CanonicaliseHost(host); | 31 const std::string canonicalised_host = CanonicaliseHost(host); |
30 if (canonicalised_host.empty()) | 32 if (canonicalised_host.empty()) |
31 return; | 33 return; |
32 | 34 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 // include_subdomains is. | 93 // include_subdomains is. |
92 if (i == 0) | 94 if (i == 0) |
93 return true; | 95 return true; |
94 | 96 |
95 return j->second.include_subdomains; | 97 return j->second.include_subdomains; |
96 } | 98 } |
97 | 99 |
98 return false; | 100 return false; |
99 } | 101 } |
100 | 102 |
| 103 // MaxAgeToInt converts a string representation of a number of seconds into a |
| 104 // int. We use strtol in order to handle overflow correctly. The string may |
| 105 // contain an arbitary number which we should truncate correctly rather than |
| 106 // throwing a parse failure. |
| 107 static bool MaxAgeToInt(std::string::const_iterator begin, |
| 108 std::string::const_iterator end, |
| 109 int* result) { |
| 110 const std::string s(begin, end); |
| 111 char* endptr; |
| 112 long int i = strtol(s.data(), &endptr, 10 /* base */); |
| 113 if (*endptr || i < 0) |
| 114 return false; |
| 115 if (i > TransportSecurityState::kMaxHSTSAgeSecs) |
| 116 i = TransportSecurityState::kMaxHSTSAgeSecs; |
| 117 *result = i; |
| 118 return true; |
| 119 } |
| 120 |
101 // "Strict-Transport-Security" ":" | 121 // "Strict-Transport-Security" ":" |
102 // "max-age" "=" delta-seconds [ ";" "includeSubDomains" ] | 122 // "max-age" "=" delta-seconds [ ";" "includeSubDomains" ] |
103 bool TransportSecurityState::ParseHeader(const std::string& value, | 123 bool TransportSecurityState::ParseHeader(const std::string& value, |
104 int* max_age, | 124 int* max_age, |
105 bool* include_subdomains) { | 125 bool* include_subdomains) { |
106 DCHECK(max_age); | 126 DCHECK(max_age); |
107 DCHECK(include_subdomains); | 127 DCHECK(include_subdomains); |
108 | 128 |
109 int max_age_candidate; | 129 int max_age_candidate = 0; |
110 | 130 |
111 enum ParserState { | 131 enum ParserState { |
112 START, | 132 START, |
113 AFTER_MAX_AGE_LABEL, | 133 AFTER_MAX_AGE_LABEL, |
114 AFTER_MAX_AGE_EQUALS, | 134 AFTER_MAX_AGE_EQUALS, |
115 AFTER_MAX_AGE, | 135 AFTER_MAX_AGE, |
116 AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER, | 136 AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER, |
117 AFTER_INCLUDE_SUBDOMAINS, | 137 AFTER_INCLUDE_SUBDOMAINS, |
118 } state = START; | 138 } state = START; |
119 | 139 |
(...skipping 15 matching lines...) Expand all Loading... |
135 continue; | 155 continue; |
136 if (*tokenizer.token_begin() != '=') | 156 if (*tokenizer.token_begin() != '=') |
137 return false; | 157 return false; |
138 DCHECK(tokenizer.token().length() == 1); | 158 DCHECK(tokenizer.token().length() == 1); |
139 state = AFTER_MAX_AGE_EQUALS; | 159 state = AFTER_MAX_AGE_EQUALS; |
140 break; | 160 break; |
141 | 161 |
142 case AFTER_MAX_AGE_EQUALS: | 162 case AFTER_MAX_AGE_EQUALS: |
143 if (IsAsciiWhitespace(*tokenizer.token_begin())) | 163 if (IsAsciiWhitespace(*tokenizer.token_begin())) |
144 continue; | 164 continue; |
145 if (!base::StringToInt(tokenizer.token_begin(), | 165 if (!MaxAgeToInt(tokenizer.token_begin(), |
146 tokenizer.token_end(), | 166 tokenizer.token_end(), |
147 &max_age_candidate)) | 167 &max_age_candidate)) |
148 return false; | |
149 if (max_age_candidate < 0) | |
150 return false; | 168 return false; |
151 state = AFTER_MAX_AGE; | 169 state = AFTER_MAX_AGE; |
152 break; | 170 break; |
153 | 171 |
154 case AFTER_MAX_AGE: | 172 case AFTER_MAX_AGE: |
155 if (IsAsciiWhitespace(*tokenizer.token_begin())) | 173 if (IsAsciiWhitespace(*tokenizer.token_begin())) |
156 continue; | 174 continue; |
157 if (*tokenizer.token_begin() != ';') | 175 if (*tokenizer.token_begin() != ';') |
158 return false; | 176 return false; |
159 state = AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER; | 177 state = AFTER_MAX_AGE_INCLUDE_SUB_DOMAINS_DELIMITER; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 *include_subdomains = kPreloadedSTS[j].include_subdomains; | 441 *include_subdomains = kPreloadedSTS[j].include_subdomains; |
424 return true; | 442 return true; |
425 } | 443 } |
426 } | 444 } |
427 } | 445 } |
428 | 446 |
429 return false; | 447 return false; |
430 } | 448 } |
431 | 449 |
432 } // namespace | 450 } // namespace |
OLD | NEW |