Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/security/nss/lib/pk11wrap/pk11nobj.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 manages Netscape specific PKCS #11 objects (CRLs, Trust objects,
6
 * etc).
7
 */
8
9
#include "secport.h"
10
#include "seccomon.h"
11
#include "secmod.h"
12
#include "secmodi.h"
13
#include "secmodti.h"
14
#include "pkcs11.h"
15
#include "pk11func.h"
16
#include "cert.h"
17
#include "certi.h"
18
#include "secitem.h"
19
#include "sechash.h"
20
#include "secoid.h"
21
22
#include "certdb.h"
23
#include "secerr.h"
24
25
#include "pki3hack.h"
26
#include "dev3hack.h"
27
28
#include "devm.h"
29
#include "pki.h"
30
#include "pkim.h"
31
32
extern const NSSError NSS_ERROR_NOT_FOUND;
33
34
CK_TRUST
35
pk11_GetTrustField(PK11SlotInfo *slot, PLArenaPool *arena,
36
                   CK_OBJECT_HANDLE id, CK_ATTRIBUTE_TYPE type)
37
0
{
38
0
    CK_TRUST rv = 0;
39
0
    SECItem item;
40
0
41
0
    item.data = NULL;
42
0
    item.len = 0;
43
0
44
0
    if (SECSuccess == PK11_ReadAttribute(slot, id, type, arena, &item)) {
45
0
        PORT_Assert(item.len == sizeof(CK_TRUST));
46
0
        PORT_Memcpy(&rv, item.data, sizeof(CK_TRUST));
47
0
        /* Damn, is there an endian problem here? */
48
0
        return rv;
49
0
    }
50
0
51
0
    return 0;
52
0
}
53
54
PRBool
55
pk11_HandleTrustObject(PK11SlotInfo *slot, CERTCertificate *cert, CERTCertTrust *trust)
56
0
{
57
0
    PLArenaPool *arena;
58
0
59
0
    CK_ATTRIBUTE tobjTemplate[] = {
60
0
        { CKA_CLASS, NULL, 0 },
61
0
        { CKA_CERT_SHA1_HASH, NULL, 0 },
62
0
    };
63
0
64
0
    CK_OBJECT_CLASS tobjc = CKO_NETSCAPE_TRUST;
65
0
    CK_OBJECT_HANDLE tobjID;
66
0
    unsigned char sha1_hash[SHA1_LENGTH];
67
0
68
0
    CK_TRUST serverAuth, codeSigning, emailProtection, clientAuth;
69
0
70
0
    PK11_HashBuf(SEC_OID_SHA1, sha1_hash, cert->derCert.data, cert->derCert.len);
71
0
72
0
    PK11_SETATTRS(&tobjTemplate[0], CKA_CLASS, &tobjc, sizeof(tobjc));
73
0
    PK11_SETATTRS(&tobjTemplate[1], CKA_CERT_SHA1_HASH, sha1_hash,
74
0
                  SHA1_LENGTH);
75
0
76
0
    tobjID = pk11_FindObjectByTemplate(slot, tobjTemplate,
77
0
                                       sizeof(tobjTemplate) / sizeof(tobjTemplate[0]));
78
0
    if (CK_INVALID_HANDLE == tobjID) {
79
0
        return PR_FALSE;
80
0
    }
81
0
82
0
    arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
83
0
    if (NULL == arena)
84
0
        return PR_FALSE;
85
0
86
0
    /* Unfortunately, it seems that PK11_GetAttributes doesn't deal
87
0
     * well with nonexistent attributes.  I guess we have to check
88
0
     * the trust info fields one at a time.
89
0
     */
90
0
91
0
    /* We could verify CKA_CERT_HASH here */
92
0
93
0
    /* We could verify CKA_EXPIRES here */
94
0
95
0
    /* "Purpose" trust information */
96
0
    serverAuth = pk11_GetTrustField(slot, arena, tobjID, CKA_TRUST_SERVER_AUTH);
97
0
    clientAuth = pk11_GetTrustField(slot, arena, tobjID, CKA_TRUST_CLIENT_AUTH);
98
0
    codeSigning = pk11_GetTrustField(slot, arena, tobjID, CKA_TRUST_CODE_SIGNING);
99
0
    emailProtection = pk11_GetTrustField(slot, arena, tobjID,
100
0
                                         CKA_TRUST_EMAIL_PROTECTION);
101
0
    /* Here's where the fun logic happens.  We have to map back from the
102
0
     * key usage, extended key usage, purpose, and possibly other trust values
103
0
     * into the old trust-flags bits.  */
104
0
105
0
    /* First implementation: keep it simple for testing.  We can study what other
106
0
     * mappings would be appropriate and add them later.. fgmr 20000724 */
107
0
108
0
    if (serverAuth == CKT_NSS_TRUSTED) {
109
0
        trust->sslFlags |= CERTDB_TERMINAL_RECORD | CERTDB_TRUSTED;
110
0
    }
111
0
112
0
    if (serverAuth == CKT_NSS_TRUSTED_DELEGATOR) {
113
0
        trust->sslFlags |= CERTDB_VALID_CA | CERTDB_TRUSTED_CA |
114
0
                           CERTDB_NS_TRUSTED_CA;
115
0
    }
116
0
    if (clientAuth == CKT_NSS_TRUSTED_DELEGATOR) {
117
0
        trust->sslFlags |= CERTDB_TRUSTED_CLIENT_CA;
118
0
    }
119
0
120
0
    if (emailProtection == CKT_NSS_TRUSTED) {
121
0
        trust->emailFlags |= CERTDB_TERMINAL_RECORD | CERTDB_TRUSTED;
122
0
    }
123
0
124
0
    if (emailProtection == CKT_NSS_TRUSTED_DELEGATOR) {
125
0
        trust->emailFlags |= CERTDB_VALID_CA | CERTDB_TRUSTED_CA | CERTDB_NS_TRUSTED_CA;
126
0
    }
127
0
128
0
    if (codeSigning == CKT_NSS_TRUSTED) {
129
0
        trust->objectSigningFlags |= CERTDB_TERMINAL_RECORD | CERTDB_TRUSTED;
130
0
    }
131
0
132
0
    if (codeSigning == CKT_NSS_TRUSTED_DELEGATOR) {
133
0
        trust->objectSigningFlags |= CERTDB_VALID_CA | CERTDB_TRUSTED_CA | CERTDB_NS_TRUSTED_CA;
134
0
    }
135
0
136
0
    /* There's certainly a lot more logic that can go here.. */
137
0
138
0
    PORT_FreeArena(arena, PR_FALSE);
139
0
140
0
    return PR_TRUE;
141
0
}
142
143
static SECStatus
144
pk11_CollectCrls(PK11SlotInfo *slot, CK_OBJECT_HANDLE crlID, void *arg)
145
0
{
146
0
    SECItem derCrl;
147
0
    CERTCrlHeadNode *head = (CERTCrlHeadNode *)arg;
148
0
    CERTCrlNode *new_node = NULL;
149
0
    CK_ATTRIBUTE fetchCrl[3] = {
150
0
        { CKA_VALUE, NULL, 0 },
151
0
        { CKA_NETSCAPE_KRL, NULL, 0 },
152
0
        { CKA_NETSCAPE_URL, NULL, 0 },
153
0
    };
154
0
    const int fetchCrlSize = sizeof(fetchCrl) / sizeof(fetchCrl[2]);
155
0
    CK_RV crv;
156
0
    SECStatus rv = SECFailure;
157
0
158
0
    crv = PK11_GetAttributes(head->arena, slot, crlID, fetchCrl, fetchCrlSize);
159
0
    if (CKR_OK != crv) {
160
0
        PORT_SetError(PK11_MapError(crv));
161
0
        goto loser;
162
0
    }
163
0
164
0
    if (!fetchCrl[1].pValue) {
165
0
        PORT_SetError(SEC_ERROR_CRL_INVALID);
166
0
        goto loser;
167
0
    }
168
0
169
0
    new_node = (CERTCrlNode *)PORT_ArenaAlloc(head->arena, sizeof(CERTCrlNode));
170
0
    if (new_node == NULL) {
171
0
        goto loser;
172
0
    }
173
0
174
0
    if (*((CK_BBOOL *)fetchCrl[1].pValue))
175
0
        new_node->type = SEC_KRL_TYPE;
176
0
    else
177
0
        new_node->type = SEC_CRL_TYPE;
178
0
179
0
    derCrl.type = siBuffer;
180
0
    derCrl.data = (unsigned char *)fetchCrl[0].pValue;
181
0
    derCrl.len = fetchCrl[0].ulValueLen;
182
0
    new_node->crl = CERT_DecodeDERCrl(head->arena, &derCrl, new_node->type);
183
0
    if (new_node->crl == NULL) {
184
0
        goto loser;
185
0
    }
186
0
187
0
    if (fetchCrl[2].pValue) {
188
0
        int nnlen = fetchCrl[2].ulValueLen;
189
0
        new_node->crl->url = (char *)PORT_ArenaAlloc(head->arena, nnlen + 1);
190
0
        if (!new_node->crl->url) {
191
0
            goto loser;
192
0
        }
193
0
        PORT_Memcpy(new_node->crl->url, fetchCrl[2].pValue, nnlen);
194
0
        new_node->crl->url[nnlen] = 0;
195
0
    } else {
196
0
        new_node->crl->url = NULL;
197
0
    }
198
0
199
0
    new_node->next = NULL;
200
0
    if (head->last) {
201
0
        head->last->next = new_node;
202
0
        head->last = new_node;
203
0
    } else {
204
0
        head->first = head->last = new_node;
205
0
    }
206
0
    rv = SECSuccess;
207
0
208
0
loser:
209
0
    return (rv);
210
0
}
211
212
/*
213
 * Return a list of all the CRLs .
214
 * CRLs are allocated in the list's arena.
215
 */
216
SECStatus
217
PK11_LookupCrls(CERTCrlHeadNode *nodes, int type, void *wincx)
218
0
{
219
0
    pk11TraverseSlot creater;
220
0
    CK_ATTRIBUTE theTemplate[2];
221
0
    CK_ATTRIBUTE *attrs;
222
0
    CK_OBJECT_CLASS certClass = CKO_NETSCAPE_CRL;
223
0
    CK_BBOOL isKrl = CK_FALSE;
224
0
225
0
    attrs = theTemplate;
226
0
    PK11_SETATTRS(attrs, CKA_CLASS, &certClass, sizeof(certClass));
227
0
    attrs++;
228
0
    if (type != -1) {
229
0
        isKrl = (CK_BBOOL)(type == SEC_KRL_TYPE);
230
0
        PK11_SETATTRS(attrs, CKA_NETSCAPE_KRL, &isKrl, sizeof(isKrl));
231
0
        attrs++;
232
0
    }
233
0
234
0
    creater.callback = pk11_CollectCrls;
235
0
    creater.callbackArg = (void *)nodes;
236
0
    creater.findTemplate = theTemplate;
237
0
    creater.templateCount = (attrs - theTemplate);
238
0
239
0
    return pk11_TraverseAllSlots(PK11_TraverseSlot, &creater, PR_FALSE, wincx);
240
0
}
241
242
struct crlOptionsStr {
243
    CERTCrlHeadNode *head;
244
    PRInt32 decodeOptions;
245
};
246
247
typedef struct crlOptionsStr crlOptions;
248
249
static SECStatus
250
pk11_RetrieveCrlsCallback(PK11SlotInfo *slot, CK_OBJECT_HANDLE crlID,
251
                          void *arg)
252
0
{
253
0
    SECItem *derCrl = NULL;
254
0
    crlOptions *options = (crlOptions *)arg;
255
0
    CERTCrlHeadNode *head = options->head;
256
0
    CERTCrlNode *new_node = NULL;
257
0
    CK_ATTRIBUTE fetchCrl[3] = {
258
0
        { CKA_VALUE, NULL, 0 },
259
0
        { CKA_NETSCAPE_KRL, NULL, 0 },
260
0
        { CKA_NETSCAPE_URL, NULL, 0 },
261
0
    };
262
0
    const int fetchCrlSize = sizeof(fetchCrl) / sizeof(fetchCrl[2]);
263
0
    CK_RV crv;
264
0
    SECStatus rv = SECFailure;
265
0
    PRBool adopted = PR_FALSE; /* whether the CRL adopted the DER memory
266
0
                                  successfully */
267
0
    int i;
268
0
269
0
    crv = PK11_GetAttributes(NULL, slot, crlID, fetchCrl, fetchCrlSize);
270
0
    if (CKR_OK != crv) {
271
0
        PORT_SetError(PK11_MapError(crv));
272
0
        goto loser;
273
0
    }
274
0
275
0
    if (!fetchCrl[1].pValue) {
276
0
        /* reject KRLs */
277
0
        PORT_SetError(SEC_ERROR_CRL_INVALID);
278
0
        goto loser;
279
0
    }
280
0
281
0
    new_node = (CERTCrlNode *)PORT_ArenaAlloc(head->arena,
282
0
                                              sizeof(CERTCrlNode));
283
0
    if (new_node == NULL) {
284
0
        goto loser;
285
0
    }
286
0
287
0
    new_node->type = SEC_CRL_TYPE;
288
0
289
0
    derCrl = SECITEM_AllocItem(NULL, NULL, 0);
290
0
    if (!derCrl) {
291
0
        goto loser;
292
0
    }
293
0
    derCrl->type = siBuffer;
294
0
    derCrl->data = (unsigned char *)fetchCrl[0].pValue;
295
0
    derCrl->len = fetchCrl[0].ulValueLen;
296
0
    new_node->crl = CERT_DecodeDERCrlWithFlags(NULL, derCrl, new_node->type,
297
0
                                               options->decodeOptions);
298
0
    if (new_node->crl == NULL) {
299
0
        goto loser;
300
0
    }
301
0
    adopted = PR_TRUE; /* now that the CRL has adopted the DER memory,
302
0
                          we won't need to free it upon exit */
303
0
304
0
    if (fetchCrl[2].pValue && fetchCrl[2].ulValueLen) {
305
0
        /* copy the URL if there is one */
306
0
        int nnlen = fetchCrl[2].ulValueLen;
307
0
        new_node->crl->url = (char *)PORT_ArenaAlloc(new_node->crl->arena,
308
0
                                                     nnlen + 1);
309
0
        if (!new_node->crl->url) {
310
0
            goto loser;
311
0
        }
312
0
        PORT_Memcpy(new_node->crl->url, fetchCrl[2].pValue, nnlen);
313
0
        new_node->crl->url[nnlen] = 0;
314
0
    } else {
315
0
        new_node->crl->url = NULL;
316
0
    }
317
0
318
0
    new_node->next = NULL;
319
0
    if (head->last) {
320
0
        head->last->next = new_node;
321
0
        head->last = new_node;
322
0
    } else {
323
0
        head->first = head->last = new_node;
324
0
    }
325
0
    rv = SECSuccess;
326
0
    new_node->crl->slot = PK11_ReferenceSlot(slot);
327
0
    new_node->crl->pkcs11ID = crlID;
328
0
329
0
loser:
330
0
    /* free attributes that weren't adopted by the CRL */
331
0
    for (i = 1; i < fetchCrlSize; i++) {
332
0
        if (fetchCrl[i].pValue) {
333
0
            PORT_Free(fetchCrl[i].pValue);
334
0
        }
335
0
    }
336
0
    /* free the DER if the CRL object didn't adopt it */
337
0
    if (fetchCrl[0].pValue && PR_FALSE == adopted) {
338
0
        PORT_Free(fetchCrl[0].pValue);
339
0
    }
340
0
    if (derCrl && !adopted) {
341
0
        /* clear the data fields, which we already took care of above */
342
0
        derCrl->data = NULL;
343
0
        derCrl->len = 0;
344
0
        /* free the memory for the SECItem structure itself */
345
0
        SECITEM_FreeItem(derCrl, PR_TRUE);
346
0
    }
347
0
    return (rv);
348
0
}
349
350
/*
351
 * Return a list of CRLs matching specified issuer and type
352
 * CRLs are not allocated in the list's arena, but rather in their own,
353
 * arena, so that they can be used individually in the CRL cache .
354
 * CRLs are always partially decoded for efficiency.
355
 */
356
SECStatus
357
pk11_RetrieveCrls(CERTCrlHeadNode *nodes, SECItem *issuer,
358
                  void *wincx)
359
0
{
360
0
    pk11TraverseSlot creater;
361
0
    CK_ATTRIBUTE theTemplate[2];
362
0
    CK_ATTRIBUTE *attrs;
363
0
    CK_OBJECT_CLASS crlClass = CKO_NETSCAPE_CRL;
364
0
    crlOptions options;
365
0
366
0
    attrs = theTemplate;
367
0
    PK11_SETATTRS(attrs, CKA_CLASS, &crlClass, sizeof(crlClass));
368
0
    attrs++;
369
0
370
0
    options.head = nodes;
371
0
372
0
    /* - do a partial decoding - we don't need to decode the entries while fetching
373
0
       - don't copy the DER for optimal performance - CRL can be very large
374
0
       - have the CRL objects adopt the DER, so SEC_DestroyCrl will free it
375
0
       - keep bad CRL objects. The CRL cache is interested in them, for
376
0
         security purposes. Bad CRL objects are a sign of something amiss.
377
0
     */
378
0
379
0
    options.decodeOptions = CRL_DECODE_SKIP_ENTRIES | CRL_DECODE_DONT_COPY_DER |
380
0
                            CRL_DECODE_ADOPT_HEAP_DER | CRL_DECODE_KEEP_BAD_CRL;
381
0
    if (issuer) {
382
0
        PK11_SETATTRS(attrs, CKA_SUBJECT, issuer->data, issuer->len);
383
0
        attrs++;
384
0
    }
385
0
386
0
    creater.callback = pk11_RetrieveCrlsCallback;
387
0
    creater.callbackArg = (void *)&options;
388
0
    creater.findTemplate = theTemplate;
389
0
    creater.templateCount = (attrs - theTemplate);
390
0
391
0
    return pk11_TraverseAllSlots(PK11_TraverseSlot, &creater, PR_FALSE, wincx);
392
0
}
393
394
/*
395
 * return the crl associated with a derSubjectName
396
 */
397
SECItem *
398
PK11_FindCrlByName(PK11SlotInfo **slot, CK_OBJECT_HANDLE *crlHandle,
399
                   SECItem *name, int type, char **pUrl)
400
0
{
401
0
    NSSCRL **crls, **crlp, *crl = NULL;
402
0
    NSSDER subject;
403
0
    SECItem *rvItem;
404
0
    NSSTrustDomain *td = STAN_GetDefaultTrustDomain();
405
0
    char *url = NULL;
406
0
407
0
    PORT_SetError(0);
408
0
    NSSITEM_FROM_SECITEM(&subject, name);
409
0
    if (*slot) {
410
0
        nssCryptokiObject **instances;
411
0
        nssPKIObjectCollection *collection;
412
0
        nssTokenSearchType tokenOnly = nssTokenSearchType_TokenOnly;
413
0
        NSSToken *token = PK11Slot_GetNSSToken(*slot);
414
0
        collection = nssCRLCollection_Create(td, NULL);
415
0
        if (!collection) {
416
0
            goto loser;
417
0
        }
418
0
        instances = nssToken_FindCRLsBySubject(token, NULL, &subject,
419
0
                                               tokenOnly, 0, NULL);
420
0
        nssPKIObjectCollection_AddInstances(collection, instances, 0);
421
0
        nss_ZFreeIf(instances);
422
0
        crls = nssPKIObjectCollection_GetCRLs(collection, NULL, 0, NULL);
423
0
        nssPKIObjectCollection_Destroy(collection);
424
0
    } else {
425
0
        crls = nssTrustDomain_FindCRLsBySubject(td, &subject);
426
0
    }
427
0
    if ((!crls) || (*crls == NULL)) {
428
0
        if (crls) {
429
0
            nssCRLArray_Destroy(crls);
430
0
        }
431
0
        if (NSS_GetError() == NSS_ERROR_NOT_FOUND) {
432
0
            PORT_SetError(SEC_ERROR_CRL_NOT_FOUND);
433
0
        }
434
0
        goto loser;
435
0
    }
436
0
    for (crlp = crls; *crlp; crlp++) {
437
0
        if ((!(*crlp)->isKRL && type == SEC_CRL_TYPE) ||
438
0
            ((*crlp)->isKRL && type != SEC_CRL_TYPE)) {
439
0
            crl = nssCRL_AddRef(*crlp);
440
0
            break;
441
0
        }
442
0
    }
443
0
    nssCRLArray_Destroy(crls);
444
0
    if (!crl) {
445
0
        /* CRL collection was found, but no interesting CRL's were on it.
446
0
         * Not an error */
447
0
        PORT_SetError(SEC_ERROR_CRL_NOT_FOUND);
448
0
        goto loser;
449
0
    }
450
0
    if (crl->url) {
451
0
        url = PORT_Strdup(crl->url);
452
0
        if (!url) {
453
0
            goto loser;
454
0
        }
455
0
    }
456
0
    rvItem = SECITEM_AllocItem(NULL, NULL, crl->encoding.size);
457
0
    if (!rvItem) {
458
0
        goto loser;
459
0
    }
460
0
    memcpy(rvItem->data, crl->encoding.data, crl->encoding.size);
461
0
    *slot = PK11_ReferenceSlot(crl->object.instances[0]->token->pk11slot);
462
0
    *crlHandle = crl->object.instances[0]->handle;
463
0
    *pUrl = url;
464
0
    nssCRL_Destroy(crl);
465
0
    return rvItem;
466
0
467
0
loser:
468
0
    if (url)
469
0
        PORT_Free(url);
470
0
    if (crl)
471
0
        nssCRL_Destroy(crl);
472
0
    if (PORT_GetError() == 0) {
473
0
        PORT_SetError(SEC_ERROR_CRL_NOT_FOUND);
474
0
    }
475
0
    return NULL;
476
0
}
477
478
CK_OBJECT_HANDLE
479
PK11_PutCrl(PK11SlotInfo *slot, SECItem *crl, SECItem *name,
480
            char *url, int type)
481
0
{
482
0
    NSSItem derCRL, derSubject;
483
0
    NSSToken *token = PK11Slot_GetNSSToken(slot);
484
0
    nssCryptokiObject *object;
485
0
    PRBool isKRL = (type == SEC_CRL_TYPE) ? PR_FALSE : PR_TRUE;
486
0
    CK_OBJECT_HANDLE rvH;
487
0
488
0
    NSSITEM_FROM_SECITEM(&derSubject, name);
489
0
    NSSITEM_FROM_SECITEM(&derCRL, crl);
490
0
491
0
    object = nssToken_ImportCRL(token, NULL,
492
0
                                &derSubject, &derCRL, isKRL, url, PR_TRUE);
493
0
494
0
    if (object) {
495
0
        rvH = object->handle;
496
0
        nssCryptokiObject_Destroy(object);
497
0
    } else {
498
0
        rvH = CK_INVALID_HANDLE;
499
0
        PORT_SetError(SEC_ERROR_CRL_IMPORT_FAILED);
500
0
    }
501
0
    return rvH;
502
0
}
503
504
/*
505
 * delete a crl.
506
 */
507
SECStatus
508
SEC_DeletePermCRL(CERTSignedCrl *crl)
509
0
{
510
0
    PRStatus status;
511
0
    NSSToken *token;
512
0
    nssCryptokiObject *object;
513
0
    PK11SlotInfo *slot = crl->slot;
514
0
515
0
    if (slot == NULL) {
516
0
        PORT_Assert(slot);
517
0
        /* shouldn't happen */
518
0
        PORT_SetError(SEC_ERROR_CRL_INVALID);
519
0
        return SECFailure;
520
0
    }
521
0
    token = PK11Slot_GetNSSToken(slot);
522
0
523
0
    object = nss_ZNEW(NULL, nssCryptokiObject);
524
0
    if (!object) {
525
0
        return SECFailure;
526
0
    }
527
0
    object->token = nssToken_AddRef(token);
528
0
    object->handle = crl->pkcs11ID;
529
0
    object->isTokenObject = PR_TRUE;
530
0
531
0
    status = nssToken_DeleteStoredObject(object);
532
0
533
0
    nssCryptokiObject_Destroy(object);
534
0
    return (status == PR_SUCCESS) ? SECSuccess : SECFailure;
535
0
}
536
537
/*
538
 * return the certificate associated with a derCert
539
 */
540
SECItem *
541
PK11_FindSMimeProfile(PK11SlotInfo **slot, char *emailAddr,
542
                      SECItem *name, SECItem **profileTime)
543
0
{
544
0
    CK_OBJECT_CLASS smimeClass = CKO_NETSCAPE_SMIME;
545
0
    CK_ATTRIBUTE theTemplate[] = {
546
0
        { CKA_SUBJECT, NULL, 0 },
547
0
        { CKA_CLASS, NULL, 0 },
548
0
        { CKA_NETSCAPE_EMAIL, NULL, 0 },
549
0
    };
550
0
    CK_ATTRIBUTE smimeData[] = {
551
0
        { CKA_SUBJECT, NULL, 0 },
552
0
        { CKA_VALUE, NULL, 0 },
553
0
    };
554
0
    /* if you change the array, change the variable below as well */
555
0
    int tsize = sizeof(theTemplate) / sizeof(theTemplate[0]);
556
0
    CK_OBJECT_HANDLE smimeh = CK_INVALID_HANDLE;
557
0
    CK_ATTRIBUTE *attrs = theTemplate;
558
0
    CK_RV crv;
559
0
    SECItem *emailProfile = NULL;
560
0
561
0
    if (!emailAddr || !emailAddr[0]) {
562
0
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
563
0
        return NULL;
564
0
    }
565
0
566
0
    PK11_SETATTRS(attrs, CKA_SUBJECT, name->data, name->len);
567
0
    attrs++;
568
0
    PK11_SETATTRS(attrs, CKA_CLASS, &smimeClass, sizeof(smimeClass));
569
0
    attrs++;
570
0
    PK11_SETATTRS(attrs, CKA_NETSCAPE_EMAIL, emailAddr, strlen(emailAddr));
571
0
    attrs++;
572
0
573
0
    if (*slot) {
574
0
        smimeh = pk11_FindObjectByTemplate(*slot, theTemplate, tsize);
575
0
    } else {
576
0
        PK11SlotList *list = PK11_GetAllTokens(CKM_INVALID_MECHANISM,
577
0
                                               PR_FALSE, PR_TRUE, NULL);
578
0
        PK11SlotListElement *le;
579
0
580
0
        if (!list) {
581
0
            return NULL;
582
0
        }
583
0
        /* loop through all the slots */
584
0
        for (le = list->head; le; le = le->next) {
585
0
            smimeh = pk11_FindObjectByTemplate(le->slot, theTemplate, tsize);
586
0
            if (smimeh != CK_INVALID_HANDLE) {
587
0
                *slot = PK11_ReferenceSlot(le->slot);
588
0
                break;
589
0
            }
590
0
        }
591
0
        PK11_FreeSlotList(list);
592
0
    }
593
0
594
0
    if (smimeh == CK_INVALID_HANDLE) {
595
0
        PORT_SetError(SEC_ERROR_NO_KRL);
596
0
        return NULL;
597
0
    }
598
0
599
0
    if (profileTime) {
600
0
        PK11_SETATTRS(smimeData, CKA_NETSCAPE_SMIME_TIMESTAMP, NULL, 0);
601
0
    }
602
0
603
0
    crv = PK11_GetAttributes(NULL, *slot, smimeh, smimeData, 2);
604
0
    if (crv != CKR_OK) {
605
0
        PORT_SetError(PK11_MapError(crv));
606
0
        goto loser;
607
0
    }
608
0
609
0
    if (!profileTime) {
610
0
        SECItem profileSubject;
611
0
612
0
        profileSubject.data = (unsigned char *)smimeData[0].pValue;
613
0
        profileSubject.len = smimeData[0].ulValueLen;
614
0
        if (!SECITEM_ItemsAreEqual(&profileSubject, name)) {
615
0
            goto loser;
616
0
        }
617
0
    }
618
0
619
0
    emailProfile = (SECItem *)PORT_ZAlloc(sizeof(SECItem));
620
0
    if (emailProfile == NULL) {
621
0
        goto loser;
622
0
    }
623
0
624
0
    emailProfile->data = (unsigned char *)smimeData[1].pValue;
625
0
    emailProfile->len = smimeData[1].ulValueLen;
626
0
627
0
    if (profileTime) {
628
0
        *profileTime = (SECItem *)PORT_ZAlloc(sizeof(SECItem));
629
0
        if (*profileTime) {
630
0
            (*profileTime)->data = (unsigned char *)smimeData[0].pValue;
631
0
            (*profileTime)->len = smimeData[0].ulValueLen;
632
0
        }
633
0
    }
634
0
635
0
loser:
636
0
    if (emailProfile == NULL) {
637
0
        if (smimeData[1].pValue) {
638
0
            PORT_Free(smimeData[1].pValue);
639
0
        }
640
0
    }
641
0
    if (profileTime == NULL || *profileTime == NULL) {
642
0
        if (smimeData[0].pValue) {
643
0
            PORT_Free(smimeData[0].pValue);
644
0
        }
645
0
    }
646
0
    return emailProfile;
647
0
}
648
649
SECStatus
650
PK11_SaveSMimeProfile(PK11SlotInfo *slot, char *emailAddr, SECItem *derSubj,
651
                      SECItem *emailProfile, SECItem *profileTime)
652
0
{
653
0
    CK_OBJECT_CLASS smimeClass = CKO_NETSCAPE_SMIME;
654
0
    CK_BBOOL ck_true = CK_TRUE;
655
0
    CK_ATTRIBUTE theTemplate[] = {
656
0
        { CKA_CLASS, NULL, 0 },
657
0
        { CKA_TOKEN, NULL, 0 },
658
0
        { CKA_SUBJECT, NULL, 0 },
659
0
        { CKA_NETSCAPE_EMAIL, NULL, 0 },
660
0
        { CKA_NETSCAPE_SMIME_TIMESTAMP, NULL, 0 },
661
0
        { CKA_VALUE, NULL, 0 }
662
0
    };
663
0
    /* if you change the array, change the variable below as well */
664
0
    int realSize = 0;
665
0
    CK_OBJECT_HANDLE smimeh = CK_INVALID_HANDLE;
666
0
    CK_ATTRIBUTE *attrs = theTemplate;
667
0
    CK_SESSION_HANDLE rwsession;
668
0
    PK11SlotInfo *free_slot = NULL;
669
0
    CK_RV crv;
670
#ifdef DEBUG
671
    int tsize = sizeof(theTemplate) / sizeof(theTemplate[0]);
672
#endif
673
674
0
    PK11_SETATTRS(attrs, CKA_CLASS, &smimeClass, sizeof(smimeClass));
675
0
    attrs++;
676
0
    PK11_SETATTRS(attrs, CKA_TOKEN, &ck_true, sizeof(ck_true));
677
0
    attrs++;
678
0
    PK11_SETATTRS(attrs, CKA_SUBJECT, derSubj->data, derSubj->len);
679
0
    attrs++;
680
0
    PK11_SETATTRS(attrs, CKA_NETSCAPE_EMAIL,
681
0
                  emailAddr, PORT_Strlen(emailAddr) + 1);
682
0
    attrs++;
683
0
    if (profileTime) {
684
0
        PK11_SETATTRS(attrs, CKA_NETSCAPE_SMIME_TIMESTAMP, profileTime->data,
685
0
                      profileTime->len);
686
0
        attrs++;
687
0
        PK11_SETATTRS(attrs, CKA_VALUE, emailProfile->data,
688
0
                      emailProfile->len);
689
0
        attrs++;
690
0
    }
691
0
    realSize = attrs - theTemplate;
692
0
    PORT_Assert(realSize <= tsize);
693
0
694
0
    if (slot == NULL) {
695
0
        free_slot = slot = PK11_GetInternalKeySlot();
696
0
        /* we need to free the key slot in the end!!! */
697
0
    }
698
0
699
0
    rwsession = PK11_GetRWSession(slot);
700
0
    if (rwsession == CK_INVALID_SESSION) {
701
0
        PORT_SetError(SEC_ERROR_READ_ONLY);
702
0
        if (free_slot) {
703
0
            PK11_FreeSlot(free_slot);
704
0
        }
705
0
        return SECFailure;
706
0
    }
707
0
708
0
    crv = PK11_GETTAB(slot)->C_CreateObject(rwsession, theTemplate, realSize, &smimeh);
709
0
    if (crv != CKR_OK) {
710
0
        PORT_SetError(PK11_MapError(crv));
711
0
    }
712
0
713
0
    PK11_RestoreROSession(slot, rwsession);
714
0
715
0
    if (free_slot) {
716
0
        PK11_FreeSlot(free_slot);
717
0
    }
718
0
    return SECSuccess;
719
0
}
720
721
CERTSignedCrl *crl_storeCRL(PK11SlotInfo *slot, char *url,
722
                            CERTSignedCrl *newCrl, SECItem *derCrl, int type);
723
724
/* import the CRL into the token */
725
726
CERTSignedCrl *
727
PK11_ImportCRL(PK11SlotInfo *slot, SECItem *derCRL, char *url,
728
               int type, void *wincx, PRInt32 importOptions, PLArenaPool *arena,
729
               PRInt32 decodeoptions)
730
0
{
731
0
    CERTSignedCrl *newCrl, *crl;
732
0
    SECStatus rv;
733
0
    CERTCertificate *caCert = NULL;
734
0
735
0
    newCrl = crl = NULL;
736
0
737
0
    do {
738
0
        newCrl = CERT_DecodeDERCrlWithFlags(arena, derCRL, type,
739
0
                                            decodeoptions);
740
0
        if (newCrl == NULL) {
741
0
            if (type == SEC_CRL_TYPE) {
742
0
                /* only promote error when the error code is too generic */
743
0
                if (PORT_GetError() == SEC_ERROR_BAD_DER)
744
0
                    PORT_SetError(SEC_ERROR_CRL_INVALID);
745
0
            } else {
746
0
                PORT_SetError(SEC_ERROR_KRL_INVALID);
747
0
            }
748
0
            break;
749
0
        }
750
0
751
0
        if (0 == (importOptions & CRL_IMPORT_BYPASS_CHECKS)) {
752
0
            CERTCertDBHandle *handle = CERT_GetDefaultCertDB();
753
0
            PR_ASSERT(handle != NULL);
754
0
            caCert = CERT_FindCertByName(handle,
755
0
                                         &newCrl->crl.derName);
756
0
            if (caCert == NULL) {
757
0
                PORT_SetError(SEC_ERROR_UNKNOWN_ISSUER);
758
0
                break;
759
0
            }
760
0
761
0
            /* If caCert is a v3 certificate, make sure that it can be used for
762
0
               crl signing purpose */
763
0
            rv = CERT_CheckCertUsage(caCert, KU_CRL_SIGN);
764
0
            if (rv != SECSuccess) {
765
0
                break;
766
0
            }
767
0
768
0
            rv = CERT_VerifySignedData(&newCrl->signatureWrap, caCert,
769
0
                                       PR_Now(), wincx);
770
0
            if (rv != SECSuccess) {
771
0
                if (type == SEC_CRL_TYPE) {
772
0
                    PORT_SetError(SEC_ERROR_CRL_BAD_SIGNATURE);
773
0
                } else {
774
0
                    PORT_SetError(SEC_ERROR_KRL_BAD_SIGNATURE);
775
0
                }
776
0
                break;
777
0
            }
778
0
        }
779
0
780
0
        crl = crl_storeCRL(slot, url, newCrl, derCRL, type);
781
0
782
0
    } while (0);
783
0
784
0
    if (crl == NULL) {
785
0
        SEC_DestroyCrl(newCrl);
786
0
    }
787
0
    if (caCert) {
788
0
        CERT_DestroyCertificate(caCert);
789
0
    }
790
0
    return (crl);
791
0
}