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