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 |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "crypto/nss_util.h" | 14 #include "crypto/nss_util.h" |
| 15 #include "crypto/scoped_nss_types.h" | 15 #include "crypto/scoped_nss_types.h" |
| 16 #include "crypto/secure_util.h" | 16 #include "crypto/secure_util.h" |
| 17 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 17 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 18 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class SymKeyHandle : public WebKit::WebCryptoKeyHandle { | 25 class SymKeyHandle : public WebKit::WebCryptoKeyHandle { |
| 26 public: | 26 public: |
| 27 explicit SymKeyHandle(crypto::ScopedPK11SymKey key) { | 27 explicit SymKeyHandle(crypto::ScopedPK11SymKey key) : key_(key.Pass()) {} |
| 28 DCHECK(!key_.get()); | |
| 29 key_ = key.Pass(); | |
| 30 } | |
| 31 | 28 |
| 32 PK11SymKey* key() { return key_.get(); } | 29 PK11SymKey* key() { return key_.get(); } |
| 33 | 30 |
| 34 private: | 31 private: |
| 35 crypto::ScopedPK11SymKey key_; | 32 crypto::ScopedPK11SymKey key_; |
| 36 | 33 |
| 37 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); | 34 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); |
| 38 }; | 35 }; |
| 39 | 36 |
| 37 class PublicKeyHandle : public WebKit::WebCryptoKeyHandle { | |
| 38 public: | |
| 39 explicit PublicKeyHandle(crypto::ScopedSECKEYPublicKey key) | |
| 40 : key_(key.Pass()) {} | |
| 41 | |
| 42 SECKEYPublicKey* key() { return key_.get(); } | |
| 43 | |
| 44 private: | |
| 45 crypto::ScopedSECKEYPublicKey key_; | |
| 46 | |
| 47 DISALLOW_COPY_AND_ASSIGN(PublicKeyHandle); | |
| 48 }; | |
| 49 | |
| 50 class PrivateKeyHandle : public WebKit::WebCryptoKeyHandle { | |
| 51 public: | |
| 52 explicit PrivateKeyHandle(crypto::ScopedSECKEYPrivateKey key) | |
| 53 : key_(key.Pass()) {} | |
| 54 | |
| 55 SECKEYPrivateKey* key() { return key_.get(); } | |
| 56 | |
| 57 private: | |
| 58 crypto::ScopedSECKEYPrivateKey key_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(PrivateKeyHandle); | |
| 61 }; | |
| 62 | |
| 40 HASH_HashType WebCryptoAlgorithmToNSSHashType( | 63 HASH_HashType WebCryptoAlgorithmToNSSHashType( |
| 41 const WebKit::WebCryptoAlgorithm& algorithm) { | 64 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 42 switch (algorithm.id()) { | 65 switch (algorithm.id()) { |
| 43 case WebKit::WebCryptoAlgorithmIdSha1: | 66 case WebKit::WebCryptoAlgorithmIdSha1: |
| 44 return HASH_AlgSHA1; | 67 return HASH_AlgSHA1; |
| 45 case WebKit::WebCryptoAlgorithmIdSha224: | 68 case WebKit::WebCryptoAlgorithmIdSha224: |
| 46 return HASH_AlgSHA224; | 69 return HASH_AlgSHA224; |
| 47 case WebKit::WebCryptoAlgorithmIdSha256: | 70 case WebKit::WebCryptoAlgorithmIdSha256: |
| 48 return HASH_AlgSHA256; | 71 return HASH_AlgSHA256; |
| 49 case WebKit::WebCryptoAlgorithmIdSha384: | 72 case WebKit::WebCryptoAlgorithmIdSha384: |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 if (!pk11_key) { | 339 if (!pk11_key) { |
| 317 return false; | 340 return false; |
| 318 } | 341 } |
| 319 | 342 |
| 320 key->reset(new SymKeyHandle(pk11_key.Pass())); | 343 key->reset(new SymKeyHandle(pk11_key.Pass())); |
| 321 *type = key_type; | 344 *type = key_type; |
| 322 | 345 |
| 323 return true; | 346 return true; |
| 324 } | 347 } |
| 325 | 348 |
| 349 bool WebCryptoImpl::GenerateKeyPairInternal( | |
| 350 const WebKit::WebCryptoAlgorithm& algorithm, | |
| 351 scoped_ptr<WebKit::WebCryptoKeyHandle>* public_key_handle, | |
| 352 scoped_ptr<WebKit::WebCryptoKeyHandle>* private_key_handle) { | |
| 353 | |
| 354 // TODO(padolph) Handle other asymmetric algorithm key generation | |
| 355 switch (algorithm.id()) { | |
| 356 case WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | |
| 357 case WebKit::WebCryptoAlgorithmIdRsaOaep: | |
| 358 case WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: { | |
| 359 const WebKit::WebCryptoRsaKeyGenParams* const params = | |
| 360 algorithm.rsaKeyGenParams(); | |
| 361 DCHECK(params); | |
| 362 | |
| 363 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); | |
| 364 if (!slot || !params->modulusLength() || | |
| 365 !params->publicExponent().size()) { | |
|
eroman
2013/10/24 22:28:59
Sleevi: General question: In WebCrypto's "BigInteg
| |
| 366 return false; | |
| 367 } | |
| 368 | |
| 369 // The Web Crypto API says params->m_publicExponent is in big-endian | |
| 370 // order: the first element in the vector is the most significant digit. | |
| 371 // Leading zeros may or may not be present. | |
| 372 | |
| 373 // First offset past any leading zeros in publicExponent. | |
|
eroman
2013/10/24 22:28:59
Could you please extract this block of code to a s
padolph
2013/10/25 01:21:46
Done.
| |
| 374 size_t start_index; | |
| 375 for (start_index = 0; start_index < params->publicExponent().size(); | |
|
eroman
2013/10/24 22:28:59
In my opinion this logic would be more readable as
padolph
2013/10/25 01:21:46
Done.
| |
| 376 ++start_index) { | |
| 377 if (params->publicExponent()[start_index] != 0x0) | |
| 378 break; | |
| 379 } | |
| 380 const size_t exponent_size = | |
| 381 params->publicExponent().size() - start_index; | |
| 382 if (start_index == params->publicExponent().size() || | |
| 383 exponent_size > sizeof(unsigned long)) { | |
| 384 return false; | |
| 385 } | |
| 386 | |
| 387 // Then convert the remaining data to an unsigned long. | |
| 388 const size_t end_index = params->publicExponent().size() - 1; | |
| 389 unsigned long public_exponent = 0; | |
| 390 for (int i = end_index; i >= static_cast<int>(start_index); --i) { | |
| 391 public_exponent |= params->publicExponent()[i] << (8 * (end_index - i)); | |
| 392 } | |
| 393 | |
| 394 PK11RSAGenParams param; | |
| 395 param.keySizeInBits = params->modulusLength(); | |
| 396 param.pe = public_exponent; | |
| 397 | |
| 398 // Flags are verified at the Blink layer; here the flags are set to all | |
| 399 // possible operations for the given key type. | |
| 400 CK_FLAGS operation_flags; | |
| 401 switch (algorithm.id()) { | |
| 402 case WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | |
| 403 operation_flags = CKF_ENCRYPT | CKF_DECRYPT | CKF_WRAP | CKF_UNWRAP; | |
| 404 break; | |
| 405 case WebKit::WebCryptoAlgorithmIdRsaOaep: | |
| 406 operation_flags = CKF_ENCRYPT | CKF_DECRYPT | CKF_WRAP | CKF_UNWRAP; | |
| 407 break; | |
| 408 case WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | |
| 409 operation_flags = CKF_SIGN | CKF_VERIFY; | |
| 410 break; | |
| 411 default: | |
| 412 NOTREACHED(); | |
| 413 return false; | |
| 414 } | |
| 415 const CK_FLAGS operation_flags_mask = CKF_ENCRYPT | CKF_DECRYPT | | |
| 416 CKF_SIGN | CKF_VERIFY | CKF_WRAP | | |
| 417 CKF_UNWRAP; | |
| 418 const PK11AttrFlags attribute_flags = 0; // default all PK11_ATTR_ flags | |
| 419 | |
| 420 SECKEYPublicKey* sec_public_key; | |
| 421 crypto::ScopedSECKEYPrivateKey private_key( | |
| 422 PK11_GenerateKeyPairWithOpFlags(slot.get(), | |
| 423 CKM_RSA_PKCS_KEY_PAIR_GEN, | |
| 424 ¶m, | |
| 425 &sec_public_key, | |
| 426 attribute_flags, | |
| 427 operation_flags, | |
| 428 operation_flags_mask, | |
| 429 NULL)); | |
| 430 if (!private_key) { | |
| 431 return false; | |
| 432 } | |
| 433 crypto::ScopedSECKEYPublicKey public_key(sec_public_key); | |
| 434 | |
| 435 public_key_handle->reset(new PublicKeyHandle(public_key.Pass())); | |
| 436 private_key_handle->reset(new PrivateKeyHandle(private_key.Pass())); | |
| 437 | |
| 438 return true; | |
| 439 } | |
| 440 default: | |
| 441 return false; | |
| 442 } | |
| 443 } | |
| 326 | 444 |
| 327 bool WebCryptoImpl::ImportKeyInternal( | 445 bool WebCryptoImpl::ImportKeyInternal( |
| 328 WebKit::WebCryptoKeyFormat format, | 446 WebKit::WebCryptoKeyFormat format, |
| 329 const unsigned char* key_data, | 447 const unsigned char* key_data, |
| 330 unsigned key_data_size, | 448 unsigned key_data_size, |
| 331 const WebKit::WebCryptoAlgorithm& algorithm, | 449 const WebKit::WebCryptoAlgorithm& algorithm, |
| 332 WebKit::WebCryptoKeyUsageMask usage_mask, | 450 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 333 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, | 451 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, |
| 334 WebKit::WebCryptoKeyType* type) { | 452 WebKit::WebCryptoKeyType* type) { |
| 335 switch (algorithm.id()) { | 453 switch (algorithm.id()) { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 break; | 617 break; |
| 500 } | 618 } |
| 501 default: | 619 default: |
| 502 return false; | 620 return false; |
| 503 } | 621 } |
| 504 | 622 |
| 505 return true; | 623 return true; |
| 506 } | 624 } |
| 507 | 625 |
| 508 } // namespace content | 626 } // namespace content |
| OLD | NEW |