972f721674a24b5c7e6cddbb35e91b58f7bf27eb
[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 #define KRB_KEYTYPE(key)        ((key)->enctype)
63
64 int
65 gssEapSign(krb5_context context,
66            krb5_cksumtype type,
67            size_t rrc,
68            krb5_keyblock *key,
69            krb5_keyusage sign_usage,
70            gss_iov_buffer_desc *iov,
71            int iov_count);
72
73 int
74 gssEapVerify(krb5_context context,
75              krb5_cksumtype type,
76              size_t rrc,  
77              krb5_keyblock *key,
78              krb5_keyusage sign_usage,
79              gss_iov_buffer_desc *iov,
80              int iov_count,
81              int *valid);
82
83 /* util_context.c */
84 OM_uint32 gssEapAllocContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
85 OM_uint32 gssEapReleaseContext(OM_uint32 *minor, gss_ctx_id_t *pCtx);
86
87 /* util_cred.c */
88 OM_uint32 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred);
89 OM_uint32 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred);
90
91 /* util_crypt.c */
92 int
93 gssEapEncrypt(krb5_context context, int dce_style, size_t ec,
94               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
95               gss_iov_buffer_desc *iov, int iov_count);
96
97 int
98 gssEapDecrypt(krb5_context context, int dce_style, size_t ec,
99               size_t rrc, krb5_keyblock *key, int usage, krb5_pointer iv,
100               gss_iov_buffer_desc *iov, int iov_count);
101
102 krb5_cryptotype
103 gssEapMapCryptoFlag(OM_uint32 type);
104
105 gss_iov_buffer_t
106 gssEapLocateIov(gss_iov_buffer_desc *iov,
107                 int iov_count,
108                 OM_uint32 type);
109
110 void
111 gssEapIovMessageLength(gss_iov_buffer_desc *iov,
112                        int iov_count,
113                        size_t *data_length,
114                        size_t *assoc_data_length);
115
116 void
117 gssEapReleaseIov(gss_iov_buffer_desc *iov, int iov_count);
118
119 int
120 gssEapIsIntegrityOnly(gss_iov_buffer_desc *iov, int iov_count);
121
122 int
123 gssEapAllocIov(gss_iov_buffer_t iov, size_t size);
124
125 /* util_mech.c */
126 void
127 gssEapInternalizeOid(const gss_OID oid,
128                      gss_OID *const pInternalizedOid);
129
130 OM_uint32
131 gssEapDefaultMech(OM_uint32 *minor,
132                   gss_OID *oid);
133
134 OM_uint32
135 gssEapIndicateMechs(OM_uint32 *minor,
136                     gss_OID_set *mechs);
137
138 OM_uint32
139 gssEapEnctypeToOid(OM_uint32 *minor,
140                    krb5_enctype enctype,
141                    gss_OID *pOid);
142
143 OM_uint32
144 gssEapOidToEnctype(OM_uint32 *minor,
145                    const gss_OID oid,
146                    krb5_enctype *enctype);
147
148 int
149 gssEapIsMechanismOid(const gss_OID oid);
150
151 /* util_name.c */
152 OM_uint32 gssEapAllocName(OM_uint32 *minor, gss_name_t *pName);
153 OM_uint32 gssEapReleaseName(OM_uint32 *minor, gss_name_t *pName);
154
155 /* util_oid.c */
156 OM_uint32
157 composeOid(OM_uint32 *minor_status,
158            const char *prefix,
159            size_t prefix_len,
160            int suffix,  
161            gss_OID_desc *oid);
162
163 OM_uint32
164 decomposeOid(OM_uint32 *minor_status,
165              const char *prefix,
166              size_t prefix_len,
167              gss_OID_desc *oid,
168              int *suffix) ;
169
170 static inline int
171 oidEqual(const gss_OID_desc *o1, const gss_OID_desc  *o2)
172 {
173     return (o1->length == o2->length &&
174             memcmp(o1->elements, o2->elements, o1->length) == 0);
175 }
176
177 /* util_ordering.c */
178 int
179 sequenceInternalize(void **vqueue, unsigned char **buf, size_t *lenremain);
180
181 int
182 sequenceExternalize(void *vqueue, unsigned char **buf, size_t *lenremain);
183
184 int
185 sequenceSize(void *vqueue, size_t *sizep);
186
187 void
188 sequenceFree(void **vqueue);
189
190 int
191 sequenceCheck(void **vqueue, uint64_t seqnum);
192
193 int
194 sequenceInit(void **vqueue, uint64_t seqnum,
195              int do_replay, int do_sequence, int wide_nums);
196
197 /* util_token.c */
198 enum gss_eap_token_type {
199     TOK_TYPE_EAP_RESP  = 0x0601,
200     TOK_TYPE_EAP_REQ   = 0x0602,
201     TOK_TYPE_GSS_CB    = 0x0603,
202     TOK_TYPE_MIC       = 0x0404,
203     TOK_TYPE_WRAP      = 0x0504,
204     TOK_TYPE_DELETE    = 0x0405,
205     TOK_TYPE_NONE      = 0xFFFF
206 };
207
208 size_t
209 tokenSize(const gss_OID_desc *mech, size_t body_size);
210
211 void
212 makeTokenHeader(const gss_OID_desc *mech,
213                 size_t body_size,
214                 unsigned char **buf,
215                 enum gss_eap_token_type tok_type);
216
217 int
218 verifyTokenHeader(const gss_OID_desc * mech,
219                   size_t *body_size,
220                   unsigned char **buf_in,
221                   size_t toksize_in,
222                   enum gss_eap_token_type tok_type);
223
224 /* Helper macros */
225 #define GSSEAP_CALLOC(count, size)      (calloc((count), (size)))
226 #define GSSEAP_FREE(ptr)                (free((ptr)))
227 #define GSSEAP_MALLOC(size)             (malloc((size)))
228 #define GSSEAP_REALLOC(ptr, size)       (realloc((ptr), (size)))
229
230 #define GSSEAP_NOT_IMPLEMENTED          do {            \
231         assert(0 && "not implemented");                 \
232         *minor = ENOSYS;                                \
233         return GSS_S_FAILURE;                           \
234     } while (0)
235
236 #include <pthread.h>
237
238 #define GSSEAP_MUTEX                    pthread_mutex_t
239 #define GSSEAP_MUTEX_INIT(m)            pthread_mutex_init((m), NULL)
240 #define GSSEAP_MUTEX_DESTROY(m)         pthread_mutex_destroy((m))
241 #define GSSEAP_MUTEX_LOCK(m)            pthread_mutex_lock((m))
242 #define GSSEAP_MUTEX_UNLOCK(m)          pthread_mutex_unlock((m))
243
244 /* Helper functions */
245 static inline void
246 store_uint16_be(uint16_t val, void *vp)
247 {
248     unsigned char *p = (unsigned char *)vp;
249
250     p[0] = (val >>  8) & 0xff;
251     p[1] = (val      ) & 0xff;
252 }
253
254 static inline uint16_t
255 load_uint16_be(const void *cvp)
256 {
257     const unsigned char *p = (const unsigned char *)cvp;
258
259     return (p[1] | (p[0] << 8));
260 }
261
262 static inline void
263 store_uint32_be(uint32_t val, void *vp)
264 {
265     unsigned char *p = (unsigned char *)vp;
266
267     p[0] = (val >> 24) & 0xff;
268     p[1] = (val >> 16) & 0xff;
269     p[2] = (val >>  8) & 0xff;
270     p[3] = (val      ) & 0xff;
271 }
272
273 static inline uint32_t
274 load_uint32_be(const void *cvp)
275 {
276     const unsigned char *p = (const unsigned char *)cvp;
277
278     return (p[3] | (p[2] << 8)
279             | ((uint32_t) p[1] << 16)
280             | ((uint32_t) p[0] << 24));
281 }
282
283 static inline void
284 store_uint64_be(uint64_t val, void *vp)
285 {
286     unsigned char *p = (unsigned char *)vp;
287
288     p[0] = (unsigned char)((val >> 56) & 0xff);
289     p[1] = (unsigned char)((val >> 48) & 0xff);
290     p[2] = (unsigned char)((val >> 40) & 0xff);
291     p[3] = (unsigned char)((val >> 32) & 0xff);
292     p[4] = (unsigned char)((val >> 24) & 0xff);
293     p[5] = (unsigned char)((val >> 16) & 0xff);
294     p[6] = (unsigned char)((val >>  8) & 0xff);
295     p[7] = (unsigned char)((val      ) & 0xff);
296 }
297
298 static inline uint64_t
299 load_uint64_be(const void *cvp)
300 {
301     const unsigned char *p = (const unsigned char *)cvp;
302
303     return ((uint64_t)load_uint32_be(p) << 32) | load_uint32_be(p + 4);
304 }
305
306 #endif /* _UTIL_H_ */