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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * This file is part of the core Kernel Cryptographic Framework. 30 * It implements the management of tables of Providers. Entries to 31 * added and removed when cryptographic providers register with 32 * and unregister from the framework, respectively. The KCF scheduler 33 * and ioctl pseudo driver call this function to obtain the list 34 * of available providers. 35 * 36 * The provider table is indexed by crypto_provider_id_t. Each 37 * element of the table contains a pointer to a provider descriptor, 38 * or NULL if the entry is free. 39 * 40 * This file also implements helper functions to allocate and free 41 * provider descriptors. 42 */ 43 44 #include <sys/types.h> 45 #include <sys/kmem.h> 46 #include <sys/cmn_err.h> 47 #include <sys/ddi.h> 48 #include <sys/sunddi.h> 49 #include <sys/ksynch.h> 50 #include <sys/crypto/common.h> 51 #include <sys/crypto/impl.h> 52 #include <sys/crypto/sched_impl.h> 53 #include <sys/crypto/spi.h> 54 55 #define KCF_MAX_PROVIDERS 512 /* max number of providers */ 56 57 static kmutex_t prov_tab_mutex; /* ensure exclusive access to the table */ 58 static kcf_provider_desc_t **prov_tab = NULL; 59 static uint_t prov_tab_num = 0; /* number of providers in table */ 60 static uint_t prov_tab_max = KCF_MAX_PROVIDERS; 61 62 #if DEBUG 63 extern int kcf_frmwrk_debug; 64 static void kcf_prov_tab_dump(void); 65 #endif /* DEBUG */ 66 67 /* 68 * Initialize the providers table. The providers table is dynamically 69 * allocated with prov_tab_max entries. 70 */ 71 void 72 kcf_prov_tab_init(void) 73 { 74 mutex_init(&prov_tab_mutex, NULL, MUTEX_DRIVER, NULL); 75 76 prov_tab = kmem_zalloc(prov_tab_max * sizeof (kcf_provider_desc_t *), 77 KM_SLEEP); 78 } 79 80 /* 81 * Add a provider to the provider table. If no free entry can be found 82 * for the new provider, returns CRYPTO_HOST_MEMORY. Otherwise, add 83 * the provider to the table, initialize the pd_prov_id field 84 * of the specified provider descriptor to the index in that table, 85 * and return CRYPTO_SUCCESS. Note that a REFHOLD is done on the 86 * provider when pointed to by a table entry. 87 */ 88 int 89 kcf_prov_tab_add_provider(kcf_provider_desc_t *prov_desc) 90 { 91 uint_t i; 92 93 ASSERT(prov_tab != NULL); 94 95 mutex_enter(&prov_tab_mutex); 96 97 /* find free slot in providers table */ 98 for (i = 0; i < KCF_MAX_PROVIDERS && prov_tab[i] != NULL; i++); 99 if (i == KCF_MAX_PROVIDERS) { 100 /* ran out of providers entries */ 101 mutex_exit(&prov_tab_mutex); 102 cmn_err(CE_WARN, "out of providers entries"); 103 return (CRYPTO_HOST_MEMORY); 104 } 105 106 /* initialize entry */ 107 prov_tab[i] = prov_desc; 108 KCF_PROV_REFHOLD(prov_desc); 109 KCF_PROV_IREFHOLD(prov_desc); 110 prov_tab_num++; 111 112 mutex_exit(&prov_tab_mutex); 113 114 /* update provider descriptor */ 115 prov_desc->pd_prov_id = i; 116 117 /* 118 * The KCF-private provider handle is defined as the internal 119 * provider id. 120 */ 121 prov_desc->pd_kcf_prov_handle = 122 (crypto_kcf_provider_handle_t)prov_desc->pd_prov_id; 123 124 #if DEBUG 125 if (kcf_frmwrk_debug >= 1) 126 kcf_prov_tab_dump(); 127 #endif /* DEBUG */ 128 129 return (CRYPTO_SUCCESS); 130 } 131 132 /* 133 * Remove the provider specified by its id. A REFRELE is done on the 134 * corresponding provider descriptor before this function returns. 135 * Returns CRYPTO_UNKNOWN_PROVIDER if the provider id is not valid. 136 */ 137 int 138 kcf_prov_tab_rem_provider(crypto_provider_id_t prov_id) 139 { 140 kcf_provider_desc_t *prov_desc; 141 142 ASSERT(prov_tab != NULL); 143 ASSERT(prov_tab_num >= 0); 144 145 /* 146 * Validate provider id, since it can be specified by a 3rd-party 147 * provider. 148 */ 149 150 mutex_enter(&prov_tab_mutex); 151 if (prov_id >= KCF_MAX_PROVIDERS || 152 ((prov_desc = prov_tab[prov_id]) == NULL)) { 153 mutex_exit(&prov_tab_mutex); 154 return (CRYPTO_INVALID_PROVIDER_ID); 155 } 156 mutex_exit(&prov_tab_mutex); 157 158 /* 159 * The provider id must remain valid until the associated provider 160 * descriptor is freed. For this reason, we simply release our 161 * reference to the descriptor here. When the reference count 162 * reaches zero, kcf_free_provider_desc() will be invoked and 163 * the associated entry in the providers table will be released 164 * at that time. 165 */ 166 167 KCF_PROV_REFRELE(prov_desc); 168 KCF_PROV_IREFRELE(prov_desc); 169 170 #if DEBUG 171 if (kcf_frmwrk_debug >= 1) 172 kcf_prov_tab_dump(); 173 #endif /* DEBUG */ 174 175 return (CRYPTO_SUCCESS); 176 } 177 178 /* 179 * Returns the provider descriptor corresponding to the specified 180 * provider id. A REFHOLD is done on the descriptor before it is 181 * returned to the caller. It is the responsibility of the caller 182 * to do a REFRELE once it is done with the provider descriptor. 183 */ 184 kcf_provider_desc_t * 185 kcf_prov_tab_lookup(crypto_provider_id_t prov_id) 186 { 187 kcf_provider_desc_t *prov_desc; 188 189 mutex_enter(&prov_tab_mutex); 190 191 prov_desc = prov_tab[prov_id]; 192 193 if (prov_desc == NULL) { 194 mutex_exit(&prov_tab_mutex); 195 return (NULL); 196 } 197 198 KCF_PROV_REFHOLD(prov_desc); 199 200 mutex_exit(&prov_tab_mutex); 201 202 return (prov_desc); 203 } 204 205 static void 206 allocate_ops_v1(crypto_ops_t *src, crypto_ops_t *dst, uint_t *mech_list_count) 207 { 208 if (src->co_control_ops != NULL) 209 dst->co_control_ops = kmem_alloc(sizeof (crypto_control_ops_t), 210 KM_SLEEP); 211 212 if (src->co_digest_ops != NULL) 213 dst->co_digest_ops = kmem_alloc(sizeof (crypto_digest_ops_t), 214 KM_SLEEP); 215 216 if (src->co_cipher_ops != NULL) 217 dst->co_cipher_ops = kmem_alloc(sizeof (crypto_cipher_ops_t), 218 KM_SLEEP); 219 220 if (src->co_mac_ops != NULL) 221 dst->co_mac_ops = kmem_alloc(sizeof (crypto_mac_ops_t), 222 KM_SLEEP); 223 224 if (src->co_sign_ops != NULL) 225 dst->co_sign_ops = kmem_alloc(sizeof (crypto_sign_ops_t), 226 KM_SLEEP); 227 228 if (src->co_verify_ops != NULL) 229 dst->co_verify_ops = kmem_alloc(sizeof (crypto_verify_ops_t), 230 KM_SLEEP); 231 232 if (src->co_dual_ops != NULL) 233 dst->co_dual_ops = kmem_alloc(sizeof (crypto_dual_ops_t), 234 KM_SLEEP); 235 236 if (src->co_dual_cipher_mac_ops != NULL) 237 dst->co_dual_cipher_mac_ops = kmem_alloc( 238 sizeof (crypto_dual_cipher_mac_ops_t), KM_SLEEP); 239 240 if (src->co_random_ops != NULL) { 241 dst->co_random_ops = kmem_alloc( 242 sizeof (crypto_random_number_ops_t), KM_SLEEP); 243 244 /* 245 * Allocate storage to store the array of supported mechanisms 246 * specified by provider. We allocate extra mechanism storage 247 * if the provider has random_ops since we keep an internal 248 * mechanism, SUN_RANDOM, in this case. 249 */ 250 (*mech_list_count)++; 251 } 252 253 if (src->co_session_ops != NULL) 254 dst->co_session_ops = kmem_alloc(sizeof (crypto_session_ops_t), 255 KM_SLEEP); 256 257 if (src->co_object_ops != NULL) 258 dst->co_object_ops = kmem_alloc(sizeof (crypto_object_ops_t), 259 KM_SLEEP); 260 261 if (src->co_key_ops != NULL) 262 dst->co_key_ops = kmem_alloc(sizeof (crypto_key_ops_t), 263 KM_SLEEP); 264 265 if (src->co_provider_ops != NULL) 266 dst->co_provider_ops = kmem_alloc( 267 sizeof (crypto_provider_management_ops_t), KM_SLEEP); 268 269 if (src->co_ctx_ops != NULL) 270 dst->co_ctx_ops = kmem_alloc(sizeof (crypto_ctx_ops_t), 271 KM_SLEEP); 272 } 273 274 static void 275 allocate_ops_v2(crypto_ops_t *src, crypto_ops_t *dst) 276 { 277 if (src->co_mech_ops != NULL) 278 dst->co_mech_ops = kmem_alloc(sizeof (crypto_mech_ops_t), 279 KM_SLEEP); 280 } 281 282 static void 283 allocate_ops_v3(crypto_ops_t *src, crypto_ops_t *dst) 284 { 285 if (src->co_nostore_key_ops != NULL) 286 dst->co_nostore_key_ops = 287 kmem_alloc(sizeof (crypto_nostore_key_ops_t), KM_SLEEP); 288 } 289 290 /* 291 * Allocate a provider descriptor. mech_list_count specifies the 292 * number of mechanisms supported by the providers, and is used 293 * to allocate storage for the mechanism table. 294 * This function may sleep while allocating memory, which is OK 295 * since it is invoked from user context during provider registration. 296 */ 297 kcf_provider_desc_t * 298 kcf_alloc_provider_desc(crypto_provider_info_t *info) 299 { 300 int i, j; 301 kcf_provider_desc_t *desc; 302 uint_t mech_list_count = info->pi_mech_list_count; 303 crypto_ops_t *src_ops = info->pi_ops_vector; 304 305 desc = kmem_zalloc(sizeof (kcf_provider_desc_t), KM_SLEEP); 306 307 /* 308 * pd_description serves two purposes 309 * - Appears as a blank padded PKCS#11 style string, that will be 310 * returned to applications in CK_SLOT_INFO.slotDescription. 311 * This means that we should not have a null character in the 312 * first CRYPTO_PROVIDER_DESCR_MAX_LEN bytes. 313 * - Appears as a null-terminated string that can be used by 314 * other kcf routines. 315 * 316 * So, we allocate enough room for one extra null terminator 317 * which keeps every one happy. 318 */ 319 desc->pd_description = kmem_alloc(CRYPTO_PROVIDER_DESCR_MAX_LEN + 1, 320 KM_SLEEP); 321 (void) memset(desc->pd_description, ' ', 322 CRYPTO_PROVIDER_DESCR_MAX_LEN); 323 desc->pd_description[CRYPTO_PROVIDER_DESCR_MAX_LEN] = '\0'; 324 325 /* 326 * Since the framework does not require the ops vector specified 327 * by the providers during registration to be persistent, 328 * KCF needs to allocate storage where copies of the ops 329 * vectors are copied. 330 */ 331 desc->pd_ops_vector = kmem_zalloc(sizeof (crypto_ops_t), KM_SLEEP); 332 333 if (info->pi_provider_type != CRYPTO_LOGICAL_PROVIDER) { 334 allocate_ops_v1(src_ops, desc->pd_ops_vector, &mech_list_count); 335 if (info->pi_interface_version >= CRYPTO_SPI_VERSION_2) 336 allocate_ops_v2(src_ops, desc->pd_ops_vector); 337 if (info->pi_interface_version == CRYPTO_SPI_VERSION_3) 338 allocate_ops_v3(src_ops, desc->pd_ops_vector); 339 } 340 341 desc->pd_mech_list_count = mech_list_count; 342 desc->pd_mechanisms = kmem_zalloc(sizeof (crypto_mech_info_t) * 343 mech_list_count, KM_SLEEP); 344 for (i = 0; i < KCF_OPS_CLASSSIZE; i++) 345 for (j = 0; j < KCF_MAXMECHTAB; j++) 346 desc->pd_mech_indx[i][j] = KCF_INVALID_INDX; 347 348 desc->pd_prov_id = KCF_PROVID_INVALID; 349 desc->pd_state = KCF_PROV_ALLOCATED; 350 351 mutex_init(&desc->pd_lock, NULL, MUTEX_DEFAULT, NULL); 352 cv_init(&desc->pd_resume_cv, NULL, CV_DEFAULT, NULL); 353 cv_init(&desc->pd_remove_cv, NULL, CV_DEFAULT, NULL); 354 355 return (desc); 356 } 357 358 /* 359 * Called by KCF_PROV_REFRELE when a provider's reference count drops 360 * to zero. We free the descriptor when the last reference is released. 361 * However, for software providers, we do not free it when there is an 362 * unregister thread waiting. We signal that thread in this case and 363 * that thread is responsible for freeing the descriptor. 364 */ 365 void 366 kcf_provider_zero_refcnt(kcf_provider_desc_t *desc) 367 { 368 mutex_enter(&desc->pd_lock); 369 switch (desc->pd_prov_type) { 370 case CRYPTO_SW_PROVIDER: 371 if (desc->pd_state == KCF_PROV_REMOVED || 372 desc->pd_state == KCF_PROV_DISABLED) { 373 desc->pd_state = KCF_PROV_FREED; 374 cv_broadcast(&desc->pd_remove_cv); 375 mutex_exit(&desc->pd_lock); 376 break; 377 } 378 /* FALLTHRU */ 379 380 case CRYPTO_HW_PROVIDER: 381 case CRYPTO_LOGICAL_PROVIDER: 382 mutex_exit(&desc->pd_lock); 383 kcf_free_provider_desc(desc); 384 } 385 } 386 387 /* 388 * Free a provider descriptor. 389 */ 390 void 391 kcf_free_provider_desc(kcf_provider_desc_t *desc) 392 { 393 if (desc == NULL) 394 return; 395 396 mutex_enter(&prov_tab_mutex); 397 if (desc->pd_prov_id != KCF_PROVID_INVALID) { 398 /* release the associated providers table entry */ 399 ASSERT(prov_tab[desc->pd_prov_id] != NULL); 400 prov_tab[desc->pd_prov_id] = NULL; 401 prov_tab_num--; 402 } 403 mutex_exit(&prov_tab_mutex); 404 405 /* free the kernel memory associated with the provider descriptor */ 406 407 if (desc->pd_description != NULL) 408 kmem_free(desc->pd_description, 409 CRYPTO_PROVIDER_DESCR_MAX_LEN + 1); 410 411 if (desc->pd_ops_vector != NULL) { 412 413 if (desc->pd_ops_vector->co_control_ops != NULL) 414 kmem_free(desc->pd_ops_vector->co_control_ops, 415 sizeof (crypto_control_ops_t)); 416 417 if (desc->pd_ops_vector->co_digest_ops != NULL) 418 kmem_free(desc->pd_ops_vector->co_digest_ops, 419 sizeof (crypto_digest_ops_t)); 420 421 if (desc->pd_ops_vector->co_cipher_ops != NULL) 422 kmem_free(desc->pd_ops_vector->co_cipher_ops, 423 sizeof (crypto_cipher_ops_t)); 424 425 if (desc->pd_ops_vector->co_mac_ops != NULL) 426 kmem_free(desc->pd_ops_vector->co_mac_ops, 427 sizeof (crypto_mac_ops_t)); 428 429 if (desc->pd_ops_vector->co_sign_ops != NULL) 430 kmem_free(desc->pd_ops_vector->co_sign_ops, 431 sizeof (crypto_sign_ops_t)); 432 433 if (desc->pd_ops_vector->co_verify_ops != NULL) 434 kmem_free(desc->pd_ops_vector->co_verify_ops, 435 sizeof (crypto_verify_ops_t)); 436 437 if (desc->pd_ops_vector->co_dual_ops != NULL) 438 kmem_free(desc->pd_ops_vector->co_dual_ops, 439 sizeof (crypto_dual_ops_t)); 440 441 if (desc->pd_ops_vector->co_dual_cipher_mac_ops != NULL) 442 kmem_free(desc->pd_ops_vector->co_dual_cipher_mac_ops, 443 sizeof (crypto_dual_cipher_mac_ops_t)); 444 445 if (desc->pd_ops_vector->co_random_ops != NULL) 446 kmem_free(desc->pd_ops_vector->co_random_ops, 447 sizeof (crypto_random_number_ops_t)); 448 449 if (desc->pd_ops_vector->co_session_ops != NULL) 450 kmem_free(desc->pd_ops_vector->co_session_ops, 451 sizeof (crypto_session_ops_t)); 452 453 if (desc->pd_ops_vector->co_object_ops != NULL) 454 kmem_free(desc->pd_ops_vector->co_object_ops, 455 sizeof (crypto_object_ops_t)); 456 457 if (desc->pd_ops_vector->co_key_ops != NULL) 458 kmem_free(desc->pd_ops_vector->co_key_ops, 459 sizeof (crypto_key_ops_t)); 460 461 if (desc->pd_ops_vector->co_provider_ops != NULL) 462 kmem_free(desc->pd_ops_vector->co_provider_ops, 463 sizeof (crypto_provider_management_ops_t)); 464 465 if (desc->pd_ops_vector->co_ctx_ops != NULL) 466 kmem_free(desc->pd_ops_vector->co_ctx_ops, 467 sizeof (crypto_ctx_ops_t)); 468 469 if (desc->pd_ops_vector->co_mech_ops != NULL) 470 kmem_free(desc->pd_ops_vector->co_mech_ops, 471 sizeof (crypto_mech_ops_t)); 472 473 if (desc->pd_ops_vector->co_nostore_key_ops != NULL) 474 kmem_free(desc->pd_ops_vector->co_nostore_key_ops, 475 sizeof (crypto_nostore_key_ops_t)); 476 477 kmem_free(desc->pd_ops_vector, sizeof (crypto_ops_t)); 478 } 479 480 if (desc->pd_mechanisms != NULL) 481 /* free the memory associated with the mechanism info's */ 482 kmem_free(desc->pd_mechanisms, sizeof (crypto_mech_info_t) * 483 desc->pd_mech_list_count); 484 485 if (desc->pd_name != NULL) { 486 kmem_free(desc->pd_name, strlen(desc->pd_name) + 1); 487 } 488 489 if (desc->pd_sched_info.ks_taskq != NULL) 490 taskq_destroy(desc->pd_sched_info.ks_taskq); 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 789 /* get the mechanism entry for this mechanism */ 790 if (kcf_get_mech_entry(mech_type, &me) != KCF_SUCCESS) 791 return (CRYPTO_MECHANISM_INVALID); 792 793 /* 794 * Get the software provider for this mechanism. 795 * Lock the mech_entry until we grab the 'pd'. 796 */ 797 mutex_enter(&me->me_mutex); 798 799 if (me->me_sw_prov == NULL || 800 (*pd = me->me_sw_prov->pm_prov_desc) == NULL) { 801 /* no SW provider for this mechanism */ 802 if (log_warn) 803 cmn_err(CE_WARN, "no SW provider for \"%s\"\n", 804 me->me_name); 805 mutex_exit(&me->me_mutex); 806 return (CRYPTO_MECH_NOT_SUPPORTED); 807 } 808 809 KCF_PROV_REFHOLD(*pd); 810 mutex_exit(&me->me_mutex); 811 812 if (mep != NULL) 813 *mep = me; 814 815 return (CRYPTO_SUCCESS); 816 } 817 818 #if DEBUG 819 820 static void 821 kcf_prov_tab_dump(void) 822 { 823 uint_t i; 824 825 mutex_enter(&prov_tab_mutex); 826 827 printf("Providers table:\n"); 828 for (i = 0; i < KCF_MAX_PROVIDERS; i++) { 829 if (prov_tab[i] != NULL) { 830 printf("[%d]: (%s) %s\n", 831 i, (prov_tab[i]->pd_prov_type == 832 CRYPTO_HW_PROVIDER) ? "HW" : "SW", 833 prov_tab[i]->pd_description); 834 } 835 } 836 printf("(end of providers table)\n"); 837 838 mutex_exit(&prov_tab_mutex); 839 } 840 841 #endif /* DEBUG */ 842 843 /* 844 * This function goes through the provider table and verifies 845 * any unverified providers. 846 * 847 * This is called when kcfd is up and the door handle is ready. 848 */ 849 void 850 verify_unverified_providers() 851 { 852 int i, rv; 853 kcf_provider_desc_t *pd; 854 boolean_t need_verify; 855 856 ASSERT(kcf_dh != NULL); 857 mutex_enter(&prov_tab_mutex); 858 859 for (i = 0; i < KCF_MAX_PROVIDERS; i++) { 860 if ((pd = prov_tab[i]) == NULL) 861 continue; 862 863 if (pd->pd_prov_type == CRYPTO_LOGICAL_PROVIDER) 864 continue; 865 866 mutex_enter(&pd->pd_lock); 867 need_verify = pd->pd_state == KCF_PROV_UNVERIFIED; 868 mutex_exit(&pd->pd_lock); 869 870 if (!need_verify) 871 continue; 872 873 if ((rv = kcf_verify_signature(pd)) == 874 CRYPTO_MODVERIFICATION_FAILED) { 875 /* 876 * We need to drop this lock, since it is 877 * acquired by crypto_unregister_provider(). 878 * This is safe, as any providers that are 879 * added to the table after we dropped the 880 * lock *will see* a non NULL 881 * kcf_dh and hence would have been 882 * verified already. 883 */ 884 mutex_exit(&prov_tab_mutex); 885 (void) crypto_unregister_provider(pd->pd_prov_id); 886 mutex_enter(&prov_tab_mutex); 887 } else { 888 /* 889 * We are in the context of the kcfd thread doing 890 * the CRYPTO_LOAD_DOOR ioctl. So, we have a valid 891 * door handle and should not get -1 (unverified). 892 */ 893 ASSERT(rv == 0); 894 mutex_enter(&pd->pd_lock); 895 pd->pd_state = KCF_PROV_READY; 896 mutex_exit(&pd->pd_lock); 897 } 898 } 899 900 mutex_exit(&prov_tab_mutex); 901 } 902