Add some OID manipulation functions
[mech_eap.git] / 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 #if 0
60 OM_uint32
61 copyOid(OM_uint32 *minor_status,
62         const gss_OID_desc * const oid,
63         gss_OID *new_oid)
64 {
65     gss_OID         p;
66
67     *minor_status = 0;
68
69     p = (gss_OID) malloc(sizeof(gss_OID_desc));
70     if (!p) {
71         *minor_status = ENOMEM;
72         return GSS_S_FAILURE;
73     }
74     p->length = oid->length;
75     p->elements = malloc(p->length);
76     if (!p->elements) {
77         free(p);
78         return GSS_S_FAILURE;
79     }
80     memcpy(p->elements, oid->elements, p->length);
81     *new_oid = p;
82     return(GSS_S_COMPLETE);
83 }
84 #endif
85
86 /* Compose an OID of a prefix and an integer suffix */
87 OM_uint32
88 composeOid(OM_uint32 *minor_status,
89            const char *prefix,
90            size_t prefix_len,
91            int suffix,
92            gss_OID_desc *oid)
93 {
94     int osuffix, i;
95     size_t nbytes;
96     unsigned char *op;
97
98     if (oid == GSS_C_NO_OID) {
99         *minor_status = EINVAL;
100         return GSS_S_FAILURE;
101     }
102     if (oid->length < prefix_len) {
103         *minor_status = ERANGE;
104         return GSS_S_FAILURE;
105     }
106
107     memcpy(oid->elements, prefix, prefix_len);
108
109     nbytes = 0;
110     osuffix = suffix;
111     while (suffix) {
112         nbytes++;
113         suffix >>= 7;
114     }
115     suffix = osuffix;
116
117     if (oid->length < prefix_len + nbytes) {
118         *minor_status = ERANGE;
119         return GSS_S_FAILURE;
120     }
121
122     op = (unsigned char *) oid->elements + prefix_len + nbytes;
123     i = -1;
124     while (suffix) {
125         op[i] = (unsigned char)suffix & 0x7f;
126         if (i != -1)
127             op[i] |= 0x80;
128         i--;
129         suffix >>= 7;
130     }
131
132     oid->length = prefix_len + nbytes;
133
134     *minor_status = 0;
135     return GSS_S_COMPLETE;
136 }
137
138 OM_uint32
139 decomposeOid(OM_uint32 *minor_status,
140              const char *prefix,
141              size_t prefix_len,
142              gss_OID_desc *oid,
143              int *suffix)
144 {
145     size_t i, slen;
146     unsigned char *op;
147
148     if (oid->length < prefix_len ||
149         memcmp(oid->elements, prefix, prefix_len) != 0) {
150         return GSS_S_BAD_MECH;
151     }
152
153     op = (unsigned char *) oid->elements + prefix_len;
154
155     *suffix = 0;
156
157     slen = oid->length - prefix_len;
158
159     for (i = 0; i < slen; i++) {
160         *suffix = (*suffix << 7) | (op[i] & 0x7f);
161         if (i + 1 != slen && (op[i] & 0x80) == 0) {
162             *minor_status = EINVAL;
163             return GSS_S_FAILURE;
164         }
165     }
166
167     return GSS_S_COMPLETE;
168 }
169
170 #if 0
171 OM_uint32
172 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid)
173 {
174     OM_uint32 major;
175
176     major = gss_internal_release_oid(minor, oid);
177     if (major == GSS_S_CONTINUE_NEEDED) {
178         GSSEAP_FREE(oid->elements);
179         GSSEAP_FREE(oid);
180         *oid = GSS_C_NO_OID;
181         major = GSS_S_COMPLETE;
182     }
183
184     return major;
185 }
186 #endif