xref: /illumos-gate/usr/src/uts/common/crypto/core/kcf_prov_tabs.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
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.
33  *
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.
37  *
38  * This file also implements helper functions to allocate and free
39  * provider descriptors.
40  */
41 
42 #include <sys/types.h>
43 #include <sys/kmem.h>
44 #include <sys/cmn_err.h>
45 #include <sys/ddi.h>
46 #include <sys/sunddi.h>
47 #include <sys/ksynch.h>
48 #include <sys/crypto/common.h>
49 #include <sys/crypto/impl.h>
50 #include <sys/crypto/sched_impl.h>
51 #include <sys/crypto/spi.h>
52 
53 #define	KCF_MAX_PROVIDERS	512	/* max number of providers */
54 
55 /*
56  * Prov_tab is an array of providers which is updated when
57  * a crypto provider registers with kcf. The provider calls the
58  * SPI routine, crypto_register_provider(), which in turn calls
59  * kcf_prov_tab_add_provider().
60  *
61  * A provider unregisters by calling crypto_unregister_provider()
62  * which triggers the removal of the prov_tab entry.
63  * It also calls kcf_remove_mech_provider().
64  *
65  * prov_tab entries are not updated from kcf.conf or by cryptoadm(1M).
66  */
67 static kcf_provider_desc_t **prov_tab = NULL;
68 kmutex_t prov_tab_mutex; /* ensure exclusive access to the table */
69 static uint_t prov_tab_num = 0; /* number of providers in table */
70 static uint_t prov_tab_max = KCF_MAX_PROVIDERS;
71 
72 static void kcf_free_unregistered_provs();
73 #if DEBUG
74 extern int kcf_frmwrk_debug;
75 static void kcf_prov_tab_dump(char *message);
76 #endif /* DEBUG */
77 
78 
79 /*
80  * Initialize a mutex and the KCF providers table, prov_tab.
81  * The providers table is dynamically allocated with prov_tab_max entries.
82  * Called from kcf module _init().
83  */
84 void
85 kcf_prov_tab_init(void)
86 {
87 	mutex_init(&prov_tab_mutex, NULL, MUTEX_DRIVER, NULL);
88 
89 	prov_tab = kmem_zalloc(prov_tab_max * sizeof (kcf_provider_desc_t *),
90 	    KM_SLEEP);
91 }
92 
93 /*
94  * Add a provider to the provider table. If no free entry can be found
95  * for the new provider, returns CRYPTO_HOST_MEMORY. Otherwise, add
96  * the provider to the table, initialize the pd_prov_id field
97  * of the specified provider descriptor to the index in that table,
98  * and return CRYPTO_SUCCESS. Note that a REFHOLD is done on the
99  * provider when pointed to by a table entry.
100  */
101 int
102 kcf_prov_tab_add_provider(kcf_provider_desc_t *prov_desc)
103 {
104 	uint_t i;
105 
106 	ASSERT(prov_tab != NULL);
107 
108 	mutex_enter(&prov_tab_mutex);
109 
110 	/* see if any slots can be freed */
111 	if (kcf_need_provtab_walk)
112 		kcf_free_unregistered_provs();
113 
114 	/* find free slot in providers table */
115 	for (i = 0; i < KCF_MAX_PROVIDERS && prov_tab[i] != NULL; i++)
116 		;
117 	if (i == KCF_MAX_PROVIDERS) {
118 		/* ran out of providers entries */
119 		mutex_exit(&prov_tab_mutex);
120 		cmn_err(CE_WARN, "out of providers entries");
121 		return (CRYPTO_HOST_MEMORY);
122 	}
123 
124 	/* initialize entry */
125 	prov_tab[i] = prov_desc;
126 	KCF_PROV_REFHOLD(prov_desc);
127 	prov_tab_num++;
128 
129 	mutex_exit(&prov_tab_mutex);
130 
131 	/* update provider descriptor */
132 	prov_desc->pd_prov_id = i;
133 
134 	/*
135 	 * The KCF-private provider handle is defined as the internal
136 	 * provider id.
137 	 */
138 	prov_desc->pd_kcf_prov_handle =
139 	    (crypto_kcf_provider_handle_t)prov_desc->pd_prov_id;
140 
141 #if DEBUG
142 	if (kcf_frmwrk_debug >= 1)
143 		kcf_prov_tab_dump("kcf_prov_tab_add_provider");
144 #endif /* DEBUG */
145 
146 	return (CRYPTO_SUCCESS);
147 }
148 
149 /*
150  * Remove the provider specified by its id. A REFRELE is done on the
151  * corresponding provider descriptor before this function returns.
152  * Returns CRYPTO_UNKNOWN_PROVIDER if the provider id is not valid.
153  */
154 int
155 kcf_prov_tab_rem_provider(crypto_provider_id_t prov_id)
156 {
157 	kcf_provider_desc_t *prov_desc;
158 
159 	ASSERT(prov_tab != NULL);
160 	ASSERT(prov_tab_num >= 0);
161 
162 	/*
163 	 * Validate provider id, since it can be specified by a 3rd-party
164 	 * provider.
165 	 */
166 
167 	mutex_enter(&prov_tab_mutex);
168 	if (prov_id >= KCF_MAX_PROVIDERS ||
169 	    ((prov_desc = prov_tab[prov_id]) == NULL)) {
170 		mutex_exit(&prov_tab_mutex);
171 		return (CRYPTO_INVALID_PROVIDER_ID);
172 	}
173 
174 	if (kcf_need_provtab_walk)
175 		kcf_free_unregistered_provs();
176 	mutex_exit(&prov_tab_mutex);
177 
178 	/*
179 	 * The provider id must remain valid until the associated provider
180 	 * descriptor is freed. For this reason, we simply release our
181 	 * reference to the descriptor here. When the reference count
182 	 * reaches zero, kcf_free_provider_desc() will be invoked and
183 	 * the associated entry in the providers table will be released
184 	 * at that time.
185 	 */
186 
187 	KCF_PROV_REFRELE(prov_desc);
188 
189 #if DEBUG
190 	if (kcf_frmwrk_debug >= 1)
191 		kcf_prov_tab_dump("kcf_prov_tab_rem_provider");
192 #endif /* DEBUG */
193 
194 	return (CRYPTO_SUCCESS);
195 }
196 
197 /*
198  * Returns the provider descriptor corresponding to the specified
199  * provider id. A REFHOLD is done on the descriptor before it is
200  * returned to the caller. It is the responsibility of the caller
201  * to do a REFRELE once it is done with the provider descriptor.
202  */
203 kcf_provider_desc_t *
204 kcf_prov_tab_lookup(crypto_provider_id_t prov_id)
205 {
206 	kcf_provider_desc_t *prov_desc;
207 
208 	mutex_enter(&prov_tab_mutex);
209 
210 	prov_desc = prov_tab[prov_id];
211 
212 	if (prov_desc == NULL) {
213 		mutex_exit(&prov_tab_mutex);
214 		return (NULL);
215 	}
216 
217 	KCF_PROV_REFHOLD(prov_desc);
218 
219 	mutex_exit(&prov_tab_mutex);
220 
221 	return (prov_desc);
222 }
223 
224 static void
225 allocate_ops_v1(crypto_ops_t *src, crypto_ops_t *dst, uint_t *mech_list_count)
226 {
227 	if (src->co_control_ops != NULL)
228 		dst->co_control_ops = kmem_alloc(sizeof (crypto_control_ops_t),
229 		    KM_SLEEP);
230 
231 	if (src->co_digest_ops != NULL)
232 		dst->co_digest_ops = kmem_alloc(sizeof (crypto_digest_ops_t),
233 		    KM_SLEEP);
234 
235 	if (src->co_cipher_ops != NULL)
236 		dst->co_cipher_ops = kmem_alloc(sizeof (crypto_cipher_ops_t),
237 		    KM_SLEEP);
238 
239 	if (src->co_mac_ops != NULL)
240 		dst->co_mac_ops = kmem_alloc(sizeof (crypto_mac_ops_t),
241 		    KM_SLEEP);
242 
243 	if (src->co_sign_ops != NULL)
244 		dst->co_sign_ops = kmem_alloc(sizeof (crypto_sign_ops_t),
245 		    KM_SLEEP);
246 
247 	if (src->co_verify_ops != NULL)
248 		dst->co_verify_ops = kmem_alloc(sizeof (crypto_verify_ops_t),
249 		    KM_SLEEP);
250 
251 	if (src->co_dual_ops != NULL)
252 		dst->co_dual_ops = kmem_alloc(sizeof (crypto_dual_ops_t),
253 		    KM_SLEEP);
254 
255 	if (src->co_dual_cipher_mac_ops != NULL)
256 		dst->co_dual_cipher_mac_ops = kmem_alloc(
257 		    sizeof (crypto_dual_cipher_mac_ops_t), KM_SLEEP);
258 
259 	if (src->co_random_ops != NULL) {
260 		dst->co_random_ops = kmem_alloc(
261 		    sizeof (crypto_random_number_ops_t), KM_SLEEP);
262 
263 		/*
264 		 * Allocate storage to store the array of supported mechanisms
265 		 * specified by provider. We allocate extra mechanism storage
266 		 * if the provider has random_ops since we keep an internal
267 		 * mechanism, SUN_RANDOM, in this case.
268 		 */
269 		(*mech_list_count)++;
270 	}
271 
272 	if (src->co_session_ops != NULL)
273 		dst->co_session_ops = kmem_alloc(sizeof (crypto_session_ops_t),
274 		    KM_SLEEP);
275 
276 	if (src->co_object_ops != NULL)
277 		dst->co_object_ops = kmem_alloc(sizeof (crypto_object_ops_t),
278 		    KM_SLEEP);
279 
280 	if (src->co_key_ops != NULL)
281 		dst->co_key_ops = kmem_alloc(sizeof (crypto_key_ops_t),
282 		    KM_SLEEP);
283 
284 	if (src->co_provider_ops != NULL)
285 		dst->co_provider_ops = kmem_alloc(
286 		    sizeof (crypto_provider_management_ops_t), KM_SLEEP);
287 
288 	if (src->co_ctx_ops != NULL)
289 		dst->co_ctx_ops = kmem_alloc(sizeof (crypto_ctx_ops_t),
290 		    KM_SLEEP);
291 }
292 
293 static void
294 allocate_ops_v2(crypto_ops_t *src, crypto_ops_t *dst)
295 {
296 	if (src->co_mech_ops != NULL)
297 		dst->co_mech_ops = kmem_alloc(sizeof (crypto_mech_ops_t),
298 		    KM_SLEEP);
299 }
300 
301 static void
302 allocate_ops_v3(crypto_ops_t *src, crypto_ops_t *dst)
303 {
304 	if (src->co_nostore_key_ops != NULL)
305 		dst->co_nostore_key_ops =
306 		    kmem_alloc(sizeof (crypto_nostore_key_ops_t), KM_SLEEP);
307 }
308 
309 /*
310  * Allocate a provider descriptor. mech_list_count specifies the
311  * number of mechanisms supported by the providers, and is used
312  * to allocate storage for the mechanism table.
313  * This function may sleep while allocating memory, which is OK
314  * since it is invoked from user context during provider registration.
315  */
316 kcf_provider_desc_t *
317 kcf_alloc_provider_desc(crypto_provider_info_t *info)
318 {
319 	int i, j;
320 	kcf_provider_desc_t *desc;
321 	uint_t mech_list_count = info->pi_mech_list_count;
322 	crypto_ops_t *src_ops = info->pi_ops_vector;
323 
324 	desc = kmem_zalloc(sizeof (kcf_provider_desc_t), KM_SLEEP);
325 
326 	/*
327 	 * pd_description serves two purposes
328 	 * - Appears as a blank padded PKCS#11 style string, that will be
329 	 *   returned to applications in CK_SLOT_INFO.slotDescription.
330 	 *   This means that we should not have a null character in the
331 	 *   first CRYPTO_PROVIDER_DESCR_MAX_LEN bytes.
332 	 * - Appears as a null-terminated string that can be used by
333 	 *   other kcf routines.
334 	 *
335 	 * So, we allocate enough room for one extra null terminator
336 	 * which keeps every one happy.
337 	 */
338 	desc->pd_description = kmem_alloc(CRYPTO_PROVIDER_DESCR_MAX_LEN + 1,
339 	    KM_SLEEP);
340 	(void) memset(desc->pd_description, ' ',
341 	    CRYPTO_PROVIDER_DESCR_MAX_LEN);
342 	desc->pd_description[CRYPTO_PROVIDER_DESCR_MAX_LEN] = '\0';
343 
344 	/*
345 	 * Since the framework does not require the ops vector specified
346 	 * by the providers during registration to be persistent,
347 	 * KCF needs to allocate storage where copies of the ops
348 	 * vectors are copied.
349 	 */
350 	desc->pd_ops_vector = kmem_zalloc(sizeof (crypto_ops_t), KM_SLEEP);
351 
352 	if (info->pi_provider_type != CRYPTO_LOGICAL_PROVIDER) {
353 		allocate_ops_v1(src_ops, desc->pd_ops_vector, &mech_list_count);
354 		if (info->pi_interface_version >= CRYPTO_SPI_VERSION_2)
355 			allocate_ops_v2(src_ops, desc->pd_ops_vector);
356 		if (info->pi_interface_version == CRYPTO_SPI_VERSION_3)
357 			allocate_ops_v3(src_ops, desc->pd_ops_vector);
358 	}
359 
360 	desc->pd_mech_list_count = mech_list_count;
361 	desc->pd_mechanisms = kmem_zalloc(sizeof (crypto_mech_info_t) *
362 	    mech_list_count, KM_SLEEP);
363 	for (i = 0; i < KCF_OPS_CLASSSIZE; i++)
364 		for (j = 0; j < KCF_MAXMECHTAB; j++)
365 			desc->pd_mech_indx[i][j] = KCF_INVALID_INDX;
366 
367 	desc->pd_prov_id = KCF_PROVID_INVALID;
368 	desc->pd_state = KCF_PROV_ALLOCATED;
369 
370 	mutex_init(&desc->pd_lock, NULL, MUTEX_DEFAULT, NULL);
371 	cv_init(&desc->pd_resume_cv, NULL, CV_DEFAULT, NULL);
372 
373 	desc->pd_nbins = max_ncpus;
374 	desc->pd_percpu_bins =
375 	    kmem_zalloc(desc->pd_nbins * sizeof (kcf_prov_cpu_t), KM_SLEEP);
376 
377 	return (desc);
378 }
379 
380 /*
381  * Free a provider descriptor. Caller must hold prov_tab_mutex.
382  *
383  * Caution: This routine drops prov_tab_mutex.
384  */
385 void
386 kcf_free_provider_desc(kcf_provider_desc_t *desc)
387 {
388 	if (desc == NULL)
389 		return;
390 
391 	ASSERT(MUTEX_HELD(&prov_tab_mutex));
392 	if (desc->pd_prov_id != KCF_PROVID_INVALID) {
393 		/* release the associated providers table entry */
394 		ASSERT(prov_tab[desc->pd_prov_id] != NULL);
395 		prov_tab[desc->pd_prov_id] = NULL;
396 		prov_tab_num--;
397 	}
398 	mutex_exit(&prov_tab_mutex);
399 
400 	/* free the kernel memory associated with the provider descriptor */
401 
402 	if (desc->pd_description != NULL)
403 		kmem_free(desc->pd_description,
404 		    CRYPTO_PROVIDER_DESCR_MAX_LEN + 1);
405 
406 	if (desc->pd_ops_vector != NULL) {
407 
408 		if (desc->pd_ops_vector->co_control_ops != NULL)
409 			kmem_free(desc->pd_ops_vector->co_control_ops,
410 			    sizeof (crypto_control_ops_t));
411 
412 		if (desc->pd_ops_vector->co_digest_ops != NULL)
413 			kmem_free(desc->pd_ops_vector->co_digest_ops,
414 			    sizeof (crypto_digest_ops_t));
415 
416 		if (desc->pd_ops_vector->co_cipher_ops != NULL)
417 			kmem_free(desc->pd_ops_vector->co_cipher_ops,
418 			    sizeof (crypto_cipher_ops_t));
419 
420 		if (desc->pd_ops_vector->co_mac_ops != NULL)
421 			kmem_free(desc->pd_ops_vector->co_mac_ops,
422 			    sizeof (crypto_mac_ops_t));
423 
424 		if (desc->pd_ops_vector->co_sign_ops != NULL)
425 			kmem_free(desc->pd_ops_vector->co_sign_ops,
426 			    sizeof (crypto_sign_ops_t));
427 
428 		if (desc->pd_ops_vector->co_verify_ops != NULL)
429 			kmem_free(desc->pd_ops_vector->co_verify_ops,
430 			    sizeof (crypto_verify_ops_t));
431 
432 		if (desc->pd_ops_vector->co_dual_ops != NULL)
433 			kmem_free(desc->pd_ops_vector->co_dual_ops,
434 			    sizeof (crypto_dual_ops_t));
435 
436 		if (desc->pd_ops_vector->co_dual_cipher_mac_ops != NULL)
437 			kmem_free(desc->pd_ops_vector->co_dual_cipher_mac_ops,
438 			    sizeof (crypto_dual_cipher_mac_ops_t));
439 
440 		if (desc->pd_ops_vector->co_random_ops != NULL)
441 			kmem_free(desc->pd_ops_vector->co_random_ops,
442 			    sizeof (crypto_random_number_ops_t));
443 
444 		if (desc->pd_ops_vector->co_session_ops != NULL)
445 			kmem_free(desc->pd_ops_vector->co_session_ops,
446 			    sizeof (crypto_session_ops_t));
447 
448 		if (desc->pd_ops_vector->co_object_ops != NULL)
449 			kmem_free(desc->pd_ops_vector->co_object_ops,
450 			    sizeof (crypto_object_ops_t));
451 
452 		if (desc->pd_ops_vector->co_key_ops != NULL)
453 			kmem_free(desc->pd_ops_vector->co_key_ops,
454 			    sizeof (crypto_key_ops_t));
455 
456 		if (desc->pd_ops_vector->co_provider_ops != NULL)
457 			kmem_free(desc->pd_ops_vector->co_provider_ops,
458 			    sizeof (crypto_provider_management_ops_t));
459 
460 		if (desc->pd_ops_vector->co_ctx_ops != NULL)
461 			kmem_free(desc->pd_ops_vector->co_ctx_ops,
462 			    sizeof (crypto_ctx_ops_t));
463 
464 		if (desc->pd_ops_vector->co_mech_ops != NULL)
465 			kmem_free(desc->pd_ops_vector->co_mech_ops,
466 			    sizeof (crypto_mech_ops_t));
467 
468 		if (desc->pd_ops_vector->co_nostore_key_ops != NULL)
469 			kmem_free(desc->pd_ops_vector->co_nostore_key_ops,
470 			    sizeof (crypto_nostore_key_ops_t));
471 
472 		kmem_free(desc->pd_ops_vector, sizeof (crypto_ops_t));
473 	}
474 
475 	if (desc->pd_mechanisms != NULL)
476 		/* free the memory associated with the mechanism info's */
477 		kmem_free(desc->pd_mechanisms, sizeof (crypto_mech_info_t) *
478 		    desc->pd_mech_list_count);
479 
480 	if (desc->pd_name != NULL) {
481 		kmem_free(desc->pd_name, strlen(desc->pd_name) + 1);
482 	}
483 
484 	if (desc->pd_taskq != NULL)
485 		taskq_destroy(desc->pd_taskq);
486 
487 	if (desc->pd_percpu_bins != NULL) {
488 		kmem_free(desc->pd_percpu_bins,
489 		    desc->pd_nbins * sizeof (kcf_prov_cpu_t));
490 	}
491 
492 	kmem_free(desc, sizeof (kcf_provider_desc_t));
493 }
494 
495 /*
496  * Returns the provider descriptor corresponding to the specified
497  * module name. A REFHOLD is done on the descriptor before it is
498  * returned to the caller. It is the responsibility of the caller
499  * to do a REFRELE once it is done with the provider descriptor.
500  * Only software providers are returned by this function.
501  */
502 kcf_provider_desc_t *
503 kcf_prov_tab_lookup_by_name(char *module_name)
504 {
505 	kcf_provider_desc_t *prov_desc;
506 	uint_t i;
507 
508 	mutex_enter(&prov_tab_mutex);
509 
510 	for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
511 		if ((prov_desc = prov_tab[i]) != NULL &&
512 		    (!KCF_IS_PROV_REMOVED(prov_desc)) &&
513 		    prov_desc->pd_prov_type == CRYPTO_SW_PROVIDER) {
514 			ASSERT(prov_desc->pd_name != NULL);
515 			if (strncmp(module_name, prov_desc->pd_name,
516 			    MAXNAMELEN) == 0) {
517 				KCF_PROV_REFHOLD(prov_desc);
518 				mutex_exit(&prov_tab_mutex);
519 				return (prov_desc);
520 			}
521 		}
522 	}
523 
524 	mutex_exit(&prov_tab_mutex);
525 	return (NULL);
526 }
527 
528 /*
529  * Returns the provider descriptor corresponding to the specified
530  * device name and instance. A REFHOLD is done on the descriptor
531  * before it is returned to the caller. It is the responsibility
532  * of the caller to do a REFRELE once it is done with the provider
533  * descriptor. Only hardware providers are returned by this function.
534  */
535 kcf_provider_desc_t *
536 kcf_prov_tab_lookup_by_dev(char *name, uint_t instance)
537 {
538 	kcf_provider_desc_t *prov_desc;
539 	uint_t i;
540 
541 	mutex_enter(&prov_tab_mutex);
542 
543 	for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
544 		if ((prov_desc = prov_tab[i]) != NULL &&
545 		    (!KCF_IS_PROV_REMOVED(prov_desc)) &&
546 		    prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER) {
547 			ASSERT(prov_desc->pd_name != NULL);
548 			if (strncmp(prov_desc->pd_name, name,
549 			    MAXNAMELEN) == 0 &&
550 			    prov_desc->pd_instance == instance) {
551 				KCF_PROV_REFHOLD(prov_desc);
552 				mutex_exit(&prov_tab_mutex);
553 				return (prov_desc);
554 			}
555 		}
556 	}
557 
558 	mutex_exit(&prov_tab_mutex);
559 	return (NULL);
560 }
561 
562 /*
563  * Returns an array of hardware and logical provider descriptors,
564  * a.k.a the PKCS#11 slot list. A REFHOLD is done on each descriptor
565  * before the array is returned. The entire table can be freed by
566  * calling kcf_free_provider_tab().
567  */
568 int
569 kcf_get_slot_list(uint_t *count, kcf_provider_desc_t ***array,
570     boolean_t unverified)
571 {
572 	kcf_provider_desc_t *prov_desc;
573 	kcf_provider_desc_t **p = NULL;
574 	char *last;
575 	uint_t cnt = 0;
576 	uint_t i, j;
577 	int rval = CRYPTO_SUCCESS;
578 	size_t n, final_size;
579 
580 	/* count the providers */
581 	mutex_enter(&prov_tab_mutex);
582 	for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
583 		if ((prov_desc = prov_tab[i]) != NULL &&
584 		    ((prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER &&
585 		    (prov_desc->pd_flags & CRYPTO_HIDE_PROVIDER) == 0) ||
586 		    prov_desc->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)) {
587 			if (KCF_IS_PROV_USABLE(prov_desc) ||
588 			    (unverified && KCF_IS_PROV_UNVERIFIED(prov_desc))) {
589 				cnt++;
590 			}
591 		}
592 	}
593 	mutex_exit(&prov_tab_mutex);
594 
595 	if (cnt == 0)
596 		goto out;
597 
598 	n = cnt * sizeof (kcf_provider_desc_t *);
599 again:
600 	p = kmem_zalloc(n, KM_SLEEP);
601 
602 	/* pointer to last entry in the array */
603 	last = (char *)&p[cnt-1];
604 
605 	mutex_enter(&prov_tab_mutex);
606 	/* fill the slot list */
607 	for (i = 0, j = 0; i < KCF_MAX_PROVIDERS; i++) {
608 		if ((prov_desc = prov_tab[i]) != NULL &&
609 		    ((prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER &&
610 		    (prov_desc->pd_flags & CRYPTO_HIDE_PROVIDER) == 0) ||
611 		    prov_desc->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)) {
612 			if (KCF_IS_PROV_USABLE(prov_desc) ||
613 			    (unverified && KCF_IS_PROV_UNVERIFIED(prov_desc))) {
614 				if ((char *)&p[j] > last) {
615 					mutex_exit(&prov_tab_mutex);
616 					kcf_free_provider_tab(cnt, p);
617 					n = n << 1;
618 					cnt = cnt << 1;
619 					goto again;
620 				}
621 				p[j++] = prov_desc;
622 				KCF_PROV_REFHOLD(prov_desc);
623 			}
624 		}
625 	}
626 	mutex_exit(&prov_tab_mutex);
627 
628 	final_size = j * sizeof (kcf_provider_desc_t *);
629 	cnt = j;
630 	ASSERT(final_size <= n);
631 
632 	/* check if buffer we allocated is too large */
633 	if (final_size < n) {
634 		char *final_buffer = NULL;
635 
636 		if (final_size > 0) {
637 			final_buffer = kmem_alloc(final_size, KM_SLEEP);
638 			bcopy(p, final_buffer, final_size);
639 		}
640 		kmem_free(p, n);
641 		p = (kcf_provider_desc_t **)final_buffer;
642 	}
643 out:
644 	*count = cnt;
645 	*array = p;
646 	return (rval);
647 }
648 
649 /*
650  * Returns an array of hardware provider descriptors. This routine
651  * used by cryptoadm(1M). A REFHOLD is done on each descriptor before
652  * the array is returned. The entire table can be freed by calling
653  * kcf_free_provider_tab().
654  *
655  * A NULL name argument puts all hardware providers in the array.
656  * A non-NULL name argument puts only those providers in the array
657  * which match the name and instance arguments.
658  */
659 int
660 kcf_get_hw_prov_tab(uint_t *count, kcf_provider_desc_t ***array,  int kmflag,
661     char *name, uint_t instance, boolean_t unverified)
662 {
663 	kcf_provider_desc_t *prov_desc;
664 	kcf_provider_desc_t **p = NULL;
665 	char *last;
666 	uint_t cnt = 0;
667 	uint_t i, j;
668 	int rval = CRYPTO_SUCCESS;
669 	size_t n, final_size;
670 
671 	/* count the providers */
672 	mutex_enter(&prov_tab_mutex);
673 	for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
674 		if ((prov_desc = prov_tab[i]) != NULL &&
675 		    prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER) {
676 			if (KCF_IS_PROV_USABLE(prov_desc) ||
677 			    (unverified && KCF_IS_PROV_UNVERIFIED(prov_desc))) {
678 				if (name == NULL ||
679 				    (strncmp(prov_desc->pd_name, name,
680 				    MAXNAMELEN) == 0 &&
681 				    prov_desc->pd_instance == instance)) {
682 					cnt++;
683 				}
684 			}
685 		}
686 	}
687 	mutex_exit(&prov_tab_mutex);
688 
689 	if (cnt == 0)
690 		goto out;
691 
692 	n = cnt * sizeof (kcf_provider_desc_t *);
693 again:
694 	p = kmem_zalloc(n, kmflag);
695 	if (p == NULL) {
696 		rval = CRYPTO_HOST_MEMORY;
697 		goto out;
698 	}
699 	/* pointer to last entry in the array */
700 	last = (char *)&p[cnt-1];
701 
702 	mutex_enter(&prov_tab_mutex);
703 	for (i = 0, j = 0; i < KCF_MAX_PROVIDERS; i++) {
704 		if ((prov_desc = prov_tab[i]) != NULL &&
705 		    prov_desc->pd_prov_type == CRYPTO_HW_PROVIDER) {
706 			if (KCF_IS_PROV_USABLE(prov_desc) ||
707 			    (unverified && KCF_IS_PROV_UNVERIFIED(prov_desc))) {
708 				if (name == NULL ||
709 				    (strncmp(prov_desc->pd_name, name,
710 				    MAXNAMELEN) == 0 &&
711 				    prov_desc->pd_instance == instance)) {
712 					if ((char *)&p[j] > last) {
713 						mutex_exit(&prov_tab_mutex);
714 						kcf_free_provider_tab(cnt, p);
715 						n = n << 1;
716 						cnt = cnt << 1;
717 						goto again;
718 					}
719 					p[j++] = prov_desc;
720 					KCF_PROV_REFHOLD(prov_desc);
721 				}
722 			}
723 		}
724 	}
725 	mutex_exit(&prov_tab_mutex);
726 
727 	final_size = j * sizeof (kcf_provider_desc_t *);
728 	ASSERT(final_size <= n);
729 
730 	/* check if buffer we allocated is too large */
731 	if (final_size < n) {
732 		char *final_buffer = NULL;
733 
734 		if (final_size > 0) {
735 			final_buffer = kmem_alloc(final_size, kmflag);
736 			if (final_buffer == NULL) {
737 				kcf_free_provider_tab(cnt, p);
738 				cnt = 0;
739 				p = NULL;
740 				rval = CRYPTO_HOST_MEMORY;
741 				goto out;
742 			}
743 			bcopy(p, final_buffer, final_size);
744 		}
745 		kmem_free(p, n);
746 		p = (kcf_provider_desc_t **)final_buffer;
747 	}
748 	cnt = j;
749 out:
750 	*count = cnt;
751 	*array = p;
752 	return (rval);
753 }
754 
755 /*
756  * Free an array of hardware provider descriptors.  A REFRELE
757  * is done on each descriptor before the table is freed.
758  */
759 void
760 kcf_free_provider_tab(uint_t count, kcf_provider_desc_t **array)
761 {
762 	kcf_provider_desc_t *prov_desc;
763 	int i;
764 
765 	for (i = 0; i < count; i++) {
766 		if ((prov_desc = array[i]) != NULL) {
767 			KCF_PROV_REFRELE(prov_desc);
768 		}
769 	}
770 	kmem_free(array, count * sizeof (kcf_provider_desc_t *));
771 }
772 
773 /*
774  * Returns in the location pointed to by pd a pointer to the descriptor
775  * for the software provider for the specified mechanism.
776  * The provider descriptor is returned held and it is the caller's
777  * responsibility to release it when done. The mechanism entry
778  * is returned if the optional argument mep is non NULL.
779  *
780  * Returns one of the CRYPTO_ * error codes on failure, and
781  * CRYPTO_SUCCESS on success.
782  */
783 int
784 kcf_get_sw_prov(crypto_mech_type_t mech_type, kcf_provider_desc_t **pd,
785     kcf_mech_entry_t **mep, boolean_t log_warn)
786 {
787 	kcf_mech_entry_t *me;
788 	kcf_lock_withpad_t *mp;
789 
790 	/* get the mechanism entry for this mechanism */
791 	if (kcf_get_mech_entry(mech_type, &me) != KCF_SUCCESS)
792 		return (CRYPTO_MECHANISM_INVALID);
793 
794 	/*
795 	 * Get the software provider for this mechanism.
796 	 * Lock the mech_entry until we grab the 'pd'.
797 	 */
798 	mp = &me_mutexes[CPU_SEQID];
799 	mutex_enter(&mp->kl_lock);
800 
801 	if (me->me_sw_prov == NULL ||
802 	    (*pd = me->me_sw_prov->pm_prov_desc) == NULL) {
803 		/* no SW provider for this mechanism */
804 		if (log_warn)
805 			cmn_err(CE_WARN, "no SW provider for \"%s\"\n",
806 			    me->me_name);
807 		mutex_exit(&mp->kl_lock);
808 		return (CRYPTO_MECH_NOT_SUPPORTED);
809 	}
810 
811 	KCF_PROV_REFHOLD(*pd);
812 	mutex_exit(&mp->kl_lock);
813 
814 	if (mep != NULL)
815 		*mep = me;
816 
817 	return (CRYPTO_SUCCESS);
818 }
819 
820 #if DEBUG
821 /*
822  * Dump the Kernel crypto providers table, prov_tab.
823  * If kcf_frmwrk_debug is >=2, also dump the mechanism lists.
824  */
825 static void
826 kcf_prov_tab_dump(char *message)
827 {
828 	uint_t i, j;
829 
830 	mutex_enter(&prov_tab_mutex);
831 	printf("Providers table prov_tab at %s:\n",
832 	    message != NULL ? message : "");
833 
834 	for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
835 		kcf_provider_desc_t *p = prov_tab[i];
836 		if (p != NULL) {
837 			printf("[%d]: (%s) %d mechanisms, %s\n", i,
838 			    (p->pd_prov_type == CRYPTO_HW_PROVIDER) ?
839 			    "HW" : "SW",
840 			    p->pd_mech_list_count, p->pd_description);
841 			if (kcf_frmwrk_debug >= 2) {
842 				printf("\tpd_mechanisms: ");
843 				for (j = 0; j < p->pd_mech_list_count; ++j) {
844 					printf("%s \n",
845 					    p->pd_mechanisms[j].cm_mech_name);
846 				}
847 				printf("\n");
848 			}
849 		}
850 	}
851 	printf("(end of providers table)\n");
852 
853 	mutex_exit(&prov_tab_mutex);
854 }
855 
856 #endif /* DEBUG */
857 
858 /*
859  * This function goes through the provider table and verifies
860  * any unverified providers.
861  *
862  * This is called when kcfd is up and the door handle is ready.
863  */
864 void
865 verify_unverified_providers()
866 {
867 	int i;
868 	kcf_provider_desc_t *pd;
869 	boolean_t need_verify;
870 
871 	ASSERT(kcf_dh != NULL);
872 	mutex_enter(&prov_tab_mutex);
873 
874 	for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
875 		if ((pd = prov_tab[i]) == NULL)
876 			continue;
877 
878 		if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER)
879 			continue;
880 
881 		mutex_enter(&pd->pd_lock);
882 		need_verify = pd->pd_state == KCF_PROV_UNVERIFIED;
883 		mutex_exit(&pd->pd_lock);
884 
885 		if (!need_verify)
886 			continue;
887 
888 		KCF_PROV_REFHOLD(pd);
889 
890 		/*
891 		 * We need to drop this lock, since it could be
892 		 * acquired by kcf_verify_signature().
893 		 * This is safe, as any providers that are
894 		 * added to the table after we dropped the
895 		 * lock *will see* a non NULL kcf_dh and hence
896 		 * would have been verified by other means.
897 		 */
898 		mutex_exit(&prov_tab_mutex);
899 		/* This routine will release the above holds */
900 		kcf_verify_signature(pd);
901 		mutex_enter(&prov_tab_mutex);
902 	}
903 
904 	mutex_exit(&prov_tab_mutex);
905 }
906 
907 /* protected by prov_tab_mutex */
908 boolean_t kcf_need_provtab_walk = B_FALSE;
909 
910 /* Caller must hold prov_tab_mutex */
911 static void
912 kcf_free_unregistered_provs()
913 {
914 	int i;
915 	kcf_provider_desc_t *pd;
916 	boolean_t walk_again = B_FALSE;
917 
918 	ASSERT(MUTEX_HELD(&prov_tab_mutex));
919 	for (i = 0; i < KCF_MAX_PROVIDERS; i++) {
920 		if ((pd = prov_tab[i]) == NULL ||
921 		    pd->pd_prov_type == CRYPTO_SW_PROVIDER ||
922 		    pd->pd_state != KCF_PROV_UNREGISTERED)
923 			continue;
924 
925 		if (kcf_get_refcnt(pd, B_TRUE) == 0) {
926 			/* kcf_free_provider_desc drops prov_tab_mutex */
927 			kcf_free_provider_desc(pd);
928 			mutex_enter(&prov_tab_mutex);
929 		} else
930 			walk_again = B_TRUE;
931 	}
932 
933 	kcf_need_provtab_walk = walk_again;
934 }
935