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/softAESCrypt.c
          +++ new/usr/src/lib/pkcs11/pkcs11_softtoken/common/softAESCrypt.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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
       25 + * Copyright (c) 2018, Joyent, Inc.
  25   26   */
  26   27  
  27   28  #include <pthread.h>
  28   29  #include <stdlib.h>
  29   30  #include <string.h>
  30   31  #include <strings.h>
  31   32  #include <sys/types.h>
  32   33  #include <security/cryptoki.h>
  33   34  #include <aes_impl.h>
  34   35  #include "softSession.h"
↓ open down ↓ 446 lines elided ↑ open up ↑
 481  482                  return (CKR_OK);
 482  483  
 483  484          /*
 484  485           * The following code will be executed if the caller is
 485  486           * soft_encrypt() or an error occurred. The encryption
 486  487           * operation will be terminated so we need to do some cleanup.
 487  488           */
 488  489  cleanup:
 489  490          (void) pthread_mutex_lock(&session_p->session_mutex);
 490  491          aes_ctx = (aes_ctx_t *)soft_aes_ctx->aes_cbc;
 491      -        if (aes_ctx != NULL) {
 492      -                bzero(aes_ctx->ac_keysched, aes_ctx->ac_keysched_len);
 493      -                free(soft_aes_ctx->aes_cbc);
      492 +        switch (mechanism) {
      493 +        case CKM_AES_ECB:
      494 +                freezero(aes_ctx, sizeof (ecb_ctx_t));
      495 +                break;
      496 +        case CKM_AES_CMAC:
      497 +        case CKM_AES_CBC:
      498 +        case CKM_AES_CBC_PAD:
      499 +                freezero(aes_ctx, sizeof (cbc_ctx_t));
      500 +                break;
      501 +        case CKM_AES_CTR:
      502 +                freezero(aes_ctx, sizeof (ctr_ctx_t));
      503 +                break;
 494  504          }
 495      -
 496      -        bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
 497      -        free(soft_aes_ctx->key_sched);
 498      -        free(session_p->encrypt.context);
      505 +        freezero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
      506 +        freezero(session_p->encrypt.context, sizeof (soft_aes_ctx_t));
 499  507          session_p->encrypt.context = NULL;
 500  508          (void) pthread_mutex_unlock(&session_p->session_mutex);
 501  509  
 502  510          return (rv);
 503  511  }
 504  512  
 505  513  
 506  514  /*
 507  515   * soft_aes_decrypt_common()
 508  516   *
↓ open down ↓ 335 lines elided ↑ open up ↑
 844  852                  return (CKR_OK);
 845  853  
 846  854          /*
 847  855           * The following code will be executed if the caller is
 848  856           * soft_decrypt() or an error occurred. The decryption
 849  857           * operation will be terminated so we need to do some cleanup.
 850  858           */
 851  859  cleanup:
 852  860          (void) pthread_mutex_lock(&session_p->session_mutex);
 853  861          aes_ctx = (aes_ctx_t *)soft_aes_ctx->aes_cbc;
 854      -        if (aes_ctx != NULL) {
 855      -                bzero(aes_ctx->ac_keysched, aes_ctx->ac_keysched_len);
 856      -                free(soft_aes_ctx->aes_cbc);
 857      -        }
 858      -
 859      -        bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
 860      -        free(soft_aes_ctx->key_sched);
 861      -        free(session_p->decrypt.context);
      862 +        free(aes_ctx);
      863 +        freezero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
      864 +        freezero(session_p->decrypt.context, sizeof (soft_aes_ctx_t));
 862  865          session_p->decrypt.context = NULL;
 863  866          (void) pthread_mutex_unlock(&session_p->session_mutex);
 864  867  
 865  868          return (rv);
 866  869  }
 867  870  
 868  871  
 869  872  /*
 870  873   * Allocate and initialize a context for AES CBC mode of operation.
 871  874   */
↓ open down ↓ 246 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX