preliminary work on fast reauth
[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 #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_GSS_CB                  = 0x0603,  /* draft-howlett-eap-gss */
92     TOK_TYPE_KRB_CRED                = 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_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                   enum gss_eap_token_type *actualToken,
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_krb.c */
248 OM_uint32
249 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
250
251 OM_uint32
252 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
253                           krb5_keyblock *key,
254                           krb5_cksumtype *cksumtype);
255
256 #define GSSEAP_KRB_INIT(ctx) do {                   \
257         OM_uint32 tmpMajor;                         \
258         tmpMajor  = gssEapKerberosInit(minor, ctx); \
259         if (GSS_ERROR(tmpMajor)) {                  \
260             return tmpMajor;                        \
261         }                                           \
262     } while (0)
263
264 /* util_mech.c */
265 int
266 gssEapInternalizeOid(const gss_OID oid,
267                      gss_OID *const pInternalizedOid);
268
269 OM_uint32
270 gssEapDefaultMech(OM_uint32 *minor,
271                   gss_OID *oid);
272
273 OM_uint32
274 gssEapIndicateMechs(OM_uint32 *minor,
275                     gss_OID_set *mechs);
276
277 OM_uint32
278 gssEapEnctypeToOid(OM_uint32 *minor,
279                    krb5_enctype enctype,
280                    gss_OID *pOid);
281
282 OM_uint32
283 gssEapOidToEnctype(OM_uint32 *minor,
284                    const gss_OID oid,
285                    krb5_enctype *enctype);
286
287 int
288 gssEapIsMechanismOid(const gss_OID oid);
289
290 int
291 gssEapIsConcreteMechanismOid(const gss_OID oid);
292
293 OM_uint32
294 gssEapValidateMechs(OM_uint32 *minor,
295                    const gss_OID_set mechs);
296
297 /* util_name.c */
298 #define EXPORT_NAME_FLAG_OID        0x1
299 #define EXPORT_NAME_FLAG_COMPOSITE  0x2
300
301 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
302 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
303 OM_uint32 gssEapExportName(OM_uint32 *minor,
304                            const gss_name_t name,
305                            gss_buffer_t exportedName);
306 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
307                                    const gss_name_t name,
308                                    gss_buffer_t exportedName,
309                                    unsigned int flags);
310 OM_uint32 gssEapImportName(OM_uint32 *minor,
311                            const gss_buffer_t input_name_buffer,
312                            gss_OID input_name_type,
313                            gss_name_t *output_name);
314 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
315                                    const gss_buffer_t input_name_buffer,
316                                    gss_name_t *output_name,
317                                    unsigned int flags);
318 OM_uint32
319 gssEapDuplicateName(OM_uint32 *minor,
320                     const gss_name_t input_name,
321                     gss_name_t *dest_name);
322
323 /* util_oid.c */
324 OM_uint32
325 composeOid(OM_uint32 *minor_status,
326            const char *prefix,
327            size_t prefix_len,
328            int suffix,
329            gss_OID_desc *oid);
330
331 OM_uint32
332 decomposeOid(OM_uint32 *minor_status,
333              const char *prefix,
334              size_t prefix_len,
335              gss_OID_desc *oid,
336              int *suffix) ;
337
338 OM_uint32
339 duplicateOid(OM_uint32 *minor_status,
340              const gss_OID_desc * const oid,
341              gss_OID *new_oid);
342
343 OM_uint32
344 duplicateOidSet(OM_uint32 *minor,
345                 const gss_OID_set src,
346                 gss_OID_set *dst);
347
348 static inline int
349 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
350 {
351     if (o1 == GSS_C_NO_OID)
352         return (o2 == GSS_C_NO_OID);
353     else if (o2 == GSS_C_NO_OID)
354         return (o1 == GSS_C_NO_OID);
355     else
356         return (o1->length == o2->length &&
357                 memcmp(o1->elements, o2->elements, o1->length) == 0);
358 }
359
360 /* util_ordering.c */
361 OM_uint32
362 sequenceInternalize(OM_uint32 *minor,
363                     void **vqueue,
364                     unsigned char **buf,
365                     size_t *lenremain);
366
367 OM_uint32
368 sequenceExternalize(OM_uint32 *minor,
369                     void *vqueue,
370                     unsigned char **buf,
371                     size_t *lenremain);
372
373 size_t
374 sequenceSize(void *vqueue);
375
376 OM_uint32
377 sequenceFree(OM_uint32 *minor, void **vqueue);
378
379 OM_uint32
380 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
381
382 OM_uint32
383 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
384              int do_replay, int do_sequence, int wide_nums);
385
386 /* util_token.c */
387 size_t
388 tokenSize(const gss_OID_desc *mech, size_t body_size);
389
390 void
391 makeTokenHeader(const gss_OID_desc *mech,
392                 size_t body_size,
393                 unsigned char **buf,
394                 enum gss_eap_token_type tok_type);
395
396 OM_uint32
397 verifyTokenHeader(OM_uint32 *minor,
398                   gss_OID mech,
399                   size_t *body_size,
400                   unsigned char **buf_in,
401                   size_t toksize_in,
402                   enum gss_eap_token_type tok_type,
403                   enum gss_eap_token_type *ret_tok_type);
404
405 /* Helper macros */
406 #define GSSEAP_CALLOC(count, size)      (calloc((count), (size)))
407 #define GSSEAP_FREE(ptr)                (free((ptr)))
408 #define GSSEAP_MALLOC(size)             (malloc((size)))
409 #define GSSEAP_REALLOC(ptr, size)       (realloc((ptr), (size)))
410
411 #define GSSEAP_NOT_IMPLEMENTED          do {            \
412         assert(0 && "not implemented");                 \
413         *minor = ENOSYS;                                \
414         return GSS_S_FAILURE;                           \
415     } while (0)
416
417 #include <pthread.h>
418
419 #define GSSEAP_MUTEX                    pthread_mutex_t
420 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
421
422 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
423 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
424 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
425 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
426
427 #define GSSEAP_THREAD_KEY               pthread_key_t
428 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
429 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
430 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
431
432 #define GSSEAP_THREAD_ONCE              pthread_once_t
433 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
434 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
435
436 /* Helper functions */
437 static inline void
438 store_uint16_be(uint16_t val, void *vp)
439 {
440     unsigned char *p = (unsigned char *)vp;
441
442     p[0] = (val >>  8) & 0xff;
443     p[1] = (val      ) & 0xff;
444 }
445
446 static inline uint16_t
447 load_uint16_be(const void *cvp)
448 {
449     const unsigned char *p = (const unsigned char *)cvp;
450
451     return (p[1] | (p[0] << 8));
452 }
453
454 static inline void
455 store_uint32_be(uint32_t val, void *vp)
456 {
457     unsigned char *p = (unsigned char *)vp;
458
459     p[0] = (val >> 24) & 0xff;
460     p[1] = (val >> 16) & 0xff;
461     p[2] = (val >>  8) & 0xff;
462     p[3] = (val      ) & 0xff;
463 }
464
465 static inline uint32_t
466 load_uint32_be(const void *cvp)
467 {
468     const unsigned char *p = (const unsigned char *)cvp;
469
470     return (p[3] | (p[2] << 8)
471             | ((uint32_t) p[1] << 16)
472             | ((uint32_t) p[0] << 24));
473 }
474
475 static inline void
476 store_uint64_be(uint64_t val, void *vp)
477 {
478     unsigned char *p = (unsigned char *)vp;
479
480     p[0] = (unsigned char)((val >> 56) & 0xff);
481     p[1] = (unsigned char)((val >> 48) & 0xff);
482     p[2] = (unsigned char)((val >> 40) & 0xff);
483     p[3] = (unsigned char)((val >> 32) & 0xff);
484     p[4] = (unsigned char)((val >> 24) & 0xff);
485     p[5] = (unsigned char)((val >> 16) & 0xff);
486     p[6] = (unsigned char)((val >>  8) & 0xff);
487     p[7] = (unsigned char)((val      ) & 0xff);
488 }
489
490 static inline uint64_t
491 load_uint64_be(const void *cvp)
492 {
493     const unsigned char *p = (const unsigned char *)cvp;
494
495     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
496 }
497
498 static inline unsigned char *
499 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
500 {
501     unsigned char *p = (unsigned char *)vp;
502
503     if (wide_nums) {
504         store_uint64_be(buffer->length, p);
505         p += 8;
506     } else {
507         store_uint32_be(buffer->length, p);
508         p += 4;
509     }
510
511     if (buffer->value != NULL) {
512         memcpy(p, buffer->value, buffer->length);
513         p += buffer->length;
514     }
515
516     return p;
517 }
518
519 static inline unsigned char *
520 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
521 {
522     buffer->length = 0;
523     buffer->value = GSSEAP_MALLOC(length);
524     if (buffer->value == NULL)
525         return NULL;
526     buffer->length = length;
527     memcpy(buffer->value, cvp, length);
528     return (unsigned char *)cvp + length;
529 }
530
531 static inline unsigned char *
532 store_oid(gss_OID oid, void *vp)
533 {
534     gss_buffer_desc buf;
535
536     if (oid != GSS_C_NO_OID) {
537         buf.length = oid->length;
538         buf.value = oid->elements;
539     } else {
540         buf.length = 0;
541         buf.value = NULL;
542     }
543
544     return store_buffer(&buf, vp, FALSE);
545 }
546
547 static inline void
548 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
549 {
550     buffer->value = (void *)data->data;
551     buffer->length = data->length;
552 }
553
554 static inline void
555 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
556 {
557     data->data = (char *)buffer->value;
558     data->length = buffer->length;
559 }
560
561 #ifdef __cplusplus
562 }
563 #endif
564
565 #include "util_attr.h"
566 #include "util_reauth.h"
567
568 #endif /* _UTIL_H_ */