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.
27 * Administration for metaslot
29 * All the "list" operations will call functions in libpkcs11.so
30 * Normally, it doesn't make sense to call functions in libpkcs11.so directly
31 * because libpkcs11.so depends on the configuration file (pkcs11.conf) the
32 * cryptoadm command is trying to administer. However, since metaslot
33 * is part of the framework, it is not possible to get information about
34 * it without actually calling functions in libpkcs11.so.
36 * So, for the listing operation, which won't modify the value of pkcs11.conf
37 * it is safe to call libpkcs11.so.
39 * For other operations that modifies the pkcs11.conf file, libpkcs11.so
44 #include <cryptoutil.h>
50 #include <security/cryptoki.h>
51 #include <cryptoutil.h>
52 #include "cryptoadm.h"
57 list_metaslot_info(boolean_t show_mechs
, boolean_t verbose
,
62 CK_SLOT_INFO slot_info
;
63 CK_TOKEN_INFO token_info
;
64 CK_MECHANISM_TYPE_PTR pmech_list
= NULL
;
67 CK_RV (*Tmp_C_GetFunctionList
)(CK_FUNCTION_LIST_PTR_PTR
);
68 CK_FUNCTION_LIST_PTR funcs
;
70 boolean_t lib_initialized
= B_FALSE
;
76 * Display the system-wide metaslot settings as specified
77 * in pkcs11.conf file.
79 if ((puent
= getent_uef(METASLOT_KEYWORD
)) == NULL
) {
80 cryptoerror(LOG_STDERR
,
81 gettext("metaslot entry doesn't exist."));
85 (void) printf(gettext("System-wide Meta Slot Configuration:\n"));
88 * Strictly for appearance's sake, this line should be as long as
89 * the length of the translated text above.
91 (void) printf(gettext("------------------------------------\n"));
92 (void) printf(gettext("Status: %s\n"), puent
->flag_metaslot_enabled
?
93 gettext("enabled") : gettext("disabled"));
94 (void) printf(gettext("Sensitive Token Object Automatic Migrate: %s\n"),
95 puent
->flag_metaslot_auto_key_migrate
? gettext("enabled") :
98 bzero(buf
, sizeof (buf
));
99 if (memcmp(puent
->metaslot_ks_slot
, buf
, SLOT_DESCRIPTION_SIZE
) != 0) {
100 (void) printf(gettext("Persistent object store slot: %s\n"),
101 puent
->metaslot_ks_slot
);
104 if (memcmp(puent
->metaslot_ks_token
, buf
, TOKEN_LABEL_SIZE
) != 0) {
105 (void) printf(gettext("Persistent object store token: %s\n"),
106 puent
->metaslot_ks_token
);
109 if ((!verbose
) && (!show_mechs
)) {
114 (void) printf(gettext("\nDetailed Meta Slot Information:\n"));
117 * Strictly for appearance's sake, this line should be as
118 * long as the length of the translated text above.
120 (void) printf(gettext("-------------------------------\n"));
124 * Need to actually make calls to libpkcs11.so to get
125 * information about metaslot.
128 dldesc
= dlopen(UEF_FRAME_LIB
, RTLD_NOW
);
129 if (dldesc
== NULL
) {
131 dl_error
= dlerror();
132 cryptodebug("Cannot load PKCS#11 framework library. "
133 "dlerror:%s", dl_error
);
137 /* Get the pointer to library's C_GetFunctionList() */
138 Tmp_C_GetFunctionList
= (CK_RV(*)())dlsym(dldesc
, "C_GetFunctionList");
139 if (Tmp_C_GetFunctionList
== NULL
) {
140 cryptodebug("Cannot get the address of the C_GetFunctionList "
147 /* Get the provider's function list */
148 rv
= Tmp_C_GetFunctionList(&funcs
);
150 cryptodebug("failed to call C_GetFunctionList in "
151 "framework library");
156 /* Initialize this provider */
157 rv
= funcs
->C_Initialize(NULL_PTR
);
159 cryptodebug("C_Initialize failed with error code 0x%x\n", rv
);
163 lib_initialized
= B_TRUE
;
167 * We know for sure that metaslot is slot 0 in the framework,
168 * so, we will do a C_GetSlotInfo() trying to see if it works.
169 * If it fails with CKR_SLOT_ID_INVALID, we know that metaslot
170 * is not really enabled.
172 rv
= funcs
->C_GetSlotInfo(METASLOT_ID
, &slot_info
);
173 if (rv
== CKR_SLOT_ID_INVALID
) {
174 (void) printf(gettext("actual status: disabled.\n"));
176 * Even if the -m and -v flag is supplied, there's nothing
177 * interesting to display about metaslot since it is disabled,
178 * so, just stop right here.
184 cryptodebug("C_GetSlotInfo failed with error "
194 (void) printf(gettext("actual status: enabled.\n"));
196 (void) printf(gettext("Description: %.64s\n"),
197 slot_info
.slotDescription
);
199 (void) printf(gettext("Token Present: %s\n"),
200 (slot_info
.flags
& CKF_TOKEN_PRESENT
?
201 gettext("True") : gettext("False")));
203 rv
= funcs
->C_GetTokenInfo(METASLOT_ID
, &token_info
);
205 cryptodebug("C_GetTokenInfo failed with error "
211 (void) printf(gettext("Token Label: %.32s\n"
212 "Manufacturer ID: %.32s\n"
214 "Serial Number: %.16s\n"
215 "Hardware Version: %d.%d\n"
216 "Firmware Version: %d.%d\n"
218 "PIN Min Length: %d\n"
219 "PIN Max Length: %d\n"),
221 token_info
.manufacturerID
,
223 token_info
.serialNumber
,
224 token_info
.hardwareVersion
.major
,
225 token_info
.hardwareVersion
.minor
,
226 token_info
.firmwareVersion
.major
,
227 token_info
.firmwareVersion
.minor
,
229 token_info
.ulMinPinLen
,
230 token_info
.ulMaxPinLen
);
232 display_token_flags(token_info
.flags
);
240 if (mechlist
== NULL
) {
241 rv
= funcs
->C_GetMechanismList(METASLOT_ID
, NULL_PTR
,
244 cryptodebug("C_GetMechanismList failed with error "
250 if (mech_count
> 0) {
251 pmech_list
= malloc(mech_count
*
252 sizeof (CK_MECHANISM_TYPE
));
253 if (pmech_list
== NULL
) {
254 cryptodebug("out of memory");
258 rv
= funcs
->C_GetMechanismList(METASLOT_ID
, pmech_list
,
261 cryptodebug("C_GetMechanismList failed with "
262 "error code 0x%x\n", rv
);
268 rc
= convert_mechlist(&pmech_list
, &mech_count
, mechlist
);
274 (void) printf(gettext("Mechanisms:\n"));
275 if (mech_count
== 0) {
276 /* should never be this case */
277 (void) printf(gettext("No mechanisms\n"));
281 display_verbose_mech_header();
284 for (i
= 0; i
< mech_count
; i
++) {
285 CK_MECHANISM_TYPE mech
= pmech_list
[i
];
287 if (mech
>= CKM_VENDOR_DEFINED
) {
288 (void) printf("%#lx", mech
);
290 (void) printf("%-29s", pkcs11_mech2str(mech
));
294 CK_MECHANISM_INFO mech_info
;
295 rv
= funcs
->C_GetMechanismInfo(METASLOT_ID
,
298 cryptodebug("C_GetMechanismInfo failed with "
299 "error code 0x%x\n", rv
);
303 display_mech_info(&mech_info
);
310 if ((rc
== FAILURE
) && (show_mechs
)) {
311 (void) printf(gettext(
312 "metaslot: failed to retrieve the mechanism list.\n"));
315 if (lib_initialized
) {
316 (void) funcs
->C_Finalize(NULL_PTR
);
319 if (dldesc
!= NULL
) {
320 (void) dlclose(dldesc
);
323 if (pmech_list
!= NULL
) {
324 (void) free(pmech_list
);
331 list_metaslot_policy()
337 if ((puent
= getent_uef(METASLOT_KEYWORD
)) == NULL
) {
338 cryptoerror(LOG_STDERR
,
339 gettext("metaslot entry doesn't exist."));
343 rc
= display_policy(puent
);
350 * disable metaslot and some of its configuration options
352 * If mechlist==NULL, and the other 2 flags are false, just disabled
353 * the metaslot feature.
355 * mechlist: list of mechanisms to disable
356 * allflag: if true, indicates all mechanisms should be disabled.
357 * auto_key_migrate_flag: if true, indicates auto key migrate should be disabled
360 disable_metaslot(mechlist_t
*mechlist
, boolean_t allflag
,
361 boolean_t auto_key_migrate_flag
)
366 if ((puent
= getent_uef(METASLOT_KEYWORD
)) == NULL
) {
367 cryptoerror(LOG_STDERR
,
368 gettext("metaslot entry doesn't exist."));
373 if ((mechlist
== NULL
) && (!auto_key_migrate_flag
) && (!allflag
)) {
374 /* disable metaslot */
375 puent
->flag_metaslot_enabled
= B_FALSE
;
379 if (auto_key_migrate_flag
) {
380 /* need to disable auto_key_migrate */
381 puent
->flag_metaslot_auto_key_migrate
= B_FALSE
;
384 if ((mechlist
== NULL
) && (!allflag
)) {
388 /* disable specified mechanisms */
390 free_umechlist(puent
->policylist
);
391 puent
->policylist
= NULL
;
393 puent
->flag_enabledlist
= B_TRUE
;
396 if (puent
->flag_enabledlist
== B_TRUE
) {
398 * The current default policy mode
399 * is "all are disabled, except ...", so if a
400 * specified mechanism is in the exception list
401 * (the policylist), delete it from the policylist.
403 rc
= update_policylist(puent
, mechlist
, DELETE_MODE
);
406 * The current default policy mode of this library
407 * is "all are enabled", so if a specified mechanism
408 * is not in the exception list (policylist), add
409 * it into the policylist.
411 rc
= update_policylist(puent
, mechlist
, ADD_MODE
);
419 /* If all mechanisms are disabled, metaslot will be disabled as well */
420 if ((puent
->flag_enabledlist
) && (puent
->count
== 0)) {
421 puent
->flag_metaslot_enabled
= B_FALSE
;
426 rc
= update_pkcs11conf(puent
);
434 * enable metaslot and some of its configuration options
436 * If mechlist==NULL, and the other flags are false, or not specified,
437 * just enable the metaslot feature.
439 * token: if specified, indicate label of token to be used as keystore.
440 * slot: if specified, indicate slot to be used as keystore.
441 * use_default: if true, indicate to use the default keystore. It should
442 * not be specified if either token or slot is specified.
443 * mechlist: list of mechanisms to enable
444 * allflag: if true, indicates all mechanisms should be enabled.
445 * auto_key_migrate_flag: if true, indicates auto key migrate should be enabled
448 enable_metaslot(char *token
, char *slot
, boolean_t use_default
,
449 mechlist_t
*mechlist
, boolean_t allflag
, boolean_t auto_key_migrate_flag
)
454 if ((puent
= getent_uef(METASLOT_KEYWORD
)) == NULL
) {
455 cryptoerror(LOG_STDERR
,
456 gettext("metaslot entry doesn't exist."));
460 puent
->flag_metaslot_enabled
= B_TRUE
;
462 if (auto_key_migrate_flag
) {
463 /* need to enable auto_key_migrate */
464 puent
->flag_metaslot_auto_key_migrate
= B_TRUE
;
469 * If enabling all, what needs to be done are cleaning up the
470 * policylist and setting the "flag_enabledlist" flag to
473 free_umechlist(puent
->policylist
);
474 puent
->policylist
= NULL
;
476 puent
->flag_enabledlist
= B_FALSE
;
480 if (puent
->flag_enabledlist
== B_TRUE
) {
482 * The current default policy mode of this
483 * library is "all are disabled, except ...",
484 * so if a specified mechanism is not in the
485 * exception list (policylist), add it.
487 rc
= update_policylist(puent
, mechlist
,
491 * The current default policy mode of this
492 * library is "all are enabled, except", so if
493 * a specified mechanism is in the exception
494 * list (policylist), delete it.
496 rc
= update_policylist(puent
, mechlist
,
506 if (!use_default
&& !token
&& !slot
) {
507 /* no need to change metaslot keystore */
511 (void) bzero((char *)puent
->metaslot_ks_token
, TOKEN_LABEL_SIZE
);
512 (void) bzero((char *)puent
->metaslot_ks_slot
, SLOT_DESCRIPTION_SIZE
);
515 (void) strlcpy((char *)puent
->metaslot_ks_token
,
516 SOFT_TOKEN_LABEL
, TOKEN_LABEL_SIZE
);
517 (void) strlcpy((char *)puent
->metaslot_ks_slot
,
518 SOFT_SLOT_DESCRIPTION
, SLOT_DESCRIPTION_SIZE
);
522 (void) strlcpy((char *)puent
->metaslot_ks_token
, token
,
527 (void) strlcpy((char *)puent
->metaslot_ks_slot
, slot
,
528 SLOT_DESCRIPTION_SIZE
);
535 rc
= update_pkcs11conf(puent
);