add get/verify conversation MIC APIs
[moonshot.git] / mech_eap / util_context.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  * Utility routines for context handles.
35  */
36
37 #include "gssapiP_eap.h"
38
39 OM_uint32
40 gssEapAllocContext(OM_uint32 *minor,
41                    gss_ctx_id_t *pCtx)
42 {
43     OM_uint32 tmpMinor;
44     gss_ctx_id_t ctx;
45
46     assert(*pCtx == GSS_C_NO_CONTEXT);
47
48     ctx = (gss_ctx_id_t)GSSEAP_CALLOC(1, sizeof(*ctx));
49     if (ctx == NULL) {
50         *minor = ENOMEM;
51         return GSS_S_FAILURE;
52     }
53
54     if (GSSEAP_MUTEX_INIT(&ctx->mutex) != 0) {
55         *minor = errno;
56         gssEapReleaseContext(&tmpMinor, &ctx);
57         return GSS_S_FAILURE;
58     }
59
60     ctx->state = GSSEAP_STATE_INITIAL;
61
62     /*
63      * Integrity, confidentiality, sequencing and replay detection are
64      * always available.  Regardless of what flags are requested in
65      * GSS_Init_sec_context, implementations MUST set the flag corresponding
66      * to these services in the output of GSS_Init_sec_context and
67      * GSS_Accept_sec_context.
68     */
69     ctx->gssFlags = GSS_C_TRANS_FLAG    |   /* exporting contexts */
70                     GSS_C_INTEG_FLAG    |   /* integrity */
71                     GSS_C_CONF_FLAG     |   /* confidentiality */
72                     GSS_C_SEQUENCE_FLAG |   /* sequencing */
73                     GSS_C_REPLAY_FLAG;      /* replay detection */
74
75     *pCtx = ctx;
76
77     return GSS_S_COMPLETE;
78 }
79
80 static void
81 releaseInitiatorContext(struct gss_eap_initiator_ctx *ctx)
82 {
83     eap_peer_sm_deinit(ctx->eap);
84 }
85
86 static void
87 releaseAcceptorContext(struct gss_eap_acceptor_ctx *ctx)
88 {
89     OM_uint32 tmpMinor;
90
91     if (ctx->radConn != NULL)
92         rs_conn_destroy(ctx->radConn);
93     if (ctx->radContext != NULL)
94         rs_context_destroy(ctx->radContext);
95     if (ctx->radServer != NULL)
96         GSSEAP_FREE(ctx->radServer);
97     gss_release_buffer(&tmpMinor, &ctx->state);
98     if (ctx->vps != NULL)
99         gssEapRadiusFreeAvps(&tmpMinor, &ctx->vps);
100 }
101
102 OM_uint32
103 gssEapReleaseContext(OM_uint32 *minor,
104                      gss_ctx_id_t *pCtx)
105 {
106     OM_uint32 tmpMinor;
107     gss_ctx_id_t ctx = *pCtx;
108     krb5_context krbContext = NULL;
109
110     if (ctx == GSS_C_NO_CONTEXT) {
111         return GSS_S_COMPLETE;
112     }
113
114     gssEapKerberosInit(&tmpMinor, &krbContext);
115
116 #ifdef GSSEAP_ENABLE_REAUTH
117     if (ctx->flags & CTX_FLAG_KRB_REAUTH) {
118         gssDeleteSecContext(&tmpMinor, &ctx->kerberosCtx, GSS_C_NO_BUFFER);
119     } else
120 #endif
121     if (CTX_IS_INITIATOR(ctx)) {
122         releaseInitiatorContext(&ctx->initiatorCtx);
123     } else {
124         releaseAcceptorContext(&ctx->acceptorCtx);
125     }
126
127     krb5_free_keyblock_contents(krbContext, &ctx->rfc3961Key);
128     gssEapReleaseName(&tmpMinor, &ctx->initiatorName);
129     gssEapReleaseName(&tmpMinor, &ctx->acceptorName);
130     gssEapReleaseOid(&tmpMinor, &ctx->mechanismUsed);
131     sequenceFree(&tmpMinor, &ctx->seqState);
132     gssEapReleaseCred(&tmpMinor, &ctx->defaultCred);
133     gss_release_buffer(&tmpMinor, &ctx->conversation);
134
135     GSSEAP_MUTEX_DESTROY(&ctx->mutex);
136
137     memset(ctx, 0, sizeof(*ctx));
138     GSSEAP_FREE(ctx);
139     *pCtx = GSS_C_NO_CONTEXT;
140
141     *minor = 0;
142     return GSS_S_COMPLETE;
143 }
144
145 static OM_uint32
146 recordTokens(OM_uint32 *minor,
147              gss_ctx_id_t ctx,
148              gss_buffer_t tokens,
149              size_t tokensCount)
150 {
151     unsigned char *buf;
152     size_t i, size, offset;
153
154     size = ctx->conversation.length;
155
156     for (i = 0; i < tokensCount; i++)
157         size += tokens[i].length;
158
159     buf = GSSEAP_REALLOC(ctx->conversation.value, size);
160     if (buf == NULL) {
161         *minor = ENOMEM;
162         return GSS_S_FAILURE;
163     }
164
165     offset = ctx->conversation.length;
166
167     ctx->conversation.length = size;
168     ctx->conversation.value = buf;
169
170     for (i = 0; i < tokensCount; i++) {
171         memcpy(buf + offset, tokens[i].value, tokens[i].length);
172         offset += tokens[i].length;
173     }
174
175     *minor = 0;
176     return GSS_S_COMPLETE;
177 }
178
179 OM_uint32
180 gssEapRecordContextTokenHeader(OM_uint32 *minor,
181                                gss_ctx_id_t ctx,
182                                enum gss_eap_token_type tokType)
183 {
184     unsigned char wireOidHeader[2], wireTokType[2];
185     gss_buffer_desc buffers[3];
186
187     assert(ctx->mechanismUsed != GSS_C_NO_OID);
188
189     wireOidHeader[0] = 0x06;
190     wireOidHeader[1] = ctx->mechanismUsed->length;
191     buffers[0].length = sizeof(wireOidHeader);
192     buffers[0].value  = wireOidHeader;
193
194     buffers[1].length = ctx->mechanismUsed->length;
195     buffers[1].value  = ctx->mechanismUsed->elements;
196
197     store_uint16_be(tokType, wireTokType);
198     buffers[2].length = sizeof(wireTokType);
199     buffers[2].value = wireTokType;
200  
201     return recordTokens(minor, ctx, buffers, sizeof(buffers)/sizeof(buffers[0]));
202 }
203
204 OM_uint32
205 gssEapRecordInnerContextToken(OM_uint32 *minor,
206                               gss_ctx_id_t ctx,
207                               gss_buffer_t innerToken,
208                               OM_uint32 itokType)
209 {
210     gss_buffer_desc buffers[3];
211     unsigned char wireItokType[4], wireLength[4];
212
213     assert(innerToken != GSS_C_NO_BUFFER);
214
215     store_uint32_be(itokType, wireItokType);
216     buffers[0].length = sizeof(wireItokType);
217     buffers[0].value  = wireItokType;
218
219     store_uint32_be(innerToken->length, wireLength);
220     buffers[1].length = sizeof(wireLength);
221     buffers[1].value  = wireLength;
222
223     buffers[2] = *innerToken;
224
225     return recordTokens(minor, ctx, buffers, sizeof(buffers)/sizeof(buffers[0]));
226 }
227
228 OM_uint32
229 gssEapVerifyContextToken(OM_uint32 *minor,
230                          gss_ctx_id_t ctx,
231                          const gss_buffer_t inputToken,
232                          enum gss_eap_token_type tokType,
233                          gss_buffer_t innerInputToken)
234 {
235     OM_uint32 major;
236     size_t bodySize;
237     unsigned char *p = (unsigned char *)inputToken->value;
238     gss_OID_desc oidBuf;
239     gss_OID oid;
240     enum gss_eap_token_type actualTokType;
241     gss_buffer_desc tokenBuf;
242
243     if (ctx->mechanismUsed != GSS_C_NO_OID) {
244         oid = ctx->mechanismUsed;
245     } else {
246         oidBuf.elements = NULL;
247         oidBuf.length = 0;
248         oid = &oidBuf;
249     }
250
251     major = verifyTokenHeader(minor, oid, &bodySize, &p,
252                               inputToken->length, &actualTokType);
253     if (GSS_ERROR(major))
254         return major;
255
256     if (actualTokType != tokType) {
257         *minor = GSSEAP_WRONG_TOK_ID;
258         return GSS_S_DEFECTIVE_TOKEN;
259     }
260
261     if (ctx->mechanismUsed == GSS_C_NO_OID) {
262         if (!gssEapIsConcreteMechanismOid(oid)) {
263             *minor = GSSEAP_WRONG_MECH;
264             return GSS_S_BAD_MECH;
265         }
266
267         if (!gssEapInternalizeOid(oid, &ctx->mechanismUsed)) {
268             major = duplicateOid(minor, oid, &ctx->mechanismUsed);
269             if (GSS_ERROR(major))
270                 return major;
271         }
272     }
273
274     innerInputToken->length = bodySize;
275     innerInputToken->value = p;
276
277     /*
278      * Add OID, tokenType, body to conversation; variable length
279      * header omitted. A better API to verifyTokenHeader would
280      * avoid this ugly pointer arithmetic. XXX FIXME
281      */
282     tokenBuf.value = p - (2 + oid->length + 2);
283     tokenBuf.length = 2 + oid->length + 2 + bodySize;
284
285     major = recordTokens(minor, ctx, &tokenBuf, 1);
286     if (GSS_ERROR(major))
287         return major;
288
289     *minor = 0;
290     return GSS_S_COMPLETE;
291 }
292
293 OM_uint32
294 gssEapContextTime(OM_uint32 *minor,
295                   gss_ctx_id_t context_handle,
296                   OM_uint32 *time_rec)
297 {
298     if (context_handle->expiryTime == 0) {
299         *time_rec = GSS_C_INDEFINITE;
300     } else {
301         time_t now, lifetime;
302
303         time(&now);
304         lifetime = context_handle->expiryTime - now;
305         if (lifetime <= 0) {
306             *time_rec = 0;
307             return GSS_S_CONTEXT_EXPIRED;
308         }
309         *time_rec = lifetime;
310     }
311
312     return GSS_S_COMPLETE;
313 }
314
315 OM_uint32
316 gssEapGetConversationMIC(OM_uint32 *minor,
317                          gss_ctx_id_t ctx,
318                          gss_buffer_t convMIC)
319 {
320     OM_uint32 major;
321     gss_iov_buffer_desc iov[2];
322
323     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
324     iov[0].buffer = ctx->conversation;
325
326     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER | GSS_IOV_BUFFER_FLAG_ALLOCATE;
327     iov[1].buffer.length = 0;
328     iov[1].buffer.value = NULL;
329
330     major = gssEapWrapOrGetMIC(minor, ctx, FALSE, NULL, iov, 2, TOK_TYPE_MIC);
331     if (GSS_ERROR(major))
332         return major;
333
334     *convMIC = iov[1].buffer;
335
336     *minor = 0;
337     return GSS_S_COMPLETE;
338 }
339
340 OM_uint32
341 gssEapVerifyConversationMIC(OM_uint32 *minor,
342                             gss_ctx_id_t ctx,
343                             const gss_buffer_t convMIC)
344 {
345     OM_uint32 major;
346     gss_iov_buffer_desc iov[3];
347     int confState;
348     size_t tokenHeaderLength;
349
350     if (convMIC->length < 16) {
351         *minor = GSSEAP_TOK_TRUNC;
352         return GSS_S_BAD_SIG;
353     }
354
355     iov[0].type = GSS_IOV_BUFFER_TYPE_DATA;
356     iov[0].buffer = ctx->conversation;
357
358     /* Back out token header for the next response token. */
359     assert(ctx->mechanismUsed != GSS_C_NO_OID);
360     tokenHeaderLength = 2 + ctx->mechanismUsed->length + 2;
361     assert(ctx->conversation.length > tokenHeaderLength);
362     iov[0].buffer.length -= tokenHeaderLength;
363
364     iov[1].type = GSS_IOV_BUFFER_TYPE_HEADER;
365     iov[1].buffer.length = 16;
366     iov[1].buffer.value = convMIC->value;
367
368     iov[2].type = GSS_IOV_BUFFER_TYPE_TRAILER;
369     iov[2].buffer.length = convMIC->length - 16;
370     iov[2].buffer.value = (unsigned char *)convMIC->value + 16;
371
372     major = gssEapUnwrapOrVerifyMIC(minor, ctx, &confState, NULL,
373                                     iov, 3, TOK_TYPE_MIC);
374
375
376     return major;
377 }