Support EAP-TLS in Moonshot (requires OpenSSL)
[mech_eap.git] / mech_eap / util.h
1 /*
2  * Copyright (c) 2011, 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 #ifdef HAVE_SYS_PARAM_H
65 #include <sys/param.h>
66 #endif
67 #ifdef HAVE_STDINT_H
68 #include <stdint.h>
69 #endif
70 #include <string.h>
71 #include <errno.h>
72
73 #include <krb5.h>
74
75 #ifdef WIN32
76 #define inline __inline
77 #define snprintf _snprintf
78 #endif
79
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83
84 #ifndef MIN
85 #define MIN(_a,_b)  ((_a)<(_b)?(_a):(_b))
86 #endif
87
88 #if !defined(WIN32) && !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
89 #define GSSEAP_UNUSED __attribute__ ((__unused__))
90 #else
91 #define GSSEAP_UNUSED
92 #endif
93
94 /* util_buffer.c */
95 OM_uint32
96 makeStringBuffer(OM_uint32 *minor,
97                  const char *string,
98                  gss_buffer_t buffer);
99
100 #define makeStringBufferOrCleanup(src, dst)             \
101     do {                                                \
102         major = makeStringBuffer((minor), (src), (dst));\
103         if (GSS_ERROR(major))                           \
104             goto cleanup;                               \
105     } while (0)
106
107 OM_uint32
108 bufferToString(OM_uint32 *minor,
109                const gss_buffer_t buffer,
110                char **pString);
111
112 OM_uint32
113 duplicateBuffer(OM_uint32 *minor,
114                 const gss_buffer_t src,
115                 gss_buffer_t dst);
116
117 #define duplicateBufferOrCleanup(src, dst)              \
118     do {                                                \
119         major = duplicateBuffer((minor), (src), (dst)); \
120         if (GSS_ERROR(major))                           \
121             goto cleanup;                               \
122     } while (0)
123
124 static inline int
125 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
126 {
127     return (b1->length == b2->length &&
128             memcmp(b1->value, b2->value, b2->length) == 0);
129 }
130
131 static inline int
132 bufferEqualString(const gss_buffer_t b1, const char *s)
133 {
134     gss_buffer_desc b2;
135
136     b2.length = strlen(s);
137     b2.value = (char *)s;
138
139     return bufferEqual(b1, &b2);
140 }
141
142 /* util_cksum.c */
143 int
144 gssEapSign(krb5_context context,
145            krb5_cksumtype type,
146            size_t rrc,
147 #ifdef HAVE_HEIMDAL_VERSION
148            krb5_crypto crypto,
149 #else
150            krb5_keyblock *key,
151 #endif
152            krb5_keyusage sign_usage,
153            gss_iov_buffer_desc *iov,
154            int iov_count);
155
156 int
157 gssEapVerify(krb5_context context,
158              krb5_cksumtype type,
159              size_t rrc,
160 #ifdef HAVE_HEIMDAL_VERSION
161              krb5_crypto crypto,
162 #else
163              krb5_keyblock *key,
164 #endif
165              krb5_keyusage sign_usage,
166              gss_iov_buffer_desc *iov,
167              int iov_count,
168              int *valid);
169
170 #if 0
171 OM_uint32
172 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
173                                gss_channel_bindings_t chanBindings,
174                                gss_buffer_t encodedBindings);
175 #endif
176
177 /* util_context.c */
178 #define EAP_EXPORT_CONTEXT_V1           1
179
180 enum gss_eap_token_type {
181     TOK_TYPE_NONE                    = 0x0000,  /* no token */
182     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
183     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
184     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
185     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* exported composite name */
186     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
187     TOK_TYPE_INITIATOR_CONTEXT       = 0x0601,  /* initiator-sent context token */
188     TOK_TYPE_ACCEPTOR_CONTEXT        = 0x0602,  /* acceptor-sent context token */
189 };
190
191 /* inner token types and flags */
192 #define ITOK_TYPE_NONE                  0x00000000
193 #define ITOK_TYPE_CONTEXT_ERR           0x00000001 /* critical */
194 #define ITOK_TYPE_ACCEPTOR_NAME_REQ     0x00000002 /* TBD */
195 #define ITOK_TYPE_ACCEPTOR_NAME_RESP    0x00000003 /* TBD */
196 #define ITOK_TYPE_EAP_RESP              0x00000004 /* critical, required, if not reauth */
197 #define ITOK_TYPE_EAP_REQ               0x00000005 /* critical, required, if not reauth */
198 #define ITOK_TYPE_GSS_CHANNEL_BINDINGS  0x00000006 /* critical, required, if not reauth */
199 #define ITOK_TYPE_REAUTH_CREDS          0x00000007 /* optional */
200 #define ITOK_TYPE_REAUTH_REQ            0x00000008 /* optional */
201 #define ITOK_TYPE_REAUTH_RESP           0x00000009 /* optional */
202 #define ITOK_TYPE_VERSION_INFO          0x0000000A /* optional */
203 #define ITOK_TYPE_VENDOR_INFO           0x0000000B /* optional */
204 #define ITOK_TYPE_GSS_FLAGS             0x0000000C /* optional */
205 #define ITOK_TYPE_INITIATOR_MIC         0x0000000D /* critical, required, if not reauth */
206 #define ITOK_TYPE_ACCEPTOR_MIC          0x0000000E /* TBD */
207
208 #define ITOK_FLAG_CRITICAL              0x80000000  /* critical, wire flag */
209 #define ITOK_FLAG_VERIFIED              0x40000000  /* verified, API flag */
210
211 #define ITOK_TYPE_MASK                  (~(ITOK_FLAG_CRITICAL | ITOK_FLAG_VERIFIED))
212
213 #define GSSEAP_WIRE_FLAGS_MASK          ( GSS_C_MUTUAL_FLAG             | \
214                                           GSS_C_DCE_STYLE               | \
215                                           GSS_C_IDENTIFY_FLAG           | \
216                                           GSS_C_EXTENDED_ERROR_FLAG       )
217
218 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
219 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
220
221 OM_uint32
222 gssEapMakeToken(OM_uint32 *minor,
223                 gss_ctx_id_t ctx,
224                 const gss_buffer_t innerToken,
225                 enum gss_eap_token_type tokenType,
226                 gss_buffer_t outputToken);
227
228 OM_uint32
229 gssEapVerifyToken(OM_uint32 *minor,
230                   gss_ctx_id_t ctx,
231                   const gss_buffer_t inputToken,
232                   enum gss_eap_token_type *tokenType,
233                   gss_buffer_t innerInputToken);
234
235 OM_uint32
236 gssEapContextTime(OM_uint32 *minor,
237                   gss_ctx_id_t context_handle,
238                   OM_uint32 *time_rec);
239
240 OM_uint32
241 gssEapMakeTokenMIC(OM_uint32 *minor,
242                    gss_ctx_id_t ctx,
243                    gss_buffer_t tokenMIC);
244
245 OM_uint32
246 gssEapVerifyTokenMIC(OM_uint32 *minor,
247                      gss_ctx_id_t ctx,
248                      const gss_buffer_t tokenMIC);
249
250 /* util_cred.c */
251 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
252 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
253
254 gss_OID
255 gssEapPrimaryMechForCred(gss_cred_id_t cred);
256
257 OM_uint32
258 gssEapAcquireCred(OM_uint32 *minor,
259                   const gss_name_t desiredName,
260                   OM_uint32 timeReq,
261                   const gss_OID_set desiredMechs,
262                   int cred_usage,
263                   gss_cred_id_t *pCred,
264                   gss_OID_set *pActualMechs,
265                   OM_uint32 *timeRec);
266
267 OM_uint32
268 gssEapSetCredPassword(OM_uint32 *minor,
269                       gss_cred_id_t cred,
270                       const gss_buffer_t password);
271
272 OM_uint32
273 gssEapSetCredClientCertificate(OM_uint32 *minor,
274                                gss_cred_id_t cred,
275                                const gss_buffer_t clientCert,
276                                const gss_buffer_t privateKey);
277
278 OM_uint32
279 gssEapSetCredService(OM_uint32 *minor,
280                      gss_cred_id_t cred,
281                      const gss_name_t target);
282
283 OM_uint32
284 gssEapResolveInitiatorCred(OM_uint32 *minor,
285                            const gss_cred_id_t cred,
286                            const gss_name_t target,
287                            gss_cred_id_t *resolvedCred);
288
289 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
290
291 OM_uint32
292 gssEapInquireCred(OM_uint32 *minor,
293                   gss_cred_id_t cred,
294                   gss_name_t *name,
295                   OM_uint32 *pLifetime,
296                   gss_cred_usage_t *cred_usage,
297                   gss_OID_set *mechanisms);
298
299 /* util_crypt.c */
300 int
301 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
302               size_t rrc,
303 #ifdef HAVE_HEIMDAL_VERSION
304               krb5_crypto crypto,
305 #else
306               krb5_keyblock *key,
307 #endif
308               int usage,
309               gss_iov_buffer_desc *iov, int iov_count);
310
311 int
312 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
313               size_t rrc,
314 #ifdef HAVE_HEIMDAL_VERSION
315               krb5_crypto crypto,
316 #else
317               krb5_keyblock *key,
318 #endif
319               int usage,
320               gss_iov_buffer_desc *iov, int iov_count);
321
322 int
323 gssEapMapCryptoFlag(OM_uint32 type);
324
325 gss_iov_buffer_t
326 gssEapLocateIov(gss_iov_buffer_desc *iov,
327                 int iov_count,
328                 OM_uint32 type);
329
330 void
331 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
332                        int iov_count,
333                        size_t *data_length,
334                        size_t *assoc_data_length);
335
336 void
337 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
338
339 int
340 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
341
342 int
343 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
344
345 OM_uint32
346 gssEapDeriveRfc3961Key(OM_uint32 *minor,
347                        const unsigned char *key,
348                        size_t keyLength,
349                        krb5_enctype enctype,
350                        krb5_keyblock *pKey);
351
352 /* util_krb.c */
353
354 #ifndef KRB_MALLOC
355 /*
356  * If your Kerberos library uses a different allocator to your
357  * GSS mechanism glue, then you might wish to define these in
358  * config.h or elsewhere. This should eventually go away when
359  * we no longer need to allocate memory that is freed by the
360  * Kerberos library.
361  */
362 #define KRB_CALLOC                      calloc
363 #define KRB_MALLOC                      malloc
364 #define KRB_FREE                        free
365 #define KRB_REALLOC                     realloc
366 #endif /* KRB_MALLOC */
367
368 #ifdef HAVE_HEIMDAL_VERSION
369
370 #define KRB_TIME_FOREVER        ((time_t)~0L)
371
372 #define KRB_KEY_TYPE(key)       ((key)->keytype)
373 #define KRB_KEY_DATA(key)       ((key)->keyvalue.data)
374 #define KRB_KEY_LENGTH(key)     ((key)->keyvalue.length)
375
376 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
377 #define KRB_PRINC_TYPE(princ)   ((princ)->name.name_type)
378 #define KRB_PRINC_NAME(princ)   ((princ)->name.name_string.val)
379 #define KRB_PRINC_REALM(princ)  ((princ)->realm)
380
381 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->keyblock)
382 #define KRB_KT_ENT_FREE(c, e)   krb5_kt_free_entry((c), (e))
383
384 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
385
386 #define KRB_DATA_INIT(d)        krb5_data_zero((d))
387
388 #else
389
390 #define KRB_TIME_FOREVER        KRB5_INT32_MAX
391
392 #define KRB_KEY_TYPE(key)       ((key)->enctype)
393 #define KRB_KEY_DATA(key)       ((key)->contents)
394 #define KRB_KEY_LENGTH(key)     ((key)->length)
395
396 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
397 #define KRB_PRINC_TYPE(princ)   (krb5_princ_type(NULL, (princ)))
398 #define KRB_PRINC_NAME(princ)   (krb5_princ_name(NULL, (princ)))
399 #define KRB_PRINC_REALM(princ)  (krb5_princ_realm(NULL, (princ)))
400
401 #define KRB_KT_ENT_KEYBLOCK(e)  (&(e)->key)
402 #define KRB_KT_ENT_FREE(c, e)   krb5_free_keytab_entry_contents((c), (e))
403
404 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
405
406 #define KRB_DATA_INIT(d)        do {        \
407         (d)->magic = KV5M_DATA;             \
408         (d)->length = 0;                    \
409         (d)->data = NULL;                   \
410     } while (0)
411
412 #endif /* HAVE_HEIMDAL_VERSION */
413
414 #define KRB_KEY_INIT(key)       do {        \
415         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
416         KRB_KEY_DATA(key) = NULL;           \
417         KRB_KEY_LENGTH(key) = 0;            \
418     } while (0)
419
420 #define GSSEAP_KRB_INIT(ctx) do {                   \
421         OM_uint32 tmpMajor;                         \
422                                                     \
423         tmpMajor  = gssEapKerberosInit(minor, ctx); \
424         if (GSS_ERROR(tmpMajor)) {                  \
425             return tmpMajor;                        \
426         }                                           \
427     } while (0)
428
429 OM_uint32
430 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
431
432 OM_uint32
433 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
434                           krb5_keyblock *key,
435                           krb5_cksumtype *cksumtype);
436
437 krb5_error_code
438 krbCryptoLength(krb5_context krbContext,
439 #ifdef HAVE_HEIMDAL_VERSION
440                 krb5_crypto krbCrypto,
441 #else
442                 krb5_keyblock *key,
443 #endif
444                 int type,
445                 size_t *length);
446
447 krb5_error_code
448 krbPaddingLength(krb5_context krbContext,
449 #ifdef HAVE_HEIMDAL_VERSION
450                  krb5_crypto krbCrypto,
451 #else
452                  krb5_keyblock *key,
453 #endif
454                  size_t dataLength,
455                  size_t *padLength);
456
457 krb5_error_code
458 krbBlockSize(krb5_context krbContext,
459 #ifdef HAVE_HEIMDAL_VERSION
460                  krb5_crypto krbCrypto,
461 #else
462                  krb5_keyblock *key,
463 #endif
464                  size_t *blockSize);
465
466 krb5_error_code
467 krbEnctypeToString(krb5_context krbContext,
468                    krb5_enctype enctype,
469                    const char *prefix,
470                    gss_buffer_t string);
471
472 krb5_error_code
473 krbMakeAuthDataKdcIssued(krb5_context context,
474                          const krb5_keyblock *key,
475                          krb5_const_principal issuer,
476 #ifdef HAVE_HEIMDAL_VERSION
477                          const AuthorizationData *authdata,
478                          AuthorizationData *adKdcIssued
479 #else
480                          krb5_authdata *const *authdata,
481                          krb5_authdata ***adKdcIssued
482 #endif
483                          );
484
485 krb5_error_code
486 krbMakeCred(krb5_context context,
487             krb5_auth_context authcontext,
488             krb5_creds *creds,
489             krb5_data *data);
490
491 /* util_lucid.c */
492 OM_uint32
493 gssEapExportLucidSecContext(OM_uint32 *minor,
494                             gss_ctx_id_t ctx,
495                             const gss_OID desiredObject,
496                             gss_buffer_set_t *data_set);
497
498 /* util_mech.c */
499 extern gss_OID GSS_EAP_MECHANISM;
500
501 #define OID_FLAG_NULL_VALID                 0x00000001
502 #define OID_FLAG_FAMILY_MECH_VALID          0x00000002
503 #define OID_FLAG_MAP_NULL_TO_DEFAULT_MECH   0x00000004
504 #define OID_FLAG_MAP_FAMILY_MECH_TO_NULL    0x00000008
505
506 OM_uint32
507 gssEapCanonicalizeOid(OM_uint32 *minor,
508                       const gss_OID oid,
509                       OM_uint32 flags,
510                       gss_OID *pOid);
511
512 OM_uint32
513 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
514
515 OM_uint32
516 gssEapDefaultMech(OM_uint32 *minor,
517                   gss_OID *oid);
518
519 OM_uint32
520 gssEapIndicateMechs(OM_uint32 *minor,
521                     gss_OID_set *mechs);
522
523 OM_uint32
524 gssEapEnctypeToOid(OM_uint32 *minor,
525                    krb5_enctype enctype,
526                    gss_OID *pOid);
527
528 OM_uint32
529 gssEapOidToEnctype(OM_uint32 *minor,
530                    const gss_OID oid,
531                    krb5_enctype *enctype);
532
533 int
534 gssEapIsMechanismOid(const gss_OID oid);
535
536 int
537 gssEapIsConcreteMechanismOid(const gss_OID oid);
538
539 OM_uint32
540 gssEapValidateMechs(OM_uint32 *minor,
541                    const gss_OID_set mechs);
542
543 gss_buffer_t
544 gssEapOidToSaslName(const gss_OID oid);
545
546 gss_OID
547 gssEapSaslNameToOid(const gss_buffer_t name);
548
549 /* util_moonshot.c */
550 OM_uint32
551 libMoonshotResolveDefaultIdentity(OM_uint32 *minor,
552                                   const gss_cred_id_t cred,
553                                   gss_name_t *pName);
554
555 OM_uint32
556 libMoonshotResolveInitiatorCred(OM_uint32 *minor,
557                                 gss_cred_id_t cred,
558                                 const gss_name_t targetName);
559
560 /* util_name.c */
561 #define EXPORT_NAME_FLAG_OID                    0x1
562 #define EXPORT_NAME_FLAG_COMPOSITE              0x2
563 #define EXPORT_NAME_FLAG_ALLOW_COMPOSITE        0x4
564
565 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
566 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
567 OM_uint32 gssEapExportName(OM_uint32 *minor,
568                            const gss_name_t name,
569                            gss_buffer_t exportedName);
570 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
571                                    const gss_name_t name,
572                                    gss_buffer_t exportedName,
573                                    OM_uint32 flags);
574 OM_uint32 gssEapImportName(OM_uint32 *minor,
575                            const gss_buffer_t input_name_buffer,
576                            const gss_OID input_name_type,
577                            const gss_OID input_mech_type,
578                            gss_name_t *output_name);
579 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
580                                    const gss_buffer_t input_name_buffer,
581                                    gss_name_t *output_name,
582                                    OM_uint32 flags);
583 OM_uint32
584 gssEapDuplicateName(OM_uint32 *minor,
585                     const gss_name_t input_name,
586                     gss_name_t *dest_name);
587
588 OM_uint32
589 gssEapCanonicalizeName(OM_uint32 *minor,
590                        const gss_name_t input_name,
591                        const gss_OID mech_type,
592                        gss_name_t *dest_name);
593
594 OM_uint32
595 gssEapDisplayName(OM_uint32 *minor,
596                   gss_name_t name,
597                   gss_buffer_t output_name_buffer,
598                   gss_OID *output_name_type);
599
600 OM_uint32
601 gssEapCompareName(OM_uint32 *minor,
602                   gss_name_t name1,
603                   gss_name_t name2,
604                   int *name_equal);
605
606 /* util_oid.c */
607 OM_uint32
608 composeOid(OM_uint32 *minor_status,
609            const char *prefix,
610            size_t prefix_len,
611            int suffix,
612            gss_OID_desc *oid);
613
614 OM_uint32
615 decomposeOid(OM_uint32 *minor_status,
616              const char *prefix,
617              size_t prefix_len,
618              gss_OID_desc *oid,
619              int *suffix) ;
620
621 OM_uint32
622 duplicateOid(OM_uint32 *minor_status,
623              const gss_OID_desc * const oid,
624              gss_OID *new_oid);
625
626 OM_uint32
627 duplicateOidSet(OM_uint32 *minor,
628                 const gss_OID_set src,
629                 gss_OID_set *dst);
630
631 static inline int
632 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
633 {
634     if (o1 == GSS_C_NO_OID)
635         return (o2 == GSS_C_NO_OID);
636     else if (o2 == GSS_C_NO_OID)
637         return (o1 == GSS_C_NO_OID);
638     else
639         return (o1->length == o2->length &&
640                 memcmp(o1->elements, o2->elements, o1->length) == 0);
641 }
642
643 /* util_ordering.c */
644 OM_uint32
645 sequenceInternalize(OM_uint32 *minor,
646                     void **vqueue,
647                     unsigned char **buf,
648                     size_t *lenremain);
649
650 OM_uint32
651 sequenceExternalize(OM_uint32 *minor,
652                     void *vqueue,
653                     unsigned char **buf,
654                     size_t *lenremain);
655
656 size_t
657 sequenceSize(void *vqueue);
658
659 OM_uint32
660 sequenceFree(OM_uint32 *minor, void **vqueue);
661
662 OM_uint32
663 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
664
665 OM_uint32
666 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
667              int do_replay, int do_sequence, int wide_nums);
668
669 /* util_sm.c */
670 enum gss_eap_state {
671     GSSEAP_STATE_INITIAL        = 0x01,     /* initial state */
672     GSSEAP_STATE_AUTHENTICATE   = 0x02,     /* exchange EAP messages */
673     GSSEAP_STATE_INITIATOR_EXTS = 0x04,     /* initiator extensions */
674     GSSEAP_STATE_ACCEPTOR_EXTS  = 0x08,     /* acceptor extensions */
675 #ifdef GSSEAP_ENABLE_REAUTH
676     GSSEAP_STATE_REAUTHENTICATE = 0x10,     /* GSS reauthentication messages */
677 #endif
678     GSSEAP_STATE_ESTABLISHED    = 0x20,     /* context established */
679     GSSEAP_STATE_ALL            = 0x3F
680 };
681
682 #define GSSEAP_STATE_NEXT(s)    ((s) << 1)
683
684 #define GSSEAP_SM_STATE(ctx)                ((ctx)->state)
685
686 #ifdef GSSEAP_DEBUG
687 void gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
688 #define GSSEAP_SM_TRANSITION(ctx, state)    gssEapSmTransition((ctx), (state))
689 #else
690 #define GSSEAP_SM_TRANSITION(ctx, newstate)    do { (ctx)->state = (newstate); } while (0)
691 #endif
692
693 #define GSSEAP_SM_TRANSITION_NEXT(ctx)      GSSEAP_SM_TRANSITION((ctx), GSSEAP_STATE_NEXT(GSSEAP_SM_STATE((ctx))))
694
695 /* state machine entry */
696 struct gss_eap_sm {
697     OM_uint32 inputTokenType;
698     OM_uint32 outputTokenType;
699     enum gss_eap_state validStates;
700     OM_uint32 itokFlags;
701     OM_uint32 (*processToken)(OM_uint32 *,
702                               gss_cred_id_t,
703                               gss_ctx_id_t,
704                               gss_name_t,
705                               gss_OID,
706                               OM_uint32,
707                               OM_uint32,
708                               gss_channel_bindings_t,
709                               gss_buffer_t,
710                               gss_buffer_t,
711                               OM_uint32 *);
712 };
713
714 /* state machine flags, set by handler */
715 #define SM_FLAG_FORCE_SEND_TOKEN            0x00000001  /* send token even if no inner tokens */
716 #define SM_FLAG_OUTPUT_TOKEN_CRITICAL       0x00000002  /* output token is critical */
717
718 /* state machine flags, set by state machine */
719 #define SM_FLAG_INPUT_TOKEN_CRITICAL        0x10000000  /* input token was critical */
720
721 #define SM_ITOK_FLAG_REQUIRED               0x00000001  /* received tokens must be present */
722
723 OM_uint32
724 gssEapSmStep(OM_uint32 *minor,
725              gss_cred_id_t cred,
726              gss_ctx_id_t ctx,
727              gss_name_t target,
728              gss_OID mech,
729              OM_uint32 reqFlags,
730              OM_uint32 timeReq,
731              gss_channel_bindings_t chanBindings,
732              gss_buffer_t inputToken,
733              gss_buffer_t outputToken,
734              struct gss_eap_sm *sm,
735              size_t smCount);
736
737 void
738 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
739
740 /* util_token.c */
741 struct gss_eap_token_buffer_set {
742     gss_buffer_set_desc buffers; /* pointers only */
743     OM_uint32 *types;
744 };
745
746 OM_uint32
747 gssEapEncodeInnerTokens(OM_uint32 *minor,
748                         struct gss_eap_token_buffer_set *tokens,
749                         gss_buffer_t buffer);
750 OM_uint32
751 gssEapDecodeInnerTokens(OM_uint32 *minor,
752                         const gss_buffer_t buffer,
753                         struct gss_eap_token_buffer_set *tokens);
754
755 OM_uint32
756 gssEapReleaseInnerTokens(OM_uint32 *minor,
757                          struct gss_eap_token_buffer_set *tokens,
758                          int freeBuffers);
759
760 OM_uint32
761 gssEapAllocInnerTokens(OM_uint32 *minor,
762                        size_t count,
763                        struct gss_eap_token_buffer_set *tokens);
764
765 size_t
766 tokenSize(const gss_OID_desc *mech, size_t body_size);
767
768 void
769 makeTokenHeader(const gss_OID_desc *mech,
770                 size_t body_size,
771                 unsigned char **buf,
772                 enum gss_eap_token_type tok_type);
773
774 OM_uint32
775 verifyTokenHeader(OM_uint32 *minor,
776                   gss_OID mech,
777                   size_t *body_size,
778                   unsigned char **buf_in,
779                   size_t toksize_in,
780                   enum gss_eap_token_type *ret_tok_type);
781
782 /* Helper macros */
783
784 #ifndef GSSEAP_MALLOC
785 #define GSSEAP_CALLOC                   calloc
786 #define GSSEAP_MALLOC                   malloc
787 #define GSSEAP_FREE                     free
788 #define GSSEAP_REALLOC                  realloc
789 #endif
790
791 #ifndef GSSAPI_CALLCONV
792 #define GSSAPI_CALLCONV                 KRB5_CALLCONV
793 #endif
794
795 #ifndef GSSEAP_ASSERT
796 #include <assert.h>
797 #define GSSEAP_ASSERT(x)                assert((x))
798 #endif /* !GSSEAP_ASSERT */
799
800 #ifdef WIN32
801 #define GSSEAP_CONSTRUCTOR
802 #define GSSEAP_DESTRUCTOR
803 #else
804 #define GSSEAP_CONSTRUCTOR              __attribute__((constructor))
805 #define GSSEAP_DESTRUCTOR               __attribute__((destructor))
806 #endif
807
808 #define GSSEAP_NOT_IMPLEMENTED          do {            \
809         GSSEAP_ASSERT(0 && "not implemented");          \
810         *minor = ENOSYS;                                \
811         return GSS_S_FAILURE;                           \
812     } while (0)
813
814 #ifdef WIN32
815
816 #include <winbase.h>
817
818 #define GSSEAP_GET_LAST_ERROR()         (GetLastError()) /* XXX FIXME */
819
820 #define GSSEAP_MUTEX                    CRITICAL_SECTION
821 #define GSSEAP_MUTEX_INIT(m)            (InitializeCriticalSection((m)), 0)
822 #define GSSEAP_MUTEX_DESTROY(m)         DeleteCriticalSection((m))
823 #define GSSEAP_MUTEX_LOCK(m)            EnterCriticalSection((m))
824 #define GSSEAP_MUTEX_UNLOCK(m)          LeaveCriticalSection((m))
825 #define GSSEAP_ONCE_LEAVE               do { return TRUE; } while (0)
826
827 /* Thread-local is handled separately */
828
829 #define GSSEAP_THREAD_ONCE              INIT_ONCE
830 #define GSSEAP_ONCE_CALLBACK(cb)        BOOL CALLBACK cb(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
831 #define GSSEAP_ONCE(o, i)               InitOnceExecuteOnce((o), (i), NULL, NULL)
832 #define GSSEAP_ONCE_INITIALIZER         INIT_ONCE_STATIC_INIT
833
834 #else
835
836 #include <pthread.h>
837
838 #define GSSEAP_GET_LAST_ERROR()         (errno)
839
840 #define GSSEAP_MUTEX                    pthread_mutex_t
841 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
842 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
843 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
844 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
845
846 #define GSSEAP_THREAD_KEY               pthread_key_t
847 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
848 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
849 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
850
851 #define GSSEAP_THREAD_ONCE              pthread_once_t
852 #define GSSEAP_ONCE_CALLBACK(cb)        void cb(void)
853 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
854 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
855 #define GSSEAP_ONCE_LEAVE               do { } while (0)
856
857 #endif /* WIN32 */
858
859 /* Helper functions */
860 static inline void
861 store_uint16_be(uint16_t val, void *vp)
862 {
863     unsigned char *p = (unsigned char *)vp;
864
865     p[0] = (val >>  8) & 0xff;
866     p[1] = (val      ) & 0xff;
867 }
868
869 static inline uint16_t
870 load_uint16_be(const void *cvp)
871 {
872     const unsigned char *p = (const unsigned char *)cvp;
873
874     return (p[1] | (p[0] << 8));
875 }
876
877 static inline void
878 store_uint32_be(uint32_t val, void *vp)
879 {
880     unsigned char *p = (unsigned char *)vp;
881
882     p[0] = (val >> 24) & 0xff;
883     p[1] = (val >> 16) & 0xff;
884     p[2] = (val >>  8) & 0xff;
885     p[3] = (val      ) & 0xff;
886 }
887
888 static inline uint32_t
889 load_uint32_be(const void *cvp)
890 {
891     const unsigned char *p = (const unsigned char *)cvp;
892
893     return (p[3] | (p[2] << 8)
894             | ((uint32_t) p[1] << 16)
895             | ((uint32_t) p[0] << 24));
896 }
897
898 static inline void
899 store_uint64_be(uint64_t val, void *vp)
900 {
901     unsigned char *p = (unsigned char *)vp;
902
903     p[0] = (unsigned char)((val >> 56) & 0xff);
904     p[1] = (unsigned char)((val >> 48) & 0xff);
905     p[2] = (unsigned char)((val >> 40) & 0xff);
906     p[3] = (unsigned char)((val >> 32) & 0xff);
907     p[4] = (unsigned char)((val >> 24) & 0xff);
908     p[5] = (unsigned char)((val >> 16) & 0xff);
909     p[6] = (unsigned char)((val >>  8) & 0xff);
910     p[7] = (unsigned char)((val      ) & 0xff);
911 }
912
913 static inline uint64_t
914 load_uint64_be(const void *cvp)
915 {
916     const unsigned char *p = (const unsigned char *)cvp;
917
918     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
919 }
920
921 static inline unsigned char *
922 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
923 {
924     unsigned char *p = (unsigned char *)vp;
925
926     if (wide_nums) {
927         store_uint64_be(buffer->length, p);
928         p += 8;
929     } else {
930         store_uint32_be(buffer->length, p);
931         p += 4;
932     }
933
934     if (buffer->value != NULL) {
935         memcpy(p, buffer->value, buffer->length);
936         p += buffer->length;
937     }
938
939     return p;
940 }
941
942 static inline unsigned char *
943 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
944 {
945     buffer->length = 0;
946     buffer->value = GSSEAP_MALLOC(length);
947     if (buffer->value == NULL)
948         return NULL;
949     buffer->length = length;
950     memcpy(buffer->value, cvp, length);
951     return (unsigned char *)cvp + length;
952 }
953
954 static inline unsigned char *
955 store_oid(gss_OID oid, void *vp)
956 {
957     gss_buffer_desc buf;
958
959     if (oid != GSS_C_NO_OID) {
960         buf.length = oid->length;
961         buf.value = oid->elements;
962     } else {
963         buf.length = 0;
964         buf.value = NULL;
965     }
966
967     return store_buffer(&buf, vp, FALSE);
968 }
969
970 static inline void
971 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
972 {
973     buffer->value = (void *)data->data;
974     buffer->length = data->length;
975 }
976
977 static inline void
978 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
979                              int index, gss_buffer_t buffer)
980 {
981 #ifdef HAVE_HEIMDAL_VERSION
982     buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
983     buffer->length = strlen((char *)buffer->value);
984 #else
985     buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
986     buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
987 #endif /* HAVE_HEIMDAL_VERSION */
988 }
989
990 static inline void
991 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
992 {
993 #ifdef HAVE_HEIMDAL_VERSION
994     buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
995     buffer->length = strlen((char *)buffer->value);
996 #else
997     krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
998 #endif
999 }
1000
1001 static inline void
1002 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
1003 {
1004     data->data = (char *)buffer->value;
1005     data->length = buffer->length;
1006 }
1007
1008 /* util_tld.c */
1009 struct gss_eap_status_info;
1010
1011 struct gss_eap_thread_local_data {
1012     krb5_context krbContext;
1013     struct gss_eap_status_info *statusInfo;
1014 };
1015
1016 struct gss_eap_thread_local_data *
1017 gssEapGetThreadLocalData(void);
1018
1019 void
1020 gssEapDestroyStatusInfo(struct gss_eap_status_info *status);
1021
1022 void
1023 gssEapDestroyKrbContext(krb5_context context);
1024
1025 #ifdef __cplusplus
1026 }
1027 #endif
1028
1029 #ifdef GSSEAP_ENABLE_ACCEPTOR
1030 #include "util_json.h"
1031 #include "util_attr.h"
1032 #include "util_base64.h"
1033 #endif /* GSSEAP_ENABLE_ACCEPTOR */
1034 #ifdef GSSEAP_ENABLE_REAUTH
1035 #include "util_reauth.h"
1036 #endif
1037
1038 #endif /* _UTIL_H_ */