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/softASN1.c
          +++ new/usr/src/lib/pkcs11/pkcs11_softtoken/common/softASN1.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  /*
  23   23   * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
  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 <strings.h>
  30   31  #include <lber.h>
  31   32  #include <security/cryptoki.h>
  32   33  #include "softDSA.h"
  33   34  #include "softDH.h"
  34   35  #include "softRSA.h"
↓ open down ↓ 45 lines elided ↑ open up ↑
  80   81          if (src->big_value_len == 0) {
  81   82                  dst->big_value = NULL;
  82   83                  dst->big_value_len = 0;
  83   84                  return (CKR_OK);
  84   85          }
  85   86          /*
  86   87           * Realloc() may free() or shrink previous memory location, so
  87   88           * clear out potentially sensitive data before that happens.
  88   89           */
  89   90          if (dst->big_value != NULL)
  90      -                (void) memset(dst->big_value, 0x0, dst->big_value_len);
       91 +                explicit_bzero(dst->big_value, dst->big_value_len);
  91   92  
  92   93          padding = (src->big_value[0] < 0x80) ? 0 : 1;
  93   94          dst->big_value_len = src->big_value_len + padding;
  94   95  
  95   96          dst->big_value = realloc(dst->big_value, dst->big_value_len);
  96   97          if (dst->big_value == NULL)
  97   98                  return (CKR_HOST_MEMORY);
  98   99  
  99  100          /* Set zero-pad at first byte, then append actual big_value. */
 100  101          dst->big_value[0] = 0x0;
↓ open down ↓ 230 lines elided ↑ open up ↑
 331  332                  *buf_len = p8obj_octs->bv_len;
 332  333                  rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 333  334                  goto cleanup_rsapri2asn;
 334  335          }
 335  336  
 336  337          *buf_len = p8obj_octs->bv_len;
 337  338          (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 338  339  
 339  340  cleanup_rsapri2asn:
 340  341  
 341      -        if (tmp_pad.big_value != NULL) {
 342      -                (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 343      -                free(tmp_pad.big_value);
 344      -        }
      342 +        freezero(tmp_pad.big_value, tmp_pad.big_value_len);
 345  343  
 346  344          if (key_asn != NULLBER)
 347  345                  ber_free(key_asn, 1);
 348  346  
 349  347          if (key_octs != NULL)
 350  348                  ber_bvfree(key_octs);
 351  349  
 352  350          if (p8obj_asn != NULLBER)
 353  351                  ber_free(p8obj_asn, 1);
 354  352  
↓ open down ↓ 165 lines elided ↑ open up ↑
 520  518                  *buf_len = p8obj_octs->bv_len;
 521  519                  rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 522  520                  goto cleanup_dsapri2asn;
 523  521          }
 524  522  
 525  523          *buf_len = p8obj_octs->bv_len;
 526  524          (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 527  525  
 528  526  cleanup_dsapri2asn:
 529  527  
 530      -        if (tmp_pad.big_value != NULL) {
 531      -                (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 532      -                free(tmp_pad.big_value);
 533      -        }
      528 +        freezero(tmp_pad.big_value, tmp_pad.big_value_len);
 534  529  
 535  530          if (key_asn != NULLBER)
 536  531                  ber_free(key_asn, 1);
 537  532  
 538  533          if (key_octs != NULL)
 539  534                  ber_bvfree(key_octs);
 540  535  
 541  536          if (p8obj_asn != NULLBER)
 542  537                  ber_free(p8obj_asn, 1);
 543  538  
↓ open down ↓ 150 lines elided ↑ open up ↑
 694  689                  *buf_len = p8obj_octs->bv_len;
 695  690                  rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 696  691                  goto cleanup_dhpri2asn;
 697  692          }
 698  693  
 699  694          *buf_len = p8obj_octs->bv_len;
 700  695          (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 701  696  
 702  697  cleanup_dhpri2asn:
 703  698  
 704      -        if (tmp_pad.big_value != NULL) {
 705      -                (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 706      -                free(tmp_pad.big_value);
 707      -        }
      699 +        freezero(tmp_pad.big_value, tmp_pad.big_value_len);
 708  700  
 709  701          if (key_asn != NULLBER)
 710  702                  ber_free(key_asn, 1);
 711  703  
 712  704          if (key_octs != NULL)
 713  705                  ber_bvfree(key_octs);
 714  706  
 715  707          if (p8obj_asn != NULLBER)
 716  708                  ber_free(p8obj_asn, 1);
 717  709  
↓ open down ↓ 168 lines elided ↑ open up ↑
 886  878                  *buf_len = p8obj_octs->bv_len;
 887  879                  rv = (buf == NULL) ? CKR_OK : CKR_BUFFER_TOO_SMALL;
 888  880                  goto cleanup_x942dhpri2asn;
 889  881          }
 890  882  
 891  883          *buf_len = p8obj_octs->bv_len;
 892  884          (void) memcpy(buf, p8obj_octs->bv_val, *buf_len);
 893  885  
 894  886  cleanup_x942dhpri2asn:
 895  887  
 896      -        if (tmp_pad.big_value != NULL) {
 897      -                (void) memset(tmp_pad.big_value, 0x0, tmp_pad.big_value_len);
 898      -                free(tmp_pad.big_value);
 899      -        }
      888 +        freezero(tmp_pad.big_value, tmp_pad.big_value_len);
 900  889  
 901  890          if (key_asn != NULLBER)
 902  891                  ber_free(key_asn, 1);
 903  892  
 904  893          if (key_octs != NULL)
 905  894                  ber_bvfree(key_octs);
 906  895  
 907  896          if (p8obj_asn != NULLBER)
 908  897                  ber_free(p8obj_asn, 1);
 909  898  
↓ open down ↓ 323 lines elided ↑ open up ↑
1233 1222          bigint_attr_cleanup(KEY_PRI_RSA_PUBEXPO(keyp));
1234 1223          bigint_attr_cleanup(KEY_PRI_RSA_PRIEXPO(keyp));
1235 1224          bigint_attr_cleanup(KEY_PRI_RSA_PRIME1(keyp));
1236 1225          bigint_attr_cleanup(KEY_PRI_RSA_PRIME2(keyp));
1237 1226          bigint_attr_cleanup(KEY_PRI_RSA_EXPO1(keyp));
1238 1227          bigint_attr_cleanup(KEY_PRI_RSA_EXPO2(keyp));
1239 1228          bigint_attr_cleanup(KEY_PRI_RSA_COEF(keyp));
1240 1229  
1241 1230  cleanup_asn2rsapri:
1242 1231  
1243      -        if (tmp_nopad.big_value != NULL) {
1244      -                (void) memset(tmp_nopad.big_value, 0x0,
1245      -                    tmp_nopad.big_value_len);
1246      -                free(tmp_nopad.big_value);
1247      -        }
     1232 +        freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);
1248 1233  
1249 1234          if (p8obj_asn != NULLBER)
1250 1235                  ber_free(p8obj_asn, 1);
1251 1236  
1252 1237          if (key_octs.bv_val != NULL)
1253 1238                  free(key_octs.bv_val);
1254 1239  
1255 1240          if (key_asn != NULLBER)
1256 1241                  ber_free(key_asn, 1);
1257 1242  
↓ open down ↓ 183 lines elided ↑ open up ↑
1441 1426  
1442 1427  error_asn2dsapri:
1443 1428  
1444 1429          bigint_attr_cleanup(KEY_PRI_DSA_PRIME(keyp));
1445 1430          bigint_attr_cleanup(KEY_PRI_DSA_SUBPRIME(keyp));
1446 1431          bigint_attr_cleanup(KEY_PRI_DSA_BASE(keyp));
1447 1432          bigint_attr_cleanup(KEY_PRI_DSA_VALUE(keyp));
1448 1433  
1449 1434  cleanup_asn2dsapri:
1450 1435  
1451      -        if (tmp_nopad.big_value != NULL) {
1452      -                (void) memset(tmp_nopad.big_value, 0x0,
1453      -                    tmp_nopad.big_value_len);
1454      -                free(tmp_nopad.big_value);
1455      -        }
     1436 +        freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);
1456 1437  
1457 1438          if (p8obj_asn != NULLBER)
1458 1439                  ber_free(p8obj_asn, 1);
1459 1440  
1460 1441          if (key_octs.bv_val != NULL)
1461 1442                  free(key_octs.bv_val);
1462 1443  
1463 1444          if (key_asn != NULLBER)
1464 1445                  ber_free(key_asn, 1);
1465 1446  
↓ open down ↓ 159 lines elided ↑ open up ↑
1625 1606          goto cleanup_asn2dhpri;
1626 1607  
1627 1608  error_asn2dhpri:
1628 1609  
1629 1610          bigint_attr_cleanup(KEY_PRI_DH_PRIME(keyp));
1630 1611          bigint_attr_cleanup(KEY_PRI_DH_BASE(keyp));
1631 1612          bigint_attr_cleanup(KEY_PRI_DH_VALUE(keyp));
1632 1613  
1633 1614  cleanup_asn2dhpri:
1634 1615  
1635      -        if (tmp_nopad.big_value != NULL) {
1636      -                (void) memset(tmp_nopad.big_value, 0x0,
1637      -                    tmp_nopad.big_value_len);
1638      -                free(tmp_nopad.big_value);
1639      -        }
     1616 +        freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);
1640 1617  
1641 1618          if (p8obj_asn != NULLBER)
1642 1619                  ber_free(p8obj_asn, 1);
1643 1620  
1644 1621          if (key_octs.bv_val != NULL)
1645 1622                  free(key_octs.bv_val);
1646 1623  
1647 1624          if (key_asn != NULLBER)
1648 1625                  ber_free(key_asn, 1);
1649 1626  
↓ open down ↓ 183 lines elided ↑ open up ↑
1833 1810  
1834 1811  error_asn2x942dhpri:
1835 1812  
1836 1813          bigint_attr_cleanup(KEY_PRI_DH942_PRIME(keyp));
1837 1814          bigint_attr_cleanup(KEY_PRI_DH942_BASE(keyp));
1838 1815          bigint_attr_cleanup(KEY_PRI_DH942_SUBPRIME(keyp));
1839 1816          bigint_attr_cleanup(KEY_PRI_DH942_VALUE(keyp));
1840 1817  
1841 1818  cleanup_asn2x942dhpri:
1842 1819  
1843      -        if (tmp_nopad.big_value != NULL) {
1844      -                (void) memset(tmp_nopad.big_value, 0x0,
1845      -                    tmp_nopad.big_value_len);
1846      -                free(tmp_nopad.big_value);
1847      -        }
     1820 +        freezero(tmp_nopad.big_value, tmp_nopad.big_value_len);
1848 1821  
1849 1822          if (p8obj_asn != NULLBER)
1850 1823                  ber_free(p8obj_asn, 1);
1851 1824  
1852 1825          if (key_octs.bv_val != NULL)
1853 1826                  free(key_octs.bv_val);
1854 1827  
1855 1828          if (key_asn != NULLBER)
1856 1829                  ber_free(key_asn, 1);
1857 1830  
1858 1831          return (rv);
1859 1832  }
1860 1833  
1861 1834  /*
1862 1835   * Decode the object key from ASN.1 format into soft_object_t.
1863 1836   */
1864 1837  CK_RV
1865 1838  soft_asn1_to_object(soft_object_t *objp, uchar_t *buf, ulong_t buf_len)
1866 1839  {
1867      -        CK_RV           rv = CKR_OK;
     1840 +        CK_RV           rv = CKR_OK;
1868 1841          CK_OBJECT_CLASS class = objp->class;
1869 1842          CK_KEY_TYPE     keytype = objp->key_type;
1870 1843          private_key_obj_t *pvk;
1871 1844  
1872 1845          switch (class) {
1873 1846  
1874 1847          case CKO_PRIVATE_KEY:
1875 1848                  /* Allocate storage for Private Key Object. */
1876 1849                  if ((pvk = calloc(1, sizeof (private_key_obj_t))) == NULL) {
1877 1850                          rv = CKR_HOST_MEMORY;
↓ open down ↓ 40 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX