4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2012 Milan Jurik. All rights reserved.
27 * This file implements the token object list operation for this tool.
28 * It loads the PKCS#11 modules, finds the object to list, lists it,
29 * and cleans up. User must be logged into the token to list private
36 #include <cryptoutil.h>
37 #include <security/cryptoki.h>
43 pk_show_certs(KMF_HANDLE_T kmfhandle
, KMF_X509_DER_CERT
*certs
, int num_certs
)
46 char *subject
, *issuer
, *serial
, *id
, *altname
;
47 char *start
, *end
, *keyusage
, *extkeyusage
;
49 for (i
= 0; i
< num_certs
; i
++) {
56 keyusage
= extkeyusage
= NULL
;
58 (void) fprintf(stdout
,
59 gettext("%d. (X.509 certificate)\n"), i
+ 1);
60 if (certs
[i
].kmf_private
.label
!= NULL
)
61 (void) fprintf(stdout
, gettext("\t%s: %s\n"),
62 (certs
[i
].kmf_private
.keystore_type
==
63 KMF_KEYSTORE_OPENSSL
? "Filename" : "Label"),
64 certs
[i
].kmf_private
.label
);
65 if (kmf_get_cert_id_str(&certs
[i
].certificate
,
67 (void) fprintf(stdout
, gettext("\tID: %s\n"), id
);
68 if (kmf_get_cert_subject_str(kmfhandle
,
69 &certs
[i
].certificate
, &subject
) == KMF_OK
)
70 (void) fprintf(stdout
, gettext("\tSubject: %s\n"),
72 if (kmf_get_cert_issuer_str(kmfhandle
,
73 &certs
[i
].certificate
, &issuer
) == KMF_OK
)
74 (void) fprintf(stdout
, gettext("\tIssuer: %s\n"),
76 if (kmf_get_cert_start_date_str(kmfhandle
,
77 &certs
[i
].certificate
, &start
) == KMF_OK
)
78 (void) fprintf(stdout
, gettext("\tNot Before: %s\n"),
80 if (kmf_get_cert_end_date_str(kmfhandle
,
81 &certs
[i
].certificate
, &end
) == KMF_OK
)
82 (void) fprintf(stdout
, gettext("\tNot After: %s\n"),
84 if (kmf_get_cert_serial_str(kmfhandle
,
85 &certs
[i
].certificate
, &serial
) == KMF_OK
)
86 (void) fprintf(stdout
, gettext("\tSerial: %s\n"),
88 if (kmf_get_cert_extn_str(kmfhandle
,
89 &certs
[i
].certificate
, KMF_X509_EXT_SUBJ_ALTNAME
,
90 &altname
) == KMF_OK
) {
91 (void) fprintf(stdout
, gettext("\t%s\n"),
94 if (kmf_get_cert_extn_str(kmfhandle
,
95 &certs
[i
].certificate
, KMF_X509_EXT_KEY_USAGE
,
96 &keyusage
) == KMF_OK
) {
97 (void) fprintf(stdout
, gettext("\t%s\n"),
100 if (kmf_get_cert_extn_str(kmfhandle
,
101 &certs
[i
].certificate
, KMF_X509_EXT_EXT_KEY_USAGE
,
102 &extkeyusage
) == KMF_OK
) {
103 (void) fprintf(stdout
, gettext("\t%s\n"),
106 kmf_free_str(subject
);
107 kmf_free_str(issuer
);
108 kmf_free_str(serial
);
110 kmf_free_str(altname
);
111 kmf_free_str(keyusage
);
112 kmf_free_str(extkeyusage
);
115 (void) fprintf(stdout
, "\n");
120 describeKey(KMF_KEY_HANDLE
*key
)
122 if (key
->keyclass
== KMF_ASYM_PUB
) {
123 if (key
->keyalg
== KMF_RSA
)
124 return (gettext("RSA public key"));
125 if (key
->keyalg
== KMF_DSA
)
126 return (gettext("DSA public key"));
127 if (key
->keyalg
== KMF_ECDSA
)
128 return (gettext("ECDSA public key"));
130 if (key
->keyclass
== KMF_ASYM_PRI
) {
131 if (key
->keyalg
== KMF_RSA
)
132 return (gettext("RSA private key"));
133 if (key
->keyalg
== KMF_DSA
)
134 return (gettext("DSA private key"));
135 if (key
->keyalg
== KMF_ECDSA
)
136 return (gettext("ECDSA private key"));
138 if (key
->keyclass
== KMF_SYMMETRIC
) {
139 switch (key
->keyalg
) {
141 return (gettext("AES"));
143 return (gettext("ARCFOUR"));
145 return (gettext("DES"));
147 return (gettext("Triple-DES"));
149 return (gettext("symmetric"));
153 return (gettext("unrecognized key object"));
159 pk_show_keys(void *handle
, KMF_KEY_HANDLE
*keys
, int numkeys
)
163 for (i
= 0; i
< numkeys
; i
++) {
164 (void) fprintf(stdout
, gettext("Key #%d - %s: %s"),
165 i
+1, describeKey(&keys
[i
]),
166 keys
[i
].keylabel
? keys
[i
].keylabel
:
167 gettext("No label"));
169 if (keys
[i
].keyclass
== KMF_SYMMETRIC
) {
171 KMF_RAW_SYM_KEY rkey
;
173 (void) memset(&rkey
, 0, sizeof (rkey
));
174 rv
= kmf_get_sym_key_value(handle
, &keys
[i
],
177 (void) fprintf(stdout
, " (%d bits)",
178 rkey
.keydata
.len
* 8);
179 kmf_free_bigint(&rkey
.keydata
);
180 } else if (keys
[i
].kstype
== KMF_KEYSTORE_PK11TOKEN
) {
181 if (rv
== KMF_ERR_SENSITIVE_KEY
) {
182 (void) fprintf(stdout
, " (sensitive)");
183 } else if (rv
== KMF_ERR_UNEXTRACTABLE_KEY
) {
184 (void) fprintf(stdout
,
185 " (non-extractable)");
188 if (kmf_get_kmf_error_str(rv
, &err
) ==
190 (void) fprintf(stdout
,
191 " (error: %s)", err
);
197 (void) fprintf(stdout
, "\n");
202 * Generic routine used by all "list cert" operations to find
203 * all matching certificates.
206 pk_find_certs(KMF_HANDLE_T kmfhandle
, KMF_ATTRIBUTE
*attrlist
, int numattr
)
208 KMF_RETURN rv
= KMF_OK
;
209 KMF_X509_DER_CERT
*certlist
= NULL
;
210 uint32_t numcerts
= 0;
211 KMF_KEYSTORE_TYPE kstype
;
213 rv
= kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR
, attrlist
, numattr
,
218 kmf_set_attr_at_index(attrlist
, numattr
, KMF_COUNT_ATTR
,
219 &numcerts
, sizeof (uint32_t));
222 rv
= kmf_find_cert(kmfhandle
, numattr
, attrlist
);
223 if (rv
== KMF_OK
&& numcerts
> 0) {
224 (void) printf(gettext("Found %d certificates.\n"),
226 certlist
= (KMF_X509_DER_CERT
*)malloc(numcerts
*
227 sizeof (KMF_X509_DER_CERT
));
228 if (certlist
== NULL
)
229 return (KMF_ERR_MEMORY
);
230 (void) memset(certlist
, 0, numcerts
*
231 sizeof (KMF_X509_DER_CERT
));
233 kmf_set_attr_at_index(attrlist
, numattr
,
234 KMF_X509_DER_CERT_ATTR
, certlist
,
235 sizeof (KMF_X509_DER_CERT
));
238 rv
= kmf_find_cert(kmfhandle
, numattr
, attrlist
);
241 (void) pk_show_certs(kmfhandle
, certlist
,
243 for (i
= 0; i
< numcerts
; i
++)
244 kmf_free_kmf_cert(kmfhandle
, &certlist
[i
]);
248 if (rv
== KMF_ERR_CERT_NOT_FOUND
&&
249 kstype
!= KMF_KEYSTORE_OPENSSL
)
256 pk_list_keys(void *handle
, KMF_ATTRIBUTE
*attrlist
, int numattr
, char *label
)
259 KMF_KEY_HANDLE
*keys
;
260 uint32_t numkeys
= 0;
261 KMF_KEYSTORE_TYPE kstype
;
263 rv
= kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR
, attrlist
, numattr
,
268 kmf_set_attr_at_index(attrlist
, numattr
, KMF_COUNT_ATTR
,
269 &numkeys
, sizeof (uint32_t));
272 rv
= kmf_find_key(handle
, numattr
, attrlist
);
273 if (rv
== KMF_OK
&& numkeys
> 0) {
275 (void) printf(gettext("Found %d %s keys.\n"),
277 keys
= (KMF_KEY_HANDLE
*)malloc(numkeys
*
278 sizeof (KMF_KEY_HANDLE
));
280 return (KMF_ERR_MEMORY
);
281 (void) memset(keys
, 0, numkeys
*
282 sizeof (KMF_KEY_HANDLE
));
284 kmf_set_attr_at_index(attrlist
, numattr
,
286 keys
, sizeof (KMF_KEY_HANDLE
));
289 rv
= kmf_find_key(handle
, numattr
, attrlist
);
291 pk_show_keys(handle
, keys
, numkeys
);
292 for (i
= 0; i
< numkeys
; i
++)
293 kmf_free_kmf_key(handle
, &keys
[i
]);
296 if (rv
== KMF_ERR_KEY_NOT_FOUND
&&
297 kstype
!= KMF_KEYSTORE_OPENSSL
)
303 list_pk11_objects(KMF_HANDLE_T kmfhandle
, char *token
, int oclass
,
304 char *objlabel
, KMF_BIGINT
*serial
, char *issuer
, char *subject
,
305 char *dir
, char *filename
, KMF_CREDENTIAL
*tokencred
,
306 KMF_CERT_VALIDITY find_criteria_flag
)
309 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_PK11TOKEN
;
311 KMF_ATTRIBUTE attrlist
[18];
312 boolean_t token_bool
= B_TRUE
;
313 boolean_t
private = B_FALSE
;
314 KMF_KEY_CLASS keyclass
;
315 KMF_ENCODE_FORMAT format
;
317 KMF_CREDENTIAL cred
= { NULL
, 0 };
320 * Symmetric keys and RSA/DSA/ECDSA private keys are always
321 * created with the "CKA_PRIVATE" field == TRUE, so
322 * make sure we search for them with it also set.
324 if (oclass
& (PK_SYMKEY_OBJ
| PK_PRIKEY_OBJ
))
325 oclass
|= PK_PRIVATE_OBJ
;
327 rv
= select_token(kmfhandle
, token
,
328 !(oclass
& (PK_PRIVATE_OBJ
| PK_PRIKEY_OBJ
)));
334 rv
= token_auth_needed(kmfhandle
, token
, &auth
);
338 if (tokencred
!= NULL
)
341 if (oclass
& (PK_KEY_OBJ
| PK_PRIVATE_OBJ
)) {
342 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
343 &kstype
, sizeof (kstype
));
346 if (objlabel
!= NULL
) {
347 kmf_set_attr_at_index(attrlist
, numattr
,
348 KMF_KEYLABEL_ATTR
, objlabel
,
353 private = ((oclass
& PK_PRIVATE_OBJ
) > 0);
355 kmf_set_attr_at_index(attrlist
, numattr
,
356 KMF_PRIVATE_BOOL_ATTR
, &private,
360 kmf_set_attr_at_index(attrlist
, numattr
,
361 KMF_TOKEN_BOOL_ATTR
, &token_bool
,
362 sizeof (token_bool
));
365 if (oclass
& PK_PRIKEY_OBJ
) {
368 keyclass
= KMF_ASYM_PRI
;
369 kmf_set_attr_at_index(attrlist
, num
,
370 KMF_KEYCLASS_ATTR
, &keyclass
,
374 if (tokencred
!= NULL
&&
375 tokencred
->credlen
> 0) {
376 kmf_set_attr_at_index(attrlist
, num
,
377 KMF_CREDENTIAL_ATTR
, tokencred
,
378 sizeof (KMF_CREDENTIAL
));
382 /* list asymmetric private keys */
383 rv
= pk_list_keys(kmfhandle
, attrlist
, num
,
384 "asymmetric private");
387 if (rv
== KMF_OK
&& (oclass
& PK_SYMKEY_OBJ
)) {
390 keyclass
= KMF_SYMMETRIC
;
391 kmf_set_attr_at_index(attrlist
, num
,
392 KMF_KEYCLASS_ATTR
, &keyclass
,
396 if (tokencred
!= NULL
&&
397 tokencred
->credlen
> 0) {
398 kmf_set_attr_at_index(attrlist
, num
,
399 KMF_CREDENTIAL_ATTR
, tokencred
,
400 sizeof (KMF_CREDENTIAL
));
404 format
= KMF_FORMAT_RAWKEY
;
405 kmf_set_attr_at_index(attrlist
, num
,
406 KMF_ENCODE_FORMAT_ATTR
, &format
,
410 /* list symmetric keys */
411 rv
= pk_list_keys(kmfhandle
, attrlist
, num
,
415 if (rv
== KMF_OK
&& (oclass
& PK_PUBKEY_OBJ
)) {
418 if (auth
> 0 && (tokencred
== NULL
||
419 tokencred
->cred
== NULL
) &&
420 (cred
.cred
== NULL
)) {
421 (void) get_token_password(kstype
, token
, &cred
);
422 kmf_set_attr_at_index(attrlist
, num
,
424 &cred
, sizeof (KMF_CREDENTIAL
));
429 keyclass
= KMF_ASYM_PUB
;
430 kmf_set_attr_at_index(attrlist
, num
,
431 KMF_KEYCLASS_ATTR
, &keyclass
,
435 /* list asymmetric public keys (if any) */
436 rv
= pk_list_keys(kmfhandle
, attrlist
, num
,
437 "asymmetric public");
445 if (oclass
& (PK_CERT_OBJ
| PK_PUBLIC_OBJ
)) {
446 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
447 &kstype
, sizeof (kstype
));
450 if (auth
> 0 && (cred
.cred
== NULL
)) {
451 (void) get_token_password(kstype
, token
, &cred
);
454 if (cred
.cred
!= NULL
) {
455 kmf_set_attr_at_index(attrlist
, numattr
,
457 &cred
, sizeof (KMF_CREDENTIAL
));
461 if (objlabel
!= NULL
) {
462 kmf_set_attr_at_index(attrlist
, numattr
,
463 KMF_CERT_LABEL_ATTR
, objlabel
,
468 if (issuer
!= NULL
) {
469 kmf_set_attr_at_index(attrlist
, numattr
,
470 KMF_ISSUER_NAME_ATTR
, issuer
,
475 if (subject
!= NULL
) {
476 kmf_set_attr_at_index(attrlist
, numattr
,
477 KMF_SUBJECT_NAME_ATTR
, subject
,
482 if (serial
!= NULL
&& serial
->val
!= NULL
) {
483 kmf_set_attr_at_index(attrlist
, numattr
,
484 KMF_BIGINT_ATTR
, serial
,
485 sizeof (KMF_BIGINT
));
489 kmf_set_attr_at_index(attrlist
, numattr
,
490 KMF_PRIVATE_BOOL_ATTR
, &private,
494 kmf_set_attr_at_index(attrlist
, numattr
,
495 KMF_CERT_VALIDITY_ATTR
, &find_criteria_flag
,
496 sizeof (KMF_CERT_VALIDITY
));
499 rv
= pk_find_certs(kmfhandle
, attrlist
, numattr
);
505 kstype
= KMF_KEYSTORE_OPENSSL
; /* CRL is file-based */
506 if (oclass
& PK_CRL_OBJ
) {
507 char *crldata
= NULL
;
509 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
510 &kstype
, sizeof (kstype
));
514 kmf_set_attr_at_index(attrlist
, numattr
,
515 KMF_DIRPATH_ATTR
, dir
, strlen(dir
));
518 if (filename
!= NULL
) {
519 kmf_set_attr_at_index(attrlist
, numattr
,
520 KMF_CRL_FILENAME_ATTR
,
521 filename
, strlen(filename
));
524 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CRL_DATA_ATTR
,
525 &crldata
, sizeof (char *));
528 rv
= kmf_list_crl(kmfhandle
, numattr
, attrlist
);
529 if (rv
== KMF_OK
&& crldata
!= NULL
) {
530 (void) printf("%s\n", crldata
);
539 list_file_objects(KMF_HANDLE_T kmfhandle
, int oclass
,
540 char *dir
, char *filename
, KMF_BIGINT
*serial
,
541 char *issuer
, char *subject
,
542 KMF_CERT_VALIDITY find_criteria_flag
)
544 KMF_RETURN rv
= KMF_OK
;
545 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_OPENSSL
;
547 KMF_ATTRIBUTE attrlist
[16];
548 KMF_KEY_CLASS keyclass
;
549 KMF_ENCODE_FORMAT format
;
550 char *defaultdir
= ".";
552 if (oclass
& PK_KEY_OBJ
) {
553 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
554 &kstype
, sizeof (kstype
));
557 if (dir
== NULL
&& filename
== NULL
)
561 kmf_set_attr_at_index(attrlist
, numattr
,
562 KMF_DIRPATH_ATTR
, dir
,
567 if (filename
!= NULL
) {
568 kmf_set_attr_at_index(attrlist
, numattr
,
569 KMF_KEY_FILENAME_ATTR
, filename
,
574 if (oclass
& PK_PRIKEY_OBJ
) {
577 keyclass
= KMF_ASYM_PRI
;
578 kmf_set_attr_at_index(attrlist
, num
,
579 KMF_KEYCLASS_ATTR
, &keyclass
,
583 /* list asymmetric private keys */
584 rv
= pk_list_keys(kmfhandle
, attrlist
, num
,
585 "asymmetric private");
587 if (rv
== KMF_ERR_KEY_NOT_FOUND
)
590 if (rv
== KMF_OK
&& (oclass
& PK_SYMKEY_OBJ
)) {
593 keyclass
= KMF_SYMMETRIC
;
594 kmf_set_attr_at_index(attrlist
, num
,
595 KMF_KEYCLASS_ATTR
, &keyclass
,
599 format
= KMF_FORMAT_RAWKEY
;
600 kmf_set_attr_at_index(attrlist
, num
,
601 KMF_ENCODE_FORMAT_ATTR
, &format
,
605 /* list symmetric keys */
606 rv
= pk_list_keys(kmfhandle
, attrlist
, num
,
609 if (rv
== KMF_ERR_KEY_NOT_FOUND
)
616 if (oclass
& PK_CERT_OBJ
) {
617 kmf_set_attr_at_index(attrlist
, numattr
,
618 KMF_KEYSTORE_TYPE_ATTR
, &kstype
,
622 if (issuer
!= NULL
) {
623 kmf_set_attr_at_index(attrlist
, numattr
,
624 KMF_ISSUER_NAME_ATTR
, issuer
,
629 if (subject
!= NULL
) {
630 kmf_set_attr_at_index(attrlist
, numattr
,
631 KMF_SUBJECT_NAME_ATTR
, subject
,
636 if (serial
!= NULL
&& serial
->val
!= NULL
) {
637 kmf_set_attr_at_index(attrlist
, numattr
,
638 KMF_BIGINT_ATTR
, serial
,
639 sizeof (KMF_BIGINT
));
643 if (filename
!= NULL
) {
644 kmf_set_attr_at_index(attrlist
, numattr
,
645 KMF_CERT_FILENAME_ATTR
, filename
,
651 kmf_set_attr_at_index(attrlist
, numattr
,
652 KMF_DIRPATH_ATTR
, dir
,
657 kmf_set_attr_at_index(attrlist
, numattr
,
658 KMF_CERT_VALIDITY_ATTR
, &find_criteria_flag
,
659 sizeof (KMF_CERT_VALIDITY
));
662 rv
= pk_find_certs(kmfhandle
, attrlist
, numattr
);
668 if (oclass
& PK_CRL_OBJ
) {
669 char *crldata
= NULL
;
671 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
672 &kstype
, sizeof (kstype
));
676 kmf_set_attr_at_index(attrlist
, numattr
,
677 KMF_DIRPATH_ATTR
, dir
, strlen(dir
));
680 if (filename
!= NULL
) {
681 kmf_set_attr_at_index(attrlist
, numattr
,
682 KMF_CRL_FILENAME_ATTR
,
683 filename
, strlen(filename
));
686 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CRL_DATA_ATTR
,
687 &crldata
, sizeof (char *));
690 rv
= kmf_list_crl(kmfhandle
, numattr
, attrlist
);
691 if (rv
== KMF_OK
&& crldata
!= NULL
) {
692 (void) printf("%s\n", crldata
);
701 list_nss_objects(KMF_HANDLE_T kmfhandle
,
702 int oclass
, char *token_spec
, char *dir
, char *prefix
,
703 char *nickname
, KMF_BIGINT
*serial
, char *issuer
, char *subject
,
704 KMF_CREDENTIAL
*tokencred
,
705 KMF_CERT_VALIDITY find_criteria_flag
)
707 KMF_RETURN rv
= KMF_OK
;
708 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_NSS
;
710 KMF_ATTRIBUTE attrlist
[16];
711 KMF_KEY_CLASS keyclass
;
712 KMF_ENCODE_FORMAT format
;
714 rv
= configure_nss(kmfhandle
, dir
, prefix
);
718 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
719 &kstype
, sizeof (kstype
));
722 if (oclass
& PK_KEY_OBJ
) {
723 if (tokencred
!= NULL
&& tokencred
->credlen
> 0) {
724 kmf_set_attr_at_index(attrlist
, numattr
,
725 KMF_CREDENTIAL_ATTR
, tokencred
,
726 sizeof (KMF_CREDENTIAL
));
730 if (token_spec
&& strlen(token_spec
)) {
731 kmf_set_attr_at_index(attrlist
, numattr
,
732 KMF_TOKEN_LABEL_ATTR
, token_spec
,
737 if (nickname
!= NULL
) {
738 kmf_set_attr_at_index(attrlist
, numattr
,
739 KMF_KEYLABEL_ATTR
, nickname
,
745 if (oclass
& PK_PRIKEY_OBJ
) {
748 keyclass
= KMF_ASYM_PRI
;
749 kmf_set_attr_at_index(attrlist
, num
,
750 KMF_KEYCLASS_ATTR
, &keyclass
,
754 /* list asymmetric private keys */
755 rv
= pk_list_keys(kmfhandle
, attrlist
, num
,
756 "asymmetric private");
759 if (rv
== KMF_OK
&& (oclass
& PK_SYMKEY_OBJ
)) {
762 keyclass
= KMF_SYMMETRIC
;
763 kmf_set_attr_at_index(attrlist
, num
,
764 KMF_KEYCLASS_ATTR
, &keyclass
,
768 format
= KMF_FORMAT_RAWKEY
;
769 kmf_set_attr_at_index(attrlist
, num
,
770 KMF_ENCODE_FORMAT_ATTR
, &format
,
774 /* list symmetric keys */
775 rv
= pk_list_keys(kmfhandle
, attrlist
, num
, "symmetric");
778 if (rv
== KMF_OK
&& (oclass
& PK_PUBKEY_OBJ
)) {
781 keyclass
= KMF_ASYM_PUB
;
782 kmf_set_attr_at_index(attrlist
, num
,
783 KMF_KEYCLASS_ATTR
, &keyclass
,
787 /* list asymmetric public keys */
788 rv
= pk_list_keys(kmfhandle
, attrlist
, num
,
789 "asymmetric public");
792 /* If searching for public objects or certificates, find certs now */
794 if (rv
== KMF_OK
&& (oclass
& PK_CERT_OBJ
)) {
795 kmf_set_attr_at_index(attrlist
, numattr
,
796 KMF_KEYSTORE_TYPE_ATTR
, &kstype
,
800 if (nickname
!= NULL
) {
801 kmf_set_attr_at_index(attrlist
, numattr
,
802 KMF_CERT_LABEL_ATTR
, nickname
,
807 if (issuer
!= NULL
) {
808 kmf_set_attr_at_index(attrlist
, numattr
,
809 KMF_ISSUER_NAME_ATTR
, issuer
,
814 if (subject
!= NULL
) {
815 kmf_set_attr_at_index(attrlist
, numattr
,
816 KMF_SUBJECT_NAME_ATTR
, subject
,
821 if (serial
!= NULL
) {
822 kmf_set_attr_at_index(attrlist
, numattr
,
823 KMF_BIGINT_ATTR
, serial
,
824 sizeof (KMF_BIGINT
));
828 if (token_spec
!= NULL
) {
829 kmf_set_attr_at_index(attrlist
, numattr
,
830 KMF_TOKEN_LABEL_ATTR
, token_spec
,
835 kmf_set_attr_at_index(attrlist
, numattr
,
836 KMF_CERT_VALIDITY_ATTR
, &find_criteria_flag
,
837 sizeof (KMF_CERT_VALIDITY
));
840 rv
= pk_find_certs(kmfhandle
, attrlist
, numattr
);
844 if (rv
== KMF_OK
&& (oclass
& PK_CRL_OBJ
)) {
847 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
848 &kstype
, sizeof (kstype
));
851 if (token_spec
!= NULL
) {
852 kmf_set_attr_at_index(attrlist
, numattr
,
853 KMF_TOKEN_LABEL_ATTR
,
854 token_spec
, strlen(token_spec
));
857 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CRL_COUNT_ATTR
,
858 &numcrls
, sizeof (int));
861 rv
= kmf_find_crl(kmfhandle
, numattr
, attrlist
);
865 (void) printf(gettext("No CRLs found in "
870 p
= malloc(numcrls
* sizeof (char *));
872 return (KMF_ERR_MEMORY
);
874 (void) memset(p
, 0, numcrls
* sizeof (char *));
876 kmf_set_attr_at_index(attrlist
, numattr
,
877 KMF_CRL_NAMELIST_ATTR
, p
, sizeof (char *));
879 rv
= kmf_find_crl(kmfhandle
, numattr
, attrlist
);
882 for (i
= 0; i
< numcrls
; i
++) {
883 (void) printf("%d. Name = %s\n",
898 pk_list(int argc
, char *argv
[])
901 extern int optind_av
;
902 extern char *optarg_av
;
903 char *token_spec
= NULL
;
904 char *subject
= NULL
;
908 char *filename
= NULL
;
910 KMF_BIGINT serial
= { NULL
, 0 };
912 char *list_label
= NULL
;
914 KMF_KEYSTORE_TYPE kstype
= 0;
915 KMF_RETURN rv
= KMF_OK
;
916 KMF_HANDLE_T kmfhandle
= NULL
;
917 char *find_criteria
= NULL
;
918 KMF_CERT_VALIDITY find_criteria_flag
= KMF_ALL_CERTS
;
919 KMF_CREDENTIAL tokencred
= { NULL
, 0 };
921 /* Parse command line options. Do NOT i18n/l10n. */
922 while ((opt
= getopt_av(argc
, argv
,
923 "k:(keystore)t:(objtype)T:(token)d:(dir)"
924 "p:(prefix)n:(nickname)S:(serial)s:(subject)"
926 "i:(issuer)l:(label)f:(infile)")) != EOF
) {
927 if (EMPTYSTRING(optarg_av
))
928 return (PK_ERR_USAGE
);
932 return (PK_ERR_USAGE
);
933 kstype
= KS2Int(optarg_av
);
935 return (PK_ERR_USAGE
);
939 return (PK_ERR_USAGE
);
940 oclass
= OT2Int(optarg_av
);
942 return (PK_ERR_USAGE
);
946 return (PK_ERR_USAGE
);
951 return (PK_ERR_USAGE
);
956 return (PK_ERR_USAGE
);
961 return (PK_ERR_USAGE
);
969 return (PK_ERR_USAGE
);
970 filename
= optarg_av
;
972 case 'T': /* token specifier */
974 return (PK_ERR_USAGE
);
975 token_spec
= optarg_av
;
978 case 'l': /* object with specific label */
980 return (PK_ERR_USAGE
);
981 list_label
= optarg_av
;
984 find_criteria
= optarg_av
;
985 if (!strcasecmp(find_criteria
, "valid"))
987 KMF_NONEXPIRED_CERTS
;
988 else if (!strcasecmp(find_criteria
, "expired"))
989 find_criteria_flag
= KMF_EXPIRED_CERTS
;
990 else if (!strcasecmp(find_criteria
, "both"))
991 find_criteria_flag
= KMF_ALL_CERTS
;
993 return (PK_ERR_USAGE
);
996 return (PK_ERR_USAGE
);
999 /* No additional args allowed. */
1003 return (PK_ERR_USAGE
);
1005 if ((rv
= kmf_initialize(&kmfhandle
, NULL
, NULL
)) != KMF_OK
) {
1006 /* Error message ? */
1010 /* Assume keystore = PKCS#11 if not specified. */
1012 kstype
= KMF_KEYSTORE_PK11TOKEN
;
1014 /* if PUBLIC or PRIVATE obj was given, the old syntax was used. */
1015 if ((oclass
& (PK_PUBLIC_OBJ
| PK_PRIVATE_OBJ
)) &&
1016 kstype
!= KMF_KEYSTORE_PK11TOKEN
) {
1018 (void) fprintf(stderr
, gettext("The objtype parameter "
1019 "is only relevant if keystore=pkcs11\n"));
1020 return (PK_ERR_USAGE
);
1024 if (kstype
== KMF_KEYSTORE_PK11TOKEN
&& EMPTYSTRING(token_spec
)) {
1025 token_spec
= PK_DEFAULT_PK11TOKEN
;
1026 } else if (kstype
== KMF_KEYSTORE_NSS
&& EMPTYSTRING(token_spec
)) {
1027 token_spec
= DEFAULT_NSS_TOKEN
;
1030 if (serstr
!= NULL
) {
1031 uchar_t
*bytes
= NULL
;
1034 rv
= kmf_hexstr_to_bytes((uchar_t
*)serstr
, &bytes
, &bytelen
);
1035 if (rv
!= KMF_OK
|| bytes
== NULL
) {
1036 (void) fprintf(stderr
, gettext("serial number "
1037 "must be specified as a hex number "
1038 "(ex: 0x0102030405ffeeddee)\n"));
1039 return (PK_ERR_USAGE
);
1042 serial
.len
= bytelen
;
1043 /* if objtype was not given, it must be for certs */
1045 oclass
= PK_CERT_OBJ
;
1047 if (oclass
== 0 && (issuer
!= NULL
|| subject
!= NULL
))
1048 oclass
= PK_CERT_OBJ
;
1050 /* If no object class specified, list public objects. */
1052 oclass
= PK_CERT_OBJ
| PK_PUBKEY_OBJ
;
1054 if ((kstype
== KMF_KEYSTORE_PK11TOKEN
||
1055 kstype
== KMF_KEYSTORE_NSS
) &&
1056 (oclass
& (PK_PRIKEY_OBJ
| PK_PRIVATE_OBJ
))) {
1058 (void) get_token_password(kstype
, token_spec
,
1061 if (kstype
== KMF_KEYSTORE_PK11TOKEN
) {
1062 rv
= list_pk11_objects(kmfhandle
, token_spec
,
1063 oclass
, list_label
, &serial
,
1064 issuer
, subject
, dir
, filename
,
1065 &tokencred
, find_criteria_flag
);
1067 } else if (kstype
== KMF_KEYSTORE_NSS
) {
1069 dir
= PK_DEFAULT_DIRECTORY
;
1070 rv
= list_nss_objects(kmfhandle
,
1071 oclass
, token_spec
, dir
, prefix
,
1072 list_label
, &serial
, issuer
, subject
,
1073 &tokencred
, find_criteria_flag
);
1075 } else if (kstype
== KMF_KEYSTORE_OPENSSL
) {
1077 rv
= list_file_objects(kmfhandle
,
1078 oclass
, dir
, filename
,
1079 &serial
, issuer
, subject
, find_criteria_flag
);
1083 display_error(kmfhandle
, rv
,
1084 gettext("Error listing objects"));
1087 if (serial
.val
!= NULL
)
1090 if (tokencred
.cred
!= NULL
)
1091 free(tokencred
.cred
);
1093 (void) kmf_finalize(kmfhandle
);