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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2012 Milan Jurik. All rights reserved.
25 * Copyright 2017 Toomas Soome <tsoome@me.com>
29 * This file implements the token object delete operation for this tool.
30 * It loads the PKCS#11 modules, finds the object to delete, deletes it,
31 * and cleans up. User must be R/W logged into the token.
36 #include <cryptoutil.h>
37 #include <security/cryptoki.h>
42 pk_destroy_keys(void *handle
, KMF_ATTRIBUTE
*attrlist
, int numattr
)
45 KMF_RETURN rv
= KMF_OK
;
47 KMF_KEY_HANDLE
*keys
= NULL
;
49 KMF_ATTRIBUTE delete_attlist
[16];
50 KMF_KEYSTORE_TYPE kstype
;
52 boolean_t destroy
= B_TRUE
;
54 char *slotlabel
= NULL
;
56 len
= sizeof (kstype
);
57 rv
= kmf_get_attr(KMF_KEYSTORE_TYPE_ATTR
, attrlist
, numattr
,
62 kmf_set_attr_at_index(delete_attlist
, del_num
,
63 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
66 /* "destroy" is optional. Default is TRUE */
67 (void) kmf_get_attr(KMF_DESTROY_BOOL_ATTR
, attrlist
, numattr
,
68 (void *)&destroy
, NULL
);
70 kmf_set_attr_at_index(delete_attlist
, del_num
,
71 KMF_DESTROY_BOOL_ATTR
, &destroy
, sizeof (boolean_t
));
75 case KMF_KEYSTORE_NSS
:
76 rv
= kmf_get_attr(KMF_CREDENTIAL_ATTR
, attrlist
, numattr
,
79 if (cred
.credlen
> 0) {
80 kmf_set_attr_at_index(delete_attlist
, del_num
,
81 KMF_CREDENTIAL_ATTR
, &cred
,
82 sizeof (KMF_CREDENTIAL
));
87 slotlabel
= kmf_get_attr_ptr(KMF_TOKEN_LABEL_ATTR
, attrlist
,
89 if (slotlabel
!= NULL
) {
90 kmf_set_attr_at_index(delete_attlist
, del_num
,
91 KMF_TOKEN_LABEL_ATTR
, slotlabel
,
96 case KMF_KEYSTORE_OPENSSL
:
98 case KMF_KEYSTORE_PK11TOKEN
:
99 rv
= kmf_get_attr(KMF_CREDENTIAL_ATTR
, attrlist
, numattr
,
100 (void *)&cred
, NULL
);
102 if (cred
.credlen
> 0) {
103 kmf_set_attr_at_index(delete_attlist
, del_num
,
104 KMF_CREDENTIAL_ATTR
, &cred
,
105 sizeof (KMF_CREDENTIAL
));
111 return (PK_ERR_USAGE
);
114 numkeys
= kmf_get_attr_ptr(KMF_COUNT_ATTR
, attrlist
, numattr
);
116 return (PK_ERR_USAGE
);
118 keys
= kmf_get_attr_ptr(KMF_KEY_HANDLE_ATTR
, attrlist
, numattr
);
120 return (PK_ERR_USAGE
);
122 for (i
= 0; rv
== KMF_OK
&& i
< *numkeys
; i
++) {
125 kmf_set_attr_at_index(delete_attlist
, num
,
126 KMF_KEY_HANDLE_ATTR
, &keys
[i
], sizeof (KMF_KEY_HANDLE
));
129 rv
= kmf_delete_key_from_keystore(handle
, num
, delete_attlist
);
135 pk_delete_keys(KMF_HANDLE_T kmfhandle
, KMF_ATTRIBUTE
*attlist
, int numattr
,
136 char *desc
, int *keysdeleted
)
138 KMF_RETURN rv
= KMF_OK
;
139 uint32_t numkeys
= 0;
145 kmf_set_attr_at_index(attlist
, num
,
146 KMF_COUNT_ATTR
, &numkeys
, sizeof (uint32_t));
149 rv
= kmf_find_key(kmfhandle
, num
, attlist
);
151 if (rv
== KMF_OK
&& numkeys
> 0) {
152 KMF_KEY_HANDLE
*keys
= NULL
;
155 (void) snprintf(prompt
, sizeof (prompt
),
156 gettext("%d %s key(s) found, do you want "
157 "to delete them (y/N) ?"), numkeys
,
158 (desc
!= NULL
? desc
: ""));
161 gettext("Respond with yes or no.\n"),
163 *keysdeleted
= numkeys
;
166 keys
= (KMF_KEY_HANDLE
*)malloc(numkeys
*
167 sizeof (KMF_KEY_HANDLE
));
169 return (KMF_ERR_MEMORY
);
170 (void) memset(keys
, 0, numkeys
*
171 sizeof (KMF_KEY_HANDLE
));
173 kmf_set_attr_at_index(attlist
, num
,
174 KMF_KEY_HANDLE_ATTR
, keys
, sizeof (KMF_KEY_HANDLE
));
177 rv
= kmf_find_key(kmfhandle
, num
, attlist
);
179 rv
= pk_destroy_keys(kmfhandle
, attlist
, num
);
185 *keysdeleted
= numkeys
;
190 pk_delete_certs(KMF_HANDLE_T kmfhandle
, KMF_ATTRIBUTE
*attlist
, int numattr
)
192 KMF_RETURN rv
= KMF_OK
;
193 uint32_t numcerts
= 0;
196 kmf_set_attr_at_index(attlist
, num
,
197 KMF_COUNT_ATTR
, &numcerts
, sizeof (uint32_t));
200 rv
= kmf_find_cert(kmfhandle
, num
, attlist
);
201 if (rv
== KMF_OK
&& numcerts
> 0) {
203 (void) snprintf(prompt
, sizeof (prompt
),
204 gettext("%d certificate(s) found, do you want "
205 "to delete them (y/N) ?"), numcerts
);
208 gettext("Respond with yes or no.\n"),
214 * Use numattr because delete cert does not require
215 * KMF_COUNT_ATTR attribute.
217 rv
= kmf_delete_cert_from_keystore(kmfhandle
, numattr
, attlist
);
225 delete_nss_keys(KMF_HANDLE_T kmfhandle
, char *dir
, char *prefix
,
226 char *token
, int oclass
, char *objlabel
,
227 KMF_CREDENTIAL
*tokencred
)
229 KMF_RETURN rv
= KMF_OK
;
230 char *keytype
= NULL
;
232 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_NSS
;
234 KMF_ATTRIBUTE attrlist
[16];
235 KMF_KEY_CLASS keyclass
;
237 rv
= configure_nss(kmfhandle
, dir
, prefix
);
241 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
242 &kstype
, sizeof (kstype
));
245 if (objlabel
!= NULL
) {
246 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYLABEL_ATTR
,
247 objlabel
, strlen(objlabel
));
251 if (tokencred
->credlen
> 0) {
252 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CREDENTIAL_ATTR
,
253 tokencred
, sizeof (KMF_CREDENTIAL
));
257 if (token
&& strlen(token
)) {
258 kmf_set_attr_at_index(attrlist
, numattr
, KMF_TOKEN_LABEL_ATTR
,
259 token
, strlen(token
));
263 if (oclass
& PK_PRIKEY_OBJ
) {
266 keyclass
= KMF_ASYM_PRI
;
267 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
268 &keyclass
, sizeof (keyclass
));
272 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, keytype
, &nk
);
274 if (rv
== KMF_ERR_KEY_NOT_FOUND
&&
275 oclass
!= PK_PRIKEY_OBJ
)
278 if (rv
== KMF_OK
&& (oclass
& PK_SYMKEY_OBJ
)) {
281 keyclass
= KMF_SYMMETRIC
;
282 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
283 &keyclass
, sizeof (keyclass
));
286 keytype
= "symmetric";
287 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, keytype
, &nk
);
289 if (rv
== KMF_ERR_KEY_NOT_FOUND
&&
290 oclass
!= PK_SYMKEY_OBJ
)
293 if (rv
== KMF_OK
&& (oclass
& PK_PUBKEY_OBJ
)) {
296 keyclass
= KMF_ASYM_PUB
;
297 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
298 &keyclass
, sizeof (keyclass
));
302 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, keytype
, &nk
);
304 if (rv
== KMF_ERR_KEY_NOT_FOUND
&&
305 oclass
!= PK_PUBKEY_OBJ
)
308 if (rv
== KMF_OK
&& numkeys
== 0)
309 rv
= KMF_ERR_KEY_NOT_FOUND
;
315 delete_nss_certs(KMF_HANDLE_T kmfhandle
,
316 char *dir
, char *prefix
,
317 char *token
, char *objlabel
,
318 KMF_BIGINT
*serno
, char *issuer
, char *subject
,
319 KMF_CERT_VALIDITY find_criteria_flag
)
321 KMF_RETURN rv
= KMF_OK
;
322 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_NSS
;
324 KMF_ATTRIBUTE attrlist
[16];
326 rv
= configure_nss(kmfhandle
, dir
, prefix
);
330 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
331 &kstype
, sizeof (kstype
));
334 if (objlabel
!= NULL
) {
335 kmf_set_attr_at_index(attrlist
, numattr
,
336 KMF_CERT_LABEL_ATTR
, objlabel
,
341 if (issuer
!= NULL
) {
342 kmf_set_attr_at_index(attrlist
, numattr
,
343 KMF_ISSUER_NAME_ATTR
, issuer
,
348 if (subject
!= NULL
) {
349 kmf_set_attr_at_index(attrlist
, numattr
,
350 KMF_SUBJECT_NAME_ATTR
, subject
,
356 kmf_set_attr_at_index(attrlist
, numattr
,
357 KMF_BIGINT_ATTR
, serno
,
358 sizeof (KMF_BIGINT
));
362 kmf_set_attr_at_index(attrlist
, numattr
,
363 KMF_CERT_VALIDITY_ATTR
, &find_criteria_flag
,
364 sizeof (KMF_CERT_VALIDITY
));
368 kmf_set_attr_at_index(attrlist
, numattr
,
369 KMF_TOKEN_LABEL_ATTR
, token
,
374 rv
= pk_delete_certs(kmfhandle
, attrlist
, numattr
);
380 delete_nss_crl(void *kmfhandle
,
381 char *dir
, char *prefix
, char *token
,
382 char *issuer
, char *subject
)
384 KMF_RETURN rv
= KMF_OK
;
386 KMF_ATTRIBUTE attrlist
[8];
387 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_NSS
;
389 rv
= configure_nss(kmfhandle
, dir
, prefix
);
393 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
394 &kstype
, sizeof (kstype
));
398 kmf_set_attr_at_index(attrlist
, numattr
, KMF_TOKEN_LABEL_ATTR
,
399 token
, strlen(token
));
402 if (issuer
!= NULL
) {
403 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ISSUER_NAME_ATTR
,
404 issuer
, strlen(issuer
));
407 if (subject
!= NULL
) {
408 kmf_set_attr_at_index(attrlist
, numattr
, KMF_SUBJECT_NAME_ATTR
,
409 subject
, strlen(subject
));
413 rv
= kmf_delete_crl(kmfhandle
, numattr
, attrlist
);
419 delete_pk11_keys(KMF_HANDLE_T kmfhandle
,
420 char *token
, int oclass
, char *objlabel
,
421 KMF_CREDENTIAL
*tokencred
)
423 KMF_RETURN rv
= KMF_OK
;
425 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_PK11TOKEN
;
427 KMF_ATTRIBUTE attrlist
[16];
428 KMF_KEY_CLASS keyclass
;
429 boolean_t token_bool
= B_TRUE
;
432 * Symmetric keys and RSA/DSA private keys are always
433 * created with the "CKA_PRIVATE" field == TRUE, so
434 * make sure we search for them with it also set.
436 if (oclass
& (PK_SYMKEY_OBJ
| PK_PRIKEY_OBJ
))
437 oclass
|= PK_PRIVATE_OBJ
;
439 rv
= select_token(kmfhandle
, token
, FALSE
);
444 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
445 &kstype
, sizeof (kstype
));
448 if (objlabel
!= NULL
) {
449 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYLABEL_ATTR
,
450 objlabel
, strlen(objlabel
));
454 if (tokencred
!= NULL
&& tokencred
->credlen
> 0) {
455 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CREDENTIAL_ATTR
,
456 tokencred
, sizeof (KMF_CREDENTIAL
));
460 private = ((oclass
& PK_PRIVATE_OBJ
) > 0);
462 kmf_set_attr_at_index(attrlist
, numattr
, KMF_PRIVATE_BOOL_ATTR
,
463 &private, sizeof (private));
466 kmf_set_attr_at_index(attrlist
, numattr
, KMF_TOKEN_BOOL_ATTR
,
467 &token_bool
, sizeof (token_bool
));
470 if (oclass
& PK_PRIKEY_OBJ
) {
473 keyclass
= KMF_ASYM_PRI
;
474 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
475 &keyclass
, sizeof (keyclass
));
478 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, "private", &nk
);
480 if (rv
== KMF_ERR_KEY_NOT_FOUND
&&
481 oclass
!= PK_PRIKEY_OBJ
)
485 if (rv
== KMF_OK
&& (oclass
& PK_SYMKEY_OBJ
)) {
488 keyclass
= KMF_SYMMETRIC
;
489 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
490 &keyclass
, sizeof (keyclass
));
493 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, "symmetric", &nk
);
495 if (rv
== KMF_ERR_KEY_NOT_FOUND
&&
496 oclass
!= PK_SYMKEY_OBJ
)
500 if (rv
== KMF_OK
&& (oclass
& PK_PUBKEY_OBJ
)) {
504 keyclass
= KMF_ASYM_PUB
;
505 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
506 &keyclass
, sizeof (keyclass
));
509 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, "public", &nk
);
511 if (rv
== KMF_ERR_KEY_NOT_FOUND
&&
512 oclass
!= PK_PUBKEY_OBJ
)
515 if (rv
== KMF_OK
&& numkeys
== 0)
516 rv
= KMF_ERR_KEY_NOT_FOUND
;
522 delete_pk11_certs(KMF_HANDLE_T kmfhandle
,
523 char *token
, char *objlabel
,
524 KMF_BIGINT
*serno
, char *issuer
, char *subject
,
525 KMF_CERT_VALIDITY find_criteria_flag
)
528 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_PK11TOKEN
;
530 KMF_ATTRIBUTE attrlist
[16];
532 kmfrv
= select_token(kmfhandle
, token
, FALSE
);
534 if (kmfrv
!= KMF_OK
) {
538 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
539 &kstype
, sizeof (kstype
));
542 if (objlabel
!= NULL
) {
543 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_LABEL_ATTR
,
544 objlabel
, strlen(objlabel
));
548 if (issuer
!= NULL
) {
549 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ISSUER_NAME_ATTR
,
550 issuer
, strlen(issuer
));
554 if (subject
!= NULL
) {
555 kmf_set_attr_at_index(attrlist
, numattr
, KMF_SUBJECT_NAME_ATTR
,
556 subject
, strlen(subject
));
561 kmf_set_attr_at_index(attrlist
, numattr
, KMF_BIGINT_ATTR
,
562 serno
, sizeof (KMF_BIGINT
));
566 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_VALIDITY_ATTR
,
567 &find_criteria_flag
, sizeof (KMF_CERT_VALIDITY
));
570 kmfrv
= pk_delete_certs(kmfhandle
, attrlist
, numattr
);
576 delete_file_certs(KMF_HANDLE_T kmfhandle
,
577 char *dir
, char *filename
, KMF_BIGINT
*serial
, char *issuer
,
578 char *subject
, KMF_CERT_VALIDITY find_criteria_flag
)
581 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_OPENSSL
;
583 KMF_ATTRIBUTE attrlist
[16];
585 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
586 &kstype
, sizeof (kstype
));
589 if (issuer
!= NULL
) {
590 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ISSUER_NAME_ATTR
,
591 issuer
, strlen(issuer
));
595 if (subject
!= NULL
) {
596 kmf_set_attr_at_index(attrlist
, numattr
, KMF_SUBJECT_NAME_ATTR
,
597 subject
, strlen(subject
));
601 if (serial
!= NULL
) {
602 kmf_set_attr_at_index(attrlist
, numattr
, KMF_BIGINT_ATTR
,
603 serial
, sizeof (KMF_BIGINT
));
608 kmf_set_attr_at_index(attrlist
, numattr
, KMF_DIRPATH_ATTR
,
613 if (filename
!= NULL
) {
614 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_FILENAME_ATTR
,
615 filename
, strlen(filename
));
619 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_VALIDITY_ATTR
,
620 &find_criteria_flag
, sizeof (KMF_CERT_VALIDITY
));
623 rv
= pk_delete_certs(kmfhandle
, attrlist
, numattr
);
629 delete_file_keys(KMF_HANDLE_T kmfhandle
, int oclass
, char *dir
, char *infile
)
631 KMF_RETURN rv
= KMF_OK
;
634 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_OPENSSL
;
636 KMF_ATTRIBUTE attrlist
[16];
637 KMF_KEY_CLASS keyclass
;
639 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
640 &kstype
, sizeof (kstype
));
644 kmf_set_attr_at_index(attrlist
, numattr
, KMF_DIRPATH_ATTR
,
649 if (infile
!= NULL
) {
650 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEY_FILENAME_ATTR
,
651 infile
, strlen(infile
));
655 if (oclass
& (PK_PUBKEY_OBJ
| PK_PRIKEY_OBJ
)) {
658 keyclass
= KMF_ASYM_PRI
;
659 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
660 &keyclass
, sizeof (keyclass
));
663 keytype
= "Asymmetric";
664 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, keytype
, &nk
);
667 if (oclass
& PK_SYMKEY_OBJ
) {
670 keyclass
= KMF_SYMMETRIC
;
671 kmf_set_attr_at_index(attrlist
, num
, KMF_KEYCLASS_ATTR
,
672 &keyclass
, sizeof (keyclass
));
675 keytype
= "symmetric";
676 rv
= pk_delete_keys(kmfhandle
, attrlist
, num
, keytype
, &nk
);
678 if (rv
== KMF_ERR_KEY_NOT_FOUND
&& numkeys
> 0)
681 if (rv
== KMF_OK
&& numkeys
== 0)
682 rv
= KMF_ERR_KEY_NOT_FOUND
;
688 delete_file_crl(void *kmfhandle
, char *filename
)
692 KMF_ATTRIBUTE attrlist
[4];
693 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_OPENSSL
;
695 if (filename
== NULL
|| strlen(filename
) == 0)
696 return (KMF_ERR_BAD_PARAMETER
);
698 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
699 &kstype
, sizeof (kstype
));
703 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CRL_FILENAME_ATTR
,
704 filename
, strlen(filename
));
708 rv
= kmf_delete_crl(kmfhandle
, numattr
, attrlist
);
714 * Delete token objects.
717 pk_delete(int argc
, char *argv
[])
720 extern int optind_av
;
721 extern char *optarg_av
;
722 char *token_spec
= NULL
;
723 char *subject
= NULL
;
728 char *object_label
= NULL
;
732 KMF_BIGINT serial
= { NULL
, 0 };
733 KMF_HANDLE_T kmfhandle
= NULL
;
734 KMF_KEYSTORE_TYPE kstype
= 0;
735 KMF_RETURN kmfrv
, keyrv
, certrv
, crlrv
;
737 char *find_criteria
= NULL
;
738 KMF_CERT_VALIDITY find_criteria_flag
= KMF_ALL_CERTS
;
739 KMF_CREDENTIAL tokencred
= { NULL
, 0 };
741 /* Parse command line options. Do NOT i18n/l10n. */
742 while ((opt
= getopt_av(argc
, argv
,
743 "T:(token)y:(objtype)l:(label)"
744 "k:(keystore)s:(subject)n:(nickname)"
745 "d:(dir)p:(prefix)S:(serial)i:(issuer)"
747 "f:(infile)")) != EOF
) {
749 if (EMPTYSTRING(optarg_av
))
750 return (PK_ERR_USAGE
);
752 case 'T': /* token specifier */
754 return (PK_ERR_USAGE
);
755 token_spec
= optarg_av
;
757 case 'y': /* object type: public, private, both */
759 return (PK_ERR_USAGE
);
760 oclass
= OT2Int(optarg_av
);
762 return (PK_ERR_USAGE
);
764 case 'l': /* objects with specific label */
767 return (PK_ERR_USAGE
);
768 object_label
= (char *)optarg_av
;
771 kstype
= KS2Int(optarg_av
);
773 return (PK_ERR_USAGE
);
794 find_criteria
= optarg_av
;
795 if (!strcasecmp(find_criteria
, "valid"))
797 KMF_NONEXPIRED_CERTS
;
798 else if (!strcasecmp(find_criteria
, "expired"))
799 find_criteria_flag
= KMF_EXPIRED_CERTS
;
800 else if (!strcasecmp(find_criteria
, "both"))
801 find_criteria_flag
= KMF_ALL_CERTS
;
803 return (PK_ERR_USAGE
);
806 return (PK_ERR_USAGE
);
810 /* Assume keystore = PKCS#11 if not specified */
812 kstype
= KMF_KEYSTORE_PK11TOKEN
;
814 /* if PUBLIC or PRIVATE obj was given, the old syntax was used. */
815 if ((oclass
& (PK_PUBLIC_OBJ
| PK_PRIVATE_OBJ
)) &&
816 kstype
!= KMF_KEYSTORE_PK11TOKEN
) {
818 (void) fprintf(stderr
, gettext("The objtype parameter "
819 "is only relevant if keystore=pkcs11\n"));
820 return (PK_ERR_USAGE
);
824 /* No additional args allowed. */
828 return (PK_ERR_USAGE
);
829 /* Done parsing command line options. */
831 DIR_OPTION_CHECK(kstype
, dir
);
833 if (kstype
== KMF_KEYSTORE_PK11TOKEN
&& token_spec
== NULL
) {
834 token_spec
= PK_DEFAULT_PK11TOKEN
;
835 } else if (kstype
== KMF_KEYSTORE_NSS
&& token_spec
== NULL
) {
836 token_spec
= DEFAULT_NSS_TOKEN
;
839 if (serstr
!= NULL
) {
840 uchar_t
*bytes
= NULL
;
843 rv
= kmf_hexstr_to_bytes((uchar_t
*)serstr
, &bytes
, &bytelen
);
844 if (rv
!= KMF_OK
|| bytes
== NULL
) {
845 (void) fprintf(stderr
, gettext("serial number "
846 "must be specified as a hex number "
847 "(ex: 0x0102030405ffeeddee)\n"));
848 return (PK_ERR_USAGE
);
851 serial
.len
= bytelen
;
852 /* If serial number was given, it must be a cert search */
854 oclass
= PK_CERT_OBJ
;
857 * If no object type was given but subject or issuer was,
858 * it must be a certificate we are looking to delete.
860 if ((issuer
!= NULL
|| subject
!= NULL
) && oclass
== 0)
861 oclass
= PK_CERT_OBJ
;
862 /* If no object class specified, delete everything but CRLs */
864 oclass
= PK_CERT_OBJ
| PK_KEY_OBJ
;
866 if ((kstype
== KMF_KEYSTORE_PK11TOKEN
||
867 kstype
== KMF_KEYSTORE_NSS
) &&
868 (oclass
& (PK_KEY_OBJ
| PK_PRIVATE_OBJ
))) {
870 (void) get_token_password(kstype
, token_spec
,
874 if ((kmfrv
= kmf_initialize(&kmfhandle
, NULL
, NULL
)) != KMF_OK
)
877 keyrv
= certrv
= crlrv
= KMF_OK
;
879 case KMF_KEYSTORE_PK11TOKEN
:
880 if (oclass
& PK_KEY_OBJ
) {
881 keyrv
= delete_pk11_keys(kmfhandle
,
883 object_label
, &tokencred
);
885 * If deleting groups of objects, it is OK
886 * to ignore the "key not found" case so that
887 * we can continue to find other objects.
889 if (keyrv
!= KMF_OK
&&
890 keyrv
!= KMF_ERR_KEY_NOT_FOUND
)
893 if (oclass
& (PK_CERT_OBJ
| PK_PUBLIC_OBJ
)) {
894 certrv
= delete_pk11_certs(kmfhandle
,
895 token_spec
, object_label
,
897 subject
, find_criteria_flag
);
899 * If cert delete failed, but we are looking at
900 * other objects, then it is OK.
902 if (certrv
!= KMF_OK
&&
903 certrv
!= KMF_ERR_CERT_NOT_FOUND
)
906 if (oclass
& PK_CRL_OBJ
)
907 crlrv
= delete_file_crl(kmfhandle
,
910 case KMF_KEYSTORE_NSS
:
911 keyrv
= certrv
= crlrv
= KMF_OK
;
912 if (oclass
& PK_KEY_OBJ
) {
913 keyrv
= delete_nss_keys(kmfhandle
,
914 dir
, prefix
, token_spec
,
915 oclass
, (char *)object_label
,
917 if (keyrv
!= KMF_OK
&&
918 keyrv
!= KMF_ERR_KEY_NOT_FOUND
)
921 if (oclass
& PK_CERT_OBJ
) {
922 certrv
= delete_nss_certs(kmfhandle
,
923 dir
, prefix
, token_spec
,
924 (char *)object_label
,
925 &serial
, issuer
, subject
,
927 if (certrv
!= KMF_OK
&&
928 certrv
!= KMF_ERR_CERT_NOT_FOUND
)
931 if (oclass
& PK_CRL_OBJ
)
932 crlrv
= delete_nss_crl(kmfhandle
,
933 dir
, prefix
, token_spec
,
934 (char *)object_label
, subject
);
936 case KMF_KEYSTORE_OPENSSL
:
937 if (oclass
& PK_KEY_OBJ
) {
938 keyrv
= delete_file_keys(kmfhandle
, oclass
,
943 if (oclass
& (PK_CERT_OBJ
)) {
944 certrv
= delete_file_certs(kmfhandle
,
945 dir
, infile
, &serial
, issuer
,
946 subject
, find_criteria_flag
);
947 if (certrv
!= KMF_OK
)
950 if (oclass
& PK_CRL_OBJ
)
951 crlrv
= delete_file_crl(kmfhandle
,
961 * If searching for more than just one class of object (key or cert)
962 * and only 1 of the classes was not found, it is not an error.
963 * If searching for just one class of object, that failure should
966 * Any error other than "KMF_ERR_[key/cert]_NOT_FOUND" should
967 * be reported either way.
969 if (keyrv
!= KMF_ERR_KEY_NOT_FOUND
&& keyrv
!= KMF_OK
)
971 else if (certrv
!= KMF_OK
&& certrv
!= KMF_ERR_CERT_NOT_FOUND
)
973 else if (crlrv
!= KMF_OK
&& crlrv
!= KMF_ERR_CRL_NOT_FOUND
)
977 * If nothing was found, return error.
979 if ((keyrv
== KMF_ERR_KEY_NOT_FOUND
&& (oclass
& PK_KEY_OBJ
)) &&
980 (certrv
== KMF_ERR_CERT_NOT_FOUND
&& (oclass
& PK_CERT_OBJ
)))
981 kmfrv
= KMF_ERR_KEY_NOT_FOUND
;
986 if (keyrv
!= KMF_OK
&& (oclass
== PK_KEY_OBJ
))
988 else if (certrv
!= KMF_OK
&& (oclass
== PK_CERT_OBJ
))
990 else if (crlrv
!= KMF_OK
&& (oclass
== PK_CRL_OBJ
))
994 if (kmfrv
!= KMF_OK
) {
995 display_error(kmfhandle
, kmfrv
,
996 gettext("Error deleting objects"));
1000 (void) kmf_finalize(kmfhandle
);