1 /* $NetBSD: pkcs11-list.c,v 1.7 2014/12/10 04:37:52 christos Exp $ */
4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
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.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
13 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
16 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Portions copyright (c) 2008 Nominet UK. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 /* Id: pkcs11-list.c,v 1.7 2009/10/26 23:36:53 each Exp */
45 /* pkcs11-list [-P] [-m module] [-s slot] [-i $id | -l $label] [-p $pin] */
56 #include <sys/types.h>
58 #include <isc/commandline.h>
59 #include <isc/result.h>
60 #include <isc/types.h>
62 #include <pk11/pk11.h>
63 #include <pk11/result.h>
65 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
66 #define getpassphrase(x) getpass(x)
70 main(int argc
, char *argv
[]) {
74 CK_SESSION_HANDLE hSession
;
76 CK_OBJECT_HANDLE akey
[50];
78 char *lib_name
= NULL
;
81 isc_boolean_t error
= ISC_FALSE
, logon
= ISC_TRUE
, all
= ISC_FALSE
;
82 unsigned int i
= 0, id
= 0;
84 CK_ULONG ulObjectCount
;
85 CK_ATTRIBUTE search_template
[] = {
86 {CKA_ID
, &attr_id
, sizeof(attr_id
)}
89 while ((c
= isc_commandline_parse(argc
, argv
, ":m:s:i:l:p:P")) != -1) {
95 lib_name
= isc_commandline_argument
;
98 slot
= atoi(isc_commandline_argument
);
101 id
= atoi(isc_commandline_argument
);
105 label
= isc_commandline_argument
;
108 pin
= isc_commandline_argument
;
111 fprintf(stderr
, "Option -%c requires an operand\n",
112 isc_commandline_option
);
117 fprintf(stderr
, "Unrecognised option: -%c\n",
118 isc_commandline_option
);
124 fprintf(stderr
, "Usage:\n");
125 fprintf(stderr
, "\tpkcs11-list [-P] [-m module] [-s slot] "
126 "[-i id | -l label] [-p pin]\n");
130 if (!id
&& (label
== NULL
))
134 printf("slot %lu\n", slot
);
137 printf("id %i\n", id
);
138 attr_id
[0] = (id
>> 8) & 0xff;
139 attr_id
[1] = id
& 0xff;
140 } else if (label
!= NULL
) {
141 printf("label %s\n", label
);
142 search_template
[0].type
= CKA_LABEL
;
143 search_template
[0].pValue
= label
;
144 search_template
[0].ulValueLen
= strlen(label
);
147 pk11_result_register();
149 /* Initialize the CRYPTOKI library */
150 if (lib_name
!= NULL
)
151 pk11_set_lib_name(lib_name
);
153 if (logon
&& pin
== NULL
)
154 pin
= getpassphrase("Enter Pin: ");
156 result
= pk11_get_session(&pctx
, OP_ANY
, ISC_FALSE
, ISC_FALSE
,
158 if (result
== PK11_R_NORANDOMSERVICE
||
159 result
== PK11_R_NODIGESTSERVICE
||
160 result
== PK11_R_NOAESSERVICE
) {
161 fprintf(stderr
, "Warning: %s\n", isc_result_totext(result
));
162 fprintf(stderr
, "This HSM will not work with BIND 9 "
163 "using native PKCS#11.\n");
164 } else if (result
!= ISC_R_SUCCESS
) {
165 fprintf(stderr
, "Unrecoverable error initializing "
166 "PKCS#11: %s\n", isc_result_totext(result
));
167 fprintf(stderr
, "Unrecoverable error initializing "
168 "PKCS#11: %s\n", isc_result_totext(result
));
173 memset(pin
, 0, strlen(pin
));
175 hSession
= pctx
.session
;
177 rv
= pkcs_C_FindObjectsInit(hSession
, search_template
, all
? 0 : 1);
179 fprintf(stderr
, "C_FindObjectsInit: Error = 0x%.8lX\n", rv
);
185 while (ulObjectCount
) {
186 rv
= pkcs_C_FindObjects(hSession
, akey
, 50, &ulObjectCount
);
189 "C_FindObjects: Error = 0x%.8lX\n",
194 for (i
= 0; i
< ulObjectCount
; i
++) {
197 CK_OBJECT_CLASS oclass
= 0;
198 CK_BYTE labelbuf
[64 + 1];
200 CK_ATTRIBUTE
template[] = {
201 {CKA_CLASS
, &oclass
, sizeof(oclass
)},
202 {CKA_LABEL
, labelbuf
, sizeof(labelbuf
) - 1},
203 {CKA_ID
, idbuf
, sizeof(idbuf
)}
206 memset(labelbuf
, 0, sizeof(labelbuf
));
207 memset(idbuf
, 0, sizeof(idbuf
));
209 rv
= pkcs_C_GetAttributeValue(hSession
, akey
[i
],
213 "C_GetAttributeValue[%u]: "
216 if (rv
== CKR_BUFFER_TOO_SMALL
)
218 "%u too small: %lu %lu %lu\n",
220 template[0].ulValueLen
,
221 template[1].ulValueLen
,
222 template[2].ulValueLen
);
227 len
= template[2].ulValueLen
;
228 printf("object[%u]: handle %lu class %lu "
229 "label[%lu] '%s' id[%lu] ",
231 template[1].ulValueLen
,
233 template[2].ulValueLen
);
235 id
= (idbuf
[0] << 8) & 0xff00;
236 id
|= idbuf
[1] & 0xff;
243 for (j
= 0; j
< len
; j
++)
244 printf("%02x", idbuf
[j
]);
245 if (template[2].ulValueLen
> len
)
254 rv
= pkcs_C_FindObjectsFinal(hSession
);
256 fprintf(stderr
, "C_FindObjectsFinal: Error = 0x%.8lX\n", rv
);
261 pk11_return_session(&pctx
);
262 (void) pk11_finalize();