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