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