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