Coverage Report

Created: 2025-07-01 06:25

/src/nss/lib/pk11wrap/pk11pbe.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
#include "plarena.h"
6
7
#include "blapit.h"
8
#include "seccomon.h"
9
#include "secitem.h"
10
#include "secport.h"
11
#include "hasht.h"
12
#include "pkcs11t.h"
13
#include "sechash.h"
14
#include "secasn1.h"
15
#include "secder.h"
16
#include "secoid.h"
17
#include "secerr.h"
18
#include "secmod.h"
19
#include "pk11func.h"
20
#include "secpkcs5.h"
21
#include "secmodi.h"
22
#include "secmodti.h"
23
#include "pkcs11.h"
24
#include "pk11func.h"
25
#include "secitem.h"
26
#include "keyhi.h"
27
28
typedef struct SEC_PKCS5PBEParameterStr SEC_PKCS5PBEParameter;
29
struct SEC_PKCS5PBEParameterStr {
30
    PLArenaPool *poolp;
31
    SECItem salt;              /* octet string */
32
    SECItem iteration;         /* integer */
33
    SECItem keyLength;         /* PKCS5v2 only */
34
    SECAlgorithmID *pPrfAlgId; /* PKCS5v2 only */
35
    SECAlgorithmID prfAlgId;   /* PKCS5v2 only */
36
};
37
38
/* PKCS5 V2 has an algorithm ID for the encryption and for
39
 * the key generation. This is valid for SEC_OID_PKCS5_PBES2
40
 * and SEC_OID_PKCS5_PBMAC1
41
 */
42
struct sec_pkcs5V2ParameterStr {
43
    PLArenaPool *poolp;
44
    SECAlgorithmID pbeAlgId;    /* real pbe algorithms */
45
    SECAlgorithmID cipherAlgId; /* encryption/mac */
46
};
47
48
typedef struct sec_pkcs5V2ParameterStr sec_pkcs5V2Parameter;
49
50
/* template for PKCS 5 PBE Parameter.  This template has been expanded
51
 * based upon the additions in PKCS 12.  This should eventually be moved
52
 * if RSA updates PKCS 5.
53
 */
54
const SEC_ASN1Template SEC_PKCS5PBEParameterTemplate[] = {
55
    { SEC_ASN1_SEQUENCE,
56
      0, NULL, sizeof(SEC_PKCS5PBEParameter) },
57
    { SEC_ASN1_OCTET_STRING,
58
      offsetof(SEC_PKCS5PBEParameter, salt) },
59
    { SEC_ASN1_INTEGER,
60
      offsetof(SEC_PKCS5PBEParameter, iteration) },
61
    { 0 }
62
};
63
64
const SEC_ASN1Template SEC_V2PKCS12PBEParameterTemplate[] = {
65
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SEC_PKCS5PBEParameter) },
66
    { SEC_ASN1_OCTET_STRING, offsetof(SEC_PKCS5PBEParameter, salt) },
67
    { SEC_ASN1_INTEGER, offsetof(SEC_PKCS5PBEParameter, iteration) },
68
    { 0 }
69
};
70
71
SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate)
72
73
/* SECOID_PKCS5_PBKDF2 */
74
const SEC_ASN1Template SEC_PKCS5V2PBEParameterTemplate[] = {
75
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SEC_PKCS5PBEParameter) },
76
    /* This is really a choice, but since we only understand this
77
     * choice, just inline it */
78
    { SEC_ASN1_OCTET_STRING, offsetof(SEC_PKCS5PBEParameter, salt) },
79
    { SEC_ASN1_INTEGER, offsetof(SEC_PKCS5PBEParameter, iteration) },
80
    { SEC_ASN1_INTEGER | SEC_ASN1_OPTIONAL,
81
      offsetof(SEC_PKCS5PBEParameter, keyLength) },
82
    { SEC_ASN1_POINTER | SEC_ASN1_XTRN | SEC_ASN1_OPTIONAL,
83
      offsetof(SEC_PKCS5PBEParameter, pPrfAlgId),
84
      SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
85
    { 0 }
86
};
87
88
/* SEC_OID_PKCS5_PBES2, SEC_OID_PKCS5_PBMAC1 */
89
const SEC_ASN1Template SEC_PKCS5V2ParameterTemplate[] = {
90
    { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SEC_PKCS5PBEParameter) },
91
    { SEC_ASN1_INLINE | SEC_ASN1_XTRN, offsetof(sec_pkcs5V2Parameter, pbeAlgId),
92
      SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
93
    { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
94
      offsetof(sec_pkcs5V2Parameter, cipherAlgId),
95
      SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
96
    { 0 }
97
};
98
99
/*
100
 * maps a PBE algorithm to a crypto algorithm. for PKCS12 and PKCS5v1
101
 * for PKCS5v2 it returns SEC_OID_PKCS5_PBKDF2.
102
 */
103
SECOidTag
104
sec_pkcs5GetCryptoFromAlgTag(SECOidTag algorithm)
105
0
{
106
0
    switch (algorithm) {
107
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC:
108
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC:
109
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_TRIPLE_DES_CBC:
110
0
            return SEC_OID_DES_EDE3_CBC;
111
0
        case SEC_OID_PKCS5_PBE_WITH_SHA1_AND_DES_CBC:
112
0
        case SEC_OID_PKCS5_PBE_WITH_MD5_AND_DES_CBC:
113
0
        case SEC_OID_PKCS5_PBE_WITH_MD2_AND_DES_CBC:
114
0
            return SEC_OID_DES_CBC;
115
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
116
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
117
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
118
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
119
0
            return SEC_OID_RC2_CBC;
120
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC4:
121
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC4:
122
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4:
123
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4:
124
0
            return SEC_OID_RC4;
125
0
        case SEC_OID_PKCS5_PBKDF2:
126
0
        case SEC_OID_PKCS5_PBES2:
127
0
        case SEC_OID_PKCS5_PBMAC1:
128
0
            return SEC_OID_PKCS5_PBKDF2;
129
0
        default:
130
0
            break;
131
0
    }
132
133
0
    return SEC_OID_UNKNOWN;
134
0
}
135
136
/*
137
 * only gets the tag from PKCS5v1 or PKCS12pbe.
138
 * PKCS5v2 requires the algid to get the full thing
139
 */
140
SECOidTag
141
SEC_PKCS5GetHashFromAlgTag(SECOidTag algtag)
142
0
{
143
0
    switch (algtag) {
144
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC:
145
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC:
146
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_TRIPLE_DES_CBC:
147
0
        case SEC_OID_PKCS5_PBE_WITH_SHA1_AND_DES_CBC:
148
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
149
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
150
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
151
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
152
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC4:
153
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC4:
154
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4:
155
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4:
156
0
            return SEC_OID_SHA1;
157
0
        case SEC_OID_PKCS5_PBE_WITH_MD5_AND_DES_CBC:
158
0
            return SEC_OID_MD5;
159
0
        case SEC_OID_PKCS5_PBE_WITH_MD2_AND_DES_CBC:
160
0
            return SEC_OID_MD2;
161
0
        default:
162
0
            break;
163
0
    }
164
0
    return SEC_OID_UNKNOWN;
165
0
}
166
167
/*
168
 * get a new PKCS5 V2 Parameter from the algorithm id.
169
 *  if arena is passed in, use it, otherwise create a new arena.
170
 */
171
sec_pkcs5V2Parameter *
172
sec_pkcs5_v2_get_v2_param(PLArenaPool *arena, SECAlgorithmID *algid)
173
0
{
174
0
    PLArenaPool *localArena = NULL;
175
0
    sec_pkcs5V2Parameter *pbeV2_param;
176
0
    SECStatus rv;
177
178
0
    if (arena == NULL) {
179
0
        localArena = arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
180
0
        if (arena == NULL) {
181
0
            return NULL;
182
0
        }
183
0
    }
184
0
    pbeV2_param = PORT_ArenaZNew(arena, sec_pkcs5V2Parameter);
185
0
    if (pbeV2_param == NULL) {
186
0
        goto loser;
187
0
    }
188
189
0
    rv = SEC_ASN1DecodeItem(arena, pbeV2_param,
190
0
                            SEC_PKCS5V2ParameterTemplate, &algid->parameters);
191
0
    if (rv == SECFailure) {
192
0
        goto loser;
193
0
    }
194
195
0
    pbeV2_param->poolp = arena;
196
0
    return pbeV2_param;
197
0
loser:
198
0
    if (localArena) {
199
0
        PORT_FreeArena(arena, PR_FALSE);
200
0
    }
201
0
    return NULL;
202
0
}
203
204
void
205
sec_pkcs5_v2_destroy_v2_param(sec_pkcs5V2Parameter *param)
206
0
{
207
0
    if (param && param->poolp) {
208
0
        PORT_FreeArena(param->poolp, PR_TRUE);
209
0
    }
210
0
}
211
212
/* maps crypto algorithm from PBE algorithm.
213
 */
214
SECOidTag
215
SEC_PKCS5GetHashAlgorithm(SECAlgorithmID *algid)
216
0
{
217
218
0
    SECOidTag pbeAlg;
219
0
    SECOidTag hashAlg = SEC_OID_UNKNOWN;
220
0
    PLArenaPool *arena = NULL;
221
222
0
    if (algid == NULL)
223
0
        return SEC_OID_UNKNOWN;
224
225
0
    pbeAlg = SECOID_GetAlgorithmTag(algid);
226
    /* if we are using a PKCS 5v2 algorithm, get the hash from the parameters */
227
0
    if ((pbeAlg == SEC_OID_PKCS5_PBES2) ||
228
0
        (pbeAlg == SEC_OID_PKCS5_PBMAC1)) {
229
0
        SEC_PKCS5PBEParameter p5_param;
230
0
        sec_pkcs5V2Parameter *pbeV2_param;
231
0
        SECOidTag kdfAlg;
232
0
        SECStatus rv;
233
234
0
        arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
235
0
        if (arena == NULL) {
236
0
            goto loser;
237
0
        }
238
239
0
        pbeV2_param = sec_pkcs5_v2_get_v2_param(arena, algid);
240
0
        if (pbeV2_param == NULL) {
241
0
            goto loser;
242
0
        }
243
244
0
        kdfAlg = SECOID_GetAlgorithmTag(&pbeV2_param->pbeAlgId);
245
        /* sanity check, they should all be PBKDF2 here */
246
0
        if (kdfAlg != SEC_OID_PKCS5_PBKDF2) {
247
0
            goto loser;
248
0
        }
249
250
0
        PORT_Memset(&p5_param, 0, sizeof(p5_param));
251
0
        rv = SEC_ASN1DecodeItem(arena, &p5_param,
252
0
                                SEC_PKCS5V2PBEParameterTemplate,
253
0
                                &pbeV2_param->pbeAlgId.parameters);
254
0
        if (rv != SECSuccess) {
255
0
            goto loser;
256
0
        }
257
        /* if the prf does not exist, it defaults to SHA1 */
258
0
        hashAlg = SEC_OID_SHA1;
259
0
        if (p5_param.pPrfAlgId &&
260
0
            p5_param.pPrfAlgId->algorithm.data != 0) {
261
0
            hashAlg = HASH_GetHashOidTagByHMACOidTag(
262
0
                SECOID_GetAlgorithmTag(p5_param.pPrfAlgId));
263
0
        }
264
0
    } else {
265
0
        return SEC_PKCS5GetHashFromAlgTag(pbeAlg);
266
0
    }
267
0
loser:
268
0
    if (arena != NULL) {
269
0
        PORT_FreeArena(arena, PR_FALSE);
270
0
    }
271
272
0
    return hashAlg;
273
0
}
274
275
/* maps crypto algorithm from PBE algorithm.
276
 */
277
SECOidTag
278
SEC_PKCS5GetCryptoAlgorithm(SECAlgorithmID *algid)
279
0
{
280
281
0
    SECOidTag pbeAlg;
282
0
    SECOidTag cipherAlg;
283
284
0
    if (algid == NULL)
285
0
        return SEC_OID_UNKNOWN;
286
287
0
    pbeAlg = SECOID_GetAlgorithmTag(algid);
288
0
    cipherAlg = sec_pkcs5GetCryptoFromAlgTag(pbeAlg);
289
0
    if ((cipherAlg == SEC_OID_PKCS5_PBKDF2) &&
290
0
        (pbeAlg != SEC_OID_PKCS5_PBKDF2)) {
291
0
        sec_pkcs5V2Parameter *pbeV2_param;
292
0
        cipherAlg = SEC_OID_UNKNOWN;
293
294
0
        pbeV2_param = sec_pkcs5_v2_get_v2_param(NULL, algid);
295
0
        if (pbeV2_param != NULL) {
296
0
            cipherAlg = SECOID_GetAlgorithmTag(&pbeV2_param->cipherAlgId);
297
0
            sec_pkcs5_v2_destroy_v2_param(pbeV2_param);
298
0
        }
299
0
    }
300
301
0
    return cipherAlg;
302
0
}
303
304
/*
305
 * only gets the tag from PKCS5v1 or PKCS12pbe.
306
 * PKCS5v2 requires the algid to get the full thing
307
 */
308
SECOidTag
309
SEC_PKCS5GetCryptoFromAlgTag(SECOidTag algtag)
310
0
{
311
0
    SECOidTag cipherAlg;
312
0
    cipherAlg = sec_pkcs5GetCryptoFromAlgTag(algtag);
313
0
    if (cipherAlg == SEC_OID_PKCS5_PBKDF2) {
314
0
        return SEC_OID_UNKNOWN;
315
0
    }
316
0
    return cipherAlg;
317
0
}
318
319
/* check to see if an oid is a pbe algorithm
320
 */
321
PRBool
322
SEC_PKCS5IsAlgorithmPBEAlg(SECAlgorithmID *algid)
323
0
{
324
0
    return (PRBool)(SEC_PKCS5GetCryptoAlgorithm(algid) != SEC_OID_UNKNOWN);
325
0
}
326
327
PRBool
328
SEC_PKCS5IsAlgorithmPBEAlgTag(SECOidTag algtag)
329
0
{
330
0
    return (PRBool)(sec_pkcs5GetCryptoFromAlgTag(algtag) != SEC_OID_UNKNOWN);
331
0
}
332
333
/*
334
 * find the most appropriate PKCS5v2 overall oid tag from a regular
335
 * cipher/hash algorithm tag.
336
 */
337
static SECOidTag
338
sec_pkcs5v2_get_pbe(SECOidTag algTag)
339
0
{
340
    /* if it's a valid hash oid... */
341
0
    if (HASH_GetHashOidTagByHMACOidTag(algTag) != SEC_OID_UNKNOWN) {
342
        /* use the MAC tag */
343
0
        return SEC_OID_PKCS5_PBMAC1;
344
0
    }
345
0
    if (HASH_GetHashTypeByOidTag(algTag) != HASH_AlgNULL) {
346
        /* eliminate Hash algorithms */
347
0
        return SEC_OID_UNKNOWN;
348
0
    }
349
0
    if (PK11_AlgtagToMechanism(algTag) != CKM_INVALID_MECHANISM) {
350
        /* it's not a hash, if it has a PKCS #11 mechanism associated
351
         * with it, assume it's a cipher. (NOTE this will generate
352
         * some false positives). */
353
0
        return SEC_OID_PKCS5_PBES2;
354
0
    }
355
0
    return SEC_OID_UNKNOWN;
356
0
}
357
358
/*
359
 * maps PBE algorithm from crypto algorithm, assumes SHA1 hashing.
360
 *  input keyLen in bits.
361
 */
362
SECOidTag
363
SEC_PKCS5GetPBEAlgorithm(SECOidTag algTag, int keyLen)
364
0
{
365
0
    switch (algTag) {
366
0
        case SEC_OID_DES_EDE3_CBC:
367
0
            switch (keyLen) {
368
0
                case 168:
369
0
                case 192:
370
0
                case 0:
371
0
                    return SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC;
372
0
                case 128:
373
0
                case 92:
374
0
                    return SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC;
375
0
                default:
376
0
                    break;
377
0
            }
378
0
            break;
379
0
        case SEC_OID_DES_CBC:
380
0
            return SEC_OID_PKCS5_PBE_WITH_SHA1_AND_DES_CBC;
381
0
        case SEC_OID_RC2_CBC:
382
0
            switch (keyLen) {
383
0
                case 40:
384
0
                    return SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC;
385
0
                case 128:
386
0
                case 0:
387
0
                    return SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC;
388
0
                default:
389
0
                    break;
390
0
            }
391
0
            break;
392
0
        case SEC_OID_RC4:
393
0
            switch (keyLen) {
394
0
                case 40:
395
0
                    return SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4;
396
0
                case 128:
397
0
                case 0:
398
0
                    return SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4;
399
0
                default:
400
0
                    break;
401
0
            }
402
0
            break;
403
0
        default:
404
0
            return sec_pkcs5v2_get_pbe(algTag);
405
0
    }
406
407
0
    return SEC_OID_UNKNOWN;
408
0
}
409
410
/*
411
 * Some oids encode the key size in the oid, while the actual PKCS
412
 * PKCS #11 mechanism does not. In those cases we can't use
413
 * the PKCS #11 automated key length code to select the key size.
414
 */
415
static int
416
sec_pkcs5v2_key_length_by_oid(SECOidTag algorithm)
417
0
{
418
0
    switch (algorithm) {
419
0
        case SEC_OID_AES_128_CBC:
420
0
        case SEC_OID_CAMELLIA_128_CBC:
421
0
            return AES_128_KEY_LENGTH;
422
0
        case SEC_OID_AES_192_CBC:
423
0
        case SEC_OID_CAMELLIA_192_CBC:
424
0
            return AES_192_KEY_LENGTH;
425
0
        case SEC_OID_AES_256_CBC:
426
0
        case SEC_OID_CAMELLIA_256_CBC:
427
0
            return AES_256_KEY_LENGTH;
428
0
        default:
429
0
            break;
430
0
    }
431
0
    return -1;
432
0
}
433
434
/* find the keylength from the algorithm id */
435
static int
436
sec_pkcs5v2_default_key_length(SECOidTag algorithm)
437
0
{
438
0
    CK_MECHANISM_TYPE cryptoMech;
439
0
    int key_length = sec_pkcs5v2_key_length_by_oid(algorithm);
440
0
    if (key_length != -1) {
441
0
        return key_length;
442
0
    }
443
0
    cryptoMech = PK11_AlgtagToMechanism(algorithm);
444
0
    if (cryptoMech == CKM_INVALID_MECHANISM) {
445
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
446
0
        return -1;
447
0
    }
448
0
    return PK11_GetMaxKeyLength(cryptoMech);
449
0
}
450
451
/*
452
 * get the key length in bytes from a PKCS5 PBE
453
 */
454
static int
455
sec_pkcs5v2_key_length(SECAlgorithmID *algid, SECAlgorithmID *cipherAlgId)
456
0
{
457
0
    SECOidTag algorithm;
458
0
    PLArenaPool *arena = NULL;
459
0
    SEC_PKCS5PBEParameter p5_param;
460
0
    SECStatus rv;
461
0
    int length = -1;
462
0
    SECOidTag cipherAlg = SEC_OID_UNKNOWN;
463
464
0
    algorithm = SECOID_GetAlgorithmTag(algid);
465
    /* sanity check, they should all be PBKDF2 here */
466
0
    if (algorithm != SEC_OID_PKCS5_PBKDF2) {
467
0
        return -1;
468
0
    }
469
470
0
    arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
471
0
    if (arena == NULL) {
472
0
        goto loser;
473
0
    }
474
0
    PORT_Memset(&p5_param, 0, sizeof(p5_param));
475
0
    rv = SEC_ASN1DecodeItem(arena, &p5_param,
476
0
                            SEC_PKCS5V2PBEParameterTemplate, &algid->parameters);
477
0
    if (rv != SECSuccess) {
478
0
        goto loser;
479
0
    }
480
481
0
    if (cipherAlgId)
482
0
        cipherAlg = SECOID_GetAlgorithmTag(cipherAlgId);
483
484
0
    if (p5_param.keyLength.data != NULL) {
485
        /* if the length is given, accept that length. This
486
         * will allow us to decode old NSS encrypted data
487
         * where we used the MAX keysize for the algorithm,
488
         * but put an incorrect header for a different keysize.
489
         */
490
0
        length = DER_GetInteger(&p5_param.keyLength);
491
0
    } else {
492
        /* if the keylength was not specified, figure it
493
         * out from the oid */
494
0
        length = sec_pkcs5v2_default_key_length(cipherAlg);
495
0
    }
496
497
0
loser:
498
0
    if (arena) {
499
0
        PORT_FreeArena(arena, PR_FALSE);
500
0
    }
501
0
    return length;
502
0
}
503
504
/*
505
 *  get the key length in bytes needed for the PBE algorithm
506
 */
507
int
508
SEC_PKCS5GetKeyLength(SECAlgorithmID *algid)
509
0
{
510
511
0
    SECOidTag algorithm;
512
513
0
    if (algid == NULL)
514
0
        return SEC_OID_UNKNOWN;
515
516
0
    algorithm = SECOID_GetAlgorithmTag(algid);
517
518
0
    switch (algorithm) {
519
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC:
520
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_TRIPLE_DES_CBC:
521
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC:
522
0
            return 24;
523
0
        case SEC_OID_PKCS5_PBE_WITH_MD2_AND_DES_CBC:
524
0
        case SEC_OID_PKCS5_PBE_WITH_SHA1_AND_DES_CBC:
525
0
        case SEC_OID_PKCS5_PBE_WITH_MD5_AND_DES_CBC:
526
0
            return 8;
527
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
528
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC4:
529
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4:
530
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
531
0
            return 5;
532
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
533
0
        case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC4:
534
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
535
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4:
536
0
            return 16;
537
0
        case SEC_OID_PKCS5_PBKDF2:
538
0
            return sec_pkcs5v2_key_length(algid, NULL);
539
0
        case SEC_OID_PKCS5_PBES2:
540
0
        case SEC_OID_PKCS5_PBMAC1: {
541
0
            sec_pkcs5V2Parameter *pbeV2_param;
542
0
            int length = -1;
543
0
            pbeV2_param = sec_pkcs5_v2_get_v2_param(NULL, algid);
544
0
            if (pbeV2_param != NULL) {
545
0
                length = sec_pkcs5v2_key_length(&pbeV2_param->pbeAlgId,
546
0
                                                &pbeV2_param->cipherAlgId);
547
0
                sec_pkcs5_v2_destroy_v2_param(pbeV2_param);
548
0
            }
549
0
            return length;
550
0
        }
551
552
0
        default:
553
0
            break;
554
0
    }
555
0
    return -1;
556
0
}
557
558
/* the PKCS12 V2 algorithms only encode the salt, there is no iteration
559
 * count so we need a check for V2 algorithm parameters.
560
 */
561
static PRBool
562
sec_pkcs5_is_algorithm_v2_pkcs12_algorithm(SECOidTag algorithm)
563
0
{
564
0
    switch (algorithm) {
565
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4:
566
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4:
567
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC:
568
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC:
569
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
570
0
        case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
571
0
            return PR_TRUE;
572
0
        default:
573
0
            break;
574
0
    }
575
576
0
    return PR_FALSE;
577
0
}
578
579
static PRBool
580
sec_pkcs5_is_algorithm_v2_pkcs5_algorithm(SECOidTag algorithm)
581
0
{
582
0
    switch (algorithm) {
583
0
        case SEC_OID_PKCS5_PBES2:
584
0
        case SEC_OID_PKCS5_PBMAC1:
585
0
        case SEC_OID_PKCS5_PBKDF2:
586
0
            return PR_TRUE;
587
0
        default:
588
0
            break;
589
0
    }
590
591
0
    return PR_FALSE;
592
0
}
593
594
/* destroy a pbe parameter.  it assumes that the parameter was
595
 * generated using the appropriate create function and therefor
596
 * contains an arena pool.
597
 */
598
static void
599
sec_pkcs5_destroy_pbe_param(SEC_PKCS5PBEParameter *pbe_param)
600
0
{
601
0
    if (pbe_param != NULL)
602
0
        PORT_FreeArena(pbe_param->poolp, PR_TRUE);
603
0
}
604
605
/* creates a PBE parameter based on the PBE algorithm.  the only required
606
 * parameters are algorithm and interation.  the return is a PBE parameter
607
 * which conforms to PKCS 5 parameter unless an extended parameter is needed.
608
 * this is primarily if keyLength and a variable key length algorithm are
609
 * specified.
610
 *   salt -  if null, a salt will be generated from random bytes.
611
 *   iteration - number of iterations to perform hashing.
612
 *   keyLength - only used in variable key length algorithms. if specified,
613
 *            should be in bytes.
614
 * once a parameter is allocated, it should be destroyed calling
615
 * sec_pkcs5_destroy_pbe_parameter or SEC_PKCS5DestroyPBEParameter.
616
 */
617
0
#define DEFAULT_SALT_LENGTH 16
618
static SEC_PKCS5PBEParameter *
619
sec_pkcs5_create_pbe_parameter(SECOidTag algorithm,
620
                               SECItem *salt,
621
                               int iteration,
622
                               int keyLength,
623
                               SECOidTag prfAlg)
624
0
{
625
0
    PLArenaPool *poolp = NULL;
626
0
    SEC_PKCS5PBEParameter *pbe_param = NULL;
627
0
    SECStatus rv = SECSuccess;
628
0
    void *dummy = NULL;
629
630
0
    if (iteration < 0) {
631
0
        return NULL;
632
0
    }
633
634
0
    poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
635
0
    if (poolp == NULL)
636
0
        return NULL;
637
638
0
    pbe_param = (SEC_PKCS5PBEParameter *)PORT_ArenaZAlloc(poolp,
639
0
                                                          sizeof(SEC_PKCS5PBEParameter));
640
0
    if (!pbe_param) {
641
0
        PORT_FreeArena(poolp, PR_TRUE);
642
0
        return NULL;
643
0
    }
644
645
0
    pbe_param->poolp = poolp;
646
647
0
    rv = SECFailure;
648
0
    if (salt && salt->data) {
649
0
        rv = SECITEM_CopyItem(poolp, &pbe_param->salt, salt);
650
0
    } else {
651
        /* sigh, the old interface generated salt on the fly, so we have to
652
         * preserve the semantics */
653
0
        pbe_param->salt.len = DEFAULT_SALT_LENGTH;
654
0
        pbe_param->salt.data = PORT_ArenaZAlloc(poolp, DEFAULT_SALT_LENGTH);
655
0
        if (pbe_param->salt.data) {
656
0
            rv = PK11_GenerateRandom(pbe_param->salt.data, DEFAULT_SALT_LENGTH);
657
0
        }
658
0
    }
659
660
0
    if (rv != SECSuccess) {
661
0
        PORT_FreeArena(poolp, PR_TRUE);
662
0
        return NULL;
663
0
    }
664
665
    /* encode the integer */
666
0
    dummy = SEC_ASN1EncodeInteger(poolp, &pbe_param->iteration,
667
0
                                  iteration);
668
0
    rv = (dummy) ? SECSuccess : SECFailure;
669
670
0
    if (rv != SECSuccess) {
671
0
        PORT_FreeArena(poolp, PR_FALSE);
672
0
        return NULL;
673
0
    }
674
675
    /*
676
     * for PKCS5 v2 Add the keylength and the prf
677
     */
678
0
    if (algorithm == SEC_OID_PKCS5_PBKDF2) {
679
0
        dummy = SEC_ASN1EncodeInteger(poolp, &pbe_param->keyLength,
680
0
                                      keyLength);
681
0
        rv = (dummy) ? SECSuccess : SECFailure;
682
0
        if (rv != SECSuccess) {
683
0
            PORT_FreeArena(poolp, PR_FALSE);
684
0
            return NULL;
685
0
        }
686
0
        rv = SECOID_SetAlgorithmID(poolp, &pbe_param->prfAlgId, prfAlg, NULL);
687
0
        if (rv != SECSuccess) {
688
0
            PORT_FreeArena(poolp, PR_FALSE);
689
0
            return NULL;
690
0
        }
691
0
        pbe_param->pPrfAlgId = &pbe_param->prfAlgId;
692
0
    }
693
694
0
    return pbe_param;
695
0
}
696
697
/* creates a algorithm ID containing the PBE algorithm and appropriate
698
 * parameters.  the required parameter is the algorithm.  if salt is
699
 * not specified, it is generated randomly.
700
 *
701
 * the returned SECAlgorithmID should be destroyed using
702
 * SECOID_DestroyAlgorithmID
703
 */
704
SECAlgorithmID *
705
sec_pkcs5CreateAlgorithmID(SECOidTag algorithm,
706
                           SECOidTag cipherAlgorithm,
707
                           SECOidTag prfAlg,
708
                           SECOidTag *pPbeAlgorithm,
709
                           int keyLength,
710
                           SECItem *salt,
711
                           int iteration)
712
0
{
713
0
    PLArenaPool *poolp = NULL;
714
0
    SECAlgorithmID *algid, *ret_algid = NULL;
715
0
    SECOidTag pbeAlgorithm = algorithm;
716
0
    SECItem der_param;
717
0
    void *dummy;
718
0
    SECStatus rv = SECFailure;
719
0
    SEC_PKCS5PBEParameter *pbe_param = NULL;
720
0
    sec_pkcs5V2Parameter pbeV2_param;
721
722
0
    if (iteration <= 0) {
723
0
        return NULL;
724
0
    }
725
726
0
    poolp = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
727
0
    if (!poolp) {
728
0
        goto loser;
729
0
    }
730
731
0
    if (!SEC_PKCS5IsAlgorithmPBEAlgTag(algorithm) ||
732
0
        sec_pkcs5_is_algorithm_v2_pkcs5_algorithm(algorithm)) {
733
        /* use PKCS 5 v2 */
734
0
        SECItem *cipherParams;
735
736
        /*
737
         * if we ask for pkcs5 Algorithms directly, then the
738
         * application needs to supply the cipher algorithm,
739
         * otherwise we are implicitly using pkcs5 v2 and the
740
         * passed in algorithm is the encryption algorithm.
741
         */
742
0
        if (sec_pkcs5_is_algorithm_v2_pkcs5_algorithm(algorithm)) {
743
0
            if (cipherAlgorithm == SEC_OID_UNKNOWN) {
744
0
                goto loser;
745
0
            }
746
0
        } else {
747
0
            cipherAlgorithm = algorithm;
748
            /* force algorithm to be chosen below */
749
0
            algorithm = SEC_OID_PKCS5_PBKDF2;
750
0
        }
751
752
0
        pbeAlgorithm = SEC_OID_PKCS5_PBKDF2;
753
        /*
754
         * 'algorithm' is the overall algorithm oid tag used to wrap the
755
         * entire algorithm ID block. For PKCS5v1 and PKCS12, this
756
         * algorithm OID has encoded in it both the PBE KDF function
757
         * and the encryption algorithm. For PKCS 5v2, PBE KDF and
758
         * encryption/macing oids are encoded as parameters in
759
         * the algorithm ID block.
760
         *
761
         * Thus in PKCS5 v1 and PKCS12, this algorithm maps to a pkcs #11
762
         * mechanism, where as in PKCS 5v2, this algorithm tag does not map
763
         * directly to a PKCS #11 mechanim, instead the 2 oids in the
764
         * algorithm ID block map the the actual PKCS #11 mechanism.
765
         * algorithm is). We use choose this algorithm oid based on the
766
         * cipherAlgorithm to determine what this should be (MAC1 or PBES2).
767
         */
768
0
        if (algorithm == SEC_OID_PKCS5_PBKDF2) {
769
            /* choose mac or pbes */
770
0
            algorithm = sec_pkcs5v2_get_pbe(cipherAlgorithm);
771
0
        }
772
773
0
        SECOidTag hashAlg = HASH_GetHashOidTagByHMACOidTag(cipherAlgorithm);
774
775
        /* set the PKCS5v2 specific parameters */
776
0
        if (keyLength == 0) {
777
0
            if (hashAlg != SEC_OID_UNKNOWN) {
778
0
                keyLength = HASH_ResultLenByOidTag(hashAlg);
779
0
            } else {
780
0
                keyLength = sec_pkcs5v2_default_key_length(cipherAlgorithm);
781
0
            }
782
0
            if (keyLength <= 0) {
783
0
                goto loser;
784
0
            }
785
0
        }
786
        /* currently SEC_OID_HMAC_SHA1 is the default */
787
0
        if (prfAlg == SEC_OID_UNKNOWN) {
788
0
            prfAlg = SEC_OID_HMAC_SHA1;
789
0
        }
790
791
        /* build the PKCS5v2 cipher algorithm id, if cipher
792
         * is an HMAC, the cipherParams should be NULL */
793
0
        if (hashAlg == SEC_OID_UNKNOWN) {
794
0
            cipherParams = pk11_GenerateNewParamWithKeyLen(
795
0
                PK11_AlgtagToMechanism(cipherAlgorithm), keyLength);
796
0
            if (!cipherParams) {
797
0
                goto loser;
798
0
            }
799
0
        } else {
800
0
            cipherParams = NULL;
801
0
        }
802
803
0
        PORT_Memset(&pbeV2_param, 0, sizeof(pbeV2_param));
804
805
0
        rv = PK11_ParamToAlgid(cipherAlgorithm, cipherParams,
806
0
                               poolp, &pbeV2_param.cipherAlgId);
807
0
        if (cipherParams) {
808
0
            SECITEM_FreeItem(cipherParams, PR_TRUE);
809
0
        }
810
0
        if (rv != SECSuccess) {
811
0
            goto loser;
812
0
        }
813
0
    }
814
815
    /* generate the parameter */
816
0
    pbe_param = sec_pkcs5_create_pbe_parameter(pbeAlgorithm, salt, iteration,
817
0
                                               keyLength, prfAlg);
818
0
    if (!pbe_param) {
819
0
        goto loser;
820
0
    }
821
822
    /* generate the algorithm id */
823
0
    algid = (SECAlgorithmID *)PORT_ArenaZAlloc(poolp, sizeof(SECAlgorithmID));
824
0
    if (algid == NULL) {
825
0
        goto loser;
826
0
    }
827
828
0
    der_param.data = NULL;
829
0
    der_param.len = 0;
830
0
    if (sec_pkcs5_is_algorithm_v2_pkcs5_algorithm(algorithm)) {
831
        /* first encode the PBE algorithm ID */
832
0
        dummy = SEC_ASN1EncodeItem(poolp, &der_param, pbe_param,
833
0
                                   SEC_PKCS5V2PBEParameterTemplate);
834
0
        if (dummy == NULL) {
835
0
            goto loser;
836
0
        }
837
0
        rv = SECOID_SetAlgorithmID(poolp, &pbeV2_param.pbeAlgId,
838
0
                                   pbeAlgorithm, &der_param);
839
0
        if (rv != SECSuccess) {
840
0
            goto loser;
841
0
        }
842
843
        /* now encode the Full PKCS 5 parameter */
844
0
        der_param.data = NULL;
845
0
        der_param.len = 0;
846
0
        dummy = SEC_ASN1EncodeItem(poolp, &der_param, &pbeV2_param,
847
0
                                   SEC_PKCS5V2ParameterTemplate);
848
0
    } else if (!sec_pkcs5_is_algorithm_v2_pkcs12_algorithm(algorithm)) {
849
0
        dummy = SEC_ASN1EncodeItem(poolp, &der_param, pbe_param,
850
0
                                   SEC_PKCS5PBEParameterTemplate);
851
0
    } else {
852
0
        dummy = SEC_ASN1EncodeItem(poolp, &der_param, pbe_param,
853
0
                                   SEC_V2PKCS12PBEParameterTemplate);
854
0
    }
855
0
    if (dummy == NULL) {
856
0
        goto loser;
857
0
    }
858
859
0
    rv = SECOID_SetAlgorithmID(poolp, algid, algorithm, &der_param);
860
0
    if (rv != SECSuccess) {
861
0
        goto loser;
862
0
    }
863
864
0
    ret_algid = (SECAlgorithmID *)PORT_ZAlloc(sizeof(SECAlgorithmID));
865
0
    if (ret_algid == NULL) {
866
0
        goto loser;
867
0
    }
868
869
0
    rv = SECOID_CopyAlgorithmID(NULL, ret_algid, algid);
870
0
    if (rv != SECSuccess) {
871
0
        SECOID_DestroyAlgorithmID(ret_algid, PR_TRUE);
872
0
        ret_algid = NULL;
873
0
    } else if (pPbeAlgorithm) {
874
0
        *pPbeAlgorithm = pbeAlgorithm;
875
0
    }
876
877
0
loser:
878
0
    if (poolp != NULL) {
879
0
        PORT_FreeArena(poolp, PR_TRUE);
880
0
        algid = NULL;
881
0
    }
882
883
0
    if (pbe_param) {
884
0
        sec_pkcs5_destroy_pbe_param(pbe_param);
885
0
    }
886
887
0
    return ret_algid;
888
0
}
889
890
SECStatus
891
pbe_PK11AlgidToParam(SECAlgorithmID *algid, SECItem *mech)
892
0
{
893
0
    SEC_PKCS5PBEParameter p5_param;
894
0
    SECItem *salt = NULL;
895
0
    SECOidTag algorithm = SECOID_GetAlgorithmTag(algid);
896
0
    PLArenaPool *arena = NULL;
897
0
    SECStatus rv = SECFailure;
898
0
    unsigned char *paramData = NULL;
899
0
    unsigned char *pSalt = NULL;
900
0
    CK_ULONG iterations;
901
0
    int paramLen = 0;
902
0
    int iv_len = -1;
903
904
0
    arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
905
0
    if (arena == NULL) {
906
0
        goto loser;
907
0
    }
908
909
    /*
910
     * decode the algid based on the pbe type
911
     */
912
0
    PORT_Memset(&p5_param, 0, sizeof(p5_param));
913
0
    if (sec_pkcs5_is_algorithm_v2_pkcs12_algorithm(algorithm)) {
914
0
        iv_len = PK11_GetIVLength(PK11_AlgtagToMechanism(algorithm));
915
0
        rv = SEC_ASN1DecodeItem(arena, &p5_param,
916
0
                                SEC_V2PKCS12PBEParameterTemplate, &algid->parameters);
917
0
    } else if (algorithm == SEC_OID_PKCS5_PBKDF2) {
918
0
        iv_len = 0;
919
0
        rv = SEC_ASN1DecodeItem(arena, &p5_param,
920
0
                                SEC_PKCS5V2PBEParameterTemplate, &algid->parameters);
921
0
    } else {
922
0
        iv_len = PK11_GetIVLength(PK11_AlgtagToMechanism(algorithm));
923
0
        rv = SEC_ASN1DecodeItem(arena, &p5_param, SEC_PKCS5PBEParameterTemplate,
924
0
                                &algid->parameters);
925
0
    }
926
927
0
    if (iv_len < 0) {
928
0
        goto loser;
929
0
    }
930
931
0
    if (rv != SECSuccess) {
932
0
        goto loser;
933
0
    }
934
935
    /* get salt */
936
0
    salt = &p5_param.salt;
937
0
    iterations = (CK_ULONG)DER_GetInteger(&p5_param.iteration);
938
939
    /* allocate and fill in the PKCS #11 parameters
940
     * based on the algorithm. */
941
0
    if (algorithm == SEC_OID_PKCS5_PBKDF2) {
942
0
        SECOidTag prfAlgTag;
943
0
        CK_PKCS5_PBKD2_PARAMS *pbeV2_params =
944
0
            (CK_PKCS5_PBKD2_PARAMS *)PORT_ZAlloc(
945
0
                sizeof(CK_PKCS5_PBKD2_PARAMS) + salt->len);
946
947
0
        if (pbeV2_params == NULL) {
948
0
            goto loser;
949
0
        }
950
0
        paramData = (unsigned char *)pbeV2_params;
951
0
        paramLen = sizeof(CK_PKCS5_PBKD2_PARAMS);
952
953
        /* set the prf */
954
0
        prfAlgTag = SEC_OID_HMAC_SHA1;
955
0
        if (p5_param.pPrfAlgId &&
956
0
            p5_param.pPrfAlgId->algorithm.data != 0) {
957
0
            prfAlgTag = SECOID_GetAlgorithmTag(p5_param.pPrfAlgId);
958
0
        }
959
0
        switch (prfAlgTag) {
960
0
            case SEC_OID_HMAC_SHA1:
961
0
                pbeV2_params->prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
962
0
                break;
963
0
            case SEC_OID_HMAC_SHA224:
964
0
                pbeV2_params->prf = CKP_PKCS5_PBKD2_HMAC_SHA224;
965
0
                break;
966
0
            case SEC_OID_HMAC_SHA256:
967
0
                pbeV2_params->prf = CKP_PKCS5_PBKD2_HMAC_SHA256;
968
0
                break;
969
0
            case SEC_OID_HMAC_SHA384:
970
0
                pbeV2_params->prf = CKP_PKCS5_PBKD2_HMAC_SHA384;
971
0
                break;
972
0
            case SEC_OID_HMAC_SHA512:
973
0
                pbeV2_params->prf = CKP_PKCS5_PBKD2_HMAC_SHA512;
974
0
                break;
975
0
            default:
976
0
                PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
977
0
                goto loser;
978
0
        }
979
980
        /* probably should fetch these from the prfAlgid */
981
0
        pbeV2_params->pPrfData = NULL;
982
0
        pbeV2_params->ulPrfDataLen = 0;
983
0
        pbeV2_params->saltSource = CKZ_SALT_SPECIFIED;
984
0
        pSalt = ((CK_CHAR_PTR)pbeV2_params) + sizeof(CK_PKCS5_PBKD2_PARAMS);
985
0
        if (salt->data) {
986
0
            PORT_Memcpy(pSalt, salt->data, salt->len);
987
0
        }
988
0
        pbeV2_params->pSaltSourceData = pSalt;
989
0
        pbeV2_params->ulSaltSourceDataLen = salt->len;
990
0
        pbeV2_params->iterations = iterations;
991
0
    } else {
992
0
        CK_PBE_PARAMS *pbe_params = NULL;
993
0
        pbe_params = (CK_PBE_PARAMS *)PORT_ZAlloc(sizeof(CK_PBE_PARAMS) +
994
0
                                                  salt->len + iv_len);
995
0
        if (pbe_params == NULL) {
996
0
            goto loser;
997
0
        }
998
0
        paramData = (unsigned char *)pbe_params;
999
0
        paramLen = sizeof(CK_PBE_PARAMS);
1000
1001
0
        pSalt = ((CK_CHAR_PTR)pbe_params) + sizeof(CK_PBE_PARAMS);
1002
0
        pbe_params->pSalt = pSalt;
1003
0
        if (salt->data) {
1004
0
            PORT_Memcpy(pSalt, salt->data, salt->len);
1005
0
        }
1006
0
        pbe_params->ulSaltLen = salt->len;
1007
0
        if (iv_len) {
1008
0
            pbe_params->pInitVector =
1009
0
                ((CK_CHAR_PTR)pbe_params) + sizeof(CK_PBE_PARAMS) + salt->len;
1010
0
        }
1011
0
        pbe_params->ulIteration = iterations;
1012
0
    }
1013
1014
    /* copy into the mechanism sec item */
1015
0
    mech->data = paramData;
1016
0
    mech->len = paramLen;
1017
0
    if (arena) {
1018
0
        PORT_FreeArena(arena, PR_TRUE);
1019
0
    }
1020
0
    return SECSuccess;
1021
1022
0
loser:
1023
0
    if (paramData) {
1024
0
        PORT_Free(paramData);
1025
0
    }
1026
0
    if (arena) {
1027
0
        PORT_FreeArena(arena, PR_TRUE);
1028
0
    }
1029
0
    return SECFailure;
1030
0
}
1031
1032
/*
1033
 * public, deprecated, not valid for pkcs5 v2
1034
 *
1035
 * use PK11_CreatePBEV2AlgorithmID or PK11_CreatePBEAlgorithmID to create
1036
 * PBE algorithmID's directly.
1037
 */
1038
SECStatus
1039
PBE_PK11ParamToAlgid(SECOidTag algTag, SECItem *param, PLArenaPool *arena,
1040
                     SECAlgorithmID *algId)
1041
0
{
1042
0
    CK_PBE_PARAMS *pbe_param;
1043
0
    SECItem pbeSalt;
1044
0
    SECAlgorithmID *pbeAlgID = NULL;
1045
0
    SECStatus rv;
1046
1047
0
    if (!param || !algId) {
1048
0
        return SECFailure;
1049
0
    }
1050
1051
0
    pbe_param = (CK_PBE_PARAMS *)param->data;
1052
0
    pbeSalt.data = (unsigned char *)pbe_param->pSalt;
1053
0
    pbeSalt.len = pbe_param->ulSaltLen;
1054
0
    pbeAlgID = sec_pkcs5CreateAlgorithmID(algTag, SEC_OID_UNKNOWN,
1055
0
                                          SEC_OID_UNKNOWN, NULL, 0,
1056
0
                                          &pbeSalt, (int)pbe_param->ulIteration);
1057
0
    if (!pbeAlgID) {
1058
0
        return SECFailure;
1059
0
    }
1060
1061
0
    rv = SECOID_CopyAlgorithmID(arena, algId, pbeAlgID);
1062
0
    SECOID_DestroyAlgorithmID(pbeAlgID, PR_TRUE);
1063
0
    return rv;
1064
0
}
1065
1066
/*
1067
 * public, Deprecated, This function is only for binary compatibility with
1068
 * older applications. Does not support PKCS5v2.
1069
 *
1070
 * Applications should use PK11_PBEKeyGen() for keys and PK11_GetPBEIV() for
1071
 * iv values rather than generating PBE bits directly.
1072
 */
1073
PBEBitGenContext *
1074
PBE_CreateContext(SECOidTag hashAlgorithm, PBEBitGenID bitGenPurpose,
1075
                  SECItem *pwitem, SECItem *salt, unsigned int bitsNeeded,
1076
                  unsigned int iterations)
1077
0
{
1078
0
    SECItem *context = NULL;
1079
0
    SECItem mechItem;
1080
0
    CK_PBE_PARAMS pbe_params;
1081
0
    CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM;
1082
0
    PK11SlotInfo *slot;
1083
0
    PK11SymKey *symKey = NULL;
1084
0
    unsigned char ivData[8];
1085
1086
    /* use the purpose to select the low level keygen algorithm */
1087
0
    switch (bitGenPurpose) {
1088
0
        case pbeBitGenIntegrityKey:
1089
0
            switch (hashAlgorithm) {
1090
0
                case SEC_OID_SHA1:
1091
0
                    mechanism = CKM_PBA_SHA1_WITH_SHA1_HMAC;
1092
0
                    break;
1093
0
                case SEC_OID_MD2:
1094
0
                    mechanism = CKM_NSS_PBE_MD2_HMAC_KEY_GEN;
1095
0
                    break;
1096
0
                case SEC_OID_MD5:
1097
0
                    mechanism = CKM_NSS_PBE_MD5_HMAC_KEY_GEN;
1098
0
                    break;
1099
0
                default:
1100
0
                    break;
1101
0
            }
1102
0
            break;
1103
0
        case pbeBitGenCipherIV:
1104
0
            if (bitsNeeded > 64) {
1105
0
                break;
1106
0
            }
1107
0
            if (hashAlgorithm != SEC_OID_SHA1) {
1108
0
                break;
1109
0
            }
1110
0
            mechanism = CKM_PBE_SHA1_DES3_EDE_CBC;
1111
0
            break;
1112
0
        case pbeBitGenCipherKey:
1113
0
            if (hashAlgorithm != SEC_OID_SHA1) {
1114
0
                break;
1115
0
            }
1116
0
            switch (bitsNeeded) {
1117
0
                case 40:
1118
0
                    mechanism = CKM_PBE_SHA1_RC4_40;
1119
0
                    break;
1120
0
                case 128:
1121
0
                    mechanism = CKM_PBE_SHA1_RC4_128;
1122
0
                    break;
1123
0
                default:
1124
0
                    break;
1125
0
            }
1126
0
        case pbeBitGenIDNull:
1127
0
            break;
1128
0
    }
1129
1130
0
    if (mechanism == CKM_INVALID_MECHANISM) {
1131
        /* we should set an error, but this is a deprecated function, and
1132
         * we are keeping bug for bug compatibility;)... */
1133
0
        return NULL;
1134
0
    }
1135
1136
0
    pbe_params.pInitVector = ivData;
1137
0
    pbe_params.pPassword = pwitem->data;
1138
0
    pbe_params.ulPasswordLen = pwitem->len;
1139
0
    pbe_params.pSalt = salt->data;
1140
0
    pbe_params.ulSaltLen = salt->len;
1141
0
    pbe_params.ulIteration = iterations;
1142
0
    mechItem.data = (unsigned char *)&pbe_params;
1143
0
    mechItem.len = sizeof(pbe_params);
1144
1145
0
    slot = PK11_GetInternalSlot();
1146
0
    symKey = PK11_RawPBEKeyGen(slot, mechanism,
1147
0
                               &mechItem, pwitem, PR_FALSE, NULL);
1148
0
    PK11_FreeSlot(slot);
1149
0
    if (symKey != NULL) {
1150
0
        if (bitGenPurpose == pbeBitGenCipherIV) {
1151
            /* NOTE: this assumes that bitsNeeded is a multiple of 8! */
1152
0
            SECItem ivItem;
1153
1154
0
            ivItem.data = ivData;
1155
0
            ivItem.len = bitsNeeded / 8;
1156
0
            context = SECITEM_DupItem(&ivItem);
1157
0
        } else {
1158
0
            SECItem *keyData;
1159
0
            PK11_ExtractKeyValue(symKey);
1160
0
            keyData = PK11_GetKeyData(symKey);
1161
1162
            /* assert bitsNeeded with length? */
1163
0
            if (keyData) {
1164
0
                context = SECITEM_DupItem(keyData);
1165
0
            }
1166
0
        }
1167
0
        PK11_FreeSymKey(symKey);
1168
0
    }
1169
1170
0
    return (PBEBitGenContext *)context;
1171
0
}
1172
1173
/*
1174
 * public, Deprecated, This function is only for binary compatibility with
1175
 * older applications. Does not support PKCS5v2.
1176
 *
1177
 * Applications should use PK11_PBEKeyGen() for keys and PK11_GetIV() for
1178
 * iv values rather than generating PBE bits directly.
1179
 */
1180
SECItem *
1181
PBE_GenerateBits(PBEBitGenContext *context)
1182
0
{
1183
0
    return (SECItem *)context;
1184
0
}
1185
1186
/*
1187
 * public, Deprecated, This function is only for binary compatibility with
1188
 * older applications. Does not support PKCS5v2.
1189
 *
1190
 * Applications should use PK11_PBEKeyGen() for keys and PK11_GetPBEIV() for
1191
 * iv values rather than generating PBE bits directly.
1192
 */
1193
void
1194
PBE_DestroyContext(PBEBitGenContext *context)
1195
0
{
1196
0
    SECITEM_FreeItem((SECItem *)context, PR_TRUE);
1197
0
}
1198
1199
/*
1200
 * public, deprecated. Replaced with PK11_GetPBEIV().
1201
 */
1202
SECItem *
1203
SEC_PKCS5GetIV(SECAlgorithmID *algid, SECItem *pwitem, PRBool faulty3DES)
1204
0
{
1205
    /* pbe stuff */
1206
0
    CK_MECHANISM_TYPE type;
1207
0
    SECItem *param = NULL;
1208
0
    SECItem *iv = NULL;
1209
0
    SECItem src;
1210
0
    int iv_len = 0;
1211
0
    PK11SymKey *symKey;
1212
0
    PK11SlotInfo *slot;
1213
0
    CK_PBE_PARAMS_PTR pPBEparams;
1214
0
    SECOidTag pbeAlg;
1215
1216
0
    pbeAlg = SECOID_GetAlgorithmTag(algid);
1217
0
    if (sec_pkcs5_is_algorithm_v2_pkcs5_algorithm(pbeAlg)) {
1218
0
        unsigned char *ivData;
1219
0
        sec_pkcs5V2Parameter *pbeV2_param = NULL;
1220
1221
        /* can only return the IV if the crypto Algorithm exists */
1222
0
        if (pbeAlg == SEC_OID_PKCS5_PBKDF2) {
1223
0
            PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
1224
0
            goto loser;
1225
0
        }
1226
0
        pbeV2_param = sec_pkcs5_v2_get_v2_param(NULL, algid);
1227
0
        if (pbeV2_param == NULL) {
1228
0
            goto loser;
1229
0
        }
1230
        /* extract the IV from the cipher algid portion of our pkcs 5 v2
1231
         * algorithm id */
1232
0
        type = PK11_AlgtagToMechanism(
1233
0
            SECOID_GetAlgorithmTag(&pbeV2_param->cipherAlgId));
1234
0
        param = PK11_ParamFromAlgid(&pbeV2_param->cipherAlgId);
1235
0
        sec_pkcs5_v2_destroy_v2_param(pbeV2_param);
1236
0
        if (!param) {
1237
0
            goto loser;
1238
0
        }
1239
        /* NOTE: NULL is a permissible return here */
1240
0
        ivData = PK11_IVFromParam(type, param, &iv_len);
1241
0
        src.data = ivData;
1242
0
        src.len = iv_len;
1243
0
        goto done;
1244
0
    }
1245
1246
0
    type = PK11_AlgtagToMechanism(pbeAlg);
1247
0
    param = PK11_ParamFromAlgid(algid);
1248
0
    if (param == NULL) {
1249
0
        goto done;
1250
0
    }
1251
0
    slot = PK11_GetInternalSlot();
1252
0
    symKey = PK11_RawPBEKeyGen(slot, type, param, pwitem, faulty3DES, NULL);
1253
0
    PK11_FreeSlot(slot);
1254
0
    if (symKey == NULL) {
1255
0
        goto loser;
1256
0
    }
1257
0
    PK11_FreeSymKey(symKey);
1258
0
    pPBEparams = (CK_PBE_PARAMS_PTR)param->data;
1259
0
    iv_len = PK11_GetIVLength(type);
1260
1261
0
    src.data = (unsigned char *)pPBEparams->pInitVector;
1262
0
    src.len = iv_len;
1263
1264
0
done:
1265
0
    iv = SECITEM_DupItem(&src);
1266
1267
0
loser:
1268
0
    if (param) {
1269
0
        SECITEM_ZfreeItem(param, PR_TRUE);
1270
0
    }
1271
0
    return iv;
1272
0
}
1273
1274
/*
1275
 * Subs from nss 3.x that are deprecated
1276
 */
1277
PBEBitGenContext *
1278
__PBE_CreateContext(SECOidTag hashAlgorithm, PBEBitGenID bitGenPurpose,
1279
                    SECItem *pwitem, SECItem *salt, unsigned int bitsNeeded,
1280
                    unsigned int iterations)
1281
0
{
1282
0
    PORT_Assert("__PBE_CreateContext is Deprecated" == NULL);
1283
0
    return NULL;
1284
0
}
1285
1286
SECItem *
1287
__PBE_GenerateBits(PBEBitGenContext *context)
1288
0
{
1289
0
    PORT_Assert("__PBE_GenerateBits is Deprecated" == NULL);
1290
0
    return NULL;
1291
0
}
1292
1293
void
1294
__PBE_DestroyContext(PBEBitGenContext *context)
1295
0
{
1296
0
    PORT_Assert("__PBE_DestroyContext is Deprecated" == NULL);
1297
0
}
1298
1299
SECStatus
1300
RSA_FormatBlock(SECItem *result, unsigned modulusLen,
1301
                int blockType, SECItem *data)
1302
0
{
1303
0
    PORT_Assert("RSA_FormatBlock is Deprecated" == NULL);
1304
0
    return SECFailure;
1305
0
}
1306
1307
/****************************************************************************
1308
 *
1309
 * Now Do The PBE Functions Here...
1310
 *
1311
 ****************************************************************************/
1312
1313
static void
1314
pk11_destroy_ck_pbe_params(CK_PBE_PARAMS *pbe_params)
1315
0
{
1316
0
    if (pbe_params) {
1317
0
        if (pbe_params->pPassword)
1318
0
            PORT_ZFree(pbe_params->pPassword, pbe_params->ulPasswordLen);
1319
0
        if (pbe_params->pSalt)
1320
0
            PORT_ZFree(pbe_params->pSalt, pbe_params->ulSaltLen);
1321
0
        PORT_ZFree(pbe_params, sizeof(CK_PBE_PARAMS));
1322
0
    }
1323
0
}
1324
1325
/*
1326
 * public, deprecated.  use PK11_CreatePBEAlgorithmID or
1327
 * PK11_CreatePBEV2AlgorithmID instead. If you needthe pkcs #11 parameters,
1328
 * use PK11_ParamFromAlgid from the algorithm id you created using
1329
 * PK11_CreatePBEAlgorithmID or PK11_CreatePBEV2AlgorithmID.
1330
 */
1331
SECItem *
1332
PK11_CreatePBEParams(SECItem *salt, SECItem *pwd, unsigned int iterations)
1333
0
{
1334
0
    CK_PBE_PARAMS *pbe_params = NULL;
1335
0
    SECItem *paramRV = NULL;
1336
1337
0
    paramRV = SECITEM_AllocItem(NULL, NULL, sizeof(CK_PBE_PARAMS));
1338
0
    if (!paramRV) {
1339
0
        goto loser;
1340
0
    }
1341
    /* init paramRV->data with zeros. SECITEM_AllocItem does not do it */
1342
0
    PORT_Memset(paramRV->data, 0, sizeof(CK_PBE_PARAMS));
1343
1344
0
    pbe_params = (CK_PBE_PARAMS *)paramRV->data;
1345
0
    pbe_params->pPassword = (CK_CHAR_PTR)PORT_ZAlloc(pwd->len);
1346
0
    if (!pbe_params->pPassword) {
1347
0
        goto loser;
1348
0
    }
1349
0
    if (pwd->data) {
1350
0
        PORT_Memcpy(pbe_params->pPassword, pwd->data, pwd->len);
1351
0
    }
1352
0
    pbe_params->ulPasswordLen = pwd->len;
1353
1354
0
    pbe_params->pSalt = (CK_CHAR_PTR)PORT_ZAlloc(salt->len);
1355
0
    if (!pbe_params->pSalt) {
1356
0
        goto loser;
1357
0
    }
1358
0
    PORT_Memcpy(pbe_params->pSalt, salt->data, salt->len);
1359
0
    pbe_params->ulSaltLen = salt->len;
1360
1361
0
    pbe_params->ulIteration = (CK_ULONG)iterations;
1362
0
    return paramRV;
1363
1364
0
loser:
1365
0
    if (pbe_params)
1366
0
        pk11_destroy_ck_pbe_params(pbe_params);
1367
0
    if (paramRV)
1368
0
        PORT_ZFree(paramRV, sizeof(SECItem));
1369
0
    return NULL;
1370
0
}
1371
1372
/*
1373
 * public, deprecated.
1374
 */
1375
void
1376
PK11_DestroyPBEParams(SECItem *pItem)
1377
0
{
1378
0
    if (pItem) {
1379
0
        CK_PBE_PARAMS *params = (CK_PBE_PARAMS *)(pItem->data);
1380
0
        if (params)
1381
0
            pk11_destroy_ck_pbe_params(params);
1382
0
        PORT_ZFree(pItem, sizeof(SECItem));
1383
0
    }
1384
0
}
1385
1386
/*
1387
 * public, Partially supports PKCS5 V2 (some parameters are not controllable
1388
 * through this interface). Use PK11_CreatePBEV2AlgorithmID() if you need
1389
 * finer control these.
1390
 */
1391
SECAlgorithmID *
1392
PK11_CreatePBEAlgorithmID(SECOidTag algorithm, int iteration, SECItem *salt)
1393
0
{
1394
0
    SECAlgorithmID *algid = NULL;
1395
0
    algid = sec_pkcs5CreateAlgorithmID(algorithm,
1396
0
                                       SEC_OID_UNKNOWN, SEC_OID_UNKNOWN, NULL,
1397
0
                                       0, salt, iteration);
1398
0
    return algid;
1399
0
}
1400
1401
/*
1402
 * public, fully support pkcs5v2.
1403
 */
1404
SECAlgorithmID *
1405
PK11_CreatePBEV2AlgorithmID(SECOidTag pbeAlgTag, SECOidTag cipherAlgTag,
1406
                            SECOidTag prfAlgTag, int keyLength, int iteration,
1407
                            SECItem *salt)
1408
0
{
1409
0
    SECAlgorithmID *algid = NULL;
1410
0
    algid = sec_pkcs5CreateAlgorithmID(pbeAlgTag, cipherAlgTag, prfAlgTag,
1411
0
                                       NULL, keyLength, salt, iteration);
1412
0
    return algid;
1413
0
}
1414
1415
/*
1416
 * private.
1417
 */
1418
PK11SymKey *
1419
pk11_RawPBEKeyGenWithKeyType(PK11SlotInfo *slot, CK_MECHANISM_TYPE type,
1420
                             SECItem *params, CK_KEY_TYPE keyType, int keyLen,
1421
                             SECItem *pwitem, void *wincx)
1422
0
{
1423
0
    CK_ULONG pwLen;
1424
    /* do some sanity checks */
1425
0
    if ((params == NULL) || (params->data == NULL)) {
1426
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
1427
0
        return NULL;
1428
0
    }
1429
1430
0
    if (type == CKM_INVALID_MECHANISM) {
1431
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
1432
0
        return NULL;
1433
0
    }
1434
1435
    /* set the password pointer in the parameters... */
1436
0
    if (type == CKM_PKCS5_PBKD2) {
1437
0
        CK_PKCS5_PBKD2_PARAMS *pbev2_params;
1438
0
        if (params->len < sizeof(CK_PKCS5_PBKD2_PARAMS)) {
1439
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
1440
0
            return NULL;
1441
0
        }
1442
0
        pbev2_params = (CK_PKCS5_PBKD2_PARAMS *)params->data;
1443
0
        pbev2_params->pPassword = pwitem->data;
1444
0
        pwLen = pwitem->len;
1445
0
        pbev2_params->ulPasswordLen = &pwLen;
1446
0
    } else {
1447
0
        CK_PBE_PARAMS *pbe_params;
1448
0
        if (params->len < sizeof(CK_PBE_PARAMS)) {
1449
0
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
1450
0
            return NULL;
1451
0
        }
1452
0
        pbe_params = (CK_PBE_PARAMS *)params->data;
1453
0
        pbe_params->pPassword = pwitem->data;
1454
0
        pbe_params->ulPasswordLen = pwitem->len;
1455
0
    }
1456
1457
    /* generate the key (and sometimes the IV as a side effect...) */
1458
0
    return pk11_TokenKeyGenWithFlagsAndKeyType(slot, type, params, keyType,
1459
0
                                               keyLen, NULL,
1460
0
                                               CKF_SIGN | CKF_ENCRYPT | CKF_DECRYPT | CKF_UNWRAP | CKF_WRAP,
1461
0
                                               0, wincx);
1462
0
}
1463
1464
/*
1465
 * public, deprecated. use PK11_PBEKeyGen instead.
1466
 */
1467
PK11SymKey *
1468
PK11_RawPBEKeyGen(PK11SlotInfo *slot, CK_MECHANISM_TYPE type, SECItem *mech,
1469
                  SECItem *pwitem, PRBool faulty3DES, void *wincx)
1470
0
{
1471
0
    if (faulty3DES && (type == CKM_NSS_PBE_SHA1_TRIPLE_DES_CBC)) {
1472
0
        type = CKM_NSS_PBE_SHA1_FAULTY_3DES_CBC;
1473
0
    }
1474
0
    return pk11_RawPBEKeyGenWithKeyType(slot, type, mech, -1, 0, pwitem, wincx);
1475
0
}
1476
1477
/*
1478
 * pubic, supports pkcs5 v2.
1479
 *
1480
 * Create symkey from a PBE key. The algid can be created with
1481
 *  PK11_CreatePBEV2AlgorithmID and PK11_CreatePBEAlgorithmID, or by
1482
 *  extraction of der data.
1483
 */
1484
PK11SymKey *
1485
PK11_PBEKeyGen(PK11SlotInfo *slot, SECAlgorithmID *algid, SECItem *pwitem,
1486
               PRBool faulty3DES, void *wincx)
1487
0
{
1488
0
    CK_MECHANISM_TYPE type;
1489
0
    SECItem *param = NULL;
1490
0
    PK11SymKey *symKey = NULL;
1491
0
    SECOidTag pbeAlg;
1492
0
    CK_KEY_TYPE keyType = -1;
1493
0
    int keyLen = 0;
1494
1495
0
    pbeAlg = SECOID_GetAlgorithmTag(algid);
1496
    /* if we're using PKCS5v2, extract the additional information we need
1497
     * (key length, key type, and pbeAlg). */
1498
0
    if (sec_pkcs5_is_algorithm_v2_pkcs5_algorithm(pbeAlg)) {
1499
0
        CK_MECHANISM_TYPE cipherMech;
1500
0
        sec_pkcs5V2Parameter *pbeV2_param;
1501
1502
0
        pbeV2_param = sec_pkcs5_v2_get_v2_param(NULL, algid);
1503
0
        if (pbeV2_param == NULL) {
1504
0
            return NULL;
1505
0
        }
1506
0
        cipherMech = PK11_AlgtagToMechanism(
1507
0
            SECOID_GetAlgorithmTag(&pbeV2_param->cipherAlgId));
1508
0
        pbeAlg = SECOID_GetAlgorithmTag(&pbeV2_param->pbeAlgId);
1509
0
        param = PK11_ParamFromAlgid(&pbeV2_param->pbeAlgId);
1510
0
        sec_pkcs5_v2_destroy_v2_param(pbeV2_param);
1511
0
        keyLen = SEC_PKCS5GetKeyLength(algid);
1512
0
        if (keyLen == -1) {
1513
0
            keyLen = 0;
1514
0
        }
1515
0
        keyType = PK11_GetKeyType(cipherMech, keyLen);
1516
0
    } else {
1517
0
        param = PK11_ParamFromAlgid(algid);
1518
0
    }
1519
1520
0
    if (param == NULL) {
1521
0
        goto loser;
1522
0
    }
1523
1524
0
    type = PK11_AlgtagToMechanism(pbeAlg);
1525
0
    if (type == CKM_INVALID_MECHANISM) {
1526
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
1527
0
        goto loser;
1528
0
    }
1529
0
    if (faulty3DES && (type == CKM_NSS_PBE_SHA1_TRIPLE_DES_CBC)) {
1530
0
        type = CKM_NSS_PBE_SHA1_FAULTY_3DES_CBC;
1531
0
    }
1532
0
    symKey = pk11_RawPBEKeyGenWithKeyType(slot, type, param, keyType, keyLen,
1533
0
                                          pwitem, wincx);
1534
1535
0
loser:
1536
0
    if (param) {
1537
0
        SECITEM_ZfreeItem(param, PR_TRUE);
1538
0
    }
1539
0
    return symKey;
1540
0
}
1541
1542
/*
1543
 * public, supports pkcs5v2
1544
 */
1545
SECItem *
1546
PK11_GetPBEIV(SECAlgorithmID *algid, SECItem *pwitem)
1547
0
{
1548
0
    return SEC_PKCS5GetIV(algid, pwitem, PR_FALSE);
1549
0
}
1550
1551
CK_MECHANISM_TYPE
1552
pk11_GetPBECryptoMechanism(SECAlgorithmID *algid, SECItem **param,
1553
                           SECItem *pbe_pwd, PRBool faulty3DES)
1554
0
{
1555
0
    int keyLen = 0;
1556
0
    SECOidTag algTag = SEC_PKCS5GetCryptoAlgorithm(algid);
1557
0
    CK_MECHANISM_TYPE mech = PK11_AlgtagToMechanism(algTag);
1558
0
    CK_MECHANISM_TYPE returnedMechanism = CKM_INVALID_MECHANISM;
1559
0
    SECItem *iv = NULL;
1560
1561
0
    if (mech == CKM_INVALID_MECHANISM) {
1562
0
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
1563
0
        goto loser;
1564
0
    }
1565
0
    if (PK11_GetIVLength(mech)) {
1566
0
        iv = SEC_PKCS5GetIV(algid, pbe_pwd, faulty3DES);
1567
0
        if (iv == NULL) {
1568
0
            goto loser;
1569
0
        }
1570
0
    }
1571
1572
0
    keyLen = SEC_PKCS5GetKeyLength(algid);
1573
1574
0
    *param = pk11_ParamFromIVWithLen(mech, iv, keyLen);
1575
0
    if (*param == NULL) {
1576
0
        goto loser;
1577
0
    }
1578
0
    returnedMechanism = mech;
1579
1580
0
loser:
1581
0
    if (iv) {
1582
0
        SECITEM_FreeItem(iv, PR_TRUE);
1583
0
    }
1584
0
    return returnedMechanism;
1585
0
}
1586
1587
/*
1588
 * Public, supports pkcs5 v2
1589
 *
1590
 * Get the crypto mechanism directly from the pbe algorithmid.
1591
 *
1592
 * It's important to go directly from the algorithm id so that we can
1593
 * handle both the PKCS #5 v1, PKCS #12, and PKCS #5 v2 cases.
1594
 *
1595
 * This function returns both the mechanism and the parameter for the mechanism.
1596
 * The caller is responsible for freeing the parameter.
1597
 */
1598
CK_MECHANISM_TYPE
1599
PK11_GetPBECryptoMechanism(SECAlgorithmID *algid, SECItem **param,
1600
                           SECItem *pbe_pwd)
1601
0
{
1602
0
    return pk11_GetPBECryptoMechanism(algid, param, pbe_pwd, PR_FALSE);
1603
0
}