OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 #include "cryptohi.h" | 4 #include "cryptohi.h" |
5 #include "keyhi.h" | 5 #include "keyhi.h" |
6 #include "secoid.h" | 6 #include "secoid.h" |
7 #include "secitem.h" | 7 #include "secitem.h" |
8 #include "secder.h" | 8 #include "secder.h" |
9 #include "base64.h" | 9 #include "base64.h" |
10 #include "secasn1.h" | 10 #include "secasn1.h" |
11 #include "cert.h" | 11 #include "cert.h" |
12 #include "pk11func.h" | 12 #include "pk11func.h" |
13 #include "secerr.h" | 13 #include "secerr.h" |
14 #include "secdig.h" | 14 #include "secdig.h" |
15 #include "prtime.h" | 15 #include "prtime.h" |
16 #include "keyi.h" | 16 #include "keyi.h" |
17 | 17 |
18 SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate) | 18 SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate) |
19 SEC_ASN1_MKSUB(SEC_IntegerTemplate) | 19 SEC_ASN1_MKSUB(SEC_IntegerTemplate) |
20 | 20 |
21 const SEC_ASN1Template CERT_SubjectPublicKeyInfoTemplate[] = { | 21 const SEC_ASN1Template CERT_SubjectPublicKeyInfoTemplate[] = { |
22 { SEC_ASN1_SEQUENCE, | 22 { SEC_ASN1_SEQUENCE, |
23 » 0, NULL, sizeof(CERTSubjectPublicKeyInfo) }, | 23 0, NULL, sizeof(CERTSubjectPublicKeyInfo) }, |
24 { SEC_ASN1_INLINE | SEC_ASN1_XTRN, | 24 { SEC_ASN1_INLINE | SEC_ASN1_XTRN, |
25 » offsetof(CERTSubjectPublicKeyInfo,algorithm), | 25 offsetof(CERTSubjectPublicKeyInfo, algorithm), |
26 » SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, | 26 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, |
27 { SEC_ASN1_BIT_STRING, | 27 { SEC_ASN1_BIT_STRING, |
28 » offsetof(CERTSubjectPublicKeyInfo,subjectPublicKey), }, | 28 offsetof(CERTSubjectPublicKeyInfo, subjectPublicKey) }, |
29 { 0, } | 29 { 0 } |
30 }; | 30 }; |
31 | 31 |
32 const SEC_ASN1Template CERT_PublicKeyAndChallengeTemplate[] = | 32 const SEC_ASN1Template CERT_PublicKeyAndChallengeTemplate[] = |
33 { | 33 { |
34 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTPublicKeyAndChallenge) }, | 34 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CERTPublicKeyAndChallenge) }, |
35 { SEC_ASN1_ANY, offsetof(CERTPublicKeyAndChallenge,spki) }, | 35 { SEC_ASN1_ANY, offsetof(CERTPublicKeyAndChallenge, spki) }, |
36 { SEC_ASN1_IA5_STRING, offsetof(CERTPublicKeyAndChallenge,challenge) }, | 36 { SEC_ASN1_IA5_STRING, offsetof(CERTPublicKeyAndChallenge, challenge) }, |
| 37 { 0 } |
| 38 }; |
| 39 |
| 40 const SEC_ASN1Template SECKEY_RSAPublicKeyTemplate[] = { |
| 41 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPublicKey) }, |
| 42 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey, u.rsa.modulus) }, |
| 43 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey, u.rsa.publicExponent) }, |
37 { 0 } | 44 { 0 } |
38 }; | 45 }; |
39 | 46 |
40 const SEC_ASN1Template SECKEY_RSAPublicKeyTemplate[] = { | |
41 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPublicKey) }, | |
42 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.rsa.modulus), }, | |
43 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.rsa.publicExponent), }, | |
44 { 0, } | |
45 }; | |
46 | |
47 static const SEC_ASN1Template seckey_PointerToAlgorithmIDTemplate[] = { | 47 static const SEC_ASN1Template seckey_PointerToAlgorithmIDTemplate[] = { |
48 { SEC_ASN1_POINTER | SEC_ASN1_XTRN, 0, | 48 { SEC_ASN1_POINTER | SEC_ASN1_XTRN, 0, |
49 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) } | 49 SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) } |
50 }; | 50 }; |
51 | 51 |
52 /* Parameters for SEC_OID_PKCS1_RSA_PSS_SIGNATURE */ | 52 /* Parameters for SEC_OID_PKCS1_RSA_PSS_SIGNATURE */ |
53 const SEC_ASN1Template SECKEY_RSAPSSParamsTemplate[] = | 53 const SEC_ASN1Template SECKEY_RSAPSSParamsTemplate[] = |
54 { | 54 { |
55 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYRSAPSSParams) }, | 55 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYRSAPSSParams) }, |
56 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | | 56 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | |
57 SEC_ASN1_CONTEXT_SPECIFIC | 0, | 57 SEC_ASN1_CONTEXT_SPECIFIC | 0, |
58 offsetof(SECKEYRSAPSSParams, hashAlg), | 58 offsetof(SECKEYRSAPSSParams, hashAlg), |
59 seckey_PointerToAlgorithmIDTemplate }, | 59 seckey_PointerToAlgorithmIDTemplate }, |
60 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | | 60 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | |
61 SEC_ASN1_CONTEXT_SPECIFIC | 1, | 61 SEC_ASN1_CONTEXT_SPECIFIC | 1, |
62 offsetof(SECKEYRSAPSSParams, maskAlg), | 62 offsetof(SECKEYRSAPSSParams, maskAlg), |
63 seckey_PointerToAlgorithmIDTemplate }, | 63 seckey_PointerToAlgorithmIDTemplate }, |
64 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | | 64 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | |
65 SEC_ASN1_XTRN | SEC_ASN1_CONTEXT_SPECIFIC | 2, | 65 SEC_ASN1_XTRN | SEC_ASN1_CONTEXT_SPECIFIC | 2, |
66 offsetof(SECKEYRSAPSSParams, saltLength), | 66 offsetof(SECKEYRSAPSSParams, saltLength), |
67 SEC_ASN1_SUB(SEC_IntegerTemplate) }, | 67 SEC_ASN1_SUB(SEC_IntegerTemplate) }, |
68 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | | 68 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT | |
69 SEC_ASN1_XTRN | SEC_ASN1_CONTEXT_SPECIFIC | 3, | 69 SEC_ASN1_XTRN | SEC_ASN1_CONTEXT_SPECIFIC | 3, |
70 offsetof(SECKEYRSAPSSParams, trailerField), | 70 offsetof(SECKEYRSAPSSParams, trailerField), |
71 SEC_ASN1_SUB(SEC_IntegerTemplate) }, | 71 SEC_ASN1_SUB(SEC_IntegerTemplate) }, |
72 { 0 } | 72 { 0 } |
73 }; | 73 }; |
74 | 74 |
75 const SEC_ASN1Template SECKEY_DSAPublicKeyTemplate[] = { | 75 const SEC_ASN1Template SECKEY_DSAPublicKeyTemplate[] = { |
76 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dsa.publicValue), }, | 76 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey, u.dsa.publicValue) }, |
77 { 0, } | 77 { 0 } |
78 }; | 78 }; |
79 | 79 |
80 const SEC_ASN1Template SECKEY_PQGParamsTemplate[] = { | 80 const SEC_ASN1Template SECKEY_PQGParamsTemplate[] = { |
81 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPQGParams) }, | 81 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPQGParams) }, |
82 { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,prime) }, | 82 { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams, prime) }, |
83 { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,subPrime) }, | 83 { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams, subPrime) }, |
84 { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams,base) }, | 84 { SEC_ASN1_INTEGER, offsetof(SECKEYPQGParams, base) }, |
85 { 0, } | 85 { 0 } |
86 }; | 86 }; |
87 | 87 |
88 const SEC_ASN1Template SECKEY_DHPublicKeyTemplate[] = { | 88 const SEC_ASN1Template SECKEY_DHPublicKeyTemplate[] = { |
89 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.publicValue), }, | 89 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey, u.dh.publicValue) }, |
90 { 0, } | 90 { 0 } |
91 }; | 91 }; |
92 | 92 |
93 const SEC_ASN1Template SECKEY_DHParamKeyTemplate[] = { | 93 const SEC_ASN1Template SECKEY_DHParamKeyTemplate[] = { |
94 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPublicKey) }, | 94 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPublicKey) }, |
95 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.prime), }, | 95 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey, u.dh.prime) }, |
96 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.base), }, | 96 { SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey, u.dh.base) }, |
97 /* XXX chrisk: this needs to be expanded for decoding of j and validationPar
ms (RFC2459 7.3.2) */ | 97 /* XXX chrisk: this needs to be expanded for decoding of j and validationPar
ms (RFC2459 7.3.2) */ |
98 { SEC_ASN1_SKIP_REST }, | 98 { SEC_ASN1_SKIP_REST }, |
99 { 0, } | 99 { 0 } |
100 }; | 100 }; |
101 | 101 |
102 SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_DSAPublicKeyTemplate) | 102 SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_DSAPublicKeyTemplate) |
103 SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_RSAPublicKeyTemplate) | 103 SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_RSAPublicKeyTemplate) |
104 SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_RSAPSSParamsTemplate) | 104 SEC_ASN1_CHOOSER_IMPLEMENT(SECKEY_RSAPSSParamsTemplate) |
105 SEC_ASN1_CHOOSER_IMPLEMENT(CERT_SubjectPublicKeyInfoTemplate) | 105 SEC_ASN1_CHOOSER_IMPLEMENT(CERT_SubjectPublicKeyInfoTemplate) |
106 | 106 |
107 /* | 107 /* |
108 * See bugzilla bug 125359 | 108 * See bugzilla bug 125359 |
109 * Since NSS (via PKCS#11) wants to handle big integers as unsigned ints, | 109 * Since NSS (via PKCS#11) wants to handle big integers as unsigned ints, |
(...skipping 25 matching lines...) Expand all Loading... |
135 | 135 |
136 static void | 136 static void |
137 prepare_dh_pub_key_for_asn1(SECKEYPublicKey *pubk) | 137 prepare_dh_pub_key_for_asn1(SECKEYPublicKey *pubk) |
138 { | 138 { |
139 pubk->u.dh.prime.type = siUnsignedInteger; | 139 pubk->u.dh.prime.type = siUnsignedInteger; |
140 pubk->u.dh.base.type = siUnsignedInteger; | 140 pubk->u.dh.base.type = siUnsignedInteger; |
141 pubk->u.dh.publicValue.type = siUnsignedInteger; | 141 pubk->u.dh.publicValue.type = siUnsignedInteger; |
142 } | 142 } |
143 | 143 |
144 /* Create an RSA key pair is any slot able to do so. | 144 /* Create an RSA key pair is any slot able to do so. |
145 ** The created keys are "session" (temporary), not "token" (permanent), | 145 ** The created keys are "session" (temporary), not "token" (permanent), |
146 ** and they are "sensitive", which makes them costly to move to another token. | 146 ** and they are "sensitive", which makes them costly to move to another token. |
147 */ | 147 */ |
148 SECKEYPrivateKey * | 148 SECKEYPrivateKey * |
149 SECKEY_CreateRSAPrivateKey(int keySizeInBits,SECKEYPublicKey **pubk, void *cx) | 149 SECKEY_CreateRSAPrivateKey(int keySizeInBits, SECKEYPublicKey **pubk, void *cx) |
150 { | 150 { |
151 SECKEYPrivateKey *privk; | 151 SECKEYPrivateKey *privk; |
152 PK11RSAGenParams param; | 152 PK11RSAGenParams param; |
153 PK11SlotInfo *slot = PK11_GetBestSlot(CKM_RSA_PKCS_KEY_PAIR_GEN,cx); | 153 PK11SlotInfo *slot = PK11_GetBestSlot(CKM_RSA_PKCS_KEY_PAIR_GEN, cx); |
154 if (!slot) { | 154 if (!slot) { |
155 » return NULL; | 155 return NULL; |
156 } | 156 } |
157 | 157 |
158 param.keySizeInBits = keySizeInBits; | 158 param.keySizeInBits = keySizeInBits; |
159 param.pe = 65537L; | 159 param.pe = 65537L; |
160 | 160 |
161 privk = PK11_GenerateKeyPair(slot,CKM_RSA_PKCS_KEY_PAIR_GEN,¶m,pubk, | 161 privk = PK11_GenerateKeyPair(slot, CKM_RSA_PKCS_KEY_PAIR_GEN, ¶m, pubk, |
162 » » » » » PR_FALSE, PR_TRUE, cx); | 162 PR_FALSE, PR_TRUE, cx); |
163 PK11_FreeSlot(slot); | 163 PK11_FreeSlot(slot); |
164 return(privk); | 164 return (privk); |
165 } | 165 } |
166 | 166 |
167 /* Create a DH key pair in any slot able to do so, | 167 /* Create a DH key pair in any slot able to do so, |
168 ** This is a "session" (temporary), not "token" (permanent) key. | 168 ** This is a "session" (temporary), not "token" (permanent) key. |
169 ** Because of the high probability that this key will need to be moved to | 169 ** Because of the high probability that this key will need to be moved to |
170 ** another token, and the high cost of moving "sensitive" keys, we attempt | 170 ** another token, and the high cost of moving "sensitive" keys, we attempt |
171 ** to create this key pair without the "sensitive" attribute, but revert to | 171 ** to create this key pair without the "sensitive" attribute, but revert to |
172 ** creating a "sensitive" key if necessary. | 172 ** creating a "sensitive" key if necessary. |
173 */ | 173 */ |
174 SECKEYPrivateKey * | 174 SECKEYPrivateKey * |
175 SECKEY_CreateDHPrivateKey(SECKEYDHParams *param, SECKEYPublicKey **pubk, void *c
x) | 175 SECKEY_CreateDHPrivateKey(SECKEYDHParams *param, SECKEYPublicKey **pubk, void *c
x) |
176 { | 176 { |
177 SECKEYPrivateKey *privk; | 177 SECKEYPrivateKey *privk; |
178 PK11SlotInfo *slot; | 178 PK11SlotInfo *slot; |
179 | 179 |
180 if (!param || !param->base.data || !param->prime.data || | 180 if (!param || !param->base.data || !param->prime.data || |
181 SECKEY_BigIntegerBitLength(¶m->prime) < DH_MIN_P_BITS || | 181 SECKEY_BigIntegerBitLength(¶m->prime) < DH_MIN_P_BITS || |
182 param->base.len == 0 || param->base.len > param->prime.len + 1 || | 182 param->base.len == 0 || param->base.len > param->prime.len + 1 || |
183 » (param->base.len == 1 && param->base.data[0] == 0)) { | 183 (param->base.len == 1 && param->base.data[0] == 0)) { |
184 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | 184 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
185 » return NULL; | 185 return NULL; |
186 } | 186 } |
187 | 187 |
188 slot = PK11_GetBestSlot(CKM_DH_PKCS_KEY_PAIR_GEN,cx); | 188 slot = PK11_GetBestSlot(CKM_DH_PKCS_KEY_PAIR_GEN, cx); |
189 if (!slot) { | 189 if (!slot) { |
190 » return NULL; | 190 return NULL; |
191 } | 191 } |
192 | 192 |
193 privk = PK11_GenerateKeyPair(slot, CKM_DH_PKCS_KEY_PAIR_GEN, param, | 193 privk = PK11_GenerateKeyPair(slot, CKM_DH_PKCS_KEY_PAIR_GEN, param, |
194 pubk, PR_FALSE, PR_FALSE, cx); | 194 pubk, PR_FALSE, PR_FALSE, cx); |
195 if (!privk) | 195 if (!privk) |
196 » privk = PK11_GenerateKeyPair(slot, CKM_DH_PKCS_KEY_PAIR_GEN, param, | 196 privk = PK11_GenerateKeyPair(slot, CKM_DH_PKCS_KEY_PAIR_GEN, param, |
197 » pubk, PR_FALSE, PR_TRUE, cx); | 197 pubk, PR_FALSE, PR_TRUE, cx); |
198 | 198 |
199 PK11_FreeSlot(slot); | 199 PK11_FreeSlot(slot); |
200 return(privk); | 200 return (privk); |
201 } | 201 } |
202 | 202 |
203 /* Create an EC key pair in any slot able to do so, | 203 /* Create an EC key pair in any slot able to do so, |
204 ** This is a "session" (temporary), not "token" (permanent) key. | 204 ** This is a "session" (temporary), not "token" (permanent) key. |
205 ** Because of the high probability that this key will need to be moved to | 205 ** Because of the high probability that this key will need to be moved to |
206 ** another token, and the high cost of moving "sensitive" keys, we attempt | 206 ** another token, and the high cost of moving "sensitive" keys, we attempt |
207 ** to create this key pair without the "sensitive" attribute, but revert to | 207 ** to create this key pair without the "sensitive" attribute, but revert to |
208 ** creating a "sensitive" key if necessary. | 208 ** creating a "sensitive" key if necessary. |
209 */ | 209 */ |
210 SECKEYPrivateKey * | 210 SECKEYPrivateKey * |
211 SECKEY_CreateECPrivateKey(SECKEYECParams *param, SECKEYPublicKey **pubk, void *c
x) | 211 SECKEY_CreateECPrivateKey(SECKEYECParams *param, SECKEYPublicKey **pubk, void *c
x) |
212 { | 212 { |
213 SECKEYPrivateKey *privk; | 213 SECKEYPrivateKey *privk; |
214 PK11SlotInfo *slot = PK11_GetBestSlot(CKM_EC_KEY_PAIR_GEN,cx); | 214 PK11SlotInfo *slot = PK11_GetBestSlot(CKM_EC_KEY_PAIR_GEN, cx); |
215 if (!slot) { | 215 if (!slot) { |
216 » return NULL; | 216 return NULL; |
217 } | 217 } |
218 | 218 |
219 privk = PK11_GenerateKeyPairWithOpFlags(slot, CKM_EC_KEY_PAIR_GEN, | 219 privk = PK11_GenerateKeyPairWithOpFlags(slot, CKM_EC_KEY_PAIR_GEN, |
220 param, pubk, | 220 param, pubk, |
221 PK11_ATTR_SESSION | PK11_ATTR_INSENSITIVE | | 221 PK11_ATTR_SESSION | |
222 PK11_ATTR_PUBLIC, | 222 PK11_ATTR_INSENSITIVE | |
223 CKF_DERIVE, CKF_DERIVE|CKF_SIGN,cx); | 223 PK11_ATTR_PUBLIC, |
224 if (!privk) | 224 CKF_DERIVE, CKF_DERIVE | |
225 privk = PK11_GenerateKeyPairWithOpFlags(slot, CKM_EC_KEY_PAIR_GEN, | 225 CKF_SIGN, |
226 param, pubk, | 226 cx); |
227 PK11_ATTR_SESSION | PK11_ATTR_SENSITIVE | | 227 if (!privk) |
228 PK11_ATTR_PRIVATE, | 228 privk = PK11_GenerateKeyPairWithOpFlags(slot, CKM_EC_KEY_PAIR_GEN, |
229 CKF_DERIVE, CKF_DERIVE|CKF_SIGN,cx); | 229 param, pubk, |
| 230 PK11_ATTR_SESSION | |
| 231 PK11_ATTR_SENSITIVE | |
| 232 PK11_ATTR_PRIVATE, |
| 233 CKF_DERIVE, CKF_DERIVE | |
| 234 CKF_SIGN, |
| 235 cx); |
230 | 236 |
231 PK11_FreeSlot(slot); | 237 PK11_FreeSlot(slot); |
232 return(privk); | 238 return (privk); |
233 } | 239 } |
234 | 240 |
235 void | 241 void |
236 SECKEY_DestroyPrivateKey(SECKEYPrivateKey *privk) | 242 SECKEY_DestroyPrivateKey(SECKEYPrivateKey *privk) |
237 { | 243 { |
238 if (privk) { | 244 if (privk) { |
239 » if (privk->pkcs11Slot) { | 245 if (privk->pkcs11Slot) { |
240 » if (privk->pkcs11IsTemp) { | 246 if (privk->pkcs11IsTemp) { |
241 » » PK11_DestroyObject(privk->pkcs11Slot,privk->pkcs11ID); | 247 PK11_DestroyObject(privk->pkcs11Slot, privk->pkcs11ID); |
242 » } | 248 } |
243 » PK11_FreeSlot(privk->pkcs11Slot); | 249 PK11_FreeSlot(privk->pkcs11Slot); |
244 | 250 } |
245 » } | 251 if (privk->arena) { |
246 » if (privk->arena) { | 252 PORT_FreeArena(privk->arena, PR_TRUE); |
247 » PORT_FreeArena(privk->arena, PR_TRUE); | 253 } |
248 » } | |
249 } | 254 } |
250 } | 255 } |
251 | 256 |
252 void | 257 void |
253 SECKEY_DestroyPublicKey(SECKEYPublicKey *pubk) | 258 SECKEY_DestroyPublicKey(SECKEYPublicKey *pubk) |
254 { | 259 { |
255 if (pubk) { | 260 if (pubk) { |
256 » if (pubk->pkcs11Slot) { | 261 if (pubk->pkcs11Slot) { |
257 » if (!PK11_IsPermObject(pubk->pkcs11Slot,pubk->pkcs11ID)) { | 262 if (!PK11_IsPermObject(pubk->pkcs11Slot, pubk->pkcs11ID)) { |
258 » » PK11_DestroyObject(pubk->pkcs11Slot,pubk->pkcs11ID); | 263 PK11_DestroyObject(pubk->pkcs11Slot, pubk->pkcs11ID); |
259 » } | 264 } |
260 » PK11_FreeSlot(pubk->pkcs11Slot); | 265 PK11_FreeSlot(pubk->pkcs11Slot); |
261 » } | 266 } |
262 » if (pubk->arena) { | 267 if (pubk->arena) { |
263 » PORT_FreeArena(pubk->arena, PR_FALSE); | 268 PORT_FreeArena(pubk->arena, PR_FALSE); |
264 » } | 269 } |
265 } | 270 } |
266 } | 271 } |
267 | 272 |
268 SECStatus | 273 SECStatus |
269 SECKEY_CopySubjectPublicKeyInfo(PLArenaPool *arena, | 274 SECKEY_CopySubjectPublicKeyInfo(PLArenaPool *arena, |
270 » » » CERTSubjectPublicKeyInfo *to, | 275 CERTSubjectPublicKeyInfo *to, |
271 » » » CERTSubjectPublicKeyInfo *from) | 276 CERTSubjectPublicKeyInfo *from) |
272 { | 277 { |
273 SECStatus rv; | 278 SECStatus rv; |
274 SECItem spk; | 279 SECItem spk; |
275 | 280 |
276 rv = SECOID_CopyAlgorithmID(arena, &to->algorithm, &from->algorithm); | 281 rv = SECOID_CopyAlgorithmID(arena, &to->algorithm, &from->algorithm); |
277 if (rv == SECSuccess) { | 282 if (rv == SECSuccess) { |
278 » /* | 283 /* |
279 » * subjectPublicKey is a bit string, whose length is in bits. | 284 * subjectPublicKey is a bit string, whose length is in bits. |
280 » * Convert the length from bits to bytes for SECITEM_CopyItem. | 285 * Convert the length from bits to bytes for SECITEM_CopyItem. |
281 » */ | 286 */ |
282 » spk = from->subjectPublicKey; | 287 spk = from->subjectPublicKey; |
283 » DER_ConvertBitString(&spk); | 288 DER_ConvertBitString(&spk); |
284 » rv = SECITEM_CopyItem(arena, &to->subjectPublicKey, &spk); | 289 rv = SECITEM_CopyItem(arena, &to->subjectPublicKey, &spk); |
285 » /* Set the length back to bits. */ | 290 /* Set the length back to bits. */ |
286 » if (rv == SECSuccess) { | 291 if (rv == SECSuccess) { |
287 » to->subjectPublicKey.len = from->subjectPublicKey.len; | 292 to->subjectPublicKey.len = from->subjectPublicKey.len; |
288 » } | 293 } |
289 } | 294 } |
290 | 295 |
291 return rv; | 296 return rv; |
292 } | 297 } |
293 | 298 |
294 /* Procedure to update the pqg parameters for a cert's public key. | 299 /* Procedure to update the pqg parameters for a cert's public key. |
295 * pqg parameters only need to be updated for DSA certificates. | 300 * pqg parameters only need to be updated for DSA certificates. |
296 * The procedure uses calls to itself recursively to update a certificate | 301 * The procedure uses calls to itself recursively to update a certificate |
297 * issuer's pqg parameters. Some important rules are: | 302 * issuer's pqg parameters. Some important rules are: |
298 * - Do nothing if the cert already has PQG parameters. | 303 * - Do nothing if the cert already has PQG parameters. |
299 * - If the cert does not have PQG parameters, obtain them from the issuer. | 304 * - If the cert does not have PQG parameters, obtain them from the issuer. |
300 * - A valid cert chain cannot have a DSA cert without | 305 * - A valid cert chain cannot have a DSA cert without |
301 * pqg parameters that has a parent that is not a DSA cert. */ | 306 * pqg parameters that has a parent that is not a DSA cert. */ |
302 | 307 |
303 static SECStatus | 308 static SECStatus |
304 seckey_UpdateCertPQGChain(CERTCertificate * subjectCert, int count) | 309 seckey_UpdateCertPQGChain(CERTCertificate *subjectCert, int count) |
305 { | 310 { |
306 SECStatus rv; | 311 SECStatus rv; |
307 SECOidData *oid=NULL; | 312 SECOidData *oid = NULL; |
308 int tag; | 313 int tag; |
309 CERTSubjectPublicKeyInfo * subjectSpki=NULL; | 314 CERTSubjectPublicKeyInfo *subjectSpki = NULL; |
310 CERTSubjectPublicKeyInfo * issuerSpki=NULL; | 315 CERTSubjectPublicKeyInfo *issuerSpki = NULL; |
311 CERTCertificate *issuerCert = NULL; | 316 CERTCertificate *issuerCert = NULL; |
312 | 317 |
313 rv = SECSuccess; | 318 rv = SECSuccess; |
314 | 319 |
315 /* increment cert chain length counter*/ | 320 /* increment cert chain length counter*/ |
316 count++; | 321 count++; |
317 | 322 |
318 /* check if cert chain length exceeds the maximum length*/ | 323 /* check if cert chain length exceeds the maximum length*/ |
319 if (count > CERT_MAX_CERT_CHAIN) { | 324 if (count > CERT_MAX_CERT_CHAIN) { |
320 » return SECFailure; | 325 return SECFailure; |
321 } | 326 } |
322 | 327 |
323 oid = SECOID_FindOID(&subjectCert->subjectPublicKeyInfo.algorithm.algorithm)
; | 328 oid = SECOID_FindOID(&subjectCert->subjectPublicKeyInfo.algorithm.algorithm)
; |
324 if (oid != NULL) { | 329 if (oid != NULL) { |
325 tag = oid->offset; | 330 tag = oid->offset; |
326 | 331 |
327 /* Check if cert has a DSA or EC public key. If not, return | 332 /* Check if cert has a DSA or EC public key. If not, return |
328 * success since no PQG params need to be updated. | 333 * success since no PQG params need to be updated. |
329 » * | 334 * |
330 » * Question: do we really need to do this for EC keys. They don't have | 335 * Question: do we really need to do this for EC keys. They don't have |
331 » * PQG parameters, but they do have parameters. The question is does | 336 * PQG parameters, but they do have parameters. The question is does |
332 » * the child cert inherit thost parameters for EC from the parent, or | 337 * the child cert inherit thost parameters for EC from the parent, or |
333 » * do we always include those parameters in each cert. | 338 * do we always include those parameters in each cert. |
334 » */ | 339 */ |
335 | 340 |
336 » if ( (tag != SEC_OID_ANSIX9_DSA_SIGNATURE) && | 341 if ((tag != SEC_OID_ANSIX9_DSA_SIGNATURE) && |
337 (tag != SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST) && | 342 (tag != SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST) && |
338 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST) && | 343 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST) && |
339 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST) && | 344 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST) && |
340 (tag != SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST) && | 345 (tag != SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST) && |
341 (tag != SEC_OID_SDN702_DSA_SIGNATURE) && | 346 (tag != SEC_OID_SDN702_DSA_SIGNATURE) && |
342 (tag != SEC_OID_ANSIX962_EC_PUBLIC_KEY) ) { | 347 (tag != SEC_OID_ANSIX962_EC_PUBLIC_KEY)) { |
343 | 348 |
344 return SECSuccess; | 349 return SECSuccess; |
345 } | 350 } |
346 } else { | 351 } else { |
347 return SECFailure; /* return failure if oid is NULL */ | 352 return SECFailure; /* return failure if oid is NULL */ |
348 } | 353 } |
349 | 354 |
350 /* if cert has PQG parameters, return success */ | 355 /* if cert has PQG parameters, return success */ |
351 | 356 |
352 subjectSpki=&subjectCert->subjectPublicKeyInfo; | 357 subjectSpki = &subjectCert->subjectPublicKeyInfo; |
353 | 358 |
354 if (subjectSpki->algorithm.parameters.len != 0) { | 359 if (subjectSpki->algorithm.parameters.len != 0) { |
355 return SECSuccess; | 360 return SECSuccess; |
356 } | 361 } |
357 | 362 |
358 /* check if the cert is self-signed */ | 363 /* check if the cert is self-signed */ |
359 if (subjectCert->isRoot) { | 364 if (subjectCert->isRoot) { |
360 /* fail since cert is self-signed and has no pqg params. */ | 365 /* fail since cert is self-signed and has no pqg params. */ |
361 » return SECFailure; | 366 return SECFailure; |
362 } | 367 } |
363 | 368 |
364 /* get issuer cert */ | 369 /* get issuer cert */ |
365 issuerCert = CERT_FindCertIssuer(subjectCert, PR_Now(), certUsageAnyCA); | 370 issuerCert = CERT_FindCertIssuer(subjectCert, PR_Now(), certUsageAnyCA); |
366 if ( ! issuerCert ) { | 371 if (!issuerCert) { |
367 » return SECFailure; | 372 return SECFailure; |
368 } | 373 } |
369 | 374 |
370 /* if parent is not DSA, return failure since | 375 /* if parent is not DSA, return failure since |
371 we don't allow this case. */ | 376 we don't allow this case. */ |
372 | 377 |
373 oid = SECOID_FindOID(&issuerCert->subjectPublicKeyInfo.algorithm.algorithm); | 378 oid = SECOID_FindOID(&issuerCert->subjectPublicKeyInfo.algorithm.algorithm); |
374 if (oid != NULL) { | 379 if (oid != NULL) { |
375 tag = oid->offset; | 380 tag = oid->offset; |
376 | 381 |
377 /* Check if issuer cert has a DSA public key. If not, | 382 /* Check if issuer cert has a DSA public key. If not, |
378 * return failure. */ | 383 * return failure. */ |
379 | 384 |
380 » if ( (tag != SEC_OID_ANSIX9_DSA_SIGNATURE) && | 385 if ((tag != SEC_OID_ANSIX9_DSA_SIGNATURE) && |
381 (tag != SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST) && | 386 (tag != SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST) && |
382 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST) && | 387 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST) && |
383 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST) && | 388 (tag != SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST) && |
384 (tag != SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST) && | 389 (tag != SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST) && |
385 (tag != SEC_OID_SDN702_DSA_SIGNATURE) && | 390 (tag != SEC_OID_SDN702_DSA_SIGNATURE) && |
386 (tag != SEC_OID_ANSIX962_EC_PUBLIC_KEY) ) { | 391 (tag != SEC_OID_ANSIX962_EC_PUBLIC_KEY)) { |
387 rv = SECFailure; | 392 rv = SECFailure; |
388 goto loser; | 393 goto loser; |
389 } | 394 } |
390 } else { | 395 } else { |
391 rv = SECFailure; /* return failure if oid is NULL */ | 396 rv = SECFailure; /* return failure if oid is NULL */ |
392 goto loser; | 397 goto loser; |
393 } | 398 } |
394 | 399 |
395 | |
396 /* at this point the subject cert has no pqg parameters and the | 400 /* at this point the subject cert has no pqg parameters and the |
397 * issuer cert has a DSA public key. Update the issuer's | 401 * issuer cert has a DSA public key. Update the issuer's |
398 * pqg parameters with a recursive call to this same function. */ | 402 * pqg parameters with a recursive call to this same function. */ |
399 | 403 |
400 rv = seckey_UpdateCertPQGChain(issuerCert, count); | 404 rv = seckey_UpdateCertPQGChain(issuerCert, count); |
401 if (rv != SECSuccess) { | 405 if (rv != SECSuccess) { |
402 rv = SECFailure; | 406 rv = SECFailure; |
403 goto loser; | 407 goto loser; |
404 } | 408 } |
405 | 409 |
406 /* ensure issuer has pqg parameters */ | 410 /* ensure issuer has pqg parameters */ |
407 | 411 |
408 issuerSpki=&issuerCert->subjectPublicKeyInfo; | 412 issuerSpki = &issuerCert->subjectPublicKeyInfo; |
409 if (issuerSpki->algorithm.parameters.len == 0) { | 413 if (issuerSpki->algorithm.parameters.len == 0) { |
410 rv = SECFailure; | 414 rv = SECFailure; |
411 } | 415 } |
412 | 416 |
413 /* if update was successful and pqg params present, then copy the | 417 /* if update was successful and pqg params present, then copy the |
414 * parameters to the subject cert's key. */ | 418 * parameters to the subject cert's key. */ |
415 | 419 |
416 if (rv == SECSuccess) { | 420 if (rv == SECSuccess) { |
417 rv = SECITEM_CopyItem(subjectCert->arena, | 421 rv = SECITEM_CopyItem(subjectCert->arena, |
418 &subjectSpki->algorithm.parameters, | 422 &subjectSpki->algorithm.parameters, |
419 » » » &issuerSpki->algorithm.parameters); | 423 &issuerSpki->algorithm.parameters); |
420 } | 424 } |
421 | 425 |
422 loser: | 426 loser: |
423 if (issuerCert) { | 427 if (issuerCert) { |
424 CERT_DestroyCertificate(issuerCert); | 428 CERT_DestroyCertificate(issuerCert); |
425 } | 429 } |
426 return rv; | 430 return rv; |
427 | |
428 } | 431 } |
429 | |
430 | 432 |
431 SECStatus | 433 SECStatus |
432 SECKEY_UpdateCertPQG(CERTCertificate * subjectCert) | 434 SECKEY_UpdateCertPQG(CERTCertificate *subjectCert) |
433 { | 435 { |
434 if (!subjectCert) { | 436 if (!subjectCert) { |
435 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 437 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
436 » return SECFailure; | 438 return SECFailure; |
437 } | 439 } |
438 return seckey_UpdateCertPQGChain(subjectCert,0); | 440 return seckey_UpdateCertPQGChain(subjectCert, 0); |
439 } | 441 } |
440 | |
441 | 442 |
442 /* Decode the DSA PQG parameters. The params could be stored in two | 443 /* Decode the DSA PQG parameters. The params could be stored in two |
443 * possible formats, the old fortezza-only wrapped format or | 444 * possible formats, the old fortezza-only wrapped format or |
444 * the normal standard format. Store the decoded parameters in | 445 * the normal standard format. Store the decoded parameters in |
445 * a V3 certificate data structure. */ | 446 * a V3 certificate data structure. */ |
446 | 447 |
447 static SECStatus | 448 static SECStatus |
448 seckey_DSADecodePQG(PLArenaPool *arena, SECKEYPublicKey *pubk, | 449 seckey_DSADecodePQG(PLArenaPool *arena, SECKEYPublicKey *pubk, |
449 const SECItem *params) { | 450 const SECItem *params) |
| 451 { |
450 SECStatus rv; | 452 SECStatus rv; |
451 SECItem newparams; | 453 SECItem newparams; |
452 | 454 |
453 if (params == NULL) return SECFailure; | 455 if (params == NULL) |
454 | 456 return SECFailure; |
455 if (params->data == NULL) return SECFailure; | 457 |
| 458 if (params->data == NULL) |
| 459 return SECFailure; |
456 | 460 |
457 PORT_Assert(arena); | 461 PORT_Assert(arena); |
458 | 462 |
459 /* make a copy of the data into the arena so QuickDER output is valid */ | 463 /* make a copy of the data into the arena so QuickDER output is valid */ |
460 rv = SECITEM_CopyItem(arena, &newparams, params); | 464 rv = SECITEM_CopyItem(arena, &newparams, params); |
461 | 465 |
462 /* Check if params use the standard format. | 466 /* Check if params use the standard format. |
463 * The value 0xa1 will appear in the first byte of the parameter data | 467 * The value 0xa1 will appear in the first byte of the parameter data |
464 * if the PQG parameters are not using the standard format. This | 468 * if the PQG parameters are not using the standard format. This |
465 * code should be changed to use a better method to detect non-standard | 469 * code should be changed to use a better method to detect non-standard |
466 * parameters. */ | 470 * parameters. */ |
467 | 471 |
468 if ((newparams.data[0] != 0xa1) && | 472 if ((newparams.data[0] != 0xa1) && |
469 (newparams.data[0] != 0xa0)) { | 473 (newparams.data[0] != 0xa0)) { |
470 | 474 |
471 if (SECSuccess == rv) { | 475 if (SECSuccess == rv) { |
472 /* PQG params are in the standard format */ | 476 /* PQG params are in the standard format */ |
473 prepare_pqg_params_for_asn1(&pubk->u.dsa.params); | 477 prepare_pqg_params_for_asn1(&pubk->u.dsa.params); |
474 rv = SEC_QuickDERDecodeItem(arena, &pubk->u.dsa.params, | 478 rv = SEC_QuickDERDecodeItem(arena, &pubk->u.dsa.params, |
475 SECKEY_PQGParamsTemplate, | 479 SECKEY_PQGParamsTemplate, |
476 &newparams); | 480 &newparams); |
477 } | 481 } |
478 } else { | 482 } else { |
479 | 483 |
480 if (SECSuccess == rv) { | 484 if (SECSuccess == rv) { |
481 /* else the old fortezza-only wrapped format is used. */ | 485 /* else the old fortezza-only wrapped format is used. */ |
482 PORT_SetError(SEC_ERROR_BAD_DER); | 486 PORT_SetError(SEC_ERROR_BAD_DER); |
483 rv = SECFailure; | 487 rv = SECFailure; |
484 } | 488 } |
485 } | 489 } |
486 return rv; | 490 return rv; |
487 } | 491 } |
488 | 492 |
489 | |
490 /* Function used to make an oid tag to a key type */ | 493 /* Function used to make an oid tag to a key type */ |
491 KeyType | 494 KeyType |
492 seckey_GetKeyType (SECOidTag tag) { | 495 seckey_GetKeyType(SECOidTag tag) |
| 496 { |
493 KeyType keyType; | 497 KeyType keyType; |
494 | 498 |
495 switch (tag) { | 499 switch (tag) { |
496 case SEC_OID_X500_RSA_ENCRYPTION: | 500 case SEC_OID_X500_RSA_ENCRYPTION: |
497 case SEC_OID_PKCS1_RSA_ENCRYPTION: | 501 case SEC_OID_PKCS1_RSA_ENCRYPTION: |
498 » keyType = rsaKey; | 502 keyType = rsaKey; |
499 » break; | 503 break; |
500 case SEC_OID_PKCS1_RSA_PSS_SIGNATURE: | 504 case SEC_OID_PKCS1_RSA_PSS_SIGNATURE: |
501 » keyType = rsaPssKey; | 505 keyType = rsaPssKey; |
502 » break; | 506 break; |
503 case SEC_OID_PKCS1_RSA_OAEP_ENCRYPTION: | 507 case SEC_OID_PKCS1_RSA_OAEP_ENCRYPTION: |
504 » keyType = rsaOaepKey; | 508 keyType = rsaOaepKey; |
505 » break; | 509 break; |
506 case SEC_OID_ANSIX9_DSA_SIGNATURE: | 510 case SEC_OID_ANSIX9_DSA_SIGNATURE: |
507 » keyType = dsaKey; | 511 keyType = dsaKey; |
508 » break; | 512 break; |
509 case SEC_OID_MISSI_KEA_DSS_OLD: | 513 case SEC_OID_MISSI_KEA_DSS_OLD: |
510 case SEC_OID_MISSI_KEA_DSS: | 514 case SEC_OID_MISSI_KEA_DSS: |
511 case SEC_OID_MISSI_DSS_OLD: | 515 case SEC_OID_MISSI_DSS_OLD: |
512 case SEC_OID_MISSI_DSS: | 516 case SEC_OID_MISSI_DSS: |
513 » keyType = fortezzaKey; | 517 keyType = fortezzaKey; |
514 » break; | 518 break; |
515 case SEC_OID_MISSI_KEA: | 519 case SEC_OID_MISSI_KEA: |
516 case SEC_OID_MISSI_ALT_KEA: | 520 case SEC_OID_MISSI_ALT_KEA: |
517 » keyType = keaKey; | 521 keyType = keaKey; |
518 » break; | 522 break; |
519 case SEC_OID_X942_DIFFIE_HELMAN_KEY: | 523 case SEC_OID_X942_DIFFIE_HELMAN_KEY: |
520 » keyType = dhKey; | 524 keyType = dhKey; |
521 » break; | 525 break; |
522 case SEC_OID_ANSIX962_EC_PUBLIC_KEY: | 526 case SEC_OID_ANSIX962_EC_PUBLIC_KEY: |
523 » keyType = ecKey; | 527 keyType = ecKey; |
524 » break; | 528 break; |
525 /* accommodate applications that hand us a signature type when they | 529 /* accommodate applications that hand us a signature type when they |
526 » * should be handing us a cipher type */ | 530 * should be handing us a cipher type */ |
527 case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION: | 531 case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION: |
528 case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION: | 532 case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION: |
529 case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION: | 533 case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION: |
530 case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION: | 534 case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION: |
531 case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION: | 535 case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION: |
532 case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION: | 536 case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION: |
533 » keyType = rsaKey; | 537 keyType = rsaKey; |
534 » break; | 538 break; |
535 default: | 539 default: |
536 » keyType = nullKey; | 540 keyType = nullKey; |
537 } | 541 } |
538 return keyType; | 542 return keyType; |
539 } | 543 } |
540 | 544 |
541 /* Function used to determine what kind of cert we are dealing with. */ | 545 /* Function used to determine what kind of cert we are dealing with. */ |
542 KeyType | 546 KeyType |
543 CERT_GetCertKeyType (const CERTSubjectPublicKeyInfo *spki) | 547 CERT_GetCertKeyType(const CERTSubjectPublicKeyInfo *spki) |
544 { | 548 { |
545 return seckey_GetKeyType(SECOID_GetAlgorithmTag(&spki->algorithm)); | 549 return seckey_GetKeyType(SECOID_GetAlgorithmTag(&spki->algorithm)); |
546 } | 550 } |
547 | 551 |
548 static SECKEYPublicKey * | 552 static SECKEYPublicKey * |
549 seckey_ExtractPublicKey(const CERTSubjectPublicKeyInfo *spki) | 553 seckey_ExtractPublicKey(const CERTSubjectPublicKeyInfo *spki) |
550 { | 554 { |
551 SECKEYPublicKey *pubk; | 555 SECKEYPublicKey *pubk; |
552 SECItem os, newOs, newParms; | 556 SECItem os, newOs, newParms; |
553 SECStatus rv; | 557 SECStatus rv; |
554 PLArenaPool *arena; | 558 PLArenaPool *arena; |
555 SECOidTag tag; | 559 SECOidTag tag; |
556 | 560 |
557 arena = PORT_NewArena (DER_DEFAULT_CHUNKSIZE); | 561 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
558 if (arena == NULL) | 562 if (arena == NULL) |
559 » return NULL; | 563 return NULL; |
560 | 564 |
561 pubk = (SECKEYPublicKey *) PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); | 565 pubk = (SECKEYPublicKey *)PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); |
562 if (pubk == NULL) { | 566 if (pubk == NULL) { |
563 » PORT_FreeArena (arena, PR_FALSE); | 567 PORT_FreeArena(arena, PR_FALSE); |
564 » return NULL; | 568 return NULL; |
565 } | 569 } |
566 | 570 |
567 pubk->arena = arena; | 571 pubk->arena = arena; |
568 pubk->pkcs11Slot = 0; | 572 pubk->pkcs11Slot = 0; |
569 pubk->pkcs11ID = CK_INVALID_HANDLE; | 573 pubk->pkcs11ID = CK_INVALID_HANDLE; |
570 | 574 |
571 | |
572 /* Convert bit string length from bits to bytes */ | 575 /* Convert bit string length from bits to bytes */ |
573 os = spki->subjectPublicKey; | 576 os = spki->subjectPublicKey; |
574 DER_ConvertBitString (&os); | 577 DER_ConvertBitString(&os); |
575 | 578 |
576 tag = SECOID_GetAlgorithmTag(&spki->algorithm); | 579 tag = SECOID_GetAlgorithmTag(&spki->algorithm); |
577 | 580 |
578 /* copy the DER into the arena, since Quick DER returns data that points | 581 /* copy the DER into the arena, since Quick DER returns data that points |
579 into the DER input, which may get freed by the caller */ | 582 into the DER input, which may get freed by the caller */ |
580 rv = SECITEM_CopyItem(arena, &newOs, &os); | 583 rv = SECITEM_CopyItem(arena, &newOs, &os); |
581 if ( rv == SECSuccess ) | 584 if (rv == SECSuccess) |
582 switch ( tag ) { | 585 switch (tag) { |
583 case SEC_OID_X500_RSA_ENCRYPTION: | 586 case SEC_OID_X500_RSA_ENCRYPTION: |
584 case SEC_OID_PKCS1_RSA_ENCRYPTION: | 587 case SEC_OID_PKCS1_RSA_ENCRYPTION: |
585 » pubk->keyType = rsaKey; | 588 pubk->keyType = rsaKey; |
586 » prepare_rsa_pub_key_for_asn1(pubk); | 589 prepare_rsa_pub_key_for_asn1(pubk); |
587 rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_RSAPublicKeyTemplate, &n
ewOs); | 590 rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_RSAPublicKeyTemp
late, &newOs); |
588 » if (rv == SECSuccess) | 591 if (rv == SECSuccess) |
589 » return pubk; | 592 return pubk; |
590 » break; | 593 break; |
591 case SEC_OID_ANSIX9_DSA_SIGNATURE: | 594 case SEC_OID_ANSIX9_DSA_SIGNATURE: |
592 case SEC_OID_SDN702_DSA_SIGNATURE: | 595 case SEC_OID_SDN702_DSA_SIGNATURE: |
593 » pubk->keyType = dsaKey; | 596 pubk->keyType = dsaKey; |
594 » prepare_dsa_pub_key_for_asn1(pubk); | 597 prepare_dsa_pub_key_for_asn1(pubk); |
595 » rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DSAPublicKeyTemplate, &n
ewOs); | 598 rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DSAPublicKeyTemp
late, &newOs); |
596 » if (rv != SECSuccess) break; | 599 if (rv != SECSuccess) |
| 600 break; |
597 | 601 |
598 rv = seckey_DSADecodePQG(arena, pubk, | 602 rv = seckey_DSADecodePQG(arena, pubk, |
599 &spki->algorithm.parameters); | 603 &spki->algorithm.parameters); |
600 | 604 |
601 » if (rv == SECSuccess) return pubk; | 605 if (rv == SECSuccess) |
602 » break; | 606 return pubk; |
603 case SEC_OID_X942_DIFFIE_HELMAN_KEY: | 607 break; |
604 » pubk->keyType = dhKey; | 608 case SEC_OID_X942_DIFFIE_HELMAN_KEY: |
605 » prepare_dh_pub_key_for_asn1(pubk); | 609 pubk->keyType = dhKey; |
606 » rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DHPublicKeyTemplate, &ne
wOs); | 610 prepare_dh_pub_key_for_asn1(pubk); |
607 » if (rv != SECSuccess) break; | 611 rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DHPublicKeyTempl
ate, &newOs); |
| 612 if (rv != SECSuccess) |
| 613 break; |
608 | 614 |
609 /* copy the DER into the arena, since Quick DER returns data that points | 615 /* copy the DER into the arena, since Quick DER returns data tha
t points |
610 into the DER input, which may get freed by the caller */ | 616 into the DER input, which may get freed by the caller */ |
611 rv = SECITEM_CopyItem(arena, &newParms, &spki->algorithm.parameters); | 617 rv = SECITEM_CopyItem(arena, &newParms, &spki->algorithm.paramet
ers); |
612 if ( rv != SECSuccess ) | 618 if (rv != SECSuccess) |
613 break; | 619 break; |
614 | 620 |
615 rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DHParamKeyTemplate, | 621 rv = SEC_QuickDERDecodeItem(arena, pubk, SECKEY_DHParamKeyTempla
te, |
616 &newParms); | 622 &newParms); |
617 | 623 |
618 » if (rv == SECSuccess) return pubk; | 624 if (rv == SECSuccess) |
619 » break; | 625 return pubk; |
620 case SEC_OID_ANSIX962_EC_PUBLIC_KEY: | 626 break; |
621 » pubk->keyType = ecKey; | 627 case SEC_OID_ANSIX962_EC_PUBLIC_KEY: |
622 » pubk->u.ec.size = 0; | 628 pubk->keyType = ecKey; |
| 629 pubk->u.ec.size = 0; |
623 | 630 |
624 » /* Since PKCS#11 directly takes the DER encoding of EC params | 631 /* Since PKCS#11 directly takes the DER encoding of EC params |
625 » * and public value, we don't need any decoding here. | 632 * and public value, we don't need any decoding here. |
626 » */ | 633 */ |
627 rv = SECITEM_CopyItem(arena, &pubk->u.ec.DEREncodedParams, | 634 rv = SECITEM_CopyItem(arena, &pubk->u.ec.DEREncodedParams, |
628 » &spki->algorithm.parameters); | 635 &spki->algorithm.parameters); |
629 if ( rv != SECSuccess ) | 636 if (rv != SECSuccess) |
630 break; | 637 break; |
631 rv = SECITEM_CopyItem(arena, &pubk->u.ec.publicValue, &newOs); | 638 rv = SECITEM_CopyItem(arena, &pubk->u.ec.publicValue, &newOs); |
632 » if (rv == SECSuccess) return pubk; | 639 if (rv == SECSuccess) |
633 » break; | 640 return pubk; |
| 641 break; |
634 | 642 |
635 default: | 643 default: |
636 » PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); | 644 PORT_SetError(SEC_ERROR_UNSUPPORTED_KEYALG); |
637 » rv = SECFailure; | 645 rv = SECFailure; |
638 » break; | 646 break; |
639 } | 647 } |
640 | 648 |
641 SECKEY_DestroyPublicKey (pubk); | 649 SECKEY_DestroyPublicKey(pubk); |
642 return NULL; | 650 return NULL; |
643 } | 651 } |
644 | 652 |
645 | |
646 /* required for JSS */ | 653 /* required for JSS */ |
647 SECKEYPublicKey * | 654 SECKEYPublicKey * |
648 SECKEY_ExtractPublicKey(const CERTSubjectPublicKeyInfo *spki) | 655 SECKEY_ExtractPublicKey(const CERTSubjectPublicKeyInfo *spki) |
649 { | 656 { |
650 return seckey_ExtractPublicKey(spki); | 657 return seckey_ExtractPublicKey(spki); |
651 } | 658 } |
652 | 659 |
653 SECKEYPublicKey * | 660 SECKEYPublicKey * |
654 CERT_ExtractPublicKey(CERTCertificate *cert) | 661 CERT_ExtractPublicKey(CERTCertificate *cert) |
655 { | 662 { |
656 SECStatus rv; | 663 SECStatus rv; |
657 | 664 |
658 if (!cert) { | 665 if (!cert) { |
659 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 666 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
660 » return NULL; | 667 return NULL; |
661 } | 668 } |
662 rv = SECKEY_UpdateCertPQG(cert); | 669 rv = SECKEY_UpdateCertPQG(cert); |
663 if (rv != SECSuccess) return NULL; | 670 if (rv != SECSuccess) |
| 671 return NULL; |
664 | 672 |
665 return seckey_ExtractPublicKey(&cert->subjectPublicKeyInfo); | 673 return seckey_ExtractPublicKey(&cert->subjectPublicKeyInfo); |
666 } | 674 } |
667 | 675 |
668 int | 676 int |
669 SECKEY_ECParamsToKeySize(const SECItem *encodedParams) | 677 SECKEY_ECParamsToKeySize(const SECItem *encodedParams) |
670 { | 678 { |
671 SECOidTag tag; | 679 SECOidTag tag; |
672 SECItem oid = { siBuffer, NULL, 0}; | 680 SECItem oid = { siBuffer, NULL, 0 }; |
673 » | 681 |
674 /* The encodedParams data contains 0x06 (SEC_ASN1_OBJECT_ID), | 682 /* The encodedParams data contains 0x06 (SEC_ASN1_OBJECT_ID), |
675 * followed by the length of the curve oid and the curve oid. | 683 * followed by the length of the curve oid and the curve oid. |
676 */ | 684 */ |
677 oid.len = encodedParams->data[1]; | 685 oid.len = encodedParams->data[1]; |
678 oid.data = encodedParams->data + 2; | 686 oid.data = encodedParams->data + 2; |
679 if ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN) | 687 if ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN) |
680 » return 0; | 688 return 0; |
681 | 689 |
682 switch (tag) { | 690 switch (tag) { |
683 case SEC_OID_SECG_EC_SECP112R1: | 691 case SEC_OID_SECG_EC_SECP112R1: |
684 case SEC_OID_SECG_EC_SECP112R2: | 692 case SEC_OID_SECG_EC_SECP112R2: |
685 return 112; | 693 return 112; |
686 | 694 |
687 case SEC_OID_SECG_EC_SECT113R1: | 695 case SEC_OID_SECG_EC_SECT113R1: |
688 case SEC_OID_SECG_EC_SECT113R2: | 696 case SEC_OID_SECG_EC_SECT113R2: |
689 » return 113; | 697 return 113; |
690 | 698 |
691 case SEC_OID_SECG_EC_SECP128R1: | 699 case SEC_OID_SECG_EC_SECP128R1: |
692 case SEC_OID_SECG_EC_SECP128R2: | 700 case SEC_OID_SECG_EC_SECP128R2: |
693 » return 128; | 701 return 128; |
694 | 702 |
695 case SEC_OID_SECG_EC_SECT131R1: | 703 case SEC_OID_SECG_EC_SECT131R1: |
696 case SEC_OID_SECG_EC_SECT131R2: | 704 case SEC_OID_SECG_EC_SECT131R2: |
697 » return 131; | 705 return 131; |
698 | 706 |
699 case SEC_OID_SECG_EC_SECP160K1: | 707 case SEC_OID_SECG_EC_SECP160K1: |
700 case SEC_OID_SECG_EC_SECP160R1: | 708 case SEC_OID_SECG_EC_SECP160R1: |
701 case SEC_OID_SECG_EC_SECP160R2: | 709 case SEC_OID_SECG_EC_SECP160R2: |
702 » return 160; | 710 return 160; |
703 | 711 |
704 case SEC_OID_SECG_EC_SECT163K1: | 712 case SEC_OID_SECG_EC_SECT163K1: |
705 case SEC_OID_SECG_EC_SECT163R1: | 713 case SEC_OID_SECG_EC_SECT163R1: |
706 case SEC_OID_SECG_EC_SECT163R2: | 714 case SEC_OID_SECG_EC_SECT163R2: |
707 case SEC_OID_ANSIX962_EC_C2PNB163V1: | 715 case SEC_OID_ANSIX962_EC_C2PNB163V1: |
708 case SEC_OID_ANSIX962_EC_C2PNB163V2: | 716 case SEC_OID_ANSIX962_EC_C2PNB163V2: |
709 case SEC_OID_ANSIX962_EC_C2PNB163V3: | 717 case SEC_OID_ANSIX962_EC_C2PNB163V3: |
710 » return 163; | 718 return 163; |
711 | 719 |
712 case SEC_OID_ANSIX962_EC_C2PNB176V1: | 720 case SEC_OID_ANSIX962_EC_C2PNB176V1: |
713 » return 176; | 721 return 176; |
714 | 722 |
715 case SEC_OID_ANSIX962_EC_C2TNB191V1: | 723 case SEC_OID_ANSIX962_EC_C2TNB191V1: |
716 case SEC_OID_ANSIX962_EC_C2TNB191V2: | 724 case SEC_OID_ANSIX962_EC_C2TNB191V2: |
717 case SEC_OID_ANSIX962_EC_C2TNB191V3: | 725 case SEC_OID_ANSIX962_EC_C2TNB191V3: |
718 case SEC_OID_ANSIX962_EC_C2ONB191V4: | 726 case SEC_OID_ANSIX962_EC_C2ONB191V4: |
719 case SEC_OID_ANSIX962_EC_C2ONB191V5: | 727 case SEC_OID_ANSIX962_EC_C2ONB191V5: |
720 » return 191; | 728 return 191; |
721 | 729 |
722 case SEC_OID_SECG_EC_SECP192K1: | 730 case SEC_OID_SECG_EC_SECP192K1: |
723 case SEC_OID_ANSIX962_EC_PRIME192V1: | 731 case SEC_OID_ANSIX962_EC_PRIME192V1: |
724 case SEC_OID_ANSIX962_EC_PRIME192V2: | 732 case SEC_OID_ANSIX962_EC_PRIME192V2: |
725 case SEC_OID_ANSIX962_EC_PRIME192V3: | 733 case SEC_OID_ANSIX962_EC_PRIME192V3: |
726 » return 192; | 734 return 192; |
727 | 735 |
728 case SEC_OID_SECG_EC_SECT193R1: | 736 case SEC_OID_SECG_EC_SECT193R1: |
729 case SEC_OID_SECG_EC_SECT193R2: | 737 case SEC_OID_SECG_EC_SECT193R2: |
730 » return 193; | 738 return 193; |
731 | 739 |
732 case SEC_OID_ANSIX962_EC_C2PNB208W1: | 740 case SEC_OID_ANSIX962_EC_C2PNB208W1: |
733 » return 208; | 741 return 208; |
734 | 742 |
735 case SEC_OID_SECG_EC_SECP224K1: | 743 case SEC_OID_SECG_EC_SECP224K1: |
736 case SEC_OID_SECG_EC_SECP224R1: | 744 case SEC_OID_SECG_EC_SECP224R1: |
737 » return 224; | 745 return 224; |
738 | 746 |
739 case SEC_OID_SECG_EC_SECT233K1: | 747 case SEC_OID_SECG_EC_SECT233K1: |
740 case SEC_OID_SECG_EC_SECT233R1: | 748 case SEC_OID_SECG_EC_SECT233R1: |
741 » return 233; | 749 return 233; |
742 | 750 |
743 case SEC_OID_SECG_EC_SECT239K1: | 751 case SEC_OID_SECG_EC_SECT239K1: |
744 case SEC_OID_ANSIX962_EC_C2TNB239V1: | 752 case SEC_OID_ANSIX962_EC_C2TNB239V1: |
745 case SEC_OID_ANSIX962_EC_C2TNB239V2: | 753 case SEC_OID_ANSIX962_EC_C2TNB239V2: |
746 case SEC_OID_ANSIX962_EC_C2TNB239V3: | 754 case SEC_OID_ANSIX962_EC_C2TNB239V3: |
747 case SEC_OID_ANSIX962_EC_C2ONB239V4: | 755 case SEC_OID_ANSIX962_EC_C2ONB239V4: |
748 case SEC_OID_ANSIX962_EC_C2ONB239V5: | 756 case SEC_OID_ANSIX962_EC_C2ONB239V5: |
749 case SEC_OID_ANSIX962_EC_PRIME239V1: | 757 case SEC_OID_ANSIX962_EC_PRIME239V1: |
750 case SEC_OID_ANSIX962_EC_PRIME239V2: | 758 case SEC_OID_ANSIX962_EC_PRIME239V2: |
751 case SEC_OID_ANSIX962_EC_PRIME239V3: | 759 case SEC_OID_ANSIX962_EC_PRIME239V3: |
752 » return 239; | 760 return 239; |
753 | 761 |
754 case SEC_OID_SECG_EC_SECP256K1: | 762 case SEC_OID_SECG_EC_SECP256K1: |
755 case SEC_OID_ANSIX962_EC_PRIME256V1: | 763 case SEC_OID_ANSIX962_EC_PRIME256V1: |
756 » return 256; | 764 return 256; |
757 | 765 |
758 case SEC_OID_ANSIX962_EC_C2PNB272W1: | 766 case SEC_OID_ANSIX962_EC_C2PNB272W1: |
759 » return 272; | 767 return 272; |
760 | 768 |
761 case SEC_OID_SECG_EC_SECT283K1: | 769 case SEC_OID_SECG_EC_SECT283K1: |
762 case SEC_OID_SECG_EC_SECT283R1: | 770 case SEC_OID_SECG_EC_SECT283R1: |
763 » return 283; | 771 return 283; |
764 | 772 |
765 case SEC_OID_ANSIX962_EC_C2PNB304W1: | 773 case SEC_OID_ANSIX962_EC_C2PNB304W1: |
766 » return 304; | 774 return 304; |
767 | 775 |
768 case SEC_OID_ANSIX962_EC_C2TNB359V1: | 776 case SEC_OID_ANSIX962_EC_C2TNB359V1: |
769 » return 359; | 777 return 359; |
770 | 778 |
771 case SEC_OID_ANSIX962_EC_C2PNB368W1: | 779 case SEC_OID_ANSIX962_EC_C2PNB368W1: |
772 » return 368; | 780 return 368; |
773 | 781 |
774 case SEC_OID_SECG_EC_SECP384R1: | 782 case SEC_OID_SECG_EC_SECP384R1: |
775 » return 384; | 783 return 384; |
776 | 784 |
777 case SEC_OID_SECG_EC_SECT409K1: | 785 case SEC_OID_SECG_EC_SECT409K1: |
778 case SEC_OID_SECG_EC_SECT409R1: | 786 case SEC_OID_SECG_EC_SECT409R1: |
779 » return 409; | 787 return 409; |
780 | 788 |
781 case SEC_OID_ANSIX962_EC_C2TNB431R1: | 789 case SEC_OID_ANSIX962_EC_C2TNB431R1: |
782 » return 431; | 790 return 431; |
783 | 791 |
784 case SEC_OID_SECG_EC_SECP521R1: | 792 case SEC_OID_SECG_EC_SECP521R1: |
785 » return 521; | 793 return 521; |
786 | 794 |
787 case SEC_OID_SECG_EC_SECT571K1: | 795 case SEC_OID_SECG_EC_SECT571K1: |
788 case SEC_OID_SECG_EC_SECT571R1: | 796 case SEC_OID_SECG_EC_SECT571R1: |
789 » return 571; | 797 return 571; |
790 | 798 |
791 default: | 799 default: |
792 » PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); | 800 PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); |
793 » return 0; | 801 return 0; |
794 } | 802 } |
795 } | 803 } |
796 | 804 |
797 int | 805 int |
798 SECKEY_ECParamsToBasePointOrderLen(const SECItem *encodedParams) | 806 SECKEY_ECParamsToBasePointOrderLen(const SECItem *encodedParams) |
799 { | 807 { |
800 SECOidTag tag; | 808 SECOidTag tag; |
801 SECItem oid = { siBuffer, NULL, 0}; | 809 SECItem oid = { siBuffer, NULL, 0 }; |
802 » | 810 |
803 /* The encodedParams data contains 0x06 (SEC_ASN1_OBJECT_ID), | 811 /* The encodedParams data contains 0x06 (SEC_ASN1_OBJECT_ID), |
804 * followed by the length of the curve oid and the curve oid. | 812 * followed by the length of the curve oid and the curve oid. |
805 */ | 813 */ |
806 oid.len = encodedParams->data[1]; | 814 oid.len = encodedParams->data[1]; |
807 oid.data = encodedParams->data + 2; | 815 oid.data = encodedParams->data + 2; |
808 if ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN) | 816 if ((tag = SECOID_FindOIDTag(&oid)) == SEC_OID_UNKNOWN) |
809 » return 0; | 817 return 0; |
810 | 818 |
811 switch (tag) { | 819 switch (tag) { |
812 case SEC_OID_SECG_EC_SECP112R1: | 820 case SEC_OID_SECG_EC_SECP112R1: |
813 return 112; | 821 return 112; |
814 case SEC_OID_SECG_EC_SECP112R2: | 822 case SEC_OID_SECG_EC_SECP112R2: |
815 return 110; | 823 return 110; |
816 | 824 |
817 case SEC_OID_SECG_EC_SECT113R1: | 825 case SEC_OID_SECG_EC_SECT113R1: |
818 case SEC_OID_SECG_EC_SECT113R2: | 826 case SEC_OID_SECG_EC_SECT113R2: |
819 » return 113; | 827 return 113; |
820 | 828 |
821 case SEC_OID_SECG_EC_SECP128R1: | 829 case SEC_OID_SECG_EC_SECP128R1: |
822 » return 128; | 830 return 128; |
823 case SEC_OID_SECG_EC_SECP128R2: | 831 case SEC_OID_SECG_EC_SECP128R2: |
824 » return 126; | 832 return 126; |
825 | 833 |
826 case SEC_OID_SECG_EC_SECT131R1: | 834 case SEC_OID_SECG_EC_SECT131R1: |
827 case SEC_OID_SECG_EC_SECT131R2: | 835 case SEC_OID_SECG_EC_SECT131R2: |
828 » return 131; | 836 return 131; |
829 | 837 |
830 case SEC_OID_SECG_EC_SECP160K1: | 838 case SEC_OID_SECG_EC_SECP160K1: |
831 case SEC_OID_SECG_EC_SECP160R1: | 839 case SEC_OID_SECG_EC_SECP160R1: |
832 case SEC_OID_SECG_EC_SECP160R2: | 840 case SEC_OID_SECG_EC_SECP160R2: |
833 » return 161; | 841 return 161; |
834 | 842 |
835 case SEC_OID_SECG_EC_SECT163K1: | 843 case SEC_OID_SECG_EC_SECT163K1: |
836 » return 163; | 844 return 163; |
837 case SEC_OID_SECG_EC_SECT163R1: | 845 case SEC_OID_SECG_EC_SECT163R1: |
838 » return 162; | 846 return 162; |
839 case SEC_OID_SECG_EC_SECT163R2: | 847 case SEC_OID_SECG_EC_SECT163R2: |
840 case SEC_OID_ANSIX962_EC_C2PNB163V1: | 848 case SEC_OID_ANSIX962_EC_C2PNB163V1: |
841 » return 163; | 849 return 163; |
842 case SEC_OID_ANSIX962_EC_C2PNB163V2: | 850 case SEC_OID_ANSIX962_EC_C2PNB163V2: |
843 case SEC_OID_ANSIX962_EC_C2PNB163V3: | 851 case SEC_OID_ANSIX962_EC_C2PNB163V3: |
844 » return 162; | 852 return 162; |
845 | 853 |
846 case SEC_OID_ANSIX962_EC_C2PNB176V1: | 854 case SEC_OID_ANSIX962_EC_C2PNB176V1: |
847 » return 161; | 855 return 161; |
848 | 856 |
849 case SEC_OID_ANSIX962_EC_C2TNB191V1: | 857 case SEC_OID_ANSIX962_EC_C2TNB191V1: |
850 » return 191; | 858 return 191; |
851 case SEC_OID_ANSIX962_EC_C2TNB191V2: | 859 case SEC_OID_ANSIX962_EC_C2TNB191V2: |
852 » return 190; | 860 return 190; |
853 case SEC_OID_ANSIX962_EC_C2TNB191V3: | 861 case SEC_OID_ANSIX962_EC_C2TNB191V3: |
854 » return 189; | 862 return 189; |
855 case SEC_OID_ANSIX962_EC_C2ONB191V4: | 863 case SEC_OID_ANSIX962_EC_C2ONB191V4: |
856 » return 191; | 864 return 191; |
857 case SEC_OID_ANSIX962_EC_C2ONB191V5: | 865 case SEC_OID_ANSIX962_EC_C2ONB191V5: |
858 » return 188; | 866 return 188; |
859 | 867 |
860 case SEC_OID_SECG_EC_SECP192K1: | 868 case SEC_OID_SECG_EC_SECP192K1: |
861 case SEC_OID_ANSIX962_EC_PRIME192V1: | 869 case SEC_OID_ANSIX962_EC_PRIME192V1: |
862 case SEC_OID_ANSIX962_EC_PRIME192V2: | 870 case SEC_OID_ANSIX962_EC_PRIME192V2: |
863 case SEC_OID_ANSIX962_EC_PRIME192V3: | 871 case SEC_OID_ANSIX962_EC_PRIME192V3: |
864 » return 192; | 872 return 192; |
865 | 873 |
866 case SEC_OID_SECG_EC_SECT193R1: | 874 case SEC_OID_SECG_EC_SECT193R1: |
867 case SEC_OID_SECG_EC_SECT193R2: | 875 case SEC_OID_SECG_EC_SECT193R2: |
868 » return 193; | 876 return 193; |
869 | 877 |
870 case SEC_OID_ANSIX962_EC_C2PNB208W1: | 878 case SEC_OID_ANSIX962_EC_C2PNB208W1: |
871 » return 193; | 879 return 193; |
872 | 880 |
873 case SEC_OID_SECG_EC_SECP224K1: | 881 case SEC_OID_SECG_EC_SECP224K1: |
874 » return 225; | 882 return 225; |
875 case SEC_OID_SECG_EC_SECP224R1: | 883 case SEC_OID_SECG_EC_SECP224R1: |
876 » return 224; | 884 return 224; |
877 | 885 |
878 case SEC_OID_SECG_EC_SECT233K1: | 886 case SEC_OID_SECG_EC_SECT233K1: |
879 » return 232; | 887 return 232; |
880 case SEC_OID_SECG_EC_SECT233R1: | 888 case SEC_OID_SECG_EC_SECT233R1: |
881 » return 233; | 889 return 233; |
882 | 890 |
883 case SEC_OID_SECG_EC_SECT239K1: | 891 case SEC_OID_SECG_EC_SECT239K1: |
884 case SEC_OID_ANSIX962_EC_C2TNB239V1: | 892 case SEC_OID_ANSIX962_EC_C2TNB239V1: |
885 » return 238; | 893 return 238; |
886 case SEC_OID_ANSIX962_EC_C2TNB239V2: | 894 case SEC_OID_ANSIX962_EC_C2TNB239V2: |
887 » return 237; | 895 return 237; |
888 case SEC_OID_ANSIX962_EC_C2TNB239V3: | 896 case SEC_OID_ANSIX962_EC_C2TNB239V3: |
889 » return 236; | 897 return 236; |
890 case SEC_OID_ANSIX962_EC_C2ONB239V4: | 898 case SEC_OID_ANSIX962_EC_C2ONB239V4: |
891 » return 238; | 899 return 238; |
892 case SEC_OID_ANSIX962_EC_C2ONB239V5: | 900 case SEC_OID_ANSIX962_EC_C2ONB239V5: |
893 » return 237; | 901 return 237; |
894 case SEC_OID_ANSIX962_EC_PRIME239V1: | 902 case SEC_OID_ANSIX962_EC_PRIME239V1: |
895 case SEC_OID_ANSIX962_EC_PRIME239V2: | 903 case SEC_OID_ANSIX962_EC_PRIME239V2: |
896 case SEC_OID_ANSIX962_EC_PRIME239V3: | 904 case SEC_OID_ANSIX962_EC_PRIME239V3: |
897 » return 239; | 905 return 239; |
898 | 906 |
899 case SEC_OID_SECG_EC_SECP256K1: | 907 case SEC_OID_SECG_EC_SECP256K1: |
900 case SEC_OID_ANSIX962_EC_PRIME256V1: | 908 case SEC_OID_ANSIX962_EC_PRIME256V1: |
901 » return 256; | 909 return 256; |
902 | 910 |
903 case SEC_OID_ANSIX962_EC_C2PNB272W1: | 911 case SEC_OID_ANSIX962_EC_C2PNB272W1: |
904 » return 257; | 912 return 257; |
905 | 913 |
906 case SEC_OID_SECG_EC_SECT283K1: | 914 case SEC_OID_SECG_EC_SECT283K1: |
907 » return 281; | 915 return 281; |
908 case SEC_OID_SECG_EC_SECT283R1: | 916 case SEC_OID_SECG_EC_SECT283R1: |
909 » return 282; | 917 return 282; |
910 | 918 |
911 case SEC_OID_ANSIX962_EC_C2PNB304W1: | 919 case SEC_OID_ANSIX962_EC_C2PNB304W1: |
912 » return 289; | 920 return 289; |
913 | 921 |
914 case SEC_OID_ANSIX962_EC_C2TNB359V1: | 922 case SEC_OID_ANSIX962_EC_C2TNB359V1: |
915 » return 353; | 923 return 353; |
916 | 924 |
917 case SEC_OID_ANSIX962_EC_C2PNB368W1: | 925 case SEC_OID_ANSIX962_EC_C2PNB368W1: |
918 » return 353; | 926 return 353; |
919 | 927 |
920 case SEC_OID_SECG_EC_SECP384R1: | 928 case SEC_OID_SECG_EC_SECP384R1: |
921 » return 384; | 929 return 384; |
922 | 930 |
923 case SEC_OID_SECG_EC_SECT409K1: | 931 case SEC_OID_SECG_EC_SECT409K1: |
924 » return 407; | 932 return 407; |
925 case SEC_OID_SECG_EC_SECT409R1: | 933 case SEC_OID_SECG_EC_SECT409R1: |
926 » return 409; | 934 return 409; |
927 | 935 |
928 case SEC_OID_ANSIX962_EC_C2TNB431R1: | 936 case SEC_OID_ANSIX962_EC_C2TNB431R1: |
929 » return 418; | 937 return 418; |
930 | 938 |
931 case SEC_OID_SECG_EC_SECP521R1: | 939 case SEC_OID_SECG_EC_SECP521R1: |
932 » return 521; | 940 return 521; |
933 | 941 |
934 case SEC_OID_SECG_EC_SECT571K1: | 942 case SEC_OID_SECG_EC_SECT571K1: |
935 case SEC_OID_SECG_EC_SECT571R1: | 943 case SEC_OID_SECG_EC_SECT571R1: |
936 » return 570; | 944 return 570; |
937 | 945 |
938 default: | 946 default: |
939 » PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); | 947 PORT_SetError(SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE); |
940 » return 0; | 948 return 0; |
941 } | 949 } |
942 } | 950 } |
943 | 951 |
944 /* The number of bits in the number from the first non-zero bit onward. */ | 952 /* The number of bits in the number from the first non-zero bit onward. */ |
945 unsigned | 953 unsigned |
946 SECKEY_BigIntegerBitLength(const SECItem *number) | 954 SECKEY_BigIntegerBitLength(const SECItem *number) |
947 { | 955 { |
948 const unsigned char *p; | 956 const unsigned char *p; |
949 unsigned octets; | 957 unsigned octets; |
950 unsigned bits; | 958 unsigned bits; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 { | 995 { |
988 unsigned bitSize = 0; | 996 unsigned bitSize = 0; |
989 | 997 |
990 if (!pubk) { | 998 if (!pubk) { |
991 PORT_SetError(SEC_ERROR_INVALID_KEY); | 999 PORT_SetError(SEC_ERROR_INVALID_KEY); |
992 return 0; | 1000 return 0; |
993 } | 1001 } |
994 | 1002 |
995 /* interpret modulus length as key strength */ | 1003 /* interpret modulus length as key strength */ |
996 switch (pubk->keyType) { | 1004 switch (pubk->keyType) { |
997 case rsaKey: | 1005 case rsaKey: |
998 bitSize = SECKEY_BigIntegerBitLength(&pubk->u.rsa.modulus); | 1006 bitSize = SECKEY_BigIntegerBitLength(&pubk->u.rsa.modulus); |
999 break; | 1007 break; |
1000 case dsaKey: | 1008 case dsaKey: |
1001 bitSize = SECKEY_BigIntegerBitLength(&pubk->u.dsa.publicValue); | 1009 bitSize = SECKEY_BigIntegerBitLength(&pubk->u.dsa.params.prime); |
1002 break; | 1010 break; |
1003 case dhKey: | 1011 case dhKey: |
1004 bitSize = SECKEY_BigIntegerBitLength(&pubk->u.dh.publicValue); | 1012 bitSize = SECKEY_BigIntegerBitLength(&pubk->u.dh.prime); |
1005 break; | 1013 break; |
1006 case ecKey: | 1014 case ecKey: |
1007 bitSize = SECKEY_ECParamsToKeySize(&pubk->u.ec.DEREncodedParams); | 1015 bitSize = SECKEY_ECParamsToKeySize(&pubk->u.ec.DEREncodedParams); |
1008 break; | 1016 break; |
1009 default: | 1017 default: |
1010 PORT_SetError(SEC_ERROR_INVALID_KEY); | 1018 PORT_SetError(SEC_ERROR_INVALID_KEY); |
1011 break; | 1019 break; |
1012 } | 1020 } |
1013 return bitSize; | 1021 return bitSize; |
1014 } | 1022 } |
1015 | 1023 |
1016 /* returns signature length in bytes (not bits) */ | 1024 /* returns signature length in bytes (not bits) */ |
1017 unsigned | 1025 unsigned |
1018 SECKEY_SignatureLen(const SECKEYPublicKey *pubk) | 1026 SECKEY_SignatureLen(const SECKEYPublicKey *pubk) |
1019 { | 1027 { |
1020 unsigned char b0; | 1028 unsigned char b0; |
1021 unsigned size; | 1029 unsigned size; |
1022 | 1030 |
1023 switch (pubk->keyType) { | 1031 switch (pubk->keyType) { |
1024 case rsaKey: | 1032 case rsaKey: |
1025 » b0 = pubk->u.rsa.modulus.data[0]; | 1033 b0 = pubk->u.rsa.modulus.data[0]; |
1026 » return b0 ? pubk->u.rsa.modulus.len : pubk->u.rsa.modulus.len - 1; | 1034 return b0 ? pubk->u.rsa.modulus.len : pubk->u.rsa.modulus.len - 1; |
1027 case dsaKey: | 1035 case dsaKey: |
1028 » return pubk->u.dsa.params.subPrime.len * 2; | 1036 return pubk->u.dsa.params.subPrime.len * 2; |
1029 case ecKey: | 1037 case ecKey: |
1030 » /* Get the base point order length in bits and adjust */ | 1038 /* Get the base point order length in bits and adjust */ |
1031 » size =» SECKEY_ECParamsToBasePointOrderLen( | 1039 size = SECKEY_ECParamsToBasePointOrderLen( |
1032 » » &pubk->u.ec.DEREncodedParams); | 1040 &pubk->u.ec.DEREncodedParams); |
1033 » return ((size + 7)/8) * 2; | 1041 return ((size + 7) / 8) * 2; |
1034 default: | 1042 default: |
1035 » break; | 1043 break; |
1036 } | 1044 } |
1037 PORT_SetError(SEC_ERROR_INVALID_KEY); | 1045 PORT_SetError(SEC_ERROR_INVALID_KEY); |
1038 return 0; | 1046 return 0; |
1039 } | 1047 } |
1040 | 1048 |
1041 SECKEYPrivateKey * | 1049 SECKEYPrivateKey * |
1042 SECKEY_CopyPrivateKey(const SECKEYPrivateKey *privk) | 1050 SECKEY_CopyPrivateKey(const SECKEYPrivateKey *privk) |
1043 { | 1051 { |
1044 SECKEYPrivateKey *copyk; | 1052 SECKEYPrivateKey *copyk; |
1045 PLArenaPool *arena; | 1053 PLArenaPool *arena; |
1046 | 1054 |
1047 if (!privk || !privk->pkcs11Slot) { | 1055 if (!privk || !privk->pkcs11Slot) { |
1048 » PORT_SetError(SEC_ERROR_INVALID_ARGS); | 1056 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1049 » return NULL; | 1057 return NULL; |
1050 } | 1058 } |
1051 | 1059 |
1052 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1060 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1053 if (arena == NULL) { | 1061 if (arena == NULL) { |
1054 » return NULL; | 1062 return NULL; |
1055 } | 1063 } |
1056 | 1064 |
1057 copyk = (SECKEYPrivateKey *) PORT_ArenaZAlloc (arena, sizeof (SECKEYPrivateK
ey)); | 1065 copyk = (SECKEYPrivateKey *)PORT_ArenaZAlloc(arena, sizeof(SECKEYPrivateKey)
); |
1058 if (copyk) { | 1066 if (copyk) { |
1059 » copyk->arena = arena; | 1067 copyk->arena = arena; |
1060 » copyk->keyType = privk->keyType; | 1068 copyk->keyType = privk->keyType; |
1061 | 1069 |
1062 » /* copy the PKCS #11 parameters */ | 1070 /* copy the PKCS #11 parameters */ |
1063 » copyk->pkcs11Slot = PK11_ReferenceSlot(privk->pkcs11Slot); | 1071 copyk->pkcs11Slot = PK11_ReferenceSlot(privk->pkcs11Slot); |
1064 » /* if the key we're referencing was a temparary key we have just | 1072 /* if the key we're referencing was a temparary key we have just |
1065 » * created, that we want to go away when we're through, we need | 1073 * created, that we want to go away when we're through, we need |
1066 » * to make a copy of it */ | 1074 * to make a copy of it */ |
1067 » if (privk->pkcs11IsTemp) { | 1075 if (privk->pkcs11IsTemp) { |
1068 » copyk->pkcs11ID = | 1076 copyk->pkcs11ID = |
1069 » » » PK11_CopyKey(privk->pkcs11Slot,privk->pkcs11ID); | 1077 PK11_CopyKey(privk->pkcs11Slot, privk->pkcs11ID); |
1070 » if (copyk->pkcs11ID == CK_INVALID_HANDLE) goto fail; | 1078 if (copyk->pkcs11ID == CK_INVALID_HANDLE) |
1071 » } else { | 1079 goto fail; |
1072 » copyk->pkcs11ID = privk->pkcs11ID; | 1080 } else { |
1073 » } | 1081 copyk->pkcs11ID = privk->pkcs11ID; |
1074 » copyk->pkcs11IsTemp = privk->pkcs11IsTemp; | 1082 } |
1075 » copyk->wincx = privk->wincx; | 1083 copyk->pkcs11IsTemp = privk->pkcs11IsTemp; |
1076 » copyk->staticflags = privk->staticflags; | 1084 copyk->wincx = privk->wincx; |
1077 » return copyk; | 1085 copyk->staticflags = privk->staticflags; |
| 1086 return copyk; |
1078 } else { | 1087 } else { |
1079 » PORT_SetError (SEC_ERROR_NO_MEMORY); | 1088 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1080 } | 1089 } |
1081 | 1090 |
1082 fail: | 1091 fail: |
1083 PORT_FreeArena (arena, PR_FALSE); | 1092 PORT_FreeArena(arena, PR_FALSE); |
1084 return NULL; | 1093 return NULL; |
1085 } | 1094 } |
1086 | 1095 |
1087 SECKEYPublicKey * | 1096 SECKEYPublicKey * |
1088 SECKEY_CopyPublicKey(const SECKEYPublicKey *pubk) | 1097 SECKEY_CopyPublicKey(const SECKEYPublicKey *pubk) |
1089 { | 1098 { |
1090 SECKEYPublicKey *copyk; | 1099 SECKEYPublicKey *copyk; |
1091 PLArenaPool *arena; | 1100 PLArenaPool *arena; |
1092 SECStatus rv = SECSuccess; | 1101 SECStatus rv = SECSuccess; |
1093 | 1102 |
1094 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1103 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1095 if (arena == NULL) { | 1104 if (arena == NULL) { |
1096 » PORT_SetError (SEC_ERROR_NO_MEMORY); | 1105 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1097 » return NULL; | 1106 return NULL; |
1098 } | 1107 } |
1099 | 1108 |
1100 copyk = (SECKEYPublicKey *) PORT_ArenaZAlloc (arena, sizeof (SECKEYPublicKey
)); | 1109 copyk = (SECKEYPublicKey *)PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); |
1101 if (!copyk) { | 1110 if (!copyk) { |
1102 PORT_FreeArena (arena, PR_FALSE); | 1111 PORT_FreeArena(arena, PR_FALSE); |
1103 PORT_SetError (SEC_ERROR_NO_MEMORY); | 1112 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1104 return NULL; | 1113 return NULL; |
1105 } | 1114 } |
1106 | 1115 |
1107 copyk->arena = arena; | 1116 copyk->arena = arena; |
1108 copyk->keyType = pubk->keyType; | 1117 copyk->keyType = pubk->keyType; |
1109 if (pubk->pkcs11Slot && | 1118 if (pubk->pkcs11Slot && |
1110 PK11_IsPermObject(pubk->pkcs11Slot,pubk->pkcs11ID)) { | 1119 PK11_IsPermObject(pubk->pkcs11Slot, pubk->pkcs11ID)) { |
1111 copyk->pkcs11Slot = PK11_ReferenceSlot(pubk->pkcs11Slot); | 1120 copyk->pkcs11Slot = PK11_ReferenceSlot(pubk->pkcs11Slot); |
1112 copyk->pkcs11ID = pubk->pkcs11ID; | 1121 copyk->pkcs11ID = pubk->pkcs11ID; |
1113 } else { | 1122 } else { |
1114 copyk->pkcs11Slot = NULL;» /* go get own reference */ | 1123 copyk->pkcs11Slot = NULL; /* go get own reference */ |
1115 copyk->pkcs11ID = CK_INVALID_HANDLE; | 1124 copyk->pkcs11ID = CK_INVALID_HANDLE; |
1116 } | 1125 } |
1117 switch (pubk->keyType) { | 1126 switch (pubk->keyType) { |
1118 case rsaKey: | 1127 case rsaKey: |
1119 rv = SECITEM_CopyItem(arena, ©k->u.rsa.modulus, | 1128 rv = SECITEM_CopyItem(arena, ©k->u.rsa.modulus, |
1120 &pubk->u.rsa.modulus); | 1129 &pubk->u.rsa.modulus); |
1121 if (rv == SECSuccess) { | 1130 if (rv == SECSuccess) { |
1122 rv = SECITEM_CopyItem (arena, ©k->u.rsa.publicExponent, | 1131 rv = SECITEM_CopyItem(arena, ©k->u.rsa.publicExponent, |
1123 &pubk->u.rsa.publicExponent); | 1132 &pubk->u.rsa.publicExponent); |
1124 if (rv == SECSuccess) | 1133 if (rv == SECSuccess) |
1125 return copyk; | 1134 return copyk; |
1126 } | 1135 } |
1127 break; | 1136 break; |
1128 case dsaKey: | 1137 case dsaKey: |
1129 rv = SECITEM_CopyItem(arena, ©k->u.dsa.publicValue, | 1138 rv = SECITEM_CopyItem(arena, ©k->u.dsa.publicValue, |
1130 &pubk->u.dsa.publicValue); | 1139 &pubk->u.dsa.publicValue); |
1131 if (rv != SECSuccess) break; | 1140 if (rv != SECSuccess) |
1132 rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.prime, | 1141 break; |
1133 &pubk->u.dsa.params.prime); | 1142 rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.prime, |
1134 if (rv != SECSuccess) break; | 1143 &pubk->u.dsa.params.prime); |
1135 rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.subPrime, | 1144 if (rv != SECSuccess) |
1136 &pubk->u.dsa.params.subPrime); | 1145 break; |
1137 if (rv != SECSuccess) break; | 1146 rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.subPrime, |
1138 rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.base, | 1147 &pubk->u.dsa.params.subPrime); |
1139 &pubk->u.dsa.params.base); | 1148 if (rv != SECSuccess) |
1140 break; | 1149 break; |
1141 case dhKey: | 1150 rv = SECITEM_CopyItem(arena, ©k->u.dsa.params.base, |
1142 rv = SECITEM_CopyItem(arena,©k->u.dh.prime,&pubk->u.dh.prime); | 1151 &pubk->u.dsa.params.base); |
1143 if (rv != SECSuccess) break; | 1152 break; |
1144 rv = SECITEM_CopyItem(arena,©k->u.dh.base,&pubk->u.dh.base); | 1153 case dhKey: |
1145 if (rv != SECSuccess) break; | 1154 rv = SECITEM_CopyItem(arena, ©k->u.dh.prime, &pubk->u.dh.prime); |
1146 rv = SECITEM_CopyItem(arena, ©k->u.dh.publicValue, | 1155 if (rv != SECSuccess) |
1147 &pubk->u.dh.publicValue); | 1156 break; |
1148 break; | 1157 rv = SECITEM_CopyItem(arena, ©k->u.dh.base, &pubk->u.dh.base); |
1149 case ecKey: | 1158 if (rv != SECSuccess) |
1150 copyk->u.ec.size = pubk->u.ec.size; | 1159 break; |
1151 rv = SECITEM_CopyItem(arena,©k->u.ec.DEREncodedParams, | 1160 rv = SECITEM_CopyItem(arena, ©k->u.dh.publicValue, |
1152 &pubk->u.ec.DEREncodedParams); | 1161 &pubk->u.dh.publicValue); |
1153 if (rv != SECSuccess) break; | 1162 break; |
1154 rv = SECITEM_CopyItem(arena,©k->u.ec.publicValue, | 1163 case ecKey: |
1155 &pubk->u.ec.publicValue); | 1164 copyk->u.ec.size = pubk->u.ec.size; |
1156 break; | 1165 rv = SECITEM_CopyItem(arena, ©k->u.ec.DEREncodedParams, |
1157 case nullKey: | 1166 &pubk->u.ec.DEREncodedParams); |
1158 return copyk; | 1167 if (rv != SECSuccess) |
1159 default: | 1168 break; |
1160 PORT_SetError(SEC_ERROR_INVALID_KEY); | 1169 rv = SECITEM_CopyItem(arena, ©k->u.ec.publicValue, |
1161 rv = SECFailure; | 1170 &pubk->u.ec.publicValue); |
1162 break; | 1171 break; |
| 1172 case nullKey: |
| 1173 return copyk; |
| 1174 default: |
| 1175 PORT_SetError(SEC_ERROR_INVALID_KEY); |
| 1176 rv = SECFailure; |
| 1177 break; |
1163 } | 1178 } |
1164 if (rv == SECSuccess) | 1179 if (rv == SECSuccess) |
1165 return copyk; | 1180 return copyk; |
1166 | 1181 |
1167 SECKEY_DestroyPublicKey (copyk); | 1182 SECKEY_DestroyPublicKey(copyk); |
1168 return NULL; | 1183 return NULL; |
1169 } | 1184 } |
1170 | 1185 |
1171 | |
1172 SECKEYPublicKey * | 1186 SECKEYPublicKey * |
1173 SECKEY_ConvertToPublicKey(SECKEYPrivateKey *privk) | 1187 SECKEY_ConvertToPublicKey(SECKEYPrivateKey *privk) |
1174 { | 1188 { |
1175 SECKEYPublicKey *pubk; | 1189 SECKEYPublicKey *pubk; |
1176 PLArenaPool *arena; | 1190 PLArenaPool *arena; |
1177 CERTCertificate *cert; | 1191 CERTCertificate *cert; |
1178 SECStatus rv; | 1192 SECStatus rv; |
1179 | 1193 |
1180 /* | 1194 /* |
1181 * First try to look up the cert. | 1195 * First try to look up the cert. |
1182 */ | 1196 */ |
1183 cert = PK11_GetCertFromPrivateKey(privk); | 1197 cert = PK11_GetCertFromPrivateKey(privk); |
1184 if (cert) { | 1198 if (cert) { |
1185 » pubk = CERT_ExtractPublicKey(cert); | 1199 pubk = CERT_ExtractPublicKey(cert); |
1186 » CERT_DestroyCertificate(cert); | 1200 CERT_DestroyCertificate(cert); |
1187 » return pubk; | 1201 return pubk; |
1188 } | 1202 } |
1189 | 1203 |
1190 /* couldn't find the cert, build pub key by hand */ | 1204 /* couldn't find the cert, build pub key by hand */ |
1191 arena = PORT_NewArena (DER_DEFAULT_CHUNKSIZE); | 1205 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1192 if (arena == NULL) { | 1206 if (arena == NULL) { |
1193 » PORT_SetError (SEC_ERROR_NO_MEMORY); | 1207 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1194 » return NULL; | 1208 return NULL; |
1195 } | 1209 } |
1196 pubk = (SECKEYPublicKey *)PORT_ArenaZAlloc(arena, | 1210 pubk = (SECKEYPublicKey *)PORT_ArenaZAlloc(arena, |
1197 » » » » » » sizeof (SECKEYPublicKey)); | 1211 sizeof(SECKEYPublicKey)); |
1198 if (pubk == NULL) { | 1212 if (pubk == NULL) { |
1199 » PORT_FreeArena(arena,PR_FALSE); | 1213 PORT_FreeArena(arena, PR_FALSE); |
1200 » return NULL; | 1214 return NULL; |
1201 } | 1215 } |
1202 pubk->keyType = privk->keyType; | 1216 pubk->keyType = privk->keyType; |
1203 pubk->pkcs11Slot = NULL; | 1217 pubk->pkcs11Slot = NULL; |
1204 pubk->pkcs11ID = CK_INVALID_HANDLE; | 1218 pubk->pkcs11ID = CK_INVALID_HANDLE; |
1205 pubk->arena = arena; | 1219 pubk->arena = arena; |
1206 | 1220 |
1207 switch(privk->keyType) { | 1221 switch (privk->keyType) { |
1208 case nullKey: | 1222 case nullKey: |
1209 case dhKey: | 1223 case dhKey: |
1210 case dsaKey: | 1224 case dsaKey: |
1211 » /* Nothing to query, if the cert isn't there, we're done -- no way | 1225 /* Nothing to query, if the cert isn't there, we're done -- no way |
1212 » * to get the public key */ | 1226 * to get the public key */ |
1213 » break; | 1227 break; |
1214 case rsaKey: | 1228 case rsaKey: |
1215 » rv = PK11_ReadAttribute(privk->pkcs11Slot,privk->pkcs11ID, | 1229 rv = PK11_ReadAttribute(privk->pkcs11Slot, privk->pkcs11ID, |
1216 » » » » CKA_MODULUS,arena,&pubk->u.rsa.modulus); | 1230 CKA_MODULUS, arena, &pubk->u.rsa.modulus); |
1217 » if (rv != SECSuccess) break; | 1231 if (rv != SECSuccess) |
1218 » rv = PK11_ReadAttribute(privk->pkcs11Slot,privk->pkcs11ID, | 1232 break; |
1219 » » » CKA_PUBLIC_EXPONENT,arena,&pubk->u.rsa.publicExponent); | 1233 rv = PK11_ReadAttribute(privk->pkcs11Slot, privk->pkcs11ID, |
1220 » if (rv != SECSuccess) break; | 1234 CKA_PUBLIC_EXPONENT, arena, &pubk->u.rsa.pub
licExponent); |
1221 » return pubk; | 1235 if (rv != SECSuccess) |
1222 » break; | 1236 break; |
1223 default: | 1237 return pubk; |
1224 » break; | 1238 break; |
| 1239 default: |
| 1240 break; |
1225 } | 1241 } |
1226 | 1242 |
1227 PORT_FreeArena (arena, PR_FALSE); | 1243 PORT_FreeArena(arena, PR_FALSE); |
1228 return NULL; | 1244 return NULL; |
1229 } | 1245 } |
1230 | 1246 |
1231 static CERTSubjectPublicKeyInfo * | 1247 static CERTSubjectPublicKeyInfo * |
1232 seckey_CreateSubjectPublicKeyInfo_helper(SECKEYPublicKey *pubk) | 1248 seckey_CreateSubjectPublicKeyInfo_helper(SECKEYPublicKey *pubk) |
1233 { | 1249 { |
1234 CERTSubjectPublicKeyInfo *spki; | 1250 CERTSubjectPublicKeyInfo *spki; |
1235 PLArenaPool *arena; | 1251 PLArenaPool *arena; |
1236 SECItem params = { siBuffer, NULL, 0 }; | 1252 SECItem params = { siBuffer, NULL, 0 }; |
1237 | 1253 |
1238 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1254 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1239 if (arena == NULL) { | 1255 if (arena == NULL) { |
1240 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 1256 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1241 » return NULL; | 1257 return NULL; |
1242 } | 1258 } |
1243 | 1259 |
1244 spki = (CERTSubjectPublicKeyInfo *) PORT_ArenaZAlloc(arena, sizeof (*spki)); | 1260 spki = (CERTSubjectPublicKeyInfo *)PORT_ArenaZAlloc(arena, sizeof(*spki)); |
1245 if (spki != NULL) { | 1261 if (spki != NULL) { |
1246 » SECStatus rv; | 1262 SECStatus rv; |
1247 » SECItem *rv_item; | 1263 SECItem *rv_item; |
1248 » | |
1249 » spki->arena = arena; | |
1250 » switch(pubk->keyType) { | |
1251 » case rsaKey: | |
1252 » rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, | |
1253 » » » » SEC_OID_PKCS1_RSA_ENCRYPTION, 0); | |
1254 » if (rv == SECSuccess) { | |
1255 » » /* | |
1256 » » * DER encode the public key into the subjectPublicKeyInfo. | |
1257 » » */ | |
1258 » » prepare_rsa_pub_key_for_asn1(pubk); | |
1259 » » rv_item = SEC_ASN1EncodeItem(arena, &spki->subjectPublicKey, | |
1260 » » » » » pubk, SECKEY_RSAPublicKeyTemplate); | |
1261 » » if (rv_item != NULL) { | |
1262 » » /* | |
1263 » » * The stored value is supposed to be a BIT_STRING, | |
1264 » » * so convert the length. | |
1265 » » */ | |
1266 » » spki->subjectPublicKey.len <<= 3; | |
1267 » » /* | |
1268 » » * We got a good one; return it. | |
1269 » » */ | |
1270 » » return spki; | |
1271 » » } | |
1272 » } | |
1273 » break; | |
1274 » case dsaKey: | |
1275 » /* DER encode the params. */ | |
1276 » prepare_pqg_params_for_asn1(&pubk->u.dsa.params); | |
1277 » rv_item = SEC_ASN1EncodeItem(arena, ¶ms, &pubk->u.dsa.params, | |
1278 » » » » » SECKEY_PQGParamsTemplate); | |
1279 » if (rv_item != NULL) { | |
1280 » » rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, | |
1281 » » » » » SEC_OID_ANSIX9_DSA_SIGNATURE, | |
1282 » » » » » ¶ms); | |
1283 » » if (rv == SECSuccess) { | |
1284 » » /* | |
1285 » » * DER encode the public key into the subjectPublicKeyInfo. | |
1286 » » */ | |
1287 » » prepare_dsa_pub_key_for_asn1(pubk); | |
1288 » » rv_item = SEC_ASN1EncodeItem(arena, &spki->subjectPublicKey, | |
1289 » » » » » » pubk, | |
1290 » » » » » » SECKEY_DSAPublicKeyTemplate); | |
1291 » » if (rv_item != NULL) { | |
1292 » » » /* | |
1293 » » » * The stored value is supposed to be a BIT_STRING, | |
1294 » » » * so convert the length. | |
1295 » » » */ | |
1296 » » » spki->subjectPublicKey.len <<= 3; | |
1297 » » » /* | |
1298 » » » * We got a good one; return it. | |
1299 » » » */ | |
1300 » » » return spki; | |
1301 » » } | |
1302 » » } | |
1303 » } | |
1304 » SECITEM_FreeItem(¶ms, PR_FALSE); | |
1305 » break; | |
1306 » case ecKey: | |
1307 » rv = SECITEM_CopyItem(arena, ¶ms, | |
1308 » » » » &pubk->u.ec.DEREncodedParams); | |
1309 » if (rv != SECSuccess) break; | |
1310 | 1264 |
1311 » rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, | 1265 spki->arena = arena; |
1312 » » » » SEC_OID_ANSIX962_EC_PUBLIC_KEY, | 1266 switch (pubk->keyType) { |
1313 » » » » ¶ms); | 1267 case rsaKey: |
1314 » if (rv != SECSuccess) break; | 1268 rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, |
| 1269 SEC_OID_PKCS1_RSA_ENCRYPTION, 0); |
| 1270 if (rv == SECSuccess) { |
| 1271 /* |
| 1272 * DER encode the public key into the subjectPublicKeyInfo. |
| 1273 */ |
| 1274 prepare_rsa_pub_key_for_asn1(pubk); |
| 1275 rv_item = SEC_ASN1EncodeItem(arena, &spki->subjectPublicKey, |
| 1276 pubk, SECKEY_RSAPublicKeyTempla
te); |
| 1277 if (rv_item != NULL) { |
| 1278 /* |
| 1279 * The stored value is supposed to be a BIT_STRING, |
| 1280 * so convert the length. |
| 1281 */ |
| 1282 spki->subjectPublicKey.len <<= 3; |
| 1283 /* |
| 1284 * We got a good one; return it. |
| 1285 */ |
| 1286 return spki; |
| 1287 } |
| 1288 } |
| 1289 break; |
| 1290 case dsaKey: |
| 1291 /* DER encode the params. */ |
| 1292 prepare_pqg_params_for_asn1(&pubk->u.dsa.params); |
| 1293 rv_item = SEC_ASN1EncodeItem(arena, ¶ms, &pubk->u.dsa.params
, |
| 1294 SECKEY_PQGParamsTemplate); |
| 1295 if (rv_item != NULL) { |
| 1296 rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, |
| 1297 SEC_OID_ANSIX9_DSA_SIGNATURE, |
| 1298 ¶ms); |
| 1299 if (rv == SECSuccess) { |
| 1300 /* |
| 1301 * DER encode the public key into the subjectPublicKeyIn
fo. |
| 1302 */ |
| 1303 prepare_dsa_pub_key_for_asn1(pubk); |
| 1304 rv_item = SEC_ASN1EncodeItem(arena, &spki->subjectPublic
Key, |
| 1305 pubk, |
| 1306 SECKEY_DSAPublicKeyTemplate
); |
| 1307 if (rv_item != NULL) { |
| 1308 /* |
| 1309 * The stored value is supposed to be a BIT_STRING, |
| 1310 * so convert the length. |
| 1311 */ |
| 1312 spki->subjectPublicKey.len <<= 3; |
| 1313 /* |
| 1314 * We got a good one; return it. |
| 1315 */ |
| 1316 return spki; |
| 1317 } |
| 1318 } |
| 1319 } |
| 1320 SECITEM_FreeItem(¶ms, PR_FALSE); |
| 1321 break; |
| 1322 case ecKey: |
| 1323 rv = SECITEM_CopyItem(arena, ¶ms, |
| 1324 &pubk->u.ec.DEREncodedParams); |
| 1325 if (rv != SECSuccess) |
| 1326 break; |
1315 | 1327 |
1316 » rv = SECITEM_CopyItem(arena, &spki->subjectPublicKey, | 1328 rv = SECOID_SetAlgorithmID(arena, &spki->algorithm, |
1317 » » » » &pubk->u.ec.publicValue); | 1329 SEC_OID_ANSIX962_EC_PUBLIC_KEY, |
| 1330 ¶ms); |
| 1331 if (rv != SECSuccess) |
| 1332 break; |
1318 | 1333 |
1319 » if (rv == SECSuccess) { | 1334 rv = SECITEM_CopyItem(arena, &spki->subjectPublicKey, |
1320 » /* | 1335 &pubk->u.ec.publicValue); |
1321 » » * The stored value is supposed to be a BIT_STRING, | |
1322 » » * so convert the length. | |
1323 » » */ | |
1324 » spki->subjectPublicKey.len <<= 3; | |
1325 » » /* | |
1326 » » * We got a good one; return it. | |
1327 » » */ | |
1328 » » return spki; | |
1329 » } | |
1330 » break; | |
1331 » case dhKey: /* later... */ | |
1332 | 1336 |
1333 » break; | 1337 if (rv == SECSuccess) { |
1334 » default: | 1338 /* |
1335 » break; | 1339 * The stored value is supposed to be a BIT_STRING, |
1336 » } | 1340 * so convert the length. |
| 1341 */ |
| 1342 spki->subjectPublicKey.len <<= 3; |
| 1343 /* |
| 1344 * We got a good one; return it. |
| 1345 */ |
| 1346 return spki; |
| 1347 } |
| 1348 break; |
| 1349 case dhKey: /* later... */ |
| 1350 |
| 1351 break; |
| 1352 default: |
| 1353 break; |
| 1354 } |
1337 } else { | 1355 } else { |
1338 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 1356 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1339 } | 1357 } |
1340 | 1358 |
1341 PORT_FreeArena(arena, PR_FALSE); | 1359 PORT_FreeArena(arena, PR_FALSE); |
1342 return NULL; | 1360 return NULL; |
1343 } | 1361 } |
1344 | 1362 |
1345 CERTSubjectPublicKeyInfo * | 1363 CERTSubjectPublicKeyInfo * |
1346 SECKEY_CreateSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) | 1364 SECKEY_CreateSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) |
1347 { | 1365 { |
1348 CERTSubjectPublicKeyInfo *spki; | 1366 CERTSubjectPublicKeyInfo *spki; |
(...skipping 10 matching lines...) Expand all Loading... |
1359 } | 1377 } |
1360 spki = seckey_CreateSubjectPublicKeyInfo_helper(tempKey); | 1378 spki = seckey_CreateSubjectPublicKeyInfo_helper(tempKey); |
1361 SECKEY_DestroyPublicKey(tempKey); | 1379 SECKEY_DestroyPublicKey(tempKey); |
1362 return spki; | 1380 return spki; |
1363 } | 1381 } |
1364 | 1382 |
1365 void | 1383 void |
1366 SECKEY_DestroySubjectPublicKeyInfo(CERTSubjectPublicKeyInfo *spki) | 1384 SECKEY_DestroySubjectPublicKeyInfo(CERTSubjectPublicKeyInfo *spki) |
1367 { | 1385 { |
1368 if (spki && spki->arena) { | 1386 if (spki && spki->arena) { |
1369 » PORT_FreeArena(spki->arena, PR_FALSE); | 1387 PORT_FreeArena(spki->arena, PR_FALSE); |
1370 } | 1388 } |
1371 } | 1389 } |
1372 | 1390 |
1373 SECItem * | 1391 SECItem * |
1374 SECKEY_EncodeDERSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) | 1392 SECKEY_EncodeDERSubjectPublicKeyInfo(const SECKEYPublicKey *pubk) |
1375 { | 1393 { |
1376 CERTSubjectPublicKeyInfo *spki=NULL; | 1394 CERTSubjectPublicKeyInfo *spki = NULL; |
1377 SECItem *spkiDER=NULL; | 1395 SECItem *spkiDER = NULL; |
1378 | 1396 |
1379 /* get the subjectpublickeyinfo */ | 1397 /* get the subjectpublickeyinfo */ |
1380 spki = SECKEY_CreateSubjectPublicKeyInfo(pubk); | 1398 spki = SECKEY_CreateSubjectPublicKeyInfo(pubk); |
1381 if( spki == NULL ) { | 1399 if (spki == NULL) { |
1382 » goto finish; | 1400 goto finish; |
1383 } | 1401 } |
1384 | 1402 |
1385 /* DER-encode the subjectpublickeyinfo */ | 1403 /* DER-encode the subjectpublickeyinfo */ |
1386 spkiDER = SEC_ASN1EncodeItem(NULL /*arena*/, NULL/*dest*/, spki, | 1404 spkiDER = SEC_ASN1EncodeItem(NULL /*arena*/, NULL /*dest*/, spki, |
1387 » » » » » CERT_SubjectPublicKeyInfoTemplate); | 1405 CERT_SubjectPublicKeyInfoTemplate); |
1388 | 1406 |
1389 SECKEY_DestroySubjectPublicKeyInfo(spki); | 1407 SECKEY_DestroySubjectPublicKeyInfo(spki); |
1390 | 1408 |
1391 finish: | 1409 finish: |
1392 return spkiDER; | 1410 return spkiDER; |
1393 } | 1411 } |
1394 | 1412 |
1395 | |
1396 CERTSubjectPublicKeyInfo * | 1413 CERTSubjectPublicKeyInfo * |
1397 SECKEY_DecodeDERSubjectPublicKeyInfo(const SECItem *spkider) | 1414 SECKEY_DecodeDERSubjectPublicKeyInfo(const SECItem *spkider) |
1398 { | 1415 { |
1399 PLArenaPool *arena; | 1416 PLArenaPool *arena; |
1400 CERTSubjectPublicKeyInfo *spki; | 1417 CERTSubjectPublicKeyInfo *spki; |
1401 SECStatus rv; | 1418 SECStatus rv; |
1402 SECItem newSpkider; | 1419 SECItem newSpkider; |
1403 | 1420 |
1404 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1421 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1405 if (arena == NULL) { | 1422 if (arena == NULL) { |
1406 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 1423 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1407 » return NULL; | 1424 return NULL; |
1408 } | 1425 } |
1409 | 1426 |
1410 spki = (CERTSubjectPublicKeyInfo *) | 1427 spki = (CERTSubjectPublicKeyInfo *) |
1411 » » PORT_ArenaZAlloc(arena, sizeof (CERTSubjectPublicKeyInfo)); | 1428 PORT_ArenaZAlloc(arena, sizeof(CERTSubjectPublicKeyInfo)); |
1412 if (spki != NULL) { | 1429 if (spki != NULL) { |
1413 » spki->arena = arena; | 1430 spki->arena = arena; |
1414 | 1431 |
1415 /* copy the DER into the arena, since Quick DER returns data that points | 1432 /* copy the DER into the arena, since Quick DER returns data that points |
1416 into the DER input, which may get freed by the caller */ | 1433 into the DER input, which may get freed by the caller */ |
1417 rv = SECITEM_CopyItem(arena, &newSpkider, spkider); | 1434 rv = SECITEM_CopyItem(arena, &newSpkider, spkider); |
1418 if ( rv == SECSuccess ) { | 1435 if (rv == SECSuccess) { |
1419 rv = SEC_QuickDERDecodeItem(arena,spki, | 1436 rv = SEC_QuickDERDecodeItem(arena, spki, |
1420 » » » » CERT_SubjectPublicKeyInfoTemplate, &newSpkid
er); | 1437 CERT_SubjectPublicKeyInfoTemplate, &newS
pkider); |
1421 } | 1438 } |
1422 » if (rv == SECSuccess) | 1439 if (rv == SECSuccess) |
1423 » return spki; | 1440 return spki; |
1424 } else { | 1441 } else { |
1425 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 1442 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1426 } | 1443 } |
1427 | 1444 |
1428 PORT_FreeArena(arena, PR_FALSE); | 1445 PORT_FreeArena(arena, PR_FALSE); |
1429 return NULL; | 1446 return NULL; |
1430 } | 1447 } |
1431 | 1448 |
1432 /* | 1449 /* |
1433 * Decode a base64 ascii encoded DER encoded subject public key info. | 1450 * Decode a base64 ascii encoded DER encoded subject public key info. |
1434 */ | 1451 */ |
1435 CERTSubjectPublicKeyInfo * | 1452 CERTSubjectPublicKeyInfo * |
1436 SECKEY_ConvertAndDecodeSubjectPublicKeyInfo(const char *spkistr) | 1453 SECKEY_ConvertAndDecodeSubjectPublicKeyInfo(const char *spkistr) |
1437 { | 1454 { |
1438 CERTSubjectPublicKeyInfo *spki; | 1455 CERTSubjectPublicKeyInfo *spki; |
1439 SECStatus rv; | 1456 SECStatus rv; |
1440 SECItem der; | 1457 SECItem der; |
1441 | 1458 |
1442 rv = ATOB_ConvertAsciiToItem(&der, spkistr); | 1459 rv = ATOB_ConvertAsciiToItem(&der, spkistr); |
1443 if (rv != SECSuccess) | 1460 if (rv != SECSuccess) |
1444 » return NULL; | 1461 return NULL; |
1445 | 1462 |
1446 spki = SECKEY_DecodeDERSubjectPublicKeyInfo(&der); | 1463 spki = SECKEY_DecodeDERSubjectPublicKeyInfo(&der); |
1447 | 1464 |
1448 PORT_Free(der.data); | 1465 PORT_Free(der.data); |
1449 return spki; | 1466 return spki; |
1450 } | 1467 } |
1451 | 1468 |
1452 /* | 1469 /* |
1453 * Decode a base64 ascii encoded DER encoded public key and challenge | 1470 * Decode a base64 ascii encoded DER encoded public key and challenge |
1454 * Verify digital signature and make sure challenge matches | 1471 * Verify digital signature and make sure challenge matches |
1455 */ | 1472 */ |
1456 CERTSubjectPublicKeyInfo * | 1473 CERTSubjectPublicKeyInfo * |
1457 SECKEY_ConvertAndDecodePublicKeyAndChallenge(char *pkacstr, char *challenge, | 1474 SECKEY_ConvertAndDecodePublicKeyAndChallenge(char *pkacstr, char *challenge, |
1458 » » » » » » » » void *wincx) | 1475 void *wincx) |
1459 { | 1476 { |
1460 CERTSubjectPublicKeyInfo *spki = NULL; | 1477 CERTSubjectPublicKeyInfo *spki = NULL; |
1461 CERTPublicKeyAndChallenge pkac; | 1478 CERTPublicKeyAndChallenge pkac; |
1462 SECStatus rv; | 1479 SECStatus rv; |
1463 SECItem signedItem; | 1480 SECItem signedItem; |
1464 PLArenaPool *arena = NULL; | 1481 PLArenaPool *arena = NULL; |
1465 CERTSignedData sd; | 1482 CERTSignedData sd; |
1466 SECItem sig; | 1483 SECItem sig; |
1467 SECKEYPublicKey *pubKey = NULL; | 1484 SECKEYPublicKey *pubKey = NULL; |
1468 unsigned int len; | 1485 unsigned int len; |
1469 | 1486 |
1470 signedItem.data = NULL; | 1487 signedItem.data = NULL; |
1471 | 1488 |
1472 /* convert the base64 encoded data to binary */ | 1489 /* convert the base64 encoded data to binary */ |
1473 rv = ATOB_ConvertAsciiToItem(&signedItem, pkacstr); | 1490 rv = ATOB_ConvertAsciiToItem(&signedItem, pkacstr); |
1474 if (rv != SECSuccess) { | 1491 if (rv != SECSuccess) { |
1475 » goto loser; | 1492 goto loser; |
1476 } | 1493 } |
1477 | 1494 |
1478 /* create an arena */ | 1495 /* create an arena */ |
1479 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1496 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1480 if (arena == NULL) { | 1497 if (arena == NULL) { |
1481 » goto loser; | 1498 goto loser; |
1482 } | 1499 } |
1483 | 1500 |
1484 /* decode the outer wrapping of signed data */ | 1501 /* decode the outer wrapping of signed data */ |
1485 PORT_Memset(&sd, 0, sizeof(CERTSignedData)); | 1502 PORT_Memset(&sd, 0, sizeof(CERTSignedData)); |
1486 rv = SEC_QuickDERDecodeItem(arena, &sd, CERT_SignedDataTemplate, &signedItem
); | 1503 rv = SEC_QuickDERDecodeItem(arena, &sd, CERT_SignedDataTemplate, &signedItem
); |
1487 if ( rv ) { | 1504 if (rv) { |
1488 » goto loser; | 1505 goto loser; |
1489 } | 1506 } |
1490 | 1507 |
1491 /* decode the public key and challenge wrapper */ | 1508 /* decode the public key and challenge wrapper */ |
1492 PORT_Memset(&pkac, 0, sizeof(CERTPublicKeyAndChallenge)); | 1509 PORT_Memset(&pkac, 0, sizeof(CERTPublicKeyAndChallenge)); |
1493 rv = SEC_QuickDERDecodeItem(arena, &pkac, CERT_PublicKeyAndChallengeTemplate
, | 1510 rv = SEC_QuickDERDecodeItem(arena, &pkac, CERT_PublicKeyAndChallengeTemplate
, |
1494 » » » &sd.data); | 1511 &sd.data); |
1495 if ( rv ) { | 1512 if (rv) { |
1496 » goto loser; | 1513 goto loser; |
1497 } | 1514 } |
1498 | 1515 |
1499 /* decode the subject public key info */ | 1516 /* decode the subject public key info */ |
1500 spki = SECKEY_DecodeDERSubjectPublicKeyInfo(&pkac.spki); | 1517 spki = SECKEY_DecodeDERSubjectPublicKeyInfo(&pkac.spki); |
1501 if ( spki == NULL ) { | 1518 if (spki == NULL) { |
1502 » goto loser; | 1519 goto loser; |
1503 } | 1520 } |
1504 | 1521 |
1505 /* get the public key */ | 1522 /* get the public key */ |
1506 pubKey = seckey_ExtractPublicKey(spki); | 1523 pubKey = seckey_ExtractPublicKey(spki); |
1507 if ( pubKey == NULL ) { | 1524 if (pubKey == NULL) { |
1508 » goto loser; | 1525 goto loser; |
1509 } | 1526 } |
1510 | 1527 |
1511 /* check the signature */ | 1528 /* check the signature */ |
1512 sig = sd.signature; | 1529 sig = sd.signature; |
1513 DER_ConvertBitString(&sig); | 1530 DER_ConvertBitString(&sig); |
1514 rv = VFY_VerifyDataWithAlgorithmID(sd.data.data, sd.data.len, pubKey, &sig, | 1531 rv = VFY_VerifyDataWithAlgorithmID(sd.data.data, sd.data.len, pubKey, &sig, |
1515 » » » &(sd.signatureAlgorithm), NULL, wincx); | 1532 &(sd.signatureAlgorithm), NULL, wincx); |
1516 if ( rv != SECSuccess ) { | 1533 if (rv != SECSuccess) { |
1517 » goto loser; | 1534 goto loser; |
1518 } | 1535 } |
1519 | 1536 |
1520 /* check the challenge */ | 1537 /* check the challenge */ |
1521 if ( challenge ) { | 1538 if (challenge) { |
1522 » len = PORT_Strlen(challenge); | 1539 len = PORT_Strlen(challenge); |
1523 » /* length is right */ | 1540 /* length is right */ |
1524 » if ( len != pkac.challenge.len ) { | 1541 if (len != pkac.challenge.len) { |
1525 » goto loser; | 1542 goto loser; |
1526 » } | 1543 } |
1527 » /* actual data is right */ | 1544 /* actual data is right */ |
1528 » if ( PORT_Memcmp(challenge, pkac.challenge.data, len) != 0 ) { | 1545 if (PORT_Memcmp(challenge, pkac.challenge.data, len) != 0) { |
1529 » goto loser; | 1546 goto loser; |
1530 » } | 1547 } |
1531 } | 1548 } |
1532 goto done; | 1549 goto done; |
1533 | 1550 |
1534 loser: | 1551 loser: |
1535 /* make sure that we return null if we got an error */ | 1552 /* make sure that we return null if we got an error */ |
1536 if ( spki ) { | 1553 if (spki) { |
1537 » SECKEY_DestroySubjectPublicKeyInfo(spki); | 1554 SECKEY_DestroySubjectPublicKeyInfo(spki); |
1538 } | 1555 } |
1539 spki = NULL; | 1556 spki = NULL; |
1540 | 1557 |
1541 done: | 1558 done: |
1542 if ( signedItem.data ) { | 1559 if (signedItem.data) { |
1543 » PORT_Free(signedItem.data); | 1560 PORT_Free(signedItem.data); |
1544 } | 1561 } |
1545 if ( arena ) { | 1562 if (arena) { |
1546 » PORT_FreeArena(arena, PR_FALSE); | 1563 PORT_FreeArena(arena, PR_FALSE); |
1547 } | 1564 } |
1548 if ( pubKey ) { | 1565 if (pubKey) { |
1549 » SECKEY_DestroyPublicKey(pubKey); | 1566 SECKEY_DestroyPublicKey(pubKey); |
1550 } | 1567 } |
1551 | 1568 |
1552 return spki; | 1569 return spki; |
1553 } | 1570 } |
1554 | 1571 |
1555 void | 1572 void |
1556 SECKEY_DestroyPrivateKeyInfo(SECKEYPrivateKeyInfo *pvk, | 1573 SECKEY_DestroyPrivateKeyInfo(SECKEYPrivateKeyInfo *pvk, |
1557 » » » PRBool freeit) | 1574 PRBool freeit) |
1558 { | 1575 { |
1559 PLArenaPool *poolp; | 1576 PLArenaPool *poolp; |
1560 | 1577 |
1561 if(pvk != NULL) { | 1578 if (pvk != NULL) { |
1562 » if(pvk->arena) { | 1579 if (pvk->arena) { |
1563 » poolp = pvk->arena; | 1580 poolp = pvk->arena; |
1564 » /* zero structure since PORT_FreeArena does not support | 1581 /* zero structure since PORT_FreeArena does not support |
1565 » * this yet. | 1582 * this yet. |
1566 » */ | 1583 */ |
1567 » PORT_Memset(pvk->privateKey.data, 0, pvk->privateKey.len); | 1584 PORT_Memset(pvk->privateKey.data, 0, pvk->privateKey.len); |
1568 » PORT_Memset(pvk, 0, sizeof(*pvk)); | 1585 PORT_Memset(pvk, 0, sizeof(*pvk)); |
1569 » if(freeit == PR_TRUE) { | 1586 if (freeit == PR_TRUE) { |
1570 » » PORT_FreeArena(poolp, PR_TRUE); | 1587 PORT_FreeArena(poolp, PR_TRUE); |
1571 » } else { | 1588 } else { |
1572 » » pvk->arena = poolp; | 1589 pvk->arena = poolp; |
1573 » } | 1590 } |
1574 » } else { | 1591 } else { |
1575 » SECITEM_ZfreeItem(&pvk->version, PR_FALSE); | 1592 SECITEM_ZfreeItem(&pvk->version, PR_FALSE); |
1576 » SECITEM_ZfreeItem(&pvk->privateKey, PR_FALSE); | 1593 SECITEM_ZfreeItem(&pvk->privateKey, PR_FALSE); |
1577 » SECOID_DestroyAlgorithmID(&pvk->algorithm, PR_FALSE); | 1594 SECOID_DestroyAlgorithmID(&pvk->algorithm, PR_FALSE); |
1578 » PORT_Memset(pvk, 0, sizeof(*pvk)); | 1595 PORT_Memset(pvk, 0, sizeof(*pvk)); |
1579 » if(freeit == PR_TRUE) { | 1596 if (freeit == PR_TRUE) { |
1580 » » PORT_Free(pvk); | 1597 PORT_Free(pvk); |
1581 » } | 1598 } |
1582 » } | 1599 } |
1583 } | 1600 } |
1584 } | 1601 } |
1585 | 1602 |
1586 void | 1603 void |
1587 SECKEY_DestroyEncryptedPrivateKeyInfo(SECKEYEncryptedPrivateKeyInfo *epki, | 1604 SECKEY_DestroyEncryptedPrivateKeyInfo(SECKEYEncryptedPrivateKeyInfo *epki, |
1588 » » » » PRBool freeit) | 1605 PRBool freeit) |
1589 { | 1606 { |
1590 PLArenaPool *poolp; | 1607 PLArenaPool *poolp; |
1591 | 1608 |
1592 if(epki != NULL) { | 1609 if (epki != NULL) { |
1593 » if(epki->arena) { | 1610 if (epki->arena) { |
1594 » poolp = epki->arena; | 1611 poolp = epki->arena; |
1595 » /* zero structure since PORT_FreeArena does not support | 1612 /* zero structure since PORT_FreeArena does not support |
1596 » * this yet. | 1613 * this yet. |
1597 » */ | 1614 */ |
1598 » PORT_Memset(epki->encryptedData.data, 0, epki->encryptedData.len); | 1615 PORT_Memset(epki->encryptedData.data, 0, epki->encryptedData.len); |
1599 » PORT_Memset(epki, 0, sizeof(*epki)); | 1616 PORT_Memset(epki, 0, sizeof(*epki)); |
1600 » if(freeit == PR_TRUE) { | 1617 if (freeit == PR_TRUE) { |
1601 » » PORT_FreeArena(poolp, PR_TRUE); | 1618 PORT_FreeArena(poolp, PR_TRUE); |
1602 » } else { | 1619 } else { |
1603 » » epki->arena = poolp; | 1620 epki->arena = poolp; |
1604 » } | 1621 } |
1605 » } else { | 1622 } else { |
1606 » SECITEM_ZfreeItem(&epki->encryptedData, PR_FALSE); | 1623 SECITEM_ZfreeItem(&epki->encryptedData, PR_FALSE); |
1607 » SECOID_DestroyAlgorithmID(&epki->algorithm, PR_FALSE); | 1624 SECOID_DestroyAlgorithmID(&epki->algorithm, PR_FALSE); |
1608 » PORT_Memset(epki, 0, sizeof(*epki)); | 1625 PORT_Memset(epki, 0, sizeof(*epki)); |
1609 » if(freeit == PR_TRUE) { | 1626 if (freeit == PR_TRUE) { |
1610 » » PORT_Free(epki); | 1627 PORT_Free(epki); |
1611 » } | 1628 } |
1612 » } | 1629 } |
1613 } | 1630 } |
1614 } | 1631 } |
1615 | 1632 |
1616 SECStatus | 1633 SECStatus |
1617 SECKEY_CopyPrivateKeyInfo(PLArenaPool *poolp, | 1634 SECKEY_CopyPrivateKeyInfo(PLArenaPool *poolp, |
1618 » » » SECKEYPrivateKeyInfo *to, | 1635 SECKEYPrivateKeyInfo *to, |
1619 » » » const SECKEYPrivateKeyInfo *from) | 1636 const SECKEYPrivateKeyInfo *from) |
1620 { | 1637 { |
1621 SECStatus rv = SECFailure; | 1638 SECStatus rv = SECFailure; |
1622 | 1639 |
1623 if((to == NULL) || (from == NULL)) { | 1640 if ((to == NULL) || (from == NULL)) { |
1624 » return SECFailure; | 1641 return SECFailure; |
1625 } | 1642 } |
1626 | 1643 |
1627 rv = SECOID_CopyAlgorithmID(poolp, &to->algorithm, &from->algorithm); | 1644 rv = SECOID_CopyAlgorithmID(poolp, &to->algorithm, &from->algorithm); |
1628 if(rv != SECSuccess) { | 1645 if (rv != SECSuccess) { |
1629 » return SECFailure; | 1646 return SECFailure; |
1630 } | 1647 } |
1631 rv = SECITEM_CopyItem(poolp, &to->privateKey, &from->privateKey); | 1648 rv = SECITEM_CopyItem(poolp, &to->privateKey, &from->privateKey); |
1632 if(rv != SECSuccess) { | 1649 if (rv != SECSuccess) { |
1633 » return SECFailure; | 1650 return SECFailure; |
1634 } | 1651 } |
1635 rv = SECITEM_CopyItem(poolp, &to->version, &from->version); | 1652 rv = SECITEM_CopyItem(poolp, &to->version, &from->version); |
1636 | 1653 |
1637 return rv; | 1654 return rv; |
1638 } | 1655 } |
1639 | 1656 |
1640 SECStatus | 1657 SECStatus |
1641 SECKEY_CopyEncryptedPrivateKeyInfo(PLArenaPool *poolp, | 1658 SECKEY_CopyEncryptedPrivateKeyInfo(PLArenaPool *poolp, |
1642 » » » » SECKEYEncryptedPrivateKeyInfo *to, | 1659 SECKEYEncryptedPrivateKeyInfo *to, |
1643 » » » » const SECKEYEncryptedPrivateKeyInfo *from) | 1660 const SECKEYEncryptedPrivateKeyInfo *from) |
1644 { | 1661 { |
1645 SECStatus rv = SECFailure; | 1662 SECStatus rv = SECFailure; |
1646 | 1663 |
1647 if((to == NULL) || (from == NULL)) { | 1664 if ((to == NULL) || (from == NULL)) { |
1648 » return SECFailure; | 1665 return SECFailure; |
1649 } | 1666 } |
1650 | 1667 |
1651 rv = SECOID_CopyAlgorithmID(poolp, &to->algorithm, &from->algorithm); | 1668 rv = SECOID_CopyAlgorithmID(poolp, &to->algorithm, &from->algorithm); |
1652 if(rv != SECSuccess) { | 1669 if (rv != SECSuccess) { |
1653 » return SECFailure; | 1670 return SECFailure; |
1654 } | 1671 } |
1655 rv = SECITEM_CopyItem(poolp, &to->encryptedData, &from->encryptedData); | 1672 rv = SECITEM_CopyItem(poolp, &to->encryptedData, &from->encryptedData); |
1656 | 1673 |
1657 return rv; | 1674 return rv; |
1658 } | 1675 } |
1659 | 1676 |
1660 KeyType | 1677 KeyType |
1661 SECKEY_GetPrivateKeyType(const SECKEYPrivateKey *privKey) | 1678 SECKEY_GetPrivateKeyType(const SECKEYPrivateKey *privKey) |
1662 { | 1679 { |
1663 return privKey->keyType; | 1680 return privKey->keyType; |
1664 } | 1681 } |
1665 | 1682 |
1666 KeyType | 1683 KeyType |
1667 SECKEY_GetPublicKeyType(const SECKEYPublicKey *pubKey) | 1684 SECKEY_GetPublicKeyType(const SECKEYPublicKey *pubKey) |
1668 { | 1685 { |
1669 return pubKey->keyType; | 1686 return pubKey->keyType; |
1670 } | 1687 } |
1671 | 1688 |
1672 SECKEYPublicKey* | 1689 SECKEYPublicKey * |
1673 SECKEY_ImportDERPublicKey(const SECItem *derKey, CK_KEY_TYPE type) | 1690 SECKEY_ImportDERPublicKey(const SECItem *derKey, CK_KEY_TYPE type) |
1674 { | 1691 { |
1675 SECKEYPublicKey *pubk = NULL; | 1692 SECKEYPublicKey *pubk = NULL; |
1676 SECStatus rv = SECFailure; | 1693 SECStatus rv = SECFailure; |
1677 SECItem newDerKey; | 1694 SECItem newDerKey; |
1678 PLArenaPool *arena = NULL; | 1695 PLArenaPool *arena = NULL; |
1679 | 1696 |
1680 if (!derKey) { | 1697 if (!derKey) { |
1681 return NULL; | 1698 return NULL; |
1682 } | 1699 } |
1683 | 1700 |
1684 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1701 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1685 if (arena == NULL) { | 1702 if (arena == NULL) { |
1686 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 1703 PORT_SetError(SEC_ERROR_NO_MEMORY); |
1687 goto finish; | 1704 goto finish; |
1688 } | 1705 } |
1689 | 1706 |
1690 pubk = PORT_ArenaZNew(arena, SECKEYPublicKey); | 1707 pubk = PORT_ArenaZNew(arena, SECKEYPublicKey); |
1691 if (pubk == NULL) { | 1708 if (pubk == NULL) { |
1692 goto finish; | 1709 goto finish; |
1693 } | 1710 } |
1694 pubk->arena = arena; | 1711 pubk->arena = arena; |
1695 | 1712 |
1696 rv = SECITEM_CopyItem(pubk->arena, &newDerKey, derKey); | 1713 rv = SECITEM_CopyItem(pubk->arena, &newDerKey, derKey); |
1697 if (SECSuccess != rv) { | 1714 if (SECSuccess != rv) { |
1698 goto finish; | 1715 goto finish; |
1699 } | 1716 } |
1700 | 1717 |
1701 pubk->pkcs11Slot = NULL; | 1718 pubk->pkcs11Slot = NULL; |
1702 pubk->pkcs11ID = CK_INVALID_HANDLE; | 1719 pubk->pkcs11ID = CK_INVALID_HANDLE; |
1703 | 1720 |
1704 switch( type ) { | 1721 switch (type) { |
1705 case CKK_RSA: | 1722 case CKK_RSA: |
1706 » prepare_rsa_pub_key_for_asn1(pubk); | 1723 prepare_rsa_pub_key_for_asn1(pubk); |
1707 rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_RSAPublicKeyTempla
te, &newDerKey); | 1724 rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_RSAPublicKeyTe
mplate, &newDerKey); |
1708 pubk->keyType = rsaKey; | 1725 pubk->keyType = rsaKey; |
1709 break; | 1726 break; |
1710 case CKK_DSA: | 1727 case CKK_DSA: |
1711 » prepare_dsa_pub_key_for_asn1(pubk); | 1728 prepare_dsa_pub_key_for_asn1(pubk); |
1712 rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_DSAPublicKeyTempla
te, &newDerKey); | 1729 rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_DSAPublicKeyTe
mplate, &newDerKey); |
1713 pubk->keyType = dsaKey; | 1730 pubk->keyType = dsaKey; |
1714 break; | 1731 break; |
1715 case CKK_DH: | 1732 case CKK_DH: |
1716 » prepare_dh_pub_key_for_asn1(pubk); | 1733 prepare_dh_pub_key_for_asn1(pubk); |
1717 rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_DHPublicKeyTemplat
e, &newDerKey); | 1734 rv = SEC_QuickDERDecodeItem(pubk->arena, pubk, SECKEY_DHPublicKeyTem
plate, &newDerKey); |
1718 pubk->keyType = dhKey; | 1735 pubk->keyType = dhKey; |
1719 break; | 1736 break; |
1720 default: | 1737 default: |
1721 rv = SECFailure; | 1738 rv = SECFailure; |
1722 break; | 1739 break; |
1723 } | 1740 } |
1724 | 1741 |
1725 finish: | 1742 finish: |
1726 if (rv != SECSuccess) { | 1743 if (rv != SECSuccess) { |
1727 if (arena != NULL) { | 1744 if (arena != NULL) { |
1728 PORT_FreeArena(arena, PR_FALSE); | 1745 PORT_FreeArena(arena, PR_FALSE); |
1729 } | 1746 } |
1730 pubk = NULL; | 1747 pubk = NULL; |
1731 } | 1748 } |
1732 return pubk; | 1749 return pubk; |
1733 } | 1750 } |
1734 | 1751 |
1735 SECKEYPrivateKeyList* | 1752 SECKEYPrivateKeyList * |
1736 SECKEY_NewPrivateKeyList(void) | 1753 SECKEY_NewPrivateKeyList(void) |
1737 { | 1754 { |
1738 PLArenaPool *arena = NULL; | 1755 PLArenaPool *arena = NULL; |
1739 SECKEYPrivateKeyList *ret = NULL; | 1756 SECKEYPrivateKeyList *ret = NULL; |
1740 | 1757 |
1741 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1758 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1742 if ( arena == NULL ) { | 1759 if (arena == NULL) { |
1743 goto loser; | 1760 goto loser; |
1744 } | 1761 } |
1745 | 1762 |
1746 ret = (SECKEYPrivateKeyList *)PORT_ArenaZAlloc(arena, | 1763 ret = (SECKEYPrivateKeyList *)PORT_ArenaZAlloc(arena, |
1747 sizeof(SECKEYPrivateKeyList)); | 1764 sizeof(SECKEYPrivateKeyList))
; |
1748 if ( ret == NULL ) { | 1765 if (ret == NULL) { |
1749 goto loser; | 1766 goto loser; |
1750 } | 1767 } |
1751 | 1768 |
1752 ret->arena = arena; | 1769 ret->arena = arena; |
1753 | 1770 |
1754 PR_INIT_CLIST(&ret->list); | 1771 PR_INIT_CLIST(&ret->list); |
1755 | 1772 |
1756 return(ret); | 1773 return (ret); |
1757 | 1774 |
1758 loser: | 1775 loser: |
1759 if ( arena != NULL ) { | 1776 if (arena != NULL) { |
1760 PORT_FreeArena(arena, PR_FALSE); | 1777 PORT_FreeArena(arena, PR_FALSE); |
1761 } | 1778 } |
1762 | 1779 |
1763 return(NULL); | 1780 return (NULL); |
1764 } | 1781 } |
1765 | 1782 |
1766 void | 1783 void |
1767 SECKEY_DestroyPrivateKeyList(SECKEYPrivateKeyList *keys) | 1784 SECKEY_DestroyPrivateKeyList(SECKEYPrivateKeyList *keys) |
1768 { | 1785 { |
1769 while( !PR_CLIST_IS_EMPTY(&keys->list) ) { | 1786 while (!PR_CLIST_IS_EMPTY(&keys->list)) { |
1770 SECKEY_RemovePrivateKeyListNode( | 1787 SECKEY_RemovePrivateKeyListNode( |
1771 (SECKEYPrivateKeyListNode*)(PR_LIST_HEAD(&keys->list)) ); | 1788 (SECKEYPrivateKeyListNode *)(PR_LIST_HEAD(&keys->list))); |
1772 } | 1789 } |
1773 | 1790 |
1774 PORT_FreeArena(keys->arena, PR_FALSE); | 1791 PORT_FreeArena(keys->arena, PR_FALSE); |
1775 | 1792 |
1776 return; | 1793 return; |
1777 } | 1794 } |
1778 | 1795 |
1779 | |
1780 void | 1796 void |
1781 SECKEY_RemovePrivateKeyListNode(SECKEYPrivateKeyListNode *node) | 1797 SECKEY_RemovePrivateKeyListNode(SECKEYPrivateKeyListNode *node) |
1782 { | 1798 { |
1783 PR_ASSERT(node->key); | 1799 PR_ASSERT(node->key); |
1784 SECKEY_DestroyPrivateKey(node->key); | 1800 SECKEY_DestroyPrivateKey(node->key); |
1785 node->key = NULL; | 1801 node->key = NULL; |
1786 PR_REMOVE_LINK(&node->links); | 1802 PR_REMOVE_LINK(&node->links); |
1787 return; | 1803 return; |
1788 | |
1789 } | 1804 } |
1790 | 1805 |
1791 SECStatus | 1806 SECStatus |
1792 SECKEY_AddPrivateKeyToListTail( SECKEYPrivateKeyList *list, | 1807 SECKEY_AddPrivateKeyToListTail(SECKEYPrivateKeyList *list, |
1793 SECKEYPrivateKey *key) | 1808 SECKEYPrivateKey *key) |
1794 { | 1809 { |
1795 SECKEYPrivateKeyListNode *node; | 1810 SECKEYPrivateKeyListNode *node; |
1796 | 1811 |
1797 node = (SECKEYPrivateKeyListNode *)PORT_ArenaZAlloc(list->arena, | 1812 node = (SECKEYPrivateKeyListNode *)PORT_ArenaZAlloc(list->arena, |
1798 sizeof(SECKEYPrivateKeyListNode)); | 1813 sizeof(SECKEYPrivateKeyL
istNode)); |
1799 if ( node == NULL ) { | 1814 if (node == NULL) { |
1800 goto loser; | 1815 goto loser; |
1801 } | 1816 } |
1802 | 1817 |
1803 PR_INSERT_BEFORE(&node->links, &list->list); | 1818 PR_INSERT_BEFORE(&node->links, &list->list); |
1804 node->key = key; | 1819 node->key = key; |
1805 return(SECSuccess); | 1820 return (SECSuccess); |
1806 | 1821 |
1807 loser: | 1822 loser: |
1808 return(SECFailure); | 1823 return (SECFailure); |
1809 } | 1824 } |
1810 | 1825 |
1811 | 1826 SECKEYPublicKeyList * |
1812 SECKEYPublicKeyList* | |
1813 SECKEY_NewPublicKeyList(void) | 1827 SECKEY_NewPublicKeyList(void) |
1814 { | 1828 { |
1815 PLArenaPool *arena = NULL; | 1829 PLArenaPool *arena = NULL; |
1816 SECKEYPublicKeyList *ret = NULL; | 1830 SECKEYPublicKeyList *ret = NULL; |
1817 | 1831 |
1818 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1832 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1819 if ( arena == NULL ) { | 1833 if (arena == NULL) { |
1820 goto loser; | 1834 goto loser; |
1821 } | 1835 } |
1822 | 1836 |
1823 ret = (SECKEYPublicKeyList *)PORT_ArenaZAlloc(arena, | 1837 ret = (SECKEYPublicKeyList *)PORT_ArenaZAlloc(arena, |
1824 sizeof(SECKEYPublicKeyList)); | 1838 sizeof(SECKEYPublicKeyList)); |
1825 if ( ret == NULL ) { | 1839 if (ret == NULL) { |
1826 goto loser; | 1840 goto loser; |
1827 } | 1841 } |
1828 | 1842 |
1829 ret->arena = arena; | 1843 ret->arena = arena; |
1830 | 1844 |
1831 PR_INIT_CLIST(&ret->list); | 1845 PR_INIT_CLIST(&ret->list); |
1832 | 1846 |
1833 return(ret); | 1847 return (ret); |
1834 | 1848 |
1835 loser: | 1849 loser: |
1836 if ( arena != NULL ) { | 1850 if (arena != NULL) { |
1837 PORT_FreeArena(arena, PR_FALSE); | 1851 PORT_FreeArena(arena, PR_FALSE); |
1838 } | 1852 } |
1839 | 1853 |
1840 return(NULL); | 1854 return (NULL); |
1841 } | 1855 } |
1842 | 1856 |
1843 void | 1857 void |
1844 SECKEY_DestroyPublicKeyList(SECKEYPublicKeyList *keys) | 1858 SECKEY_DestroyPublicKeyList(SECKEYPublicKeyList *keys) |
1845 { | 1859 { |
1846 while( !PR_CLIST_IS_EMPTY(&keys->list) ) { | 1860 while (!PR_CLIST_IS_EMPTY(&keys->list)) { |
1847 SECKEY_RemovePublicKeyListNode( | 1861 SECKEY_RemovePublicKeyListNode( |
1848 (SECKEYPublicKeyListNode*)(PR_LIST_HEAD(&keys->list)) ); | 1862 (SECKEYPublicKeyListNode *)(PR_LIST_HEAD(&keys->list))); |
1849 } | 1863 } |
1850 | 1864 |
1851 PORT_FreeArena(keys->arena, PR_FALSE); | 1865 PORT_FreeArena(keys->arena, PR_FALSE); |
1852 | 1866 |
1853 return; | 1867 return; |
1854 } | 1868 } |
1855 | 1869 |
1856 | |
1857 void | 1870 void |
1858 SECKEY_RemovePublicKeyListNode(SECKEYPublicKeyListNode *node) | 1871 SECKEY_RemovePublicKeyListNode(SECKEYPublicKeyListNode *node) |
1859 { | 1872 { |
1860 PR_ASSERT(node->key); | 1873 PR_ASSERT(node->key); |
1861 SECKEY_DestroyPublicKey(node->key); | 1874 SECKEY_DestroyPublicKey(node->key); |
1862 node->key = NULL; | 1875 node->key = NULL; |
1863 PR_REMOVE_LINK(&node->links); | 1876 PR_REMOVE_LINK(&node->links); |
1864 return; | 1877 return; |
1865 | |
1866 } | 1878 } |
1867 | 1879 |
1868 SECStatus | 1880 SECStatus |
1869 SECKEY_AddPublicKeyToListTail( SECKEYPublicKeyList *list, | 1881 SECKEY_AddPublicKeyToListTail(SECKEYPublicKeyList *list, |
1870 SECKEYPublicKey *key) | 1882 SECKEYPublicKey *key) |
1871 { | 1883 { |
1872 SECKEYPublicKeyListNode *node; | 1884 SECKEYPublicKeyListNode *node; |
1873 | 1885 |
1874 node = (SECKEYPublicKeyListNode *)PORT_ArenaZAlloc(list->arena, | 1886 node = (SECKEYPublicKeyListNode *)PORT_ArenaZAlloc(list->arena, |
1875 sizeof(SECKEYPublicKeyListNode)); | 1887 sizeof(SECKEYPublicKeyLis
tNode)); |
1876 if ( node == NULL ) { | 1888 if (node == NULL) { |
1877 goto loser; | 1889 goto loser; |
1878 } | 1890 } |
1879 | 1891 |
1880 PR_INSERT_BEFORE(&node->links, &list->list); | 1892 PR_INSERT_BEFORE(&node->links, &list->list); |
1881 node->key = key; | 1893 node->key = key; |
1882 return(SECSuccess); | 1894 return (SECSuccess); |
1883 | 1895 |
1884 loser: | 1896 loser: |
1885 return(SECFailure); | 1897 return (SECFailure); |
1886 } | 1898 } |
1887 | 1899 |
1888 #define SECKEY_CacheAttribute(key, attribute) \ | 1900 #define SECKEY_CacheAttribute(key, attribute)
\ |
1889 if (CK_TRUE == PK11_HasAttributeSet(key->pkcs11Slot, key->pkcs11ID, attribut
e, PR_FALSE)) { \ | 1901 if (CK_TRUE == PK11_HasAttributeSet(key->pkcs11Slot, key->pkcs11ID, attribut
e, PR_FALSE)) { \ |
1890 key->staticflags |= SECKEY_##attribute; \ | 1902 key->staticflags |= SECKEY_##attribute;
\ |
1891 } else { \ | 1903 } else {
\ |
1892 key->staticflags &= (~SECKEY_##attribute); \ | 1904 key->staticflags &= (~SECKEY_##attribute);
\ |
1893 } | 1905 } |
1894 | 1906 |
1895 SECStatus | 1907 SECStatus |
1896 SECKEY_CacheStaticFlags(SECKEYPrivateKey* key) | 1908 SECKEY_CacheStaticFlags(SECKEYPrivateKey *key) |
1897 { | 1909 { |
1898 SECStatus rv = SECFailure; | 1910 SECStatus rv = SECFailure; |
1899 if (key && key->pkcs11Slot && key->pkcs11ID) { | 1911 if (key && key->pkcs11Slot && key->pkcs11ID) { |
1900 key->staticflags |= SECKEY_Attributes_Cached; | 1912 key->staticflags |= SECKEY_Attributes_Cached; |
1901 SECKEY_CacheAttribute(key, CKA_PRIVATE); | 1913 SECKEY_CacheAttribute(key, CKA_PRIVATE); |
1902 SECKEY_CacheAttribute(key, CKA_ALWAYS_AUTHENTICATE); | 1914 SECKEY_CacheAttribute(key, CKA_ALWAYS_AUTHENTICATE); |
1903 rv = SECSuccess; | 1915 rv = SECSuccess; |
1904 } | 1916 } |
1905 return rv; | 1917 return rv; |
1906 } | 1918 } |
1907 | 1919 |
1908 SECOidTag | 1920 SECOidTag |
1909 SECKEY_GetECCOid(const SECKEYECParams * params) | 1921 SECKEY_GetECCOid(const SECKEYECParams *params) |
1910 { | 1922 { |
1911 SECItem oid = { siBuffer, NULL, 0}; | 1923 SECItem oid = { siBuffer, NULL, 0 }; |
1912 SECOidData *oidData = NULL; | 1924 SECOidData *oidData = NULL; |
1913 | 1925 |
1914 /* | 1926 /* |
1915 * params->data needs to contain the ASN encoding of an object ID (OID) | 1927 * params->data needs to contain the ASN encoding of an object ID (OID) |
1916 * representing a named curve. Here, we strip away everything | 1928 * representing a named curve. Here, we strip away everything |
1917 * before the actual OID and use the OID to look up a named curve. | 1929 * before the actual OID and use the OID to look up a named curve. |
1918 */ | 1930 */ |
1919 if (params->data[0] != SEC_ASN1_OBJECT_ID) return 0; | 1931 if (params->data[0] != SEC_ASN1_OBJECT_ID) |
| 1932 return 0; |
1920 oid.len = params->len - 2; | 1933 oid.len = params->len - 2; |
1921 oid.data = params->data + 2; | 1934 oid.data = params->data + 2; |
1922 if ((oidData = SECOID_FindOID(&oid)) == NULL) return 0; | 1935 if ((oidData = SECOID_FindOID(&oid)) == NULL) |
| 1936 return 0; |
1923 | 1937 |
1924 return oidData->offset; | 1938 return oidData->offset; |
1925 } | 1939 } |
OLD | NEW |