2 * Copyright (c) 2010, 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,
118 krb5_keyusage sign_usage,
119 gss_iov_buffer_desc *iov,
123 gssEapVerify(krb5_context context,
127 krb5_keyusage sign_usage,
128 gss_iov_buffer_desc *iov,
134 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
135 gss_channel_bindings_t chanBindings,
136 gss_buffer_t encodedBindings);
140 #define EAP_EXPORT_CONTEXT_V1 1
142 enum gss_eap_token_type {
143 TOK_TYPE_NONE = 0x0000, /* no token */
144 TOK_TYPE_MIC = 0x0404, /* RFC 4121 MIC token */
145 TOK_TYPE_WRAP = 0x0504, /* RFC 4121 wrap token */
146 TOK_TYPE_EXPORT_NAME = 0x0401, /* RFC 2743 exported name */
147 TOK_TYPE_EXPORT_NAME_COMPOSITE = 0x0402, /* exported composite name */
148 TOK_TYPE_DELETE_CONTEXT = 0x0405, /* RFC 2743 delete context */
149 TOK_TYPE_EAP_RESP = 0x0601, /* EAP response */
150 TOK_TYPE_EAP_REQ = 0x0602, /* EAP request */
151 TOK_TYPE_EXT_REQ = 0x0603, /* GSS EAP extensions request */
152 TOK_TYPE_EXT_RESP = 0x0604, /* GSS EAP extensions response */
153 TOK_TYPE_GSS_REAUTH = 0x0605, /* GSS EAP fast reauthentication token */
154 TOK_TYPE_CONTEXT_ERR = 0x0606, /* context error */
157 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
158 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
161 gssEapMakeToken(OM_uint32 *minor,
163 const gss_buffer_t innerToken,
164 enum gss_eap_token_type tokenType,
165 gss_buffer_t outputToken);
168 gssEapVerifyToken(OM_uint32 *minor,
170 const gss_buffer_t inputToken,
171 enum gss_eap_token_type *tokenType,
172 gss_buffer_t innerInputToken);
175 gssEapContextTime(OM_uint32 *minor,
176 gss_ctx_id_t context_handle,
177 OM_uint32 *time_rec);
180 gssEapDisplayName(OM_uint32 *minor,
182 gss_buffer_t output_name_buffer,
183 gss_OID *output_name_type);
186 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
187 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
190 gssEapAcquireCred(OM_uint32 *minor,
191 const gss_name_t desiredName,
192 const gss_buffer_t password,
194 const gss_OID_set desiredMechs,
196 gss_cred_id_t *pCred,
197 gss_OID_set *pActualMechs,
200 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
204 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
205 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
206 gss_iov_buffer_desc *iov, int iov_count);
209 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
210 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
211 gss_iov_buffer_desc *iov, int iov_count);
214 gssEapMapCryptoFlag(OM_uint32 type);
217 gssEapLocateIov(gss_iov_buffer_desc *iov,
222 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
225 size_t *assoc_data_length);
228 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
231 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
234 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
237 gssEapDeriveRfc3961Key(OM_uint32 *minor,
238 const unsigned char *key,
240 krb5_enctype enctype,
241 krb5_keyblock *pKey);
244 #define EXT_FLAG_CRITICAL 0x80000000 /* critical, wire flag */
245 #define EXT_FLAG_VERIFIED 0x40000000 /* verified, API flag */
247 #define EXT_TYPE_GSS_CHANNEL_BINDINGS 0x00000000
248 #define EXT_TYPE_REAUTH_CREDS 0x00000001
249 #define EXT_TYPE_MASK (~(EXT_FLAG_CRITICAL | EXT_FLAG_VERIFIED))
251 struct gss_eap_extension_provider {
253 int critical; /* client */
254 int required; /* server */
255 OM_uint32 (*make)(OM_uint32 *,
258 gss_channel_bindings_t,
260 OM_uint32 (*verify)(OM_uint32 *,
263 gss_channel_bindings_t,
268 gssEapMakeExtensions(OM_uint32 *minor,
271 gss_channel_bindings_t chanBindings,
272 gss_buffer_t buffer);
275 gssEapVerifyExtensions(OM_uint32 *minor,
278 gss_channel_bindings_t chanBindings,
279 const gss_buffer_t buffer);
282 #define KRB_KEY_TYPE(key) ((key)->enctype)
283 #define KRB_KEY_DATA(key) ((key)->contents)
284 #define KRB_KEY_LENGTH(key) ((key)->length)
285 #define KRB_KEY_INIT(key) do { \
286 KRB_KEY_TYPE(key) = ENCTYPE_NULL; \
287 KRB_KEY_DATA(key) = NULL; \
288 KRB_KEY_LENGTH(key) = 0; \
291 #define GSSEAP_KRB_INIT(ctx) do { \
292 OM_uint32 tmpMajor; \
294 tmpMajor = gssEapKerberosInit(minor, ctx); \
295 if (GSS_ERROR(tmpMajor)) { \
301 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
304 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
306 krb5_cksumtype *cksumtype);
310 gssEapExportLucidSecContext(OM_uint32 *minor,
312 const gss_OID desiredObject,
313 gss_buffer_set_t *data_set);
316 extern gss_OID GSS_EAP_MECHANISM;
319 gssEapInternalizeOid(const gss_OID oid,
320 gss_OID *const pInternalizedOid);
323 gssEapDefaultMech(OM_uint32 *minor,
327 gssEapIndicateMechs(OM_uint32 *minor,
331 gssEapEnctypeToOid(OM_uint32 *minor,
332 krb5_enctype enctype,
336 gssEapOidToEnctype(OM_uint32 *minor,
338 krb5_enctype *enctype);
341 gssEapIsMechanismOid(const gss_OID oid);
344 gssEapIsConcreteMechanismOid(const gss_OID oid);
347 gssEapValidateMechs(OM_uint32 *minor,
348 const gss_OID_set mechs);
351 gssEapOidToSaslName(const gss_OID oid);
354 gssEapSaslNameToOid(const gss_buffer_t name);
357 #define EXPORT_NAME_FLAG_OID 0x1
358 #define EXPORT_NAME_FLAG_COMPOSITE 0x2
360 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
361 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
362 OM_uint32 gssEapExportName(OM_uint32 *minor,
363 const gss_name_t name,
364 gss_buffer_t exportedName);
365 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
366 const gss_name_t name,
367 gss_buffer_t exportedName,
369 OM_uint32 gssEapImportName(OM_uint32 *minor,
370 const gss_buffer_t input_name_buffer,
371 gss_OID input_name_type,
372 gss_name_t *output_name);
373 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
374 const gss_buffer_t input_name_buffer,
375 gss_name_t *output_name,
378 gssEapDuplicateName(OM_uint32 *minor,
379 const gss_name_t input_name,
380 gss_name_t *dest_name);
384 composeOid(OM_uint32 *minor_status,
391 decomposeOid(OM_uint32 *minor_status,
398 duplicateOid(OM_uint32 *minor_status,
399 const gss_OID_desc * const oid,
403 duplicateOidSet(OM_uint32 *minor,
404 const gss_OID_set src,
408 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
410 if (o1 == GSS_C_NO_OID)
411 return (o2 == GSS_C_NO_OID);
412 else if (o2 == GSS_C_NO_OID)
413 return (o1 == GSS_C_NO_OID);
415 return (o1->length == o2->length &&
416 memcmp(o1->elements, o2->elements, o1->length) == 0);
419 /* util_ordering.c */
421 sequenceInternalize(OM_uint32 *minor,
427 sequenceExternalize(OM_uint32 *minor,
433 sequenceSize(void *vqueue);
436 sequenceFree(OM_uint32 *minor, void **vqueue);
439 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
442 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
443 int do_replay, int do_sequence, int wide_nums);
447 tokenSize(const gss_OID_desc *mech, size_t body_size);
450 makeTokenHeader(const gss_OID_desc *mech,
453 enum gss_eap_token_type tok_type);
456 verifyTokenHeader(OM_uint32 *minor,
459 unsigned char **buf_in,
461 enum gss_eap_token_type *ret_tok_type);
465 #define GSSEAP_CALLOC calloc
466 #define GSSEAP_MALLOC malloc
467 #define GSSEAP_FREE free
468 #define GSSEAP_REALLOC realloc
470 #define GSSEAP_NOT_IMPLEMENTED do { \
471 assert(0 && "not implemented"); \
473 return GSS_S_FAILURE; \
478 #define GSSEAP_MUTEX pthread_mutex_t
479 #define GSSEAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
481 #define GSSEAP_MUTEX_INIT(m) pthread_mutex_init((m), NULL)
482 #define GSSEAP_MUTEX_DESTROY(m) pthread_mutex_destroy((m))
483 #define GSSEAP_MUTEX_LOCK(m) pthread_mutex_lock((m))
484 #define GSSEAP_MUTEX_UNLOCK(m) pthread_mutex_unlock((m))
486 #define GSSEAP_THREAD_KEY pthread_key_t
487 #define GSSEAP_KEY_CREATE(k, d) pthread_key_create((k), (d))
488 #define GSSEAP_GETSPECIFIC(k) pthread_getspecific((k))
489 #define GSSEAP_SETSPECIFIC(k, d) pthread_setspecific((k), (d))
491 #define GSSEAP_THREAD_ONCE pthread_once_t
492 #define GSSEAP_ONCE(o, i) pthread_once((o), (i))
493 #define GSSEAP_ONCE_INITIALIZER PTHREAD_ONCE_INIT
495 /* Helper functions */
497 store_uint16_be(uint16_t val, void *vp)
499 unsigned char *p = (unsigned char *)vp;
501 p[0] = (val >> 8) & 0xff;
502 p[1] = (val ) & 0xff;
505 static inline uint16_t
506 load_uint16_be(const void *cvp)
508 const unsigned char *p = (const unsigned char *)cvp;
510 return (p[1] | (p[0] << 8));
514 store_uint32_be(uint32_t val, void *vp)
516 unsigned char *p = (unsigned char *)vp;
518 p[0] = (val >> 24) & 0xff;
519 p[1] = (val >> 16) & 0xff;
520 p[2] = (val >> 8) & 0xff;
521 p[3] = (val ) & 0xff;
524 static inline uint32_t
525 load_uint32_be(const void *cvp)
527 const unsigned char *p = (const unsigned char *)cvp;
529 return (p[3] | (p[2] << 8)
530 | ((uint32_t) p[1] << 16)
531 | ((uint32_t) p[0] << 24));
535 store_uint64_be(uint64_t val, void *vp)
537 unsigned char *p = (unsigned char *)vp;
539 p[0] = (unsigned char)((val >> 56) & 0xff);
540 p[1] = (unsigned char)((val >> 48) & 0xff);
541 p[2] = (unsigned char)((val >> 40) & 0xff);
542 p[3] = (unsigned char)((val >> 32) & 0xff);
543 p[4] = (unsigned char)((val >> 24) & 0xff);
544 p[5] = (unsigned char)((val >> 16) & 0xff);
545 p[6] = (unsigned char)((val >> 8) & 0xff);
546 p[7] = (unsigned char)((val ) & 0xff);
549 static inline uint64_t
550 load_uint64_be(const void *cvp)
552 const unsigned char *p = (const unsigned char *)cvp;
554 return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
557 static inline unsigned char *
558 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
560 unsigned char *p = (unsigned char *)vp;
563 store_uint64_be(buffer->length, p);
566 store_uint32_be(buffer->length, p);
570 if (buffer->value != NULL) {
571 memcpy(p, buffer->value, buffer->length);
578 static inline unsigned char *
579 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
582 buffer->value = GSSEAP_MALLOC(length);
583 if (buffer->value == NULL)
585 buffer->length = length;
586 memcpy(buffer->value, cvp, length);
587 return (unsigned char *)cvp + length;
590 static inline unsigned char *
591 store_oid(gss_OID oid, void *vp)
595 if (oid != GSS_C_NO_OID) {
596 buf.length = oid->length;
597 buf.value = oid->elements;
603 return store_buffer(&buf, vp, FALSE);
607 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
609 buffer->value = (void *)data->data;
610 buffer->length = data->length;
614 gssBufferToKrbData(gss_buffer_t buffer, krb5_data *data)
616 data->data = (char *)buffer->value;
617 data->length = buffer->length;
624 #include "util_attr.h"
625 #ifdef GSSEAP_ENABLE_REAUTH
626 #include "util_reauth.h"
629 #endif /* _UTIL_H_ */