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/softDecryptUtil.c
          +++ new/usr/src/lib/pkcs11/pkcs11_softtoken/common/softDecryptUtil.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  /*
  23   23   * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
       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 <modes/modes.h>
  33   34  #include <arcfour.h>
↓ open down ↓ 110 lines elided ↑ open up ↑
 144  145                  /* Save Initialization Vector (IV) in the context. */
 145  146                  (void) memcpy(soft_des_ctx->ivec, pMechanism->pParameter,
 146  147                      DES_BLOCK_LEN);
 147  148  
 148  149                  /* Allocate a context for DES cipher-block chaining. */
 149  150                  soft_des_ctx->des_cbc = (void *)des_cbc_ctx_init(
 150  151                      soft_des_ctx->key_sched, soft_des_ctx->keysched_len,
 151  152                      soft_des_ctx->ivec, key_p->key_type);
 152  153  
 153  154                  if (soft_des_ctx->des_cbc == NULL) {
 154      -                        bzero(soft_des_ctx->key_sched,
      155 +                        freezero(soft_des_ctx->key_sched,
 155  156                              soft_des_ctx->keysched_len);
 156      -                        free(soft_des_ctx->key_sched);
 157      -                        free(session_p->decrypt.context);
      157 +                        freezero(session_p->decrypt.context,
      158 +                            sizeof (soft_des_ctx_t));
 158  159                          session_p->decrypt.context = NULL;
 159  160                          (void) pthread_mutex_unlock(&session_p->session_mutex);
 160  161                          return (CKR_HOST_MEMORY);
 161  162                  }
 162  163  
 163  164                  (void) pthread_mutex_unlock(&session_p->session_mutex);
 164  165  
 165  166                  return (rv);
 166  167          }
 167  168          case CKM_AES_ECB:
↓ open down ↓ 32 lines elided ↑ open up ↑
 200  201                  /* Save Initialization Vector (IV) in the context. */
 201  202                  (void) memcpy(soft_aes_ctx->ivec, pMechanism->pParameter,
 202  203                      AES_BLOCK_LEN);
 203  204  
 204  205                  /* Allocate a context for AES cipher-block chaining. */
 205  206                  soft_aes_ctx->aes_cbc = (void *)aes_cbc_ctx_init(
 206  207                      soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
 207  208                      soft_aes_ctx->ivec);
 208  209  
 209  210                  if (soft_aes_ctx->aes_cbc == NULL) {
 210      -                        bzero(soft_aes_ctx->key_sched,
      211 +                        freezero(soft_aes_ctx->key_sched,
 211  212                              soft_aes_ctx->keysched_len);
 212      -                        free(soft_aes_ctx->key_sched);
 213      -                        free(session_p->decrypt.context);
      213 +                        freezero(session_p->decrypt.context,
      214 +                            sizeof (soft_aes_ctx_t));
 214  215                          session_p->decrypt.context = NULL;
 215  216                          (void) pthread_mutex_unlock(&session_p->session_mutex);
 216  217                          return (CKR_HOST_MEMORY);
 217  218                  }
 218  219  
 219  220                  (void) pthread_mutex_unlock(&session_p->session_mutex);
 220  221  
 221  222                  return (rv);
 222  223          }
 223  224          case CKM_AES_CTR:
↓ open down ↓ 16 lines elided ↑ open up ↑
 240  241                          return (rv);
 241  242  
 242  243                  (void) pthread_mutex_lock(&session_p->session_mutex);
 243  244  
 244  245                  soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 245  246                  soft_aes_ctx->aes_cbc = aes_ctr_ctx_init(
 246  247                      soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
 247  248                      pMechanism->pParameter);
 248  249  
 249  250                  if (soft_aes_ctx->aes_cbc == NULL) {
 250      -                        bzero(soft_aes_ctx->key_sched,
      251 +                        freezero(soft_aes_ctx->key_sched,
 251  252                              soft_aes_ctx->keysched_len);
 252      -                        free(soft_aes_ctx->key_sched);
 253      -                        free(session_p->decrypt.context);
      253 +                        freezero(session_p->decrypt.context,
      254 +                            sizeof (soft_aes_ctx_t));
 254  255                          session_p->decrypt.context = NULL;
 255  256                          rv = CKR_HOST_MEMORY;
 256  257                  }
 257  258  
 258  259                  (void) pthread_mutex_unlock(&session_p->session_mutex);
 259  260  
 260  261                  return (rv);
 261  262          }
 262  263          case CKM_BLOWFISH_CBC:
 263  264          {
↓ open down ↓ 21 lines elided ↑ open up ↑
 285  286                  (void) memcpy(soft_blowfish_ctx->ivec, pMechanism->pParameter,
 286  287                      BLOWFISH_BLOCK_LEN);
 287  288  
 288  289                  /* Allocate a context for CBC */
 289  290                  soft_blowfish_ctx->blowfish_cbc =
 290  291                      (void *)blowfish_cbc_ctx_init(soft_blowfish_ctx->key_sched,
 291  292                      soft_blowfish_ctx->keysched_len,
 292  293                      soft_blowfish_ctx->ivec);
 293  294  
 294  295                  if (soft_blowfish_ctx->blowfish_cbc == NULL) {
 295      -                        bzero(soft_blowfish_ctx->key_sched,
      296 +                        freezero(soft_blowfish_ctx->key_sched,
 296  297                              soft_blowfish_ctx->keysched_len);
 297      -                        free(soft_blowfish_ctx->key_sched);
 298      -                        free(session_p->decrypt.context = NULL);
      298 +                        freezero(session_p->decrypt.context,
      299 +                            sizeof (soft_blowfish_ctx_t));
      300 +                        session_p->decrypt.context = NULL;
 299  301                          (void) pthread_mutex_unlock(&session_p->session_mutex);
 300  302                          return (CKR_HOST_MEMORY);
 301  303                  }
 302  304  
 303  305                  (void) pthread_mutex_unlock(&session_p->session_mutex);
 304  306                  return (rv);
 305  307          }
 306  308  
 307  309          case CKM_RC4:
 308  310  
↓ open down ↓ 238 lines elided ↑ open up ↑
 547  549  
 548  550                  /*
 549  551                   * We should have only one block of data left in the
 550  552                   * remaining buffer.
 551  553                   */
 552  554                  if (soft_des_ctx->remain_len != DES_BLOCK_LEN) {
 553  555                          *pulLastPartLen = 0;
 554  556                          rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 555  557                          /* Cleanup memory space. */
 556  558                          free(soft_des_ctx->des_cbc);
 557      -                        bzero(soft_des_ctx->key_sched,
      559 +                        freezero(soft_des_ctx->key_sched,
 558  560                              soft_des_ctx->keysched_len);
 559      -                        free(soft_des_ctx->key_sched);
 560  561  
 561  562                          goto clean1;
 562  563                  }
 563  564  
 564  565                  out_len = DES_BLOCK_LEN;
 565  566  
 566  567                  /*
 567  568                   * If application asks for the length of the output buffer
 568  569                   * to hold the plaintext?
 569  570                   */
↓ open down ↓ 31 lines elided ↑ open up ↑
 601  602                                          *pulLastPartLen = 0;
 602  603                                  else
 603  604                                          *pulLastPartLen = out_len;
 604  605                          } else {
 605  606                                  *pulLastPartLen = 0;
 606  607                                  rv = CKR_FUNCTION_FAILED;
 607  608                          }
 608  609  
 609  610                          /* Cleanup memory space. */
 610  611                          free(soft_des_ctx->des_cbc);
 611      -                        bzero(soft_des_ctx->key_sched,
      612 +                        freezero(soft_des_ctx->key_sched,
 612  613                              soft_des_ctx->keysched_len);
 613      -                        free(soft_des_ctx->key_sched);
 614  614  
 615  615                  }
 616  616  
 617  617                  break;
 618  618          }
 619  619  
 620  620          case CKM_DES_CBC:
 621  621          case CKM_DES_ECB:
 622  622          case CKM_DES3_CBC:
 623  623          case CKM_DES3_ECB:
↓ open down ↓ 10 lines elided ↑ open up ↑
 634  634                  *pulLastPartLen = 0;
 635  635                  if (soft_des_ctx->remain_len != 0) {
 636  636                          rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 637  637                  } else {
 638  638                          if (pLastPart == NULL)
 639  639                                  goto clean2;
 640  640                  }
 641  641  
 642  642                  /* Cleanup memory space. */
 643  643                  free(soft_des_ctx->des_cbc);
 644      -                bzero(soft_des_ctx->key_sched, soft_des_ctx->keysched_len);
 645      -                free(soft_des_ctx->key_sched);
      644 +                freezero(soft_des_ctx->key_sched,
      645 +                    soft_des_ctx->keysched_len);
 646  646  
 647  647                  break;
 648  648          }
 649  649  
 650  650          case CKM_AES_CBC_PAD:
 651  651          {
 652  652  
 653  653                  soft_aes_ctx_t *soft_aes_ctx;
 654  654  
 655  655                  soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 656  656  
 657  657                  /*
 658  658                   * We should have only one block of data left in the
 659  659                   * remaining buffer.
 660  660                   */
 661  661                  if (soft_aes_ctx->remain_len != AES_BLOCK_LEN) {
 662  662                          *pulLastPartLen = 0;
 663  663                          rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 664  664                          /* Cleanup memory space. */
 665  665                          free(soft_aes_ctx->aes_cbc);
 666      -                        bzero(soft_aes_ctx->key_sched,
      666 +                        freezero(soft_aes_ctx->key_sched,
 667  667                              soft_aes_ctx->keysched_len);
 668      -                        free(soft_aes_ctx->key_sched);
 669  668  
 670  669                          goto clean1;
 671  670                  }
 672  671  
 673  672                  out_len = AES_BLOCK_LEN;
 674  673  
 675  674                  /*
 676  675                   * If application asks for the length of the output buffer
 677  676                   * to hold the plaintext?
 678  677                   */
↓ open down ↓ 31 lines elided ↑ open up ↑
 710  709                                          *pulLastPartLen = 0;
 711  710                                  else
 712  711                                          *pulLastPartLen = out_len;
 713  712                          } else {
 714  713                                  *pulLastPartLen = 0;
 715  714                                  rv = CKR_FUNCTION_FAILED;
 716  715                          }
 717  716  
 718  717                          /* Cleanup memory space. */
 719  718                          free(soft_aes_ctx->aes_cbc);
 720      -                        bzero(soft_aes_ctx->key_sched,
      719 +                        freezero(soft_aes_ctx->key_sched,
 721  720                              soft_aes_ctx->keysched_len);
 722      -                        free(soft_aes_ctx->key_sched);
 723  721  
 724  722                  }
 725  723  
 726  724                  break;
 727  725          }
 728  726  
 729  727          case CKM_AES_CBC:
 730  728          case CKM_AES_ECB:
 731  729          {
 732  730                  soft_aes_ctx_t *soft_aes_ctx;
↓ open down ↓ 7 lines elided ↑ open up ↑
 740  738                  *pulLastPartLen = 0;
 741  739                  if (soft_aes_ctx->remain_len != 0) {
 742  740                          rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 743  741                  } else {
 744  742                          if (pLastPart == NULL)
 745  743                                  goto clean2;
 746  744                  }
 747  745  
 748  746                  /* Cleanup memory space. */
 749  747                  free(soft_aes_ctx->aes_cbc);
 750      -                bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
 751      -                free(soft_aes_ctx->key_sched);
      748 +                freezero(soft_aes_ctx->key_sched,
      749 +                    soft_aes_ctx->keysched_len);
 752  750  
 753  751                  break;
 754  752          }
 755  753          case CKM_AES_CTR:
 756  754          {
 757  755                  crypto_data_t out;
 758  756                  soft_aes_ctx_t *soft_aes_ctx;
 759  757                  ctr_ctx_t *ctr_ctx;
 760  758                  size_t len;
 761  759  
↓ open down ↓ 15 lines elided ↑ open up ↑
 777  775                          if (rv == CRYPTO_DATA_LEN_RANGE)
 778  776                                  rv = CRYPTO_ENCRYPTED_DATA_LEN_RANGE;
 779  777                  }
 780  778                  if (rv == CRYPTO_BUFFER_TOO_SMALL) {
 781  779                          *pulLastPartLen = len;
 782  780                          goto clean1;
 783  781                  }
 784  782  
 785  783                  /* Cleanup memory space. */
 786  784                  free(ctr_ctx);
 787      -                bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
 788      -                free(soft_aes_ctx->key_sched);
      785 +                freezero(soft_aes_ctx->key_sched,
      786 +                    soft_aes_ctx->keysched_len);
 789  787  
 790  788                  break;
 791  789          }
 792  790          case CKM_BLOWFISH_CBC:
 793  791          {
 794  792                  soft_blowfish_ctx_t *soft_blowfish_ctx;
 795  793  
 796  794                  soft_blowfish_ctx =
 797  795                      (soft_blowfish_ctx_t *)session_p->decrypt.context;
 798  796  
 799  797                  *pulLastPartLen = 0;
 800  798                  if (soft_blowfish_ctx->remain_len != 0)
 801  799                          rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 802  800                  else {
 803  801                          if (pLastPart == NULL)
 804  802                                  goto clean2;
 805  803                  }
 806  804  
 807  805                  free(soft_blowfish_ctx->blowfish_cbc);
 808      -                bzero(soft_blowfish_ctx->key_sched,
      806 +                freezero(soft_blowfish_ctx->key_sched,
 809  807                      soft_blowfish_ctx->keysched_len);
 810      -                free(soft_blowfish_ctx->key_sched);
 811  808  
 812  809                  break;
 813  810          }
 814  811  
 815  812          case CKM_RC4:
 816  813          {
 817  814                  ARCFour_key *key = (ARCFour_key *)session_p->decrypt.context;
 818      -                bzero(key, sizeof (*key));
      815 +                explicit_bzero(key, sizeof (*key));
 819  816                  *pulLastPartLen = 0;
 820  817                  break;
 821  818          }
 822  819  
 823  820          default:
 824  821                  /* PKCS11: The mechanism only supports single-part operation. */
 825  822                  rv = CKR_MECHANISM_INVALID;
 826  823                  break;
 827  824          }
 828  825  
 829  826  clean1:
 830  827          free(session_p->decrypt.context);
 831  828          session_p->decrypt.context = NULL;
 832  829  
 833  830  clean2:
 834  831          (void) pthread_mutex_unlock(&session_p->session_mutex);
 835  832  
 836  833          return (rv);
 837  834  
 838  835  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX