Blame SOURCES/Use-OpenSSL-s-SSKDF-in-PKINIT-when-available.patch

a53771
From 8bbb492f2be1418e1e4bb2cf197414810dac9589 Mon Sep 17 00:00:00 2001
a53771
From: Robbie Harwood <rharwood@redhat.com>
a53771
Date: Fri, 20 Sep 2019 17:20:59 -0400
a53771
Subject: [PATCH] Use OpenSSL's SSKDF in PKINIT when available
a53771
a53771
Starting in 3.0, OpenSSL implements SSKDF, which is the basis of our
a53771
id-pkinit-kdf (RFC 8636).  Factor out common setup code around
a53771
other_info.  Adjust code to comply to existing style.
a53771
a53771
(cherry picked from commit 4376a22e41fb639be31daf81275a332d3f930996)
a53771
---
a53771
 .../preauth/pkinit/pkinit_crypto_openssl.c    | 294 +++++++++++-------
a53771
 1 file changed, 181 insertions(+), 113 deletions(-)
a53771
a53771
diff --git a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
a53771
index e1153344e..350c2118a 100644
a53771
--- a/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
a53771
+++ b/src/plugins/preauth/pkinit/pkinit_crypto_openssl.c
a53771
@@ -38,6 +38,12 @@
a53771
 #include <dirent.h>
a53771
 #include <arpa/inet.h>
a53771
 
a53771
+#ifdef HAVE_EVP_KDF_FETCH
a53771
+#include <openssl/core_names.h>
a53771
+#include <openssl/kdf.h>
a53771
+#include <openssl/params.h>
a53771
+#endif
a53771
+
a53771
 static krb5_error_code pkinit_init_pkinit_oids(pkinit_plg_crypto_context );
a53771
 static void pkinit_fini_pkinit_oids(pkinit_plg_crypto_context );
a53771
 
a53771
@@ -2294,15 +2300,16 @@ cleanup:
a53771
 }
a53771
 
a53771
 
a53771
-/**
a53771
+/*
a53771
  * Given an algorithm_identifier, this function returns the hash length
a53771
  * and EVP function associated with that algorithm.
a53771
+ *
a53771
+ * RFC 8636 defines a SHA384 variant, but we don't use it.
a53771
  */
a53771
 static krb5_error_code
a53771
-pkinit_alg_values(krb5_context context,
a53771
-                  const krb5_data *alg_id,
a53771
-                  size_t *hash_bytes,
a53771
-                  const EVP_MD *(**func)(void))
a53771
+pkinit_alg_values(krb5_context context, const krb5_data *alg_id,
a53771
+                  size_t *hash_bytes, const EVP_MD *(**func)(void),
a53771
+                  char **hash_name)
a53771
 {
a53771
     *hash_bytes = 0;
a53771
     *func = NULL;
a53771
@@ -2311,18 +2318,21 @@ pkinit_alg_values(krb5_context context,
a53771
                      krb5_pkinit_sha1_oid_len))) {
a53771
         *hash_bytes = 20;
a53771
         *func = &EVP_sha1;
a53771
+        *hash_name = strdup("SHA1");
a53771
         return 0;
a53771
     } else if ((alg_id->length == krb5_pkinit_sha256_oid_len) &&
a53771
                (0 == memcmp(alg_id->data, krb5_pkinit_sha256_oid,
a53771
                             krb5_pkinit_sha256_oid_len))) {
a53771
         *hash_bytes = 32;
a53771
         *func = &EVP_sha256;
a53771
+        *hash_name = strdup("SHA256");
a53771
         return 0;
a53771
     } else if ((alg_id->length == krb5_pkinit_sha512_oid_len) &&
a53771
                (0 == memcmp(alg_id->data, krb5_pkinit_sha512_oid,
a53771
                             krb5_pkinit_sha512_oid_len))) {
a53771
         *hash_bytes = 64;
a53771
         *func = &EVP_sha512;
a53771
+        *hash_name = strdup("SHA512");
a53771
         return 0;
a53771
     } else {
a53771
         krb5_set_error_message(context, KRB5_ERR_BAD_S2K_PARAMS,
a53771
@@ -2331,11 +2341,60 @@ pkinit_alg_values(krb5_context context,
a53771
     }
a53771
 } /* pkinit_alg_values() */
a53771
 
a53771
+#ifdef HAVE_EVP_KDF_FETCH
a53771
+static krb5_error_code
a53771
+openssl_sskdf(krb5_context context, size_t hash_bytes, krb5_data *key,
a53771
+              krb5_data *info, char *out, size_t out_len, char *digest)
a53771
+{
a53771
+    krb5_error_code ret;
a53771
+    EVP_KDF *kdf = NULL;
a53771
+    EVP_KDF_CTX *kctx = NULL;
a53771
+    OSSL_PARAM params[4];
a53771
+    size_t i = 0;
a53771
 
a53771
-/* pkinit_alg_agility_kdf() --
a53771
- * This function generates a key using the KDF described in
a53771
- * draft_ietf_krb_wg_pkinit_alg_agility-04.txt.  The algorithm is
a53771
- * described as follows:
a53771
+    if (digest == NULL) {
a53771
+        ret = oerr(context, ENOMEM,
a53771
+                   _("Failed to allocate space for digest algorithm name"));
a53771
+        goto done;
a53771
+    }
a53771
+
a53771
+    kdf = EVP_KDF_fetch(NULL, "SSKDF", NULL);
a53771
+    if (kdf == NULL) {
a53771
+        ret = oerr(context, KRB5_CRYPTO_INTERNAL, _("Failed to fetch SSKDF"));
a53771
+        goto done;
a53771
+    }
a53771
+
a53771
+    kctx = EVP_KDF_CTX_new(kdf);
a53771
+    if (!kctx) {
a53771
+        ret = oerr(context, KRB5_CRYPTO_INTERNAL,
a53771
+                   _("Failed to instantiate SSKDF"));
a53771
+        goto done;
a53771
+    }
a53771
+
a53771
+    params[i++] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
a53771
+                                                   digest, 0);
a53771
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
a53771
+                                                    key->data, key->length);
a53771
+    params[i++] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
a53771
+                                                    info->data, info->length);
a53771
+    params[i] = OSSL_PARAM_construct_end();
a53771
+    if (EVP_KDF_derive(kctx, (unsigned char *)out, out_len, params) <= 0) {
a53771
+        ret = oerr(context, KRB5_CRYPTO_INTERNAL,
a53771
+                   _("Failed to derive key using SSKDF"));
a53771
+        goto done;
a53771
+    }
a53771
+
a53771
+    ret = 0;
a53771
+done:
a53771
+    EVP_KDF_free(kdf);
a53771
+    EVP_KDF_CTX_free(kctx);
a53771
+    return ret;
a53771
+}
a53771
+#else
a53771
+/*
a53771
+ * Generate a key using the KDF described in RFC 8636, also known as SSKDF
a53771
+ * (single-step kdf).  Our caller precomputes `reps`, but otherwise the
a53771
+ * algorithm is as follows:
a53771
  *
a53771
  *     1.  reps = keydatalen (K) / hash length (H)
a53771
  *
a53771
@@ -2349,95 +2408,16 @@ pkinit_alg_values(krb5_context context,
a53771
  *
a53771
  *     4.  Set key = Hash1 || Hash2 || ... so that length of key is K bytes.
a53771
  */
a53771
-krb5_error_code
a53771
-pkinit_alg_agility_kdf(krb5_context context,
a53771
-                       krb5_data *secret,
a53771
-                       krb5_data *alg_oid,
a53771
-                       krb5_const_principal party_u_info,
a53771
-                       krb5_const_principal party_v_info,
a53771
-                       krb5_enctype enctype,
a53771
-                       krb5_data *as_req,
a53771
-                       krb5_data *pk_as_rep,
a53771
-                       krb5_keyblock *key_block)
a53771
+static krb5_error_code
a53771
+builtin_sskdf(krb5_context context, unsigned int reps, size_t hash_len,
a53771
+              const EVP_MD *(*EVP_func)(void), krb5_data *secret,
a53771
+              krb5_data *other_info, char *out, size_t out_len)
a53771
 {
a53771
-    krb5_error_code retval = 0;
a53771
+    krb5_error_code ret = 0;
a53771
 
a53771
-    unsigned int reps = 0;
a53771
-    uint32_t counter = 1;       /* Does this type work on Windows? */
a53771
+    uint32_t counter = 1;
a53771
     size_t offset = 0;
a53771
-    size_t hash_len = 0;
a53771
-    size_t rand_len = 0;
a53771
-    size_t key_len = 0;
a53771
-    krb5_data random_data;
a53771
-    krb5_sp80056a_other_info other_info_fields;
a53771
-    krb5_pkinit_supp_pub_info supp_pub_info_fields;
a53771
-    krb5_data *other_info = NULL;
a53771
-    krb5_data *supp_pub_info = NULL;
a53771
-    krb5_algorithm_identifier alg_id;
a53771
     EVP_MD_CTX *ctx = NULL;
a53771
-    const EVP_MD *(*EVP_func)(void);
a53771
-
a53771
-    /* initialize random_data here to make clean-up safe */
a53771
-    random_data.length = 0;
a53771
-    random_data.data = NULL;
a53771
-
a53771
-    /* allocate and initialize the key block */
a53771
-    key_block->magic = 0;
a53771
-    key_block->enctype = enctype;
a53771
-    if (0 != (retval = krb5_c_keylengths(context, enctype, &rand_len,
a53771
-                                         &key_len)))
a53771
-        goto cleanup;
a53771
-
a53771
-    random_data.length = rand_len;
a53771
-    key_block->length = key_len;
a53771
-
a53771
-    if (NULL == (key_block->contents = malloc(key_block->length))) {
a53771
-        retval = ENOMEM;
a53771
-        goto cleanup;
a53771
-    }
a53771
-
a53771
-    memset (key_block->contents, 0, key_block->length);
a53771
-
a53771
-    /* If this is anonymous pkinit, use the anonymous principle for party_u_info */
a53771
-    if (party_u_info && krb5_principal_compare_any_realm(context, party_u_info,
a53771
-                                                         krb5_anonymous_principal()))
a53771
-        party_u_info = (krb5_principal)krb5_anonymous_principal();
a53771
-
a53771
-    if (0 != (retval = pkinit_alg_values(context, alg_oid, &hash_len, &EVP_func)))
a53771
-        goto cleanup;
a53771
-
a53771
-    /* 1.  reps = keydatalen (K) / hash length (H) */
a53771
-    reps = key_block->length/hash_len;
a53771
-
a53771
-    /* ... and round up, if necessary */
a53771
-    if (key_block->length > (reps * hash_len))
a53771
-        reps++;
a53771
-
a53771
-    /* Allocate enough space in the random data buffer to hash directly into
a53771
-     * it, even if the last hash will make it bigger than the key length. */
a53771
-    if (NULL == (random_data.data = malloc(reps * hash_len))) {
a53771
-        retval = ENOMEM;
a53771
-        goto cleanup;
a53771
-    }
a53771
-
a53771
-    /* Encode the ASN.1 octet string for "SuppPubInfo" */
a53771
-    supp_pub_info_fields.enctype = enctype;
a53771
-    supp_pub_info_fields.as_req = *as_req;
a53771
-    supp_pub_info_fields.pk_as_rep = *pk_as_rep;
a53771
-    if (0 != ((retval = encode_krb5_pkinit_supp_pub_info(&supp_pub_info_fields,
a53771
-                                                         &supp_pub_info))))
a53771
-        goto cleanup;
a53771
-
a53771
-    /* Now encode the ASN.1 octet string for "OtherInfo" */
a53771
-    memset(&alg_id, 0, sizeof alg_id);
a53771
-    alg_id.algorithm = *alg_oid; /*alias*/
a53771
-
a53771
-    other_info_fields.algorithm_identifier = alg_id;
a53771
-    other_info_fields.party_u_info = (krb5_principal) party_u_info;
a53771
-    other_info_fields.party_v_info = (krb5_principal) party_v_info;
a53771
-    other_info_fields.supp_pub_info = *supp_pub_info;
a53771
-    if (0 != (retval = encode_krb5_sp80056a_other_info(&other_info_fields, &other_info)))
a53771
-        goto cleanup;
a53771
 
a53771
     /* 2.  Initialize a 32-bit, big-endian bit string counter as 1.
a53771
      * 3.  For i = 1 to reps by 1, do the following:
a53771
@@ -2450,7 +2430,7 @@ pkinit_alg_agility_kdf(krb5_context context,
a53771
 
a53771
         ctx = EVP_MD_CTX_new();
a53771
         if (ctx == NULL) {
a53771
-            retval = KRB5_CRYPTO_INTERNAL;
a53771
+            ret = KRB5_CRYPTO_INTERNAL;
a53771
             goto cleanup;
a53771
         }
a53771
 
a53771
@@ -2458,7 +2438,7 @@ pkinit_alg_agility_kdf(krb5_context context,
a53771
         if (!EVP_DigestInit(ctx, EVP_func())) {
a53771
             krb5_set_error_message(context, KRB5_CRYPTO_INTERNAL,
a53771
                                    "Call to OpenSSL EVP_DigestInit() returned an error.");
a53771
-            retval = KRB5_CRYPTO_INTERNAL;
a53771
+            ret = KRB5_CRYPTO_INTERNAL;
a53771
             goto cleanup;
a53771
         }
a53771
 
a53771
@@ -2467,15 +2447,16 @@ pkinit_alg_agility_kdf(krb5_context context,
a53771
             !EVP_DigestUpdate(ctx, other_info->data, other_info->length)) {
a53771
             krb5_set_error_message(context, KRB5_CRYPTO_INTERNAL,
a53771
                                    "Call to OpenSSL EVP_DigestUpdate() returned an error.");
a53771
-            retval = KRB5_CRYPTO_INTERNAL;
a53771
+            ret = KRB5_CRYPTO_INTERNAL;
a53771
             goto cleanup;
a53771
         }
a53771
 
a53771
-        /* 4.  Set key = Hash1 || Hash2 || ... so that length of key is K bytes. */
a53771
-        if (!EVP_DigestFinal(ctx, (uint8_t *)random_data.data + offset, &s)) {
a53771
+        /* 4.  Set key = Hash1 || Hash2 || ... so that length of key is K
a53771
+         * bytes. */
a53771
+        if (!EVP_DigestFinal(ctx, (unsigned char *)out + offset, &s)) {
a53771
             krb5_set_error_message(context, KRB5_CRYPTO_INTERNAL,
a53771
                                    "Call to OpenSSL EVP_DigestUpdate() returned an error.");
a53771
-            retval = KRB5_CRYPTO_INTERNAL;
a53771
+            ret = KRB5_CRYPTO_INTERNAL;
a53771
             goto cleanup;
a53771
         }
a53771
         offset += s;
a53771
@@ -2484,26 +2465,113 @@ pkinit_alg_agility_kdf(krb5_context context,
a53771
         EVP_MD_CTX_free(ctx);
a53771
         ctx = NULL;
a53771
     }
a53771
-
a53771
-    retval = krb5_c_random_to_key(context, enctype, &random_data,
a53771
-                                  key_block);
a53771
-
a53771
 cleanup:
a53771
     EVP_MD_CTX_free(ctx);
a53771
+    return ret;
a53771
+} /* builtin_sskdf() */
a53771
+#endif /* HAVE_EVP_KDF_FETCH */
a53771
 
a53771
-    /* If this has been an error, free the allocated key_block, if any */
a53771
-    if (retval) {
a53771
-        krb5_free_keyblock_contents(context, key_block);
a53771
+/* id-pkinit-kdf family, as specified by RFC 8636. */
a53771
+krb5_error_code
a53771
+pkinit_alg_agility_kdf(krb5_context context, krb5_data *secret,
a53771
+                       krb5_data *alg_oid, krb5_const_principal party_u_info,
a53771
+                       krb5_const_principal party_v_info,
a53771
+                       krb5_enctype enctype, krb5_data *as_req,
a53771
+                       krb5_data *pk_as_rep, krb5_keyblock *key_block)
a53771
+{
a53771
+    krb5_error_code ret;
a53771
+    size_t hash_len = 0, rand_len = 0, key_len = 0;
a53771
+    const EVP_MD *(*EVP_func)(void);
a53771
+    krb5_sp80056a_other_info other_info_fields;
a53771
+    krb5_pkinit_supp_pub_info supp_pub_info_fields;
a53771
+    krb5_data *other_info = NULL, *supp_pub_info = NULL;
a53771
+    krb5_data random_data = empty_data();
a53771
+    krb5_algorithm_identifier alg_id;
a53771
+    unsigned int reps;
a53771
+    char *hash_name = NULL;
a53771
+
a53771
+    /* Allocate and initialize the key block. */
a53771
+    key_block->magic = 0;
a53771
+    key_block->enctype = enctype;
a53771
+
a53771
+    /* Use separate variables to avoid alignment restriction problems. */
a53771
+    ret = krb5_c_keylengths(context, enctype, &rand_len, &key_len);
a53771
+    if (ret)
a53771
+        goto cleanup;
a53771
+    random_data.length = rand_len;
a53771
+    key_block->length = key_len;
a53771
+
a53771
+    key_block->contents = k5calloc(key_block->length, 1, &ret;;
a53771
+    if (key_block->contents == NULL)
a53771
+        goto cleanup;
a53771
+
a53771
+    /* If this is anonymous pkinit, use the anonymous principle for
a53771
+     * party_u_info. */
a53771
+    if (party_u_info &&
a53771
+        krb5_principal_compare_any_realm(context, party_u_info,
a53771
+                                         krb5_anonymous_principal())) {
a53771
+        party_u_info = (krb5_principal)krb5_anonymous_principal();
a53771
     }
a53771
 
a53771
-    /* free other allocated resources, either way */
a53771
-    if (random_data.data)
a53771
-        free(random_data.data);
a53771
+    ret = pkinit_alg_values(context, alg_oid, &hash_len, &EVP_func,
a53771
+                            &hash_name);
a53771
+    if (ret)
a53771
+        goto cleanup;
a53771
+
a53771
+    /* 1.  reps = keydatalen (K) / hash length (H) */
a53771
+    reps = key_block->length / hash_len;
a53771
+
a53771
+    /* ... and round up, if necessary. */
a53771
+    if (key_block->length > (reps * hash_len))
a53771
+        reps++;
a53771
+
a53771
+    /* Allocate enough space in the random data buffer to hash directly into
a53771
+     * it, even if the last hash will make it bigger than the key length. */
a53771
+    random_data.data = k5alloc(reps * hash_len, &ret;;
a53771
+    if (random_data.data == NULL)
a53771
+        goto cleanup;
a53771
+
a53771
+    /* Encode the ASN.1 octet string for "SuppPubInfo". */
a53771
+    supp_pub_info_fields.enctype = enctype;
a53771
+    supp_pub_info_fields.as_req = *as_req;
a53771
+    supp_pub_info_fields.pk_as_rep = *pk_as_rep;
a53771
+    ret = encode_krb5_pkinit_supp_pub_info(&supp_pub_info_fields,
a53771
+                                           &supp_pub_info);
a53771
+    if (ret)
a53771
+        goto cleanup;
a53771
+
a53771
+    /* Now encode the ASN.1 octet string for "OtherInfo". */
a53771
+    memset(&alg_id, 0, sizeof(alg_id));
a53771
+    alg_id.algorithm = *alg_oid;
a53771
+    other_info_fields.algorithm_identifier = alg_id;
a53771
+    other_info_fields.party_u_info = (krb5_principal)party_u_info;
a53771
+    other_info_fields.party_v_info = (krb5_principal)party_v_info;
a53771
+    other_info_fields.supp_pub_info = *supp_pub_info;
a53771
+    ret = encode_krb5_sp80056a_other_info(&other_info_fields, &other_info);
a53771
+    if (ret)
a53771
+        goto cleanup;
a53771
+
a53771
+#ifdef HAVE_EVP_KDF_FETCH
a53771
+    ret = openssl_sskdf(context, hash_len, secret, other_info,
a53771
+                        random_data.data, key_block->length, hash_name);
a53771
+#else
a53771
+    ret = builtin_sskdf(context, reps, hash_len, EVP_func, secret,
a53771
+                        other_info, random_data.data, key_block->length);
a53771
+#endif
a53771
+    if (ret)
a53771
+        goto cleanup;
a53771
+
a53771
+    ret = krb5_c_random_to_key(context, enctype, &random_data, key_block);
a53771
+cleanup:
a53771
+    if (ret)
a53771
+        krb5_free_keyblock_contents(context, key_block);
a53771
+
a53771
+    free(hash_name);
a53771
+    zapfree(random_data.data, random_data.length);
a53771
     krb5_free_data(context, other_info);
a53771
     krb5_free_data(context, supp_pub_info);
a53771
-
a53771
-    return retval;
a53771
-} /*pkinit_alg_agility_kdf() */
a53771
+    return ret;
a53771
+}
a53771
 
a53771
 /* Call DH_compute_key() and ensure that we left-pad short results instead of
a53771
  * leaving junk bytes at the end of the buffer. */