summaryrefslogtreecommitdiff
path: root/security/nss/cmd/pkiutil/pkiutil.c
blob: 7d45c31918806a76359871b429ca64ed69733b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

#include "nspr.h"
#include "prtypes.h"
#include "prtime.h"
#include "prlong.h"
#include "nss.h"
#include "cmdutil.h"
#include "nsspki.h"
/* hmmm...*/
#include "pki.h"

#define PKIUTIL_VERSION_STRING "pkiutil version 0.1"

char *progName = NULL;

typedef struct {
    PRBool      raw;
    PRBool      ascii;
    char       *name;
    PRFileDesc *file;
} objOutputMode;

typedef enum {
    PKIUnknown = -1,
    PKICertificate,
    PKIPublicKey,
    PKIPrivateKey,
    PKIAny
} PKIObjectType;

static PKIObjectType
get_object_class(char *type)
{
    if (strcmp(type, "certificate") == 0 || strcmp(type, "cert") == 0 ||
        strcmp(type, "Certificate") == 0 || strcmp(type, "Cert") == 0) {
	return PKICertificate;
    } else if (strcmp(type, "public_key") == 0 || 
               strcmp(type, "PublicKey") == 0) {
	return PKIPublicKey;
    } else if (strcmp(type, "private_key") == 0 || 
               strcmp(type, "PrivateKey") == 0) {
	return PKIPrivateKey;
    } else if (strcmp(type, "all") == 0 || strcmp(type, "any") == 0) {
	return PKIAny;
    }
    fprintf(stderr, "%s: \"%s\" is not a valid PKCS#11 object type.\n",
                     progName, type);
    return PKIUnknown;
}

static PRStatus
print_cert_callback(NSSCertificate *c, void *arg)
{
    int i;
    NSSUTF8 *label;
    NSSItem *id;
    label = NSSCertificate_GetLabel(c);
    printf("%s\n", label);
    nss_ZFreeIf((void*)label);
#if 0
    id = NSSCertificate_GetID(c);
    for (i=0; i<id->size; i++) {
	printf("%c", ((char *)id->data)[i]);
    }
    printf("\n");
#endif
    return PR_SUCCESS;
}

/*  pkiutil commands  */
enum {
    cmd_Add = 0,
    cmd_Dump,
    cmd_List,
    cmd_Version,
    pkiutil_num_commands
};

/*  pkiutil options */
enum {
    opt_Help = 0,
    opt_Ascii,
    opt_ProfileDir,
    opt_TokenName,
    opt_InputFile,
    opt_Nickname,
    opt_OutputFile,
    opt_Binary,
    opt_Trust,
    opt_Type,
    pkiutil_num_options
};

static cmdCommandLineArg pkiutil_commands[] =
{
 { /* cmd_Add         */  'A', "add", CMDNoArg, 0, PR_FALSE, 
                           CMDBIT(opt_Nickname) | CMDBIT(opt_Trust), 
                           CMDBIT(opt_Ascii) | CMDBIT(opt_ProfileDir) 
                           | CMDBIT(opt_TokenName) | CMDBIT(opt_InputFile) 
                           | CMDBIT(opt_Binary) | CMDBIT(opt_Type) },
 { /* cmd_Dump        */   0 , "dump", CMDNoArg, 0, PR_FALSE,
                           CMDBIT(opt_Nickname),
                           CMDBIT(opt_Ascii) | CMDBIT(opt_ProfileDir) 
                           | CMDBIT(opt_TokenName) | CMDBIT(opt_Binary)
                           | CMDBIT(opt_Type) },
 { /* cmd_List        */  'L', "list", CMDNoArg, 0, PR_FALSE, 0, 
                           CMDBIT(opt_Ascii) | CMDBIT(opt_ProfileDir) 
                           | CMDBIT(opt_TokenName) | CMDBIT(opt_Binary)
                           | CMDBIT(opt_Nickname) | CMDBIT(opt_Type) },
 { /* cmd_Version     */  'Y', "version", CMDNoArg, 0, PR_FALSE, 0, 0 }
};

static cmdCommandLineOpt pkiutil_options[] =
{
 { /* opt_Help        */  '?', "help",     CMDNoArg,   0, PR_FALSE },
 { /* opt_Ascii       */  'a', "ascii",    CMDNoArg,   0, PR_FALSE },
 { /* opt_ProfileDir  */  'd', "dbdir",    CMDArgReq,  0, PR_FALSE },
 { /* opt_TokenName   */  'h', "token",    CMDArgReq,  0, PR_FALSE },
 { /* opt_InputFile   */  'i', "infile",   CMDArgReq,  0, PR_FALSE },
 { /* opt_Nickname    */  'n', "nickname", CMDArgReq,  0, PR_FALSE },
 { /* opt_OutputFile  */  'o', "outfile",  CMDArgReq,  0, PR_FALSE },
 { /* opt_Binary      */  'r', "raw",      CMDNoArg,   0, PR_FALSE },
 { /* opt_Trust       */  't', "trust",    CMDArgReq,  0, PR_FALSE },
 { /* opt_Type        */   0 , "type",     CMDArgReq,  0, PR_FALSE }
};

void pkiutil_usage(cmdPrintState *ps, 
                   int num, PRBool cmd, PRBool header, PRBool footer)
{
#define pusg CMD_PrintUsageString
    if (header) {
	pusg(ps, "utility for managing PKCS#11 objects (certs and keys)\n");
    } else if (footer) {
	/*
	printf("certificate trust can be:\n");
	printf(" p - valid peer, P - trusted peer (implies p)\n");
	printf(" c - valid CA\n");
	printf("  T - trusted CA to issue client certs (implies c)\n");
	printf("  C - trusted CA to issue server certs (implies c)\n");
	printf(" u - user cert\n");
	printf(" w - send warning\n");
	*/
    } else if (cmd) {
	switch(num) {
	case cmd_Add:     
	    pusg(ps, "Add an object to the token"); break;
	case cmd_Dump:
	    pusg(ps, "Dump a single object"); break;
	case cmd_List:    
	    pusg(ps, "List objects on the token (-n for single object)"); break;
	case cmd_Version: 
	    pusg(ps, "Report version"); break;
	default:
	    pusg(ps, "Unrecognized command"); break;
	}
    } else {
	switch(num) {
	case opt_Ascii:      
	    pusg(ps, "Use ascii (base-64 encoded) mode for I/O"); break;
	case opt_ProfileDir:    
	    pusg(ps, "Directory containing security databases (def: \".\")"); 
	    break;
	case opt_TokenName:  
	    pusg(ps, "Name of PKCS#11 token to use (def: internal)"); break;
	case opt_InputFile:  
	    pusg(ps, "File for input (def: stdin)"); break;
	case opt_Nickname:   
	    pusg(ps, "Nickname of object"); break;
	case opt_OutputFile: 
	    pusg(ps, "File for output (def: stdout)"); break;
	case opt_Binary:    
	    pusg(ps, "Use raw (binary der-encoded) mode for I/O"); break;
	case opt_Trust:      
	    pusg(ps, "Trust level for certificate"); break;
	case opt_Help: break;
	default:
	    pusg(ps, "Unrecognized option");
	}
    }
}

int 
main(int argc, char **argv)
{
    PRFileDesc     *infile        = NULL;
    PRFileDesc     *outfile       = NULL;
    char           *profiledir       = "./";
#if 0
    secuPWData      pwdata        = { PW_NONE, 0 };
#endif
    int             objclass      = 3; /* ANY */
    NSSTrustDomain *root_cert_td  = NULL;
    char           *rootpath      = NULL;
    char            builtin_name[]= "libnssckbi.so"; /* temporary hardcode */
    PRStatus        rv            = PR_SUCCESS;

    int cmdToRun;
    cmdCommand pkiutil;
    pkiutil.ncmd = pkiutil_num_commands;
    pkiutil.nopt = pkiutil_num_options;
    pkiutil.cmd = pkiutil_commands;
    pkiutil.opt = pkiutil_options;

    progName = strrchr(argv[0], '/');
    progName = progName ? progName+1 : argv[0];

    cmdToRun = CMD_ParseCommandLine(argc, argv, progName, &pkiutil);

#if 0
    { int i, nc;
    for (i=0; i<pkiutil.ncmd; i++)
	printf("%s: %s <%s>\n", pkiutil.cmd[i].s, 
	                        (pkiutil.cmd[i].on) ? "on" : "off",
				pkiutil.cmd[i].arg);
    for (i=0; i<pkiutil.nopt; i++)
	printf("%s: %s <%s>\n", pkiutil.opt[i].s, 
	                        (pkiutil.opt[i].on) ? "on" : "off",
				pkiutil.opt[i].arg);
    }
#endif

    if (pkiutil.opt[opt_Help].on)
	CMD_LongUsage(progName, &pkiutil, pkiutil_usage);

    if (cmdToRun < 0)
	CMD_Usage(progName, &pkiutil);

    /* -d */
    if (pkiutil.opt[opt_ProfileDir].on) {
	profiledir = strdup(pkiutil.opt[opt_ProfileDir].arg);
    }

    /* -i */
    if (pkiutil.opt[opt_InputFile].on) {
	char *fn = pkiutil.opt[opt_InputFile].arg;
	infile = PR_Open(fn, PR_RDONLY, 0660);
    } else {
	infile = PR_STDIN;
    }

    /* -o */
    if (pkiutil.opt[opt_OutputFile].on) {
	char *fn = pkiutil.opt[opt_OutputFile].arg;
	outfile = PR_Open(fn, PR_WRONLY | PR_CREATE_FILE, 0660);
    } else {
	outfile = PR_STDOUT;
    }

    /* --type can be found on many options */
    if (pkiutil.opt[opt_Type].on)
	objclass = get_object_class(pkiutil.opt[opt_Type].arg);
    else if (cmdToRun == cmd_Dump && pkiutil.cmd[cmd_Dump].arg)
	objclass = get_object_class(pkiutil.cmd[cmd_Dump].arg);
    else if (cmdToRun == cmd_List && pkiutil.cmd[cmd_List].arg)
	objclass = get_object_class(pkiutil.cmd[cmd_List].arg);
    else if (cmdToRun == cmd_Add && pkiutil.cmd[cmd_Add].arg)
	objclass = get_object_class(pkiutil.cmd[cmd_Add].arg);
    if (objclass < 0)
	goto done;

    /* --print is an alias for --list --nickname */
    if (cmdToRun == cmd_Dump) cmdToRun = cmd_List;

    /* if list has raw | ascii must have -n.  can't have both raw and ascii */
    if (pkiutil.opt[opt_Binary].on || pkiutil.opt[opt_Ascii].on) {
	if (cmdToRun == cmd_List && !pkiutil.opt[opt_Nickname].on) {
	    fprintf(stderr, "%s: specify a object to output with -n\n", 
	                     progName);
	    CMD_LongUsage(progName, &pkiutil, pkiutil_usage);
	}
    }

    /* initialize */
    PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
    /* NSS_InitReadWrite(profiledir); */
    NSS_NoDB_Init(NULL);

    /* Display version info and exit */
    if (cmdToRun == cmd_Version) {
	printf("%s\nNSS Version %s\n", PKIUTIL_VERSION_STRING, NSS_VERSION);
	goto done;
    }

    /* XXX okay - bootstrap stan by loading the root cert module for testing */
    root_cert_td = NSSTrustDomain_Create(NULL, NULL, NULL, NULL);
    {
	int rootpathlen = strlen(profiledir) + strlen(builtin_name) + 1;
	rootpath = (char *)malloc(rootpathlen);
	memcpy(rootpath, profiledir, strlen(profiledir));
	memcpy(rootpath + strlen(profiledir), 
               builtin_name, strlen(builtin_name));
	rootpath[rootpathlen - 1] = '\0';
    }
    NSSTrustDomain_LoadModule(root_cert_td, "Builtin Root Module", rootpath, 
                              NULL, NULL);

    printf("\n");
    if (pkiutil.opt[opt_Nickname].on) {
	int i;
	NSSCertificate **certs;
	NSSCertificate *cert;
	certs = NSSTrustDomain_FindCertificatesByNickname(root_cert_td,
			pkiutil.opt[opt_Nickname].arg, NULL, 0, NULL);
	i = 0;
	while ((cert = certs[i++]) != NULL) {
	    printf("Found cert:\n");
	    print_cert_callback(cert, NULL);
	}
    } else {
        NSSTrustDomain_TraverseCertificates(root_cert_td, print_cert_callback, 0);
    }

    NSSTrustDomain_Destroy(root_cert_td);

    /* List token objects */
    if (cmdToRun == cmd_List)  {
#if 0
	rv = list_token_objects(slot, objclass, 
	                        pkiutil.opt[opt_Nickname].arg,
	                        pkiutil.opt[opt_Binary].on,
	                        pkiutil.opt[opt_Ascii].on,
	                        outfile, &pwdata);
#endif
	goto done;
    }

#if 0
    /* Import an object into the token. */
    if (cmdToRun == cmd_Add) {
	rv = add_object_to_token(slot, object);
	goto done;
    }
#endif

done:
    NSS_Shutdown();

    return rv;
}