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