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

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