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 <vector> | |
6 #include <openssl/evp.h> | |
7 | |
8 #include "base/logging.h" | |
9 #include "base/stl_util.h" | 5 #include "base/stl_util.h" |
| 6 #include "content/child/webcrypto/algorithm_dispatch.h" |
10 #include "content/child/webcrypto/crypto_data.h" | 7 #include "content/child/webcrypto/crypto_data.h" |
11 #include "content/child/webcrypto/openssl/aes_key_openssl.h" | |
12 #include "content/child/webcrypto/openssl/key_openssl.h" | |
13 #include "content/child/webcrypto/openssl/util_openssl.h" | |
14 #include "content/child/webcrypto/status.h" | 8 #include "content/child/webcrypto/status.h" |
| 9 #include "content/child/webcrypto/test/test_helpers.h" |
15 #include "content/child/webcrypto/webcrypto_util.h" | 10 #include "content/child/webcrypto/webcrypto_util.h" |
16 #include "crypto/openssl_util.h" | 11 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
17 #include "crypto/scoped_openssl_types.h" | |
18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 13 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
19 | 14 |
20 namespace content { | 15 namespace content { |
21 | 16 |
22 namespace webcrypto { | 17 namespace webcrypto { |
23 | 18 |
24 namespace { | 19 namespace { |
25 | 20 |
26 const EVP_AEAD* GetAesGcmAlgorithmFromKeySize(unsigned int key_size_bytes) { | 21 // Tests several Status objects against their expected hard coded values, as |
27 switch (key_size_bytes) { | 22 // well as ensuring that comparison of Status objects works. |
28 case 16: | 23 // Comparison should take into account both the error details, as well as the |
29 return EVP_aead_aes_128_gcm(); | 24 // error type. |
30 case 32: | 25 TEST(WebCryptoStatusTest, Basic) { |
31 return EVP_aead_aes_256_gcm(); | 26 // Even though the error message is the same, these should not be considered |
32 default: | 27 // the same by the tests because the error type is different. |
33 return NULL; | 28 EXPECT_NE(Status::DataError(), Status::OperationError()); |
34 } | 29 EXPECT_NE(Status::Success(), Status::OperationError()); |
| 30 |
| 31 EXPECT_EQ(Status::Success(), Status::Success()); |
| 32 EXPECT_EQ(Status::ErrorJwkPropertyWrongType("kty", "string"), |
| 33 Status::ErrorJwkPropertyWrongType("kty", "string")); |
| 34 |
| 35 Status status = Status::Success(); |
| 36 |
| 37 EXPECT_FALSE(status.IsError()); |
| 38 EXPECT_EQ("", status.error_details()); |
| 39 |
| 40 status = Status::OperationError(); |
| 41 EXPECT_TRUE(status.IsError()); |
| 42 EXPECT_EQ("", status.error_details()); |
| 43 EXPECT_EQ(blink::WebCryptoErrorTypeOperation, status.error_type()); |
| 44 |
| 45 status = Status::DataError(); |
| 46 EXPECT_TRUE(status.IsError()); |
| 47 EXPECT_EQ("", status.error_details()); |
| 48 EXPECT_EQ(blink::WebCryptoErrorTypeData, status.error_type()); |
| 49 |
| 50 status = Status::ErrorUnsupported(); |
| 51 EXPECT_TRUE(status.IsError()); |
| 52 EXPECT_EQ("The requested operation is unsupported", status.error_details()); |
| 53 EXPECT_EQ(blink::WebCryptoErrorTypeNotSupported, status.error_type()); |
| 54 |
| 55 status = Status::ErrorJwkPropertyMissing("kty"); |
| 56 EXPECT_TRUE(status.IsError()); |
| 57 EXPECT_EQ("The required JWK property \"kty\" was missing", |
| 58 status.error_details()); |
| 59 EXPECT_EQ(blink::WebCryptoErrorTypeData, status.error_type()); |
| 60 |
| 61 status = Status::ErrorJwkPropertyWrongType("kty", "string"); |
| 62 EXPECT_TRUE(status.IsError()); |
| 63 EXPECT_EQ("The JWK property \"kty\" must be a string", |
| 64 status.error_details()); |
| 65 EXPECT_EQ(blink::WebCryptoErrorTypeData, status.error_type()); |
| 66 |
| 67 status = Status::ErrorJwkBase64Decode("n"); |
| 68 EXPECT_TRUE(status.IsError()); |
| 69 EXPECT_EQ("The JWK property \"n\" could not be base64 decoded", |
| 70 status.error_details()); |
| 71 EXPECT_EQ(blink::WebCryptoErrorTypeData, status.error_type()); |
35 } | 72 } |
36 | 73 |
37 Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode, | |
38 const blink::WebCryptoAlgorithm& algorithm, | |
39 const blink::WebCryptoKey& key, | |
40 const CryptoData& data, | |
41 std::vector<uint8_t>* buffer) { | |
42 const std::vector<uint8_t>& raw_key = | |
43 SymKeyOpenSsl::Cast(key)->raw_key_data(); | |
44 const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams(); | |
45 | |
46 unsigned int tag_length_bits; | |
47 Status status = GetAesGcmTagLengthInBits(params, &tag_length_bits); | |
48 if (status.IsError()) | |
49 return status; | |
50 | |
51 return AeadEncryptDecrypt(mode, | |
52 raw_key, | |
53 data, | |
54 tag_length_bits / 8, | |
55 CryptoData(params->iv()), | |
56 CryptoData(params->optionalAdditionalData()), | |
57 GetAesGcmAlgorithmFromKeySize(raw_key.size()), | |
58 buffer); | |
59 } | |
60 | |
61 class AesGcmImplementation : public AesAlgorithm { | |
62 public: | |
63 AesGcmImplementation() : AesAlgorithm("GCM") {} | |
64 | |
65 virtual Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
66 const blink::WebCryptoKey& key, | |
67 const CryptoData& data, | |
68 std::vector<uint8_t>* buffer) const OVERRIDE { | |
69 return AesGcmEncryptDecrypt(ENCRYPT, algorithm, key, data, buffer); | |
70 } | |
71 | |
72 virtual Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
73 const blink::WebCryptoKey& key, | |
74 const CryptoData& data, | |
75 std::vector<uint8_t>* buffer) const OVERRIDE { | |
76 return AesGcmEncryptDecrypt(DECRYPT, algorithm, key, data, buffer); | |
77 } | |
78 }; | |
79 | |
80 } // namespace | 74 } // namespace |
81 | 75 |
82 AlgorithmImplementation* CreatePlatformAesGcmImplementation() { | |
83 return new AesGcmImplementation; | |
84 } | |
85 | |
86 } // namespace webcrypto | 76 } // namespace webcrypto |
87 | 77 |
88 } // namespace content | 78 } // namespace content |
OLD | NEW |