74641c786ca5e4013aebe56ba14c22d7180a1e26
[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 <string.h>
61 #include <errno.h>
62
63 #include <krb5.h>
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 #ifndef MIN             /* Usually found in <sys/param.h>. */
70 #define MIN(_a,_b)  ((_a)<(_b)?(_a):(_b))
71 #endif
72
73 #define KRB_KEY_TYPE(key)       ((key)->enctype)
74 #define KRB_KEY_DATA(key)       ((key)->contents)
75 #define KRB_KEY_LENGTH(key)     ((key)->length)
76 #define KRB_KEY_INIT(key)       do {        \
77         KRB_KEY_TYPE(key) = ENCTYPE_NULL;   \
78         KRB_KEY_DATA(key) = NULL;           \
79         KRB_KEY_LENGTH(key) = 0;            \
80     } while (0)
81
82 enum gss_eap_token_type {
83     TOK_TYPE_NONE                    = 0x0000,  /* no token */
84     TOK_TYPE_MIC                     = 0x0404,  /* RFC 4121 MIC token */
85     TOK_TYPE_WRAP                    = 0x0504,  /* RFC 4121 wrap token */
86     TOK_TYPE_EXPORT_NAME             = 0x0401,  /* RFC 2743 exported name */
87     TOK_TYPE_EXPORT_NAME_COMPOSITE   = 0x0402,  /* draft-ietf-kitten-gss-naming */
88     TOK_TYPE_DELETE_CONTEXT          = 0x0405,  /* RFC 2743 delete context */
89     TOK_TYPE_EAP_RESP                = 0x0601,  /* draft-howlett-eap-gss */
90     TOK_TYPE_EAP_REQ                 = 0x0602,  /* draft-howlett-eap-gss */
91     TOK_TYPE_GSS_CB                  = 0x0603,  /* draft-howlett-eap-gss */
92 };
93
94 #define EAP_EXPORT_CONTEXT_V1           1
95
96 /* util_buffer.c */
97 OM_uint32
98 makeStringBuffer(OM_uint32 *minor,
99                  const char *string,
100                  gss_buffer_t buffer);
101
102 OM_uint32
103 bufferToString(OM_uint32 *minor,
104                const gss_buffer_t buffer,
105                char **pString);
106
107 OM_uint32
108 duplicateBuffer(OM_uint32 *minor,
109                 const gss_buffer_t src,
110                 gss_buffer_t dst);
111
112 static inline int
113 bufferEqual(const gss_buffer_t b1, const gss_buffer_t b2)
114 {
115     return (b1->length == b2->length &&
116             memcmp(b1->value, b2->value, b2->length) == 0);
117 }
118
119 static inline int
120 bufferEqualString(const gss_buffer_t b1, const char *s)
121 {
122     gss_buffer_desc b2;
123
124     b2.length = strlen(s);
125     b2.value = (char *)s;
126
127     return bufferEqual(b1, &b2);
128 }
129
130 /* util_cksum.c */
131 int
132 gssEapSign(krb5_context context,
133            krb5_cksumtype type,
134            size_t rrc,
135            krb5_keyblock *key,
136            krb5_keyusage sign_usage,
137            gss_iov_buffer_desc *iov,
138            int iov_count);
139
140 int
141 gssEapVerify(krb5_context context,
142              krb5_cksumtype type,
143              size_t rrc,
144              krb5_keyblock *key,
145              krb5_keyusage sign_usage,
146              gss_iov_buffer_desc *iov,
147              int iov_count,
148              int *valid);
149
150 #if 0
151 OM_uint32
152 gssEapEncodeGssChannelBindings(OM_uint32 *minor,
153                                gss_channel_bindings_t chanBindings,
154                                gss_buffer_t encodedBindings);
155 #endif
156
157 /* util_context.c */
158 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
159 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
160
161 OM_uint32
162 gssEapMakeToken(OM_uint32 *minor,
163                 gss_ctx_id_t ctx,
164                 const gss_buffer_t innerToken,
165                 enum gss_eap_token_type tokenType,
166                 gss_buffer_t outputToken);
167
168 OM_uint32
169 gssEapVerifyToken(OM_uint32 *minor,
170                   gss_ctx_id_t ctx,
171                   const gss_buffer_t inputToken,
172                   enum gss_eap_token_type tokenType,
173                   gss_buffer_t innerInputToken);
174
175 OM_uint32
176 gssEapContextTime(OM_uint32 *minor,
177                   gss_ctx_id_t context_handle,
178                   OM_uint32 *time_rec);
179
180 OM_uint32
181 gssEapDisplayName(OM_uint32 *minor,
182                   gss_name_t name,
183                   gss_buffer_t output_name_buffer,
184                   gss_OID *output_name_type);
185
186 /* util_cred.c */
187 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
188 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
189
190 OM_uint32
191 gssEapAcquireCred(OM_uint32 *minor,
192                   const gss_name_t desiredName,
193                   const gss_buffer_t password,
194                   OM_uint32 timeReq,
195                   const gss_OID_set desiredMechs,
196                   int cred_usage,
197                   gss_cred_id_t *pCred,
198                   gss_OID_set *pActualMechs,
199                   OM_uint32 *timeRec);
200
201 int gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech);
202
203 /* util_crypt.c */
204 int
205 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
206               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
207               gss_iov_buffer_desc *iov, int iov_count);
208
209 int
210 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
211               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
212               gss_iov_buffer_desc *iov, int iov_count);
213
214 krb5_cryptotype
215 gssEapMapCryptoFlag(OM_uint32 type);
216
217 gss_iov_buffer_t
218 gssEapLocateIov(gss_iov_buffer_desc *iov,
219                 int iov_count,
220                 OM_uint32 type);
221
222 void
223 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
224                        int iov_count,
225                        size_t *data_length,
226                        size_t *assoc_data_length);
227
228 void
229 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
230
231 int
232 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
233
234 int
235 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
236
237 OM_uint32
238 gssEapDeriveRfc3961Key(OM_uint32 *minor,
239                        const unsigned char *key,
240                        size_t keyLength,
241                        krb5_enctype enctype,
242                        krb5_keyblock *pKey);
243
244 /* util_krb.c */
245 OM_uint32
246 gssEapKerberosInit(OM_uint32 *minor, krb5_context *context);
247
248 OM_uint32
249 rfc3961ChecksumTypeForKey(OM_uint32 *minor,
250                           krb5_keyblock *key,
251                           krb5_cksumtype *cksumtype);
252
253 #define GSSEAP_KRB_INIT(ctx) do {                   \
254         OM_uint32 tmpMajor;                         \
255         tmpMajor  = gssEapKerberosInit(minor, ctx); \
256         if (GSS_ERROR(tmpMajor)) {                  \
257             return tmpMajor;                        \
258         }                                           \
259     } while (0)
260
261 /* util_mech.c */
262 int
263 gssEapInternalizeOid(const gss_OID oid,
264                      gss_OID *const pInternalizedOid);
265
266 OM_uint32
267 gssEapDefaultMech(OM_uint32 *minor,
268                   gss_OID *oid);
269
270 OM_uint32
271 gssEapIndicateMechs(OM_uint32 *minor,
272                     gss_OID_set *mechs);
273
274 OM_uint32
275 gssEapEnctypeToOid(OM_uint32 *minor,
276                    krb5_enctype enctype,
277                    gss_OID *pOid);
278
279 OM_uint32
280 gssEapOidToEnctype(OM_uint32 *minor,
281                    const gss_OID oid,
282                    krb5_enctype *enctype);
283
284 int
285 gssEapIsMechanismOid(const gss_OID oid);
286
287 int
288 gssEapIsConcreteMechanismOid(const gss_OID oid);
289
290 OM_uint32
291 gssEapValidateMechs(OM_uint32 *minor,
292                    const gss_OID_set mechs);
293
294 /* util_name.c */
295 #define EXPORT_NAME_FLAG_OID        0x1
296 #define EXPORT_NAME_FLAG_COMPOSITE  0x2
297
298 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
299 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
300 OM_uint32 gssEapExportName(OM_uint32 *minor,
301                            const gss_name_t name,
302                            gss_buffer_t exportedName);
303 OM_uint32 gssEapExportNameInternal(OM_uint32 *minor,
304                                    const gss_name_t name,
305                                    gss_buffer_t exportedName,
306                                    unsigned int flags);
307 OM_uint32 gssEapImportName(OM_uint32 *minor,
308                            const gss_buffer_t input_name_buffer,
309                            gss_OID input_name_type,
310                            gss_name_t *output_name);
311 OM_uint32 gssEapImportNameInternal(OM_uint32 *minor,
312                                    const gss_buffer_t input_name_buffer,
313                                    gss_name_t *output_name,
314                                    unsigned int flags);
315 OM_uint32
316 gssEapDuplicateName(OM_uint32 *minor,
317                     const gss_name_t input_name,
318                     gss_name_t *dest_name);
319
320 /* util_oid.c */
321 OM_uint32
322 composeOid(OM_uint32 *minor_status,
323            const char *prefix,
324            size_t prefix_len,
325            int suffix,
326            gss_OID_desc *oid);
327
328 OM_uint32
329 decomposeOid(OM_uint32 *minor_status,
330              const char *prefix,
331              size_t prefix_len,
332              gss_OID_desc *oid,
333              int *suffix) ;
334
335 OM_uint32
336 duplicateOid(OM_uint32 *minor_status,
337              const gss_OID_desc * const oid,
338              gss_OID *new_oid);
339
340 OM_uint32
341 duplicateOidSet(OM_uint32 *minor,
342                 const gss_OID_set src,
343                 gss_OID_set *dst);
344
345 static inline int
346 oidEqual(const gss_OID_desc *o1, const gss_OID_desc *o2)
347 {
348     if (o1 == GSS_C_NO_OID)
349         return (o2 == GSS_C_NO_OID);
350     else if (o2 == GSS_C_NO_OID)
351         return (o1 == GSS_C_NO_OID);
352     else
353         return (o1->length == o2->length &&
354                 memcmp(o1->elements, o2->elements, o1->length) == 0);
355 }
356
357 /* util_ordering.c */
358 OM_uint32
359 sequenceInternalize(OM_uint32 *minor,
360                     void **vqueue,
361                     unsigned char **buf,
362                     size_t *lenremain);
363
364 OM_uint32
365 sequenceExternalize(OM_uint32 *minor,
366                     void *vqueue,
367                     unsigned char **buf,
368                     size_t *lenremain);
369
370 size_t
371 sequenceSize(void *vqueue);
372
373 OM_uint32
374 sequenceFree(OM_uint32 *minor, void **vqueue);
375
376 OM_uint32
377 sequenceCheck(OM_uint32 *minor, void **vqueue, uint64_t seqnum);
378
379 OM_uint32
380 sequenceInit(OM_uint32 *minor, void **vqueue, uint64_t seqnum,
381              int do_replay, int do_sequence, int wide_nums);
382
383 /* util_token.c */
384 size_t
385 tokenSize(const gss_OID_desc *mech, size_t body_size);
386
387 void
388 makeTokenHeader(const gss_OID_desc *mech,
389                 size_t body_size,
390                 unsigned char **buf,
391                 enum gss_eap_token_type tok_type);
392
393 OM_uint32
394 verifyTokenHeader(OM_uint32 *minor,
395                   gss_OID mech,
396                   size_t *body_size,
397                   unsigned char **buf_in,
398                   size_t toksize_in,
399                   enum gss_eap_token_type tok_type);
400
401 /* Helper macros */
402 #define GSSEAP_CALLOC(count, size)      (calloc((count), (size)))
403 #define GSSEAP_FREE(ptr)                (free((ptr)))
404 #define GSSEAP_MALLOC(size)             (malloc((size)))
405 #define GSSEAP_REALLOC(ptr, size)       (realloc((ptr), (size)))
406
407 #define GSSEAP_NOT_IMPLEMENTED          do {            \
408         assert(0 && "not implemented");                 \
409         *minor = ENOSYS;                                \
410         return GSS_S_FAILURE;                           \
411     } while (0)
412
413 #include <pthread.h>
414
415 #define GSSEAP_MUTEX                    pthread_mutex_t
416 #define GSSEAP_MUTEX_INITIALIZER        PTHREAD_MUTEX_INITIALIZER
417
418 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
419 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
420 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
421 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
422
423 #define GSSEAP_THREAD_KEY               pthread_key_t
424 #define GSSEAP_KEY_CREATE(k, d)         pthread_key_create((k), (d))
425 #define GSSEAP_GETSPECIFIC(k)           pthread_getspecific((k))
426 #define GSSEAP_SETSPECIFIC(k, d)        pthread_setspecific((k), (d))
427
428 #define GSSEAP_THREAD_ONCE              pthread_once_t
429 #define GSSEAP_ONCE(o, i)               pthread_once((o), (i))
430 #define GSSEAP_ONCE_INITIALIZER         PTHREAD_ONCE_INIT
431
432 /* Helper functions */
433 static inline void
434 store_uint16_be(uint16_t val, void *vp)
435 {
436     unsigned char *p = (unsigned char *)vp;
437
438     p[0] = (val >>  8) & 0xff;
439     p[1] = (val      ) & 0xff;
440 }
441
442 static inline uint16_t
443 load_uint16_be(const void *cvp)
444 {
445     const unsigned char *p = (const unsigned char *)cvp;
446
447     return (p[1] | (p[0] << 8));
448 }
449
450 static inline void
451 store_uint32_be(uint32_t val, void *vp)
452 {
453     unsigned char *p = (unsigned char *)vp;
454
455     p[0] = (val >> 24) & 0xff;
456     p[1] = (val >> 16) & 0xff;
457     p[2] = (val >>  8) & 0xff;
458     p[3] = (val      ) & 0xff;
459 }
460
461 static inline uint32_t
462 load_uint32_be(const void *cvp)
463 {
464     const unsigned char *p = (const unsigned char *)cvp;
465
466     return (p[3] | (p[2] << 8)
467             | ((uint32_t) p[1] << 16)
468             | ((uint32_t) p[0] << 24));
469 }
470
471 static inline void
472 store_uint64_be(uint64_t val, void *vp)
473 {
474     unsigned char *p = (unsigned char *)vp;
475
476     p[0] = (unsigned char)((val >> 56) & 0xff);
477     p[1] = (unsigned char)((val >> 48) & 0xff);
478     p[2] = (unsigned char)((val >> 40) & 0xff);
479     p[3] = (unsigned char)((val >> 32) & 0xff);
480     p[4] = (unsigned char)((val >> 24) & 0xff);
481     p[5] = (unsigned char)((val >> 16) & 0xff);
482     p[6] = (unsigned char)((val >>  8) & 0xff);
483     p[7] = (unsigned char)((val      ) & 0xff);
484 }
485
486 static inline uint64_t
487 load_uint64_be(const void *cvp)
488 {
489     const unsigned char *p = (const unsigned char *)cvp;
490
491     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
492 }
493
494 static inline unsigned char *
495 store_buffer(gss_buffer_t buffer, void *vp, int wide_nums)
496 {
497     unsigned char *p = (unsigned char *)vp;
498
499     if (wide_nums) {
500         store_uint64_be(buffer->length, p);
501         p += 8;
502     } else {
503         store_uint32_be(buffer->length, p);
504         p += 4;
505     }
506
507     if (buffer->value != NULL) {
508         memcpy(p, buffer->value, buffer->length);
509         p += buffer->length;
510     }
511
512     return p;
513 }
514
515 static inline unsigned char *
516 load_buffer(const void *cvp, size_t length, gss_buffer_t buffer)
517 {
518     buffer->length = 0;
519     buffer->value = GSSEAP_MALLOC(length);
520     if (buffer->value == NULL)
521         return NULL;
522     buffer->length = length;
523     memcpy(buffer->value, cvp, length);
524     return (unsigned char *)cvp + length;
525 }
526
527 static inline unsigned char *
528 store_oid(gss_OID oid, void *vp)
529 {
530     gss_buffer_desc buf;
531
532     if (oid != GSS_C_NO_OID) {
533         buf.length = oid->length;
534         buf.value = oid->elements;
535     } else {
536         buf.length = 0;
537         buf.value = NULL;
538     }
539
540     return store_buffer(&buf, vp, FALSE);
541 }
542
543 static inline void
544 krbDataToGssBuffer(krb5_data *data, gss_buffer_t buffer)
545 {
546     buffer->value = (void *)data->data;
547     buffer->length = data->length;
548 }
549
550 #ifdef __cplusplus
551 }
552 #endif
553
554 #include "util_attr.h"
555
556 #endif /* _UTIL_H_ */