Print this page
9156 Remove openssl dependency from pkcs11_tpm

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/pkcs11/pkcs11_tpm/common/tpm_specific.c
          +++ new/usr/src/lib/pkcs11/pkcs11_tpm/common/tpm_specific.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   *
  17   17   * You should have received a copy of the Common Public License
  18   18   * along with this program; if not, a copy can be viewed at
  19   19   * http://www.opensource.org/licenses/cpl1.0.php.
  20   20   */
  21   21  /*
  22   22   * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   * Copyright 2012 Milan Jurik. All rights reserved.
  25   25   * Copyright (c) 2016 by Delphix. All rights reserved.
       26 + * Copyright 2018 Jason King
  26   27   */
  27   28  
  28   29  #include <pthread.h>
  29   30  #include <string.h>
  30   31  
  31   32  #include <sys/types.h>
  32   33  #include <sys/stat.h>
  33   34  #include <uuid/uuid.h>
  34   35  #include <fcntl.h>
  35   36  #include <errno.h>
  36   37  #include <pwd.h>
  37   38  #include <syslog.h>
  38   39  
  39      -#include <openssl/rsa.h>
       40 +#include <sys/crypto/common.h>  /* For CRYPTO_BYTES2BITS */
       41 +#include <rsa_impl.h>
       42 +#include <padding.h>
  40   43  
  41   44  #include <tss/platform.h>
  42   45  #include <tss/tss_defines.h>
  43   46  #include <tss/tss_typedef.h>
  44   47  #include <tss/tss_structs.h>
  45   48  #include <tss/tss_error.h>
  46   49  #include <tss/tcs_error.h>
  47   50  #include <tss/tspi.h>
  48   51  #include <trousers/trousers.h>
  49   52  
↓ open down ↓ 2683 lines elided ↑ open up ↑
2733 2736          rc  = tpm_encrypt_data(hContext, hKey, in_data, in_data_len,
2734 2737              out_data, out_data_len);
2735 2738  
2736 2739          return (rc);
2737 2740  }
2738 2741  
2739 2742  /*
2740 2743   * RSA Verify Recover
2741 2744   *
2742 2745   * Public key crypto is done in software, not by the TPM.
2743      - * We bypass the TSPI library here in favor of calls directly
2744      - * to OpenSSL because we don't want to add any padding, the in_data (signature)
2745      - * already contains the data stream to be decrypted and is already
2746      - * padded and formatted correctly.
     2746 + * We use libsoftcrypto and perform the RSA operations ourselves similar
     2747 + * to how pkcs11_softtoken performs the operation.
2747 2748   */
2748 2749  CK_RV
2749 2750  token_specific_rsa_verify_recover(
2750 2751          TSS_HCONTEXT    hContext,
2751      -        CK_BYTE         *in_data,       /* signature */
2752      -        CK_ULONG        in_data_len,
2753      -        CK_BYTE         *out_data,      /* decrypted */
2754      -        CK_ULONG        *out_data_len,
     2752 +        CK_BYTE_PTR     pSignature,
     2753 +        CK_ULONG        ulSignatureLen,
     2754 +        CK_BYTE_PTR     pData,
     2755 +        CK_ULONG_PTR    pulDataLen,
2755 2756          OBJECT          *key_obj)
2756 2757  {
2757 2758          TSS_HKEY        hKey;
2758 2759          TSS_RESULT      result;
2759 2760          CK_RV           rc;
2760 2761          BYTE            *modulus;
2761 2762          UINT32          modLen;
2762      -        RSA             *rsa = NULL;
     2763 +        RSAbytekey      rsa = { 0 };
2763 2764          uchar_t         exp[] = { 0x01, 0x00, 0x01 };
2764      -        int             sslrv, num;
2765      -        BYTE            temp[MAX_RSA_KEYLENGTH];
2766      -        BYTE            outdata[MAX_RSA_KEYLENGTH];
2767      -        int             i;
     2765 +        CK_BYTE         plain_data[MAX_RSA_KEYLENGTH];
     2766 +        size_t          data_len;
2768 2767  
2769 2768          if ((rc = token_rsa_load_key(hContext, key_obj, &hKey))) {
2770 2769                  return (rc);
2771 2770          }
2772 2771  
2773 2772          if ((result = Tspi_GetAttribData(hKey, TSS_TSPATTRIB_RSAKEY_INFO,
2774 2773              TSS_TSPATTRIB_KEYINFO_RSA_MODULUS, &modLen, &modulus))) {
2775 2774                  stlogit("Tspi_GetAttribData: 0x%0x - %s",
2776 2775                      result, Trspi_Error_String(result));
2777 2776                  return (CKR_FUNCTION_FAILED);
2778 2777          }
2779 2778  
2780      -        if (in_data_len != modLen) {
     2779 +        if (ulSignatureLen != modLen) {
2781 2780                  rc = CKR_SIGNATURE_LEN_RANGE;
2782 2781                  goto end;
2783 2782          }
2784 2783  
2785      -        rsa = RSA_new();
2786      -        if (rsa == NULL) {
2787      -                rc = CKR_HOST_MEMORY;
2788      -                goto end;
2789      -        }
     2784 +        rsa.modulus = modulus;
     2785 +        rsa.modulus_bits = CRYPTO_BYTES2BITS(modLen);
     2786 +        rsa.pubexpo = exp;
     2787 +        rsa.pubexpo_bytes = sizeof (exp);
2790 2788  
2791      -        rsa->n = BN_bin2bn(modulus, modLen, rsa->n);
2792      -        rsa->e = BN_bin2bn(exp, sizeof (exp), rsa->e);
2793      -        if (rsa->n == NULL || rsa->e == NULL) {
2794      -                rc = CKR_HOST_MEMORY;
     2789 +        if ((rc = rsa_encrypt(&rsa, pSignature, modLen, plain_data)) != CKR_OK)
2795 2790                  goto end;
2796      -        }
2797 2791  
2798      -        rsa->flags |= RSA_FLAG_SIGN_VER;
2799      -
2800      -        /* use RSA_NO_PADDING because the data is already padded (PKCS1) */
2801      -        sslrv = RSA_public_encrypt(in_data_len, in_data, outdata,
2802      -            rsa, RSA_NO_PADDING);
2803      -        if (sslrv == -1) {
2804      -                rc = CKR_FUNCTION_FAILED;
     2792 +        data_len = modLen;
     2793 +        if ((rc = pkcs1_decode(PKCS1_VERIFY, plain_data, &data_len)) != CKR_OK)
2805 2794                  goto end;
2806      -        }
2807 2795  
2808      -        /* Strip leading 0's before stripping the padding */
2809      -        for (i = 0; i < sslrv; i++)
2810      -                if (outdata[i] != 0)
2811      -                        break;
     2796 +        (void) memcpy(pData, &plain_data[modLen - data_len], data_len);
     2797 +        *pulDataLen = data_len;
2812 2798  
2813      -        num = BN_num_bytes(rsa->n);
2814      -
2815      -        /* Use OpenSSL function for stripping PKCS#1 padding */
2816      -        sslrv = RSA_padding_check_PKCS1_type_1(temp, sizeof (temp),
2817      -            &outdata[i], sslrv - i, num);
2818      -
2819      -        if (sslrv < 0) {
2820      -                rc = CKR_FUNCTION_FAILED;
2821      -                goto end;
2822      -        }
2823      -
2824      -        if (*out_data_len < sslrv) {
2825      -                rc = CKR_BUFFER_TOO_SMALL;
2826      -                *out_data_len = 0;
2827      -                goto end;
2828      -        }
2829      -
2830      -        /* The return code indicates the number of bytes remaining */
2831      -        (void) memcpy(out_data, temp, sslrv);
2832      -        *out_data_len = sslrv;
2833 2799  end:
2834 2800          Tspi_Context_FreeMemory(hContext, modulus);
2835      -        if (rsa)
2836      -                RSA_free(rsa);
2837      -
2838 2801          return (rc);
2839 2802  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX