| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <limits.h> | 5 #include <limits.h> |
| 6 #include <stddef.h> | 6 #include <stddef.h> |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> |
| 10 |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
| 10 #include "base/values.h" | 13 #include "base/values.h" |
| 11 #include "components/webcrypto/algorithm_dispatch.h" | 14 #include "components/webcrypto/algorithm_dispatch.h" |
| 12 #include "components/webcrypto/algorithms/test_helpers.h" | 15 #include "components/webcrypto/algorithms/test_helpers.h" |
| 13 #include "components/webcrypto/crypto_data.h" | 16 #include "components/webcrypto/crypto_data.h" |
| 14 #include "components/webcrypto/status.h" | 17 #include "components/webcrypto/status.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 17 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 20 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 18 | 21 |
| 19 namespace webcrypto { | 22 namespace webcrypto { |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 CreateHmacImportAlgorithmNoLength( | 224 CreateHmacImportAlgorithmNoLength( |
| 222 blink::kWebCryptoAlgorithmIdSha1), | 225 blink::kWebCryptoAlgorithmIdSha1), |
| 223 true, 0, &key)); | 226 true, 0, &key)); |
| 224 } | 227 } |
| 225 | 228 |
| 226 TEST_F(WebCryptoHmacTest, ImportKeyJwkKeyOpsSignVerify) { | 229 TEST_F(WebCryptoHmacTest, ImportKeyJwkKeyOpsSignVerify) { |
| 227 blink::WebCryptoKey key; | 230 blink::WebCryptoKey key; |
| 228 base::DictionaryValue dict; | 231 base::DictionaryValue dict; |
| 229 dict.SetString("kty", "oct"); | 232 dict.SetString("kty", "oct"); |
| 230 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); | 233 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); |
| 231 base::ListValue* key_ops = new base::ListValue; | 234 base::ListValue* key_ops = |
| 232 dict.Set("key_ops", key_ops); // Takes ownership. | 235 dict.SetList("key_ops", base::MakeUnique<base::ListValue>()); |
| 233 | 236 |
| 234 key_ops->AppendString("sign"); | 237 key_ops->AppendString("sign"); |
| 235 | 238 |
| 236 EXPECT_EQ(Status::Success(), | 239 EXPECT_EQ(Status::Success(), |
| 237 ImportKeyJwkFromDict(dict, | 240 ImportKeyJwkFromDict(dict, |
| 238 CreateHmacImportAlgorithmNoLength( | 241 CreateHmacImportAlgorithmNoLength( |
| 239 blink::kWebCryptoAlgorithmIdSha256), | 242 blink::kWebCryptoAlgorithmIdSha256), |
| 240 false, blink::kWebCryptoKeyUsageSign, &key)); | 243 false, blink::kWebCryptoKeyUsageSign, &key)); |
| 241 | 244 |
| 242 EXPECT_EQ(blink::kWebCryptoKeyUsageSign, key.Usages()); | 245 EXPECT_EQ(blink::kWebCryptoKeyUsageSign, key.Usages()); |
| 243 | 246 |
| 244 key_ops->AppendString("verify"); | 247 key_ops->AppendString("verify"); |
| 245 | 248 |
| 246 EXPECT_EQ(Status::Success(), | 249 EXPECT_EQ(Status::Success(), |
| 247 ImportKeyJwkFromDict(dict, | 250 ImportKeyJwkFromDict(dict, |
| 248 CreateHmacImportAlgorithmNoLength( | 251 CreateHmacImportAlgorithmNoLength( |
| 249 blink::kWebCryptoAlgorithmIdSha256), | 252 blink::kWebCryptoAlgorithmIdSha256), |
| 250 false, blink::kWebCryptoKeyUsageVerify, &key)); | 253 false, blink::kWebCryptoKeyUsageVerify, &key)); |
| 251 | 254 |
| 252 EXPECT_EQ(blink::kWebCryptoKeyUsageVerify, key.Usages()); | 255 EXPECT_EQ(blink::kWebCryptoKeyUsageVerify, key.Usages()); |
| 253 } | 256 } |
| 254 | 257 |
| 255 // Test 'use' inconsistent with 'key_ops'. | 258 // Test 'use' inconsistent with 'key_ops'. |
| 256 TEST_F(WebCryptoHmacTest, ImportKeyJwkUseInconsisteWithKeyOps) { | 259 TEST_F(WebCryptoHmacTest, ImportKeyJwkUseInconsisteWithKeyOps) { |
| 257 blink::WebCryptoKey key; | 260 blink::WebCryptoKey key; |
| 258 base::DictionaryValue dict; | 261 base::DictionaryValue dict; |
| 259 dict.SetString("kty", "oct"); | 262 dict.SetString("kty", "oct"); |
| 260 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); | 263 dict.SetString("k", "GADWrMRHwQfoNaXU5fZvTg"); |
| 261 base::ListValue* key_ops = new base::ListValue; | |
| 262 dict.Set("key_ops", key_ops); // Takes ownership. | |
| 263 | |
| 264 dict.SetString("alg", "HS256"); | 264 dict.SetString("alg", "HS256"); |
| 265 dict.SetString("use", "sig"); | 265 dict.SetString("use", "sig"); |
| 266 |
| 267 auto key_ops = base::MakeUnique<base::ListValue>(); |
| 266 key_ops->AppendString("sign"); | 268 key_ops->AppendString("sign"); |
| 267 key_ops->AppendString("verify"); | 269 key_ops->AppendString("verify"); |
| 268 key_ops->AppendString("encrypt"); | 270 key_ops->AppendString("encrypt"); |
| 271 dict.Set("key_ops", std::move(key_ops)); |
| 269 EXPECT_EQ( | 272 EXPECT_EQ( |
| 270 Status::ErrorJwkUseAndKeyopsInconsistent(), | 273 Status::ErrorJwkUseAndKeyopsInconsistent(), |
| 271 ImportKeyJwkFromDict( | 274 ImportKeyJwkFromDict( |
| 272 dict, | 275 dict, |
| 273 CreateHmacImportAlgorithmNoLength(blink::kWebCryptoAlgorithmIdSha256), | 276 CreateHmacImportAlgorithmNoLength(blink::kWebCryptoAlgorithmIdSha256), |
| 274 false, | 277 false, |
| 275 blink::kWebCryptoKeyUsageSign | blink::kWebCryptoKeyUsageVerify, | 278 blink::kWebCryptoKeyUsageSign | blink::kWebCryptoKeyUsageVerify, |
| 276 &key)); | 279 &key)); |
| 277 } | 280 } |
| 278 | 281 |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 // On export the last 4 bits has been set to zero. | 607 // On export the last 4 bits has been set to zero. |
| 605 std::vector<uint8_t> raw_key; | 608 std::vector<uint8_t> raw_key; |
| 606 EXPECT_EQ(Status::Success(), | 609 EXPECT_EQ(Status::Success(), |
| 607 ExportKey(blink::kWebCryptoKeyFormatRaw, key, &raw_key)); | 610 ExportKey(blink::kWebCryptoKeyFormatRaw, key, &raw_key)); |
| 608 EXPECT_BYTES_EQ(HexStringToBytes("b1f0"), raw_key); | 611 EXPECT_BYTES_EQ(HexStringToBytes("b1f0"), raw_key); |
| 609 } | 612 } |
| 610 | 613 |
| 611 } // namespace | 614 } // namespace |
| 612 | 615 |
| 613 } // namespace webcrypto | 616 } // namespace webcrypto |
| OLD | NEW |