Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: content/renderer/webcrypto/webcrypto_impl_nss.cc

Issue 76363006: [webcrypto] Add JWK import of RSA public key for NSS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 size_t reverse_i = data_size - i - 1; 232 size_t reverse_i = data_size - i - 1;
233 233
234 if (reverse_i >= sizeof(unsigned long) && data[i]) 234 if (reverse_i >= sizeof(unsigned long) && data[i])
235 return false; // Too large for a long. 235 return false; // Too large for a long.
236 236
237 *result |= data[i] << 8 * reverse_i; 237 *result |= data[i] << 8 * reverse_i;
238 } 238 }
239 return true; 239 return true;
240 } 240 }
241 241
242 typedef scoped_ptr_malloc<
Ryan Sleevi 2013/11/21 01:41:34 scoped_ptr_malloc is DEPRECATED, as scoped_ptr sup
padolph 2013/11/21 03:38:13 This typedef is no longer needed because of the ch
243 PRArenaPool, crypto::NSSDestroyer1<PRArenaPool,
244 PORT_FreeArena,
245 PR_FALSE> > ScopedPRArenaPool;
246
242 } // namespace 247 } // namespace
243 248
244 void WebCryptoImpl::Init() { 249 void WebCryptoImpl::Init() {
245 crypto::EnsureNSSInit(); 250 crypto::EnsureNSSInit();
246 } 251 }
247 252
248 bool WebCryptoImpl::EncryptInternal( 253 bool WebCryptoImpl::EncryptInternal(
249 const blink::WebCryptoAlgorithm& algorithm, 254 const blink::WebCryptoAlgorithm& algorithm,
250 const blink::WebCryptoKey& key, 255 const blink::WebCryptoKey& key,
251 const unsigned char* data, 256 const unsigned char* data,
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 647
643 break; 648 break;
644 } 649 }
645 default: 650 default:
646 return false; 651 return false;
647 } 652 }
648 653
649 return true; 654 return true;
650 } 655 }
651 656
657 bool WebCryptoImpl::ImportRsaPublicKeyInternal(
eroman 2013/11/20 23:37:12 @rsleevi, dark lord of NSS: Can you review this fu
658 const unsigned char* modulus_data,
659 unsigned modulus_size,
660 const unsigned char* exponent_data,
661 unsigned exponent_size,
662 const blink::WebCryptoAlgorithm& algorithm,
663 bool extractable,
664 blink::WebCryptoKeyUsageMask usage_mask,
665 blink::WebCryptoKey* key) {
666
667 DCHECK(modulus_data);
668 DCHECK(modulus_size);
669 DCHECK(exponent_data);
670 DCHECK(exponent_size);
Ryan Sleevi 2013/11/21 01:41:34 Is Blink going to require these four fields be val
eroman 2013/11/21 01:55:41 The caller here is the JWK parsing code, which val
padolph 2013/11/21 03:38:13 Done.
671
672 // NSS does not provide a way to create an RSA public key directly from the
673 // modulus and exponent values. But it can import an DER-encoded ASN.1 blob
Ryan Sleevi 2013/11/21 01:41:34 "values. But" -> "values, but"
padolph 2013/11/21 03:38:13 Done.
674 // with the values and create the key from that. The code below follows the
675 // recommendation described in
676 // https://developer.mozilla.org/en-US/docs/NSS/NSS_Tech_Notes/nss_tech_note7
677
678 ScopedPRArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
679 if (!arena.get())
680 return false;
681
682 // Pack the input values into struct compatible with NSS ASN.1 encoding, and
683 // set up the ASN.1 encoder template for it.
684 struct RsaPublicKeyData {
685 SECItem modulus;
686 SECItem exponent;
687 };
688 const RsaPublicKeyData pubkey_in = {
689 {siUnsignedInteger, const_cast<unsigned char*>(modulus_data),
690 modulus_size},
691 {siUnsignedInteger, const_cast<unsigned char*>(exponent_data),
692 exponent_size}};
693 const SEC_ASN1Template RsaPublicKeyTemplate[] = {
694 {SEC_ASN1_SEQUENCE, 0, NULL, sizeof(RsaPublicKeyData)},
695 {SEC_ASN1_INTEGER, offsetof(RsaPublicKeyData, modulus), },
696 {SEC_ASN1_INTEGER, offsetof(RsaPublicKeyData, exponent), },
697 {0, }};
Ryan Sleevi 2013/11/21 01:41:34 nit: I seem to recall that clang-format/the style
padolph 2013/11/21 03:38:13 The current format is what clang-format -style=Chr
698
699 // Do the ASN.1 encoding to produce the DER-formatted public key. In this
700 // usage, SEC_ASN1EncodeItem() returns a pointer to the provided output
701 // SECItem (pubkey_der) on success, or NULL on failure.
Ryan Sleevi 2013/11/21 01:41:34 A subtle element of this is the fact that the SECI
padolph 2013/11/21 03:38:13 Done.
702 SECItem pubkey_der = {siBuffer, NULL, 0};
703 SECItem* const check_der_ptr = SEC_ASN1EncodeItem(
704 arena.get(), &pubkey_der, &pubkey_in, RsaPublicKeyTemplate);
705 if (!check_der_ptr)
706 return false;
707 DCHECK_EQ(&pubkey_der, check_der_ptr);
708
709 // Import the DER to create an RSA SECKEYPublicKey.
Ryan Sleevi 2013/11/21 01:41:34 nit: // Import the DER-encoded public key to...
padolph 2013/11/21 03:38:13 Done.
710 crypto::ScopedSECKEYPublicKey pubkey(
711 SECKEY_ImportDERPublicKey(&pubkey_der, CKK_RSA));
712 if (!pubkey.get())
713 return false;
714
715 *key = blink::WebCryptoKey::create(new PublicKeyHandle(pubkey.Pass()),
716 blink::WebCryptoKeyTypePublic,
717 extractable,
718 algorithm,
719 usage_mask);
720 return true;
721 }
722
652 } // namespace content 723 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/webcrypto/webcrypto_impl.cc ('k') | content/renderer/webcrypto/webcrypto_impl_openssl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698