Blame SOURCES/Save-SANs-separately-and-unparse-them-with-NO_REALM.patch

d738b9
From c796a84ffa455b60e08508f4b706f7ecae0054de Mon Sep 17 00:00:00 2001
d738b9
From: Greg Hudson <ghudson@mit.edu>
d738b9
Date: Thu, 22 Mar 2018 19:46:22 -0400
d738b9
Subject: [PATCH] Save SANs separately and unparse them with NO_REALM
d738b9
d738b9
(cherry picked from commit 23ea8d6a9617d17ae5a529c23174d77adac39055)
d738b9
---
d738b9
 src/plugins/preauth/pkinit/pkinit_crypto.h    |  4 +-
d738b9
 .../preauth/pkinit/pkinit_crypto_openssl.c    | 37 ++-----------------
d738b9
 src/plugins/preauth/pkinit/pkinit_matching.c  | 30 +++++++++++----
d738b9
 3 files changed, 28 insertions(+), 43 deletions(-)
d738b9
d738b9
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto.h b/src/plugins/preauth/pkinit/pkinit_crypto.h
d738b9
index a0176acad..c14f4456a 100644
d738b9
--- a/src/plugins/preauth/pkinit/pkinit_crypto.h
d738b9
+++ b/src/plugins/preauth/pkinit/pkinit_crypto.h
d738b9
@@ -100,8 +100,8 @@ typedef struct _pkinit_cert_matching_data {
d738b9
     char *issuer_dn;	    /* rfc2253-style issuer name string */
d738b9
     unsigned int ku_bits;   /* key usage information */
d738b9
     unsigned int eku_bits;  /* extended key usage information */
d738b9
-    krb5_principal *sans;   /* Null-terminated array of subject alternative
d738b9
-			       name info (pkinit and ms-upn) */
d738b9
+    krb5_principal *sans;   /* Null-terminated array of PKINIT SANs */
d738b9
+    krb5_principal *upns;   /* Null-terimnated array of UPN SANs */
d738b9
 } pkinit_cert_matching_data;
d738b9
 
d738b9
 /*
d738b9
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
d738b9
index 34ed7afaf..cf2f16294 100644
d738b9
--- a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
d738b9
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
d738b9
@@ -5110,6 +5110,9 @@ crypto_cert_free_matching_data(krb5_context context,
d738b9
     for (i = 0; md->sans != NULL && md->sans[i] != NULL; i++)
d738b9
         krb5_free_principal(context, md->sans[i]);
d738b9
     free(md->sans);
d738b9
+    for (i = 0; md->upns != NULL && md->upns[i] != NULL; i++)
d738b9
+        krb5_free_principal(context, md->upns[i]);
d738b9
+    free(md->upns);
d738b9
     free(md);
d738b9
 }
d738b9
 
d738b9
@@ -5138,8 +5141,6 @@ get_matching_data(krb5_context context,
d738b9
 {
d738b9
     krb5_error_code ret = ENOMEM;
d738b9
     pkinit_cert_matching_data *md = NULL;
d738b9
-    krb5_principal *pkinit_sans = NULL, *upn_sans = NULL;
d738b9
-    size_t i, j;
d738b9
 
d738b9
     *md_out = NULL;
d738b9
 
d738b9
@@ -5156,40 +5157,10 @@ get_matching_data(krb5_context context,
d738b9
 
d738b9
     /* Get the SAN data. */
d738b9
     ret = crypto_retrieve_X509_sans(context, plg_cryptoctx, req_cryptoctx,
d738b9
-                                    cert, &pkinit_sans, &upn_sans, NULL);
d738b9
+                                    cert, &md->sans, &md->upns, NULL);
d738b9
     if (ret)
d738b9
         goto cleanup;
d738b9
 
d738b9
-    j = 0;
d738b9
-    if (pkinit_sans != NULL) {
d738b9
-        for (i = 0; pkinit_sans[i] != NULL; i++)
d738b9
-            j++;
d738b9
-    }
d738b9
-    if (upn_sans != NULL) {
d738b9
-        for (i = 0; upn_sans[i] != NULL; i++)
d738b9
-            j++;
d738b9
-    }
d738b9
-    if (j != 0) {
d738b9
-        md->sans = calloc((size_t)j+1, sizeof(*md->sans));
d738b9
-        if (md->sans == NULL) {
d738b9
-            ret = ENOMEM;
d738b9
-            goto cleanup;
d738b9
-        }
d738b9
-        j = 0;
d738b9
-        if (pkinit_sans != NULL) {
d738b9
-            for (i = 0; pkinit_sans[i] != NULL; i++)
d738b9
-                md->sans[j++] = pkinit_sans[i];
d738b9
-            free(pkinit_sans);
d738b9
-        }
d738b9
-        if (upn_sans != NULL) {
d738b9
-            for (i = 0; upn_sans[i] != NULL; i++)
d738b9
-                md->sans[j++] = upn_sans[i];
d738b9
-            free(upn_sans);
d738b9
-        }
d738b9
-        md->sans[j] = NULL;
d738b9
-    } else
d738b9
-        md->sans = NULL;
d738b9
-
d738b9
     /* Get the KU and EKU data. */
d738b9
     ret = crypto_retrieve_X509_key_usage(context, plg_cryptoctx,
d738b9
                                          req_cryptoctx, cert, &md->ku_bits,
d738b9
diff --git a/src/plugins/preauth/pkinit/pkinit_matching.c b/src/plugins/preauth/pkinit/pkinit_matching.c
d738b9
index d6775dc4f..fe1e0f386 100644
d738b9
--- a/src/plugins/preauth/pkinit/pkinit_matching.c
d738b9
+++ b/src/plugins/preauth/pkinit/pkinit_matching.c
d738b9
@@ -470,7 +470,6 @@ component_match(krb5_context context,
d738b9
 {
d738b9
     int match = 0;
d738b9
     int i;
d738b9
-    krb5_principal p;
d738b9
     char *princ_string;
d738b9
 
d738b9
     switch (rc->kwval_type) {
d738b9
@@ -483,10 +482,17 @@ component_match(krb5_context context,
d738b9
             match = regexp_match(context, rc, md->issuer_dn);
d738b9
             break;
d738b9
         case kw_san:
d738b9
-            if (md->sans == NULL)
d738b9
-                break;
d738b9
-            for (i = 0, p = md->sans[i]; p != NULL; p = md->sans[++i]) {
d738b9
-                krb5_unparse_name(context, p, &princ_string);
d738b9
+            for (i = 0; md->sans != NULL && md->sans[i] != NULL; i++) {
d738b9
+                krb5_unparse_name(context, md->sans[i], &princ_string);
d738b9
+                match = regexp_match(context, rc, princ_string);
d738b9
+                krb5_free_unparsed_name(context, princ_string);
d738b9
+                if (match)
d738b9
+                    break;
d738b9
+            }
d738b9
+            for (i = 0; md->upns != NULL && md->upns[i] != NULL; i++) {
d738b9
+                krb5_unparse_name_flags(context, md->upns[i],
d738b9
+                                        KRB5_PRINCIPAL_UNPARSE_NO_REALM,
d738b9
+                                        &princ_string);
d738b9
                 match = regexp_match(context, rc, princ_string);
d738b9
                 krb5_free_unparsed_name(context, princ_string);
d738b9
                 if (match)
d738b9
@@ -572,10 +578,18 @@ check_all_certs(krb5_context context,
d738b9
         pkiDebug("%s: subject: '%s'\n", __FUNCTION__, md->subject_dn);
d738b9
 #if 0
d738b9
         pkiDebug("%s: issuer:  '%s'\n", __FUNCTION__, md->subject_dn);
d738b9
-        for (j = 0, p = md->sans[j]; p != NULL; p = md->sans[++j]) {
d738b9
+        for (j = 0; md->sans != NULL && md->sans[j] != NULL; j++) {
d738b9
             char *san_string;
d738b9
-            krb5_unparse_name(context, p, &san_string);
d738b9
-            pkiDebug("%s: san: '%s'\n", __FUNCTION__, san_string);
d738b9
+            krb5_unparse_name(context, md->sans[j], &san_string);
d738b9
+            pkiDebug("%s: PKINIT san: '%s'\n", __FUNCTION__, san_string);
d738b9
+            krb5_free_unparsed_name(context, san_string);
d738b9
+        }
d738b9
+        for (j = 0; md->upns != NULL && md->upns[j] != NULL; j++) {
d738b9
+            char *san_string;
d738b9
+            krb5_unparse_name_flags(context, md->upns[j],
d738b9
+                                    KRB5_PRINCIPAL_UNPARSE_NO_REALM,
d738b9
+                                    &san_string);
d738b9
+            pkiDebug("%s: UPN san: '%s'\n", __FUNCTION__, san_string);
d738b9
             krb5_free_unparsed_name(context, san_string);
d738b9
         }
d738b9
 #endif