Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_piece.h" | |
| 8 #include "base/strings/string_tokenizer.h" | 9 #include "base/strings/string_tokenizer.h" |
| 9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 10 #include "net/http/http_security_headers.h" | 11 #include "net/http/http_security_headers.h" |
| 11 #include "net/http/http_util.h" | 12 #include "net/http/http_util.h" |
| 13 #include "url/gurl.h" | |
| 12 | 14 |
| 13 namespace net { | 15 namespace net { |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 static_assert(kMaxHSTSAgeSecs <= kuint32max, "kMaxHSTSAgeSecs too large"); | 19 static_assert(kMaxHSTSAgeSecs <= kuint32max, "kMaxHSTSAgeSecs too large"); |
| 18 | 20 |
| 19 // MaxAgeToInt converts a string representation of a "whole number" of | 21 // MaxAgeToInt converts a string representation of a "whole number" of |
| 20 // seconds into a uint32. The string may contain an arbitrarily large number, | 22 // seconds into a uint32. The string may contain an arbitrarily large number, |
| 21 // which will be clipped to kMaxHSTSAgeSecs and which is guaranteed to fit | 23 // which will be clipped to kMaxHSTSAgeSecs and which is guaranteed to fit |
| 22 // within a 32-bit unsigned integer. False is returned on any parse error. | 24 // within a 32-bit unsigned integer. False is returned on any parse error. |
| 23 bool MaxAgeToInt(std::string::const_iterator begin, | 25 bool MaxAgeToInt(std::string::const_iterator begin, |
| 24 std::string::const_iterator end, | 26 std::string::const_iterator end, |
| 25 uint32* result) { | 27 uint32* result) { |
| 26 const std::string s(begin, end); | 28 const base::StringPiece s(begin, end); |
| 29 if (s.empty()) | |
| 30 return false; | |
| 31 | |
| 27 int64 i = 0; | 32 int64 i = 0; |
| 28 | 33 |
| 29 // Return false on any StringToInt64 parse errors *except* for | 34 // Return false on any StringToInt64 parse errors *except* for |
| 30 // int64 overflow. StringToInt64 is used, rather than StringToUint64, | 35 // int64 overflow. StringToInt64 is used, rather than StringToUint64, |
| 31 // in order to properly handle and reject negative numbers | 36 // in order to properly handle and reject negative numbers |
| 32 // (StringToUint64 does not return false on negative numbers). | 37 // (StringToUint64 does not return false on negative numbers). |
| 33 // For values too large to be stored in an int64, StringToInt64 will | 38 // For values too large to be stored in an int64, StringToInt64 will |
| 34 // return false with i set to kint64max, so this case is detected | 39 // return false with i set to kint64max, so this case is detected |
| 35 // by the immediately following if-statement and allowed to fall | 40 // by the immediately following if-statement and allowed to fall |
| 36 // through so that i gets clipped to kMaxHSTSAgeSecs. | 41 // through so that i gets clipped to kMaxHSTSAgeSecs. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 if (pins.size() < 2) | 90 if (pins.size() < 2) |
| 86 return false; | 91 return false; |
| 87 | 92 |
| 88 if (from_cert_chain.empty()) | 93 if (from_cert_chain.empty()) |
| 89 return false; | 94 return false; |
| 90 | 95 |
| 91 return IsBackupPinPresent(pins, from_cert_chain) && | 96 return IsBackupPinPresent(pins, from_cert_chain) && |
| 92 HashesIntersect(pins, from_cert_chain); | 97 HashesIntersect(pins, from_cert_chain); |
| 93 } | 98 } |
| 94 | 99 |
| 95 std::string Strip(const std::string& source) { | 100 bool ParseAndAppendPin(std::string::const_iterator begin, |
| 96 if (source.empty()) | 101 std::string::const_iterator end, |
| 97 return source; | |
| 98 | |
| 99 std::string::const_iterator start = source.begin(); | |
| 100 std::string::const_iterator end = source.end(); | |
| 101 HttpUtil::TrimLWS(&start, &end); | |
| 102 return std::string(start, end); | |
| 103 } | |
| 104 | |
| 105 typedef std::pair<std::string, std::string> StringPair; | |
| 106 | |
| 107 StringPair Split(const std::string& source, char delimiter) { | |
| 108 StringPair pair; | |
| 109 size_t point = source.find(delimiter); | |
| 110 | |
| 111 pair.first = source.substr(0, point); | |
| 112 if (std::string::npos != point) | |
| 113 pair.second = source.substr(point + 1); | |
| 114 | |
| 115 return pair; | |
| 116 } | |
| 117 | |
| 118 bool ParseAndAppendPin(const std::string& value, | |
| 119 HashValueTag tag, | 102 HashValueTag tag, |
| 120 HashValueVector* hashes) { | 103 HashValueVector* hashes) { |
| 121 // Pins are always quoted. | 104 const base::StringPiece value(begin, end); |
| 122 if (value.empty() || !HttpUtil::IsQuote(value[0])) | 105 if (value.empty()) |
| 123 return false; | 106 return false; |
| 124 | 107 |
| 125 std::string unquoted = HttpUtil::Unquote(value); | |
| 126 if (unquoted.empty()) | |
| 127 return false; | |
| 128 | |
| 129 std::string decoded; | 108 std::string decoded; |
| 130 if (!base::Base64Decode(unquoted, &decoded)) | 109 if (!base::Base64Decode(value, &decoded)) |
| 131 return false; | 110 return false; |
| 132 | 111 |
| 133 HashValue hash(tag); | 112 HashValue hash(tag); |
| 134 if (decoded.size() != hash.size()) | 113 if (decoded.size() != hash.size()) |
| 135 return false; | 114 return false; |
| 136 | 115 |
| 137 memcpy(hash.data(), decoded.data(), hash.size()); | 116 memcpy(hash.data(), decoded.data(), hash.size()); |
| 138 hashes->push_back(hash); | 117 hashes->push_back(hash); |
| 139 return true; | 118 return true; |
| 140 } | 119 } |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 266 case START: | 245 case START: |
| 267 case AFTER_MAX_AGE_LABEL: | 246 case AFTER_MAX_AGE_LABEL: |
| 268 case AFTER_MAX_AGE_EQUALS: | 247 case AFTER_MAX_AGE_EQUALS: |
| 269 return false; | 248 return false; |
| 270 default: | 249 default: |
| 271 NOTREACHED(); | 250 NOTREACHED(); |
| 272 return false; | 251 return false; |
| 273 } | 252 } |
| 274 } | 253 } |
| 275 | 254 |
| 276 // "Public-Key-Pins" ":" | 255 // "Public-Key-Pins[-Report-Only]" ":" |
| 277 // "max-age" "=" delta-seconds ";" | 256 // "max-age" "=" delta-seconds ";" |
| 278 // "pin-" algo "=" base64 [ ";" ... ] | 257 // "pin-" algo "=" base64 [ ";" ... ] |
| 258 // [ ";" "includeSubdomains" ] | |
| 259 // [ ";" "report-uri" "=" uri-reference ] | |
| 279 bool ParseHPKPHeader(const std::string& value, | 260 bool ParseHPKPHeader(const std::string& value, |
| 280 const HashValueVector& chain_hashes, | 261 const HashValueVector& chain_hashes, |
| 281 base::TimeDelta* max_age, | 262 base::TimeDelta* max_age, |
| 282 bool* include_subdomains, | 263 bool* include_subdomains, |
| 283 HashValueVector* hashes) { | 264 HashValueVector* hashes, |
| 265 GURL* report_uri) { | |
| 284 bool parsed_max_age = false; | 266 bool parsed_max_age = false; |
| 285 bool include_subdomains_candidate = false; | 267 bool include_subdomains_candidate = false; |
| 286 uint32 max_age_candidate = 0; | 268 uint32 max_age_candidate = 0; |
| 269 GURL parsed_report_uri; | |
| 287 HashValueVector pins; | 270 HashValueVector pins; |
| 288 | 271 |
| 289 std::string source = value; | 272 HttpUtil::NameValuePairsIterator name_value_pairs( |
| 273 value.begin(), value.end(), ';', | |
| 274 HttpUtil::NameValuePairsIterator::VALUES_OPTIONAL); | |
| 290 | 275 |
| 291 while (!source.empty()) { | 276 while (name_value_pairs.GetNext()) { |
| 292 StringPair semicolon = Split(source, ';'); | 277 if (base::LowerCaseEqualsASCII(name_value_pairs.name(), "max-age")) { |
|
Ryan Sleevi
2015/07/16 05:07:51
World's cruelest reviewer nit:
Each of these can
estark
2015/07/16 05:18:48
Done.
| |
| 293 semicolon.first = Strip(semicolon.first); | 278 if (!MaxAgeToInt(name_value_pairs.value_begin(), |
| 294 semicolon.second = Strip(semicolon.second); | 279 name_value_pairs.value_end(), &max_age_candidate)) { |
| 295 StringPair equals = Split(semicolon.first, '='); | |
| 296 equals.first = Strip(equals.first); | |
| 297 equals.second = Strip(equals.second); | |
| 298 | |
| 299 if (base::LowerCaseEqualsASCII(equals.first, "max-age")) { | |
| 300 if (equals.second.empty() || | |
| 301 !MaxAgeToInt(equals.second.begin(), equals.second.end(), | |
| 302 &max_age_candidate)) { | |
| 303 return false; | 280 return false; |
| 304 } | 281 } |
| 305 parsed_max_age = true; | 282 parsed_max_age = true; |
| 306 } else if (base::LowerCaseEqualsASCII(equals.first, "pin-sha1")) { | 283 } else if (base::LowerCaseEqualsASCII(name_value_pairs.name(), |
| 307 if (!ParseAndAppendPin(equals.second, HASH_VALUE_SHA1, &pins)) | 284 "pin-sha1")) { |
| 285 // Pins are always quoted. | |
| 286 if (!name_value_pairs.value_is_quoted() || | |
| 287 !ParseAndAppendPin(name_value_pairs.value_begin(), | |
| 288 name_value_pairs.value_end(), HASH_VALUE_SHA1, | |
| 289 &pins)) { | |
| 308 return false; | 290 return false; |
| 309 } else if (base::LowerCaseEqualsASCII(equals.first, "pin-sha256")) { | 291 } |
| 310 if (!ParseAndAppendPin(equals.second, HASH_VALUE_SHA256, &pins)) | 292 } else if (base::LowerCaseEqualsASCII(name_value_pairs.name(), |
| 293 "pin-sha256")) { | |
| 294 // Pins are always quoted. | |
| 295 if (!name_value_pairs.value_is_quoted() || | |
| 296 !ParseAndAppendPin(name_value_pairs.value_begin(), | |
| 297 name_value_pairs.value_end(), HASH_VALUE_SHA256, | |
| 298 &pins)) { | |
| 311 return false; | 299 return false; |
| 312 } else if (base::LowerCaseEqualsASCII(equals.first, "includesubdomains")) { | 300 } |
| 301 } else if (base::LowerCaseEqualsASCII(name_value_pairs.name(), | |
| 302 "includesubdomains")) { | |
| 313 include_subdomains_candidate = true; | 303 include_subdomains_candidate = true; |
| 304 } else if (base::LowerCaseEqualsASCII(name_value_pairs.name(), | |
| 305 "report-uri")) { | |
| 306 // report-uris are always quoted. | |
| 307 if (!name_value_pairs.value_is_quoted()) | |
| 308 return false; | |
| 309 | |
| 310 parsed_report_uri = GURL(name_value_pairs.value()); | |
| 311 if (parsed_report_uri.is_empty() || !parsed_report_uri.is_valid()) | |
| 312 return false; | |
| 314 } else { | 313 } else { |
| 315 // Silently ignore unknown directives for forward compatibility. | 314 // Silently ignore unknown directives for forward compatibility. |
| 316 } | 315 } |
| 316 } | |
| 317 | 317 |
| 318 source = semicolon.second; | 318 if (!name_value_pairs.valid()) |
| 319 } | 319 return false; |
| 320 | 320 |
| 321 if (!parsed_max_age) | 321 if (!parsed_max_age) |
| 322 return false; | 322 return false; |
| 323 | 323 |
| 324 if (!IsPinListValid(pins, chain_hashes)) | 324 if (!IsPinListValid(pins, chain_hashes)) |
| 325 return false; | 325 return false; |
| 326 | 326 |
| 327 *max_age = base::TimeDelta::FromSeconds(max_age_candidate); | 327 *max_age = base::TimeDelta::FromSeconds(max_age_candidate); |
| 328 *include_subdomains = include_subdomains_candidate; | 328 *include_subdomains = include_subdomains_candidate; |
| 329 hashes->swap(pins); | 329 hashes->swap(pins); |
| 330 *report_uri = parsed_report_uri; | |
| 330 | 331 |
| 331 return true; | 332 return true; |
| 332 } | 333 } |
| 333 | 334 |
| 334 } // namespace net | 335 } // namespace net |
| OLD | NEW |