Blame SOURCES/0059-Make-openssl-accept-the-right-set-of-KU-EKUs.patch

d84fc6
From 705d47ac2c90b8de07a4ef3e1930de6c4b8fece0 Mon Sep 17 00:00:00 2001
d84fc6
From: Peter Jones <pjones@redhat.com>
d84fc6
Date: Wed, 22 Jul 2020 19:54:58 -0400
d84fc6
Subject: [PATCH 59/62] Make openssl accept the right set of KU/EKUs
d84fc6
d84fc6
Signed-off-by: Peter Jones <pjones@redhat.com>
d84fc6
Upstream: pr#211
d84fc6
---
d84fc6
 Cryptlib/Pk/CryptPkcs7Verify.c | 87 ++++++++++++++++++++++++++++++++++
d84fc6
 1 file changed, 87 insertions(+)
d84fc6
d84fc6
diff --git a/Cryptlib/Pk/CryptPkcs7Verify.c b/Cryptlib/Pk/CryptPkcs7Verify.c
d84fc6
index dcaba436797..09895d8c66a 100644
d84fc6
--- a/Cryptlib/Pk/CryptPkcs7Verify.c
d84fc6
+++ b/Cryptlib/Pk/CryptPkcs7Verify.c
d84fc6
@@ -30,6 +30,91 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
d84fc6
 
d84fc6
 UINT8 mOidValue[9] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02 };
d84fc6
 
d84fc6
+#if 1
d84fc6
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
d84fc6
+#define X509_OBJECT_get0_X509(obj) ((obj)->data.x509)
d84fc6
+#define X509_OBJECT_get_type(obj) ((obj)->type)
d84fc6
+#define X509_STORE_CTX_get0_cert(ctx) ((ctx)->cert)
d84fc6
+#define X509_STORE_get0_objects(certs) ((certs)->objs)
d84fc6
+#define X509_get_extended_key_usage(cert) ((cert)->ex_xkusage)
d84fc6
+#if OPENSSL_VERSION_NUMBER < 0x10020000L
d84fc6
+#define X509_STORE_CTX_get0_store(ctx) ((ctx)->ctx)
d84fc6
+#endif
d84fc6
+#endif
d84fc6
+
d84fc6
+static int cert_in_store(X509 *cert, X509_STORE_CTX *ctx)
d84fc6
+{
d84fc6
+  X509_OBJECT obj;
d84fc6
+  obj.type = X509_LU_X509;
d84fc6
+  obj.data.x509 = cert;
d84fc6
+  return X509_OBJECT_retrieve_match(ctx->ctx->objs, &obj) != NULL;
d84fc6
+}
d84fc6
+#else
d84fc6
+/*
d84fc6
+ * Later versions of openssl will need this instead.
d84fc6
+ */
d84fc6
+static int cert_in_store(X509 *cert, X509_STORE_CTX *ctx)
d84fc6
+{
d84fc6
+  STACK_OF(X509_OBJECT) *objs;
d84fc6
+  X509_OBJECT *obj;
d84fc6
+  int i;
d84fc6
+
d84fc6
+  objs = X509_STORE_get0_objects(X509_STORE_CTX_get0_store(ctx));
d84fc6
+
d84fc6
+  for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
d84fc6
+    obj = sk_X509_OBJECT_value(objs, i);
d84fc6
+
d84fc6
+    if (X509_OBJECT_get_type(obj) == X509_LU_X509 &&
d84fc6
+	!X509_cmp(X509_OBJECT_get0_X509(obj), cert))
d84fc6
+      return 1;
d84fc6
+  }
d84fc6
+
d84fc6
+  return 0;
d84fc6
+}
d84fc6
+#endif
d84fc6
+
d84fc6
+int
d84fc6
+X509VerifyCb (
d84fc6
+  IN int            Status,
d84fc6
+  IN X509_STORE_CTX *Context
d84fc6
+  )
d84fc6
+{
d84fc6
+  INTN         Error;
d84fc6
+
d84fc6
+  Error = (INTN) X509_STORE_CTX_get_error (Context);
d84fc6
+
d84fc6
+  /* Accept code-signing keys */
d84fc6
+  if (Error == X509_V_ERR_INVALID_PURPOSE &&
d84fc6
+      X509_get_extended_key_usage(X509_STORE_CTX_get0_cert(Context)) == XKU_CODE_SIGN) {
d84fc6
+    Status = 1;
d84fc6
+  } else if (Error == X509_V_ERR_CERT_UNTRUSTED ||
d84fc6
+	     Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT ||
d84fc6
+	     Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY ||
d84fc6
+	     Error == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE) {
d84fc6
+    /* all certs in our cert database are explicitly trusted */
d84fc6
+
d84fc6
+    if (cert_in_store(X509_STORE_CTX_get_current_cert(Context), Context))
d84fc6
+      Status = 1;
d84fc6
+  } else if (Error == X509_V_ERR_CERT_HAS_EXPIRED ||
d84fc6
+	     Error == X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD ||
d84fc6
+	     Error == X509_V_ERR_CERT_NOT_YET_VALID ||
d84fc6
+	     Error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY ||
d84fc6
+	     Error == X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD) {
d84fc6
+    /* UEFI explicitly allows expired certificates */
d84fc6
+    Status = 1;
d84fc6
+#if 0
d84fc6
+  } else if (Error == X509_V_ERR_INVALID_CA) {
d84fc6
+    /* Due to the historical reason, we have to relax the the x509 v3 extension
d84fc6
+     * check to allow the CA certificates without the CA flag in the basic
d84fc6
+     * constraints or KeyCertSign in the key usage to be loaded. In the future,
d84fc6
+     * this callback should be removed to enforce the proper check. */
d84fc6
+    Status = 1;
d84fc6
+#endif
d84fc6
+  }
d84fc6
+
d84fc6
+  return Status;
d84fc6
+}
d84fc6
+
d84fc6
 /**
d84fc6
   Check input P7Data is a wrapped ContentInfo structure or not. If not construct
d84fc6
   a new structure to wrap P7Data.
d84fc6
@@ -844,6 +929,8 @@ Pkcs7Verify (
d84fc6
     goto _Exit;
d84fc6
   }
d84fc6
 
d84fc6
+  X509_STORE_set_verify_cb (CertStore, X509VerifyCb);
d84fc6
+
d84fc6
   //
d84fc6
   // For generic PKCS#7 handling, InData may be NULL if the content is present
d84fc6
   // in PKCS#7 structure. So ignore NULL checking here.
d84fc6
-- 
d84fc6
2.26.2
d84fc6