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

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