Coverage Report

Created: 2025-07-01 06:25

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