1/*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "mech_locl.h"
35
36/**
37 * Turn an mech OID into an name
38 *
39 * Try to turn a OID into a mechanism name. If a matching OID can't be
40 * found, this function will return NULL.
41 *
42 * The caller must free the oid_str buffer with gss_release_buffer()
43 * when done with the string.
44 *
45 * @param minor_status an minor status code
46 * @param oid an oid
47 * @param oid_str buffer that will point to a NUL terminated string that is the numreric OID
48 *
49 * @returns a gss major status code
50 *
51 * @ingroup gssapi
52 */
53
54GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
55gss_oid_to_str(OM_uint32 *minor_status, gss_OID oid, gss_buffer_t oid_str)
56{
57    int ret;
58    size_t size;
59    heim_oid o;
60    char *p;
61
62    _mg_buffer_zero(oid_str);
63
64    if (oid == GSS_C_NULL_OID)
65	return GSS_S_FAILURE;
66
67    ret = der_get_oid (oid->elements, oid->length, &o, &size);
68    if (ret) {
69	*minor_status = ret;
70	return GSS_S_FAILURE;
71    }
72
73    ret = der_print_heim_oid(&o, ' ', &p);
74    der_free_oid(&o);
75    if (ret) {
76	*minor_status = ret;
77	return GSS_S_FAILURE;
78    }
79
80    oid_str->value = p;
81    oid_str->length = strlen(p);
82
83    *minor_status = 0;
84    return GSS_S_COMPLETE;
85}
86
87/**
88 * Turn an mech OID into an name
89 *
90 * Try to turn a OID into a mechanism name. If a matching OID can't be
91 * found, this function will return NULL.
92 *
93 * @param oid an mechanism oid
94 *
95 * @returns pointer a static strng to a name or NULL when not found.
96 *
97 * @ingroup gssapi
98 */
99
100GSSAPI_LIB_FUNCTION const char * GSSAPI_LIB_CALL
101gss_oid_to_name(gss_const_OID oid)
102{
103    size_t i;
104
105    for (i = 0; _gss_ont_mech[i].oid; i++) {
106	if (gss_oid_equal(oid, _gss_ont_mech[i].oid)) {
107	    if (_gss_ont_mech[i].short_desc)
108		return _gss_ont_mech[i].short_desc;
109	    return _gss_ont_mech[i].name;
110	}
111    }
112    return NULL;
113}
114
115/**
116 * Turn an mech name into an OID
117 *
118 * Try to turn a string/name into an OID. If a static OID can't be
119 * found, this function will return NULL.
120 *
121 * WARNING: do _NOT_ call gss_release_oid() on the OID returned.
122 *
123 * @param name name of a OID
124 *
125 * @returns pointer a static OID or GSS_C_NO_OID when not found.
126 *
127 *
128 * @ingroup gssapi
129 */
130
131GSSAPI_LIB_FUNCTION gss_const_OID GSSAPI_LIB_CALL
132gss_name_to_oid(const char *name)
133{
134    size_t i, partial = (size_t)-1;
135
136    for (i = 0; _gss_ont_mech[i].oid; i++) {
137	if (strcasecmp(name, _gss_ont_mech[i].short_desc) == 0)
138	    return _gss_ont_mech[i].oid;
139	if (strncasecmp(name, _gss_ont_mech[i].short_desc, strlen(name)) == 0) {
140	    if (partial != (size_t)-1)
141		return NULL;
142	    partial = i;
143	}
144    }
145    if (partial != (size_t)-1)
146	return _gss_ont_mech[partial].oid;
147
148    return NULL;
149}
150