Update name OIDs
[mech_eap.git] / mech_eap / util_mech.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 /*
34  * General mechanism utility routines.
35  */
36
37 #include "gssapiP_eap.h"
38
39 /*
40  * Many OIDs are taken from 1.3.6.1.4.1.5322(padl)
41  *      gssEap(22)
42  *       mechanisms(1) (no longer used)
43  *        eap-aes128-cts-hmac-sha1-96(17)
44  *        eap-aes256-cts-hmac-sha1-96(18)
45  *       nameTypes(2) (no longer used)
46  *       apiExtensions(3)
47  *        inquireSecContextByOid(1)
48  *        inquireCredByOid(2)
49  *        setSecContextOption(3)
50  *        setCredOption(4)
51  *        mechInvoke(5)
52  *        Mechanisms and name types are now taken from
53  *      http://www.iana.org/assignments/smi-numbers . See Prefix:
54  *      iso.org.dod.internet.security.mechanisms.abfab (1.3.6.1.5.5.15)
55  */
56
57 /*
58  * Note: the enctype-less OID is used as the mechanism OID in non-
59  * canonicalized exported names.
60  */
61 static gss_OID_desc gssEapMechOids[] = {
62     /* 1.3.6.1.5.5.15.1.1  */
63     { 8, "\x2B\x06\x01\x05\x05\x0f\x01\x01" },
64     /* 1.3.6.1.5.5.15.1.1.17  */
65     { 9, "\x2B\x06\x01\x05\x05\x0f\x01\x01\x11" },
66     /* 1.3.6.1.5.5.15.1.1.18  */
67     { 9, "\x2B\x06\x01\x05\x05\x0f\x01\x01\x12" },
68 };
69
70 gss_OID GSS_EAP_MECHANISM                            = &gssEapMechOids[0];
71 gss_OID GSS_EAP_AES128_CTS_HMAC_SHA1_96_MECHANISM    = &gssEapMechOids[1];
72 gss_OID GSS_EAP_AES256_CTS_HMAC_SHA1_96_MECHANISM    = &gssEapMechOids[2];
73
74 static int
75 internalizeOid(const gss_OID oid,
76                gss_OID *const pInternalizedOid);
77
78 /*
79  * Returns TRUE is the OID is a concrete mechanism OID, that is, one
80  * with a Kerberos enctype as the last element.
81  */
82 int
83 gssEapIsConcreteMechanismOid(const gss_OID oid)
84 {
85     return oid->length > GSS_EAP_MECHANISM->length &&
86            memcmp(oid->elements, GSS_EAP_MECHANISM->elements,
87                   GSS_EAP_MECHANISM->length) == 0;
88 }
89
90 int
91 gssEapIsMechanismOid(const gss_OID oid)
92 {
93     return oid == GSS_C_NO_OID ||
94            oidEqual(oid, GSS_EAP_MECHANISM) ||
95            gssEapIsConcreteMechanismOid(oid);
96 }
97
98 /*
99  * Validate that all elements are concrete mechanism OIDs.
100  */
101 OM_uint32
102 gssEapValidateMechs(OM_uint32 *minor,
103                     const gss_OID_set mechs)
104 {
105     int i;
106
107     *minor = 0;
108
109     if (mechs == GSS_C_NO_OID_SET) {
110         return GSS_S_COMPLETE;
111     }
112
113     for (i = 0; i < mechs->count; i++) {
114         gss_OID oid = &mechs->elements[i];
115
116         if (!gssEapIsConcreteMechanismOid(oid)) {
117             *minor = GSSEAP_WRONG_MECH;
118             return GSS_S_BAD_MECH;
119         }
120     }
121
122     return GSS_S_COMPLETE;
123 }
124
125 OM_uint32
126 gssEapOidToEnctype(OM_uint32 *minor,
127                    const gss_OID oid,
128                    krb5_enctype *enctype)
129 {
130     OM_uint32 major;
131     int suffix;
132
133     major = decomposeOid(minor,
134                          GSS_EAP_MECHANISM->elements,
135                          GSS_EAP_MECHANISM->length,
136                          oid,
137                          &suffix);
138     if (major == GSS_S_COMPLETE)
139         *enctype = suffix;
140
141     return major;
142 }
143
144 OM_uint32
145 gssEapEnctypeToOid(OM_uint32 *minor,
146                    krb5_enctype enctype,
147                    gss_OID *pOid)
148 {
149     OM_uint32 major;
150     gss_OID oid;
151
152     *pOid = NULL;
153
154     oid = (gss_OID)GSSEAP_MALLOC(sizeof(*oid));
155     if (oid == NULL) {
156         *minor = ENOMEM;
157         return GSS_S_FAILURE;
158     }
159
160     oid->length = GSS_EAP_MECHANISM->length + 1;
161     oid->elements = GSSEAP_MALLOC(oid->length);
162     if (oid->elements == NULL) {
163         *minor = ENOMEM;
164         GSSEAP_FREE(oid);
165         return GSS_S_FAILURE;
166     }
167
168     major = composeOid(minor,
169                        GSS_EAP_MECHANISM->elements,
170                        GSS_EAP_MECHANISM->length,
171                        enctype,
172                        oid);
173     if (major == GSS_S_COMPLETE) {
174         internalizeOid(oid, pOid);
175         *pOid = oid;
176     } else {
177         GSSEAP_FREE(oid->elements);
178         GSSEAP_FREE(oid);
179     }
180
181     return major;
182 }
183
184 OM_uint32
185 gssEapIndicateMechs(OM_uint32 *minor,
186                     gss_OID_set *mechs)
187 {
188     krb5_context krbContext;
189     OM_uint32 major;
190     krb5_enctype *etypes;
191     int i;
192
193     GSSEAP_KRB_INIT(&krbContext);
194
195     *minor = krb5_get_permitted_enctypes(krbContext, &etypes);
196     if (*minor != 0) {
197         return GSS_S_FAILURE;
198     }
199
200     major = gss_create_empty_oid_set(minor, mechs);
201     if (GSS_ERROR(major)) {
202         GSSEAP_FREE(etypes);
203         return major;
204     }
205
206     for (i = 0; etypes[i] != ENCTYPE_NULL; i++) {
207         gss_OID mechOid;
208 #ifndef HAVE_HEIMDAL_VERSION
209         OM_uint32 tmpMinor;
210 #endif
211
212         /* XXX currently we aren't equipped to encode these enctypes */
213         if (etypes[i] < 0 || etypes[i] > 127)
214             continue;
215
216         major = gssEapEnctypeToOid(minor, etypes[i], &mechOid);
217         if (GSS_ERROR(major))
218             break;
219
220         major = gss_add_oid_set_member(minor, mechOid, mechs);
221         if (GSS_ERROR(major))
222             break;
223
224 #ifndef HAVE_HEIMDAL_VERSION
225         gss_release_oid(&tmpMinor, &mechOid);
226 #endif
227     }
228
229     GSSEAP_FREE(etypes);
230
231     *minor = 0;
232     return major;
233 }
234
235 OM_uint32
236 gssEapDefaultMech(OM_uint32 *minor,
237                   gss_OID *oid)
238 {
239     gss_OID_set mechs;
240     OM_uint32 major, tmpMinor;
241
242     major = gssEapIndicateMechs(minor, &mechs);
243     if (GSS_ERROR(major)) {
244         return major;
245     }
246
247     if (mechs->count == 0) {
248         gss_release_oid_set(&tmpMinor, &mechs);
249         return GSS_S_BAD_MECH;
250     }
251
252     if (!internalizeOid(&mechs->elements[0], oid)) {
253         /* don't double-free if we didn't internalize it */
254         mechs->elements[0].length = 0;
255         mechs->elements[0].elements = NULL;
256     }
257
258     gss_release_oid_set(&tmpMinor, &mechs);
259
260     *minor = 0;
261     return GSS_S_COMPLETE;
262 }
263
264 static int
265 internalizeOid(const gss_OID oid,
266                gss_OID *const pInternalizedOid)
267 {
268     int i;
269
270     *pInternalizedOid = GSS_C_NO_OID;
271
272     for (i = 0;
273          i < sizeof(gssEapMechOids) / sizeof(gssEapMechOids[0]);
274          i++) {
275         if (oidEqual(oid, &gssEapMechOids[i])) {
276             *pInternalizedOid = (const gss_OID)&gssEapMechOids[i];
277             break;
278         }
279     }
280
281     if (*pInternalizedOid == GSS_C_NO_OID) {
282         if (oidEqual(oid, GSS_EAP_NT_EAP_NAME))
283             *pInternalizedOid = (const gss_OID)GSS_EAP_NT_EAP_NAME;
284     }
285
286     if (*pInternalizedOid == GSS_C_NO_OID) {
287         *pInternalizedOid = oid;
288         return 0;
289     }
290
291     return 1;
292 }
293
294 OM_uint32
295 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid)
296 {
297     gss_OID internalizedOid = GSS_C_NO_OID;
298
299     *minor = 0;
300
301     if (internalizeOid(*oid, &internalizedOid)) {
302         /* OID was internalized, so we can mark it as "freed" */
303         *oid = GSS_C_NO_OID;
304         return GSS_S_COMPLETE;
305     }
306
307     /* we don't know about this OID */
308     return GSS_S_CONTINUE_NEEDED;
309 }
310
311 OM_uint32
312 gssEapCanonicalizeOid(OM_uint32 *minor,
313                       const gss_OID oid,
314                       OM_uint32 flags,
315                       gss_OID *pOid)
316 {
317     OM_uint32 major;
318     int mapToNull = 0;
319
320     major = GSS_S_COMPLETE;
321     *minor = 0;
322     *pOid = GSS_C_NULL_OID;
323
324     if (oid == GSS_C_NULL_OID) {
325         if ((flags & OID_FLAG_NULL_VALID) == 0) {
326             *minor = GSSEAP_WRONG_MECH;
327             return GSS_S_BAD_MECH;
328         } else if (flags & OID_FLAG_MAP_NULL_TO_DEFAULT_MECH) {
329             return gssEapDefaultMech(minor, pOid);
330         } else {
331             mapToNull = 1;
332         }
333     } else if (oidEqual(oid, GSS_EAP_MECHANISM)) {
334         if ((flags & OID_FLAG_FAMILY_MECH_VALID) == 0) {
335             *minor = GSSEAP_WRONG_MECH;
336             return GSS_S_BAD_MECH;
337         } else if (flags & OID_FLAG_MAP_FAMILY_MECH_TO_NULL) {
338             mapToNull = 1;
339         }
340     } else if (!gssEapIsConcreteMechanismOid(oid)) {
341         *minor = GSSEAP_WRONG_MECH;
342         return GSS_S_BAD_MECH;
343     }
344
345     if (!mapToNull) {
346         if (!internalizeOid(oid, pOid))
347             major = duplicateOid(minor, oid, pOid);
348     }
349
350     return major;
351 }
352
353 static gss_buffer_desc gssEapSaslMechs[] = {
354     { sizeof("EAP") - 1,        "EAP",       }, /* not used */
355     { sizeof("EAP-AES128") - 1, "EAP-AES128" },
356     { sizeof("EAP-AES256") - 1, "EAP-AES256" },
357 };
358
359 gss_buffer_t
360 gssEapOidToSaslName(const gss_OID oid)
361 {
362     size_t i;
363
364     for (i = 1; i < sizeof(gssEapMechOids)/sizeof(gssEapMechOids[0]); i++) {
365         if (oidEqual(&gssEapMechOids[i], oid))
366             return &gssEapSaslMechs[i];
367     }
368
369     return GSS_C_NO_BUFFER;
370 }
371
372 gss_OID
373 gssEapSaslNameToOid(const gss_buffer_t name)
374 {
375     size_t i;
376
377     for (i = 1; i < sizeof(gssEapSaslMechs)/sizeof(gssEapSaslMechs[0]); i++) {
378         if (bufferEqual(&gssEapSaslMechs[i], name))
379             return &gssEapMechOids[i];
380     }
381
382     return GSS_C_NO_OID;
383 }