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