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