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