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