call gssEapCanonicalizeOid, gssEapInternalizeOid is no longer public
[mech_eap.orig] / 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  * 1.3.6.1.4.1.5322(padl)
41  *      gssEap(22)
42  *       mechanisms(1)
43  *        eap-aes128-cts-hmac-sha1-96(17)
44  *        eap-aes256-cts-hmac-sha1-96(18)
45  *       nameTypes(2)
46  *       apiExtensions(3)
47  *        inquireSecContextByOid(1)
48  *        inquireCredByOid(2)
49  *        setSecContextOption(3)
50  *        setCredOption(4)
51  *        mechInvoke(5)
52  */
53
54 /*
55  * Note: the enctype-less OID is used as the mechanism OID in exported
56  * names. There is no public symbol for it. This is consistent with
57  * the krb5 mechanism which, whilst known by many OIDs, always uses a
58  * canonical OID for exported names. (This OID is also returned by
59  * gss_inquire_name.)
60  */
61 static gss_OID_desc gssEapMechOids[] = {
62     /* 1.3.6.1.4.1.5322.22.1  */
63     { 9, "\x2B\x06\x01\x04\x01\xA9\x4A\x16\x01" },
64     /* 1.3.6.1.4.1.5322.22.1.17 */
65     { 10, "\x2B\x06\x01\x04\x01\xA9\x4A\x16\x01\x11" },
66     /* 1.3.6.1.4.1.5322.22.1.18 */
67     { 10, "\x2B\x06\x01\x04\x01\xA9\x4A\x16\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, tmpMinor;
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
209         /* XXX currently we aren't equipped to encode these enctypes */
210         if (etypes[i] < 0 || etypes[i] > 127)
211             continue;
212
213         major = gssEapEnctypeToOid(minor, etypes[i], &mechOid);
214         if (GSS_ERROR(major))
215             break;
216
217         major = gss_add_oid_set_member(minor, mechOid, mechs);
218         if (GSS_ERROR(major))
219             break;
220
221         gss_release_oid(&tmpMinor, &mechOid);
222     }
223
224     GSSEAP_FREE(etypes);
225
226     *minor = 0;
227     return major;
228 }
229
230 OM_uint32
231 gssEapDefaultMech(OM_uint32 *minor,
232                   gss_OID *oid)
233 {
234     gss_OID_set mechs;
235     OM_uint32 major, tmpMinor;
236
237     major = gssEapIndicateMechs(minor, &mechs);
238     if (GSS_ERROR(major)) {
239         return major;
240     }
241
242     if (mechs->count == 0) {
243         gss_release_oid_set(&tmpMinor, &mechs);
244         return GSS_S_BAD_MECH;
245     }
246
247     if (!internalizeOid(&mechs->elements[0], oid)) {
248         /* don't double-free if we didn't internalize it */
249         mechs->elements[0].length = 0;
250         mechs->elements[0].elements = NULL;
251     }
252
253     gss_release_oid_set(&tmpMinor, &mechs);
254
255     *minor = 0;
256     return GSS_S_COMPLETE;
257 }
258
259 static int
260 internalizeOid(const gss_OID oid,
261                gss_OID *const pInternalizedOid)
262 {
263     int i;
264
265     *pInternalizedOid = GSS_C_NO_OID;
266
267     for (i = 0;
268          i < sizeof(gssEapMechOids) / sizeof(gssEapMechOids[0]);
269          i++) {
270         if (oidEqual(oid, &gssEapMechOids[i])) {
271             *pInternalizedOid = (const gss_OID)&gssEapMechOids[i];
272             break;
273         }
274     }
275
276     if (*pInternalizedOid == GSS_C_NO_OID) {
277         if (oidEqual(oid, GSS_EAP_NT_PRINCIPAL_NAME))
278             *pInternalizedOid = (const gss_OID)GSS_EAP_NT_PRINCIPAL_NAME;
279     }
280
281     if (*pInternalizedOid == GSS_C_NO_OID) {
282         *pInternalizedOid = oid;
283         return 0;
284     }
285
286     return 1;
287 }
288
289 OM_uint32
290 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid)
291 {
292     gss_OID internalizedOid = GSS_C_NO_OID;
293
294     *minor = 0;
295
296     if (internalizeOid(*oid, &internalizedOid)) {
297         /* OID was internalized, so we can mark it as "freed" */
298         *oid = GSS_C_NO_OID;
299         return GSS_S_COMPLETE;
300     }
301
302     /* we don't know about this OID */
303     return GSS_S_CONTINUE_NEEDED;
304 }
305
306 OM_uint32
307 gssEapCanonicalizeOid(OM_uint32 *minor,
308                       const gss_OID oid,
309                       OM_uint32 flags,
310                       gss_OID *pOid)
311 {
312     OM_uint32 major;
313     int mapToNull = 0;
314
315     major = GSS_S_COMPLETE;
316     *minor = 0;
317     *pOid = GSS_C_NULL_OID;
318
319     if (oid == GSS_C_NULL_OID) {
320         if ((flags & OID_FLAG_NULL_VALID) == 0) {
321             *minor = GSSEAP_WRONG_MECH;
322             return GSS_S_BAD_MECH;
323         } else if (flags & OID_FLAG_MAP_NULL_TO_DEFAULT_MECH) {
324             return gssEapDefaultMech(minor, pOid);
325         }
326     } else if (oidEqual(oid, GSS_EAP_MECHANISM)) {
327         if ((flags & OID_FLAG_FAMILY_MECH_VALID) == 0) {
328             *minor = GSSEAP_WRONG_MECH;
329             return GSS_S_BAD_MECH;
330         } else if (flags & OID_FLAG_MAP_FAMILY_MECH_TO_NULL) {
331             mapToNull = 1;
332         }
333     } else if (!gssEapIsConcreteMechanismOid(oid)) {
334         *minor = GSSEAP_WRONG_MECH;
335         return GSS_S_BAD_MECH;
336     }
337
338     if (!mapToNull && oid != GSS_C_NO_OID) {
339         if (!internalizeOid(oid, pOid))
340             major = duplicateOid(minor, oid, pOid);
341     }
342
343     assert(!GSS_ERROR(major));
344     return major;
345 }
346
347 static gss_buffer_desc gssEapSaslMechs[] = {
348     { sizeof("EAP") - 1,        "EAP",       }, /* not used */
349     { sizeof("EAP-AES128") - 1, "EAP-AES128" },
350     { sizeof("EAP-AES256") - 1, "EAP-AES256" },
351 };
352
353 gss_buffer_t
354 gssEapOidToSaslName(const gss_OID oid)
355 {
356     size_t i;
357
358     for (i = 1; i < sizeof(gssEapMechOids)/sizeof(gssEapMechOids[0]); i++) {
359         if (oidEqual(&gssEapMechOids[i], oid))
360             return &gssEapSaslMechs[i];
361     }
362
363     return GSS_C_NO_BUFFER;
364 }
365
366 gss_OID
367 gssEapSaslNameToOid(const gss_buffer_t name)
368 {
369     size_t i;
370
371     for (i = 1; i < sizeof(gssEapSaslMechs)/sizeof(gssEapSaslMechs[0]); i++) {
372         if (bufferEqual(&gssEapSaslMechs[i], name))
373             return &gssEapMechOids[i];
374     }
375
376     return GSS_C_NO_OID;
377 }