Fixes for Heimdal (macOS) builds from Stefan.
[mech_eap.git] / mech_eap / util_oid.c
1 /*
2  * Copyright (c) 2011, 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 /*
58  * OID utility routines.
59  */
60
61 #include "gssapiP_eap.h"
62
63 OM_uint32
64 duplicateOid(OM_uint32 *minor,
65              const gss_OID_desc * const oid,
66              gss_OID *newOid)
67 {
68     gss_OID p;
69
70     *newOid = GSS_C_NO_OID;
71
72     p = (gss_OID)GSSEAP_MALLOC(sizeof(*p));
73     if (p == NULL) {
74         *minor = ENOMEM;
75         return GSS_S_FAILURE;
76     }
77     p->length = oid->length;
78     p->elements = GSSEAP_MALLOC(p->length);
79     if (p->elements == NULL) {
80         GSSEAP_FREE(p);
81         *minor = ENOMEM;
82         return GSS_S_FAILURE;
83     }
84
85     memcpy(p->elements, oid->elements, p->length);
86     *newOid = p;
87
88     *minor = 0;
89     return GSS_S_COMPLETE;
90 }
91
92 /* Compose an OID of a prefix and an integer suffix */
93 OM_uint32
94 composeOid(OM_uint32 *minor,
95            const char *prefix,
96            size_t prefix_len,
97            int suffix,
98            gss_OID_desc *oid)
99 {
100     int osuffix, i;
101     size_t nbytes;
102     unsigned char *op;
103
104     if (oid == GSS_C_NO_OID) {
105         *minor = EINVAL;
106         return GSS_S_CALL_INACCESSIBLE_READ | GSS_S_FAILURE;
107     }
108
109     if (oid->length < prefix_len) {
110         *minor = GSSEAP_WRONG_SIZE;
111         return GSS_S_FAILURE;
112     }
113
114     memcpy(oid->elements, prefix, prefix_len);
115
116     nbytes = 0;
117     osuffix = suffix;
118     while (suffix) {
119         nbytes++;
120         suffix >>= 7;
121     }
122     suffix = osuffix;
123
124     if (oid->length < prefix_len + nbytes) {
125         *minor = GSSEAP_WRONG_SIZE;
126         return GSS_S_FAILURE;
127     }
128
129     op = (unsigned char *) oid->elements + prefix_len + nbytes;
130     i = -1;
131     while (suffix) {
132         op[i] = (unsigned char)suffix & 0x7f;
133         if (i != -1)
134             op[i] |= 0x80;
135         i--;
136         suffix >>= 7;
137     }
138
139     oid->length = prefix_len + nbytes;
140
141     *minor = 0;
142     return GSS_S_COMPLETE;
143 }
144
145 OM_uint32
146 decomposeOid(OM_uint32 *minor,
147              const char *prefix,
148              size_t prefix_len,
149              gss_OID_desc *oid,
150              int *suffix)
151 {
152     size_t i, slen;
153     unsigned char *op;
154
155     if (oid->length < prefix_len ||
156         memcmp(oid->elements, prefix, prefix_len) != 0) {
157         return GSS_S_BAD_MECH;
158     }
159
160     op = (unsigned char *) oid->elements + prefix_len;
161
162     *suffix = 0;
163
164     slen = oid->length - prefix_len;
165
166     for (i = 0; i < slen; i++) {
167         *suffix = (*suffix << 7) | (op[i] & 0x7f);
168         if (i + 1 != slen && (op[i] & 0x80) == 0) {
169             *minor = GSSEAP_WRONG_SIZE;
170             return GSS_S_FAILURE;
171         }
172     }
173
174     return GSS_S_COMPLETE;
175 }
176
177 OM_uint32
178 duplicateOidSet(OM_uint32 *minor,
179                 const gss_OID_set src,
180                 gss_OID_set *dst)
181 {
182     OM_uint32 major, tmpMinor;
183     int i;
184
185     if (src == GSS_C_NO_OID_SET) {
186         *dst = GSS_C_NO_OID_SET;
187         return GSS_S_COMPLETE;
188     }
189
190     major = gss_create_empty_oid_set(minor, dst);
191     if (GSS_ERROR(major))
192         return major;
193
194     for (i = 0; i < src->count; i++) {
195         gss_OID oid = &src->elements[i];
196
197         major = gss_add_oid_set_member(minor, oid, dst);
198         if (GSS_ERROR(major))
199             break;
200     }
201
202     if (GSS_ERROR(major))
203         gss_release_oid_set(&tmpMinor, dst);
204
205     return major;
206 }
207
208 int
209 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
210 {
211     if (o1 == GSS_C_NO_OID)
212         return (o2 == GSS_C_NO_OID);
213     else if (o2 == GSS_C_NO_OID)
214         return (o1 == GSS_C_NO_OID);
215     else
216         return (o1->length == o2->length &&
217                 memcmp(o1->elements, o2->elements, o1->length) == 0);
218 }