6e8676dd06cb516312f2c0168d434dbf72e55911
[mech_eap.git] / 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(21)
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 static const gss_OID_desc gssEapMechPrefix = {
51     /* Note that alone this is not a valid DER encoded OID */
52     11, "\x06\x0A\x2B\x06\x01\x04\x01\xA9\x4A\x15\x01\x00"
53 };
54
55 static const gss_OID_desc gssEapConcreteMechs[] = {
56     /* 1.3.6.1.4.1.5322.21.1  */
57     { 11, "\x06\x0A\x2B\x06\x01\x04\x01\xA9\x4A\x15\x01" },
58     /* 1.3.6.1.4.1.5322.21.1.17 */
59     { 12, "\x06\x0A\x2B\x06\x01\x04\x01\xA9\x4A\x15\x01\x11" },
60     /* 1.3.6.1.4.1.5322.21.1.18 */
61     { 12, "\x06\x0A\x2B\x06\x01\x04\x01\xA9\x4A\x15\x01\x12" }
62 };
63
64 const gss_OID_desc *const gss_mech_eap =
65     &gssEapConcreteMechs[0];
66 const gss_OID_desc *const gss_mech_eap_aes128_cts_hmac_sha1_96 =
67     &gssEapConcreteMechs[1];
68 const gss_OID_desc *const gss_mech_eap_aes256_cts_hmac_sha1_96 =
69     &gssEapConcreteMechs[2];
70
71 int
72 gssEapIsMechanismOid(const gss_OID oid)
73 {
74     if (oidEqual(oid, gss_mech_eap)) {
75         return TRUE;
76     } else if (oid->length > gssEapMechPrefix.length &&
77                memcmp(oid->elements, gssEapMechPrefix.elements,
78                       gssEapMechPrefix.length) == 0) {
79         return TRUE;
80     }
81
82     return FALSE;
83 }
84
85 OM_uint32
86 gssEapOidToEnctype(OM_uint32 *minor,
87                    const gss_OID oid,
88                    krb5_enctype *enctype)
89 {
90     OM_uint32 major;
91     int suffix;
92
93     major = decomposeOid(minor,
94                          gssEapMechPrefix.elements,
95                          gssEapMechPrefix.length,
96                          oid,
97                          &suffix);
98     if (major == GSS_S_COMPLETE)
99         *enctype = suffix;
100
101     return major;
102 }
103
104 OM_uint32
105 gssEapEnctypeToOid(OM_uint32 *minor,
106                    krb5_enctype enctype,
107                    gss_OID *pOid)
108 {
109     OM_uint32 major;
110     gss_OID oid;
111
112     *pOid = NULL;
113
114     oid = (gss_OID)GSSEAP_MALLOC(sizeof(*oid));
115     if (oid == NULL) {
116         *minor = ENOMEM;
117         return GSS_S_FAILURE;
118     }
119
120     oid->elements = GSSEAP_MALLOC(gssEapMechPrefix.length + 1);
121     if (oid->elements == NULL) {
122         *minor = ENOMEM;
123         free(oid);
124         return GSS_S_FAILURE;
125     }
126
127     major = composeOid(minor,
128                        gssEapMechPrefix.elements,
129                        gssEapMechPrefix.length,
130                        enctype,
131                        oid);
132     if (major == GSS_S_COMPLETE) {
133         gssEapInternalizeOid(oid, pOid);
134         *pOid = oid;
135     } else {
136         free(oid->elements);
137         free(oid);
138     }
139
140     return major;
141 }
142
143 OM_uint32
144 gssEapIndicateMechs(OM_uint32 *minor,
145                     gss_OID_set *mechs)
146 {
147     krb5_context context;
148     OM_uint32 major, tmpMinor;
149     krb5_enctype *etypes;
150     int i;
151
152     *minor = krb5_init_context(&context);
153     if (*minor != 0) {
154         return GSS_S_FAILURE;
155     }
156
157     *minor = krb5_get_permitted_enctypes(context, &etypes);
158     if (*minor != 0) {
159         krb5_free_context(context);
160         return GSS_S_FAILURE;
161     }
162
163     major = gss_create_empty_oid_set(minor, mechs);
164     if (GSS_ERROR(major)) {
165         krb5_free_context(context);
166         GSSEAP_FREE(etypes); /* XXX */
167         return major;
168     }
169
170     for (i = 0; etypes[i] != ENCTYPE_NULL; i++) {
171         gss_OID mechOid;
172
173         /* XXX currently we aren't equipped to encode these enctypes */
174         if (etypes[i] < 0 || etypes[i] > 127)
175             continue;
176
177         major = gssEapEnctypeToOid(minor, etypes[i], &mechOid);
178         if (GSS_ERROR(major))
179             break;
180
181         major = gss_add_oid_set_member(minor, mechOid, mechs);
182         if (GSS_ERROR(major))
183             break;
184
185         gss_release_oid(&tmpMinor, &mechOid);
186     }
187
188     GSSEAP_FREE(etypes); /* XXX */
189     krb5_free_context(context);
190
191     return major;
192 }
193
194 OM_uint32
195 gssEapDefaultMech(OM_uint32 *minor,
196                   gss_OID *oid)
197 {
198     gss_OID_set mechs;
199     OM_uint32 major, tmpMinor;
200
201     major = gssEapIndicateMechs(minor, &mechs);
202     if (GSS_ERROR(major)) {
203         return major;
204     }
205
206     if (mechs->count == 0) {
207         gss_release_oid_set(&tmpMinor, &mechs);
208         return GSS_S_BAD_MECH;
209     }
210
211     gssEapInternalizeOid(&mechs->elements[0], oid);
212     if (*oid == &mechs->elements[0]) {
213         /* don't double-free if we didn't internalize it */
214         mechs->elements[0].length = 0;
215         mechs->elements[0].elements = NULL;
216     }
217
218     gss_release_oid_set(&tmpMinor, &mechs);
219
220     *minor = 0;
221     return GSS_S_COMPLETE;
222 }
223
224 void
225 gssEapInternalizeOid(const gss_OID oid,
226                      gss_OID *const pInternalizedOid)
227 {
228     int i;
229
230     *pInternalizedOid = GSS_C_NO_OID;
231
232     for (i = 0;
233          i < sizeof(gssEapConcreteMechs) / sizeof(gssEapConcreteMechs[0]);
234          i++) {
235         if (oidEqual(oid, &gssEapConcreteMechs[i])) {
236             *pInternalizedOid = (const gss_OID)&gssEapConcreteMechs[i];
237             break;
238         }
239     }
240
241     if (*pInternalizedOid == GSS_C_NO_OID) {
242         *pInternalizedOid = oid;
243     }
244 }