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