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