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/softKeysUtil.c
          +++ new/usr/src/lib/pkcs11/pkcs11_softtoken/common/softKeysUtil.c
↓ open down ↓ 13 lines elided ↑ open up ↑
  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   22   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
       24 + * Copyright (c) 2018, Joyent, Inc.
  24   25   */
  25   26  
  26   27  #include <pthread.h>
  27   28  #include <stdlib.h>
  28   29  #include <string.h>
  29   30  #include <strings.h>
  30   31  #include <sys/types.h>
  31   32  #include <security/cryptoki.h>
  32   33  #include <sys/crypto/common.h>
  33   34  #include <aes_impl.h>
↓ open down ↓ 781 lines elided ↑ open up ↑
 815  816                  (void) memcpy(A + i*hashSize, Ai, AiLen);
 816  817          }
 817  818  
 818  819          /*
 819  820           * Step 8.
 820  821           * The final output of this process is the A buffer
 821  822           */
 822  823          (void) memcpy(keybuf, A, keysize);
 823  824  
 824  825  cleanup:
 825      -        if (A) {
 826      -                bzero(A, Alen);
 827      -                free(A);
 828      -        }
 829      -        if (Ai) {
 830      -                bzero(Ai, AiLen);
 831      -                free(Ai);
 832      -        }
 833      -        if (B) {
 834      -                bzero(B, Blen);
 835      -                free(B);
 836      -        }
 837      -        if (D) {
 838      -                bzero(D, Dlen);
 839      -                free(D);
 840      -        }
 841      -        if (I) {
 842      -                bzero(I, Ilen);
 843      -                free(I);
 844      -        }
      826 +        freezero(A, Alen);
      827 +        freezero(Ai, AiLen);
      828 +        freezero(B, Blen);
      829 +        freezero(D, Dlen);
      830 +        freezero(I, Ilen);
 845  831          return (rv);
 846  832  }
 847  833  
 848  834  CK_RV
 849  835  soft_derivekey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
 850  836      soft_object_t *basekey_p, CK_ATTRIBUTE_PTR pTemplate,
 851  837      CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
 852  838  {
 853  839  
 854  840          CK_RV rv = CKR_OK;
↓ open down ↓ 538 lines elided ↑ open up ↑
1393 1379                   * Adjust the key pointer so we always append the
1394 1380                   * PRF output to the current key.
1395 1381                   */
1396 1382                  rv = do_prf(session_p, params, hmac_key,
1397 1383                      salt, params->ulSaltSourceDataLen + 4, keydata,
1398 1384                      ((i + 1) == blocks ? remainder : hLen));
1399 1385  
1400 1386                  keydata += hLen;
1401 1387          }
1402 1388          (void) soft_delete_object(session_p, hmac_key, B_FALSE, B_FALSE);
1403      -        free(salt);
     1389 +        freezero(salt, params->ulSaltSourceDataLen);
1404 1390  
1405 1391          return (rv);
1406 1392  }
1407 1393  
1408 1394  CK_RV
1409 1395  soft_wrapkey(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
1410 1396                  soft_object_t *wrappingKey_p, soft_object_t *hkey_p,
1411 1397                  CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
1412 1398  {
1413 1399          CK_RV           rv = CKR_OK;
↓ open down ↓ 114 lines elided ↑ open up ↑
1528 1514          rv = soft_encrypt_init(session_p, pMechanism, wrappingKey_p);
1529 1515          if (rv != CKR_OK)
1530 1516                  goto cleanup_wrap;
1531 1517  
1532 1518          rv = soft_encrypt(session_p, padded_data, padded_len,
1533 1519              pWrappedKey, pulWrappedKeyLen);
1534 1520  
1535 1521  cleanup_wrap:
1536 1522          if (padded_data != NULL && padded_len != plain_len) {
1537 1523                  /* Clear buffer before returning to memory pool. */
1538      -                (void) memset(padded_data, 0x0, padded_len);
1539      -                free(padded_data);
     1524 +                freezero(padded_data, padded_len);
1540 1525          }
1541 1526  
1542 1527          if ((hkey_p->class != CKO_SECRET_KEY) && (plain_data != NULL)) {
1543 1528                  /* Clear buffer before returning to memory pool. */
1544      -                (void) memset(plain_data, 0x0, plain_len);
1545      -                free(plain_data);
     1529 +                freezero(plain_data, plain_len);
1546 1530          }
1547 1531  
1548 1532          return (rv);
1549 1533  }
1550 1534  
1551 1535  /*
1552 1536   * Quick check for whether unwrapped key length is appropriate for key type
1553 1537   * and whether it needs to be truncated (in case the wrapping function had
1554 1538   * to pad the key prior to wrapping).
1555 1539   */
↓ open down ↓ 259 lines elided ↑ open up ↑
1815 1799          /* If it needs to be persistent, write it to the keystore */
1816 1800          if (IS_TOKEN_OBJECT(new_objp)) {
1817 1801                  persistent = B_TRUE;
1818 1802                  rv = soft_put_object_to_keystore(new_objp);
1819 1803                  if (rv != CKR_OK)
1820 1804                          goto cleanup_unwrap;
1821 1805          }
1822 1806  
1823 1807          if (new_objp->class != CKO_SECRET_KEY) {
1824 1808                  /* Clear buffer before returning to memory pool. */
1825      -                (void) memset(plain_data, 0x0, plain_len);
1826      -                free(plain_data);
     1809 +                freezero(plain_data, plain_len);
1827 1810          }
1828 1811  
1829 1812          *phKey = (CK_OBJECT_HANDLE)new_objp;
1830 1813  
1831 1814          return (CKR_OK);
1832 1815  
1833 1816  cleanup_unwrap:
1834 1817          /* The decrypted private key buffer must be freed explicitly. */
1835 1818          if ((new_objp->class != CKO_SECRET_KEY) && (plain_data != NULL)) {
1836 1819                  /* Clear buffer before returning to memory pool. */
1837      -                (void) memset(plain_data, 0x0, plain_len);
1838      -                free(plain_data);
     1820 +                freezero(plain_data, plain_len);
1839 1821          }
1840 1822  
1841 1823          /* sck and new_objp are indirectly free()d inside these functions */
1842 1824          if (IS_TOKEN_OBJECT(new_objp))
1843 1825                  soft_delete_token_object(new_objp, persistent, B_FALSE);
1844 1826          else
1845 1827                  soft_delete_object(session_p, new_objp, B_FALSE, B_FALSE);
1846 1828  
1847 1829          return (rv);
1848 1830  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX