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