1 /*	$NetBSD: pkcs11-tokens.c,v 1.1.1.4 2014/12/10 03:34:27 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id */
20 
21 /* pkcs11-tokens [-m module] */
22 
23 /*! \file */
24 
25 #include <config.h>
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <sys/types.h>
33 
34 #include <isc/commandline.h>
35 #include <isc/mem.h>
36 #include <isc/result.h>
37 #include <isc/types.h>
38 
39 #include <pk11/pk11.h>
40 #include <pk11/result.h>
41 
42 int
43 main(int argc, char *argv[]) {
44 	isc_result_t result;
45 	char *lib_name = NULL;
46 	int c, errflg = 0;
47 	isc_mem_t *mctx = NULL;
48 	pk11_context_t pctx;
49 
50 	while ((c = isc_commandline_parse(argc, argv, ":m:")) != -1) {
51 		switch (c) {
52 		case 'm':
53 			lib_name = isc_commandline_argument;
54 			break;
55 		case ':':
56 			fprintf(stderr, "Option -%c requires an operand\n",
57 				isc_commandline_option);
58 			errflg++;
59 			break;
60 		case '?':
61 		default:
62 			fprintf(stderr, "Unrecognised option: -%c\n",
63 				isc_commandline_option);
64 			errflg++;
65 		}
66 	}
67 
68 	if (errflg) {
69 		fprintf(stderr, "Usage:\n");
70 		fprintf(stderr, "\tpkcs11-tokens [-m module]\n");
71 		exit(1);
72 	}
73 
74 	if (isc_mem_create(0, 0, &mctx) != ISC_R_SUCCESS) {
75 		fprintf(stderr, "isc_mem_create() failed\n");
76 		exit(1);
77 	}
78 
79 	pk11_result_register();
80 
81 	/* Initialize the CRYPTOKI library */
82 	if (lib_name != NULL)
83 		pk11_set_lib_name(lib_name);
84 
85 	result = pk11_get_session(&pctx, OP_ANY, ISC_FALSE, ISC_FALSE,
86 				  ISC_FALSE, NULL, 0);
87 	if (result == PK11_R_NORANDOMSERVICE ||
88 	    result == PK11_R_NODIGESTSERVICE ||
89 	    result == PK11_R_NOAESSERVICE) {
90 		fprintf(stderr, "Warning: %s\n", isc_result_totext(result));
91 		fprintf(stderr, "This HSM will not work with BIND 9 "
92 				"using native PKCS#11.\n\n");
93 	} else if (result != ISC_R_SUCCESS) {
94 		fprintf(stderr, "Unrecoverable error initializing "
95 				"PKCS#11: %s\n", isc_result_totext(result));
96 		exit(1);
97 	}
98 
99 	pk11_dump_tokens();
100 
101 	if (pctx.handle != NULL)
102 		pk11_return_session(&pctx);
103 	(void) pk11_finalize();
104 
105 	isc_mem_destroy(&mctx);
106 
107 	exit(0);
108 }
109