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