Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: net/base/strict_transport_security_state.cc

Issue 201033: ForceTLS: hash hostnames, handle subdomains, canonicalise. (Closed)
Patch Set: ... Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/strict_transport_security_state.h" 5 #include "net/base/strict_transport_security_state.h"
6 6
7 #include "base/json_reader.h" 7 #include "base/json_reader.h"
8 #include "base/json_writer.h" 8 #include "base/json_writer.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/scoped_ptr.h" 10 #include "base/scoped_ptr.h"
11 #include "base/sha2.h"
11 #include "base/string_tokenizer.h" 12 #include "base/string_tokenizer.h"
12 #include "base/string_util.h" 13 #include "base/string_util.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
15 #include "net/base/registry_controlled_domain.h" 16 #include "net/base/base64.h"
17 #include "net/base/dns_util.h"
16 18
17 namespace net { 19 namespace net {
18 20
19 StrictTransportSecurityState::StrictTransportSecurityState() 21 StrictTransportSecurityState::StrictTransportSecurityState()
20 : delegate_(NULL) { 22 : delegate_(NULL) {
21 } 23 }
22 24
23 void StrictTransportSecurityState::DidReceiveHeader(const GURL& url, 25 void StrictTransportSecurityState::DidReceiveHeader(const GURL& url,
24 const std::string& value) { 26 const std::string& value) {
25 int max_age; 27 int max_age;
26 bool include_subdomains; 28 bool include_subdomains;
27 29
28 if (!ParseHeader(value, &max_age, &include_subdomains)) 30 if (!ParseHeader(value, &max_age, &include_subdomains))
29 return; 31 return;
30 32
31 base::Time current_time(base::Time::Now()); 33 base::Time current_time(base::Time::Now());
32 base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age); 34 base::TimeDelta max_age_delta = base::TimeDelta::FromSeconds(max_age);
33 base::Time expiry = current_time + max_age_delta; 35 base::Time expiry = current_time + max_age_delta;
34 36
35 EnableHost(url.host(), expiry, include_subdomains); 37 EnableHost(url.host(), expiry, include_subdomains);
36 } 38 }
37 39
38 void StrictTransportSecurityState::EnableHost(const std::string& host, 40 void StrictTransportSecurityState::EnableHost(const std::string& host,
39 base::Time expiry, 41 base::Time expiry,
40 bool include_subdomains) { 42 bool include_subdomains) {
41 // TODO(abarth): Canonicalize host. 43 const std::string canonicalised_host = CanonicaliseHost(host);
44 if (canonicalised_host.empty())
45 return;
46 char hashed[base::SHA256_LENGTH];
47 base::SHA256HashString(canonicalised_host, hashed, sizeof(hashed));
48
42 AutoLock lock(lock_); 49 AutoLock lock(lock_);
43 50
44 State state = {expiry, include_subdomains}; 51 State state = {expiry, include_subdomains};
45 enabled_hosts_[host] = state; 52 enabled_hosts_[std::string(hashed, sizeof(hashed))] = state;
46 DirtyNotify(); 53 DirtyNotify();
47 } 54 }
48 55
49 bool StrictTransportSecurityState::IsEnabledForHost(const std::string& host) { 56 bool StrictTransportSecurityState::IsEnabledForHost(const std::string& host) {
50 // TODO(abarth): Canonicalize host. 57 const std::string canonicalised_host = CanonicaliseHost(host);
51 // TODO: check for subdomains too. 58 if (canonicalised_host.empty())
52
53 AutoLock lock(lock_);
54 std::map<std::string, State>::iterator i = enabled_hosts_.find(host);
55 if (i == enabled_hosts_.end())
56 return false; 59 return false;
57 60
58 base::Time current_time(base::Time::Now()); 61 base::Time current_time(base::Time::Now());
59 if (current_time > i->second.expiry) { 62 AutoLock lock(lock_);
60 enabled_hosts_.erase(i); 63
61 DirtyNotify(); 64 for (size_t i = 0; canonicalised_host[i]; i += canonicalised_host[i] + 1) {
62 return false; 65 char hashed_domain[base::SHA256_LENGTH];
66
67 base::SHA256HashString(&canonicalised_host[i], &hashed_domain,
68 sizeof(hashed_domain));
69 std::map<std::string, State>::iterator j =
70 enabled_hosts_.find(std::string(hashed_domain, sizeof(hashed_domain)));
71 if (j == enabled_hosts_.end())
72 continue;
73
74 if (current_time > j->second.expiry) {
75 enabled_hosts_.erase(j);
76 DirtyNotify();
77 continue;
78 }
79
80 // If we matched the domain exactly, it doesn't matter what the value of
81 // include_subdomains is.
82 if (i == 0)
83 return true;
84
85 return j->second.include_subdomains;
63 } 86 }
64 87
65 return true; 88 return false;
66 } 89 }
67 90
68 // "X-Force-TLS" ":" "max-age" "=" delta-seconds *1INCLUDESUBDOMAINS 91 // "X-Force-TLS" ":" "max-age" "=" delta-seconds *1INCLUDESUBDOMAINS
69 // INCLUDESUBDOMAINS = [ " includeSubDomains" ] 92 // INCLUDESUBDOMAINS = [ " includeSubDomains" ]
70 bool StrictTransportSecurityState::ParseHeader(const std::string& value, 93 bool StrictTransportSecurityState::ParseHeader(const std::string& value,
71 int* max_age, 94 int* max_age,
72 bool* include_subdomains) { 95 bool* include_subdomains) {
73 DCHECK(max_age); 96 DCHECK(max_age);
74 DCHECK(include_subdomains); 97 DCHECK(include_subdomains);
75 98
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 } 185 }
163 } 186 }
164 187
165 void StrictTransportSecurityState::SetDelegate( 188 void StrictTransportSecurityState::SetDelegate(
166 StrictTransportSecurityState::Delegate* delegate) { 189 StrictTransportSecurityState::Delegate* delegate) {
167 AutoLock lock(lock_); 190 AutoLock lock(lock_);
168 191
169 delegate_ = delegate; 192 delegate_ = delegate;
170 } 193 }
171 194
195 // This function converts the binary hashes, which we store in
196 // |enabled_hosts_|, to a base64 string which we can include in a JSON file.
197 static std::wstring HashedDomainToExternalString(const std::string& hashed) {
198 std::string out;
199 CHECK(Base64Encode(hashed, &out));
200 return ASCIIToWide(out);
201 }
202
203 // This inverts |HashedDomainToExternalString|, above. It turns an external
204 // string (from a JSON file) into an internal (binary) string.
205 static std::string ExternalStringToHashedDomain(const std::wstring& external) {
206 std::string external_ascii = WideToASCII(external);
207 std::string out;
208 if (!Base64Decode(external_ascii, &out) ||
209 out.size() != base::SHA256_LENGTH) {
210 return std::string();
211 }
212
213 return out;
214 }
215
172 bool StrictTransportSecurityState::Serialise(std::string* output) { 216 bool StrictTransportSecurityState::Serialise(std::string* output) {
173 AutoLock lock(lock_); 217 AutoLock lock(lock_);
174 218
175 DictionaryValue toplevel; 219 DictionaryValue toplevel;
176 for (std::map<std::string, State>::const_iterator 220 for (std::map<std::string, State>::const_iterator
177 i = enabled_hosts_.begin(); i != enabled_hosts_.end(); ++i) { 221 i = enabled_hosts_.begin(); i != enabled_hosts_.end(); ++i) {
178 DictionaryValue* state = new DictionaryValue; 222 DictionaryValue* state = new DictionaryValue;
179 state->SetBoolean(L"include_subdomains", i->second.include_subdomains); 223 state->SetBoolean(L"include_subdomains", i->second.include_subdomains);
180 state->SetReal(L"expiry", i->second.expiry.ToDoubleT()); 224 state->SetReal(L"expiry", i->second.expiry.ToDoubleT());
181 225
182 toplevel.Set(ASCIIToWide(i->first), state); 226 toplevel.Set(HashedDomainToExternalString(i->first), state);
183 } 227 }
184 228
185 JSONWriter::Write(&toplevel, true /* pretty print */, output); 229 JSONWriter::Write(&toplevel, true /* pretty print */, output);
186 return true; 230 return true;
187 } 231 }
188 232
189 bool StrictTransportSecurityState::Deserialise(const std::string& input) { 233 bool StrictTransportSecurityState::Deserialise(const std::string& input) {
190 AutoLock lock(lock_); 234 AutoLock lock(lock_);
191 235
192 enabled_hosts_.clear(); 236 enabled_hosts_.clear();
193 237
194 scoped_ptr<Value> value( 238 scoped_ptr<Value> value(
195 JSONReader::Read(input, false /* do not allow trailing commas */)); 239 JSONReader::Read(input, false /* do not allow trailing commas */));
196 if (!value.get() || !value->IsType(Value::TYPE_DICTIONARY)) 240 if (!value.get() || !value->IsType(Value::TYPE_DICTIONARY))
197 return false; 241 return false;
198 242
199 DictionaryValue* dict_value = reinterpret_cast<DictionaryValue*>(value.get()); 243 DictionaryValue* dict_value = reinterpret_cast<DictionaryValue*>(value.get());
200 const base::Time current_time(base::Time::Now()); 244 const base::Time current_time(base::Time::Now());
201 245
202 for (DictionaryValue::key_iterator 246 for (DictionaryValue::key_iterator
203 i = dict_value->begin_keys(); i != dict_value->end_keys(); ++i) { 247 i = dict_value->begin_keys(); i != dict_value->end_keys(); ++i) {
204 DictionaryValue* state; 248 DictionaryValue* state;
205 if (!dict_value->GetDictionary(*i, &state)) 249 if (!dict_value->GetDictionary(*i, &state))
206 continue; 250 continue;
207 251
208 const std::string host = WideToASCII(*i);
209 bool include_subdomains; 252 bool include_subdomains;
210 double expiry; 253 double expiry;
211 254
212 if (!state->GetBoolean(L"include_subdomains", &include_subdomains) || 255 if (!state->GetBoolean(L"include_subdomains", &include_subdomains) ||
213 !state->GetReal(L"expiry", &expiry)) { 256 !state->GetReal(L"expiry", &expiry)) {
214 continue; 257 continue;
215 } 258 }
216 259
217 base::Time expiry_time = base::Time::FromDoubleT(expiry); 260 base::Time expiry_time = base::Time::FromDoubleT(expiry);
218 if (expiry_time <= current_time) 261 if (expiry_time <= current_time)
219 continue; 262 continue;
220 263
264 std::string hashed = ExternalStringToHashedDomain(*i);
265 if (hashed.empty())
266 continue;
267
221 State new_state = { expiry_time, include_subdomains }; 268 State new_state = { expiry_time, include_subdomains };
222 enabled_hosts_[host] = new_state; 269 enabled_hosts_[hashed] = new_state;
223 } 270 }
224 271
225 return enabled_hosts_.size() > 0; 272 return true;
226 } 273 }
227 274
228 void StrictTransportSecurityState::DirtyNotify() { 275 void StrictTransportSecurityState::DirtyNotify() {
229 if (delegate_) 276 if (delegate_)
230 delegate_->StateIsDirty(this); 277 delegate_->StateIsDirty(this);
231 } 278 }
232 279
280 // static
281 std::string StrictTransportSecurityState::CanonicaliseHost(
282 const std::string& host) {
283 // We cannot perform the operations as detailed in the spec here as |host|
284 // has already undergone IDN processing before it reached us. Thus, we check
285 // that there are no invalid characters in the host and lowercase the result.
286
287 std::string new_host;
288 if (!DNSDomainFromDot(host, &new_host)) {
289 NOTREACHED();
290 return std::string();
291 }
292
293 for (size_t i = 0; new_host[i]; i += new_host[i] + 1) {
294 const unsigned label_length = static_cast<unsigned>(new_host[i]);
295 if (!label_length)
296 break;
297
298 for (size_t j = 0; j < label_length; ++j) {
299 // RFC 3490, 4.1, step 3
300 if (!IsSTD3ASCIIValidCharacter(new_host[i + 1 + j]))
301 return std::string();
302
303 new_host[i + 1 + j] = tolower(new_host[i + 1 + j]);
304 }
305
306 // step 3(b)
307 if (new_host[i + 1] == '-' ||
308 new_host[i + label_length] == '-') {
309 return std::string();
310 }
311 }
312
313 return new_host;
314 }
315
233 } // namespace 316 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698