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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright 2010 Nexenta Systems, Inc. All rights resrved.
28 #include <cryptoutil.h>
38 #include <sys/types.h>
40 #include <security/cryptoki.h>
41 #include "cryptoadm.h"
44 #define HDR2 " S V K a U D\n"
45 #define HDR3 " i e e i n e\n"
46 #define HDR4 " S g V r y r W w r\n"
47 #define HDR5 " E D D i n e i G G r r i\n"
48 #define HDR6 " H n e i g + r + e e a a v E\n"
49 #define HDR7 "min max W c c g n R i R n n p p e C\n"
52 static int err
; /* To store errno which may be overwritten by gettext() */
53 static boolean_t
is_in_policylist(midstr_t
, umechlist_t
*);
54 static char *uent2str(uentry_t
*);
55 static boolean_t
check_random(CK_SLOT_ID
, CK_FUNCTION_LIST_PTR
);
57 static void display_slot_flags(CK_FLAGS flags
)
59 (void) printf(gettext("Slot Flags: "));
60 if (flags
& CKF_TOKEN_PRESENT
)
61 (void) printf("CKF_TOKEN_PRESENT ");
62 if (flags
& CKF_REMOVABLE_DEVICE
)
63 (void) printf("CKF_REMOVABLE_DEVICE ");
64 if (flags
& CKF_HW_SLOT
)
65 (void) printf("CKF_HW_SLOT ");
70 display_token_flags(CK_FLAGS flags
)
72 (void) printf(gettext("Flags: "));
74 (void) printf("CKF_RNG ");
75 if (flags
& CKF_WRITE_PROTECTED
)
76 (void) printf("CKF_WRITE_PROTECTED ");
77 if (flags
& CKF_LOGIN_REQUIRED
)
78 (void) printf("CKF_LOGIN_REQUIRED ");
79 if (flags
& CKF_USER_PIN_INITIALIZED
)
80 (void) printf("CKF_USER_PIN_INITIALIZED ");
81 if (flags
& CKF_RESTORE_KEY_NOT_NEEDED
)
82 (void) printf("CKF_RESTORE_KEY_NOT_NEEDED ");
83 if (flags
& CKF_CLOCK_ON_TOKEN
)
84 (void) printf("CKF_CLOCK_ON_TOKEN ");
85 if (flags
& CKF_PROTECTED_AUTHENTICATION_PATH
)
86 (void) printf("CKF_PROTECTED_AUTHENTICATION_PATH ");
87 if (flags
& CKF_DUAL_CRYPTO_OPERATIONS
)
88 (void) printf("CKF_DUAL_CRYPTO_OPERATIONS ");
89 if (flags
& CKF_TOKEN_INITIALIZED
)
90 (void) printf("CKF_TOKEN_INITIALIZED ");
91 if (flags
& CKF_SECONDARY_AUTHENTICATION
)
92 (void) printf("CKF_SECONDARY_AUTHENTICATION ");
93 if (flags
& CKF_USER_PIN_COUNT_LOW
)
94 (void) printf("CKF_USER_PIN_COUNT_LOW ");
95 if (flags
& CKF_USER_PIN_FINAL_TRY
)
96 (void) printf("CKF_USER_PIN_FINAL_TRY ");
97 if (flags
& CKF_USER_PIN_LOCKED
)
98 (void) printf("CKF_USER_PIN_LOCKED ");
99 if (flags
& CKF_USER_PIN_TO_BE_CHANGED
)
100 (void) printf("CKF_USER_PIN_TO_BE_CHANGED ");
101 if (flags
& CKF_SO_PIN_COUNT_LOW
)
102 (void) printf("CKF_SO_PIN_COUNT_LOW ");
103 if (flags
& CKF_SO_PIN_FINAL_TRY
)
104 (void) printf("CKF_SO_PIN_FINAL_TRY ");
105 if (flags
& CKF_SO_PIN_LOCKED
)
106 (void) printf("CKF_SO_PIN_LOCKED ");
107 if (flags
& CKF_SO_PIN_TO_BE_CHANGED
)
108 (void) printf("CKF_SO_PIN_TO_BE_CHANGED ");
109 if (flags
& CKF_SO_PIN_TO_BE_CHANGED
)
110 (void) printf("CKF_SO_PIN_TO_BE_CHANGED ");
115 display_mech_info(CK_MECHANISM_INFO
*mechInfo
)
117 CK_FLAGS ec_flags
= CKF_EC_F_P
| CKF_EC_F_2M
| CKF_EC_ECPARAMETERS
|
118 CKF_EC_NAMEDCURVE
| CKF_EC_UNCOMPRESS
| CKF_EC_COMPRESS
;
120 (void) printf("%-4ld %-4ld ", mechInfo
->ulMinKeySize
,
121 mechInfo
->ulMaxKeySize
);
122 (void) printf("%s %s %s %s %s %s %s %s %s %s %s %s "
124 (mechInfo
->flags
& CKF_HW
) ? "X" : ".",
125 (mechInfo
->flags
& CKF_ENCRYPT
) ? "X" : ".",
126 (mechInfo
->flags
& CKF_DECRYPT
) ? "X" : ".",
127 (mechInfo
->flags
& CKF_DIGEST
) ? "X" : ".",
128 (mechInfo
->flags
& CKF_SIGN
) ? "X" : ".",
129 (mechInfo
->flags
& CKF_SIGN_RECOVER
) ? "X" : ".",
130 (mechInfo
->flags
& CKF_VERIFY
) ? "X" : ".",
131 (mechInfo
->flags
& CKF_VERIFY_RECOVER
) ? "X" : ".",
132 (mechInfo
->flags
& CKF_GENERATE
) ? "X" : ".",
133 (mechInfo
->flags
& CKF_GENERATE_KEY_PAIR
) ? "X" : ".",
134 (mechInfo
->flags
& CKF_WRAP
) ? "X" : ".",
135 (mechInfo
->flags
& CKF_UNWRAP
) ? "X" : ".",
136 (mechInfo
->flags
& CKF_DERIVE
) ? "X" : ".",
137 (mechInfo
->flags
& ec_flags
) ? "X" : ".");
141 * Converts the provided list of mechanism names in their string format to
142 * their corresponding PKCS#11 mechanism IDs.
144 * The list of mechanism names to be converted is provided in the
145 * "mlist" argument. The list of converted mechanism IDs is returned
146 * in the "pmech_list" argument.
148 * This function is called by list_metaslot_info() and
149 * list_mechlist_for_lib() functions.
152 convert_mechlist(CK_MECHANISM_TYPE
**pmech_list
, CK_ULONG
*mech_count
,
156 mechlist_t
*p
= mlist
;
163 *pmech_list
= malloc(n
* sizeof (CK_MECHANISM_TYPE
));
164 if (pmech_list
== NULL
) {
165 cryptodebug("out of memory");
169 for (i
= 0; i
< n
; i
++) {
170 if (pkcs11_str2mech(p
->name
, &(*pmech_list
[i
])) != CKR_OK
) {
181 * Display the mechanism list for a user-level library
184 list_mechlist_for_lib(char *libname
, mechlist_t
*mlist
,
185 flag_val_t
*rng_flag
, boolean_t no_warn
,
186 boolean_t verbose
, boolean_t show_mechs
)
189 CK_RV (*Tmp_C_GetFunctionList
)(CK_FUNCTION_LIST_PTR_PTR
);
190 CK_FUNCTION_LIST_PTR prov_funcs
; /* Provider's function list */
191 CK_SLOT_ID_PTR prov_slots
= NULL
; /* Provider's slot list */
192 CK_MECHANISM_TYPE_PTR pmech_list
= NULL
; /* mech list for a slot */
193 CK_SLOT_INFO slotinfo
;
196 uentry_t
*puent
= NULL
;
197 boolean_t lib_initialized
= B_FALSE
;
200 const char *mech_name
;
202 char libpath
[MAXPATHLEN
];
203 char buf
[MAXPATHLEN
];
207 if (libname
== NULL
) {
208 /* should not happen */
209 cryptoerror(LOG_STDERR
, gettext("internal error."));
210 cryptodebug("list_mechlist_for_lib() - libname is NULL.");
214 /* Check if the library is in the pkcs11.conf file */
215 if ((puent
= getent_uef(libname
)) == NULL
) {
216 cryptoerror(LOG_STDERR
,
217 gettext("%s does not exist."), libname
);
222 /* Remove $ISA from the library name */
223 if (strlcpy(buf
, libname
, sizeof (buf
)) >= sizeof (buf
)) {
224 (void) printf(gettext("%s: the provider name is too long."),
229 if ((isa
= strstr(buf
, PKCS11_ISA
)) != NULL
) {
231 isa
+= strlen(PKCS11_ISA
);
232 (void) snprintf(libpath
, MAXPATHLEN
, "%s%s%s", buf
, "/", isa
);
234 (void) strlcpy(libpath
, libname
, sizeof (libpath
));
238 * Open the provider. Use RTLD_NOW here, as a way to
239 * catch any providers with incomplete symbols that
240 * might otherwise cause problems during libpkcs11's
243 dldesc
= dlopen(libpath
, RTLD_NOW
);
244 if (dldesc
== NULL
) {
245 dl_error
= dlerror();
246 cryptodebug("Cannot load PKCS#11 library %s. dlerror: %s",
247 libname
, dl_error
!= NULL
? dl_error
: "Unknown");
252 /* Get the pointer to provider's C_GetFunctionList() */
253 Tmp_C_GetFunctionList
= (CK_RV(*)())dlsym(dldesc
, "C_GetFunctionList");
254 if (Tmp_C_GetFunctionList
== NULL
) {
255 cryptodebug("Cannot get the address of the C_GetFunctionList "
261 /* Get the provider's function list */
262 rv
= Tmp_C_GetFunctionList(&prov_funcs
);
264 cryptodebug("failed to call C_GetFunctionList from %s",
270 /* Initialize this provider */
271 rv
= prov_funcs
->C_Initialize(NULL_PTR
);
273 cryptodebug("failed to call C_Initialize from %s, "
274 "return code = %d", libname
, rv
);
278 lib_initialized
= B_TRUE
;
282 * Find out how many slots this provider has, call with tokenPresent
283 * set to FALSE so all potential slots are returned.
285 rv
= prov_funcs
->C_GetSlotList(FALSE
, NULL_PTR
, &slot_count
);
287 cryptodebug("failed to get the slotlist from %s.", libname
);
290 } else if (slot_count
== 0) {
292 (void) printf(gettext("%s: no slots presented.\n"),
298 /* Allocate memory for the slot list */
299 prov_slots
= malloc(slot_count
* sizeof (CK_SLOT_ID
));
300 if (prov_slots
== NULL
) {
301 cryptodebug("out of memory.");
306 /* Get the slot list from provider */
307 rv
= prov_funcs
->C_GetSlotList(FALSE
, prov_slots
, &slot_count
);
309 cryptodebug("failed to call C_GetSlotList() from %s.",
316 (void) printf(gettext("Number of slots: %d\n"), slot_count
);
319 /* Get the mechanism list for each slot */
320 for (i
= 0; i
< slot_count
; i
++) {
324 * In some languages, the # symbol should be
325 * converted to "no", an "n" followed by a
328 (void) printf(gettext("\nSlot #%d\n"), i
+1);
330 if ((rng_flag
!= NULL
) && (*rng_flag
== NO_RNG
)) {
331 if (check_random(prov_slots
[i
], prov_funcs
)) {
339 rv
= prov_funcs
->C_GetSlotInfo(prov_slots
[i
], &slotinfo
);
341 cryptodebug("failed to get slotinfo from %s", libname
);
346 CK_TOKEN_INFO tokeninfo
;
348 (void) printf(gettext("Description: %.64s\n"
349 "Manufacturer: %.32s\n"
350 "PKCS#11 Version: %d.%d\n"),
351 slotinfo
.slotDescription
,
352 slotinfo
.manufacturerID
,
353 prov_funcs
->version
.major
,
354 prov_funcs
->version
.minor
);
356 (void) printf(gettext("Hardware Version: %d.%d\n"
357 "Firmware Version: %d.%d\n"),
358 slotinfo
.hardwareVersion
.major
,
359 slotinfo
.hardwareVersion
.minor
,
360 slotinfo
.firmwareVersion
.major
,
361 slotinfo
.firmwareVersion
.minor
);
363 (void) printf(gettext("Token Present: %s\n"),
364 (slotinfo
.flags
& CKF_TOKEN_PRESENT
?
365 gettext("True") : gettext("False")));
367 display_slot_flags(slotinfo
.flags
);
369 rv
= prov_funcs
->C_GetTokenInfo(prov_slots
[i
],
372 cryptodebug("Failed to get "
373 "token info from %s", libname
);
378 (void) printf(gettext("Token Label: %.32s\n"
379 "Manufacturer ID: %.32s\n"
381 "Serial Number: %.16s\n"
382 "Hardware Version: %d.%d\n"
383 "Firmware Version: %d.%d\n"
385 "PIN Min Length: %d\n"
386 "PIN Max Length: %d\n"),
388 tokeninfo
.manufacturerID
,
390 tokeninfo
.serialNumber
,
391 tokeninfo
.hardwareVersion
.major
,
392 tokeninfo
.hardwareVersion
.minor
,
393 tokeninfo
.firmwareVersion
.major
,
394 tokeninfo
.firmwareVersion
.minor
,
396 tokeninfo
.ulMinPinLen
,
397 tokeninfo
.ulMaxPinLen
);
399 display_token_flags(tokeninfo
.flags
);
403 rv
= prov_funcs
->C_GetMechanismList(prov_slots
[i
],
404 NULL_PTR
, &mech_count
);
407 "failed to call C_GetMechanismList() "
408 "from %s.", libname
);
413 if (mech_count
== 0) {
414 /* no mechanisms in this slot */
418 pmech_list
= malloc(mech_count
*
419 sizeof (CK_MECHANISM_TYPE
));
420 if (pmech_list
== NULL
) {
421 cryptodebug("out of memory");
426 /* Get the actual mechanism list */
427 rv
= prov_funcs
->C_GetMechanismList(prov_slots
[i
],
428 pmech_list
, &mech_count
);
431 "failed to call C_GetMechanismList() "
432 "from %s.", libname
);
438 /* use the mechanism list passed in */
439 rc
= convert_mechlist(&pmech_list
, &mech_count
, mlist
);
445 (void) printf(gettext("Mechanisms:\n"));
447 if (verbose
&& show_mechs
) {
448 display_verbose_mech_header();
451 * Merge the current mechanism list into the returning
454 for (j
= 0; show_mechs
&& j
< mech_count
; j
++) {
455 CK_MECHANISM_TYPE mech
= pmech_list
[j
];
456 CK_MECHANISM_INFO mech_info
;
458 rv
= prov_funcs
->C_GetMechanismInfo(
459 prov_slots
[i
], mech
, &mech_info
);
463 "C_GetMechanismInfo() from %s.",
470 if (mech
>= CKM_VENDOR_DEFINED
) {
471 (void) printf("%#lx", mech
);
473 mech_name
= pkcs11_mech2str(mech
);
474 (void) printf("%-29s", mech_name
);
478 display_mech_info(&mech_info
);
488 if (rng_flag
!= NULL
|| rc
== FAILURE
) {
495 (void) printf(gettext(
496 "%s: failed to retrieve the mechanism list.\n"), libname
);
499 if (lib_initialized
) {
500 (void) prov_funcs
->C_Finalize(NULL_PTR
);
503 if (dldesc
!= NULL
) {
504 (void) dlclose(dldesc
);
507 if (prov_slots
!= NULL
) {
516 * Display the mechanism policy for a user-level library
519 list_policy_for_lib(char *libname
)
521 uentry_t
*puent
= NULL
;
524 if (libname
== NULL
) {
525 /* should not happen */
526 cryptoerror(LOG_STDERR
, gettext("internal error."));
527 cryptodebug("list_policy_for_lib() - libname is NULL.");
531 /* Get the library entry from the pkcs11.conf file */
532 if ((puent
= getent_uef(libname
)) == NULL
) {
533 cryptoerror(LOG_STDERR
,
534 gettext("%s does not exist."), libname
);
538 /* Print the policy for this library */
539 rc
= print_uef_policy(puent
);
547 * Disable mechanisms for a user-level library
550 disable_uef_lib(char *libname
, boolean_t rndflag
, boolean_t allflag
,
551 mechlist_t
*marglist
)
556 if (libname
== NULL
) {
557 /* should not happen */
558 cryptoerror(LOG_STDERR
, gettext("internal error."));
559 cryptodebug("disable_uef_lib() - libname is NULL.");
563 /* Get the provider entry from the pkcs11.conf file */
564 if ((puent
= getent_uef(libname
)) == NULL
) {
565 cryptoerror(LOG_STDERR
,
566 gettext("%s does not exist."), libname
);
571 * Update the mechanism policy of this library entry, based on
572 * the current policy mode of the library and the mechanisms specified
577 * If disabling all, just need to clean up the policylist and
578 * set the flag_enabledlist flag to be B_TRUE.
580 free_umechlist(puent
->policylist
);
581 puent
->policylist
= NULL
;
583 puent
->flag_enabledlist
= B_TRUE
;
585 } else if (marglist
!= NULL
) {
586 if (puent
->flag_enabledlist
== B_TRUE
) {
588 * The current default policy mode of this library
589 * is "all are disabled, except ...", so if a
590 * specified mechanism is in the exception list
591 * (the policylist), delete it from the policylist.
593 rc
= update_policylist(puent
, marglist
, DELETE_MODE
);
596 * The current default policy mode of this library
597 * is "all are enabled", so if a specified mechanism
598 * is not in the exception list (policylist), add
599 * it into the policylist.
601 rc
= update_policylist(puent
, marglist
, ADD_MODE
);
603 } else if (!rndflag
) {
604 /* should not happen */
605 cryptoerror(LOG_STDERR
, gettext("internal error."));
606 cryptodebug("disable_uef_lib() - wrong arguments.");
611 puent
->flag_norandom
= B_TRUE
;
618 /* Update the pkcs11.conf file with the updated entry */
619 rc
= update_pkcs11conf(puent
);
626 * Enable disabled mechanisms for a user-level library.
629 enable_uef_lib(char *libname
, boolean_t rndflag
, boolean_t allflag
,
630 mechlist_t
*marglist
)
635 if (libname
== NULL
) {
636 /* should not happen */
637 cryptoerror(LOG_STDERR
, gettext("internal error."));
638 cryptodebug("enable_uef_lib() - libname is NULL.");
642 /* Get the provider entry from the pkcs11.conf file */
643 if ((puent
= getent_uef(libname
)) == NULL
) {
644 cryptoerror(LOG_STDERR
,
645 gettext("%s does not exist."), libname
);
650 * Update the mechanism policy of this library entry, based on
651 * the current policy mode of the library and the mechanisms
656 * If enabling all, what needs to be done are cleaning up the
657 * policylist and setting the "flag_enabledlist" flag to
660 free_umechlist(puent
->policylist
);
661 puent
->policylist
= NULL
;
663 puent
->flag_enabledlist
= B_FALSE
;
665 } else if (marglist
!= NULL
) {
666 if (puent
->flag_enabledlist
== B_TRUE
) {
668 * The current default policy mode of this library
669 * is "all are disabled, except ...", so if a
670 * specified mechanism is not in the exception list
671 * (policylist), add it.
673 rc
= update_policylist(puent
, marglist
, ADD_MODE
);
676 * The current default policy mode of this library
677 * is "all are enabled, except", so if a specified
678 * mechanism is in the exception list (policylist),
681 rc
= update_policylist(puent
, marglist
, DELETE_MODE
);
683 } else if (!rndflag
) {
684 /* should not come here */
685 cryptoerror(LOG_STDERR
, gettext("internal error."));
686 cryptodebug("enable_uef_lib() - wrong arguments.");
691 puent
->flag_norandom
= B_FALSE
;
698 /* Update the pkcs11.conf file with the updated entry */
699 rc
= update_pkcs11conf(puent
);
706 * Install a user-level library.
709 install_uef_lib(char *libname
)
713 char libpath
[MAXPATHLEN
];
714 char libbuf
[MAXPATHLEN
];
717 if (libname
== NULL
) {
718 /* should not happen */
719 cryptoerror(LOG_STDERR
, gettext("internal error."));
720 cryptodebug("install_uef_lib() - libname is NULL.");
724 /* Check if the provider already exists in the framework */
725 if ((puent
= getent_uef(libname
)) != NULL
) {
726 cryptoerror(LOG_STDERR
, gettext("%s exists already."),
733 * Check if the library exists in the system. if $ISA is in the
734 * path, only check the 32bit version.
736 if (strlcpy(libbuf
, libname
, MAXPATHLEN
) >= MAXPATHLEN
) {
737 cryptoerror(LOG_STDERR
,
738 gettext("the provider name is too long - %s"), libname
);
742 if ((isa
= strstr(libbuf
, PKCS11_ISA
)) != NULL
) {
744 isa
+= strlen(PKCS11_ISA
);
745 (void) snprintf(libpath
, sizeof (libpath
), "%s%s%s", libbuf
,
748 (void) strlcpy(libpath
, libname
, sizeof (libpath
));
751 /* Check if it is same as the framework library */
752 if (strcmp(libpath
, UEF_FRAME_LIB
) == 0) {
753 cryptoerror(LOG_STDERR
, gettext(
754 "The framework library %s can not be installed."),
759 if (stat(libpath
, &statbuf
) != 0) {
760 cryptoerror(LOG_STDERR
, gettext("%s not found"), libname
);
764 /* Need to add "\n" to libname for adding into the config file */
765 if (strlcat(libname
, "\n", MAXPATHLEN
) >= MAXPATHLEN
) {
766 cryptoerror(LOG_STDERR
, gettext(
767 "can not install %s; the name is too long."), libname
);
771 return (update_conf(_PATH_PKCS11_CONF
, libname
));
777 * Uninstall a user-level library.
780 uninstall_uef_lib(char *libname
)
786 char buffer2
[BUFSIZ
];
787 char tmpfile_name
[MAXPATHLEN
];
790 boolean_t in_package
;
794 if (libname
== NULL
) {
795 /* should not happen */
796 cryptoerror(LOG_STDERR
, gettext("internal error."));
797 cryptodebug("uninstall_uef_lib() - libname is NULL.");
801 /* Check if the provider exists */
802 if ((puent
= getent_uef(libname
)) == NULL
) {
803 cryptoerror(LOG_STDERR
,
804 gettext("%s does not exist."), libname
);
809 /* Open the pkcs11.conf file and lock it */
810 if ((pfile
= fopen(_PATH_PKCS11_CONF
, "r+")) == NULL
) {
812 cryptoerror(LOG_STDERR
,
813 gettext("failed to update the configuration - %s"),
815 cryptodebug("failed to open %s for write.", _PATH_PKCS11_CONF
);
819 if (lockf(fileno(pfile
), F_TLOCK
, 0) == -1) {
821 cryptoerror(LOG_STDERR
,
822 gettext("failed to lock the configuration - %s"),
824 (void) fclose(pfile
);
829 * Create a temporary file in the /etc/crypto directory to save
830 * the new configuration file first.
832 (void) strlcpy(tmpfile_name
, TMPFILE_TEMPLATE
, sizeof (tmpfile_name
));
833 if (mkstemp(tmpfile_name
) == -1) {
835 cryptoerror(LOG_STDERR
,
836 gettext("failed to create a temporary file - %s"),
838 (void) fclose(pfile
);
842 if ((pfile_tmp
= fopen(tmpfile_name
, "w")) == NULL
) {
844 cryptoerror(LOG_STDERR
, gettext("failed to open %s - %s"),
845 tmpfile_name
, strerror(err
));
846 if (unlink(tmpfile_name
) != 0) {
848 cryptoerror(LOG_STDERR
, gettext(
849 "(Warning) failed to remove %s: %s"),
850 tmpfile_name
, strerror(err
));
852 (void) fclose(pfile
);
858 * Loop thru the config file. If the library to be uninstalled
859 * is in a package, just comment it off.
861 in_package
= B_FALSE
;
862 while (fgets(buffer
, BUFSIZ
, pfile
) != NULL
) {
864 if (!(buffer
[0] == ' ' || buffer
[0] == '\n' ||
865 buffer
[0] == '\t')) {
866 if (strstr(buffer
, " Start ") != NULL
) {
868 } else if (strstr(buffer
, " End ") != NULL
) {
869 in_package
= B_FALSE
;
870 } else if (buffer
[0] != '#') {
871 (void) strlcpy(buffer2
, buffer
, BUFSIZ
);
873 /* get rid of trailing '\n' */
874 len
= strlen(buffer2
);
875 if (buffer2
[len
-1] == '\n') {
880 if ((name
= strtok(buffer2
, SEP_COLON
))
884 } else if (strcmp(libname
, name
) == 0) {
892 (void) snprintf(buffer2
, sizeof (buffer2
),
893 "%s%s%s", "#", libname
, "\n");
894 if (fputs(buffer2
, pfile_tmp
) == EOF
) {
899 if (fputs(buffer
, pfile_tmp
) == EOF
) {
910 cryptoerror(LOG_STDERR
, gettext("write error."));
911 (void) fclose(pfile
);
912 (void) fclose(pfile_tmp
);
913 if (unlink(tmpfile_name
) != 0) {
915 cryptoerror(LOG_STDERR
, gettext(
916 "(Warning) failed to remove %s: %s"),
917 tmpfile_name
, strerror(err
));
922 (void) fclose(pfile
);
923 if (fclose(pfile_tmp
) != 0) {
925 cryptoerror(LOG_STDERR
,
926 gettext("failed to close a temporary file - %s"),
931 /* Now update the real config file */
932 if (rename(tmpfile_name
, _PATH_PKCS11_CONF
) == -1) {
934 cryptoerror(LOG_STDERR
,
935 gettext("failed to update the configuration - %s"),
937 cryptodebug("failed to rename %s to %s: %s", tmpfile
,
938 _PATH_PKCS11_CONF
, strerror(err
));
940 } else if (chmod(_PATH_PKCS11_CONF
,
941 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) == -1) {
943 cryptoerror(LOG_STDERR
,
944 gettext("failed to update the configuration - %s"),
946 cryptodebug("failed to chmod to %s: %s", _PATH_PKCS11_CONF
,
953 if ((rc
== FAILURE
) && (unlink(tmpfile_name
) != 0)) {
955 cryptoerror(LOG_STDERR
, gettext(
956 "(Warning) failed to remove %s: %s"),
957 tmpfile_name
, strerror(err
));
965 display_policy(uentry_t
*puent
)
967 CK_MECHANISM_TYPE mech_id
;
968 const char *mech_name
;
975 if (puent
->flag_enabledlist
== B_FALSE
) {
976 (void) printf(gettext("%s: all mechanisms are enabled"),
978 ptr
= puent
->policylist
;
982 (void) printf(gettext(", except "));
983 while (ptr
!= NULL
) {
984 mech_id
= strtoul(ptr
->name
, NULL
, 0);
985 if (mech_id
& CKO_VENDOR_DEFINED
) {
986 /* vendor defined mechanism */
987 (void) printf("%s", ptr
->name
);
989 if (mech_id
>= CKM_VENDOR_DEFINED
) {
990 (void) printf("%#lx", mech_id
);
992 mech_name
= pkcs11_mech2str(
994 if (mech_name
== NULL
) {
997 (void) printf("%s", mech_name
);
1009 } else { /* puent->flag_enabledlist == B_TRUE */
1010 (void) printf(gettext("%s: all mechanisms are disabled"),
1012 ptr
= puent
->policylist
;
1016 (void) printf(gettext(", except "));
1017 while (ptr
!= NULL
) {
1018 mech_id
= strtoul(ptr
->name
, NULL
, 0);
1019 if (mech_id
& CKO_VENDOR_DEFINED
) {
1020 /* vendor defined mechanism */
1021 (void) printf("%s", ptr
->name
);
1023 mech_name
= pkcs11_mech2str(mech_id
);
1024 if (mech_name
== NULL
) {
1027 (void) printf("%s", mech_name
);
1044 * Print out the mechanism policy for a user-level provider pointed by puent.
1047 print_uef_policy(uentry_t
*puent
)
1049 flag_val_t rng_flag
;
1051 if (puent
== NULL
) {
1056 if (list_mechlist_for_lib(puent
->name
, NULL
, &rng_flag
, B_TRUE
,
1057 B_FALSE
, B_FALSE
) != SUCCESS
) {
1058 cryptoerror(LOG_STDERR
,
1059 gettext("%s internal error."), puent
->name
);
1063 if (display_policy(puent
) != SUCCESS
) {
1068 if (puent
->flag_norandom
== B_TRUE
)
1071 * "random" is a keyword and not to be translated.
1073 (void) printf(gettext(" %s is disabled."), "random");
1075 if (rng_flag
== HAS_RNG
)
1078 * "random" is a keyword and not to be translated.
1080 (void) printf(gettext(" %s is enabled."), "random");
1082 (void) printf("\n");
1088 (void) printf(gettext("\nout of memory.\n"));
1094 * Check if the mechanism is in the mechanism list.
1097 is_in_policylist(midstr_t mechname
, umechlist_t
*plist
)
1099 boolean_t found
= B_FALSE
;
1101 if (mechname
== NULL
) {
1105 while (plist
!= NULL
) {
1106 if (strcmp(plist
->name
, mechname
) == 0) {
1110 plist
= plist
->next
;
1118 * Update the pkcs11.conf file with the updated entry.
1121 update_pkcs11conf(uentry_t
*puent
)
1125 char buffer
[BUFSIZ
];
1126 char buffer2
[BUFSIZ
];
1127 char tmpfile_name
[MAXPATHLEN
];
1134 if (puent
== NULL
) {
1135 cryptoerror(LOG_STDERR
, gettext("internal error."));
1139 /* Open the pkcs11.conf file */
1140 if ((pfile
= fopen(_PATH_PKCS11_CONF
, "r+")) == NULL
) {
1142 cryptoerror(LOG_STDERR
,
1143 gettext("failed to update the configuration - %s"),
1145 cryptodebug("failed to open %s for write.", _PATH_PKCS11_CONF
);
1149 /* Lock the pkcs11.conf file */
1150 if (lockf(fileno(pfile
), F_TLOCK
, 0) == -1) {
1152 cryptoerror(LOG_STDERR
,
1153 gettext("failed to update the configuration - %s"),
1155 (void) fclose(pfile
);
1160 * Create a temporary file in the /etc/crypto directory to save
1161 * updated configuration file first.
1163 (void) strlcpy(tmpfile_name
, TMPFILE_TEMPLATE
, sizeof (tmpfile_name
));
1164 if (mkstemp(tmpfile_name
) == -1) {
1166 cryptoerror(LOG_STDERR
,
1167 gettext("failed to create a temporary file - %s"),
1169 (void) fclose(pfile
);
1173 if ((pfile_tmp
= fopen(tmpfile_name
, "w")) == NULL
) {
1175 cryptoerror(LOG_STDERR
, gettext("failed to open %s - %s"),
1176 tmpfile_name
, strerror(err
));
1177 if (unlink(tmpfile_name
) != 0) {
1179 cryptoerror(LOG_STDERR
, gettext(
1180 "(Warning) failed to remove %s: %s"),
1181 tmpfile_name
, strerror(err
));
1183 (void) fclose(pfile
);
1189 * Loop thru entire pkcs11.conf file, update the entry to be
1190 * updated and save the updated file to the temporary file first.
1192 while (fgets(buffer
, BUFSIZ
, pfile
) != NULL
) {
1194 if (!(buffer
[0] == '#' || buffer
[0] == ' ' ||
1195 buffer
[0] == '\n'|| buffer
[0] == '\t')) {
1197 * Get the provider name from this line and check if
1198 * this is the entry to be updated. Note: can not use
1199 * "buffer" directly because strtok will change its
1202 (void) strlcpy(buffer2
, buffer
, BUFSIZ
);
1204 /* get rid of trailing '\n' */
1205 len
= strlen(buffer2
);
1206 if (buffer2
[len
-1] == '\n') {
1209 buffer2
[len
] = '\0';
1211 if ((name
= strtok(buffer2
, SEP_COLON
)) == NULL
) {
1214 } else if (strcmp(puent
->name
, name
) == 0) {
1221 * This is the entry to be modified, get the updated
1224 if ((str
= uent2str(puent
)) == NULL
) {
1228 (void) strlcpy(buffer
, str
, BUFSIZ
);
1233 if (fputs(buffer
, pfile_tmp
) == EOF
) {
1235 cryptoerror(LOG_STDERR
, gettext(
1236 "failed to write to a temp file: %s."),
1243 if (rc
== FAILURE
) {
1244 (void) fclose(pfile
);
1245 (void) fclose(pfile_tmp
);
1246 if (unlink(tmpfile_name
) != 0) {
1248 cryptoerror(LOG_STDERR
, gettext(
1249 "(Warning) failed to remove %s: %s"),
1250 tmpfile_name
, strerror(err
));
1255 (void) fclose(pfile
);
1256 if (fclose(pfile_tmp
) != 0) {
1258 cryptoerror(LOG_STDERR
,
1259 gettext("failed to close %s: %s"), tmpfile_name
,
1264 /* Copy the temporary file to the pkcs11.conf file */
1265 if (rename(tmpfile_name
, _PATH_PKCS11_CONF
) == -1) {
1267 cryptoerror(LOG_STDERR
,
1268 gettext("failed to update the configuration - %s"),
1270 cryptodebug("failed to rename %s to %s: %s", tmpfile_name
,
1271 _PATH_PKCS11_CONF
, strerror(err
));
1273 } else if (chmod(_PATH_PKCS11_CONF
,
1274 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) == -1) {
1276 cryptoerror(LOG_STDERR
,
1277 gettext("failed to update the configuration - %s"),
1279 cryptodebug("failed to chmod to %s: %s", _PATH_PKCS11_CONF
,
1286 if ((rc
== FAILURE
) && (unlink(tmpfile_name
) != 0)) {
1288 cryptoerror(LOG_STDERR
, gettext(
1289 "(Warning) failed to remove %s: %s"),
1290 tmpfile_name
, strerror(err
));
1298 * Convert an uentry to a character string
1301 uent2str(uentry_t
*puent
)
1304 boolean_t tok1_present
= B_FALSE
;
1306 char blank_buf
[128];
1308 if (puent
== NULL
) {
1309 cryptoerror(LOG_STDERR
, gettext("internal error."));
1313 buf
= malloc(BUFSIZ
);
1315 cryptoerror(LOG_STDERR
, gettext("out of memory."));
1319 /* convert the library name */
1320 if (strlcpy(buf
, puent
->name
, BUFSIZ
) >= BUFSIZ
) {
1326 /* convert the enabledlist or the disabledlist */
1327 if (puent
->flag_enabledlist
== B_TRUE
) {
1328 if (strlcat(buf
, SEP_COLON
, BUFSIZ
) >= BUFSIZ
) {
1333 if (strlcat(buf
, EF_ENABLED
, BUFSIZ
) >= BUFSIZ
) {
1338 phead
= puent
->policylist
;
1339 while (phead
!= NULL
) {
1340 if (strlcat(buf
, phead
->name
, BUFSIZ
) >= BUFSIZ
) {
1345 phead
= phead
->next
;
1346 if (phead
!= NULL
) {
1347 if (strlcat(buf
, SEP_COMMA
, BUFSIZ
)
1354 tok1_present
= B_TRUE
;
1355 } else if (puent
->policylist
!= NULL
) {
1356 if (strlcat(buf
, SEP_COLON
, BUFSIZ
) >= BUFSIZ
) {
1361 if (strlcat(buf
, EF_DISABLED
, BUFSIZ
) >= BUFSIZ
) {
1365 phead
= puent
->policylist
;
1366 while (phead
!= NULL
) {
1367 if (strlcat(buf
, phead
->name
, BUFSIZ
) >= BUFSIZ
) {
1372 phead
= phead
->next
;
1373 if (phead
!= NULL
) {
1374 if (strlcat(buf
, SEP_COMMA
, BUFSIZ
)
1381 tok1_present
= B_TRUE
;
1384 if (puent
->flag_norandom
== B_TRUE
) {
1385 if (strlcat(buf
, (tok1_present
? SEP_SEMICOLON
: SEP_COLON
),
1386 BUFSIZ
) >= BUFSIZ
) {
1391 if (strlcat(buf
, EF_NORANDOM
, BUFSIZ
) >= BUFSIZ
) {
1397 if (strcmp(puent
->name
, METASLOT_KEYWORD
) == 0) {
1399 /* write the metaslot_status= value */
1400 if (strlcat(buf
, (tok1_present
? SEP_SEMICOLON
: SEP_COLON
),
1401 BUFSIZ
) >= BUFSIZ
) {
1406 if (strlcat(buf
, METASLOT_STATUS
, BUFSIZ
) >= BUFSIZ
) {
1411 if (puent
->flag_metaslot_enabled
) {
1412 if (strlcat(buf
, ENABLED_KEYWORD
, BUFSIZ
) >= BUFSIZ
) {
1417 if (strlcat(buf
, DISABLED_KEYWORD
, BUFSIZ
)
1424 if (!tok1_present
) {
1425 tok1_present
= B_TRUE
;
1428 if (strlcat(buf
, SEP_SEMICOLON
, BUFSIZ
) >= BUFSIZ
) {
1433 if (strlcat(buf
, METASLOT_AUTO_KEY_MIGRATE
, BUFSIZ
) >= BUFSIZ
) {
1438 if (puent
->flag_metaslot_auto_key_migrate
) {
1439 if (strlcat(buf
, ENABLED_KEYWORD
, BUFSIZ
) >= BUFSIZ
) {
1444 if (strlcat(buf
, DISABLED_KEYWORD
, BUFSIZ
) >= BUFSIZ
) {
1450 bzero(blank_buf
, sizeof (blank_buf
));
1452 /* write metaslot_token= if specified */
1453 if (memcmp(puent
->metaslot_ks_token
, blank_buf
,
1454 TOKEN_LABEL_SIZE
) != 0) {
1455 /* write the metaslot_status= value */
1456 if (strlcat(buf
, (tok1_present
?
1457 SEP_SEMICOLON
: SEP_COLON
), BUFSIZ
) >= BUFSIZ
) {
1462 if (strlcat(buf
, METASLOT_TOKEN
, BUFSIZ
) >= BUFSIZ
) {
1468 (const char *)puent
->metaslot_ks_token
, BUFSIZ
)
1475 /* write metaslot_slot= if specified */
1476 if (memcmp(puent
->metaslot_ks_slot
, blank_buf
,
1477 SLOT_DESCRIPTION_SIZE
) != 0) {
1478 /* write the metaslot_status= value */
1479 if (strlcat(buf
, (tok1_present
?
1480 SEP_SEMICOLON
: SEP_COLON
), BUFSIZ
) >= BUFSIZ
) {
1485 if (strlcat(buf
, METASLOT_SLOT
, BUFSIZ
) >= BUFSIZ
) {
1491 (const char *)puent
->metaslot_ks_slot
, BUFSIZ
)
1499 if (strlcat(buf
, "\n", BUFSIZ
) >= BUFSIZ
) {
1509 * This function updates the default policy mode and the policy exception list
1510 * for a user-level provider based on the mechanism specified in the disable
1511 * or enable subcommand and the update mode. This function is called by the
1512 * enable_uef_lib() or disable_uef_lib().
1515 update_policylist(uentry_t
*puent
, mechlist_t
*marglist
, int update_mode
)
1517 CK_MECHANISM_TYPE mech_type
;
1521 umechlist_t
*pumech
;
1525 if ((puent
== NULL
) || (marglist
== NULL
)) {
1526 /* should not happen */
1527 cryptoerror(LOG_STDERR
, gettext("internal error."));
1528 cryptodebug("update_policylist()- puent or marglist is NULL.");
1532 if ((update_mode
!= ADD_MODE
) && (update_mode
!= DELETE_MODE
)) {
1533 /* should not happen */
1534 cryptoerror(LOG_STDERR
, gettext("internal error."));
1535 cryptodebug("update_policylist() - update_mode is incorrect.");
1540 * For each mechanism operand, get its mechanism type first.
1541 * If fails to get the mechanism type, the mechanism operand must be
1542 * invalid, gives an warning and ignore it. Otherwise,
1543 * - convert the mechanism type to the internal representation (hex)
1544 * in the pkcs11.conf file
1545 * - If update_mode == DELETE_MODE,
1546 * If the mechanism is in the policy list, delete it.
1547 * If the mechanism is not in the policy list, do nothing.
1548 * - If update_mode == ADD_MODE,
1549 * If the mechanism is not in the policy list, add it.
1550 * If the mechanism is in the policy list already, do nothing.
1553 if (pkcs11_str2mech(marglist
->name
, &mech_type
) != CKR_OK
) {
1555 * This mechanism is not a valid PKCS11 mechanism,
1556 * give warning and ignore it.
1558 cryptoerror(LOG_STDERR
, gettext(
1559 "(Warning) %s is not a valid PKCS#11 mechanism."),
1563 (void) snprintf(midname
, sizeof (midname
), "%#010x",
1565 if (update_mode
== DELETE_MODE
) {
1567 phead
= pcur
= puent
->policylist
;
1568 while (!found
&& pcur
) {
1569 if (strcmp(pcur
->name
, midname
) == 0) {
1578 if (phead
== pcur
) {
1580 puent
->policylist
->next
;
1583 phead
->next
= pcur
->next
;
1587 if (puent
->count
== 0) {
1588 puent
->policylist
= NULL
;
1591 } else if (update_mode
== ADD_MODE
) {
1592 if (!is_in_policylist(midname
,
1593 puent
->policylist
)) {
1594 pumech
= create_umech(midname
);
1595 if (pumech
== NULL
) {
1599 phead
= puent
->policylist
;
1600 puent
->policylist
= pumech
;
1601 pumech
->next
= phead
;
1606 marglist
= marglist
->next
;
1613 * Open a session to the given slot and check if we can do
1614 * random numbers by asking for one byte.
1617 check_random(CK_SLOT_ID slot_id
, CK_FUNCTION_LIST_PTR prov_funcs
)
1620 CK_SESSION_HANDLE hSession
;
1622 CK_BYTE_PTR test_byte_ptr
= &test_byte
;
1624 rv
= prov_funcs
->C_OpenSession(slot_id
, CKF_SERIAL_SESSION
,
1625 NULL_PTR
, NULL
, &hSession
);
1629 /* We care only about the return value */
1630 rv
= prov_funcs
->C_GenerateRandom(hSession
, test_byte_ptr
,
1631 sizeof (test_byte
));
1632 (void) prov_funcs
->C_CloseSession(hSession
);
1635 * These checks are purely to determine whether the slot can do
1636 * random numbers. So, we don't check whether the routine
1637 * succeeds. The reason we check for CKR_RANDOM_NO_RNG also is that
1638 * this error effectively means CKR_FUNCTION_NOT_SUPPORTED.
1640 if (rv
!= CKR_FUNCTION_NOT_SUPPORTED
&& rv
!= CKR_RANDOM_NO_RNG
)
1647 display_verbose_mech_header()
1649 (void) printf("%28s %s", " ", HDR1
);
1650 (void) printf("%28s %s", " ", HDR2
);
1651 (void) printf("%28s %s", " ", HDR3
);
1652 (void) printf("%28s %s", " ", HDR4
);
1653 (void) printf("%28s %s", " ", HDR5
);
1654 (void) printf("%28s %s", " ", HDR6
);
1655 (void) printf("%-28.28s %s", gettext("mechanism name"), HDR7
);
1658 * Strictly for appearance's sake, the first header line should be
1659 * as long as the length of the translated text above. The format
1660 * lengths should all match too.
1662 (void) printf("%28s ---- ---- "
1663 "- - - - - - - - - - - - - -\n",
1664 gettext("----------------------------"));