guess acceptor service name if none specified
[mech_eap.orig] / util_oid.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Copyright 1995-2010 by the Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  *
55  */
56
57 #include "gssapiP_eap.h"
58
59 OM_uint32
60 duplicateOid(OM_uint32 *minor,
61              const gss_OID_desc * const oid,
62              gss_OID *newOid)
63 {
64     gss_OID p;
65
66     *newOid = GSS_C_NO_OID;
67
68     p = (gss_OID)GSSEAP_MALLOC(sizeof(*p));
69     if (p == NULL) {
70         *minor = ENOMEM;
71         return GSS_S_FAILURE;
72     }
73     p->length = oid->length;
74     p->elements = GSSEAP_MALLOC(p->length);
75     if (p->elements == NULL) {
76         GSSEAP_FREE(p);
77         *minor = ENOMEM;
78         return GSS_S_FAILURE;
79     }
80
81     memcpy(p->elements, oid->elements, p->length);
82     *newOid = p;
83
84     *minor = 0;
85     return GSS_S_COMPLETE;
86 }
87
88 /* Compose an OID of a prefix and an integer suffix */
89 OM_uint32
90 composeOid(OM_uint32 *minor,
91            const char *prefix,
92            size_t prefix_len,
93            int suffix,
94            gss_OID_desc *oid)
95 {
96     int osuffix, i;
97     size_t nbytes;
98     unsigned char *op;
99
100     if (oid == GSS_C_NO_OID) {
101         *minor = EINVAL;
102         return GSS_S_FAILURE;
103     }
104     if (oid->length < prefix_len) {
105         *minor = GSSEAP_WRONG_SIZE;
106         return GSS_S_FAILURE;
107     }
108
109     memcpy(oid->elements, prefix, prefix_len);
110
111     nbytes = 0;
112     osuffix = suffix;
113     while (suffix) {
114         nbytes++;
115         suffix >>= 7;
116     }
117     suffix = osuffix;
118
119     if (oid->length < prefix_len + nbytes) {
120         *minor = GSSEAP_WRONG_SIZE;
121         return GSS_S_FAILURE;
122     }
123
124     op = (unsigned char *) oid->elements + prefix_len + nbytes;
125     i = -1;
126     while (suffix) {
127         op[i] = (unsigned char)suffix & 0x7f;
128         if (i != -1)
129             op[i] |= 0x80;
130         i--;
131         suffix >>= 7;
132     }
133
134     oid->length = prefix_len + nbytes;
135
136     *minor = 0;
137     return GSS_S_COMPLETE;
138 }
139
140 OM_uint32
141 decomposeOid(OM_uint32 *minor,
142              const char *prefix,
143              size_t prefix_len,
144              gss_OID_desc *oid,
145              int *suffix)
146 {
147     size_t i, slen;
148     unsigned char *op;
149
150     if (oid->length < prefix_len ||
151         memcmp(oid->elements, prefix, prefix_len) != 0) {
152         return GSS_S_BAD_MECH;
153     }
154
155     op = (unsigned char *) oid->elements + prefix_len;
156
157     *suffix = 0;
158
159     slen = oid->length - prefix_len;
160
161     for (i = 0; i < slen; i++) {
162         *suffix = (*suffix << 7) | (op[i] & 0x7f);
163         if (i + 1 != slen && (op[i] & 0x80) == 0) {
164             *minor = GSSEAP_WRONG_SIZE;
165             return GSS_S_FAILURE;
166         }
167     }
168
169     return GSS_S_COMPLETE;
170 }
171
172 OM_uint32
173 duplicateOidSet(OM_uint32 *minor,
174                 const gss_OID_set src,
175                 gss_OID_set *dst)
176 {
177     OM_uint32 major, tmpMinor;
178     int i;
179
180     if (src == GSS_C_NO_OID_SET) {
181         *dst = GSS_C_NO_OID_SET;
182         return GSS_S_COMPLETE;
183     }
184
185     major = gss_create_empty_oid_set(minor, dst);
186     if (GSS_ERROR(major))
187         return major;
188
189     for (i = 0; i < src->count; i++) {
190         gss_OID oid = &src->elements[i];
191
192         major = gss_add_oid_set_member(minor, oid, dst);
193         if (GSS_ERROR(major))
194             break;
195     }
196
197     if (GSS_ERROR(major))
198         gss_release_oid_set(&tmpMinor, dst);
199
200     return major;
201 }