2 * Example application showing how EAP server code from hostapd can be used as
4 * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
19 #include "crypto/tls.h"
20 #include "eap_server/eap.h"
23 void eap_example_peer_rx(const u8
*data
, size_t data_len
);
26 struct eap_server_ctx
{
27 struct eap_eapol_interface
*eap_if
;
32 static struct eap_server_ctx eap_ctx
;
35 static int server_get_eap_user(void *ctx
, const u8
*identity
,
36 size_t identity_len
, int phase2
,
37 struct eap_user
*user
)
39 os_memset(user
, 0, sizeof(*user
));
42 /* Only allow EAP-PEAP as the Phase 1 method */
43 user
->methods
[0].vendor
= EAP_VENDOR_IETF
;
44 user
->methods
[0].method
= EAP_TYPE_PEAP
;
48 if (identity_len
!= 4 || identity
== NULL
||
49 os_memcmp(identity
, "user", 4) != 0) {
50 printf("Unknown user\n");
54 /* Only allow EAP-MSCHAPv2 as the Phase 2 method */
55 user
->methods
[0].vendor
= EAP_VENDOR_IETF
;
56 user
->methods
[0].method
= EAP_TYPE_MSCHAPV2
;
57 user
->password
= (u8
*) os_strdup("password");
58 user
->password_len
= 8;
64 static const char * server_get_eap_req_id_text(void *ctx
, size_t *len
)
71 static struct eapol_callbacks eap_cb
;
72 static struct eap_config eap_conf
;
74 static int eap_example_server_init_tls(void)
76 struct tls_config tconf
;
77 struct tls_connection_params tparams
;
79 os_memset(&tconf
, 0, sizeof(tconf
));
80 eap_ctx
.tls_ctx
= tls_init(&tconf
);
81 if (eap_ctx
.tls_ctx
== NULL
)
84 os_memset(&tparams
, 0, sizeof(tparams
));
85 tparams
.ca_cert
= "ca.pem";
86 tparams
.client_cert
= "server.pem";
87 /* tparams.private_key = "server.key"; */
88 tparams
.private_key
= "server-key.pem";
89 /* tparams.private_key_passwd = "whatever"; */
91 if (tls_global_set_params(eap_ctx
.tls_ctx
, &tparams
)) {
92 printf("Failed to set TLS parameters\n");
96 if (tls_global_set_verify(eap_ctx
.tls_ctx
, 0)) {
97 printf("Failed to set check_crl\n");
105 static int eap_server_register_methods(void)
109 #ifdef EAP_SERVER_IDENTITY
111 ret
= eap_server_identity_register();
112 #endif /* EAP_SERVER_IDENTITY */
114 #ifdef EAP_SERVER_MD5
116 ret
= eap_server_md5_register();
117 #endif /* EAP_SERVER_MD5 */
119 #ifdef EAP_SERVER_TLS
121 ret
= eap_server_tls_register();
122 #endif /* EAP_SERVER_TLS */
124 #ifdef EAP_SERVER_MSCHAPV2
126 ret
= eap_server_mschapv2_register();
127 #endif /* EAP_SERVER_MSCHAPV2 */
129 #ifdef EAP_SERVER_PEAP
131 ret
= eap_server_peap_register();
132 #endif /* EAP_SERVER_PEAP */
134 #ifdef EAP_SERVER_TLV
136 ret
= eap_server_tlv_register();
137 #endif /* EAP_SERVER_TLV */
139 #ifdef EAP_SERVER_GTC
141 ret
= eap_server_gtc_register();
142 #endif /* EAP_SERVER_GTC */
144 #ifdef EAP_SERVER_TTLS
146 ret
= eap_server_ttls_register();
147 #endif /* EAP_SERVER_TTLS */
149 #ifdef EAP_SERVER_SIM
151 ret
= eap_server_sim_register();
152 #endif /* EAP_SERVER_SIM */
154 #ifdef EAP_SERVER_AKA
156 ret
= eap_server_aka_register();
157 #endif /* EAP_SERVER_AKA */
159 #ifdef EAP_SERVER_AKA_PRIME
161 ret
= eap_server_aka_prime_register();
162 #endif /* EAP_SERVER_AKA_PRIME */
164 #ifdef EAP_SERVER_PAX
166 ret
= eap_server_pax_register();
167 #endif /* EAP_SERVER_PAX */
169 #ifdef EAP_SERVER_PSK
171 ret
= eap_server_psk_register();
172 #endif /* EAP_SERVER_PSK */
174 #ifdef EAP_SERVER_SAKE
176 ret
= eap_server_sake_register();
177 #endif /* EAP_SERVER_SAKE */
179 #ifdef EAP_SERVER_GPSK
181 ret
= eap_server_gpsk_register();
182 #endif /* EAP_SERVER_GPSK */
184 #ifdef EAP_SERVER_VENDOR_TEST
186 ret
= eap_server_vendor_test_register();
187 #endif /* EAP_SERVER_VENDOR_TEST */
189 #ifdef EAP_SERVER_FAST
191 ret
= eap_server_fast_register();
192 #endif /* EAP_SERVER_FAST */
194 #ifdef EAP_SERVER_WSC
196 ret
= eap_server_wsc_register();
197 #endif /* EAP_SERVER_WSC */
199 #ifdef EAP_SERVER_IKEV2
201 ret
= eap_server_ikev2_register();
202 #endif /* EAP_SERVER_IKEV2 */
204 #ifdef EAP_SERVER_TNC
206 ret
= eap_server_tnc_register();
207 #endif /* EAP_SERVER_TNC */
213 int eap_example_server_init(void)
215 if (eap_server_register_methods() < 0)
218 os_memset(&eap_ctx
, 0, sizeof(eap_ctx
));
220 if (eap_example_server_init_tls() < 0)
223 os_memset(&eap_cb
, 0, sizeof(eap_cb
));
224 eap_cb
.get_eap_user
= server_get_eap_user
;
225 eap_cb
.get_eap_req_id_text
= server_get_eap_req_id_text
;
227 os_memset(&eap_conf
, 0, sizeof(eap_conf
));
228 eap_conf
.eap_server
= 1;
229 eap_conf
.ssl_ctx
= eap_ctx
.tls_ctx
;
231 eap_ctx
.eap
= eap_server_sm_init(&eap_ctx
, &eap_cb
, &eap_conf
);
232 if (eap_ctx
.eap
== NULL
)
235 eap_ctx
.eap_if
= eap_get_interface(eap_ctx
.eap
);
237 /* Enable "port" and request EAP to start authentication. */
238 eap_ctx
.eap_if
->portEnabled
= TRUE
;
239 eap_ctx
.eap_if
->eapRestart
= TRUE
;
245 void eap_example_server_deinit(void)
247 eap_server_sm_deinit(eap_ctx
.eap
);
248 eap_server_unregister_methods();
249 tls_deinit(eap_ctx
.tls_ctx
);
253 int eap_example_server_step(void)
255 int res
, process
= 0;
257 res
= eap_server_sm_step(eap_ctx
.eap
);
259 if (eap_ctx
.eap_if
->eapReq
) {
260 printf("==> Request\n");
262 eap_ctx
.eap_if
->eapReq
= 0;
265 if (eap_ctx
.eap_if
->eapSuccess
) {
266 printf("==> Success\n");
269 eap_ctx
.eap_if
->eapSuccess
= 0;
271 if (eap_ctx
.eap_if
->eapKeyAvailable
) {
272 wpa_hexdump(MSG_DEBUG
, "EAP keying material",
273 eap_ctx
.eap_if
->eapKeyData
,
274 eap_ctx
.eap_if
->eapKeyDataLen
);
278 if (eap_ctx
.eap_if
->eapFail
) {
279 printf("==> Fail\n");
281 eap_ctx
.eap_if
->eapFail
= 0;
284 if (process
&& eap_ctx
.eap_if
->eapReqData
) {
285 /* Send EAP response to the server */
286 eap_example_peer_rx(wpabuf_head(eap_ctx
.eap_if
->eapReqData
),
287 wpabuf_len(eap_ctx
.eap_if
->eapReqData
));
294 void eap_example_server_rx(const u8
*data
, size_t data_len
)
296 /* Make received EAP message available to the EAP library */
297 wpabuf_free(eap_ctx
.eap_if
->eapRespData
);
298 eap_ctx
.eap_if
->eapRespData
= wpabuf_alloc_copy(data
, data_len
);
299 if (eap_ctx
.eap_if
->eapRespData
)
300 eap_ctx
.eap_if
->eapResp
= TRUE
;