Utility functions to move allocations from k5buf/krb5_data to gss_buffer_t
authorSam Hartman <hartmans@mit.edu>
Fri, 14 Oct 2011 14:40:05 +0000 (14:40 +0000)
committerSam Hartman <hartmans@mit.edu>
Fri, 14 Oct 2011 14:40:05 +0000 (14:40 +0000)
On Unix, these simply move the buffer pointer, but on windows they need to
reallocated with gssalloc_malloc and coied since the gss_buffer_t may need
to be freed in a separate module with potentially mismatched c runtime.

Also fix a mismatched parameter warning in generic_gss_copy_oid_set().

Signed-off-by: Kevin Wasserman <kevin.wasserman@painless-security.com>
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25331 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/gssapi/generic/gssapiP_generic.h
src/lib/gssapi/krb5/gssapiP_krb5.h

index e084b81bd051d5079c4e97713e6c3e59a9ba3c21..1124c51e591f2cdb7cce363c6daafa2b694a702f 100644 (file)
@@ -41,6 +41,7 @@
 
 #include "gssapi_generic.h"
 #include "gssapi_ext.h"
+#include <gssapi/gssapi_alloc.h>
 #include "gssapi_err_generic.h"
 #include <errno.h>
 
@@ -264,6 +265,42 @@ int gssint_mecherrmap_get(OM_uint32 minor, gss_OID mech_oid,
                           OM_uint32 *mech_minor);
 OM_uint32 gssint_mecherrmap_map_errcode(OM_uint32 errcode);
 
+/*
+ * Transfer contents of a k5buf to a gss_buffer and invalidate the source
+ * On unix, this is a simple pointer copy
+ * On windows, memory is reallocated and copied.
+ */
+static inline OM_uint32
+k5buf_to_gss(OM_uint32 *minor,
+             struct k5buf *input_k5buf,
+             gss_buffer_t output_buffer)
+{
+    OM_uint32 status = GSS_S_COMPLETE;
+    char *bp = krb5int_buf_data(input_k5buf);
+    output_buffer->length = krb5int_buf_len(input_k5buf)+1;
+#ifdef _WIN32
+    if (output_buffer->length > 0) {
+        output_buffer->value = gssalloc_malloc(output_buffer->length);
+        if (output_buffer->value) {
+            memcpy(output_buffer->value, bp, output_buffer->length);
+        } else {
+            status = GSS_S_FAILURE;
+            *minor = ENOMEM;
+        }
+    } else {
+        output_buffer->value = NULL;
+    }
+    krb5int_free_buf(input_k5buf);
+#else
+    output_buffer->value = bp;
+    /*
+     * it would be nice to invalidate input_k5buf here
+     * but there is no api for that currently...
+     */
+#endif
+    return status;
+}
+
 OM_uint32 generic_gss_create_empty_buffer_set
 (OM_uint32 * /*minor_status*/,
             gss_buffer_set_t * /*buffer_set*/);
@@ -279,7 +316,7 @@ OM_uint32 generic_gss_release_buffer_set
 
 OM_uint32 generic_gss_copy_oid_set
 (OM_uint32 *, /* minor_status */
-            const gss_OID_set_desc *, /* const oidset*/
+            const gss_OID_set_desc * const /*oidset*/,
             gss_OID_set * /*new_oidset*/);
 
 extern gss_OID_set gss_ma_known_attrs;
index 08155e820f029b860065808c93d71951b7725053..016a2e621524ce074b8246f9c78eedb4a4fb21e0 100644 (file)
@@ -1186,6 +1186,34 @@ iakerb_verify_finished(krb5_context context,
                        const krb5_data *conv,
                        const krb5_data *finished);
 
+/*
+ * Transfer contents of a krb5_data to a gss_buffer and invalidate the source
+ * On unix, this is a simple pointer copy
+ * On windows, memory is reallocated and copied.
+ */
+static inline krb5_error_code
+data_to_gss(krb5_data *input_k5data, gss_buffer_t output_buffer)
+{
+    krb5_error_code code = 0;
+    output_buffer->length = input_k5data->length;
+#ifdef _WIN32
+    if (output_buffer->length > 0) {
+        output_buffer->value = gssalloc_malloc(output_buffer->length);
+        if (output_buffer->value)
+            memcpy(output_buffer->value, input_k5data->data, output_buffer->length);
+        else
+            code = ENOMEM;
+    } else {
+        output_buffer->value = NULL;
+    }
+    free(input_k5data->data);
+#else
+    output_buffer->value = input_k5data->data;
+#endif
+    *input_k5data = empty_data();
+    return code;
+}
+
 #define KRB5_GSS_EXTS_IAKERB_FINISHED 1
 
 #endif /* _GSSAPIP_KRB5_H_ */