xref: /freebsd/lib/libgssapi/gss_oid_to_str.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1 /*-
2  * Copyright (c) 2000 The Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
6  * All rights reserved, all wrongs reversed.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /* $FreeBSD$ */
34 
35 #include <gssapi/gssapi.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 
41 #include "utils.h"
42 
43 OM_uint32
44 gss_oid_to_str(OM_uint32 *minor_status, gss_OID oid, gss_buffer_t oid_str)
45 {
46 	char		numstr[128];
47 	unsigned long	number;
48 	int		numshift;
49 	size_t		string_length;
50 	size_t		i;
51 	unsigned char	*cp;
52 	char		*bp;
53 
54 	*minor_status = 0;
55 	_gss_buffer_zero(oid_str);
56 
57 	if (oid == GSS_C_NULL_OID)
58 		return (GSS_S_FAILURE);
59 
60 	/* Decoded according to krb5/gssapi_krb5.c */
61 
62 	/* First determine the size of the string */
63 	string_length = 0;
64 	number = 0;
65 	numshift = 0;
66 	cp = (unsigned char *) oid->elements;
67 	number = (unsigned long) cp[0];
68 	sprintf(numstr, "%ld ", number/40);
69 	string_length += strlen(numstr);
70 	sprintf(numstr, "%ld ", number%40);
71 	string_length += strlen(numstr);
72 	for (i=1; i<oid->length; i++) {
73 		if ( (size_t) (numshift+7) < (sizeof(unsigned long)*8)) {
74 			number = (number << 7) | (cp[i] & 0x7f);
75 			numshift += 7;
76 		}
77 		else {
78 			*minor_status = 0;
79 			return(GSS_S_FAILURE);
80 		}
81 		if ((cp[i] & 0x80) == 0) {
82 			sprintf(numstr, "%ld ", number);
83 			string_length += strlen(numstr);
84 			number = 0;
85 			numshift = 0;
86 		}
87 	}
88 	/*
89 	 * If we get here, we've calculated the length of "n n n ... n ".
90 	 * Add 4 here for "{ " and "}\0".
91 	 */
92 	string_length += 4;
93 	if ((bp = (char *) malloc(string_length))) {
94 		strcpy(bp, "{ ");
95 		number = (unsigned long) cp[0];
96 		sprintf(numstr, "%ld ", number/40);
97 		strcat(bp, numstr);
98 		sprintf(numstr, "%ld ", number%40);
99 		strcat(bp, numstr);
100 		number = 0;
101 		cp = (unsigned char *) oid->elements;
102 		for (i=1; i<oid->length; i++) {
103 			number = (number << 7) | (cp[i] & 0x7f);
104 			if ((cp[i] & 0x80) == 0) {
105 				sprintf(numstr, "%ld ", number);
106 				strcat(bp, numstr);
107 				number = 0;
108 			}
109 		}
110 		strcat(bp, "}");
111 		oid_str->length = strlen(bp)+1;
112 		oid_str->value = (void *) bp;
113 		*minor_status = 0;
114 		return(GSS_S_COMPLETE);
115 	}
116 	*minor_status = ENOMEM;
117 	return(GSS_S_FAILURE);
118 }
119