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