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