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