more work on SAML ification
[mech_eap.orig] / util.h
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 /*
33  * Portions Copyright 2003-2010 Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
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.
40  *
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.
54  *
55  */
56
57 #ifndef _UTIL_H_
58 #define _UTIL_H_ 1
59
60 #include <string.h>
61 #include <errno.h>
62
63 #include <krb5.h>
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 #include "util_saml.h"
70
71 #define KRB_KEY_TYPE(key)       ((key)->enctype)
72 #define KRB_KEY_DATA(key)       ((key)->contents)
73 #define KRB_KEY_LENGTH(key)     ((key)->length)
74 #define KRB_KEY_INIT(key)       do {        \
75         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
76         KRB_KEY_DATA(key) = NULL;           \
77         KRB_KEY_LENGTH(key) = 0;            \
78     } while (0)
79
80 enum gss_eap_token_type {
81     TOK_TYPE_NONE                    = 0x0000,  /* no token */
82     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
83     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
84     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
85     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* draft-ietf-kitten-gss-naming */
86     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
87     TOK_TYPE_EAP_RESP                = 0x0601,  /* draft-howlett-eap-gss */
88     TOK_TYPE_EAP_REQ                 = 0x0602,  /* draft-howlett-eap-gss */
89     TOK_TYPE_GSS_CB                  = 0x0603,  /* draft-howlett-eap-gss */
90 };
91
92 #define EAP_EXPORT_CONTEXT_V1           1
93
94 /* util_buffer.c */
95 OM_uint32
96 makeStringBuffer(OM_uint32 *minor,
97                  const char *string,
98                  gss_buffer_t buffer);
99
100 OM_uint32
101 bufferToString(OM_uint32 *minor,
102                const gss_buffer_t buffer,
103                char **pString);
104
105 OM_uint32
106 duplicateBuffer(OM_uint32 *minor,
107                 const gss_buffer_t src,
108                 gss_buffer_t dst);
109
110 /* util_cksum.c */
111 int
112 gssEapSign(krb5_context context,
113            krb5_cksumtype type,
114            size_t rrc,
115            krb5_keyblock *key,
116            krb5_keyusage sign_usage,
117            gss_iov_buffer_desc *iov,
118            int iov_count);
119
120 int
121 gssEapVerify(krb5_context context,
122              krb5_cksumtype type,
123              size_t rrc,  
124              krb5_keyblock *key,
125              krb5_keyusage sign_usage,
126              gss_iov_buffer_desc *iov,
127              int iov_count,
128              int *valid);
129
130 #if 0
131 OM_uint32
132 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
133                                gss_channel_bindings_t chanBindings,
134                                gss_buffer_t encodedBindings);
135 #endif
136
137 /* util_context.c */
138 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
139 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
140
141 OM_uint32
142 gssEapMakeToken(OM_uint32 *minor,
143                 gss_ctx_id_t ctx,
144                 const gss_buffer_t innerToken,
145                 enum gss_eap_token_type tokenType,
146                 gss_buffer_t outputToken);
147
148 OM_uint32
149 gssEapVerifyToken(OM_uint32 *minor,
150                   gss_ctx_id_t ctx,
151                   const gss_buffer_t inputToken,
152                   enum gss_eap_token_type tokenType,
153                   gss_buffer_t innerInputToken);
154
155 /* util_cred.c */
156 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
157 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
158
159 OM_uint32
160 gssEapAcquireCred(OM_uint32 *minor,
161                   const gss_name_t desiredName,
162                   const gss_buffer_t password,
163                   OM_uint32 timeReq,
164                   const gss_OID_set desiredMechs,
165                   int cred_usage,
166                   gss_cred_id_t *pCred,
167                   gss_OID_set *pActualMechs,
168                   OM_uint32 *timeRec);
169
170 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
171
172 /* util_crypt.c */
173 int
174 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
175               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
176               gss_iov_buffer_desc *iov, int iov_count);
177
178 int
179 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
180               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
181               gss_iov_buffer_desc *iov, int iov_count);
182
183 krb5_cryptotype
184 gssEapMapCryptoFlag(OM_uint32 type);
185
186 gss_iov_buffer_t
187 gssEapLocateIov(gss_iov_buffer_desc *iov,
188                 int iov_count,
189                 OM_uint32 type);
190
191 void
192 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
193                        int iov_count,
194                        size_t *data_length,
195                        size_t *assoc_data_length);
196
197 void
198 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
199
200 int
201 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
202
203 int
204 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
205
206 OM_uint32
207 gssEapDeriveRfc3961Key(OM_uint32 *minor,
208                        const unsigned char *key,
209                        size_t keyLength,
210                        krb5_enctype enctype,
211                        krb5_keyblock *pKey);
212
213 /* util_krb.c */
214 OM_uint32
215 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
216
217 OM_uint32
218 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
219                           krb5_keyblock *key,
220                           krb5_cksumtype *cksumtype);
221
222 #define GSSEAP_KRB_INIT(ctx) do {                   \
223         OM_uint32 tmpMajor;                         \
224         tmpMajor  = gssEapKerberosInit(minor, ctx); \
225         if (GSS_ERROR(tmpMajor)) {                  \
226             return tmpMajor;                        \
227         }                                           \
228     } while (0)
229
230 /* util_mech.c */
231 int
232 gssEapInternalizeOid(const gss_OID oid,
233                      gss_OID *const pInternalizedOid);
234
235 OM_uint32
236 gssEapDefaultMech(OM_uint32 *minor,
237                   gss_OID *oid);
238
239 OM_uint32
240 gssEapIndicateMechs(OM_uint32 *minor,
241                     gss_OID_set *mechs);
242
243 OM_uint32
244 gssEapEnctypeToOid(OM_uint32 *minor,
245                    krb5_enctype enctype,
246                    gss_OID *pOid);
247
248 OM_uint32
249 gssEapOidToEnctype(OM_uint32 *minor,
250                    const gss_OID oid,
251                    krb5_enctype *enctype);
252
253 int
254 gssEapIsMechanismOid(const gss_OID oid);
255
256 int
257 gssEapIsConcreteMechanismOid(const gss_OID oid);
258
259 OM_uint32
260 gssEapValidateMechs(OM_uint32 *minor,
261                    const gss_OID_set mechs);
262
263 /* util_name.c */
264 enum gss_eap_attribute_type {
265     ATTR_TYPE_NONE                  = 0,
266     ATTR_TYPE_SAML_AAA_ASSERTION    = 1,
267     ATTR_TYPE_SAML_ATTR             = 2,
268     ATTR_TYPE_RADIUS_AVP            = 3
269 };
270
271 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
272 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
273 OM_uint32 gssEapExportName(OM_uint32 *minor,
274                            const gss_name_t name,
275                            gss_buffer_t exportedName,
276                            int composite);
277 OM_uint32 gssEapImportName(OM_uint32 *minor,
278                            const gss_buffer_t input_name_buffer,
279                            gss_OID input_name_type,
280                            gss_name_t *output_name);
281
282 enum gss_eap_attribute_type
283 gssEapAttributePrefixToType(const gss_buffer_t prefix);
284 gss_buffer_t
285 gssEapAttributeTypeToPrefix(enum gss_eap_attribute_type type);
286 OM_uint32
287 decomposeAttributeName(OM_uint32 *minor,
288                        const gss_buffer_t attribute,
289                        gss_buffer_t prefix,
290                        gss_buffer_t suffix);
291 OM_uint32
292 composeAttributeName(OM_uint32 *minor,
293                      const gss_buffer_t prefix,
294                      const gss_buffer_t suffix,
295                      gss_buffer_t attribute);
296
297 /* util_oid.c */
298 OM_uint32
299 composeOid(OM_uint32 *minor_status,
300            const char *prefix,
301            size_t prefix_len,
302            int suffix,  
303            gss_OID_desc *oid);
304
305 OM_uint32
306 decomposeOid(OM_uint32 *minor_status,
307              const char *prefix,
308              size_t prefix_len,
309              gss_OID_desc *oid,
310              int *suffix) ;
311
312 OM_uint32
313 duplicateOid(OM_uint32 *minor_status,
314              const gss_OID_desc * const oid,
315              gss_OID *new_oid);
316
317 OM_uint32
318 duplicateOidSet(OM_uint32 *minor,
319                 const gss_OID_set src,
320                 gss_OID_set *dst);
321
322 static inline int
323 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
324 {
325     if (o1 == GSS_C_NO_OID)
326         return (o2 == GSS_C_NO_OID);
327     else if (o2 == GSS_C_NO_OID)
328         return (o1 == GSS_C_NO_OID);
329     else
330         return (o1->length == o2->length &&
331                 memcmp(o1->elements, o2->elements, o1->length) == 0);
332 }
333
334 /* util_ordering.c */
335 int
336 sequenceInternalize(void **vqueue, unsigned char **buf, size_t *lenremain);
337
338 int
339 sequenceExternalize(void *vqueue, unsigned char **buf, size_t *lenremain);
340
341 size_t
342 sequenceSize(void *vqueue);
343
344 void
345 sequenceFree(void **vqueue);
346
347 int
348 sequenceCheck(void **vqueue, uint64_t seqnum);
349
350 int
351 sequenceInit(void **vqueue, uint64_t seqnum,
352              int do_replay, int do_sequence, int wide_nums);
353
354 /* util_token.c */
355 size_t
356 tokenSize(const gss_OID_desc *mech, size_t body_size);
357
358 void
359 makeTokenHeader(const gss_OID_desc *mech,
360                 size_t body_size,
361                 unsigned char **buf,
362                 enum gss_eap_token_type tok_type);
363
364 OM_uint32
365 verifyTokenHeader(OM_uint32 *minor,
366                   gss_OID mech,
367                   size_t *body_size,
368                   unsigned char **buf_in,
369                   size_t toksize_in,
370                   enum gss_eap_token_type tok_type);
371
372 /* Helper macros */
373 #define GSSEAP_CALLOC(count, size)      (calloc((count), (size)))
374 #define GSSEAP_FREE(ptr)                (free((ptr)))
375 #define GSSEAP_MALLOC(size)             (malloc((size)))
376 #define GSSEAP_REALLOC(ptr, size)       (realloc((ptr), (size)))
377
378 #define GSSEAP_NOT_IMPLEMENTED          do {            \
379         assert(0 && "not implemented");                 \
380         *minor = ENOSYS;                                \
381         return GSS_S_FAILURE;                           \
382     } while (0)
383
384 #include <pthread.h>
385
386 #define GSSEAP_MUTEX                    pthread_mutex_t
387 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
388
389 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
390 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
391 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
392 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
393
394 #define GSSEAP_THREAD_KEY               pthread_key_t
395 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
396 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
397 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
398
399 #define GSSEAP_THREAD_ONCE              pthread_once_t
400 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
401 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
402
403 /* Helper functions */
404 static inline void
405 store_uint16_be(uint16_t val, void *vp)
406 {
407     unsigned char *p = (unsigned char *)vp;
408
409     p[0] = (val >>  8) & 0xff;
410     p[1] = (val      ) & 0xff;
411 }
412
413 static inline uint16_t
414 load_uint16_be(const void *cvp)
415 {
416     const unsigned char *p = (const unsigned char *)cvp;
417
418     return (p[1] | (p[0] << 8));
419 }
420
421 static inline void
422 store_uint32_be(uint32_t val, void *vp)
423 {
424     unsigned char *p = (unsigned char *)vp;
425
426     p[0] = (val >> 24) & 0xff;
427     p[1] = (val >> 16) & 0xff;
428     p[2] = (val >>  8) & 0xff;
429     p[3] = (val      ) & 0xff;
430 }
431
432 static inline uint32_t
433 load_uint32_be(const void *cvp)
434 {
435     const unsigned char *p = (const unsigned char *)cvp;
436
437     return (p[3] | (p[2] << 8)
438             | ((uint32_t) p[1] << 16)
439             | ((uint32_t) p[0] << 24));
440 }
441
442 static inline void
443 store_uint64_be(uint64_t val, void *vp)
444 {
445     unsigned char *p = (unsigned char *)vp;
446
447     p[0] = (unsigned char)((val >> 56) & 0xff);
448     p[1] = (unsigned char)((val >> 48) & 0xff);
449     p[2] = (unsigned char)((val >> 40) & 0xff);
450     p[3] = (unsigned char)((val >> 32) & 0xff);
451     p[4] = (unsigned char)((val >> 24) & 0xff);
452     p[5] = (unsigned char)((val >> 16) & 0xff);
453     p[6] = (unsigned char)((val >>  8) & 0xff);
454     p[7] = (unsigned char)((val      ) & 0xff);
455 }
456
457 static inline uint64_t
458 load_uint64_be(const void *cvp)
459 {
460     const unsigned char *p = (const unsigned char *)cvp;
461
462     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
463 }
464
465 static inline unsigned char *
466 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
467 {
468     unsigned char *p = (unsigned char *)vp;
469
470     if (wide_nums) {
471         store_uint64_be(buffer->length, p);
472         p += 8;
473     } else {
474         store_uint32_be(buffer->length, p);
475         p += 4;
476     }
477
478     if (buffer->value != NULL) {
479         memcpy(p, buffer->value, buffer->length);
480         p += buffer->length;
481     }
482
483     return p;
484 }
485
486 static inline unsigned char *
487 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
488 {
489     buffer->length = 0;
490     buffer->value = GSSEAP_MALLOC(length);
491     if (buffer->value == NULL)
492         return NULL;
493     buffer->length = length;
494     memcpy(buffer->value, cvp, length);
495     return (unsigned char *)cvp + length;
496 }
497
498 static inline unsigned char *
499 store_oid(gss_OID oid, void *vp)
500 {
501     gss_buffer_desc buf;
502
503     if (oid != GSS_C_NO_OID) {
504         buf.length = oid->length;
505         buf.value = oid->elements;
506     } else {
507         buf.length = 0;
508         buf.value = NULL;
509     }
510
511     return store_buffer(&buf, vp, FALSE);
512 }
513
514 #ifdef __cplusplus
515 }
516 #endif
517
518 #endif /* _UTIL_H_ */