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