5be9538891a22afd4aacffe8b4c6c15aa0b80f17
[mech_eap.orig] / util.h
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  * Portions Copyright 2003-2010 Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  *
55  */
56
57 #ifndef _UTIL_H_
58 #define _UTIL_H_ 1
59
60 #include <krb5.h>
61
62 #include "util_saml.h"
63 #include "util_radius.h"
64
65 #define KRB_KEYTYPE(key)        ((key)->enctype)
66
67 enum gss_eap_token_type {
68     TOK_TYPE_NONE                    = 0x0000,  /* no token */
69     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
70     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
71     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
72     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* draft-ietf-kitten-gss-naming */
73     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
74     TOK_TYPE_EAP_RESP                = 0x0601,  /* draft-howlett-eap-gss */
75     TOK_TYPE_EAP_REQ                 = 0x0602,  /* draft-howlett-eap-gss */
76     TOK_TYPE_GSS_CHANNEL_BINDINGS    = 0x0603,  /* draft-howlett-eap-gss */
77 };
78
79 int
80 gssEapSign(krb5_context context,
81            krb5_cksumtype type,
82            size_t rrc,
83            krb5_keyblock *key,
84            krb5_keyusage sign_usage,
85            gss_iov_buffer_desc *iov,
86            int iov_count);
87
88 int
89 gssEapVerify(krb5_context context,
90              krb5_cksumtype type,
91              size_t rrc,  
92              krb5_keyblock *key,
93              krb5_keyusage sign_usage,
94              gss_iov_buffer_desc *iov,
95              int iov_count,
96              int *valid);
97
98 /* util_context.c */
99 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
100 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
101
102 OM_uint32
103 gssEapMakeToken(OM_uint32 *minor,
104                 gss_ctx_id_t ctx,
105                 const gss_buffer_t innerToken,
106                 enum gss_eap_token_type tokenType,
107                 gss_buffer_t outputToken);
108
109 OM_uint32
110 gssEapVerifyToken(OM_uint32 *minor,
111                   gss_ctx_id_t ctx,
112                   const gss_buffer_t inputToken,
113                   enum gss_eap_token_type tokenType,
114                   gss_buffer_t innerInputToken);
115
116 /* util_cred.c */
117 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
118 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
119
120 OM_uint32
121 gssEapAcquireCred(OM_uint32 *minor,
122                   const gss_name_t desiredName,
123                   const gss_buffer_t password,
124                   OM_uint32 timeReq,
125                   const gss_OID_set desiredMechs,
126                   int cred_usage,
127                   gss_cred_id_t *pCred,
128                   gss_OID_set *pActualMechs,
129                   OM_uint32 *timeRec);
130
131 /* util_crypt.c */
132 int
133 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
134               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
135               gss_iov_buffer_desc *iov, int iov_count);
136
137 int
138 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
139               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
140               gss_iov_buffer_desc *iov, int iov_count);
141
142 krb5_cryptotype
143 gssEapMapCryptoFlag(OM_uint32 type);
144
145 gss_iov_buffer_t
146 gssEapLocateIov(gss_iov_buffer_desc *iov,
147                 int iov_count,
148                 OM_uint32 type);
149
150 void
151 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
152                        int iov_count,
153                        size_t *data_length,
154                        size_t *assoc_data_length);
155
156 void
157 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
158
159 int
160 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
161
162 int
163 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
164
165 OM_uint32
166 gssEapDeriveRFC3961Key(OM_uint32 *minor,
167                        gss_buffer_t msk,
168                        krb5_enctype enctype,
169                        krb5_keyblock *pKey);
170
171 /* util_krb.c */
172 OM_uint32
173 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
174
175 #define GSSEAP_KRB_INIT(ctx) do {                   \
176         OM_uint32 tmpMajor;                         \
177         tmpMajor  = gssEapKerberosInit(minor, ctx); \
178         if (GSS_ERROR(tmpMajor)) {                  \
179             return tmpMajor;                        \
180         }                                           \
181     } while (0)
182
183 /* util_mech.c */
184 int
185 gssEapInternalizeOid(const gss_OID oid,
186                      gss_OID *const pInternalizedOid);
187
188 OM_uint32
189 gssEapDefaultMech(OM_uint32 *minor,
190                   gss_OID *oid);
191
192 OM_uint32
193 gssEapIndicateMechs(OM_uint32 *minor,
194                     gss_OID_set *mechs);
195
196 OM_uint32
197 gssEapEnctypeToOid(OM_uint32 *minor,
198                    krb5_enctype enctype,
199                    gss_OID *pOid);
200
201 OM_uint32
202 gssEapOidToEnctype(OM_uint32 *minor,
203                    const gss_OID oid,
204                    krb5_enctype *enctype);
205
206 int
207 gssEapIsMechanismOid(const gss_OID oid);
208
209 OM_uint32
210 gssEapValidateMechs(OM_uint32 *minor,
211                    const gss_OID_set mechs);
212
213 /* util_name.c */
214 enum gss_eap_attribute_type {
215     ATTR_TYPE_NONE                  = 0,
216     ATTR_TYPE_SAML_AAA_ASSERTION    = 1,
217     ATTR_TYPE_SAML_ATTR             = 2,
218     ATTR_TYPE_RADIUS_AVP            = 3
219 };
220
221 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
222 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
223 OM_uint32 gssEapExportName(OM_uint32 *minor,
224                            const gss_name_t name,
225                            gss_buffer_t exportedName,
226                            int composite);
227 OM_uint32 gssEapImportName(OM_uint32 *minor,
228                            const gss_buffer_t input_name_buffer,
229                            gss_OID input_name_type,
230                            gss_name_t *output_name);
231
232 enum gss_eap_attribute_type
233 gssEapAttributePrefixToType(const gss_buffer_t prefix);
234 gss_buffer_t
235 gssEapAttributeTypeToPrefix(enum gss_eap_attribute_type type);
236 OM_uint32
237 decomposeAttributeName(OM_uint32 *minor,
238                        const gss_buffer_t attribute,
239                        gss_buffer_t prefix,
240                        gss_buffer_t suffix);
241 OM_uint32
242 composeAttributeName(OM_uint32 *minor,
243                      const gss_buffer_t prefix,
244                      const gss_buffer_t suffix,
245                      gss_buffer_t attribute);
246
247 /* util_oid.c */
248 OM_uint32
249 composeOid(OM_uint32 *minor_status,
250            const char *prefix,
251            size_t prefix_len,
252            int suffix,  
253            gss_OID_desc *oid);
254
255 OM_uint32
256 decomposeOid(OM_uint32 *minor_status,
257              const char *prefix,
258              size_t prefix_len,
259              gss_OID_desc *oid,
260              int *suffix) ;
261
262 OM_uint32
263 duplicateOid(OM_uint32 *minor_status,
264              const gss_OID_desc * const oid,
265              gss_OID *new_oid);
266
267 OM_uint32
268 duplicateOidSet(OM_uint32 *minor,
269                 const gss_OID_set src,
270                 gss_OID_set *dst);
271
272 static inline int
273 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
274 {
275     if (o1 == GSS_C_NO_OID)
276         return (o2 == GSS_C_NO_OID);
277     else if (o2 == GSS_C_NO_OID)
278         return (o1 == GSS_C_NO_OID);
279     else
280         return (o1->length == o2->length &&
281                 memcmp(o1->elements, o2->elements, o1->length) == 0);
282 }
283
284 /* util_ordering.c */
285 int
286 sequenceInternalize(void **vqueue, unsigned char **buf, size_t *lenremain);
287
288 int
289 sequenceExternalize(void *vqueue, unsigned char **buf, size_t *lenremain);
290
291 int
292 sequenceSize(void *vqueue, size_t *sizep);
293
294 void
295 sequenceFree(void **vqueue);
296
297 int
298 sequenceCheck(void **vqueue, uint64_t seqnum);
299
300 int
301 sequenceInit(void **vqueue, uint64_t seqnum,
302              int do_replay, int do_sequence, int wide_nums);
303
304 /* util_token.c */
305 size_t
306 tokenSize(const gss_OID_desc *mech, size_t body_size);
307
308 void
309 makeTokenHeader(const gss_OID_desc *mech,
310                 size_t body_size,
311                 unsigned char **buf,
312                 enum gss_eap_token_type tok_type);
313
314 int
315 verifyTokenHeader(const gss_OID_desc * mech,
316                   size_t *body_size,
317                   unsigned char **buf_in,
318                   size_t toksize_in,
319                   enum gss_eap_token_type tok_type);
320
321 /* Helper macros */
322 #define GSSEAP_CALLOC(count, size)      (calloc((count), (size)))
323 #define GSSEAP_FREE(ptr)                (free((ptr)))
324 #define GSSEAP_MALLOC(size)             (malloc((size)))
325 #define GSSEAP_REALLOC(ptr, size)       (realloc((ptr), (size)))
326
327 #define GSSEAP_NOT_IMPLEMENTED          do {            \
328         assert(0 && "not implemented");                 \
329         *minor = ENOSYS;                                \
330         return GSS_S_FAILURE;                           \
331     } while (0)
332
333 #include <pthread.h>
334
335 #define GSSEAP_MUTEX                    pthread_mutex_t
336 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
337
338 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
339 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
340 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
341 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
342
343 #define GSSEAP_THREAD_KEY               pthread_key_t
344 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
345 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
346 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
347
348 #define GSSEAP_THREAD_ONCE              pthread_once_t
349 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
350 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
351
352 /* Helper functions */
353 static inline void
354 store_uint16_be(uint16_t val, void *vp)
355 {
356     unsigned char *p = (unsigned char *)vp;
357
358     p[0] = (val >>  8) & 0xff;
359     p[1] = (val      ) & 0xff;
360 }
361
362 static inline uint16_t
363 load_uint16_be(const void *cvp)
364 {
365     const unsigned char *p = (const unsigned char *)cvp;
366
367     return (p[1] | (p[0] << 8));
368 }
369
370 static inline void
371 store_uint32_be(uint32_t val, void *vp)
372 {
373     unsigned char *p = (unsigned char *)vp;
374
375     p[0] = (val >> 24) & 0xff;
376     p[1] = (val >> 16) & 0xff;
377     p[2] = (val >>  8) & 0xff;
378     p[3] = (val      ) & 0xff;
379 }
380
381 static inline uint32_t
382 load_uint32_be(const void *cvp)
383 {
384     const unsigned char *p = (const unsigned char *)cvp;
385
386     return (p[3] | (p[2] << 8)
387             | ((uint32_t) p[1] << 16)
388             | ((uint32_t) p[0] << 24));
389 }
390
391 static inline void
392 store_uint64_be(uint64_t val, void *vp)
393 {
394     unsigned char *p = (unsigned char *)vp;
395
396     p[0] = (unsigned char)((val >> 56) & 0xff);
397     p[1] = (unsigned char)((val >> 48) & 0xff);
398     p[2] = (unsigned char)((val >> 40) & 0xff);
399     p[3] = (unsigned char)((val >> 32) & 0xff);
400     p[4] = (unsigned char)((val >> 24) & 0xff);
401     p[5] = (unsigned char)((val >> 16) & 0xff);
402     p[6] = (unsigned char)((val >>  8) & 0xff);
403     p[7] = (unsigned char)((val      ) & 0xff);
404 }
405
406 static inline uint64_t
407 load_uint64_be(const void *cvp)
408 {
409     const unsigned char *p = (const unsigned char *)cvp;
410
411     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
412 }
413
414 static OM_uint32
415 makeStringBuffer(OM_uint32 *minor,
416                  const char *string,
417                  gss_buffer_t buffer)
418 {
419     size_t len = strlen(string);
420
421     buffer->value = GSSEAP_MALLOC(len + 1);
422     if (buffer->value == NULL) {
423         *minor = ENOMEM;
424         return GSS_S_FAILURE;
425     }
426     memcpy(buffer->value, string, len + 1);
427     buffer->length = len;
428
429     *minor = 0;
430     return GSS_S_COMPLETE;
431 }
432
433 static OM_uint32
434 bufferToString(OM_uint32 *minor,
435                const gss_buffer_t buffer,
436                char **pString)
437 {
438     char *s;
439
440     s = GSSEAP_MALLOC(buffer->length + 1);
441     if (s == NULL) {
442         *minor = ENOMEM;
443         return GSS_S_FAILURE;
444     }
445     memcpy(s, buffer->value, buffer->length);
446     s[buffer->length] = '\0';
447
448     *pString = s;
449
450     *minor = 0;
451     return GSS_S_COMPLETE;
452 }
453
454 static OM_uint32
455 duplicateBuffer(OM_uint32 *minor,
456                 const gss_buffer_t src,
457                 gss_buffer_t dst)
458 {
459     dst->length = 0;
460     dst->value = NULL;
461
462     if (src == GSS_C_NO_BUFFER)
463         return GSS_S_COMPLETE;
464
465     dst->value = GSSEAP_MALLOC(src->length + 1);
466     if (dst->value == NULL) {
467         *minor = ENOMEM;
468         return GSS_S_FAILURE;
469     }
470
471     dst->length = src->length;
472     memcpy(dst->value, src->value, dst->length);
473
474     ((unsigned char *)dst->value)[dst->length] = '\0';
475
476     *minor = 0;
477     return GSS_S_COMPLETE;
478 }
479
480 #endif /* _UTIL_H_ */