Blame SOURCES/0001-EP11-Unify-key-pair-generation-functions.patch

253609
From 27088567f4375578e39c5b75b4ceae9dff231962 Mon Sep 17 00:00:00 2001
253609
From: Ingo Franzki <ifranzki@linux.ibm.com>
253609
Date: Tue, 11 Oct 2022 13:46:08 +0200
253609
Subject: [PATCH 01/34] EP11: Unify key-pair generation functions
253609
253609
Unify the DSA and DH key-pair generation functions with those for
253609
RSA, EC and Dilithium. Make sure that the attribute handling is done
253609
in the same sequence for all those functions.
253609
253609
Also remove obsolete parameters for all the key-pair generation functions.
253609
253609
Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
253609
---
253609
 usr/lib/ep11_stdll/ep11_specific.c | 494 ++++++++++++++-----------------------
253609
 1 file changed, 187 insertions(+), 307 deletions(-)
253609
253609
diff --git a/usr/lib/ep11_stdll/ep11_specific.c b/usr/lib/ep11_stdll/ep11_specific.c
253609
index a6a33719..5d7c5607 100644
253609
--- a/usr/lib/ep11_stdll/ep11_specific.c
253609
+++ b/usr/lib/ep11_stdll/ep11_specific.c
253609
@@ -5374,15 +5374,10 @@ error:
253609
 
253609
 
253609
 
253609
-static CK_RV dh_generate_keypair(STDLL_TokData_t * tokdata,
253609
-                                 SESSION * sess,
253609
+static CK_RV dh_generate_keypair(STDLL_TokData_t *tokdata,
253609
+                                 SESSION *sess,
253609
                                  CK_MECHANISM_PTR pMechanism,
253609
-                                 TEMPLATE * publ_tmpl, TEMPLATE * priv_tmpl,
253609
-                                 CK_ATTRIBUTE_PTR pPublicKeyTemplate,
253609
-                                 CK_ULONG ulPublicKeyAttributeCount,
253609
-                                 CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
253609
-                                 CK_ULONG ulPrivateKeyAttributeCount,
253609
-                                 CK_SESSION_HANDLE h)
253609
+                                 TEMPLATE *publ_tmpl, TEMPLATE *priv_tmpl)
253609
 {
253609
     CK_RV rc;
253609
     CK_BYTE publblob[MAX_BLOBSIZE];
253609
@@ -5399,9 +5394,6 @@ static CK_RV dh_generate_keypair(STDLL_TokData_t * tokdata,
253609
     CK_ULONG dh_ulPublicKeyAttributeCount = 0;
253609
     CK_ATTRIBUTE_PTR dh_pPrivateKeyTemplate = NULL;
253609
     CK_ULONG dh_ulPrivateKeyAttributeCount = 0;
253609
-    size_t p_len = 0, g_len = 0;
253609
-    int new_public_attr;
253609
-    CK_ULONG i;
253609
     CK_ULONG data_len;
253609
     CK_ULONG field_len;
253609
     CK_BYTE *data;
253609
@@ -5421,149 +5413,122 @@ static CK_RV dh_generate_keypair(STDLL_TokData_t * tokdata,
253609
         unsigned char *pg;
253609
     } dh_pgs;
253609
 
253609
-    UNUSED(h);
253609
-
253609
     memset(&dh_pgs, 0, sizeof(dh_pgs));
253609
     memset(publblob, 0, sizeof(publblob));
253609
     memset(privblob, 0, sizeof(privblob));
253609
 
253609
-    /* card does not want CKA_PRIME/CKA_BASE in template but in dh_pgs */
253609
-    pPublicKeyTemplate_new =
253609
-        (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) *
253609
-                                ulPublicKeyAttributeCount);
253609
-    if (!pPublicKeyTemplate_new) {
253609
-        TRACE_ERROR("%s Memory allocation failed\n", __func__);
253609
-        return CKR_HOST_MEMORY;
253609
-    }
253609
-    memset(pPublicKeyTemplate_new, 0,
253609
-           sizeof(CK_ATTRIBUTE) * ulPublicKeyAttributeCount);
253609
-
253609
-    for (i = 0, new_public_attr = 0; i < ulPublicKeyAttributeCount; i++) {
253609
-        /* filter out CKA_PRIME/CKA_BASE,
253609
-         * but remember where they can  be found
253609
-         */
253609
-        switch (pPublicKeyTemplate[i].type) {
253609
-        case CKA_PRIME:
253609
-            prime_attr = &(pPublicKeyTemplate[i]);
253609
-            p_len = pPublicKeyTemplate[i].ulValueLen;
253609
-            break;
253609
-        case CKA_BASE:
253609
-            base_attr = &(pPublicKeyTemplate[i]);
253609
-            g_len = pPublicKeyTemplate[i].ulValueLen;
253609
-            break;
253609
-        default:
253609
-            /* copy all other attributes */
253609
-            memcpy(&pPublicKeyTemplate_new[new_public_attr],
253609
-                   &(pPublicKeyTemplate[i]), sizeof(CK_ATTRIBUTE));
253609
-            new_public_attr++;
253609
-        }
253609
+    rc = build_ep11_attrs(tokdata, publ_tmpl, &dh_pPublicKeyTemplate,
253609
+                          &dh_ulPublicKeyAttributeCount,
253609
+                          CKK_DH, CKO_PUBLIC_KEY, -1, pMechanism);
253609
+    if (rc != CKR_OK) {
253609
+        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
+        goto dh_generate_keypair_end;
253609
     }
253609
 
253609
-    if (prime_attr == NULL || base_attr == NULL) {
253609
-        TRACE_ERROR("%s Incomplete template prime_attr=%p base_attr=%p\n",
253609
-                    __func__, (void *)prime_attr, (void *)base_attr);
253609
-        rc = CKR_TEMPLATE_INCOMPLETE;
253609
+    rc = build_ep11_attrs(tokdata, priv_tmpl, &dh_pPrivateKeyTemplate,
253609
+                          &dh_ulPrivateKeyAttributeCount,
253609
+                          CKK_DH, CKO_PRIVATE_KEY, -1, pMechanism);
253609
+    if (rc != CKR_OK) {
253609
+        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
 
253609
-    /* copy CKA_PRIME/CKA_BASE to private template */
253609
-    rc = build_attribute(CKA_PRIME, prime_attr->pValue,
253609
-                         prime_attr->ulValueLen, &attr);
253609
+    rc = check_key_attributes(tokdata, CKK_DH, CKO_PUBLIC_KEY,
253609
+                              dh_pPublicKeyTemplate,
253609
+                              dh_ulPublicKeyAttributeCount,
253609
+                              &new_publ_attrs, &new_publ_attrs_len, -1);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s DH check public key attributes failed with "
253609
+                    "rc=0x%lx\n", __func__, rc);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
-    rc = template_update_attribute(priv_tmpl, attr);
253609
+
253609
+    rc = check_key_attributes(tokdata, CKK_DH, CKO_PRIVATE_KEY,
253609
+                              dh_pPrivateKeyTemplate,
253609
+                              dh_ulPrivateKeyAttributeCount,
253609
+                              &new_priv_attrs, &new_priv_attrs_len, -1);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
-                    __func__, rc);
253609
-        free(attr);
253609
+        TRACE_ERROR("%s DH check private key attributes failed with "
253609
+                    "rc=0x%lx\n", __func__, rc);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
-    rc = build_attribute(CKA_BASE, base_attr->pValue,
253609
-                         base_attr->ulValueLen, &attr);
253609
+
253609
+    /* card does not want CKA_PRIME/CKA_BASE in template but in dh_pgs */
253609
+    rc = template_attribute_get_non_empty(publ_tmpl, CKA_PRIME,
253609
+                                          &prime_attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s DH No CKA_PRIME attribute found\n", __func__);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
-    rc = template_update_attribute(priv_tmpl, attr);
253609
+
253609
+    rc = template_attribute_get_non_empty(publ_tmpl, CKA_BASE,
253609
+                                          &base_attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
-                    __func__, rc);
253609
-        free(attr);
253609
+        TRACE_ERROR("%s DH No CKA_BASE attribute found\n", __func__);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
 
253609
-    /* copy CKA_PRIME/CKA_BASE values */
253609
-    dh_pgs.pg = malloc(p_len * 2);
253609
+    dh_pgs.pg = malloc(prime_attr->ulValueLen * 2);
253609
     if (!dh_pgs.pg) {
253609
         TRACE_ERROR("%s Memory allocation failed\n", __func__);
253609
         rc = CKR_HOST_MEMORY;
253609
         goto dh_generate_keypair_end;
253609
     }
253609
-    memset(dh_pgs.pg, 0, p_len * 2);
253609
-    memcpy(dh_pgs.pg, prime_attr->pValue, p_len);     /* copy CKA_PRIME value */
253609
+
253609
+    memset(dh_pgs.pg, 0, prime_attr->ulValueLen * 2);
253609
+    /* copy CKA_PRIME value */
253609
+    memcpy(dh_pgs.pg, prime_attr->pValue, prime_attr->ulValueLen);
253609
     /* copy CKA_BASE value, it must have leading zeros
253609
      * if it is shorter than CKA_PRIME
253609
      */
253609
-    memcpy(dh_pgs.pg + p_len + (p_len - g_len), base_attr->pValue, g_len);
253609
-    dh_pgs.pg_bytes = p_len * 2;
253609
+    memcpy(dh_pgs.pg + prime_attr->ulValueLen +
253609
+                        (prime_attr->ulValueLen - base_attr->ulValueLen),
253609
+           base_attr->pValue, base_attr->ulValueLen);
253609
+    dh_pgs.pg_bytes = prime_attr->ulValueLen * 2;
253609
 
253609
 #ifdef DEBUG
253609
     TRACE_DEBUG("%s P:\n", __func__);
253609
-    TRACE_DEBUG_DUMP("    ", &dh_pgs.pg[0], p_len);
253609
+    TRACE_DEBUG_DUMP("    ", &dh_pgs.pg[0], prime_attr->ulValueLen);
253609
     TRACE_DEBUG("%s G:\n", __func__);
253609
-    TRACE_DEBUG_DUMP("    ", &dh_pgs.pg[p_len], p_len);
253609
+    TRACE_DEBUG_DUMP("    ", &dh_pgs.pg[prime_attr->ulValueLen],
253609
+                     prime_attr->ulValueLen);
253609
 #endif
253609
 
253609
-    /* add special attribute, do not add it to ock's pPublicKeyTemplate */
253609
-    CK_ATTRIBUTE pgs[] = { {CKA_IBM_STRUCT_PARAMS, (CK_VOID_PTR) dh_pgs.pg,
253609
-                            dh_pgs.pg_bytes}
253609
-    };
253609
-    memcpy(&(pPublicKeyTemplate_new[new_public_attr]),
253609
-           &(pgs[0]), sizeof(CK_ATTRIBUTE));
253609
-
253609
-    rc = check_key_attributes(tokdata, CKK_DH, CKO_PUBLIC_KEY,
253609
-                              pPublicKeyTemplate_new, new_public_attr + 1,
253609
-                              &dh_pPublicKeyTemplate,
253609
-                              &dh_ulPublicKeyAttributeCount, -1);
253609
+    rc = add_to_attribute_array(&new_publ_attrs, &new_publ_attrs_len,
253609
+                                CKA_IBM_STRUCT_PARAMS, dh_pgs.pg,
253609
+                                dh_pgs.pg_bytes);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s DH check public key attributes failed with "
253609
-                    "rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s add_to_attribute_array failed with rc=0x%lx\n",
253609
+                    __func__, rc);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = check_key_attributes(tokdata, CKK_DH, CKO_PRIVATE_KEY,
253609
-                              pPrivateKeyTemplate, ulPrivateKeyAttributeCount,
253609
-                              &dh_pPrivateKeyTemplate,
253609
-                              &dh_ulPrivateKeyAttributeCount, -1);
253609
+    /* copy CKA_PRIME/CKA_BASE to private template */
253609
+    rc = build_attribute(CKA_PRIME, prime_attr->pValue,
253609
+                         prime_attr->ulValueLen, &attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s DH check private key attributes failed with "
253609
-                    "rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
-
253609
-    rc = build_ep11_attrs(tokdata, publ_tmpl,
253609
-                          &new_publ_attrs, &new_publ_attrs_len,
253609
-                          CKK_DH, CKO_PUBLIC_KEY, -1, pMechanism);
253609
+    rc = template_update_attribute(priv_tmpl, attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
+                    __func__, rc);
253609
+        free(attr);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = add_to_attribute_array(&new_publ_attrs, &new_publ_attrs_len,
253609
-                           CKA_IBM_STRUCT_PARAMS, (CK_VOID_PTR) dh_pgs.pg,
253609
-                           dh_pgs.pg_bytes);
253609
+    rc = build_attribute(CKA_BASE, base_attr->pValue,
253609
+                         base_attr->ulValueLen, &attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s add_to_attribute_array failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
-
253609
-    rc = build_ep11_attrs(tokdata, priv_tmpl,
253609
-                          &new_priv_attrs, &new_priv_attrs_len,
253609
-                          CKK_DH, CKO_PRIVATE_KEY, -1, pMechanism);
253609
+    rc = template_update_attribute(priv_tmpl, attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
+                    __func__, rc);
253609
+        free(attr);
253609
         goto dh_generate_keypair_end;
253609
     }
253609
 
253609
@@ -5573,10 +5538,10 @@ static CK_RV dh_generate_keypair(STDLL_TokData_t * tokdata,
253609
                      new_priv_attrs, new_priv_attrs_len);
253609
 
253609
     ep11_get_pin_blob(ep11_session,
253609
-                      (ep11_is_session_object
253609
-                       (pPublicKeyTemplate, ulPublicKeyAttributeCount)
253609
-                       || ep11_is_session_object(pPrivateKeyTemplate,
253609
-                                                 ulPrivateKeyAttributeCount)),
253609
+                      (ep11_is_session_object(new_publ_attrs,
253609
+                                              new_publ_attrs_len) ||
253609
+                       ep11_is_session_object(new_priv_attrs,
253609
+                                              new_priv_attrs_len)),
253609
                       &ep11_pin_blob, &ep11_pin_blob_len);
253609
 
253609
     RETRY_START(rc, tokdata)
253609
@@ -5595,7 +5560,7 @@ static CK_RV dh_generate_keypair(STDLL_TokData_t * tokdata,
253609
     }
253609
 
253609
     TRACE_INFO("%s rc=0x%lx plen=%zd publblobsize=0x%zx privblobsize=0x%zx\n",
253609
-               __func__, rc, p_len, publblobsize, privblobsize);
253609
+               __func__, rc, prime_attr->ulValueLen, publblobsize, privblobsize);
253609
 
253609
     if (check_expected_mkvp(tokdata, privblob, privblobsize) != CKR_OK) {
253609
         TRACE_ERROR("%s\n", ock_err(ERR_DEVICE_ERROR));
253609
@@ -5700,15 +5665,10 @@ dh_generate_keypair_end:
253609
     return rc;
253609
 }
253609
 
253609
-static CK_RV dsa_generate_keypair(STDLL_TokData_t * tokdata,
253609
-                                  SESSION * sess,
253609
+static CK_RV dsa_generate_keypair(STDLL_TokData_t *tokdata,
253609
+                                  SESSION *sess,
253609
                                   CK_MECHANISM_PTR pMechanism,
253609
-                                  TEMPLATE * publ_tmpl, TEMPLATE * priv_tmpl,
253609
-                                  CK_ATTRIBUTE_PTR pPublicKeyTemplate,
253609
-                                  CK_ULONG ulPublicKeyAttributeCount,
253609
-                                  CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
253609
-                                  CK_ULONG ulPrivateKeyAttributeCount,
253609
-                                  CK_SESSION_HANDLE h)
253609
+                                  TEMPLATE *publ_tmpl, TEMPLATE *priv_tmpl)
253609
 {
253609
     CK_RV rc;
253609
     CK_BYTE publblob[MAX_BLOBSIZE];
253609
@@ -5721,9 +5681,6 @@ static CK_RV dsa_generate_keypair(STDLL_TokData_t * tokdata,
253609
     CK_ATTRIBUTE *opaque_attr = NULL;
253609
     CK_ATTRIBUTE *value_attr = NULL;
253609
     CK_ATTRIBUTE *attr = NULL;
253609
-    size_t p_len = 0, q_len = 0, g_len = 0;
253609
-    int new_public_attr;
253609
-    CK_ULONG i;
253609
     CK_ATTRIBUTE *pPublicKeyTemplate_new = NULL;
253609
     CK_BYTE *key;
253609
     CK_BYTE *data, *oid, *parm;
253609
@@ -5737,8 +5694,6 @@ static CK_RV dsa_generate_keypair(STDLL_TokData_t * tokdata,
253609
     ep11_session_t *ep11_session = (ep11_session_t *) sess->private_data;
253609
     CK_ATTRIBUTE *new_publ_attrs = NULL, *new_priv_attrs = NULL;
253609
     CK_ULONG new_publ_attrs_len = 0, new_priv_attrs_len = 0;
253609
-    CK_ATTRIBUTE *new_publ_attrs2 = NULL, *new_priv_attrs2 = NULL;
253609
-    CK_ULONG new_publ_attrs2_len = 0, new_priv_attrs2_len = 0;
253609
 
253609
     /* ep11 accepts CKA_PRIME,CKA_SUBPRIME,CKA_BASE only in this format */
253609
     struct {
253609
@@ -5746,95 +5701,68 @@ static CK_RV dsa_generate_keypair(STDLL_TokData_t * tokdata,
253609
         unsigned char *pqg;
253609
     } dsa_pqgs;
253609
 
253609
-    UNUSED(h);
253609
-
253609
     memset(&dsa_pqgs, 0, sizeof(dsa_pqgs));
253609
     memset(publblob, 0, sizeof(publblob));
253609
     memset(privblob, 0, sizeof(privblob));
253609
 
253609
-    /* card does not want CKA_PRIME/CKA_BASE/CKA_SUBPRIME
253609
-     * in template but in dsa_pqgs
253609
-     */
253609
-    pPublicKeyTemplate_new =
253609
-        (CK_ATTRIBUTE *) malloc(sizeof(CK_ATTRIBUTE) *
253609
-                                ulPublicKeyAttributeCount);
253609
-    if (!pPublicKeyTemplate_new) {
253609
-        TRACE_ERROR("%s Memory allocation failed\n", __func__);
253609
-        return CKR_HOST_MEMORY;
253609
-    }
253609
-    memset(pPublicKeyTemplate_new, 0,
253609
-           sizeof(CK_ATTRIBUTE) * ulPublicKeyAttributeCount);
253609
-
253609
-    for (i = 0, new_public_attr = 0; i < ulPublicKeyAttributeCount; i++) {
253609
-        switch (pPublicKeyTemplate[i].type) {
253609
-        case CKA_PRIME:
253609
-            prime_attr = &(pPublicKeyTemplate[i]);
253609
-            p_len = pPublicKeyTemplate[i].ulValueLen;
253609
-            break;
253609
-        case CKA_SUBPRIME:
253609
-            sub_prime_attr = &(pPublicKeyTemplate[i]);
253609
-            q_len = pPublicKeyTemplate[i].ulValueLen;
253609
-            break;
253609
-        case CKA_BASE:
253609
-            base_attr = &(pPublicKeyTemplate[i]);
253609
-            g_len = pPublicKeyTemplate[i].ulValueLen;
253609
-            break;
253609
-        default:
253609
-            /* copy all other attributes */
253609
-            memcpy(&pPublicKeyTemplate_new[new_public_attr],
253609
-                   &(pPublicKeyTemplate[i]), sizeof(CK_ATTRIBUTE));
253609
-            new_public_attr++;
253609
-        }
253609
-    }
253609
-
253609
-    if (prime_attr == NULL || sub_prime_attr == NULL || base_attr == NULL) {
253609
-        rc = CKR_TEMPLATE_INCOMPLETE;
253609
+    rc = build_ep11_attrs(tokdata, publ_tmpl, &dsa_pPublicKeyTemplate,
253609
+                          &dsa_ulPublicKeyAttributeCount,
253609
+                          CKK_DSA, CKO_PUBLIC_KEY, -1, pMechanism);
253609
+    if (rc != CKR_OK) {
253609
+        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    /* copy CKA_PRIME/CKA_BASE/CKA_SUBPRIME to private template */
253609
-    rc = build_attribute(CKA_PRIME, prime_attr->pValue,
253609
-                         prime_attr->ulValueLen, &attr);
253609
+    rc = build_ep11_attrs(tokdata, priv_tmpl, &dsa_pPrivateKeyTemplate,
253609
+                          &dsa_ulPrivateKeyAttributeCount,
253609
+                          CKK_DSA, CKO_PRIVATE_KEY, -1, pMechanism);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = template_update_attribute(priv_tmpl, attr);
253609
+    rc = check_key_attributes(tokdata, CKK_DSA, CKO_PUBLIC_KEY,
253609
+                              dsa_pPublicKeyTemplate,
253609
+                              dsa_ulPublicKeyAttributeCount,
253609
+                              &new_publ_attrs, &new_publ_attrs_len, -1);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
-                    __func__, rc);
253609
-        free(attr);
253609
+        TRACE_ERROR("%s DSA check public key attributes failed with "
253609
+                    "rc=0x%lx\n", __func__, rc);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = build_attribute(CKA_BASE, base_attr->pValue,
253609
-                         base_attr->ulValueLen, &attr);
253609
+    rc = check_key_attributes(tokdata, CKK_DSA, CKO_PRIVATE_KEY,
253609
+                              dsa_pPrivateKeyTemplate,
253609
+                              dsa_ulPrivateKeyAttributeCount,
253609
+                              &new_priv_attrs, &new_priv_attrs_len, -1);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s DSA check private key attributes failed with "
253609
+                    "rc=0x%lx\n", __func__, rc);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = template_update_attribute(priv_tmpl, attr);
253609
+    /*
253609
+     * card does not want CKA_PRIME/CKA_BASE/CKA_SUBPRIME in template but in
253609
+     * dsa_pqgs
253609
+     */
253609
+    rc = template_attribute_get_non_empty(publ_tmpl, CKA_PRIME,
253609
+                                          &prime_attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
-                    __func__, rc);
253609
-        free(attr);
253609
+        TRACE_ERROR("%s DSA No CKA_PRIME attribute found\n", __func__);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = build_attribute(CKA_SUBPRIME, sub_prime_attr->pValue,
253609
-                         sub_prime_attr->ulValueLen, &attr);
253609
+    rc = template_attribute_get_non_empty(publ_tmpl, CKA_SUBPRIME,
253609
+                                          &sub_prime_attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s DSA No CKA_SUBPRIME attribute found\n", __func__);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = template_update_attribute(priv_tmpl, attr);
253609
+    rc = template_attribute_get_non_empty(publ_tmpl, CKA_BASE,
253609
+                                          &base_attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
-                    __func__, rc);
253609
-        free(attr);
253609
+        TRACE_ERROR("%s DSA No CKA_BASE attribute found\n", __func__);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
@@ -5842,95 +5770,102 @@ static CK_RV dsa_generate_keypair(STDLL_TokData_t * tokdata,
253609
      * then they are extented by leading zeros till they have
253609
      * the size of CKA_PRIME
253609
      */
253609
-    dsa_pqgs.pqg = malloc(p_len * 3);
253609
+    dsa_pqgs.pqg = malloc(prime_attr->ulValueLen * 3);
253609
     if (!dsa_pqgs.pqg) {
253609
         TRACE_ERROR("%s Memory allocation failed\n", __func__);
253609
         rc = CKR_HOST_MEMORY;
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
-    memset(dsa_pqgs.pqg, 0, p_len * 3);
253609
-    memcpy(dsa_pqgs.pqg, prime_attr->pValue, p_len);
253609
-    memcpy(dsa_pqgs.pqg + p_len + (p_len - q_len),
253609
-           sub_prime_attr->pValue, q_len);
253609
-    memcpy(dsa_pqgs.pqg + 2 * p_len + (p_len - g_len),
253609
-           base_attr->pValue, g_len);
253609
-    dsa_pqgs.pqg_bytes = p_len * 3;
253609
+
253609
+    memset(dsa_pqgs.pqg, 0, prime_attr->ulValueLen * 3);
253609
+    memcpy(dsa_pqgs.pqg, prime_attr->pValue, prime_attr->ulValueLen);
253609
+    memcpy(dsa_pqgs.pqg + prime_attr->ulValueLen +
253609
+                         (prime_attr->ulValueLen - sub_prime_attr->ulValueLen),
253609
+           sub_prime_attr->pValue, sub_prime_attr->ulValueLen);
253609
+    memcpy(dsa_pqgs.pqg + 2 * prime_attr->ulValueLen +
253609
+                         (prime_attr->ulValueLen - base_attr->ulValueLen),
253609
+           base_attr->pValue, base_attr->ulValueLen);
253609
+    dsa_pqgs.pqg_bytes = prime_attr->ulValueLen * 3;
253609
 
253609
 #ifdef DEBUG
253609
     TRACE_DEBUG("%s P:\n", __func__);
253609
-    TRACE_DEBUG_DUMP("    ", &dsa_pqgs.pqg[0], p_len);
253609
+    TRACE_DEBUG_DUMP("    ", &dsa_pqgs.pqg[0], prime_attr->ulValueLen);
253609
     TRACE_DEBUG("%s Q:\n", __func__);
253609
-    TRACE_DEBUG_DUMP("    ", &dsa_pqgs.pqg[p_len], p_len);
253609
+    TRACE_DEBUG_DUMP("    ", &dsa_pqgs.pqg[prime_attr->ulValueLen],
253609
+                     prime_attr->ulValueLen);
253609
     TRACE_DEBUG("%s G:\n", __func__);
253609
-    TRACE_DEBUG_DUMP("    ", &dsa_pqgs.pqg[2 * p_len], p_len);
253609
+    TRACE_DEBUG_DUMP("    ", &dsa_pqgs.pqg[2 * prime_attr->ulValueLen],
253609
+                     prime_attr->ulValueLen);
253609
 #endif
253609
 
253609
-    CK_ATTRIBUTE pqgs[] = { {CKA_IBM_STRUCT_PARAMS,
253609
-                             (CK_VOID_PTR) dsa_pqgs.pqg, dsa_pqgs.pqg_bytes}
253609
-    };
253609
-
253609
-    /* add special attribute, do not add it to ock's pPublicKeyTemplate */
253609
-    memcpy(&(pPublicKeyTemplate_new[new_public_attr]),
253609
-           &(pqgs[0]), sizeof(CK_ATTRIBUTE));
253609
-
253609
-    rc = build_ep11_attrs(tokdata, publ_tmpl,
253609
-                          &new_publ_attrs, &new_publ_attrs_len,
253609
-                          CKK_DSA, CKO_PUBLIC_KEY, -1, pMechanism);
253609
+    rc = add_to_attribute_array(&new_publ_attrs, &new_publ_attrs_len,
253609
+                                CKA_IBM_STRUCT_PARAMS, dsa_pqgs.pqg,
253609
+                                dsa_pqgs.pqg_bytes);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s add_to_attribute_array failed with rc=0x%lx\n",
253609
+                    __func__, rc);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = check_key_attributes(tokdata, CKK_DSA, CKO_PUBLIC_KEY,
253609
-                              new_publ_attrs, new_publ_attrs_len,
253609
-                              &new_publ_attrs2, &new_publ_attrs2_len, -1);
253609
+    /* copy CKA_PRIME/CKA_BASE/CKA_SUBPRIME to private template */
253609
+    rc = build_attribute(CKA_PRIME, prime_attr->pValue,
253609
+                         prime_attr->ulValueLen, &attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s DSA check public key attributes failed with "
253609
-                    "rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
-
253609
-    rc = add_to_attribute_array(&new_publ_attrs2, &new_publ_attrs2_len,
253609
-                           CKA_IBM_STRUCT_PARAMS, (CK_VOID_PTR) dsa_pqgs.pqg,
253609
-                           dsa_pqgs.pqg_bytes);
253609
+    rc = template_update_attribute(priv_tmpl, attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s add_to_attribute_array failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
+                    __func__, rc);
253609
+        free(attr);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = build_ep11_attrs(tokdata, priv_tmpl,
253609
-                          &new_priv_attrs, &new_priv_attrs_len,
253609
-                          CKK_DSA, CKO_PRIVATE_KEY, -1, pMechanism);
253609
+    rc = build_attribute(CKA_SUBPRIME, sub_prime_attr->pValue,
253609
+                         sub_prime_attr->ulValueLen, &attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s build_ep11_attrs failed with rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
+        goto dsa_generate_keypair_end;
253609
+    }
253609
+    rc = template_update_attribute(priv_tmpl, attr);
253609
+    if (rc != CKR_OK) {
253609
+        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
+                    __func__, rc);
253609
+        free(attr);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    rc = check_key_attributes(tokdata, CKK_DSA, CKO_PRIVATE_KEY,
253609
-                              new_priv_attrs, new_priv_attrs_len,
253609
-                              &new_priv_attrs2, &new_priv_attrs2_len, -1);
253609
+    rc = build_attribute(CKA_BASE, base_attr->pValue,
253609
+                         base_attr->ulValueLen, &attr);
253609
     if (rc != CKR_OK) {
253609
-        TRACE_ERROR("%s DSA check private key attributes failed with "
253609
-                    "rc=0x%lx\n", __func__, rc);
253609
+        TRACE_ERROR("%s build_attribute failed with rc=0x%lx\n", __func__, rc);
253609
+        goto dsa_generate_keypair_end;
253609
+    }
253609
+    rc = template_update_attribute(priv_tmpl, attr);
253609
+    if (rc != CKR_OK) {
253609
+        TRACE_ERROR("%s template_update_attribute failed with rc=0x%lx\n",
253609
+                    __func__, rc);
253609
+        free(attr);
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
     trace_attributes(__func__, "DSA public key attributes:",
253609
-                     new_publ_attrs2, new_publ_attrs2_len);
253609
+                     new_publ_attrs, new_publ_attrs_len);
253609
     trace_attributes(__func__, "DSA private key attributes:",
253609
-                     new_priv_attrs2, new_priv_attrs2_len);
253609
+                     new_priv_attrs, new_priv_attrs_len);
253609
 
253609
     ep11_get_pin_blob(ep11_session,
253609
-                      (ep11_is_session_object
253609
-                       (pPublicKeyTemplate, ulPublicKeyAttributeCount)
253609
-                       || ep11_is_session_object(pPrivateKeyTemplate,
253609
-                                                 ulPrivateKeyAttributeCount)),
253609
+                      (ep11_is_session_object(new_publ_attrs,
253609
+                                              new_publ_attrs_len) ||
253609
+                       ep11_is_session_object(new_priv_attrs,
253609
+                                              new_priv_attrs_len)),
253609
                       &ep11_pin_blob, &ep11_pin_blob_len);
253609
 
253609
     RETRY_START(rc, tokdata)
253609
         rc = dll_m_GenerateKeyPair(pMechanism,
253609
-                                   new_publ_attrs2, new_publ_attrs2_len,
253609
-                                   new_priv_attrs2, new_priv_attrs2_len,
253609
+                                   new_publ_attrs, new_publ_attrs_len,
253609
+                                   new_priv_attrs, new_priv_attrs_len,
253609
                                    ep11_pin_blob, ep11_pin_blob_len, privblob,
253609
                                    &privblobsize, publblob, &publblobsize,
253609
                                    target_info->target);
253609
@@ -5943,10 +5878,8 @@ static CK_RV dsa_generate_keypair(STDLL_TokData_t * tokdata,
253609
         goto dsa_generate_keypair_end;
253609
     }
253609
 
253609
-    TRACE_INFO("%s rc=0x%lx p_len=%zd publblobsize=0x%zx privblobsize=0x%zx "
253609
-               "npattr=0x%x\n",
253609
-               __func__, rc, p_len, publblobsize, privblobsize,
253609
-               new_public_attr + 1);
253609
+    TRACE_INFO("%s rc=0x%lx plen=%zd publblobsize=0x%zx privblobsize=0x%zx\n",
253609
+               __func__, rc, prime_attr->ulValueLen, publblobsize, privblobsize);
253609
 
253609
     if (check_expected_mkvp(tokdata, privblob, privblobsize) != CKR_OK) {
253609
         TRACE_ERROR("%s\n", ock_err(ERR_DEVICE_ERROR));
253609
@@ -6030,22 +5963,13 @@ dsa_generate_keypair_end:
253609
         free_attribute_array(new_publ_attrs, new_publ_attrs_len);
253609
     if (new_priv_attrs)
253609
         free_attribute_array(new_priv_attrs, new_priv_attrs_len);
253609
-    if (new_publ_attrs2)
253609
-        free_attribute_array(new_publ_attrs2, new_publ_attrs2_len);
253609
-    if (new_priv_attrs)
253609
-        free_attribute_array(new_priv_attrs2, new_priv_attrs2_len);
253609
     return rc;
253609
 }
253609
 
253609
-static CK_RV rsa_ec_generate_keypair(STDLL_TokData_t * tokdata,
253609
-                                     SESSION * sess,
253609
+static CK_RV rsa_ec_generate_keypair(STDLL_TokData_t *tokdata,
253609
+                                     SESSION *sess,
253609
                                      CK_MECHANISM_PTR pMechanism,
253609
-                                     TEMPLATE * publ_tmpl, TEMPLATE * priv_tmpl,
253609
-                                     CK_ATTRIBUTE_PTR pPublicKeyTemplate,
253609
-                                     CK_ULONG ulPublicKeyAttributeCount,
253609
-                                     CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
253609
-                                     CK_ULONG ulPrivateKeyAttributeCount,
253609
-                                     CK_SESSION_HANDLE h)
253609
+                                     TEMPLATE *publ_tmpl, TEMPLATE *priv_tmpl)
253609
 {
253609
     CK_RV rc;
253609
     CK_ATTRIBUTE *attr = NULL;
253609
@@ -6054,7 +5978,6 @@ static CK_RV rsa_ec_generate_keypair(STDLL_TokData_t * tokdata,
253609
     size_t privkey_blob_len = sizeof(privkey_blob);
253609
     unsigned char spki[MAX_BLOBSIZE];
253609
     size_t spki_len = sizeof(spki);
253609
-    CK_ULONG i;
253609
     CK_ULONG bit_str_len;
253609
     CK_BYTE *key;
253609
     CK_BYTE *data, *oid, *parm;
253609
@@ -6074,8 +5997,6 @@ static CK_RV rsa_ec_generate_keypair(STDLL_TokData_t * tokdata,
253609
     CK_ULONG new_publ_attrs2_len = 0, new_priv_attrs2_len = 0;
253609
     const struct _ec *curve = NULL;
253609
 
253609
-    UNUSED(h);
253609
-
253609
     if (pMechanism->mechanism == CKM_EC_KEY_PAIR_GEN) {
253609
         ktype = CKK_EC;
253609
     } else if ((pMechanism->mechanism == CKM_RSA_PKCS_KEY_PAIR_GEN) ||
253609
@@ -6132,24 +6053,16 @@ static CK_RV rsa_ec_generate_keypair(STDLL_TokData_t * tokdata,
253609
         goto error;
253609
     }
253609
 
253609
-    /* debug */
253609
-    for (i = 0; i < new_ulPrivateKeyAttributeCount; i++) {
253609
-        TRACE_INFO("%s gen priv attr type=0x%lx valuelen=0x%lx attrcnt=0x%lx\n",
253609
-                   __func__, new_pPrivateKeyTemplate[i].type,
253609
-                   new_pPrivateKeyTemplate[i].ulValueLen,
253609
-                   new_ulPrivateKeyAttributeCount);
253609
-    }
253609
-
253609
     trace_attributes(__func__, "RSA/EC public key attributes:",
253609
                      new_publ_attrs2, new_publ_attrs2_len);
253609
     trace_attributes(__func__, "RSA/EC private key attributes:",
253609
                      new_priv_attrs2, new_priv_attrs2_len);
253609
 
253609
     ep11_get_pin_blob(ep11_session,
253609
-                      (ep11_is_session_object
253609
-                       (pPublicKeyTemplate, ulPublicKeyAttributeCount)
253609
-                       || ep11_is_session_object(pPrivateKeyTemplate,
253609
-                                                 ulPrivateKeyAttributeCount)),
253609
+                      (ep11_is_session_object(new_publ_attrs2,
253609
+                                              new_publ_attrs2_len) ||
253609
+                       ep11_is_session_object(new_priv_attrs2,
253609
+                                              new_priv_attrs2_len)),
253609
                       &ep11_pin_blob, &ep11_pin_blob_len);
253609
 
253609
     RETRY_START(rc, tokdata)
253609
@@ -6406,15 +6319,10 @@ error:
253609
     return rc;
253609
 }
253609
 
253609
-static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t * tokdata,
253609
-                                     SESSION * sess,
253609
+static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t *tokdata,
253609
+                                     SESSION *sess,
253609
                                      CK_MECHANISM_PTR pMechanism,
253609
-                                     TEMPLATE * publ_tmpl, TEMPLATE * priv_tmpl,
253609
-                                     CK_ATTRIBUTE_PTR pPublicKeyTemplate,
253609
-                                     CK_ULONG ulPublicKeyAttributeCount,
253609
-                                     CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
253609
-                                     CK_ULONG ulPrivateKeyAttributeCount,
253609
-                                     CK_SESSION_HANDLE h)
253609
+                                     TEMPLATE *publ_tmpl, TEMPLATE *priv_tmpl)
253609
 {
253609
     CK_RV rc;
253609
     CK_ATTRIBUTE *attr = NULL;
253609
@@ -6422,7 +6330,6 @@ static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t * tokdata,
253609
     size_t privkey_blob_len = sizeof(privkey_blob);
253609
     unsigned char spki[MAX_BLOBSIZE];
253609
     size_t spki_len = sizeof(spki);
253609
-    CK_ULONG i;
253609
     CK_ULONG bit_str_len;
253609
     CK_BYTE *key;
253609
     CK_BYTE *data, *oid, *parm;
253609
@@ -6444,8 +6351,6 @@ static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t * tokdata,
253609
     const CK_BYTE dilithium_oid[] = { 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x04, 0x01,
253609
                                       0x02, 0x82, 0x0b, 0x01, 0x06, 0x05 };
253609
 
253609
-    UNUSED(h);
253609
-
253609
     if (pMechanism->mechanism != CKM_IBM_DILITHIUM) {
253609
         TRACE_ERROR("Invalid mechanism provided for %s\n ", __func__);
253609
         return CKR_MECHANISM_INVALID;
253609
@@ -6503,24 +6408,16 @@ static CK_RV ibm_dilithium_generate_keypair(STDLL_TokData_t * tokdata,
253609
         goto error;
253609
     }
253609
 
253609
-    /* debug */
253609
-    for (i = 0; i < new_ulPrivateKeyAttributeCount; i++) {
253609
-        TRACE_INFO("%s gen priv attr type=0x%lx valuelen=0x%lx attrcnt=0x%lx\n",
253609
-                   __func__, new_pPrivateKeyTemplate[i].type,
253609
-                   new_pPrivateKeyTemplate[i].ulValueLen,
253609
-                   new_ulPrivateKeyAttributeCount);
253609
-    }
253609
-
253609
     trace_attributes(__func__, "Dilithium public key attributes:",
253609
                      new_publ_attrs2, new_publ_attrs2_len);
253609
     trace_attributes(__func__, "Dilithium private key attributes:",
253609
                      new_priv_attrs2, new_priv_attrs2_len);
253609
 
253609
     ep11_get_pin_blob(ep11_session,
253609
-                      (ep11_is_session_object
253609
-                       (pPublicKeyTemplate, ulPublicKeyAttributeCount)
253609
-                       || ep11_is_session_object(pPrivateKeyTemplate,
253609
-                                                 ulPrivateKeyAttributeCount)),
253609
+                      (ep11_is_session_object(new_publ_attrs2,
253609
+                                              new_publ_attrs2_len) ||
253609
+                       ep11_is_session_object(new_priv_attrs2,
253609
+                                              new_priv_attrs2_len)),
253609
                       &ep11_pin_blob, &ep11_pin_blob_len);
253609
 
253609
     RETRY_START(rc, tokdata)
253609
@@ -6763,42 +6660,25 @@ CK_RV ep11tok_generate_key_pair(STDLL_TokData_t * tokdata, SESSION * sess,
253609
     case CKM_DH_PKCS_KEY_PAIR_GEN:
253609
         rc = dh_generate_keypair(tokdata, sess, pMechanism,
253609
                                  public_key_obj->template,
253609
-                                 private_key_obj->template,
253609
-                                 pPublicKeyTemplate,
253609
-                                 ulPublicKeyAttributeCount,
253609
-                                 pPrivateKeyTemplate,
253609
-                                 ulPrivateKeyAttributeCount, sess->handle);
253609
+                                 private_key_obj->template);
253609
         break;
253609
     case CKM_EC_KEY_PAIR_GEN:  /* takes same parameters as RSA */
253609
     case CKM_RSA_PKCS_KEY_PAIR_GEN:
253609
     case CKM_RSA_X9_31_KEY_PAIR_GEN:
253609
         rc = rsa_ec_generate_keypair(tokdata, sess, pMechanism,
253609
                                      public_key_obj->template,
253609
-                                     private_key_obj->template,
253609
-                                     pPublicKeyTemplate,
253609
-                                     ulPublicKeyAttributeCount,
253609
-                                     pPrivateKeyTemplate,
253609
-                                     ulPrivateKeyAttributeCount, sess->handle);
253609
+                                     private_key_obj->template);
253609
         break;
253609
     case CKM_DSA_PARAMETER_GEN:
253609
     case CKM_DSA_KEY_PAIR_GEN:
253609
         rc = dsa_generate_keypair(tokdata, sess, pMechanism,
253609
                                   public_key_obj->template,
253609
-                                  private_key_obj->template,
253609
-                                  pPublicKeyTemplate,
253609
-                                  ulPublicKeyAttributeCount,
253609
-                                  pPrivateKeyTemplate,
253609
-                                  ulPrivateKeyAttributeCount, sess->handle);
253609
+                                  private_key_obj->template);
253609
         break;
253609
     case CKM_IBM_DILITHIUM:
253609
         rc = ibm_dilithium_generate_keypair(tokdata, sess, pMechanism,
253609
                                             public_key_obj->template,
253609
-                                            private_key_obj->template,
253609
-                                            pPublicKeyTemplate,
253609
-                                            ulPublicKeyAttributeCount,
253609
-                                            pPrivateKeyTemplate,
253609
-                                            ulPrivateKeyAttributeCount,
253609
-                                            sess->handle);
253609
+                                            private_key_obj->template);
253609
         break;
253609
     default:
253609
         TRACE_ERROR("%s invalid mech %s\n", __func__,
253609
-- 
253609
2.16.2.windows.1
253609