Chromium Code Reviews| Index: content/common/experiments/api_key.cc |
| diff --git a/content/common/experiments/api_key.cc b/content/common/experiments/api_key.cc |
| index 09b607f5a523005feca5332e64728b2801dfb127..dcb3fe46e029a91f55666003d72263c79ae03d8a 100644 |
| --- a/content/common/experiments/api_key.cc |
| +++ b/content/common/experiments/api_key.cc |
| @@ -5,6 +5,7 @@ |
| #include "content/common/experiments/api_key.h" |
| #include <vector> |
| +#include <openssl/curve25519.h> |
|
davidben
2016/01/11 20:18:57
Nit: I believe this should be
#include <openssl/c
iclelland
2016/01/12 14:52:49
Thanks, done.
davidben
2016/01/13 20:50:39
Yeah, part of my motivation for switching it to th
iclelland
2016/01/13 21:30:11
Acknowledged.
|
| #include "base/base64.h" |
| #include "base/strings/string_number_conversions.h" |
| @@ -18,8 +19,18 @@ namespace content { |
| namespace { |
| +// This is the default public key used for validating signatures. |
| +// TODO(iclelland): Move this to the embedder, and provide a mechanism to allow |
| +// for multiple signing keys. https://crbug.com/543220 |
| +static const uint8_t kPublicKey[] = { |
| + 0x7c, 0xc4, 0xb8, 0x9a, 0x93, 0xba, 0x6e, 0xe2, 0xd0, 0xfd, 0x03, |
| + 0x1d, 0xfb, 0x32, 0x66, 0xc7, 0x3b, 0x72, 0xfd, 0x54, 0x3a, 0x07, |
| + 0x51, 0x14, 0x66, 0xaa, 0x02, 0x53, 0x4e, 0x33, 0xa1, 0x15, |
| +}; |
| + |
| const char* kApiKeyFieldSeparator = "|"; |
| -} |
| + |
| +} // namespace |
| ApiKey::~ApiKey() {} |
| @@ -30,6 +41,8 @@ scoped_ptr<ApiKey> ApiKey::Parse(const std::string& key_text) { |
| // API Key should resemble: |
| // signature|origin|api_name|expiry_timestamp |
| + // TODO(iclelland): Add version code to API key format to identify key algo |
| + // https://crbug.com/570684 |
| std::vector<std::string> parts = |
| SplitString(key_text, kApiKeyFieldSeparator, base::KEEP_WHITESPACE, |
| base::SPLIT_WANT_ALL); |
| @@ -77,9 +90,7 @@ bool ApiKey::IsAppropriate(const std::string& origin, |
| } |
| bool ApiKey::IsValid(const base::Time& now) const { |
| - // TODO(iclelland): Validate signature on key data here as well. |
| - // https://crbug.com/543215 |
| - return ValidateDate(now); |
| + return ValidateDate(now) && ValidateSignature(kPublicKey); |
| } |
| bool ApiKey::ValidateOrigin(const std::string& origin) const { |
| @@ -95,4 +106,25 @@ bool ApiKey::ValidateDate(const base::Time& now) const { |
| return expiry_time > now; |
| } |
| +bool ApiKey::ValidateSignature(const uint8_t* public_key) const { |
| + return ValidateSignature(signature_, data_, public_key); |
| +} |
| + |
| +// static |
| +bool ApiKey::ValidateSignature(const std::string& signature_text, |
| + const std::string& data, |
| + const uint8_t* public_key) { |
| + std::string signature; |
| + // signature is base64-encoded |
| + if (!base::Base64Decode(signature_text, &signature)) { |
| + return false; |
| + } |
| + |
| + // TODO: verify signature length is 64 |
|
davidben
2016/01/11 20:18:57
That seems pretty important to do. Otherwise you'l
iclelland
2016/01/12 14:52:49
Done.
|
| + int result = ED25519_verify( |
| + reinterpret_cast<const uint8_t*>(data.data()), data.length(), |
| + reinterpret_cast<const uint8_t*>(signature.data()), public_key); |
| + return (result != 0); |
| +} |
| + |
| } // namespace content |