Blame SOURCES/Simplify-PKINIT-cert-iteration-and-selection.patch

167778
From 68c478bbc5a130bf4cc800b856da73b2fd5e83ed Mon Sep 17 00:00:00 2001
167778
From: Matt Rogers <mrogers@redhat.com>
167778
Date: Tue, 21 Mar 2017 21:24:14 -0400
167778
Subject: [PATCH] Simplify PKINIT cert iteration and selection
167778
167778
Remove the pkinit_cert_handle structures and iteration functions used
167778
during certificate matching.  Instead, make pkinit_matching.c obtain a
167778
list of matching data objects from the crypto code, and then select a
167778
cert based on the index into that list.
167778
167778
Also fix a typo in the name of crypto_retrieve_X509_key_usage().
167778
167778
[ghudson@mit.edu: simplified code]
167778
167778
(cherry picked from commit 01b1c0e26252a00f2215408b0e473b84aa0f6a87)
167778
---
167778
 src/plugins/preauth/pkinit/pkinit_crypto.h    |  75 +---
167778
 .../preauth/pkinit/pkinit_crypto_openssl.c    | 383 +++++++-----------
167778
 .../preauth/pkinit/pkinit_crypto_openssl.h    |  19 -
167778
 src/plugins/preauth/pkinit/pkinit_matching.c  | 139 +------
167778
 4 files changed, 194 insertions(+), 422 deletions(-)
167778
167778
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto.h b/src/plugins/preauth/pkinit/pkinit_crypto.h
167778
index 49b96b8ee..a0176acad 100644
167778
--- a/src/plugins/preauth/pkinit/pkinit_crypto.h
167778
+++ b/src/plugins/preauth/pkinit/pkinit_crypto.h
167778
@@ -96,7 +96,6 @@ typedef struct _pkinit_cert_iter_info *pkinit_cert_iter_handle;
167778
 #define PKINIT_ITER_NO_MORE	0x11111111  /* XXX */
167778
 
167778
 typedef struct _pkinit_cert_matching_data {
167778
-    pkinit_cert_handle ch;  /* cert handle for this certificate */
167778
     char *subject_dn;	    /* rfc2253-style subject name string */
167778
     char *issuer_dn;	    /* rfc2253-style issuer name string */
167778
     unsigned int ku_bits;   /* key usage information */
167778
@@ -458,68 +457,38 @@ krb5_error_code crypto_free_cert_info
167778
 
167778
 
167778
 /*
167778
- * Get number of certificates available after crypto_load_certs()
167778
+ * Get a null-terminated list of certificate matching data objects for the
167778
+ * certificates loaded in id_cryptoctx.
167778
  */
167778
-krb5_error_code crypto_cert_get_count
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_plg_crypto_context plg_cryptoctx,	/* IN */
167778
-	pkinit_req_crypto_context req_cryptoctx,	/* IN */
167778
-	pkinit_identity_crypto_context id_cryptoctx,	/* IN */
167778
-	int *cert_count);				/* OUT */
167778
+krb5_error_code
167778
+crypto_cert_get_matching_data(krb5_context context,
167778
+			      pkinit_plg_crypto_context plg_cryptoctx,
167778
+			      pkinit_req_crypto_context req_cryptoctx,
167778
+			      pkinit_identity_crypto_context id_cryptoctx,
167778
+			      pkinit_cert_matching_data ***md_out);
167778
 
167778
 /*
167778
- * Begin iteration over the certs loaded in crypto_load_certs()
167778
+ * Free a matching data object.
167778
  */
167778
-krb5_error_code crypto_cert_iteration_begin
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_plg_crypto_context plg_cryptoctx,	/* IN */
167778
-	pkinit_req_crypto_context req_cryptoctx,	/* IN */
167778
-	pkinit_identity_crypto_context id_cryptoctx,	/* IN */
167778
-	pkinit_cert_iter_handle *iter_handle);		/* OUT */
167778
+void
167778
+crypto_cert_free_matching_data(krb5_context context,
167778
+			       pkinit_cert_matching_data *md);
167778
 
167778
 /*
167778
- * End iteration over the certs loaded in crypto_load_certs()
167778
+ * Free a list of matching data objects.
167778
  */
167778
-krb5_error_code crypto_cert_iteration_end
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_cert_iter_handle iter_handle);		/* IN */
167778
+void
167778
+crypto_cert_free_matching_data_list(krb5_context context,
167778
+				    pkinit_cert_matching_data **matchdata);
167778
 
167778
 /*
167778
- * Get next certificate handle
167778
+ * Choose one of the certificates loaded in idctx to use for PKINIT client
167778
+ * operations.  cred_index must be an index into the array of matching objects
167778
+ * returned by crypto_cert_get_matching_data().
167778
  */
167778
-krb5_error_code crypto_cert_iteration_next
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_cert_iter_handle iter_handle,		/* IN */
167778
-	pkinit_cert_handle *cert_handle);		/* OUT */
167778
-
167778
-/*
167778
- * Release cert handle
167778
- */
167778
-krb5_error_code crypto_cert_release
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_cert_handle cert_handle);		/* IN */
167778
-
167778
-/*
167778
- * Get certificate matching information
167778
- */
167778
-krb5_error_code crypto_cert_get_matching_data
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_cert_handle cert_handle,			/* IN */
167778
-	pkinit_cert_matching_data **ret_data);		/* OUT */
167778
-
167778
-/*
167778
- * Free certificate information
167778
- */
167778
-krb5_error_code crypto_cert_free_matching_data
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_cert_matching_data *data);		/* IN */
167778
-
167778
-/*
167778
- * Make the given certificate "the chosen one"
167778
- */
167778
-krb5_error_code crypto_cert_select
167778
-	(krb5_context context,				/* IN */
167778
-	pkinit_cert_matching_data *data);		/* IN */
167778
+krb5_error_code
167778
+crypto_cert_select(krb5_context context, pkinit_identity_crypto_context idctx,
167778
+		   size_t cred_index);
167778
 
167778
 /*
167778
  * Select the default certificate as "the chosen one"
167778
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
167778
index 6a95f8035..b243dca30 100644
167778
--- a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
167778
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
167778
@@ -4974,136 +4974,16 @@ cleanup:
167778
     return retval;
167778
 }
167778
 
167778
-/*
167778
- * Get number of certificates available after crypto_load_certs()
167778
- */
167778
-krb5_error_code
167778
-crypto_cert_get_count(krb5_context context,
167778
-                      pkinit_plg_crypto_context plg_cryptoctx,
167778
-                      pkinit_req_crypto_context req_cryptoctx,
167778
-                      pkinit_identity_crypto_context id_cryptoctx,
167778
-                      int *cert_count)
167778
-{
167778
-    int count;
167778
-
167778
-    if (id_cryptoctx == NULL || id_cryptoctx->creds[0] == NULL)
167778
-        return EINVAL;
167778
-
167778
-    for (count = 0;
167778
-         count <= MAX_CREDS_ALLOWED && id_cryptoctx->creds[count] != NULL;
167778
-         count++);
167778
-    *cert_count = count;
167778
-    return 0;
167778
-}
167778
-
167778
-
167778
-/*
167778
- * Begin iteration over the certs loaded in crypto_load_certs()
167778
- */
167778
-krb5_error_code
167778
-crypto_cert_iteration_begin(krb5_context context,
167778
-                            pkinit_plg_crypto_context plg_cryptoctx,
167778
-                            pkinit_req_crypto_context req_cryptoctx,
167778
-                            pkinit_identity_crypto_context id_cryptoctx,
167778
-                            pkinit_cert_iter_handle *ih_ret)
167778
-{
167778
-    struct _pkinit_cert_iter_data *id;
167778
-
167778
-    if (id_cryptoctx == NULL || ih_ret == NULL)
167778
-        return EINVAL;
167778
-    if (id_cryptoctx->creds[0] == NULL) /* No cred info available */
167778
-        return ENOENT;
167778
-
167778
-    id = calloc(1, sizeof(*id));
167778
-    if (id == NULL)
167778
-        return ENOMEM;
167778
-    id->magic = ITER_MAGIC;
167778
-    id->plgctx = plg_cryptoctx,
167778
-        id->reqctx = req_cryptoctx,
167778
-        id->idctx = id_cryptoctx;
167778
-    id->index = 0;
167778
-    *ih_ret = (pkinit_cert_iter_handle) id;
167778
-    return 0;
167778
-}
167778
-
167778
-/*
167778
- * End iteration over the certs loaded in crypto_load_certs()
167778
- */
167778
-krb5_error_code
167778
-crypto_cert_iteration_end(krb5_context context,
167778
-                          pkinit_cert_iter_handle ih)
167778
-{
167778
-    struct _pkinit_cert_iter_data *id = (struct _pkinit_cert_iter_data *)ih;
167778
-
167778
-    if (id == NULL || id->magic != ITER_MAGIC)
167778
-        return EINVAL;
167778
-    free(ih);
167778
-    return 0;
167778
-}
167778
-
167778
-/*
167778
- * Get next certificate handle
167778
- */
167778
-krb5_error_code
167778
-crypto_cert_iteration_next(krb5_context context,
167778
-                           pkinit_cert_iter_handle ih,
167778
-                           pkinit_cert_handle *ch_ret)
167778
-{
167778
-    struct _pkinit_cert_iter_data *id = (struct _pkinit_cert_iter_data *)ih;
167778
-    struct _pkinit_cert_data *cd;
167778
-    pkinit_identity_crypto_context id_cryptoctx;
167778
-
167778
-    if (id == NULL || id->magic != ITER_MAGIC)
167778
-        return EINVAL;
167778
-
167778
-    if (ch_ret == NULL)
167778
-        return EINVAL;
167778
-
167778
-    id_cryptoctx = id->idctx;
167778
-    if (id_cryptoctx == NULL)
167778
-        return EINVAL;
167778
-
167778
-    if (id_cryptoctx->creds[id->index] == NULL)
167778
-        return PKINIT_ITER_NO_MORE;
167778
-
167778
-    cd = calloc(1, sizeof(*cd));
167778
-    if (cd == NULL)
167778
-        return ENOMEM;
167778
-
167778
-    cd->magic = CERT_MAGIC;
167778
-    cd->plgctx = id->plgctx;
167778
-    cd->reqctx = id->reqctx;
167778
-    cd->idctx = id->idctx;
167778
-    cd->index = id->index;
167778
-    cd->cred = id_cryptoctx->creds[id->index++];
167778
-    *ch_ret = (pkinit_cert_handle)cd;
167778
-    return 0;
167778
-}
167778
-
167778
-/*
167778
- * Release cert handle
167778
- */
167778
-krb5_error_code
167778
-crypto_cert_release(krb5_context context,
167778
-                    pkinit_cert_handle ch)
167778
-{
167778
-    struct _pkinit_cert_data *cd = (struct _pkinit_cert_data *)ch;
167778
-    if (cd == NULL || cd->magic != CERT_MAGIC)
167778
-        return EINVAL;
167778
-    free(cd);
167778
-    return 0;
167778
-}
167778
-
167778
 /*
167778
  * Get certificate Key Usage and Extended Key Usage
167778
  */
167778
 static krb5_error_code
167778
-crypto_retieve_X509_key_usage(krb5_context context,
167778
-                              pkinit_plg_crypto_context plgcctx,
167778
-                              pkinit_req_crypto_context reqcctx,
167778
-                              X509 *x,
167778
-                              unsigned int *ret_ku_bits,
167778
-                              unsigned int *ret_eku_bits)
167778
+crypto_retrieve_X509_key_usage(krb5_context context,
167778
+                               pkinit_plg_crypto_context plgcctx,
167778
+                               pkinit_req_crypto_context reqcctx,
167778
+                               X509 *x,
167778
+                               unsigned int *ret_ku_bits,
167778
+                               unsigned int *ret_eku_bits)
167778
 {
167778
     krb5_error_code retval = 0;
167778
     int i;
167778
@@ -5202,55 +5082,99 @@ X509_NAME_oneline_ex(X509_NAME * a,
167778
 }
167778
 
167778
 /*
167778
- * Get certificate information
167778
+ * Get number of certificates available after crypto_load_certs()
167778
  */
167778
-krb5_error_code
167778
-crypto_cert_get_matching_data(krb5_context context,
167778
-                              pkinit_cert_handle ch,
167778
-                              pkinit_cert_matching_data **ret_md)
167778
+static krb5_error_code
167778
+crypto_cert_get_count(pkinit_identity_crypto_context id_cryptoctx,
167778
+                      int *cert_count)
167778
 {
167778
-    krb5_error_code retval;
167778
-    pkinit_cert_matching_data *md;
167778
-    krb5_principal *pkinit_sans =NULL, *upn_sans = NULL;
167778
-    struct _pkinit_cert_data *cd = (struct _pkinit_cert_data *)ch;
167778
-    unsigned int i, j;
167778
+    int count;
167778
+
167778
+    *cert_count = 0;
167778
+    if (id_cryptoctx == NULL || id_cryptoctx->creds[0] == NULL)
167778
+        return EINVAL;
167778
+
167778
+    for (count = 0;
167778
+         count <= MAX_CREDS_ALLOWED && id_cryptoctx->creds[count] != NULL;
167778
+         count++);
167778
+    *cert_count = count;
167778
+    return 0;
167778
+}
167778
+
167778
+void
167778
+crypto_cert_free_matching_data(krb5_context context,
167778
+                               pkinit_cert_matching_data *md)
167778
+{
167778
+    int i;
167778
+
167778
+    if (md == NULL)
167778
+        return;
167778
+    free(md->subject_dn);
167778
+    free(md->issuer_dn);
167778
+    for (i = 0; md->sans != NULL && md->sans[i] != NULL; i++)
167778
+        krb5_free_principal(context, md->sans[i]);
167778
+    free(md->sans);
167778
+    free(md);
167778
+}
167778
+
167778
+/*
167778
+ * Free certificate matching data.
167778
+ */
167778
+void
167778
+crypto_cert_free_matching_data_list(krb5_context context,
167778
+                                    pkinit_cert_matching_data **list)
167778
+{
167778
+    int i;
167778
+
167778
+    for (i = 0; list != NULL && list[i] != NULL; i++)
167778
+        crypto_cert_free_matching_data(context, list[i]);
167778
+    free(list);
167778
+}
167778
+
167778
+/*
167778
+ * Get certificate matching data for cert.
167778
+ */
167778
+static krb5_error_code
167778
+get_matching_data(krb5_context context,
167778
+                  pkinit_plg_crypto_context plg_cryptoctx,
167778
+                  pkinit_req_crypto_context req_cryptoctx, X509 *cert,
167778
+                  pkinit_cert_matching_data **md_out)
167778
+{
167778
+    krb5_error_code ret = ENOMEM;
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
-    if (cd == NULL || cd->magic != CERT_MAGIC)
167778
-        return EINVAL;
167778
-    if (ret_md == NULL)
167778
-        return EINVAL;
167778
+    *md_out = NULL;
167778
 
167778
     md = calloc(1, sizeof(*md));
167778
     if (md == NULL)
167778
-        return ENOMEM;
167778
+        goto cleanup;
167778
 
167778
-    md->ch = ch;
167778
-
167778
-    /* get the subject name (in rfc2253 format) */
167778
-    X509_NAME_oneline_ex(X509_get_subject_name(cd->cred->cert),
167778
-                         buf, &bufsize, XN_FLAG_SEP_COMMA_PLUS);
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
-        retval = ENOMEM;
167778
+        ret = ENOMEM;
167778
         goto cleanup;
167778
     }
167778
 
167778
-    /* get the issuer name (in rfc2253 format) */
167778
-    X509_NAME_oneline_ex(X509_get_issuer_name(cd->cred->cert),
167778
-                         buf, &bufsize, XN_FLAG_SEP_COMMA_PLUS);
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
-        retval = ENOMEM;
167778
+        ret = ENOMEM;
167778
         goto cleanup;
167778
     }
167778
 
167778
-    /* get the san data */
167778
-    retval = crypto_retrieve_X509_sans(context, cd->plgctx, cd->reqctx,
167778
-                                       cd->cred->cert, &pkinit_sans,
167778
-                                       &upn_sans, NULL);
167778
-    if (retval)
167778
+    /* Get the SAN data. */
167778
+    ret = crypto_retrieve_X509_sans(context, plg_cryptoctx, req_cryptoctx,
167778
+                                    cert, &pkinit_sans, &upn_sans, NULL);
167778
+    if (ret)
167778
         goto cleanup;
167778
 
167778
     j = 0;
167778
@@ -5265,7 +5189,7 @@ crypto_cert_get_matching_data(krb5_context context,
167778
     if (j != 0) {
167778
         md->sans = calloc((size_t)j+1, sizeof(*md->sans));
167778
         if (md->sans == NULL) {
167778
-            retval = ENOMEM;
167778
+            ret = ENOMEM;
167778
             goto cleanup;
167778
         }
167778
         j = 0;
167778
@@ -5283,88 +5207,96 @@ crypto_cert_get_matching_data(krb5_context context,
167778
     } else
167778
         md->sans = NULL;
167778
 
167778
-    /* get the KU and EKU data */
167778
-
167778
-    retval = crypto_retieve_X509_key_usage(context, cd->plgctx, cd->reqctx,
167778
-                                           cd->cred->cert,
167778
-                                           &md->ku_bits, &md->eku_bits);
167778
-    if (retval)
167778
+    /* Get the KU and EKU data. */
167778
+    ret = crypto_retrieve_X509_key_usage(context, plg_cryptoctx,
167778
+                                         req_cryptoctx, cert, &md->ku_bits,
167778
+                                         &md->eku_bits);
167778
+    if (ret)
167778
         goto cleanup;
167778
 
167778
-    *ret_md = md;
167778
-    retval = 0;
167778
+    *md_out = md;
167778
+    md = NULL;
167778
+
167778
 cleanup:
167778
-    if (retval) {
167778
-        if (md)
167778
-            crypto_cert_free_matching_data(context, md);
167778
+    crypto_cert_free_matching_data(context, md);
167778
+    return ret;
167778
+}
167778
+
167778
+krb5_error_code
167778
+crypto_cert_get_matching_data(krb5_context context,
167778
+                              pkinit_plg_crypto_context plg_cryptoctx,
167778
+                              pkinit_req_crypto_context req_cryptoctx,
167778
+                              pkinit_identity_crypto_context id_cryptoctx,
167778
+                              pkinit_cert_matching_data ***md_out)
167778
+{
167778
+    krb5_error_code ret;
167778
+    pkinit_cert_matching_data **md_list = NULL;
167778
+    int count, i;
167778
+
167778
+    ret = crypto_cert_get_count(id_cryptoctx, &count);
167778
+    if (ret)
167778
+        goto cleanup;
167778
+
167778
+    md_list = calloc(count + 1, sizeof(*md_list));
167778
+    if (md_list == NULL) {
167778
+        ret = ENOMEM;
167778
+        goto cleanup;
167778
     }
167778
-    return retval;
167778
+
167778
+    for (i = 0; i < count; i++) {
167778
+        ret = get_matching_data(context, plg_cryptoctx, req_cryptoctx,
167778
+                                id_cryptoctx->creds[i]->cert, &md_list[i]);
167778
+        if (ret) {
167778
+            pkiDebug("%s: crypto_cert_get_matching_data error %d, %s\n",
167778
+                     __FUNCTION__, ret, error_message(ret));
167778
+            goto cleanup;
167778
+        }
167778
+    }
167778
+
167778
+    *md_out = md_list;
167778
+    md_list = NULL;
167778
+
167778
+cleanup:
167778
+    crypto_cert_free_matching_data_list(context, md_list);
167778
+    return ret;
167778
 }
167778
 
167778
 /*
167778
- * Free certificate information
167778
+ * Set the certificate in idctx->creds[cred_index] as the selected certificate.
167778
  */
167778
 krb5_error_code
167778
-crypto_cert_free_matching_data(krb5_context context,
167778
-                               pkinit_cert_matching_data *md)
167778
+crypto_cert_select(krb5_context context, pkinit_identity_crypto_context idctx,
167778
+                   size_t cred_index)
167778
 {
167778
-    krb5_principal p;
167778
-    int i;
167778
+    pkinit_cred_info ci = NULL;
167778
 
167778
-    if (md == NULL)
167778
-        return EINVAL;
167778
-    if (md->subject_dn)
167778
-        free(md->subject_dn);
167778
-    if (md->issuer_dn)
167778
-        free(md->issuer_dn);
167778
-    if (md->sans) {
167778
-        for (i = 0, p = md->sans[i]; p != NULL; p = md->sans[++i])
167778
-            krb5_free_principal(context, p);
167778
-        free(md->sans);
167778
-    }
167778
-    free(md);
167778
-    return 0;
167778
-}
167778
-
167778
-/*
167778
- * Make this matching certificate "the chosen one"
167778
- */
167778
-krb5_error_code
167778
-crypto_cert_select(krb5_context context,
167778
-                   pkinit_cert_matching_data *md)
167778
-{
167778
-    struct _pkinit_cert_data *cd;
167778
-    if (md == NULL)
167778
-        return EINVAL;
167778
-
167778
-    cd = (struct _pkinit_cert_data *)md->ch;
167778
-    if (cd == NULL || cd->magic != CERT_MAGIC)
167778
-        return EINVAL;
167778
+    if (cred_index >= MAX_CREDS_ALLOWED || idctx->creds[cred_index] == NULL)
167778
+        return ENOENT;
167778
 
167778
+    ci = idctx->creds[cred_index];
167778
     /* copy the selected cert into our id_cryptoctx */
167778
-    if (cd->idctx->my_certs != NULL) {
167778
-        sk_X509_pop_free(cd->idctx->my_certs, X509_free);
167778
-    }
167778
-    cd->idctx->my_certs = sk_X509_new_null();
167778
-    sk_X509_push(cd->idctx->my_certs, cd->cred->cert);
167778
-    free(cd->idctx->identity);
167778
+    if (idctx->my_certs != NULL)
167778
+        sk_X509_pop_free(idctx->my_certs, X509_free);
167778
+    idctx->my_certs = sk_X509_new_null();
167778
+    sk_X509_push(idctx->my_certs, ci->cert);
167778
+    free(idctx->identity);
167778
     /* hang on to the selected credential name */
167778
-    if (cd->idctx->creds[cd->index]->name != NULL)
167778
-        cd->idctx->identity = strdup(cd->idctx->creds[cd->index]->name);
167778
+    if (ci->name != NULL)
167778
+        idctx->identity = strdup(ci->name);
167778
     else
167778
-        cd->idctx->identity = NULL;
167778
-    cd->idctx->creds[cd->index]->cert = NULL;       /* Don't free it twice */
167778
-    cd->idctx->cert_index = 0;
167778
+        idctx->identity = NULL;
167778
 
167778
-    if (cd->idctx->pkcs11_method != 1) {
167778
-        cd->idctx->my_key = cd->cred->key;
167778
-        cd->idctx->creds[cd->index]->key = NULL;    /* Don't free it twice */
167778
+    ci->cert = NULL;       /* Don't free it twice */
167778
+    idctx->cert_index = 0;
167778
+    if (idctx->pkcs11_method != 1) {
167778
+        idctx->my_key = ci->key;
167778
+        ci->key = NULL;    /* Don't free it twice */
167778
     }
167778
 #ifndef WITHOUT_PKCS11
167778
     else {
167778
-        cd->idctx->cert_id = cd->cred->cert_id;
167778
-        cd->idctx->creds[cd->index]->cert_id = NULL; /* Don't free it twice */
167778
-        cd->idctx->cert_id_len = cd->cred->cert_id_len;
167778
+        idctx->cert_id = ci->cert_id;
167778
+        ci->cert_id = NULL; /* Don't free it twice */
167778
+        idctx->cert_id_len = ci->cert_id_len;
167778
     }
167778
 #endif
167778
     return 0;
167778
@@ -5380,15 +5312,12 @@ crypto_cert_select_default(krb5_context context,
167778
                            pkinit_identity_crypto_context id_cryptoctx)
167778
 {
167778
     krb5_error_code retval;
167778
-    int cert_count = 0;
167778
+    int cert_count;
167778
 
167778
-    retval = crypto_cert_get_count(context, plg_cryptoctx, req_cryptoctx,
167778
-                                   id_cryptoctx, &cert_count);
167778
-    if (retval) {
167778
-        pkiDebug("%s: crypto_cert_get_count error %d, %s\n",
167778
-                 __FUNCTION__, retval, error_message(retval));
167778
+    retval = crypto_cert_get_count(id_cryptoctx, &cert_count);
167778
+    if (retval)
167778
         goto errout;
167778
-    }
167778
+
167778
     if (cert_count != 1) {
167778
         TRACE_PKINIT_NO_DEFAULT_CERT(context, cert_count);
167778
         retval = EINVAL;
167778
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.h b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.h
167778
index 2fe357c5e..7411348fa 100644
167778
--- a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.h
167778
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.h
167778
@@ -115,23 +115,4 @@ struct _pkinit_req_crypto_context {
167778
     DH *dh;
167778
 };
167778
 
167778
-#define CERT_MAGIC 0x53534c43
167778
-struct _pkinit_cert_data {
167778
-    unsigned int magic;
167778
-    pkinit_plg_crypto_context plgctx;
167778
-    pkinit_req_crypto_context reqctx;
167778
-    pkinit_identity_crypto_context idctx;
167778
-    pkinit_cred_info cred;
167778
-    unsigned int index;	    /* Index of this cred in the creds[] array */
167778
-};
167778
-
167778
-#define ITER_MAGIC 0x53534c49
167778
-struct _pkinit_cert_iter_data {
167778
-    unsigned int magic;
167778
-    pkinit_plg_crypto_context plgctx;
167778
-    pkinit_req_crypto_context reqctx;
167778
-    pkinit_identity_crypto_context idctx;
167778
-    unsigned int index;
167778
-};
167778
-
167778
 #endif	/* _PKINIT_CRYPTO_OPENSSL_H */
167778
diff --git a/src/plugins/preauth/pkinit/pkinit_matching.c b/src/plugins/preauth/pkinit/pkinit_matching.c
167778
index cad4c2b9a..d6775dc4f 100644
167778
--- a/src/plugins/preauth/pkinit/pkinit_matching.c
167778
+++ b/src/plugins/preauth/pkinit/pkinit_matching.c
167778
@@ -544,7 +544,7 @@ check_all_certs(krb5_context context,
167778
                 rule_set *rs,   /* rule to check */
167778
                 pkinit_cert_matching_data **matchdata,
167778
                 int *match_found,
167778
-                pkinit_cert_matching_data **matching_cert)
167778
+                size_t *match_index)
167778
 {
167778
     krb5_error_code retval;
167778
     pkinit_cert_matching_data *md;
167778
@@ -553,12 +553,12 @@ check_all_certs(krb5_context context,
167778
     int total_cert_matches = 0;
167778
     rule_component *rc;
167778
     int certs_checked = 0;
167778
-    pkinit_cert_matching_data *save_match = NULL;
167778
+    size_t save_index = 0;
167778
 
167778
-    if (match_found == NULL || matching_cert == NULL)
167778
+    if (match_found == NULL || match_index == NULL)
167778
         return EINVAL;
167778
 
167778
-    *matching_cert = NULL;
167778
+    *match_index = 0;
167778
     *match_found = 0;
167778
 
167778
     pkiDebug("%s: matching rule relation is %s with %d components\n",
167778
@@ -590,7 +590,7 @@ check_all_certs(krb5_context context,
167778
                 pkiDebug("%s: cert matches rule (OR relation)\n",
167778
                          __FUNCTION__);
167778
                 total_cert_matches++;
167778
-                save_match = md;
167778
+                save_index = i;
167778
                 goto nextcert;
167778
             }
167778
             if (!comp_match && rs->relation == relation_and) {
167778
@@ -602,7 +602,7 @@ check_all_certs(krb5_context context,
167778
         if (rc == NULL && comp_match) {
167778
             pkiDebug("%s: cert matches rule (AND relation)\n", __FUNCTION__);
167778
             total_cert_matches++;
167778
-            save_match = md;
167778
+            save_index = i;
167778
         }
167778
     nextcert:
167778
         continue;
167778
@@ -611,7 +611,7 @@ check_all_certs(krb5_context context,
167778
              __FUNCTION__, certs_checked, total_cert_matches);
167778
     if (total_cert_matches == 1) {
167778
         *match_found = 1;
167778
-        *matching_cert = save_match;
167778
+        *match_index = save_index;
167778
     }
167778
 
167778
     retval = 0;
167778
@@ -621,111 +621,6 @@ check_all_certs(krb5_context context,
167778
     return retval;
167778
 }
167778
 
167778
-static krb5_error_code
167778
-free_all_cert_matching_data(krb5_context context,
167778
-                            pkinit_cert_matching_data **matchdata)
167778
-{
167778
-    krb5_error_code retval;
167778
-    pkinit_cert_matching_data *md;
167778
-    int i;
167778
-
167778
-    if (matchdata == NULL)
167778
-        return EINVAL;
167778
-
167778
-    for (i = 0, md = matchdata[i]; md != NULL; md = matchdata[++i]) {
167778
-        pkinit_cert_handle ch = md->ch;
167778
-        retval = crypto_cert_free_matching_data(context, md);
167778
-        if (retval) {
167778
-            pkiDebug("%s: crypto_cert_free_matching_data error %d, %s\n",
167778
-                     __FUNCTION__, retval, error_message(retval));
167778
-            goto cleanup;
167778
-        }
167778
-        retval = crypto_cert_release(context, ch);
167778
-        if (retval) {
167778
-            pkiDebug("%s: crypto_cert_release error %d, %s\n",
167778
-                     __FUNCTION__, retval, error_message(retval));
167778
-            goto cleanup;
167778
-        }
167778
-    }
167778
-    free(matchdata);
167778
-    retval = 0;
167778
-
167778
-cleanup:
167778
-    return retval;
167778
-}
167778
-
167778
-static krb5_error_code
167778
-obtain_all_cert_matching_data(krb5_context context,
167778
-                              pkinit_plg_crypto_context plg_cryptoctx,
167778
-                              pkinit_req_crypto_context req_cryptoctx,
167778
-                              pkinit_identity_crypto_context id_cryptoctx,
167778
-                              pkinit_cert_matching_data ***all_matching_data)
167778
-{
167778
-    krb5_error_code retval;
167778
-    int i, cert_count;
167778
-    pkinit_cert_iter_handle ih = NULL;
167778
-    pkinit_cert_handle ch;
167778
-    pkinit_cert_matching_data **matchdata = NULL;
167778
-
167778
-    retval = crypto_cert_get_count(context, plg_cryptoctx, req_cryptoctx,
167778
-                                   id_cryptoctx, &cert_count);
167778
-    if (retval) {
167778
-        pkiDebug("%s: crypto_cert_get_count error %d, %s\n",
167778
-                 __FUNCTION__, retval, error_message(retval));
167778
-        goto cleanup;
167778
-    }
167778
-
167778
-    pkiDebug("%s: crypto_cert_get_count says there are %d certs\n",
167778
-             __FUNCTION__, cert_count);
167778
-
167778
-    matchdata = calloc((size_t)cert_count + 1, sizeof(*matchdata));
167778
-    if (matchdata == NULL)
167778
-        return ENOMEM;
167778
-
167778
-    retval = crypto_cert_iteration_begin(context, plg_cryptoctx, req_cryptoctx,
167778
-                                         id_cryptoctx, &ih;;
167778
-    if (retval) {
167778
-        pkiDebug("%s: crypto_cert_iteration_begin returned %d, %s\n",
167778
-                 __FUNCTION__, retval, error_message(retval));
167778
-        goto cleanup;
167778
-    }
167778
-
167778
-    for (i = 0; i < cert_count; i++) {
167778
-        retval = crypto_cert_iteration_next(context, ih, &ch);
167778
-        if (retval) {
167778
-            if (retval == PKINIT_ITER_NO_MORE)
167778
-                pkiDebug("%s: We thought there were %d certs, but "
167778
-                         "crypto_cert_iteration_next stopped after %d?\n",
167778
-                         __FUNCTION__, cert_count, i);
167778
-            else
167778
-                pkiDebug("%s: crypto_cert_iteration_next error %d, %s\n",
167778
-                         __FUNCTION__, retval, error_message(retval));
167778
-            goto cleanup;
167778
-        }
167778
-
167778
-        retval = crypto_cert_get_matching_data(context, ch, &matchdata[i]);
167778
-        if (retval) {
167778
-            pkiDebug("%s: crypto_cert_get_matching_data error %d, %s\n",
167778
-                     __FUNCTION__, retval, error_message(retval));
167778
-            goto cleanup;
167778
-        }
167778
-
167778
-    }
167778
-
167778
-    *all_matching_data = matchdata;
167778
-    retval = 0;
167778
-cleanup:
167778
-    if (ih != NULL)
167778
-        crypto_cert_iteration_end(context, ih);
167778
-    if (retval) {
167778
-        if (matchdata != NULL)
167778
-            free_all_cert_matching_data(context, matchdata);
167778
-    }
167778
-    pkiDebug("%s: returning %d, certinfo %p\n",
167778
-             __FUNCTION__, retval, *all_matching_data);
167778
-    return retval;
167778
-}
167778
-
167778
 krb5_error_code
167778
 pkinit_cert_matching(krb5_context context,
167778
                      pkinit_plg_crypto_context plg_cryptoctx,
167778
@@ -740,7 +635,7 @@ pkinit_cert_matching(krb5_context context,
167778
     rule_set *rs = NULL;
167778
     int match_found = 0;
167778
     pkinit_cert_matching_data **matchdata = NULL;
167778
-    pkinit_cert_matching_data *the_matching_cert = NULL;
167778
+    size_t match_index = 0;
167778
 
167778
     /* If no matching rules, select the default cert and we're done */
167778
     pkinit_libdefault_strings(context, krb5_princ_realm(context, princ),
167778
@@ -777,7 +672,7 @@ pkinit_cert_matching(krb5_context context,
167778
          * until we are done.
167778
          */
167778
         if (matchdata == NULL) {
167778
-            retval = obtain_all_cert_matching_data(context, plg_cryptoctx,
167778
+            retval = crypto_cert_get_matching_data(context, plg_cryptoctx,
167778
                                                    req_cryptoctx, id_cryptoctx,
167778
                                                    &matchdata);
167778
             if (retval || matchdata == NULL) {
167778
@@ -790,7 +685,7 @@ pkinit_cert_matching(krb5_context context,
167778
 
167778
         retval = check_all_certs(context, plg_cryptoctx, req_cryptoctx,
167778
                                  id_cryptoctx, princ, rs, matchdata,
167778
-                                 &match_found, &the_matching_cert);
167778
+                                 &match_found, &match_index);
167778
         if (retval) {
167778
             pkiDebug("%s: Error %d, checking certs against rule '%s'\n",
167778
                      __FUNCTION__, retval, rules[x]);
167778
@@ -803,9 +698,9 @@ pkinit_cert_matching(krb5_context context,
167778
         }
167778
     }
167778
 
167778
-    if (match_found && the_matching_cert != NULL) {
167778
+    if (match_found) {
167778
         pkiDebug("%s: Selecting the matching cert!\n", __FUNCTION__);
167778
-        retval = crypto_cert_select(context, the_matching_cert);
167778
+        retval = crypto_cert_select(context, id_cryptoctx, match_index);
167778
         if (retval) {
167778
             pkiDebug("%s: crypto_cert_select error %d, %s\n",
167778
                      __FUNCTION__, retval, error_message(retval));
167778
@@ -818,12 +713,10 @@ pkinit_cert_matching(krb5_context context,
167778
     }
167778
 
167778
     retval = 0;
167778
+
167778
 cleanup:
167778
-    if (rules != NULL)
167778
-        profile_free_list(rules);
167778
-    if (rs != NULL)
167778
-        free_rule_set(context, rs);
167778
-    if (matchdata != NULL)
167778
-        free_all_cert_matching_data(context, matchdata);
167778
+    profile_free_list(rules);
167778
+    free_rule_set(context, rs);
167778
+    crypto_cert_free_matching_data_list(context, matchdata);
167778
     return retval;
167778
 }