/src/nss/lib/pk11wrap/pk11akey.c
Line | Count | Source |
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 |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | /* |
5 | | * This file contains functions to manage asymetric keys, (public and |
6 | | * private keys). |
7 | | */ |
8 | | #include <limits.h> |
9 | | #include <stddef.h> |
10 | | |
11 | | #include "seccomon.h" |
12 | | #include "secmod.h" |
13 | | #include "secmodi.h" |
14 | | #include "secmodti.h" |
15 | | #include "pkcs11.h" |
16 | | #include "pkcs11t.h" |
17 | | #include "pk11func.h" |
18 | | #include "cert.h" |
19 | | #include "keyhi.h" |
20 | | #include "keyi.h" |
21 | | #include "secitem.h" |
22 | | #include "secasn1.h" |
23 | | #include "secoid.h" |
24 | | #include "secerr.h" |
25 | | #include "sechash.h" |
26 | | |
27 | | #include "secpkcs5.h" |
28 | | #include "blapit.h" |
29 | | |
30 | | const SECItem * |
31 | | PK11_GetPublicValueFromPublicKey(const SECKEYPublicKey *pubKey) |
32 | 174k | { |
33 | | /* set the ID to the public key so we can find it again */ |
34 | 174k | const SECItem *pubKeyIndex = NULL; |
35 | 174k | switch (pubKey->keyType) { |
36 | 41.1k | case rsaKey: |
37 | 41.1k | case rsaPssKey: |
38 | 41.1k | case rsaOaepKey: |
39 | 41.1k | pubKeyIndex = &pubKey->u.rsa.modulus; |
40 | 41.1k | break; |
41 | 3.79k | case dsaKey: |
42 | 3.79k | pubKeyIndex = &pubKey->u.dsa.publicValue; |
43 | 3.79k | break; |
44 | 6.50k | case dhKey: |
45 | 6.50k | pubKeyIndex = &pubKey->u.dh.publicValue; |
46 | 6.50k | break; |
47 | 877 | case edKey: |
48 | 109k | case ecKey: |
49 | 109k | case ecMontKey: |
50 | 109k | pubKeyIndex = &pubKey->u.ec.publicValue; |
51 | 109k | break; |
52 | 12.9k | case kyberKey: |
53 | 12.9k | pubKeyIndex = &pubKey->u.kyber.publicValue; |
54 | 12.9k | break; |
55 | 158 | case mldsaKey: |
56 | 158 | pubKeyIndex = &pubKey->u.mldsa.publicValue; |
57 | 158 | break; |
58 | | /* explicitly put unsupported keys here. The |
59 | | * compiler will tell you when you need to |
60 | | * add switch statements */ |
61 | 0 | case fortezzaKey: |
62 | 0 | case keaKey: |
63 | 0 | case nullKey: |
64 | 0 | return NULL; |
65 | 174k | } |
66 | 174k | PORT_Assert(pubKeyIndex != NULL); |
67 | | |
68 | 174k | return pubKeyIndex; |
69 | 174k | } |
70 | | |
71 | | KeyType |
72 | | pk11_getKeyTypeFromPKCS11KeyType(CK_KEY_TYPE pk11KeyType) |
73 | 1.31k | { |
74 | 1.31k | KeyType keyType = nullKey; |
75 | 1.31k | switch (pk11KeyType) { |
76 | 1.13k | case CKK_RSA: |
77 | 1.13k | keyType = rsaKey; |
78 | 1.13k | break; |
79 | 0 | case CKK_DSA: |
80 | 0 | keyType = dsaKey; |
81 | 0 | break; |
82 | 0 | case CKK_DH: |
83 | 0 | keyType = dhKey; |
84 | 0 | break; |
85 | 180 | case CKK_EC: |
86 | 180 | keyType = ecKey; |
87 | 180 | break; |
88 | 0 | case CKK_EC_MONTGOMERY: |
89 | 0 | keyType = ecMontKey; |
90 | 0 | break; |
91 | 0 | case CKK_EC_EDWARDS: |
92 | 0 | keyType = edKey; |
93 | 0 | break; |
94 | 0 | case CKK_NSS_ML_KEM: |
95 | 0 | case CKK_ML_KEM: |
96 | 0 | keyType = kyberKey; |
97 | 0 | break; |
98 | 0 | case CKK_ML_DSA: |
99 | 0 | keyType = mldsaKey; |
100 | 0 | break; |
101 | 0 | default: |
102 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
103 | 0 | break; |
104 | 1.31k | } |
105 | 1.31k | return keyType; |
106 | 1.31k | } |
107 | | |
108 | | CK_KEY_TYPE |
109 | | pk11_getPKCS11KeyTypeFromKeyType(KeyType keyType) |
110 | 0 | { |
111 | 0 | CK_KEY_TYPE pk11KeyType = CKK_INVALID_KEY_TYPE; |
112 | 0 | switch (keyType) { |
113 | 0 | case rsaKey: |
114 | 0 | case rsaPssKey: |
115 | 0 | case rsaOaepKey: |
116 | 0 | pk11KeyType = CKK_RSA; |
117 | 0 | break; |
118 | 0 | case dsaKey: |
119 | 0 | pk11KeyType = CKK_DSA; |
120 | 0 | break; |
121 | 0 | case dhKey: |
122 | 0 | pk11KeyType = CKK_DH; |
123 | 0 | break; |
124 | 0 | case ecKey: |
125 | 0 | pk11KeyType = CKK_EC; |
126 | 0 | break; |
127 | 0 | case ecMontKey: |
128 | 0 | pk11KeyType = CKK_EC_MONTGOMERY; |
129 | 0 | break; |
130 | 0 | case edKey: |
131 | 0 | pk11KeyType = CKK_EC_EDWARDS; |
132 | 0 | break; |
133 | 0 | case kyberKey: |
134 | 0 | pk11KeyType = CKK_ML_KEM; |
135 | 0 | break; |
136 | 0 | case mldsaKey: |
137 | 0 | pk11KeyType = CKK_ML_DSA; |
138 | 0 | break; |
139 | | /* explicitly put unsupported keys here. The |
140 | | * compiler will tell you when you need to |
141 | | * add switch statements */ |
142 | 0 | case fortezzaKey: |
143 | 0 | case keaKey: |
144 | 0 | case nullKey: |
145 | 0 | break; |
146 | 0 | } |
147 | 0 | if (pk11KeyType == CKK_INVALID_KEY_TYPE) { |
148 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
149 | 0 | } |
150 | 0 | return pk11KeyType; |
151 | 0 | } |
152 | | |
153 | | SECItem * |
154 | | pk11_MakeIDFromPublicKey(const SECKEYPublicKey *pubKey) |
155 | 92.2k | { |
156 | | /* set the ID to the public key so we can find it again */ |
157 | 92.2k | const SECItem *pubKeyIndex = PK11_GetPublicValueFromPublicKey(pubKey); |
158 | 92.2k | PORT_Assert(pubKeyIndex != NULL); |
159 | | |
160 | 92.2k | return PK11_MakeIDFromPubKey(pubKeyIndex); |
161 | 92.2k | } |
162 | | |
163 | | /* |
164 | | * import a public key into the desired slot |
165 | | * |
166 | | * This function takes a public key structure and creates a public key in a |
167 | | * given slot. If isToken is set, then a persistant public key is created. |
168 | | * |
169 | | * Note: it is possible for this function to return a handle for a key which |
170 | | * is persistant, even if isToken is not set. |
171 | | */ |
172 | | CK_OBJECT_HANDLE |
173 | | PK11_ImportPublicKey(PK11SlotInfo *slot, SECKEYPublicKey *pubKey, |
174 | | PRBool isToken) |
175 | 48.3k | { |
176 | 48.3k | CK_BBOOL cktrue = CK_TRUE; |
177 | 48.3k | CK_BBOOL ckfalse = CK_FALSE; |
178 | 48.3k | CK_OBJECT_CLASS keyClass = CKO_PUBLIC_KEY; |
179 | 48.3k | CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; |
180 | 48.3k | CK_OBJECT_HANDLE objectID; |
181 | 48.3k | CK_ATTRIBUTE theTemplate[11]; |
182 | 48.3k | CK_ATTRIBUTE *signedattr = NULL; |
183 | 48.3k | CK_ATTRIBUTE *attrs = theTemplate; |
184 | 48.3k | CK_NSS_KEM_PARAMETER_SET_TYPE kemParams; |
185 | 48.3k | SECItem *ckaId = NULL; |
186 | 48.3k | SECItem *pubValue = NULL; |
187 | 48.3k | int signedcount = 0; |
188 | 48.3k | unsigned int templateCount = 0; |
189 | 48.3k | SECStatus rv; |
190 | 48.3k | CK_ML_DSA_PARAMETER_SET_TYPE paramSet; |
191 | | |
192 | | /* if we already have an object in the desired slot, use it */ |
193 | 48.3k | if (!isToken && pubKey->pkcs11Slot == slot) { |
194 | 4 | return pubKey->pkcs11ID; |
195 | 4 | } |
196 | | |
197 | | /* free the existing key */ |
198 | 48.3k | if (pubKey->pkcs11Slot != NULL) { |
199 | 0 | PK11SlotInfo *oSlot = pubKey->pkcs11Slot; |
200 | 0 | if (!PK11_IsPermObject(pubKey->pkcs11Slot, pubKey->pkcs11ID)) { |
201 | 0 | PK11_EnterSlotMonitor(oSlot); |
202 | 0 | (void)PK11_GETTAB(oSlot)->C_DestroyObject(oSlot->session, |
203 | 0 | pubKey->pkcs11ID); |
204 | 0 | PK11_ExitSlotMonitor(oSlot); |
205 | 0 | } |
206 | 0 | PK11_FreeSlot(oSlot); |
207 | 0 | pubKey->pkcs11Slot = NULL; |
208 | 0 | } |
209 | 48.3k | PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, sizeof(keyClass)); |
210 | 48.3k | attrs++; |
211 | 48.3k | PK11_SETATTRS(attrs, CKA_KEY_TYPE, &keyType, sizeof(keyType)); |
212 | 48.3k | attrs++; |
213 | 48.3k | PK11_SETATTRS(attrs, CKA_TOKEN, isToken ? &cktrue : &ckfalse, |
214 | 48.3k | sizeof(CK_BBOOL)); |
215 | 48.3k | attrs++; |
216 | 48.3k | if (isToken) { |
217 | 2 | ckaId = pk11_MakeIDFromPublicKey(pubKey); |
218 | 2 | if (ckaId == NULL) { |
219 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
220 | 0 | return CK_INVALID_HANDLE; |
221 | 0 | } |
222 | 2 | PK11_SETATTRS(attrs, CKA_ID, ckaId->data, ckaId->len); |
223 | 2 | attrs++; |
224 | 2 | } |
225 | | |
226 | | /* now import the key */ |
227 | 48.3k | { |
228 | 48.3k | switch (pubKey->keyType) { |
229 | 34.7k | case rsaKey: |
230 | 34.7k | keyType = CKK_RSA; |
231 | 34.7k | PK11_SETATTRS(attrs, CKA_WRAP, &cktrue, sizeof(CK_BBOOL)); |
232 | 34.7k | attrs++; |
233 | 34.7k | PK11_SETATTRS(attrs, CKA_ENCRYPT, &cktrue, |
234 | 34.7k | sizeof(CK_BBOOL)); |
235 | 34.7k | attrs++; |
236 | 34.7k | PK11_SETATTRS(attrs, CKA_VERIFY, &cktrue, sizeof(CK_BBOOL)); |
237 | 34.7k | attrs++; |
238 | 34.7k | signedattr = attrs; |
239 | 34.7k | PK11_SETATTRS(attrs, CKA_MODULUS, pubKey->u.rsa.modulus.data, |
240 | 34.7k | pubKey->u.rsa.modulus.len); |
241 | 34.7k | attrs++; |
242 | 34.7k | PK11_SETATTRS(attrs, CKA_PUBLIC_EXPONENT, |
243 | 34.7k | pubKey->u.rsa.publicExponent.data, |
244 | 34.7k | pubKey->u.rsa.publicExponent.len); |
245 | 34.7k | attrs++; |
246 | 34.7k | break; |
247 | 3.82k | case dsaKey: |
248 | 3.82k | keyType = CKK_DSA; |
249 | 3.82k | PK11_SETATTRS(attrs, CKA_VERIFY, &cktrue, sizeof(CK_BBOOL)); |
250 | 3.82k | attrs++; |
251 | 3.82k | signedattr = attrs; |
252 | 3.82k | PK11_SETATTRS(attrs, CKA_PRIME, pubKey->u.dsa.params.prime.data, |
253 | 3.82k | pubKey->u.dsa.params.prime.len); |
254 | 3.82k | attrs++; |
255 | 3.82k | PK11_SETATTRS(attrs, CKA_SUBPRIME, pubKey->u.dsa.params.subPrime.data, |
256 | 3.82k | pubKey->u.dsa.params.subPrime.len); |
257 | 3.82k | attrs++; |
258 | 3.82k | PK11_SETATTRS(attrs, CKA_BASE, pubKey->u.dsa.params.base.data, |
259 | 3.82k | pubKey->u.dsa.params.base.len); |
260 | 3.82k | attrs++; |
261 | 3.82k | PK11_SETATTRS(attrs, CKA_VALUE, pubKey->u.dsa.publicValue.data, |
262 | 3.82k | pubKey->u.dsa.publicValue.len); |
263 | 3.82k | attrs++; |
264 | 3.82k | break; |
265 | 0 | case fortezzaKey: |
266 | 0 | keyType = CKK_DSA; |
267 | 0 | PK11_SETATTRS(attrs, CKA_VERIFY, &cktrue, sizeof(CK_BBOOL)); |
268 | 0 | attrs++; |
269 | 0 | signedattr = attrs; |
270 | 0 | PK11_SETATTRS(attrs, CKA_PRIME, pubKey->u.fortezza.params.prime.data, |
271 | 0 | pubKey->u.fortezza.params.prime.len); |
272 | 0 | attrs++; |
273 | 0 | PK11_SETATTRS(attrs, CKA_SUBPRIME, |
274 | 0 | pubKey->u.fortezza.params.subPrime.data, |
275 | 0 | pubKey->u.fortezza.params.subPrime.len); |
276 | 0 | attrs++; |
277 | 0 | PK11_SETATTRS(attrs, CKA_BASE, pubKey->u.fortezza.params.base.data, |
278 | 0 | pubKey->u.fortezza.params.base.len); |
279 | 0 | attrs++; |
280 | 0 | PK11_SETATTRS(attrs, CKA_VALUE, pubKey->u.fortezza.DSSKey.data, |
281 | 0 | pubKey->u.fortezza.DSSKey.len); |
282 | 0 | attrs++; |
283 | 0 | break; |
284 | 11 | case dhKey: |
285 | 11 | keyType = CKK_DH; |
286 | 11 | PK11_SETATTRS(attrs, CKA_DERIVE, &cktrue, sizeof(CK_BBOOL)); |
287 | 11 | attrs++; |
288 | 11 | signedattr = attrs; |
289 | 11 | PK11_SETATTRS(attrs, CKA_PRIME, pubKey->u.dh.prime.data, |
290 | 11 | pubKey->u.dh.prime.len); |
291 | 11 | attrs++; |
292 | 11 | PK11_SETATTRS(attrs, CKA_BASE, pubKey->u.dh.base.data, |
293 | 11 | pubKey->u.dh.base.len); |
294 | 11 | attrs++; |
295 | 11 | PK11_SETATTRS(attrs, CKA_VALUE, pubKey->u.dh.publicValue.data, |
296 | 11 | pubKey->u.dh.publicValue.len); |
297 | 11 | attrs++; |
298 | 11 | break; |
299 | 6.64k | case edKey: |
300 | 6.64k | keyType = CKK_EC_EDWARDS; |
301 | 6.64k | PK11_SETATTRS(attrs, CKA_VERIFY, &cktrue, sizeof(CK_BBOOL)); |
302 | 6.64k | attrs++; |
303 | 6.64k | PK11_SETATTRS(attrs, CKA_EC_PARAMS, |
304 | 6.64k | pubKey->u.ec.DEREncodedParams.data, |
305 | 6.64k | pubKey->u.ec.DEREncodedParams.len); |
306 | 6.64k | attrs++; |
307 | 6.64k | PK11_SETATTRS(attrs, CKA_EC_POINT, |
308 | 6.64k | pubKey->u.ec.publicValue.data, |
309 | 6.64k | pubKey->u.ec.publicValue.len); |
310 | 6.64k | attrs++; |
311 | 6.64k | break; |
312 | 0 | case ecMontKey: |
313 | 0 | keyType = CKK_EC_MONTGOMERY; |
314 | 0 | PK11_SETATTRS(attrs, CKA_DERIVE, &cktrue, sizeof(CK_BBOOL)); |
315 | 0 | attrs++; |
316 | 0 | PK11_SETATTRS(attrs, CKA_EC_PARAMS, |
317 | 0 | pubKey->u.ec.DEREncodedParams.data, |
318 | 0 | pubKey->u.ec.DEREncodedParams.len); |
319 | 0 | attrs++; |
320 | 0 | PK11_SETATTRS(attrs, CKA_EC_POINT, |
321 | 0 | pubKey->u.ec.publicValue.data, |
322 | 0 | pubKey->u.ec.publicValue.len); |
323 | 0 | attrs++; |
324 | 0 | break; |
325 | 2.23k | case ecKey: |
326 | 2.23k | keyType = CKK_EC; |
327 | 2.23k | PK11_SETATTRS(attrs, CKA_VERIFY, &cktrue, sizeof(CK_BBOOL)); |
328 | 2.23k | attrs++; |
329 | 2.23k | PK11_SETATTRS(attrs, CKA_DERIVE, &cktrue, sizeof(CK_BBOOL)); |
330 | 2.23k | attrs++; |
331 | 2.23k | PK11_SETATTRS(attrs, CKA_EC_PARAMS, |
332 | 2.23k | pubKey->u.ec.DEREncodedParams.data, |
333 | 2.23k | pubKey->u.ec.DEREncodedParams.len); |
334 | 2.23k | attrs++; |
335 | 2.23k | if (PR_GetEnvSecure("NSS_USE_DECODED_CKA_EC_POINT")) { |
336 | 0 | PK11_SETATTRS(attrs, CKA_EC_POINT, |
337 | 0 | pubKey->u.ec.publicValue.data, |
338 | 0 | pubKey->u.ec.publicValue.len); |
339 | 0 | attrs++; |
340 | 2.23k | } else { |
341 | 2.23k | pubValue = SEC_ASN1EncodeItem(NULL, NULL, |
342 | 2.23k | &pubKey->u.ec.publicValue, |
343 | 2.23k | SEC_ASN1_GET(SEC_OctetStringTemplate)); |
344 | 2.23k | if (pubValue == NULL) { |
345 | 0 | if (ckaId) { |
346 | 0 | SECITEM_FreeItem(ckaId, PR_TRUE); |
347 | 0 | } |
348 | 0 | return CK_INVALID_HANDLE; |
349 | 0 | } |
350 | 2.23k | PK11_SETATTRS(attrs, CKA_EC_POINT, |
351 | 2.23k | pubValue->data, pubValue->len); |
352 | 2.23k | attrs++; |
353 | 2.23k | } |
354 | 2.23k | break; |
355 | 2.23k | case kyberKey: |
356 | 697 | keyType = CKK_ML_KEM; |
357 | 697 | PK11_SETATTRS(attrs, CKA_ENCAPSULATE, &cktrue, sizeof(CK_BBOOL)); |
358 | 697 | attrs++; |
359 | 697 | kemParams = seckey_GetMLKEMPkcs11ParamsByKyberParams( |
360 | 697 | pubKey->u.kyber.params); |
361 | 697 | PK11_SETATTRS(attrs, CKA_PARAMETER_SET, |
362 | 697 | &kemParams, |
363 | 697 | sizeof(CK_NSS_KEM_PARAMETER_SET_TYPE)); |
364 | 697 | attrs++; |
365 | 697 | PK11_SETATTRS(attrs, CKA_VALUE, pubKey->u.kyber.publicValue.data, |
366 | 697 | pubKey->u.kyber.publicValue.len); |
367 | 697 | attrs++; |
368 | 697 | break; |
369 | 155 | case mldsaKey: |
370 | 155 | keyType = CKK_ML_DSA; |
371 | 155 | PK11_SETATTRS(attrs, CKA_VERIFY, &cktrue, sizeof(CK_BBOOL)); |
372 | 155 | attrs++; |
373 | 155 | paramSet = SECKEY_GetMLDSAPkcs11ParamSetByOidTag(pubKey->u.mldsa.paramSet); |
374 | 155 | if (paramSet == CKP_INVALID_ID) { |
375 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
376 | 0 | return CK_INVALID_HANDLE; |
377 | 0 | } |
378 | 155 | PK11_SETATTRS(attrs, CKA_PARAMETER_SET, ¶mSet, |
379 | 155 | sizeof(CK_ML_DSA_PARAMETER_SET_TYPE)); |
380 | 155 | attrs++; |
381 | 155 | PK11_SETATTRS(attrs, CKA_VALUE, pubKey->u.mldsa.publicValue.data, |
382 | 155 | pubKey->u.mldsa.publicValue.len); |
383 | 155 | attrs++; |
384 | 155 | break; |
385 | 0 | default: |
386 | 0 | if (ckaId) { |
387 | 0 | SECITEM_FreeItem(ckaId, PR_TRUE); |
388 | 0 | } |
389 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
390 | 0 | return CK_INVALID_HANDLE; |
391 | 48.3k | } |
392 | 48.3k | templateCount = attrs - theTemplate; |
393 | 48.3k | PORT_Assert(templateCount <= (sizeof(theTemplate) / sizeof(CK_ATTRIBUTE))); |
394 | 48.3k | if (signedattr) { |
395 | 38.5k | signedcount = attrs - signedattr; |
396 | 123k | for (attrs = signedattr; signedcount; attrs++, signedcount--) { |
397 | 84.8k | pk11_SignedToUnsigned(attrs); |
398 | 84.8k | } |
399 | 38.5k | } |
400 | 48.3k | rv = PK11_CreateNewObject(slot, CK_INVALID_HANDLE, theTemplate, |
401 | 48.3k | templateCount, isToken, &objectID); |
402 | 48.3k | if (ckaId) { |
403 | 2 | SECITEM_FreeItem(ckaId, PR_TRUE); |
404 | 2 | } |
405 | 48.3k | if (pubValue) { |
406 | 2.23k | SECITEM_FreeItem(pubValue, PR_TRUE); |
407 | 2.23k | } |
408 | 48.3k | if (rv != SECSuccess) { |
409 | 1.34k | return CK_INVALID_HANDLE; |
410 | 1.34k | } |
411 | 48.3k | } |
412 | | |
413 | 46.9k | pubKey->pkcs11ID = objectID; |
414 | 46.9k | pubKey->pkcs11Slot = PK11_ReferenceSlot(slot); |
415 | | |
416 | 46.9k | return objectID; |
417 | 48.3k | } |
418 | | |
419 | | /* |
420 | | * take an attribute and copy it into a secitem |
421 | | */ |
422 | | static CK_RV |
423 | | pk11_Attr2SecItem(PLArenaPool *arena, const CK_ATTRIBUTE *attr, SECItem *item) |
424 | 123k | { |
425 | 123k | item->data = NULL; |
426 | | |
427 | 123k | (void)SECITEM_AllocItem(arena, item, attr->ulValueLen); |
428 | 123k | if (item->data == NULL) { |
429 | 0 | return CKR_HOST_MEMORY; |
430 | 0 | } |
431 | 123k | PORT_Memcpy(item->data, attr->pValue, item->len); |
432 | 123k | return CKR_OK; |
433 | 123k | } |
434 | | |
435 | | /* |
436 | | * get a curve length from a set of ecParams. |
437 | | * |
438 | | * We need this so we can reliably determine if the ecPoint passed to us |
439 | | * was encoded or not. With out this, for many curves, we would incorrectly |
440 | | * identify an unencoded curve as an encoded curve 1 in 65536 times, and for |
441 | | * a few we would make that same mistake 1 in 32768 times. These are bad |
442 | | * numbers since they are rare enough to pass tests, but common enough to |
443 | | * be tripped over in the field. |
444 | | * |
445 | | * This function will only work for curves we recognized as of March 2009. |
446 | | * The assumption is curves in use after March of 2009 would be supplied by |
447 | | * PKCS #11 modules that already pass the correct encoding to us. |
448 | | * |
449 | | * Point length = (Roundup(curveLenInBits/8)*2+1) |
450 | | */ |
451 | | static int |
452 | | pk11_get_EC_PointLenInBytes(PLArenaPool *arena, const SECItem *ecParams, |
453 | | PRBool *plain) |
454 | 72.8k | { |
455 | 72.8k | SECItem oid; |
456 | 72.8k | SECOidTag tag; |
457 | 72.8k | SECStatus rv; |
458 | | |
459 | | /* decode the OID tag */ |
460 | 72.8k | rv = SEC_QuickDERDecodeItem(arena, &oid, |
461 | 72.8k | SEC_ASN1_GET(SEC_ObjectIDTemplate), ecParams); |
462 | 72.8k | if (rv != SECSuccess) { |
463 | | /* could be explict curves, allow them to work if the |
464 | | * PKCS #11 module support them. If we try to parse the |
465 | | * explicit curve value in the future, we may return -1 here |
466 | | * to indicate an invalid parameter if the explicit curve |
467 | | * decode fails. */ |
468 | 0 | return 0; |
469 | 0 | } |
470 | | |
471 | 72.8k | *plain = PR_FALSE; |
472 | 72.8k | tag = SECOID_FindOIDTag(&oid); |
473 | 72.8k | switch (tag) { |
474 | 0 | case SEC_OID_SECG_EC_SECP112R1: |
475 | 0 | case SEC_OID_SECG_EC_SECP112R2: |
476 | 0 | return 29; /* curve len in bytes = 14 bytes */ |
477 | 0 | case SEC_OID_SECG_EC_SECT113R1: |
478 | 0 | case SEC_OID_SECG_EC_SECT113R2: |
479 | 0 | return 31; /* curve len in bytes = 15 bytes */ |
480 | 0 | case SEC_OID_SECG_EC_SECP128R1: |
481 | 0 | case SEC_OID_SECG_EC_SECP128R2: |
482 | 0 | return 33; /* curve len in bytes = 16 bytes */ |
483 | 0 | case SEC_OID_SECG_EC_SECT131R1: |
484 | 0 | case SEC_OID_SECG_EC_SECT131R2: |
485 | 0 | return 35; /* curve len in bytes = 17 bytes */ |
486 | 0 | case SEC_OID_SECG_EC_SECP160K1: |
487 | 0 | case SEC_OID_SECG_EC_SECP160R1: |
488 | 0 | case SEC_OID_SECG_EC_SECP160R2: |
489 | 0 | return 41; /* curve len in bytes = 20 bytes */ |
490 | 0 | case SEC_OID_SECG_EC_SECT163K1: |
491 | 0 | case SEC_OID_SECG_EC_SECT163R1: |
492 | 0 | case SEC_OID_SECG_EC_SECT163R2: |
493 | 0 | case SEC_OID_ANSIX962_EC_C2PNB163V1: |
494 | 0 | case SEC_OID_ANSIX962_EC_C2PNB163V2: |
495 | 0 | case SEC_OID_ANSIX962_EC_C2PNB163V3: |
496 | 0 | return 43; /* curve len in bytes = 21 bytes */ |
497 | 0 | case SEC_OID_ANSIX962_EC_C2PNB176V1: |
498 | 0 | return 45; /* curve len in bytes = 22 bytes */ |
499 | 0 | case SEC_OID_ANSIX962_EC_C2TNB191V1: |
500 | 0 | case SEC_OID_ANSIX962_EC_C2TNB191V2: |
501 | 0 | case SEC_OID_ANSIX962_EC_C2TNB191V3: |
502 | 0 | case SEC_OID_SECG_EC_SECP192K1: |
503 | 0 | case SEC_OID_ANSIX962_EC_PRIME192V1: |
504 | 0 | case SEC_OID_ANSIX962_EC_PRIME192V2: |
505 | 0 | case SEC_OID_ANSIX962_EC_PRIME192V3: |
506 | 0 | return 49; /*curve len in bytes = 24 bytes */ |
507 | 0 | case SEC_OID_SECG_EC_SECT193R1: |
508 | 0 | case SEC_OID_SECG_EC_SECT193R2: |
509 | 0 | return 51; /*curve len in bytes = 25 bytes */ |
510 | 0 | case SEC_OID_ANSIX962_EC_C2PNB208W1: |
511 | 0 | return 53; /*curve len in bytes = 26 bytes */ |
512 | 0 | case SEC_OID_SECG_EC_SECP224K1: |
513 | 0 | case SEC_OID_SECG_EC_SECP224R1: |
514 | 0 | return 57; /*curve len in bytes = 28 bytes */ |
515 | 0 | case SEC_OID_SECG_EC_SECT233K1: |
516 | 0 | case SEC_OID_SECG_EC_SECT233R1: |
517 | 0 | case SEC_OID_SECG_EC_SECT239K1: |
518 | 0 | case SEC_OID_ANSIX962_EC_PRIME239V1: |
519 | 0 | case SEC_OID_ANSIX962_EC_PRIME239V2: |
520 | 0 | case SEC_OID_ANSIX962_EC_PRIME239V3: |
521 | 0 | case SEC_OID_ANSIX962_EC_C2TNB239V1: |
522 | 0 | case SEC_OID_ANSIX962_EC_C2TNB239V2: |
523 | 0 | case SEC_OID_ANSIX962_EC_C2TNB239V3: |
524 | 0 | return 61; /*curve len in bytes = 30 bytes */ |
525 | 51.0k | case SEC_OID_ANSIX962_EC_PRIME256V1: |
526 | 51.0k | case SEC_OID_SECG_EC_SECP256K1: |
527 | 51.0k | return 65; /*curve len in bytes = 32 bytes */ |
528 | 0 | case SEC_OID_ANSIX962_EC_C2PNB272W1: |
529 | 0 | return 69; /*curve len in bytes = 34 bytes */ |
530 | 0 | case SEC_OID_SECG_EC_SECT283K1: |
531 | 0 | case SEC_OID_SECG_EC_SECT283R1: |
532 | 0 | return 73; /*curve len in bytes = 36 bytes */ |
533 | 0 | case SEC_OID_ANSIX962_EC_C2PNB304W1: |
534 | 0 | return 77; /*curve len in bytes = 38 bytes */ |
535 | 0 | case SEC_OID_ANSIX962_EC_C2TNB359V1: |
536 | 0 | return 91; /*curve len in bytes = 45 bytes */ |
537 | 0 | case SEC_OID_ANSIX962_EC_C2PNB368W1: |
538 | 0 | return 93; /*curve len in bytes = 46 bytes */ |
539 | 3.51k | case SEC_OID_SECG_EC_SECP384R1: |
540 | 3.51k | return 97; /*curve len in bytes = 48 bytes */ |
541 | 0 | case SEC_OID_SECG_EC_SECT409K1: |
542 | 0 | case SEC_OID_SECG_EC_SECT409R1: |
543 | 0 | return 105; /*curve len in bytes = 52 bytes */ |
544 | 0 | case SEC_OID_ANSIX962_EC_C2TNB431R1: |
545 | 0 | return 109; /*curve len in bytes = 54 bytes */ |
546 | 503 | case SEC_OID_SECG_EC_SECP521R1: |
547 | 503 | return 133; /*curve len in bytes = 66 bytes */ |
548 | 0 | case SEC_OID_SECG_EC_SECT571K1: |
549 | 0 | case SEC_OID_SECG_EC_SECT571R1: |
550 | 0 | return 145; /*curve len in bytes = 72 bytes */ |
551 | 3.15k | case SEC_OID_X25519: |
552 | 17.8k | case SEC_OID_CURVE25519: |
553 | 17.8k | case SEC_OID_ED25519_PUBLIC_KEY: |
554 | 17.8k | *plain = PR_TRUE; |
555 | 17.8k | return 32; /* curve len in bytes = 32 bytes (only X) */ |
556 | | /* unknown or unrecognized OIDs. return unknown length */ |
557 | 0 | default: |
558 | 0 | break; |
559 | 72.8k | } |
560 | 0 | return 0; |
561 | 72.8k | } |
562 | | |
563 | | /* |
564 | | * returns the decoded point. In some cases the point may already be decoded. |
565 | | * this function tries to detect those cases and return the point in |
566 | | * publicKeyValue. In other cases it's DER encoded. In those cases the point |
567 | | * is first decoded and returned. Space for the point is allocated out of |
568 | | * the passed in arena. |
569 | | */ |
570 | | static CK_RV |
571 | | pk11_get_Decoded_ECPoint(PLArenaPool *arena, const SECItem *ecParams, |
572 | | const CK_ATTRIBUTE *ecPoint, SECItem *publicKeyValue) |
573 | 72.8k | { |
574 | 72.8k | SECItem encodedPublicValue; |
575 | 72.8k | SECStatus rv; |
576 | 72.8k | int keyLen; |
577 | 72.8k | PRBool plain = PR_FALSE; |
578 | | |
579 | 72.8k | if (ecPoint->ulValueLen == 0) { |
580 | 0 | return CKR_ATTRIBUTE_VALUE_INVALID; |
581 | 0 | } |
582 | | |
583 | | /* |
584 | | * The PKCS #11 spec requires ecPoints to be encoded as a DER OCTET String. |
585 | | * NSS has mistakenly passed unencoded values, and some PKCS #11 vendors |
586 | | * followed that mistake. Now we need to detect which encoding we were |
587 | | * passed in. The task is made more complicated by the fact the the |
588 | | * DER encoding byte (SEC_ASN_OCTET_STRING) is the same as the |
589 | | * EC_POINT_FORM_UNCOMPRESSED byte (0x04), so we can't use that to |
590 | | * determine which curve we are using. |
591 | | */ |
592 | | |
593 | | /* get the expected key length for the passed in curve. |
594 | | * pk11_get_EC_PointLenInBytes only returns valid values for curves |
595 | | * NSS has traditionally recognized. If the curve is not recognized, |
596 | | * it will return '0', and we have to figure out if the key was |
597 | | * encoded or not heuristically. If the ecParams are invalid, it |
598 | | * will return -1 for the keyLen. |
599 | | */ |
600 | 72.8k | keyLen = pk11_get_EC_PointLenInBytes(arena, ecParams, &plain); |
601 | 72.8k | if (keyLen < 0) { |
602 | 0 | return CKR_ATTRIBUTE_VALUE_INVALID; |
603 | 0 | } |
604 | | |
605 | | /* |
606 | | * Some curves are not encoded but we don't have the name here. |
607 | | * Instead, pk11_get_EC_PointLenInBytes returns true plain if this is the |
608 | | * case. |
609 | | */ |
610 | 72.8k | if (plain && ecPoint->ulValueLen == (unsigned int)keyLen) { |
611 | 17.8k | return pk11_Attr2SecItem(arena, ecPoint, publicKeyValue); |
612 | 17.8k | } |
613 | | |
614 | | /* If the point is uncompressed and the lengths match, it |
615 | | * must be an unencoded point */ |
616 | 55.0k | if ((*((char *)ecPoint->pValue) == EC_POINT_FORM_UNCOMPRESSED) && |
617 | 55.0k | (ecPoint->ulValueLen == (unsigned int)keyLen)) { |
618 | 0 | return pk11_Attr2SecItem(arena, ecPoint, publicKeyValue); |
619 | 0 | } |
620 | | |
621 | | /* now assume the key passed to us was encoded and decode it */ |
622 | 55.0k | if (*((char *)ecPoint->pValue) == SEC_ASN1_OCTET_STRING) { |
623 | | /* OK, now let's try to decode it and see if it's valid */ |
624 | 55.0k | encodedPublicValue.data = ecPoint->pValue; |
625 | 55.0k | encodedPublicValue.len = ecPoint->ulValueLen; |
626 | 55.0k | rv = SEC_QuickDERDecodeItem(arena, publicKeyValue, |
627 | 55.0k | SEC_ASN1_GET(SEC_OctetStringTemplate), &encodedPublicValue); |
628 | | |
629 | | /* it coded correctly & we know the key length (and they match) |
630 | | * then we are done, return the results. */ |
631 | 55.0k | if (keyLen && rv == SECSuccess && publicKeyValue->len == (unsigned int)keyLen) { |
632 | 55.0k | return CKR_OK; |
633 | 55.0k | } |
634 | | |
635 | | /* if we know the key length, one of the above tests should have |
636 | | * succeded. If it doesn't the module gave us bad data */ |
637 | 0 | if (keyLen) { |
638 | 0 | return CKR_ATTRIBUTE_VALUE_INVALID; |
639 | 0 | } |
640 | | |
641 | | /* We don't know the key length, so we don't know deterministically |
642 | | * which encoding was used. We now will try to pick the most likely |
643 | | * form that's correct, with a preference for the encoded form if we |
644 | | * can't determine for sure. We do this by checking the key we got |
645 | | * back from SEC_QuickDERDecodeItem for defects. If no defects are |
646 | | * found, we assume the encoded parameter was was passed to us. |
647 | | * our defect tests include: |
648 | | * 1) it didn't decode. |
649 | | * 2) The decode key had an invalid length (must be odd). |
650 | | * 3) The decoded key wasn't an UNCOMPRESSED key. |
651 | | * 4) The decoded key didn't include the entire encoded block |
652 | | * except the DER encoding values. (fixing DER length to one |
653 | | * particular value). |
654 | | */ |
655 | 0 | if ((rv != SECSuccess) || ((publicKeyValue->len & 1) != 1) || |
656 | 0 | (publicKeyValue->data[0] != EC_POINT_FORM_UNCOMPRESSED) || |
657 | 0 | (PORT_Memcmp(&encodedPublicValue.data[encodedPublicValue.len - publicKeyValue->len], |
658 | 0 | publicKeyValue->data, |
659 | 0 | publicKeyValue->len) != 0)) { |
660 | | /* The decoded public key was flawed, the original key must have |
661 | | * already been in decoded form. Do a quick sanity check then |
662 | | * return the original key value. |
663 | | */ |
664 | 0 | if ((encodedPublicValue.len & 1) == 0) { |
665 | 0 | return CKR_ATTRIBUTE_VALUE_INVALID; |
666 | 0 | } |
667 | 0 | return pk11_Attr2SecItem(arena, ecPoint, publicKeyValue); |
668 | 0 | } |
669 | | |
670 | | /* as best we can figure, the passed in key was encoded, and we've |
671 | | * now decoded it. Note: there is a chance this could be wrong if the |
672 | | * following conditions hold: |
673 | | * 1) The first byte or bytes of the X point looks like a valid length |
674 | | * of precisely the right size (2*curveSize -1). this means for curves |
675 | | * less than 512 bits (64 bytes), this will happen 1 in 256 times*. |
676 | | * for curves between 512 and 1024, this will happen 1 in 65,536 times* |
677 | | * for curves between 1024 and 256K this will happen 1 in 16 million* |
678 | | * 2) The length of the 'DER length field' is odd |
679 | | * (making both the encoded and decode |
680 | | * values an odd length. this is true of all curves less than 512, |
681 | | * as well as curves between 1024 and 256K). |
682 | | * 3) The X[length of the 'DER length field'] == 0x04, 1 in 256. |
683 | | * |
684 | | * (* assuming all values are equally likely in the first byte, |
685 | | * This isn't true if the curve length is not a multiple of 8. In these |
686 | | * cases, if the DER length is possible, it's more likely, |
687 | | * if it's not possible, then we have no false decodes). |
688 | | * |
689 | | * For reference here are the odds for the various curves we currently |
690 | | * have support for (and the only curves SSL will negotiate at this |
691 | | * time). NOTE: None of the supported curves will show up here |
692 | | * because we return a valid length for all of these curves. |
693 | | * The only way to get here is to have some application (not SSL) |
694 | | * which supports some unknown curve and have some vendor supplied |
695 | | * PKCS #11 module support that curve. NOTE: in this case, one |
696 | | * presumes that that pkcs #11 module is likely to be using the |
697 | | * correct encodings. |
698 | | * |
699 | | * Prime Curves (GFp): |
700 | | * Bit False Odds of |
701 | | * Size DER Len False Decode Positive |
702 | | * 112 27 1 in 65536 |
703 | | * 128 31 1 in 65536 |
704 | | * 160 39 1 in 65536 |
705 | | * 192 47 1 in 65536 |
706 | | * 224 55 1 in 65536 |
707 | | * 239 59 1 in 32768 (top byte can only be 0-127) |
708 | | * 256 63 1 in 65536 |
709 | | * 521 129,131 0 (decoded value would be even) |
710 | | * |
711 | | * Binary curves (GF2m). |
712 | | * Bit False Odds of |
713 | | * Size DER Len False Decode Positive |
714 | | * 131 33 0 (top byte can only be 0-7) |
715 | | * 163 41 0 (top byte can only be 0-7) |
716 | | * 176 43 1 in 65536 |
717 | | * 191 47 1 in 32768 (top byte can only be 0-127) |
718 | | * 193 49 0 (top byte can only be 0-1) |
719 | | * 208 51 1 in 65536 |
720 | | * 233 59 0 (top byte can only be 0-1) |
721 | | * 239 59 1 in 32768 (top byte can only be 0-127) |
722 | | * 272 67 1 in 65536 |
723 | | * 283 71 0 (top byte can only be 0-7) |
724 | | * 304 75 1 in 65536 |
725 | | * 359 89 1 in 32768 (top byte can only be 0-127) |
726 | | * 368 91 1 in 65536 |
727 | | * 409 103 0 (top byte can only be 0-1) |
728 | | * 431 107 1 in 32768 (top byte can only be 0-127) |
729 | | * 571 129,143 0 (decoded value would be even) |
730 | | * |
731 | | */ |
732 | | |
733 | 0 | return CKR_OK; |
734 | 0 | } |
735 | | |
736 | | /* In theory, we should handle the case where the curve == 0 and |
737 | | * the first byte is EC_POINT_FORM_UNCOMPRESSED, (which would be |
738 | | * handled by doing a santity check on the key length and returning |
739 | | * pk11_Attr2SecItem() to copy the ecPoint to the publicKeyValue). |
740 | | * |
741 | | * This test is unnecessary, however, due to the fact that |
742 | | * EC_POINT_FORM_UNCOMPRESSED == SEC_ASIN1_OCTET_STRING, that case is |
743 | | * handled in the above if. That means if we get here, the initial |
744 | | * byte of our ecPoint value was invalid, so we can safely return. |
745 | | * invalid attribute. |
746 | | */ |
747 | | |
748 | 0 | return CKR_ATTRIBUTE_VALUE_INVALID; |
749 | 55.0k | } |
750 | | |
751 | | /* |
752 | | * extract a public key from a slot and id |
753 | | */ |
754 | | SECKEYPublicKey * |
755 | | PK11_ExtractPublicKey(PK11SlotInfo *slot, KeyType keyType, CK_OBJECT_HANDLE id) |
756 | 92.2k | { |
757 | 92.2k | CK_OBJECT_CLASS keyClass = CKO_PUBLIC_KEY; |
758 | 92.2k | PLArenaPool *arena; |
759 | 92.2k | PLArenaPool *tmp_arena; |
760 | 92.2k | SECKEYPublicKey *pubKey; |
761 | 92.2k | unsigned int templateCount = 0; |
762 | 92.2k | CK_KEY_TYPE pk11KeyType; |
763 | 92.2k | CK_RV crv; |
764 | 92.2k | CK_ATTRIBUTE template[8]; |
765 | 92.2k | CK_ATTRIBUTE *attrs = template; |
766 | 92.2k | CK_ATTRIBUTE *modulus, *exponent, *base, *prime, *subprime, *value; |
767 | 92.2k | CK_ATTRIBUTE *ecparams, *kemParams, *mldsaParams; |
768 | | |
769 | | /* if we didn't know the key type, get it */ |
770 | 92.2k | if (keyType == nullKey) { |
771 | 0 | pk11KeyType = PK11_ReadULongAttribute(slot, id, CKA_KEY_TYPE); |
772 | 0 | if (pk11KeyType == CK_UNAVAILABLE_INFORMATION) { |
773 | 0 | return NULL; |
774 | 0 | } |
775 | 0 | keyType = pk11_getKeyTypeFromPKCS11KeyType(pk11KeyType); |
776 | 0 | if (keyType == nullKey) { |
777 | 0 | return NULL; |
778 | 0 | } |
779 | 0 | } |
780 | | |
781 | | /* now we need to create space for the public key */ |
782 | 92.2k | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
783 | 92.2k | if (arena == NULL) |
784 | 0 | return NULL; |
785 | 92.2k | tmp_arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
786 | 92.2k | if (tmp_arena == NULL) { |
787 | 0 | PORT_FreeArena(arena, PR_FALSE); |
788 | 0 | return NULL; |
789 | 0 | } |
790 | | |
791 | 92.2k | pubKey = (SECKEYPublicKey *) |
792 | 92.2k | PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); |
793 | 92.2k | if (pubKey == NULL) { |
794 | 0 | PORT_FreeArena(arena, PR_FALSE); |
795 | 0 | PORT_FreeArena(tmp_arena, PR_FALSE); |
796 | 0 | return NULL; |
797 | 0 | } |
798 | | |
799 | 92.2k | pubKey->arena = arena; |
800 | 92.2k | pubKey->keyType = keyType; |
801 | 92.2k | pubKey->pkcs11Slot = PK11_ReferenceSlot(slot); |
802 | 92.2k | pubKey->pkcs11ID = id; |
803 | 92.2k | PK11_SETATTRS(attrs, CKA_CLASS, &keyClass, |
804 | 92.2k | sizeof(keyClass)); |
805 | 92.2k | attrs++; |
806 | 92.2k | PK11_SETATTRS(attrs, CKA_KEY_TYPE, &pk11KeyType, |
807 | 92.2k | sizeof(pk11KeyType)); |
808 | 92.2k | attrs++; |
809 | 92.2k | switch (pubKey->keyType) { |
810 | 0 | case rsaKey: |
811 | 0 | modulus = attrs; |
812 | 0 | PK11_SETATTRS(attrs, CKA_MODULUS, NULL, 0); |
813 | 0 | attrs++; |
814 | 0 | exponent = attrs; |
815 | 0 | PK11_SETATTRS(attrs, CKA_PUBLIC_EXPONENT, NULL, 0); |
816 | 0 | attrs++; |
817 | |
|
818 | 0 | templateCount = attrs - template; |
819 | 0 | PR_ASSERT(templateCount <= sizeof(template) / sizeof(CK_ATTRIBUTE)); |
820 | 0 | crv = PK11_GetAttributes(tmp_arena, slot, id, template, templateCount); |
821 | 0 | if (crv != CKR_OK) |
822 | 0 | break; |
823 | | |
824 | 0 | if ((keyClass != CKO_PUBLIC_KEY) || (pk11KeyType != CKK_RSA)) { |
825 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
826 | 0 | break; |
827 | 0 | } |
828 | 0 | crv = pk11_Attr2SecItem(arena, modulus, &pubKey->u.rsa.modulus); |
829 | 0 | if (crv != CKR_OK) |
830 | 0 | break; |
831 | 0 | crv = pk11_Attr2SecItem(arena, exponent, &pubKey->u.rsa.publicExponent); |
832 | 0 | if (crv != CKR_OK) |
833 | 0 | break; |
834 | 0 | break; |
835 | 0 | case dsaKey: |
836 | 0 | prime = attrs; |
837 | 0 | PK11_SETATTRS(attrs, CKA_PRIME, NULL, 0); |
838 | 0 | attrs++; |
839 | 0 | subprime = attrs; |
840 | 0 | PK11_SETATTRS(attrs, CKA_SUBPRIME, NULL, 0); |
841 | 0 | attrs++; |
842 | 0 | base = attrs; |
843 | 0 | PK11_SETATTRS(attrs, CKA_BASE, NULL, 0); |
844 | 0 | attrs++; |
845 | 0 | value = attrs; |
846 | 0 | PK11_SETATTRS(attrs, CKA_VALUE, NULL, 0); |
847 | 0 | attrs++; |
848 | 0 | templateCount = attrs - template; |
849 | 0 | PR_ASSERT(templateCount <= sizeof(template) / sizeof(CK_ATTRIBUTE)); |
850 | 0 | crv = PK11_GetAttributes(tmp_arena, slot, id, template, templateCount); |
851 | 0 | if (crv != CKR_OK) |
852 | 0 | break; |
853 | | |
854 | 0 | if ((keyClass != CKO_PUBLIC_KEY) || (pk11KeyType != CKK_DSA)) { |
855 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
856 | 0 | break; |
857 | 0 | } |
858 | 0 | crv = pk11_Attr2SecItem(arena, prime, &pubKey->u.dsa.params.prime); |
859 | 0 | if (crv != CKR_OK) |
860 | 0 | break; |
861 | 0 | crv = pk11_Attr2SecItem(arena, subprime, &pubKey->u.dsa.params.subPrime); |
862 | 0 | if (crv != CKR_OK) |
863 | 0 | break; |
864 | 0 | crv = pk11_Attr2SecItem(arena, base, &pubKey->u.dsa.params.base); |
865 | 0 | if (crv != CKR_OK) |
866 | 0 | break; |
867 | 0 | crv = pk11_Attr2SecItem(arena, value, &pubKey->u.dsa.publicValue); |
868 | 0 | if (crv != CKR_OK) |
869 | 0 | break; |
870 | 0 | break; |
871 | 6.45k | case dhKey: |
872 | 6.45k | prime = attrs; |
873 | 6.45k | PK11_SETATTRS(attrs, CKA_PRIME, NULL, 0); |
874 | 6.45k | attrs++; |
875 | 6.45k | base = attrs; |
876 | 6.45k | PK11_SETATTRS(attrs, CKA_BASE, NULL, 0); |
877 | 6.45k | attrs++; |
878 | 6.45k | value = attrs; |
879 | 6.45k | PK11_SETATTRS(attrs, CKA_VALUE, NULL, 0); |
880 | 6.45k | attrs++; |
881 | 6.45k | templateCount = attrs - template; |
882 | 6.45k | PR_ASSERT(templateCount <= sizeof(template) / sizeof(CK_ATTRIBUTE)); |
883 | 6.45k | crv = PK11_GetAttributes(tmp_arena, slot, id, template, templateCount); |
884 | 6.45k | if (crv != CKR_OK) |
885 | 0 | break; |
886 | | |
887 | 6.45k | if ((keyClass != CKO_PUBLIC_KEY) || (pk11KeyType != CKK_DH)) { |
888 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
889 | 0 | break; |
890 | 0 | } |
891 | 6.45k | crv = pk11_Attr2SecItem(arena, prime, &pubKey->u.dh.prime); |
892 | 6.45k | if (crv != CKR_OK) |
893 | 0 | break; |
894 | 6.45k | crv = pk11_Attr2SecItem(arena, base, &pubKey->u.dh.base); |
895 | 6.45k | if (crv != CKR_OK) |
896 | 0 | break; |
897 | 6.45k | crv = pk11_Attr2SecItem(arena, value, &pubKey->u.dh.publicValue); |
898 | 6.45k | if (crv != CKR_OK) |
899 | 0 | break; |
900 | 6.45k | break; |
901 | 6.45k | case edKey: |
902 | 72.8k | case ecKey: |
903 | 72.8k | case ecMontKey: |
904 | 72.8k | pubKey->u.ec.size = 0; |
905 | 72.8k | ecparams = attrs; |
906 | 72.8k | PK11_SETATTRS(attrs, CKA_EC_PARAMS, NULL, 0); |
907 | 72.8k | attrs++; |
908 | 72.8k | value = attrs; |
909 | 72.8k | PK11_SETATTRS(attrs, CKA_EC_POINT, NULL, 0); |
910 | 72.8k | attrs++; |
911 | 72.8k | templateCount = attrs - template; |
912 | 72.8k | PR_ASSERT(templateCount <= sizeof(template) / sizeof(CK_ATTRIBUTE)); |
913 | 72.8k | crv = PK11_GetAttributes(arena, slot, id, template, templateCount); |
914 | 72.8k | if (crv != CKR_OK) |
915 | 0 | break; |
916 | | |
917 | 72.8k | if ((keyClass != CKO_PUBLIC_KEY) || |
918 | 72.8k | (pubKey->keyType == ecKey && pk11KeyType != CKK_EC) || |
919 | 72.8k | (pubKey->keyType == edKey && pk11KeyType != CKK_EC_EDWARDS) || |
920 | 72.8k | (pubKey->keyType == ecMontKey && pk11KeyType != CKK_EC_MONTGOMERY)) { |
921 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
922 | 0 | break; |
923 | 0 | } |
924 | | |
925 | 72.8k | crv = pk11_Attr2SecItem(arena, ecparams, |
926 | 72.8k | &pubKey->u.ec.DEREncodedParams); |
927 | 72.8k | if (crv != CKR_OK) |
928 | 0 | break; |
929 | 72.8k | pubKey->u.ec.encoding = ECPoint_Undefined; |
930 | 72.8k | crv = pk11_get_Decoded_ECPoint(arena, |
931 | 72.8k | &pubKey->u.ec.DEREncodedParams, value, |
932 | 72.8k | &pubKey->u.ec.publicValue); |
933 | 72.8k | break; |
934 | 0 | case mldsaKey: |
935 | 0 | value = attrs; |
936 | 0 | PK11_SETATTRS(attrs, CKA_VALUE, NULL, 0); |
937 | 0 | attrs++; |
938 | 0 | mldsaParams = attrs; |
939 | 0 | PK11_SETATTRS(attrs, CKA_PARAMETER_SET, NULL, 0); |
940 | 0 | attrs++; |
941 | 0 | templateCount = attrs - template; |
942 | 0 | PR_ASSERT(templateCount <= sizeof(template) / sizeof(CK_ATTRIBUTE)); |
943 | 0 | crv = PK11_GetAttributes(tmp_arena, slot, id, template, templateCount); |
944 | 0 | if (crv != CKR_OK) |
945 | 0 | break; |
946 | | |
947 | 0 | if ((keyClass != CKO_PUBLIC_KEY) || (pk11KeyType != CKK_ML_DSA)) { |
948 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
949 | 0 | break; |
950 | 0 | } |
951 | | |
952 | 0 | if (mldsaParams->ulValueLen != sizeof(CK_ML_DSA_PARAMETER_SET_TYPE)) { |
953 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
954 | 0 | break; |
955 | 0 | } |
956 | 0 | pubKey->u.mldsa.paramSet = SECKEY_GetMLDSAOidTagByPkcs11ParamSet( |
957 | 0 | *(CK_ML_DSA_PARAMETER_SET_TYPE *)mldsaParams->pValue); |
958 | |
|
959 | 0 | crv = pk11_Attr2SecItem(arena, value, &pubKey->u.mldsa.publicValue); |
960 | 0 | if (crv != CKR_OK) |
961 | 0 | break; |
962 | 0 | break; |
963 | 12.9k | case kyberKey: |
964 | 12.9k | value = attrs; |
965 | 12.9k | PK11_SETATTRS(attrs, CKA_VALUE, NULL, 0); |
966 | 12.9k | attrs++; |
967 | 12.9k | kemParams = attrs; |
968 | 12.9k | PK11_SETATTRS(attrs, CKA_PARAMETER_SET, NULL, 0); |
969 | 12.9k | attrs++; |
970 | 12.9k | templateCount = attrs - template; |
971 | 12.9k | PR_ASSERT(templateCount <= sizeof(template) / sizeof(CK_ATTRIBUTE)); |
972 | | |
973 | 12.9k | crv = PK11_GetAttributes(arena, slot, id, template, templateCount); |
974 | 12.9k | if (crv != CKR_OK) { |
975 | 0 | kemParams->type = CKA_NSS_PARAMETER_SET; |
976 | 0 | crv = PK11_GetAttributes(arena, slot, id, template, |
977 | 0 | templateCount); |
978 | 0 | if (crv != CKR_OK) { |
979 | 0 | break; |
980 | 0 | } |
981 | 0 | } |
982 | | |
983 | 12.9k | if (keyClass != CKO_PUBLIC_KEY) { |
984 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
985 | 0 | break; |
986 | 0 | } |
987 | | |
988 | 12.9k | switch (pk11KeyType) { |
989 | 12.9k | case CKK_NSS_ML_KEM: |
990 | 12.9k | case CKK_ML_KEM: |
991 | 12.9k | break; |
992 | 0 | default: |
993 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
994 | 0 | break; |
995 | 12.9k | } |
996 | 12.9k | if (crv != CKR_OK) { |
997 | 0 | break; |
998 | 0 | } |
999 | | |
1000 | 12.9k | if (kemParams->ulValueLen != sizeof(CK_NSS_KEM_PARAMETER_SET_TYPE)) { |
1001 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
1002 | 0 | break; |
1003 | 0 | } |
1004 | 12.9k | CK_NSS_KEM_PARAMETER_SET_TYPE *pPK11Params = kemParams->pValue; |
1005 | 12.9k | pubKey->u.kyber.params = seckey_GetKyberParamsByPkcs11ParamSet( |
1006 | 12.9k | *pPK11Params); |
1007 | 12.9k | if (pubKey->u.kyber.params == params_kyber_invalid) { |
1008 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
1009 | 0 | break; |
1010 | 0 | } |
1011 | 12.9k | crv = pk11_Attr2SecItem(arena, value, &pubKey->u.kyber.publicValue); |
1012 | 12.9k | break; |
1013 | 0 | case fortezzaKey: |
1014 | 0 | case nullKey: |
1015 | 0 | default: |
1016 | 0 | crv = CKR_OBJECT_HANDLE_INVALID; |
1017 | 0 | break; |
1018 | 92.2k | } |
1019 | | |
1020 | 92.2k | PORT_FreeArena(tmp_arena, PR_FALSE); |
1021 | | |
1022 | 92.2k | if (crv != CKR_OK) { |
1023 | 0 | PORT_FreeArena(arena, PR_FALSE); |
1024 | 0 | PK11_FreeSlot(slot); |
1025 | 0 | PORT_SetError(PK11_MapError(crv)); |
1026 | 0 | return NULL; |
1027 | 0 | } |
1028 | | |
1029 | 92.2k | return pubKey; |
1030 | 92.2k | } |
1031 | | |
1032 | | /* |
1033 | | * Build a Private Key structure from raw PKCS #11 information. |
1034 | | */ |
1035 | | SECKEYPrivateKey * |
1036 | | pk11_MakePrivKey(PK11SlotInfo *slot, KeyType keyType, |
1037 | | PRBool isOwner, CK_OBJECT_HANDLE privID, void *wincx) |
1038 | 96.3k | { |
1039 | 96.3k | PLArenaPool *arena; |
1040 | 96.3k | SECKEYPrivateKey *privKey; |
1041 | 96.3k | PRBool isPrivate; |
1042 | 96.3k | SECStatus rv; |
1043 | 96.3k | PRBool isTemp = isOwner; |
1044 | | |
1045 | | /* don't know? look it up */ |
1046 | 96.3k | if (keyType == nullKey) { |
1047 | 1.31k | CK_KEY_TYPE pk11Type = CKK_RSA; |
1048 | | |
1049 | 1.31k | pk11Type = PK11_ReadULongAttribute(slot, privID, CKA_KEY_TYPE); |
1050 | 1.31k | isTemp = (PRBool)!PK11_HasAttributeSet(slot, privID, CKA_TOKEN, PR_FALSE); |
1051 | | /* we can never own a perm key, make sure we don't set owner if |
1052 | | * the key isn't a temp key */ |
1053 | 1.31k | isOwner = isOwner && isTemp; |
1054 | 1.31k | keyType = pk11_getKeyTypeFromPKCS11KeyType(pk11Type); |
1055 | 1.31k | if (keyType == nullKey) { |
1056 | 0 | return NULL; |
1057 | 0 | } |
1058 | 1.31k | } |
1059 | | |
1060 | | /* if the key is private, make sure we are authenticated to the |
1061 | | * token before we try to use it */ |
1062 | 96.3k | isPrivate = (PRBool)PK11_HasAttributeSet(slot, privID, CKA_PRIVATE, PR_FALSE); |
1063 | 96.3k | if (isPrivate) { |
1064 | 4.47k | rv = PK11_Authenticate(slot, PR_TRUE, wincx); |
1065 | 4.47k | if (rv != SECSuccess) { |
1066 | 0 | return NULL; |
1067 | 0 | } |
1068 | 4.47k | } |
1069 | | |
1070 | | /* now we need to create space for the private key */ |
1071 | 96.3k | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1072 | 96.3k | if (arena == NULL) |
1073 | 0 | return NULL; |
1074 | | |
1075 | 96.3k | privKey = (SECKEYPrivateKey *) |
1076 | 96.3k | PORT_ArenaZAlloc(arena, sizeof(SECKEYPrivateKey)); |
1077 | 96.3k | if (privKey == NULL) { |
1078 | 0 | PORT_FreeArena(arena, PR_FALSE); |
1079 | 0 | return NULL; |
1080 | 0 | } |
1081 | | |
1082 | 96.3k | privKey->arena = arena; |
1083 | 96.3k | privKey->keyType = keyType; |
1084 | 96.3k | privKey->pkcs11Slot = PK11_ReferenceSlot(slot); |
1085 | 96.3k | privKey->pkcs11ID = privID; |
1086 | 96.3k | privKey->pkcs11IsTemp = 0; |
1087 | 96.3k | SECKEYPRIVATEKEY_SET_TEMP(privKey, isTemp); |
1088 | 96.3k | SECKEYPRIVATEKEY_SET_OWNED(privKey, isOwner); |
1089 | 96.3k | privKey->wincx = wincx; |
1090 | | |
1091 | 96.3k | return privKey; |
1092 | 96.3k | } |
1093 | | |
1094 | | PK11SlotInfo * |
1095 | | PK11_GetSlotFromPrivateKey(SECKEYPrivateKey *key) |
1096 | 48.5k | { |
1097 | 48.5k | PK11SlotInfo *slot = key->pkcs11Slot; |
1098 | 48.5k | slot = PK11_ReferenceSlot(slot); |
1099 | 48.5k | return slot; |
1100 | 48.5k | } |
1101 | | |
1102 | | /* |
1103 | | * Get the modulus length for raw parsing |
1104 | | */ |
1105 | | int |
1106 | | PK11_GetPrivateModulusLen(SECKEYPrivateKey *key) |
1107 | 27.8k | { |
1108 | 27.8k | CK_ATTRIBUTE theTemplate = { CKA_MODULUS, NULL, 0 }; |
1109 | 27.8k | PK11SlotInfo *slot = key->pkcs11Slot; |
1110 | 27.8k | CK_RV crv; |
1111 | 27.8k | int length; |
1112 | | |
1113 | 27.8k | switch (key->keyType) { |
1114 | 25.1k | case rsaKey: |
1115 | 25.1k | crv = PK11_GetAttributes(NULL, slot, key->pkcs11ID, &theTemplate, 1); |
1116 | 25.1k | if (crv != CKR_OK) { |
1117 | 0 | PORT_SetError(PK11_MapError(crv)); |
1118 | 0 | return -1; |
1119 | 0 | } |
1120 | 25.1k | if (theTemplate.pValue == NULL) { |
1121 | 0 | PORT_SetError(PK11_MapError(CKR_ATTRIBUTE_VALUE_INVALID)); |
1122 | 0 | return -1; |
1123 | 0 | } |
1124 | 25.1k | length = theTemplate.ulValueLen; |
1125 | 25.1k | if (*(unsigned char *)theTemplate.pValue == 0) { |
1126 | 0 | length--; |
1127 | 0 | } |
1128 | 25.1k | PORT_Free(theTemplate.pValue); |
1129 | 25.1k | return (int)length; |
1130 | 2.67k | default: |
1131 | 2.67k | break; |
1132 | 27.8k | } |
1133 | 2.67k | if (theTemplate.pValue != NULL) |
1134 | 0 | PORT_Free(theTemplate.pValue); |
1135 | 2.67k | PORT_SetError(SEC_ERROR_INVALID_KEY); |
1136 | 2.67k | return -1; |
1137 | 27.8k | } |
1138 | | |
1139 | | /* |
1140 | | * take a private key in one pkcs11 module and load it into another: |
1141 | | * NOTE: the source private key is a rare animal... it can't be sensitive. |
1142 | | * This is used to do a key gen using one pkcs11 module and storing the |
1143 | | * result into another. |
1144 | | */ |
1145 | | static SECKEYPrivateKey * |
1146 | | pk11_loadPrivKeyWithFlags(PK11SlotInfo *slot, SECKEYPrivateKey *privKey, |
1147 | | SECKEYPublicKey *pubKey, PK11AttrFlags attrFlags) |
1148 | 0 | { |
1149 | 0 | CK_ATTRIBUTE privTemplate[] = { |
1150 | | /* class must be first */ |
1151 | 0 | { CKA_CLASS, NULL, 0 }, |
1152 | 0 | { CKA_KEY_TYPE, NULL, 0 }, |
1153 | 0 | { CKA_ID, NULL, 0 }, |
1154 | | /* RSA - only place the RSA attributes below |
1155 | | * they will be replaced for other |
1156 | | * key types. We assume no key has more attributes |
1157 | | * than RSA. |
1158 | | */ |
1159 | 0 | { CKA_MODULUS, NULL, 0 }, |
1160 | 0 | { CKA_PRIVATE_EXPONENT, NULL, 0 }, |
1161 | 0 | { CKA_PUBLIC_EXPONENT, NULL, 0 }, |
1162 | 0 | { CKA_PRIME_1, NULL, 0 }, |
1163 | 0 | { CKA_PRIME_2, NULL, 0 }, |
1164 | 0 | { CKA_EXPONENT_1, NULL, 0 }, |
1165 | 0 | { CKA_EXPONENT_2, NULL, 0 }, |
1166 | 0 | { CKA_COEFFICIENT, NULL, 0 }, |
1167 | 0 | { CKA_DECRYPT, NULL, 0 }, |
1168 | 0 | { CKA_SIGN, NULL, 0 }, |
1169 | 0 | { CKA_SIGN_RECOVER, NULL, 0 }, |
1170 | 0 | { CKA_UNWRAP, NULL, 0 }, |
1171 | | /* reserve space for the attributes that may be |
1172 | | * specified in attrFlags */ |
1173 | 0 | { CKA_TOKEN, NULL, 0 }, |
1174 | 0 | { CKA_PRIVATE, NULL, 0 }, |
1175 | 0 | { CKA_MODIFIABLE, NULL, 0 }, |
1176 | 0 | { CKA_SENSITIVE, NULL, 0 }, |
1177 | 0 | { CKA_EXTRACTABLE, NULL, 0 }, |
1178 | 0 | #define NUM_RESERVED_ATTRS 5 /* number of reserved attributes above if \ |
1179 | | * new non_rsa attributes are added, you \ |
1180 | | * must adjust this value */ |
1181 | 0 | }; |
1182 | 0 | CK_BBOOL cktrue = CK_TRUE; |
1183 | 0 | CK_BBOOL ckfalse = CK_FALSE; |
1184 | 0 | CK_ATTRIBUTE *attrs = NULL, *ap; |
1185 | 0 | const int templateSize = sizeof(privTemplate) / sizeof(privTemplate[0]); |
1186 | 0 | PLArenaPool *arena; |
1187 | 0 | CK_OBJECT_HANDLE objectID; |
1188 | 0 | int i, count = 0; |
1189 | 0 | int extra_count = 0; /* count of signed attributes */ |
1190 | 0 | CK_RV crv; |
1191 | 0 | SECStatus rv; |
1192 | 0 | PRBool token = ((attrFlags & PK11_ATTR_TOKEN) != 0); |
1193 | |
|
1194 | 0 | if (pk11_BadAttrFlags(attrFlags)) { |
1195 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1196 | 0 | return NULL; |
1197 | 0 | } |
1198 | | |
1199 | 0 | for (i = 0; i < templateSize; i++) { |
1200 | 0 | if (privTemplate[i].type == CKA_MODULUS) { |
1201 | 0 | attrs = &privTemplate[i]; |
1202 | 0 | count = i; |
1203 | 0 | break; |
1204 | 0 | } |
1205 | 0 | } |
1206 | 0 | PORT_Assert(attrs != NULL); |
1207 | 0 | if (attrs == NULL) { |
1208 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1209 | 0 | return NULL; |
1210 | 0 | } |
1211 | | |
1212 | 0 | ap = attrs; |
1213 | |
|
1214 | 0 | switch (privKey->keyType) { |
1215 | 0 | case rsaKey: |
1216 | 0 | count = templateSize - NUM_RESERVED_ATTRS; |
1217 | 0 | extra_count = count - (attrs - privTemplate); |
1218 | 0 | break; |
1219 | 0 | case dsaKey: |
1220 | 0 | ap->type = CKA_PRIME; |
1221 | 0 | ap++; |
1222 | 0 | count++; |
1223 | 0 | extra_count++; |
1224 | 0 | ap->type = CKA_SUBPRIME; |
1225 | 0 | ap++; |
1226 | 0 | count++; |
1227 | 0 | extra_count++; |
1228 | 0 | ap->type = CKA_BASE; |
1229 | 0 | ap++; |
1230 | 0 | count++; |
1231 | 0 | extra_count++; |
1232 | 0 | ap->type = CKA_VALUE; |
1233 | 0 | ap++; |
1234 | 0 | count++; |
1235 | 0 | extra_count++; |
1236 | 0 | ap->type = CKA_SIGN; |
1237 | 0 | ap++; |
1238 | 0 | count++; |
1239 | 0 | extra_count++; |
1240 | 0 | break; |
1241 | 0 | case dhKey: |
1242 | 0 | ap->type = CKA_PRIME; |
1243 | 0 | ap++; |
1244 | 0 | count++; |
1245 | 0 | extra_count++; |
1246 | 0 | ap->type = CKA_BASE; |
1247 | 0 | ap++; |
1248 | 0 | count++; |
1249 | 0 | extra_count++; |
1250 | 0 | ap->type = CKA_VALUE; |
1251 | 0 | ap++; |
1252 | 0 | count++; |
1253 | 0 | extra_count++; |
1254 | 0 | ap->type = CKA_DERIVE; |
1255 | 0 | ap++; |
1256 | 0 | count++; |
1257 | 0 | extra_count++; |
1258 | 0 | break; |
1259 | 0 | case mldsaKey: |
1260 | 0 | ap->type = CKA_PARAMETER_SET; |
1261 | 0 | ap++; |
1262 | 0 | count++; |
1263 | 0 | ap->type = CKA_SEED; |
1264 | 0 | ap++; |
1265 | 0 | count++; |
1266 | 0 | ap->type = CKA_VALUE; |
1267 | 0 | ap++; |
1268 | 0 | count++; |
1269 | 0 | ap->type = CKA_SIGN; |
1270 | 0 | ap++; |
1271 | 0 | count++; |
1272 | 0 | break; |
1273 | 0 | case kyberKey: |
1274 | 0 | ap->type = CKA_PARAMETER_SET; |
1275 | 0 | ap++; |
1276 | 0 | count++; |
1277 | 0 | ap->type = CKA_SEED; |
1278 | 0 | ap++; |
1279 | 0 | count++; |
1280 | 0 | ap->type = CKA_VALUE; |
1281 | 0 | ap++; |
1282 | 0 | count++; |
1283 | 0 | ap->type = CKA_DECAPSULATE; |
1284 | 0 | ap++; |
1285 | 0 | count++; |
1286 | 0 | break; |
1287 | 0 | case ecKey: |
1288 | 0 | case edKey: |
1289 | 0 | case ecMontKey: |
1290 | 0 | ap->type = CKA_EC_PARAMS; |
1291 | 0 | ap++; |
1292 | 0 | count++; |
1293 | 0 | ap->type = CKA_VALUE; |
1294 | 0 | ap++; |
1295 | 0 | count++; |
1296 | 0 | if (privKey->keyType == ecKey) { |
1297 | 0 | ap->type = CKA_DERIVE; |
1298 | 0 | ap++; |
1299 | 0 | count++; |
1300 | 0 | } |
1301 | |
|
1302 | 0 | ap->type = CKA_SIGN; |
1303 | 0 | ap++; |
1304 | 0 | count++; |
1305 | 0 | break; |
1306 | 0 | default: |
1307 | 0 | count = 0; |
1308 | 0 | extra_count = 0; |
1309 | 0 | break; |
1310 | 0 | } |
1311 | | |
1312 | 0 | if (count == 0) { |
1313 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1314 | 0 | return NULL; |
1315 | 0 | } |
1316 | | |
1317 | | /* This would be a programming error */ |
1318 | 0 | PR_ASSERT(count <= (templateSize - NUM_RESERVED_ATTRS)); |
1319 | |
|
1320 | 0 | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1321 | 0 | if (arena == NULL) |
1322 | 0 | return NULL; |
1323 | | /* |
1324 | | * read out the old attributes. |
1325 | | */ |
1326 | 0 | crv = PK11_GetAttributes(arena, privKey->pkcs11Slot, privKey->pkcs11ID, |
1327 | 0 | privTemplate, count); |
1328 | 0 | if (crv != CKR_OK) { |
1329 | 0 | PORT_SetError(PK11_MapError(crv)); |
1330 | 0 | PORT_FreeArena(arena, PR_TRUE); |
1331 | 0 | return NULL; |
1332 | 0 | } |
1333 | | |
1334 | | /* Set token, private, modifiable, sensitive, and extractable */ |
1335 | 0 | count += pk11_AttrFlagsToAttributes(attrFlags, &privTemplate[count], |
1336 | 0 | &cktrue, &ckfalse); |
1337 | | |
1338 | | /* Not everyone can handle zero padded key values, give |
1339 | | * them the raw data as unsigned. Where the values are encoded or |
1340 | | * zero-preserving per-RFC5915. NOTE Most new algorithms are |
1341 | | * OCTET strings, not signed integers, so this conversion |
1342 | | * isn't necessary for them. */ |
1343 | 0 | if (extra_count) { |
1344 | 0 | for (ap = attrs; extra_count; ap++, extra_count--) { |
1345 | 0 | pk11_SignedToUnsigned(ap); |
1346 | 0 | } |
1347 | 0 | } |
1348 | | |
1349 | | /* now Store the puppies */ |
1350 | 0 | rv = PK11_CreateNewObject(slot, CK_INVALID_HANDLE, privTemplate, |
1351 | 0 | count, token, &objectID); |
1352 | 0 | PORT_FreeArena(arena, PR_TRUE); |
1353 | 0 | if (rv != SECSuccess) { |
1354 | 0 | return NULL; |
1355 | 0 | } |
1356 | | |
1357 | | /* try loading the public key. PK11_ImportPublicKey leaves the new object |
1358 | | * attached to pubKey, which is what lets SECKEY_DestroyPublicKey clean it |
1359 | | * up: it destroys a session object and skips a permanent one. Detaching it |
1360 | | * here would strand a session object on the slot with nothing left to |
1361 | | * destroy it. */ |
1362 | 0 | if (pubKey) { |
1363 | 0 | PK11_ImportPublicKey(slot, pubKey, token); |
1364 | 0 | } |
1365 | | |
1366 | | /* build new key structure */ |
1367 | 0 | return pk11_MakePrivKey(slot, privKey->keyType, !token, |
1368 | 0 | objectID, privKey->wincx); |
1369 | 0 | } |
1370 | | |
1371 | | static SECKEYPrivateKey * |
1372 | | pk11_loadPrivKey(PK11SlotInfo *slot, SECKEYPrivateKey *privKey, |
1373 | | SECKEYPublicKey *pubKey, PRBool token, PRBool sensitive) |
1374 | 0 | { |
1375 | 0 | PK11AttrFlags attrFlags = 0; |
1376 | 0 | if (token) { |
1377 | 0 | attrFlags |= (PK11_ATTR_TOKEN | PK11_ATTR_PRIVATE); |
1378 | 0 | } else { |
1379 | 0 | attrFlags |= (PK11_ATTR_SESSION | PK11_ATTR_PUBLIC); |
1380 | 0 | } |
1381 | 0 | if (sensitive) { |
1382 | 0 | attrFlags |= PK11_ATTR_SENSITIVE; |
1383 | 0 | } else { |
1384 | 0 | attrFlags |= PK11_ATTR_INSENSITIVE; |
1385 | 0 | } |
1386 | 0 | return pk11_loadPrivKeyWithFlags(slot, privKey, pubKey, attrFlags); |
1387 | 0 | } |
1388 | | |
1389 | | /* |
1390 | | * export this for PSM |
1391 | | */ |
1392 | | SECKEYPrivateKey * |
1393 | | PK11_LoadPrivKey(PK11SlotInfo *slot, SECKEYPrivateKey *privKey, |
1394 | | SECKEYPublicKey *pubKey, PRBool token, PRBool sensitive) |
1395 | 0 | { |
1396 | 0 | return pk11_loadPrivKey(slot, privKey, pubKey, token, sensitive); |
1397 | 0 | } |
1398 | | |
1399 | | /* |
1400 | | * Use the token to generate a key pair. |
1401 | | */ |
1402 | | SECKEYPrivateKey * |
1403 | | PK11_GenerateKeyPairWithOpFlags(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, |
1404 | | void *param, SECKEYPublicKey **pubKey, PK11AttrFlags attrFlags, |
1405 | | CK_FLAGS opFlags, CK_FLAGS opFlagsMask, void *wincx) |
1406 | 93.3k | { |
1407 | | /* we have to use these native types because when we call PKCS 11 modules |
1408 | | * we have to make sure that we are using the correct sizes for all the |
1409 | | * parameters. */ |
1410 | 93.3k | CK_BBOOL ckfalse = CK_FALSE; |
1411 | 93.3k | CK_BBOOL cktrue = CK_TRUE; |
1412 | 93.3k | CK_ULONG modulusBits; |
1413 | 93.3k | CK_BYTE publicExponent[4]; |
1414 | 93.3k | CK_ULONG pubTemplateSize = 0; |
1415 | 93.3k | CK_ATTRIBUTE privTemplate[] = { |
1416 | 93.3k | { CKA_SENSITIVE, NULL, 0 }, |
1417 | 93.3k | { CKA_TOKEN, NULL, 0 }, |
1418 | 93.3k | { CKA_PRIVATE, NULL, 0 }, |
1419 | 93.3k | { CKA_DERIVE, NULL, 0 }, |
1420 | 93.3k | { CKA_UNWRAP, NULL, 0 }, |
1421 | 93.3k | { CKA_SIGN, NULL, 0 }, |
1422 | 93.3k | { CKA_DECRYPT, NULL, 0 }, |
1423 | 93.3k | { CKA_EXTRACTABLE, NULL, 0 }, |
1424 | 93.3k | { CKA_MODIFIABLE, NULL, 0 }, |
1425 | 93.3k | { CKA_DECAPSULATE, NULL, 0 }, |
1426 | 93.3k | }; |
1427 | 93.3k | CK_ATTRIBUTE rsaPubTemplate[] = { |
1428 | 93.3k | { CKA_MODULUS_BITS, NULL, 0 }, |
1429 | 93.3k | { CKA_PUBLIC_EXPONENT, NULL, 0 }, |
1430 | 93.3k | { CKA_TOKEN, NULL, 0 }, |
1431 | 93.3k | { CKA_DERIVE, NULL, 0 }, |
1432 | 93.3k | { CKA_WRAP, NULL, 0 }, |
1433 | 93.3k | { CKA_VERIFY, NULL, 0 }, |
1434 | 93.3k | { CKA_VERIFY_RECOVER, NULL, 0 }, |
1435 | 93.3k | { CKA_ENCRYPT, NULL, 0 }, |
1436 | 93.3k | { CKA_MODIFIABLE, NULL, 0 }, |
1437 | 93.3k | { CKA_ENCAPSULATE, NULL, 0 }, |
1438 | 93.3k | }; |
1439 | 93.3k | CK_ATTRIBUTE dsaPubTemplate[] = { |
1440 | 93.3k | { CKA_PRIME, NULL, 0 }, |
1441 | 93.3k | { CKA_SUBPRIME, NULL, 0 }, |
1442 | 93.3k | { CKA_BASE, NULL, 0 }, |
1443 | 93.3k | { CKA_TOKEN, NULL, 0 }, |
1444 | 93.3k | { CKA_DERIVE, NULL, 0 }, |
1445 | 93.3k | { CKA_WRAP, NULL, 0 }, |
1446 | 93.3k | { CKA_VERIFY, NULL, 0 }, |
1447 | 93.3k | { CKA_VERIFY_RECOVER, NULL, 0 }, |
1448 | 93.3k | { CKA_ENCRYPT, NULL, 0 }, |
1449 | 93.3k | { CKA_MODIFIABLE, NULL, 0 }, |
1450 | 93.3k | { CKA_ENCAPSULATE, NULL, 0 }, |
1451 | 93.3k | }; |
1452 | 93.3k | CK_ATTRIBUTE dhPubTemplate[] = { |
1453 | 93.3k | { CKA_PRIME, NULL, 0 }, |
1454 | 93.3k | { CKA_BASE, NULL, 0 }, |
1455 | 93.3k | { CKA_TOKEN, NULL, 0 }, |
1456 | 93.3k | { CKA_DERIVE, NULL, 0 }, |
1457 | 93.3k | { CKA_WRAP, NULL, 0 }, |
1458 | 93.3k | { CKA_VERIFY, NULL, 0 }, |
1459 | 93.3k | { CKA_VERIFY_RECOVER, NULL, 0 }, |
1460 | 93.3k | { CKA_ENCRYPT, NULL, 0 }, |
1461 | 93.3k | { CKA_MODIFIABLE, NULL, 0 }, |
1462 | 93.3k | { CKA_ENCAPSULATE, NULL, 0 }, |
1463 | 93.3k | }; |
1464 | 93.3k | CK_ATTRIBUTE ecPubTemplate[] = { |
1465 | 93.3k | { CKA_EC_PARAMS, NULL, 0 }, |
1466 | 93.3k | { CKA_TOKEN, NULL, 0 }, |
1467 | 93.3k | { CKA_DERIVE, NULL, 0 }, |
1468 | 93.3k | { CKA_WRAP, NULL, 0 }, |
1469 | 93.3k | { CKA_VERIFY, NULL, 0 }, |
1470 | 93.3k | { CKA_VERIFY_RECOVER, NULL, 0 }, |
1471 | 93.3k | { CKA_ENCRYPT, NULL, 0 }, |
1472 | 93.3k | { CKA_MODIFIABLE, NULL, 0 }, |
1473 | 93.3k | { CKA_ENCAPSULATE, NULL, 0 }, |
1474 | 93.3k | }; |
1475 | 93.3k | SECKEYECParams *ecParams; |
1476 | | |
1477 | 93.3k | CK_ATTRIBUTE mlDsaPubTemplate[] = { |
1478 | 93.3k | { CKA_PARAMETER_SET, NULL, 0 }, |
1479 | 93.3k | { CKA_TOKEN, NULL, 0 }, |
1480 | 93.3k | { CKA_DERIVE, NULL, 0 }, |
1481 | 93.3k | { CKA_WRAP, NULL, 0 }, |
1482 | 93.3k | { CKA_VERIFY, NULL, 0 }, |
1483 | 93.3k | { CKA_VERIFY_RECOVER, NULL, 0 }, |
1484 | 93.3k | { CKA_ENCRYPT, NULL, 0 }, |
1485 | 93.3k | { CKA_MODIFIABLE, NULL, 0 }, |
1486 | 93.3k | { CKA_ENCAPSULATE, NULL, 0 }, |
1487 | 93.3k | }; |
1488 | 93.3k | CK_ATTRIBUTE kyberPubTemplate[] = { |
1489 | 93.3k | { CKA_PARAMETER_SET, NULL, 0 }, |
1490 | 93.3k | { CKA_TOKEN, NULL, 0 }, |
1491 | 93.3k | { CKA_DERIVE, NULL, 0 }, |
1492 | 93.3k | { CKA_WRAP, NULL, 0 }, |
1493 | 93.3k | { CKA_VERIFY, NULL, 0 }, |
1494 | 93.3k | { CKA_VERIFY_RECOVER, NULL, 0 }, |
1495 | 93.3k | { CKA_ENCRYPT, NULL, 0 }, |
1496 | 93.3k | { CKA_MODIFIABLE, NULL, 0 }, |
1497 | 93.3k | { CKA_ENCAPSULATE, NULL, 0 }, |
1498 | 93.3k | }; |
1499 | | |
1500 | | /*CK_ULONG key_size = 0;*/ |
1501 | 93.3k | CK_ATTRIBUTE *pubTemplate; |
1502 | 93.3k | int privCount = 0; |
1503 | 93.3k | int pubCount = 0; |
1504 | 93.3k | PK11RSAGenParams *rsaParams; |
1505 | 93.3k | SECKEYPQGParams *dsaParams; |
1506 | 93.3k | SECKEYDHParams *dhParams; |
1507 | 93.3k | CK_NSS_KEM_PARAMETER_SET_TYPE *kemParams; |
1508 | 93.3k | CK_ML_DSA_PARAMETER_SET_TYPE *mldsaParams; |
1509 | 93.3k | CK_MECHANISM mechanism; |
1510 | 93.3k | CK_MECHANISM test_mech; |
1511 | 93.3k | CK_MECHANISM test_mech2; |
1512 | 93.3k | CK_SESSION_HANDLE session_handle; |
1513 | 93.3k | CK_RV crv; |
1514 | 93.3k | CK_OBJECT_HANDLE privID, pubID; |
1515 | 93.3k | SECKEYPrivateKey *privKey; |
1516 | 93.3k | KeyType keyType; |
1517 | 93.3k | PRBool restore; |
1518 | 93.3k | int peCount, i; |
1519 | 93.3k | CK_ATTRIBUTE *attrs; |
1520 | 93.3k | CK_ATTRIBUTE *privattrs; |
1521 | 93.3k | CK_ATTRIBUTE setTemplate; |
1522 | 93.3k | CK_MECHANISM_INFO mechanism_info; |
1523 | 93.3k | CK_OBJECT_CLASS keyClass; |
1524 | 93.3k | SECItem *cka_id; |
1525 | 93.3k | PRBool haslock = PR_FALSE; |
1526 | 93.3k | PRBool pubIsToken = PR_FALSE; |
1527 | 93.3k | PRBool token = ((attrFlags & PK11_ATTR_TOKEN) != 0); |
1528 | | /* subset of attrFlags applicable to the public key */ |
1529 | 93.3k | PK11AttrFlags pubKeyAttrFlags = attrFlags & |
1530 | 93.3k | (PK11_ATTR_TOKEN | PK11_ATTR_SESSION | PK11_ATTR_MODIFIABLE | PK11_ATTR_UNMODIFIABLE); |
1531 | | |
1532 | 93.3k | if (pk11_BadAttrFlags(attrFlags)) { |
1533 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1534 | 0 | return NULL; |
1535 | 0 | } |
1536 | | |
1537 | 93.3k | if (!param) { |
1538 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1539 | 0 | return NULL; |
1540 | 0 | } |
1541 | | |
1542 | | /* |
1543 | | * The opFlags and opFlagMask parameters allow us to control the |
1544 | | * settings of the key usage attributes (CKA_ENCRYPT and friends). |
1545 | | * opFlagMask is set to one if the flag is specified in opFlags and |
1546 | | * zero if it is to take on a default value calculated by |
1547 | | * PK11_GenerateKeyPairWithOpFlags. |
1548 | | * opFlags specifies the actual value of the flag 1 or 0. |
1549 | | * Bits not corresponding to one bits in opFlagMask should be zero. |
1550 | | */ |
1551 | | |
1552 | | /* if we are trying to turn on a flag, it better be in the mask */ |
1553 | 93.3k | PORT_Assert((opFlags & ~opFlagsMask) == 0); |
1554 | 93.3k | opFlags &= opFlagsMask; |
1555 | | |
1556 | 93.3k | PORT_Assert(slot != NULL); |
1557 | 93.3k | if (slot == NULL) { |
1558 | 0 | PORT_SetError(SEC_ERROR_NO_MODULE); |
1559 | 0 | return NULL; |
1560 | 0 | } |
1561 | | |
1562 | | /* if our slot really doesn't do this mechanism, Generate the key |
1563 | | * in our internal token and write it out */ |
1564 | 93.3k | if (!PK11_DoesMechanism(slot, type)) { |
1565 | 0 | PK11SlotInfo *int_slot = PK11_GetInternalSlot(); |
1566 | | |
1567 | | /* don't loop forever looking for a slot */ |
1568 | 0 | if (slot == int_slot) { |
1569 | 0 | PK11_FreeSlot(int_slot); |
1570 | | /* if we don't have support in softoken, then the mechanism |
1571 | | * just isnt' supported */ |
1572 | 0 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
1573 | 0 | return NULL; |
1574 | 0 | } |
1575 | | |
1576 | | /* if there isn't a suitable slot, then we can't do the keygen */ |
1577 | 0 | if (int_slot == NULL) { |
1578 | 0 | PORT_SetError(SEC_ERROR_NO_MODULE); |
1579 | 0 | return NULL; |
1580 | 0 | } |
1581 | | |
1582 | | /* generate the temporary key to load */ |
1583 | 0 | privKey = PK11_GenerateKeyPair(int_slot, type, param, pubKey, PR_FALSE, |
1584 | 0 | PR_FALSE, wincx); |
1585 | 0 | PK11_FreeSlot(int_slot); |
1586 | | |
1587 | | /* if successful, load the temp key into the new token */ |
1588 | 0 | if (privKey != NULL) { |
1589 | 0 | SECKEYPrivateKey *newPrivKey = pk11_loadPrivKeyWithFlags(slot, |
1590 | 0 | privKey, *pubKey, attrFlags); |
1591 | 0 | SECKEY_DestroyPrivateKey(privKey); |
1592 | 0 | if (newPrivKey == NULL) { |
1593 | 0 | SECKEY_DestroyPublicKey(*pubKey); |
1594 | 0 | *pubKey = NULL; |
1595 | 0 | } |
1596 | 0 | return newPrivKey; |
1597 | 0 | } |
1598 | 0 | return NULL; |
1599 | 0 | } |
1600 | | |
1601 | 93.3k | mechanism.mechanism = type; |
1602 | 93.3k | mechanism.pParameter = NULL; |
1603 | 93.3k | mechanism.ulParameterLen = 0; |
1604 | 93.3k | test_mech.pParameter = NULL; |
1605 | 93.3k | test_mech.ulParameterLen = 0; |
1606 | 93.3k | test_mech2.mechanism = CKM_INVALID_MECHANISM; |
1607 | 93.3k | test_mech2.pParameter = NULL; |
1608 | 93.3k | test_mech2.ulParameterLen = 0; |
1609 | | |
1610 | | /* set up the private key template */ |
1611 | 93.3k | privattrs = privTemplate; |
1612 | 93.3k | privattrs += pk11_AttrFlagsToAttributes(attrFlags, privattrs, |
1613 | 93.3k | &cktrue, &ckfalse); |
1614 | | |
1615 | | /* set up the mechanism specific info */ |
1616 | 93.3k | switch (type) { |
1617 | 0 | case CKM_RSA_PKCS_KEY_PAIR_GEN: |
1618 | 0 | case CKM_RSA_X9_31_KEY_PAIR_GEN: |
1619 | 0 | rsaParams = (PK11RSAGenParams *)param; |
1620 | 0 | if (rsaParams->pe == 0 || rsaParams->pe > PR_UINT32_MAX) { |
1621 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
1622 | 0 | return NULL; |
1623 | 0 | } |
1624 | 0 | modulusBits = rsaParams->keySizeInBits; |
1625 | 0 | peCount = 0; |
1626 | | |
1627 | | /* convert pe to a PKCS #11 string */ |
1628 | 0 | for (i = 0; i < 4; i++) { |
1629 | 0 | if (peCount || (rsaParams->pe & |
1630 | 0 | ((unsigned long)0xff000000L >> (i * 8)))) { |
1631 | 0 | publicExponent[peCount] = |
1632 | 0 | (CK_BYTE)((rsaParams->pe >> (3 - i) * 8) & 0xff); |
1633 | 0 | peCount++; |
1634 | 0 | } |
1635 | 0 | } |
1636 | 0 | PORT_Assert(peCount != 0); |
1637 | 0 | attrs = rsaPubTemplate; |
1638 | 0 | PK11_SETATTRS(attrs, CKA_MODULUS_BITS, |
1639 | 0 | &modulusBits, sizeof(modulusBits)); |
1640 | 0 | attrs++; |
1641 | 0 | PK11_SETATTRS(attrs, CKA_PUBLIC_EXPONENT, |
1642 | 0 | publicExponent, peCount); |
1643 | 0 | attrs++; |
1644 | 0 | pubTemplate = rsaPubTemplate; |
1645 | 0 | pubTemplateSize = PR_ARRAY_SIZE(rsaPubTemplate); |
1646 | 0 | keyType = rsaKey; |
1647 | 0 | test_mech.mechanism = CKM_RSA_PKCS; |
1648 | 0 | break; |
1649 | 0 | case CKM_DSA_KEY_PAIR_GEN: |
1650 | 0 | dsaParams = (SECKEYPQGParams *)param; |
1651 | 0 | attrs = dsaPubTemplate; |
1652 | 0 | PK11_SETATTRS(attrs, CKA_PRIME, dsaParams->prime.data, |
1653 | 0 | dsaParams->prime.len); |
1654 | 0 | attrs++; |
1655 | 0 | PK11_SETATTRS(attrs, CKA_SUBPRIME, dsaParams->subPrime.data, |
1656 | 0 | dsaParams->subPrime.len); |
1657 | 0 | attrs++; |
1658 | 0 | PK11_SETATTRS(attrs, CKA_BASE, dsaParams->base.data, |
1659 | 0 | dsaParams->base.len); |
1660 | 0 | attrs++; |
1661 | 0 | pubTemplate = dsaPubTemplate; |
1662 | 0 | pubTemplateSize = PR_ARRAY_SIZE(dsaPubTemplate); |
1663 | 0 | keyType = dsaKey; |
1664 | 0 | test_mech.mechanism = CKM_DSA; |
1665 | 0 | break; |
1666 | 6.84k | case CKM_DH_PKCS_KEY_PAIR_GEN: |
1667 | 6.84k | dhParams = (SECKEYDHParams *)param; |
1668 | 6.84k | attrs = dhPubTemplate; |
1669 | 6.84k | PK11_SETATTRS(attrs, CKA_PRIME, dhParams->prime.data, |
1670 | 6.84k | dhParams->prime.len); |
1671 | 6.84k | attrs++; |
1672 | 6.84k | PK11_SETATTRS(attrs, CKA_BASE, dhParams->base.data, |
1673 | 6.84k | dhParams->base.len); |
1674 | 6.84k | attrs++; |
1675 | 6.84k | pubTemplate = dhPubTemplate; |
1676 | 6.84k | pubTemplateSize = PR_ARRAY_SIZE(dhPubTemplate); |
1677 | 6.84k | keyType = dhKey; |
1678 | 6.84k | test_mech.mechanism = CKM_DH_PKCS_DERIVE; |
1679 | 6.84k | break; |
1680 | 3.25k | case CKM_EC_KEY_PAIR_GEN: |
1681 | 73.5k | case CKM_NSS_ECDHE_NO_PAIRWISE_CHECK_KEY_PAIR_GEN: |
1682 | 73.5k | ecParams = (SECKEYECParams *)param; |
1683 | 73.5k | attrs = ecPubTemplate; |
1684 | 73.5k | PK11_SETATTRS(attrs, CKA_EC_PARAMS, ecParams->data, |
1685 | 73.5k | ecParams->len); |
1686 | 73.5k | attrs++; |
1687 | 73.5k | pubTemplate = ecPubTemplate; |
1688 | 73.5k | pubTemplateSize = PR_ARRAY_SIZE(ecPubTemplate); |
1689 | 73.5k | keyType = ecKey; |
1690 | | /* |
1691 | | * ECC supports 2 different mechanism types (unlike RSA, which |
1692 | | * supports different usages with the same mechanism). |
1693 | | * We may need to query both mechanism types and or the results |
1694 | | * together -- but we only do that if either the user has |
1695 | | * requested both usages, or not specified any usages. |
1696 | | */ |
1697 | 73.5k | if ((opFlags & (CKF_SIGN | CKF_DERIVE)) == (CKF_SIGN | CKF_DERIVE)) { |
1698 | | /* We've explicitly turned on both flags, use both mechanism */ |
1699 | 0 | test_mech.mechanism = CKM_ECDH1_DERIVE; |
1700 | 0 | test_mech2.mechanism = CKM_ECDSA; |
1701 | 73.5k | } else if (opFlags & CKF_SIGN) { |
1702 | | /* just do signing */ |
1703 | 0 | test_mech.mechanism = CKM_ECDSA; |
1704 | 73.5k | } else if (opFlags & CKF_DERIVE) { |
1705 | | /* just do ECDH */ |
1706 | 70.4k | test_mech.mechanism = CKM_ECDH1_DERIVE; |
1707 | 70.4k | } else { |
1708 | | /* neither was specified default to both */ |
1709 | 3.18k | test_mech.mechanism = CKM_ECDH1_DERIVE; |
1710 | 3.18k | test_mech2.mechanism = CKM_ECDSA; |
1711 | 3.18k | } |
1712 | 73.5k | break; |
1713 | 0 | case CKM_NSS_KYBER_KEY_PAIR_GEN: |
1714 | 12.9k | case CKM_NSS_ML_KEM_KEY_PAIR_GEN: |
1715 | 12.9k | case CKM_ML_KEM_KEY_PAIR_GEN: |
1716 | 12.9k | kemParams = (CK_NSS_KEM_PARAMETER_SET_TYPE *)param; |
1717 | 12.9k | attrs = kyberPubTemplate; |
1718 | 12.9k | PK11_SETATTRS(attrs, CKA_PARAMETER_SET, |
1719 | 12.9k | kemParams, |
1720 | 12.9k | sizeof(CK_NSS_KEM_PARAMETER_SET_TYPE)); |
1721 | 12.9k | attrs++; |
1722 | 12.9k | pubTemplate = kyberPubTemplate; |
1723 | 12.9k | pubTemplateSize = PR_ARRAY_SIZE(kyberPubTemplate); |
1724 | 12.9k | keyType = kyberKey; |
1725 | 12.9k | test_mech.mechanism = CKM_ML_KEM; |
1726 | 12.9k | break; |
1727 | 0 | case CKM_EC_MONTGOMERY_KEY_PAIR_GEN: |
1728 | 0 | ecParams = (SECKEYECParams *)param; |
1729 | 0 | attrs = ecPubTemplate; |
1730 | 0 | PK11_SETATTRS(attrs, CKA_EC_PARAMS, ecParams->data, |
1731 | 0 | ecParams->len); |
1732 | 0 | attrs++; |
1733 | 0 | pubTemplate = ecPubTemplate; |
1734 | 0 | pubTemplateSize = PR_ARRAY_SIZE(ecPubTemplate); |
1735 | 0 | keyType = ecMontKey; |
1736 | 0 | test_mech.mechanism = CKM_ECDH1_DERIVE; |
1737 | 0 | break; |
1738 | 0 | case CKM_EC_EDWARDS_KEY_PAIR_GEN: |
1739 | 0 | ecParams = (SECKEYECParams *)param; |
1740 | 0 | attrs = ecPubTemplate; |
1741 | 0 | PK11_SETATTRS(attrs, CKA_EC_PARAMS, ecParams->data, |
1742 | 0 | ecParams->len); |
1743 | 0 | attrs++; |
1744 | 0 | pubTemplate = ecPubTemplate; |
1745 | 0 | pubTemplateSize = PR_ARRAY_SIZE(ecPubTemplate); |
1746 | 0 | keyType = edKey; |
1747 | 0 | test_mech.mechanism = CKM_EDDSA; |
1748 | 0 | break; |
1749 | 0 | case CKM_ML_DSA_KEY_PAIR_GEN: |
1750 | 0 | mldsaParams = (CK_ML_DSA_PARAMETER_SET_TYPE *)param; |
1751 | 0 | attrs = mlDsaPubTemplate; |
1752 | 0 | PK11_SETATTRS(attrs, |
1753 | 0 | CKA_PARAMETER_SET, |
1754 | 0 | mldsaParams, |
1755 | 0 | sizeof(CK_ML_DSA_PARAMETER_SET_TYPE)); |
1756 | 0 | attrs++; |
1757 | 0 | pubTemplate = mlDsaPubTemplate; |
1758 | 0 | pubTemplateSize = PR_ARRAY_SIZE(mlDsaPubTemplate); |
1759 | 0 | keyType = mldsaKey; |
1760 | 0 | test_mech.mechanism = CKM_ML_DSA; |
1761 | 0 | break; |
1762 | 0 | default: |
1763 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
1764 | 0 | return NULL; |
1765 | 93.3k | } |
1766 | | |
1767 | | /* now query the slot to find out how "good" a key we can generate */ |
1768 | 93.3k | if (!slot->isThreadSafe) |
1769 | 0 | PK11_EnterSlotMonitor(slot); |
1770 | 93.3k | crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, |
1771 | 93.3k | test_mech.mechanism, &mechanism_info); |
1772 | | /* |
1773 | | * EC keys are used in multiple different types of mechanism, if we |
1774 | | * are using dual use keys, we need to query the second mechanism |
1775 | | * as well. |
1776 | | */ |
1777 | 93.3k | if (test_mech2.mechanism != CKM_INVALID_MECHANISM) { |
1778 | 3.18k | CK_MECHANISM_INFO mechanism_info2; |
1779 | 3.18k | CK_RV crv2; |
1780 | | |
1781 | 3.18k | if (crv != CKR_OK) { |
1782 | | /* the first failed, make sure there is no trash in the |
1783 | | * mechanism flags when we or it below */ |
1784 | 0 | mechanism_info.flags = 0; |
1785 | 0 | } |
1786 | 3.18k | crv2 = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, |
1787 | 3.18k | test_mech2.mechanism, &mechanism_info2); |
1788 | 3.18k | if (crv2 == CKR_OK) { |
1789 | 3.18k | crv = CKR_OK; /* succeed if either mechnaism info succeeds */ |
1790 | | /* combine the 2 sets of mechnanism flags */ |
1791 | 3.18k | mechanism_info.flags |= mechanism_info2.flags; |
1792 | 3.18k | } |
1793 | 3.18k | } |
1794 | 93.3k | if (!slot->isThreadSafe) |
1795 | 0 | PK11_ExitSlotMonitor(slot); |
1796 | 93.3k | if ((crv != CKR_OK) || (mechanism_info.flags == 0)) { |
1797 | | /* must be old module... guess what it should be... */ |
1798 | 0 | switch (test_mech.mechanism) { |
1799 | 0 | case CKM_RSA_PKCS: |
1800 | 0 | mechanism_info.flags = (CKF_SIGN | CKF_DECRYPT | |
1801 | 0 | CKF_WRAP | CKF_VERIFY_RECOVER | CKF_ENCRYPT | CKF_WRAP); |
1802 | 0 | break; |
1803 | 0 | case CKM_DSA: |
1804 | 0 | case CKM_ECDSA: |
1805 | 0 | case CKM_EDDSA: |
1806 | 0 | case CKM_ML_DSA: |
1807 | 0 | mechanism_info.flags = CKF_SIGN | CKF_VERIFY; |
1808 | 0 | break; |
1809 | 0 | case CKM_DH_PKCS_DERIVE: |
1810 | 0 | mechanism_info.flags = CKF_DERIVE; |
1811 | 0 | break; |
1812 | 0 | case CKM_ECDH1_DERIVE: |
1813 | 0 | mechanism_info.flags = CKF_DERIVE; |
1814 | 0 | if (test_mech2.mechanism == CKM_ECDSA) { |
1815 | 0 | mechanism_info.flags |= CKF_SIGN | CKF_VERIFY; |
1816 | 0 | } |
1817 | 0 | break; |
1818 | 0 | case CKM_NSS_KYBER: |
1819 | 0 | case CKM_NSS_ML_KEM: |
1820 | 0 | case CKM_ML_KEM: |
1821 | 0 | mechanism_info.flags = CKF_ENCAPSULATE | CKF_DECAPSULATE; |
1822 | 0 | break; |
1823 | 0 | default: |
1824 | 0 | break; |
1825 | 0 | } |
1826 | 0 | } |
1827 | | /* now adjust our flags according to the user's key usage passed to us */ |
1828 | 93.3k | mechanism_info.flags = (mechanism_info.flags & (~opFlagsMask)) | opFlags; |
1829 | | /* set the public key attributes */ |
1830 | 93.3k | attrs += pk11_AttrFlagsToAttributes(pubKeyAttrFlags, attrs, |
1831 | 93.3k | &cktrue, &ckfalse); |
1832 | 93.3k | PK11_SETATTRS(attrs, CKA_DERIVE, |
1833 | 93.3k | mechanism_info.flags & CKF_DERIVE ? &cktrue : &ckfalse, |
1834 | 93.3k | sizeof(CK_BBOOL)); |
1835 | 93.3k | attrs++; |
1836 | 93.3k | PK11_SETATTRS(attrs, CKA_WRAP, |
1837 | 93.3k | mechanism_info.flags & CKF_WRAP ? &cktrue : &ckfalse, |
1838 | 93.3k | sizeof(CK_BBOOL)); |
1839 | 93.3k | attrs++; |
1840 | 93.3k | PK11_SETATTRS(attrs, CKA_VERIFY, |
1841 | 93.3k | mechanism_info.flags & CKF_VERIFY ? &cktrue : &ckfalse, |
1842 | 93.3k | sizeof(CK_BBOOL)); |
1843 | 93.3k | attrs++; |
1844 | 93.3k | PK11_SETATTRS(attrs, CKA_VERIFY_RECOVER, |
1845 | 93.3k | mechanism_info.flags & CKF_VERIFY_RECOVER ? &cktrue : &ckfalse, |
1846 | 93.3k | sizeof(CK_BBOOL)); |
1847 | 93.3k | attrs++; |
1848 | 93.3k | PK11_SETATTRS(attrs, CKA_ENCRYPT, |
1849 | 93.3k | mechanism_info.flags & CKF_ENCRYPT ? &cktrue : &ckfalse, |
1850 | 93.3k | sizeof(CK_BBOOL)); |
1851 | 93.3k | attrs++; |
1852 | | /* only set encapsulate if it's requested and shows up in the mechanism |
1853 | | * list, that way we don't confuse pre-3.2 PKCS #11 modules */ |
1854 | 93.3k | if (mechanism_info.flags & CKF_ENCAPSULATE) { |
1855 | 12.9k | PK11_SETATTRS(attrs, CKA_ENCAPSULATE, &cktrue, sizeof(CK_BBOOL)); |
1856 | 12.9k | attrs++; |
1857 | 12.9k | } |
1858 | | |
1859 | | /* set the private key attributes */ |
1860 | 93.3k | PK11_SETATTRS(privattrs, CKA_DERIVE, |
1861 | 93.3k | mechanism_info.flags & CKF_DERIVE ? &cktrue : &ckfalse, |
1862 | 93.3k | sizeof(CK_BBOOL)); |
1863 | 93.3k | privattrs++; |
1864 | 93.3k | PK11_SETATTRS(privattrs, CKA_UNWRAP, |
1865 | 93.3k | mechanism_info.flags & CKF_UNWRAP ? &cktrue : &ckfalse, |
1866 | 93.3k | sizeof(CK_BBOOL)); |
1867 | 93.3k | privattrs++; |
1868 | 93.3k | PK11_SETATTRS(privattrs, CKA_SIGN, |
1869 | 93.3k | mechanism_info.flags & CKF_SIGN ? &cktrue : &ckfalse, |
1870 | 93.3k | sizeof(CK_BBOOL)); |
1871 | 93.3k | privattrs++; |
1872 | 93.3k | PK11_SETATTRS(privattrs, CKA_DECRYPT, |
1873 | 93.3k | mechanism_info.flags & CKF_DECRYPT ? &cktrue : &ckfalse, |
1874 | 93.3k | sizeof(CK_BBOOL)); |
1875 | 93.3k | privattrs++; |
1876 | | /* only set it if the attribute exists so we don't confuse old modules */ |
1877 | 93.3k | if (mechanism_info.flags & CKF_DECAPSULATE) { |
1878 | 12.9k | PK11_SETATTRS(privattrs, CKA_DECAPSULATE, &cktrue, sizeof(CK_BBOOL)); |
1879 | 12.9k | privattrs++; |
1880 | 12.9k | } |
1881 | | |
1882 | 93.3k | if (token) { |
1883 | 0 | session_handle = PK11_GetRWSession(slot); |
1884 | 0 | haslock = PK11_RWSessionHasLock(slot, session_handle); |
1885 | 0 | restore = PR_TRUE; |
1886 | 93.3k | } else { |
1887 | 93.3k | session_handle = slot->session; |
1888 | 93.3k | if (session_handle != CK_INVALID_HANDLE) |
1889 | 93.3k | PK11_EnterSlotMonitor(slot); |
1890 | 93.3k | restore = PR_FALSE; |
1891 | 93.3k | haslock = PR_TRUE; |
1892 | 93.3k | } |
1893 | | |
1894 | 93.3k | if (session_handle == CK_INVALID_HANDLE) { |
1895 | 0 | PORT_SetError(SEC_ERROR_BAD_DATA); |
1896 | 0 | return NULL; |
1897 | 0 | } |
1898 | 93.3k | privCount = privattrs - privTemplate; |
1899 | 93.3k | PORT_Assert(privCount <= PR_ARRAY_SIZE(privTemplate)); |
1900 | 93.3k | if (privCount > PR_ARRAY_SIZE(privTemplate)) { |
1901 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1902 | 0 | return NULL; |
1903 | 0 | } |
1904 | 93.3k | pubCount = attrs - pubTemplate; |
1905 | 93.3k | PORT_Assert(pubCount <= pubTemplateSize); |
1906 | 93.3k | if (pubCount > pubTemplateSize) { |
1907 | 0 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
1908 | 0 | return NULL; |
1909 | 0 | } |
1910 | 93.3k | crv = PK11_GETTAB(slot)->C_GenerateKeyPair(session_handle, &mechanism, |
1911 | 93.3k | pubTemplate, pubCount, privTemplate, privCount, &pubID, &privID); |
1912 | | |
1913 | 93.3k | if (crv != CKR_OK) { |
1914 | 1.11k | if (restore) { |
1915 | 0 | PK11_RestoreROSession(slot, session_handle); |
1916 | 0 | } else |
1917 | 1.11k | PK11_ExitSlotMonitor(slot); |
1918 | 1.11k | PORT_SetError(PK11_MapError(crv)); |
1919 | 1.11k | return NULL; |
1920 | 1.11k | } |
1921 | | /* This locking code is dangerous and needs to be more thought |
1922 | | * out... the real problem is that we're holding the mutex open this long |
1923 | | */ |
1924 | 92.2k | if (haslock) { |
1925 | 92.2k | PK11_ExitSlotMonitor(slot); |
1926 | 92.2k | } |
1927 | | |
1928 | | /* swap around the ID's for older PKCS #11 modules */ |
1929 | 92.2k | keyClass = PK11_ReadULongAttribute(slot, pubID, CKA_CLASS); |
1930 | 92.2k | if (keyClass != CKO_PUBLIC_KEY) { |
1931 | 0 | CK_OBJECT_HANDLE tmp = pubID; |
1932 | 0 | pubID = privID; |
1933 | 0 | privID = tmp; |
1934 | 0 | } |
1935 | | |
1936 | 92.2k | *pubKey = PK11_ExtractPublicKey(slot, keyType, pubID); |
1937 | 92.2k | if (*pubKey == NULL) { |
1938 | 0 | if (restore) { |
1939 | | /* we may have to restore the mutex so it get's exited properly |
1940 | | * in RestoreROSession */ |
1941 | 0 | if (haslock) |
1942 | 0 | PK11_EnterSlotMonitor(slot); |
1943 | 0 | PK11_RestoreROSession(slot, session_handle); |
1944 | 0 | } |
1945 | 0 | PK11_DestroyObject(slot, pubID); |
1946 | 0 | PK11_DestroyObject(slot, privID); |
1947 | 0 | return NULL; |
1948 | 0 | } |
1949 | | |
1950 | | /* set the ID to the public key so we can find it again */ |
1951 | 92.2k | cka_id = pk11_MakeIDFromPublicKey(*pubKey); |
1952 | 92.2k | pubIsToken = (PRBool)PK11_HasAttributeSet(slot, pubID, CKA_TOKEN, PR_FALSE); |
1953 | | |
1954 | 92.2k | PK11_SETATTRS(&setTemplate, CKA_ID, cka_id->data, cka_id->len); |
1955 | | |
1956 | 92.2k | if (haslock) { |
1957 | 92.2k | PK11_EnterSlotMonitor(slot); |
1958 | 92.2k | } |
1959 | 92.2k | crv = PK11_GETTAB(slot)->C_SetAttributeValue(session_handle, privID, |
1960 | 92.2k | &setTemplate, 1); |
1961 | | |
1962 | 92.2k | if (crv == CKR_OK && pubIsToken) { |
1963 | 0 | crv = PK11_GETTAB(slot)->C_SetAttributeValue(session_handle, pubID, |
1964 | 0 | &setTemplate, 1); |
1965 | 0 | } |
1966 | | |
1967 | 92.2k | if (restore) { |
1968 | 0 | PK11_RestoreROSession(slot, session_handle); |
1969 | 92.2k | } else { |
1970 | 92.2k | PK11_ExitSlotMonitor(slot); |
1971 | 92.2k | } |
1972 | 92.2k | SECITEM_FreeItem(cka_id, PR_TRUE); |
1973 | | |
1974 | 92.2k | if (crv != CKR_OK) { |
1975 | | /* SECKEY_DestroyPublicKey frees *pubKey and destroys the underlying |
1976 | | * PKCS #11 object, but only for session objects; a freshly generated |
1977 | | * token object must be removed explicitly. */ |
1978 | 0 | SECKEY_DestroyPublicKey(*pubKey); |
1979 | 0 | if (pubIsToken) { |
1980 | 0 | PK11_DestroyObject(slot, pubID); |
1981 | 0 | } |
1982 | 0 | PK11_DestroyObject(slot, privID); |
1983 | 0 | PORT_SetError(PK11_MapError(crv)); |
1984 | 0 | *pubKey = NULL; |
1985 | 0 | return NULL; |
1986 | 0 | } |
1987 | | |
1988 | 92.2k | privKey = pk11_MakePrivKey(slot, keyType, !token, privID, wincx); |
1989 | 92.2k | if (privKey == NULL) { |
1990 | 0 | SECKEY_DestroyPublicKey(*pubKey); |
1991 | 0 | if (pubIsToken) { |
1992 | 0 | PK11_DestroyObject(slot, pubID); |
1993 | 0 | } |
1994 | 0 | PK11_DestroyObject(slot, privID); |
1995 | 0 | *pubKey = NULL; |
1996 | 0 | return NULL; |
1997 | 0 | } |
1998 | | |
1999 | 92.2k | return privKey; |
2000 | 92.2k | } |
2001 | | |
2002 | | SECKEYPrivateKey * |
2003 | | PK11_GenerateKeyPairWithFlags(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, |
2004 | | void *param, SECKEYPublicKey **pubKey, PK11AttrFlags attrFlags, void *wincx) |
2005 | 10.0k | { |
2006 | 10.0k | return PK11_GenerateKeyPairWithOpFlags(slot, type, param, pubKey, attrFlags, |
2007 | 10.0k | 0, 0, wincx); |
2008 | 10.0k | } |
2009 | | |
2010 | | /* |
2011 | | * Use the token to generate a key pair. |
2012 | | */ |
2013 | | SECKEYPrivateKey * |
2014 | | PK11_GenerateKeyPair(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, |
2015 | | void *param, SECKEYPublicKey **pubKey, PRBool token, |
2016 | | PRBool sensitive, void *wincx) |
2017 | 10.0k | { |
2018 | 10.0k | PK11AttrFlags attrFlags = 0; |
2019 | | |
2020 | 10.0k | if (token) { |
2021 | 0 | attrFlags |= PK11_ATTR_TOKEN; |
2022 | 10.0k | } else { |
2023 | 10.0k | attrFlags |= PK11_ATTR_SESSION; |
2024 | 10.0k | } |
2025 | 10.0k | if (sensitive) { |
2026 | 3.37k | attrFlags |= (PK11_ATTR_SENSITIVE | PK11_ATTR_PRIVATE); |
2027 | 6.65k | } else { |
2028 | 6.65k | attrFlags |= (PK11_ATTR_INSENSITIVE | PK11_ATTR_PUBLIC); |
2029 | 6.65k | } |
2030 | 10.0k | return PK11_GenerateKeyPairWithFlags(slot, type, param, pubKey, |
2031 | 10.0k | attrFlags, wincx); |
2032 | 10.0k | } |
2033 | | |
2034 | | /* build a public KEA key from the public value */ |
2035 | | SECKEYPublicKey * |
2036 | | PK11_MakeKEAPubKey(unsigned char *keyData, int length) |
2037 | 0 | { |
2038 | 0 | SECKEYPublicKey *pubk; |
2039 | 0 | SECItem pkData; |
2040 | 0 | SECStatus rv; |
2041 | 0 | PLArenaPool *arena; |
2042 | |
|
2043 | 0 | pkData.data = keyData; |
2044 | 0 | pkData.len = length; |
2045 | 0 | pkData.type = siBuffer; |
2046 | |
|
2047 | 0 | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
2048 | 0 | if (arena == NULL) |
2049 | 0 | return NULL; |
2050 | | |
2051 | 0 | pubk = (SECKEYPublicKey *)PORT_ArenaZAlloc(arena, sizeof(SECKEYPublicKey)); |
2052 | 0 | if (pubk == NULL) { |
2053 | 0 | PORT_FreeArena(arena, PR_FALSE); |
2054 | 0 | return NULL; |
2055 | 0 | } |
2056 | | |
2057 | 0 | pubk->arena = arena; |
2058 | 0 | pubk->pkcs11Slot = 0; |
2059 | 0 | pubk->pkcs11ID = CK_INVALID_HANDLE; |
2060 | 0 | pubk->keyType = fortezzaKey; |
2061 | 0 | rv = SECITEM_CopyItem(arena, &pubk->u.fortezza.KEAKey, &pkData); |
2062 | 0 | if (rv != SECSuccess) { |
2063 | 0 | PORT_FreeArena(arena, PR_FALSE); |
2064 | 0 | return NULL; |
2065 | 0 | } |
2066 | 0 | return pubk; |
2067 | 0 | } |
2068 | | |
2069 | | SECStatus |
2070 | | SECKEY_SetPublicValue(SECKEYPrivateKey *privKey, const SECItem *publicValue) |
2071 | 2 | { |
2072 | 2 | SECStatus rv; |
2073 | 2 | SECKEYPublicKey pubKey; |
2074 | 2 | PLArenaPool *arena; |
2075 | 2 | PK11SlotInfo *slot; |
2076 | 2 | CK_OBJECT_HANDLE privKeyID; |
2077 | 2 | CK_ULONG paramSet; |
2078 | | |
2079 | 2 | if (privKey == NULL || publicValue == NULL || |
2080 | 2 | publicValue->data == NULL || publicValue->len == 0) { |
2081 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2082 | 0 | return SECFailure; |
2083 | 0 | } |
2084 | | |
2085 | 2 | pubKey.arena = NULL; |
2086 | 2 | pubKey.keyType = privKey->keyType; |
2087 | 2 | pubKey.pkcs11Slot = NULL; |
2088 | 2 | pubKey.pkcs11ID = CK_INVALID_HANDLE; |
2089 | | /* can't use PORT_InitCheapArena here becase SECKEY_DestroyPublic is used |
2090 | | * to free it, and it uses PORT_FreeArena which not only frees the |
2091 | | * underlying arena, it also frees the allocated arena struct. */ |
2092 | 2 | arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
2093 | 2 | pubKey.arena = arena; |
2094 | 2 | if (arena == NULL) { |
2095 | 0 | return SECFailure; |
2096 | 0 | } |
2097 | | |
2098 | 2 | slot = privKey->pkcs11Slot; |
2099 | 2 | privKeyID = privKey->pkcs11ID; |
2100 | 2 | rv = SECFailure; |
2101 | 2 | switch (privKey->keyType) { |
2102 | 0 | default: |
2103 | | /* error code already set to SECFailure */ |
2104 | 0 | break; |
2105 | 1 | case rsaKey: |
2106 | 1 | pubKey.u.rsa.modulus = *publicValue; |
2107 | 1 | rv = PK11_ReadAttribute(slot, privKeyID, CKA_PUBLIC_EXPONENT, |
2108 | 1 | arena, &pubKey.u.rsa.publicExponent); |
2109 | 1 | break; |
2110 | 0 | case dsaKey: |
2111 | 0 | pubKey.u.dsa.publicValue = *publicValue; |
2112 | 0 | rv = PK11_ReadAttribute(slot, privKeyID, CKA_PRIME, |
2113 | 0 | arena, &pubKey.u.dsa.params.prime); |
2114 | 0 | if (rv != SECSuccess) { |
2115 | 0 | break; |
2116 | 0 | } |
2117 | 0 | rv = PK11_ReadAttribute(slot, privKeyID, CKA_SUBPRIME, |
2118 | 0 | arena, &pubKey.u.dsa.params.subPrime); |
2119 | 0 | if (rv != SECSuccess) { |
2120 | 0 | break; |
2121 | 0 | } |
2122 | 0 | rv = PK11_ReadAttribute(slot, privKeyID, CKA_BASE, |
2123 | 0 | arena, &pubKey.u.dsa.params.base); |
2124 | 0 | break; |
2125 | 0 | case dhKey: |
2126 | 0 | pubKey.u.dh.publicValue = *publicValue; |
2127 | 0 | rv = PK11_ReadAttribute(slot, privKeyID, CKA_PRIME, |
2128 | 0 | arena, &pubKey.u.dh.prime); |
2129 | 0 | if (rv != SECSuccess) { |
2130 | 0 | break; |
2131 | 0 | } |
2132 | 0 | rv = PK11_ReadAttribute(slot, privKeyID, CKA_BASE, |
2133 | 0 | arena, &pubKey.u.dh.base); |
2134 | 0 | break; |
2135 | 1 | case ecKey: |
2136 | 1 | case edKey: |
2137 | 1 | case ecMontKey: |
2138 | 1 | pubKey.u.ec.publicValue = *publicValue; |
2139 | 1 | pubKey.u.ec.encoding = ECPoint_Undefined; |
2140 | 1 | pubKey.u.ec.size = 0; |
2141 | 1 | rv = PK11_ReadAttribute(slot, privKeyID, CKA_EC_PARAMS, |
2142 | 1 | arena, &pubKey.u.ec.DEREncodedParams); |
2143 | 1 | break; |
2144 | 0 | case mldsaKey: |
2145 | 0 | pubKey.u.mldsa.publicValue = *publicValue; |
2146 | 0 | paramSet = PK11_ReadULongAttribute(slot, privKeyID, |
2147 | 0 | CKA_PARAMETER_SET); |
2148 | 0 | if (paramSet == CK_UNAVAILABLE_INFORMATION) { |
2149 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
2150 | 0 | break; |
2151 | 0 | } |
2152 | 0 | pubKey.u.mldsa.paramSet = SECKEY_GetMLDSAOidTagByPkcs11ParamSet(paramSet); |
2153 | 0 | if (pubKey.u.mldsa.paramSet == SEC_OID_UNKNOWN) { |
2154 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
2155 | 0 | break; |
2156 | 0 | } |
2157 | 0 | rv = SECSuccess; |
2158 | 0 | break; |
2159 | 0 | case kyberKey: |
2160 | 0 | pubKey.u.kyber.publicValue = *publicValue; |
2161 | 0 | paramSet = PK11_ReadULongAttribute(slot, privKeyID, |
2162 | 0 | CKA_PARAMETER_SET); |
2163 | 0 | if (paramSet == CK_UNAVAILABLE_INFORMATION) { |
2164 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
2165 | 0 | break; |
2166 | 0 | } |
2167 | 0 | pubKey.u.kyber.params = seckey_GetKyberParamsByPkcs11ParamSet( |
2168 | 0 | paramSet); |
2169 | 0 | if (pubKey.u.kyber.params == params_kyber_invalid) { |
2170 | 0 | PORT_SetError(SEC_ERROR_BAD_KEY); |
2171 | 0 | break; |
2172 | 0 | } |
2173 | 0 | rv = SECSuccess; |
2174 | 0 | break; |
2175 | 2 | } |
2176 | 2 | if (rv == SECSuccess) { |
2177 | 2 | CK_OBJECT_HANDLE pubID = PK11_ImportPublicKey(slot, &pubKey, PR_TRUE); |
2178 | 2 | if (pubID == CK_INVALID_HANDLE) { |
2179 | 0 | rv = SECFailure; |
2180 | 0 | } |
2181 | 2 | } |
2182 | | /* Even though pubKey is stored on the stack, we've allocated |
2183 | | * some of it's data from the arena. SECKEY_DestroyPublicKey |
2184 | | * destroys keys by freeing the arena, so this will clean up all |
2185 | | * the data we allocated specifically for the key above. It will |
2186 | | * also free any slot references which we may have picked up in |
2187 | | * PK11_ImportPublicKey. It won't delete the underlying key if |
2188 | | * its a Token/Permanent key (which it will be if |
2189 | | * PK11_ImportPublicKey succeeds). */ |
2190 | 2 | SECKEY_DestroyPublicKey(&pubKey); |
2191 | | |
2192 | 2 | return rv; |
2193 | 2 | } |
2194 | | |
2195 | | /* |
2196 | | * NOTE: This function doesn't return a SECKEYPrivateKey struct to represent |
2197 | | * the new private key object. If it were to create a session object that |
2198 | | * could later be looked up by its nickname, it would leak a SECKEYPrivateKey. |
2199 | | * So isPerm must be true. |
2200 | | */ |
2201 | | SECStatus |
2202 | | PK11_ImportEncryptedPrivateKeyInfo(PK11SlotInfo *slot, |
2203 | | SECKEYEncryptedPrivateKeyInfo *epki, SECItem *pwitem, |
2204 | | SECItem *nickname, const SECItem *publicValue, PRBool isPerm, |
2205 | | PRBool isPrivate, KeyType keyType, |
2206 | | unsigned int keyUsage, void *wincx) |
2207 | 0 | { |
2208 | 0 | if (!isPerm) { |
2209 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2210 | 0 | return SECFailure; |
2211 | 0 | } |
2212 | 0 | return PK11_ImportEncryptedPrivateKeyInfoAndReturnKey(slot, epki, |
2213 | 0 | pwitem, nickname, publicValue, isPerm, isPrivate, keyType, |
2214 | 0 | keyUsage, NULL, wincx); |
2215 | 0 | } |
2216 | | |
2217 | | SECStatus |
2218 | | PK11_ImportEncryptedPrivateKeyInfoAndReturnKey(PK11SlotInfo *slot, |
2219 | | SECKEYEncryptedPrivateKeyInfo *epki, SECItem *pwitem, |
2220 | | SECItem *nickname, const SECItem *publicValue, PRBool isPerm, |
2221 | | PRBool isPrivate, KeyType keyType, |
2222 | | unsigned int keyUsage, SECKEYPrivateKey **privk, |
2223 | | void *wincx) |
2224 | 0 | { |
2225 | 0 | CK_MECHANISM_TYPE pbeMechType; |
2226 | 0 | SECItem *crypto_param = NULL; |
2227 | 0 | PK11SymKey *key = NULL; |
2228 | 0 | SECStatus rv = SECSuccess; |
2229 | 0 | CK_MECHANISM_TYPE cryptoMechType; |
2230 | 0 | SECKEYPrivateKey *privKey = NULL; |
2231 | 0 | PRBool faulty3DES = PR_FALSE; |
2232 | 0 | if ((epki == NULL) || (pwitem == NULL)) |
2233 | 0 | return SECFailure; |
2234 | | |
2235 | 0 | pbeMechType = PK11_AlgtagToMechanism(SECOID_FindOIDTag( |
2236 | 0 | &epki->algorithm.algorithm)); |
2237 | |
|
2238 | 0 | try_faulty_3des: |
2239 | |
|
2240 | 0 | key = PK11_PBEKeyGen(slot, &epki->algorithm, pwitem, faulty3DES, wincx); |
2241 | 0 | if (key == NULL) { |
2242 | 0 | rv = SECFailure; |
2243 | 0 | goto done; |
2244 | 0 | } |
2245 | 0 | cryptoMechType = pk11_GetPBECryptoMechanism(&epki->algorithm, |
2246 | 0 | &crypto_param, pwitem, faulty3DES); |
2247 | 0 | if (cryptoMechType == CKM_INVALID_MECHANISM) { |
2248 | 0 | rv = SECFailure; |
2249 | 0 | goto done; |
2250 | 0 | } |
2251 | | |
2252 | 0 | cryptoMechType = PK11_GetPadMechanism(cryptoMechType); |
2253 | |
|
2254 | 0 | privKey = PK11_UnwrapPrivKeyByKeyType(slot, key, cryptoMechType, |
2255 | 0 | crypto_param, &epki->encryptedData, |
2256 | 0 | nickname, publicValue, isPerm, |
2257 | 0 | isPrivate, keyType, keyUsage, wincx); |
2258 | 0 | if (privKey) { |
2259 | 0 | rv = SECSuccess; |
2260 | 0 | goto done; |
2261 | 0 | } |
2262 | | |
2263 | | /* if we are unable to import the key and the pbeMechType is |
2264 | | * CKM_NSS_PBE_SHA1_TRIPLE_DES_CBC, then it is possible that |
2265 | | * the encrypted blob was created with a buggy key generation method |
2266 | | * which is described in the PKCS 12 implementation notes. So we |
2267 | | * need to try importing via that method. |
2268 | | */ |
2269 | 0 | if ((pbeMechType == CKM_NSS_PBE_SHA1_TRIPLE_DES_CBC) && (!faulty3DES)) { |
2270 | | /* clean up after ourselves before redoing the key generation. */ |
2271 | |
|
2272 | 0 | PK11_FreeSymKey(key); |
2273 | 0 | key = NULL; |
2274 | |
|
2275 | 0 | if (crypto_param) { |
2276 | 0 | SECITEM_ZfreeItem(crypto_param, PR_TRUE); |
2277 | 0 | crypto_param = NULL; |
2278 | 0 | } |
2279 | |
|
2280 | 0 | faulty3DES = PR_TRUE; |
2281 | 0 | goto try_faulty_3des; |
2282 | 0 | } |
2283 | | |
2284 | | /* key import really did fail */ |
2285 | 0 | rv = SECFailure; |
2286 | |
|
2287 | 0 | done: |
2288 | 0 | if ((rv == SECSuccess) && isPerm) { |
2289 | | /* If we are importing a token object, |
2290 | | * create the corresponding public key. |
2291 | | * If this fails, just continue as the target |
2292 | | * token simply might not support persistant |
2293 | | * public keys. Such tokens are usable, but |
2294 | | * need to be authenticated before searching |
2295 | | * for user certs. */ |
2296 | 0 | (void)SECKEY_SetPublicValue(privKey, publicValue); |
2297 | 0 | } |
2298 | |
|
2299 | 0 | if (privKey) { |
2300 | 0 | if (privk) { |
2301 | 0 | *privk = privKey; |
2302 | 0 | } else { |
2303 | 0 | SECKEY_DestroyPrivateKey(privKey); |
2304 | 0 | } |
2305 | 0 | privKey = NULL; |
2306 | 0 | } |
2307 | 0 | if (crypto_param != NULL) { |
2308 | 0 | SECITEM_ZfreeItem(crypto_param, PR_TRUE); |
2309 | 0 | } |
2310 | |
|
2311 | 0 | if (key != NULL) { |
2312 | 0 | PK11_FreeSymKey(key); |
2313 | 0 | } |
2314 | |
|
2315 | 0 | return rv; |
2316 | 0 | } |
2317 | | |
2318 | | SECKEYPrivateKeyInfo * |
2319 | | PK11_ExportPrivateKeyInfo(CERTCertificate *cert, void *wincx) |
2320 | 0 | { |
2321 | 0 | SECKEYPrivateKeyInfo *pki = NULL; |
2322 | 0 | SECKEYPrivateKey *pk = PK11_FindKeyByAnyCert(cert, wincx); |
2323 | 0 | if (pk != NULL) { |
2324 | 0 | pki = PK11_ExportPrivKeyInfo(pk, wincx); |
2325 | 0 | SECKEY_DestroyPrivateKey(pk); |
2326 | 0 | } |
2327 | 0 | return pki; |
2328 | 0 | } |
2329 | | |
2330 | | /* V2 refers to PKCS #5 V2 here. If a PKCS #5 v1 or PKCS #12 pbe is passed |
2331 | | * for pbeTag, then encTag and hashTag are ignored. If pbe is an encryption |
2332 | | * algorithm, then PKCS #5 V2 is used with prfTag for the prf. If prfTag isn't |
2333 | | * supplied prf will be SEC_OID_HMAC_SHA1 */ |
2334 | | SECKEYEncryptedPrivateKeyInfo * |
2335 | | PK11_ExportEncryptedPrivKeyInfoV2( |
2336 | | PK11SlotInfo *slot, /* optional, encrypt key in this slot */ |
2337 | | SECOidTag pbeAlg, /* PBE algorithm to encrypt the with key */ |
2338 | | SECOidTag encAlg, /* Encryption algorithm to Encrypt the key with */ |
2339 | | SECOidTag prfAlg, /* Hash algorithm for PRF */ |
2340 | | SECItem *pwitem, /* password for PBE encryption */ |
2341 | | SECKEYPrivateKey *pk, /* encrypt this private key */ |
2342 | | int iteration, /* interations for PBE alg */ |
2343 | | void *pwArg) /* context for password callback */ |
2344 | 0 | { |
2345 | 0 | SECKEYEncryptedPrivateKeyInfo *epki = NULL; |
2346 | 0 | PLArenaPool *arena = NULL; |
2347 | 0 | SECAlgorithmID *algid; |
2348 | 0 | SECOidTag pbeAlgTag = SEC_OID_UNKNOWN; |
2349 | 0 | SECItem *crypto_param = NULL; |
2350 | 0 | PK11SymKey *key = NULL; |
2351 | 0 | SECKEYPrivateKey *tmpPK = NULL; |
2352 | 0 | SECStatus rv = SECSuccess; |
2353 | 0 | CK_RV crv; |
2354 | 0 | CK_ULONG encBufLen; |
2355 | 0 | CK_MECHANISM_TYPE pbeMechType; |
2356 | 0 | CK_MECHANISM_TYPE cryptoMechType; |
2357 | 0 | CK_MECHANISM cryptoMech; |
2358 | |
|
2359 | 0 | if (!pwitem || !pk) { |
2360 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2361 | 0 | return NULL; |
2362 | 0 | } |
2363 | | |
2364 | 0 | algid = sec_pkcs5CreateAlgorithmID(pbeAlg, encAlg, prfAlg, |
2365 | 0 | &pbeAlgTag, 0, NULL, iteration); |
2366 | 0 | if (algid == NULL) { |
2367 | 0 | return NULL; |
2368 | 0 | } |
2369 | | |
2370 | 0 | arena = PORT_NewArena(2048); |
2371 | 0 | if (arena) |
2372 | 0 | epki = PORT_ArenaZNew(arena, SECKEYEncryptedPrivateKeyInfo); |
2373 | 0 | if (epki == NULL) { |
2374 | 0 | rv = SECFailure; |
2375 | 0 | goto loser; |
2376 | 0 | } |
2377 | 0 | epki->arena = arena; |
2378 | | |
2379 | | /* if we didn't specify a slot, use the slot the private key was in */ |
2380 | 0 | if (!slot) { |
2381 | 0 | slot = pk->pkcs11Slot; |
2382 | 0 | } |
2383 | | |
2384 | | /* if we specified a different slot, and the private key slot can do the |
2385 | | * pbe key gen, generate the key in the private key slot so we don't have |
2386 | | * to move it later */ |
2387 | 0 | pbeMechType = PK11_AlgtagToMechanism(pbeAlgTag); |
2388 | 0 | if (slot != pk->pkcs11Slot) { |
2389 | 0 | if (PK11_DoesMechanism(pk->pkcs11Slot, pbeMechType)) { |
2390 | 0 | slot = pk->pkcs11Slot; |
2391 | 0 | } |
2392 | 0 | } |
2393 | 0 | key = PK11_PBEKeyGen(slot, algid, pwitem, PR_FALSE, pwArg); |
2394 | 0 | if (key == NULL) { |
2395 | 0 | rv = SECFailure; |
2396 | 0 | goto loser; |
2397 | 0 | } |
2398 | | |
2399 | 0 | cryptoMechType = PK11_GetPBECryptoMechanism(algid, &crypto_param, pwitem); |
2400 | 0 | if (cryptoMechType == CKM_INVALID_MECHANISM) { |
2401 | 0 | rv = SECFailure; |
2402 | 0 | goto loser; |
2403 | 0 | } |
2404 | | |
2405 | 0 | cryptoMech.mechanism = PK11_GetPadMechanism(cryptoMechType); |
2406 | 0 | cryptoMech.pParameter = crypto_param ? crypto_param->data : NULL; |
2407 | 0 | cryptoMech.ulParameterLen = crypto_param ? crypto_param->len : 0; |
2408 | | |
2409 | | /* If the key isn't in the private key slot, move it */ |
2410 | 0 | if (key->slot != pk->pkcs11Slot) { |
2411 | 0 | PK11SymKey *newkey = pk11_CopyToSlot(pk->pkcs11Slot, |
2412 | 0 | key->type, CKA_WRAP, key); |
2413 | 0 | if (newkey == NULL) { |
2414 | | /* couldn't import the wrapping key, try exporting the |
2415 | | * private key */ |
2416 | 0 | tmpPK = pk11_loadPrivKey(key->slot, pk, NULL, PR_FALSE, PR_TRUE); |
2417 | 0 | if (tmpPK == NULL) { |
2418 | 0 | rv = SECFailure; |
2419 | 0 | goto loser; |
2420 | 0 | } |
2421 | 0 | pk = tmpPK; |
2422 | 0 | } else { |
2423 | | /* free the old key and use the new key */ |
2424 | 0 | PK11_FreeSymKey(key); |
2425 | 0 | key = newkey; |
2426 | 0 | } |
2427 | 0 | } |
2428 | | |
2429 | | /* we are extracting an encrypted privateKey structure. |
2430 | | * which needs to be freed along with the buffer into which it is |
2431 | | * returned. eventually, we should retrieve an encrypted key using |
2432 | | * pkcs8/pkcs5. |
2433 | | */ |
2434 | 0 | encBufLen = 0; |
2435 | 0 | PK11_EnterSlotMonitor(pk->pkcs11Slot); |
2436 | 0 | crv = PK11_GETTAB(pk->pkcs11Slot)->C_WrapKey(pk->pkcs11Slot->session, &cryptoMech, key->objectID, pk->pkcs11ID, NULL, &encBufLen); |
2437 | 0 | PK11_ExitSlotMonitor(pk->pkcs11Slot); |
2438 | 0 | if (crv != CKR_OK) { |
2439 | 0 | rv = SECFailure; |
2440 | 0 | goto loser; |
2441 | 0 | } |
2442 | 0 | epki->encryptedData.data = PORT_ArenaAlloc(arena, encBufLen); |
2443 | 0 | if (!epki->encryptedData.data) { |
2444 | 0 | rv = SECFailure; |
2445 | 0 | goto loser; |
2446 | 0 | } |
2447 | 0 | PK11_EnterSlotMonitor(pk->pkcs11Slot); |
2448 | 0 | crv = PK11_GETTAB(pk->pkcs11Slot)->C_WrapKey(pk->pkcs11Slot->session, &cryptoMech, key->objectID, pk->pkcs11ID, epki->encryptedData.data, &encBufLen); |
2449 | 0 | PK11_ExitSlotMonitor(pk->pkcs11Slot); |
2450 | 0 | epki->encryptedData.len = (unsigned int)encBufLen; |
2451 | 0 | if (crv != CKR_OK) { |
2452 | 0 | rv = SECFailure; |
2453 | 0 | goto loser; |
2454 | 0 | } |
2455 | | |
2456 | 0 | if (!epki->encryptedData.len) { |
2457 | 0 | rv = SECFailure; |
2458 | 0 | goto loser; |
2459 | 0 | } |
2460 | | |
2461 | 0 | rv = SECOID_CopyAlgorithmID(arena, &epki->algorithm, algid); |
2462 | |
|
2463 | 0 | loser: |
2464 | 0 | if (crypto_param != NULL) { |
2465 | 0 | SECITEM_ZfreeItem(crypto_param, PR_TRUE); |
2466 | 0 | crypto_param = NULL; |
2467 | 0 | } |
2468 | |
|
2469 | 0 | if (key != NULL) { |
2470 | 0 | PK11_FreeSymKey(key); |
2471 | 0 | } |
2472 | 0 | if (tmpPK != NULL) { |
2473 | 0 | SECKEY_DestroyPrivateKey(tmpPK); |
2474 | 0 | } |
2475 | 0 | SECOID_DestroyAlgorithmID(algid, PR_TRUE); |
2476 | |
|
2477 | 0 | if (rv == SECFailure) { |
2478 | 0 | if (arena != NULL) { |
2479 | 0 | PORT_FreeArena(arena, PR_TRUE); |
2480 | 0 | } |
2481 | 0 | epki = NULL; |
2482 | 0 | } |
2483 | |
|
2484 | 0 | return epki; |
2485 | 0 | } |
2486 | | |
2487 | | SECKEYEncryptedPrivateKeyInfo * |
2488 | | PK11_ExportEncryptedPrivKeyInfo( |
2489 | | PK11SlotInfo *slot, /* optional, encrypt key in this slot */ |
2490 | | SECOidTag algTag, /* PBE algorithm to encrypt the with key */ |
2491 | | SECItem *pwitem, /* password for PBE encryption */ |
2492 | | SECKEYPrivateKey *pk, /* encrypt this private key */ |
2493 | | int iteration, /* interations for PBE alg */ |
2494 | | void *pwArg) /* context for password callback */ |
2495 | 0 | { |
2496 | 0 | return PK11_ExportEncryptedPrivKeyInfoV2(slot, algTag, SEC_OID_UNKNOWN, |
2497 | 0 | SEC_OID_UNKNOWN, pwitem, pk, |
2498 | 0 | iteration, pwArg); |
2499 | 0 | } |
2500 | | |
2501 | | /* V2 refers to PKCS #5 V2 here. If a PKCS #5 v1 or PKCS #12 pbe is passed |
2502 | | * for pbeTag, then encTag and hashTag are ignored. If pbe is an encryption |
2503 | | * algorithm, then PKCS #5 V2 is used with prfTag for the prf. If prfTag isn't |
2504 | | * supplied prf will be SEC_OID_HMAC_SHA1 */ |
2505 | | SECKEYEncryptedPrivateKeyInfo * |
2506 | | PK11_ExportEncryptedPrivateKeyInfoV2( |
2507 | | PK11SlotInfo *slot, /* optional, encrypt key in this slot */ |
2508 | | SECOidTag pbeAlg, /* PBE algorithm to encrypt the with key */ |
2509 | | SECOidTag encAlg, /* Encryption algorithm to Encrypt the key with */ |
2510 | | SECOidTag prfAlg, /* HMAC algorithm for PRF*/ |
2511 | | SECItem *pwitem, /* password for PBE encryption */ |
2512 | | CERTCertificate *cert, /* wrap priv key for this user cert */ |
2513 | | int iteration, /* interations for PBE alg */ |
2514 | | void *pwArg) /* context for password callback */ |
2515 | 0 | { |
2516 | 0 | SECKEYEncryptedPrivateKeyInfo *epki = NULL; |
2517 | 0 | SECKEYPrivateKey *pk = PK11_FindKeyByAnyCert(cert, pwArg); |
2518 | 0 | if (pk != NULL) { |
2519 | 0 | epki = PK11_ExportEncryptedPrivKeyInfoV2(slot, pbeAlg, encAlg, prfAlg, |
2520 | 0 | pwitem, pk, iteration, |
2521 | 0 | pwArg); |
2522 | 0 | SECKEY_DestroyPrivateKey(pk); |
2523 | 0 | } |
2524 | 0 | return epki; |
2525 | 0 | } |
2526 | | |
2527 | | SECKEYEncryptedPrivateKeyInfo * |
2528 | | PK11_ExportEncryptedPrivateKeyInfo( |
2529 | | PK11SlotInfo *slot, /* optional, encrypt key in this slot */ |
2530 | | SECOidTag algTag, /* encrypt key with this algorithm */ |
2531 | | SECItem *pwitem, /* password for PBE encryption */ |
2532 | | CERTCertificate *cert, /* wrap priv key for this user cert */ |
2533 | | int iteration, /* interations for PBE alg */ |
2534 | | void *pwArg) /* context for password callback */ |
2535 | 0 | { |
2536 | 0 | return PK11_ExportEncryptedPrivateKeyInfoV2(slot, algTag, SEC_OID_UNKNOWN, |
2537 | 0 | SEC_OID_UNKNOWN, pwitem, cert, |
2538 | 0 | iteration, pwArg); |
2539 | 0 | } |
2540 | | |
2541 | | SECItem * |
2542 | | PK11_DEREncodePublicKey(const SECKEYPublicKey *pubk) |
2543 | 0 | { |
2544 | 0 | return SECKEY_EncodeDERSubjectPublicKeyInfo(pubk); |
2545 | 0 | } |
2546 | | |
2547 | | char * |
2548 | | PK11_GetPrivateKeyNickname(SECKEYPrivateKey *privKey) |
2549 | 2.69k | { |
2550 | 2.69k | return PK11_GetObjectNickname(privKey->pkcs11Slot, privKey->pkcs11ID); |
2551 | 2.69k | } |
2552 | | |
2553 | | char * |
2554 | | PK11_GetPublicKeyNickname(SECKEYPublicKey *pubKey) |
2555 | 0 | { |
2556 | 0 | return PK11_GetObjectNickname(pubKey->pkcs11Slot, pubKey->pkcs11ID); |
2557 | 0 | } |
2558 | | |
2559 | | SECStatus |
2560 | | PK11_SetPrivateKeyNickname(SECKEYPrivateKey *privKey, const char *nickname) |
2561 | 0 | { |
2562 | 0 | return PK11_SetObjectNickname(privKey->pkcs11Slot, |
2563 | 0 | privKey->pkcs11ID, nickname); |
2564 | 0 | } |
2565 | | |
2566 | | SECStatus |
2567 | | PK11_SetPublicKeyNickname(SECKEYPublicKey *pubKey, const char *nickname) |
2568 | 0 | { |
2569 | 0 | return PK11_SetObjectNickname(pubKey->pkcs11Slot, |
2570 | 0 | pubKey->pkcs11ID, nickname); |
2571 | 0 | } |
2572 | | |
2573 | | SECKEYPQGParams * |
2574 | | PK11_GetPQGParamsFromPrivateKey(SECKEYPrivateKey *privKey) |
2575 | 2.69k | { |
2576 | 2.69k | CK_ATTRIBUTE pTemplate[] = { |
2577 | 2.69k | { CKA_PRIME, NULL, 0 }, |
2578 | 2.69k | { CKA_SUBPRIME, NULL, 0 }, |
2579 | 2.69k | { CKA_BASE, NULL, 0 }, |
2580 | 2.69k | }; |
2581 | 2.69k | int pTemplateLen = sizeof(pTemplate) / sizeof(pTemplate[0]); |
2582 | 2.69k | PLArenaPool *arena = NULL; |
2583 | 2.69k | SECKEYPQGParams *params; |
2584 | 2.69k | CK_RV crv; |
2585 | | |
2586 | 2.69k | arena = PORT_NewArena(2048); |
2587 | 2.69k | if (arena == NULL) { |
2588 | 0 | goto loser; |
2589 | 0 | } |
2590 | 2.69k | params = (SECKEYPQGParams *)PORT_ArenaZAlloc(arena, sizeof(SECKEYPQGParams)); |
2591 | 2.69k | if (params == NULL) { |
2592 | 0 | goto loser; |
2593 | 0 | } |
2594 | | |
2595 | 2.69k | crv = PK11_GetAttributes(arena, privKey->pkcs11Slot, privKey->pkcs11ID, |
2596 | 2.69k | pTemplate, pTemplateLen); |
2597 | 2.69k | if (crv != CKR_OK) { |
2598 | 100 | PORT_SetError(PK11_MapError(crv)); |
2599 | 100 | goto loser; |
2600 | 100 | } |
2601 | | |
2602 | 2.59k | params->arena = arena; |
2603 | 2.59k | params->prime.data = pTemplate[0].pValue; |
2604 | 2.59k | params->prime.len = pTemplate[0].ulValueLen; |
2605 | 2.59k | params->subPrime.data = pTemplate[1].pValue; |
2606 | 2.59k | params->subPrime.len = pTemplate[1].ulValueLen; |
2607 | 2.59k | params->base.data = pTemplate[2].pValue; |
2608 | 2.59k | params->base.len = pTemplate[2].ulValueLen; |
2609 | | |
2610 | 2.59k | return params; |
2611 | | |
2612 | 100 | loser: |
2613 | 100 | if (arena != NULL) { |
2614 | 100 | PORT_FreeArena(arena, PR_FALSE); |
2615 | 100 | } |
2616 | 100 | return NULL; |
2617 | 2.69k | } |
2618 | | |
2619 | | SECKEYPrivateKey * |
2620 | | PK11_CopyTokenPrivKeyToSessionPrivKey(PK11SlotInfo *destSlot, |
2621 | | SECKEYPrivateKey *privKey) |
2622 | 16 | { |
2623 | 16 | CK_RV crv; |
2624 | 16 | CK_OBJECT_HANDLE newKeyID; |
2625 | | |
2626 | 16 | static const CK_BBOOL ckfalse = CK_FALSE; |
2627 | 16 | static const CK_ATTRIBUTE template[1] = { |
2628 | 16 | { CKA_TOKEN, (CK_BBOOL *)&ckfalse, sizeof ckfalse } |
2629 | 16 | }; |
2630 | | |
2631 | 16 | if (destSlot && destSlot != privKey->pkcs11Slot) { |
2632 | 0 | SECKEYPrivateKey *newKey = |
2633 | 0 | pk11_loadPrivKey(destSlot, |
2634 | 0 | privKey, |
2635 | 0 | NULL, /* pubKey */ |
2636 | 0 | PR_FALSE, /* token */ |
2637 | 0 | PR_FALSE); /* sensitive */ |
2638 | 0 | if (newKey) |
2639 | 0 | return newKey; |
2640 | 0 | } |
2641 | 16 | destSlot = privKey->pkcs11Slot; |
2642 | 16 | PK11_Authenticate(destSlot, PR_TRUE, privKey->wincx); |
2643 | 16 | PK11_EnterSlotMonitor(destSlot); |
2644 | 16 | crv = PK11_GETTAB(destSlot)->C_CopyObject(destSlot->session, |
2645 | 16 | privKey->pkcs11ID, |
2646 | 16 | (CK_ATTRIBUTE *)template, |
2647 | 16 | 1, &newKeyID); |
2648 | 16 | PK11_ExitSlotMonitor(destSlot); |
2649 | | |
2650 | 16 | if (crv != CKR_OK) { |
2651 | 0 | PORT_SetError(PK11_MapError(crv)); |
2652 | 0 | return NULL; |
2653 | 0 | } |
2654 | | |
2655 | 16 | return pk11_MakePrivKey(destSlot, privKey->keyType, PR_TRUE /*isOwner*/, |
2656 | 16 | newKeyID, privKey->wincx); |
2657 | 16 | } |
2658 | | |
2659 | | SECKEYPrivateKey * |
2660 | | PK11_ConvertSessionPrivKeyToTokenPrivKey(SECKEYPrivateKey *privk, void *wincx) |
2661 | 0 | { |
2662 | 0 | PK11SlotInfo *slot = privk->pkcs11Slot; |
2663 | 0 | CK_ATTRIBUTE template[1]; |
2664 | 0 | CK_ATTRIBUTE *attrs = template; |
2665 | 0 | CK_BBOOL cktrue = CK_TRUE; |
2666 | 0 | CK_RV crv; |
2667 | 0 | CK_OBJECT_HANDLE newKeyID; |
2668 | 0 | CK_SESSION_HANDLE rwsession; |
2669 | |
|
2670 | 0 | PK11_SETATTRS(attrs, CKA_TOKEN, &cktrue, sizeof(cktrue)); |
2671 | 0 | attrs++; |
2672 | |
|
2673 | 0 | PK11_Authenticate(slot, PR_TRUE, wincx); |
2674 | 0 | rwsession = PK11_GetRWSession(slot); |
2675 | 0 | if (rwsession == CK_INVALID_HANDLE) { |
2676 | 0 | PORT_SetError(SEC_ERROR_BAD_DATA); |
2677 | 0 | return NULL; |
2678 | 0 | } |
2679 | 0 | crv = PK11_GETTAB(slot)->C_CopyObject(rwsession, privk->pkcs11ID, |
2680 | 0 | template, 1, &newKeyID); |
2681 | 0 | PK11_RestoreROSession(slot, rwsession); |
2682 | |
|
2683 | 0 | if (crv != CKR_OK) { |
2684 | 0 | PORT_SetError(PK11_MapError(crv)); |
2685 | 0 | return NULL; |
2686 | 0 | } |
2687 | | |
2688 | 0 | return pk11_MakePrivKey(slot, nullKey /*KeyType*/, PR_FALSE /*isOwner*/, |
2689 | 0 | newKeyID, NULL /*wincx*/); |
2690 | 0 | } |
2691 | | |
2692 | | /* |
2693 | | * destroy a private key if there are no matching certs. |
2694 | | * this function also frees the privKey structure. |
2695 | | */ |
2696 | | SECStatus |
2697 | | PK11_DeleteTokenPrivateKey(SECKEYPrivateKey *privKey, PRBool force) |
2698 | 0 | { |
2699 | 0 | CERTCertificate *cert = PK11_GetCertFromPrivateKey(privKey); |
2700 | 0 | SECStatus rv = SECWouldBlock; |
2701 | |
|
2702 | 0 | if (!cert || force) { |
2703 | | /* now, then it's safe for the key to go away */ |
2704 | 0 | rv = PK11_DestroyTokenObject(privKey->pkcs11Slot, privKey->pkcs11ID); |
2705 | 0 | } |
2706 | 0 | if (cert) { |
2707 | 0 | CERT_DestroyCertificate(cert); |
2708 | 0 | } |
2709 | 0 | SECKEY_DestroyPrivateKey(privKey); |
2710 | 0 | return rv; |
2711 | 0 | } |
2712 | | |
2713 | | /* |
2714 | | * destroy a private key if there are no matching certs. |
2715 | | * this function also frees the privKey structure. |
2716 | | */ |
2717 | | SECStatus |
2718 | | PK11_DeleteTokenPublicKey(SECKEYPublicKey *pubKey) |
2719 | 0 | { |
2720 | | /* now, then it's safe for the key to go away */ |
2721 | 0 | if (pubKey->pkcs11Slot == NULL) { |
2722 | 0 | return SECFailure; |
2723 | 0 | } |
2724 | 0 | PK11_DestroyTokenObject(pubKey->pkcs11Slot, pubKey->pkcs11ID); |
2725 | 0 | SECKEY_DestroyPublicKey(pubKey); |
2726 | 0 | return SECSuccess; |
2727 | 0 | } |
2728 | | |
2729 | | /* |
2730 | | * key call back structure. |
2731 | | */ |
2732 | | typedef struct pk11KeyCallbackStr { |
2733 | | SECStatus (*callback)(SECKEYPrivateKey *, void *); |
2734 | | void *callbackArg; |
2735 | | void *wincx; |
2736 | | } pk11KeyCallback; |
2737 | | |
2738 | | /* |
2739 | | * callback to map Object Handles to Private Keys; |
2740 | | */ |
2741 | | SECStatus |
2742 | | pk11_DoKeys(PK11SlotInfo *slot, CK_OBJECT_HANDLE keyHandle, void *arg) |
2743 | 0 | { |
2744 | 0 | SECStatus rv = SECSuccess; |
2745 | 0 | SECKEYPrivateKey *privKey; |
2746 | 0 | pk11KeyCallback *keycb = (pk11KeyCallback *)arg; |
2747 | 0 | if (!arg) { |
2748 | 0 | return SECFailure; |
2749 | 0 | } |
2750 | | |
2751 | 0 | privKey = pk11_MakePrivKey(slot, nullKey, PR_FALSE, keyHandle, keycb->wincx); |
2752 | |
|
2753 | 0 | if (privKey == NULL) { |
2754 | 0 | return SECFailure; |
2755 | 0 | } |
2756 | | |
2757 | 0 | if (keycb->callback) { |
2758 | 0 | rv = (*keycb->callback)(privKey, keycb->callbackArg); |
2759 | 0 | } |
2760 | |
|
2761 | 0 | SECKEY_DestroyPrivateKey(privKey); |
2762 | 0 | return rv; |
2763 | 0 | } |
2764 | | |
2765 | | /*********************************************************************** |
2766 | | * PK11_TraversePrivateKeysInSlot |
2767 | | * |
2768 | | * Traverses all the private keys on a slot. |
2769 | | * |
2770 | | * INPUTS |
2771 | | * slot |
2772 | | * The PKCS #11 slot whose private keys you want to traverse. |
2773 | | * callback |
2774 | | * A callback function that will be called for each key. |
2775 | | * arg |
2776 | | * An argument that will be passed to the callback function. |
2777 | | */ |
2778 | | SECStatus |
2779 | | PK11_TraversePrivateKeysInSlot(PK11SlotInfo *slot, |
2780 | | SECStatus (*callback)(SECKEYPrivateKey *, void *), void *arg) |
2781 | 0 | { |
2782 | 0 | pk11KeyCallback perKeyCB; |
2783 | 0 | pk11TraverseSlot perObjectCB; |
2784 | 0 | CK_OBJECT_CLASS privkClass = CKO_PRIVATE_KEY; |
2785 | 0 | CK_BBOOL ckTrue = CK_TRUE; |
2786 | 0 | CK_ATTRIBUTE theTemplate[2]; |
2787 | 0 | int templateSize = 2; |
2788 | |
|
2789 | 0 | theTemplate[0].type = CKA_CLASS; |
2790 | 0 | theTemplate[0].pValue = &privkClass; |
2791 | 0 | theTemplate[0].ulValueLen = sizeof(privkClass); |
2792 | 0 | theTemplate[1].type = CKA_TOKEN; |
2793 | 0 | theTemplate[1].pValue = &ckTrue; |
2794 | 0 | theTemplate[1].ulValueLen = sizeof(ckTrue); |
2795 | |
|
2796 | 0 | if (slot == NULL) { |
2797 | 0 | return SECSuccess; |
2798 | 0 | } |
2799 | | |
2800 | 0 | perObjectCB.callback = pk11_DoKeys; |
2801 | 0 | perObjectCB.callbackArg = &perKeyCB; |
2802 | 0 | perObjectCB.findTemplate = theTemplate; |
2803 | 0 | perObjectCB.templateCount = templateSize; |
2804 | 0 | perKeyCB.callback = callback; |
2805 | 0 | perKeyCB.callbackArg = arg; |
2806 | 0 | perKeyCB.wincx = NULL; |
2807 | |
|
2808 | 0 | return PK11_TraverseSlot(slot, &perObjectCB); |
2809 | 0 | } |
2810 | | |
2811 | | /* |
2812 | | * return the private key with the given ID |
2813 | | */ |
2814 | | CK_OBJECT_HANDLE |
2815 | | pk11_FindPrivateKeyFromCertID(PK11SlotInfo *slot, SECItem *keyID) |
2816 | 0 | { |
2817 | 0 | CK_OBJECT_CLASS privKey = CKO_PRIVATE_KEY; |
2818 | 0 | CK_ATTRIBUTE theTemplate[] = { |
2819 | 0 | { CKA_ID, NULL, 0 }, |
2820 | 0 | { CKA_CLASS, NULL, 0 }, |
2821 | 0 | }; |
2822 | | /* if you change the array, change the variable below as well */ |
2823 | 0 | int tsize = sizeof(theTemplate) / sizeof(theTemplate[0]); |
2824 | 0 | CK_ATTRIBUTE *attrs = theTemplate; |
2825 | |
|
2826 | 0 | PK11_SETATTRS(attrs, CKA_ID, keyID->data, keyID->len); |
2827 | 0 | attrs++; |
2828 | 0 | PK11_SETATTRS(attrs, CKA_CLASS, &privKey, sizeof(privKey)); |
2829 | |
|
2830 | 0 | return pk11_FindObjectByTemplate(slot, theTemplate, tsize); |
2831 | 0 | } |
2832 | | |
2833 | | SECKEYPrivateKey * |
2834 | | PK11_FindKeyByKeyID(PK11SlotInfo *slot, SECItem *keyID, void *wincx) |
2835 | 0 | { |
2836 | 0 | CK_OBJECT_HANDLE keyHandle; |
2837 | 0 | SECKEYPrivateKey *privKey; |
2838 | |
|
2839 | 0 | keyHandle = pk11_FindPrivateKeyFromCertID(slot, keyID); |
2840 | 0 | if (keyHandle == CK_INVALID_HANDLE) { |
2841 | 0 | return NULL; |
2842 | 0 | } |
2843 | 0 | privKey = pk11_MakePrivKey(slot, nullKey, PR_FALSE, keyHandle, wincx); |
2844 | 0 | return privKey; |
2845 | 0 | } |
2846 | | |
2847 | | /* |
2848 | | * Create a SECKEYPrivateKey directly from a PKCS #11 private key template. |
2849 | | * |
2850 | | * The template must fully describe a private key object (at minimum CKA_CLASS = |
2851 | | * CKO_PRIVATE_KEY, CKA_KEY_TYPE, and the key material). The object is created |
2852 | | * as a session (non-token) object on 'slot'. The returned SECKEYPrivateKey owns |
2853 | | * the underlying PKCS #11 object and destroys it when the key is freed with |
2854 | | * SECKEY_DestroyPrivateKey. |
2855 | | */ |
2856 | | SECKEYPrivateKey * |
2857 | | PK11_CreatePrivateKeyFromTemplate(PK11SlotInfo *slot, |
2858 | | const CK_ATTRIBUTE *theTemplate, |
2859 | | unsigned int count, void *wincx) |
2860 | 0 | { |
2861 | 0 | CK_OBJECT_HANDLE keyHandle; |
2862 | 0 | SECKEYPrivateKey *privKey; |
2863 | 0 | SECStatus rv; |
2864 | | |
2865 | | /* PK11_CreateNewObject takes the count as an int, so guard against a |
2866 | | * value that would overflow when narrowed. */ |
2867 | 0 | if (slot == NULL || theTemplate == NULL || count == 0 || count > INT_MAX) { |
2868 | 0 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
2869 | 0 | return NULL; |
2870 | 0 | } |
2871 | | |
2872 | 0 | rv = PK11_CreateNewObject(slot, CK_INVALID_HANDLE, theTemplate, count, |
2873 | 0 | PR_FALSE, &keyHandle); |
2874 | 0 | if (rv != SECSuccess) { |
2875 | 0 | return NULL; |
2876 | 0 | } |
2877 | | |
2878 | | /* Pass isOwner = PR_TRUE so the returned key owns the session object we |
2879 | | * just created and destroys it on cleanup. */ |
2880 | 0 | privKey = pk11_MakePrivKey(slot, nullKey, PR_TRUE, keyHandle, wincx); |
2881 | 0 | if (privKey == NULL) { |
2882 | | /* Don't leak the object we created if wrapping it failed. */ |
2883 | 0 | (void)PK11_DestroyObject(slot, keyHandle); |
2884 | 0 | return NULL; |
2885 | 0 | } |
2886 | | |
2887 | 0 | return privKey; |
2888 | 0 | } |
2889 | | |
2890 | | /* |
2891 | | * Generate a CKA_ID from the relevant public key data. The CKA_ID is generated |
2892 | | * from the pubKeyData by SHA1_Hashing it to produce a smaller CKA_ID (to make |
2893 | | * smart cards happy. |
2894 | | */ |
2895 | | SECItem * |
2896 | | PK11_MakeIDFromPubKey(const SECItem *pubKeyData) |
2897 | 175k | { |
2898 | 175k | PK11Context *context; |
2899 | 175k | SECItem *certCKA_ID; |
2900 | 175k | SECStatus rv; |
2901 | | |
2902 | 175k | if (pubKeyData->len <= SHA1_LENGTH) { |
2903 | | /* probably an already hashed value. The strongest known public |
2904 | | * key values <= 160 bits would be less than 40 bit symetric in |
2905 | | * strength. Don't hash them, just return the value. There are |
2906 | | * none at the time of this writing supported by previous versions |
2907 | | * of NSS, so change is binary compatible safe */ |
2908 | 1.11k | return SECITEM_DupItem(pubKeyData); |
2909 | 1.11k | } |
2910 | | |
2911 | 174k | context = PK11_CreateDigestContext(SEC_OID_SHA1); |
2912 | 174k | if (context == NULL) { |
2913 | 0 | return NULL; |
2914 | 0 | } |
2915 | | |
2916 | 174k | rv = PK11_DigestBegin(context); |
2917 | 174k | if (rv == SECSuccess) { |
2918 | 174k | rv = PK11_DigestOp(context, pubKeyData->data, pubKeyData->len); |
2919 | 174k | } |
2920 | 174k | if (rv != SECSuccess) { |
2921 | 0 | PK11_DestroyContext(context, PR_TRUE); |
2922 | 0 | return NULL; |
2923 | 0 | } |
2924 | | |
2925 | 174k | certCKA_ID = (SECItem *)PORT_Alloc(sizeof(SECItem)); |
2926 | 174k | if (certCKA_ID == NULL) { |
2927 | 0 | PK11_DestroyContext(context, PR_TRUE); |
2928 | 0 | return NULL; |
2929 | 0 | } |
2930 | | |
2931 | 174k | certCKA_ID->len = SHA1_LENGTH; |
2932 | 174k | certCKA_ID->data = (unsigned char *)PORT_Alloc(certCKA_ID->len); |
2933 | 174k | if (certCKA_ID->data == NULL) { |
2934 | 0 | PORT_Free(certCKA_ID); |
2935 | 0 | PK11_DestroyContext(context, PR_TRUE); |
2936 | 0 | return NULL; |
2937 | 0 | } |
2938 | | |
2939 | 174k | rv = PK11_DigestFinal(context, certCKA_ID->data, &certCKA_ID->len, |
2940 | 174k | SHA1_LENGTH); |
2941 | 174k | PK11_DestroyContext(context, PR_TRUE); |
2942 | 174k | if (rv != SECSuccess) { |
2943 | 0 | SECITEM_FreeItem(certCKA_ID, PR_TRUE); |
2944 | 0 | return NULL; |
2945 | 0 | } |
2946 | | |
2947 | 174k | return certCKA_ID; |
2948 | 174k | } |
2949 | | |
2950 | | /* Looking for PK11_GetKeyIDFromPrivateKey? |
2951 | | * Call PK11_GetLowLevelKeyIDForPrivateKey instead. |
2952 | | */ |
2953 | | |
2954 | | SECItem * |
2955 | | PK11_GetLowLevelKeyIDForPrivateKey(SECKEYPrivateKey *privKey) |
2956 | 2.69k | { |
2957 | 2.69k | return pk11_GetLowLevelKeyFromHandle(privKey->pkcs11Slot, privKey->pkcs11ID); |
2958 | 2.69k | } |
2959 | | |
2960 | | static SECStatus |
2961 | | privateKeyListCallback(SECKEYPrivateKey *key, void *arg) |
2962 | 0 | { |
2963 | 0 | SECKEYPrivateKeyList *list = (SECKEYPrivateKeyList *)arg; |
2964 | 0 | return SECKEY_AddPrivateKeyToListTail(list, SECKEY_CopyPrivateKey(key)); |
2965 | 0 | } |
2966 | | |
2967 | | SECKEYPrivateKeyList * |
2968 | | PK11_ListPrivateKeysInSlot(PK11SlotInfo *slot) |
2969 | 0 | { |
2970 | 0 | SECStatus status; |
2971 | 0 | SECKEYPrivateKeyList *keys; |
2972 | |
|
2973 | 0 | keys = SECKEY_NewPrivateKeyList(); |
2974 | 0 | if (keys == NULL) |
2975 | 0 | return NULL; |
2976 | | |
2977 | 0 | status = PK11_TraversePrivateKeysInSlot(slot, privateKeyListCallback, |
2978 | 0 | (void *)keys); |
2979 | |
|
2980 | 0 | if (status != SECSuccess) { |
2981 | 0 | SECKEY_DestroyPrivateKeyList(keys); |
2982 | 0 | keys = NULL; |
2983 | 0 | } |
2984 | |
|
2985 | 0 | return keys; |
2986 | 0 | } |
2987 | | |
2988 | | SECKEYPublicKeyList * |
2989 | | PK11_ListPublicKeysInSlot(PK11SlotInfo *slot, char *nickname) |
2990 | 0 | { |
2991 | 0 | CK_ATTRIBUTE findTemp[4]; |
2992 | 0 | CK_ATTRIBUTE *attrs; |
2993 | 0 | CK_BBOOL ckTrue = CK_TRUE; |
2994 | 0 | CK_OBJECT_CLASS keyclass = CKO_PUBLIC_KEY; |
2995 | 0 | size_t tsize = 0; |
2996 | 0 | int objCount = 0; |
2997 | 0 | CK_OBJECT_HANDLE *key_ids; |
2998 | 0 | SECKEYPublicKeyList *keys; |
2999 | 0 | int i, len; |
3000 | |
|
3001 | 0 | attrs = findTemp; |
3002 | 0 | PK11_SETATTRS(attrs, CKA_CLASS, &keyclass, sizeof(keyclass)); |
3003 | 0 | attrs++; |
3004 | 0 | PK11_SETATTRS(attrs, CKA_TOKEN, &ckTrue, sizeof(ckTrue)); |
3005 | 0 | attrs++; |
3006 | 0 | if (nickname) { |
3007 | 0 | len = PORT_Strlen(nickname); |
3008 | 0 | PK11_SETATTRS(attrs, CKA_LABEL, nickname, len); |
3009 | 0 | attrs++; |
3010 | 0 | } |
3011 | 0 | tsize = attrs - findTemp; |
3012 | 0 | PORT_Assert(tsize <= sizeof(findTemp) / sizeof(CK_ATTRIBUTE)); |
3013 | |
|
3014 | 0 | key_ids = pk11_FindObjectsByTemplate(slot, findTemp, tsize, &objCount); |
3015 | 0 | if (key_ids == NULL) { |
3016 | 0 | return NULL; |
3017 | 0 | } |
3018 | 0 | keys = SECKEY_NewPublicKeyList(); |
3019 | 0 | if (keys == NULL) { |
3020 | 0 | PORT_Free(key_ids); |
3021 | 0 | return NULL; |
3022 | 0 | } |
3023 | | |
3024 | 0 | for (i = 0; i < objCount; i++) { |
3025 | 0 | SECKEYPublicKey *pubKey = |
3026 | 0 | PK11_ExtractPublicKey(slot, nullKey, key_ids[i]); |
3027 | 0 | if (pubKey) { |
3028 | 0 | SECKEY_AddPublicKeyToListTail(keys, pubKey); |
3029 | 0 | } |
3030 | 0 | } |
3031 | |
|
3032 | 0 | PORT_Free(key_ids); |
3033 | 0 | return keys; |
3034 | 0 | } |
3035 | | |
3036 | | SECKEYPrivateKeyList * |
3037 | | PK11_ListPrivKeysInSlot(PK11SlotInfo *slot, char *nickname, void *wincx) |
3038 | 0 | { |
3039 | 0 | CK_ATTRIBUTE findTemp[4]; |
3040 | 0 | CK_ATTRIBUTE *attrs; |
3041 | 0 | CK_BBOOL ckTrue = CK_TRUE; |
3042 | 0 | CK_OBJECT_CLASS keyclass = CKO_PRIVATE_KEY; |
3043 | 0 | size_t tsize = 0; |
3044 | 0 | int objCount = 0; |
3045 | 0 | CK_OBJECT_HANDLE *key_ids; |
3046 | 0 | SECKEYPrivateKeyList *keys; |
3047 | 0 | int i, len; |
3048 | |
|
3049 | 0 | attrs = findTemp; |
3050 | 0 | PK11_SETATTRS(attrs, CKA_CLASS, &keyclass, sizeof(keyclass)); |
3051 | 0 | attrs++; |
3052 | 0 | PK11_SETATTRS(attrs, CKA_TOKEN, &ckTrue, sizeof(ckTrue)); |
3053 | 0 | attrs++; |
3054 | 0 | if (nickname) { |
3055 | 0 | len = PORT_Strlen(nickname); |
3056 | 0 | PK11_SETATTRS(attrs, CKA_LABEL, nickname, len); |
3057 | 0 | attrs++; |
3058 | 0 | } |
3059 | 0 | tsize = attrs - findTemp; |
3060 | 0 | PORT_Assert(tsize <= sizeof(findTemp) / sizeof(CK_ATTRIBUTE)); |
3061 | |
|
3062 | 0 | key_ids = pk11_FindObjectsByTemplate(slot, findTemp, tsize, &objCount); |
3063 | 0 | if (key_ids == NULL) { |
3064 | 0 | return NULL; |
3065 | 0 | } |
3066 | 0 | keys = SECKEY_NewPrivateKeyList(); |
3067 | 0 | if (keys == NULL) { |
3068 | 0 | PORT_Free(key_ids); |
3069 | 0 | return NULL; |
3070 | 0 | } |
3071 | | |
3072 | 0 | for (i = 0; i < objCount; i++) { |
3073 | 0 | SECKEYPrivateKey *privKey = |
3074 | 0 | pk11_MakePrivKey(slot, nullKey, PR_FALSE, key_ids[i], wincx); |
3075 | 0 | SECKEY_AddPrivateKeyToListTail(keys, privKey); |
3076 | 0 | } |
3077 | |
|
3078 | 0 | PORT_Free(key_ids); |
3079 | 0 | return keys; |
3080 | 0 | } |