xref: /freebsd/lib/libgssapi/gss_oid_to_str.c (revision 02e9120893770924227138ba49df1edb3896112a)
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 
36 #include <gssapi/gssapi.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 
42 #include "utils.h"
43 
44 OM_uint32
45 gss_oid_to_str(OM_uint32 *minor_status, gss_OID oid, gss_buffer_t oid_str)
46 {
47 	char		numstr[128];
48 	unsigned long	number;
49 	int		numshift;
50 	size_t		string_length;
51 	size_t		i;
52 	unsigned char	*cp;
53 	char		*bp;
54 
55 	*minor_status = 0;
56 	_gss_buffer_zero(oid_str);
57 
58 	if (oid == GSS_C_NULL_OID)
59 		return (GSS_S_FAILURE);
60 
61 	/* Decoded according to krb5/gssapi_krb5.c */
62 
63 	/* First determine the size of the string */
64 	string_length = 0;
65 	number = 0;
66 	numshift = 0;
67 	cp = (unsigned char *) oid->elements;
68 	number = (unsigned long) cp[0];
69 	sprintf(numstr, "%ld ", number/40);
70 	string_length += strlen(numstr);
71 	sprintf(numstr, "%ld ", number%40);
72 	string_length += strlen(numstr);
73 	for (i=1; i<oid->length; i++) {
74 		if ( (size_t) (numshift+7) < (sizeof(unsigned long)*8)) {
75 			number = (number << 7) | (cp[i] & 0x7f);
76 			numshift += 7;
77 		}
78 		else {
79 			*minor_status = 0;
80 			return(GSS_S_FAILURE);
81 		}
82 		if ((cp[i] & 0x80) == 0) {
83 			sprintf(numstr, "%ld ", number);
84 			string_length += strlen(numstr);
85 			number = 0;
86 			numshift = 0;
87 		}
88 	}
89 	/*
90 	 * If we get here, we've calculated the length of "n n n ... n ".
91 	 * Add 4 here for "{ " and "}\0".
92 	 */
93 	string_length += 4;
94 	if ((bp = (char *) malloc(string_length))) {
95 		strcpy(bp, "{ ");
96 		number = (unsigned long) cp[0];
97 		sprintf(numstr, "%ld ", number/40);
98 		strcat(bp, numstr);
99 		sprintf(numstr, "%ld ", number%40);
100 		strcat(bp, numstr);
101 		number = 0;
102 		cp = (unsigned char *) oid->elements;
103 		for (i=1; i<oid->length; i++) {
104 			number = (number << 7) | (cp[i] & 0x7f);
105 			if ((cp[i] & 0x80) == 0) {
106 				sprintf(numstr, "%ld ", number);
107 				strcat(bp, numstr);
108 				number = 0;
109 			}
110 		}
111 		strcat(bp, "}");
112 		oid_str->length = strlen(bp)+1;
113 		oid_str->value = (void *) bp;
114 		*minor_status = 0;
115 		return(GSS_S_COMPLETE);
116 	}
117 	*minor_status = ENOMEM;
118 	return(GSS_S_FAILURE);
119 }
120