Chromium Code Reviews| Index: components/gcm_driver/crypto/encryption_header_parsers.cc |
| diff --git a/components/gcm_driver/crypto/encryption_header_parsers.cc b/components/gcm_driver/crypto/encryption_header_parsers.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..597c7dc5759692c9a5919c4a4e30d79d9c4c2b1c |
| --- /dev/null |
| +++ b/components/gcm_driver/crypto/encryption_header_parsers.cc |
| @@ -0,0 +1,174 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/gcm_driver/crypto/encryption_header_parsers.h" |
| + |
| +#include "base/base64.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_piece.h" |
| +#include "base/strings/string_util.h" |
| +#include "net/http/http_util.h" |
| + |
| +namespace gcm { |
| + |
| +namespace { |
| + |
| +// The default record size in bytes, as defined in section two of |
| +// https://tools.ietf.org/html/draft-thomson-http-encryption-01. |
| +const int64_t kDefaultRecordSizeBytes = 4096; |
|
eroman
2015/09/25 18:18:19
uint64_t
Peter Beverloo
2015/09/28 11:27:15
Done.
|
| + |
| +bool Base64URLDecode(const base::StringPiece& input, std::string* output) { |
|
eroman
2015/09/25 18:18:19
There are already several duplications of base64ur
Peter Beverloo
2015/09/28 11:27:15
Certainly agreed. I filed https://crbug.com/536745
eroman
2015/09/28 16:25:29
sgtm
|
| + size_t padded_size = |
| + input.size() % 4 ? input.size() + 4 - (input.size() % 4) : input.size(); |
| + |
| + // Add padding to |input|. |
| + std::string padded_input(input.begin(), input.end()); |
| + padded_input.resize(padded_size, '='); |
| + |
| + // Convert to standard base64 alphabet. |
|
eroman
2015/09/25 18:18:19
It looks like this was copied from GCMNetworkChann
Peter Beverloo
2015/09/28 11:27:15
Yes, and I now mixed in parts from components/prox
|
| + base::ReplaceChars(padded_input, "-", "+", &padded_input); |
| + base::ReplaceChars(padded_input, "_", "/", &padded_input); |
|
eroman
2015/09/25 18:18:19
This parsing is too permissive, in that it will al
Peter Beverloo
2015/09/28 11:27:15
I've included a check that the characters "/" and
|
| + |
| + return base::Base64Decode(padded_input, output); |
| +} |
| + |
| +bool ValueToDecodedString(const std::string::const_iterator& begin, |
| + const std::string::const_iterator& end, |
| + std::string* salt) { |
| + const base::StringPiece value(begin, end); |
| + if (value.empty()) |
| + return false; |
| + |
| + return Base64URLDecode(value, salt); |
| +} |
| + |
| +bool RecordSizeToInt(const std::string::const_iterator& begin, |
| + const std::string::const_iterator& end, |
| + int64_t* rs) { |
| + const base::StringPiece value(begin, end); |
| + if (value.empty()) |
| + return false; |
| + |
| + if (!base::StringToInt64(value, rs)) |
|
eroman
2015/09/25 18:18:19
why not use a uint64 and StringToUint64() since ne
Peter Beverloo
2015/09/28 11:27:15
Done.
|
| + return false; |
| + |
| + // The record size MUST be greater than 1. |
| + return *rs > 1; |
| +} |
| + |
| +bool ParseEncryptionHeaderImpl(const std::string& input, |
| + std::string* keyid, |
| + std::string* salt, |
| + int64_t* rs) { |
| + net::HttpUtil::NameValuePairsIterator name_value_pairs( |
| + input.begin(), input.end(), ';', |
| + net::HttpUtil::NameValuePairsIterator::VALUES_NOT_OPTIONAL); |
| + |
| + while (name_value_pairs.GetNext()) { |
| + const base::StringPiece name(name_value_pairs.name_begin(), |
| + name_value_pairs.name_end()); |
| + |
| + if (base::LowerCaseEqualsASCII(name, "keyid")) { |
| + keyid->assign(name_value_pairs.value_begin(), |
| + name_value_pairs.value_end()); |
| + } else if (base::LowerCaseEqualsASCII(name, "salt")) { |
| + if (!ValueToDecodedString(name_value_pairs.value_begin(), |
| + name_value_pairs.value_end(), salt)) { |
| + return false; |
| + } |
| + } else if (base::LowerCaseEqualsASCII(name, "rs")) { |
| + if (!RecordSizeToInt(name_value_pairs.value_begin(), |
| + name_value_pairs.value_end(), rs)) { |
| + return false; |
| + } |
| + } else { |
| + // Silently ignore unknown directives for forward compatibility. |
| + } |
| + } |
| + |
| + return name_value_pairs.valid(); |
| +} |
| + |
| +bool ParseEncryptionKeyHeaderImpl(const std::string& input, |
| + std::string* keyid, |
| + std::string* key, |
| + std::string* dh) { |
| + net::HttpUtil::NameValuePairsIterator name_value_pairs( |
| + input.begin(), input.end(), ';', |
| + net::HttpUtil::NameValuePairsIterator::VALUES_NOT_OPTIONAL); |
| + |
| + while (name_value_pairs.GetNext()) { |
| + const base::StringPiece name(name_value_pairs.name_begin(), |
| + name_value_pairs.name_end()); |
| + |
| + if (base::LowerCaseEqualsASCII(name, "keyid")) { |
| + keyid->assign(name_value_pairs.value_begin(), |
| + name_value_pairs.value_end()); |
| + } else if (base::LowerCaseEqualsASCII(name, "key")) { |
| + if (!ValueToDecodedString(name_value_pairs.value_begin(), |
| + name_value_pairs.value_end(), key)) { |
| + return false; |
| + } |
| + } else if (base::LowerCaseEqualsASCII(name, "dh")) { |
| + if (!ValueToDecodedString(name_value_pairs.value_begin(), |
| + name_value_pairs.value_end(), dh)) { |
| + return false; |
| + } |
| + } else { |
| + // Silently ignore unknown directives for forward compatibility. |
| + } |
| + } |
| + |
| + return name_value_pairs.valid(); |
| +} |
| + |
| +} // namespace |
| + |
| +// "Encryption" ":" |
| +// [ "keyid" "=" string ] |
| +// [ ";" "salt" "=" base64url ] |
| +// [ ";" "rs" "=" octet-count ] |
| +bool ParseEncryptionHeader(const std::string& input, |
| + std::string* keyid, |
| + std::string* salt, |
| + int64_t* rs) { |
| + std::string candidate_keyid; |
| + std::string candidate_salt; |
| + int64_t candidate_rs = kDefaultRecordSizeBytes; |
| + |
| + if (!ParseEncryptionHeaderImpl(input, &candidate_keyid, &candidate_salt, |
| + &candidate_rs)) { |
| + return false; |
| + } |
| + |
| + keyid->swap(candidate_keyid); |
| + salt->swap(candidate_salt); |
| + *rs = candidate_rs; |
| + return true; |
| +} |
| + |
| +// "Encryption-Key" ":" |
| +// [ "keyid" "=" string ] |
| +// [ ";" "key" "=" base64url ] |
| +// [ ";" "dh" "=" base64url ] |
| +bool ParseEncryptionKeyHeader(const std::string& input, |
| + std::string* keyid, |
| + std::string* key, |
| + std::string* dh) { |
| + std::string candidate_keyid; |
| + std::string candidate_key; |
| + std::string candidate_dh; |
| + |
| + if (!ParseEncryptionKeyHeaderImpl(input, &candidate_keyid, &candidate_key, |
| + &candidate_dh)) { |
| + return false; |
| + } |
| + |
| + keyid->swap(candidate_keyid); |
| + key->swap(candidate_key); |
| + dh->swap(candidate_dh); |
| + return true; |
| +} |
| + |
| +} // namespace gcm |