OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "crypto/nss_key_util.h" | 5 #include "crypto/nss_key_util.h" |
6 | 6 |
7 #include <cryptohi.h> | 7 #include <cryptohi.h> |
8 #include <keyhi.h> | 8 #include <keyhi.h> |
9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/stl_util.h" | |
13 #include "crypto/nss_util.h" | 12 #include "crypto/nss_util.h" |
14 | 13 |
15 #if defined(USE_NSS_CERTS) | 14 #if defined(USE_NSS_CERTS) |
16 #include <secmod.h> | 15 #include <secmod.h> |
17 #include "crypto/nss_util_internal.h" | 16 #include "crypto/nss_util_internal.h" |
18 #endif | 17 #endif |
19 | 18 |
20 namespace crypto { | 19 namespace crypto { |
21 | 20 |
22 namespace { | 21 namespace { |
23 | 22 |
24 #if defined(USE_NSS_CERTS) | 23 #if defined(USE_NSS_CERTS) |
25 | 24 |
26 struct PublicKeyInfoDeleter { | 25 struct PublicKeyInfoDeleter { |
27 inline void operator()(CERTSubjectPublicKeyInfo* spki) { | 26 inline void operator()(CERTSubjectPublicKeyInfo* spki) { |
28 SECKEY_DestroySubjectPublicKeyInfo(spki); | 27 SECKEY_DestroySubjectPublicKeyInfo(spki); |
29 } | 28 } |
30 }; | 29 }; |
31 | 30 |
32 typedef scoped_ptr<CERTSubjectPublicKeyInfo, PublicKeyInfoDeleter> | 31 typedef scoped_ptr<CERTSubjectPublicKeyInfo, PublicKeyInfoDeleter> |
33 ScopedPublicKeyInfo; | 32 ScopedPublicKeyInfo; |
34 | 33 |
35 // Decodes |input| as a SubjectPublicKeyInfo and returns a SECItem containing | 34 // Decodes |input| as a SubjectPublicKeyInfo and returns a SECItem containing |
36 // the CKA_ID of that public key or nullptr on error. | 35 // the CKA_ID of that public key or nullptr on error. |
37 ScopedSECItem MakeIDFromSPKI(const std::vector<uint8_t>& input) { | 36 ScopedSECItem MakeIDFromSPKI(const std::vector<uint8_t>& input) { |
38 // First, decode and save the public key. | 37 // First, decode and save the public key. |
39 SECItem key_der; | 38 SECItem key_der; |
40 key_der.type = siBuffer; | 39 key_der.type = siBuffer; |
41 key_der.data = const_cast<unsigned char*>(vector_as_array(&input)); | 40 key_der.data = const_cast<unsigned char*>(input.data()); |
42 key_der.len = input.size(); | 41 key_der.len = input.size(); |
43 | 42 |
44 ScopedPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der)); | 43 ScopedPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(&key_der)); |
45 if (!spki) | 44 if (!spki) |
46 return nullptr; | 45 return nullptr; |
47 | 46 |
48 ScopedSECKEYPublicKey result(SECKEY_ExtractPublicKey(spki.get())); | 47 ScopedSECKEYPublicKey result(SECKEY_ExtractPublicKey(spki.get())); |
49 if (!result) | 48 if (!result) |
50 return nullptr; | 49 return nullptr; |
51 | 50 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 const std::vector<uint8_t>& input, | 86 const std::vector<uint8_t>& input, |
88 bool permanent) { | 87 bool permanent) { |
89 DCHECK(slot); | 88 DCHECK(slot); |
90 | 89 |
91 ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE)); | 90 ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE)); |
92 DCHECK(arena); | 91 DCHECK(arena); |
93 | 92 |
94 // Excess data is illegal, but NSS silently accepts it, so first ensure that | 93 // Excess data is illegal, but NSS silently accepts it, so first ensure that |
95 // |input| consists of a single ASN.1 element. | 94 // |input| consists of a single ASN.1 element. |
96 SECItem input_item; | 95 SECItem input_item; |
97 input_item.data = const_cast<unsigned char*>(vector_as_array(&input)); | 96 input_item.data = const_cast<unsigned char*>(input.data()); |
98 input_item.len = input.size(); | 97 input_item.len = input.size(); |
99 SECItem der_private_key_info; | 98 SECItem der_private_key_info; |
100 SECStatus rv = | 99 SECStatus rv = |
101 SEC_QuickDERDecodeItem(arena.get(), &der_private_key_info, | 100 SEC_QuickDERDecodeItem(arena.get(), &der_private_key_info, |
102 SEC_ASN1_GET(SEC_AnyTemplate), &input_item); | 101 SEC_ASN1_GET(SEC_AnyTemplate), &input_item); |
103 if (rv != SECSuccess) | 102 if (rv != SECSuccess) |
104 return nullptr; | 103 return nullptr; |
105 | 104 |
106 // Allow the private key to be used for key unwrapping, data decryption, | 105 // Allow the private key to be used for key unwrapping, data decryption, |
107 // and signature generation. | 106 // and signature generation. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 if (!cka_id) | 153 if (!cka_id) |
155 return nullptr; | 154 return nullptr; |
156 | 155 |
157 return ScopedSECKEYPrivateKey( | 156 return ScopedSECKEYPrivateKey( |
158 PK11_FindKeyByKeyID(slot, cka_id.get(), nullptr)); | 157 PK11_FindKeyByKeyID(slot, cka_id.get(), nullptr)); |
159 } | 158 } |
160 | 159 |
161 #endif // defined(USE_NSS_CERTS) | 160 #endif // defined(USE_NSS_CERTS) |
162 | 161 |
163 } // namespace crypto | 162 } // namespace crypto |
OLD | NEW |