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_KEY_TYPE(key) ((key)->enctype)
66 #define KRB_KEY_DATA(key) ((key)->contents)
67 #define KRB_KEY_LENGTH(key) ((key)->length)
68 #define KRB_KEY_INIT(key) do { \
69 KRB_KEY_TYPE(key) = ENCTYPE_NULL; \
70 KRB_KEY_DATA(key) = NULL; \
71 KRB_KEY_LENGTH(key) = 0; \
74 enum gss_eap_token_type {
75 TOK_TYPE_NONE = 0x0000, /* no token */
76 TOK_TYPE_MIC = 0x0404, /* RFC 4121 MIC token */
77 TOK_TYPE_WRAP = 0x0504, /* RFC 4121 wrap token */
78 TOK_TYPE_EXPORT_NAME = 0x0401, /* RFC 2743 exported name */
79 TOK_TYPE_EXPORT_NAME_COMPOSITE = 0x0402, /* draft-ietf-kitten-gss-naming */
80 TOK_TYPE_DELETE_CONTEXT = 0x0405, /* RFC 2743 delete context */
81 TOK_TYPE_EAP_RESP = 0x0601, /* draft-howlett-eap-gss */
82 TOK_TYPE_EAP_REQ = 0x0602, /* draft-howlett-eap-gss */
83 TOK_TYPE_GSS_CB = 0x0603, /* draft-howlett-eap-gss */
86 #define EAP_EXPORT_CONTEXT_V1 1
90 makeStringBuffer(OM_uint32 *minor,
95 bufferToString(OM_uint32 *minor,
96 const gss_buffer_t buffer,
100 duplicateBuffer(OM_uint32 *minor,
101 const gss_buffer_t src,
106 gssEapSign(krb5_context context,
110 krb5_keyusage sign_usage,
111 gss_iov_buffer_desc *iov,
115 gssEapVerify(krb5_context context,
119 krb5_keyusage sign_usage,
120 gss_iov_buffer_desc *iov,
126 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
127 gss_channel_bindings_t chanBindings,
128 gss_buffer_t encodedBindings);
132 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
133 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
136 gssEapMakeToken(OM_uint32 *minor,
138 const gss_buffer_t innerToken,
139 enum gss_eap_token_type tokenType,
140 gss_buffer_t outputToken);
143 gssEapVerifyToken(OM_uint32 *minor,
145 const gss_buffer_t inputToken,
146 enum gss_eap_token_type tokenType,
147 gss_buffer_t innerInputToken);
150 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
151 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
154 gssEapAcquireCred(OM_uint32 *minor,
155 const gss_name_t desiredName,
156 const gss_buffer_t password,
158 const gss_OID_set desiredMechs,
160 gss_cred_id_t *pCred,
161 gss_OID_set *pActualMechs,
164 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
168 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
169 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
170 gss_iov_buffer_desc *iov, int iov_count);
173 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
174 size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
175 gss_iov_buffer_desc *iov, int iov_count);
178 gssEapMapCryptoFlag(OM_uint32 type);
181 gssEapLocateIov(gss_iov_buffer_desc *iov,
186 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
189 size_t *assoc_data_length);
192 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
195 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
198 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
201 gssEapDeriveRFC3961Key(OM_uint32 *minor,
202 const unsigned char *key,
204 krb5_enctype enctype,
205 krb5_keyblock *pKey);
209 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
211 #define GSSEAP_KRB_INIT(ctx) do { \
212 OM_uint32 tmpMajor; \
213 tmpMajor = gssEapKerberosInit(minor, ctx); \
214 if (GSS_ERROR(tmpMajor)) { \
221 gssEapInternalizeOid(const gss_OID oid,
222 gss_OID *const pInternalizedOid);
225 gssEapDefaultMech(OM_uint32 *minor,
229 gssEapIndicateMechs(OM_uint32 *minor,
233 gssEapEnctypeToOid(OM_uint32 *minor,
234 krb5_enctype enctype,
238 gssEapOidToEnctype(OM_uint32 *minor,
240 krb5_enctype *enctype);
243 gssEapIsMechanismOid(const gss_OID oid);
246 gssEapIsConcreteMechanismOid(const gss_OID oid);
249 gssEapValidateMechs(OM_uint32 *minor,
250 const gss_OID_set mechs);
253 enum gss_eap_attribute_type {
255 ATTR_TYPE_SAML_AAA_ASSERTION = 1,
256 ATTR_TYPE_SAML_ATTR = 2,
257 ATTR_TYPE_RADIUS_AVP = 3
260 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
261 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
262 OM_uint32 gssEapExportName(OM_uint32 *minor,
263 const gss_name_t name,
264 gss_buffer_t exportedName,
266 OM_uint32 gssEapImportName(OM_uint32 *minor,
267 const gss_buffer_t input_name_buffer,
268 gss_OID input_name_type,
269 gss_name_t *output_name);
271 enum gss_eap_attribute_type
272 gssEapAttributePrefixToType(const gss_buffer_t prefix);
274 gssEapAttributeTypeToPrefix(enum gss_eap_attribute_type type);
276 decomposeAttributeName(OM_uint32 *minor,
277 const gss_buffer_t attribute,
279 gss_buffer_t suffix);
281 composeAttributeName(OM_uint32 *minor,
282 const gss_buffer_t prefix,
283 const gss_buffer_t suffix,
284 gss_buffer_t attribute);
288 composeOid(OM_uint32 *minor_status,
295 decomposeOid(OM_uint32 *minor_status,
302 duplicateOid(OM_uint32 *minor_status,
303 const gss_OID_desc * const oid,
307 duplicateOidSet(OM_uint32 *minor,
308 const gss_OID_set src,
312 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
314 if (o1 == GSS_C_NO_OID)
315 return (o2 == GSS_C_NO_OID);
316 else if (o2 == GSS_C_NO_OID)
317 return (o1 == GSS_C_NO_OID);
319 return (o1->length == o2->length &&
320 memcmp(o1->elements, o2->elements, o1->length) == 0);
323 /* util_ordering.c */
325 sequenceInternalize(void **vqueue, unsigned char **buf, size_t *lenremain);
328 sequenceExternalize(void *vqueue, unsigned char **buf, size_t *lenremain);
331 sequenceSize(void *vqueue);
334 sequenceFree(void **vqueue);
337 sequenceCheck(void **vqueue, uint64_t seqnum);
340 sequenceInit(void **vqueue, uint64_t seqnum,
341 int do_replay, int do_sequence, int wide_nums);
345 tokenSize(const gss_OID_desc *mech, size_t body_size);
348 makeTokenHeader(const gss_OID_desc *mech,
351 enum gss_eap_token_type tok_type);
354 verifyTokenHeader(OM_uint32 *minor,
357 unsigned char **buf_in,
359 enum gss_eap_token_type tok_type);
362 #define GSSEAP_CALLOC(count, size) (calloc((count), (size)))
363 #define GSSEAP_FREE(ptr) (free((ptr)))
364 #define GSSEAP_MALLOC(size) (malloc((size)))
365 #define GSSEAP_REALLOC(ptr, size) (realloc((ptr), (size)))
367 #define GSSEAP_NOT_IMPLEMENTED do { \
368 assert(0 && "not implemented"); \
370 return GSS_S_FAILURE; \
375 #define GSSEAP_MUTEX pthread_mutex_t
376 #define GSSEAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
378 #define GSSEAP_MUTEX_INIT(m) pthread_mutex_init((m), NULL)
379 #define GSSEAP_MUTEX_DESTROY(m) pthread_mutex_destroy((m))
380 #define GSSEAP_MUTEX_LOCK(m) pthread_mutex_lock((m))
381 #define GSSEAP_MUTEX_UNLOCK(m) pthread_mutex_unlock((m))
383 #define GSSEAP_THREAD_KEY pthread_key_t
384 #define GSSEAP_KEY_CREATE(k, d) pthread_key_create((k), (d))
385 #define GSSEAP_GETSPECIFIC(k) pthread_getspecific((k))
386 #define GSSEAP_SETSPECIFIC(k, d) pthread_setspecific((k), (d))
388 #define GSSEAP_THREAD_ONCE pthread_once_t
389 #define GSSEAP_ONCE(o, i) pthread_once((o), (i))
390 #define GSSEAP_ONCE_INITIALIZER PTHREAD_ONCE_INIT
392 /* Helper functions */
394 store_uint16_be(uint16_t val, void *vp)
396 unsigned char *p = (unsigned char *)vp;
398 p[0] = (val >> 8) & 0xff;
399 p[1] = (val ) & 0xff;
402 static inline uint16_t
403 load_uint16_be(const void *cvp)
405 const unsigned char *p = (const unsigned char *)cvp;
407 return (p[1] | (p[0] << 8));
411 store_uint32_be(uint32_t val, void *vp)
413 unsigned char *p = (unsigned char *)vp;
415 p[0] = (val >> 24) & 0xff;
416 p[1] = (val >> 16) & 0xff;
417 p[2] = (val >> 8) & 0xff;
418 p[3] = (val ) & 0xff;
421 static inline uint32_t
422 load_uint32_be(const void *cvp)
424 const unsigned char *p = (const unsigned char *)cvp;
426 return (p[3] | (p[2] << 8)
427 | ((uint32_t) p[1] << 16)
428 | ((uint32_t) p[0] << 24));
432 store_uint64_be(uint64_t val, void *vp)
434 unsigned char *p = (unsigned char *)vp;
436 p[0] = (unsigned char)((val >> 56) & 0xff);
437 p[1] = (unsigned char)((val >> 48) & 0xff);
438 p[2] = (unsigned char)((val >> 40) & 0xff);
439 p[3] = (unsigned char)((val >> 32) & 0xff);
440 p[4] = (unsigned char)((val >> 24) & 0xff);
441 p[5] = (unsigned char)((val >> 16) & 0xff);
442 p[6] = (unsigned char)((val >> 8) & 0xff);
443 p[7] = (unsigned char)((val ) & 0xff);
446 static inline uint64_t
447 load_uint64_be(const void *cvp)
449 const unsigned char *p = (const unsigned char *)cvp;
451 return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
454 static inline unsigned char *
455 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
457 unsigned char *p = (unsigned char *)vp;
460 store_uint64_be(buffer->length, p);
463 store_uint32_be(buffer->length, p);
467 if (buffer->value != NULL) {
468 memcpy(p, buffer->value, buffer->length);
475 static inline unsigned char *
476 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
479 buffer->value = GSSEAP_MALLOC(length);
480 if (buffer->value == NULL)
482 buffer->length = length;
483 memcpy(buffer->value, cvp, length);
484 return (unsigned char *)cvp + length;
487 static inline unsigned char *
488 store_oid(gss_OID oid, void *vp)
492 if (oid != GSS_C_NO_OID) {
493 buf.length = oid->length;
494 buf.value = oid->elements;
500 return store_buffer(&buf, vp, FALSE);
502 #endif /* _UTIL_H_ */