~ubuntu-branches/ubuntu/feisty/firefox/feisty-updates

« back to all changes in this revision

Viewing changes to security/nss-fips/cmd/crlutil/crlutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Sack, Alexander Sack
  • Date: 2008-06-23 15:08:12 UTC
  • mfrom: (1.1.24 upstream)
  • Revision ID: james.westby@ubuntu.com-20080623150812-sxdwhn3dz9pmapvf
Tags: 2.0.0.15+0nobinonly-0ubuntu0.7.4
[ Alexander Sack ]
* New security/stability upstream release (v2.0.0.15)
  - see USN-619-1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
4
 * The contents of this file are subject to the Mozilla Public License Version
 
5
 * 1.1 (the "License"); you may not use this file except in compliance with
 
6
 * the License. You may obtain a copy of the License at
 
7
 * http://www.mozilla.org/MPL/
 
8
 *
 
9
 * Software distributed under the License is distributed on an "AS IS" basis,
 
10
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
11
 * for the specific language governing rights and limitations under the
 
12
 * License.
 
13
 *
 
14
 * The Original Code is the Netscape security libraries.
 
15
 *
 
16
 * The Initial Developer of the Original Code is
 
17
 * Netscape Communications Corporation.
 
18
 * Portions created by the Initial Developer are Copyright (C) 1994-2000
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *
 
23
 * Alternatively, the contents of this file may be used under the terms of
 
24
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
25
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
26
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
27
 * of those above. If you wish to allow use of your version of this file only
 
28
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
29
 * use your version of this file under the terms of the MPL, indicate your
 
30
 * decision by deleting the provisions above and replace them with the notice
 
31
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
32
 * the provisions above, a recipient may use your version of this file under
 
33
 * the terms of any one of the MPL, the GPL or the LGPL.
 
34
 *
 
35
 * ***** END LICENSE BLOCK ***** */
 
36
 
 
37
/*
 
38
** certutil.c
 
39
**
 
40
** utility for managing certificates and the cert database
 
41
**
 
42
*/
 
43
/* test only */
 
44
 
 
45
#include "nspr.h"
 
46
#include "plgetopt.h"
 
47
#include "secutil.h"
 
48
#include "cert.h"
 
49
#include "certi.h"
 
50
#include "certdb.h"
 
51
#include "nss.h"
 
52
#include "pk11func.h"
 
53
#include "crlgen.h"
 
54
 
 
55
#define SEC_CERT_DB_EXISTS 0
 
56
#define SEC_CREATE_CERT_DB 1
 
57
 
 
58
static char *progName;
 
59
 
 
60
static CERTSignedCrl *FindCRL
 
61
   (CERTCertDBHandle *certHandle, char *name, int type)
 
62
{
 
63
    CERTSignedCrl *crl = NULL;    
 
64
    CERTCertificate *cert = NULL;
 
65
    SECItem derName;
 
66
 
 
67
    derName.data = NULL;
 
68
    derName.len = 0;
 
69
 
 
70
    cert = CERT_FindCertByNicknameOrEmailAddr(certHandle, name);
 
71
    if (!cert) {
 
72
        CERTName *certName = NULL;
 
73
        PRArenaPool *arena = NULL;
 
74
    
 
75
        certName = CERT_AsciiToName(name);
 
76
        if (certName) {
 
77
            arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
 
78
            if (arena) {
 
79
                SECItem *nameItem = 
 
80
                    SEC_ASN1EncodeItem (arena, NULL, (void *)certName,
 
81
                                        SEC_ASN1_GET(CERT_NameTemplate));
 
82
                if (nameItem) {
 
83
                    SECITEM_CopyItem(NULL, &derName, nameItem);
 
84
                }
 
85
                PORT_FreeArena(arena, PR_FALSE);
 
86
            }
 
87
            CERT_DestroyName(certName);
 
88
        }
 
89
 
 
90
        if (!derName.len || !derName.data) {
 
91
            SECU_PrintError(progName, "could not find certificate named '%s'", name);
 
92
            return ((CERTSignedCrl *)NULL);
 
93
        }
 
94
    } else {
 
95
        SECITEM_CopyItem(NULL, &derName, &cert->derSubject);
 
96
        CERT_DestroyCertificate (cert);
 
97
    }
 
98
 
 
99
    crl = SEC_FindCrlByName(certHandle, &derName, type);
 
100
    if (crl ==NULL) 
 
101
        SECU_PrintError
 
102
                (progName, "could not find %s's CRL", name);
 
103
    if (derName.data) {
 
104
        SECITEM_FreeItem(&derName, PR_FALSE);
 
105
    }
 
106
    return (crl);
 
107
}
 
108
 
 
109
static void DisplayCRL (CERTCertDBHandle *certHandle, char *nickName, int crlType)
 
110
{
 
111
    CERTSignedCrl *crl = NULL;
 
112
 
 
113
    crl = FindCRL (certHandle, nickName, crlType);
 
114
        
 
115
    if (crl) {
 
116
        SECU_PrintCRLInfo (stdout, &crl->crl, "CRL Info:\n", 0);
 
117
        SEC_DestroyCrl (crl);
 
118
    }
 
119
}
 
120
 
 
121
static void ListCRLNames (CERTCertDBHandle *certHandle, int crlType, PRBool deletecrls)
 
122
{
 
123
    CERTCrlHeadNode *crlList = NULL;
 
124
    CERTCrlNode *crlNode = NULL;
 
125
    CERTName *name = NULL;
 
126
    PRArenaPool *arena = NULL;
 
127
    SECStatus rv;
 
128
 
 
129
    do {
 
130
        arena = PORT_NewArena (SEC_ASN1_DEFAULT_ARENA_SIZE);
 
131
        if (arena == NULL) {
 
132
            fprintf(stderr, "%s: fail to allocate memory\n", progName);
 
133
            break;
 
134
        }
 
135
        
 
136
        name = PORT_ArenaZAlloc (arena, sizeof(*name));
 
137
        if (name == NULL) {
 
138
            fprintf(stderr, "%s: fail to allocate memory\n", progName);
 
139
            break;
 
140
        }
 
141
        name->arena = arena;
 
142
            
 
143
        rv = SEC_LookupCrls (certHandle, &crlList, crlType);
 
144
        if (rv != SECSuccess) {
 
145
            fprintf(stderr, "%s: fail to look up CRLs (%s)\n", progName,
 
146
            SECU_Strerror(PORT_GetError()));
 
147
            break;
 
148
        }
 
149
        
 
150
        /* just in case */
 
151
        if (!crlList)
 
152
            break;
 
153
 
 
154
        crlNode  = crlList->first;
 
155
 
 
156
        fprintf (stdout, "\n");
 
157
        fprintf (stdout, "\n%-40s %-5s\n\n", "CRL names", "CRL Type");
 
158
        while (crlNode) {
 
159
            char* asciiname = NULL;
 
160
            CERTCertificate *cert = NULL;
 
161
            if (crlNode->crl && &crlNode->crl->crl.derName) {
 
162
                cert = CERT_FindCertByName(certHandle, 
 
163
                                           &crlNode->crl->crl.derName);
 
164
                if (!cert) {
 
165
                    SECU_PrintError(progName, "could not find signing "
 
166
                                 "certificate in database");
 
167
                }
 
168
            }
 
169
            if (cert) {
 
170
                char* certName = NULL;
 
171
                 if (cert->nickname && PORT_Strlen(cert->nickname) > 0) {
 
172
                    certName = cert->nickname;
 
173
                } else if (cert->emailAddr && PORT_Strlen(cert->emailAddr) > 0) {
 
174
                    certName = cert->emailAddr;
 
175
                }
 
176
                if (certName) {
 
177
                    asciiname = PORT_Strdup(certName);
 
178
                }
 
179
                CERT_DestroyCertificate(cert);
 
180
            }
 
181
                
 
182
            if (!asciiname) {
 
183
                name = &crlNode->crl->crl.name;
 
184
                if (!name){
 
185
                    SECU_PrintError(progName, "fail to get the CRL "
 
186
                                   "issuer name");
 
187
                    continue;
 
188
                }
 
189
                asciiname = CERT_NameToAscii(name);
 
190
            }
 
191
            fprintf (stdout, "%-40s %-5s\n", asciiname, "CRL");
 
192
            if (asciiname) {
 
193
                PORT_Free(asciiname);
 
194
            }
 
195
            if ( PR_TRUE == deletecrls) {
 
196
                CERTSignedCrl* acrl = NULL;
 
197
                SECItem* issuer = &crlNode->crl->crl.derName;
 
198
                acrl = SEC_FindCrlByName(certHandle, issuer, crlType);
 
199
                if (acrl)
 
200
                {
 
201
                    SEC_DeletePermCRL(acrl);
 
202
                    SEC_DestroyCrl(acrl);
 
203
                }
 
204
            }
 
205
            crlNode = crlNode->next;
 
206
        } 
 
207
        
 
208
    } while (0);
 
209
    if (crlList)
 
210
        PORT_FreeArena (crlList->arena, PR_FALSE);
 
211
    PORT_FreeArena (arena, PR_FALSE);
 
212
}
 
213
 
 
214
static void ListCRL (CERTCertDBHandle *certHandle, char *nickName, int crlType)
 
215
{
 
216
    if (nickName == NULL)
 
217
        ListCRLNames (certHandle, crlType, PR_FALSE);
 
218
    else
 
219
        DisplayCRL (certHandle, nickName, crlType);
 
220
}
 
221
 
 
222
 
 
223
 
 
224
static SECStatus DeleteCRL (CERTCertDBHandle *certHandle, char *name, int type)
 
225
{
 
226
    CERTSignedCrl *crl = NULL;    
 
227
    SECStatus rv = SECFailure;
 
228
 
 
229
    crl = FindCRL (certHandle, name, type);
 
230
    if (!crl) {
 
231
        SECU_PrintError
 
232
                (progName, "could not find the issuer %s's CRL", name);
 
233
        return SECFailure;
 
234
    }
 
235
    rv = SEC_DeletePermCRL (crl);
 
236
    SEC_DestroyCrl(crl);
 
237
    if (rv != SECSuccess) {
 
238
        SECU_PrintError(progName, "fail to delete the issuer %s's CRL "
 
239
                        "from the perm database (reason: %s)",
 
240
                        name, SECU_Strerror(PORT_GetError()));
 
241
        return SECFailure;
 
242
    }
 
243
    return (rv);
 
244
}
 
245
 
 
246
SECStatus ImportCRL (CERTCertDBHandle *certHandle, char *url, int type, 
 
247
                     PRFileDesc *inFile, PRInt32 importOptions, PRInt32 decodeOptions)
 
248
{
 
249
    CERTSignedCrl *crl = NULL;
 
250
    SECItem crlDER;
 
251
    PK11SlotInfo* slot = NULL;
 
252
    int rv;
 
253
#if defined(DEBUG_jpierre)
 
254
    PRIntervalTime starttime, endtime, elapsed;
 
255
    PRUint32 mins, secs, msecs;
 
256
#endif
 
257
 
 
258
    crlDER.data = NULL;
 
259
 
 
260
 
 
261
    /* Read in the entire file specified with the -f argument */
 
262
    rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE);
 
263
    if (rv != SECSuccess) {
 
264
        SECU_PrintError(progName, "unable to read input file");
 
265
        return (SECFailure);
 
266
    }
 
267
 
 
268
    decodeOptions |= CRL_DECODE_DONT_COPY_DER;
 
269
 
 
270
    slot = PK11_GetInternalKeySlot();
 
271
 
 
272
#if defined(DEBUG_jpierre)
 
273
    starttime = PR_IntervalNow();
 
274
#endif
 
275
    crl = PK11_ImportCRL(slot, &crlDER, url, type,
 
276
          NULL, importOptions, NULL, decodeOptions);
 
277
#if defined(DEBUG_jpierre)
 
278
    endtime = PR_IntervalNow();
 
279
    elapsed = endtime - starttime;
 
280
    mins = PR_IntervalToSeconds(elapsed) / 60;
 
281
    secs = PR_IntervalToSeconds(elapsed) % 60;
 
282
    msecs = PR_IntervalToMilliseconds(elapsed) % 1000;
 
283
    printf("Elapsed : %2d:%2d.%3d\n", mins, secs, msecs);
 
284
#endif
 
285
    if (!crl) {
 
286
        const char *errString;
 
287
 
 
288
        rv = SECFailure;
 
289
        errString = SECU_Strerror(PORT_GetError());
 
290
        if ( errString && PORT_Strlen (errString) == 0)
 
291
            SECU_PrintError (progName, 
 
292
                "CRL is not imported (error: input CRL is not up to date.)");
 
293
        else    
 
294
            SECU_PrintError (progName, "unable to import CRL");
 
295
    } else {
 
296
        SEC_DestroyCrl (crl);
 
297
    }
 
298
    if (slot) {
 
299
        PK11_FreeSlot(slot);
 
300
    }
 
301
    return (rv);
 
302
}
 
303
 
 
304
 
 
305
static CERTCertificate*
 
306
FindSigningCert(CERTCertDBHandle *certHandle, CERTSignedCrl *signCrl,
 
307
                char *certNickName)
 
308
{                   
 
309
    CERTCertificate *cert = NULL, *certTemp = NULL;
 
310
    SECStatus rv = SECFailure;
 
311
    CERTAuthKeyID* authorityKeyID = NULL;
 
312
    SECItem* subject = NULL;
 
313
 
 
314
    PORT_Assert(certHandle != NULL);
 
315
    if (!certHandle || (!signCrl && !certNickName)) {
 
316
        SECU_PrintError(progName, "invalid args for function "
 
317
                        "FindSigningCert \n");
 
318
        return NULL;
 
319
    }
 
320
 
 
321
    if (signCrl) {
 
322
#if 0
 
323
        authorityKeyID = SECU_FindCRLAuthKeyIDExten(tmpArena, scrl);
 
324
#endif
 
325
        subject = &signCrl->crl.derName;
 
326
    } else {
 
327
        certTemp = CERT_FindCertByNickname(certHandle, certNickName);
 
328
        if (!certTemp) {
 
329
            SECU_PrintError(progName, "could not find certificate \"%s\" "
 
330
                            "in database", certNickName);
 
331
            goto loser;
 
332
        }
 
333
        subject = &certTemp->derSubject;
 
334
    }
 
335
 
 
336
    cert = SECU_FindCrlIssuer(certHandle, subject, authorityKeyID, PR_Now());
 
337
    if (!cert) {
 
338
        SECU_PrintError(progName, "could not find signing certificate "
 
339
                        "in database");
 
340
        goto loser;
 
341
    } else {
 
342
        rv = SECSuccess;
 
343
    }
 
344
 
 
345
  loser:
 
346
    if (certTemp)
 
347
        CERT_DestroyCertificate(certTemp);
 
348
    if (cert && rv != SECSuccess)
 
349
        CERT_DestroyCertificate(cert);
 
350
    return cert;
 
351
}
 
352
 
 
353
static CERTSignedCrl*
 
354
DuplicateModCrl(PRArenaPool *arena, CERTCertDBHandle *certHandle,
 
355
                CERTCertificate **cert, char *certNickName,
 
356
                PRFileDesc *inFile, PRInt32 decodeOptions,
 
357
                PRInt32 importOptions)
 
358
{
 
359
    SECItem crlDER;
 
360
    CERTSignedCrl *signCrl = NULL;
 
361
    CERTSignedCrl *modCrl = NULL;
 
362
    PRArenaPool *modArena = NULL;
 
363
    SECStatus rv = SECSuccess;
 
364
 
 
365
    PORT_Assert(arena != NULL && certHandle != NULL &&
 
366
                certNickName != NULL);
 
367
    if (!arena || !certHandle || !certNickName) {
 
368
        SECU_PrintError(progName, "DuplicateModCrl: invalid args\n");
 
369
        return NULL;
 
370
    }
 
371
 
 
372
    modArena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
 
373
    if (!modArena) {
 
374
        SECU_PrintError(progName, "fail to allocate memory\n");
 
375
        return NULL;
 
376
    }
 
377
    
 
378
    if (inFile != NULL) {
 
379
        rv = SECU_ReadDERFromFile(&crlDER, inFile, PR_FALSE);
 
380
        if (rv != SECSuccess) {
 
381
            SECU_PrintError(progName, "unable to read input file");
 
382
            PORT_FreeArena(modArena, PR_FALSE);
 
383
            goto loser;
 
384
        }
 
385
        
 
386
        decodeOptions |= CRL_DECODE_DONT_COPY_DER;
 
387
        
 
388
        modCrl = CERT_DecodeDERCrlWithFlags(modArena, &crlDER, SEC_CRL_TYPE,
 
389
                                            decodeOptions);
 
390
        if (!modCrl) {
 
391
            SECU_PrintError(progName, "fail to decode CRL");
 
392
            goto loser;
 
393
        }
 
394
        
 
395
        if (0 == (importOptions & CRL_IMPORT_BYPASS_CHECKS)){
 
396
            /* If caCert is a v2 certificate, make sure that it
 
397
             * can be used for crl signing purpose */
 
398
            *cert = FindSigningCert(certHandle, modCrl, NULL);
 
399
            if (!*cert) {
 
400
                goto loser;
 
401
            }
 
402
 
 
403
            rv = CERT_VerifySignedData(&modCrl->signatureWrap, *cert,
 
404
                                       PR_Now(), NULL);
 
405
            if (rv != SECSuccess) {
 
406
                SECU_PrintError(progName, "fail to verify signed data\n");
 
407
                goto loser;
 
408
            }
 
409
        }
 
410
    } else {
 
411
        modCrl = FindCRL(certHandle, certNickName, SEC_CRL_TYPE);
 
412
        if (!modCrl) {
 
413
            SECU_PrintError(progName, "fail to find crl %s in database\n",
 
414
                            certNickName);
 
415
            goto loser;
 
416
        }
 
417
    }
 
418
 
 
419
    signCrl = PORT_ArenaZNew(arena, CERTSignedCrl);
 
420
    if (signCrl == NULL) {
 
421
        SECU_PrintError(progName, "fail to allocate memory\n");
 
422
        goto loser;
 
423
    }
 
424
 
 
425
    rv = SECU_CopyCRL(arena, &signCrl->crl, &modCrl->crl);
 
426
    if (rv != SECSuccess) {
 
427
        SECU_PrintError(progName, "unable to dublicate crl for "
 
428
                        "modification.");
 
429
        goto loser;
 
430
    }  
 
431
 
 
432
    signCrl->arena = arena;    
 
433
 
 
434
  loser:
 
435
    SECITEM_FreeItem(&crlDER, PR_FALSE);
 
436
    if (modCrl)
 
437
        SEC_DestroyCrl(modCrl);
 
438
    if (rv != SECSuccess && signCrl) {
 
439
        SEC_DestroyCrl(signCrl);
 
440
        signCrl = NULL;
 
441
    }
 
442
    return signCrl;
 
443
}
 
444
 
 
445
 
 
446
static CERTSignedCrl*
 
447
CreateNewCrl(PRArenaPool *arena, CERTCertDBHandle *certHandle,
 
448
             CERTCertificate *cert)
 
449
 
450
    CERTSignedCrl *signCrl = NULL;
 
451
    void *dummy = NULL;
 
452
    SECStatus rv;
 
453
    void* mark = NULL;
 
454
 
 
455
    /* if the CERTSignedCrl structure changes, this function will need to be
 
456
       updated as well */
 
457
    PORT_Assert(cert != NULL);
 
458
    if (!cert || !arena) {
 
459
        SECU_PrintError(progName, "invalid args for function "
 
460
                        "CreateNewCrl\n");
 
461
        return NULL;
 
462
    }
 
463
 
 
464
    mark = PORT_ArenaMark(arena);
 
465
        
 
466
    signCrl = PORT_ArenaZNew(arena, CERTSignedCrl);
 
467
    if (signCrl == NULL) {
 
468
        SECU_PrintError(progName, "fail to allocate memory\n");
 
469
        return NULL;
 
470
    }
 
471
 
 
472
    dummy = SEC_ASN1EncodeInteger(arena, &signCrl->crl.version,
 
473
                                  SEC_CRL_VERSION_2);
 
474
    /* set crl->version */
 
475
    if (!dummy) {
 
476
        SECU_PrintError(progName, "fail to create crl version data "
 
477
                        "container\n");
 
478
        goto loser;
 
479
    }
 
480
    
 
481
    /* copy SECItem name from cert */
 
482
    rv = SECITEM_CopyItem(arena, &signCrl->crl.derName, &cert->derSubject);
 
483
    if (rv != SECSuccess) {
 
484
        SECU_PrintError(progName, "fail to duplicate der name from "
 
485
                        "certificate.\n");
 
486
        goto loser;
 
487
    }
 
488
        
 
489
    /* copy CERTName name structure from cert issuer */
 
490
    rv = CERT_CopyName (arena, &signCrl->crl.name, &cert->subject);
 
491
    if (rv != SECSuccess) {
 
492
        SECU_PrintError(progName, "fail to duplicate RD name from "
 
493
                        "certificate.\n");
 
494
        goto loser;
 
495
    }
 
496
 
 
497
    rv = DER_EncodeTimeChoice(arena, &signCrl->crl.lastUpdate, PR_Now());
 
498
    if (rv != SECSuccess) {
 
499
        SECU_PrintError(progName, "fail to encode current time\n");
 
500
        goto loser;
 
501
    }
 
502
 
 
503
    /* set fields */
 
504
    signCrl->arena = arena;
 
505
    signCrl->dbhandle = certHandle;
 
506
    signCrl->crl.arena = arena;
 
507
 
 
508
    return signCrl;
 
509
 
 
510
  loser:
 
511
    PORT_ArenaRelease(arena, mark);
 
512
    return NULL;
 
513
}
 
514
 
 
515
 
 
516
static SECStatus
 
517
UpdateCrl(CERTSignedCrl *signCrl, PRFileDesc *inCrlInitFile)
 
518
{
 
519
    CRLGENGeneratorData *crlGenData = NULL;
 
520
    SECStatus rv;
 
521
    
 
522
    PORT_Assert(signCrl != NULL && inCrlInitFile != NULL);
 
523
    if (!signCrl || !inCrlInitFile) {
 
524
        SECU_PrintError(progName, "invalid args for function "
 
525
                        "CreateNewCrl\n");
 
526
        return SECFailure;
 
527
    }
 
528
 
 
529
    crlGenData = CRLGEN_InitCrlGeneration(signCrl, inCrlInitFile);
 
530
    if (!crlGenData) {
 
531
        SECU_PrintError(progName, "can not initialize parser structure.\n");
 
532
        return SECFailure;
 
533
    }
 
534
 
 
535
    rv = CRLGEN_ExtHandleInit(crlGenData);
 
536
    if (rv == SECFailure) {
 
537
        SECU_PrintError(progName, "can not initialize entries handle.\n");
 
538
        goto loser;
 
539
    }
 
540
        
 
541
    rv = CRLGEN_StartCrlGen(crlGenData);
 
542
    if (rv != SECSuccess) {
 
543
        SECU_PrintError(progName, "crl generation failed");
 
544
        goto loser;
 
545
    }
 
546
 
 
547
  loser:
 
548
    /* CommitExtensionsAndEntries is partially responsible for freeing
 
549
     * up memory that was used for CRL generation. Should be called regardless
 
550
     * of previouse call status, but only after initialization of
 
551
     * crlGenData was done. It will commit all changes that was done before
 
552
     * an error has occured.
 
553
     */
 
554
    if (SECSuccess != CRLGEN_CommitExtensionsAndEntries(crlGenData)) {
 
555
        SECU_PrintError(progName, "crl generation failed");
 
556
        rv = SECFailure;
 
557
    }
 
558
    CRLGEN_FinalizeCrlGeneration(crlGenData);    
 
559
    return rv;
 
560
}
 
561
 
 
562
static SECStatus
 
563
SignAndStoreCrl(CERTSignedCrl *signCrl, CERTCertificate *cert,
 
564
                char *outFileName, SECOidTag hashAlgTag, int ascii,
 
565
                char *slotName, char *url, secuPWData *pwdata)
 
566
{
 
567
    PK11SlotInfo *slot = NULL;
 
568
    PRFileDesc   *outFile = NULL;
 
569
    SECStatus rv;
 
570
    SignAndEncodeFuncExitStat errCode;
 
571
 
 
572
    PORT_Assert(signCrl && (!ascii || outFileName));
 
573
    if (!signCrl || (ascii && !outFileName)) {
 
574
        SECU_PrintError(progName, "invalid args for function "
 
575
                        "SignAndStoreCrl\n");
 
576
        return SECFailure;
 
577
    }
 
578
 
 
579
    if (!slotName || !PL_strcmp(slotName, "internal"))
 
580
        slot = PK11_GetInternalKeySlot();
 
581
    else
 
582
        slot = PK11_FindSlotByName(slotName);
 
583
    if (!slot) {
 
584
        SECU_PrintError(progName, "can not find requested slot");
 
585
        return SECFailure;
 
586
    }
 
587
 
 
588
    if (PK11_NeedLogin(slot)) {
 
589
        rv = PK11_Authenticate(slot, PR_TRUE, pwdata);
 
590
        if (rv != SECSuccess)
 
591
            goto loser;
 
592
    }
 
593
 
 
594
    rv = SECU_SignAndEncodeCRL(cert, signCrl, hashAlgTag, &errCode);
 
595
    if (rv != SECSuccess) {
 
596
        char* errMsg = NULL;
 
597
        switch (errCode)
 
598
        {
 
599
            case noKeyFound:
 
600
                errMsg = "No private key found of signing cert";
 
601
                break;
 
602
 
 
603
            case noSignatureMatch:
 
604
                errMsg = "Key and Algorithm OId are do not match";
 
605
                break;
 
606
 
 
607
            default:
 
608
            case failToEncode:
 
609
                errMsg = "Failed to encode crl structure";
 
610
                break;
 
611
 
 
612
            case failToSign:
 
613
                errMsg = "Failed to sign crl structure";
 
614
                break;
 
615
 
 
616
            case noMem:
 
617
                errMsg = "Can not allocate memory";
 
618
                break;
 
619
        }
 
620
        SECU_PrintError(progName, "%s\n", errMsg);
 
621
        goto loser;
 
622
    }
 
623
 
 
624
    if (outFileName) {
 
625
        outFile = PR_Open(outFileName, PR_WRONLY|PR_CREATE_FILE, PR_IRUSR | PR_IWUSR);
 
626
        if (!outFile) {
 
627
            SECU_PrintError(progName, "unable to open \"%s\" for writing\n",
 
628
                            outFileName);
 
629
            goto loser;
 
630
        }
 
631
    }
 
632
 
 
633
    rv = SECU_StoreCRL(slot, signCrl->derCrl, outFile, ascii, url);
 
634
    if (rv != SECSuccess) {
 
635
        SECU_PrintError(progName, "fail to save CRL\n");
 
636
    }
 
637
 
 
638
  loser:
 
639
    if (outFile)
 
640
        PR_Close(outFile);
 
641
    if (slot)
 
642
        PK11_FreeSlot(slot);
 
643
    return rv;
 
644
}
 
645
 
 
646
static SECStatus
 
647
GenerateCRL (CERTCertDBHandle *certHandle, char *certNickName, 
 
648
             PRFileDesc *inCrlInitFile,  PRFileDesc *inFile,
 
649
             char *outFileName, int ascii, char *slotName,
 
650
             PRInt32 importOptions, char *alg, PRBool quiet,
 
651
             PRInt32 decodeOptions, char *url, secuPWData *pwdata,
 
652
             int modifyFlag)
 
653
{
 
654
    CERTCertificate *cert = NULL;
 
655
    CERTSignedCrl *signCrl = NULL;
 
656
    PRArenaPool *arena = NULL;
 
657
    SECStatus rv;
 
658
    SECOidTag hashAlgTag = SEC_OID_UNKNOWN;
 
659
 
 
660
    if (alg) {
 
661
        hashAlgTag = SECU_StringToSignatureAlgTag(alg);
 
662
        if (hashAlgTag == SEC_OID_UNKNOWN) {
 
663
            SECU_PrintError(progName, "%s -Z:  %s is not a recognized type.\n",
 
664
                            progName, alg);
 
665
            return SECFailure;
 
666
        }
 
667
    } else {
 
668
        hashAlgTag = SEC_OID_UNKNOWN;
 
669
    }
 
670
 
 
671
    arena = PORT_NewArena (SEC_ASN1_DEFAULT_ARENA_SIZE);
 
672
    if (!arena) {
 
673
        SECU_PrintError(progName, "fail to allocate memory\n");
 
674
        return SECFailure;
 
675
    }
 
676
 
 
677
    if (modifyFlag == PR_TRUE) {
 
678
        signCrl = DuplicateModCrl(arena, certHandle, &cert, certNickName,
 
679
                                         inFile, decodeOptions, importOptions);
 
680
        if (signCrl == NULL) {
 
681
            goto loser;
 
682
        }
 
683
    }
 
684
 
 
685
    if (!cert) {
 
686
        cert = FindSigningCert(certHandle, signCrl, certNickName);
 
687
        if (cert == NULL) {
 
688
            goto loser;
 
689
        }
 
690
    }
 
691
 
 
692
    if (!signCrl) {
 
693
        if (modifyFlag == PR_TRUE) {
 
694
            if (!outFileName) {
 
695
                int len = strlen(certNickName) + 5;
 
696
                outFileName = PORT_ArenaAlloc(arena, len);
 
697
                PR_snprintf(outFileName, len, "%s.crl", certNickName);
 
698
            }
 
699
            SECU_PrintError(progName, "Will try to generate crl. "
 
700
                            "It will be saved in file: %s",
 
701
                            outFileName);
 
702
        }
 
703
        signCrl = CreateNewCrl(arena, certHandle, cert);
 
704
        if (!signCrl)
 
705
            goto loser;
 
706
    }
 
707
 
 
708
    rv = UpdateCrl(signCrl, inCrlInitFile);
 
709
    if (rv != SECSuccess) {
 
710
        goto loser;
 
711
    }
 
712
 
 
713
    rv = SignAndStoreCrl(signCrl, cert, outFileName, hashAlgTag, ascii,
 
714
                         slotName, url, pwdata);
 
715
    if (rv != SECSuccess) {
 
716
        goto loser;
 
717
    }
 
718
 
 
719
    if (signCrl && !quiet) {
 
720
        SECU_PrintCRLInfo (stdout, &signCrl->crl, "CRL Info:\n", 0);
 
721
    }
 
722
 
 
723
  loser:
 
724
    if (arena && (!signCrl || !signCrl->arena))
 
725
        PORT_FreeArena (arena, PR_FALSE);
 
726
    if (signCrl)
 
727
        SEC_DestroyCrl (signCrl);
 
728
    if (cert)
 
729
        CERT_DestroyCertificate (cert);
 
730
    return (rv);
 
731
}
 
732
 
 
733
static void Usage(char *progName)
 
734
{
 
735
    fprintf(stderr,
 
736
            "Usage:  %s -L [-n nickname] [-d keydir] [-P dbprefix] [-t crlType]\n"
 
737
            "        %s -D -n nickname [-d keydir] [-P dbprefix]\n"
 
738
            "        %s -I -i crl -t crlType [-u url] [-d keydir] [-P dbprefix] [-B] "
 
739
            "[-p pwd-file] -w [pwd-string]\n"
 
740
            "        %s -E -t crlType [-d keydir] [-P dbprefix]\n"
 
741
            "        %s -T\n"
 
742
            "        %s -G|-M -c crl-init-file -n nickname [-i crl] [-u url] "
 
743
            "[-d keydir] [-P dbprefix] [-Z alg] ] [-p pwd-file] -w [pwd-string] "
 
744
            "[-a] [-B]\n",
 
745
            progName, progName, progName, progName, progName, progName);
 
746
 
 
747
    fprintf (stderr, "%-15s List CRL\n", "-L");
 
748
    fprintf(stderr, "%-20s Specify the nickname of the CA certificate\n",
 
749
            "-n nickname");
 
750
    fprintf(stderr, "%-20s Key database directory (default is ~/.netscape)\n",
 
751
            "-d keydir");
 
752
    fprintf(stderr, "%-20s Cert & Key database prefix (default is \"\")\n",
 
753
            "-P dbprefix");
 
754
   
 
755
    fprintf (stderr, "%-15s Delete a CRL from the cert database\n", "-D");    
 
756
    fprintf(stderr, "%-20s Specify the nickname for the CA certificate\n",
 
757
            "-n nickname");
 
758
    fprintf(stderr, "%-20s Specify the crl type.\n", "-t crlType");
 
759
    fprintf(stderr, "%-20s Key database directory (default is ~/.netscape)\n",
 
760
            "-d keydir");
 
761
    fprintf(stderr, "%-20s Cert & Key database prefix (default is \"\")\n",
 
762
            "-P dbprefix");
 
763
 
 
764
    fprintf (stderr, "%-15s Erase all CRLs of specified type from hte cert database\n", "-E");
 
765
    fprintf(stderr, "%-20s Specify the crl type.\n", "-t crlType");
 
766
    fprintf(stderr, "%-20s Key database directory (default is ~/.netscape)\n",
 
767
            "-d keydir");
 
768
    fprintf(stderr, "%-20s Cert & Key database prefix (default is \"\")\n",
 
769
            "-P dbprefix");
 
770
 
 
771
    fprintf (stderr, "%-15s Import a CRL to the cert database\n", "-I");    
 
772
    fprintf(stderr, "%-20s Specify the file which contains the CRL to import\n",
 
773
            "-i crl");
 
774
    fprintf(stderr, "%-20s Specify the url.\n", "-u url");
 
775
    fprintf(stderr, "%-20s Specify the crl type.\n", "-t crlType");
 
776
    fprintf(stderr, "%-20s Key database directory (default is ~/.netscape)\n",
 
777
            "-d keydir");
 
778
    fprintf(stderr, "%-20s Cert & Key database prefix (default is \"\")\n",
 
779
            "-P dbprefix");
 
780
#ifdef DEBUG
 
781
    fprintf (stderr, "%-15s Test . Only for debugging purposes. See source code\n", "-T");
 
782
#endif
 
783
    fprintf(stderr, "%-20s CRL Types (default is SEC_CRL_TYPE):\n", " ");
 
784
    fprintf(stderr, "%-20s \t 0 - SEC_KRL_TYPE\n", " ");
 
785
    fprintf(stderr, "%-20s \t 1 - SEC_CRL_TYPE\n", " ");        
 
786
    fprintf(stderr, "\n%-20s Bypass CA certificate checks.\n", "-B");
 
787
    fprintf(stderr, "\n%-20s Partial decode for faster operation.\n", "-p");
 
788
    fprintf(stderr, "%-20s Repeat the operation.\n", "-r <iterations>");
 
789
    fprintf(stderr, "\n%-15s Create CRL\n", "-G");
 
790
    fprintf(stderr, "%-15s Modify CRL\n", "-M");
 
791
    fprintf(stderr, "%-20s Specify crl initialization file\n",
 
792
            "-c crl-conf-file");
 
793
    fprintf(stderr, "%-20s Specify the nickname of the CA certificate\n",
 
794
            "-n nickname");
 
795
    fprintf(stderr, "%-20s Specify the file which contains the CRL to import\n",
 
796
            "-i crl");
 
797
    fprintf(stderr, "%-20s Specify a CRL output file\n",
 
798
            "-o crl-output-file");
 
799
    fprintf(stderr, "%-20s Specify to use base64 encoded CRL output format\n",
 
800
            "-a");
 
801
    fprintf(stderr, "%-20s Key database directory (default is ~/.netscape)\n",
 
802
            "-d keydir");
 
803
    fprintf(stderr, "%-20s Provide path to a default pwd file\n",
 
804
            "-f pwd-file");
 
805
    fprintf(stderr, "%-20s Provide db password in command line\n",
 
806
            "-w pwd-string");
 
807
    fprintf(stderr, "%-20s Cert & Key database prefix (default is \"\")\n",
 
808
            "-P dbprefix");
 
809
    fprintf(stderr, "%-20s Specify the url.\n", "-u url");
 
810
    fprintf(stderr, "\n%-20s Bypass CA certificate checks.\n", "-B");
 
811
 
 
812
    exit(-1);
 
813
}
 
814
 
 
815
int main(int argc, char **argv)
 
816
{
 
817
    SECItem privKeyDER;
 
818
    CERTCertDBHandle *certHandle;
 
819
    FILE *certFile;
 
820
    PRFileDesc *inFile;
 
821
    PRFileDesc *inCrlInitFile = NULL;
 
822
    int generateCRL;
 
823
    int modifyCRL;
 
824
    int listCRL;
 
825
    int importCRL;
 
826
    int deleteCRL;
 
827
    int rv;
 
828
    char *nickName;
 
829
    char *url;
 
830
    char *dbPrefix = "";
 
831
    char *alg = NULL;
 
832
    char *outFile = NULL;
 
833
    char *slotName = NULL;
 
834
    int ascii = 0;
 
835
    int crlType;
 
836
    PLOptState *optstate;
 
837
    PLOptStatus status;
 
838
    SECStatus secstatus;
 
839
    PRInt32 decodeOptions = CRL_DECODE_DEFAULT_OPTIONS;
 
840
    PRInt32 importOptions = CRL_IMPORT_DEFAULT_OPTIONS;
 
841
    PRBool quiet = PR_FALSE;
 
842
    PRBool test = PR_FALSE;
 
843
    PRBool erase = PR_FALSE;
 
844
    PRInt32 i = 0;
 
845
    PRInt32 iterations = 1;
 
846
 
 
847
    secuPWData  pwdata          = { PW_NONE, 0 };
 
848
 
 
849
    progName = strrchr(argv[0], '/');
 
850
    progName = progName ? progName+1 : argv[0];
 
851
 
 
852
    rv = 0;
 
853
    deleteCRL = importCRL = listCRL = generateCRL = modifyCRL = 0;
 
854
    certFile = NULL;
 
855
    inFile = NULL;
 
856
    nickName = url = NULL;
 
857
    privKeyDER.data = NULL;
 
858
    certHandle = NULL;
 
859
    crlType = SEC_CRL_TYPE;
 
860
    /*
 
861
     * Parse command line arguments
 
862
     */
 
863
    optstate = PL_CreateOptState(argc, argv, "sqBCDGILMTEP:f:d:i:h:n:p:t:u:r:aZ:o:c:");
 
864
    while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
 
865
        switch (optstate->option) {
 
866
          case '?':
 
867
            Usage(progName);
 
868
            break;
 
869
 
 
870
          case 'T':
 
871
            test = PR_TRUE;
 
872
            break;
 
873
 
 
874
          case 'E':
 
875
            erase = PR_TRUE;
 
876
            break;
 
877
 
 
878
          case 'B':
 
879
            importOptions |= CRL_IMPORT_BYPASS_CHECKS;
 
880
            break;
 
881
 
 
882
          case 'G':
 
883
            generateCRL = 1;
 
884
            break;
 
885
 
 
886
          case 'M':
 
887
            modifyCRL = 1;
 
888
            break;
 
889
 
 
890
          case 'D':
 
891
              deleteCRL = 1;
 
892
              break;
 
893
 
 
894
          case 'I':
 
895
              importCRL = 1;
 
896
              break;
 
897
                   
 
898
          case 'C':
 
899
          case 'L':
 
900
              listCRL = 1;
 
901
              break;
 
902
 
 
903
          case 'P':
 
904
            dbPrefix = strdup(optstate->value);
 
905
            break;
 
906
              
 
907
          case 'Z':
 
908
              alg = strdup(optstate->value);
 
909
              break;
 
910
              
 
911
          case 'a':
 
912
              ascii = 1;
 
913
              break;
 
914
 
 
915
          case 'c':
 
916
              inCrlInitFile = PR_Open(optstate->value, PR_RDONLY, 0);
 
917
              if (!inCrlInitFile) {
 
918
                  PR_fprintf(PR_STDERR, "%s: unable to open \"%s\" for reading\n",
 
919
                             progName, optstate->value);
 
920
                  PL_DestroyOptState(optstate);
 
921
                  return -1;
 
922
              }
 
923
              break;
 
924
            
 
925
          case 'd':
 
926
              SECU_ConfigDirectory(optstate->value);
 
927
              break;
 
928
 
 
929
          case 'f':
 
930
              pwdata.source = PW_FROMFILE;
 
931
              pwdata.data = strdup(optstate->value);
 
932
              break;
 
933
 
 
934
          case 'h':
 
935
              slotName = strdup(optstate->value);
 
936
              break;
 
937
 
 
938
          case 'i':
 
939
            inFile = PR_Open(optstate->value, PR_RDONLY, 0);
 
940
            if (!inFile) {
 
941
                PR_fprintf(PR_STDERR, "%s: unable to open \"%s\" for reading\n",
 
942
                        progName, optstate->value);
 
943
                PL_DestroyOptState(optstate);
 
944
                return -1;
 
945
            }
 
946
            break;
 
947
            
 
948
          case 'n':
 
949
            nickName = strdup(optstate->value);
 
950
            break;
 
951
 
 
952
          case 'o':
 
953
            outFile = strdup(optstate->value);
 
954
            break;
 
955
            
 
956
          case 'p':
 
957
            decodeOptions |= CRL_DECODE_SKIP_ENTRIES;
 
958
            break;
 
959
 
 
960
          case 'r': {
 
961
            const char* str = optstate->value;
 
962
            if (str && atoi(str)>0)
 
963
                iterations = atoi(str);
 
964
            }
 
965
            break;
 
966
            
 
967
          case 't': {
 
968
            char *type;
 
969
            
 
970
            type = strdup(optstate->value);
 
971
            crlType = atoi (type);
 
972
            if (crlType != SEC_CRL_TYPE && crlType != SEC_KRL_TYPE) {
 
973
                PR_fprintf(PR_STDERR, "%s: invalid crl type\n", progName);
 
974
                PL_DestroyOptState(optstate);
 
975
                return -1;
 
976
            }
 
977
            break;
 
978
 
 
979
          case 'q':
 
980
            quiet = PR_TRUE;
 
981
            break;
 
982
 
 
983
          case 'w':
 
984
              pwdata.source = PW_PLAINTEXT;
 
985
              pwdata.data = strdup(optstate->value);
 
986
              break;
 
987
 
 
988
          case 'u':
 
989
            url = strdup(optstate->value);
 
990
            break;
 
991
 
 
992
          }
 
993
        }
 
994
    }
 
995
    PL_DestroyOptState(optstate);
 
996
 
 
997
    if (deleteCRL && !nickName) Usage (progName);
 
998
    if (importCRL && !inFile) Usage (progName);
 
999
    if ((generateCRL && !nickName) ||
 
1000
        (modifyCRL && !inFile && !nickName)) Usage (progName);
 
1001
    if (!(listCRL || deleteCRL || importCRL || generateCRL ||
 
1002
          modifyCRL || test || erase)) Usage (progName);
 
1003
    
 
1004
    PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
 
1005
 
 
1006
    PK11_SetPasswordFunc(SECU_GetModulePassword);
 
1007
 
 
1008
    secstatus = NSS_Initialize(SECU_ConfigDirectory(NULL), dbPrefix, dbPrefix,
 
1009
                               "secmod.db", 0);
 
1010
    if (secstatus != SECSuccess) {
 
1011
        SECU_PrintPRandOSError(progName);
 
1012
        return -1;
 
1013
    }
 
1014
    SECU_RegisterDynamicOids();
 
1015
 
 
1016
    certHandle = CERT_GetDefaultCertDB();
 
1017
    if (certHandle == NULL) {
 
1018
        SECU_PrintError(progName, "unable to open the cert db");                
 
1019
        /*ignoring return value of NSS_Shutdown() as code returns -1*/
 
1020
        (void) NSS_Shutdown();
 
1021
        return (-1);
 
1022
    }
 
1023
 
 
1024
    CRLGEN_InitCrlGenParserLock();
 
1025
 
 
1026
    for (i=0; i<iterations; i++) {
 
1027
        /* Read in the private key info */
 
1028
        if (deleteCRL) 
 
1029
            DeleteCRL (certHandle, nickName, crlType);
 
1030
        else if (listCRL) {
 
1031
            ListCRL (certHandle, nickName, crlType);
 
1032
        }
 
1033
        else if (importCRL) {
 
1034
            rv = ImportCRL (certHandle, url, crlType, inFile, importOptions,
 
1035
                            decodeOptions);
 
1036
        } else if (generateCRL || modifyCRL) {
 
1037
            if (!inCrlInitFile)
 
1038
                inCrlInitFile = PR_STDIN;
 
1039
            rv = GenerateCRL (certHandle, nickName, inCrlInitFile,
 
1040
                              inFile, outFile, ascii,  slotName,
 
1041
                              importOptions, alg, quiet,
 
1042
                              decodeOptions, url, &pwdata,
 
1043
                              modifyCRL);
 
1044
        }
 
1045
        else if (erase) {
 
1046
            /* list and delete all CRLs */
 
1047
            ListCRLNames (certHandle, crlType, PR_TRUE);
 
1048
        }
 
1049
#ifdef DEBUG
 
1050
        else if (test) {
 
1051
            /* list and delete all CRLs */
 
1052
            ListCRLNames (certHandle, crlType, PR_TRUE);
 
1053
            /* list CRLs */
 
1054
            ListCRLNames (certHandle, crlType, PR_FALSE);
 
1055
            /* import CRL as a blob */
 
1056
            rv = ImportCRL (certHandle, url, crlType, inFile, importOptions,
 
1057
                            decodeOptions);
 
1058
            /* list CRLs */
 
1059
            ListCRLNames (certHandle, crlType, PR_FALSE);
 
1060
        }
 
1061
#endif    
 
1062
    }
 
1063
 
 
1064
    CRLGEN_DestroyCrlGenParserLock();
 
1065
 
 
1066
    if (NSS_Shutdown() != SECSuccess) {
 
1067
        rv = SECFailure;
 
1068
    }
 
1069
 
 
1070
    return (rv != SECSuccess);
 
1071
}