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