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 "content/child/webcrypto/shared_crypto.h" | 5 #include "content/child/webcrypto/shared_crypto.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 3942 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3953 public_exponent), | 3953 public_exponent), |
3954 true, | 3954 true, |
3955 blink::WebCryptoKeyUsageSign, | 3955 blink::WebCryptoKeyUsageSign, |
3956 &public_key, | 3956 &public_key, |
3957 &private_key)); | 3957 &private_key)); |
3958 | 3958 |
3959 EXPECT_EQ(0, public_key.usages()); | 3959 EXPECT_EQ(0, public_key.usages()); |
3960 EXPECT_EQ(blink::WebCryptoKeyUsageSign, private_key.usages()); | 3960 EXPECT_EQ(blink::WebCryptoKeyUsageSign, private_key.usages()); |
3961 } | 3961 } |
3962 | 3962 |
3963 // Generate an AES-CBC key, and an RSA key pair. Use the AES-CBC key to wrap the | |
Ryan Sleevi
2014/05/24 18:18:38
unnecessary comma after key
| |
3964 // key pair (using SPKI format for public key, PKCS8 format for private key). | |
3965 // Then unwrap the wrapped key pair and verify that the key data is the same. | |
3966 TEST_F(SharedCryptoTest, MAYBE(WrapUnwrapRoundtripSpkiPkcs8UsingAesCbc)) { | |
3967 // Generate the wrapping key. | |
3968 blink::WebCryptoKey wrapping_key = blink::WebCryptoKey::createNull(); | |
3969 ASSERT_EQ(Status::Success(), | |
3970 GenerateSecretKey(CreateAesCbcKeyGenAlgorithm(128), | |
3971 true, | |
3972 blink::WebCryptoKeyUsageWrapKey | | |
3973 blink::WebCryptoKeyUsageUnwrapKey, | |
3974 &wrapping_key)); | |
3975 | |
3976 // Generate an RSA key pair to be wrapped. | |
3977 const unsigned int modulus_length = 256; | |
3978 const std::vector<uint8> public_exponent = HexStringToBytes("010001"); | |
3979 | |
3980 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | |
3981 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | |
3982 ASSERT_EQ(Status::Success(), | |
3983 GenerateKeyPair(CreateRsaHashedKeyGenAlgorithm( | |
3984 blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | |
3985 blink::WebCryptoAlgorithmIdSha256, | |
3986 modulus_length, | |
3987 public_exponent), | |
3988 true, | |
3989 0, | |
3990 &public_key, | |
3991 &private_key)); | |
3992 | |
3993 // Export key pair as SPKI + PKCS8 | |
3994 std::vector<uint8> public_key_spki; | |
3995 ASSERT_EQ( | |
3996 Status::Success(), | |
3997 ExportKey(blink::WebCryptoKeyFormatSpki, public_key, &public_key_spki)); | |
3998 | |
3999 std::vector<uint8> private_key_pkcs8; | |
4000 ASSERT_EQ( | |
4001 Status::Success(), | |
4002 ExportKey( | |
4003 blink::WebCryptoKeyFormatPkcs8, private_key, &private_key_pkcs8)); | |
4004 | |
4005 // Wrap the key pair. | |
4006 blink::WebCryptoAlgorithm wrap_algorithm = | |
4007 CreateAesCbcAlgorithm(std::vector<uint8>(16, 0)); | |
4008 | |
4009 std::vector<uint8> wrapped_public_key; | |
4010 ASSERT_EQ(Status::Success(), | |
4011 WrapKey(blink::WebCryptoKeyFormatSpki, | |
4012 public_key, | |
4013 wrapping_key, | |
4014 wrap_algorithm, | |
4015 &wrapped_public_key)); | |
4016 | |
4017 std::vector<uint8> wrapped_private_key; | |
4018 ASSERT_EQ(Status::Success(), | |
4019 WrapKey(blink::WebCryptoKeyFormatPkcs8, | |
4020 private_key, | |
4021 wrapping_key, | |
4022 wrap_algorithm, | |
4023 &wrapped_private_key)); | |
4024 | |
4025 // Unwrap the key pair. | |
4026 blink::WebCryptoAlgorithm rsa_import_algorithm = | |
4027 CreateRsaHashedImportAlgorithm(blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5, | |
4028 blink::WebCryptoAlgorithmIdSha256); | |
4029 | |
4030 blink::WebCryptoKey unwrapped_public_key = blink::WebCryptoKey::createNull(); | |
4031 | |
4032 ASSERT_EQ(Status::Success(), | |
4033 UnwrapKey(blink::WebCryptoKeyFormatSpki, | |
4034 CryptoData(wrapped_public_key), | |
4035 wrapping_key, | |
4036 wrap_algorithm, | |
4037 rsa_import_algorithm, | |
4038 true, | |
4039 0, | |
4040 &unwrapped_public_key)); | |
4041 | |
4042 blink::WebCryptoKey unwrapped_private_key = blink::WebCryptoKey::createNull(); | |
4043 | |
4044 ASSERT_EQ(Status::Success(), | |
4045 UnwrapKey(blink::WebCryptoKeyFormatPkcs8, | |
4046 CryptoData(wrapped_private_key), | |
4047 wrapping_key, | |
4048 wrap_algorithm, | |
4049 rsa_import_algorithm, | |
4050 true, | |
4051 0, | |
4052 &unwrapped_private_key)); | |
4053 | |
4054 // Export unwrapped key pair as SPKI + PKCS8 | |
4055 std::vector<uint8> unwrapped_public_key_spki; | |
4056 ASSERT_EQ(Status::Success(), | |
4057 ExportKey(blink::WebCryptoKeyFormatSpki, | |
4058 unwrapped_public_key, | |
4059 &unwrapped_public_key_spki)); | |
4060 | |
4061 std::vector<uint8> unwrapped_private_key_pkcs8; | |
4062 ASSERT_EQ(Status::Success(), | |
4063 ExportKey(blink::WebCryptoKeyFormatPkcs8, | |
4064 unwrapped_private_key, | |
4065 &unwrapped_private_key_pkcs8)); | |
4066 | |
4067 EXPECT_EQ(public_key_spki, unwrapped_public_key_spki); | |
4068 EXPECT_EQ(private_key_pkcs8, unwrapped_private_key_pkcs8); | |
4069 | |
4070 EXPECT_NE(public_key_spki, wrapped_public_key); | |
4071 EXPECT_NE(private_key_pkcs8, wrapped_private_key); | |
4072 } | |
4073 | |
3963 } // namespace webcrypto | 4074 } // namespace webcrypto |
3964 | 4075 |
3965 } // namespace content | 4076 } // namespace content |
OLD | NEW |