1178828Sdfr/*-
2178828Sdfr * Copyright (c) 2000 The Regents of the University of Michigan.
3178828Sdfr * All rights reserved.
4178828Sdfr *
5178828Sdfr * Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
6178828Sdfr * All rights reserved, all wrongs reversed.
7178828Sdfr *
8178828Sdfr * Redistribution and use in source and binary forms, with or without
9178828Sdfr * modification, are permitted provided that the following conditions
10178828Sdfr * are met:
11178828Sdfr *
12178828Sdfr * 1. Redistributions of source code must retain the above copyright
13178828Sdfr *    notice, this list of conditions and the following disclaimer.
14178828Sdfr * 2. Redistributions in binary form must reproduce the above copyright
15178828Sdfr *    notice, this list of conditions and the following disclaimer in the
16178828Sdfr *    documentation and/or other materials provided with the distribution.
17178828Sdfr * 3. Neither the name of the University nor the names of its
18178828Sdfr *    contributors may be used to endorse or promote products derived
19178828Sdfr *    from this software without specific prior written permission.
20178828Sdfr *
21178828Sdfr * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22178828Sdfr * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23178828Sdfr * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24178828Sdfr * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25178828Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26178828Sdfr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27178828Sdfr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28178828Sdfr * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29178828Sdfr * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30178828Sdfr * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31178828Sdfr * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32178828Sdfr */
33178828Sdfr/* $FreeBSD$ */
34178828Sdfr
35178828Sdfr#include <gssapi/gssapi.h>
36178828Sdfr#include <stdio.h>
37178828Sdfr#include <stdlib.h>
38178828Sdfr#include <string.h>
39178828Sdfr#include <errno.h>
40178828Sdfr
41178828Sdfr#include "utils.h"
42178828Sdfr
43178828SdfrOM_uint32
44178828Sdfrgss_oid_to_str(OM_uint32 *minor_status, gss_OID oid, gss_buffer_t oid_str)
45178828Sdfr{
46178828Sdfr	char		numstr[128];
47178828Sdfr	unsigned long	number;
48178828Sdfr	int		numshift;
49178828Sdfr	size_t		string_length;
50178828Sdfr	size_t		i;
51178828Sdfr	unsigned char	*cp;
52178828Sdfr	char		*bp;
53178828Sdfr
54178828Sdfr	*minor_status = 0;
55178828Sdfr	_gss_buffer_zero(oid_str);
56178828Sdfr
57178828Sdfr	if (oid == GSS_C_NULL_OID)
58178828Sdfr		return (GSS_S_FAILURE);
59178828Sdfr
60178828Sdfr	/* Decoded according to krb5/gssapi_krb5.c */
61178828Sdfr
62178828Sdfr	/* First determine the size of the string */
63178828Sdfr	string_length = 0;
64178828Sdfr	number = 0;
65178828Sdfr	numshift = 0;
66178828Sdfr	cp = (unsigned char *) oid->elements;
67178828Sdfr	number = (unsigned long) cp[0];
68178828Sdfr	sprintf(numstr, "%ld ", number/40);
69178828Sdfr	string_length += strlen(numstr);
70178828Sdfr	sprintf(numstr, "%ld ", number%40);
71178828Sdfr	string_length += strlen(numstr);
72178828Sdfr	for (i=1; i<oid->length; i++) {
73178828Sdfr		if ( (size_t) (numshift+7) < (sizeof(unsigned long)*8)) {
74178828Sdfr			number = (number << 7) | (cp[i] & 0x7f);
75178828Sdfr			numshift += 7;
76178828Sdfr		}
77178828Sdfr		else {
78178828Sdfr			*minor_status = 0;
79178828Sdfr			return(GSS_S_FAILURE);
80178828Sdfr		}
81178828Sdfr		if ((cp[i] & 0x80) == 0) {
82178828Sdfr			sprintf(numstr, "%ld ", number);
83178828Sdfr			string_length += strlen(numstr);
84178828Sdfr			number = 0;
85178828Sdfr			numshift = 0;
86178828Sdfr		}
87178828Sdfr	}
88178828Sdfr	/*
89178828Sdfr	 * If we get here, we've calculated the length of "n n n ... n ".
90178828Sdfr	 * Add 4 here for "{ " and "}\0".
91178828Sdfr	 */
92178828Sdfr	string_length += 4;
93178828Sdfr	if ((bp = (char *) malloc(string_length))) {
94178828Sdfr		strcpy(bp, "{ ");
95178828Sdfr		number = (unsigned long) cp[0];
96178828Sdfr		sprintf(numstr, "%ld ", number/40);
97178828Sdfr		strcat(bp, numstr);
98178828Sdfr		sprintf(numstr, "%ld ", number%40);
99178828Sdfr		strcat(bp, numstr);
100178828Sdfr		number = 0;
101178828Sdfr		cp = (unsigned char *) oid->elements;
102178828Sdfr		for (i=1; i<oid->length; i++) {
103178828Sdfr			number = (number << 7) | (cp[i] & 0x7f);
104178828Sdfr			if ((cp[i] & 0x80) == 0) {
105178828Sdfr				sprintf(numstr, "%ld ", number);
106178828Sdfr				strcat(bp, numstr);
107178828Sdfr				number = 0;
108178828Sdfr			}
109178828Sdfr		}
110178828Sdfr		strcat(bp, "}");
111178828Sdfr		oid_str->length = strlen(bp)+1;
112178828Sdfr		oid_str->value = (void *) bp;
113178828Sdfr		*minor_status = 0;
114178828Sdfr		return(GSS_S_COMPLETE);
115178828Sdfr	}
116178828Sdfr	*minor_status = ENOMEM;
117178828Sdfr	return(GSS_S_FAILURE);
118178828Sdfr}
119