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