Print this page
9642 PKCS#11 softtoken should use explicit_bzero
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Alex Wilson <alex.wilson@joyent.com>

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/pkcs11/pkcs11_softtoken/common/softAttributeUtil.c
          +++ new/usr/src/lib/pkcs11/pkcs11_softtoken/common/softAttributeUtil.c
↓ open down ↓ 14 lines elided ↑ open up ↑
  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   22   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   * Copyright 2012 Milan Jurik. All rights reserved.
       25 + * Copyright (c) 2018, Joyent, Inc.
  25   26   */
  26   27  
  27   28  #include <stdlib.h>
  28   29  #include <string.h>
  29   30  #include <security/cryptoki.h>
  30   31  #include <sys/crypto/common.h>
  31   32  #include <arcfour.h>
  32   33  #include <aes_impl.h>
  33   34  #include <blowfish_impl.h>
  34   35  #include <bignum.h>
↓ open down ↓ 280 lines elided ↑ open up ↑
 315  316                          }
 316  317                          break;
 317  318                  }
 318  319          }
 319  320          return (rv);
 320  321  }
 321  322  
 322  323  static void
 323  324  cleanup_cert_attr(cert_attr_t *attr)
 324  325  {
 325      -        if (attr) {
 326      -                if (attr->value) {
 327      -                        (void) memset(attr->value, 0, attr->length);
 328      -                        free(attr->value);
 329      -                }
      326 +        if (attr != NULL) {
      327 +                freezero(attr->value, attr->length);
 330  328                  attr->value = NULL;
 331  329                  attr->length = 0;
 332  330          }
 333  331  }
 334  332  
 335  333  static CK_RV
 336  334  copy_cert_attr(cert_attr_t *src_attr, cert_attr_t **dest_attr)
 337  335  {
 338  336          CK_RV rv = CKR_OK;
 339  337  
 340  338          if (src_attr == NULL || dest_attr == NULL)
 341  339                  return (CKR_HOST_MEMORY);
 342  340  
 343  341          if (src_attr->value == NULL)
 344  342                  return (CKR_HOST_MEMORY);
 345  343  
 346  344          /* free memory if its already allocated */
 347  345          if (*dest_attr != NULL) {
 348      -                if ((*dest_attr)->value != (CK_BYTE *)NULL)
 349      -                        free((*dest_attr)->value);
      346 +                cleanup_cert_attr(*dest_attr);
 350  347          } else {
 351  348                  *dest_attr = malloc(sizeof (cert_attr_t));
 352  349                  if (*dest_attr == NULL)
 353  350                          return (CKR_HOST_MEMORY);
 354  351          }
 355  352  
 356  353          (*dest_attr)->value = NULL;
 357  354          (*dest_attr)->length = 0;
 358  355  
 359  356          if (src_attr->length) {
↓ open down ↓ 54 lines elided ↑ open up ↑
 414  411  void
 415  412  soft_cleanup_extra_attr(soft_object_t *object_p)
 416  413  {
 417  414  
 418  415          CK_ATTRIBUTE_INFO_PTR extra_attr;
 419  416          CK_ATTRIBUTE_INFO_PTR tmp;
 420  417  
 421  418          extra_attr = object_p->extra_attrlistp;
 422  419          while (extra_attr) {
 423  420                  tmp = extra_attr->next;
 424      -                if (extra_attr->attr.pValue)
      421 +                if (extra_attr->attr.pValue != NULL) {
 425  422                          /*
 426  423                           * All extra attributes in the extra attribute
 427  424                           * list have pValue points to the value of the
 428  425                           * attribute (with simple byte array type).
 429  426                           * Free the storage for the value of the attribute.
 430  427                           */
 431      -                        free(extra_attr->attr.pValue);
      428 +                        freezero(extra_attr->attr.pValue,
      429 +                            extra_attr->attr.ulValueLen);
      430 +                }
 432  431  
 433  432                  /* Free the storage for the attribute_info struct. */
 434  433                  free(extra_attr);
 435  434                  extra_attr = tmp;
 436  435          }
 437  436  
 438  437          object_p->extra_attrlistp = NULL;
 439  438  }
 440  439  
 441  440  
↓ open down ↓ 223 lines elided ↑ open up ↑
 665  664                   * the extra attribute list.
 666  665                   */
 667  666                  return (soft_add_extra_attr(template, object_p));
 668  667          }
 669  668  
 670  669          /* We found the attribute in the extra attribute list. */
 671  670          if ((template->pValue != NULL) &&
 672  671              (template->ulValueLen > 0)) {
 673  672                  if (template->ulValueLen > extra_attr->attr.ulValueLen) {
 674  673                          /* The old buffer is too small to hold the new value. */
 675      -                        if (extra_attr->attr.pValue != NULL)
      674 +                        if (extra_attr->attr.pValue != NULL) {
 676  675                                  /* Free storage for the old attribute value. */
 677      -                                free(extra_attr->attr.pValue);
      676 +                                freezero(extra_attr->attr.pValue,
      677 +                                    extra_attr->attr.ulValueLen);
      678 +                        }
 678  679  
 679  680                          /* Allocate storage for the new attribute value. */
 680  681                          extra_attr->attr.pValue = malloc(template->ulValueLen);
 681  682                          if (extra_attr->attr.pValue == NULL) {
 682  683                                  return (CKR_HOST_MEMORY);
 683  684                          }
 684  685                  }
 685  686  
 686  687                  /* Replace the attribute with new value. */
 687  688                  extra_attr->attr.ulValueLen = template->ulValueLen;
↓ open down ↓ 235 lines elided ↑ open up ↑
 923  924  
 924  925  CK_RV
 925  926  get_cert_attr_from_template(cert_attr_t **dest, CK_ATTRIBUTE_PTR src)
 926  927  {
 927  928          if (src->pValue != NULL && src->ulValueLen > 0) {
 928  929                  /*
 929  930                   * If the attribute was already set, clear out the
 930  931                   * existing value and release the memory.
 931  932                   */
 932  933                  if (*dest != NULL) {
 933      -                        if ((*dest)->value != NULL) {
 934      -                                (void) memset((*dest)->value, 0,
 935      -                                    (*dest)->length);
 936      -                                free((*dest)->value);
 937      -                        }
      934 +                        cleanup_cert_attr(*dest);
 938  935                  } else {
 939  936                          *dest = malloc(sizeof (cert_attr_t));
 940  937                          if (*dest == NULL) {
 941  938                                  return (CKR_HOST_MEMORY);
 942  939                          }
 943  940                          (void) memset(*dest, 0, sizeof (cert_attr_t));
 944  941                  }
 945  942                  (*dest)->value = malloc(src->ulValueLen);
 946  943                  if ((*dest)->value == NULL) {
 947  944                          free(*dest);
↓ open down ↓ 32 lines elided ↑ open up ↑
 980  977                   * not have enough space to hold the value.
 981  978                   */
 982  979                  template->ulValueLen = (CK_ULONG)-1;
 983  980                  return (CKR_BUFFER_TOO_SMALL);
 984  981          }
 985  982  }
 986  983  
 987  984  void
 988  985  string_attr_cleanup(CK_ATTRIBUTE_PTR template)
 989  986  {
 990      -
 991      -        if (template->pValue) {
 992      -                free(template->pValue);
 993      -                template->pValue = NULL;
 994      -                template->ulValueLen = 0;
 995      -        }
      987 +        freezero(template->pValue, template->ulValueLen);
      988 +        template->pValue = NULL;
      989 +        template->ulValueLen = 0;
 996  990  }
 997  991  
 998  992  /*
 999  993   * Release the storage allocated for object attribute with big integer
1000  994   * value.
1001  995   */
1002  996  void
1003  997  bigint_attr_cleanup(biginteger_t *big)
1004  998  {
1005  999  
1006 1000          if (big == NULL)
1007 1001                  return;
1008 1002  
1009      -        if (big->big_value) {
1010      -                (void) memset(big->big_value, 0, big->big_value_len);
1011      -                free(big->big_value);
1012      -                big->big_value = NULL;
1013      -                big->big_value_len = 0;
1014      -        }
     1003 +        freezero(big->big_value, big->big_value_len);
     1004 +        big->big_value = NULL;
     1005 +        big->big_value_len = 0;
1015 1006  }
1016 1007  
1017 1008  
1018 1009  /*
1019 1010   * Clean up and release all the storage allocated to hold the big integer
1020 1011   * attributes associated with the type (i.e. class) of the object. Also,
1021 1012   * release the storage allocated to the type of the object.
1022 1013   */
1023 1014  void
1024 1015  soft_cleanup_object_bigint_attrs(soft_object_t *object_p)
↓ open down ↓ 119 lines elided ↑ open up ↑
1144 1135                          free(OBJ_PRI(object_p));
1145 1136                          OBJ_PRI(object_p) = NULL;
1146 1137                  }
1147 1138                  break;
1148 1139  
1149 1140          case CKO_SECRET_KEY:
1150 1141                  if (OBJ_SEC(object_p)) {
1151 1142                          /* cleanup key data area */
1152 1143                          if (OBJ_SEC_VALUE(object_p) != NULL &&
1153 1144                              OBJ_SEC_VALUE_LEN(object_p) > 0) {
1154      -                                (void) memset(OBJ_SEC_VALUE(object_p), 0,
     1145 +                                freezero(OBJ_SEC_VALUE(object_p),
1155 1146                                      OBJ_SEC_VALUE_LEN(object_p));
1156      -                                free(OBJ_SEC_VALUE(object_p));
1157 1147                          }
1158 1148                          /* cleanup key schedule data area */
1159 1149                          if (OBJ_KEY_SCHED(object_p) != NULL &&
1160 1150                              OBJ_KEY_SCHED_LEN(object_p) > 0) {
1161      -                                (void) memset(OBJ_KEY_SCHED(object_p), 0,
     1151 +                                freezero(OBJ_KEY_SCHED(object_p),
1162 1152                                      OBJ_KEY_SCHED_LEN(object_p));
1163      -                                free(OBJ_KEY_SCHED(object_p));
1164 1153                          }
1165 1154  
1166 1155                          /* Release Secret Key Object struct. */
1167 1156                          free(OBJ_SEC(object_p));
1168 1157                          OBJ_SEC(object_p) = NULL;
1169 1158                  }
1170 1159                  break;
1171 1160  
1172 1161          case CKO_DOMAIN_PARAMETERS:
1173 1162                  if (OBJ_DOM(object_p)) {
↓ open down ↓ 5138 lines elided ↑ open up ↑
6312 6301  {
6313 6302          secret_key_obj_t *sk;
6314 6303  
6315 6304          sk = malloc(sizeof (secret_key_obj_t));
6316 6305          if (sk == NULL) {
6317 6306                  return (CKR_HOST_MEMORY);
6318 6307          }
6319 6308          (void) memcpy(sk, old_secret_key_obj_p, sizeof (secret_key_obj_t));
6320 6309  
6321 6310          /* copy the secret key value */
6322      -        sk->sk_value = malloc((sizeof (CK_BYTE) * sk->sk_value_len));
     6311 +        sk->sk_value = malloc(sk->sk_value_len);
6323 6312          if (sk->sk_value == NULL) {
6324 6313                  free(sk);
6325 6314                  return (CKR_HOST_MEMORY);
6326 6315          }
6327 6316          (void) memcpy(sk->sk_value, old_secret_key_obj_p->sk_value,
6328 6317              (sizeof (CK_BYTE) * sk->sk_value_len));
6329 6318  
6330 6319          /*
6331 6320           * Copy the pre-expanded key schedule.
6332 6321           */
6333 6322          if (old_secret_key_obj_p->key_sched != NULL &&
6334 6323              old_secret_key_obj_p->keysched_len > 0) {
6335 6324                  sk->key_sched = malloc(old_secret_key_obj_p->keysched_len);
6336 6325                  if (sk->key_sched == NULL) {
     6326 +                        freezero(sk->sk_value, sk->sk_value_len);
6337 6327                          free(sk);
6338 6328                          return (CKR_HOST_MEMORY);
6339 6329                  }
6340 6330                  sk->keysched_len = old_secret_key_obj_p->keysched_len;
6341 6331                  (void) memcpy(sk->key_sched, old_secret_key_obj_p->key_sched,
6342 6332                      sk->keysched_len);
6343 6333          }
6344 6334  
6345 6335          *new_secret_key_obj_p = sk;
6346 6336  
↓ open down ↓ 757 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX