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 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
32 #include <cryptoutil.h>
33 #include <security/cryptoki.h>
38 #define SET_VALUE(f, s) \
40 if (kmfrv != KMF_OK) { \
41 cryptoerror(LOG_STDERR, \
42 gettext("Failed to set %s: 0x%02x\n"), \
48 genkeypair_pkcs11(KMF_HANDLE_T kmfhandle
,
49 char *token
, char *keylabel
, KMF_KEY_ALG keyAlg
,
50 int keylen
, KMF_CREDENTIAL
*tokencred
, KMF_OID
*curveoid
,
51 KMF_KEY_HANDLE
*outPriKey
, KMF_KEY_HANDLE
*outPubKey
)
53 KMF_RETURN kmfrv
= KMF_OK
;
54 KMF_KEY_HANDLE pubk
, prik
;
55 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_PK11TOKEN
;
56 KMF_ATTRIBUTE attrlist
[16];
61 keylength
= keylen
; /* bits */
64 /* Select a PKCS11 token */
65 kmfrv
= select_token(kmfhandle
, token
, FALSE
);
69 kmf_set_attr_at_index(attrlist
, numattr
,
70 KMF_KEYSTORE_TYPE_ATTR
, &kstype
,
74 kmf_set_attr_at_index(attrlist
, numattr
,
75 KMF_KEYALG_ATTR
, &keytype
,
79 kmf_set_attr_at_index(attrlist
, numattr
,
80 KMF_KEYLENGTH_ATTR
, &keylength
,
84 if (keylabel
!= NULL
) {
85 kmf_set_attr_at_index(attrlist
, numattr
,
86 KMF_KEYLABEL_ATTR
, keylabel
,
91 if (tokencred
!= NULL
&& tokencred
->cred
!= NULL
) {
92 kmf_set_attr_at_index(attrlist
, numattr
,
93 KMF_CREDENTIAL_ATTR
, tokencred
,
94 sizeof (KMF_CREDENTIAL
));
98 kmf_set_attr_at_index(attrlist
, numattr
,
99 KMF_PRIVKEY_HANDLE_ATTR
, &prik
,
100 sizeof (KMF_KEY_HANDLE
));
103 kmf_set_attr_at_index(attrlist
, numattr
,
104 KMF_PUBKEY_HANDLE_ATTR
, &pubk
,
105 sizeof (KMF_KEY_HANDLE
));
108 if (keytype
== KMF_ECDSA
&& curveoid
!= NULL
) {
109 kmf_set_attr_at_index(attrlist
, numattr
,
110 KMF_ECC_CURVE_OID_ATTR
, curveoid
,
115 kmfrv
= kmf_create_keypair(kmfhandle
, numattr
, attrlist
);
116 if (kmfrv
!= KMF_OK
) {
121 if (kmfrv
== KMF_OK
) {
122 if (outPriKey
!= NULL
)
124 if (outPubKey
!= NULL
)
132 genkeypair_file(KMF_HANDLE_T kmfhandle
,
133 KMF_KEY_ALG keyAlg
, int keylen
, KMF_ENCODE_FORMAT fmt
,
135 KMF_KEY_HANDLE
*outPriKey
, KMF_KEY_HANDLE
*outPubKey
)
138 KMF_KEY_HANDLE pubk
, prik
;
139 char *fullkeypath
= NULL
;
140 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_OPENSSL
;
141 KMF_ATTRIBUTE attrlist
[10];
145 KMF_ENCODE_FORMAT format
;
147 if (EMPTYSTRING(outkey
)) {
148 cryptoerror(LOG_STDERR
,
149 gettext("No output file was specified for "
151 return (PK_ERR_USAGE
);
154 fullkeypath
= strdup(outkey
);
155 if (verify_file(fullkeypath
)) {
156 cryptoerror(LOG_STDERR
,
157 gettext("Cannot write the indicated output "
158 "key file (%s).\n"), fullkeypath
);
160 return (PK_ERR_USAGE
);
163 keylength
= keylen
; /* bits */
167 kmf_set_attr_at_index(attrlist
, numattr
,
168 KMF_KEYSTORE_TYPE_ATTR
, &kstype
,
172 kmf_set_attr_at_index(attrlist
, numattr
,
173 KMF_KEYALG_ATTR
, &keytype
,
177 kmf_set_attr_at_index(attrlist
, numattr
,
178 KMF_KEYLENGTH_ATTR
, &keylength
,
182 if (fullkeypath
!= NULL
) {
183 kmf_set_attr_at_index(attrlist
, numattr
,
184 KMF_KEY_FILENAME_ATTR
, fullkeypath
,
185 strlen(fullkeypath
));
189 kmf_set_attr_at_index(attrlist
, numattr
,
190 KMF_ENCODE_FORMAT_ATTR
, &format
,
194 kmf_set_attr_at_index(attrlist
, numattr
,
195 KMF_PRIVKEY_HANDLE_ATTR
, &prik
,
196 sizeof (KMF_KEY_HANDLE
));
199 kmf_set_attr_at_index(attrlist
, numattr
,
200 KMF_PUBKEY_HANDLE_ATTR
, &pubk
,
201 sizeof (KMF_KEY_HANDLE
));
204 kmfrv
= kmf_create_keypair(kmfhandle
, numattr
, attrlist
);
205 if (kmfrv
!= KMF_OK
) {
212 if (kmfrv
== KMF_OK
) {
213 if (outPriKey
!= NULL
)
215 if (outPubKey
!= NULL
)
223 genkeypair_nss(KMF_HANDLE_T kmfhandle
,
225 char *nickname
, char *dir
, char *prefix
,
227 int keylen
, KMF_CREDENTIAL
*tokencred
,
229 KMF_KEY_HANDLE
*outPriKey
, KMF_KEY_HANDLE
*outPubKey
)
232 KMF_KEY_HANDLE pubk
, prik
;
233 KMF_KEYSTORE_TYPE kstype
= KMF_KEYSTORE_NSS
;
234 KMF_ATTRIBUTE attrlist
[16];
240 token
= DEFAULT_NSS_TOKEN
;
242 kmfrv
= configure_nss(kmfhandle
, dir
, prefix
);
246 keylength
= keylen
; /* bits */
249 kmf_set_attr_at_index(attrlist
, numattr
,
250 KMF_KEYSTORE_TYPE_ATTR
, &kstype
,
254 kmf_set_attr_at_index(attrlist
, numattr
,
255 KMF_KEYALG_ATTR
, &keytype
,
259 kmf_set_attr_at_index(attrlist
, numattr
,
260 KMF_KEYLENGTH_ATTR
, &keylength
,
264 if (nickname
!= NULL
) {
265 kmf_set_attr_at_index(attrlist
, numattr
,
266 KMF_KEYLABEL_ATTR
, nickname
,
271 if (tokencred
!= NULL
&& tokencred
->cred
!= NULL
) {
272 kmf_set_attr_at_index(attrlist
, numattr
,
273 KMF_CREDENTIAL_ATTR
, tokencred
,
274 sizeof (KMF_CREDENTIAL
));
279 kmf_set_attr_at_index(attrlist
, numattr
,
280 KMF_TOKEN_LABEL_ATTR
, token
,
285 kmf_set_attr_at_index(attrlist
, numattr
,
286 KMF_PRIVKEY_HANDLE_ATTR
, &prik
,
287 sizeof (KMF_KEY_HANDLE
));
290 kmf_set_attr_at_index(attrlist
, numattr
,
291 KMF_PUBKEY_HANDLE_ATTR
, &pubk
,
292 sizeof (KMF_KEY_HANDLE
));
295 if (keytype
== KMF_ECDSA
&& curveoid
!= NULL
) {
296 kmf_set_attr_at_index(attrlist
, numattr
,
297 KMF_ECC_CURVE_OID_ATTR
, curveoid
,
302 kmfrv
= kmf_create_keypair(kmfhandle
, numattr
, attrlist
);
303 if (kmfrv
!= KMF_OK
) {
307 if (kmfrv
== KMF_OK
) {
308 if (outPriKey
!= NULL
)
310 if (outPubKey
!= NULL
)
317 pk_genkeypair(int argc
, char *argv
[])
321 extern int optind_av
;
322 extern char *optarg_av
;
323 KMF_KEYSTORE_TYPE kstype
= 0;
324 char *tokenname
= NULL
;
327 char *keytype
= PK_DEFAULT_KEYTYPE
;
328 int keylen
= PK_DEFAULT_KEYLENGTH
;
332 KMF_HANDLE_T kmfhandle
= NULL
;
333 KMF_ENCODE_FORMAT fmt
= KMF_FORMAT_ASN1
;
334 KMF_KEY_ALG keyAlg
= KMF_RSA
;
335 KMF_ALGORITHM_INDEX sigAlg
;
336 KMF_CREDENTIAL tokencred
= { NULL
, 0 };
337 KMF_OID
*curveoid
= NULL
; /* ECC */
340 while ((opt
= getopt_av(argc
, argv
,
341 "k:(keystore)s:(subject)n:(nickname)"
342 "T:(token)d:(dir)p:(prefix)t:(keytype)y:(keylen)"
343 "l:(label)K:(outkey)F:(format)C:(curve)"
344 "E(listcurves)")) != EOF
) {
346 if (opt
!= 'i' && opt
!= 'E' && EMPTYSTRING(optarg_av
))
347 return (PK_ERR_USAGE
);
351 kstype
= KS2Int(optarg_av
);
353 return (PK_ERR_USAGE
);
358 return (PK_ERR_USAGE
);
363 return (PK_ERR_USAGE
);
364 tokenname
= optarg_av
;
368 return (PK_ERR_USAGE
);
373 return (PK_ERR_USAGE
);
380 if (sscanf(optarg_av
, "%d",
382 cryptoerror(LOG_STDERR
,
383 gettext("key length must be"
384 "a numeric value (%s)\n"),
386 return (PK_ERR_USAGE
);
392 return (PK_ERR_USAGE
);
397 return (PK_ERR_USAGE
);
401 curveoid
= ecc_name_to_oid(optarg_av
);
402 if (curveoid
== NULL
) {
403 cryptoerror(LOG_STDERR
,
405 "Unrecognized ECC curve.\n"));
406 return (PK_ERR_USAGE
);
413 return (PK_ERR_USAGE
);
417 /* No additional args allowed. */
421 return (PK_ERR_USAGE
);
424 if ((rv
= kmf_initialize(&kmfhandle
, NULL
, NULL
)) != KMF_OK
) {
425 cryptoerror(LOG_STDERR
, gettext("Error initializing KMF\n"));
426 return (PK_ERR_USAGE
);
429 /* Assume keystore = PKCS#11 if not specified. */
431 kstype
= KMF_KEYSTORE_PK11TOKEN
;
433 DIR_OPTION_CHECK(kstype
, dir
);
435 if (format
&& (fmt
= Str2Format(format
)) == KMF_FORMAT_UNDEF
) {
436 cryptoerror(LOG_STDERR
,
437 gettext("Error parsing format string (%s).\n"),
439 return (PK_ERR_USAGE
);
442 if (Str2KeyType(keytype
, NULL
, &keyAlg
, &sigAlg
) != 0) {
443 cryptoerror(LOG_STDERR
, gettext("Unrecognized keytype (%s).\n"),
445 return (PK_ERR_USAGE
);
447 if (curveoid
!= NULL
&& keyAlg
!= KMF_ECDSA
) {
448 cryptoerror(LOG_STDERR
, gettext("EC curves are only "
449 "valid for EC keytypes.\n"));
450 return (PK_ERR_USAGE
);
452 if (keyAlg
== KMF_ECDSA
&& curveoid
== NULL
) {
453 cryptoerror(LOG_STDERR
, gettext("A curve must be "
454 "specifed when using EC keys.\n"));
455 return (PK_ERR_USAGE
);
457 if (keyAlg
== KMF_ECDSA
&& kstype
== KMF_KEYSTORE_OPENSSL
) {
458 (void) fprintf(stderr
, gettext("ECC certificates are"
459 "only supported with the pkcs11 and nss keystores\n"));
463 /* Adjust default keylength for NSS and DSA */
464 if (keyAlg
== KMF_DSA
&& kstype
== KMF_KEYSTORE_NSS
) {
465 /* NSS only allows for 512-1024 bit DSA keys */
467 /* If nothing was given, default to 1024 */
469 else if (keylen
> 1024 || keylen
< 512) {
470 (void) fprintf(stderr
, gettext("NSS keystore only "
471 "supports DSA keylengths of 512 - 1024 bits\n"));
477 if (kstype
== KMF_KEYSTORE_NSS
|| kstype
== KMF_KEYSTORE_PK11TOKEN
) {
479 (void) fprintf(stderr
,
480 gettext("No key label specified\n"));
484 if (tokenname
== NULL
|| !strlen(tokenname
)) {
485 if (kstype
== KMF_KEYSTORE_NSS
) {
486 tokenname
= "internal";
488 tokenname
= PK_DEFAULT_PK11TOKEN
;
492 (void) get_token_password(kstype
, tokenname
, &tokencred
);
495 if (kstype
== KMF_KEYSTORE_NSS
) {
497 dir
= PK_DEFAULT_DIRECTORY
;
499 rv
= genkeypair_nss(kmfhandle
,
500 tokenname
, label
, dir
, prefix
, keyAlg
, keylen
,
501 &tokencred
, curveoid
, NULL
, NULL
);
503 } else if (kstype
== KMF_KEYSTORE_PK11TOKEN
) {
504 rv
= genkeypair_pkcs11(kmfhandle
,
505 tokenname
, label
, keyAlg
, keylen
,
506 &tokencred
, curveoid
, NULL
, NULL
);
508 } else if (kstype
== KMF_KEYSTORE_OPENSSL
) {
509 rv
= genkeypair_file(kmfhandle
, keyAlg
, keylen
,
510 fmt
, outkey
, NULL
, NULL
);
514 display_error(kmfhandle
, rv
,
515 gettext("Error creating and keypair"));
517 free(tokencred
.cred
);
519 (void) kmf_finalize(kmfhandle
);