OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/webcrypto/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto/platform_crypto.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 #include <openssl/aes.h> | 8 #include <openssl/aes.h> |
9 #include <openssl/evp.h> | 9 #include <openssl/evp.h> |
10 #include <openssl/hmac.h> | 10 #include <openssl/hmac.h> |
11 #include <openssl/rand.h> | 11 #include <openssl/rand.h> |
12 #include <openssl/sha.h> | 12 #include <openssl/sha.h> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "content/renderer/webcrypto/crypto_data.h" |
15 #include "content/renderer/webcrypto/webcrypto_util.h" | 16 #include "content/renderer/webcrypto/webcrypto_util.h" |
16 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
17 #include "crypto/secure_util.h" | |
18 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 18 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
21 | 21 |
22 namespace content { | 22 namespace content { |
23 | 23 |
24 using webcrypto::Status; | 24 namespace webcrypto { |
25 | 25 |
26 namespace { | 26 namespace platform { |
27 | 27 |
28 class SymKeyHandle : public blink::WebCryptoKeyHandle { | 28 class SymKey : public Key { |
29 public: | 29 public: |
30 SymKeyHandle(const unsigned char* key_data, unsigned int key_data_size) | 30 explicit SymKey(const CryptoData& key_data) |
31 : key_(key_data, key_data + key_data_size) {} | 31 : key_(key_data.bytes(), key_data.bytes() + key_data.byte_length()) {} |
| 32 |
| 33 virtual SymKey* AsSymKey() OVERRIDE { return this; } |
| 34 virtual PublicKey* AsPublicKey() OVERRIDE { return NULL; } |
| 35 virtual PrivateKey* AsPrivateKey() OVERRIDE { return NULL; } |
32 | 36 |
33 const std::vector<unsigned char>& key() const { return key_; } | 37 const std::vector<unsigned char>& key() const { return key_; } |
34 | 38 |
35 private: | 39 private: |
36 const std::vector<unsigned char> key_; | 40 const std::vector<unsigned char> key_; |
37 | 41 |
38 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); | 42 DISALLOW_COPY_AND_ASSIGN(SymKey); |
39 }; | 43 }; |
40 | 44 |
| 45 namespace { |
| 46 |
41 const EVP_CIPHER* GetAESCipherByKeyLength(unsigned int key_length_bytes) { | 47 const EVP_CIPHER* GetAESCipherByKeyLength(unsigned int key_length_bytes) { |
42 // OpenSSL supports AES CBC ciphers for only 3 key lengths: 128, 192, 256 bits | 48 // OpenSSL supports AES CBC ciphers for only 3 key lengths: 128, 192, 256 bits |
43 switch (key_length_bytes) { | 49 switch (key_length_bytes) { |
44 case 16: | 50 case 16: |
45 return EVP_aes_128_cbc(); | 51 return EVP_aes_128_cbc(); |
46 case 24: | 52 case 24: |
47 return EVP_aes_192_cbc(); | 53 return EVP_aes_192_cbc(); |
48 case 32: | 54 case 32: |
49 return EVP_aes_256_cbc(); | 55 return EVP_aes_256_cbc(); |
50 default: | 56 default: |
51 return NULL; | 57 return NULL; |
52 } | 58 } |
53 } | 59 } |
54 | 60 |
55 // OpenSSL constants for EVP_CipherInit_ex(), do not change | 61 // OpenSSL constants for EVP_CipherInit_ex(), do not change |
56 enum CipherOperation { | 62 enum CipherOperation { |
57 kDoDecrypt = 0, | 63 kDoDecrypt = 0, |
58 kDoEncrypt = 1 | 64 kDoEncrypt = 1 |
59 }; | 65 }; |
60 | 66 |
61 Status AesCbcEncryptDecrypt(CipherOperation cipher_operation, | 67 Status AesCbcEncryptDecrypt(EncryptOrDecrypt mode, |
62 const blink::WebCryptoAlgorithm& algorithm, | 68 SymKey* key, |
63 const blink::WebCryptoKey& key, | 69 const CryptoData& iv, |
64 const unsigned char* data, | 70 const CryptoData& data, |
65 unsigned int data_size, | |
66 blink::WebArrayBuffer* buffer) { | 71 blink::WebArrayBuffer* buffer) { |
67 DCHECK_EQ(blink::WebCryptoAlgorithmIdAesCbc, algorithm.id()); | 72 CipherOperation cipher_operation = |
68 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | 73 (mode == ENCRYPT) ? kDoEncrypt : kDoDecrypt; |
69 DCHECK_EQ(blink::WebCryptoKeyTypeSecret, key.type()); | |
70 | 74 |
71 if (data_size >= INT_MAX - AES_BLOCK_SIZE) { | 75 if (data.byte_length() >= INT_MAX - AES_BLOCK_SIZE) { |
72 // TODO(padolph): Handle this by chunking the input fed into OpenSSL. Right | 76 // TODO(padolph): Handle this by chunking the input fed into OpenSSL. Right |
73 // now it doesn't make much difference since the one-shot API would end up | 77 // now it doesn't make much difference since the one-shot API would end up |
74 // blowing out the memory and crashing anyway. | 78 // blowing out the memory and crashing anyway. |
75 return Status::ErrorDataTooLarge(); | 79 return Status::ErrorDataTooLarge(); |
76 } | 80 } |
77 | 81 |
78 // Note: PKCS padding is enabled by default | 82 // Note: PKCS padding is enabled by default |
79 crypto::ScopedOpenSSL<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> context( | 83 crypto::ScopedOpenSSL<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free> context( |
80 EVP_CIPHER_CTX_new()); | 84 EVP_CIPHER_CTX_new()); |
81 | 85 |
82 if (!context.get()) | 86 if (!context.get()) |
83 return Status::Error(); | 87 return Status::Error(); |
84 | 88 |
85 SymKeyHandle* const sym_key = reinterpret_cast<SymKeyHandle*>(key.handle()); | 89 const EVP_CIPHER* const cipher = GetAESCipherByKeyLength(key->key().size()); |
86 | |
87 const EVP_CIPHER* const cipher = | |
88 GetAESCipherByKeyLength(sym_key->key().size()); | |
89 DCHECK(cipher); | 90 DCHECK(cipher); |
90 | 91 |
91 const blink::WebCryptoAesCbcParams* const params = algorithm.aesCbcParams(); | |
92 if (params->iv().size() != AES_BLOCK_SIZE) | |
93 return Status::ErrorIncorrectSizeAesCbcIv(); | |
94 | |
95 if (!EVP_CipherInit_ex(context.get(), | 92 if (!EVP_CipherInit_ex(context.get(), |
96 cipher, | 93 cipher, |
97 NULL, | 94 NULL, |
98 &sym_key->key()[0], | 95 &key->key()[0], |
99 params->iv().data(), | 96 iv.bytes(), |
100 cipher_operation)) { | 97 cipher_operation)) { |
101 return Status::Error(); | 98 return Status::Error(); |
102 } | 99 } |
103 | 100 |
104 // According to the openssl docs, the amount of data written may be as large | 101 // According to the openssl docs, the amount of data written may be as large |
105 // as (data_size + cipher_block_size - 1), constrained to a multiple of | 102 // as (data_size + cipher_block_size - 1), constrained to a multiple of |
106 // cipher_block_size. | 103 // cipher_block_size. |
107 unsigned int output_max_len = data_size + AES_BLOCK_SIZE - 1; | 104 unsigned int output_max_len = data.byte_length() + AES_BLOCK_SIZE - 1; |
108 const unsigned remainder = output_max_len % AES_BLOCK_SIZE; | 105 const unsigned remainder = output_max_len % AES_BLOCK_SIZE; |
109 if (remainder != 0) | 106 if (remainder != 0) |
110 output_max_len += AES_BLOCK_SIZE - remainder; | 107 output_max_len += AES_BLOCK_SIZE - remainder; |
111 DCHECK_GT(output_max_len, data_size); | 108 DCHECK_GT(output_max_len, data.byte_length()); |
112 | 109 |
113 *buffer = blink::WebArrayBuffer::create(output_max_len, 1); | 110 *buffer = blink::WebArrayBuffer::create(output_max_len, 1); |
114 | 111 |
115 unsigned char* const buffer_data = | 112 unsigned char* const buffer_data = |
116 reinterpret_cast<unsigned char*>(buffer->data()); | 113 reinterpret_cast<unsigned char*>(buffer->data()); |
117 | 114 |
118 int output_len = 0; | 115 int output_len = 0; |
119 if (!EVP_CipherUpdate( | 116 if (!EVP_CipherUpdate(context.get(), |
120 context.get(), buffer_data, &output_len, data, data_size)) | 117 buffer_data, |
| 118 &output_len, |
| 119 data.bytes(), |
| 120 data.byte_length())) |
121 return Status::Error(); | 121 return Status::Error(); |
122 int final_output_chunk_len = 0; | 122 int final_output_chunk_len = 0; |
123 if (!EVP_CipherFinal_ex( | 123 if (!EVP_CipherFinal_ex( |
124 context.get(), buffer_data + output_len, &final_output_chunk_len)) { | 124 context.get(), buffer_data + output_len, &final_output_chunk_len)) { |
125 return Status::Error(); | 125 return Status::Error(); |
126 } | 126 } |
127 | 127 |
128 const unsigned int final_output_len = | 128 const unsigned int final_output_len = |
129 static_cast<unsigned int>(output_len) + | 129 static_cast<unsigned int>(output_len) + |
130 static_cast<unsigned int>(final_output_chunk_len); | 130 static_cast<unsigned int>(final_output_chunk_len); |
131 DCHECK_LE(final_output_len, output_max_len); | 131 DCHECK_LE(final_output_len, output_max_len); |
132 | 132 |
133 webcrypto::ShrinkBuffer(buffer, final_output_len); | 133 ShrinkBuffer(buffer, final_output_len); |
134 | |
135 return Status::Success(); | |
136 } | |
137 | |
138 Status ExportKeyInternalRaw( | |
139 const blink::WebCryptoKey& key, | |
140 blink::WebArrayBuffer* buffer) { | |
141 | |
142 DCHECK(key.handle()); | |
143 DCHECK(buffer); | |
144 | |
145 if (key.type() != blink::WebCryptoKeyTypeSecret) | |
146 return Status::ErrorUnexpectedKeyType(); | |
147 | |
148 // TODO(eroman): This should be in a more generic location. | |
149 if (!key.extractable()) | |
150 return Status::ErrorKeyNotExtractable(); | |
151 | |
152 const SymKeyHandle* sym_key = reinterpret_cast<SymKeyHandle*>(key.handle()); | |
153 | |
154 *buffer = webcrypto::CreateArrayBuffer( | |
155 webcrypto::Uint8VectorStart(sym_key->key()), sym_key->key().size()); | |
156 | 134 |
157 return Status::Success(); | 135 return Status::Success(); |
158 } | 136 } |
159 | 137 |
160 } // namespace | 138 } // namespace |
161 | 139 |
162 void WebCryptoImpl::Init() { crypto::EnsureOpenSSLInit(); } | 140 Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) { |
163 | 141 *buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size()); |
164 Status WebCryptoImpl::EncryptInternal( | 142 return Status::Success(); |
165 const blink::WebCryptoAlgorithm& algorithm, | |
166 const blink::WebCryptoKey& key, | |
167 const unsigned char* data, | |
168 unsigned int data_size, | |
169 blink::WebArrayBuffer* buffer) { | |
170 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { | |
171 return AesCbcEncryptDecrypt( | |
172 kDoEncrypt, algorithm, key, data, data_size, buffer); | |
173 } | |
174 | |
175 return Status::ErrorUnsupported(); | |
176 } | 143 } |
177 | 144 |
178 Status WebCryptoImpl::DecryptInternal( | 145 void Init() { |
179 const blink::WebCryptoAlgorithm& algorithm, | 146 crypto::EnsureOpenSSLInit(); |
180 const blink::WebCryptoKey& key, | |
181 const unsigned char* data, | |
182 unsigned int data_size, | |
183 blink::WebArrayBuffer* buffer) { | |
184 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { | |
185 return AesCbcEncryptDecrypt( | |
186 kDoDecrypt, algorithm, key, data, data_size, buffer); | |
187 } | |
188 | |
189 return Status::ErrorUnsupported(); | |
190 } | 147 } |
191 | 148 |
192 Status WebCryptoImpl::DigestInternal(const blink::WebCryptoAlgorithm& algorithm, | 149 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode, |
193 const unsigned char* data, | 150 SymKey* key, |
194 unsigned int data_size, | 151 const CryptoData& iv, |
195 blink::WebArrayBuffer* buffer) { | 152 const CryptoData& data, |
| 153 blink::WebArrayBuffer* buffer) { |
| 154 // TODO(eroman): inline the function here. |
| 155 return AesCbcEncryptDecrypt(mode, key, iv, data, buffer); |
| 156 } |
196 | 157 |
| 158 Status DigestSha(blink::WebCryptoAlgorithmId algorithm, |
| 159 const CryptoData& data, |
| 160 blink::WebArrayBuffer* buffer) { |
197 crypto::OpenSSLErrStackTracer(FROM_HERE); | 161 crypto::OpenSSLErrStackTracer(FROM_HERE); |
198 | 162 |
199 const EVP_MD* digest_algorithm; | 163 const EVP_MD* digest_algorithm; |
200 switch (algorithm.id()) { | 164 switch (algorithm) { |
201 case blink::WebCryptoAlgorithmIdSha1: | 165 case blink::WebCryptoAlgorithmIdSha1: |
202 digest_algorithm = EVP_sha1(); | 166 digest_algorithm = EVP_sha1(); |
203 break; | 167 break; |
204 case blink::WebCryptoAlgorithmIdSha224: | 168 case blink::WebCryptoAlgorithmIdSha224: |
205 digest_algorithm = EVP_sha224(); | 169 digest_algorithm = EVP_sha224(); |
206 break; | 170 break; |
207 case blink::WebCryptoAlgorithmIdSha256: | 171 case blink::WebCryptoAlgorithmIdSha256: |
208 digest_algorithm = EVP_sha256(); | 172 digest_algorithm = EVP_sha256(); |
209 break; | 173 break; |
210 case blink::WebCryptoAlgorithmIdSha384: | 174 case blink::WebCryptoAlgorithmIdSha384: |
211 digest_algorithm = EVP_sha384(); | 175 digest_algorithm = EVP_sha384(); |
212 break; | 176 break; |
213 case blink::WebCryptoAlgorithmIdSha512: | 177 case blink::WebCryptoAlgorithmIdSha512: |
214 digest_algorithm = EVP_sha512(); | 178 digest_algorithm = EVP_sha512(); |
215 break; | 179 break; |
216 default: | 180 default: |
217 // Not a digest algorithm. | 181 // Not a SHA algorithm. |
218 return Status::ErrorUnsupported(); | 182 return Status::ErrorUnexpected(); |
219 } | 183 } |
220 | 184 |
221 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context( | 185 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context( |
222 EVP_MD_CTX_create()); | 186 EVP_MD_CTX_create()); |
223 if (!digest_context.get()) | 187 if (!digest_context.get()) |
224 return Status::Error(); | 188 return Status::Error(); |
225 | 189 |
226 if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) || | 190 if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) || |
227 !EVP_DigestUpdate(digest_context.get(), data, data_size)) { | 191 !EVP_DigestUpdate( |
| 192 digest_context.get(), data.bytes(), data.byte_length())) { |
228 return Status::Error(); | 193 return Status::Error(); |
229 } | 194 } |
230 | 195 |
231 const int hash_expected_size = EVP_MD_CTX_size(digest_context.get()); | 196 const int hash_expected_size = EVP_MD_CTX_size(digest_context.get()); |
232 if (hash_expected_size <= 0) { | 197 if (hash_expected_size <= 0) |
233 return Status::ErrorUnexpected(); | 198 return Status::ErrorUnexpected(); |
234 } | |
235 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE); | 199 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE); |
236 | 200 |
237 *buffer = blink::WebArrayBuffer::create(hash_expected_size, 1); | 201 *buffer = blink::WebArrayBuffer::create(hash_expected_size, 1); |
238 unsigned char* const hash_buffer = | 202 unsigned char* const hash_buffer = |
239 reinterpret_cast<unsigned char* const>(buffer->data()); | 203 reinterpret_cast<unsigned char* const>(buffer->data()); |
240 | 204 |
241 unsigned int hash_size = 0; | 205 unsigned int hash_size = 0; |
242 if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) || | 206 if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) || |
243 static_cast<int>(hash_size) != hash_expected_size) { | 207 static_cast<int>(hash_size) != hash_expected_size) { |
244 buffer->reset(); | 208 buffer->reset(); |
245 return Status::Error(); | 209 return Status::Error(); |
246 } | 210 } |
247 | 211 |
248 return Status::Success(); | 212 return Status::Success(); |
249 } | 213 } |
250 | 214 |
251 Status WebCryptoImpl::GenerateSecretKeyInternal( | 215 Status GenerateSecretKey( |
252 const blink::WebCryptoAlgorithm& algorithm, | 216 const blink::WebCryptoAlgorithm& algorithm, |
253 bool extractable, | 217 bool extractable, |
254 blink::WebCryptoKeyUsageMask usage_mask, | 218 blink::WebCryptoKeyUsageMask usage_mask, |
| 219 unsigned keylen_bytes, |
255 blink::WebCryptoKey* key) { | 220 blink::WebCryptoKey* key) { |
256 | 221 // TODO(eroman): Is this right? |
257 unsigned int keylen_bytes = 0; | |
258 blink::WebCryptoKeyType key_type; | |
259 switch (algorithm.id()) { | |
260 case blink::WebCryptoAlgorithmIdAesCbc: { | |
261 const blink::WebCryptoAesKeyGenParams* params = | |
262 algorithm.aesKeyGenParams(); | |
263 DCHECK(params); | |
264 if (params->lengthBits() % 8) | |
265 return Status::ErrorGenerateKeyLength(); | |
266 keylen_bytes = params->lengthBits() / 8; | |
267 if (!GetAESCipherByKeyLength(keylen_bytes)) | |
268 return Status::Error(); | |
269 key_type = blink::WebCryptoKeyTypeSecret; | |
270 break; | |
271 } | |
272 case blink::WebCryptoAlgorithmIdHmac: { | |
273 const blink::WebCryptoHmacKeyParams* params = algorithm.hmacKeyParams(); | |
274 DCHECK(params); | |
275 if (params->hasLengthBytes()) | |
276 keylen_bytes = params->optionalLengthBytes(); | |
277 else | |
278 keylen_bytes = webcrypto::ShaBlockSizeBytes(params->hash().id()); | |
279 key_type = blink::WebCryptoKeyTypeSecret; | |
280 break; | |
281 } | |
282 | |
283 default: { return Status::ErrorUnsupported(); } | |
284 } | |
285 | |
286 if (keylen_bytes == 0) | 222 if (keylen_bytes == 0) |
287 return Status::ErrorGenerateKeyLength(); | 223 return Status::ErrorGenerateKeyLength(); |
288 | 224 |
289 crypto::OpenSSLErrStackTracer(FROM_HERE); | 225 crypto::OpenSSLErrStackTracer(FROM_HERE); |
290 | 226 |
291 std::vector<unsigned char> random_bytes(keylen_bytes, 0); | 227 std::vector<unsigned char> random_bytes(keylen_bytes, 0); |
292 if (!(RAND_bytes(&random_bytes[0], keylen_bytes))) | 228 if (!(RAND_bytes(&random_bytes[0], keylen_bytes))) |
293 return Status::Error(); | 229 return Status::Error(); |
294 | 230 |
295 *key = blink::WebCryptoKey::create( | 231 *key = blink::WebCryptoKey::create(new SymKey(CryptoData(random_bytes)), |
296 new SymKeyHandle(&random_bytes[0], random_bytes.size()), | 232 blink::WebCryptoKeyTypeSecret, |
297 key_type, extractable, algorithm, usage_mask); | 233 extractable, |
| 234 algorithm, |
| 235 usage_mask); |
298 | 236 |
299 return Status::Success(); | 237 return Status::Success(); |
300 } | 238 } |
301 | 239 |
302 Status WebCryptoImpl::GenerateKeyPairInternal( | 240 Status GenerateRsaKeyPair( |
303 const blink::WebCryptoAlgorithm& algorithm, | 241 const blink::WebCryptoAlgorithm& algorithm, |
304 bool extractable, | 242 bool extractable, |
305 blink::WebCryptoKeyUsageMask usage_mask, | 243 blink::WebCryptoKeyUsageMask usage_mask, |
306 blink::WebCryptoKey* public_key, | 244 blink::WebCryptoKey* public_key, |
307 blink::WebCryptoKey* private_key) { | 245 blink::WebCryptoKey* private_key) { |
308 // TODO(padolph): Placeholder for OpenSSL implementation. | 246 // TODO(padolph): Placeholder for OpenSSL implementation. |
309 // Issue http://crbug.com/267888. | 247 // Issue http://crbug.com/267888. |
310 return Status::ErrorUnsupported(); | 248 return Status::ErrorUnsupported(); |
311 } | 249 } |
312 | 250 |
313 Status WebCryptoImpl::ImportKeyInternal( | 251 Status ImportKeyRaw(const blink::WebCryptoAlgorithm& algorithm, |
314 blink::WebCryptoKeyFormat format, | 252 const CryptoData& key_data, |
315 const unsigned char* key_data, | 253 bool extractable, |
316 unsigned int key_data_size, | 254 blink::WebCryptoKeyUsageMask usage_mask, |
317 const blink::WebCryptoAlgorithm& algorithm_or_null, | 255 blink::WebCryptoKey* key) { |
318 bool extractable, | 256 *key = blink::WebCryptoKey::create(new SymKey(key_data), |
319 blink::WebCryptoKeyUsageMask usage_mask, | 257 blink::WebCryptoKeyTypeSecret, |
320 blink::WebCryptoKey* key) { | 258 extractable, |
321 // TODO(eroman): Currently expects algorithm to always be specified, as it is | 259 algorithm, |
322 // required for raw format. | 260 usage_mask); |
323 if (algorithm_or_null.isNull()) | |
324 return Status::ErrorMissingAlgorithmImportRawKey(); | |
325 const blink::WebCryptoAlgorithm& algorithm = algorithm_or_null; | |
326 | |
327 // TODO(padolph): Support all relevant alg types and then remove this gate. | |
328 if (algorithm.id() != blink::WebCryptoAlgorithmIdHmac && | |
329 algorithm.id() != blink::WebCryptoAlgorithmIdAesCbc) { | |
330 return Status::ErrorUnsupported(); | |
331 } | |
332 | |
333 // TODO(padolph): Need to split handling for symmetric (raw format) and | |
334 // asymmetric (spki or pkcs8 format) keys. | |
335 // Currently only supporting symmetric. | |
336 | |
337 // Symmetric keys are always type secret | |
338 blink::WebCryptoKeyType type = blink::WebCryptoKeyTypeSecret; | |
339 | |
340 const unsigned char* raw_key_data; | |
341 unsigned int raw_key_data_size; | |
342 switch (format) { | |
343 case blink::WebCryptoKeyFormatRaw: | |
344 raw_key_data = key_data; | |
345 raw_key_data_size = key_data_size; | |
346 // The NSS implementation fails when importing a raw AES key with a length | |
347 // incompatible with AES. The line below is to match this behavior. | |
348 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc && | |
349 !GetAESCipherByKeyLength(raw_key_data_size)) { | |
350 return Status::Error(); | |
351 } | |
352 break; | |
353 case blink::WebCryptoKeyFormatJwk: | |
354 return Status::ErrorUnexpected(); | |
355 default: | |
356 return Status::ErrorUnsupported(); | |
357 } | |
358 | |
359 *key = blink::WebCryptoKey::create( | |
360 new SymKeyHandle(raw_key_data, raw_key_data_size), | |
361 type, extractable, algorithm, usage_mask); | |
362 | 261 |
363 return Status::Success(); | 262 return Status::Success(); |
364 } | 263 } |
365 | 264 |
366 Status WebCryptoImpl::ExportKeyInternal( | 265 Status SignHmac(SymKey* key, |
367 blink::WebCryptoKeyFormat format, | 266 const blink::WebCryptoAlgorithm& hash, |
368 const blink::WebCryptoKey& key, | 267 const CryptoData& data, |
369 blink::WebArrayBuffer* buffer) { | 268 blink::WebArrayBuffer* buffer) { |
370 switch (format) { | |
371 case blink::WebCryptoKeyFormatRaw: | |
372 return ExportKeyInternalRaw(key, buffer); | |
373 case blink::WebCryptoKeyFormatSpki: | |
374 // TODO(padolph): Implement spki export | |
375 return Status::ErrorUnsupported(); | |
376 case blink::WebCryptoKeyFormatPkcs8: | |
377 // TODO(padolph): Implement pkcs8 export | |
378 return Status::ErrorUnsupported(); | |
379 default: | |
380 return Status::ErrorUnsupported(); | |
381 } | |
382 return Status::ErrorUnsupported(); | |
383 } | |
384 | |
385 Status WebCryptoImpl::SignInternal( | |
386 const blink::WebCryptoAlgorithm& algorithm, | |
387 const blink::WebCryptoKey& key, | |
388 const unsigned char* data, | |
389 unsigned int data_size, | |
390 blink::WebArrayBuffer* buffer) { | |
391 | |
392 blink::WebArrayBuffer result; | 269 blink::WebArrayBuffer result; |
393 | 270 |
394 switch (algorithm.id()) { | 271 // TODO(eroman): De-indent this code. |
395 case blink::WebCryptoAlgorithmIdHmac: { | |
396 | |
397 DCHECK_EQ(key.algorithm().id(), blink::WebCryptoAlgorithmIdHmac); | |
398 DCHECK_NE(0, key.usages() & blink::WebCryptoKeyUsageSign); | |
399 | |
400 const blink::WebCryptoHmacParams* const params = algorithm.hmacParams(); | |
401 if (!params) | |
402 return Status::ErrorUnexpected(); | |
403 | |
404 const EVP_MD* evp_sha = 0; | 272 const EVP_MD* evp_sha = 0; |
405 unsigned int hmac_expected_length = 0; | 273 unsigned int hmac_expected_length = 0; |
406 // Note that HMAC length is determined by the hash used. | 274 // Note that HMAC length is determined by the hash used. |
407 switch (params->hash().id()) { | 275 switch (hash.id()) { |
408 case blink::WebCryptoAlgorithmIdSha1: | 276 case blink::WebCryptoAlgorithmIdSha1: |
409 evp_sha = EVP_sha1(); | 277 evp_sha = EVP_sha1(); |
410 hmac_expected_length = SHA_DIGEST_LENGTH; | 278 hmac_expected_length = SHA_DIGEST_LENGTH; |
411 break; | 279 break; |
412 case blink::WebCryptoAlgorithmIdSha224: | 280 case blink::WebCryptoAlgorithmIdSha224: |
413 evp_sha = EVP_sha224(); | 281 evp_sha = EVP_sha224(); |
414 hmac_expected_length = SHA224_DIGEST_LENGTH; | 282 hmac_expected_length = SHA224_DIGEST_LENGTH; |
415 break; | 283 break; |
416 case blink::WebCryptoAlgorithmIdSha256: | 284 case blink::WebCryptoAlgorithmIdSha256: |
417 evp_sha = EVP_sha256(); | 285 evp_sha = EVP_sha256(); |
418 hmac_expected_length = SHA256_DIGEST_LENGTH; | 286 hmac_expected_length = SHA256_DIGEST_LENGTH; |
419 break; | 287 break; |
420 case blink::WebCryptoAlgorithmIdSha384: | 288 case blink::WebCryptoAlgorithmIdSha384: |
421 evp_sha = EVP_sha384(); | 289 evp_sha = EVP_sha384(); |
422 hmac_expected_length = SHA384_DIGEST_LENGTH; | 290 hmac_expected_length = SHA384_DIGEST_LENGTH; |
423 break; | 291 break; |
424 case blink::WebCryptoAlgorithmIdSha512: | 292 case blink::WebCryptoAlgorithmIdSha512: |
425 evp_sha = EVP_sha512(); | 293 evp_sha = EVP_sha512(); |
426 hmac_expected_length = SHA512_DIGEST_LENGTH; | 294 hmac_expected_length = SHA512_DIGEST_LENGTH; |
427 break; | 295 break; |
428 default: | 296 default: |
429 // Not a digest algorithm. | 297 // Not a digest algorithm. |
430 return Status::ErrorUnsupported(); | 298 return Status::ErrorUnsupported(); |
431 } | 299 } |
432 | 300 |
433 SymKeyHandle* const sym_key = | 301 const std::vector<unsigned char>& raw_key = key->key(); |
434 reinterpret_cast<SymKeyHandle*>(key.handle()); | |
435 const std::vector<unsigned char>& raw_key = sym_key->key(); | |
436 | 302 |
437 // OpenSSL wierdness here. | 303 // OpenSSL wierdness here. |
438 // First, HMAC() needs a void* for the key data, so make one up front as a | 304 // First, HMAC() needs a void* for the key data, so make one up front as a |
439 // cosmetic to avoid a cast. Second, OpenSSL does not like a NULL key, | 305 // cosmetic to avoid a cast. Second, OpenSSL does not like a NULL key, |
440 // which will result if the raw_key vector is empty; an entirely valid | 306 // which will result if the raw_key vector is empty; an entirely valid |
441 // case. Handle this specific case by pointing to an empty array. | 307 // case. Handle this specific case by pointing to an empty array. |
442 const unsigned char null_key[] = {}; | 308 const unsigned char null_key[] = {}; |
443 const void* const raw_key_voidp = raw_key.size() ? &raw_key[0] : null_key; | 309 const void* const raw_key_voidp = raw_key.size() ? &raw_key[0] : null_key; |
444 | 310 |
445 result = blink::WebArrayBuffer::create(hmac_expected_length, 1); | 311 result = blink::WebArrayBuffer::create(hmac_expected_length, 1); |
446 crypto::ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> hmac_result( | 312 crypto::ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> hmac_result( |
447 reinterpret_cast<unsigned char*>(result.data()), | 313 reinterpret_cast<unsigned char*>(result.data()), |
448 hmac_expected_length); | 314 hmac_expected_length); |
449 | 315 |
450 crypto::OpenSSLErrStackTracer(FROM_HERE); | 316 crypto::OpenSSLErrStackTracer(FROM_HERE); |
451 | 317 |
452 unsigned int hmac_actual_length; | 318 unsigned int hmac_actual_length; |
453 unsigned char* const success = HMAC(evp_sha, | 319 unsigned char* const success = HMAC(evp_sha, |
454 raw_key_voidp, | 320 raw_key_voidp, |
455 raw_key.size(), | 321 raw_key.size(), |
456 data, | 322 data.bytes(), |
457 data_size, | 323 data.byte_length(), |
458 hmac_result.safe_buffer(), | 324 hmac_result.safe_buffer(), |
459 &hmac_actual_length); | 325 &hmac_actual_length); |
460 if (!success || hmac_actual_length != hmac_expected_length) | 326 if (!success || hmac_actual_length != hmac_expected_length) |
461 return Status::Error(); | 327 return Status::Error(); |
462 | 328 |
463 break; | |
464 } | |
465 default: | |
466 return Status::ErrorUnsupported(); | |
467 } | |
468 | |
469 *buffer = result; | 329 *buffer = result; |
470 return Status::Success(); | 330 return Status::Success(); |
471 } | 331 } |
472 | 332 |
473 Status WebCryptoImpl::VerifySignatureInternal( | 333 Status ImportRsaPublicKey(const blink::WebCryptoAlgorithm& algorithm, |
474 const blink::WebCryptoAlgorithm& algorithm, | 334 bool extractable, |
475 const blink::WebCryptoKey& key, | 335 blink::WebCryptoKeyUsageMask usage_mask, |
476 const unsigned char* signature, | 336 const CryptoData& modulus_data, |
477 unsigned int signature_size, | 337 const CryptoData& exponent_data, |
478 const unsigned char* data, | 338 blink::WebCryptoKey* key) { |
479 unsigned int data_size, | |
480 bool* signature_match) { | |
481 switch (algorithm.id()) { | |
482 case blink::WebCryptoAlgorithmIdHmac: { | |
483 blink::WebArrayBuffer result; | |
484 Status status = SignInternal(algorithm, key, data, data_size, &result); | |
485 if (status.IsError()) | |
486 return status; | |
487 | |
488 // Handling of truncated signatures is underspecified in the WebCrypto | |
489 // spec, so here we fail verification if a truncated signature is being | |
490 // verified. | |
491 // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=23097 | |
492 *signature_match = | |
493 result.byteLength() == signature_size && | |
494 crypto::SecureMemEqual(result.data(), signature, signature_size); | |
495 | |
496 break; | |
497 } | |
498 default: | |
499 return Status::ErrorUnsupported(); | |
500 } | |
501 return Status::Success(); | |
502 } | |
503 | |
504 Status WebCryptoImpl::ImportRsaPublicKeyInternal( | |
505 const unsigned char* modulus_data, | |
506 unsigned int modulus_size, | |
507 const unsigned char* exponent_data, | |
508 unsigned int exponent_size, | |
509 const blink::WebCryptoAlgorithm& algorithm, | |
510 bool extractable, | |
511 blink::WebCryptoKeyUsageMask usage_mask, | |
512 blink::WebCryptoKey* key) { | |
513 // TODO(padolph): Placeholder for OpenSSL implementation. | 339 // TODO(padolph): Placeholder for OpenSSL implementation. |
514 // Issue http://crbug.com/267888. | 340 // Issue |
515 return Status::ErrorUnsupported(); | 341 return Status::ErrorUnsupported(); |
516 } | 342 } |
517 | 343 |
| 344 Status EncryptDecryptAesGcm(EncryptOrDecrypt mode, |
| 345 SymKey* key, |
| 346 const CryptoData& data, |
| 347 const CryptoData& iv, |
| 348 const CryptoData& additional_data, |
| 349 unsigned int tag_length_bits, |
| 350 blink::WebArrayBuffer* buffer) { |
| 351 // TODO(eroman): http://crbug.com/267888 |
| 352 return Status::ErrorUnsupported(); |
| 353 } |
| 354 |
| 355 // Guaranteed that key is valid. |
| 356 Status EncryptRsaEsPkcs1v1_5(PublicKey* key, |
| 357 const CryptoData& data, |
| 358 blink::WebArrayBuffer* buffer) { |
| 359 // TODO(eroman): http://crbug.com/267888 |
| 360 return Status::ErrorUnsupported(); |
| 361 } |
| 362 |
| 363 Status DecryptRsaEsPkcs1v1_5(PrivateKey* key, |
| 364 const CryptoData& data, |
| 365 blink::WebArrayBuffer* buffer) { |
| 366 // TODO(eroman): http://crbug.com/267888 |
| 367 return Status::ErrorUnsupported(); |
| 368 } |
| 369 |
| 370 Status SignRsaSsaPkcs1v1_5(PrivateKey* key, |
| 371 const blink::WebCryptoAlgorithm& hash, |
| 372 const CryptoData& data, |
| 373 blink::WebArrayBuffer* buffer) { |
| 374 // TODO(eroman): http://crbug.com/267888 |
| 375 return Status::ErrorUnsupported(); |
| 376 } |
| 377 |
| 378 // Key is guaranteed to be an RSA SSA key. |
| 379 Status VerifyRsaSsaPkcs1v1_5(PublicKey* key, |
| 380 const blink::WebCryptoAlgorithm& hash, |
| 381 const CryptoData& signature, |
| 382 const CryptoData& data, |
| 383 bool* signature_match) { |
| 384 // TODO(eroman): http://crbug.com/267888 |
| 385 return Status::ErrorUnsupported(); |
| 386 } |
| 387 |
| 388 Status ImportKeySpki(const blink::WebCryptoAlgorithm& algorithm_or_null, |
| 389 const CryptoData& key_data, |
| 390 bool extractable, |
| 391 blink::WebCryptoKeyUsageMask usage_mask, |
| 392 blink::WebCryptoKey* key) { |
| 393 // TODO(eroman): http://crbug.com/267888 |
| 394 return Status::ErrorUnsupported(); |
| 395 } |
| 396 |
| 397 Status ImportKeyPkcs8(const blink::WebCryptoAlgorithm& algorithm_or_null, |
| 398 const CryptoData& key_data, |
| 399 bool extractable, |
| 400 blink::WebCryptoKeyUsageMask usage_mask, |
| 401 blink::WebCryptoKey* key) { |
| 402 // TODO(eroman): http://crbug.com/267888 |
| 403 return Status::ErrorUnsupported(); |
| 404 } |
| 405 |
| 406 Status ExportKeySpki(PublicKey* key, blink::WebArrayBuffer* buffer) { |
| 407 // TODO(eroman): http://crbug.com/267888 |
| 408 return Status::ErrorUnsupported(); |
| 409 } |
| 410 |
| 411 } // namespace platform |
| 412 |
| 413 } // namespace webcrypto |
| 414 |
518 } // namespace content | 415 } // namespace content |
OLD | NEW |