c95dc376d861b0b7635c1be308d8e66bb568119d
[mech_eap.git] / 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 <string.h>
61 #include <errno.h>
62
63 #include <krb5.h>
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 #include "util_saml.h"
70
71 #ifndef MIN             /* Usually found in <sys/param.h>. */
72 #define MIN(_a,_b)  ((_a)<(_b)?(_a):(_b))
73 #endif
74
75 #define KRB_KEY_TYPE(key)       ((key)->enctype)
76 #define KRB_KEY_DATA(key)       ((key)->contents)
77 #define KRB_KEY_LENGTH(key)     ((key)->length)
78 #define KRB_KEY_INIT(key)       do {        \
79         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
80         KRB_KEY_DATA(key) = NULL;           \
81         KRB_KEY_LENGTH(key) = 0;            \
82     } while (0)
83
84 enum gss_eap_token_type {
85     TOK_TYPE_NONE                    = 0x0000,  /* no token */
86     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
87     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
88     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
89     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* draft-ietf-kitten-gss-naming */
90     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
91     TOK_TYPE_EAP_RESP                = 0x0601,  /* draft-howlett-eap-gss */
92     TOK_TYPE_EAP_REQ                 = 0x0602,  /* draft-howlett-eap-gss */
93     TOK_TYPE_GSS_CB                  = 0x0603,  /* draft-howlett-eap-gss */
94 };
95
96 #define EAP_EXPORT_CONTEXT_V1           1
97
98 /* util_buffer.c */
99 OM_uint32
100 makeStringBuffer(OM_uint32 *minor,
101                  const char *string,
102                  gss_buffer_t buffer);
103
104 OM_uint32
105 bufferToString(OM_uint32 *minor,
106                const gss_buffer_t buffer,
107                char **pString);
108
109 OM_uint32
110 duplicateBuffer(OM_uint32 *minor,
111                 const gss_buffer_t src,
112                 gss_buffer_t dst);
113
114 static inline int
115 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
116 {
117     return (b1->length == b2->length &&
118             memcmp(b1->value, b2->value, b2->length) == 0);
119 }
120
121 static inline int
122 bufferEqualString(const gss_buffer_t b1, const char *s)
123 {
124     gss_buffer_desc b2;
125
126     b2.length = strlen(s);
127     b2.value = (char *)s;
128
129     return bufferEqual(b1, &b2);
130 }
131
132 /* util_cksum.c */
133 int
134 gssEapSign(krb5_context context,
135            krb5_cksumtype type,
136            size_t rrc,
137            krb5_keyblock *key,
138            krb5_keyusage sign_usage,
139            gss_iov_buffer_desc *iov,
140            int iov_count);
141
142 int
143 gssEapVerify(krb5_context context,
144              krb5_cksumtype type,
145              size_t rrc,  
146              krb5_keyblock *key,
147              krb5_keyusage sign_usage,
148              gss_iov_buffer_desc *iov,
149              int iov_count,
150              int *valid);
151
152 #if 0
153 OM_uint32
154 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
155                                gss_channel_bindings_t chanBindings,
156                                gss_buffer_t encodedBindings);
157 #endif
158
159 /* util_context.c */
160 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
161 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
162
163 OM_uint32
164 gssEapMakeToken(OM_uint32 *minor,
165                 gss_ctx_id_t ctx,
166                 const gss_buffer_t innerToken,
167                 enum gss_eap_token_type tokenType,
168                 gss_buffer_t outputToken);
169
170 OM_uint32
171 gssEapVerifyToken(OM_uint32 *minor,
172                   gss_ctx_id_t ctx,
173                   const gss_buffer_t inputToken,
174                   enum gss_eap_token_type tokenType,
175                   gss_buffer_t innerInputToken);
176
177 /* util_cred.c */
178 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
179 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
180
181 OM_uint32
182 gssEapAcquireCred(OM_uint32 *minor,
183                   const gss_name_t desiredName,
184                   const gss_buffer_t password,
185                   OM_uint32 timeReq,
186                   const gss_OID_set desiredMechs,
187                   int cred_usage,
188                   gss_cred_id_t *pCred,
189                   gss_OID_set *pActualMechs,
190                   OM_uint32 *timeRec);
191
192 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
193
194 /* util_crypt.c */
195 int
196 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
197               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
198               gss_iov_buffer_desc *iov, int iov_count);
199
200 int
201 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
202               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
203               gss_iov_buffer_desc *iov, int iov_count);
204
205 krb5_cryptotype
206 gssEapMapCryptoFlag(OM_uint32 type);
207
208 gss_iov_buffer_t
209 gssEapLocateIov(gss_iov_buffer_desc *iov,
210                 int iov_count,
211                 OM_uint32 type);
212
213 void
214 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
215                        int iov_count,
216                        size_t *data_length,
217                        size_t *assoc_data_length);
218
219 void
220 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
221
222 int
223 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
224
225 int
226 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
227
228 OM_uint32
229 gssEapDeriveRfc3961Key(OM_uint32 *minor,
230                        const unsigned char *key,
231                        size_t keyLength,
232                        krb5_enctype enctype,
233                        krb5_keyblock *pKey);
234
235 /* util_krb.c */
236 OM_uint32
237 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
238
239 OM_uint32
240 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
241                           krb5_keyblock *key,
242                           krb5_cksumtype *cksumtype);
243
244 #define GSSEAP_KRB_INIT(ctx) do {                   \
245         OM_uint32 tmpMajor;                         \
246         tmpMajor  = gssEapKerberosInit(minor, ctx); \
247         if (GSS_ERROR(tmpMajor)) {                  \
248             return tmpMajor;                        \
249         }                                           \
250     } while (0)
251
252 /* util_mech.c */
253 int
254 gssEapInternalizeOid(const gss_OID oid,
255                      gss_OID *const pInternalizedOid);
256
257 OM_uint32
258 gssEapDefaultMech(OM_uint32 *minor,
259                   gss_OID *oid);
260
261 OM_uint32
262 gssEapIndicateMechs(OM_uint32 *minor,
263                     gss_OID_set *mechs);
264
265 OM_uint32
266 gssEapEnctypeToOid(OM_uint32 *minor,
267                    krb5_enctype enctype,
268                    gss_OID *pOid);
269
270 OM_uint32
271 gssEapOidToEnctype(OM_uint32 *minor,
272                    const gss_OID oid,
273                    krb5_enctype *enctype);
274
275 int
276 gssEapIsMechanismOid(const gss_OID oid);
277
278 int
279 gssEapIsConcreteMechanismOid(const gss_OID oid);
280
281 OM_uint32
282 gssEapValidateMechs(OM_uint32 *minor,
283                    const gss_OID_set mechs);
284
285 /* util_name.c */
286 enum gss_eap_attribute_type {
287     ATTR_TYPE_NONE                  = 0,
288     ATTR_TYPE_SAML_AAA_ASSERTION    = 1,
289     ATTR_TYPE_SAML_ATTR             = 2,
290     ATTR_TYPE_RADIUS_AVP            = 3
291 };
292
293 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
294 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
295 OM_uint32 gssEapExportName(OM_uint32 *minor,
296                            const gss_name_t name,
297                            gss_buffer_t exportedName,
298                            int composite);
299 OM_uint32 gssEapImportName(OM_uint32 *minor,
300                            const gss_buffer_t input_name_buffer,
301                            gss_OID input_name_type,
302                            gss_name_t *output_name);
303
304 enum gss_eap_attribute_type
305 gssEapAttributePrefixToType(const gss_buffer_t prefix);
306 gss_buffer_t
307 gssEapAttributeTypeToPrefix(enum gss_eap_attribute_type type);
308 OM_uint32
309 decomposeAttributeName(OM_uint32 *minor,
310                        const gss_buffer_t attribute,
311                        gss_buffer_t prefix,
312                        gss_buffer_t suffix);
313 OM_uint32
314 composeAttributeName(OM_uint32 *minor,
315                      const gss_buffer_t prefix,
316                      const gss_buffer_t suffix,
317                      gss_buffer_t attribute);
318
319 /* util_oid.c */
320 OM_uint32
321 composeOid(OM_uint32 *minor_status,
322            const char *prefix,
323            size_t prefix_len,
324            int suffix,  
325            gss_OID_desc *oid);
326
327 OM_uint32
328 decomposeOid(OM_uint32 *minor_status,
329              const char *prefix,
330              size_t prefix_len,
331              gss_OID_desc *oid,
332              int *suffix) ;
333
334 OM_uint32
335 duplicateOid(OM_uint32 *minor_status,
336              const gss_OID_desc * const oid,
337              gss_OID *new_oid);
338
339 OM_uint32
340 duplicateOidSet(OM_uint32 *minor,
341                 const gss_OID_set src,
342                 gss_OID_set *dst);
343
344 static inline int
345 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
346 {
347     if (o1 == GSS_C_NO_OID)
348         return (o2 == GSS_C_NO_OID);
349     else if (o2 == GSS_C_NO_OID)
350         return (o1 == GSS_C_NO_OID);
351     else
352         return (o1->length == o2->length &&
353                 memcmp(o1->elements, o2->elements, o1->length) == 0);
354 }
355
356 /* util_ordering.c */
357 OM_uint32
358 sequenceInternalize(OM_uint32 *minor,
359                     void **vqueue,
360                     unsigned char **buf,
361                     size_t *lenremain);
362
363 OM_uint32
364 sequenceExternalize(OM_uint32 *minor,
365                     void *vqueue,
366                     unsigned char **buf,
367                     size_t *lenremain);
368
369 size_t
370 sequenceSize(void *vqueue);
371
372 OM_uint32
373 sequenceFree(OM_uint32 *minor, void **vqueue);
374
375 OM_uint32
376 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
377
378 OM_uint32
379 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
380              int do_replay, int do_sequence, int wide_nums);
381
382 /* util_token.c */
383 size_t
384 tokenSize(const gss_OID_desc *mech, size_t body_size);
385
386 void
387 makeTokenHeader(const gss_OID_desc *mech,
388                 size_t body_size,
389                 unsigned char **buf,
390                 enum gss_eap_token_type tok_type);
391
392 OM_uint32
393 verifyTokenHeader(OM_uint32 *minor,
394                   gss_OID mech,
395                   size_t *body_size,
396                   unsigned char **buf_in,
397                   size_t toksize_in,
398                   enum gss_eap_token_type tok_type);
399
400 /* Helper macros */
401 #define GSSEAP_CALLOC(count, size)      (calloc((count), (size)))
402 #define GSSEAP_FREE(ptr)                (free((ptr)))
403 #define GSSEAP_MALLOC(size)             (malloc((size)))
404 #define GSSEAP_REALLOC(ptr, size)       (realloc((ptr), (size)))
405
406 #define GSSEAP_NOT_IMPLEMENTED          do {            \
407         assert(0 && "not implemented");                 \
408         *minor = ENOSYS;                                \
409         return GSS_S_FAILURE;                           \
410     } while (0)
411
412 #include <pthread.h>
413
414 #define GSSEAP_MUTEX                    pthread_mutex_t
415 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
416
417 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
418 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
419 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
420 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
421
422 #define GSSEAP_THREAD_KEY               pthread_key_t
423 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
424 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
425 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
426
427 #define GSSEAP_THREAD_ONCE              pthread_once_t
428 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
429 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
430
431 /* Helper functions */
432 static inline void
433 store_uint16_be(uint16_t val, void *vp)
434 {
435     unsigned char *p = (unsigned char *)vp;
436
437     p[0] = (val >>  8) & 0xff;
438     p[1] = (val      ) & 0xff;
439 }
440
441 static inline uint16_t
442 load_uint16_be(const void *cvp)
443 {
444     const unsigned char *p = (const unsigned char *)cvp;
445
446     return (p[1] | (p[0] << 8));
447 }
448
449 static inline void
450 store_uint32_be(uint32_t val, void *vp)
451 {
452     unsigned char *p = (unsigned char *)vp;
453
454     p[0] = (val >> 24) & 0xff;
455     p[1] = (val >> 16) & 0xff;
456     p[2] = (val >>  8) & 0xff;
457     p[3] = (val      ) & 0xff;
458 }
459
460 static inline uint32_t
461 load_uint32_be(const void *cvp)
462 {
463     const unsigned char *p = (const unsigned char *)cvp;
464
465     return (p[3] | (p[2] << 8)
466             | ((uint32_t) p[1] << 16)
467             | ((uint32_t) p[0] << 24));
468 }
469
470 static inline void
471 store_uint64_be(uint64_t val, void *vp)
472 {
473     unsigned char *p = (unsigned char *)vp;
474
475     p[0] = (unsigned char)((val >> 56) & 0xff);
476     p[1] = (unsigned char)((val >> 48) & 0xff);
477     p[2] = (unsigned char)((val >> 40) & 0xff);
478     p[3] = (unsigned char)((val >> 32) & 0xff);
479     p[4] = (unsigned char)((val >> 24) & 0xff);
480     p[5] = (unsigned char)((val >> 16) & 0xff);
481     p[6] = (unsigned char)((val >>  8) & 0xff);
482     p[7] = (unsigned char)((val      ) & 0xff);
483 }
484
485 static inline uint64_t
486 load_uint64_be(const void *cvp)
487 {
488     const unsigned char *p = (const unsigned char *)cvp;
489
490     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
491 }
492
493 static inline unsigned char *
494 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
495 {
496     unsigned char *p = (unsigned char *)vp;
497
498     if (wide_nums) {
499         store_uint64_be(buffer->length, p);
500         p += 8;
501     } else {
502         store_uint32_be(buffer->length, p);
503         p += 4;
504     }
505
506     if (buffer->value != NULL) {
507         memcpy(p, buffer->value, buffer->length);
508         p += buffer->length;
509     }
510
511     return p;
512 }
513
514 static inline unsigned char *
515 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
516 {
517     buffer->length = 0;
518     buffer->value = GSSEAP_MALLOC(length);
519     if (buffer->value == NULL)
520         return NULL;
521     buffer->length = length;
522     memcpy(buffer->value, cvp, length);
523     return (unsigned char *)cvp + length;
524 }
525
526 static inline unsigned char *
527 store_oid(gss_OID oid, void *vp)
528 {
529     gss_buffer_desc buf;
530
531     if (oid != GSS_C_NO_OID) {
532         buf.length = oid->length;
533         buf.value = oid->elements;
534     } else {
535         buf.length = 0;
536         buf.value = NULL;
537     }
538
539     return store_buffer(&buf, vp, FALSE);
540 }
541
542 #ifdef __cplusplus
543 }
544 #endif
545
546 #endif /* _UTIL_H_ */