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.
62 #include "util_saml.h"
63 #include "util_radius.h"
65 #define KRB_KEYTYPE(key) ((key)->enctype)
68 gssEapSign(krb5_context context,
72 krb5_keyusage sign_usage,
73 gss_iov_buffer_desc *iov,
77 gssEapVerify(krb5_context context,
81 krb5_keyusage sign_usage,
82 gss_iov_buffer_desc *iov,
87 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
88 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
91 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
92 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
95 gssEapAcquireCred(OM_uint32 *minor,
96 const gss_name_t desiredName,
97 const gss_buffer_t password,
99 const gss_OID_set desiredMechs,
101 gss_cred_id_t *pCred,
102 gss_OID_set *pActualMechs,
107 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
108 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
109 gss_iov_buffer_desc *iov, int iov_count);
112 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
113 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
114 gss_iov_buffer_desc *iov, int iov_count);
117 gssEapMapCryptoFlag(OM_uint32 type);
120 gssEapLocateIov(gss_iov_buffer_desc *iov,
125 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
128 size_t *assoc_data_length);
131 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
134 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
137 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
140 gssEapDeriveRFC3961Key(OM_uint32 *minor,
142 krb5_enctype enctype,
143 krb5_keyblock *pKey);
147 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
149 #define GSSEAP_KRB_INIT(ctx) do { \
150 OM_uint32 tmpMajor; \
151 tmpMajor = gssEapKerberosInit(minor, ctx); \
152 if (GSS_ERROR(tmpMajor)) { \
159 gssEapInternalizeOid(const gss_OID oid,
160 gss_OID *const pInternalizedOid);
163 gssEapDefaultMech(OM_uint32 *minor,
167 gssEapIndicateMechs(OM_uint32 *minor,
171 gssEapEnctypeToOid(OM_uint32 *minor,
172 krb5_enctype enctype,
176 gssEapOidToEnctype(OM_uint32 *minor,
178 krb5_enctype *enctype);
181 gssEapIsMechanismOid(const gss_OID oid);
184 gssEapValidateMechs(OM_uint32 *minor,
185 const gss_OID_set mechs);
188 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
189 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
190 OM_uint32 gssEapExportName(OM_uint32 *minor,
191 const gss_name_t name,
192 gss_buffer_t exportedName,
194 OM_uint32 gssEapImportName(OM_uint32 *minor,
195 const gss_buffer_t input_name_buffer,
196 gss_OID input_name_type,
197 gss_name_t *output_name);
201 composeOid(OM_uint32 *minor_status,
208 decomposeOid(OM_uint32 *minor_status,
215 duplicateOidSet(OM_uint32 *minor,
216 const gss_OID_set src,
220 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
222 if (o1 == GSS_C_NO_OID)
223 return (o2 == GSS_C_NO_OID);
224 else if (o2 == GSS_C_NO_OID)
225 return (o1 == GSS_C_NO_OID);
227 return (o1->length == o2->length &&
228 memcmp(o1->elements, o2->elements, o1->length) == 0);
231 /* util_ordering.c */
233 sequenceInternalize(void **vqueue, unsigned char **buf, size_t *lenremain);
236 sequenceExternalize(void *vqueue, unsigned char **buf, size_t *lenremain);
239 sequenceSize(void *vqueue, size_t *sizep);
242 sequenceFree(void **vqueue);
245 sequenceCheck(void **vqueue, uint64_t seqnum);
248 sequenceInit(void **vqueue, uint64_t seqnum,
249 int do_replay, int do_sequence, int wide_nums);
252 enum gss_eap_token_type {
253 TOK_TYPE_NONE = 0x0000,
254 TOK_TYPE_EAP_RESP = 0x0601,
255 TOK_TYPE_EAP_REQ = 0x0602,
256 TOK_TYPE_GSS_CHANNEL_BINDINGS = 0x0603,
257 TOK_TYPE_MIC = 0x0404,
258 TOK_TYPE_WRAP = 0x0504,
259 TOK_TYPE_EXPORT_NAME = 0x0401,
260 TOK_TYPE_EXPORT_NAME_COMPOSITE = 0x0402,
261 TOK_TYPE_DELETE_CONTEXT = 0x0405,
265 tokenSize(const gss_OID_desc *mech, size_t body_size);
268 makeTokenHeader(const gss_OID_desc *mech,
271 enum gss_eap_token_type tok_type);
274 verifyTokenHeader(const gss_OID_desc * mech,
276 unsigned char **buf_in,
278 enum gss_eap_token_type tok_type);
281 #define GSSEAP_CALLOC(count, size) (calloc((count), (size)))
282 #define GSSEAP_FREE(ptr) (free((ptr)))
283 #define GSSEAP_MALLOC(size) (malloc((size)))
284 #define GSSEAP_REALLOC(ptr, size) (realloc((ptr), (size)))
286 #define GSSEAP_NOT_IMPLEMENTED do { \
287 assert(0 && "not implemented"); \
289 return GSS_S_FAILURE; \
294 #define GSSEAP_MUTEX pthread_mutex_t
295 #define GSSEAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
297 #define GSSEAP_MUTEX_INIT(m) pthread_mutex_init((m), NULL)
298 #define GSSEAP_MUTEX_DESTROY(m) pthread_mutex_destroy((m))
299 #define GSSEAP_MUTEX_LOCK(m) pthread_mutex_lock((m))
300 #define GSSEAP_MUTEX_UNLOCK(m) pthread_mutex_unlock((m))
302 #define GSSEAP_THREAD_KEY pthread_key_t
303 #define GSSEAP_KEY_CREATE(k, d) pthread_key_create((k), (d))
304 #define GSSEAP_GETSPECIFIC(k) pthread_getspecific((k))
305 #define GSSEAP_SETSPECIFIC(k, d) pthread_setspecific((k), (d))
307 #define GSSEAP_THREAD_ONCE pthread_once_t
308 #define GSSEAP_ONCE(o, i) pthread_once((o), (i))
309 #define GSSEAP_ONCE_INITIALIZER PTHREAD_ONCE_INIT
311 /* Helper functions */
313 store_uint16_be(uint16_t val, void *vp)
315 unsigned char *p = (unsigned char *)vp;
317 p[0] = (val >> 8) & 0xff;
318 p[1] = (val ) & 0xff;
321 static inline uint16_t
322 load_uint16_be(const void *cvp)
324 const unsigned char *p = (const unsigned char *)cvp;
326 return (p[1] | (p[0] << 8));
330 store_uint32_be(uint32_t val, void *vp)
332 unsigned char *p = (unsigned char *)vp;
334 p[0] = (val >> 24) & 0xff;
335 p[1] = (val >> 16) & 0xff;
336 p[2] = (val >> 8) & 0xff;
337 p[3] = (val ) & 0xff;
340 static inline uint32_t
341 load_uint32_be(const void *cvp)
343 const unsigned char *p = (const unsigned char *)cvp;
345 return (p[3] | (p[2] << 8)
346 | ((uint32_t) p[1] << 16)
347 | ((uint32_t) p[0] << 24));
351 store_uint64_be(uint64_t val, void *vp)
353 unsigned char *p = (unsigned char *)vp;
355 p[0] = (unsigned char)((val >> 56) & 0xff);
356 p[1] = (unsigned char)((val >> 48) & 0xff);
357 p[2] = (unsigned char)((val >> 40) & 0xff);
358 p[3] = (unsigned char)((val >> 32) & 0xff);
359 p[4] = (unsigned char)((val >> 24) & 0xff);
360 p[5] = (unsigned char)((val >> 16) & 0xff);
361 p[6] = (unsigned char)((val >> 8) & 0xff);
362 p[7] = (unsigned char)((val ) & 0xff);
365 static inline uint64_t
366 load_uint64_be(const void *cvp)
368 const unsigned char *p = (const unsigned char *)cvp;
370 return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
374 makeStringBuffer(OM_uint32 *minor,
378 size_t len = strlen(string);
380 buffer->value = GSSEAP_MALLOC(len + 1);
381 if (buffer->value == NULL) {
383 return GSS_S_FAILURE;
385 memcpy(buffer->value, string, len + 1);
386 buffer->length = len;
389 return GSS_S_COMPLETE;
393 bufferToString(OM_uint32 *minor,
394 const gss_buffer_t buffer,
399 s = GSSEAP_MALLOC(buffer->length + 1);
402 return GSS_S_FAILURE;
404 memcpy(s, buffer->value, buffer->length);
405 s[buffer->length] = '\0';
410 return GSS_S_COMPLETE;
414 duplicateBuffer(OM_uint32 *minor,
415 const gss_buffer_t src,
421 if (src == GSS_C_NO_BUFFER)
422 return GSS_S_COMPLETE;
424 dst->value = GSSEAP_MALLOC(src->length + 1);
425 if (dst->value == NULL) {
427 return GSS_S_FAILURE;
430 dst->length = src->length;
431 memcpy(dst->value, src->value, dst->length);
433 ((unsigned char *)dst->value)[dst->length] = '\0';
436 return GSS_S_COMPLETE;
439 #endif /* _UTIL_H_ */