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

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