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-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] */
56 #include <sys/types.h>
60 #define sleep(x) Sleep(x)
63 #ifndef FORCE_STATIC_PROVIDER
68 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
69 #define getpassphrase(x) getpass(x)
73 main(int argc
, char *argv
[])
77 CK_SESSION_HANDLE hSession
;
78 CK_UTF8CHAR
*pin
= NULL
;
80 CK_OBJECT_HANDLE akey
[50];
83 unsigned int id
= 0, i
= 0;
85 CK_ULONG ulObjectCount
;
86 CK_ATTRIBUTE search_template
[] = {
87 {CKA_ID
, &attr_id
, sizeof(attr_id
)}
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) {
101 pk11_libname
= optarg
;
114 pin
= (CK_UTF8CHAR
*)optarg
;
118 "Option -%c requires an operand\n",
124 fprintf(stderr
, "Unrecognised option: -%c\n", optopt
);
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");
137 printf("id %i\n", id
);
138 attr_id
[0] = (id
>> 8) & 0xff;
139 attr_id
[1] = id
& 0xff;
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
);
152 "Can't load or link module \"%s\"\n",
155 fprintf(stderr
, "C_Initialize: Error = 0x%.8lX\n", rv
);
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
);
163 fprintf(stderr
, "C_OpenSession: Error = 0x%.8lX\n", rv
);
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
));
175 fprintf(stderr
, "C_Login: Error = 0x%.8lX\n", rv
);
180 rv
= C_FindObjectsInit(hSession
, search_template
,
181 ((id
!= 0) || (label
!= NULL
)) ? 1 : 0);
184 fprintf(stderr
, "C_FindObjectsInit: Error = 0x%.8lX\n", rv
);
189 rv
= C_FindObjects(hSession
, akey
, 50, &ulObjectCount
);
191 fprintf(stderr
, "C_FindObjects: Error = 0x%.8lX\n", rv
);
196 for (i
= 0; i
< ulObjectCount
; i
++) {
197 CK_OBJECT_CLASS oclass
= 0;
198 CK_BYTE labelbuf
[64 + 1];
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);
212 "C_GetAttributeValue[%u]: rv = 0x%.8lX\n",
217 len
= attr_template
[2].ulValueLen
;
218 printf("object[%u]: class %lu label '%s' id[%lu] ",
219 i
, oclass
, labelbuf
, attr_template
[2].ulValueLen
);
224 for (j
= 0; j
< len
; j
++)
225 printf("%02x", idbuf
[j
]);
226 if (attr_template
[2].ulValueLen
> len
)
232 /* give a chance to kill this */
233 printf("sleeping 5 seconds...\n");
236 for (i
= 0; i
< ulObjectCount
; i
++) {
237 rv
= C_DestroyObject(hSession
, akey
[i
]);
240 "C_DestroyObject[%u]: rv = 0x%.8lX\n",
247 rv
= C_FindObjectsFinal(hSession
);
249 fprintf(stderr
, "C_FindObjectsFinal: Error = 0x%.8lX\n", rv
);
254 (void)C_CloseSession(hSession
);
257 (void)C_Finalize(NULL_PTR
);