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) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
30 #include <cryptoutil.h>
31 #include <security/cryptoki.h>
36 #define SET_VALUE(f, s) \
38 if (kmfrv != KMF_OK) { \
39 cryptoerror(LOG_STDERR, \
40 gettext("Failed to set %s: 0x%02x\n"), \
46 gencert_pkcs11(KMF_HANDLE_T kmfhandle
,
47 char *token
, char *subject
, char *altname
,
48 KMF_GENERALNAMECHOICES alttype
, int altcrit
,
49 char *certlabel
, KMF_KEY_ALG keyAlg
,
50 KMF_ALGORITHM_INDEX sigAlg
,
51 int keylen
, uint32_t ltime
, KMF_BIGINT
*serial
,
52 uint16_t kubits
, int kucrit
, KMF_CREDENTIAL
*tokencred
,
53 EKU_LIST
*ekulist
, KMF_OID
*curveoid
)
55 KMF_RETURN kmfrv
= KMF_OK
;
56 KMF_KEY_HANDLE pubk
, prik
;
57 KMF_X509_CERTIFICATE signedCert
;
58 KMF_X509_NAME certSubject
;
59 KMF_X509_NAME certIssuer
;
61 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_PK11TOKEN
;
62 KMF_ATTRIBUTE attrlist
[16];
67 (void) memset(&signedCert
, 0, sizeof (signedCert
));
68 (void) memset(&certSubject
, 0, sizeof (certSubject
));
69 (void) memset(&certIssuer
, 0, sizeof (certIssuer
));
70 (void) memset(&x509DER
, 0, sizeof (x509DER
));
72 /* If the subject name cannot be parsed, flag it now and exit */
73 if (kmf_dn_parser(subject
, &certSubject
) != KMF_OK
) {
74 cryptoerror(LOG_STDERR
,
75 gettext("Subject name cannot be parsed.\n"));
76 return (PK_ERR_USAGE
);
79 /* For a self-signed cert, the issuser and subject are the same */
80 if (kmf_dn_parser(subject
, &certIssuer
) != KMF_OK
) {
81 cryptoerror(LOG_STDERR
,
82 gettext("Subject name cannot be parsed.\n"));
83 return (PK_ERR_USAGE
);
86 keylength
= keylen
; /* bits */
89 /* Select a PKCS11 token */
90 kmfrv
= select_token(kmfhandle
, token
, FALSE
);
91 if (kmfrv
!= KMF_OK
) {
96 * Share the "genkeypair" routine for creating the keypair.
98 kmfrv
= genkeypair_pkcs11(kmfhandle
, token
, certlabel
,
99 keytype
, keylength
, tokencred
, curveoid
, &prik
, &pubk
);
103 SET_VALUE(kmf_set_cert_pubkey(kmfhandle
, &pubk
, &signedCert
),
106 SET_VALUE(kmf_set_cert_version(&signedCert
, 2), "version number");
108 SET_VALUE(kmf_set_cert_serial(&signedCert
, serial
),
111 SET_VALUE(kmf_set_cert_validity(&signedCert
, NULL
, ltime
),
114 SET_VALUE(kmf_set_cert_sig_alg(&signedCert
, sigAlg
),
115 "signature algorithm");
117 SET_VALUE(kmf_set_cert_subject(&signedCert
, &certSubject
),
120 SET_VALUE(kmf_set_cert_issuer(&signedCert
, &certIssuer
),
124 SET_VALUE(kmf_set_cert_subject_altname(&signedCert
, altcrit
,
125 alttype
, altname
), "subjectAltName");
128 SET_VALUE(kmf_set_cert_ku(&signedCert
, kucrit
, kubits
),
131 if (ekulist
!= NULL
) {
133 for (i
= 0; kmfrv
== KMF_OK
&& i
< ekulist
->eku_count
; i
++) {
134 SET_VALUE(kmf_add_cert_eku(&signedCert
,
135 &ekulist
->ekulist
[i
], ekulist
->critlist
[i
]),
136 "Extended Key Usage");
141 * Construct attributes for the kmf_sign_cert operation.
144 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
145 &kstype
, sizeof (kstype
));
148 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEY_HANDLE_ATTR
,
149 &prik
, sizeof (KMF_KEY_HANDLE_ATTR
));
152 /* cert data that is to be signed */
153 kmf_set_attr_at_index(attrlist
, numattr
, KMF_X509_CERTIFICATE_ATTR
,
154 &signedCert
, sizeof (KMF_X509_CERTIFICATE
));
157 /* output buffer for the signed cert */
158 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_DATA_ATTR
,
159 &x509DER
, sizeof (KMF_DATA
));
162 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ALGORITHM_INDEX_ATTR
,
163 &sigAlg
, sizeof (sigAlg
));
166 if ((kmfrv
= kmf_sign_cert(kmfhandle
, numattr
, attrlist
)) !=
172 * Store the cert in the DB.
175 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
176 &kstype
, sizeof (kstype
));
178 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_DATA_ATTR
,
179 &x509DER
, sizeof (KMF_DATA
));
182 if (certlabel
!= NULL
) {
183 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_LABEL_ATTR
,
184 certlabel
, strlen(certlabel
));
188 kmfrv
= kmf_store_cert(kmfhandle
, numattr
, attrlist
);
191 kmf_free_data(&x509DER
);
192 kmf_free_dn(&certSubject
);
193 kmf_free_dn(&certIssuer
);
196 * If kmf_sign_cert or kmf_store_cert failed, then we need to clean up
197 * the key pair from the token.
199 if (kmfrv
!= KMF_OK
) {
200 /* delete the public key */
202 kmf_set_attr_at_index(attrlist
, numattr
,
203 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
206 kmf_set_attr_at_index(attrlist
, numattr
,
207 KMF_KEY_HANDLE_ATTR
, &pubk
, sizeof (KMF_KEY_HANDLE
));
210 if (tokencred
!= NULL
&& tokencred
->cred
!= NULL
) {
211 kmf_set_attr_at_index(attrlist
, numattr
,
212 KMF_CREDENTIAL_ATTR
, tokencred
,
213 sizeof (KMF_CREDENTIAL
));
217 (void) kmf_delete_key_from_keystore(kmfhandle
, numattr
,
220 /* delete the private key */
222 kmf_set_attr_at_index(attrlist
, numattr
,
223 KMF_KEYSTORE_TYPE_ATTR
, &kstype
, sizeof (kstype
));
226 kmf_set_attr_at_index(attrlist
, numattr
,
227 KMF_KEY_HANDLE_ATTR
, &prik
, sizeof (KMF_KEY_HANDLE
));
230 if (tokencred
!= NULL
&& tokencred
->cred
!= NULL
) {
231 kmf_set_attr_at_index(attrlist
, numattr
,
232 KMF_CREDENTIAL_ATTR
, tokencred
,
233 sizeof (KMF_CREDENTIAL
));
237 (void) kmf_delete_key_from_keystore(kmfhandle
, numattr
,
245 gencert_file(KMF_HANDLE_T kmfhandle
,
246 KMF_KEY_ALG keyAlg
, KMF_ALGORITHM_INDEX sigAlg
,
247 int keylen
, KMF_ENCODE_FORMAT fmt
,
248 uint32_t ltime
, char *subject
, char *altname
,
249 KMF_GENERALNAMECHOICES alttype
, int altcrit
,
250 KMF_BIGINT
*serial
, uint16_t kubits
, int kucrit
,
251 char *outcert
, char *outkey
,
255 KMF_KEY_HANDLE pubk
, prik
;
256 KMF_X509_CERTIFICATE signedCert
;
257 KMF_X509_NAME certSubject
;
258 KMF_X509_NAME certIssuer
;
260 char *fullcertpath
= NULL
;
261 char *fullkeypath
= NULL
;
262 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_OPENSSL
;
263 KMF_ATTRIBUTE attrlist
[10];
266 (void) memset(&signedCert
, 0, sizeof (signedCert
));
267 (void) memset(&certSubject
, 0, sizeof (certSubject
));
268 (void) memset(&certIssuer
, 0, sizeof (certIssuer
));
269 (void) memset(&x509DER
, 0, sizeof (x509DER
));
271 if (EMPTYSTRING(outcert
) || EMPTYSTRING(outkey
)) {
272 cryptoerror(LOG_STDERR
,
273 gettext("No output file was specified for "
274 "the cert or key\n"));
275 return (PK_ERR_USAGE
);
277 fullcertpath
= strdup(outcert
);
278 if (verify_file(fullcertpath
)) {
279 cryptoerror(LOG_STDERR
,
280 gettext("Cannot write the indicated output "
281 "certificate file (%s).\n"), fullcertpath
);
283 return (PK_ERR_USAGE
);
286 /* If the subject name cannot be parsed, flag it now and exit */
287 if (kmf_dn_parser(subject
, &certSubject
) != KMF_OK
) {
288 cryptoerror(LOG_STDERR
,
289 gettext("Subject name cannot be parsed (%s)\n"), subject
);
290 return (PK_ERR_USAGE
);
293 /* For a self-signed cert, the issuser and subject are the same */
294 if (kmf_dn_parser(subject
, &certIssuer
) != KMF_OK
) {
295 cryptoerror(LOG_STDERR
,
296 gettext("Subject name cannot be parsed (%s)\n"), subject
);
297 kmf_free_dn(&certSubject
);
298 return (PK_ERR_USAGE
);
302 * Share the "genkeypair" routine for creating the keypair.
304 kmfrv
= genkeypair_file(kmfhandle
, keyAlg
, keylen
,
305 fmt
, outkey
, &prik
, &pubk
);
309 SET_VALUE(kmf_set_cert_pubkey(kmfhandle
, &pubk
, &signedCert
),
312 SET_VALUE(kmf_set_cert_version(&signedCert
, 2), "version number");
314 SET_VALUE(kmf_set_cert_serial(&signedCert
, serial
),
317 SET_VALUE(kmf_set_cert_validity(&signedCert
, NULL
, ltime
),
320 SET_VALUE(kmf_set_cert_sig_alg(&signedCert
, sigAlg
),
321 "signature algorithm");
323 SET_VALUE(kmf_set_cert_subject(&signedCert
, &certSubject
),
326 SET_VALUE(kmf_set_cert_issuer(&signedCert
, &certIssuer
),
330 SET_VALUE(kmf_set_cert_subject_altname(&signedCert
, altcrit
,
331 alttype
, altname
), "subjectAltName");
334 SET_VALUE(kmf_set_cert_ku(&signedCert
, kucrit
, kubits
),
337 if (ekulist
!= NULL
) {
339 for (i
= 0; kmfrv
== KMF_OK
&& i
< ekulist
->eku_count
; i
++) {
340 SET_VALUE(kmf_add_cert_eku(&signedCert
,
341 &ekulist
->ekulist
[i
],
342 ekulist
->critlist
[i
]), "Extended Key Usage");
346 * Construct attributes for the kmf_sign_cert operation.
349 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
350 &kstype
, sizeof (kstype
));
353 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEY_HANDLE_ATTR
,
354 &prik
, sizeof (KMF_KEY_HANDLE_ATTR
));
357 /* cert data that is to be signed */
358 kmf_set_attr_at_index(attrlist
, numattr
, KMF_X509_CERTIFICATE_ATTR
,
359 &signedCert
, sizeof (KMF_X509_CERTIFICATE
));
362 /* output buffer for the signed cert */
363 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_DATA_ATTR
,
364 &x509DER
, sizeof (KMF_DATA
));
367 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ALGORITHM_INDEX_ATTR
,
368 &sigAlg
, sizeof (sigAlg
));
371 if ((kmfrv
= kmf_sign_cert(kmfhandle
, numattr
, attrlist
)) !=
377 * Store the cert in the DB.
380 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
381 &kstype
, sizeof (kstype
));
383 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_DATA_ATTR
,
384 &x509DER
, sizeof (KMF_DATA
));
386 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_FILENAME_ATTR
,
387 fullcertpath
, strlen(fullcertpath
));
389 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ENCODE_FORMAT_ATTR
,
393 kmfrv
= kmf_store_cert(kmfhandle
, numattr
, attrlist
);
396 if (fullkeypath
!= NULL
)
398 if (fullcertpath
!= NULL
)
401 kmf_free_data(&x509DER
);
402 kmf_free_dn(&certSubject
);
403 kmf_free_dn(&certIssuer
);
408 gencert_nss(KMF_HANDLE_T kmfhandle
,
409 char *token
, char *subject
, char *altname
,
410 KMF_GENERALNAMECHOICES alttype
, int altcrit
,
411 char *nickname
, char *dir
, char *prefix
,
413 KMF_ALGORITHM_INDEX sigAlg
,
414 int keylen
, char *trust
,
415 uint32_t ltime
, KMF_BIGINT
*serial
, uint16_t kubits
,
416 int kucrit
, KMF_CREDENTIAL
*tokencred
,
417 EKU_LIST
*ekulist
, KMF_OID
*curveoid
)
420 KMF_KEY_HANDLE pubk
, prik
;
421 KMF_X509_CERTIFICATE signedCert
;
422 KMF_X509_NAME certSubject
;
423 KMF_X509_NAME certIssuer
;
425 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_NSS
;
426 KMF_ATTRIBUTE attrlist
[16];
430 token
= DEFAULT_NSS_TOKEN
;
432 kmfrv
= configure_nss(kmfhandle
, dir
, prefix
);
436 (void) memset(&signedCert
, 0, sizeof (signedCert
));
437 (void) memset(&certSubject
, 0, sizeof (certSubject
));
438 (void) memset(&certIssuer
, 0, sizeof (certIssuer
));
439 (void) memset(&x509DER
, 0, sizeof (x509DER
));
441 /* If the subject name cannot be parsed, flag it now and exit */
442 if (kmf_dn_parser(subject
, &certSubject
) != KMF_OK
) {
443 cryptoerror(LOG_STDERR
,
444 gettext("Subject name cannot be parsed.\n"));
445 return (PK_ERR_USAGE
);
448 /* For a self-signed cert, the issuser and subject are the same */
449 if (kmf_dn_parser(subject
, &certIssuer
) != KMF_OK
) {
450 cryptoerror(LOG_STDERR
,
451 gettext("Subject name cannot be parsed.\n"));
452 return (PK_ERR_USAGE
);
455 kmfrv
= genkeypair_nss(kmfhandle
, token
, nickname
, dir
,
456 prefix
, keyAlg
, keylen
, tokencred
, curveoid
,
461 SET_VALUE(kmf_set_cert_pubkey(kmfhandle
, &pubk
, &signedCert
),
464 SET_VALUE(kmf_set_cert_version(&signedCert
, 2), "version number");
466 SET_VALUE(kmf_set_cert_serial(&signedCert
, serial
),
469 SET_VALUE(kmf_set_cert_validity(&signedCert
, NULL
, ltime
),
472 SET_VALUE(kmf_set_cert_sig_alg(&signedCert
, sigAlg
),
473 "signature algorithm");
475 SET_VALUE(kmf_set_cert_subject(&signedCert
, &certSubject
),
478 SET_VALUE(kmf_set_cert_issuer(&signedCert
, &certIssuer
),
482 SET_VALUE(kmf_set_cert_subject_altname(&signedCert
, altcrit
,
483 alttype
, altname
), "subjectAltName");
486 SET_VALUE(kmf_set_cert_ku(&signedCert
, kucrit
, kubits
),
489 if (ekulist
!= NULL
) {
491 for (i
= 0; kmfrv
== KMF_OK
&& i
< ekulist
->eku_count
; i
++) {
492 SET_VALUE(kmf_add_cert_eku(&signedCert
,
493 &ekulist
->ekulist
[i
],
494 ekulist
->critlist
[i
]), "Extended Key Usage");
498 * Construct attributes for the kmf_sign_cert operation.
501 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
502 &kstype
, sizeof (kstype
));
505 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEY_HANDLE_ATTR
,
506 &prik
, sizeof (KMF_KEY_HANDLE_ATTR
));
509 /* cert data that is to be signed */
510 kmf_set_attr_at_index(attrlist
, numattr
, KMF_X509_CERTIFICATE_ATTR
,
511 &signedCert
, sizeof (KMF_X509_CERTIFICATE
));
514 /* output buffer for the signed cert */
515 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_DATA_ATTR
,
516 &x509DER
, sizeof (KMF_DATA
));
519 kmf_set_attr_at_index(attrlist
, numattr
, KMF_ALGORITHM_INDEX_ATTR
,
520 &sigAlg
, sizeof (sigAlg
));
523 if ((kmfrv
= kmf_sign_cert(kmfhandle
, numattr
, attrlist
)) !=
529 * Store the cert in the DB.
532 kmf_set_attr_at_index(attrlist
, numattr
, KMF_KEYSTORE_TYPE_ATTR
,
533 &kstype
, sizeof (kstype
));
536 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_DATA_ATTR
,
537 &x509DER
, sizeof (KMF_DATA
));
540 if (nickname
!= NULL
) {
541 kmf_set_attr_at_index(attrlist
, numattr
, KMF_CERT_LABEL_ATTR
,
542 nickname
, strlen(nickname
));
547 kmf_set_attr_at_index(attrlist
, numattr
, KMF_TRUSTFLAG_ATTR
,
548 trust
, strlen(trust
));
553 kmf_set_attr_at_index(attrlist
, numattr
, KMF_TOKEN_LABEL_ATTR
,
554 token
, strlen(token
));
558 kmfrv
= kmf_store_cert(kmfhandle
, numattr
, attrlist
);
561 kmf_free_data(&x509DER
);
562 kmf_free_dn(&certSubject
);
563 kmf_free_dn(&certIssuer
);
568 pk_gencert(int argc
, char *argv
[])
572 extern int optind_av
;
573 extern char *optarg_av
;
574 KMF_KEYSTORE_TYPE kstype
= 0;
575 char *subject
= NULL
;
576 char *tokenname
= NULL
;
579 char *keytype
= PK_DEFAULT_KEYTYPE
;
580 int keylen
= PK_DEFAULT_KEYLENGTH
;
582 char *lifetime
= NULL
;
583 char *certlabel
= NULL
;
584 char *outcert
= NULL
;
588 char *altname
= NULL
;
589 char *keyusagestr
= NULL
;
591 char *hashname
= NULL
;
592 KMF_GENERALNAMECHOICES alttype
= 0;
593 KMF_BIGINT serial
= { NULL
, 0 };
595 KMF_HANDLE_T kmfhandle
= NULL
;
596 KMF_ENCODE_FORMAT fmt
= KMF_FORMAT_ASN1
;
597 KMF_KEY_ALG keyAlg
= KMF_RSA
;
598 KMF_ALGORITHM_INDEX sigAlg
= KMF_ALGID_SHA1WithRSA
;
599 boolean_t interactive
= B_FALSE
;
600 char *subname
= NULL
;
601 KMF_CREDENTIAL tokencred
= { NULL
, 0 };
603 int altcrit
= 0, kucrit
= 0;
604 EKU_LIST
*ekulist
= NULL
;
605 KMF_OID
*curveoid
= NULL
; /* ECC */
606 KMF_OID
*hashoid
= NULL
;
609 while ((opt
= getopt_av(argc
, argv
,
610 "ik:(keystore)s:(subject)n:(nickname)A:(altname)"
611 "T:(token)d:(dir)p:(prefix)t:(keytype)y:(keylen)"
612 "r:(trust)L:(lifetime)l:(label)c:(outcert)e:(eku)"
613 "K:(outkey)S:(serial)F:(format)u:(keyusage)C:(curve)"
614 "E(listcurves)h:(hash)")) != EOF
) {
616 if (opt
!= 'i' && opt
!= 'E' && EMPTYSTRING(optarg_av
))
617 return (PK_ERR_USAGE
);
624 if (interactive
|| subject
)
625 return (PK_ERR_USAGE
);
627 interactive
= B_TRUE
;
630 kstype
= KS2Int(optarg_av
);
632 return (PK_ERR_USAGE
);
635 if (interactive
|| subject
)
636 return (PK_ERR_USAGE
);
643 return (PK_ERR_USAGE
);
644 certlabel
= optarg_av
;
648 return (PK_ERR_USAGE
);
649 tokenname
= optarg_av
;
653 return (PK_ERR_USAGE
);
658 return (PK_ERR_USAGE
);
665 keyusagestr
= optarg_av
;
668 if (sscanf(optarg_av
, "%d",
670 cryptoerror(LOG_STDERR
,
671 gettext("key length must be"
672 "a numeric value (%s)\n"),
674 return (PK_ERR_USAGE
);
680 return (PK_ERR_USAGE
);
685 return (PK_ERR_USAGE
);
686 lifetime
= optarg_av
;
690 return (PK_ERR_USAGE
);
695 return (PK_ERR_USAGE
);
703 return (PK_ERR_USAGE
);
710 curveoid
= ecc_name_to_oid(optarg_av
);
711 if (curveoid
== NULL
) {
712 cryptoerror(LOG_STDERR
,
713 gettext("Unrecognized ECC "
715 return (PK_ERR_USAGE
);
720 * This argument is only to be used
721 * by itself, no other options should
725 cryptoerror(LOG_STDERR
,
726 gettext("listcurves has no other "
728 return (PK_ERR_USAGE
);
733 hashname
= optarg_av
;
734 hashoid
= ecc_name_to_oid(optarg_av
);
735 if (hashoid
== NULL
) {
736 cryptoerror(LOG_STDERR
,
737 gettext("Unrecognized hash.\n"));
738 return (PK_ERR_USAGE
);
742 return (PK_ERR_USAGE
);
746 /* No additional args allowed. */
750 return (PK_ERR_USAGE
);
753 if ((rv
= kmf_initialize(&kmfhandle
, NULL
, NULL
)) != KMF_OK
) {
754 cryptoerror(LOG_STDERR
, gettext("Error initializing KMF\n"));
755 return (PK_ERR_USAGE
);
758 /* Assume keystore = PKCS#11 if not specified. */
760 kstype
= KMF_KEYSTORE_PK11TOKEN
;
762 if ((kstype
== KMF_KEYSTORE_NSS
|| kstype
== KMF_KEYSTORE_PK11TOKEN
)) {
763 if (interactive
&& EMPTYSTRING(certlabel
)) {
764 (void) get_certlabel(&certlabel
);
766 /* It better not be empty now */
767 if (EMPTYSTRING(certlabel
)) {
768 cryptoerror(LOG_STDERR
, gettext("A label must be "
769 "specified to create a self-signed certificate."
771 return (PK_ERR_USAGE
);
773 } else if (kstype
== KMF_KEYSTORE_OPENSSL
&& EMPTYSTRING(outcert
)) {
774 cryptoerror(LOG_STDERR
, gettext("A certificate filename must "
775 "be specified to create a self-signed certificate.\n"));
776 return (PK_ERR_USAGE
);
779 DIR_OPTION_CHECK(kstype
, dir
);
781 if (format
&& (fmt
= Str2Format(format
)) == KMF_FORMAT_UNDEF
) {
782 cryptoerror(LOG_STDERR
,
783 gettext("Error parsing format string (%s).\n"),
785 return (PK_ERR_USAGE
);
788 if (Str2Lifetime(lifetime
, <ime
) != 0) {
789 cryptoerror(LOG_STDERR
,
790 gettext("Error parsing lifetime string\n"));
791 return (PK_ERR_USAGE
);
794 if (Str2KeyType(keytype
, hashoid
, &keyAlg
, &sigAlg
) != 0) {
795 cryptoerror(LOG_STDERR
,
796 gettext("Unsupported key/hash combination (%s/%s).\n"),
797 keytype
, (hashname
? hashname
: "none"));
798 return (PK_ERR_USAGE
);
800 if (curveoid
!= NULL
&& keyAlg
!= KMF_ECDSA
) {
801 cryptoerror(LOG_STDERR
, gettext("EC curves are only "
802 "valid for EC keytypes.\n"));
803 return (PK_ERR_USAGE
);
805 if (keyAlg
== KMF_ECDSA
&& curveoid
== NULL
) {
806 cryptoerror(LOG_STDERR
, gettext("A curve must be "
807 "specifed when using EC keys.\n"));
808 return (PK_ERR_USAGE
);
810 /* Adjust default keylength for NSS and DSA */
811 if (keyAlg
== KMF_DSA
&& !y_flag
&& kstype
== KMF_KEYSTORE_NSS
)
815 * Check the subject name.
816 * If interactive is true, get it now interactively.
820 if (get_subname(&subname
) != KMF_OK
|| subname
== NULL
) {
821 cryptoerror(LOG_STDERR
, gettext("Failed to get the "
822 "subject name interactively.\n"));
823 return (PK_ERR_USAGE
);
825 if (serstr
== NULL
) {
826 (void) get_serial(&serstr
);
829 if (EMPTYSTRING(subject
)) {
830 cryptoerror(LOG_STDERR
, gettext("A subject name or "
831 "-i must be specified to create a self-signed "
833 return (PK_ERR_USAGE
);
835 subname
= strdup(subject
);
836 if (subname
== NULL
) {
837 cryptoerror(LOG_STDERR
,
838 gettext("Out of memory.\n"));
839 return (PK_ERR_SYSTEM
);
844 if (serstr
== NULL
) {
845 (void) fprintf(stderr
, gettext("A serial number "
846 "must be specified as a hex number when creating"
847 " a self-signed certificate "
848 "(ex: serial=0x0102030405feedface)\n"));
852 uchar_t
*bytes
= NULL
;
855 rv
= kmf_hexstr_to_bytes((uchar_t
*)serstr
, &bytes
, &bytelen
);
856 if (rv
!= KMF_OK
|| bytes
== NULL
) {
857 (void) fprintf(stderr
, gettext("serial number "
858 "must be specified as a hex number "
859 "(ex: 0x0102030405ffeeddee)\n"));
864 serial
.len
= bytelen
;
867 if (altname
!= NULL
) {
868 rv
= verify_altname(altname
, &alttype
, &altcrit
);
870 (void) fprintf(stderr
, gettext("Subject AltName "
871 "must be specified as a name=value pair. "
872 "See the man page for details.\n"));
876 /* advance the altname past the '=' sign */
877 char *p
= strchr(altname
, '=');
883 if (keyusagestr
!= NULL
) {
884 rv
= verify_keyusage(keyusagestr
, &kubits
, &kucrit
);
886 (void) fprintf(stderr
, gettext("KeyUsage "
887 "must be specified as a comma-separated list. "
888 "See the man page for details.\n"));
893 if (ekustr
!= NULL
) {
894 rv
= verify_ekunames(ekustr
, &ekulist
);
896 (void) fprintf(stderr
, gettext("EKUs must "
897 "be specified as a comma-separated list. "
898 "See the man page for details.\n"));
903 if (keyAlg
== KMF_ECDSA
&& kstype
== KMF_KEYSTORE_OPENSSL
) {
904 (void) fprintf(stderr
, gettext("ECC certificates are"
905 "only supported with the pkcs11 and nss keystores\n"));
910 if (kstype
== KMF_KEYSTORE_NSS
|| kstype
== KMF_KEYSTORE_PK11TOKEN
) {
911 if (tokenname
== NULL
|| !strlen(tokenname
)) {
912 if (kstype
== KMF_KEYSTORE_NSS
) {
913 tokenname
= "internal";
915 tokenname
= PK_DEFAULT_PK11TOKEN
;
919 (void) get_token_password(kstype
, tokenname
, &tokencred
);
922 if (kstype
== KMF_KEYSTORE_NSS
) {
924 dir
= PK_DEFAULT_DIRECTORY
;
926 rv
= gencert_nss(kmfhandle
,
927 tokenname
, subname
, altname
, alttype
, altcrit
,
928 certlabel
, dir
, prefix
, keyAlg
, sigAlg
, keylen
,
929 trust
, ltime
, &serial
, kubits
, kucrit
, &tokencred
,
932 } else if (kstype
== KMF_KEYSTORE_PK11TOKEN
) {
933 rv
= gencert_pkcs11(kmfhandle
,
934 tokenname
, subname
, altname
, alttype
, altcrit
,
935 certlabel
, keyAlg
, sigAlg
, keylen
, ltime
,
936 &serial
, kubits
, kucrit
, &tokencred
, ekulist
,
939 } else if (kstype
== KMF_KEYSTORE_OPENSSL
) {
940 rv
= gencert_file(kmfhandle
,
941 keyAlg
, sigAlg
, keylen
, fmt
,
942 ltime
, subname
, altname
, alttype
, altcrit
,
943 &serial
, kubits
, kucrit
, outcert
, outkey
,
948 display_error(kmfhandle
, rv
,
949 gettext("Error creating certificate and keypair"));
952 free_eku_list(ekulist
);
955 if (tokencred
.cred
!= NULL
)
956 free(tokencred
.cred
);
958 if (serial
.val
!= NULL
)
961 (void) kmf_finalize(kmfhandle
);