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