| 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/evp.h> |
| 8 #include <openssl/hmac.h> | 9 #include <openssl/hmac.h> |
| 9 #include <openssl/sha.h> | 10 #include <openssl/sha.h> |
| 10 | 11 |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "crypto/openssl_util.h" | 13 #include "crypto/openssl_util.h" |
| 13 #include "crypto/secure_util.h" | 14 #include "crypto/secure_util.h" |
| 14 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 15 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 15 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 16 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 17 | 18 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 46 // Issue http://crbug.com/267888. | 47 // Issue http://crbug.com/267888. |
| 47 return false; | 48 return false; |
| 48 } | 49 } |
| 49 | 50 |
| 50 bool WebCryptoImpl::DecryptInternal( | 51 bool WebCryptoImpl::DecryptInternal( |
| 51 const WebKit::WebCryptoAlgorithm& algorithm, | 52 const WebKit::WebCryptoAlgorithm& algorithm, |
| 52 const WebKit::WebCryptoKey& key, | 53 const WebKit::WebCryptoKey& key, |
| 53 const unsigned char* data, | 54 const unsigned char* data, |
| 54 unsigned data_size, | 55 unsigned data_size, |
| 55 WebKit::WebArrayBuffer* buffer) { | 56 WebKit::WebArrayBuffer* buffer) { |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 bool WebCryptoImpl::DigestInternal( | |
| 60 const WebKit::WebCryptoAlgorithm& algorithm, | |
| 61 const unsigned char* data, | |
| 62 unsigned data_size, | |
| 63 WebKit::WebArrayBuffer* buffer) { | |
| 64 // TODO(padolph): Placeholder for OpenSSL implementation. | 57 // TODO(padolph): Placeholder for OpenSSL implementation. |
| 65 // Issue http://crbug.com/267888. | 58 // Issue http://crbug.com/267888. |
| 66 return false; | 59 return false; |
| 67 } | 60 } |
| 68 | 61 |
| 62 bool WebCryptoImpl::DigestInternal(const WebKit::WebCryptoAlgorithm& algorithm, |
| 63 const unsigned char* data, |
| 64 unsigned data_size, |
| 65 WebKit::WebArrayBuffer* buffer) { |
| 66 |
| 67 crypto::OpenSSLErrStackTracer(FROM_HERE); |
| 68 |
| 69 const EVP_MD* digest_algorithm; |
| 70 switch (algorithm.id()) { |
| 71 case WebKit::WebCryptoAlgorithmIdSha1: |
| 72 digest_algorithm = EVP_sha1(); |
| 73 break; |
| 74 case WebKit::WebCryptoAlgorithmIdSha224: |
| 75 digest_algorithm = EVP_sha224(); |
| 76 break; |
| 77 case WebKit::WebCryptoAlgorithmIdSha256: |
| 78 digest_algorithm = EVP_sha256(); |
| 79 break; |
| 80 case WebKit::WebCryptoAlgorithmIdSha384: |
| 81 digest_algorithm = EVP_sha384(); |
| 82 break; |
| 83 case WebKit::WebCryptoAlgorithmIdSha512: |
| 84 digest_algorithm = EVP_sha512(); |
| 85 break; |
| 86 default: |
| 87 // Not a digest algorithm. |
| 88 return false; |
| 89 } |
| 90 |
| 91 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context( |
| 92 EVP_MD_CTX_create()); |
| 93 if (!digest_context.get()) { |
| 94 return false; |
| 95 } |
| 96 |
| 97 if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) || |
| 98 !EVP_DigestUpdate(digest_context.get(), data, data_size)) { |
| 99 return false; |
| 100 } |
| 101 |
| 102 const int hash_expected_size = EVP_MD_CTX_size(digest_context.get()); |
| 103 if (hash_expected_size <= 0) { |
| 104 return false; |
| 105 } |
| 106 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE); |
| 107 |
| 108 *buffer = WebKit::WebArrayBuffer::create(hash_expected_size, 1); |
| 109 unsigned char* const hash_buffer = |
| 110 reinterpret_cast<unsigned char* const>(buffer->data()); |
| 111 |
| 112 unsigned hash_size = 0; |
| 113 if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) || |
| 114 static_cast<int>(hash_size) != hash_expected_size) { |
| 115 buffer->reset(); |
| 116 return false; |
| 117 } |
| 118 |
| 119 return true; |
| 120 } |
| 121 |
| 69 bool WebCryptoImpl::ImportKeyInternal( | 122 bool WebCryptoImpl::ImportKeyInternal( |
| 70 WebKit::WebCryptoKeyFormat format, | 123 WebKit::WebCryptoKeyFormat format, |
| 71 const unsigned char* key_data, | 124 const unsigned char* key_data, |
| 72 unsigned key_data_size, | 125 unsigned key_data_size, |
| 73 const WebKit::WebCryptoAlgorithm& algorithm, | 126 const WebKit::WebCryptoAlgorithm& algorithm, |
| 74 WebKit::WebCryptoKeyUsageMask /*usage_mask*/, | 127 WebKit::WebCryptoKeyUsageMask /*usage_mask*/, |
| 75 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, | 128 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, |
| 76 WebKit::WebCryptoKeyType* type) { | 129 WebKit::WebCryptoKeyType* type) { |
| 77 | 130 |
| 78 // TODO(padolph): Support all relevant alg types and then remove this gate. | 131 // TODO(padolph): Support all relevant alg types and then remove this gate. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 | 278 |
| 226 break; | 279 break; |
| 227 } | 280 } |
| 228 default: | 281 default: |
| 229 return false; | 282 return false; |
| 230 } | 283 } |
| 231 return true; | 284 return true; |
| 232 } | 285 } |
| 233 | 286 |
| 234 } // namespace content | 287 } // namespace content |
| OLD | NEW |