Blame SOURCES/Use-SHA256-instead-of-SHA1-for-PKINIT-CMS-digest.patch

73a275
From baa2a485190d1b31f3dae06a18dc24d71dbe35bf Mon Sep 17 00:00:00 2001
73a275
From: Julien Rische <jrische@redhat.com>
73a275
Date: Fri, 11 Mar 2022 12:04:14 +0100
73a275
Subject: [PATCH] Use SHA-256 instead of SHA-1 for PKINIT CMS digest
73a275
73a275
Various organizations including NIST have been strongly recommending to
73a275
stop using SHA-1 for digital signatures for some years already. CMS
73a275
digest is used to generate such signatures, hence it should be upgraded
73a275
to use SHA-256.
73a275
---
73a275
 .../preauth/pkinit/pkinit_crypto_openssl.c    | 40 ++++++++++---------
73a275
 1 file changed, 22 insertions(+), 18 deletions(-)
73a275
73a275
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
73a275
index dbb054378..32291e3ac 100644
73a275
--- a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
73a275
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
73a275
@@ -1234,7 +1234,7 @@ cms_signeddata_create(krb5_context context,
73a275
         /* will not fill-out EVP_PKEY because it's on the smartcard */
73a275
 
73a275
         /* Set digest algs */
73a275
-        p7si->digest_alg->algorithm = OBJ_nid2obj(NID_sha1);
73a275
+        p7si->digest_alg->algorithm = OBJ_nid2obj(NID_sha256);
73a275
 
73a275
         if (p7si->digest_alg->parameter != NULL)
73a275
             ASN1_TYPE_free(p7si->digest_alg->parameter);
73a275
@@ -1245,17 +1245,18 @@ cms_signeddata_create(krb5_context context,
73a275
         /* Set sig algs */
73a275
         if (p7si->digest_enc_alg->parameter != NULL)
73a275
             ASN1_TYPE_free(p7si->digest_enc_alg->parameter);
73a275
-        p7si->digest_enc_alg->algorithm = OBJ_nid2obj(NID_sha1WithRSAEncryption);
73a275
+        p7si->digest_enc_alg->algorithm =
73a275
+            OBJ_nid2obj(NID_sha256WithRSAEncryption);
73a275
         if (!(p7si->digest_enc_alg->parameter = ASN1_TYPE_new()))
73a275
             goto cleanup;
73a275
         p7si->digest_enc_alg->parameter->type = V_ASN1_NULL;
73a275
 
73a275
         /* add signed attributes */
73a275
-        /* compute sha1 digest over the EncapsulatedContentInfo */
73a275
+        /* compute sha256 digest over the EncapsulatedContentInfo */
73a275
         ctx = EVP_MD_CTX_new();
73a275
         if (ctx == NULL)
73a275
             goto cleanup;
73a275
-        EVP_DigestInit_ex(ctx, EVP_sha1(), NULL);
73a275
+        EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
73a275
         EVP_DigestUpdate(ctx, data, data_len);
73a275
         md_tmp = EVP_MD_CTX_md(ctx);
73a275
         EVP_DigestFinal_ex(ctx, md_data, &md_len);
73a275
@@ -1283,12 +1284,14 @@ cms_signeddata_create(krb5_context context,
73a275
             goto cleanup2;
73a275
 
73a275
 #ifndef WITHOUT_PKCS11
73a275
-        /* Some tokens can only do RSAEncryption without sha1 hash */
73a275
-        /* to compute sha1WithRSAEncryption, encode the algorithm ID for the hash
73a275
-         * function and the hash value into an ASN.1 value of type DigestInfo
73a275
-         * DigestInfo::=SEQUENCE {
73a275
-         *  digestAlgorithm  AlgorithmIdentifier,
73a275
-         *  digest OCTET STRING }
73a275
+        /*
73a275
+         * Some tokens can only do RSAEncryption without a hash.  To compute
73a275
+         * sha256WithRSAEncryption, encode the algorithm ID for the hash
73a275
+         * function and the hash value into an ASN.1 value of type DigestInfo:
73a275
+         * DigestInfo ::= SEQUENCE {
73a275
+         *   digestAlgorithm  AlgorithmIdentifier,
73a275
+         *   digest  OCTET STRING
73a275
+         * }
73a275
          */
73a275
         if (id_cryptoctx->pkcs11_method == 1 &&
73a275
             id_cryptoctx->mech == CKM_RSA_PKCS) {
73a275
@@ -1304,7 +1307,7 @@ cms_signeddata_create(krb5_context context,
73a275
             alg = X509_ALGOR_new();
73a275
             if (alg == NULL)
73a275
                 goto cleanup2;
73a275
-            X509_ALGOR_set0(alg, OBJ_nid2obj(NID_sha1), V_ASN1_NULL, NULL);
73a275
+            X509_ALGOR_set0(alg, OBJ_nid2obj(NID_sha256), V_ASN1_NULL, NULL);
73a275
             alg_len = i2d_X509_ALGOR(alg, NULL);
73a275
 
73a275
             digest = ASN1_OCTET_STRING_new();
73a275
@@ -1333,7 +1336,7 @@ cms_signeddata_create(krb5_context context,
73a275
 #endif
73a275
         {
73a275
             pkiDebug("mech = %s\n",
73a275
-                     id_cryptoctx->pkcs11_method == 1 ? "CKM_SHA1_RSA_PKCS" : "FS");
73a275
+                     id_cryptoctx->pkcs11_method == 1 ? "CKM_SHA256_RSA_PKCS" : "FS");
73a275
             retval = pkinit_sign_data(context, id_cryptoctx, abuf, alen,
73a275
                                       &sig, &sig_len);
73a275
         }
73a275
@@ -4147,7 +4150,7 @@ create_signature(unsigned char **sig, unsigned int *sig_len,
73a275
     ctx = EVP_MD_CTX_new();
73a275
     if (ctx == NULL)
73a275
         return ENOMEM;
73a275
-    EVP_SignInit(ctx, EVP_sha1());
73a275
+    EVP_SignInit(ctx, EVP_sha256());
73a275
     EVP_SignUpdate(ctx, data, data_len);
73a275
     *sig_len = EVP_PKEY_size(pkey);
73a275
     if ((*sig = malloc(*sig_len)) == NULL)
73a275
@@ -4623,10 +4626,11 @@ pkinit_get_certs_pkcs11(krb5_context context,
73a275
 
73a275
 #ifndef PKINIT_USE_MECH_LIST
73a275
     /*
73a275
-     * We'd like to use CKM_SHA1_RSA_PKCS for signing if it's available, but
73a275
-     * many cards seems to be confused about whether they are capable of
73a275
-     * this or not. The safe thing seems to be to ignore the mechanism list,
73a275
-     * always use CKM_RSA_PKCS and calculate the sha1 digest ourselves.
73a275
+     * We'd like to use CKM_SHA256_RSA_PKCS for signing if it's available, but
73a275
+     * historically many cards seem to be confused about whether they are
73a275
+     * capable of mechanisms or not. The safe thing seems to be to ignore the
73a275
+     * mechanism list, always use CKM_RSA_PKCS and calculate the sha256 digest
73a275
+     * ourselves.
73a275
      */
73a275
 
73a275
     id_cryptoctx->mech = CKM_RSA_PKCS;
73a275
@@ -4654,7 +4658,7 @@ pkinit_get_certs_pkcs11(krb5_context context,
73a275
         if (mechp[i] == CKM_RSA_PKCS) {
73a275
             /* This seems backwards... */
73a275
             id_cryptoctx->mech =
73a275
-                (info.flags & CKF_SIGN) ? CKM_SHA1_RSA_PKCS : CKM_RSA_PKCS;
73a275
+                (info.flags & CKF_SIGN) ? CKM_SHA256_RSA_PKCS : CKM_RSA_PKCS;
73a275
         }
73a275
     }
73a275
     free(mechp);
73a275
-- 
73a275
2.35.1
73a275