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 19 matching lines...) Expand all Loading... | |
| 30 } | 30 } |
| 31 | 31 |
| 32 PK11SymKey* key() { return key_.get(); } | 32 PK11SymKey* key() { return key_.get(); } |
| 33 | 33 |
| 34 private: | 34 private: |
| 35 crypto::ScopedPK11SymKey key_; | 35 crypto::ScopedPK11SymKey key_; |
| 36 | 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); | 37 DISALLOW_COPY_AND_ASSIGN(SymKeyHandle); |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 class PublicKeyHandle : public WebKit::WebCryptoKeyHandle { | |
| 41 public: | |
| 42 explicit PublicKeyHandle(crypto::ScopedSECKEYPublicKey key) { | |
| 43 DCHECK(!key_.get()); | |
| 44 key_ = key.Pass(); | |
| 45 } | |
| 46 | |
| 47 SECKEYPublicKey* key() { return key_.get(); } | |
| 48 | |
| 49 private: | |
| 50 crypto::ScopedSECKEYPublicKey key_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(PublicKeyHandle); | |
| 53 }; | |
| 54 | |
| 55 class PrivateKeyHandle : public WebKit::WebCryptoKeyHandle { | |
| 56 public: | |
| 57 explicit PrivateKeyHandle(crypto::ScopedSECKEYPrivateKey key) { | |
| 58 DCHECK(!key_.get()); | |
| 59 key_ = key.Pass(); | |
| 60 } | |
| 61 | |
| 62 SECKEYPrivateKey* key() { return key_.get(); } | |
| 63 | |
| 64 private: | |
| 65 crypto::ScopedSECKEYPrivateKey key_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(PrivateKeyHandle); | |
| 68 }; | |
| 69 | |
| 40 HASH_HashType WebCryptoAlgorithmToNSSHashType( | 70 HASH_HashType WebCryptoAlgorithmToNSSHashType( |
| 41 const WebKit::WebCryptoAlgorithm& algorithm) { | 71 const WebKit::WebCryptoAlgorithm& algorithm) { |
| 42 switch (algorithm.id()) { | 72 switch (algorithm.id()) { |
| 43 case WebKit::WebCryptoAlgorithmIdSha1: | 73 case WebKit::WebCryptoAlgorithmIdSha1: |
| 44 return HASH_AlgSHA1; | 74 return HASH_AlgSHA1; |
| 45 case WebKit::WebCryptoAlgorithmIdSha224: | 75 case WebKit::WebCryptoAlgorithmIdSha224: |
| 46 return HASH_AlgSHA224; | 76 return HASH_AlgSHA224; |
| 47 case WebKit::WebCryptoAlgorithmIdSha256: | 77 case WebKit::WebCryptoAlgorithmIdSha256: |
| 48 return HASH_AlgSHA256; | 78 return HASH_AlgSHA256; |
| 49 case WebKit::WebCryptoAlgorithmIdSha384: | 79 case WebKit::WebCryptoAlgorithmIdSha384: |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 316 if (!pk11_key) { | 346 if (!pk11_key) { |
| 317 return false; | 347 return false; |
| 318 } | 348 } |
| 319 | 349 |
| 320 key->reset(new SymKeyHandle(pk11_key.Pass())); | 350 key->reset(new SymKeyHandle(pk11_key.Pass())); |
| 321 *type = key_type; | 351 *type = key_type; |
| 322 | 352 |
| 323 return true; | 353 return true; |
| 324 } | 354 } |
| 325 | 355 |
| 356 bool WebCryptoImpl::GenerateKeyPairInternal( | |
| 357 const WebKit::WebCryptoAlgorithm& algorithm, | |
| 358 scoped_ptr<WebKit::WebCryptoKeyHandle>* public_key_handle, | |
| 359 scoped_ptr<WebKit::WebCryptoKeyHandle>* private_key_handle) { | |
| 360 | |
| 361 // TODO (padolph) Handle other asymmetric algorithm key generation | |
|
eroman
2013/10/23 20:02:46
nit: remove the space after TODO
padolph
2013/10/23 23:21:47
Done.
| |
| 362 switch (algorithm.id()) { | |
| 363 case WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | |
| 364 case WebKit::WebCryptoAlgorithmIdRsaOaep: | |
| 365 case WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: { | |
| 366 const WebKit::WebCryptoRsaKeyGenParams* const params = | |
| 367 algorithm.rsaKeyGenParams(); | |
| 368 DCHECK(params); | |
| 369 | |
| 370 crypto::ScopedPK11Slot slot(PK11_GetInternalKeySlot()); | |
| 371 if (!slot || !params->modulusLength() || | |
| 372 !params->publicExponent().size()) { | |
| 373 return false; | |
| 374 } | |
| 375 | |
| 376 // The Web Crypto API says params->m_publicExponent is in big-endian | |
| 377 // order: the first element in the vector is the most significant digit. | |
| 378 // Leading zeros may or may not be present. | |
| 379 DCHECK_LE(params->publicExponent().size(), sizeof(unsigned long)); | |
|
eroman
2013/10/23 20:02:46
This needs to be a runtime error (i.e. "return fal
padolph
2013/10/23 23:21:47
Done.
| |
| 380 const size_t size_minus_1 = params->publicExponent().size() - 1; | |
| 381 unsigned long public_exponent = 0; | |
| 382 for (int i = size_minus_1; i >= 0; --i) { | |
| 383 public_exponent |= params->publicExponent()[i] | |
| 384 << (8 * (size_minus_1 - i)); | |
| 385 } | |
| 386 // TODO (padolph): should we limit the public exponent to the 'safe' set | |
|
eroman
2013/10/23 20:02:46
Ryan: Can you comment on this?
| |
| 387 // of {3, 5, 17, 257, 65537}? | |
| 388 | |
| 389 PK11RSAGenParams param; | |
| 390 param.keySizeInBits = params->modulusLength(); | |
| 391 param.pe = public_exponent; | |
| 392 | |
| 393 // Flags are verified at the Blink layer; here the flags are set to all | |
| 394 // possible operations for the given key type. | |
| 395 CK_FLAGS operation_flags; | |
| 396 switch (algorithm.id()) { | |
| 397 case WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | |
| 398 operation_flags = CKF_ENCRYPT | CKF_DECRYPT; | |
| 399 break; | |
| 400 case WebKit::WebCryptoAlgorithmIdRsaOaep: | |
| 401 operation_flags = CKF_WRAP | CKF_UNWRAP; | |
| 402 break; | |
| 403 case WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | |
| 404 operation_flags = CKF_SIGN | CKF_VERIFY; | |
| 405 break; | |
| 406 default: | |
| 407 DCHECK(false); | |
|
eroman
2013/10/23 20:02:46
NOTREACHED();
Also would be good to add a defensiv
padolph
2013/10/23 23:21:47
Done.
| |
| 408 } | |
| 409 const CK_FLAGS operation_flags_mask = CKF_ENCRYPT | CKF_DECRYPT | | |
| 410 CKF_SIGN | CKF_VERIFY | CKF_WRAP | | |
| 411 CKF_UNWRAP; | |
| 412 const PK11AttrFlags attribute_flags = 0; // default all PK11_ATTR_ flags | |
| 413 | |
| 414 SECKEYPublicKey* sec_public_key; | |
| 415 crypto::ScopedSECKEYPrivateKey private_key( | |
| 416 PK11_GenerateKeyPairWithOpFlags(slot.get(), | |
| 417 CKM_RSA_PKCS_KEY_PAIR_GEN, | |
| 418 ¶m, | |
| 419 &sec_public_key, | |
| 420 attribute_flags, | |
| 421 operation_flags, | |
| 422 operation_flags_mask, | |
| 423 NULL)); | |
| 424 if (!private_key) { | |
| 425 return false; | |
|
eroman
2013/10/23 20:02:46
Is it guaranteed that sec_public_key hasn't been s
padolph
2013/10/23 23:21:47
Looking at the NSS source, the pubkey is destroyed
| |
| 426 } | |
| 427 crypto::ScopedSECKEYPublicKey public_key(sec_public_key); | |
| 428 | |
| 429 public_key_handle->reset(new PublicKeyHandle(public_key.Pass())); | |
| 430 private_key_handle->reset(new PrivateKeyHandle(private_key.Pass())); | |
| 431 | |
| 432 return true; | |
| 433 } | |
| 434 default: | |
| 435 return false; | |
| 436 } | |
| 437 } | |
| 326 | 438 |
| 327 bool WebCryptoImpl::ImportKeyInternal( | 439 bool WebCryptoImpl::ImportKeyInternal( |
| 328 WebKit::WebCryptoKeyFormat format, | 440 WebKit::WebCryptoKeyFormat format, |
| 329 const unsigned char* key_data, | 441 const unsigned char* key_data, |
| 330 unsigned key_data_size, | 442 unsigned key_data_size, |
| 331 const WebKit::WebCryptoAlgorithm& algorithm, | 443 const WebKit::WebCryptoAlgorithm& algorithm, |
| 332 WebKit::WebCryptoKeyUsageMask usage_mask, | 444 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 333 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, | 445 scoped_ptr<WebKit::WebCryptoKeyHandle>* handle, |
| 334 WebKit::WebCryptoKeyType* type) { | 446 WebKit::WebCryptoKeyType* type) { |
| 335 switch (algorithm.id()) { | 447 switch (algorithm.id()) { |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 break; | 611 break; |
| 500 } | 612 } |
| 501 default: | 613 default: |
| 502 return false; | 614 return false; |
| 503 } | 615 } |
| 504 | 616 |
| 505 return true; | 617 return true; |
| 506 } | 618 } |
| 507 | 619 |
| 508 } // namespace content | 620 } // namespace content |
| OLD | NEW |