2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
33 * Portions Copyright 2003-2010 Massachusetts Institute of Technology.
34 * All Rights Reserved.
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.
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.
64 #include <sys/param.h>
75 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
80 makeStringBuffer(OM_uint32 *minor,
85 bufferToString(OM_uint32 *minor,
86 const gss_buffer_t buffer,
90 duplicateBuffer(OM_uint32 *minor,
91 const gss_buffer_t src,
95 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
97 return (b1->length == b2->length &&
98 memcmp(b1->value, b2->value, b2->length) == 0);
102 bufferEqualString(const gss_buffer_t b1, const char *s)
106 b2.length = strlen(s);
107 b2.value = (char *)s;
109 return bufferEqual(b1, &b2);
114 gssEapSign(krb5_context context,
117 #ifdef HAVE_HEIMDAL_VERSION
122 krb5_keyusage sign_usage,
123 gss_iov_buffer_desc *iov,
127 gssEapVerify(krb5_context context,
130 #ifdef HAVE_HEIMDAL_VERSION
135 krb5_keyusage sign_usage,
136 gss_iov_buffer_desc *iov,
142 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
143 gss_channel_bindings_t chanBindings,
144 gss_buffer_t encodedBindings);
148 #define EAP_EXPORT_CONTEXT_V1 1
150 enum gss_eap_token_type {
151 TOK_TYPE_NONE = 0x0000, /* no token */
152 TOK_TYPE_MIC = 0x0404, /* RFC 4121 MIC token */
153 TOK_TYPE_WRAP = 0x0504, /* RFC 4121 wrap token */
154 TOK_TYPE_EXPORT_NAME = 0x0401, /* RFC 2743 exported name */
155 TOK_TYPE_EXPORT_NAME_COMPOSITE = 0x0402, /* exported composite name */
156 TOK_TYPE_DELETE_CONTEXT = 0x0405, /* RFC 2743 delete context */
157 TOK_TYPE_ESTABLISH_CONTEXT = 0x0601, /* establish context */
160 /* inner token types and flags */
161 #define ITOK_TYPE_NONE 0x00000000
162 #define ITOK_TYPE_CONTEXT_ERR 0x00000001
163 #define ITOK_TYPE_ACCEPTOR_NAME_REQ 0x00000002
164 #define ITOK_TYPE_ACCEPTOR_NAME_RESP 0x00000003
165 #define ITOK_TYPE_EAP_RESP 0x00000004
166 #define ITOK_TYPE_EAP_REQ 0x00000005
167 #define ITOK_TYPE_GSS_CHANNEL_BINDINGS 0x00000006
168 #define ITOK_TYPE_REAUTH_CREDS 0x00000007
169 #define ITOK_TYPE_REAUTH_REQ 0x00000008
170 #define ITOK_TYPE_REAUTH_RESP 0x00000009
171 #define ITOK_TYPE_VERSION_INFO 0x0000000A
172 #define ITOK_TYPE_VENDOR_INFO 0x0000000B
174 #define ITOK_FLAG_CRITICAL 0x80000000 /* critical, wire flag */
175 #define ITOK_FLAG_VERIFIED 0x40000000 /* verified, API flag */
177 #define ITOK_TYPE_MASK (~(ITOK_FLAG_CRITICAL | ITOK_FLAG_VERIFIED))
179 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
180 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
183 gssEapMakeToken(OM_uint32 *minor,
185 const gss_buffer_t innerToken,
186 enum gss_eap_token_type tokenType,
187 gss_buffer_t outputToken);
190 gssEapVerifyToken(OM_uint32 *minor,
192 const gss_buffer_t inputToken,
193 enum gss_eap_token_type *tokenType,
194 gss_buffer_t innerInputToken);
197 gssEapContextTime(OM_uint32 *minor,
198 gss_ctx_id_t context_handle,
199 OM_uint32 *time_rec);
202 gssEapDisplayName(OM_uint32 *minor,
204 gss_buffer_t output_name_buffer,
205 gss_OID *output_name_type);
208 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
209 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
212 gssEapAcquireCred(OM_uint32 *minor,
213 const gss_name_t desiredName,
214 const gss_buffer_t password,
216 const gss_OID_set desiredMechs,
218 gss_cred_id_t *pCred,
219 gss_OID_set *pActualMechs,
222 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
226 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
228 #ifdef HAVE_HEIMDAL_VERSION
234 gss_iov_buffer_desc *iov, int iov_count);
237 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
239 #ifdef HAVE_HEIMDAL_VERSION
245 gss_iov_buffer_desc *iov, int iov_count);
248 gssEapMapCryptoFlag(OM_uint32 type);
251 gssEapLocateIov(gss_iov_buffer_desc *iov,
256 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
259 size_t *assoc_data_length);
262 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
265 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
268 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
271 gssEapDeriveRfc3961Key(OM_uint32 *minor,
272 const unsigned char *key,
274 krb5_enctype enctype,
275 krb5_keyblock *pKey);
278 #ifdef HAVE_HEIMDAL_VERSION
280 #define KRB_TIME_FOREVER ((time_t)~0L)
282 #define KRB_KEY_TYPE(key) ((key)->keytype)
283 #define KRB_KEY_DATA(key) ((key)->keyvalue.data)
284 #define KRB_KEY_LENGTH(key) ((key)->keyvalue.length)
286 #define KRB_PRINC_LENGTH(princ) ((princ)->name.name_string.len)
287 #define KRB_PRINC_TYPE(princ) ((princ)->name.name_type)
288 #define KRB_PRINC_NAME(princ) ((princ)->name.name_string.val)
289 #define KRB_PRINC_REALM(princ) ((princ)->realm)
291 #define KRB_KT_ENT_KEYBLOCK(e) (&(e)->keyblock)
292 #define KRB_KT_ENT_FREE(c, e) krb5_kt_free_entry((c), (e))
294 #define KRB_CRYPTO_CONTEXT(ctx) (krbCrypto)
298 #define KRB_TIME_FOREVER KRB5_INT32_MAX
300 #define KRB_KEY_TYPE(key) ((key)->enctype)
301 #define KRB_KEY_DATA(key) ((key)->contents)
302 #define KRB_KEY_LENGTH(key) ((key)->length)
304 #define KRB_PRINC_LENGTH(princ) (krb5_princ_size(NULL, (princ)))
305 #define KRB_PRINC_TYPE(princ) (krb5_princ_type(NULL, (princ)))
306 #define KRB_PRINC_NAME(princ) (krb5_princ_name(NULL, (princ)))
307 #define KRB_PRINC_REALM(princ) (krb5_princ_realm(NULL, (princ)))
309 #define KRB_KT_ENT_KEYBLOCK(e) (&(e)->key)
310 #define KRB_KT_ENT_FREE(c, e) krb5_free_keytab_entry_contents((c), (e))
312 #define KRB_CRYPTO_CONTEXT(ctx) (&(ctx)->rfc3961Key)
314 #endif /* HAVE_HEIMDAL_VERSION */
316 #define KRB_KEY_INIT(key) do { \
317 KRB_KEY_TYPE(key) = ENCTYPE_NULL; \
318 KRB_KEY_DATA(key) = NULL; \
319 KRB_KEY_LENGTH(key) = 0; \
322 #define GSSEAP_KRB_INIT(ctx) do { \
323 OM_uint32 tmpMajor; \
325 tmpMajor = gssEapKerberosInit(minor, ctx); \
326 if (GSS_ERROR(tmpMajor)) { \
332 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
335 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
337 krb5_cksumtype *cksumtype);
340 krbAnonymousPrincipal(void);
343 krbCryptoLength(krb5_context krbContext,
344 #ifdef HAVE_HEIMDAL_VERSION
345 krb5_crypto krbCrypto,
353 krbPaddingLength(krb5_context krbContext,
354 #ifdef HAVE_HEIMDAL_VERSION
355 krb5_crypto krbCrypto,
363 krbBlockSize(krb5_context krbContext,
364 #ifdef HAVE_HEIMDAL_VERSION
365 krb5_crypto krbCrypto,
372 krbEnctypeToString(krb5_context krbContext,
373 krb5_enctype enctype,
375 gss_buffer_t string);
378 krbMakeAuthDataKdcIssued(krb5_context context,
379 const krb5_keyblock *key,
380 krb5_const_principal issuer,
381 #ifdef HAVE_HEIMDAL_VERSION
382 const AuthorizationData *authdata,
383 AuthorizationData *adKdcIssued
385 krb5_authdata *const *authdata,
386 krb5_authdata ***adKdcIssued
391 krbMakeCred(krb5_context context,
392 krb5_auth_context authcontext,
398 gssEapExportLucidSecContext(OM_uint32 *minor,
400 const gss_OID desiredObject,
401 gss_buffer_set_t *data_set);
404 extern gss_OID GSS_EAP_MECHANISM;
407 gssEapInternalizeOid(const gss_OID oid,
408 gss_OID *const pInternalizedOid);
411 gssEapReleaseOid(OM_uint32 *minor, gss_OID *oid);
414 gssEapDefaultMech(OM_uint32 *minor,
418 gssEapIndicateMechs(OM_uint32 *minor,
422 gssEapEnctypeToOid(OM_uint32 *minor,
423 krb5_enctype enctype,
427 gssEapOidToEnctype(OM_uint32 *minor,
429 krb5_enctype *enctype);
432 gssEapIsMechanismOid(const gss_OID oid);
435 gssEapIsConcreteMechanismOid(const gss_OID oid);
438 gssEapValidateMechs(OM_uint32 *minor,
439 const gss_OID_set mechs);
442 gssEapOidToSaslName(const gss_OID oid);
445 gssEapSaslNameToOid(const gss_buffer_t name);
448 #define EXPORT_NAME_FLAG_OID 0x1
449 #define EXPORT_NAME_FLAG_COMPOSITE 0x2
451 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
452 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
453 OM_uint32 gssEapExportName(OM_uint32 *minor,
454 const gss_name_t name,
455 gss_buffer_t exportedName);
456 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
457 const gss_name_t name,
458 gss_buffer_t exportedName,
460 OM_uint32 gssEapImportName(OM_uint32 *minor,
461 const gss_buffer_t input_name_buffer,
462 gss_OID input_name_type,
463 gss_name_t *output_name);
464 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
465 const gss_buffer_t input_name_buffer,
466 gss_name_t *output_name,
469 gssEapDuplicateName(OM_uint32 *minor,
470 const gss_name_t input_name,
471 gss_name_t *dest_name);
475 composeOid(OM_uint32 *minor_status,
482 decomposeOid(OM_uint32 *minor_status,
489 duplicateOid(OM_uint32 *minor_status,
490 const gss_OID_desc * const oid,
494 duplicateOidSet(OM_uint32 *minor,
495 const gss_OID_set src,
499 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
501 if (o1 == GSS_C_NO_OID)
502 return (o2 == GSS_C_NO_OID);
503 else if (o2 == GSS_C_NO_OID)
504 return (o1 == GSS_C_NO_OID);
506 return (o1->length == o2->length &&
507 memcmp(o1->elements, o2->elements, o1->length) == 0);
510 /* util_ordering.c */
512 sequenceInternalize(OM_uint32 *minor,
518 sequenceExternalize(OM_uint32 *minor,
524 sequenceSize(void *vqueue);
527 sequenceFree(OM_uint32 *minor, void **vqueue);
530 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
533 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
534 int do_replay, int do_sequence, int wide_nums);
538 GSSEAP_STATE_INITIAL = 0x01, /* initial state */
539 GSSEAP_STATE_AUTHENTICATE = 0x02, /* exchange EAP messages */
540 GSSEAP_STATE_INITIATOR_EXTS = 0x04, /* initiator extensions */
541 GSSEAP_STATE_ACCEPTOR_EXTS = 0x08, /* acceptor extensions */
542 GSSEAP_STATE_REAUTHENTICATE = 0x10, /* GSS reauthentication messages */
543 GSSEAP_STATE_ESTABLISHED = 0x20, /* context established */
544 GSSEAP_STATE_ALL = 0x3F
547 #define GSSEAP_STATE_NEXT(s) ((s) << 1)
549 /* state machine entry */
551 OM_uint32 inputTokenType;
552 OM_uint32 outputTokenType;
553 enum gss_eap_state validStates;
555 OM_uint32 (*processToken)(OM_uint32 *,
562 gss_channel_bindings_t,
568 #define SM_FLAG_TRANSITION 0x00000001 /* transition to next state */
569 #define SM_FLAG_FORCE_SEND_TOKEN 0x00000002 /* send token even if empty */
570 #define SM_FLAG_STOP_EVAL 0x00000004 /* no more handlers for this state */
572 #define SM_ITOK_FLAG_CRITICAL 0x00000001 /* sent tokens marked critical */
573 #define SM_ITOK_FLAG_REQUIRED 0x00000002 /* received tokens must be present */
576 gssEapSmStep(OM_uint32 *minor,
583 gss_channel_bindings_t chanBindings,
584 gss_buffer_t inputToken,
585 gss_buffer_t outputToken,
586 struct gss_eap_sm *sm,
590 gssEapSmTransition(gss_ctx_id_t ctx, enum gss_eap_state state);
594 gssEapEncodeInnerTokens(OM_uint32 *minor,
595 gss_buffer_set_t extensions,
597 gss_buffer_t buffer);
599 gssEapDecodeInnerTokens(OM_uint32 *minor,
600 const gss_buffer_t buffer,
601 gss_buffer_set_t *pExtensions,
605 tokenSize(const gss_OID_desc *mech, size_t body_size);
608 makeTokenHeader(const gss_OID_desc *mech,
611 enum gss_eap_token_type tok_type);
614 verifyTokenHeader(OM_uint32 *minor,
617 unsigned char **buf_in,
619 enum gss_eap_token_type *ret_tok_type);
623 #define GSSEAP_CALLOC calloc
624 #define GSSEAP_MALLOC malloc
625 #define GSSEAP_FREE free
626 #define GSSEAP_REALLOC realloc
628 #define GSSEAP_NOT_IMPLEMENTED do { \
629 assert(0 && "not implemented"); \
631 return GSS_S_FAILURE; \
636 #define GSSEAP_MUTEX pthread_mutex_t
637 #define GSSEAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
639 #define GSSEAP_MUTEX_INIT(m) pthread_mutex_init((m), NULL)
640 #define GSSEAP_MUTEX_DESTROY(m) pthread_mutex_destroy((m))
641 #define GSSEAP_MUTEX_LOCK(m) pthread_mutex_lock((m))
642 #define GSSEAP_MUTEX_UNLOCK(m) pthread_mutex_unlock((m))
644 #define GSSEAP_THREAD_KEY pthread_key_t
645 #define GSSEAP_KEY_CREATE(k, d) pthread_key_create((k), (d))
646 #define GSSEAP_GETSPECIFIC(k) pthread_getspecific((k))
647 #define GSSEAP_SETSPECIFIC(k, d) pthread_setspecific((k), (d))
649 #define GSSEAP_THREAD_ONCE pthread_once_t
650 #define GSSEAP_ONCE(o, i) pthread_once((o), (i))
651 #define GSSEAP_ONCE_INITIALIZER PTHREAD_ONCE_INIT
653 /* Helper functions */
655 store_uint16_be(uint16_t val, void *vp)
657 unsigned char *p = (unsigned char *)vp;
659 p[0] = (val >> 8) & 0xff;
660 p[1] = (val ) & 0xff;
663 static inline uint16_t
664 load_uint16_be(const void *cvp)
666 const unsigned char *p = (const unsigned char *)cvp;
668 return (p[1] | (p[0] << 8));
672 store_uint32_be(uint32_t val, void *vp)
674 unsigned char *p = (unsigned char *)vp;
676 p[0] = (val >> 24) & 0xff;
677 p[1] = (val >> 16) & 0xff;
678 p[2] = (val >> 8) & 0xff;
679 p[3] = (val ) & 0xff;
682 static inline uint32_t
683 load_uint32_be(const void *cvp)
685 const unsigned char *p = (const unsigned char *)cvp;
687 return (p[3] | (p[2] << 8)
688 | ((uint32_t) p[1] << 16)
689 | ((uint32_t) p[0] << 24));
693 store_uint64_be(uint64_t val, void *vp)
695 unsigned char *p = (unsigned char *)vp;
697 p[0] = (unsigned char)((val >> 56) & 0xff);
698 p[1] = (unsigned char)((val >> 48) & 0xff);
699 p[2] = (unsigned char)((val >> 40) & 0xff);
700 p[3] = (unsigned char)((val >> 32) & 0xff);
701 p[4] = (unsigned char)((val >> 24) & 0xff);
702 p[5] = (unsigned char)((val >> 16) & 0xff);
703 p[6] = (unsigned char)((val >> 8) & 0xff);
704 p[7] = (unsigned char)((val ) & 0xff);
707 static inline uint64_t
708 load_uint64_be(const void *cvp)
710 const unsigned char *p = (const unsigned char *)cvp;
712 return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
715 static inline unsigned char *
716 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
718 unsigned char *p = (unsigned char *)vp;
721 store_uint64_be(buffer->length, p);
724 store_uint32_be(buffer->length, p);
728 if (buffer->value != NULL) {
729 memcpy(p, buffer->value, buffer->length);
736 static inline unsigned char *
737 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
740 buffer->value = GSSEAP_MALLOC(length);
741 if (buffer->value == NULL)
743 buffer->length = length;
744 memcpy(buffer->value, cvp, length);
745 return (unsigned char *)cvp + length;
748 static inline unsigned char *
749 store_oid(gss_OID oid, void *vp)
753 if (oid != GSS_C_NO_OID) {
754 buf.length = oid->length;
755 buf.value = oid->elements;
761 return store_buffer(&buf, vp, FALSE);
765 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
767 buffer->value = (void *)data->data;
768 buffer->length = data->length;
772 krbPrincComponentToGssBuffer(krb5_principal krbPrinc,
773 int index, gss_buffer_t buffer)
775 #ifdef HAVE_HEIMDAL_VERSION
776 buffer->value = (void *)KRB_PRINC_NAME(krbPrinc)[index];
777 buffer->length = strlen((char *)buffer->value);
779 buffer->value = (void *)krb5_princ_component(NULL, krbPrinc, index)->data;
780 buffer->length = krb5_princ_component(NULL, krbPrinc, index)->length;
781 #endif /* HAVE_HEIMDAL_VERSION */
785 krbPrincRealmToGssBuffer(krb5_principal krbPrinc, gss_buffer_t buffer)
787 #ifdef HAVE_HEIMDAL_VERSION
788 buffer->value = (void *)KRB_PRINC_REALM(krbPrinc);
789 buffer->length = strlen((char *)buffer->value);
791 krbDataToGssBuffer(KRB_PRINC_REALM(krbPrinc), buffer);
796 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
798 data->data = (char *)buffer->value;
799 data->length = buffer->length;
806 #include "util_attr.h"
807 #ifdef GSSEAP_ENABLE_REAUTH
808 #include "util_reauth.h"
811 #endif /* _UTIL_H_ */