xref: /freebsd/lib/libgssapi/gss_oid_to_str.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
133f12199SDoug Rabson /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
433f12199SDoug Rabson  * Copyright (c) 2000 The Regents of the University of Michigan.
533f12199SDoug Rabson  * All rights reserved.
633f12199SDoug Rabson  *
733f12199SDoug Rabson  * Copyright (c) 2000 Dug Song <dugsong@UMICH.EDU>.
833f12199SDoug Rabson  * All rights reserved, all wrongs reversed.
933f12199SDoug Rabson  *
1033f12199SDoug Rabson  * Redistribution and use in source and binary forms, with or without
1133f12199SDoug Rabson  * modification, are permitted provided that the following conditions
1233f12199SDoug Rabson  * are met:
1333f12199SDoug Rabson  *
1433f12199SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1533f12199SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1633f12199SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1733f12199SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1833f12199SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1933f12199SDoug Rabson  * 3. Neither the name of the University nor the names of its
2033f12199SDoug Rabson  *    contributors may be used to endorse or promote products derived
2133f12199SDoug Rabson  *    from this software without specific prior written permission.
2233f12199SDoug Rabson  *
2333f12199SDoug Rabson  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
2433f12199SDoug Rabson  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2533f12199SDoug Rabson  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2633f12199SDoug Rabson  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2733f12199SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2833f12199SDoug Rabson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2933f12199SDoug Rabson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
3033f12199SDoug Rabson  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3133f12199SDoug Rabson  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3233f12199SDoug Rabson  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3333f12199SDoug Rabson  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3433f12199SDoug Rabson  */
3533f12199SDoug Rabson 
3633f12199SDoug Rabson #include <gssapi/gssapi.h>
3733f12199SDoug Rabson #include <stdio.h>
3833f12199SDoug Rabson #include <stdlib.h>
3933f12199SDoug Rabson #include <string.h>
4033f12199SDoug Rabson #include <errno.h>
4133f12199SDoug Rabson 
4233f12199SDoug Rabson #include "utils.h"
4333f12199SDoug Rabson 
4433f12199SDoug Rabson OM_uint32
gss_oid_to_str(OM_uint32 * minor_status,gss_OID oid,gss_buffer_t oid_str)4533f12199SDoug Rabson gss_oid_to_str(OM_uint32 *minor_status, gss_OID oid, gss_buffer_t oid_str)
4633f12199SDoug Rabson {
4733f12199SDoug Rabson 	char		numstr[128];
4833f12199SDoug Rabson 	unsigned long	number;
4933f12199SDoug Rabson 	int		numshift;
5033f12199SDoug Rabson 	size_t		string_length;
5133f12199SDoug Rabson 	size_t		i;
5233f12199SDoug Rabson 	unsigned char	*cp;
5333f12199SDoug Rabson 	char		*bp;
5433f12199SDoug Rabson 
5533f12199SDoug Rabson 	*minor_status = 0;
5633f12199SDoug Rabson 	_gss_buffer_zero(oid_str);
5733f12199SDoug Rabson 
5833f12199SDoug Rabson 	if (oid == GSS_C_NULL_OID)
5933f12199SDoug Rabson 		return (GSS_S_FAILURE);
6033f12199SDoug Rabson 
6133f12199SDoug Rabson 	/* Decoded according to krb5/gssapi_krb5.c */
6233f12199SDoug Rabson 
6333f12199SDoug Rabson 	/* First determine the size of the string */
6433f12199SDoug Rabson 	string_length = 0;
6533f12199SDoug Rabson 	number = 0;
6633f12199SDoug Rabson 	numshift = 0;
6733f12199SDoug Rabson 	cp = (unsigned char *) oid->elements;
6833f12199SDoug Rabson 	number = (unsigned long) cp[0];
6933f12199SDoug Rabson 	sprintf(numstr, "%ld ", number/40);
7033f12199SDoug Rabson 	string_length += strlen(numstr);
7133f12199SDoug Rabson 	sprintf(numstr, "%ld ", number%40);
7233f12199SDoug Rabson 	string_length += strlen(numstr);
7333f12199SDoug Rabson 	for (i=1; i<oid->length; i++) {
7433f12199SDoug Rabson 		if ( (size_t) (numshift+7) < (sizeof(unsigned long)*8)) {
7533f12199SDoug Rabson 			number = (number << 7) | (cp[i] & 0x7f);
7633f12199SDoug Rabson 			numshift += 7;
7733f12199SDoug Rabson 		}
7833f12199SDoug Rabson 		else {
7933f12199SDoug Rabson 			*minor_status = 0;
8033f12199SDoug Rabson 			return(GSS_S_FAILURE);
8133f12199SDoug Rabson 		}
8233f12199SDoug Rabson 		if ((cp[i] & 0x80) == 0) {
8333f12199SDoug Rabson 			sprintf(numstr, "%ld ", number);
8433f12199SDoug Rabson 			string_length += strlen(numstr);
8533f12199SDoug Rabson 			number = 0;
8633f12199SDoug Rabson 			numshift = 0;
8733f12199SDoug Rabson 		}
8833f12199SDoug Rabson 	}
8933f12199SDoug Rabson 	/*
9033f12199SDoug Rabson 	 * If we get here, we've calculated the length of "n n n ... n ".
9133f12199SDoug Rabson 	 * Add 4 here for "{ " and "}\0".
9233f12199SDoug Rabson 	 */
9333f12199SDoug Rabson 	string_length += 4;
9433f12199SDoug Rabson 	if ((bp = (char *) malloc(string_length))) {
9533f12199SDoug Rabson 		strcpy(bp, "{ ");
9633f12199SDoug Rabson 		number = (unsigned long) cp[0];
9733f12199SDoug Rabson 		sprintf(numstr, "%ld ", number/40);
9833f12199SDoug Rabson 		strcat(bp, numstr);
9933f12199SDoug Rabson 		sprintf(numstr, "%ld ", number%40);
10033f12199SDoug Rabson 		strcat(bp, numstr);
10133f12199SDoug Rabson 		number = 0;
10233f12199SDoug Rabson 		cp = (unsigned char *) oid->elements;
10333f12199SDoug Rabson 		for (i=1; i<oid->length; i++) {
10433f12199SDoug Rabson 			number = (number << 7) | (cp[i] & 0x7f);
10533f12199SDoug Rabson 			if ((cp[i] & 0x80) == 0) {
10633f12199SDoug Rabson 				sprintf(numstr, "%ld ", number);
10733f12199SDoug Rabson 				strcat(bp, numstr);
10833f12199SDoug Rabson 				number = 0;
10933f12199SDoug Rabson 			}
11033f12199SDoug Rabson 		}
11133f12199SDoug Rabson 		strcat(bp, "}");
11233f12199SDoug Rabson 		oid_str->length = strlen(bp)+1;
11333f12199SDoug Rabson 		oid_str->value = (void *) bp;
11433f12199SDoug Rabson 		*minor_status = 0;
11533f12199SDoug Rabson 		return(GSS_S_COMPLETE);
11633f12199SDoug Rabson 	}
11733f12199SDoug Rabson 	*minor_status = ENOMEM;
11833f12199SDoug Rabson 	return(GSS_S_FAILURE);
11933f12199SDoug Rabson }
120