Blame SOURCES/Add-APIs-for-marshalling-credentials.patch

cb4cef
From 3d11179707923b033fa413387a33296b673ff52d Mon Sep 17 00:00:00 2001
cb4cef
From: Robbie Harwood <rharwood@redhat.com>
cb4cef
Date: Thu, 14 Jan 2021 18:13:09 -0500
cb4cef
Subject: [PATCH] Add APIs for marshalling credentials
cb4cef
cb4cef
Faciliate KCM daemon implementations by providing functions to
cb4cef
deserialize and reserialize credentials in the FILE v4 format.
cb4cef
cb4cef
[ghudson@mit.edu: minor editorial changes]
cb4cef
cb4cef
ticket: 8980 (new)
cb4cef
(cherry picked from commit 18ea3bd2fca55b789b7de9c663624bc11d348fa6)
cb4cef
---
cb4cef
 doc/appdev/refs/api/index.rst   |  2 ++
cb4cef
 src/include/krb5/krb5.hin       | 36 ++++++++++++++++++++++
cb4cef
 src/lib/krb5/ccache/ccmarshal.c | 53 +++++++++++++++++++++++++++++++++
cb4cef
 src/lib/krb5/ccache/t_marshal.c | 15 +++++++++-
cb4cef
 src/lib/krb5/libkrb5.exports    |  2 ++
cb4cef
 src/lib/krb5_32.def             |  4 +++
cb4cef
 6 files changed, 111 insertions(+), 1 deletion(-)
cb4cef
cb4cef
diff --git a/doc/appdev/refs/api/index.rst b/doc/appdev/refs/api/index.rst
cb4cef
index 727d9b492..9e03fd386 100644
cb4cef
--- a/doc/appdev/refs/api/index.rst
cb4cef
+++ b/doc/appdev/refs/api/index.rst
cb4cef
@@ -232,6 +232,7 @@ Rarely used public interfaces
cb4cef
    krb5_kt_remove_entry.rst
cb4cef
    krb5_kt_start_seq_get.rst
cb4cef
    krb5_make_authdata_kdc_issued.rst
cb4cef
+   krb5_marshal_credentials.rst
cb4cef
    krb5_merge_authdata.rst
cb4cef
    krb5_mk_1cred.rst
cb4cef
    krb5_mk_error.rst
cb4cef
@@ -285,6 +286,7 @@ Rarely used public interfaces
cb4cef
    krb5_tkt_creds_get_times.rst
cb4cef
    krb5_tkt_creds_init.rst
cb4cef
    krb5_tkt_creds_step.rst
cb4cef
+   krb5_unmarshal_credentials.rst
cb4cef
    krb5_verify_init_creds.rst
cb4cef
    krb5_verify_init_creds_opt_init.rst
cb4cef
    krb5_verify_init_creds_opt_set_ap_req_nofail.rst
cb4cef
diff --git a/src/include/krb5/krb5.hin b/src/include/krb5/krb5.hin
cb4cef
index 63e67a2ba..c26dde535 100644
cb4cef
--- a/src/include/krb5/krb5.hin
cb4cef
+++ b/src/include/krb5/krb5.hin
cb4cef
@@ -3125,6 +3125,42 @@ krb5_get_credentials(krb5_context context, krb5_flags options,
cb4cef
                      krb5_ccache ccache, krb5_creds *in_creds,
cb4cef
                      krb5_creds **out_creds);
cb4cef
 
cb4cef
+/**
cb4cef
+ * Serialize a @c krb5_creds object.
cb4cef
+ *
cb4cef
+ * @param [in]  context         Library context
cb4cef
+ * @param [in]  creds           The credentials object to serialize
cb4cef
+ * @param [out] data_out        The serialized credentials
cb4cef
+ *
cb4cef
+ * Serialize @a creds in the format used by the FILE ccache format (vesion 4)
cb4cef
+ * and KCM ccache protocol.
cb4cef
+ *
cb4cef
+ * Use krb5_free_data() to free @a data_out when it is no longer needed.
cb4cef
+ *
cb4cef
+ * @retval 0 Success; otherwise - Kerberos error codes
cb4cef
+ */
cb4cef
+krb5_error_code KRB5_CALLCONV
cb4cef
+krb5_marshal_credentials(krb5_context context, krb5_creds *in_creds,
cb4cef
+                         krb5_data **data_out);
cb4cef
+
cb4cef
+/**
cb4cef
+ * Deserialize a @c krb5_creds object.
cb4cef
+ *
cb4cef
+ * @param [in]  context         Library context
cb4cef
+ * @param [in]  data            The serialized credentials
cb4cef
+ * @param [out] creds_out       The resulting creds object
cb4cef
+ *
cb4cef
+ * Deserialize @a data to credentials in the format used by the FILE ccache
cb4cef
+ * format (vesion 4) and KCM ccache protocol.
cb4cef
+ *
cb4cef
+ * Use krb5_free_creds() to free @a creds_out when it is no longer needed.
cb4cef
+ *
cb4cef
+ * @retval 0 Success; otherwise - Kerberos error codes
cb4cef
+ */
cb4cef
+krb5_error_code KRB5_CALLCONV
cb4cef
+krb5_unmarshal_credentials(krb5_context context, const krb5_data *data,
cb4cef
+                           krb5_creds **creds_out);
cb4cef
+
cb4cef
 /** @deprecated Replaced by krb5_get_validated_creds. */
cb4cef
 krb5_error_code KRB5_CALLCONV
cb4cef
 krb5_get_credentials_validate(krb5_context context, krb5_flags options,
cb4cef
diff --git a/src/lib/krb5/ccache/ccmarshal.c b/src/lib/krb5/ccache/ccmarshal.c
cb4cef
index ae634ccab..ab284e721 100644
cb4cef
--- a/src/lib/krb5/ccache/ccmarshal.c
cb4cef
+++ b/src/lib/krb5/ccache/ccmarshal.c
cb4cef
@@ -515,3 +515,56 @@ k5_marshal_mcred(struct k5buf *buf, krb5_creds *mcred)
cb4cef
     if (mcred->second_ticket.length > 0)
cb4cef
         put_data(buf, version, &mcred->second_ticket);
cb4cef
 }
cb4cef
+
cb4cef
+krb5_error_code KRB5_CALLCONV
cb4cef
+krb5_marshal_credentials(krb5_context context, krb5_creds *in_creds,
cb4cef
+                         krb5_data **data_out)
cb4cef
+{
cb4cef
+    krb5_error_code ret;
cb4cef
+    krb5_data *data;
cb4cef
+    struct k5buf buf;
cb4cef
+
cb4cef
+    *data_out = NULL;
cb4cef
+
cb4cef
+    data = k5alloc(sizeof(krb5_data), &ret;;
cb4cef
+    if (ret)
cb4cef
+        return ret;
cb4cef
+
cb4cef
+    k5_buf_init_dynamic(&buf;;
cb4cef
+    k5_marshal_cred(&buf, 4, in_creds);
cb4cef
+
cb4cef
+    ret = k5_buf_status(&buf;;
cb4cef
+    if (ret) {
cb4cef
+        free(data);
cb4cef
+        return ret;
cb4cef
+    }
cb4cef
+
cb4cef
+    /* Steal payload from buf. */
cb4cef
+    *data = make_data(buf.data, buf.len);
cb4cef
+    *data_out = data;
cb4cef
+    return 0;
cb4cef
+}
cb4cef
+
cb4cef
+krb5_error_code KRB5_CALLCONV
cb4cef
+krb5_unmarshal_credentials(krb5_context context, const krb5_data *data,
cb4cef
+                           krb5_creds **creds_out)
cb4cef
+{
cb4cef
+    krb5_error_code ret;
cb4cef
+    krb5_creds *creds;
cb4cef
+
cb4cef
+    *creds_out = NULL;
cb4cef
+
cb4cef
+    creds = k5alloc(sizeof(krb5_creds), &ret;;
cb4cef
+    if (ret)
cb4cef
+        return ret;
cb4cef
+
cb4cef
+    ret = k5_unmarshal_cred((unsigned char *)data->data, data->length, 4,
cb4cef
+                            creds);
cb4cef
+    if (ret) {
cb4cef
+        free(creds);
cb4cef
+        return ret;
cb4cef
+    }
cb4cef
+
cb4cef
+    *creds_out = creds;
cb4cef
+    return 0;
cb4cef
+}
cb4cef
diff --git a/src/lib/krb5/ccache/t_marshal.c b/src/lib/krb5/ccache/t_marshal.c
cb4cef
index bd0284afa..96e0931a2 100644
cb4cef
--- a/src/lib/krb5/ccache/t_marshal.c
cb4cef
+++ b/src/lib/krb5/ccache/t_marshal.c
cb4cef
@@ -268,13 +268,14 @@ main(int argc, char **argv)
cb4cef
     krb5_context context;
cb4cef
     krb5_ccache cache;
cb4cef
     krb5_principal princ;
cb4cef
-    krb5_creds cred1, cred2;
cb4cef
+    krb5_creds cred1, cred2, *alloc_cred;
cb4cef
     krb5_cc_cursor cursor;
cb4cef
     const char *filename;
cb4cef
     char *ccname, filebuf[256];
cb4cef
     int version, fd;
cb4cef
     const struct test *t;
cb4cef
     struct k5buf buf;
cb4cef
+    krb5_data ser_data, *alloc_data;
cb4cef
 
cb4cef
     if (argc != 2)
cb4cef
         abort();
cb4cef
@@ -285,6 +286,18 @@ main(int argc, char **argv)
cb4cef
     if (krb5_init_context(&context) != 0)
cb4cef
         abort();
cb4cef
 
cb4cef
+    /* Test public functions for unmarshalling and marshalling. */
cb4cef
+    ser_data = make_data((char *)tests[3].cred1, tests[3].cred1len);
cb4cef
+    if (krb5_unmarshal_credentials(context, &ser_data, &alloc_cred) != 0)
cb4cef
+        abort();
cb4cef
+    verify_cred1(alloc_cred);
cb4cef
+    if (krb5_marshal_credentials(context, alloc_cred, &alloc_data) != 0)
cb4cef
+        abort();
cb4cef
+    assert(alloc_data->length == tests[3].cred1len);
cb4cef
+    assert(memcmp(tests[3].cred1, alloc_data->data, alloc_data->length) == 0);
cb4cef
+    krb5_free_data(context, alloc_data);
cb4cef
+    krb5_free_creds(context, alloc_cred);
cb4cef
+
cb4cef
     for (version = FIRST_VERSION; version <= 4; version++) {
cb4cef
         t = &tests[version - 1];
cb4cef
 
cb4cef
diff --git a/src/lib/krb5/libkrb5.exports b/src/lib/krb5/libkrb5.exports
cb4cef
index 2d9d56530..adbfa332b 100644
cb4cef
--- a/src/lib/krb5/libkrb5.exports
cb4cef
+++ b/src/lib/krb5/libkrb5.exports
cb4cef
@@ -489,6 +489,7 @@ krb5_lock_file
cb4cef
 krb5_make_authdata_kdc_issued
cb4cef
 krb5_make_full_ipaddr
cb4cef
 krb5_make_fulladdr
cb4cef
+krb5_marshal_credentials
cb4cef
 krb5_mcc_ops
cb4cef
 krb5_merge_authdata
cb4cef
 krb5_mk_1cred
cb4cef
@@ -592,6 +593,7 @@ krb5_timeofday
cb4cef
 krb5_timestamp_to_sfstring
cb4cef
 krb5_timestamp_to_string
cb4cef
 krb5_unlock_file
cb4cef
+krb5_unmarshal_credentials
cb4cef
 krb5_unpack_full_ipaddr
cb4cef
 krb5_unparse_name
cb4cef
 krb5_unparse_name_ext
cb4cef
diff --git a/src/lib/krb5_32.def b/src/lib/krb5_32.def
cb4cef
index 4953907aa..60b8dd311 100644
cb4cef
--- a/src/lib/krb5_32.def
cb4cef
+++ b/src/lib/krb5_32.def
cb4cef
@@ -503,3 +503,7 @@ EXPORTS
cb4cef
 ; new in 1.19
cb4cef
 	k5_cc_store_primary_cred			@470 ; PRIVATE
cb4cef
 	k5_kt_have_match				@471 ; PRIVATE GSSAPI
cb4cef
+
cb4cef
+; new in 1.20
cb4cef
+	krb5_marshal_credentials			@472
cb4cef
+	krb5_unmarshal_credentials			@473