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