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