Print this page
5031131 perf: pkcs11_kernel can benefit from a more efficient pkcs11_mech2str()

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/cmd-crypto/cryptoadm/adm_uef.c
          +++ new/usr/src/cmd/cmd-crypto/cryptoadm/adm_uef.c
↓ open down ↓ 11 lines elided ↑ open up ↑
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22      - * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
       22 + * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26      -#pragma ident   "@(#)adm_uef.c  1.12    06/11/02 SMI"
       26 +#pragma ident   "@(#)adm_uef.c  1.13    08/06/27 SMI"
  27   27  
  28   28  #include <cryptoutil.h>
  29   29  #include <fcntl.h>
  30   30  #include <libintl.h>
  31   31  #include <stdio.h>
  32   32  #include <stdlib.h>
  33   33  #include <strings.h>
  34   34  #include <unistd.h>
  35   35  #include <errno.h>
  36   36  #include <dlfcn.h>
↓ open down ↓ 71 lines elided ↑ open up ↑
 108  108                  (void) printf("CKF_SO_PIN_TO_BE_CHANGED ");
 109  109          if (flags & CKF_SO_PIN_TO_BE_CHANGED)
 110  110                  (void) printf("CKF_SO_PIN_TO_BE_CHANGED ");
 111  111          (void) printf("\n");
 112  112  }
 113  113  
 114  114  void
 115  115  display_mech_info(CK_MECHANISM_INFO *mechInfo)
 116  116  {
 117  117          CK_FLAGS ec_flags = CKF_EC_F_P | CKF_EC_F_2M | CKF_EC_ECPARAMETERS |
 118      -                CKF_EC_NAMEDCURVE | CKF_EC_UNCOMPRESS | CKF_EC_COMPRESS;
      118 +            CKF_EC_NAMEDCURVE | CKF_EC_UNCOMPRESS | CKF_EC_COMPRESS;
 119  119  
 120  120          (void) printf("%-4ld %-4ld ", mechInfo->ulMinKeySize,
 121      -                mechInfo->ulMaxKeySize);
      121 +            mechInfo->ulMaxKeySize);
 122  122          (void) printf("%s  %s  %s  %s  %s  %s  %s  %s  %s  %s  %s  %s  "
 123      -                "%s  %s",
 124      -                (mechInfo->flags & CKF_HW) ? "X" : ".",
 125      -                (mechInfo->flags & CKF_ENCRYPT) ? "X" : ".",
 126      -                (mechInfo->flags & CKF_DECRYPT) ? "X" : ".",
 127      -                (mechInfo->flags & CKF_DIGEST) ? "X" : ".",
 128      -                (mechInfo->flags & CKF_SIGN) ? "X" : ".",
 129      -                (mechInfo->flags & CKF_SIGN_RECOVER) ? "X" : ".",
 130      -                (mechInfo->flags & CKF_VERIFY) ? "X" : ".",
 131      -                (mechInfo->flags & CKF_VERIFY_RECOVER) ? "X" : ".",
 132      -                (mechInfo->flags & CKF_GENERATE) ? "X" : ".",
 133      -                (mechInfo->flags & CKF_GENERATE_KEY_PAIR) ? "X" : ".",
 134      -                (mechInfo->flags & CKF_WRAP) ? "X" : ".",
 135      -                (mechInfo->flags & CKF_UNWRAP) ? "X" : ".",
 136      -                (mechInfo->flags & CKF_DERIVE) ? "X" : ".",
 137      -                (mechInfo->flags & ec_flags) ? "X" : ".");
      123 +            "%s  %s",
      124 +            (mechInfo->flags & CKF_HW) ? "X" : ".",
      125 +            (mechInfo->flags & CKF_ENCRYPT) ? "X" : ".",
      126 +            (mechInfo->flags & CKF_DECRYPT) ? "X" : ".",
      127 +            (mechInfo->flags & CKF_DIGEST) ? "X" : ".",
      128 +            (mechInfo->flags & CKF_SIGN) ? "X" : ".",
      129 +            (mechInfo->flags & CKF_SIGN_RECOVER) ? "X" : ".",
      130 +            (mechInfo->flags & CKF_VERIFY) ? "X" : ".",
      131 +            (mechInfo->flags & CKF_VERIFY_RECOVER) ? "X" : ".",
      132 +            (mechInfo->flags & CKF_GENERATE) ? "X" : ".",
      133 +            (mechInfo->flags & CKF_GENERATE_KEY_PAIR) ? "X" : ".",
      134 +            (mechInfo->flags & CKF_WRAP) ? "X" : ".",
      135 +            (mechInfo->flags & CKF_UNWRAP) ? "X" : ".",
      136 +            (mechInfo->flags & CKF_DERIVE) ? "X" : ".",
      137 +            (mechInfo->flags & ec_flags) ? "X" : ".");
 138  138  }
 139  139  
 140  140  /*
 141  141   * Converts the provided list of mechanism names in their string format to
 142      - * their corrsponding PKCS#11 mechanism IDs.
      142 + * their corresponding PKCS#11 mechanism IDs.
 143  143   *
 144  144   * The list of mechanism names to be converted is provided in the
 145  145   * "mlist" argument.  The list of converted mechanism IDs is returned
 146  146   * in the "pmech_list" argument.
 147  147   *
 148  148   * This function is called by list_metaslot_info() and
 149  149   * list_mechlist_for_lib() functions.
 150  150   */
 151  151  int
 152  152  convert_mechlist(CK_MECHANISM_TYPE **pmech_list, CK_ULONG *mech_count,
↓ open down ↓ 34 lines elided ↑ open up ↑
 187  187  {
 188  188          CK_RV   rv = CKR_OK;
 189  189          CK_RV   (*Tmp_C_GetFunctionList)(CK_FUNCTION_LIST_PTR_PTR);
 190  190          CK_FUNCTION_LIST_PTR    prov_funcs; /* Provider's function list */
 191  191          CK_SLOT_ID_PTR          prov_slots = NULL; /* Provider's slot list */
 192  192          CK_MECHANISM_TYPE_PTR   pmech_list; /* mechanism list for a slot */
 193  193          CK_SLOT_INFO    slotinfo;
 194  194          CK_ULONG        slot_count;
 195  195          CK_ULONG        mech_count;
 196  196          uentry_t        *puent = NULL;
 197      -        boolean_t lib_initialized = B_FALSE;
 198      -        void    *dldesc = NULL;
 199      -        char    *dl_error;
 200      -        char    *mech_name;
 201      -        char    *isa;
 202      -        char    libpath[MAXPATHLEN];
 203      -        char    buf[MAXPATHLEN];
 204      -        int     i, j;
 205      -        int     rc = SUCCESS;
      197 +        boolean_t       lib_initialized = B_FALSE;
      198 +        void            *dldesc = NULL;
      199 +        char            *dl_error;
      200 +        const char      *mech_name;
      201 +        char            *isa;
      202 +        char            libpath[MAXPATHLEN];
      203 +        char            buf[MAXPATHLEN];
      204 +        int             i, j;
      205 +        int             rc = SUCCESS;
 206  206  
 207  207          if (libname == NULL) {
 208  208                  /* should not happen */
 209  209                  cryptoerror(LOG_STDERR, gettext("internal error."));
 210  210                  cryptodebug("list_mechlist_for_lib() - libname is NULL.");
 211  211                  return (FAILURE);
 212  212          }
 213  213  
 214  214          /* Check if the library is in the pkcs11.conf file */
 215  215          if ((puent = getent_uef(libname)) == NULL) {
↓ open down ↓ 62 lines elided ↑ open up ↑
 278  278           * set to FALSE so all potential slots are returned.
 279  279           */
 280  280          rv = prov_funcs->C_GetSlotList(FALSE, NULL_PTR, &slot_count);
 281  281          if (rv != CKR_OK) {
 282  282                  cryptodebug("failed to get the slotlist from %s.", libname);
 283  283                  rc = FAILURE;
 284  284                  goto clean_exit;
 285  285          } else if (slot_count == 0) {
 286  286                  if (!no_warn)
 287  287                          (void) printf(gettext("%s: no slots presented.\n"),
 288      -                                libname);
      288 +                            libname);
 289  289                  rc = SUCCESS;
 290  290                  goto clean_exit;
 291  291          }
 292  292  
 293  293          /* Allocate memory for the slot list */
 294  294          prov_slots = malloc(slot_count * sizeof (CK_SLOT_ID));
 295  295          if (prov_slots == NULL) {
 296  296                  cryptodebug("out of memory.");
 297  297                  rc = FAILURE;
 298  298                  goto clean_exit;
↓ open down ↓ 35 lines elided ↑ open up ↑
 334  334                  rv = prov_funcs->C_GetSlotInfo(prov_slots[i], &slotinfo);
 335  335                  if (rv != CKR_OK) {
 336  336                          cryptodebug("failed to get slotinfo from %s", libname);
 337  337                          rc = FAILURE;
 338  338                          break;
 339  339                  }
 340  340                  if (verbose) {
 341  341                          CK_TOKEN_INFO tokeninfo;
 342  342  
 343  343                          (void) printf(gettext("Description: %.64s\n"
 344      -                                "Manufacturer: %.32s\n"
 345      -                                "PKCS#11 Version: %d.%d\n"),
 346      -                                slotinfo.slotDescription,
 347      -                                slotinfo.manufacturerID,
 348      -                                prov_funcs->version.major,
 349      -                                prov_funcs->version.minor);
      344 +                            "Manufacturer: %.32s\n"
      345 +                            "PKCS#11 Version: %d.%d\n"),
      346 +                            slotinfo.slotDescription,
      347 +                            slotinfo.manufacturerID,
      348 +                            prov_funcs->version.major,
      349 +                            prov_funcs->version.minor);
 350  350  
 351  351                          (void) printf(gettext("Hardware Version: %d.%d\n"
 352      -                                "Firmware Version: %d.%d\n"),
 353      -                                slotinfo.hardwareVersion.major,
 354      -                                slotinfo.hardwareVersion.minor,
 355      -                                slotinfo.firmwareVersion.major,
 356      -                                slotinfo.firmwareVersion.minor);
      352 +                            "Firmware Version: %d.%d\n"),
      353 +                            slotinfo.hardwareVersion.major,
      354 +                            slotinfo.hardwareVersion.minor,
      355 +                            slotinfo.firmwareVersion.major,
      356 +                            slotinfo.firmwareVersion.minor);
 357  357  
 358  358                          (void) printf(gettext("Token Present: %s\n"),
 359      -                                (slotinfo.flags & CKF_TOKEN_PRESENT ?
 360      -                                gettext("True") : gettext("False")));
      359 +                            (slotinfo.flags & CKF_TOKEN_PRESENT ?
      360 +                            gettext("True") : gettext("False")));
 361  361  
 362  362                          display_slot_flags(slotinfo.flags);
 363  363  
 364  364                          rv = prov_funcs->C_GetTokenInfo(prov_slots[i],
 365      -                                &tokeninfo);
      365 +                            &tokeninfo);
 366  366                          if (rv != CKR_OK) {
 367  367                                  cryptodebug("Failed to get "
 368      -                                        "token info from %s", libname);
      368 +                                    "token info from %s", libname);
 369  369                                  rc = FAILURE;
 370  370                                  break;
 371  371                          }
 372  372  
 373  373                          (void) printf(gettext("Token Label: %.32s\n"
 374      -                                "Manufacturer ID: %.32s\n"
 375      -                                "Model: %.16s\n"
 376      -                                "Serial Number: %.16s\n"
 377      -                                "Hardware Version: %d.%d\n"
 378      -                                "Firmware Version: %d.%d\n"
 379      -                                "UTC Time: %.16s\n"
 380      -                                "PIN Length: %d-%d\n"),
 381      -                                tokeninfo.label,
 382      -                                tokeninfo.manufacturerID,
 383      -                                tokeninfo.model,
 384      -                                tokeninfo.serialNumber,
 385      -                                tokeninfo.hardwareVersion.major,
 386      -                                tokeninfo.hardwareVersion.minor,
 387      -                                tokeninfo.firmwareVersion.major,
 388      -                                tokeninfo.firmwareVersion.minor,
 389      -                                tokeninfo.utcTime,
 390      -                                tokeninfo.ulMinPinLen,
 391      -                                tokeninfo.ulMaxPinLen);
      374 +                            "Manufacturer ID: %.32s\n"
      375 +                            "Model: %.16s\n"
      376 +                            "Serial Number: %.16s\n"
      377 +                            "Hardware Version: %d.%d\n"
      378 +                            "Firmware Version: %d.%d\n"
      379 +                            "UTC Time: %.16s\n"
      380 +                            "PIN Length: %d-%d\n"),
      381 +                            tokeninfo.label,
      382 +                            tokeninfo.manufacturerID,
      383 +                            tokeninfo.model,
      384 +                            tokeninfo.serialNumber,
      385 +                            tokeninfo.hardwareVersion.major,
      386 +                            tokeninfo.hardwareVersion.minor,
      387 +                            tokeninfo.firmwareVersion.major,
      388 +                            tokeninfo.firmwareVersion.minor,
      389 +                            tokeninfo.utcTime,
      390 +                            tokeninfo.ulMinPinLen,
      391 +                            tokeninfo.ulMaxPinLen);
 392  392  
 393  393                          display_token_flags(tokeninfo.flags);
 394  394                  }
 395  395  
 396  396                  if (mlist == NULL) {
 397  397                          rv = prov_funcs->C_GetMechanismList(prov_slots[i],
 398      -                                NULL_PTR, &mech_count);
      398 +                            NULL_PTR, &mech_count);
 399  399                          if (rv != CKR_OK) {
 400  400                                  cryptodebug(
 401      -                                        "failed to call C_GetMechanismList() "
 402      -                                        "from %s.", libname);
      401 +                                    "failed to call C_GetMechanismList() "
      402 +                                    "from %s.", libname);
 403  403                                  rc = FAILURE;
 404  404                                  break;
 405  405                          }
 406  406  
 407  407                          if (mech_count == 0) {
 408  408                                  /* no mechanisms in this slot */
 409  409                                  continue;
 410  410                          }
 411  411  
 412  412                          pmech_list = malloc(mech_count *
 413      -                                        sizeof (CK_MECHANISM_TYPE));
      413 +                            sizeof (CK_MECHANISM_TYPE));
 414  414                          if (pmech_list == NULL) {
 415  415                                  cryptodebug("out of memory");
 416  416                                  rc = FAILURE;
 417  417                                  break;
 418  418                          }
 419  419  
 420  420                          /* Get the actual mechanism list */
 421  421                          rv = prov_funcs->C_GetMechanismList(prov_slots[i],
 422      -                                pmech_list, &mech_count);
      422 +                            pmech_list, &mech_count);
 423  423                          if (rv != CKR_OK) {
 424  424                                  cryptodebug(
 425      -                                        "failed to call C_GetMechanismList() "
 426      -                                        "from %s.", libname);
      425 +                                    "failed to call C_GetMechanismList() "
      426 +                                    "from %s.", libname);
 427  427                                  (void) free(pmech_list);
 428  428                                  rc = FAILURE;
 429  429                                  break;
 430  430                          }
 431  431                  } else  {
 432  432                          /* use the mechanism list passed in */
 433  433                          rc = convert_mechlist(&pmech_list, &mech_count, mlist);
 434  434                          if (rc != SUCCESS) {
 435  435                                  goto clean_exit;
 436  436                          }
↓ open down ↓ 2 lines elided ↑ open up ↑
 439  439                          (void) printf(gettext("Mechanisms:\n"));
 440  440  
 441  441                  if (verbose && show_mechs) {
 442  442                          display_verbose_mech_header();
 443  443                  }
 444  444                  /*
 445  445                   * Merge the current mechanism list into the returning
 446  446                   * mechanism list.
 447  447                   */
 448  448                  for (j = 0; show_mechs && j < mech_count; j++) {
 449      -                        mech_name = pkcs11_mech2str(pmech_list[j]);
 450      -                        (void) printf("%-29s", mech_name);
      449 +                        CK_MECHANISM_TYPE       mech = pmech_list[j];
      450 +
      451 +                        if (mech > CKM_VENDOR_DEFINED) {
      452 +                                (void) printf("%#lx", mech);
      453 +                        } else {
      454 +                                mech_name = pkcs11_mech2str(mech);
      455 +                                (void) printf("%-29s", mech_name);
      456 +                        }
      457 +
 451  458                          if (verbose) {
 452  459                                  CK_MECHANISM_INFO mech_info;
 453  460                                  rv = prov_funcs->C_GetMechanismInfo(
 454      -                                    prov_slots[i], pmech_list[j], &mech_info);
      461 +                                    prov_slots[i], mech, &mech_info);
 455  462                                  if (rv != CKR_OK) {
 456  463                                          cryptodebug(
 457  464                                              "failed to call "
 458  465                                              "C_GetMechanismInfo() from %s.",
 459  466                                              libname);
 460  467                                          (void) free(pmech_list);
 461  468                                          rc = FAILURE;
 462  469                                          break;
 463  470                                  }
 464  471                                  display_mech_info(&mech_info);
↓ open down ↓ 642 lines elided ↑ open up ↑
1107 1114                      tmpfile_name, strerror(err));
1108 1115          }
1109 1116  
1110 1117          return (rc);
1111 1118  }
1112 1119  
1113 1120  
1114 1121  int
1115 1122  display_policy(uentry_t *puent)
1116 1123  {
1117      -        CK_MECHANISM_TYPE  mech_id;
1118      -        char *mech_name;
1119      -        umechlist_t *ptr;
     1124 +        CK_MECHANISM_TYPE       mech_id;
     1125 +        const char              *mech_name;
     1126 +        umechlist_t             *ptr;
1120 1127  
1121 1128          if (puent == NULL) {
1122 1129                  return (SUCCESS);
1123 1130          }
1124 1131  
1125 1132          if (puent->flag_enabledlist == B_FALSE) {
1126 1133                  (void) printf(gettext("%s: all mechanisms are enabled"),
1127 1134                      puent->name);
1128 1135                  ptr = puent->policylist;
1129 1136                  if (ptr == NULL) {
1130 1137                          (void) printf(".");
1131 1138                  } else {
1132 1139                          (void) printf(gettext(", except "));
1133 1140                          while (ptr != NULL) {
1134 1141                                  mech_id = strtoul(ptr->name, NULL, 0);
1135 1142                                  if (mech_id & CKO_VENDOR_DEFINED) {
1136 1143                                          /* vendor defined mechanism */
1137 1144                                          (void) printf("%s", ptr->name);
1138 1145                                  } else {
1139      -                                        mech_name = pkcs11_mech2str(mech_id);
1140      -                                        if (mech_name == NULL) {
1141      -                                                return (FAILURE);
     1146 +                                        if (mech_id > CKM_VENDOR_DEFINED) {
     1147 +                                                (void) printf("%#lx", mech_id);
     1148 +                                        } else {
     1149 +                                                mech_name = pkcs11_mech2str(
     1150 +                                                    mech_id);
     1151 +                                                if (mech_name == NULL) {
     1152 +                                                        return (FAILURE);
     1153 +                                                }
     1154 +                                                (void) printf("%s", mech_name);
1142 1155                                          }
1143      -                                        (void) printf("%s", mech_name);
1144      -                                        free(mech_name);
1145 1156                                  }
1146 1157  
1147 1158                                  ptr = ptr->next;
1148 1159                                  if (ptr == NULL) {
1149 1160                                          (void) printf(".");
1150 1161                                  } else {
1151 1162                                          (void) printf(",");
1152 1163                                  }
1153 1164                          }
1154 1165                  }
↓ open down ↓ 9 lines elided ↑ open up ↑
1164 1175                                  mech_id = strtoul(ptr->name, NULL, 0);
1165 1176                                  if (mech_id & CKO_VENDOR_DEFINED) {
1166 1177                                          /* vendor defined mechanism */
1167 1178                                          (void) printf("%s", ptr->name);
1168 1179                                  } else {
1169 1180                                          mech_name = pkcs11_mech2str(mech_id);
1170 1181                                          if (mech_name == NULL) {
1171 1182                                                  return (FAILURE);
1172 1183                                          }
1173 1184                                          (void) printf("%s", mech_name);
1174      -                                        free(mech_name);
1175 1185                                  }
1176 1186                                  ptr = ptr->next;
1177 1187                                  if (ptr == NULL) {
1178 1188                                          (void) printf(".");
1179 1189                                  } else {
1180 1190                                          (void) printf(",");
1181 1191                                  }
1182 1192                          }
1183 1193                  }
1184 1194          }
↓ open down ↓ 9 lines elided ↑ open up ↑
1194 1204  print_uef_policy(uentry_t *puent)
1195 1205  {
1196 1206          flag_val_t rng_flag;
1197 1207  
1198 1208          if (puent == NULL) {
1199 1209                  return (FAILURE);
1200 1210          }
1201 1211  
1202 1212          rng_flag = NO_RNG;
1203 1213          if (list_mechlist_for_lib(puent->name, NULL, &rng_flag, B_TRUE,
1204      -                B_FALSE, B_FALSE) != SUCCESS) {
     1214 +            B_FALSE, B_FALSE) != SUCCESS) {
1205 1215                  cryptoerror(LOG_STDERR,
1206 1216                      gettext("%s internal error."), puent->name);
1207 1217                  return (FAILURE);
1208 1218          }
1209 1219  
1210 1220          if (display_policy(puent) != SUCCESS) {
1211 1221                  goto failed_exit;
1212 1222          }
1213 1223  
1214 1224  
↓ open down ↓ 76 lines elided ↑ open up ↑
1291 1301                      strerror(err));
1292 1302                  cryptodebug("failed to open %s for write.", _PATH_PKCS11_CONF);
1293 1303                  return (FAILURE);
1294 1304          }
1295 1305  
1296 1306          /* Lock the pkcs11.conf file */
1297 1307          if (lockf(fileno(pfile), F_TLOCK, 0) == -1) {
1298 1308                  err = errno;
1299 1309                  cryptoerror(LOG_STDERR,
1300 1310                      gettext("failed to update the configuration - %s"),
1301      -                        strerror(err));
     1311 +                    strerror(err));
1302 1312                  (void) fclose(pfile);
1303 1313                  return (FAILURE);
1304 1314          }
1305 1315  
1306 1316          /*
1307 1317           * Create a temporary file in the /etc/crypto directory to save
1308 1318           * updated configuration file first.
1309 1319           */
1310 1320          (void) strlcpy(tmpfile_name, TMPFILE_TEMPLATE, sizeof (tmpfile_name));
1311 1321          if (mkstemp(tmpfile_name) == -1) {
↓ open down ↓ 501 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX