Blame SOURCES/opencryptoki-openssl3-62fc2bcd98672c5d0ff8a2c926f3103110e91ed7.patch

2c1758
commit 62fc2bcd98672c5d0ff8a2c926f3103110e91ed7
2c1758
Author: Ingo Franzki <ifranzki@linux.ibm.com>
2c1758
Date:   Thu Jul 1 13:37:04 2021 +0200
2c1758
2c1758
    COMMON: Perform proper context cleanup for 3DES/AES CMAC mechanisms
2c1758
    
2c1758
    The handling of 3DES/AES CMAC mechanisms use a complex context structure,
2c1758
    that contains pointers. Such state can not be saved, and needs a custom
2c1758
    context free routine to properly clean up the context.
2c1758
    
2c1758
    Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
2c1758
2c1758
diff --git a/usr/lib/common/mech_aes.c b/usr/lib/common/mech_aes.c
2c1758
index ad6af16b..59f82482 100644
2c1758
--- a/usr/lib/common/mech_aes.c
2c1758
+++ b/usr/lib/common/mech_aes.c
2c1758
@@ -2691,6 +2691,24 @@ CK_RV aes_mac_verify_final(STDLL_TokData_t *tokdata,
2c1758
     return CKR_SIGNATURE_INVALID;
2c1758
 }
2c1758
 
2c1758
+static void aes_cmac_cleanup(STDLL_TokData_t *tokdata, SESSION *sess,
2c1758
+                             CK_BYTE *context, CK_ULONG context_len)
2c1758
+{
2c1758
+    UNUSED(tokdata);
2c1758
+    UNUSED(sess);
2c1758
+    UNUSED(context_len);
2c1758
+
2c1758
+    if (((AES_CMAC_CONTEXT *)context)->ctx != NULL) {
2c1758
+        token_specific.t_aes_cmac(tokdata, (CK_BYTE *)"", 0, NULL,
2c1758
+                                  ((AES_CMAC_CONTEXT *)context)->iv,
2c1758
+                                  CK_FALSE, CK_TRUE,
2c1758
+                                  ((AES_CMAC_CONTEXT *)context)->ctx);
2c1758
+        ((AES_CMAC_CONTEXT *)context)->ctx = NULL;
2c1758
+    }
2c1758
+
2c1758
+    free(context);
2c1758
+}
2c1758
+
2c1758
 CK_RV aes_cmac_sign(STDLL_TokData_t *tokdata,
2c1758
                     SESSION *sess,
2c1758
                     CK_BBOOL length_only,
2c1758
@@ -2743,6 +2761,8 @@ CK_RV aes_cmac_sign(STDLL_TokData_t *tokdata,
2c1758
     if (((AES_CMAC_CONTEXT *)ctx->context)->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = aes_cmac_cleanup;
2c1758
+
2c1758
     memcpy(out_data, ((AES_CMAC_CONTEXT *) ctx->context)->iv, mac_len);
2c1758
     *out_data_len = mac_len;
2c1758
 
2c1758
@@ -2816,6 +2836,8 @@ CK_RV aes_cmac_sign_update(STDLL_TokData_t *tokdata,
2c1758
 
2c1758
             if (context->ctx != NULL)
2c1758
                 ctx->state_unsaveable = CK_TRUE;
2c1758
+
2c1758
+            ctx->context_free_func = aes_cmac_cleanup;
2c1758
         } else {
2c1758
             TRACE_DEVEL("Token specific aes cmac failed.\n");
2c1758
         }
2c1758
@@ -2882,6 +2904,8 @@ CK_RV aes_cmac_sign_final(STDLL_TokData_t *tokdata,
2c1758
     if (context->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = aes_cmac_cleanup;
2c1758
+
2c1758
     memcpy(out_data, context->iv, mac_len);
2c1758
     *out_data_len = mac_len;
2c1758
 
2c1758
@@ -2941,6 +2965,8 @@ CK_RV aes_cmac_verify(STDLL_TokData_t *tokdata,
2c1758
     if (((AES_CMAC_CONTEXT *)ctx->context)->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = aes_cmac_cleanup;
2c1758
+
2c1758
     if (CRYPTO_memcmp(out_data, ((AES_CMAC_CONTEXT *) ctx->context)->iv,
2c1758
                       out_data_len) == 0) {
2c1758
         return CKR_OK;
2c1758
@@ -3012,6 +3038,8 @@ CK_RV aes_cmac_verify_update(STDLL_TokData_t *tokdata,
2c1758
 
2c1758
             if (context->ctx != NULL)
2c1758
                 ctx->state_unsaveable = CK_TRUE;
2c1758
+
2c1758
+            ctx->context_free_func = aes_cmac_cleanup;
2c1758
         } else {
2c1758
             TRACE_DEVEL("Token specific aes cmac failed.\n");
2c1758
         }
2c1758
@@ -3070,6 +3098,8 @@ CK_RV aes_cmac_verify_final(STDLL_TokData_t *tokdata,
2c1758
     if (context->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = aes_cmac_cleanup;
2c1758
+
2c1758
     if (rc != CKR_OK) {
2c1758
         TRACE_DEVEL("Token specific aes mac failed.\n");
2c1758
         return rc;
2c1758
diff --git a/usr/lib/common/mech_des3.c b/usr/lib/common/mech_des3.c
2c1758
index be8d6075..591ad3fa 100644
2c1758
--- a/usr/lib/common/mech_des3.c
2c1758
+++ b/usr/lib/common/mech_des3.c
2c1758
@@ -2334,6 +2334,24 @@ CK_RV des3_mac_verify_final(STDLL_TokData_t *tokdata,
2c1758
     return CKR_SIGNATURE_INVALID;
2c1758
 }
2c1758
 
2c1758
+static void des3_cmac_cleanup(STDLL_TokData_t *tokdata, SESSION *sess,
2c1758
+                              CK_BYTE *context, CK_ULONG context_len)
2c1758
+{
2c1758
+    UNUSED(tokdata);
2c1758
+    UNUSED(sess);
2c1758
+    UNUSED(context_len);
2c1758
+
2c1758
+    if (((DES_CMAC_CONTEXT *)context)->ctx != NULL) {
2c1758
+        token_specific.t_tdes_cmac(tokdata, (CK_BYTE *)"", 0, NULL,
2c1758
+                                   ((DES_CMAC_CONTEXT *)context)->iv,
2c1758
+                                   CK_FALSE, CK_TRUE,
2c1758
+                                   ((DES_CMAC_CONTEXT *)context)->ctx);
2c1758
+        ((DES_CMAC_CONTEXT *)context)->ctx = NULL;
2c1758
+    }
2c1758
+
2c1758
+    free(context);
2c1758
+}
2c1758
+
2c1758
 CK_RV des3_cmac_sign(STDLL_TokData_t *tokdata,
2c1758
                      SESSION *sess,
2c1758
                      CK_BBOOL length_only,
2c1758
@@ -2383,6 +2401,8 @@ CK_RV des3_cmac_sign(STDLL_TokData_t *tokdata,
2c1758
     if (((DES_CMAC_CONTEXT *)ctx->context)->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = des3_cmac_cleanup;
2c1758
+
2c1758
     memcpy(out_data, ((DES_CMAC_CONTEXT *) ctx->context)->iv, mac_len);
2c1758
 
2c1758
     *out_data_len = mac_len;
2c1758
@@ -2456,6 +2476,8 @@ CK_RV des3_cmac_sign_update(STDLL_TokData_t *tokdata,
2c1758
 
2c1758
             if (context->ctx != NULL)
2c1758
                 ctx->state_unsaveable = CK_TRUE;
2c1758
+
2c1758
+            ctx->context_free_func = des3_cmac_cleanup;
2c1758
         } else {
2c1758
             TRACE_DEVEL("Token specific des3 cmac failed.\n");
2c1758
         }
2c1758
@@ -2521,6 +2543,8 @@ CK_RV des3_cmac_sign_final(STDLL_TokData_t *tokdata,
2c1758
     if (context->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = des3_cmac_cleanup;
2c1758
+
2c1758
     memcpy(out_data, context->iv, mac_len);
2c1758
 
2c1758
     *out_data_len = mac_len;
2c1758
@@ -2577,6 +2601,8 @@ CK_RV des3_cmac_verify(STDLL_TokData_t *tokdata,
2c1758
     if (((DES_CMAC_CONTEXT *)ctx->context)->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = des3_cmac_cleanup;
2c1758
+
2c1758
     if (CRYPTO_memcmp(out_data, ((DES_CMAC_CONTEXT *) ctx->context)->iv,
2c1758
                       out_data_len) == 0) {
2c1758
         return CKR_OK;
2c1758
@@ -2646,6 +2672,8 @@ CK_RV des3_cmac_verify_update(STDLL_TokData_t *tokdata,
2c1758
 
2c1758
             if (context->ctx != NULL)
2c1758
                 ctx->state_unsaveable = CK_TRUE;
2c1758
+
2c1758
+            ctx->context_free_func = des3_cmac_cleanup;
2c1758
         } else {
2c1758
             TRACE_DEVEL("Token specific des3 cmac failed.\n");
2c1758
         }
2c1758
@@ -2709,6 +2737,8 @@ CK_RV des3_cmac_verify_final(STDLL_TokData_t *tokdata,
2c1758
     if (context->ctx != NULL)
2c1758
         ctx->state_unsaveable = CK_TRUE;
2c1758
 
2c1758
+    ctx->context_free_func = des3_cmac_cleanup;
2c1758
+
2c1758
     if (CRYPTO_memcmp(signature, context->iv, signature_len) == 0)
2c1758
         return CKR_OK;
2c1758
 
2c1758
diff --git a/usr/lib/ica_s390_stdll/ica_specific.c b/usr/lib/ica_s390_stdll/ica_specific.c
2c1758
index 77876467..881a430c 100644
2c1758
--- a/usr/lib/ica_s390_stdll/ica_specific.c
2c1758
+++ b/usr/lib/ica_s390_stdll/ica_specific.c
2c1758
@@ -713,6 +713,9 @@ CK_RV token_specific_tdes_cmac(STDLL_TokData_t *tokdata, CK_BYTE *message,
2c1758
     UNUSED(tokdata);
2c1758
     UNUSED(ctx);
2c1758
 
2c1758
+    if (key == NULL)
2c1758
+        return CKR_ARGUMENTS_BAD;
2c1758
+
2c1758
     // get the key type
2c1758
     rc = template_attribute_get_ulong(key->template, CKA_KEY_TYPE, &keytype);
2c1758
     if (rc != CKR_OK) {
2c1758
@@ -3621,6 +3624,9 @@ CK_RV token_specific_aes_cmac(STDLL_TokData_t *tokdata, CK_BYTE *message,
2c1758
     UNUSED(tokdata);
2c1758
     UNUSED(ctx);
2c1758
 
2c1758
+    if (key == NULL)
2c1758
+        return CKR_ARGUMENTS_BAD;
2c1758
+
2c1758
     rc = template_attribute_get_non_empty(key->template, CKA_VALUE, &attr);
2c1758
     if (rc != CKR_OK) {
2c1758
         TRACE_ERROR("Could not find CKA_VALUE for the key.\n");
2c1758
diff --git a/usr/lib/soft_stdll/soft_specific.c b/usr/lib/soft_stdll/soft_specific.c
2c1758
index aeff39a9..5ca22693 100644
2c1758
--- a/usr/lib/soft_stdll/soft_specific.c
2c1758
+++ b/usr/lib/soft_stdll/soft_specific.c
2c1758
@@ -3994,6 +3994,9 @@ CK_RV token_specific_tdes_cmac(STDLL_TokData_t *tokdata, CK_BYTE *message,
2c1758
     UNUSED(tokdata);
2c1758
 
2c1758
     if (first) {
2c1758
+        if (key == NULL)
2c1758
+            return CKR_ARGUMENTS_BAD;
2c1758
+
2c1758
         // get the key type
2c1758
         rv = template_attribute_get_ulong(key->template, CKA_KEY_TYPE, &keytype);
2c1758
         if (rv != CKR_OK) {
2c1758
@@ -4194,6 +4197,9 @@ CK_RV token_specific_aes_cmac(STDLL_TokData_t *tokdata, CK_BYTE *message,
2c1758
     UNUSED(tokdata);
2c1758
 
2c1758
     if (first) {
2c1758
+        if (key == NULL)
2c1758
+            return CKR_ARGUMENTS_BAD;
2c1758
+
2c1758
         // get the key value
2c1758
         rc = template_attribute_get_non_empty(key->template, CKA_VALUE, &attr);
2c1758
         if (rc != CKR_OK) {