Chromium Code Reviews| 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 <cryptohi.h> | 7 #include <cryptohi.h> |
| 8 #include <pk11pub.h> | 8 #include <pk11pub.h> |
| 9 #include <sechash.h> | 9 #include <sechash.h> |
| 10 | 10 |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 void WebCryptoImpl::Init() { | 243 void WebCryptoImpl::Init() { |
| 244 crypto::EnsureNSSInit(); | 244 crypto::EnsureNSSInit(); |
| 245 } | 245 } |
| 246 | 246 |
| 247 bool WebCryptoImpl::EncryptInternal( | 247 bool WebCryptoImpl::EncryptInternal( |
| 248 const blink::WebCryptoAlgorithm& algorithm, | 248 const blink::WebCryptoAlgorithm& algorithm, |
| 249 const blink::WebCryptoKey& key, | 249 const blink::WebCryptoKey& key, |
| 250 const unsigned char* data, | 250 const unsigned char* data, |
| 251 unsigned data_size, | 251 unsigned data_size, |
| 252 blink::WebArrayBuffer* buffer) { | 252 blink::WebArrayBuffer* buffer) { |
| 253 | |
| 254 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | |
| 255 DCHECK(key.handle()); | |
| 256 DCHECK(buffer); | |
| 257 | |
| 253 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { | 258 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { |
| 254 return AesCbcEncryptDecrypt( | 259 return AesCbcEncryptDecrypt( |
| 255 CKA_ENCRYPT, algorithm, key, data, data_size, buffer); | 260 CKA_ENCRYPT, algorithm, key, data, data_size, buffer); |
| 261 } else if (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5) { | |
|
eroman
2013/11/21 02:07:01
@rsleevi: Can you review this file as well? (Since
| |
| 262 | |
| 263 // RSAES encryption does not support empty input | |
| 264 if (!data_size) | |
| 265 return false; | |
| 266 DCHECK(data); | |
| 267 | |
| 268 if (key.type() != blink::WebCryptoKeyTypePublic) | |
| 269 return false; | |
| 270 | |
| 271 PublicKeyHandle* const public_key = | |
| 272 reinterpret_cast<PublicKeyHandle*>(key.handle()); | |
| 273 | |
| 274 const unsigned encrypted_length_bytes = | |
| 275 SECKEY_PublicKeyStrength(public_key->key()); | |
| 276 | |
| 277 // RSAES can operate on messages up to a length of k - 11, where k is the | |
| 278 // octet length of the RSA modulus. | |
| 279 if (encrypted_length_bytes < 11 || encrypted_length_bytes - 11 < data_size) | |
| 280 return false; | |
| 281 | |
| 282 *buffer = blink::WebArrayBuffer::create(encrypted_length_bytes, 1); | |
| 283 unsigned char* const buffer_data = | |
| 284 reinterpret_cast<unsigned char*>(buffer->data()); | |
| 285 | |
| 286 if (PK11_PubEncryptPKCS1(public_key->key(), | |
| 287 buffer_data, | |
| 288 const_cast<unsigned char*>(data), | |
| 289 data_size, | |
| 290 NULL) != SECSuccess) { | |
| 291 return false; | |
| 292 } | |
| 293 return true; | |
| 256 } | 294 } |
| 257 | 295 |
| 258 return false; | 296 return false; |
| 259 } | 297 } |
| 260 | 298 |
| 261 bool WebCryptoImpl::DecryptInternal( | 299 bool WebCryptoImpl::DecryptInternal( |
| 262 const blink::WebCryptoAlgorithm& algorithm, | 300 const blink::WebCryptoAlgorithm& algorithm, |
| 263 const blink::WebCryptoKey& key, | 301 const blink::WebCryptoKey& key, |
| 264 const unsigned char* data, | 302 const unsigned char* data, |
| 265 unsigned data_size, | 303 unsigned data_size, |
| 266 blink::WebArrayBuffer* buffer) { | 304 blink::WebArrayBuffer* buffer) { |
| 305 | |
| 306 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | |
| 307 DCHECK(key.handle()); | |
| 308 DCHECK(buffer); | |
| 309 | |
| 267 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { | 310 if (algorithm.id() == blink::WebCryptoAlgorithmIdAesCbc) { |
| 268 return AesCbcEncryptDecrypt( | 311 return AesCbcEncryptDecrypt( |
| 269 CKA_DECRYPT, algorithm, key, data, data_size, buffer); | 312 CKA_DECRYPT, algorithm, key, data, data_size, buffer); |
| 313 } else if (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5) { | |
| 314 | |
| 315 // RSAES decryption does not support empty input | |
| 316 if (!data_size) | |
| 317 return false; | |
| 318 DCHECK(data); | |
| 319 | |
| 320 if (key.type() != blink::WebCryptoKeyTypePrivate) | |
| 321 return false; | |
| 322 | |
| 323 PrivateKeyHandle* const private_key = | |
| 324 reinterpret_cast<PrivateKeyHandle*>(key.handle()); | |
| 325 | |
| 326 const int modulus_length_bytes = | |
| 327 PK11_GetPrivateModulusLen(private_key->key()); | |
| 328 if (modulus_length_bytes <= 0) | |
| 329 return false; | |
| 330 const unsigned max_output_length_bytes = modulus_length_bytes; | |
| 331 | |
| 332 *buffer = blink::WebArrayBuffer::create(max_output_length_bytes, 1); | |
| 333 unsigned char* const buffer_data = | |
| 334 reinterpret_cast<unsigned char*>(buffer->data()); | |
| 335 | |
| 336 unsigned output_length_bytes = 0; | |
| 337 if (PK11_PrivDecryptPKCS1(private_key->key(), | |
| 338 buffer_data, | |
| 339 &output_length_bytes, | |
| 340 max_output_length_bytes, | |
| 341 const_cast<unsigned char*>(data), | |
| 342 data_size) != SECSuccess) { | |
| 343 return false; | |
| 344 } | |
| 345 DCHECK_LE(output_length_bytes, max_output_length_bytes); | |
| 346 WebCryptoImpl::ShrinkBuffer(buffer, output_length_bytes); | |
| 347 return true; | |
| 270 } | 348 } |
| 271 | 349 |
| 272 return false; | 350 return false; |
| 273 } | 351 } |
| 274 | 352 |
| 275 bool WebCryptoImpl::DigestInternal( | 353 bool WebCryptoImpl::DigestInternal( |
| 276 const blink::WebCryptoAlgorithm& algorithm, | 354 const blink::WebCryptoAlgorithm& algorithm, |
| 277 const unsigned char* data, | 355 const unsigned char* data, |
| 278 unsigned data_size, | 356 unsigned data_size, |
| 279 blink::WebArrayBuffer* buffer) { | 357 blink::WebArrayBuffer* buffer) { |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 642 break; | 720 break; |
| 643 } | 721 } |
| 644 default: | 722 default: |
| 645 return false; | 723 return false; |
| 646 } | 724 } |
| 647 | 725 |
| 648 return true; | 726 return true; |
| 649 } | 727 } |
| 650 | 728 |
| 651 } // namespace content | 729 } // namespace content |
| OLD | NEW |