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 * This file is part of the core Kernel Cryptographic Framework.
28 * It implements the management of tables of Providers. Entries to
29 * added and removed when cryptographic providers register with
30 * and unregister from the framework, respectively. The KCF scheduler
31 * and ioctl pseudo driver call this function to obtain the list
32 * of available providers.
34 * The provider table is indexed by crypto_provider_id_t. Each
35 * element of the table contains a pointer to a provider descriptor,
36 * or NULL if the entry is free.
38 * This file also implements helper functions to allocate and free
39 * provider descriptors.
42 #include <sys/zfs_context.h>
43 #include <sys/crypto/common.h>
44 #include <sys/crypto/impl.h>
45 #include <sys/crypto/sched_impl.h>
46 #include <sys/crypto/spi.h>
48 #define KCF_MAX_PROVIDERS 512 /* max number of providers */
51 * Prov_tab is an array of providers which is updated when
52 * a crypto provider registers with kcf. The provider calls the
53 * SPI routine, crypto_register_provider(), which in turn calls
54 * kcf_prov_tab_add_provider().
56 * A provider unregisters by calling crypto_unregister_provider()
57 * which triggers the removal of the prov_tab entry.
58 * It also calls kcf_remove_mech_provider().
60 * prov_tab entries are not updated from kcf.conf or by cryptoadm(1M).
62 static kcf_provider_desc_t **prov_tab = NULL;
63 static kmutex_t prov_tab_mutex; /* ensure exclusive access to the table */
64 static uint_t prov_tab_num = 0; /* number of providers in table */
65 static uint_t prov_tab_max = KCF_MAX_PROVIDERS;
68 kcf_prov_tab_destroy(void)
70 mutex_destroy(&prov_tab_mutex);
73 kmem_free(prov_tab, prov_tab_max *
74 sizeof (kcf_provider_desc_t *));
78 * Initialize a mutex and the KCF providers table, prov_tab.
79 * The providers table is dynamically allocated with prov_tab_max entries.
80 * Called from kcf module _init().
83 kcf_prov_tab_init(void)
85 mutex_init(&prov_tab_mutex, NULL, MUTEX_DEFAULT, NULL);
87 prov_tab = kmem_zalloc(prov_tab_max * sizeof (kcf_provider_desc_t *),
92 * Add a provider to the provider table. If no free entry can be found
93 * for the new provider, returns CRYPTO_HOST_MEMORY. Otherwise, add
94 * the provider to the table, initialize the pd_prov_id field
95 * of the specified provider descriptor to the index in that table,
96 * and return CRYPTO_SUCCESS. Note that a REFHOLD is done on the
97 * provider when pointed to by a table entry.
100 kcf_prov_tab_add_provider(kcf_provider_desc_t *prov_desc)
104 ASSERT(prov_tab != NULL);
106 mutex_enter(&prov_tab_mutex);
108 /* find free slot in providers table */
109 for (i = 1; i < KCF_MAX_PROVIDERS && prov_tab[i] != NULL; i++)
111 if (i == KCF_MAX_PROVIDERS) {
112 /* ran out of providers entries */
113 mutex_exit(&prov_tab_mutex);
114 cmn_err(CE_WARN, "out of providers entries");
115 return (CRYPTO_HOST_MEMORY);
118 /* initialize entry */
119 prov_tab[i] = prov_desc;
120 KCF_PROV_REFHOLD(prov_desc);
121 KCF_PROV_IREFHOLD(prov_desc);
124 mutex_exit(&prov_tab_mutex);
126 /* update provider descriptor */
127 prov_desc->pd_prov_id = i;
130 * The KCF-private provider handle is defined as the internal
133 prov_desc->pd_kcf_prov_handle =
134 (crypto_kcf_provider_handle_t)prov_desc->pd_prov_id;
136 return (CRYPTO_SUCCESS);
140 * Remove the provider specified by its id. A REFRELE is done on the
141 * corresponding provider descriptor before this function returns.
142 * Returns CRYPTO_UNKNOWN_PROVIDER if the provider id is not valid.
145 kcf_prov_tab_rem_provider(crypto_provider_id_t prov_id)
147 kcf_provider_desc_t *prov_desc;
149 ASSERT(prov_tab != NULL);
150 ASSERT(prov_tab_num >= 0);
153 * Validate provider id, since it can be specified by a 3rd-party
157 mutex_enter(&prov_tab_mutex);
158 if (prov_id >= KCF_MAX_PROVIDERS ||
159 ((prov_desc = prov_tab[prov_id]) == NULL)) {
160 mutex_exit(&prov_tab_mutex);
161 return (CRYPTO_INVALID_PROVIDER_ID);
163 mutex_exit(&prov_tab_mutex);
166 * The provider id must remain valid until the associated provider
167 * descriptor is freed. For this reason, we simply release our
168 * reference to the descriptor here. When the reference count
169 * reaches zero, kcf_free_provider_desc() will be invoked and
170 * the associated entry in the providers table will be released
174 KCF_PROV_REFRELE(prov_desc);
175 KCF_PROV_IREFRELE(prov_desc);
177 return (CRYPTO_SUCCESS);
181 * Returns the provider descriptor corresponding to the specified
182 * provider id. A REFHOLD is done on the descriptor before it is
183 * returned to the caller. It is the responsibility of the caller
184 * to do a REFRELE once it is done with the provider descriptor.
186 kcf_provider_desc_t *
187 kcf_prov_tab_lookup(crypto_provider_id_t prov_id)
189 kcf_provider_desc_t *prov_desc;
191 mutex_enter(&prov_tab_mutex);
193 prov_desc = prov_tab[prov_id];
195 if (prov_desc == NULL) {
196 mutex_exit(&prov_tab_mutex);
200 KCF_PROV_REFHOLD(prov_desc);
202 mutex_exit(&prov_tab_mutex);
208 allocate_ops_v1(crypto_ops_t *src, crypto_ops_t *dst, uint_t *mech_list_count)
210 if (src->co_control_ops != NULL)
211 dst->co_control_ops = kmem_alloc(sizeof (crypto_control_ops_t),
214 if (src->co_digest_ops != NULL)
215 dst->co_digest_ops = kmem_alloc(sizeof (crypto_digest_ops_t),
218 if (src->co_cipher_ops != NULL)
219 dst->co_cipher_ops = kmem_alloc(sizeof (crypto_cipher_ops_t),
222 if (src->co_mac_ops != NULL)
223 dst->co_mac_ops = kmem_alloc(sizeof (crypto_mac_ops_t),
226 if (src->co_sign_ops != NULL)
227 dst->co_sign_ops = kmem_alloc(sizeof (crypto_sign_ops_t),
230 if (src->co_verify_ops != NULL)
231 dst->co_verify_ops = kmem_alloc(sizeof (crypto_verify_ops_t),
234 if (src->co_dual_ops != NULL)
235 dst->co_dual_ops = kmem_alloc(sizeof (crypto_dual_ops_t),
238 if (src->co_dual_cipher_mac_ops != NULL)
239 dst->co_dual_cipher_mac_ops = kmem_alloc(
240 sizeof (crypto_dual_cipher_mac_ops_t), KM_SLEEP);
242 if (src->co_random_ops != NULL) {
243 dst->co_random_ops = kmem_alloc(
244 sizeof (crypto_random_number_ops_t), KM_SLEEP);
247 * Allocate storage to store the array of supported mechanisms
248 * specified by provider. We allocate extra mechanism storage
249 * if the provider has random_ops since we keep an internal
250 * mechanism, SUN_RANDOM, in this case.
252 (*mech_list_count)++;
255 if (src->co_session_ops != NULL)
256 dst->co_session_ops = kmem_alloc(sizeof (crypto_session_ops_t),
259 if (src->co_object_ops != NULL)
260 dst->co_object_ops = kmem_alloc(sizeof (crypto_object_ops_t),
263 if (src->co_key_ops != NULL)
264 dst->co_key_ops = kmem_alloc(sizeof (crypto_key_ops_t),
267 if (src->co_provider_ops != NULL)
268 dst->co_provider_ops = kmem_alloc(
269 sizeof (crypto_provider_management_ops_t), KM_SLEEP);
271 if (src->co_ctx_ops != NULL)
272 dst->co_ctx_ops = kmem_alloc(sizeof (crypto_ctx_ops_t),
277 allocate_ops_v2(crypto_ops_t *src, crypto_ops_t *dst)
279 if (src->co_mech_ops != NULL)
280 dst->co_mech_ops = kmem_alloc(sizeof (crypto_mech_ops_t),
285 allocate_ops_v3(crypto_ops_t *src, crypto_ops_t *dst)
287 if (src->co_nostore_key_ops != NULL)
288 dst->co_nostore_key_ops =
289 kmem_alloc(sizeof (crypto_nostore_key_ops_t), KM_SLEEP);
293 * Allocate a provider descriptor. mech_list_count specifies the
294 * number of mechanisms supported by the providers, and is used
295 * to allocate storage for the mechanism table.
296 * This function may sleep while allocating memory, which is OK
297 * since it is invoked from user context during provider registration.
299 kcf_provider_desc_t *
300 kcf_alloc_provider_desc(crypto_provider_info_t *info)
303 kcf_provider_desc_t *desc;
304 uint_t mech_list_count = info->pi_mech_list_count;
305 crypto_ops_t *src_ops = info->pi_ops_vector;
307 desc = kmem_zalloc(sizeof (kcf_provider_desc_t), KM_SLEEP);
310 * pd_description serves two purposes
311 * - Appears as a blank padded PKCS#11 style string, that will be
312 * returned to applications in CK_SLOT_INFO.slotDescription.
313 * This means that we should not have a null character in the
314 * first CRYPTO_PROVIDER_DESCR_MAX_LEN bytes.
315 * - Appears as a null-terminated string that can be used by
316 * other kcf routines.
318 * So, we allocate enough room for one extra null terminator
319 * which keeps every one happy.
321 desc->pd_description = kmem_alloc(CRYPTO_PROVIDER_DESCR_MAX_LEN + 1,
323 (void) memset(desc->pd_description, ' ',
324 CRYPTO_PROVIDER_DESCR_MAX_LEN);
325 desc->pd_description[CRYPTO_PROVIDER_DESCR_MAX_LEN] = '\0';
328 * Since the framework does not require the ops vector specified
329 * by the providers during registration to be persistent,
330 * KCF needs to allocate storage where copies of the ops
331 * vectors are copied.
333 desc->pd_ops_vector = kmem_zalloc(sizeof (crypto_ops_t), KM_SLEEP);
335 if (info->pi_provider_type != CRYPTO_LOGICAL_PROVIDER) {
336 allocate_ops_v1(src_ops, desc->pd_ops_vector, &mech_list_count);
337 if (info->pi_interface_version >= CRYPTO_SPI_VERSION_2)
338 allocate_ops_v2(src_ops, desc->pd_ops_vector);
339 if (info->pi_interface_version == CRYPTO_SPI_VERSION_3)
340 allocate_ops_v3(src_ops, desc->pd_ops_vector);
343 desc->pd_mech_list_count = mech_list_count;
344 desc->pd_mechanisms = kmem_zalloc(sizeof (crypto_mech_info_t) *
345 mech_list_count, KM_SLEEP);
346 for (i = 0; i < KCF_OPS_CLASSSIZE; i++)
347 for (j = 0; j < KCF_MAXMECHTAB; j++)
348 desc->pd_mech_indx[i][j] = KCF_INVALID_INDX;
350 desc->pd_prov_id = KCF_PROVID_INVALID;
351 desc->pd_state = KCF_PROV_ALLOCATED;
353 mutex_init(&desc->pd_lock, NULL, MUTEX_DEFAULT, NULL);
354 cv_init(&desc->pd_resume_cv, NULL, CV_DEFAULT, NULL);
355 cv_init(&desc->pd_remove_cv, NULL, CV_DEFAULT, NULL);
361 * Called by KCF_PROV_REFRELE when a provider's reference count drops
362 * to zero. We free the descriptor when the last reference is released.
363 * However, for software providers, we do not free it when there is an
364 * unregister thread waiting. We signal that thread in this case and
365 * that thread is responsible for freeing the descriptor.
368 kcf_provider_zero_refcnt(kcf_provider_desc_t *desc)
370 mutex_enter(&desc->pd_lock);
371 switch (desc->pd_prov_type) {
372 case CRYPTO_SW_PROVIDER:
373 if (desc->pd_state == KCF_PROV_REMOVED ||
374 desc->pd_state == KCF_PROV_DISABLED) {
375 desc->pd_state = KCF_PROV_FREED;
376 cv_broadcast(&desc->pd_remove_cv);
377 mutex_exit(&desc->pd_lock);
382 case CRYPTO_HW_PROVIDER:
383 case CRYPTO_LOGICAL_PROVIDER:
384 mutex_exit(&desc->pd_lock);
385 kcf_free_provider_desc(desc);
390 * Free a provider descriptor.
393 kcf_free_provider_desc(kcf_provider_desc_t *desc)
398 mutex_enter(&prov_tab_mutex);
399 if (desc->pd_prov_id != KCF_PROVID_INVALID) {
400 /* release the associated providers table entry */
401 ASSERT(prov_tab[desc->pd_prov_id] != NULL);
402 prov_tab[desc->pd_prov_id] = NULL;
405 mutex_exit(&prov_tab_mutex);
407 /* free the kernel memory associated with the provider descriptor */
409 if (desc->pd_description != NULL)
410 kmem_free(desc->pd_description,
411 CRYPTO_PROVIDER_DESCR_MAX_LEN + 1);
413 if (desc->pd_ops_vector != NULL) {
415 if (desc->pd_ops_vector->co_control_ops != NULL)
416 kmem_free(desc->pd_ops_vector->co_control_ops,
417 sizeof (crypto_control_ops_t));
419 if (desc->pd_ops_vector->co_digest_ops != NULL)
420 kmem_free(desc->pd_ops_vector->co_digest_ops,
421 sizeof (crypto_digest_ops_t));
423 if (desc->pd_ops_vector->co_cipher_ops != NULL)
424 kmem_free(desc->pd_ops_vector->co_cipher_ops,
425 sizeof (crypto_cipher_ops_t));
427 if (desc->pd_ops_vector->co_mac_ops != NULL)
428 kmem_free(desc->pd_ops_vector->co_mac_ops,
429 sizeof (crypto_mac_ops_t));
431 if (desc->pd_ops_vector->co_sign_ops != NULL)
432 kmem_free(desc->pd_ops_vector->co_sign_ops,
433 sizeof (crypto_sign_ops_t));
435 if (desc->pd_ops_vector->co_verify_ops != NULL)
436 kmem_free(desc->pd_ops_vector->co_verify_ops,
437 sizeof (crypto_verify_ops_t));
439 if (desc->pd_ops_vector->co_dual_ops != NULL)
440 kmem_free(desc->pd_ops_vector->co_dual_ops,
441 sizeof (crypto_dual_ops_t));
443 if (desc->pd_ops_vector->co_dual_cipher_mac_ops != NULL)
444 kmem_free(desc->pd_ops_vector->co_dual_cipher_mac_ops,
445 sizeof (crypto_dual_cipher_mac_ops_t));
447 if (desc->pd_ops_vector->co_random_ops != NULL)
448 kmem_free(desc->pd_ops_vector->co_random_ops,
449 sizeof (crypto_random_number_ops_t));
451 if (desc->pd_ops_vector->co_session_ops != NULL)
452 kmem_free(desc->pd_ops_vector->co_session_ops,
453 sizeof (crypto_session_ops_t));
455 if (desc->pd_ops_vector->co_object_ops != NULL)
456 kmem_free(desc->pd_ops_vector->co_object_ops,
457 sizeof (crypto_object_ops_t));
459 if (desc->pd_ops_vector->co_key_ops != NULL)
460 kmem_free(desc->pd_ops_vector->co_key_ops,
461 sizeof (crypto_key_ops_t));
463 if (desc->pd_ops_vector->co_provider_ops != NULL)
464 kmem_free(desc->pd_ops_vector->co_provider_ops,
465 sizeof (crypto_provider_management_ops_t));
467 if (desc->pd_ops_vector->co_ctx_ops != NULL)
468 kmem_free(desc->pd_ops_vector->co_ctx_ops,
469 sizeof (crypto_ctx_ops_t));
471 if (desc->pd_ops_vector->co_mech_ops != NULL)
472 kmem_free(desc->pd_ops_vector->co_mech_ops,
473 sizeof (crypto_mech_ops_t));
475 if (desc->pd_ops_vector->co_nostore_key_ops != NULL)
476 kmem_free(desc->pd_ops_vector->co_nostore_key_ops,
477 sizeof (crypto_nostore_key_ops_t));
479 kmem_free(desc->pd_ops_vector, sizeof (crypto_ops_t));
482 if (desc->pd_mechanisms != NULL)
483 /* free the memory associated with the mechanism info's */
484 kmem_free(desc->pd_mechanisms, sizeof (crypto_mech_info_t) *
485 desc->pd_mech_list_count);
487 if (desc->pd_sched_info.ks_taskq != NULL)
488 taskq_destroy(desc->pd_sched_info.ks_taskq);
490 mutex_destroy(&desc->pd_lock);
491 cv_destroy(&desc->pd_resume_cv);
492 cv_destroy(&desc->pd_remove_cv);
494 kmem_free(desc, sizeof (kcf_provider_desc_t));
498 * Returns an array of hardware and logical provider descriptors,
499 * a.k.a the PKCS#11 slot list. A REFHOLD is done on each descriptor
500 * before the array is returned. The entire table can be freed by
501 * calling kcf_free_provider_tab().
504 kcf_get_slot_list(uint_t *count, kcf_provider_desc_t ***array,
505 boolean_t unverified)
507 kcf_provider_desc_t *prov_desc;
508 kcf_provider_desc_t **p = NULL;
512 int rval = CRYPTO_SUCCESS;
513 size_t n, final_size;
515 /* count the providers */
516 mutex_enter(&prov_tab_mutex);
517 for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
518 if ((prov_desc = prov_tab[i]) != NULL &&
519 ((prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER &&
520 (prov_desc->pd_flags & CRYPTO_HIDE_PROVIDER) == 0) ||
521 prov_desc->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)) {
522 if (KCF_IS_PROV_USABLE(prov_desc) ||
523 (unverified && KCF_IS_PROV_UNVERIFIED(prov_desc))) {
528 mutex_exit(&prov_tab_mutex);
533 n = cnt * sizeof (kcf_provider_desc_t *);
535 p = kmem_zalloc(n, KM_SLEEP);
537 /* pointer to last entry in the array */
538 last = (char *)&p[cnt-1];
540 mutex_enter(&prov_tab_mutex);
541 /* fill the slot list */
542 for (i = 0, j = 0; i < KCF_MAX_PROVIDERS; i++) {
543 if ((prov_desc = prov_tab[i]) != NULL &&
544 ((prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER &&
545 (prov_desc->pd_flags & CRYPTO_HIDE_PROVIDER) == 0) ||
546 prov_desc->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)) {
547 if (KCF_IS_PROV_USABLE(prov_desc) ||
548 (unverified && KCF_IS_PROV_UNVERIFIED(prov_desc))) {
549 if ((char *)&p[j] > last) {
550 mutex_exit(&prov_tab_mutex);
551 kcf_free_provider_tab(cnt, p);
557 KCF_PROV_REFHOLD(prov_desc);
561 mutex_exit(&prov_tab_mutex);
563 final_size = j * sizeof (kcf_provider_desc_t *);
565 ASSERT(final_size <= n);
567 /* check if buffer we allocated is too large */
568 if (final_size < n) {
569 char *final_buffer = NULL;
571 if (final_size > 0) {
572 final_buffer = kmem_alloc(final_size, KM_SLEEP);
573 bcopy(p, final_buffer, final_size);
576 p = (kcf_provider_desc_t **)final_buffer;
585 * Free an array of hardware provider descriptors. A REFRELE
586 * is done on each descriptor before the table is freed.
589 kcf_free_provider_tab(uint_t count, kcf_provider_desc_t **array)
591 kcf_provider_desc_t *prov_desc;
594 for (i = 0; i < count; i++) {
595 if ((prov_desc = array[i]) != NULL) {
596 KCF_PROV_REFRELE(prov_desc);
599 kmem_free(array, count * sizeof (kcf_provider_desc_t *));
603 * Returns in the location pointed to by pd a pointer to the descriptor
604 * for the software provider for the specified mechanism.
605 * The provider descriptor is returned held and it is the caller's
606 * responsibility to release it when done. The mechanism entry
607 * is returned if the optional argument mep is non NULL.
609 * Returns one of the CRYPTO_ * error codes on failure, and
610 * CRYPTO_SUCCESS on success.
613 kcf_get_sw_prov(crypto_mech_type_t mech_type, kcf_provider_desc_t **pd,
614 kcf_mech_entry_t **mep, boolean_t log_warn)
616 kcf_mech_entry_t *me;
618 /* get the mechanism entry for this mechanism */
619 if (kcf_get_mech_entry(mech_type, &me) != KCF_SUCCESS)
620 return (CRYPTO_MECHANISM_INVALID);
623 * Get the software provider for this mechanism.
624 * Lock the mech_entry until we grab the 'pd'.
626 mutex_enter(&me->me_mutex);
628 if (me->me_sw_prov == NULL ||
629 (*pd = me->me_sw_prov->pm_prov_desc) == NULL) {
630 /* no SW provider for this mechanism */
632 cmn_err(CE_WARN, "no SW provider for \"%s\"\n",
634 mutex_exit(&me->me_mutex);
635 return (CRYPTO_MECH_NOT_SUPPORTED);
638 KCF_PROV_REFHOLD(*pd);
639 mutex_exit(&me->me_mutex);
644 return (CRYPTO_SUCCESS);