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