OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/common/experiments/api_key.h" | 5 #include "content/common/experiments/api_key.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 #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.
| |
8 | 9 |
9 #include "base/base64.h" | 10 #include "base/base64.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "url/origin.h" | 16 #include "url/origin.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
22 // This is the default public key used for validating signatures. | |
23 // TODO(iclelland): Move this to the embedder, and provide a mechanism to allow | |
24 // for multiple signing keys. https://crbug.com/543220 | |
25 static const uint8_t kPublicKey[] = { | |
26 0x7c, 0xc4, 0xb8, 0x9a, 0x93, 0xba, 0x6e, 0xe2, 0xd0, 0xfd, 0x03, | |
27 0x1d, 0xfb, 0x32, 0x66, 0xc7, 0x3b, 0x72, 0xfd, 0x54, 0x3a, 0x07, | |
28 0x51, 0x14, 0x66, 0xaa, 0x02, 0x53, 0x4e, 0x33, 0xa1, 0x15, | |
29 }; | |
30 | |
21 const char* kApiKeyFieldSeparator = "|"; | 31 const char* kApiKeyFieldSeparator = "|"; |
22 } | 32 |
33 } // namespace | |
23 | 34 |
24 ApiKey::~ApiKey() {} | 35 ApiKey::~ApiKey() {} |
25 | 36 |
26 scoped_ptr<ApiKey> ApiKey::Parse(const std::string& key_text) { | 37 scoped_ptr<ApiKey> ApiKey::Parse(const std::string& key_text) { |
27 if (key_text.empty()) { | 38 if (key_text.empty()) { |
28 return nullptr; | 39 return nullptr; |
29 } | 40 } |
30 | 41 |
31 // API Key should resemble: | 42 // API Key should resemble: |
32 // signature|origin|api_name|expiry_timestamp | 43 // signature|origin|api_name|expiry_timestamp |
44 // TODO(iclelland): Add version code to API key format to identify key algo | |
45 // https://crbug.com/570684 | |
33 std::vector<std::string> parts = | 46 std::vector<std::string> parts = |
34 SplitString(key_text, kApiKeyFieldSeparator, base::KEEP_WHITESPACE, | 47 SplitString(key_text, kApiKeyFieldSeparator, base::KEEP_WHITESPACE, |
35 base::SPLIT_WANT_ALL); | 48 base::SPLIT_WANT_ALL); |
36 if (parts.size() != 4) { | 49 if (parts.size() != 4) { |
37 return nullptr; | 50 return nullptr; |
38 } | 51 } |
39 | 52 |
40 const std::string& signature = parts[0]; | 53 const std::string& signature = parts[0]; |
41 const std::string& origin_string = parts[1]; | 54 const std::string& origin_string = parts[1]; |
42 const std::string& api_name = parts[2]; | 55 const std::string& api_name = parts[2]; |
(...skipping 27 matching lines...) Expand all Loading... | |
70 origin_(origin), | 83 origin_(origin), |
71 api_name_(api_name), | 84 api_name_(api_name), |
72 expiry_timestamp_(expiry_timestamp) {} | 85 expiry_timestamp_(expiry_timestamp) {} |
73 | 86 |
74 bool ApiKey::IsAppropriate(const std::string& origin, | 87 bool ApiKey::IsAppropriate(const std::string& origin, |
75 const std::string& api_name) const { | 88 const std::string& api_name) const { |
76 return ValidateOrigin(origin) && ValidateApiName(api_name); | 89 return ValidateOrigin(origin) && ValidateApiName(api_name); |
77 } | 90 } |
78 | 91 |
79 bool ApiKey::IsValid(const base::Time& now) const { | 92 bool ApiKey::IsValid(const base::Time& now) const { |
80 // TODO(iclelland): Validate signature on key data here as well. | 93 return ValidateDate(now) && ValidateSignature(kPublicKey); |
81 // https://crbug.com/543215 | |
82 return ValidateDate(now); | |
83 } | 94 } |
84 | 95 |
85 bool ApiKey::ValidateOrigin(const std::string& origin) const { | 96 bool ApiKey::ValidateOrigin(const std::string& origin) const { |
86 return GURL(origin) == origin_; | 97 return GURL(origin) == origin_; |
87 } | 98 } |
88 | 99 |
89 bool ApiKey::ValidateApiName(const std::string& api_name) const { | 100 bool ApiKey::ValidateApiName(const std::string& api_name) const { |
90 return base::EqualsCaseInsensitiveASCII(api_name, api_name_); | 101 return base::EqualsCaseInsensitiveASCII(api_name, api_name_); |
91 } | 102 } |
92 | 103 |
93 bool ApiKey::ValidateDate(const base::Time& now) const { | 104 bool ApiKey::ValidateDate(const base::Time& now) const { |
94 base::Time expiry_time = base::Time::FromDoubleT((double)expiry_timestamp_); | 105 base::Time expiry_time = base::Time::FromDoubleT((double)expiry_timestamp_); |
95 return expiry_time > now; | 106 return expiry_time > now; |
96 } | 107 } |
97 | 108 |
109 bool ApiKey::ValidateSignature(const uint8_t* public_key) const { | |
110 return ValidateSignature(signature_, data_, public_key); | |
111 } | |
112 | |
113 // static | |
114 bool ApiKey::ValidateSignature(const std::string& signature_text, | |
115 const std::string& data, | |
116 const uint8_t* public_key) { | |
117 std::string signature; | |
118 // signature is base64-encoded | |
119 if (!base::Base64Decode(signature_text, &signature)) { | |
120 return false; | |
121 } | |
122 | |
123 // 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.
| |
124 int result = ED25519_verify( | |
125 reinterpret_cast<const uint8_t*>(data.data()), data.length(), | |
126 reinterpret_cast<const uint8_t*>(signature.data()), public_key); | |
127 return (result != 0); | |
128 } | |
129 | |
98 } // namespace content | 130 } // namespace content |
OLD | NEW |