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]
21 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
22 * Copyright 2012 Milan Jurik. All rights reserved.
26 * This file implements the export operation for this tool.
27 * The basic flow of the process is to find the soft token,
28 * log into it, find the PKCS#11 objects in the soft token
29 * to be exported matching keys with their certificates, export
30 * them to the PKCS#12 file encrypting them with a file password
31 * if desired, and log out.
44 pk_find_export_cert(KMF_HANDLE_T kmfhandle
, KMF_ATTRIBUTE
*attrlist
,
45 int numattr
, KMF_X509_DER_CERT
*cert
)
47 KMF_RETURN rv
= KMF_OK
;
48 uint32_t numcerts
= 0;
51 (void) memset(cert
, 0, sizeof (KMF_X509_DER_CERT
));
53 kmf_set_attr_at_index(attrlist
, numattr
, KMF_COUNT_ATTR
,
54 &numcerts
, sizeof (uint32_t));
57 rv
= kmf_find_cert(kmfhandle
, numattr
, attrlist
);
62 cryptoerror(LOG_STDERR
,
63 gettext("No matching certificates found."));
64 return (KMF_ERR_CERT_NOT_FOUND
);
66 } else if (numcerts
== 1) {
67 kmf_set_attr_at_index(attrlist
, numattr
,
68 KMF_X509_DER_CERT_ATTR
, cert
,
69 sizeof (KMF_X509_DER_CERT
));
71 rv
= kmf_find_cert(kmfhandle
, numattr
, attrlist
);
73 } else if (numcerts
> 1) {
74 cryptoerror(LOG_STDERR
,
75 gettext("%d certificates found, refine the "
76 "search parameters to eliminate ambiguity\n"),
78 return (KMF_ERR_BAD_PARAMETER
);
84 pk_export_file_objects(KMF_HANDLE_T kmfhandle
, int oclass
,
85 char *issuer
, char *subject
, KMF_BIGINT
*serial
,
86 char *infile
, char *filename
)
88 KMF_RETURN rv
= KMF_OK
;
89 KMF_X509_DER_CERT kmfcert
;
90 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_OPENSSL
;
92 KMF_ATTRIBUTE attrlist
[16];
94 /* If searching for public objects or certificates, find certs now */
95 if (oclass
& (PK_CERT_OBJ
| PK_PUBLIC_OBJ
)) {
96 kmf_set_attr_at_index(attrlist
, numattr
,
97 KMF_KEYSTORE_TYPE_ATTR
, &kstype
,
101 if (issuer
!= NULL
) {
102 kmf_set_attr_at_index(attrlist
, numattr
,
103 KMF_ISSUER_NAME_ATTR
, issuer
,
108 if (subject
!= NULL
) {
109 kmf_set_attr_at_index(attrlist
, numattr
,
110 KMF_SUBJECT_NAME_ATTR
, subject
,
115 if (serial
!= NULL
) {
116 kmf_set_attr_at_index(attrlist
, numattr
,
117 KMF_BIGINT_ATTR
, serial
,
118 sizeof (KMF_BIGINT
));
122 if (infile
!= NULL
) {
123 kmf_set_attr_at_index(attrlist
, numattr
,
124 KMF_CERT_FILENAME_ATTR
, infile
,
129 rv
= pk_find_export_cert(kmfhandle
, attrlist
, numattr
,
132 kstype
= KMF_KEYSTORE_OPENSSL
;
135 kmf_set_attr_at_index(attrlist
, numattr
,
136 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
139 kmf_set_attr_at_index(attrlist
, numattr
,
140 KMF_CERT_DATA_ATTR
, &kmfcert
.certificate
,
144 kmf_set_attr_at_index(attrlist
, numattr
,
145 KMF_CERT_FILENAME_ATTR
, filename
,
149 rv
= kmf_store_cert(kmfhandle
, numattr
,
152 kmf_free_kmf_cert(kmfhandle
, &kmfcert
);
159 pk_export_pk12_nss(KMF_HANDLE_T kmfhandle
,
160 char *token_spec
, char *dir
, char *prefix
,
161 char *certlabel
, char *issuer
, char *subject
,
162 KMF_BIGINT
*serial
, KMF_CREDENTIAL
*tokencred
,
165 KMF_RETURN rv
= KMF_OK
;
166 KMF_KEYSTORE_TYPE kstype
;
167 KMF_CREDENTIAL p12cred
= { NULL
, 0 };
168 KMF_ATTRIBUTE attrlist
[16];
171 rv
= configure_nss(kmfhandle
, dir
, prefix
);
175 if (token_spec
== NULL
)
176 token_spec
= DEFAULT_NSS_TOKEN
;
178 kstype
= KMF_KEYSTORE_NSS
;
179 kmf_set_attr_at_index(attrlist
, numattr
,
180 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
183 if (certlabel
!= NULL
) {
184 kmf_set_attr_at_index(attrlist
, numattr
,
185 KMF_CERT_LABEL_ATTR
, certlabel
, strlen(certlabel
));
189 if (issuer
!= NULL
) {
190 kmf_set_attr_at_index(attrlist
, numattr
,
191 KMF_ISSUER_NAME_ATTR
, issuer
, strlen(issuer
));
195 if (subject
!= NULL
) {
196 kmf_set_attr_at_index(attrlist
, numattr
,
197 KMF_SUBJECT_NAME_ATTR
, subject
, strlen(subject
));
201 if (serial
!= NULL
) {
202 kmf_set_attr_at_index(attrlist
, numattr
,
203 KMF_BIGINT_ATTR
, serial
, sizeof (KMF_BIGINT
));
207 if (tokencred
!= NULL
) {
208 kmf_set_attr_at_index(attrlist
, numattr
,
209 KMF_CREDENTIAL_ATTR
, tokencred
, sizeof (KMF_CREDENTIAL
));
213 kmf_set_attr_at_index(attrlist
, numattr
, KMF_TOKEN_LABEL_ATTR
,
214 token_spec
, strlen(token_spec
));
217 (void) get_pk12_password(&p12cred
);
218 kmf_set_attr_at_index(attrlist
, numattr
,
219 KMF_PK12CRED_ATTR
, &p12cred
, sizeof (KMF_CREDENTIAL
));
222 kmf_set_attr_at_index(attrlist
, numattr
,
223 KMF_OUTPUT_FILENAME_ATTR
, filename
, strlen(filename
));
226 rv
= kmf_export_pk12(kmfhandle
, numattr
, attrlist
);
235 pk_export_pk12_files(KMF_HANDLE_T kmfhandle
,
236 char *certfile
, char *keyfile
,
240 KMF_KEYSTORE_TYPE kstype
;
241 KMF_CREDENTIAL p12cred
= { NULL
, 0 };
242 KMF_ATTRIBUTE attrlist
[16];
245 kstype
= KMF_KEYSTORE_OPENSSL
;
246 kmf_set_attr_at_index(attrlist
, numattr
,
247 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
250 if (certfile
!= NULL
) {
251 kmf_set_attr_at_index(attrlist
, numattr
,
252 KMF_CERT_FILENAME_ATTR
, certfile
, strlen(certfile
));
256 if (keyfile
!= NULL
) {
257 kmf_set_attr_at_index(attrlist
, numattr
,
258 KMF_KEY_FILENAME_ATTR
, keyfile
, strlen(keyfile
));
262 (void) get_pk12_password(&p12cred
);
263 kmf_set_attr_at_index(attrlist
, numattr
,
264 KMF_PK12CRED_ATTR
, &p12cred
, sizeof (KMF_CREDENTIAL
));
267 kmf_set_attr_at_index(attrlist
, numattr
,
268 KMF_OUTPUT_FILENAME_ATTR
, outfile
, strlen(outfile
));
271 rv
= kmf_export_pk12(kmfhandle
, numattr
, attrlist
);
280 pk_export_nss_objects(KMF_HANDLE_T kmfhandle
, char *token_spec
,
281 int oclass
, char *certlabel
, char *issuer
, char *subject
,
282 KMF_BIGINT
*serial
, KMF_ENCODE_FORMAT kfmt
, char *dir
,
283 char *prefix
, char *filename
)
285 KMF_RETURN rv
= KMF_OK
;
286 KMF_X509_DER_CERT kmfcert
;
287 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_NSS
;
288 KMF_ATTRIBUTE attrlist
[16];
291 rv
= configure_nss(kmfhandle
, dir
, prefix
);
295 /* If searching for public objects or certificates, find certs now */
296 if (oclass
& (PK_CERT_OBJ
| PK_PUBLIC_OBJ
)) {
297 kmf_set_attr_at_index(attrlist
, numattr
,
298 KMF_KEYSTORE_TYPE_ATTR
, &kstype
,
302 if (certlabel
!= NULL
) {
303 kmf_set_attr_at_index(attrlist
, numattr
,
304 KMF_CERT_LABEL_ATTR
, certlabel
,
309 if (issuer
!= NULL
) {
310 kmf_set_attr_at_index(attrlist
, numattr
,
311 KMF_ISSUER_NAME_ATTR
, issuer
,
316 if (subject
!= NULL
) {
317 kmf_set_attr_at_index(attrlist
, numattr
,
318 KMF_SUBJECT_NAME_ATTR
, subject
,
323 if (serial
!= NULL
) {
324 kmf_set_attr_at_index(attrlist
, numattr
,
325 KMF_BIGINT_ATTR
, serial
,
326 sizeof (KMF_BIGINT
));
330 if (token_spec
!= NULL
) {
331 kmf_set_attr_at_index(attrlist
, numattr
,
332 KMF_TOKEN_LABEL_ATTR
, token_spec
,
337 rv
= pk_find_export_cert(kmfhandle
, attrlist
, numattr
,
340 kstype
= KMF_KEYSTORE_OPENSSL
;
343 kmf_set_attr_at_index(attrlist
, numattr
,
344 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
347 kmf_set_attr_at_index(attrlist
, numattr
,
348 KMF_CERT_DATA_ATTR
, &kmfcert
.certificate
,
352 kmf_set_attr_at_index(attrlist
, numattr
,
353 KMF_CERT_FILENAME_ATTR
, filename
,
357 kmf_set_attr_at_index(attrlist
, numattr
,
358 KMF_ENCODE_FORMAT_ATTR
, &kfmt
, sizeof (kfmt
));
361 rv
= kmf_store_cert(kmfhandle
, numattr
, attrlist
);
363 kmf_free_kmf_cert(kmfhandle
, &kmfcert
);
370 pk_export_pk12_pk11(KMF_HANDLE_T kmfhandle
, char *token_spec
,
371 char *certlabel
, char *issuer
, char *subject
,
372 KMF_BIGINT
*serial
, KMF_CREDENTIAL
*tokencred
, char *filename
)
374 KMF_RETURN rv
= KMF_OK
;
375 KMF_KEYSTORE_TYPE kstype
;
376 KMF_CREDENTIAL p12cred
= { NULL
, 0 };
377 KMF_ATTRIBUTE attrlist
[16];
380 rv
= select_token(kmfhandle
, token_spec
, TRUE
);
385 kstype
= KMF_KEYSTORE_PK11TOKEN
;
386 kmf_set_attr_at_index(attrlist
, numattr
,
387 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
390 if (certlabel
!= NULL
) {
391 kmf_set_attr_at_index(attrlist
, numattr
,
392 KMF_CERT_LABEL_ATTR
, certlabel
, strlen(certlabel
));
396 if (issuer
!= NULL
) {
397 kmf_set_attr_at_index(attrlist
, numattr
,
398 KMF_ISSUER_NAME_ATTR
, issuer
, strlen(issuer
));
402 if (subject
!= NULL
) {
403 kmf_set_attr_at_index(attrlist
, numattr
,
404 KMF_SUBJECT_NAME_ATTR
, subject
, strlen(subject
));
408 if (serial
!= NULL
) {
409 kmf_set_attr_at_index(attrlist
, numattr
,
410 KMF_BIGINT_ATTR
, serial
, sizeof (KMF_BIGINT
));
414 if (tokencred
!= NULL
) {
415 kmf_set_attr_at_index(attrlist
, numattr
,
416 KMF_CREDENTIAL_ATTR
, tokencred
, sizeof (KMF_CREDENTIAL
));
420 (void) get_pk12_password(&p12cred
);
421 kmf_set_attr_at_index(attrlist
, numattr
,
422 KMF_PK12CRED_ATTR
, &p12cred
, sizeof (KMF_CREDENTIAL
));
425 kmf_set_attr_at_index(attrlist
, numattr
,
426 KMF_OUTPUT_FILENAME_ATTR
, filename
, strlen(filename
));
429 rv
= kmf_export_pk12(kmfhandle
, numattr
, attrlist
);
438 pk_export_pk11_keys(KMF_HANDLE_T kmfhandle
, char *token
,
439 KMF_CREDENTIAL
*cred
, KMF_ENCODE_FORMAT format
,
440 char *label
, char *filename
, int oclass
)
442 KMF_RETURN rv
= KMF_OK
;
443 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_PK11TOKEN
;
444 KMF_KEY_CLASS kclass
= KMF_KEYCLASS_NONE
;
446 uint32_t numkeys
= 1;
447 KMF_ATTRIBUTE attrlist
[16];
449 boolean_t is_token
= B_TRUE
;
451 if (EMPTYSTRING(label
)) {
452 cryptoerror(LOG_STDERR
, gettext("A label "
453 "must be specified to export a key."));
454 return (KMF_ERR_BAD_PARAMETER
);
457 rv
= select_token(kmfhandle
, token
, TRUE
);
462 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
463 &kstype
, sizeof (kstype
));
467 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CREDENTIAL_ATTR
,
468 cred
, sizeof (KMF_CREDENTIAL
));
472 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYLABEL_ATTR
,
473 label
, strlen(label
));
476 kmf_set_attr_at_index(attrlist
, numattr
, KMF_COUNT_ATTR
,
477 &numkeys
, sizeof (numkeys
));
480 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEY_HANDLE_ATTR
,
484 kmf_set_attr_at_index(attrlist
, numattr
, KMF_TOKEN_BOOL_ATTR
,
485 &is_token
, sizeof (is_token
));
488 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ENCODE_FORMAT_ATTR
,
489 &format
, sizeof (format
));
492 /* Check to see if we are exporting private or public only */
493 if ((oclass
& PK_KEY_OBJ
) == PK_PRIKEY_OBJ
)
494 kclass
= KMF_ASYM_PRI
;
495 else if ((oclass
& PK_KEY_OBJ
) == PK_PUBKEY_OBJ
)
496 kclass
= KMF_ASYM_PUB
;
497 else if ((oclass
& PK_KEY_OBJ
) == PK_SYMKEY_OBJ
)
498 kclass
= KMF_SYMMETRIC
;
499 else /* only 1 key at a time can be exported here, so default to pri */
500 kclass
= KMF_ASYM_PRI
;
502 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYCLASS_ATTR
,
503 &kclass
, sizeof (kclass
));
506 rv
= kmf_find_key(kmfhandle
, numattr
, attrlist
);
508 * If nothing found but caller wanted ALL keys, try symmetric
511 if (rv
== KMF_ERR_KEY_NOT_FOUND
&& (oclass
== PK_KEY_OBJ
)) {
512 kclass
= KMF_SYMMETRIC
;
513 rv
= kmf_find_key(kmfhandle
, numattr
, attrlist
);
516 * If nothing found but caller wanted ALL keys, try asymmetric
519 if (rv
== KMF_ERR_KEY_NOT_FOUND
&& (oclass
== PK_KEY_OBJ
)) {
520 kclass
= KMF_ASYM_PUB
;
521 rv
= kmf_find_key(kmfhandle
, numattr
, attrlist
);
523 if (rv
== KMF_OK
&& key
.keyclass
== KMF_SYMMETRIC
) {
524 KMF_RAW_SYM_KEY rkey
;
526 (void) memset(&rkey
, 0, sizeof (KMF_RAW_SYM_KEY
));
527 rv
= kmf_get_sym_key_value(kmfhandle
, &key
, &rkey
);
529 int fd
, n
, total
= 0;
531 fd
= open(filename
, O_CREAT
| O_RDWR
|O_TRUNC
, 0600);
533 rv
= KMF_ERR_OPEN_FILE
;
537 n
= write(fd
, rkey
.keydata
.val
+ total
,
538 rkey
.keydata
.len
- total
);
543 rv
= KMF_ERR_WRITE_FILE
;
548 } while (total
< rkey
.keydata
.len
);
552 kmf_free_bigint(&rkey
.keydata
);
553 kmf_free_kmf_key(kmfhandle
, &key
);
554 } else if (rv
== KMF_OK
) {
555 KMF_KEYSTORE_TYPE sslks
= KMF_KEYSTORE_OPENSSL
;
556 (void) printf(gettext("Found %d asymmetric keys\n"), numkeys
);
559 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
560 &sslks
, sizeof (sslks
));
563 kmf_set_attr_at_index(attrlist
, numattr
, KMF_RAW_KEY_ATTR
,
564 key
.keyp
, sizeof (KMF_RAW_KEY_DATA
));
567 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ENCODE_FORMAT_ATTR
,
568 &format
, sizeof (format
));
571 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEY_FILENAME_ATTR
,
572 filename
, strlen(filename
));
575 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYCLASS_ATTR
,
576 &key
.keyclass
, sizeof (KMF_KEY_CLASS
));
579 rv
= kmf_store_key(kmfhandle
, numattr
, attrlist
);
580 kmf_free_kmf_key(kmfhandle
, &key
);
587 pk_export_pk11_objects(KMF_HANDLE_T kmfhandle
, char *token_spec
,
588 KMF_CREDENTIAL
*cred
, char *certlabel
, char *issuer
, char *subject
,
589 KMF_BIGINT
*serial
, KMF_ENCODE_FORMAT kfmt
,
592 KMF_RETURN rv
= KMF_OK
;
593 KMF_X509_DER_CERT kmfcert
;
594 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_PK11TOKEN
;
596 KMF_ATTRIBUTE attrlist
[16];
598 rv
= select_token(kmfhandle
, token_spec
, TRUE
);
603 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
604 &kstype
, sizeof (kstype
));
608 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CREDENTIAL_ATTR
,
609 cred
, sizeof (KMF_CREDENTIAL
));
612 if (certlabel
!= NULL
) {
613 kmf_set_attr_at_index(attrlist
, numattr
,
614 KMF_CERT_LABEL_ATTR
, certlabel
,
619 if (issuer
!= NULL
) {
620 kmf_set_attr_at_index(attrlist
, numattr
,
621 KMF_ISSUER_NAME_ATTR
, issuer
,
626 if (subject
!= NULL
) {
627 kmf_set_attr_at_index(attrlist
, numattr
,
628 KMF_SUBJECT_NAME_ATTR
, subject
,
633 if (serial
!= NULL
) {
634 kmf_set_attr_at_index(attrlist
, numattr
,
635 KMF_BIGINT_ATTR
, serial
,
636 sizeof (KMF_BIGINT
));
640 rv
= pk_find_export_cert(kmfhandle
, attrlist
, numattr
, &kmfcert
);
643 kstype
= KMF_KEYSTORE_OPENSSL
;
646 kmf_set_attr_at_index(attrlist
, numattr
,
647 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
650 kmf_set_attr_at_index(attrlist
, numattr
,
651 KMF_CERT_DATA_ATTR
, &kmfcert
.certificate
,
655 kmf_set_attr_at_index(attrlist
, numattr
,
656 KMF_CERT_FILENAME_ATTR
, filename
, strlen(filename
));
659 kmf_set_attr_at_index(attrlist
, numattr
,
660 KMF_ENCODE_FORMAT_ATTR
, &kfmt
, sizeof (kfmt
));
663 rv
= kmf_store_cert(kmfhandle
, numattr
, attrlist
);
665 kmf_free_kmf_cert(kmfhandle
, &kmfcert
);
671 * Export objects from one keystore to a file.
674 pk_export(int argc
, char *argv
[])
677 extern int optind_av
;
678 extern char *optarg_av
;
679 char *token_spec
= NULL
;
680 char *filename
= NULL
;
683 char *certlabel
= NULL
;
684 char *subject
= NULL
;
687 char *keyfile
= NULL
;
688 char *certfile
= NULL
;
690 KMF_KEYSTORE_TYPE kstype
= 0;
691 KMF_ENCODE_FORMAT kfmt
= KMF_FORMAT_PKCS12
;
692 KMF_RETURN rv
= KMF_OK
;
693 int oclass
= PK_CERT_OBJ
;
694 KMF_BIGINT serial
= { NULL
, 0 };
695 KMF_HANDLE_T kmfhandle
= NULL
;
696 KMF_CREDENTIAL tokencred
= { NULL
, 0 };
698 /* Parse command line options. Do NOT i18n/l10n. */
699 while ((opt
= getopt_av(argc
, argv
,
700 "k:(keystore)y:(objtype)T:(token)"
702 "l:(label)n:(nickname)s:(subject)"
703 "i:(issuer)S:(serial)"
704 "K:(keyfile)c:(certfile)"
706 "I:(infile)o:(outfile)")) != EOF
) {
707 if (EMPTYSTRING(optarg_av
))
708 return (PK_ERR_USAGE
);
711 kstype
= KS2Int(optarg_av
);
713 return (PK_ERR_USAGE
);
716 oclass
= OT2Int(optarg_av
);
718 return (PK_ERR_USAGE
);
720 case 'T': /* token specifier */
722 return (PK_ERR_USAGE
);
723 token_spec
= optarg_av
;
727 return (PK_ERR_USAGE
);
732 return (PK_ERR_USAGE
);
738 return (PK_ERR_USAGE
);
739 certlabel
= optarg_av
;
743 return (PK_ERR_USAGE
);
748 return (PK_ERR_USAGE
);
755 kfmt
= Str2Format(optarg_av
);
756 if (kfmt
== KMF_FORMAT_UNDEF
)
757 return (PK_ERR_USAGE
);
759 case 'I': /* output file name */
761 return (PK_ERR_USAGE
);
764 case 'o': /* output file name */
766 return (PK_ERR_USAGE
);
767 filename
= optarg_av
;
769 case 'c': /* input cert file name */
771 return (PK_ERR_USAGE
);
772 certfile
= optarg_av
;
774 case 'K': /* input key file name */
776 return (PK_ERR_USAGE
);
780 return (PK_ERR_USAGE
);
784 /* Assume keystore = PKCS#11 if not specified */
786 kstype
= KMF_KEYSTORE_PK11TOKEN
;
788 /* Filename arg is required. */
789 if (EMPTYSTRING(filename
)) {
790 cryptoerror(LOG_STDERR
, gettext("You must specify "
791 "an 'outfile' parameter when exporting.\n"));
792 return (PK_ERR_USAGE
);
795 /* No additional args allowed. */
799 return (PK_ERR_USAGE
);
801 DIR_OPTION_CHECK(kstype
, dir
);
803 /* if PUBLIC or PRIVATE obj was given, the old syntax was used. */
804 if ((oclass
& (PK_PUBLIC_OBJ
| PK_PRIVATE_OBJ
)) &&
805 kstype
!= KMF_KEYSTORE_PK11TOKEN
) {
807 (void) fprintf(stderr
, gettext("The objtype parameter "
808 "is only relevant if keystore=pkcs11\n"));
809 return (PK_ERR_USAGE
);
812 if (kstype
== KMF_KEYSTORE_PK11TOKEN
&& EMPTYSTRING(token_spec
))
813 token_spec
= PK_DEFAULT_PK11TOKEN
;
814 else if (kstype
== KMF_KEYSTORE_NSS
&& EMPTYSTRING(token_spec
))
815 token_spec
= DEFAULT_NSS_TOKEN
;
817 if (kstype
== KMF_KEYSTORE_OPENSSL
) {
818 if (kfmt
!= KMF_FORMAT_PKCS12
) {
819 cryptoerror(LOG_STDERR
, gettext("PKCS12 "
820 "is the only export format "
821 "supported for the 'file' "
823 return (PK_ERR_USAGE
);
825 if (EMPTYSTRING(keyfile
) || EMPTYSTRING(certfile
)) {
826 cryptoerror(LOG_STDERR
, gettext("A cert file"
827 "and a key file must be specified "
828 "when exporting to PKCS12 from the "
829 "'file' keystore.\n"));
830 return (PK_ERR_USAGE
);
834 /* Check if the file exists */
835 if (verify_file(filename
) != KMF_OK
) {
836 cryptoerror(LOG_STDERR
,
837 gettext("Warning: file \"%s\" exists, "
838 "will be overwritten."), filename
);
839 if (yesno(gettext("Continue with export? "),
840 gettext("Respond with yes or no.\n"), B_FALSE
) == B_FALSE
) {
843 /* remove the file */
844 (void) unlink(filename
);
848 if (serstr
!= NULL
) {
849 uchar_t
*bytes
= NULL
;
852 rv
= kmf_hexstr_to_bytes((uchar_t
*)serstr
, &bytes
, &bytelen
);
853 if (rv
!= KMF_OK
|| bytes
== NULL
) {
854 (void) fprintf(stderr
, gettext("serial number "
855 "must be specified as a hex number "
856 "(ex: 0x0102030405ffeeddee)\n"));
857 return (PK_ERR_USAGE
);
860 serial
.len
= bytelen
;
864 * We need a password in the following situations:
865 * 1. When accessing PKCS11 token
866 * 2. If NSS keystore, when making a PKCS12 file or when
867 * accessing any private object or key.
869 if (kstype
== KMF_KEYSTORE_PK11TOKEN
||
870 ((kstype
== KMF_KEYSTORE_NSS
) &&
871 ((oclass
& (PK_KEY_OBJ
| PK_PRIVATE_OBJ
)) ||
872 (kfmt
== KMF_FORMAT_PKCS12
)))) {
873 (void) get_token_password(kstype
, token_spec
,
877 if ((rv
= kmf_initialize(&kmfhandle
, NULL
, NULL
)) != KMF_OK
) {
878 cryptoerror(LOG_STDERR
, gettext("Error initializing "
879 "KMF: 0x%02x\n"), rv
);
884 case KMF_KEYSTORE_PK11TOKEN
:
885 if (kfmt
== KMF_FORMAT_PKCS12
)
886 rv
= pk_export_pk12_pk11(kmfhandle
,
887 token_spec
, certlabel
,
891 else if ((oclass
& PK_KEY_OBJ
) ||
892 kfmt
== KMF_FORMAT_RAWKEY
)
893 rv
= pk_export_pk11_keys(kmfhandle
,
894 token_spec
, &tokencred
, kfmt
,
895 certlabel
, filename
, oclass
);
897 rv
= pk_export_pk11_objects(kmfhandle
,
898 token_spec
, &tokencred
, certlabel
,
899 issuer
, subject
, &serial
, kfmt
,
902 case KMF_KEYSTORE_NSS
:
904 dir
= PK_DEFAULT_DIRECTORY
;
905 if (kfmt
== KMF_FORMAT_PKCS12
)
906 rv
= pk_export_pk12_nss(kmfhandle
,
907 token_spec
, dir
, prefix
,
910 &tokencred
, filename
);
912 rv
= pk_export_nss_objects(kmfhandle
,
914 oclass
, certlabel
, issuer
, subject
,
915 &serial
, kfmt
, dir
, prefix
, filename
);
917 case KMF_KEYSTORE_OPENSSL
:
918 if (kfmt
== KMF_FORMAT_PKCS12
)
919 rv
= pk_export_pk12_files(kmfhandle
,
920 certfile
, keyfile
, filename
);
922 rv
= pk_export_file_objects(kmfhandle
, oclass
,
923 issuer
, subject
, &serial
,
932 display_error(kmfhandle
, rv
,
933 gettext("Error exporting objects"));
936 if (serial
.val
!= NULL
)
939 (void) kmf_finalize(kmfhandle
);