initial libradsec port
[mech_eap.orig] / util_mech.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 #include "gssapiP_eap.h"
34
35 /*
36  * 1.3.6.1.4.1.5322(padl)
37  *      gssEap(22)
38  *       mechanisms(1)
39  *        eap-aes128-cts-hmac-sha1-96(17)
40  *        eap-aes256-cts-hmac-sha1-96(18)
41  *       nameTypes(2)
42  *       apiExtensions(3)
43  *        inquireSecContextByOid(1)
44  *        inquireCredByOid(2)
45  *        setSecContextOption(3)
46  *        setCredOption(4)
47  *        mechInvoke(5)
48  */
49
50 /*
51  * Note: the enctype-less OID is used as the mechanism OID in exported
52  * names. There is no public symbol for it. This is consistent with
53  * the krb5 mechanism which, whilst known by many OIDs, always uses a
54  * canonical OID for exported names. (This OID is also returned by
55  * gss_inquire_name.)
56  */
57 static gss_OID_desc gssEapMechOids[] = {
58     /* 1.3.6.1.4.1.5322.22.1  */
59     { 9, "\x2B\x06\x01\x04\x01\xA9\x4A\x16\x01" },
60     /* 1.3.6.1.4.1.5322.22.1.17 */
61     { 10, "\x2B\x06\x01\x04\x01\xA9\x4A\x16\x01\x11" },
62     /* 1.3.6.1.4.1.5322.22.1.18 */
63     { 10, "\x2B\x06\x01\x04\x01\xA9\x4A\x16\x01\x12" }
64 };
65
66 gss_OID GSS_EAP_MECHANISM                            = &gssEapMechOids[0];
67 gss_OID GSS_EAP_AES128_CTS_HMAC_SHA1_96_MECHANISM    = &gssEapMechOids[1];
68 gss_OID GSS_EAP_AES256_CTS_HMAC_SHA1_96_MECHANISM    = &gssEapMechOids[2];
69
70 /*
71  * Returns TRUE is the OID is a concrete mechanism OID, that is, one
72  * with a Kerberos enctype as the last element.
73  */
74 int
75 gssEapIsConcreteMechanismOid(const gss_OID oid)
76 {
77     return oid->length > GSS_EAP_MECHANISM->length &&
78            memcmp(oid->elements, GSS_EAP_MECHANISM->elements,
79                   GSS_EAP_MECHANISM->length) == 0;
80 }
81
82 int
83 gssEapIsMechanismOid(const gss_OID oid)
84 {
85     return oid == GSS_C_NO_OID ||
86            oidEqual(oid, GSS_EAP_MECHANISM) ||
87            gssEapIsConcreteMechanismOid(oid);
88 }
89
90 /*
91  * Validate that all elements are concrete mechanism OIDs.
92  */
93 OM_uint32
94 gssEapValidateMechs(OM_uint32 *minor,
95                     const gss_OID_set mechs)
96 {
97     int i;
98
99     *minor = 0;
100
101     if (mechs == GSS_C_NO_OID_SET) {
102         return GSS_S_COMPLETE;
103     }
104
105     for (i = 0; i < mechs->count; i++) {
106         gss_OID oid = &mechs->elements[i];
107
108         if (!gssEapIsConcreteMechanismOid(oid))
109             return GSS_S_BAD_MECH;
110     }
111
112     return GSS_S_COMPLETE;
113 }
114
115 OM_uint32
116 gssEapOidToEnctype(OM_uint32 *minor,
117                    const gss_OID oid,
118                    krb5_enctype *enctype)
119 {
120     OM_uint32 major;
121     int suffix;
122
123     major = decomposeOid(minor,
124                          GSS_EAP_MECHANISM->elements,
125                          GSS_EAP_MECHANISM->length,
126                          oid,
127                          &suffix);
128     if (major == GSS_S_COMPLETE)
129         *enctype = suffix;
130
131     return major;
132 }
133
134 OM_uint32
135 gssEapEnctypeToOid(OM_uint32 *minor,
136                    krb5_enctype enctype,
137                    gss_OID *pOid)
138 {
139     OM_uint32 major;
140     gss_OID oid;
141
142     *pOid = NULL;
143
144     oid = (gss_OID)GSSEAP_MALLOC(sizeof(*oid));
145     if (oid == NULL) {
146         *minor = ENOMEM;
147         return GSS_S_FAILURE;
148     }
149
150     oid->length = GSS_EAP_MECHANISM->length + 1;
151     oid->elements = GSSEAP_MALLOC(oid->length);
152     if (oid->elements == NULL) {
153         *minor = ENOMEM;
154         GSSEAP_FREE(oid);
155         return GSS_S_FAILURE;
156     }
157
158     major = composeOid(minor,
159                        GSS_EAP_MECHANISM->elements,
160                        GSS_EAP_MECHANISM->length,
161                        enctype,
162                        oid);
163     if (major == GSS_S_COMPLETE) {
164         gssEapInternalizeOid(oid, pOid);
165         *pOid = oid;
166     } else {
167         GSSEAP_FREE(oid->elements);
168         GSSEAP_FREE(oid);
169     }
170
171     return major;
172 }
173
174 OM_uint32
175 gssEapIndicateMechs(OM_uint32 *minor,
176                     gss_OID_set *mechs)
177 {
178     krb5_context krbContext;
179     OM_uint32 major, tmpMinor;
180     krb5_enctype *etypes;
181     int i;
182
183     GSSEAP_KRB_INIT(&krbContext);
184
185     *minor = krb5_get_permitted_enctypes(krbContext, &etypes);
186     if (*minor != 0) {
187         return GSS_S_FAILURE;
188     }
189
190     major = gss_create_empty_oid_set(minor, mechs);
191     if (GSS_ERROR(major)) {
192         GSSEAP_FREE(etypes); /* XXX */
193         return major;
194     }
195
196     for (i = 0; etypes[i] != ENCTYPE_NULL; i++) {
197         gss_OID mechOid;
198
199         /* XXX currently we aren't equipped to encode these enctypes */
200         if (etypes[i] < 0 || etypes[i] > 127)
201             continue;
202
203         major = gssEapEnctypeToOid(minor, etypes[i], &mechOid);
204         if (GSS_ERROR(major))
205             break;
206
207         major = gss_add_oid_set_member(minor, mechOid, mechs);
208         if (GSS_ERROR(major))
209             break;
210
211         gss_release_oid(&tmpMinor, &mechOid);
212     }
213
214     GSSEAP_FREE(etypes); /* XXX */
215
216     *minor = 0;
217     return major;
218 }
219
220 OM_uint32
221 gssEapDefaultMech(OM_uint32 *minor,
222                   gss_OID *oid)
223 {
224     gss_OID_set mechs;
225     OM_uint32 major, tmpMinor;
226
227     major = gssEapIndicateMechs(minor, &mechs);
228     if (GSS_ERROR(major)) {
229         return major;
230     }
231
232     if (mechs->count == 0) {
233         gss_release_oid_set(&tmpMinor, &mechs);
234         return GSS_S_BAD_MECH;
235     }
236
237     if (!gssEapInternalizeOid(&mechs->elements[0], oid)) {
238         /* don't double-free if we didn't internalize it */
239         mechs->elements[0].length = 0;
240         mechs->elements[0].elements = NULL;
241     }
242
243     gss_release_oid_set(&tmpMinor, &mechs);
244
245     *minor = 0;
246     return GSS_S_COMPLETE;
247 }
248
249 int
250 gssEapInternalizeOid(const gss_OID oid,
251                      gss_OID *const pInternalizedOid)
252 {
253     int i;
254
255     *pInternalizedOid = GSS_C_NO_OID;
256
257     for (i = 0;
258          i < sizeof(gssEapMechOids) / sizeof(gssEapMechOids[0]);
259          i++) {
260         if (oidEqual(oid, &gssEapMechOids[i])) {
261             *pInternalizedOid = (const gss_OID)&gssEapMechOids[i];
262             break;
263         }
264     }
265
266     if (*pInternalizedOid == GSS_C_NO_OID) {
267         if (oidEqual(oid, GSS_EAP_NT_PRINCIPAL_NAME))
268             *pInternalizedOid = (const gss_OID)GSS_EAP_NT_PRINCIPAL_NAME;
269     }
270
271     if (*pInternalizedOid == GSS_C_NO_OID) {
272         *pInternalizedOid = oid;
273         return 0;
274     }
275
276     return 1;
277 }
278
279 static gss_buffer_desc gssEapSaslMechs[] = {
280     { sizeof("EAP") - 1,        "EAP",       }, /* not used */
281     { sizeof("EAP-AES128") - 1, "EAP-AES128" },
282     { sizeof("EAP-AES256") - 1, "EAP-AES256" },
283 };
284
285 gss_buffer_t
286 gssEapOidToSaslName(const gss_OID oid)
287 {
288     size_t i;
289
290     for (i = 1; i < sizeof(gssEapMechOids)/sizeof(gssEapMechOids[0]); i++) {
291         if (oidEqual(&gssEapMechOids[i], oid))
292             return &gssEapSaslMechs[i];
293     }
294
295     return GSS_C_NO_BUFFER;
296 }
297
298 gss_OID
299 gssEapSaslNameToOid(const gss_buffer_t name)
300 {
301     size_t i;
302
303     for (i = 1; i < sizeof(gssEapSaslMechs)/sizeof(gssEapSaslMechs[0]); i++) {
304         if (bufferEqual(&gssEapSaslMechs[i], name))
305             return &gssEapMechOids[i];
306     }
307
308     return GSS_C_NO_OID;
309 }