Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / pkcs11 / pkcs11-destroy.c
blob0e961bfaf0585e84acb81fbfdc5e5cd72228a971
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-destroy.c,v 1.7 2009/10/26 23:36:53 each Exp */
45 /* pkcs11-destroy [-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 #define sleep(x) Sleep(x)
61 #include "win32.c"
62 #else
63 #ifndef FORCE_STATIC_PROVIDER
64 #include "unix.c"
65 #endif
66 #endif
68 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
69 #define getpassphrase(x) getpass(x)
70 #endif
72 int
73 main(int argc, char *argv[])
75 CK_RV rv;
76 CK_SLOT_ID slot = 0;
77 CK_SESSION_HANDLE hSession;
78 CK_UTF8CHAR *pin = NULL;
79 CK_BYTE attr_id[2];
80 CK_OBJECT_HANDLE akey[50];
81 char *label = NULL;
82 int error = 0;
83 unsigned int id = 0, i = 0;
84 int c, errflg = 0;
85 CK_ULONG ulObjectCount;
86 CK_ATTRIBUTE search_template[] = {
87 {CKA_ID, &attr_id, sizeof(attr_id)}
89 char *pk11_provider;
90 unsigned int j, len;
91 extern char *optarg;
92 extern int optopt;
94 pk11_provider = getenv("PKCS11_PROVIDER");
95 if (pk11_provider != NULL)
96 pk11_libname = pk11_provider;
98 while ((c = getopt(argc, argv, ":m:s:i:l:p:")) != -1) {
99 switch (c) {
100 case 'm':
101 pk11_libname = optarg;
102 break;
103 case 's':
104 slot = atoi(optarg);
105 break;
106 case 'i':
107 id = atoi(optarg);
108 id &= 0xffff;
109 break;
110 case 'l':
111 label = optarg;
112 break;
113 case 'p':
114 pin = (CK_UTF8CHAR *)optarg;
115 break;
116 case ':':
117 fprintf(stderr,
118 "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 || (!id && (label != NULL))) {
130 fprintf(stderr, "Usage:\n");
131 fprintf(stderr, "\tpkcs11-destroy [-m module] [-s slot] "
132 "[-i id | -l label] [-p pin]\n");
133 exit(1);
136 if (id) {
137 printf("id %i\n", id);
138 attr_id[0] = (id >> 8) & 0xff;
139 attr_id[1] = id & 0xff;
140 } else if (label) {
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 /* Initialize the CRYPTOKI library */
148 rv = C_Initialize(NULL_PTR);
149 if (rv != CKR_OK) {
150 if (rv == 0xfe)
151 fprintf(stderr,
152 "Can't load or link module \"%s\"\n",
153 pk11_libname);
154 else
155 fprintf(stderr, "C_Initialize: Error = 0x%.8lX\n", rv);
156 exit(1);
159 /* Open a session on the slot found */
160 rv = C_OpenSession(slot, CKF_RW_SESSION+CKF_SERIAL_SESSION,
161 NULL_PTR, NULL_PTR, &hSession);
162 if (rv != CKR_OK) {
163 fprintf(stderr, "C_OpenSession: Error = 0x%.8lX\n", rv);
164 error = 1;
165 goto exit_program;
168 if (pin == NULL)
169 pin = (CK_UTF8CHAR *)getpassphrase("Enter Pin: ");
171 /* Login to the Token (Keystore) */
172 rv = C_Login(hSession, CKU_USER, pin, strlen((char *)pin));
173 memset(pin, 0, strlen((char *)pin));
174 if (rv != CKR_OK) {
175 fprintf(stderr, "C_Login: Error = 0x%.8lX\n", rv);
176 error = 1;
177 goto exit_session;
180 rv = C_FindObjectsInit(hSession, search_template,
181 ((id != 0) || (label != NULL)) ? 1 : 0);
183 if (rv != CKR_OK) {
184 fprintf(stderr, "C_FindObjectsInit: Error = 0x%.8lX\n", rv);
185 error = 1;
186 goto exit_session;
189 rv = C_FindObjects(hSession, akey, 50, &ulObjectCount);
190 if (rv != CKR_OK) {
191 fprintf(stderr, "C_FindObjects: Error = 0x%.8lX\n", rv);
192 error = 1;
193 goto exit_search;
196 for (i = 0; i < ulObjectCount; i++) {
197 CK_OBJECT_CLASS oclass = 0;
198 CK_BYTE labelbuf[64 + 1];
199 CK_BYTE idbuf[64];
200 CK_ATTRIBUTE attr_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 = C_GetAttributeValue(hSession, akey[i], attr_template, 3);
210 if (rv != CKR_OK) {
211 fprintf(stderr,
212 "C_GetAttributeValue[%u]: rv = 0x%.8lX\n",
213 i, rv);
214 error = 1;
215 goto exit_search;
217 len = attr_template[2].ulValueLen;
218 printf("object[%u]: class %lu label '%s' id[%lu] ",
219 i, oclass, labelbuf, attr_template[2].ulValueLen);
220 if (len > 4)
221 len = 4;
222 if (len > 0)
223 printf("0x");
224 for (j = 0; j < len; j++)
225 printf("%02x", idbuf[j]);
226 if (attr_template[2].ulValueLen > len)
227 printf("...\n");
228 else
229 printf("\n");
232 /* give a chance to kill this */
233 printf("sleeping 5 seconds...\n");
234 sleep(5);
236 for (i = 0; i < ulObjectCount; i++) {
237 rv = C_DestroyObject(hSession, akey[i]);
238 if (rv != CKR_OK) {
239 fprintf(stderr,
240 "C_DestroyObject[%u]: rv = 0x%.8lX\n",
241 i, rv);
242 error = 1;
246 exit_search:
247 rv = C_FindObjectsFinal(hSession);
248 if (rv != CKR_OK) {
249 fprintf(stderr, "C_FindObjectsFinal: Error = 0x%.8lX\n", rv);
250 error = 1;
253 exit_session:
254 (void)C_CloseSession(hSession);
256 exit_program:
257 (void)C_Finalize(NULL_PTR);
259 exit(error);