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/renderer/origin_trials/trial_token.h" | 5 #include "content/renderer/origin_trials/trial_token.h" |
6 | 6 |
7 #include <openssl/curve25519.h> | 7 #include <openssl/curve25519.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "url/gurl.h" |
18 #include "url/origin.h" | 19 #include "url/origin.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Version 1 is the only token version currently supported | 25 // Version 1 is the only token version currently supported |
25 const uint8_t kVersion1 = 1; | 26 const uint8_t kVersion1 = 1; |
26 | 27 |
27 const char* kFieldSeparator = "|"; | 28 const char* kFieldSeparator = "|"; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 const std::string& origin_string = parts[1]; | 71 const std::string& origin_string = parts[1]; |
71 const std::string& feature_name = parts[2]; | 72 const std::string& feature_name = parts[2]; |
72 const std::string& expiry_string = parts[3]; | 73 const std::string& expiry_string = parts[3]; |
73 | 74 |
74 uint64_t expiry_timestamp; | 75 uint64_t expiry_timestamp; |
75 if (!base::StringToUint64(expiry_string, &expiry_timestamp)) { | 76 if (!base::StringToUint64(expiry_string, &expiry_timestamp)) { |
76 return nullptr; | 77 return nullptr; |
77 } | 78 } |
78 | 79 |
79 // Ensure that the origin is a valid (non-unique) origin URL | 80 // Ensure that the origin is a valid (non-unique) origin URL |
80 GURL origin_url(origin_string); | 81 url::Origin origin = url::Origin(GURL(origin_string)); |
81 if (url::Origin(origin_url).unique()) { | 82 if (origin.unique()) { |
82 return nullptr; | 83 return nullptr; |
83 } | 84 } |
84 | 85 |
85 // Signed data is (origin + "|" + feature_name + "|" + expiry). | 86 // Signed data is (origin + "|" + feature_name + "|" + expiry). |
86 std::string data = token_contents.substr(signature.length() + 1); | 87 std::string data = token_contents.substr(signature.length() + 1); |
87 | 88 |
88 return make_scoped_ptr(new TrialToken(version, signature, data, origin_url, | 89 return make_scoped_ptr(new TrialToken(version, signature, data, origin, |
89 feature_name, expiry_timestamp)); | 90 feature_name, expiry_timestamp)); |
90 } | 91 } |
91 | 92 |
92 TrialToken::TrialToken(uint8_t version, | 93 bool TrialToken::IsAppropriate(const url::Origin& origin, |
93 const std::string& signature, | 94 base::StringPiece feature_name) const { |
94 const std::string& data, | |
95 const GURL& origin, | |
96 const std::string& feature_name, | |
97 uint64_t expiry_timestamp) | |
98 : version_(version), | |
99 signature_(signature), | |
100 data_(data), | |
101 origin_(origin), | |
102 feature_name_(feature_name), | |
103 expiry_timestamp_(expiry_timestamp) {} | |
104 | |
105 bool TrialToken::IsAppropriate(const std::string& origin, | |
106 const std::string& feature_name) const { | |
107 return ValidateOrigin(origin) && ValidateFeatureName(feature_name); | 95 return ValidateOrigin(origin) && ValidateFeatureName(feature_name); |
108 } | 96 } |
109 | 97 |
110 bool TrialToken::IsValid(const base::Time& now, | 98 bool TrialToken::IsValid(const base::Time& now, |
111 const base::StringPiece& public_key) const { | 99 base::StringPiece public_key) const { |
112 // TODO(iclelland): Allow for multiple signing keys, and iterate over all | 100 // TODO(iclelland): Allow for multiple signing keys, and iterate over all |
113 // active keys here. https://crbug.com/543220 | 101 // active keys here. https://crbug.com/543220 |
114 return ValidateDate(now) && ValidateSignature(public_key); | 102 return ValidateDate(now) && ValidateSignature(public_key); |
115 } | 103 } |
116 | 104 |
117 bool TrialToken::ValidateOrigin(const std::string& origin) const { | 105 bool TrialToken::ValidateOrigin(const url::Origin& origin) const { |
118 return GURL(origin) == origin_; | 106 return origin == origin_; |
119 } | 107 } |
120 | 108 |
121 bool TrialToken::ValidateFeatureName(const std::string& feature_name) const { | 109 bool TrialToken::ValidateFeatureName(base::StringPiece feature_name) const { |
122 return feature_name == feature_name_; | 110 return feature_name == feature_name_; |
123 } | 111 } |
124 | 112 |
125 bool TrialToken::ValidateDate(const base::Time& now) const { | 113 bool TrialToken::ValidateDate(const base::Time& now) const { |
126 base::Time expiry_time = base::Time::FromDoubleT((double)expiry_timestamp_); | 114 return expiry_time_ > now; |
127 return expiry_time > now; | |
128 } | 115 } |
129 | 116 |
130 bool TrialToken::ValidateSignature(const base::StringPiece& public_key) const { | 117 bool TrialToken::ValidateSignature(base::StringPiece public_key) const { |
131 return ValidateSignature(signature_, data_, public_key); | 118 return ValidateSignature(signature_, data_, public_key); |
132 } | 119 } |
133 | 120 |
134 // static | 121 // static |
135 bool TrialToken::ValidateSignature(const std::string& signature_text, | 122 bool TrialToken::ValidateSignature(const std::string& signature_text, |
136 const std::string& data, | 123 const std::string& data, |
137 const base::StringPiece& public_key) { | 124 base::StringPiece public_key) { |
138 // Public key must be 32 bytes long for Ed25519. | 125 // Public key must be 32 bytes long for Ed25519. |
139 CHECK_EQ(public_key.length(), 32UL); | 126 CHECK_EQ(public_key.length(), 32UL); |
140 | 127 |
141 std::string signature; | 128 std::string signature; |
142 // signature_text is base64-encoded; decode first. | 129 // signature_text is base64-encoded; decode first. |
143 if (!base::Base64Decode(signature_text, &signature)) { | 130 if (!base::Base64Decode(signature_text, &signature)) { |
144 return false; | 131 return false; |
145 } | 132 } |
146 | 133 |
147 // Signature must be 64 bytes long | 134 // Signature must be 64 bytes long |
148 if (signature.length() != 64) { | 135 if (signature.length() != 64) { |
149 return false; | 136 return false; |
150 } | 137 } |
151 | 138 |
152 int result = ED25519_verify( | 139 int result = ED25519_verify( |
153 reinterpret_cast<const uint8_t*>(data.data()), data.length(), | 140 reinterpret_cast<const uint8_t*>(data.data()), data.length(), |
154 reinterpret_cast<const uint8_t*>(signature.data()), | 141 reinterpret_cast<const uint8_t*>(signature.data()), |
155 reinterpret_cast<const uint8_t*>(public_key.data())); | 142 reinterpret_cast<const uint8_t*>(public_key.data())); |
156 return (result != 0); | 143 return (result != 0); |
157 } | 144 } |
158 | 145 |
| 146 TrialToken::TrialToken(uint8_t version, |
| 147 const std::string& signature, |
| 148 const std::string& data, |
| 149 const url::Origin& origin, |
| 150 const std::string& feature_name, |
| 151 uint64_t expiry_timestamp) |
| 152 : version_(version), |
| 153 signature_(signature), |
| 154 data_(data), |
| 155 origin_(origin), |
| 156 feature_name_(feature_name), |
| 157 expiry_time_(base::Time::FromDoubleT(expiry_timestamp)) {} |
| 158 |
159 } // namespace content | 159 } // namespace content |
OLD | NEW |