Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / pkcs11 / pkcs11-list.c
blob9bf4ad5e463818c3c935b6f5158c5827ac943107
1 /* $NetBSD$ */
3 /*
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
24 * are met:
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] */
47 /*! \file */
49 #include <config.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <fcntl.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <sys/types.h>
57 #include "cryptoki.h"
59 #ifdef WIN32
60 #include "win32.c"
61 #else
62 #ifndef FORCE_STATIC_PROVIDER
63 #include "unix.c"
64 #endif
65 #endif
67 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
68 #define getpassphrase(x) getpass(x)
69 #endif
71 int
72 main(int argc, char *argv[])
74 CK_RV rv;
75 CK_SLOT_ID slot = 0;
76 CK_SESSION_HANDLE hSession;
77 CK_UTF8CHAR *pin = NULL;
78 CK_BYTE attr_id[2];
79 CK_OBJECT_HANDLE akey[50];
80 char *label = NULL;
81 int error = 0, public = 0, all = 0;
82 unsigned int i = 0, id = 0;
83 int c, errflg = 0;
84 CK_ULONG ulObjectCount;
85 CK_ATTRIBUTE search_template[] = {
86 {CKA_ID, &attr_id, sizeof(attr_id)}
88 char *pk11_provider;
89 extern char *optarg;
90 extern int optopt;
92 pk11_provider = getenv("PKCS11_PROVIDER");
93 if (pk11_provider != NULL)
94 pk11_libname = pk11_provider;
96 while ((c = getopt(argc, argv, ":m:s:i:l:p:P")) != -1) {
97 switch (c) {
98 case 'P':
99 public = 1;
100 break;
101 case 'm':
102 pk11_libname = optarg;
103 break;
104 case 's':
105 slot = atoi(optarg);
106 break;
107 case 'i':
108 id = atoi(optarg);
109 id &= 0xffff;
110 break;
111 case 'l':
112 label = optarg;
113 break;
114 case 'p':
115 pin = (CK_UTF8CHAR *)optarg;
116 break;
117 case ':':
118 fprintf(stderr, "Option -%c requires an operand\n",
119 optopt);
120 errflg++;
121 break;
122 case '?':
123 default:
124 fprintf(stderr, "Unrecognised option: -%c\n", optopt);
125 errflg++;
129 if (errflg) {
130 fprintf(stderr, "Usage:\n");
131 fprintf(stderr, "\tpkcs11-list [-P] [-m module] [-s slot] "
132 "[-i id | -l label] [-p pin]\n");
133 exit(1);
136 if (!id && (label == NULL))
137 all = 1;
139 if (slot)
140 printf("slot %lu\n", slot);
142 if (id) {
143 printf("id %i\n", id);
144 attr_id[0] = (id >> 8) & 0xff;
145 attr_id[1] = id & 0xff;
146 } else if (label != NULL) {
147 printf("label %s\n", label);
148 search_template[0].type = CKA_LABEL;
149 search_template[0].pValue = label;
150 search_template[0].ulValueLen = strlen(label);
153 /* Initialize the CRYPTOKI library */
154 rv = C_Initialize(NULL_PTR);
155 if (rv != CKR_OK) {
156 if (rv == 0xfe)
157 fprintf(stderr,
158 "Can't load or link module \"%s\"\n",
159 pk11_libname);
160 else
161 fprintf(stderr, "C_Initialize: Error = 0x%.8lX\n", rv);
162 exit(1);
165 /* Open a session on the slot found */
166 rv = C_OpenSession(slot, CKF_SERIAL_SESSION,
167 NULL_PTR, NULL_PTR, &hSession);
168 if (rv != CKR_OK) {
169 fprintf(stderr, "C_OpenSession: Error = 0x%.8lX\n", rv);
170 error = 1;
171 goto exit_program;
174 /* Login to the Token (Keystore) */
175 if (!public) {
176 if (pin == NULL)
177 pin = (CK_UTF8CHAR *)getpassphrase("Enter Pin: ");
178 rv = C_Login(hSession, CKU_USER, pin, strlen((char *)pin));
179 memset(pin, 0, strlen((char *)pin));
180 if (rv != CKR_OK) {
181 fprintf(stderr, "C_Login: Error = 0x%.8lX\n", rv);
182 error = 1;
183 goto exit_session;
187 rv = C_FindObjectsInit(hSession, search_template, all ? 0 : 1);
188 if (rv != CKR_OK) {
189 fprintf(stderr, "C_FindObjectsInit: Error = 0x%.8lX\n", rv);
190 error = 1;
191 goto exit_session;
194 ulObjectCount = 1;
195 while (ulObjectCount) {
196 rv = C_FindObjects(hSession, akey, 50, &ulObjectCount);
197 if (rv != CKR_OK) {
198 fprintf(stderr,
199 "C_FindObjects: Error = 0x%.8lX\n",
200 rv);
201 error = 1;
202 goto exit_search;
205 for (i = 0; i < ulObjectCount; i++) {
206 unsigned int j, len;
208 CK_OBJECT_CLASS oclass = 0;
209 CK_BYTE labelbuf[64 + 1];
210 CK_BYTE idbuf[64];
211 CK_ATTRIBUTE template[] = {
212 {CKA_CLASS, &oclass, sizeof(oclass)},
213 {CKA_LABEL, labelbuf, sizeof(labelbuf) - 1},
214 {CKA_ID, idbuf, sizeof(idbuf)}
217 memset(labelbuf, 0, sizeof(labelbuf));
218 memset(idbuf, 0, sizeof(idbuf));
220 rv = C_GetAttributeValue(hSession, akey[i],
221 template, 3);
222 if (rv != CKR_OK) {
223 fprintf(stderr,
224 "C_GetAttributeValue[%u]: "
225 "rv = 0x%.8lX\n",
226 i, rv);
227 if (rv == CKR_BUFFER_TOO_SMALL)
228 fprintf(stderr,
229 "%u too small: %lu %lu %lu\n",
231 template[0].ulValueLen,
232 template[1].ulValueLen,
233 template[2].ulValueLen);
234 error = 1;
235 continue;
238 len = template[2].ulValueLen;
239 printf("object[%u]: handle %lu class %lu "
240 "label[%lu] '%s' id[%lu] ",
241 i, akey[i], oclass,
242 template[1].ulValueLen,
243 labelbuf,
244 template[2].ulValueLen);
245 if (len == 2) {
246 id = (idbuf[0] << 8) & 0xff00;
247 id |= idbuf[1] & 0xff;
248 printf("%u\n", id);
249 } else {
250 if (len > 8)
251 len = 8;
252 if (len > 0)
253 printf("0x");
254 for (j = 0; j < len; j++)
255 printf("%02x", idbuf[j]);
256 if (template[2].ulValueLen > len)
257 printf("...\n");
258 else
259 printf("\n");
264 exit_search:
265 rv = C_FindObjectsFinal(hSession);
266 if (rv != CKR_OK) {
267 fprintf(stderr, "C_FindObjectsFinal: Error = 0x%.8lX\n", rv);
268 error = 1;
271 exit_session:
272 (void)C_CloseSession(hSession);
274 exit_program:
275 (void)C_Finalize(NULL_PTR);
277 exit(error);