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 918 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
929 | 929 |
930 break; | 930 break; |
931 } | 931 } |
932 default: | 932 default: |
933 return false; | 933 return false; |
934 } | 934 } |
935 | 935 |
936 return true; | 936 return true; |
937 } | 937 } |
938 | 938 |
| 939 bool WebCryptoImpl::ImportRsaPublicKeyInternal( |
| 940 const unsigned char* modulus_data, |
| 941 unsigned modulus_size, |
| 942 const unsigned char* exponent_data, |
| 943 unsigned exponent_size, |
| 944 const blink::WebCryptoAlgorithm& algorithm, |
| 945 bool extractable, |
| 946 blink::WebCryptoKeyUsageMask usage_mask, |
| 947 blink::WebCryptoKey* key) { |
| 948 |
| 949 if (!modulus_size || !exponent_size) |
| 950 return false; |
| 951 DCHECK(modulus_data); |
| 952 DCHECK(exponent_data); |
| 953 |
| 954 // NSS does not provide a way to create an RSA public key directly from the |
| 955 // modulus and exponent values, but it can import an DER-encoded ASN.1 blob |
| 956 // with these values and create the public key from that. The code below |
| 957 // follows the recommendation described in |
| 958 // https://developer.mozilla.org/en-US/docs/NSS/NSS_Tech_Notes/nss_tech_note7 |
| 959 |
| 960 // Pack the input values into a struct compatible with NSS ASN.1 encoding, and |
| 961 // set up an ASN.1 encoder template for it. |
| 962 struct RsaPublicKeyData { |
| 963 SECItem modulus; |
| 964 SECItem exponent; |
| 965 }; |
| 966 const RsaPublicKeyData pubkey_in = { |
| 967 {siUnsignedInteger, const_cast<unsigned char*>(modulus_data), |
| 968 modulus_size}, |
| 969 {siUnsignedInteger, const_cast<unsigned char*>(exponent_data), |
| 970 exponent_size}}; |
| 971 const SEC_ASN1Template rsa_public_key_template[] = { |
| 972 {SEC_ASN1_SEQUENCE, 0, NULL, sizeof(RsaPublicKeyData)}, |
| 973 {SEC_ASN1_INTEGER, offsetof(RsaPublicKeyData, modulus), }, |
| 974 {SEC_ASN1_INTEGER, offsetof(RsaPublicKeyData, exponent), }, |
| 975 {0, }}; |
| 976 |
| 977 // DER-encode the public key. |
| 978 crypto::ScopedSECItem pubkey_der(SEC_ASN1EncodeItem( |
| 979 NULL, NULL, &pubkey_in, rsa_public_key_template)); |
| 980 if (!pubkey_der) |
| 981 return false; |
| 982 |
| 983 // Import the DER-encoded public key to create an RSA SECKEYPublicKey. |
| 984 crypto::ScopedSECKEYPublicKey pubkey( |
| 985 SECKEY_ImportDERPublicKey(pubkey_der.get(), CKK_RSA)); |
| 986 if (!pubkey) |
| 987 return false; |
| 988 |
| 989 *key = blink::WebCryptoKey::create(new PublicKeyHandle(pubkey.Pass()), |
| 990 blink::WebCryptoKeyTypePublic, |
| 991 extractable, |
| 992 algorithm, |
| 993 usage_mask); |
| 994 return true; |
| 995 } |
| 996 |
939 } // namespace content | 997 } // namespace content |
OLD | NEW |