Cleanup
[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 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 /* util_crypt.c */
95 int
96 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
97               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
98               gss_iov_buffer_desc *iov, int iov_count);
99
100 int
101 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
102               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
103               gss_iov_buffer_desc *iov, int iov_count);
104
105 krb5_cryptotype
106 gssEapMapCryptoFlag(OM_uint32 type);
107
108 gss_iov_buffer_t
109 gssEapLocateIov(gss_iov_buffer_desc *iov,
110                 int iov_count,
111                 OM_uint32 type);
112
113 void
114 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
115                        int iov_count,
116                        size_t *data_length,
117                        size_t *assoc_data_length);
118
119 void
120 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
121
122 int
123 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
124
125 int
126 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
127
128 OM_uint32
129 gssEapDeriveRFC3961Key(OM_uint32 *minor,
130                        gss_buffer_t msk,
131                        krb5_enctype enctype,
132                        krb5_keyblock *pKey);
133
134 /* util_krb.c */
135 OM_uint32
136 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
137
138 #define GSSEAP_KRB_INIT(ctx) do {                   \
139         OM_uint32 tmpMajor;                         \
140         tmpMajor  = gssEapKerberosInit(minor, ctx); \
141         if (GSS_ERROR(tmpMajor)) {                  \
142             return tmpMajor;                        \
143         }                                           \
144     } while (0)
145
146 /* util_mech.c */
147 void
148 gssEapInternalizeOid(const gss_OID oid,
149                      gss_OID *const pInternalizedOid);
150
151 OM_uint32
152 gssEapDefaultMech(OM_uint32 *minor,
153                   gss_OID *oid);
154
155 OM_uint32
156 gssEapIndicateMechs(OM_uint32 *minor,
157                     gss_OID_set *mechs);
158
159 OM_uint32
160 gssEapEnctypeToOid(OM_uint32 *minor,
161                    krb5_enctype enctype,
162                    gss_OID *pOid);
163
164 OM_uint32
165 gssEapOidToEnctype(OM_uint32 *minor,
166                    const gss_OID oid,
167                    krb5_enctype *enctype);
168
169 int
170 gssEapIsMechanismOid(const gss_OID oid);
171
172 /* util_name.c */
173 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
174 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
175 OM_uint32 gssEapExportName(OM_uint32 *minor,
176                            const gss_name_t name,
177                            gss_buffer_t exportedName,
178                            int composite);
179 OM_uint32 gssEapImportName(OM_uint32 *minor,
180                            const gss_buffer_t input_name_buffer,
181                            gss_OID input_name_type,
182                            gss_name_t *output_name);
183
184 /* util_oid.c */
185 OM_uint32
186 composeOid(OM_uint32 *minor_status,
187            const char *prefix,
188            size_t prefix_len,
189            int suffix,  
190            gss_OID_desc *oid);
191
192 OM_uint32
193 decomposeOid(OM_uint32 *minor_status,
194              const char *prefix,
195              size_t prefix_len,
196              gss_OID_desc *oid,
197              int *suffix) ;
198
199 static inline int
200 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
201 {
202     if (o1 == GSS_C_NO_OID)
203         return (o2 == GSS_C_NO_OID);
204     else if (o2 == GSS_C_NO_OID)
205         return (o1 == GSS_C_NO_OID);
206     else
207         return (o1->length == o2->length &&
208                 memcmp(o1->elements, o2->elements, o1->length) == 0);
209 }
210
211 /* util_ordering.c */
212 int
213 sequenceInternalize(void **vqueue, unsigned char **buf, size_t *lenremain);
214
215 int
216 sequenceExternalize(void *vqueue, unsigned char **buf, size_t *lenremain);
217
218 int
219 sequenceSize(void *vqueue, size_t *sizep);
220
221 void
222 sequenceFree(void **vqueue);
223
224 int
225 sequenceCheck(void **vqueue, uint64_t seqnum);
226
227 int
228 sequenceInit(void **vqueue, uint64_t seqnum,
229              int do_replay, int do_sequence, int wide_nums);
230
231 /* util_token.c */
232 enum gss_eap_token_type {
233     TOK_TYPE_NONE                    = 0x0000,
234     TOK_TYPE_EAP_RESP                = 0x0601,
235     TOK_TYPE_EAP_REQ                 = 0x0602,
236     TOK_TYPE_GSS_CB                  = 0x0603,
237     TOK_TYPE_MIC                     = 0x0404,
238     TOK_TYPE_WRAP                    = 0x0504,
239     TOK_TYPE_EXPORT_NAME             = 0x0401,
240     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,
241     TOK_TYPE_DELETE                  = 0x0405,
242 };
243
244 size_t
245 tokenSize(const gss_OID_desc *mech, size_t body_size);
246
247 void
248 makeTokenHeader(const gss_OID_desc *mech,
249                 size_t body_size,
250                 unsigned char **buf,
251                 enum gss_eap_token_type tok_type);
252
253 int
254 verifyTokenHeader(const gss_OID_desc * mech,
255                   size_t *body_size,
256                   unsigned char **buf_in,
257                   size_t toksize_in,
258                   enum gss_eap_token_type tok_type);
259
260 /* Helper macros */
261 #define GSSEAP_CALLOC(count, size)      (calloc((count), (size)))
262 #define GSSEAP_FREE(ptr)                (free((ptr)))
263 #define GSSEAP_MALLOC(size)             (malloc((size)))
264 #define GSSEAP_REALLOC(ptr, size)       (realloc((ptr), (size)))
265
266 #define GSSEAP_NOT_IMPLEMENTED          do {            \
267         assert(0 && "not implemented");                 \
268         *minor = ENOSYS;                                \
269         return GSS_S_FAILURE;                           \
270     } while (0)
271
272 #include <pthread.h>
273
274 #define GSSEAP_MUTEX                    pthread_mutex_t
275 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
276
277 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
278 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
279 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
280 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
281
282 #define GSSEAP_THREAD_KEY               pthread_key_t
283 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
284 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
285 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
286
287 #define GSSEAP_THREAD_ONCE              pthread_once_t
288 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
289 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
290
291 /* Helper functions */
292 static inline void
293 store_uint16_be(uint16_t val, void *vp)
294 {
295     unsigned char *p = (unsigned char *)vp;
296
297     p[0] = (val >>  8) & 0xff;
298     p[1] = (val      ) & 0xff;
299 }
300
301 static inline uint16_t
302 load_uint16_be(const void *cvp)
303 {
304     const unsigned char *p = (const unsigned char *)cvp;
305
306     return (p[1] | (p[0] << 8));
307 }
308
309 static inline void
310 store_uint32_be(uint32_t val, void *vp)
311 {
312     unsigned char *p = (unsigned char *)vp;
313
314     p[0] = (val >> 24) & 0xff;
315     p[1] = (val >> 16) & 0xff;
316     p[2] = (val >>  8) & 0xff;
317     p[3] = (val      ) & 0xff;
318 }
319
320 static inline uint32_t
321 load_uint32_be(const void *cvp)
322 {
323     const unsigned char *p = (const unsigned char *)cvp;
324
325     return (p[3] | (p[2] << 8)
326             | ((uint32_t) p[1] << 16)
327             | ((uint32_t) p[0] << 24));
328 }
329
330 static inline void
331 store_uint64_be(uint64_t val, void *vp)
332 {
333     unsigned char *p = (unsigned char *)vp;
334
335     p[0] = (unsigned char)((val >> 56) & 0xff);
336     p[1] = (unsigned char)((val >> 48) & 0xff);
337     p[2] = (unsigned char)((val >> 40) & 0xff);
338     p[3] = (unsigned char)((val >> 32) & 0xff);
339     p[4] = (unsigned char)((val >> 24) & 0xff);
340     p[5] = (unsigned char)((val >> 16) & 0xff);
341     p[6] = (unsigned char)((val >>  8) & 0xff);
342     p[7] = (unsigned char)((val      ) & 0xff);
343 }
344
345 static inline uint64_t
346 load_uint64_be(const void *cvp)
347 {
348     const unsigned char *p = (const unsigned char *)cvp;
349
350     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
351 }
352
353 static OM_uint32
354 makeStringBuffer(OM_uint32 *minor,
355                  const char *string,
356                  gss_buffer_t buffer)
357 {
358     size_t len = strlen(string);
359
360     buffer->value = GSSEAP_MALLOC(len + 1);
361     if (buffer->value == NULL) {
362         *minor = ENOMEM;
363         return GSS_S_FAILURE;
364     }
365     memcpy(buffer->value, string, len + 1);
366     buffer->length = len;
367
368     *minor = 0;
369     return GSS_S_COMPLETE;
370 }
371
372 static OM_uint32
373 bufferToString(OM_uint32 *minor,
374                const gss_buffer_t buffer,
375                char **pString)
376 {
377     char *s;
378
379     s = GSSEAP_MALLOC(buffer->length + 1);
380     if (s == NULL) {
381         *minor = ENOMEM;
382         return GSS_S_FAILURE;
383     }
384     memcpy(s, buffer->value, buffer->length);
385     s[buffer->length] = '\0';
386
387     *pString = s;
388
389     *minor = 0;
390     return GSS_S_COMPLETE;
391 }
392 #endif /* _UTIL_H_ */