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

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