#include "mglueP.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <string.h>
#include <errno.h>
#define MSO_BIT (8*(sizeof (int) - 1))
extern gss_mechanism *gssint_mechs_array;
int
gssint_get_der_length(unsigned char **buf, unsigned int buf_len, unsigned int *bytes)
{
unsigned char *p = *buf;
int length, new_length;
int octets;
if (buf_len < 1)
return (-1);
*bytes = 1;
if (*p < 128) {
*buf = p+1;
return (*p);
}
octets = *p++ & 0x7f;
*bytes += octets;
if (octets > buf_len - 1)
return (-1);
for (length = 0; octets; octets--) {
new_length = (length << 8) + *p++;
if (new_length < length)
return (-1);
length = new_length;
}
*buf = p;
return (length);
}
unsigned int
gssint_der_length_size(unsigned int len)
{
int i;
if (len < 128)
return (1);
for (i = 0; len; i++) {
len >>= 8;
}
return (i+1);
}
int
gssint_put_der_length(unsigned int length, unsigned char **buf, unsigned int max_len)
{
unsigned char *s = *buf, *p;
unsigned int buf_len = 0;
int i, first;
if (buf == 0 || max_len < 1)
return (-1);
if (length < 128) {
*s++ = length;
*buf = s;
return (0);
}
p = s + 1;
buf_len = 0;
first = 0;
for (i = MSO_BIT; i >= 0 && buf_len <= max_len; i -= 8) {
unsigned int v;
v = (length >> i) & 0xff;
if ((v) || first) {
buf_len += 1;
*p++ = v;
first = 1;
}
}
if (i >= 0)
return (-1);
*s = buf_len | 0x80;
*buf = p;
return (0);
}
OM_uint32 gssint_get_mech_type(OID, token)
gss_OID OID;
gss_buffer_t token;
{
unsigned char * buffer_ptr;
int length;
if (OID == NULL)
return (GSS_S_CALL_INACCESSIBLE_WRITE);
if ((token == NULL) || (token->value == NULL))
return (GSS_S_DEFECTIVE_TOKEN);
buffer_ptr = (unsigned char *) token->value;
if (*(buffer_ptr++) != 0x60)
return (GSS_S_DEFECTIVE_TOKEN);
length = *buffer_ptr++;
if (length == 0)
return (GSS_S_DEFECTIVE_TOKEN);
if (length & 0x80) {
if ((length & 0x7f) > 4)
return (GSS_S_DEFECTIVE_TOKEN);
buffer_ptr += length & 0x7f;
}
if (*(buffer_ptr++) != 0x06)
return (GSS_S_DEFECTIVE_TOKEN);
OID->length = (OM_uint32) *(buffer_ptr++);
OID->elements = (void *) buffer_ptr;
return (GSS_S_COMPLETE);
}
#include "mglueP.h"
OM_uint32 gssint_import_internal_name (minor_status, mech_type, union_name,
internal_name)
OM_uint32 *minor_status;
gss_OID mech_type;
gss_union_name_t union_name;
gss_name_t *internal_name;
{
OM_uint32 status;
gss_mechanism mech;
mech = gssint_get_mechanism (mech_type);
if (mech) {
if (mech->gss_import_name)
status = mech->gss_import_name (
mech->context,
minor_status,
union_name->external_name,
union_name->name_type,
internal_name);
else
status = GSS_S_UNAVAILABLE;
return (status);
}
return (GSS_S_BAD_MECH);
}
OM_uint32 gssint_export_internal_name(minor_status, mech_type,
internal_name, name_buf)
OM_uint32 *minor_status;
const gss_OID mech_type;
const gss_name_t internal_name;
gss_buffer_t name_buf;
{
OM_uint32 status;
gss_mechanism mech;
gss_buffer_desc dispName;
gss_OID nameOid;
unsigned char *buf = NULL;
const unsigned char tokId[] = "\x04\x01";
const unsigned int tokIdLen = 2;
const int mechOidLenLen = 2, mechOidTagLen = 1, nameLenLen = 4;
int mechOidDERLen = 0;
int mechOidLen = 0;
mech = gssint_get_mechanism(mech_type);
if (!mech)
return (GSS_S_BAD_MECH);
if (mech->gss_export_name)
return (mech->gss_export_name(mech->context,
minor_status,
internal_name,
name_buf));
if (!mech->gss_display_name)
return (GSS_S_UNAVAILABLE);
if ((status = mech->gss_display_name(mech->context,
minor_status,
internal_name,
&dispName,
&nameOid))
!= GSS_S_COMPLETE)
return (status);
mechOidDERLen = gssint_der_length_size(mech_type->length);
name_buf->length = tokIdLen + mechOidLenLen +
mechOidTagLen + mechOidDERLen +
mech_type->length +
nameLenLen + dispName.length;
if ((name_buf->value = (void*)malloc(name_buf->length)) ==
(void*)NULL) {
name_buf->length = 0;
(void) gss_release_buffer(&status, &dispName);
return (GSS_S_FAILURE);
}
buf = (unsigned char *)name_buf->value;
(void) memset(name_buf->value, 0, name_buf->length);
(void) memcpy(buf, tokId, tokIdLen);
buf += tokIdLen;
mechOidLen = mechOidDERLen + mechOidTagLen + mech_type->length;
*buf++ = (mechOidLen & 0xFF00) >> 8;
*buf++ = (mechOidLen & 0x00FF);
*buf++ = 0x06;
if (gssint_put_der_length(mech_type->length, &buf,
(name_buf->length - tokIdLen -2)) != 0) {
name_buf->length = 0;
free(name_buf->value);
(void) gss_release_buffer(&status, &dispName);
return (GSS_S_FAILURE);
}
(void) memcpy(buf, mech_type->elements, mech_type->length);
buf += mech_type->length;
*buf++ = (dispName.length & 0xFF000000) >> 24;
*buf++ = (dispName.length & 0x00FF0000) >> 16;
*buf++ = (dispName.length & 0x0000FF00) >> 8;
*buf++ = (dispName.length & 0X000000FF);
(void) memcpy(buf, dispName.value, dispName.length);
(void) gss_release_buffer(minor_status, &dispName);
return (GSS_S_COMPLETE);
}
OM_uint32 gssint_display_internal_name (minor_status, mech_type, internal_name,
external_name, name_type)
OM_uint32 *minor_status;
gss_OID mech_type;
gss_name_t internal_name;
gss_buffer_t external_name;
gss_OID *name_type;
{
OM_uint32 status;
gss_mechanism mech;
mech = gssint_get_mechanism (mech_type);
if (mech) {
if (mech->gss_display_name)
status = mech->gss_display_name (
mech->context,
minor_status,
internal_name,
external_name,
name_type);
else
status = GSS_S_UNAVAILABLE;
return (status);
}
return (GSS_S_BAD_MECH);
}
OM_uint32 gssint_release_internal_name (minor_status, mech_type, internal_name)
OM_uint32 *minor_status;
gss_OID mech_type;
gss_name_t *internal_name;
{
OM_uint32 status;
gss_mechanism mech;
mech = gssint_get_mechanism (mech_type);
if (mech) {
if (mech->gss_release_name)
status = mech->gss_release_name (
mech->context,
minor_status,
internal_name);
else
status = GSS_S_UNAVAILABLE;
return (status);
}
return (GSS_S_BAD_MECH);
}
OM_uint32 gssint_convert_name_to_union_name(minor_status, mech,
internal_name, external_name)
OM_uint32 *minor_status;
gss_mechanism mech;
gss_name_t internal_name;
gss_name_t *external_name;
{
OM_uint32 major_status,tmp;
gss_union_name_t union_name;
union_name = (gss_union_name_t) malloc (sizeof(gss_union_name_desc));
if (!union_name) {
goto allocation_failure;
}
union_name->mech_type = 0;
union_name->mech_name = internal_name;
union_name->name_type = 0;
union_name->external_name = 0;
major_status = generic_gss_copy_oid(minor_status, &mech->mech_type,
&union_name->mech_type);
if (major_status != GSS_S_COMPLETE)
goto allocation_failure;
union_name->external_name =
(gss_buffer_t) malloc(sizeof(gss_buffer_desc));
if (!union_name->external_name) {
goto allocation_failure;
}
major_status = mech->gss_display_name(mech->context, minor_status,
internal_name,
union_name->external_name,
&union_name->name_type);
if (major_status != GSS_S_COMPLETE)
goto allocation_failure;
union_name->loopback = union_name;
*external_name = union_name;
return (GSS_S_COMPLETE);
allocation_failure:
if (union_name) {
if (union_name->external_name) {
if (union_name->external_name->value)
free(union_name->external_name->value);
free(union_name->external_name);
}
if (union_name->name_type)
gss_release_oid(&tmp, &union_name->name_type);
if (union_name->mech_type)
gss_release_oid(&tmp, &union_name->mech_type);
free(union_name);
}
if (internal_name)
(void) gssint_release_internal_name(&tmp, &mech->mech_type,
&internal_name);
return (major_status);
}
gss_cred_id_t
gssint_get_mechanism_cred(union_cred, mech_type)
gss_union_cred_t union_cred;
gss_OID mech_type;
{
int i;
if (union_cred == GSS_C_NO_CREDENTIAL)
return GSS_C_NO_CREDENTIAL;
for (i=0; i < union_cred->count; i++) {
if (g_OID_equal(mech_type, &union_cred->mechs_array[i]))
return union_cred->cred_array[i];
}
return GSS_C_NO_CREDENTIAL;
}
OM_uint32
gssint_create_copy_buffer(srcBuf, destBuf, addNullChar)
const gss_buffer_t srcBuf;
gss_buffer_t *destBuf;
int addNullChar;
{
gss_buffer_t aBuf;
unsigned int len;
if (destBuf == NULL)
return (GSS_S_CALL_INACCESSIBLE_WRITE);
*destBuf = 0;
aBuf = (gss_buffer_t)malloc(sizeof (gss_buffer_desc));
if (!aBuf)
return (GSS_S_FAILURE);
if (addNullChar)
len = srcBuf->length + 1;
else
len = srcBuf->length;
if (!(aBuf->value = (void*)malloc(len))) {
free(aBuf);
return (GSS_S_FAILURE);
}
(void) memcpy(aBuf->value, srcBuf->value, srcBuf->length);
aBuf->length = srcBuf->length;
*destBuf = aBuf;
if (addNullChar)
((char *)aBuf->value)[aBuf->length] = '\0';
return (GSS_S_COMPLETE);
}