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