fix comment explaining key derivation
[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             *minor = GSSEAP_WRONG_MECH;
110             return GSS_S_BAD_MECH;
111         }
112     }
113
114     return GSS_S_COMPLETE;
115 }
116
117 OM_uint32
118 gssEapOidToEnctype(OM_uint32 *minor,
119                    const gss_OID oid,
120                    krb5_enctype *enctype)
121 {
122     OM_uint32 major;
123     int suffix;
124
125     major = decomposeOid(minor,
126                          GSS_EAP_MECHANISM->elements,
127                          GSS_EAP_MECHANISM->length,
128                          oid,
129                          &suffix);
130     if (major == GSS_S_COMPLETE)
131         *enctype = suffix;
132
133     return major;
134 }
135
136 OM_uint32
137 gssEapEnctypeToOid(OM_uint32 *minor,
138                    krb5_enctype enctype,
139                    gss_OID *pOid)
140 {
141     OM_uint32 major;
142     gss_OID oid;
143
144     *pOid = NULL;
145
146     oid = (gss_OID)GSSEAP_MALLOC(sizeof(*oid));
147     if (oid == NULL) {
148         *minor = ENOMEM;
149         return GSS_S_FAILURE;
150     }
151
152     oid->length = GSS_EAP_MECHANISM->length + 1;
153     oid->elements = GSSEAP_MALLOC(oid->length);
154     if (oid->elements == NULL) {
155         *minor = ENOMEM;
156         GSSEAP_FREE(oid);
157         return GSS_S_FAILURE;
158     }
159
160     major = composeOid(minor,
161                        GSS_EAP_MECHANISM->elements,
162                        GSS_EAP_MECHANISM->length,
163                        enctype,
164                        oid);
165     if (major == GSS_S_COMPLETE) {
166         gssEapInternalizeOid(oid, pOid);
167         *pOid = oid;
168     } else {
169         GSSEAP_FREE(oid->elements);
170         GSSEAP_FREE(oid);
171     }
172
173     return major;
174 }
175
176 OM_uint32
177 gssEapIndicateMechs(OM_uint32 *minor,
178                     gss_OID_set *mechs)
179 {
180     krb5_context krbContext;
181     OM_uint32 major, tmpMinor;
182     krb5_enctype *etypes;
183     int i;
184
185     GSSEAP_KRB_INIT(&krbContext);
186
187     *minor = krb5_get_permitted_enctypes(krbContext, &etypes);
188     if (*minor != 0) {
189         return GSS_S_FAILURE;
190     }
191
192     major = gss_create_empty_oid_set(minor, mechs);
193     if (GSS_ERROR(major)) {
194         GSSEAP_FREE(etypes); /* XXX */
195         return major;
196     }
197
198     for (i = 0; etypes[i] != ENCTYPE_NULL; i++) {
199         gss_OID mechOid;
200
201         /* XXX currently we aren't equipped to encode these enctypes */
202         if (etypes[i] < 0 || etypes[i] > 127)
203             continue;
204
205         major = gssEapEnctypeToOid(minor, etypes[i], &mechOid);
206         if (GSS_ERROR(major))
207             break;
208
209         major = gss_add_oid_set_member(minor, mechOid, mechs);
210         if (GSS_ERROR(major))
211             break;
212
213         gss_release_oid(&tmpMinor, &mechOid);
214     }
215
216     GSSEAP_FREE(etypes); /* XXX */
217
218     *minor = 0;
219     return major;
220 }
221
222 OM_uint32
223 gssEapDefaultMech(OM_uint32 *minor,
224                   gss_OID *oid)
225 {
226     gss_OID_set mechs;
227     OM_uint32 major, tmpMinor;
228
229     major = gssEapIndicateMechs(minor, &mechs);
230     if (GSS_ERROR(major)) {
231         return major;
232     }
233
234     if (mechs->count == 0) {
235         gss_release_oid_set(&tmpMinor, &mechs);
236         return GSS_S_BAD_MECH;
237     }
238
239     if (!gssEapInternalizeOid(&mechs->elements[0], oid)) {
240         /* don't double-free if we didn't internalize it */
241         mechs->elements[0].length = 0;
242         mechs->elements[0].elements = NULL;
243     }
244
245     gss_release_oid_set(&tmpMinor, &mechs);
246
247     *minor = 0;
248     return GSS_S_COMPLETE;
249 }
250
251 int
252 gssEapInternalizeOid(const gss_OID oid,
253                      gss_OID *const pInternalizedOid)
254 {
255     int i;
256
257     *pInternalizedOid = GSS_C_NO_OID;
258
259     for (i = 0;
260          i < sizeof(gssEapMechOids) / sizeof(gssEapMechOids[0]);
261          i++) {
262         if (oidEqual(oid, &gssEapMechOids[i])) {
263             *pInternalizedOid = (const gss_OID)&gssEapMechOids[i];
264             break;
265         }
266     }
267
268     if (*pInternalizedOid == GSS_C_NO_OID) {
269         if (oidEqual(oid, GSS_EAP_NT_PRINCIPAL_NAME))
270             *pInternalizedOid = (const gss_OID)GSS_EAP_NT_PRINCIPAL_NAME;
271     }
272
273     if (*pInternalizedOid == GSS_C_NO_OID) {
274         *pInternalizedOid = oid;
275         return 0;
276     }
277
278     return 1;
279 }
280
281 static gss_buffer_desc gssEapSaslMechs[] = {
282     { sizeof("EAP") - 1,        "EAP",       }, /* not used */
283     { sizeof("EAP-AES128") - 1, "EAP-AES128" },
284     { sizeof("EAP-AES256") - 1, "EAP-AES256" },
285 };
286
287 gss_buffer_t
288 gssEapOidToSaslName(const gss_OID oid)
289 {
290     size_t i;
291
292     for (i = 1; i < sizeof(gssEapMechOids)/sizeof(gssEapMechOids[0]); i++) {
293         if (oidEqual(&gssEapMechOids[i], oid))
294             return &gssEapSaslMechs[i];
295     }
296
297     return GSS_C_NO_BUFFER;
298 }
299
300 gss_OID
301 gssEapSaslNameToOid(const gss_buffer_t name)
302 {
303     size_t i;
304
305     for (i = 1; i < sizeof(gssEapSaslMechs)/sizeof(gssEapSaslMechs[0]); i++) {
306         if (bufferEqual(&gssEapSaslMechs[i], name))
307             return &gssEapMechOids[i];
308     }
309
310     return GSS_C_NO_OID;
311 }