Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/child/webcrypto/platform_crypto.h" | 5 #include "content/child/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 "base/memory/scoped_ptr.h" | |
| 15 #include "content/child/webcrypto/crypto_data.h" | 16 #include "content/child/webcrypto/crypto_data.h" |
| 16 #include "content/child/webcrypto/status.h" | 17 #include "content/child/webcrypto/status.h" |
| 17 #include "content/child/webcrypto/webcrypto_util.h" | 18 #include "content/child/webcrypto/webcrypto_util.h" |
| 18 #include "crypto/openssl_util.h" | 19 #include "crypto/openssl_util.h" |
| 19 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 20 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 21 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 22 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 22 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 23 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 23 | 24 |
| 24 namespace content { | 25 namespace content { |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 static_cast<unsigned int>(final_output_chunk_len); | 145 static_cast<unsigned int>(final_output_chunk_len); |
| 145 DCHECK_LE(final_output_len, output_max_len); | 146 DCHECK_LE(final_output_len, output_max_len); |
| 146 | 147 |
| 147 ShrinkBuffer(buffer, final_output_len); | 148 ShrinkBuffer(buffer, final_output_len); |
| 148 | 149 |
| 149 return Status::Success(); | 150 return Status::Success(); |
| 150 } | 151 } |
| 151 | 152 |
| 152 } // namespace | 153 } // namespace |
| 153 | 154 |
| 155 class DigestorOpenSSL : public blink::WebCryptoDigestor { | |
| 156 public: | |
| 157 DigestorOpenSSL(blink::WebCryptoAlgorithmId algorithm_id) | |
|
eroman
2014/03/25 23:26:23
explicit
jww
2014/03/26 00:42:31
Done.
| |
| 158 : initialized_(false), | |
| 159 digest_context_(EVP_MD_CTX_create()), | |
| 160 algorithm_id_(algorithm_id) {} | |
| 161 | |
| 162 virtual bool consume(const unsigned char* data, unsigned int size) OVERRIDE { | |
|
eroman
2014/03/25 23:26:23
ditto on OVERRIDE. I expect this will be pretty st
jww
2014/03/26 00:42:31
Done.
| |
| 163 return consumeWithStatus(data, size).IsSuccess(); | |
| 164 } | |
| 165 | |
| 166 Status consumeWithStatus(const unsigned char* data, unsigned int size) { | |
|
eroman
2014/03/25 23:26:23
For ConsumeWithStatus (should use chromium style f
jww
2014/03/26 00:42:31
Done.
| |
| 167 crypto::OpenSSLErrStackTracer(FROM_HERE); | |
| 168 Status error = init(); | |
| 169 if (!error.IsSuccess()) | |
| 170 return error; | |
| 171 | |
| 172 if (!EVP_DigestUpdate(digest_context_.get(), data, size)) | |
| 173 return Status::Error(); | |
| 174 | |
| 175 return Status::Success(); | |
| 176 } | |
| 177 | |
| 178 virtual bool finish(unsigned char*& result_data, | |
| 179 unsigned int& result_data_size) OVERRIDE { | |
| 180 Status error = finishInternal(result_, &result_data_size); | |
| 181 if (!error.IsSuccess()) | |
| 182 return false; | |
| 183 result_data = result_; | |
| 184 return true; | |
| 185 } | |
| 186 | |
| 187 virtual Status finishWithWebArrayAndStatus(blink::WebArrayBuffer& result) { | |
|
eroman
2014/03/25 23:26:23
Make this non-virtual. Also use chromium-style fun
jww
2014/03/26 00:42:31
See my question in platform_crypto_nss.cc about th
| |
| 188 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); | |
| 189 result = blink::WebArrayBuffer::create(hash_expected_size, 1); | |
| 190 unsigned char* const hash_buffer = | |
| 191 static_cast<unsigned char* const>(result.data()); | |
| 192 unsigned int hash_buffer_size; // ignored | |
| 193 Status error = finishInternal(hash_buffer, &hash_buffer_size); | |
| 194 if (!error.IsSuccess()) | |
| 195 result.reset(); | |
| 196 return error; | |
| 197 } | |
| 198 | |
| 199 private: | |
| 200 bool initialized_; | |
| 201 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context_; | |
| 202 blink::WebCryptoAlgorithmId algorithm_id_; | |
| 203 unsigned char result_[EVP_MAX_MD_SIZE]; | |
| 204 | |
| 205 DigestorOpenSSL() {} | |
|
eroman
2014/03/25 23:26:23
Is this necessary?
jww
2014/03/26 00:42:31
Removed.
| |
| 206 | |
| 207 Status init() { | |
| 208 if (initialized_) | |
| 209 return Status::Success(); | |
| 210 | |
| 211 const EVP_MD* digest_algorithm = GetDigest(algorithm_id_); | |
| 212 if (!digest_algorithm) | |
| 213 return Status::ErrorUnexpected(); | |
| 214 | |
| 215 if (!digest_context_.get()) | |
| 216 return Status::Error(); | |
| 217 | |
| 218 if (!EVP_DigestInit_ex(digest_context_.get(), digest_algorithm, NULL)) | |
| 219 return Status::Error(); | |
| 220 | |
| 221 initialized_ = true; | |
| 222 return Status::Success(); | |
| 223 } | |
| 224 | |
| 225 Status finishInternal(unsigned char* result, unsigned int* result_size) { | |
| 226 crypto::OpenSSLErrStackTracer(FROM_HERE); | |
| 227 Status error = init(); | |
| 228 if (!error.IsSuccess()) | |
| 229 return error; | |
| 230 | |
| 231 const int hash_expected_size = EVP_MD_CTX_size(digest_context_.get()); | |
| 232 if (hash_expected_size <= 0) | |
| 233 return Status::ErrorUnexpected(); | |
| 234 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE); | |
| 235 | |
| 236 if (!EVP_DigestFinal_ex(digest_context_.get(), result, result_size) || | |
| 237 static_cast<int>(*result_size) != hash_expected_size) | |
| 238 return Status::Error(); | |
| 239 | |
| 240 return Status::Success(); | |
| 241 } | |
| 242 }; | |
| 243 | |
| 154 Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) { | 244 Status ExportKeyRaw(SymKey* key, blink::WebArrayBuffer* buffer) { |
| 155 *buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size()); | 245 *buffer = CreateArrayBuffer(Uint8VectorStart(key->key()), key->key().size()); |
| 156 return Status::Success(); | 246 return Status::Success(); |
| 157 } | 247 } |
| 158 | 248 |
| 159 void Init() { crypto::EnsureOpenSSLInit(); } | 249 void Init() { crypto::EnsureOpenSSLInit(); } |
| 160 | 250 |
| 161 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode, | 251 Status EncryptDecryptAesCbc(EncryptOrDecrypt mode, |
| 162 SymKey* key, | 252 SymKey* key, |
| 163 const CryptoData& data, | 253 const CryptoData& data, |
| 164 const CryptoData& iv, | 254 const CryptoData& iv, |
| 165 blink::WebArrayBuffer* buffer) { | 255 blink::WebArrayBuffer* buffer) { |
| 166 // TODO(eroman): inline the function here. | 256 // TODO(eroman): inline the function here. |
| 167 return AesCbcEncryptDecrypt(mode, key, iv, data, buffer); | 257 return AesCbcEncryptDecrypt(mode, key, iv, data, buffer); |
| 168 } | 258 } |
| 169 | 259 |
| 170 Status DigestSha(blink::WebCryptoAlgorithmId algorithm, | 260 Status DigestSha(blink::WebCryptoAlgorithmId algorithm, |
| 171 const CryptoData& data, | 261 const CryptoData& data, |
| 172 blink::WebArrayBuffer* buffer) { | 262 blink::WebArrayBuffer* buffer) { |
| 173 crypto::OpenSSLErrStackTracer(FROM_HERE); | 263 DigestorOpenSSL digestor(algorithm); |
| 264 Status error = digestor.consumeWithStatus(data.bytes(), data.byte_length()); | |
| 265 if (!error.IsSuccess()) | |
| 266 return error; | |
| 267 return digestor.finishWithWebArrayAndStatus(*buffer); | |
| 268 } | |
| 174 | 269 |
| 175 const EVP_MD* digest_algorithm = GetDigest(algorithm); | 270 blink::WebCryptoDigestor* CreateDigestor( |
| 176 if (!digest_algorithm) | 271 blink::WebCryptoAlgorithmId algorithm_id) { |
| 177 return Status::ErrorUnexpected(); | 272 return new DigestorOpenSSL(algorithm_id); |
| 178 | |
| 179 crypto::ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> digest_context( | |
| 180 EVP_MD_CTX_create()); | |
| 181 if (!digest_context.get()) | |
| 182 return Status::Error(); | |
| 183 | |
| 184 if (!EVP_DigestInit_ex(digest_context.get(), digest_algorithm, NULL) || | |
| 185 !EVP_DigestUpdate( | |
| 186 digest_context.get(), data.bytes(), data.byte_length())) { | |
| 187 return Status::Error(); | |
| 188 } | |
| 189 | |
| 190 const int hash_expected_size = EVP_MD_CTX_size(digest_context.get()); | |
| 191 if (hash_expected_size <= 0) | |
| 192 return Status::ErrorUnexpected(); | |
| 193 DCHECK_LE(hash_expected_size, EVP_MAX_MD_SIZE); | |
| 194 | |
| 195 *buffer = blink::WebArrayBuffer::create(hash_expected_size, 1); | |
| 196 unsigned char* const hash_buffer = | |
| 197 reinterpret_cast<unsigned char* const>(buffer->data()); | |
| 198 | |
| 199 unsigned int hash_size = 0; | |
| 200 if (!EVP_DigestFinal_ex(digest_context.get(), hash_buffer, &hash_size) || | |
| 201 static_cast<int>(hash_size) != hash_expected_size) { | |
| 202 buffer->reset(); | |
| 203 return Status::Error(); | |
| 204 } | |
| 205 | |
| 206 return Status::Success(); | |
| 207 } | 273 } |
| 208 | 274 |
| 209 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, | 275 Status GenerateSecretKey(const blink::WebCryptoAlgorithm& algorithm, |
| 210 bool extractable, | 276 bool extractable, |
| 211 blink::WebCryptoKeyUsageMask usage_mask, | 277 blink::WebCryptoKeyUsageMask usage_mask, |
| 212 unsigned keylen_bytes, | 278 unsigned keylen_bytes, |
| 213 blink::WebCryptoKey* key) { | 279 blink::WebCryptoKey* key) { |
| 214 // TODO(eroman): Is this right? | 280 // TODO(eroman): Is this right? |
| 215 if (keylen_bytes == 0) | 281 if (keylen_bytes == 0) |
| 216 return Status::ErrorGenerateKeyLength(); | 282 return Status::ErrorGenerateKeyLength(); |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 blink::WebCryptoKey* key) { | 499 blink::WebCryptoKey* key) { |
| 434 // TODO(eroman): http://crbug.com/267888 | 500 // TODO(eroman): http://crbug.com/267888 |
| 435 return Status::ErrorUnsupported(); | 501 return Status::ErrorUnsupported(); |
| 436 } | 502 } |
| 437 | 503 |
| 438 } // namespace platform | 504 } // namespace platform |
| 439 | 505 |
| 440 } // namespace webcrypto | 506 } // namespace webcrypto |
| 441 | 507 |
| 442 } // namespace content | 508 } // namespace content |
| OLD | NEW |