Heimdal portability fixes (except for reauth)
[mech_eap.orig] / gssapiP_eap.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 #ifndef _GSSAPIP_EAP_H_
34 #define _GSSAPIP_EAP_H_ 1
35
36 #include "config.h"
37
38 #include <assert.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <sys/param.h>
45
46 /* GSS headers */
47 #include <gssapi/gssapi.h>
48 #include <gssapi/gssapi_krb5.h>
49 #ifndef HAVE_HEIMDAL_VERSION
50 #include <gssapi/gssapi_ext.h>
51 #endif
52 #include "gssapi_eap.h"
53
54 /* Kerberos headers */
55 #include <krb5.h>
56
57 /* EAP headers */
58 #include <common.h>
59 #include <eap_peer/eap.h>
60 #include <eap_peer/eap_config.h>
61 #include <eap_peer/eap_methods.h>
62 #include <wpabuf.h>
63
64 /* FreeRADIUS headers */
65 #ifdef __cplusplus
66 extern "C" {
67 #define operator fr_operator
68 #endif
69 #include <freeradius/libradius.h>
70 #include <freeradius/radius.h>
71 #include <radsec/radsec.h>
72 #include <radsec/request.h>
73 #ifdef __cplusplus
74 #undef operator
75 }
76 #endif
77
78 #include "gsseap_err.h"
79 #include "radsec_err.h"
80 #include "util.h"
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86 /* These name flags are informative and not actually used by anything yet */
87 #define NAME_FLAG_NAI                       0x00000001
88 #define NAME_FLAG_SERVICE                   0x00000002
89 #define NAME_FLAG_COMPOSITE                 0x00000004
90
91 struct gss_eap_saml_attr_ctx;
92 struct gss_eap_attr_ctx;
93
94 #ifdef HAVE_HEIMDAL_VERSION
95 struct gss_name_t_desc_struct
96 #else
97 struct gss_name_struct
98 #endif
99 {
100     GSSEAP_MUTEX mutex; /* mutex protects attrCtx */
101     OM_uint32 flags;
102     krb5_principal krbPrincipal; /* this is immutable */
103     struct gss_eap_attr_ctx *attrCtx;
104 };
105
106 #define CRED_FLAG_INITIATE                  0x00010000
107 #define CRED_FLAG_ACCEPT                    0x00020000
108 #define CRED_FLAG_DEFAULT_IDENTITY          0x00040000
109 #define CRED_FLAG_PASSWORD                  0x00080000
110 #define CRED_FLAG_DEFAULT_CCACHE            0x00100000
111 #define CRED_FLAG_PUBLIC_MASK               0x0000FFFF
112
113 #ifdef HAVE_HEIMDAL_VERSION
114 struct gss_cred_id_t_desc_struct
115 #else
116 struct gss_cred_id_struct
117 #endif
118 {
119     GSSEAP_MUTEX mutex;
120     OM_uint32 flags;
121     gss_name_t name;
122     gss_buffer_desc password;
123     gss_OID_set mechanisms;
124     time_t expiryTime;
125     char *radiusConfigFile;
126     char *radiusConfigStanza;
127 #ifdef GSSEAP_ENABLE_REAUTH
128     krb5_ccache krbCredCache;
129     gss_cred_id_t krbCred;
130 #endif
131 };
132
133 #define CTX_FLAG_INITIATOR                  0x00000001
134 #define CTX_FLAG_KRB_REAUTH                 0x00000002
135
136 #define CTX_IS_INITIATOR(ctx)               (((ctx)->flags & CTX_FLAG_INITIATOR) != 0)
137
138 enum gss_eap_state {
139     GSSEAP_STATE_IDENTITY = 0,              /* identify peer */
140     GSSEAP_STATE_AUTHENTICATE,              /* exchange EAP messages */
141     GSSEAP_STATE_EXTENSIONS_REQ,            /* initiator extensions */
142     GSSEAP_STATE_EXTENSIONS_RESP,           /* acceptor extensions */
143     GSSEAP_STATE_ESTABLISHED,               /* context established */
144     GSSEAP_STATE_ERROR,                     /* context error */
145 #ifdef GSSEAP_ENABLE_REAUTH
146     GSSEAP_STATE_KRB_REAUTH                 /* fast reauthentication */
147 #endif
148 };
149
150 #define CTX_IS_ESTABLISHED(ctx)             ((ctx)->state == GSSEAP_STATE_ESTABLISHED)
151
152 /* Initiator context flags */
153 #define CTX_FLAG_EAP_SUCCESS                0x00010000
154 #define CTX_FLAG_EAP_RESTART                0x00020000
155 #define CTX_FLAG_EAP_FAIL                   0x00040000
156 #define CTX_FLAG_EAP_RESP                   0x00080000
157 #define CTX_FLAG_EAP_NO_RESP                0x00100000
158 #define CTX_FLAG_EAP_REQ                    0x00200000
159 #define CTX_FLAG_EAP_PORT_ENABLED           0x00400000
160 #define CTX_FLAG_EAP_ALT_ACCEPT             0x00800000
161 #define CTX_FLAG_EAP_ALT_REJECT             0x01000000
162 #define CTX_FLAG_EAP_MASK                   0xFFFF0000
163
164 struct gss_eap_initiator_ctx {
165     gss_cred_id_t defaultCred;
166     unsigned int idleWhile;
167 #ifndef __cplusplus
168     struct eap_peer_config eapPeerConfig;
169     struct eap_sm *eap;
170     struct wpabuf reqData;
171 #endif
172 };
173
174 struct gss_eap_acceptor_ctx {
175     struct rs_context *radContext;
176     struct rs_connection *radConn;
177     char *radServer;
178     gss_buffer_desc state;
179     VALUE_PAIR *vps;
180 };
181
182 #ifdef HAVE_HEIMDAL_VERSION
183 struct gss_ctx_id_t_desc_struct
184 #else
185 struct gss_ctx_id_struct
186 #endif
187 {
188     GSSEAP_MUTEX mutex;
189     enum gss_eap_state state;
190     OM_uint32 flags;
191     OM_uint32 gssFlags;
192     gss_OID mechanismUsed;
193     krb5_cksumtype checksumType;
194     krb5_enctype encryptionType;
195     krb5_keyblock rfc3961Key;
196     gss_name_t initiatorName;
197     gss_name_t acceptorName;
198     time_t expiryTime;
199     uint64_t sendSeq, recvSeq;
200     void *seqState;
201     union {
202         struct gss_eap_initiator_ctx initiator;
203         #define initiatorCtx         ctxU.initiator
204         struct gss_eap_acceptor_ctx  acceptor;
205         #define acceptorCtx          ctxU.acceptor
206 #ifdef GSSEAP_ENABLE_REAUTH
207         gss_ctx_id_t                 kerberos;
208         #define kerberosCtx          ctxU.kerberos
209 #endif
210     } ctxU;
211 };
212
213 #define TOK_FLAG_SENDER_IS_ACCEPTOR         0x01
214 #define TOK_FLAG_WRAP_CONFIDENTIAL          0x02
215 #define TOK_FLAG_ACCEPTOR_SUBKEY            0x04
216
217 #define KEY_USAGE_ACCEPTOR_SEAL             22
218 #define KEY_USAGE_ACCEPTOR_SIGN             23
219 #define KEY_USAGE_INITIATOR_SEAL            24
220 #define KEY_USAGE_INITIATOR_SIGN            25
221
222 /* wrap_iov.c */
223 OM_uint32
224 gssEapWrapOrGetMIC(OM_uint32 *minor,
225                    gss_ctx_id_t ctx,
226                    int conf_req_flag,
227                    int *conf_state,
228                    gss_iov_buffer_desc *iov,
229                    int iov_count,
230                    enum gss_eap_token_type toktype);
231
232 OM_uint32
233 gssEapUnwrapOrVerifyMIC(OM_uint32 *minor_status,
234                         gss_ctx_id_t ctx,
235                         int *conf_state,
236                         gss_qop_t *qop_state,
237                         gss_iov_buffer_desc *iov,
238                         int iov_count,
239                         enum gss_eap_token_type toktype);
240
241 OM_uint32
242 gssEapWrapIovLength(OM_uint32 *minor,
243                     gss_ctx_id_t ctx,
244                     int conf_req_flag,
245                     gss_qop_t qop_req,
246                     int *conf_state,
247                     gss_iov_buffer_desc *iov,
248                     int iov_count);
249 OM_uint32
250 gssEapWrap(OM_uint32 *minor,
251            gss_ctx_id_t ctx,
252            int conf_req_flag,
253            gss_qop_t qop_req,
254            gss_buffer_t input_message_buffer,
255            int *conf_state,
256            gss_buffer_t output_message_buffer);
257
258 unsigned char
259 rfc4121Flags(gss_ctx_id_t ctx, int receiving);
260
261 /* display_status.c */
262 void
263 gssEapSaveStatusInfo(OM_uint32 minor, const char *format, ...);
264
265 #define IS_WIRE_ERROR(err)              ((err) > GSSEAP_RESERVED && \
266                                          (err) <= GSSEAP_RADIUS_PROT_FAILURE)
267
268 #ifdef __cplusplus
269 }
270 #endif
271
272 #endif /* _GSSAPIP_EAP_H_ */