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