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>
62 #ifndef FORCE_STATIC_PROVIDER
67 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
68 #define getpassphrase(x) getpass(x)
72 main(int argc
, char *argv
[])
76 CK_SESSION_HANDLE hSession
;
77 CK_UTF8CHAR
*pin
= NULL
;
79 CK_OBJECT_HANDLE akey
[50];
81 int error
= 0, public = 0, all
= 0;
82 unsigned int i
= 0, id
= 0;
84 CK_ULONG ulObjectCount
;
85 CK_ATTRIBUTE search_template
[] = {
86 {CKA_ID
, &attr_id
, sizeof(attr_id
)}
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) {
102 pk11_libname
= optarg
;
115 pin
= (CK_UTF8CHAR
*)optarg
;
118 fprintf(stderr
, "Option -%c requires an operand\n",
124 fprintf(stderr
, "Unrecognised option: -%c\n", optopt
);
130 fprintf(stderr
, "Usage:\n");
131 fprintf(stderr
, "\tpkcs11-list [-P] [-m module] [-s slot] "
132 "[-i id | -l label] [-p pin]\n");
136 if (!id
&& (label
== NULL
))
140 printf("slot %lu\n", slot
);
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
);
158 "Can't load or link module \"%s\"\n",
161 fprintf(stderr
, "C_Initialize: Error = 0x%.8lX\n", rv
);
165 /* Open a session on the slot found */
166 rv
= C_OpenSession(slot
, CKF_SERIAL_SESSION
,
167 NULL_PTR
, NULL_PTR
, &hSession
);
169 fprintf(stderr
, "C_OpenSession: Error = 0x%.8lX\n", rv
);
174 /* Login to the Token (Keystore) */
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
));
181 fprintf(stderr
, "C_Login: Error = 0x%.8lX\n", rv
);
187 rv
= C_FindObjectsInit(hSession
, search_template
, all
? 0 : 1);
189 fprintf(stderr
, "C_FindObjectsInit: Error = 0x%.8lX\n", rv
);
195 while (ulObjectCount
) {
196 rv
= C_FindObjects(hSession
, akey
, 50, &ulObjectCount
);
199 "C_FindObjects: Error = 0x%.8lX\n",
205 for (i
= 0; i
< ulObjectCount
; i
++) {
208 CK_OBJECT_CLASS oclass
= 0;
209 CK_BYTE labelbuf
[64 + 1];
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
],
224 "C_GetAttributeValue[%u]: "
227 if (rv
== CKR_BUFFER_TOO_SMALL
)
229 "%u too small: %lu %lu %lu\n",
231 template[0].ulValueLen
,
232 template[1].ulValueLen
,
233 template[2].ulValueLen
);
238 len
= template[2].ulValueLen
;
239 printf("object[%u]: handle %lu class %lu "
240 "label[%lu] '%s' id[%lu] ",
242 template[1].ulValueLen
,
244 template[2].ulValueLen
);
246 id
= (idbuf
[0] << 8) & 0xff00;
247 id
|= idbuf
[1] & 0xff;
254 for (j
= 0; j
< len
; j
++)
255 printf("%02x", idbuf
[j
]);
256 if (template[2].ulValueLen
> len
)
265 rv
= C_FindObjectsFinal(hSession
);
267 fprintf(stderr
, "C_FindObjectsFinal: Error = 0x%.8lX\n", rv
);
272 (void)C_CloseSession(hSession
);
275 (void)C_Finalize(NULL_PTR
);