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/webcrypto_impl.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/sha.h> | 11 #include <openssl/sha.h> |
12 #include <openssl/evp.h> | 12 #include <openssl/evp.h> |
13 #include <openssl/rand.h> | 13 #include <openssl/rand.h> |
14 | 14 |
15 #include "base/logging.h" | 15 #include "base/logging.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 "crypto/secure_util.h" |
18 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 19 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
21 | 22 |
22 namespace content { | 23 namespace content { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 int final_output_chunk_len = 0; | 145 int final_output_chunk_len = 0; |
145 if (!EVP_CipherFinal_ex( | 146 if (!EVP_CipherFinal_ex( |
146 context.get(), buffer_data + output_len, &final_output_chunk_len)) | 147 context.get(), buffer_data + output_len, &final_output_chunk_len)) |
147 return false; | 148 return false; |
148 | 149 |
149 const unsigned final_output_len = | 150 const unsigned final_output_len = |
150 static_cast<unsigned>(output_len) + | 151 static_cast<unsigned>(output_len) + |
151 static_cast<unsigned>(final_output_chunk_len); | 152 static_cast<unsigned>(final_output_chunk_len); |
152 DCHECK_LE(final_output_len, output_max_len); | 153 DCHECK_LE(final_output_len, output_max_len); |
153 | 154 |
154 WebCryptoImpl::ShrinkBuffer(buffer, final_output_len); | 155 ShrinkBuffer(buffer, final_output_len); |
155 | 156 |
156 return true; | 157 return true; |
157 } | 158 } |
158 | 159 |
159 } // namespace | 160 } // namespace |
160 | 161 |
161 void WebCryptoImpl::Init() { crypto::EnsureOpenSSLInit(); } | 162 void WebCryptoImpl::Init() { crypto::EnsureOpenSSLInit(); } |
162 | 163 |
163 bool WebCryptoImpl::EncryptInternal(const WebKit::WebCryptoAlgorithm& algorithm, | 164 bool WebCryptoImpl::EncryptInternal(const WebKit::WebCryptoAlgorithm& algorithm, |
164 const WebKit::WebCryptoKey& key, | 165 const WebKit::WebCryptoKey& key, |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 if (algorithm_or_null.isNull()) | 313 if (algorithm_or_null.isNull()) |
313 return false; | 314 return false; |
314 const WebKit::WebCryptoAlgorithm& algorithm = algorithm_or_null; | 315 const WebKit::WebCryptoAlgorithm& algorithm = algorithm_or_null; |
315 | 316 |
316 // TODO(padolph): Support all relevant alg types and then remove this gate. | 317 // TODO(padolph): Support all relevant alg types and then remove this gate. |
317 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac && | 318 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac && |
318 algorithm.id() != WebKit::WebCryptoAlgorithmIdAesCbc) { | 319 algorithm.id() != WebKit::WebCryptoAlgorithmIdAesCbc) { |
319 return false; | 320 return false; |
320 } | 321 } |
321 | 322 |
322 // TODO(padolph): Need to split handling for symmetric (raw or jwk format) and | 323 // TODO(padolph): Need to split handling for symmetric (raw format) and |
323 // asymmetric (jwk, spki, or pkcs8 format) keys. | 324 // asymmetric (spki or pkcs8 format) keys. |
324 // Currently only supporting symmetric. | 325 // Currently only supporting symmetric. |
325 | 326 |
326 // TODO(padolph): jwk handling. Define precedence between jwk contents and | |
327 // this method's parameters, e.g. 'alg' in jwk vs algorithm.id(). Who wins if | |
328 // they differ? (jwk, probably) | |
329 | |
330 // Symmetric keys are always type secret | 327 // Symmetric keys are always type secret |
331 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypeSecret; | 328 WebKit::WebCryptoKeyType type = WebKit::WebCryptoKeyTypeSecret; |
332 | 329 |
333 const unsigned char* raw_key_data; | 330 const unsigned char* raw_key_data; |
334 unsigned raw_key_data_size; | 331 unsigned raw_key_data_size; |
335 switch (format) { | 332 switch (format) { |
336 case WebKit::WebCryptoKeyFormatRaw: | 333 case WebKit::WebCryptoKeyFormatRaw: |
337 raw_key_data = key_data; | 334 raw_key_data = key_data; |
338 raw_key_data_size = key_data_size; | 335 raw_key_data_size = key_data_size; |
339 // The NSS implementation fails when importing a raw AES key with a length | 336 // The NSS implementation fails when importing a raw AES key with a length |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 | 468 |
472 break; | 469 break; |
473 } | 470 } |
474 default: | 471 default: |
475 return false; | 472 return false; |
476 } | 473 } |
477 return true; | 474 return true; |
478 } | 475 } |
479 | 476 |
480 } // namespace content | 477 } // namespace content |
OLD | NEW |