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.
69 #ifndef MIN /* Usually found in <sys/param.h>. */
70 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
73 #define KRB_KEY_TYPE(key) ((key)->enctype)
74 #define KRB_KEY_DATA(key) ((key)->contents)
75 #define KRB_KEY_LENGTH(key) ((key)->length)
76 #define KRB_KEY_INIT(key) do { \
77 KRB_KEY_TYPE(key) = ENCTYPE_NULL; \
78 KRB_KEY_DATA(key) = NULL; \
79 KRB_KEY_LENGTH(key) = 0; \
82 enum gss_eap_token_type {
83 TOK_TYPE_NONE = 0x0000, /* no token */
84 TOK_TYPE_MIC = 0x0404, /* RFC 4121 MIC token */
85 TOK_TYPE_WRAP = 0x0504, /* RFC 4121 wrap token */
86 TOK_TYPE_EXPORT_NAME = 0x0401, /* RFC 2743 exported name */
87 TOK_TYPE_EXPORT_NAME_COMPOSITE = 0x0402, /* draft-ietf-kitten-gss-naming */
88 TOK_TYPE_DELETE_CONTEXT = 0x0405, /* RFC 2743 delete context */
89 TOK_TYPE_EAP_RESP = 0x0601, /* draft-howlett-eap-gss */
90 TOK_TYPE_EAP_REQ = 0x0602, /* draft-howlett-eap-gss */
91 TOK_TYPE_GSS_CB = 0x0603, /* draft-howlett-eap-gss */
94 #define EAP_EXPORT_CONTEXT_V1 1
98 makeStringBuffer(OM_uint32 *minor,
100 gss_buffer_t buffer);
103 bufferToString(OM_uint32 *minor,
104 const gss_buffer_t buffer,
108 duplicateBuffer(OM_uint32 *minor,
109 const gss_buffer_t src,
113 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
115 return (b1->length == b2->length &&
116 memcmp(b1->value, b2->value, b2->length) == 0);
120 bufferEqualString(const gss_buffer_t b1, const char *s)
124 b2.length = strlen(s);
125 b2.value = (char *)s;
127 return bufferEqual(b1, &b2);
132 gssEapSign(krb5_context context,
136 krb5_keyusage sign_usage,
137 gss_iov_buffer_desc *iov,
141 gssEapVerify(krb5_context context,
145 krb5_keyusage sign_usage,
146 gss_iov_buffer_desc *iov,
152 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
153 gss_channel_bindings_t chanBindings,
154 gss_buffer_t encodedBindings);
158 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
159 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
162 gssEapMakeToken(OM_uint32 *minor,
164 const gss_buffer_t innerToken,
165 enum gss_eap_token_type tokenType,
166 gss_buffer_t outputToken);
169 gssEapVerifyToken(OM_uint32 *minor,
171 const gss_buffer_t inputToken,
172 enum gss_eap_token_type tokenType,
173 gss_buffer_t innerInputToken);
176 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
177 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
180 gssEapAcquireCred(OM_uint32 *minor,
181 const gss_name_t desiredName,
182 const gss_buffer_t password,
184 const gss_OID_set desiredMechs,
186 gss_cred_id_t *pCred,
187 gss_OID_set *pActualMechs,
190 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
194 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
195 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
196 gss_iov_buffer_desc *iov, int iov_count);
199 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
200 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
201 gss_iov_buffer_desc *iov, int iov_count);
204 gssEapMapCryptoFlag(OM_uint32 type);
207 gssEapLocateIov(gss_iov_buffer_desc *iov,
212 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
215 size_t *assoc_data_length);
218 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
221 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
224 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
227 gssEapDeriveRfc3961Key(OM_uint32 *minor,
228 const unsigned char *key,
230 krb5_enctype enctype,
231 krb5_keyblock *pKey);
235 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
238 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
240 krb5_cksumtype *cksumtype);
242 #define GSSEAP_KRB_INIT(ctx) do { \
243 OM_uint32 tmpMajor; \
244 tmpMajor = gssEapKerberosInit(minor, ctx); \
245 if (GSS_ERROR(tmpMajor)) { \
252 gssEapInternalizeOid(const gss_OID oid,
253 gss_OID *const pInternalizedOid);
256 gssEapDefaultMech(OM_uint32 *minor,
260 gssEapIndicateMechs(OM_uint32 *minor,
264 gssEapEnctypeToOid(OM_uint32 *minor,
265 krb5_enctype enctype,
269 gssEapOidToEnctype(OM_uint32 *minor,
271 krb5_enctype *enctype);
274 gssEapIsMechanismOid(const gss_OID oid);
277 gssEapIsConcreteMechanismOid(const gss_OID oid);
280 gssEapValidateMechs(OM_uint32 *minor,
281 const gss_OID_set mechs);
284 #define EXPORT_NAME_FLAG_OID 0x1
285 #define EXPORT_NAME_FLAG_COMPOSITE 0x2
287 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
288 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
289 OM_uint32 gssEapExportName(OM_uint32 *minor,
290 const gss_name_t name,
291 gss_buffer_t exportedName);
292 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
293 const gss_name_t name,
294 gss_buffer_t exportedName,
296 OM_uint32 gssEapImportName(OM_uint32 *minor,
297 const gss_buffer_t input_name_buffer,
298 gss_OID input_name_type,
299 gss_name_t *output_name);
300 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
301 const gss_buffer_t input_name_buffer,
302 gss_name_t *output_name,
307 composeOid(OM_uint32 *minor_status,
314 decomposeOid(OM_uint32 *minor_status,
321 duplicateOid(OM_uint32 *minor_status,
322 const gss_OID_desc * const oid,
326 duplicateOidSet(OM_uint32 *minor,
327 const gss_OID_set src,
331 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
333 if (o1 == GSS_C_NO_OID)
334 return (o2 == GSS_C_NO_OID);
335 else if (o2 == GSS_C_NO_OID)
336 return (o1 == GSS_C_NO_OID);
338 return (o1->length == o2->length &&
339 memcmp(o1->elements, o2->elements, o1->length) == 0);
342 /* util_ordering.c */
344 sequenceInternalize(OM_uint32 *minor,
350 sequenceExternalize(OM_uint32 *minor,
356 sequenceSize(void *vqueue);
359 sequenceFree(OM_uint32 *minor, void **vqueue);
362 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
365 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
366 int do_replay, int do_sequence, int wide_nums);
370 tokenSize(const gss_OID_desc *mech, size_t body_size);
373 makeTokenHeader(const gss_OID_desc *mech,
376 enum gss_eap_token_type tok_type);
379 verifyTokenHeader(OM_uint32 *minor,
382 unsigned char **buf_in,
384 enum gss_eap_token_type tok_type);
387 #define GSSEAP_CALLOC(count, size) (calloc((count), (size)))
388 #define GSSEAP_FREE(ptr) (free((ptr)))
389 #define GSSEAP_MALLOC(size) (malloc((size)))
390 #define GSSEAP_REALLOC(ptr, size) (realloc((ptr), (size)))
392 #define GSSEAP_NOT_IMPLEMENTED do { \
393 assert(0 && "not implemented"); \
395 return GSS_S_FAILURE; \
400 #define GSSEAP_MUTEX pthread_mutex_t
401 #define GSSEAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
403 #define GSSEAP_MUTEX_INIT(m) pthread_mutex_init((m), NULL)
404 #define GSSEAP_MUTEX_DESTROY(m) pthread_mutex_destroy((m))
405 #define GSSEAP_MUTEX_LOCK(m) pthread_mutex_lock((m))
406 #define GSSEAP_MUTEX_UNLOCK(m) pthread_mutex_unlock((m))
408 #define GSSEAP_THREAD_KEY pthread_key_t
409 #define GSSEAP_KEY_CREATE(k, d) pthread_key_create((k), (d))
410 #define GSSEAP_GETSPECIFIC(k) pthread_getspecific((k))
411 #define GSSEAP_SETSPECIFIC(k, d) pthread_setspecific((k), (d))
413 #define GSSEAP_THREAD_ONCE pthread_once_t
414 #define GSSEAP_ONCE(o, i) pthread_once((o), (i))
415 #define GSSEAP_ONCE_INITIALIZER PTHREAD_ONCE_INIT
417 /* Helper functions */
419 store_uint16_be(uint16_t val, void *vp)
421 unsigned char *p = (unsigned char *)vp;
423 p[0] = (val >> 8) & 0xff;
424 p[1] = (val ) & 0xff;
427 static inline uint16_t
428 load_uint16_be(const void *cvp)
430 const unsigned char *p = (const unsigned char *)cvp;
432 return (p[1] | (p[0] << 8));
436 store_uint32_be(uint32_t val, void *vp)
438 unsigned char *p = (unsigned char *)vp;
440 p[0] = (val >> 24) & 0xff;
441 p[1] = (val >> 16) & 0xff;
442 p[2] = (val >> 8) & 0xff;
443 p[3] = (val ) & 0xff;
446 static inline uint32_t
447 load_uint32_be(const void *cvp)
449 const unsigned char *p = (const unsigned char *)cvp;
451 return (p[3] | (p[2] << 8)
452 | ((uint32_t) p[1] << 16)
453 | ((uint32_t) p[0] << 24));
457 store_uint64_be(uint64_t val, void *vp)
459 unsigned char *p = (unsigned char *)vp;
461 p[0] = (unsigned char)((val >> 56) & 0xff);
462 p[1] = (unsigned char)((val >> 48) & 0xff);
463 p[2] = (unsigned char)((val >> 40) & 0xff);
464 p[3] = (unsigned char)((val >> 32) & 0xff);
465 p[4] = (unsigned char)((val >> 24) & 0xff);
466 p[5] = (unsigned char)((val >> 16) & 0xff);
467 p[6] = (unsigned char)((val >> 8) & 0xff);
468 p[7] = (unsigned char)((val ) & 0xff);
471 static inline uint64_t
472 load_uint64_be(const void *cvp)
474 const unsigned char *p = (const unsigned char *)cvp;
476 return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
479 static inline unsigned char *
480 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
482 unsigned char *p = (unsigned char *)vp;
485 store_uint64_be(buffer->length, p);
488 store_uint32_be(buffer->length, p);
492 if (buffer->value != NULL) {
493 memcpy(p, buffer->value, buffer->length);
500 static inline unsigned char *
501 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
504 buffer->value = GSSEAP_MALLOC(length);
505 if (buffer->value == NULL)
507 buffer->length = length;
508 memcpy(buffer->value, cvp, length);
509 return (unsigned char *)cvp + length;
512 static inline unsigned char *
513 store_oid(gss_OID oid, void *vp)
517 if (oid != GSS_C_NO_OID) {
518 buf.length = oid->length;
519 buf.value = oid->elements;
525 return store_buffer(&buf, vp, FALSE);
532 #include "util_attr.h"
534 #endif /* _UTIL_H_ */