Blame SOURCES/Fix-PKINIT-cert-matching-data-construction.patch

665228
From 1bde0be47ab0c6f94b474c0a3b1d03ec32db1293 Mon Sep 17 00:00:00 2001
665228
From: Greg Hudson <ghudson@mit.edu>
665228
Date: Tue, 17 Oct 2017 18:50:15 -0400
665228
Subject: [PATCH] Fix PKINIT cert matching data construction
665228
665228
Rewrite X509_NAME_oneline_ex() and its call sites to use dynamic
665228
allocation and to perform proper error checking.
665228
665228
ticket: 8617
665228
target_version: 1.16
665228
target_version: 1.15-next
665228
target_version: 1.14-next
665228
tags: pullup
665228
665228
(cherry picked from commit fbb687db1088ddd894d975996e5f6a4252b9a2b4)
665228
---
665228
 .../preauth/pkinit/pkinit_crypto_openssl.c    | 67 +++++++------------
665228
 1 file changed, 25 insertions(+), 42 deletions(-)
665228
665228
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
665228
index b243dca30..1eb273808 100644
665228
--- a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
665228
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
665228
@@ -5052,33 +5052,29 @@ out:
665228
     return retval;
665228
 }
665228
 
665228
-/*
665228
- * Return a string format of an X509_NAME in buf where
665228
- * size is an in/out parameter.  On input it is the size
665228
- * of the buffer, and on output it is the actual length
665228
- * of the name.
665228
- * If buf is NULL, returns the length req'd to hold name
665228
- */
665228
-static char *
665228
-X509_NAME_oneline_ex(X509_NAME * a,
665228
-                     char *buf,
665228
-                     unsigned int *size,
665228
-                     unsigned long flag)
665228
+static krb5_error_code
665228
+rfc2253_name(X509_NAME *name, char **str_out)
665228
 {
665228
-    BIO *out = NULL;
665228
+    BIO *b = NULL;
665228
+    char *str;
665228
 
665228
-    out = BIO_new(BIO_s_mem ());
665228
-    if (X509_NAME_print_ex(out, a, 0, flag) > 0) {
665228
-        if (buf != NULL && (*size) >  (unsigned int) BIO_number_written(out)) {
665228
-            memset(buf, 0, *size);
665228
-            BIO_read(out, buf, (int) BIO_number_written(out));
665228
-        }
665228
-        else {
665228
-            *size = BIO_number_written(out);
665228
-        }
665228
-    }
665228
-    BIO_free(out);
665228
-    return (buf);
665228
+    *str_out = NULL;
665228
+    b = BIO_new(BIO_s_mem());
665228
+    if (b == NULL)
665228
+        return ENOMEM;
665228
+    if (X509_NAME_print_ex(b, name, 0, XN_FLAG_SEP_COMMA_PLUS) < 0)
665228
+        goto error;
665228
+    str = calloc(BIO_number_written(b) + 1, 1);
665228
+    if (str == NULL)
665228
+        goto error;
665228
+    BIO_read(b, str, BIO_number_written(b));
665228
+    BIO_free(b);
665228
+    *str_out = str;
665228
+    return 0;
665228
+
665228
+error:
665228
+    BIO_free(b);
665228
+    return ENOMEM;
665228
 }
665228
 
665228
 /*
665228
@@ -5144,8 +5140,6 @@ get_matching_data(krb5_context context,
665228
     pkinit_cert_matching_data *md = NULL;
665228
     krb5_principal *pkinit_sans = NULL, *upn_sans = NULL;
665228
     size_t i, j;
665228
-    char buf[DN_BUF_LEN];
665228
-    unsigned int bufsize = sizeof(buf);
665228
 
665228
     *md_out = NULL;
665228
 
665228
@@ -5153,23 +5147,12 @@ get_matching_data(krb5_context context,
665228
     if (md == NULL)
665228
         goto cleanup;
665228
 
665228
-    /* Get the subject name (in rfc2253 format). */
665228
-    X509_NAME_oneline_ex(X509_get_subject_name(cert), buf, &bufsize,
665228
-                         XN_FLAG_SEP_COMMA_PLUS);
665228
-    md->subject_dn = strdup(buf);
665228
-    if (md->subject_dn == NULL) {
665228
-        ret = ENOMEM;
665228
+    ret = rfc2253_name(X509_get_subject_name(cert), &md->subject_dn);
665228
+    if (ret)
665228
         goto cleanup;
665228
-    }
665228
-
665228
-    /* Get the issuer name (in rfc2253 format). */
665228
-    X509_NAME_oneline_ex(X509_get_issuer_name(cert), buf, &bufsize,
665228
-                         XN_FLAG_SEP_COMMA_PLUS);
665228
-    md->issuer_dn = strdup(buf);
665228
-    if (md->issuer_dn == NULL) {
665228
-        ret = ENOMEM;
665228
+    ret = rfc2253_name(X509_get_issuer_name(cert), &md->issuer_dn);
665228
+    if (ret)
665228
         goto cleanup;
665228
-    }
665228
 
665228
     /* Get the SAN data. */
665228
     ret = crypto_retrieve_X509_sans(context, plg_cryptoctx, req_cryptoctx,