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

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