| 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 WebKit::WebCryptoKeyUsageMask /*usage_mask*/, | 306 WebKit::WebCryptoKeyUsageMask /*usage_mask*/, |
| 306 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, | 307 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, |
| 307 WebKit::WebCryptoKeyType* type) { | 308 WebKit::WebCryptoKeyType* type) { |
| 308 | 309 |
| 309 // TODO(padolph): Support all relevant alg types and then remove this gate. | 310 // TODO(padolph): Support all relevant alg types and then remove this gate. |
| 310 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac && | 311 if (algorithm.id() != WebKit::WebCryptoAlgorithmIdHmac && |
| 311 algorithm.id() != WebKit::WebCryptoAlgorithmIdAesCbc) { | 312 algorithm.id() != WebKit::WebCryptoAlgorithmIdAesCbc) { |
| 312 return false; | 313 return false; |
| 313 } | 314 } |
| 314 | 315 |
| 315 // TODO(padolph): Need to split handling for symmetric (raw or jwk format) and | 316 // TODO(padolph): Need to split handling for symmetric (raw format) and |
| 316 // asymmetric (jwk, spki, or pkcs8 format) keys. | 317 // asymmetric (spki or pkcs8 format) keys. |
| 317 // Currently only supporting symmetric. | 318 // Currently only supporting symmetric. |
| 318 | 319 |
| 319 // TODO(padolph): jwk handling. Define precedence between jwk contents and | |
| 320 // this method's parameters, e.g. 'alg' in jwk vs algorithm.id(). Who wins if | |
| 321 // they differ? (jwk, probably) | |
| 322 | |
| 323 // Symmetric keys are always type secret | 320 // Symmetric keys are always type secret |
| 324 *type = WebKit::WebCryptoKeyTypeSecret; | 321 *type = WebKit::WebCryptoKeyTypeSecret; |
| 325 | 322 |
| 326 const unsigned char* raw_key_data; | 323 const unsigned char* raw_key_data; |
| 327 unsigned raw_key_data_size; | 324 unsigned raw_key_data_size; |
| 328 switch (format) { | 325 switch (format) { |
| 329 case WebKit::WebCryptoKeyFormatRaw: | 326 case WebKit::WebCryptoKeyFormatRaw: |
| 330 raw_key_data = key_data; | 327 raw_key_data = key_data; |
| 331 raw_key_data_size = key_data_size; | 328 raw_key_data_size = key_data_size; |
| 332 // The NSS implementation fails when importing a raw AES key with a length | 329 // The NSS implementation fails when importing a raw AES key with a length |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 | 459 |
| 463 break; | 460 break; |
| 464 } | 461 } |
| 465 default: | 462 default: |
| 466 return false; | 463 return false; |
| 467 } | 464 } |
| 468 return true; | 465 return true; |
| 469 } | 466 } |
| 470 | 467 |
| 471 } // namespace content | 468 } // namespace content |
| OLD | NEW |