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/openssl/aes_algorithm_openssl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "content/child/webcrypto/crypto_data.h" | |
9 #include "content/child/webcrypto/jwk.h" | |
10 #include "content/child/webcrypto/openssl/key_openssl.h" | |
11 #include "content/child/webcrypto/openssl/util_openssl.h" | |
12 #include "content/child/webcrypto/status.h" | |
13 #include "content/child/webcrypto/webcrypto_util.h" | |
14 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
15 | |
16 namespace content { | |
17 | |
18 namespace webcrypto { | |
19 | |
20 AesAlgorithm::AesAlgorithm(blink::WebCryptoKeyUsageMask all_key_usages, | |
21 const std::string& jwk_suffix) | |
22 : all_key_usages_(all_key_usages), jwk_suffix_(jwk_suffix) { | |
23 } | |
24 | |
25 AesAlgorithm::AesAlgorithm(const std::string& jwk_suffix) | |
26 : all_key_usages_(blink::WebCryptoKeyUsageEncrypt | | |
27 blink::WebCryptoKeyUsageDecrypt | | |
28 blink::WebCryptoKeyUsageWrapKey | | |
29 blink::WebCryptoKeyUsageUnwrapKey), | |
30 jwk_suffix_(jwk_suffix) { | |
31 } | |
32 | |
33 Status AesAlgorithm::GenerateKey(const blink::WebCryptoAlgorithm& algorithm, | |
34 bool extractable, | |
35 blink::WebCryptoKeyUsageMask usages, | |
36 GenerateKeyResult* result) const { | |
37 Status status = CheckKeyCreationUsages(all_key_usages_, usages, false); | |
38 if (status.IsError()) | |
39 return status; | |
40 | |
41 unsigned int keylen_bits; | |
42 status = GetAesKeyGenLengthInBits(algorithm.aesKeyGenParams(), &keylen_bits); | |
43 if (status.IsError()) | |
44 return status; | |
45 | |
46 return GenerateWebCryptoSecretKey( | |
47 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits), | |
48 extractable, usages, keylen_bits, result); | |
49 } | |
50 | |
51 Status AesAlgorithm::VerifyKeyUsagesBeforeImportKey( | |
52 blink::WebCryptoKeyFormat format, | |
53 blink::WebCryptoKeyUsageMask usages) const { | |
54 switch (format) { | |
55 case blink::WebCryptoKeyFormatRaw: | |
56 case blink::WebCryptoKeyFormatJwk: | |
57 return CheckKeyCreationUsages(all_key_usages_, usages, false); | |
58 default: | |
59 return Status::ErrorUnsupportedImportKeyFormat(); | |
60 } | |
61 } | |
62 | |
63 Status AesAlgorithm::ImportKeyRaw(const CryptoData& key_data, | |
64 const blink::WebCryptoAlgorithm& algorithm, | |
65 bool extractable, | |
66 blink::WebCryptoKeyUsageMask usages, | |
67 blink::WebCryptoKey* key) const { | |
68 const unsigned int keylen_bytes = key_data.byte_length(); | |
69 Status status = VerifyAesKeyLengthForImport(keylen_bytes); | |
70 if (status.IsError()) | |
71 return status; | |
72 | |
73 // No possibility of overflow. | |
74 unsigned int keylen_bits = keylen_bytes * 8; | |
75 | |
76 return CreateWebCryptoSecretKey( | |
77 key_data, | |
78 blink::WebCryptoKeyAlgorithm::createAes(algorithm.id(), keylen_bits), | |
79 extractable, usages, key); | |
80 } | |
81 | |
82 Status AesAlgorithm::ImportKeyJwk(const CryptoData& key_data, | |
83 const blink::WebCryptoAlgorithm& algorithm, | |
84 bool extractable, | |
85 blink::WebCryptoKeyUsageMask usages, | |
86 blink::WebCryptoKey* key) const { | |
87 std::vector<uint8_t> raw_data; | |
88 Status status = ReadAesSecretKeyJwk(key_data, jwk_suffix_, extractable, | |
89 usages, &raw_data); | |
90 if (status.IsError()) | |
91 return status; | |
92 | |
93 return ImportKeyRaw(CryptoData(raw_data), algorithm, extractable, usages, | |
94 key); | |
95 } | |
96 | |
97 Status AesAlgorithm::ExportKeyRaw(const blink::WebCryptoKey& key, | |
98 std::vector<uint8_t>* buffer) const { | |
99 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data(); | |
100 return Status::Success(); | |
101 } | |
102 | |
103 Status AesAlgorithm::ExportKeyJwk(const blink::WebCryptoKey& key, | |
104 std::vector<uint8_t>* buffer) const { | |
105 const std::vector<uint8_t>& raw_data = | |
106 SymKeyOpenSsl::Cast(key)->raw_key_data(); | |
107 | |
108 WriteSecretKeyJwk(CryptoData(raw_data), | |
109 MakeJwkAesAlgorithmName(jwk_suffix_, raw_data.size()), | |
110 key.extractable(), key.usages(), buffer); | |
111 | |
112 return Status::Success(); | |
113 } | |
114 | |
115 Status AesAlgorithm::SerializeKeyForClone( | |
116 const blink::WebCryptoKey& key, | |
117 blink::WebVector<uint8_t>* key_data) const { | |
118 key_data->assign(SymKeyOpenSsl::Cast(key)->serialized_key_data()); | |
119 return Status::Success(); | |
120 } | |
121 | |
122 Status AesAlgorithm::DeserializeKeyForClone( | |
123 const blink::WebCryptoKeyAlgorithm& algorithm, | |
124 blink::WebCryptoKeyType type, | |
125 bool extractable, | |
126 blink::WebCryptoKeyUsageMask usages, | |
127 const CryptoData& key_data, | |
128 blink::WebCryptoKey* key) const { | |
129 return ImportKeyRaw(key_data, CreateAlgorithm(algorithm.id()), extractable, | |
130 usages, key); | |
131 } | |
132 | |
133 Status AesAlgorithm::GetKeyLength( | |
134 const blink::WebCryptoAlgorithm& key_length_algorithm, | |
135 bool* has_length_bits, | |
136 unsigned int* length_bits) const { | |
137 return GetAesKeyLength(key_length_algorithm, has_length_bits, length_bits); | |
138 } | |
139 | |
140 } // namespace webcrypto | |
141 | |
142 } // namespace content | |
OLD | NEW |