2 * Example application showing how EAP peer code from wpa_supplicant can be
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 "eap_peer/eap.h"
20 #include "eap_peer/eap_config.h"
23 void eap_example_server_rx(const u8
*data
, size_t data_len
);
34 Boolean altAccept
; /* for EAP */
35 Boolean altReject
; /* for EAP */
37 struct wpabuf
*eapReqData
; /* for EAP */
39 unsigned int idleWhile
; /* for EAP state machine */
41 struct eap_peer_config eap_config
;
46 static struct eap_peer_ctx eap_ctx
;
49 static struct eap_peer_config
* peer_get_config(void *ctx
)
51 struct eap_peer_ctx
*peer
= ctx
;
52 return &peer
->eap_config
;
56 static Boolean
peer_get_bool(void *ctx
, enum eapol_bool_var variable
)
58 struct eap_peer_ctx
*peer
= ctx
;
62 case EAPOL_eapSuccess
:
63 return peer
->eapSuccess
;
64 case EAPOL_eapRestart
:
65 return peer
->eapRestart
;
71 return peer
->eapNoResp
;
74 case EAPOL_portEnabled
:
75 return peer
->portEnabled
;
77 return peer
->altAccept
;
79 return peer
->altReject
;
85 static void peer_set_bool(void *ctx
, enum eapol_bool_var variable
,
88 struct eap_peer_ctx
*peer
= ctx
;
92 case EAPOL_eapSuccess
:
93 peer
->eapSuccess
= value
;
95 case EAPOL_eapRestart
:
96 peer
->eapRestart
= value
;
99 peer
->eapFail
= value
;
102 peer
->eapResp
= value
;
104 case EAPOL_eapNoResp
:
105 peer
->eapNoResp
= value
;
108 peer
->eapReq
= value
;
110 case EAPOL_portEnabled
:
111 peer
->portEnabled
= value
;
113 case EAPOL_altAccept
:
114 peer
->altAccept
= value
;
116 case EAPOL_altReject
:
117 peer
->altReject
= value
;
123 static unsigned int peer_get_int(void *ctx
, enum eapol_int_var variable
)
125 struct eap_peer_ctx
*peer
= ctx
;
129 case EAPOL_idleWhile
:
130 return peer
->idleWhile
;
136 static void peer_set_int(void *ctx
, enum eapol_int_var variable
,
139 struct eap_peer_ctx
*peer
= ctx
;
143 case EAPOL_idleWhile
:
144 peer
->idleWhile
= value
;
150 static struct wpabuf
* peer_get_eapReqData(void *ctx
)
152 struct eap_peer_ctx
*peer
= ctx
;
153 if (peer
== NULL
|| peer
->eapReqData
== NULL
)
156 return peer
->eapReqData
;
160 static void peer_set_config_blob(void *ctx
, struct wpa_config_blob
*blob
)
162 printf("TODO: %s\n", __func__
);
166 static const struct wpa_config_blob
*
167 peer_get_config_blob(void *ctx
, const char *name
)
169 printf("TODO: %s\n", __func__
);
174 static void peer_notify_pending(void *ctx
)
176 printf("TODO: %s\n", __func__
);
180 static int eap_peer_register_methods(void)
186 ret
= eap_peer_md5_register();
191 ret
= eap_peer_tls_register();
196 ret
= eap_peer_mschapv2_register();
197 #endif /* EAP_MSCHAPv2 */
201 ret
= eap_peer_peap_register();
202 #endif /* EAP_PEAP */
206 ret
= eap_peer_ttls_register();
207 #endif /* EAP_TTLS */
211 ret
= eap_peer_gtc_register();
216 ret
= eap_peer_otp_register();
221 ret
= eap_peer_sim_register();
226 ret
= eap_peer_leap_register();
227 #endif /* EAP_LEAP */
231 ret
= eap_peer_psk_register();
236 ret
= eap_peer_aka_register();
241 ret
= eap_peer_aka_prime_register();
242 #endif /* EAP_AKA_PRIME */
246 ret
= eap_peer_fast_register();
247 #endif /* EAP_FAST */
251 ret
= eap_peer_pax_register();
256 ret
= eap_peer_sake_register();
257 #endif /* EAP_SAKE */
261 ret
= eap_peer_gpsk_register();
262 #endif /* EAP_GPSK */
266 ret
= eap_peer_wsc_register();
271 ret
= eap_peer_ikev2_register();
272 #endif /* EAP_IKEV2 */
274 #ifdef EAP_VENDOR_TEST
276 ret
= eap_peer_vendor_test_register();
277 #endif /* EAP_VENDOR_TEST */
281 ret
= eap_peer_tnc_register();
288 static struct eapol_callbacks eap_cb
;
289 static struct eap_config eap_conf
;
291 int eap_example_peer_init(void)
293 if (eap_peer_register_methods() < 0)
296 os_memset(&eap_ctx
, 0, sizeof(eap_ctx
));
298 eap_ctx
.eap_config
.identity
= (u8
*) os_strdup("user");
299 eap_ctx
.eap_config
.identity_len
= 4;
300 eap_ctx
.eap_config
.password
= (u8
*) os_strdup("password");
301 eap_ctx
.eap_config
.password_len
= 8;
302 eap_ctx
.eap_config
.ca_cert
= (u8
*) os_strdup("ca.pem");
303 eap_ctx
.eap_config
.fragment_size
= 1398;
305 os_memset(&eap_cb
, 0, sizeof(eap_cb
));
306 eap_cb
.get_config
= peer_get_config
;
307 eap_cb
.get_bool
= peer_get_bool
;
308 eap_cb
.set_bool
= peer_set_bool
;
309 eap_cb
.get_int
= peer_get_int
;
310 eap_cb
.set_int
= peer_set_int
;
311 eap_cb
.get_eapReqData
= peer_get_eapReqData
;
312 eap_cb
.set_config_blob
= peer_set_config_blob
;
313 eap_cb
.get_config_blob
= peer_get_config_blob
;
314 eap_cb
.notify_pending
= peer_notify_pending
;
316 os_memset(&eap_conf
, 0, sizeof(eap_conf
));
317 eap_ctx
.eap
= eap_peer_sm_init(&eap_ctx
, &eap_cb
, &eap_ctx
, &eap_conf
);
318 if (eap_ctx
.eap
== NULL
)
321 /* Enable "port" to allow authentication */
322 eap_ctx
.portEnabled
= TRUE
;
328 void eap_example_peer_deinit(void)
330 eap_peer_sm_deinit(eap_ctx
.eap
);
331 eap_peer_unregister_methods();
332 wpabuf_free(eap_ctx
.eapReqData
);
333 os_free(eap_ctx
.eap_config
.identity
);
334 os_free(eap_ctx
.eap_config
.password
);
335 os_free(eap_ctx
.eap_config
.ca_cert
);
339 int eap_example_peer_step(void)
342 res
= eap_peer_sm_step(eap_ctx
.eap
);
344 if (eap_ctx
.eapResp
) {
346 printf("==> Response\n");
347 eap_ctx
.eapResp
= FALSE
;
348 resp
= eap_get_eapRespData(eap_ctx
.eap
);
350 /* Send EAP response to the server */
351 eap_example_server_rx(wpabuf_head(resp
),
357 if (eap_ctx
.eapSuccess
) {
359 if (eap_key_available(eap_ctx
.eap
)) {
362 key
= eap_get_eapKeyData(eap_ctx
.eap
, &key_len
);
363 wpa_hexdump(MSG_DEBUG
, "EAP keying material",
372 void eap_example_peer_rx(const u8
*data
, size_t data_len
)
374 /* Make received EAP message available to the EAP library */
375 eap_ctx
.eapReq
= TRUE
;
376 wpabuf_free(eap_ctx
.eapReqData
);
377 eap_ctx
.eapReqData
= wpabuf_alloc_copy(data
, data_len
);