OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/child/webcrypto/test/test_helpers.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/files/file_util.h" | |
10 #include "base/json/json_reader.h" | |
11 #include "base/json/json_writer.h" | |
12 #include "base/logging.h" | |
13 #include "base/path_service.h" | |
14 #include "base/stl_util.h" | |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "base/strings/string_util.h" | |
17 #include "base/values.h" | |
18 #include "content/child/webcrypto/algorithm_dispatch.h" | |
19 #include "content/child/webcrypto/crypto_data.h" | |
20 #include "content/child/webcrypto/generate_key_result.h" | |
21 #include "content/child/webcrypto/jwk.h" | |
22 #include "content/child/webcrypto/status.h" | |
23 #include "content/child/webcrypto/webcrypto_util.h" | |
24 #include "content/public/common/content_paths.h" | |
25 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | |
26 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
27 #include "third_party/re2/re2/re2.h" | |
28 | |
29 #if !defined(USE_OPENSSL) | |
30 #include <nss.h> | |
31 #include <pk11pub.h> | |
32 | |
33 #include "crypto/nss_util.h" | |
34 #include "crypto/scoped_nss_types.h" | |
35 #endif | |
36 | |
37 namespace content { | |
38 | |
39 namespace webcrypto { | |
40 | |
41 void PrintTo(const Status& status, ::std::ostream* os) { | |
42 *os << StatusToString(status); | |
43 } | |
44 | |
45 bool operator==(const Status& a, const Status& b) { | |
46 if (a.IsSuccess() != b.IsSuccess()) | |
47 return false; | |
48 if (a.IsSuccess()) | |
49 return true; | |
50 return a.error_type() == b.error_type() && | |
51 a.error_details() == b.error_details(); | |
52 } | |
53 | |
54 bool operator!=(const Status& a, const Status& b) { | |
55 return !(a == b); | |
56 } | |
57 | |
58 void PrintTo(const CryptoData& data, ::std::ostream* os) { | |
59 *os << "[" << base::HexEncode(data.bytes(), data.byte_length()) << "]"; | |
60 } | |
61 | |
62 bool operator==(const CryptoData& a, const CryptoData& b) { | |
63 return a.byte_length() == b.byte_length() && | |
64 memcmp(a.bytes(), b.bytes(), a.byte_length()) == 0; | |
65 } | |
66 | |
67 bool operator!=(const CryptoData& a, const CryptoData& b) { | |
68 return !(a == b); | |
69 } | |
70 | |
71 static std::string ErrorTypeToString(blink::WebCryptoErrorType type) { | |
72 switch (type) { | |
73 case blink::WebCryptoErrorTypeNotSupported: | |
74 return "NotSupported"; | |
75 case blink::WebCryptoErrorTypeType: | |
76 return "TypeError"; | |
77 case blink::WebCryptoErrorTypeData: | |
78 return "DataError"; | |
79 case blink::WebCryptoErrorTypeSyntax: | |
80 return "SyntaxError"; | |
81 case blink::WebCryptoErrorTypeOperation: | |
82 return "OperationError"; | |
83 case blink::WebCryptoErrorTypeInvalidAccess: | |
84 return "InvalidAccess"; | |
85 default: | |
86 return "?"; | |
87 } | |
88 } | |
89 | |
90 std::string StatusToString(const Status& status) { | |
91 if (status.IsSuccess()) | |
92 return "Success"; | |
93 | |
94 std::string result = ErrorTypeToString(status.error_type()); | |
95 if (!status.error_details().empty()) | |
96 result += ": " + status.error_details(); | |
97 return result; | |
98 } | |
99 | |
100 bool SupportsAesGcm() { | |
101 std::vector<uint8_t> key_raw(16, 0); | |
102 | |
103 blink::WebCryptoKey key; | |
104 Status status = ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(key_raw), | |
105 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesGcm), | |
106 true, blink::WebCryptoKeyUsageEncrypt, &key); | |
107 | |
108 if (status.IsError()) | |
109 EXPECT_EQ(blink::WebCryptoErrorTypeNotSupported, status.error_type()); | |
110 return status.IsSuccess(); | |
111 } | |
112 | |
113 bool SupportsRsaOaep() { | |
114 #if defined(USE_OPENSSL) | |
115 return true; | |
116 #else | |
117 crypto::EnsureNSSInit(); | |
118 // TODO(eroman): Exclude version test for OS_CHROMEOS | |
119 #if defined(USE_NSS) | |
120 if (!NSS_VersionCheck("3.16.2")) | |
121 return false; | |
122 #endif | |
123 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); | |
124 return !!PK11_DoesMechanism(slot.get(), CKM_RSA_PKCS_OAEP); | |
125 #endif | |
126 } | |
127 | |
128 bool SupportsRsaPrivateKeyImport() { | |
129 // TODO(eroman): Exclude version test for OS_CHROMEOS | |
130 #if defined(USE_NSS) | |
131 crypto::EnsureNSSInit(); | |
132 if (!NSS_VersionCheck("3.16.2")) { | |
133 LOG(WARNING) << "RSA key import is not supported by this version of NSS. " | |
134 "Skipping some tests"; | |
135 return false; | |
136 } | |
137 #endif | |
138 return true; | |
139 } | |
140 | |
141 blink::WebCryptoAlgorithm CreateRsaHashedKeyGenAlgorithm( | |
142 blink::WebCryptoAlgorithmId algorithm_id, | |
143 const blink::WebCryptoAlgorithmId hash_id, | |
144 unsigned int modulus_length, | |
145 const std::vector<uint8_t>& public_exponent) { | |
146 DCHECK(blink::WebCryptoAlgorithm::isHash(hash_id)); | |
147 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | |
148 algorithm_id, | |
149 new blink::WebCryptoRsaHashedKeyGenParams( | |
150 CreateAlgorithm(hash_id), modulus_length, | |
151 vector_as_array(&public_exponent), public_exponent.size())); | |
152 } | |
153 | |
154 std::vector<uint8_t> Corrupted(const std::vector<uint8_t>& input) { | |
155 std::vector<uint8_t> corrupted_data(input); | |
156 if (corrupted_data.empty()) | |
157 corrupted_data.push_back(0); | |
158 corrupted_data[corrupted_data.size() / 2] ^= 0x01; | |
159 return corrupted_data; | |
160 } | |
161 | |
162 std::vector<uint8_t> HexStringToBytes(const std::string& hex) { | |
163 std::vector<uint8_t> bytes; | |
164 base::HexStringToBytes(hex, &bytes); | |
165 return bytes; | |
166 } | |
167 | |
168 std::vector<uint8_t> MakeJsonVector(const std::string& json_string) { | |
169 return std::vector<uint8_t>(json_string.begin(), json_string.end()); | |
170 } | |
171 | |
172 std::vector<uint8_t> MakeJsonVector(const base::DictionaryValue& dict) { | |
173 std::string json; | |
174 base::JSONWriter::Write(&dict, &json); | |
175 return MakeJsonVector(json); | |
176 } | |
177 | |
178 ::testing::AssertionResult ReadJsonTestFile(const char* test_file_name, | |
179 scoped_ptr<base::Value>* value) { | |
180 base::FilePath test_data_dir; | |
181 if (!PathService::Get(DIR_TEST_DATA, &test_data_dir)) | |
182 return ::testing::AssertionFailure() << "Couldn't retrieve test dir"; | |
183 | |
184 base::FilePath file_path = | |
185 test_data_dir.AppendASCII("webcrypto").AppendASCII(test_file_name); | |
186 | |
187 std::string file_contents; | |
188 if (!base::ReadFileToString(file_path, &file_contents)) { | |
189 return ::testing::AssertionFailure() | |
190 << "Couldn't read test file: " << file_path.value(); | |
191 } | |
192 | |
193 // Strip C++ style comments out of the "json" file, otherwise it cannot be | |
194 // parsed. | |
195 re2::RE2::GlobalReplace(&file_contents, re2::RE2("\\s*//.*"), ""); | |
196 | |
197 // Parse the JSON to a dictionary. | |
198 value->reset(base::JSONReader::Read(file_contents)); | |
199 if (!value->get()) { | |
200 return ::testing::AssertionFailure() | |
201 << "Couldn't parse test file JSON: " << file_path.value(); | |
202 } | |
203 | |
204 return ::testing::AssertionSuccess(); | |
205 } | |
206 | |
207 ::testing::AssertionResult ReadJsonTestFileToList( | |
208 const char* test_file_name, | |
209 scoped_ptr<base::ListValue>* list) { | |
210 // Read the JSON. | |
211 scoped_ptr<base::Value> json; | |
212 ::testing::AssertionResult result = ReadJsonTestFile(test_file_name, &json); | |
213 if (!result) | |
214 return result; | |
215 | |
216 // Cast to an ListValue. | |
217 base::ListValue* list_value = NULL; | |
218 if (!json->GetAsList(&list_value) || !list_value) | |
219 return ::testing::AssertionFailure() << "The JSON was not a list"; | |
220 | |
221 list->reset(list_value); | |
222 ignore_result(json.release()); | |
223 | |
224 return ::testing::AssertionSuccess(); | |
225 } | |
226 | |
227 ::testing::AssertionResult ReadJsonTestFileToDictionary( | |
228 const char* test_file_name, | |
229 scoped_ptr<base::DictionaryValue>* dict) { | |
230 // Read the JSON. | |
231 scoped_ptr<base::Value> json; | |
232 ::testing::AssertionResult result = ReadJsonTestFile(test_file_name, &json); | |
233 if (!result) | |
234 return result; | |
235 | |
236 // Cast to an DictionaryValue. | |
237 base::DictionaryValue* dict_value = NULL; | |
238 if (!json->GetAsDictionary(&dict_value) || !dict_value) | |
239 return ::testing::AssertionFailure() << "The JSON was not a dictionary"; | |
240 | |
241 dict->reset(dict_value); | |
242 ignore_result(json.release()); | |
243 | |
244 return ::testing::AssertionSuccess(); | |
245 } | |
246 | |
247 std::vector<uint8_t> GetBytesFromHexString(const base::DictionaryValue* dict, | |
248 const std::string& property_name) { | |
249 std::string hex_string; | |
250 if (!dict->GetString(property_name, &hex_string)) { | |
251 ADD_FAILURE() << "Couldn't get string property: " << property_name; | |
252 return std::vector<uint8_t>(); | |
253 } | |
254 | |
255 return HexStringToBytes(hex_string); | |
256 } | |
257 | |
258 blink::WebCryptoAlgorithm GetDigestAlgorithm(const base::DictionaryValue* dict, | |
259 const char* property_name) { | |
260 std::string algorithm_name; | |
261 if (!dict->GetString(property_name, &algorithm_name)) { | |
262 ADD_FAILURE() << "Couldn't get string property: " << property_name; | |
263 return blink::WebCryptoAlgorithm::createNull(); | |
264 } | |
265 | |
266 struct { | |
267 const char* name; | |
268 blink::WebCryptoAlgorithmId id; | |
269 } kDigestNameToId[] = { | |
270 {"sha-1", blink::WebCryptoAlgorithmIdSha1}, | |
271 {"sha-256", blink::WebCryptoAlgorithmIdSha256}, | |
272 {"sha-384", blink::WebCryptoAlgorithmIdSha384}, | |
273 {"sha-512", blink::WebCryptoAlgorithmIdSha512}, | |
274 }; | |
275 | |
276 for (size_t i = 0; i < arraysize(kDigestNameToId); ++i) { | |
277 if (kDigestNameToId[i].name == algorithm_name) | |
278 return CreateAlgorithm(kDigestNameToId[i].id); | |
279 } | |
280 | |
281 return blink::WebCryptoAlgorithm::createNull(); | |
282 } | |
283 | |
284 // Creates a comparator for |bufs| which operates on indices rather than values. | |
285 class CompareUsingIndex { | |
286 public: | |
287 explicit CompareUsingIndex(const std::vector<std::vector<uint8_t>>* bufs) | |
288 : bufs_(bufs) {} | |
289 | |
290 bool operator()(size_t i1, size_t i2) { return (*bufs_)[i1] < (*bufs_)[i2]; } | |
291 | |
292 private: | |
293 const std::vector<std::vector<uint8_t>>* bufs_; | |
294 }; | |
295 | |
296 bool CopiesExist(const std::vector<std::vector<uint8_t>>& bufs) { | |
297 // Sort the indices of |bufs| into a separate vector. This reduces the amount | |
298 // of data copied versus sorting |bufs| directly. | |
299 std::vector<size_t> sorted_indices(bufs.size()); | |
300 for (size_t i = 0; i < sorted_indices.size(); ++i) | |
301 sorted_indices[i] = i; | |
302 std::sort(sorted_indices.begin(), sorted_indices.end(), | |
303 CompareUsingIndex(&bufs)); | |
304 | |
305 // Scan for adjacent duplicates. | |
306 for (size_t i = 1; i < sorted_indices.size(); ++i) { | |
307 if (bufs[sorted_indices[i]] == bufs[sorted_indices[i - 1]]) | |
308 return true; | |
309 } | |
310 return false; | |
311 } | |
312 | |
313 blink::WebCryptoAlgorithm CreateAesKeyGenAlgorithm( | |
314 blink::WebCryptoAlgorithmId aes_alg_id, | |
315 unsigned short length) { | |
316 return blink::WebCryptoAlgorithm::adoptParamsAndCreate( | |
317 aes_alg_id, new blink::WebCryptoAesKeyGenParams(length)); | |
318 } | |
319 | |
320 // The following key pair is comprised of the SPKI (public key) and PKCS#8 | |
321 // (private key) representations of the key pair provided in Example 1 of the | |
322 // NIST test vectors at | |
323 // ftp://ftp.rsa.com/pub/rsalabs/tmp/pkcs1v15sign-vectors.txt | |
324 const unsigned int kModulusLengthBits = 1024; | |
325 const char* const kPublicKeySpkiDerHex = | |
326 "30819f300d06092a864886f70d010101050003818d0030818902818100a5" | |
327 "6e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad9" | |
328 "91d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfc" | |
329 "e0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e" | |
330 "6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cf" | |
331 "fb2249bd9a21370203010001"; | |
332 const char* const kPrivateKeyPkcs8DerHex = | |
333 "30820275020100300d06092a864886f70d01010105000482025f3082025b" | |
334 "02010002818100a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52" | |
335 "a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab" | |
336 "7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921c" | |
337 "b23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef" | |
338 "22e1e1f20d0ce8cffb2249bd9a2137020301000102818033a5042a90b27d" | |
339 "4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c" | |
340 "568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee" | |
341 "896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31" | |
342 "b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b3" | |
343 "25024100e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e8629" | |
344 "6b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b" | |
345 "3b6dcd3eda8e6443024100b69dca1cf7d4d7ec81e75b90fcca874abcde12" | |
346 "3fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc72" | |
347 "3e6963364a1f9425452b269a6799fd024028fa13938655be1f8a159cbaca" | |
348 "5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8d" | |
349 "d3ede2448328f385d81b30e8e43b2fffa02786197902401a8b38f398fa71" | |
350 "2049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd" | |
351 "48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729024027" | |
352 "156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319" | |
353 "584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24" | |
354 "a79f4d"; | |
355 // The modulus and exponent (in hex) of kPublicKeySpkiDerHex | |
356 const char* const kPublicKeyModulusHex = | |
357 "A56E4A0E701017589A5187DC7EA841D156F2EC0E36AD52A44DFEB1E61F7AD991D8C51056" | |
358 "FFEDB162B4C0F283A12A88A394DFF526AB7291CBB307CEABFCE0B1DFD5CD9508096D5B2B" | |
359 "8B6DF5D671EF6377C0921CB23C270A70E2598E6FF89D19F105ACC2D3F0CB35F29280E138" | |
360 "6B6F64C4EF22E1E1F20D0CE8CFFB2249BD9A2137"; | |
361 const char* const kPublicKeyExponentHex = "010001"; | |
362 | |
363 blink::WebCryptoKey ImportSecretKeyFromRaw( | |
364 const std::vector<uint8_t>& key_raw, | |
365 const blink::WebCryptoAlgorithm& algorithm, | |
366 blink::WebCryptoKeyUsageMask usage) { | |
367 blink::WebCryptoKey key; | |
368 bool extractable = true; | |
369 EXPECT_EQ(Status::Success(), | |
370 ImportKey(blink::WebCryptoKeyFormatRaw, CryptoData(key_raw), | |
371 algorithm, extractable, usage, &key)); | |
372 | |
373 EXPECT_FALSE(key.isNull()); | |
374 EXPECT_TRUE(key.handle()); | |
375 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | |
376 EXPECT_EQ(algorithm.id(), key.algorithm().id()); | |
377 EXPECT_EQ(extractable, key.extractable()); | |
378 EXPECT_EQ(usage, key.usages()); | |
379 return key; | |
380 } | |
381 | |
382 void ImportRsaKeyPair(const std::vector<uint8_t>& spki_der, | |
383 const std::vector<uint8_t>& pkcs8_der, | |
384 const blink::WebCryptoAlgorithm& algorithm, | |
385 bool extractable, | |
386 blink::WebCryptoKeyUsageMask public_key_usages, | |
387 blink::WebCryptoKeyUsageMask private_key_usages, | |
388 blink::WebCryptoKey* public_key, | |
389 blink::WebCryptoKey* private_key) { | |
390 ASSERT_EQ(Status::Success(), | |
391 ImportKey(blink::WebCryptoKeyFormatSpki, CryptoData(spki_der), | |
392 algorithm, true, public_key_usages, public_key)); | |
393 EXPECT_FALSE(public_key->isNull()); | |
394 EXPECT_TRUE(public_key->handle()); | |
395 EXPECT_EQ(blink::WebCryptoKeyTypePublic, public_key->type()); | |
396 EXPECT_EQ(algorithm.id(), public_key->algorithm().id()); | |
397 EXPECT_TRUE(public_key->extractable()); | |
398 EXPECT_EQ(public_key_usages, public_key->usages()); | |
399 | |
400 ASSERT_EQ(Status::Success(), | |
401 ImportKey(blink::WebCryptoKeyFormatPkcs8, CryptoData(pkcs8_der), | |
402 algorithm, extractable, private_key_usages, private_key)); | |
403 EXPECT_FALSE(private_key->isNull()); | |
404 EXPECT_TRUE(private_key->handle()); | |
405 EXPECT_EQ(blink::WebCryptoKeyTypePrivate, private_key->type()); | |
406 EXPECT_EQ(algorithm.id(), private_key->algorithm().id()); | |
407 EXPECT_EQ(extractable, private_key->extractable()); | |
408 EXPECT_EQ(private_key_usages, private_key->usages()); | |
409 } | |
410 | |
411 Status ImportKeyJwkFromDict(const base::DictionaryValue& dict, | |
412 const blink::WebCryptoAlgorithm& algorithm, | |
413 bool extractable, | |
414 blink::WebCryptoKeyUsageMask usages, | |
415 blink::WebCryptoKey* key) { | |
416 return ImportKey(blink::WebCryptoKeyFormatJwk, | |
417 CryptoData(MakeJsonVector(dict)), algorithm, extractable, | |
418 usages, key); | |
419 } | |
420 | |
421 scoped_ptr<base::DictionaryValue> GetJwkDictionary( | |
422 const std::vector<uint8_t>& json) { | |
423 base::StringPiece json_string( | |
424 reinterpret_cast<const char*>(vector_as_array(&json)), json.size()); | |
425 base::Value* value = base::JSONReader::Read(json_string); | |
426 EXPECT_TRUE(value); | |
427 base::DictionaryValue* dict_value = NULL; | |
428 value->GetAsDictionary(&dict_value); | |
429 return scoped_ptr<base::DictionaryValue>(dict_value); | |
430 } | |
431 | |
432 // Verifies the input dictionary contains the expected values. Exact matches are | |
433 // required on the fields examined. | |
434 ::testing::AssertionResult VerifyJwk( | |
435 const scoped_ptr<base::DictionaryValue>& dict, | |
436 const std::string& kty_expected, | |
437 const std::string& alg_expected, | |
438 blink::WebCryptoKeyUsageMask use_mask_expected) { | |
439 // ---- kty | |
440 std::string value_string; | |
441 if (!dict->GetString("kty", &value_string)) | |
442 return ::testing::AssertionFailure() << "Missing 'kty'"; | |
443 if (value_string != kty_expected) | |
444 return ::testing::AssertionFailure() << "Expected 'kty' to be " | |
445 << kty_expected << "but found " | |
446 << value_string; | |
447 | |
448 // ---- alg | |
449 if (!dict->GetString("alg", &value_string)) | |
450 return ::testing::AssertionFailure() << "Missing 'alg'"; | |
451 if (value_string != alg_expected) | |
452 return ::testing::AssertionFailure() << "Expected 'alg' to be " | |
453 << alg_expected << " but found " | |
454 << value_string; | |
455 | |
456 // ---- ext | |
457 // always expect ext == true in this case | |
458 bool ext_value; | |
459 if (!dict->GetBoolean("ext", &ext_value)) | |
460 return ::testing::AssertionFailure() << "Missing 'ext'"; | |
461 if (!ext_value) | |
462 return ::testing::AssertionFailure() | |
463 << "Expected 'ext' to be true but found false"; | |
464 | |
465 // ---- key_ops | |
466 base::ListValue* key_ops; | |
467 if (!dict->GetList("key_ops", &key_ops)) | |
468 return ::testing::AssertionFailure() << "Missing 'key_ops'"; | |
469 blink::WebCryptoKeyUsageMask key_ops_mask = 0; | |
470 Status status = | |
471 GetWebCryptoUsagesFromJwkKeyOpsForTest(key_ops, &key_ops_mask); | |
472 if (status.IsError()) | |
473 return ::testing::AssertionFailure() << "Failure extracting 'key_ops'"; | |
474 if (key_ops_mask != use_mask_expected) | |
475 return ::testing::AssertionFailure() | |
476 << "Expected 'key_ops' mask to be " << use_mask_expected | |
477 << " but found " << key_ops_mask << " (" << value_string << ")"; | |
478 | |
479 return ::testing::AssertionSuccess(); | |
480 } | |
481 | |
482 ::testing::AssertionResult VerifySecretJwk( | |
483 const std::vector<uint8_t>& json, | |
484 const std::string& alg_expected, | |
485 const std::string& k_expected_hex, | |
486 blink::WebCryptoKeyUsageMask use_mask_expected) { | |
487 scoped_ptr<base::DictionaryValue> dict = GetJwkDictionary(json); | |
488 if (!dict.get() || dict->empty()) | |
489 return ::testing::AssertionFailure() << "JSON parsing failed"; | |
490 | |
491 // ---- k | |
492 std::string value_string; | |
493 if (!dict->GetString("k", &value_string)) | |
494 return ::testing::AssertionFailure() << "Missing 'k'"; | |
495 std::string k_value; | |
496 if (!Base64DecodeUrlSafe(value_string, &k_value)) | |
497 return ::testing::AssertionFailure() << "Base64DecodeUrlSafe(k) failed"; | |
498 if (!LowerCaseEqualsASCII(base::HexEncode(k_value.data(), k_value.size()), | |
499 k_expected_hex.c_str())) { | |
500 return ::testing::AssertionFailure() << "Expected 'k' to be " | |
501 << k_expected_hex | |
502 << " but found something different"; | |
503 } | |
504 | |
505 return VerifyJwk(dict, "oct", alg_expected, use_mask_expected); | |
506 } | |
507 | |
508 ::testing::AssertionResult VerifyPublicJwk( | |
509 const std::vector<uint8_t>& json, | |
510 const std::string& alg_expected, | |
511 const std::string& n_expected_hex, | |
512 const std::string& e_expected_hex, | |
513 blink::WebCryptoKeyUsageMask use_mask_expected) { | |
514 scoped_ptr<base::DictionaryValue> dict = GetJwkDictionary(json); | |
515 if (!dict.get() || dict->empty()) | |
516 return ::testing::AssertionFailure() << "JSON parsing failed"; | |
517 | |
518 // ---- n | |
519 std::string value_string; | |
520 if (!dict->GetString("n", &value_string)) | |
521 return ::testing::AssertionFailure() << "Missing 'n'"; | |
522 std::string n_value; | |
523 if (!Base64DecodeUrlSafe(value_string, &n_value)) | |
524 return ::testing::AssertionFailure() << "Base64DecodeUrlSafe(n) failed"; | |
525 if (base::HexEncode(n_value.data(), n_value.size()) != n_expected_hex) { | |
526 return ::testing::AssertionFailure() << "'n' does not match the expected " | |
527 "value"; | |
528 } | |
529 // TODO(padolph): LowerCaseEqualsASCII() does not work for above! | |
530 | |
531 // ---- e | |
532 if (!dict->GetString("e", &value_string)) | |
533 return ::testing::AssertionFailure() << "Missing 'e'"; | |
534 std::string e_value; | |
535 if (!Base64DecodeUrlSafe(value_string, &e_value)) | |
536 return ::testing::AssertionFailure() << "Base64DecodeUrlSafe(e) failed"; | |
537 if (!LowerCaseEqualsASCII(base::HexEncode(e_value.data(), e_value.size()), | |
538 e_expected_hex.c_str())) { | |
539 return ::testing::AssertionFailure() << "Expected 'e' to be " | |
540 << e_expected_hex | |
541 << " but found something different"; | |
542 } | |
543 | |
544 return VerifyJwk(dict, "RSA", alg_expected, use_mask_expected); | |
545 } | |
546 | |
547 void ImportExportJwkSymmetricKey( | |
548 int key_len_bits, | |
549 const blink::WebCryptoAlgorithm& import_algorithm, | |
550 blink::WebCryptoKeyUsageMask usages, | |
551 const std::string& jwk_alg) { | |
552 std::vector<uint8_t> json; | |
553 std::string key_hex; | |
554 | |
555 // Hardcoded pseudo-random bytes to use for keys of different lengths. | |
556 switch (key_len_bits) { | |
557 case 128: | |
558 key_hex = "3f1e7cd4f6f8543f6b1e16002e688623"; | |
559 break; | |
560 case 256: | |
561 key_hex = | |
562 "bd08286b81a74783fd1ccf46b7e05af84ee25ae021210074159e0c4d9d907692"; | |
563 break; | |
564 case 384: | |
565 key_hex = | |
566 "a22c5441c8b185602283d64c7221de1d0951e706bfc09539435ec0e0ed614e1d40" | |
567 "6623f2b31d31819fec30993380dd82"; | |
568 break; | |
569 case 512: | |
570 key_hex = | |
571 "5834f639000d4cf82de124fbfd26fb88d463e99f839a76ba41ac88967c80a3f61e" | |
572 "1239a452e573dba0750e988152988576efd75b8d0229b7aca2ada2afd392ee"; | |
573 break; | |
574 default: | |
575 FAIL() << "Unexpected key_len_bits" << key_len_bits; | |
576 } | |
577 | |
578 // Import a raw key. | |
579 blink::WebCryptoKey key = ImportSecretKeyFromRaw(HexStringToBytes(key_hex), | |
580 import_algorithm, usages); | |
581 | |
582 // Export the key in JWK format and validate. | |
583 ASSERT_EQ(Status::Success(), | |
584 ExportKey(blink::WebCryptoKeyFormatJwk, key, &json)); | |
585 EXPECT_TRUE(VerifySecretJwk(json, jwk_alg, key_hex, usages)); | |
586 | |
587 // Import the JWK-formatted key. | |
588 ASSERT_EQ(Status::Success(), | |
589 ImportKey(blink::WebCryptoKeyFormatJwk, CryptoData(json), | |
590 import_algorithm, true, usages, &key)); | |
591 EXPECT_TRUE(key.handle()); | |
592 EXPECT_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | |
593 EXPECT_EQ(import_algorithm.id(), key.algorithm().id()); | |
594 EXPECT_EQ(true, key.extractable()); | |
595 EXPECT_EQ(usages, key.usages()); | |
596 | |
597 // Export the key in raw format and compare to the original. | |
598 std::vector<uint8_t> key_raw_out; | |
599 ASSERT_EQ(Status::Success(), | |
600 ExportKey(blink::WebCryptoKeyFormatRaw, key, &key_raw_out)); | |
601 EXPECT_BYTES_EQ_HEX(key_hex, key_raw_out); | |
602 } | |
603 | |
604 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | |
605 bool extractable, | |
606 blink::WebCryptoKeyUsageMask usages, | |
607 blink::WebCryptoKey* key) { | |
608 GenerateKeyResult result; | |
609 Status status = GenerateKey(algorithm, extractable, usages, &result); | |
610 if (status.IsError()) | |
611 return status; | |
612 | |
613 if (result.type() != GenerateKeyResult::TYPE_SECRET_KEY) | |
614 return Status::ErrorUnexpected(); | |
615 | |
616 *key = result.secret_key(); | |
617 | |
618 return Status::Success(); | |
619 } | |
620 | |
621 Status GenerateKeyPair(const blink::WebCryptoAlgorithm& algorithm, | |
622 bool extractable, | |
623 blink::WebCryptoKeyUsageMask usages, | |
624 blink::WebCryptoKey* public_key, | |
625 blink::WebCryptoKey* private_key) { | |
626 GenerateKeyResult result; | |
627 Status status = GenerateKey(algorithm, extractable, usages, &result); | |
628 if (status.IsError()) | |
629 return status; | |
630 | |
631 if (result.type() != GenerateKeyResult::TYPE_PUBLIC_PRIVATE_KEY_PAIR) | |
632 return Status::ErrorUnexpected(); | |
633 | |
634 *public_key = result.public_key(); | |
635 *private_key = result.private_key(); | |
636 | |
637 return Status::Success(); | |
638 } | |
639 | |
640 blink::WebCryptoKeyFormat GetKeyFormatFromJsonTestCase( | |
641 const base::DictionaryValue* test) { | |
642 std::string format; | |
643 EXPECT_TRUE(test->GetString("key_format", &format)); | |
644 if (format == "jwk") | |
645 return blink::WebCryptoKeyFormatJwk; | |
646 else if (format == "pkcs8") | |
647 return blink::WebCryptoKeyFormatPkcs8; | |
648 else if (format == "spki") | |
649 return blink::WebCryptoKeyFormatSpki; | |
650 else if (format == "raw") | |
651 return blink::WebCryptoKeyFormatRaw; | |
652 | |
653 ADD_FAILURE() << "Unrecognized key format: " << format; | |
654 return blink::WebCryptoKeyFormatRaw; | |
655 } | |
656 | |
657 std::vector<uint8_t> GetKeyDataFromJsonTestCase( | |
658 const base::DictionaryValue* test, | |
659 blink::WebCryptoKeyFormat key_format) { | |
660 if (key_format == blink::WebCryptoKeyFormatJwk) { | |
661 const base::DictionaryValue* json; | |
662 EXPECT_TRUE(test->GetDictionary("key", &json)); | |
663 return MakeJsonVector(*json); | |
664 } | |
665 return GetBytesFromHexString(test, "key"); | |
666 } | |
667 | |
668 blink::WebCryptoNamedCurve GetCurveNameFromDictionary( | |
669 const base::DictionaryValue* dict) { | |
670 std::string curve_str; | |
671 if (!dict->GetString("crv", &curve_str)) { | |
672 ADD_FAILURE() << "Missing crv parameter"; | |
673 return blink::WebCryptoNamedCurveP384; | |
674 } | |
675 | |
676 if (curve_str == "P-256") | |
677 return blink::WebCryptoNamedCurveP256; | |
678 if (curve_str == "P-384") | |
679 return blink::WebCryptoNamedCurveP384; | |
680 if (curve_str == "P-521") | |
681 return blink::WebCryptoNamedCurveP521; | |
682 else | |
683 ADD_FAILURE() << "Unrecognized curve name: " << curve_str; | |
684 | |
685 return blink::WebCryptoNamedCurveP384; | |
686 } | |
687 | |
688 } // namespace webcrypto | |
689 | |
690 } // namesapce content | |
OLD | NEW |