xref: /titanic_53/usr/src/lib/libgss/g_glue.c (revision ba7b222e36bac28710a7f43739283302b617e7f5)
17c478bd9Sstevel@tonic-gate /*
2*ba7b222eSGlenn Barry  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*ba7b222eSGlenn Barry #include "mglueP.h"
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate #include <stdio.h>
9*ba7b222eSGlenn Barry #ifdef HAVE_STDLIB_H
107c478bd9Sstevel@tonic-gate #include <stdlib.h>
11*ba7b222eSGlenn Barry #endif
12*ba7b222eSGlenn Barry #include <string.h>
137c478bd9Sstevel@tonic-gate #include <errno.h>
147c478bd9Sstevel@tonic-gate 
15*ba7b222eSGlenn Barry #include "k5-platform-store_32.h"
16*ba7b222eSGlenn Barry #include "k5-platform-store_16.h"
17*ba7b222eSGlenn Barry /*
18*ba7b222eSGlenn Barry  * SUNW17PACresync
19*ba7b222eSGlenn Barry  * MIT has diff names for these GSS utilities.  Solaris needs to change
20*ba7b222eSGlenn Barry  * them globally to get in sync w/MIT.
21*ba7b222eSGlenn Barry  * Revisit for full 1.7 resync.
22*ba7b222eSGlenn Barry  */
23*ba7b222eSGlenn Barry #define gssint_get_modOptions __gss_get_modOptions
24*ba7b222eSGlenn Barry #define gssint_der_length_size der_length_size
25*ba7b222eSGlenn Barry #define gssint_get_der_length get_der_length
26*ba7b222eSGlenn Barry #define gssint_put_der_length put_der_length
27*ba7b222eSGlenn Barry #define gssint_get_mechanism __gss_get_mechanism
28*ba7b222eSGlenn Barry #define gssint_get_mechanism_cred __gss_get_mechanism_cred
29*ba7b222eSGlenn Barry #define gssint_copy_oid_set gss_copy_oid_set
30*ba7b222eSGlenn Barry #define gssint_get_mech_type __gss_get_mech_type
31*ba7b222eSGlenn Barry #define gssint_export_internal_name __gss_export_internal_name
32*ba7b222eSGlenn Barry #define gssint_release_internal_name __gss_release_internal_name
33*ba7b222eSGlenn Barry #define gssint_convert_name_to_union_name __gss_convert_name_to_union_name
34*ba7b222eSGlenn Barry #define gssint_import_internal_name __gss_import_internal_name
35*ba7b222eSGlenn Barry #define gssint_display_internal_name __gss_display_internal_name
36*ba7b222eSGlenn Barry 
37*ba7b222eSGlenn Barry 
387c478bd9Sstevel@tonic-gate #define	MSO_BIT (8*(sizeof (int) - 1))  /* Most significant octet bit */
397c478bd9Sstevel@tonic-gate 
40*ba7b222eSGlenn Barry extern gss_mechanism *gssint_mechs_array;
41*ba7b222eSGlenn Barry 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * This file contains the support routines for the glue layer.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * get_der_length: Givin a pointer to a buffer that contains a DER encoded
487c478bd9Sstevel@tonic-gate  * length, decode the length updating the buffer to point to the character
497c478bd9Sstevel@tonic-gate  * after the DER encoding. The parameter bytes will point to the number of
507c478bd9Sstevel@tonic-gate  * bytes that made up the DER encoding of the length originally pointed to
517c478bd9Sstevel@tonic-gate  * by the buffer. Note we return -1 on error.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate int
54*ba7b222eSGlenn Barry gssint_get_der_length(unsigned char **buf, unsigned int buf_len, unsigned int *bytes)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate     /* p points to the beginning of the buffer */
577c478bd9Sstevel@tonic-gate     unsigned char *p = *buf;
587c478bd9Sstevel@tonic-gate     int length, new_length;
59*ba7b222eSGlenn Barry     unsigned int octets;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate     if (buf_len < 1)
627c478bd9Sstevel@tonic-gate 	return (-1);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate     /* We should have at least one byte */
657c478bd9Sstevel@tonic-gate     *bytes = 1;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate     /*
687c478bd9Sstevel@tonic-gate      * If the High order bit is not set then the length is just the value
697c478bd9Sstevel@tonic-gate      * of *p.
707c478bd9Sstevel@tonic-gate      */
717c478bd9Sstevel@tonic-gate     if (*p < 128) {
727c478bd9Sstevel@tonic-gate 	*buf = p+1;	/* Advance the buffer */
737c478bd9Sstevel@tonic-gate 	return (*p);		/* return the length */
747c478bd9Sstevel@tonic-gate     }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate     /*
777c478bd9Sstevel@tonic-gate      * if the High order bit is set, then the low order bits represent
787c478bd9Sstevel@tonic-gate      * the number of bytes that contain the DER encoding of the length.
797c478bd9Sstevel@tonic-gate      */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate     octets = *p++ & 0x7f;
827c478bd9Sstevel@tonic-gate     *bytes += octets;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate     /* See if the supplied buffer contains enough bytes for the length. */
857c478bd9Sstevel@tonic-gate     if (octets > buf_len - 1)
867c478bd9Sstevel@tonic-gate 	return (-1);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate     /*
897c478bd9Sstevel@tonic-gate      * Calculate a multibyte length. The length is encoded as an
907c478bd9Sstevel@tonic-gate      * unsigned integer base 256.
917c478bd9Sstevel@tonic-gate      */
927c478bd9Sstevel@tonic-gate     for (length = 0; octets; octets--) {
937c478bd9Sstevel@tonic-gate 	new_length = (length << 8) + *p++;
947c478bd9Sstevel@tonic-gate 	if (new_length < length)  /* overflow */
957c478bd9Sstevel@tonic-gate 	    return (-1);
967c478bd9Sstevel@tonic-gate 	length = new_length;
977c478bd9Sstevel@tonic-gate     }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate     *buf = p; /* Advance the buffer */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate     return (length);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * der_length_size: Return the number of bytes to encode a given length.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate unsigned int
108*ba7b222eSGlenn Barry gssint_der_length_size(unsigned int len)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate     int i;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate     if (len < 128)
1137c478bd9Sstevel@tonic-gate 	return (1);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate     for (i = 0; len; i++) {
1167c478bd9Sstevel@tonic-gate 	len >>= 8;
1177c478bd9Sstevel@tonic-gate     }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate     return (i+1);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * put_der_length: Encode the supplied length into the buffer pointed to
1247c478bd9Sstevel@tonic-gate  * by buf. max_length represents the maximum length of the buffer pointed
1257c478bd9Sstevel@tonic-gate  * to by buff. We will advance buf to point to the character after the newly
1267c478bd9Sstevel@tonic-gate  * DER encoded length. We return 0 on success or -l it the length cannot
1277c478bd9Sstevel@tonic-gate  * be encoded in max_len characters.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate int
130*ba7b222eSGlenn Barry gssint_put_der_length(unsigned int length, unsigned char **buf, unsigned int max_len)
1317c478bd9Sstevel@tonic-gate {
132*ba7b222eSGlenn Barry     unsigned char *s, *p;
1337c478bd9Sstevel@tonic-gate     unsigned int buf_len = 0;
1347c478bd9Sstevel@tonic-gate     int i, first;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate     /* Oops */
1377c478bd9Sstevel@tonic-gate     if (buf == 0 || max_len < 1)
1387c478bd9Sstevel@tonic-gate 	return (-1);
1397c478bd9Sstevel@tonic-gate 
140*ba7b222eSGlenn Barry     s = *buf;
141*ba7b222eSGlenn Barry 
1427c478bd9Sstevel@tonic-gate     /* Single byte is the length */
1437c478bd9Sstevel@tonic-gate     if (length < 128) {
1447c478bd9Sstevel@tonic-gate 	*s++ = length;
1457c478bd9Sstevel@tonic-gate 	*buf = s;
1467c478bd9Sstevel@tonic-gate 	return (0);
1477c478bd9Sstevel@tonic-gate     }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate     /* First byte contains the number of octets */
1507c478bd9Sstevel@tonic-gate     p = s + 1;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate     /* Running total of the DER encoding length */
1537c478bd9Sstevel@tonic-gate     buf_len = 0;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate     /*
1567c478bd9Sstevel@tonic-gate      * Encode MSB first. We do the encoding by setting a shift
1577c478bd9Sstevel@tonic-gate      * factor to MSO_BIT (24 for 32 bit words) and then shifting the length
1587c478bd9Sstevel@tonic-gate      * by the factor. We then encode the resulting low order byte.
1597c478bd9Sstevel@tonic-gate      * We subtract 8 from the shift factor and repeat to ecnode the next
1607c478bd9Sstevel@tonic-gate      * byte. We stop when the shift factor is zero or we've run out of
1617c478bd9Sstevel@tonic-gate      * buffer to encode into.
1627c478bd9Sstevel@tonic-gate      */
1637c478bd9Sstevel@tonic-gate     first = 0;
1647c478bd9Sstevel@tonic-gate     for (i = MSO_BIT; i >= 0 && buf_len <= max_len; i -= 8) {
1657c478bd9Sstevel@tonic-gate 	unsigned int v;
1667c478bd9Sstevel@tonic-gate 	v = (length >> i) & 0xff;
1677c478bd9Sstevel@tonic-gate 	if ((v) || first) {
1687c478bd9Sstevel@tonic-gate 	    buf_len += 1;
1697c478bd9Sstevel@tonic-gate 	    *p++ = v;
1707c478bd9Sstevel@tonic-gate 	    first = 1;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate     }
1737c478bd9Sstevel@tonic-gate     if (i >= 0)			/* buffer overflow */
1747c478bd9Sstevel@tonic-gate 	return (-1);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate     /*
1777c478bd9Sstevel@tonic-gate      * We go back now and set the first byte to be the length with
1787c478bd9Sstevel@tonic-gate      * the high order bit set.
1797c478bd9Sstevel@tonic-gate      */
1807c478bd9Sstevel@tonic-gate     *s = buf_len | 0x80;
1817c478bd9Sstevel@tonic-gate     *buf = p;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate     return (0);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  *  glue routine for get_mech_type
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  */
191*ba7b222eSGlenn Barry 
192*ba7b222eSGlenn Barry OM_uint32 gssint_get_mech_type_oid(OID, token)
1937c478bd9Sstevel@tonic-gate     gss_OID		OID;
194*ba7b222eSGlenn Barry     gss_buffer_t	token;
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate     unsigned char * buffer_ptr;
1977c478bd9Sstevel@tonic-gate     int length;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate     /*
2007c478bd9Sstevel@tonic-gate      * This routine reads the prefix of "token" in order to determine
2017c478bd9Sstevel@tonic-gate      * its mechanism type. It assumes the encoding suggested in
2027c478bd9Sstevel@tonic-gate      * Appendix B of RFC 1508. This format starts out as follows :
2037c478bd9Sstevel@tonic-gate      *
2047c478bd9Sstevel@tonic-gate      * tag for APPLICATION 0, Sequence[constructed, definite length]
2057c478bd9Sstevel@tonic-gate      * length of remainder of token
2067c478bd9Sstevel@tonic-gate      * tag of OBJECT IDENTIFIER
2077c478bd9Sstevel@tonic-gate      * length of mechanism OID
2087c478bd9Sstevel@tonic-gate      * encoding of mechanism OID
2097c478bd9Sstevel@tonic-gate      * <the rest of the token>
2107c478bd9Sstevel@tonic-gate      *
2117c478bd9Sstevel@tonic-gate      * Numerically, this looks like :
2127c478bd9Sstevel@tonic-gate      *
2137c478bd9Sstevel@tonic-gate      * 0x60
2147c478bd9Sstevel@tonic-gate      * <length> - could be multiple bytes
2157c478bd9Sstevel@tonic-gate      * 0x06
2167c478bd9Sstevel@tonic-gate      * <length> - assume only one byte, hence OID length < 127
2177c478bd9Sstevel@tonic-gate      * <mech OID bytes>
2187c478bd9Sstevel@tonic-gate      *
2197c478bd9Sstevel@tonic-gate      * The routine fills in the OID value and returns an error as necessary.
2207c478bd9Sstevel@tonic-gate      */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (OID == NULL)
2237c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if ((token == NULL) || (token->value == NULL))
2267c478bd9Sstevel@tonic-gate 	return (GSS_S_DEFECTIVE_TOKEN);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate     /* Skip past the APP/Sequnce byte and the token length */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate     buffer_ptr = (unsigned char *) token->value;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate     if (*(buffer_ptr++) != 0x60)
2337c478bd9Sstevel@tonic-gate 	return (GSS_S_DEFECTIVE_TOKEN);
2347c478bd9Sstevel@tonic-gate     length = *buffer_ptr++;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/* check if token length is null */
2377c478bd9Sstevel@tonic-gate 	if (length == 0)
2387c478bd9Sstevel@tonic-gate 	    return (GSS_S_DEFECTIVE_TOKEN);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate     if (length & 0x80) {
2417c478bd9Sstevel@tonic-gate 	if ((length & 0x7f) > 4)
2427c478bd9Sstevel@tonic-gate 	    return (GSS_S_DEFECTIVE_TOKEN);
2437c478bd9Sstevel@tonic-gate 	buffer_ptr += length & 0x7f;
2447c478bd9Sstevel@tonic-gate     }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate     if (*(buffer_ptr++) != 0x06)
2477c478bd9Sstevel@tonic-gate 	return (GSS_S_DEFECTIVE_TOKEN);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate     OID->length = (OM_uint32) *(buffer_ptr++);
2507c478bd9Sstevel@tonic-gate     OID->elements = (void *) buffer_ptr;
2517c478bd9Sstevel@tonic-gate     return (GSS_S_COMPLETE);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
254*ba7b222eSGlenn Barry /*
255*ba7b222eSGlenn Barry  * The following mechanisms do not always identify themselves
256*ba7b222eSGlenn Barry  * per the GSS-API specification, when interoperating with MS
257*ba7b222eSGlenn Barry  * peers. We include the OIDs here so we do not have to link
258*ba7b222eSGlenn Barry  * with the mechanism.
259*ba7b222eSGlenn Barry  */
260*ba7b222eSGlenn Barry static gss_OID_desc gss_ntlm_mechanism_oid_desc =
261*ba7b222eSGlenn Barry 	{10, (void *)"\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a"};
262*ba7b222eSGlenn Barry static gss_OID_desc gss_spnego_mechanism_oid_desc =
263*ba7b222eSGlenn Barry 	{6, (void *)"\x2b\x06\x01\x05\x05\x02"};
264*ba7b222eSGlenn Barry static gss_OID_desc gss_krb5_mechanism_oid_desc =
265*ba7b222eSGlenn Barry 	{9, (void *)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"};
266*ba7b222eSGlenn Barry 
267*ba7b222eSGlenn Barry #define NTLMSSP_SIGNATURE "NTLMSSP"
268*ba7b222eSGlenn Barry 
269*ba7b222eSGlenn Barry OM_uint32 gssint_get_mech_type(OID, token)
270*ba7b222eSGlenn Barry     gss_OID		OID;
271*ba7b222eSGlenn Barry     gss_buffer_t	token;
272*ba7b222eSGlenn Barry {
273*ba7b222eSGlenn Barry     /* Check for interoperability exceptions */
274*ba7b222eSGlenn Barry     if (token->length >= sizeof(NTLMSSP_SIGNATURE) &&
275*ba7b222eSGlenn Barry 	memcmp(token->value, NTLMSSP_SIGNATURE,
276*ba7b222eSGlenn Barry 	       sizeof(NTLMSSP_SIGNATURE)) == 0) {
277*ba7b222eSGlenn Barry 	*OID = gss_ntlm_mechanism_oid_desc;
278*ba7b222eSGlenn Barry     } else if (token->length != 0 &&
279*ba7b222eSGlenn Barry 	       ((char *)token->value)[0] == 0x6E) {
280*ba7b222eSGlenn Barry  	/* Could be a raw AP-REQ (check for APPLICATION tag) */
281*ba7b222eSGlenn Barry 	*OID = gss_krb5_mechanism_oid_desc;
282*ba7b222eSGlenn Barry     } else if (token->length == 0) {
283*ba7b222eSGlenn Barry 	*OID = gss_spnego_mechanism_oid_desc;
284*ba7b222eSGlenn Barry     } else {
285*ba7b222eSGlenn Barry 	return gssint_get_mech_type_oid(OID, token);
286*ba7b222eSGlenn Barry     }
287*ba7b222eSGlenn Barry 
288*ba7b222eSGlenn Barry     return (GSS_S_COMPLETE);
289*ba7b222eSGlenn Barry }
290*ba7b222eSGlenn Barry 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  *  Internal routines to get and release an internal mechanism name
2947c478bd9Sstevel@tonic-gate  */
295*ba7b222eSGlenn Barry 
296*ba7b222eSGlenn Barry #if 0 /* SUNW17PACresync */
297*ba7b222eSGlenn Barry #include "mglueP.h"
298*ba7b222eSGlenn Barry #endif
299*ba7b222eSGlenn Barry 
300*ba7b222eSGlenn Barry OM_uint32 gssint_import_internal_name (minor_status, mech_type, union_name,
3017c478bd9Sstevel@tonic-gate 				internal_name)
3027c478bd9Sstevel@tonic-gate OM_uint32		*minor_status;
303*ba7b222eSGlenn Barry gss_OID			mech_type;
3047c478bd9Sstevel@tonic-gate gss_union_name_t	union_name;
3057c478bd9Sstevel@tonic-gate gss_name_t		*internal_name;
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate     OM_uint32		status;
3087c478bd9Sstevel@tonic-gate     gss_mechanism	mech;
3097c478bd9Sstevel@tonic-gate 
310*ba7b222eSGlenn Barry     mech = gssint_get_mechanism (mech_type);
3117c478bd9Sstevel@tonic-gate     if (mech) {
312*ba7b222eSGlenn Barry 	if (mech->gss_import_name) {
3137c478bd9Sstevel@tonic-gate 	    status = mech->gss_import_name (
314*ba7b222eSGlenn Barry 		    mech->context, /* SUNW17PACresync */
3157c478bd9Sstevel@tonic-gate 					    minor_status,
3167c478bd9Sstevel@tonic-gate 					    union_name->external_name,
3177c478bd9Sstevel@tonic-gate 					    union_name->name_type,
3187c478bd9Sstevel@tonic-gate 					    internal_name);
319*ba7b222eSGlenn Barry 	    if (status != GSS_S_COMPLETE)
320*ba7b222eSGlenn Barry 		map_error(minor_status, mech);
321*ba7b222eSGlenn Barry 	} else
3227c478bd9Sstevel@tonic-gate 	    status = GSS_S_UNAVAILABLE;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	return (status);
3257c478bd9Sstevel@tonic-gate     }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate     return (GSS_S_BAD_MECH);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
330*ba7b222eSGlenn Barry OM_uint32 gssint_export_internal_name(minor_status, mech_type,
3317c478bd9Sstevel@tonic-gate 				     internal_name, name_buf)
3327c478bd9Sstevel@tonic-gate     OM_uint32		*minor_status;
3337c478bd9Sstevel@tonic-gate     const gss_OID		mech_type;
3347c478bd9Sstevel@tonic-gate     const gss_name_t	internal_name;
3357c478bd9Sstevel@tonic-gate     gss_buffer_t		name_buf;
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate     OM_uint32 status;
3387c478bd9Sstevel@tonic-gate     gss_mechanism mech;
3397c478bd9Sstevel@tonic-gate     gss_buffer_desc dispName;
3407c478bd9Sstevel@tonic-gate     gss_OID nameOid;
3417c478bd9Sstevel@tonic-gate     unsigned char *buf = NULL;
3427c478bd9Sstevel@tonic-gate     const unsigned char tokId[] = "\x04\x01";
343*ba7b222eSGlenn Barry     const unsigned int tokIdLen = 2;
3447c478bd9Sstevel@tonic-gate     const int mechOidLenLen = 2, mechOidTagLen = 1, nameLenLen = 4;
3457c478bd9Sstevel@tonic-gate     int mechOidDERLen = 0;
3467c478bd9Sstevel@tonic-gate     int mechOidLen = 0;
3477c478bd9Sstevel@tonic-gate 
348*ba7b222eSGlenn Barry     mech = gssint_get_mechanism(mech_type);
3497c478bd9Sstevel@tonic-gate     if (!mech)
3507c478bd9Sstevel@tonic-gate 	return (GSS_S_BAD_MECH);
3517c478bd9Sstevel@tonic-gate 
352*ba7b222eSGlenn Barry     if (mech->gss_export_name) {
353*ba7b222eSGlenn Barry 	status = mech->gss_export_name(
354*ba7b222eSGlenn Barry 		mech->context,  /* SUNW17PACresync */
3557c478bd9Sstevel@tonic-gate 		minor_status,
3567c478bd9Sstevel@tonic-gate 		internal_name,
357*ba7b222eSGlenn Barry 		name_buf);
358*ba7b222eSGlenn Barry 	if (status != GSS_S_COMPLETE)
359*ba7b222eSGlenn Barry 	    map_error(minor_status, mech);
360*ba7b222eSGlenn Barry 	return status;
361*ba7b222eSGlenn Barry     }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate     /*
3647c478bd9Sstevel@tonic-gate      * if we are here it is because the mechanism does not provide
3657c478bd9Sstevel@tonic-gate      * a gss_export_name so we will use our implementation.  We
3667c478bd9Sstevel@tonic-gate      * do required that the mechanism define a gss_display_name.
3677c478bd9Sstevel@tonic-gate      */
3687c478bd9Sstevel@tonic-gate     if (!mech->gss_display_name)
3697c478bd9Sstevel@tonic-gate 	return (GSS_S_UNAVAILABLE);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate     /*
3727c478bd9Sstevel@tonic-gate      * NOTE: RFC2743 (section 3.2) governs the format of the outer
3737c478bd9Sstevel@tonic-gate      *	 wrapper of exported names; the mechanisms' specs govern
3747c478bd9Sstevel@tonic-gate      *	 the format of the inner portion of the exported name
3757c478bd9Sstevel@tonic-gate      *	 and, for some (e.g., RFC1964, the Kerberos V mech), a
3767c478bd9Sstevel@tonic-gate      *	 generic default as implemented here will do.
3777c478bd9Sstevel@tonic-gate      *
3787c478bd9Sstevel@tonic-gate      * The outer wrapper of an exported MN is: 2-octet tok Id
3797c478bd9Sstevel@tonic-gate      * (0x0401) + 2-octet network-byte order mech OID length + mech
3807c478bd9Sstevel@tonic-gate      * oid (in DER format, including DER tag and DER length) +
3817c478bd9Sstevel@tonic-gate      * 4-octet network-byte order length of inner portion + inner
3827c478bd9Sstevel@tonic-gate      * portion.
3837c478bd9Sstevel@tonic-gate      *
3847c478bd9Sstevel@tonic-gate      * For the Kerberos V mechanism the inner portion of an exported
3857c478bd9Sstevel@tonic-gate      * MN is the display name string and ignores the name type OID
3867c478bd9Sstevel@tonic-gate      * altogether.  And we hope this will be so for any future
3877c478bd9Sstevel@tonic-gate      * mechanisms also, so that factoring name export/import out of
3887c478bd9Sstevel@tonic-gate      * the mech and into libgss pays off.
3897c478bd9Sstevel@tonic-gate      */
390*ba7b222eSGlenn Barry     if ((status = mech->gss_display_name(
391*ba7b222eSGlenn Barry 		mech->context,
3927c478bd9Sstevel@tonic-gate 		minor_status,
3937c478bd9Sstevel@tonic-gate 					 internal_name,
3947c478bd9Sstevel@tonic-gate 					 &dispName,
3957c478bd9Sstevel@tonic-gate 					 &nameOid))
396*ba7b222eSGlenn Barry 	!= GSS_S_COMPLETE) {
397*ba7b222eSGlenn Barry 	map_error(minor_status, mech);
3987c478bd9Sstevel@tonic-gate 	return (status);
399*ba7b222eSGlenn Barry     }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate     /* determine the size of the buffer needed */
402*ba7b222eSGlenn Barry     mechOidDERLen = gssint_der_length_size(mech_type->length);
4037c478bd9Sstevel@tonic-gate     name_buf->length = tokIdLen + mechOidLenLen +
4047c478bd9Sstevel@tonic-gate 	mechOidTagLen + mechOidDERLen +
4057c478bd9Sstevel@tonic-gate 	mech_type->length +
4067c478bd9Sstevel@tonic-gate 	nameLenLen + dispName.length;
4077c478bd9Sstevel@tonic-gate     if ((name_buf->value = (void*)malloc(name_buf->length)) ==
4087c478bd9Sstevel@tonic-gate 	(void*)NULL) {
4097c478bd9Sstevel@tonic-gate 	name_buf->length = 0;
4107c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&status, &dispName);
4117c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
4127c478bd9Sstevel@tonic-gate     }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate     /* now create the name ..... */
4157c478bd9Sstevel@tonic-gate     buf = (unsigned char *)name_buf->value;
4167c478bd9Sstevel@tonic-gate     (void) memset(name_buf->value, 0, name_buf->length);
4177c478bd9Sstevel@tonic-gate     (void) memcpy(buf, tokId, tokIdLen);
4187c478bd9Sstevel@tonic-gate     buf += tokIdLen;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate     /* spec allows only 2 bytes for the mech oid length */
4217c478bd9Sstevel@tonic-gate     mechOidLen = mechOidDERLen + mechOidTagLen + mech_type->length;
422*ba7b222eSGlenn Barry     store_16_be(mechOidLen, buf);
423*ba7b222eSGlenn Barry     buf += 2;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate     /*
4267c478bd9Sstevel@tonic-gate      * DER Encoding of mech OID contains OID Tag (0x06), length and
4277c478bd9Sstevel@tonic-gate      * mech OID value
4287c478bd9Sstevel@tonic-gate      */
4297c478bd9Sstevel@tonic-gate     *buf++ = 0x06;
430*ba7b222eSGlenn Barry     if (gssint_put_der_length(mech_type->length, &buf,
4317c478bd9Sstevel@tonic-gate 		       (name_buf->length - tokIdLen -2)) != 0) {
4327c478bd9Sstevel@tonic-gate 	name_buf->length = 0;
4337c478bd9Sstevel@tonic-gate 	free(name_buf->value);
4347c478bd9Sstevel@tonic-gate 	(void) gss_release_buffer(&status, &dispName);
4357c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
4367c478bd9Sstevel@tonic-gate     }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate     (void) memcpy(buf, mech_type->elements, mech_type->length);
4397c478bd9Sstevel@tonic-gate     buf += mech_type->length;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate     /* spec designates the next 4 bytes for the name length */
442*ba7b222eSGlenn Barry     store_32_be(dispName.length, buf);
443*ba7b222eSGlenn Barry     buf += 4;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate     /* for the final ingredient - add the name from gss_display_name */
4467c478bd9Sstevel@tonic-gate     (void) memcpy(buf, dispName.value, dispName.length);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate     /* release the buffer obtained from gss_display_name */
4497c478bd9Sstevel@tonic-gate     (void) gss_release_buffer(minor_status, &dispName);
4507c478bd9Sstevel@tonic-gate     return (GSS_S_COMPLETE);
451*ba7b222eSGlenn Barry } /*  gssint_export_internal_name */
4527c478bd9Sstevel@tonic-gate 
453*ba7b222eSGlenn Barry OM_uint32 gssint_display_internal_name (minor_status, mech_type, internal_name,
4547c478bd9Sstevel@tonic-gate 				 external_name, name_type)
4557c478bd9Sstevel@tonic-gate OM_uint32	*minor_status;
456*ba7b222eSGlenn Barry gss_OID		mech_type;
457*ba7b222eSGlenn Barry gss_name_t	internal_name;
4587c478bd9Sstevel@tonic-gate gss_buffer_t	external_name;
4597c478bd9Sstevel@tonic-gate gss_OID		*name_type;
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate     OM_uint32		status;
4627c478bd9Sstevel@tonic-gate     gss_mechanism	mech;
4637c478bd9Sstevel@tonic-gate 
464*ba7b222eSGlenn Barry     mech = gssint_get_mechanism (mech_type);
4657c478bd9Sstevel@tonic-gate     if (mech) {
466*ba7b222eSGlenn Barry 	if (mech->gss_display_name) {
4677c478bd9Sstevel@tonic-gate 	    status = mech->gss_display_name (
4687c478bd9Sstevel@tonic-gate 		    mech->context,
4697c478bd9Sstevel@tonic-gate 					     minor_status,
4707c478bd9Sstevel@tonic-gate 					     internal_name,
4717c478bd9Sstevel@tonic-gate 					     external_name,
4727c478bd9Sstevel@tonic-gate 					     name_type);
473*ba7b222eSGlenn Barry 	    if (status != GSS_S_COMPLETE)
474*ba7b222eSGlenn Barry 		map_error(minor_status, mech);
475*ba7b222eSGlenn Barry 	} else
4767c478bd9Sstevel@tonic-gate 	    status = GSS_S_UNAVAILABLE;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	return (status);
4797c478bd9Sstevel@tonic-gate     }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate     return (GSS_S_BAD_MECH);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
484*ba7b222eSGlenn Barry OM_uint32 gssint_release_internal_name (minor_status, mech_type, internal_name)
4857c478bd9Sstevel@tonic-gate OM_uint32	*minor_status;
486*ba7b222eSGlenn Barry gss_OID		mech_type;
4877c478bd9Sstevel@tonic-gate gss_name_t	*internal_name;
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate     OM_uint32		status;
4907c478bd9Sstevel@tonic-gate     gss_mechanism	mech;
4917c478bd9Sstevel@tonic-gate 
492*ba7b222eSGlenn Barry     mech = gssint_get_mechanism (mech_type);
4937c478bd9Sstevel@tonic-gate     if (mech) {
494*ba7b222eSGlenn Barry 	if (mech->gss_release_name) {
4957c478bd9Sstevel@tonic-gate 	    status = mech->gss_release_name (
4967c478bd9Sstevel@tonic-gate 		    mech->context,
4977c478bd9Sstevel@tonic-gate 					     minor_status,
4987c478bd9Sstevel@tonic-gate 					     internal_name);
499*ba7b222eSGlenn Barry 	    if (status != GSS_S_COMPLETE)
500*ba7b222eSGlenn Barry 		map_error(minor_status, mech);
501*ba7b222eSGlenn Barry 	} else
5027c478bd9Sstevel@tonic-gate 	    status = GSS_S_UNAVAILABLE;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	return (status);
5057c478bd9Sstevel@tonic-gate     }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate     return (GSS_S_BAD_MECH);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
510*ba7b222eSGlenn Barry OM_uint32 gssint_delete_internal_sec_context (minor_status,
511*ba7b222eSGlenn Barry 					      mech_type,
512*ba7b222eSGlenn Barry 					      internal_ctx,
513*ba7b222eSGlenn Barry 					      output_token)
514*ba7b222eSGlenn Barry OM_uint32	*minor_status;
515*ba7b222eSGlenn Barry gss_OID		mech_type;
516*ba7b222eSGlenn Barry gss_ctx_id_t	*internal_ctx;
517*ba7b222eSGlenn Barry gss_buffer_t	output_token;
518*ba7b222eSGlenn Barry {
519*ba7b222eSGlenn Barry     OM_uint32		status;
520*ba7b222eSGlenn Barry     gss_mechanism	mech;
521*ba7b222eSGlenn Barry 
522*ba7b222eSGlenn Barry     mech = gssint_get_mechanism (mech_type);
523*ba7b222eSGlenn Barry     if (mech) {
524*ba7b222eSGlenn Barry 	if (mech->gss_delete_sec_context)
525*ba7b222eSGlenn Barry 	    status = mech->gss_delete_sec_context (
526*ba7b222eSGlenn Barry 		    mech->context,  /* SUNW17PACresync */
527*ba7b222eSGlenn Barry 		    minor_status,
528*ba7b222eSGlenn Barry 		    internal_ctx,
529*ba7b222eSGlenn Barry 		    output_token);
530*ba7b222eSGlenn Barry 	else
531*ba7b222eSGlenn Barry 	    /* SUNW17PACresync - map error here? */
532*ba7b222eSGlenn Barry 	    status = GSS_S_UNAVAILABLE;
533*ba7b222eSGlenn Barry 
534*ba7b222eSGlenn Barry 	return (status);
535*ba7b222eSGlenn Barry     }
536*ba7b222eSGlenn Barry 
537*ba7b222eSGlenn Barry     return (GSS_S_BAD_MECH);
538*ba7b222eSGlenn Barry }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate  * This function converts an internal gssapi name to a union gssapi
5427c478bd9Sstevel@tonic-gate  * name.  Note that internal_name should be considered "consumed" by
5437c478bd9Sstevel@tonic-gate  * this call, whether or not we return an error.
5447c478bd9Sstevel@tonic-gate  */
545*ba7b222eSGlenn Barry OM_uint32 gssint_convert_name_to_union_name(minor_status, mech,
5467c478bd9Sstevel@tonic-gate 					   internal_name, external_name)
5477c478bd9Sstevel@tonic-gate     OM_uint32 *minor_status;
5487c478bd9Sstevel@tonic-gate     gss_mechanism	mech;
5497c478bd9Sstevel@tonic-gate     gss_name_t	internal_name;
5507c478bd9Sstevel@tonic-gate     gss_name_t	*external_name;
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate     OM_uint32 major_status,tmp;
5537c478bd9Sstevel@tonic-gate     gss_union_name_t union_name;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate     union_name = (gss_union_name_t) malloc (sizeof(gss_union_name_desc));
5567c478bd9Sstevel@tonic-gate     if (!union_name) {
557*ba7b222eSGlenn Barry 	major_status = GSS_S_FAILURE;
558*ba7b222eSGlenn Barry 	*minor_status = ENOMEM;
559*ba7b222eSGlenn Barry 	map_errcode(minor_status);
5607c478bd9Sstevel@tonic-gate 	goto allocation_failure;
5617c478bd9Sstevel@tonic-gate     }
5627c478bd9Sstevel@tonic-gate     union_name->mech_type = 0;
5637c478bd9Sstevel@tonic-gate     union_name->mech_name = internal_name;
5647c478bd9Sstevel@tonic-gate     union_name->name_type = 0;
5657c478bd9Sstevel@tonic-gate     union_name->external_name = 0;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate     major_status = generic_gss_copy_oid(minor_status, &mech->mech_type,
5687c478bd9Sstevel@tonic-gate 					&union_name->mech_type);
569*ba7b222eSGlenn Barry     if (major_status != GSS_S_COMPLETE) {
570*ba7b222eSGlenn Barry 	map_errcode(minor_status);
5717c478bd9Sstevel@tonic-gate 	goto allocation_failure;
572*ba7b222eSGlenn Barry     }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate     union_name->external_name =
5757c478bd9Sstevel@tonic-gate 	(gss_buffer_t) malloc(sizeof(gss_buffer_desc));
5767c478bd9Sstevel@tonic-gate     if (!union_name->external_name) {
577*ba7b222eSGlenn Barry 	    major_status = GSS_S_FAILURE;
578*ba7b222eSGlenn Barry 	    *minor_status = ENOMEM;
5797c478bd9Sstevel@tonic-gate 	    goto allocation_failure;
5807c478bd9Sstevel@tonic-gate     }
5817c478bd9Sstevel@tonic-gate 
582*ba7b222eSGlenn Barry     major_status = mech->gss_display_name(
583*ba7b222eSGlenn Barry 	    mech->context,  /* SUNW17PACresync */
584*ba7b222eSGlenn Barry 	    minor_status,
5857c478bd9Sstevel@tonic-gate 	    internal_name,
5867c478bd9Sstevel@tonic-gate 	    union_name->external_name,
5877c478bd9Sstevel@tonic-gate 	    &union_name->name_type);
588*ba7b222eSGlenn Barry     if (major_status != GSS_S_COMPLETE) {
589*ba7b222eSGlenn Barry 	map_error(minor_status, mech);
5907c478bd9Sstevel@tonic-gate 	goto allocation_failure;
591*ba7b222eSGlenn Barry     }
5927c478bd9Sstevel@tonic-gate 
593*ba7b222eSGlenn Barry     union_name->loopback = union_name;
5947c478bd9Sstevel@tonic-gate     *external_name = (gss_name_t) union_name;
5957c478bd9Sstevel@tonic-gate     return (GSS_S_COMPLETE);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate allocation_failure:
5987c478bd9Sstevel@tonic-gate     if (union_name) {
5997c478bd9Sstevel@tonic-gate 	if (union_name->external_name) {
6007c478bd9Sstevel@tonic-gate 	    if (union_name->external_name->value)
6017c478bd9Sstevel@tonic-gate 		free(union_name->external_name->value);
6027c478bd9Sstevel@tonic-gate 	    free(union_name->external_name);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	if (union_name->name_type)
6057c478bd9Sstevel@tonic-gate 		(void) gss_release_oid(&tmp, &union_name->name_type);
6067c478bd9Sstevel@tonic-gate 	if (union_name->mech_type)
6077c478bd9Sstevel@tonic-gate 		(void) gss_release_oid(&tmp, &union_name->mech_type);
6087c478bd9Sstevel@tonic-gate 	free(union_name);
6097c478bd9Sstevel@tonic-gate     }
6107c478bd9Sstevel@tonic-gate     /*
6117c478bd9Sstevel@tonic-gate      * do as the top comment says - since we are now owners of
6127c478bd9Sstevel@tonic-gate      * internal_name, we must clean it up
6137c478bd9Sstevel@tonic-gate      */
6147c478bd9Sstevel@tonic-gate     if (internal_name)
615*ba7b222eSGlenn Barry 	(void) gssint_release_internal_name(&tmp, &mech->mech_type,
6167c478bd9Sstevel@tonic-gate 					   &internal_name);
6177c478bd9Sstevel@tonic-gate     return (major_status);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate  * Glue routine for returning the mechanism-specific credential from a
6227c478bd9Sstevel@tonic-gate  * external union credential.
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate gss_cred_id_t
625*ba7b222eSGlenn Barry gssint_get_mechanism_cred(union_cred, mech_type)
626*ba7b222eSGlenn Barry     gss_union_cred_t	union_cred;
627*ba7b222eSGlenn Barry     gss_OID		mech_type;
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate     int		i;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate     if (union_cred == (gss_union_cred_t) GSS_C_NO_CREDENTIAL)
632*ba7b222eSGlenn Barry 	return GSS_C_NO_CREDENTIAL;
633*ba7b222eSGlenn Barry 
634*ba7b222eSGlenn Barry     /*
635*ba7b222eSGlenn Barry      * SUNW17PACresync
636*ba7b222eSGlenn Barry      * Disable this block as it causes problems for gss_add_cred
637*ba7b222eSGlenn Barry      * for HTTP SSO (and also probably causes STC gss.13 to fail too).
638*ba7b222eSGlenn Barry      */
639*ba7b222eSGlenn Barry #if 0
640*ba7b222eSGlenn Barry     /* SPNEGO mechanism will again call into GSSAPI */
641*ba7b222eSGlenn Barry     if (g_OID_equal(&gss_spnego_mechanism_oid_desc, mech_type))
642*ba7b222eSGlenn Barry 	return (gss_cred_id_t)union_cred;
643*ba7b222eSGlenn Barry #endif
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate     for (i=0; i < union_cred->count; i++) {
6467c478bd9Sstevel@tonic-gate 	if (g_OID_equal(mech_type, &union_cred->mechs_array[i]))
647*ba7b222eSGlenn Barry 	    return union_cred->cred_array[i];
648*ba7b222eSGlenn Barry 
649*ba7b222eSGlenn Barry 	/* for SPNEGO, check the next-lower set of creds */
650*ba7b222eSGlenn Barry 	if (g_OID_equal(&gss_spnego_mechanism_oid_desc, &union_cred->mechs_array[i])) {
651*ba7b222eSGlenn Barry 	    gss_union_cred_t candidate_cred;
652*ba7b222eSGlenn Barry 	    gss_cred_id_t    sub_cred;
653*ba7b222eSGlenn Barry 
654*ba7b222eSGlenn Barry 	    candidate_cred = (gss_union_cred_t)union_cred->cred_array[i];
655*ba7b222eSGlenn Barry 	    sub_cred = gssint_get_mechanism_cred(candidate_cred, mech_type);
656*ba7b222eSGlenn Barry 
657*ba7b222eSGlenn Barry 	    if(sub_cred != GSS_C_NO_CREDENTIAL)
658*ba7b222eSGlenn Barry 		return sub_cred;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate     }
6617c478bd9Sstevel@tonic-gate 
662*ba7b222eSGlenn Barry     return GSS_C_NO_CREDENTIAL;
663*ba7b222eSGlenn Barry }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate /*
6667c478bd9Sstevel@tonic-gate  * Routine to create and copy the gss_buffer_desc structure.
6677c478bd9Sstevel@tonic-gate  * Both space for the structure and the data is allocated.
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate OM_uint32
670ab9b2e15Sgtb gssint_create_copy_buffer(srcBuf, destBuf, addNullChar)
6717c478bd9Sstevel@tonic-gate     const gss_buffer_t	srcBuf;
6727c478bd9Sstevel@tonic-gate     gss_buffer_t 		*destBuf;
6737c478bd9Sstevel@tonic-gate     int			addNullChar;
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate     gss_buffer_t aBuf;
676*ba7b222eSGlenn Barry     unsigned int len;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate     if (destBuf == NULL)
6797c478bd9Sstevel@tonic-gate 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate     *destBuf = 0;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate     aBuf = (gss_buffer_t)malloc(sizeof (gss_buffer_desc));
6847c478bd9Sstevel@tonic-gate     if (!aBuf)
6857c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate     if (addNullChar)
6887c478bd9Sstevel@tonic-gate 	len = srcBuf->length + 1;
6897c478bd9Sstevel@tonic-gate     else
6907c478bd9Sstevel@tonic-gate 	len = srcBuf->length;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate     if (!(aBuf->value = (void*)malloc(len))) {
6937c478bd9Sstevel@tonic-gate 	free(aBuf);
6947c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
6957c478bd9Sstevel@tonic-gate     }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate     (void) memcpy(aBuf->value, srcBuf->value, srcBuf->length);
6997c478bd9Sstevel@tonic-gate     aBuf->length = srcBuf->length;
7007c478bd9Sstevel@tonic-gate     *destBuf = aBuf;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate     /* optionally add a NULL character */
7037c478bd9Sstevel@tonic-gate     if (addNullChar)
7047c478bd9Sstevel@tonic-gate 	((char *)aBuf->value)[aBuf->length] = '\0';
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate     return (GSS_S_COMPLETE);
707*ba7b222eSGlenn Barry } /* ****** gssint_create_copy_buffer  ****** */
708*ba7b222eSGlenn Barry 
709