blob: 95953de928bd2e11ac679d55655c966ca69de68a [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - test code
Dmitry Shmidt051af732013-10-22 13:52:46 -07003 * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
9 * Not used in production version.
10 */
11
12#include "includes.h"
13#include <assert.h>
14
15#include "common.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070016#include "utils/ext_password.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080017#include "common/version.h"
Sunil Ravia04bd252022-05-02 22:54:18 -070018#include "crypto/crypto.h"
Hai Shalom81f62d82019-07-22 12:10:00 -070019#include "crypto/tls.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "config.h"
21#include "eapol_supp/eapol_supp_sm.h"
22#include "eap_peer/eap.h"
23#include "eap_server/eap_methods.h"
24#include "eloop.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080025#include "utils/base64.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa_supplicant_i.h"
28#include "radius/radius.h"
29#include "radius/radius_client.h"
Dmitry Shmidtc55524a2011-07-07 11:18:38 -070030#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031#include "ctrl_iface.h"
32#include "pcsc_funcs.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070033#include "wpas_glue.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034
35
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070036const struct wpa_driver_ops *const wpa_drivers[] = { NULL };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037
38
39struct extra_radius_attr {
40 u8 type;
41 char syntax;
42 char *data;
43 struct extra_radius_attr *next;
44};
45
46struct eapol_test_data {
47 struct wpa_supplicant *wpa_s;
48
49 int eapol_test_num_reauths;
50 int no_mppe_keys;
51 int num_mppe_ok, num_mppe_mismatch;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -070052 int req_eap_key_name;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053
54 u8 radius_identifier;
55 struct radius_msg *last_recv_radius;
56 struct in_addr own_ip_addr;
57 struct radius_client_data *radius;
58 struct hostapd_radius_servers *radius_conf;
59
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070060 /* last received EAP Response from Authentication Server */
61 struct wpabuf *last_eap_radius;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062
63 u8 authenticator_pmk[PMK_LEN];
64 size_t authenticator_pmk_len;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -070065 u8 authenticator_eap_key_name[256];
66 size_t authenticator_eap_key_name_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067 int radius_access_accept_received;
68 int radius_access_reject_received;
69 int auth_timed_out;
70
71 u8 *eap_identity;
72 size_t eap_identity_len;
73
74 char *connect_info;
75 u8 own_addr[ETH_ALEN];
76 struct extra_radius_attr *extra_attrs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080077
78 FILE *server_cert_file;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -070079
80 const char *pcsc_reader;
81 const char *pcsc_pin;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080082
83 unsigned int ctrl_iface:1;
84 unsigned int id_req_sent:1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085};
86
87static struct eapol_test_data eapol_test;
88
89
90static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
91
92
93static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
94 int level, const char *txt, size_t len)
95{
96 if (addr)
97 wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
98 MAC2STR(addr), txt);
99 else
100 wpa_printf(MSG_DEBUG, "%s", txt);
101}
102
103
104static int add_extra_attr(struct radius_msg *msg,
105 struct extra_radius_attr *attr)
106{
107 size_t len;
108 char *pos;
109 u32 val;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800110 char buf[RADIUS_MAX_ATTR_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111
112 switch (attr->syntax) {
113 case 's':
114 os_snprintf(buf, sizeof(buf), "%s", attr->data);
115 len = os_strlen(buf);
116 break;
117 case 'n':
118 buf[0] = '\0';
119 len = 1;
120 break;
121 case 'x':
122 pos = attr->data;
123 if (pos[0] == '0' && pos[1] == 'x')
124 pos += 2;
125 len = os_strlen(pos);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800126 if ((len & 1) || (len / 2) > RADIUS_MAX_ATTR_LEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127 printf("Invalid extra attribute hexstring\n");
128 return -1;
129 }
130 len /= 2;
131 if (hexstr2bin(pos, (u8 *) buf, len) < 0) {
132 printf("Invalid extra attribute hexstring\n");
133 return -1;
134 }
135 break;
136 case 'd':
137 val = htonl(atoi(attr->data));
138 os_memcpy(buf, &val, 4);
139 len = 4;
140 break;
141 default:
142 printf("Incorrect extra attribute syntax specification\n");
143 return -1;
144 }
145
146 if (!radius_msg_add_attr(msg, attr->type, (u8 *) buf, len)) {
147 printf("Could not add attribute %d\n", attr->type);
148 return -1;
149 }
150
151 return 0;
152}
153
154
155static int add_extra_attrs(struct radius_msg *msg,
156 struct extra_radius_attr *attrs)
157{
158 struct extra_radius_attr *p;
159 for (p = attrs; p; p = p->next) {
160 if (add_extra_attr(msg, p) < 0)
161 return -1;
162 }
163 return 0;
164}
165
166
167static struct extra_radius_attr *
168find_extra_attr(struct extra_radius_attr *attrs, u8 type)
169{
170 struct extra_radius_attr *p;
171 for (p = attrs; p; p = p->next) {
172 if (p->type == type)
173 return p;
174 }
175 return NULL;
176}
177
178
179static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
180 const u8 *eap, size_t len)
181{
182 struct radius_msg *msg;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800183 char buf[RADIUS_MAX_ATTR_LEN + 1];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 const struct eap_hdr *hdr;
185 const u8 *pos;
186
187 wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
188 "packet");
189
190 e->radius_identifier = radius_client_get_id(e->radius);
191 msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
192 e->radius_identifier);
193 if (msg == NULL) {
194 printf("Could not create net RADIUS packet\n");
195 return;
196 }
197
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800198 radius_msg_make_authenticator(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199
200 hdr = (const struct eap_hdr *) eap;
201 pos = (const u8 *) (hdr + 1);
202 if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
203 pos[0] == EAP_TYPE_IDENTITY) {
204 pos++;
205 os_free(e->eap_identity);
206 e->eap_identity_len = len - sizeof(*hdr) - 1;
207 e->eap_identity = os_malloc(e->eap_identity_len);
208 if (e->eap_identity) {
209 os_memcpy(e->eap_identity, pos, e->eap_identity_len);
210 wpa_hexdump(MSG_DEBUG, "Learned identity from "
211 "EAP-Response-Identity",
212 e->eap_identity, e->eap_identity_len);
213 }
214 }
215
216 if (e->eap_identity &&
217 !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
218 e->eap_identity, e->eap_identity_len)) {
219 printf("Could not add User-Name\n");
220 goto fail;
221 }
222
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700223 if (e->req_eap_key_name &&
224 !radius_msg_add_attr(msg, RADIUS_ATTR_EAP_KEY_NAME, (u8 *) "\0",
225 1)) {
226 printf("Could not add EAP-Key-Name\n");
227 goto fail;
228 }
229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_IP_ADDRESS) &&
231 !radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
232 (u8 *) &e->own_ip_addr, 4)) {
233 printf("Could not add NAS-IP-Address\n");
234 goto fail;
235 }
236
237 os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
238 MAC2STR(e->wpa_s->own_addr));
239 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CALLING_STATION_ID)
240 &&
241 !radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
242 (u8 *) buf, os_strlen(buf))) {
243 printf("Could not add Calling-Station-Id\n");
244 goto fail;
245 }
246
247 /* TODO: should probably check MTU from driver config; 2304 is max for
248 * IEEE 802.11, but use 1400 to avoid problems with too large packets
249 */
250 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_FRAMED_MTU) &&
251 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
252 printf("Could not add Framed-MTU\n");
253 goto fail;
254 }
255
256 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_NAS_PORT_TYPE) &&
257 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
258 RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
259 printf("Could not add NAS-Port-Type\n");
260 goto fail;
261 }
262
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800263 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_SERVICE_TYPE) &&
264 !radius_msg_add_attr_int32(msg, RADIUS_ATTR_SERVICE_TYPE,
265 RADIUS_SERVICE_TYPE_FRAMED)) {
266 printf("Could not add Service-Type\n");
267 goto fail;
268 }
269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
271 if (!find_extra_attr(e->extra_attrs, RADIUS_ATTR_CONNECT_INFO) &&
272 !radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
273 (u8 *) buf, os_strlen(buf))) {
274 printf("Could not add Connect-Info\n");
275 goto fail;
276 }
277
278 if (add_extra_attrs(msg, e->extra_attrs) < 0)
279 goto fail;
280
281 if (eap && !radius_msg_add_eap(msg, eap, len)) {
282 printf("Could not add EAP-Message\n");
283 goto fail;
284 }
285
286 /* State attribute must be copied if and only if this packet is
287 * Access-Request reply to the previous Access-Challenge */
288 if (e->last_recv_radius &&
289 radius_msg_get_hdr(e->last_recv_radius)->code ==
290 RADIUS_CODE_ACCESS_CHALLENGE) {
291 int res = radius_msg_copy_attr(msg, e->last_recv_radius,
292 RADIUS_ATTR_STATE);
293 if (res < 0) {
294 printf("Could not copy State attribute from previous "
295 "Access-Challenge\n");
296 goto fail;
297 }
298 if (res > 0) {
299 wpa_printf(MSG_DEBUG, " Copied RADIUS State "
300 "Attribute");
301 }
302 }
303
Dmitry Shmidt04949592012-07-19 12:16:46 -0700304 if (radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr)
305 < 0)
306 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 return;
308
309 fail:
310 radius_msg_free(msg);
311}
312
313
314static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
315 size_t len)
316{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
318 type, (unsigned long) len);
319 if (type == IEEE802_1X_TYPE_EAP_PACKET) {
320 wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
321 ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
322 }
323 return 0;
324}
325
326
327static void eapol_test_set_config_blob(void *ctx,
328 struct wpa_config_blob *blob)
329{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330 struct eapol_test_data *e = ctx;
331 wpa_config_set_blob(e->wpa_s->conf, blob);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332}
333
334
335static const struct wpa_config_blob *
336eapol_test_get_config_blob(void *ctx, const char *name)
337{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800338 struct eapol_test_data *e = ctx;
339 return wpa_config_get_blob(e->wpa_s->conf, name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340}
341
342
343static void eapol_test_eapol_done_cb(void *ctx)
344{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800345 struct eapol_test_data *e = ctx;
346
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 printf("WPA: EAPOL processing complete\n");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800348 wpa_supplicant_cancel_auth_timeout(e->wpa_s);
349 wpa_supplicant_set_state(e->wpa_s, WPA_COMPLETED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350}
351
352
353static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
354{
355 struct eapol_test_data *e = eloop_ctx;
356 printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
357 e->radius_access_accept_received = 0;
358 send_eap_request_identity(e->wpa_s, NULL);
359}
360
361
362static int eapol_test_compare_pmk(struct eapol_test_data *e)
363{
364 u8 pmk[PMK_LEN];
365 int ret = 1;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700366 const u8 *sess_id;
367 size_t sess_id_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368
369 if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
370 wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
371 if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
372 printf("WARNING: PMK mismatch\n");
373 wpa_hexdump(MSG_DEBUG, "PMK from AS",
374 e->authenticator_pmk, PMK_LEN);
375 } else if (e->radius_access_accept_received)
376 ret = 0;
377 } else if (e->authenticator_pmk_len == 16 &&
378 eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
379 wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
380 if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
381 printf("WARNING: PMK mismatch\n");
382 wpa_hexdump(MSG_DEBUG, "PMK from AS",
383 e->authenticator_pmk, 16);
384 } else if (e->radius_access_accept_received)
385 ret = 0;
386 } else if (e->radius_access_accept_received && e->no_mppe_keys) {
387 /* No keying material expected */
388 ret = 0;
389 }
390
391 if (ret && !e->no_mppe_keys)
392 e->num_mppe_mismatch++;
393 else if (!e->no_mppe_keys)
394 e->num_mppe_ok++;
395
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700396 sess_id = eapol_sm_get_session_id(e->wpa_s->eapol, &sess_id_len);
397 if (!sess_id)
398 return ret;
399 if (e->authenticator_eap_key_name_len == 0) {
400 wpa_printf(MSG_INFO, "No EAP-Key-Name received from server");
401 return ret;
402 }
403
404 if (e->authenticator_eap_key_name_len != sess_id_len ||
405 os_memcmp(e->authenticator_eap_key_name, sess_id, sess_id_len) != 0)
406 {
407 wpa_printf(MSG_INFO,
408 "Locally derived EAP Session-Id does not match EAP-Key-Name from server");
409 wpa_hexdump(MSG_DEBUG, "EAP Session-Id", sess_id, sess_id_len);
410 wpa_hexdump(MSG_DEBUG, "EAP-Key-Name from server",
411 e->authenticator_eap_key_name,
412 e->authenticator_eap_key_name_len);
413 } else {
414 wpa_printf(MSG_INFO,
415 "Locally derived EAP Session-Id matches EAP-Key-Name from server");
416 }
417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418 return ret;
419}
420
421
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800422static void eapol_sm_cb(struct eapol_sm *eapol, enum eapol_supp_result result,
423 void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424{
425 struct eapol_test_data *e = ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800426 printf("eapol_sm_cb: result=%d\n", result);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800427 e->id_req_sent = 0;
428 if (e->ctrl_iface)
429 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 e->eapol_test_num_reauths--;
431 if (e->eapol_test_num_reauths < 0)
432 eloop_terminate();
433 else {
434 eapol_test_compare_pmk(e);
435 eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
436 }
437}
438
439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800440static void eapol_test_write_cert(FILE *f, const char *subject,
441 const struct wpabuf *cert)
442{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800443 char *encoded;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800444
445 encoded = base64_encode(wpabuf_head(cert), wpabuf_len(cert), NULL);
446 if (encoded == NULL)
447 return;
448 fprintf(f, "%s\n-----BEGIN CERTIFICATE-----\n%s"
449 "-----END CERTIFICATE-----\n\n", subject, encoded);
450 os_free(encoded);
451}
452
453
Dmitry Shmidt051af732013-10-22 13:52:46 -0700454#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
455static void eapol_test_eap_param_needed(void *ctx, enum wpa_ctrl_req_type field,
456 const char *default_txt)
457{
458 struct eapol_test_data *e = ctx;
459 struct wpa_supplicant *wpa_s = e->wpa_s;
460 struct wpa_ssid *ssid = wpa_s->current_ssid;
461 const char *field_name, *txt = NULL;
462 char *buf;
463 size_t buflen;
464 int len;
465
466 if (ssid == NULL)
467 return;
468
469 field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
470 &txt);
471 if (field_name == NULL) {
472 wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
473 field);
474 return;
475 }
476
477 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
478 buf = os_malloc(buflen);
479 if (buf == NULL)
480 return;
481 len = os_snprintf(buf, buflen,
482 WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
483 field_name, ssid->id, txt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800484 if (os_snprintf_error(buflen, len)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700485 os_free(buf);
486 return;
487 }
488 if (ssid->ssid && buflen > len + ssid->ssid_len) {
489 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
490 len += ssid->ssid_len;
491 buf[len] = '\0';
492 }
493 buf[buflen - 1] = '\0';
494 wpa_msg(wpa_s, MSG_INFO, "%s", buf);
495 os_free(buf);
496}
497#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
498#define eapol_test_eap_param_needed NULL
499#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
500
501
Hai Shalom81f62d82019-07-22 12:10:00 -0700502static void eapol_test_cert_cb(void *ctx, struct tls_cert_data *cert,
503 const char *cert_hash)
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700504{
505 struct eapol_test_data *e = ctx;
Hai Shalom81f62d82019-07-22 12:10:00 -0700506 int i;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700507
508 wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
509 "depth=%d subject='%s'%s%s",
Hai Shalom81f62d82019-07-22 12:10:00 -0700510 cert->depth, cert->subject,
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700511 cert_hash ? " hash=" : "",
512 cert_hash ? cert_hash : "");
513
Hai Shalom81f62d82019-07-22 12:10:00 -0700514 if (cert->cert) {
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700515 char *cert_hex;
Hai Shalom81f62d82019-07-22 12:10:00 -0700516 size_t len = wpabuf_len(cert->cert) * 2 + 1;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700517 cert_hex = os_malloc(len);
518 if (cert_hex) {
Hai Shalom81f62d82019-07-22 12:10:00 -0700519 wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert->cert),
520 wpabuf_len(cert->cert));
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700521 wpa_msg_ctrl(e->wpa_s, MSG_INFO,
522 WPA_EVENT_EAP_PEER_CERT
523 "depth=%d subject='%s' cert=%s",
Hai Shalom81f62d82019-07-22 12:10:00 -0700524 cert->depth, cert->subject, cert_hex);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700525 os_free(cert_hex);
526 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800527
528 if (e->server_cert_file)
529 eapol_test_write_cert(e->server_cert_file,
Hai Shalom81f62d82019-07-22 12:10:00 -0700530 cert->subject, cert->cert);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700531 }
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800532
Hai Shalom81f62d82019-07-22 12:10:00 -0700533 for (i = 0; i < cert->num_altsubject; i++)
534 wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_ALT
535 "depth=%d %s", cert->depth, cert->altsubject[i]);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700536}
537
538
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700539static void eapol_test_set_anon_id(void *ctx, const u8 *id, size_t len)
540{
541 struct eapol_test_data *e = ctx;
542 struct wpa_supplicant *wpa_s = e->wpa_s;
543 char *str;
544 int res;
545
546 wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
547 id, len);
548
549 if (wpa_s->current_ssid == NULL)
550 return;
551
552 if (id == NULL) {
553 if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
554 "NULL", 0) < 0)
555 return;
556 } else {
557 str = os_malloc(len * 2 + 1);
558 if (str == NULL)
559 return;
560 wpa_snprintf_hex(str, len * 2 + 1, id, len);
561 res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
562 str, 0);
563 os_free(str);
564 if (res < 0)
565 return;
566 }
567}
568
569
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800570static enum wpa_states eapol_test_get_state(void *ctx)
571{
572 struct eapol_test_data *e = ctx;
573 struct wpa_supplicant *wpa_s = e->wpa_s;
574
575 return wpa_s->wpa_state;
576}
577
578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
580 struct wpa_ssid *ssid)
581{
582 struct eapol_config eapol_conf;
583 struct eapol_ctx *ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800584 struct wpa_sm_ctx *wctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585
586 ctx = os_zalloc(sizeof(*ctx));
587 if (ctx == NULL) {
588 printf("Failed to allocate EAPOL context.\n");
589 return -1;
590 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800591 ctx->ctx = e;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 ctx->msg_ctx = wpa_s;
593 ctx->scard_ctx = wpa_s->scard;
594 ctx->cb = eapol_sm_cb;
595 ctx->cb_ctx = e;
596 ctx->eapol_send_ctx = wpa_s;
597 ctx->preauth = 0;
598 ctx->eapol_done_cb = eapol_test_eapol_done_cb;
599 ctx->eapol_send = eapol_test_eapol_send;
600 ctx->set_config_blob = eapol_test_set_config_blob;
601 ctx->get_config_blob = eapol_test_get_config_blob;
602 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
603 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
604 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800605 ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700606 ctx->eap_param_needed = eapol_test_eap_param_needed;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700607 ctx->cert_cb = eapol_test_cert_cb;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800608 ctx->cert_in_cb = 1;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700609 ctx->set_anon_id = eapol_test_set_anon_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610
611 wpa_s->eapol = eapol_sm_init(ctx);
612 if (wpa_s->eapol == NULL) {
613 os_free(ctx);
614 printf("Failed to initialize EAPOL state machines.\n");
615 return -1;
616 }
617
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
619 wctx = os_zalloc(sizeof(*wctx));
620 if (wctx == NULL) {
621 os_free(ctx);
622 return -1;
623 }
624 wctx->ctx = e;
625 wctx->msg_ctx = wpa_s;
626 wctx->get_state = eapol_test_get_state;
627 wpa_s->wpa = wpa_sm_init(wctx);
628 if (!wpa_s->wpa) {
629 os_free(ctx);
630 os_free(wctx);
631 return -1;
632 }
633
634 if (!ssid)
635 return 0;
636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 wpa_s->current_ssid = ssid;
638 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
639 eapol_conf.accept_802_1x_keys = 1;
640 eapol_conf.required_keys = 0;
641 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
642 eapol_conf.workaround = ssid->eap_workaround;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700643 eapol_conf.external_sim = wpa_s->conf->external_sim;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
645 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
646
647
Hai Shalome21d4e82020-04-29 16:34:06 -0700648 eapol_sm_notify_portValid(wpa_s->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 /* 802.1X::portControl = Auto */
Hai Shalome21d4e82020-04-29 16:34:06 -0700650 eapol_sm_notify_portEnabled(wpa_s->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651
652 return 0;
653}
654
655
656static void test_eapol_clean(struct eapol_test_data *e,
657 struct wpa_supplicant *wpa_s)
658{
659 struct extra_radius_attr *p, *prev;
660
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800661 wpa_sm_deinit(wpa_s->wpa);
662 wpa_s->wpa = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 radius_client_deinit(e->radius);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700664 wpabuf_free(e->last_eap_radius);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665 radius_msg_free(e->last_recv_radius);
666 e->last_recv_radius = NULL;
667 os_free(e->eap_identity);
668 e->eap_identity = NULL;
669 eapol_sm_deinit(wpa_s->eapol);
670 wpa_s->eapol = NULL;
671 if (e->radius_conf && e->radius_conf->auth_server) {
672 os_free(e->radius_conf->auth_server->shared_secret);
Sunil Ravi99c035e2024-07-12 01:42:03 +0000673 os_free(e->radius_conf->auth_server->ca_cert);
674 os_free(e->radius_conf->auth_server->client_cert);
675 os_free(e->radius_conf->auth_server->private_key);
676 os_free(e->radius_conf->auth_server->private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677 os_free(e->radius_conf->auth_server);
678 }
679 os_free(e->radius_conf);
680 e->radius_conf = NULL;
681 scard_deinit(wpa_s->scard);
Jouni Malinenf3f8d3c2021-02-05 00:28:17 +0200682 wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface);
683 wpa_s->ctrl_iface = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700684
685 ext_password_deinit(wpa_s->ext_pw);
686 wpa_s->ext_pw = NULL;
687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 wpa_config_free(wpa_s->conf);
689
690 p = e->extra_attrs;
691 while (p) {
692 prev = p;
693 p = p->next;
694 os_free(prev);
695 }
696}
697
698
699static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
700{
701 struct wpa_supplicant *wpa_s = eloop_ctx;
702 u8 buf[100], *pos;
703 struct ieee802_1x_hdr *hdr;
704 struct eap_hdr *eap;
705
706 hdr = (struct ieee802_1x_hdr *) buf;
707 hdr->version = EAPOL_VERSION;
708 hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
709 hdr->length = htons(5);
710
711 eap = (struct eap_hdr *) (hdr + 1);
712 eap->code = EAP_CODE_REQUEST;
Hai Shalom74f70d42019-02-11 14:42:39 -0800713 if (os_get_random((u8 *) &eap->identifier, sizeof(eap->identifier)) < 0)
714 eap->identifier = os_random() & 0xff;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 eap->length = htons(5);
716 pos = (u8 *) (eap + 1);
717 *pos = EAP_TYPE_IDENTITY;
718
719 printf("Sending fake EAP-Request-Identity\n");
720 eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
Sunil8cd6f4d2022-06-28 18:40:46 +0000721 sizeof(*hdr) + 5, FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722}
723
724
725static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
726{
727 struct eapol_test_data *e = eloop_ctx;
728 printf("EAPOL test timed out\n");
729 e->auth_timed_out = 1;
730 eloop_terminate();
731}
732
733
734static char *eap_type_text(u8 type)
735{
736 switch (type) {
737 case EAP_TYPE_IDENTITY: return "Identity";
738 case EAP_TYPE_NOTIFICATION: return "Notification";
739 case EAP_TYPE_NAK: return "Nak";
Sunil Ravia04bd252022-05-02 22:54:18 -0700740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 case EAP_TYPE_MD5: return "MD5";
742 case EAP_TYPE_OTP: return "OTP";
Sunil Ravia04bd252022-05-02 22:54:18 -0700743 case EAP_TYPE_GTC: return "GTC";
744 case EAP_TYPE_TLS: return "TLS";
745 case EAP_TYPE_LEAP: return "LEAP";
746 case EAP_TYPE_SIM: return "SIM";
747 case EAP_TYPE_TTLS: return "TTLS";
748 case EAP_TYPE_AKA: return "AKA";
749 case EAP_TYPE_PEAP: return "PEAP";
750 case EAP_TYPE_MSCHAPV2: return "MSCHAPv2";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 case EAP_TYPE_FAST: return "FAST";
Sunil Ravia04bd252022-05-02 22:54:18 -0700752 case EAP_TYPE_PAX: return "PAX";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 case EAP_TYPE_PSK: return "PSK";
Sunil Ravia04bd252022-05-02 22:54:18 -0700754 case EAP_TYPE_SAKE: return "SAKE";
755 case EAP_TYPE_IKEV2: return "IKEv2";
756 case EAP_TYPE_AKA_PRIME: return "AKA-PRIME";
757 case EAP_TYPE_GPSK: return "GPSK";
758 case EAP_TYPE_PWD: return "PWD";
759 case EAP_TYPE_EKE: return "EKE";
760 case EAP_TYPE_TEAP: return "TEAP";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 default: return "Unknown";
762 }
763}
764
765
766static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
767{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700768 struct wpabuf *eap;
769 const struct eap_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 int eap_type = -1;
771 char buf[64];
772 struct radius_msg *msg;
773
774 if (e->last_recv_radius == NULL)
775 return;
776
777 msg = e->last_recv_radius;
778
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700779 eap = radius_msg_get_eap(msg);
Sunil Ravia04bd252022-05-02 22:54:18 -0700780 if (!eap) {
781 /* RFC 3579, Chap. 2.6.3:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
783 * attribute */
Sunil Ravia04bd252022-05-02 22:54:18 -0700784 wpa_printf(MSG_DEBUG,
785 "could not extract EAP-Message from RADIUS message");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700786 wpabuf_free(e->last_eap_radius);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787 e->last_eap_radius = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788 return;
789 }
790
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700791 if (wpabuf_len(eap) < sizeof(*hdr)) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700792 wpa_printf(MSG_DEBUG,
793 "too short EAP packet received from authentication server");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700794 wpabuf_free(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 return;
796 }
797
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700798 if (wpabuf_len(eap) > sizeof(*hdr))
799 eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700801 hdr = wpabuf_head(eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 switch (hdr->code) {
803 case EAP_CODE_REQUEST:
804 os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
805 eap_type >= 0 ? eap_type_text(eap_type) : "??",
806 eap_type);
807 break;
808 case EAP_CODE_RESPONSE:
809 os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
810 eap_type >= 0 ? eap_type_text(eap_type) : "??",
811 eap_type);
812 break;
813 case EAP_CODE_SUCCESS:
814 os_strlcpy(buf, "EAP Success", sizeof(buf));
815 /* LEAP uses EAP Success within an authentication, so must not
816 * stop here with eloop_terminate(); */
817 break;
818 case EAP_CODE_FAILURE:
819 os_strlcpy(buf, "EAP Failure", sizeof(buf));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800820 if (e->ctrl_iface)
821 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 eloop_terminate();
823 break;
824 default:
825 os_strlcpy(buf, "unknown EAP code", sizeof(buf));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700826 wpa_hexdump_buf(MSG_DEBUG, "Decapsulated EAP packet", eap);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 break;
828 }
Sunil Ravia04bd252022-05-02 22:54:18 -0700829 buf[sizeof(buf) - 1] = '\0';
830 wpa_printf(MSG_DEBUG,
831 "decapsulated EAP packet (code=%d id=%d len=%d) from RADIUS server: %s",
832 hdr->code, hdr->identifier, be_to_host16(hdr->length),
833 buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700835 wpabuf_free(e->last_eap_radius);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 e->last_eap_radius = eap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837
838 {
839 struct ieee802_1x_hdr *dot1x;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700840 dot1x = os_malloc(sizeof(*dot1x) + wpabuf_len(eap));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 assert(dot1x != NULL);
842 dot1x->version = EAPOL_VERSION;
843 dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700844 dot1x->length = htons(wpabuf_len(eap));
845 os_memcpy((u8 *) (dot1x + 1), wpabuf_head(eap),
846 wpabuf_len(eap));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700848 (u8 *) dot1x,
Sunil8cd6f4d2022-06-28 18:40:46 +0000849 sizeof(*dot1x) + wpabuf_len(eap),
850 FRAME_ENCRYPTION_UNKNOWN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 os_free(dot1x);
852 }
853}
854
855
856static void ieee802_1x_get_keys(struct eapol_test_data *e,
857 struct radius_msg *msg, struct radius_msg *req,
858 const u8 *shared_secret,
859 size_t shared_secret_len)
860{
861 struct radius_ms_mppe_keys *keys;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700862 u8 *buf;
863 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864
865 keys = radius_msg_get_ms_keys(msg, req, shared_secret,
866 shared_secret_len);
Sunil Ravia04bd252022-05-02 22:54:18 -0700867 if (keys && !keys->send && !keys->recv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 os_free(keys);
869 keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
870 shared_secret_len);
871 }
872
873 if (keys) {
874 if (keys->send) {
875 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
876 keys->send, keys->send_len);
877 }
878 if (keys->recv) {
879 wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
880 keys->recv, keys->recv_len);
881 e->authenticator_pmk_len =
882 keys->recv_len > PMK_LEN ? PMK_LEN :
883 keys->recv_len;
884 os_memcpy(e->authenticator_pmk, keys->recv,
885 e->authenticator_pmk_len);
886 if (e->authenticator_pmk_len == 16 && keys->send &&
887 keys->send_len == 16) {
888 /* MS-CHAP-v2 derives 16 octet keys */
889 wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
890 "to extend PMK to 32 octets");
891 os_memcpy(e->authenticator_pmk +
892 e->authenticator_pmk_len,
893 keys->send, keys->send_len);
894 e->authenticator_pmk_len += keys->send_len;
895 }
896 }
897
898 os_free(keys->send);
899 os_free(keys->recv);
900 os_free(keys);
901 }
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700902
903 if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
904 NULL) == 0) {
905 os_memcpy(e->authenticator_eap_key_name, buf, len);
906 e->authenticator_eap_key_name_len = len;
907 } else {
908 e->authenticator_eap_key_name_len = 0;
909 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910}
911
912
913/* Process the RADIUS frames from Authentication Server */
914static RadiusRxResult
915ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
916 const u8 *shared_secret, size_t shared_secret_len,
917 void *data)
918{
919 struct eapol_test_data *e = data;
920 struct radius_hdr *hdr = radius_msg_get_hdr(msg);
921
922 /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
923 * present when packet contains an EAP-Message attribute */
924 if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
925 radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
926 0) < 0 &&
927 radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700928 wpa_printf(MSG_DEBUG,
929 "Allowing RADIUS Access-Reject without Message-Authenticator since it does not include EAP-Message");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
931 req, 1)) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700932 wpa_printf(MSG_INFO,
933 "Incoming RADIUS packet did not have correct Message-Authenticator - dropped");
934 return RADIUS_RX_INVALID_AUTHENTICATOR;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 }
936
937 if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
938 hdr->code != RADIUS_CODE_ACCESS_REJECT &&
939 hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700940 wpa_printf(MSG_INFO, "Unknown RADIUS message code");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 return RADIUS_RX_UNKNOWN;
942 }
943
944 e->radius_identifier = -1;
945 wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
946
947 radius_msg_free(e->last_recv_radius);
948 e->last_recv_radius = msg;
949
950 switch (hdr->code) {
951 case RADIUS_CODE_ACCESS_ACCEPT:
952 e->radius_access_accept_received = 1;
953 ieee802_1x_get_keys(e, msg, req, shared_secret,
954 shared_secret_len);
955 break;
956 case RADIUS_CODE_ACCESS_REJECT:
957 e->radius_access_reject_received = 1;
958 break;
959 }
960
961 ieee802_1x_decapsulate_radius(e);
962
963 if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
964 e->eapol_test_num_reauths < 0) ||
965 hdr->code == RADIUS_CODE_ACCESS_REJECT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800966 if (!e->ctrl_iface)
967 eloop_terminate();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 }
969
970 return RADIUS_RX_QUEUED;
971}
972
973
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800974static int driver_get_ssid(void *priv, u8 *ssid)
975{
976 ssid[0] = 0;
977 return 0;
978}
979
980
981static int driver_get_bssid(void *priv, u8 *bssid)
982{
983 struct eapol_test_data *e = priv;
984
985 if (e->ctrl_iface && !e->id_req_sent) {
986 eloop_register_timeout(0, 0, send_eap_request_identity,
987 e->wpa_s, NULL);
988 e->id_req_sent = 1;
989 }
990
991 os_memset(bssid, 0, ETH_ALEN);
992 bssid[5] = 1;
993 return 0;
994}
995
996
997static int driver_get_capa(void *priv, struct wpa_driver_capa *capa)
998{
999 os_memset(capa, 0, sizeof(*capa));
1000 capa->flags = WPA_DRIVER_FLAGS_WIRED;
1001 return 0;
1002}
1003
1004
1005struct wpa_driver_ops eapol_test_drv_ops = {
1006 .name = "test",
1007 .get_ssid = driver_get_ssid,
1008 .get_bssid = driver_get_bssid,
1009 .get_capa = driver_get_capa,
1010};
1011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012static void wpa_init_conf(struct eapol_test_data *e,
1013 struct wpa_supplicant *wpa_s, const char *authsrv,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001014 int port, bool tls, const char *secret,
1015 const char *ca_cert, const char *client_cert,
1016 const char *private_key,
1017 const char *private_key_passwd,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001018 const char *cli_addr, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019{
1020 struct hostapd_radius_server *as;
1021 int res;
1022
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001023 wpa_s->driver = &eapol_test_drv_ops;
1024 wpa_s->drv_priv = e;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 wpa_s->bssid[5] = 1;
1026 os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
1027 e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001028 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029
1030 e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
1031 assert(e->radius_conf != NULL);
1032 e->radius_conf->num_auth_servers = 1;
1033 as = os_zalloc(sizeof(struct hostapd_radius_server));
1034 assert(as != NULL);
1035#if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
1036 {
1037 int a[4];
1038 u8 *pos;
1039 sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
1040 pos = (u8 *) &as->addr.u.v4;
1041 *pos++ = a[0];
1042 *pos++ = a[1];
1043 *pos++ = a[2];
1044 *pos++ = a[3];
Hai Shaloma20dcd72022-02-04 13:43:00 -08001045 as->addr.af = AF_INET;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 }
1047#else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001048 if (hostapd_parse_ip_addr(authsrv, &as->addr) < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001049 wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1050 authsrv);
1051 assert(0);
1052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053#endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 as->port = port;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001055 as->tls = tls;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 as->shared_secret = (u8 *) os_strdup(secret);
1057 as->shared_secret_len = os_strlen(secret);
Sunil Ravi99c035e2024-07-12 01:42:03 +00001058 if (ca_cert)
1059 as->ca_cert = os_strdup(ca_cert);
1060 if (client_cert)
1061 as->client_cert = os_strdup(client_cert);
1062 if (private_key)
1063 as->private_key = os_strdup(private_key);
1064 if (private_key_passwd)
1065 as->private_key_passwd = os_strdup(private_key_passwd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 e->radius_conf->auth_server = as;
1067 e->radius_conf->auth_servers = as;
1068 e->radius_conf->msg_dumps = 1;
1069 if (cli_addr) {
1070 if (hostapd_parse_ip_addr(cli_addr,
1071 &e->radius_conf->client_addr) == 0)
1072 e->radius_conf->force_client_addr = 1;
1073 else {
1074 wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
1075 cli_addr);
1076 assert(0);
1077 }
1078 }
1079
1080 e->radius = radius_client_init(wpa_s, e->radius_conf);
1081 assert(e->radius != NULL);
1082
1083 res = radius_client_register(e->radius, RADIUS_AUTH,
1084 ieee802_1x_receive_auth, e);
1085 assert(res == 0);
1086}
1087
1088
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001089static int scard_test(struct eapol_test_data *e)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090{
1091 struct scard_data *scard;
1092 size_t len;
1093 char imsi[20];
1094 unsigned char _rand[16];
1095#ifdef PCSC_FUNCS
1096 unsigned char sres[4];
1097 unsigned char kc[8];
1098#endif /* PCSC_FUNCS */
1099#define num_triplets 5
1100 unsigned char rand_[num_triplets][16];
1101 unsigned char sres_[num_triplets][4];
1102 unsigned char kc_[num_triplets][8];
1103 int i, res;
1104 size_t j;
1105
1106#define AKA_RAND_LEN 16
1107#define AKA_AUTN_LEN 16
1108#define AKA_AUTS_LEN 14
1109#define RES_MAX_LEN 16
1110#define IK_LEN 16
1111#define CK_LEN 16
1112 unsigned char aka_rand[AKA_RAND_LEN];
1113 unsigned char aka_autn[AKA_AUTN_LEN];
1114 unsigned char aka_auts[AKA_AUTS_LEN];
1115 unsigned char aka_res[RES_MAX_LEN];
1116 size_t aka_res_len;
1117 unsigned char aka_ik[IK_LEN];
1118 unsigned char aka_ck[CK_LEN];
1119
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001120 scard = scard_init(e->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 if (scard == NULL)
1122 return -1;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001123 if (scard_set_pin(scard, e->pcsc_pin)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 wpa_printf(MSG_WARNING, "PIN validation failed");
1125 scard_deinit(scard);
1126 return -1;
1127 }
1128
1129 len = sizeof(imsi);
1130 if (scard_get_imsi(scard, imsi, &len))
1131 goto failed;
1132 wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
1133 /* NOTE: Permanent Username: 1 | IMSI */
1134
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001135 wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
1136 scard_get_mnc_len(scard));
1137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 os_memset(_rand, 0, sizeof(_rand));
1139 if (scard_gsm_auth(scard, _rand, sres, kc))
1140 goto failed;
1141
1142 os_memset(_rand, 0xff, sizeof(_rand));
1143 if (scard_gsm_auth(scard, _rand, sres, kc))
1144 goto failed;
1145
1146 for (i = 0; i < num_triplets; i++) {
1147 os_memset(rand_[i], i, sizeof(rand_[i]));
1148 if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
1149 goto failed;
1150 }
1151
1152 for (i = 0; i < num_triplets; i++) {
1153 printf("1");
1154 for (j = 0; j < len; j++)
1155 printf("%c", imsi[j]);
1156 printf(",");
1157 for (j = 0; j < 16; j++)
1158 printf("%02X", rand_[i][j]);
1159 printf(",");
1160 for (j = 0; j < 4; j++)
1161 printf("%02X", sres_[i][j]);
1162 printf(",");
1163 for (j = 0; j < 8; j++)
1164 printf("%02X", kc_[i][j]);
1165 printf("\n");
1166 }
1167
1168 wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
1169
1170 /* seq 39 (0x28) */
1171 os_memset(aka_rand, 0xaa, 16);
1172 os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
1173 "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
1174
1175 res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
1176 aka_ik, aka_ck, aka_auts);
1177 if (res == 0) {
1178 wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
1179 wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
1180 wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
1181 wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
1182 } else if (res == -2) {
1183 wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
1184 "failure");
1185 wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
1186 } else {
1187 wpa_printf(MSG_DEBUG, "UMTS auth failed");
1188 }
1189
1190failed:
1191 scard_deinit(scard);
1192
1193 return 0;
1194#undef num_triplets
1195}
1196
1197
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001198static int scard_get_triplets(struct eapol_test_data *e, int argc, char *argv[])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199{
1200 struct scard_data *scard;
1201 size_t len;
1202 char imsi[20];
1203 unsigned char _rand[16];
1204 unsigned char sres[4];
1205 unsigned char kc[8];
1206 int num_triplets;
1207 int i;
1208 size_t j;
1209
1210 if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
1211 printf("invalid parameters for sim command\n");
1212 return -1;
1213 }
1214
1215 if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
1216 /* disable debug output */
1217 wpa_debug_level = 99;
1218 }
1219
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001220 scard = scard_init(e->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 if (scard == NULL) {
1222 printf("Failed to open smartcard connection\n");
1223 return -1;
1224 }
1225 if (scard_set_pin(scard, argv[0])) {
1226 wpa_printf(MSG_WARNING, "PIN validation failed");
1227 scard_deinit(scard);
1228 return -1;
1229 }
1230
1231 len = sizeof(imsi);
1232 if (scard_get_imsi(scard, imsi, &len)) {
1233 scard_deinit(scard);
1234 return -1;
1235 }
1236
1237 for (i = 0; i < num_triplets; i++) {
1238 os_memset(_rand, i, sizeof(_rand));
1239 if (scard_gsm_auth(scard, _rand, sres, kc))
1240 break;
1241
1242 /* IMSI:Kc:SRES:RAND */
1243 for (j = 0; j < len; j++)
1244 printf("%c", imsi[j]);
1245 printf(":");
1246 for (j = 0; j < 8; j++)
1247 printf("%02X", kc[j]);
1248 printf(":");
1249 for (j = 0; j < 4; j++)
1250 printf("%02X", sres[j]);
1251 printf(":");
1252 for (j = 0; j < 16; j++)
1253 printf("%02X", _rand[j]);
1254 printf("\n");
1255 }
1256
1257 scard_deinit(scard);
1258
1259 return 0;
1260}
1261
1262
1263static void eapol_test_terminate(int sig, void *signal_ctx)
1264{
1265 struct wpa_supplicant *wpa_s = signal_ctx;
1266 wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
1267 eloop_terminate();
1268}
1269
1270
1271static void usage(void)
1272{
1273 printf("usage:\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001274 "eapol_test [-enWSv] -c<conf> [-a<AS IP>] [-p<AS port>] "
Sunil Ravi99c035e2024-07-12 01:42:03 +00001275 "[-s<AS secret>] \\\n"
1276 " [-X<RADIUS protocol> \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001278 " [-M<client MAC address>] [-o<server cert file] \\\n"
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001279 " [-N<attr spec>] [-R<PC/SC reader>] "
1280 "[-P<PC/SC PIN>] \\\n"
Sunil Ravi99c035e2024-07-12 01:42:03 +00001281#ifdef CONFIG_RADIUS_TLS
1282 " [-j<CA cert>] [-J<client cert>] \\\n"
1283 "[-k<private key] [-K<private key passwd>] \\\n"
1284#endif /* CONFIG_RADIUS_TLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001285 " [-A<client IP>] [-i<ifname>] [-T<ctrl_iface>]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 "eapol_test scard\n"
1287 "eapol_test sim <PIN> <num triplets> [debug]\n"
1288 "\n");
1289 printf("options:\n"
1290 " -c<conf> = configuration file\n"
1291 " -a<AS IP> = IP address of the authentication server, "
1292 "default 127.0.0.1\n"
Sunil Ravi99c035e2024-07-12 01:42:03 +00001293 " -p<AS port> = Port of the authentication server,\n"
1294 " default 1812 for RADIUS/UDP and 2083 for RADIUS/TLS\n"
1295 " -s<AS secret> = shared secret with the authentication server,\n"
1296 " default 'radius' for RADIUS/UDP and 'radsec' for RADIUS/TLS\n"
1297 " -X<RADIUS protocol> = RADIUS protocol to use: UDP (default) or TLS\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298 " -A<client IP> = IP address of the client, default: select "
1299 "automatically\n"
1300 " -r<count> = number of re-authentications\n"
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07001301 " -e = Request EAP-Key-Name\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 " -W = wait for a control interface monitor before starting\n"
1303 " -S = save configuration after authentication\n"
1304 " -n = no MPPE keys expected\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001305 " -v = show version\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
1307 " -C<Connect-Info> = RADIUS Connect-Info (default: "
1308 "CONNECT 11Mbps 802.11b)\n"
1309 " -M<client MAC address> = Set own MAC address "
1310 "(Calling-Station-Id,\n"
1311 " default: 02:00:00:00:00:01)\n"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001312 " -o<server cert file> = Write received server certificate\n"
1313 " chain to the specified file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 " -N<attr spec> = send arbitrary attribute specified by:\n"
1315 " attr_id:syntax:value or attr_id\n"
1316 " attr_id - number id of the attribute\n"
1317 " syntax - one of: s, d, x\n"
1318 " s = string\n"
1319 " d = integer\n"
1320 " x = octet string\n"
1321 " value - attribute value.\n"
1322 " When only attr_id is specified, NULL will be used as "
1323 "value.\n"
1324 " Multiple attributes can be specified by using the "
1325 "option several times.\n");
1326}
1327
1328
1329int main(int argc, char *argv[])
1330{
Dmitry Shmidt051af732013-10-22 13:52:46 -07001331 struct wpa_global global;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 struct wpa_supplicant wpa_s;
1333 int c, ret = 1, wait_for_monitor = 0, save_config = 0;
1334 char *as_addr = "127.0.0.1";
Sunil Ravi99c035e2024-07-12 01:42:03 +00001335 int as_port = -1;
1336 char *as_secret = NULL;
1337 char *ca_cert = NULL, *client_cert = NULL;
1338 char *private_key = NULL, *private_key_passwd = NULL;
1339 bool tls = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 char *cli_addr = NULL;
1341 char *conf = NULL;
1342 int timeout = 30;
1343 char *pos;
1344 struct extra_radius_attr *p = NULL, *p1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001345 const char *ifname = "test";
1346 const char *ctrl_iface = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347
1348 if (os_program_init())
1349 return -1;
1350
1351 hostapd_logger_register_cb(hostapd_logger_cb);
1352
1353 os_memset(&eapol_test, 0, sizeof(eapol_test));
1354 eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
1355 os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001356 eapol_test.pcsc_pin = "1234";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357
1358 wpa_debug_level = 0;
1359 wpa_debug_show_keys = 1;
1360
1361 for (;;) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001362 c = getopt(argc, argv,
1363 "a:A:c:C:ei:j:J:k:K:M:nN:o:p:P:r:R:s:St:T:vWX:");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 if (c < 0)
1365 break;
1366 switch (c) {
1367 case 'a':
1368 as_addr = optarg;
1369 break;
1370 case 'A':
1371 cli_addr = optarg;
1372 break;
1373 case 'c':
1374 conf = optarg;
1375 break;
1376 case 'C':
1377 eapol_test.connect_info = optarg;
1378 break;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07001379 case 'e':
1380 eapol_test.req_eap_key_name = 1;
1381 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001382 case 'i':
1383 ifname = optarg;
1384 break;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001385#ifdef CONFIG_RADIUS_TLS
1386 case 'j':
1387 ca_cert = optarg;
1388 break;
1389 case 'J':
1390 client_cert = optarg;
1391 break;
1392 case 'k':
1393 private_key = optarg;
1394 break;
1395 case 'K':
1396 private_key_passwd = optarg;
1397 break;
1398#endif /* CONFIG_RADIUS_TLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 case 'M':
1400 if (hwaddr_aton(optarg, eapol_test.own_addr)) {
1401 usage();
1402 return -1;
1403 }
1404 break;
1405 case 'n':
1406 eapol_test.no_mppe_keys++;
1407 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408 case 'o':
1409 if (eapol_test.server_cert_file)
1410 fclose(eapol_test.server_cert_file);
1411 eapol_test.server_cert_file = fopen(optarg, "w");
1412 if (eapol_test.server_cert_file == NULL) {
1413 printf("Could not open '%s' for writing\n",
1414 optarg);
1415 return -1;
1416 }
1417 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 case 'p':
1419 as_port = atoi(optarg);
1420 break;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001421 case 'P':
1422 eapol_test.pcsc_pin = optarg;
1423 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 case 'r':
1425 eapol_test.eapol_test_num_reauths = atoi(optarg);
1426 break;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001427 case 'R':
1428 eapol_test.pcsc_reader = optarg;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001429 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 case 's':
1431 as_secret = optarg;
1432 break;
1433 case 'S':
1434 save_config++;
1435 break;
1436 case 't':
1437 timeout = atoi(optarg);
1438 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001439 case 'T':
1440 ctrl_iface = optarg;
1441 eapol_test.ctrl_iface = 1;
1442 break;
1443 case 'v':
Hai Shalomfdcde762020-04-02 11:19:20 -07001444 printf("eapol_test v%s\n", VERSION_STR);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001445 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 case 'W':
1447 wait_for_monitor++;
1448 break;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001449 case 'X':
1450 if (os_strcmp(optarg, "UDP") == 0) {
1451 tls = false;
1452 } else if (os_strcmp(optarg, "TLS") == 0) {
1453 tls = true;
1454 } else {
1455 usage();
1456 return -1;
1457 }
1458 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459 case 'N':
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001460 p1 = os_zalloc(sizeof(*p1));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 if (p1 == NULL)
1462 break;
1463 if (!p)
1464 eapol_test.extra_attrs = p1;
1465 else
1466 p->next = p1;
1467 p = p1;
1468
1469 p->type = atoi(optarg);
1470 pos = os_strchr(optarg, ':');
1471 if (pos == NULL) {
1472 p->syntax = 'n';
1473 p->data = NULL;
1474 break;
1475 }
1476
1477 pos++;
1478 if (pos[0] == '\0' || pos[1] != ':') {
1479 printf("Incorrect format of attribute "
1480 "specification\n");
1481 break;
1482 }
1483
1484 p->syntax = pos[0];
1485 p->data = pos + 2;
1486 break;
1487 default:
1488 usage();
1489 return -1;
1490 }
1491 }
1492
Sunil Ravi99c035e2024-07-12 01:42:03 +00001493 if (!as_secret)
1494 as_secret = tls ? "radsec" : "radius";
1495 if (as_port < 0)
1496 as_port = tls ? 2083 : 1812;
1497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001499 return scard_test(&eapol_test);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 }
1501
1502 if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001503 return scard_get_triplets(&eapol_test, argc - optind - 1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 &argv[optind + 1]);
1505 }
1506
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001507 if (conf == NULL && !ctrl_iface) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 usage();
1509 printf("Configuration file is required.\n");
1510 return -1;
1511 }
1512
1513 if (eap_register_methods()) {
1514 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1515 return -1;
1516 }
1517
1518 if (eloop_init()) {
1519 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1520 return -1;
1521 }
1522
Dmitry Shmidt051af732013-10-22 13:52:46 -07001523 os_memset(&global, 0, sizeof(global));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 os_memset(&wpa_s, 0, sizeof(wpa_s));
Dmitry Shmidt051af732013-10-22 13:52:46 -07001525 wpa_s.global = &global;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 eapol_test.wpa_s = &wpa_s;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001527 dl_list_init(&wpa_s.bss);
1528 dl_list_init(&wpa_s.bss_id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001529 if (conf)
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001530 wpa_s.conf = wpa_config_read(conf, NULL, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001531 else
1532 wpa_s.conf = wpa_config_alloc_empty(ctrl_iface, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 if (wpa_s.conf == NULL) {
1534 printf("Failed to parse configuration file '%s'.\n", conf);
1535 return -1;
1536 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001537 if (!ctrl_iface && wpa_s.conf->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 printf("No networks defined.\n");
1539 return -1;
1540 }
1541
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001542 if (eapol_test.pcsc_reader) {
1543 os_free(wpa_s.conf->pcsc_reader);
1544 wpa_s.conf->pcsc_reader = os_strdup(eapol_test.pcsc_reader);
1545 }
1546
Sunil Ravi99c035e2024-07-12 01:42:03 +00001547 wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, tls, as_secret,
1548 ca_cert, client_cert, private_key, private_key_passwd,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001549 cli_addr, ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
1551 if (wpa_s.ctrl_iface == NULL) {
1552 printf("Failed to initialize control interface '%s'.\n"
1553 "You may have another eapol_test process already "
1554 "running or the file was\n"
1555 "left by an unclean termination of eapol_test in "
1556 "which case you will need\n"
1557 "to manually remove this file before starting "
1558 "eapol_test again.\n",
1559 wpa_s.conf->ctrl_interface);
1560 return -1;
1561 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001562 if (wpa_s.conf->ssid &&
1563 wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 return -1;
1565
1566 if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
1567 return -1;
1568
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001569 if (wpas_init_ext_pw(&wpa_s) < 0)
1570 return -1;
1571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 if (wait_for_monitor)
1573 wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
1574
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001575 if (!ctrl_iface) {
1576 eloop_register_timeout(timeout, 0, eapol_test_timeout,
1577 &eapol_test, NULL);
1578 eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s,
1579 NULL);
1580 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
1582 eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
1583 eloop_run();
1584
1585 eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
1586 eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
1587
1588 if (eapol_test_compare_pmk(&eapol_test) == 0 ||
1589 eapol_test.no_mppe_keys)
1590 ret = 0;
1591 if (eapol_test.auth_timed_out)
1592 ret = -2;
1593 if (eapol_test.radius_access_reject_received)
1594 ret = -3;
1595
1596 if (save_config)
1597 wpa_config_write(conf, wpa_s.conf);
1598
1599 test_eapol_clean(&eapol_test, &wpa_s);
1600
1601 eap_peer_unregister_methods();
1602#ifdef CONFIG_AP
1603 eap_server_unregister_methods();
1604#endif /* CONFIG_AP */
1605
1606 eloop_destroy();
1607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 if (eapol_test.server_cert_file)
1609 fclose(eapol_test.server_cert_file);
1610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 printf("MPPE keys OK: %d mismatch: %d\n",
1612 eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
1613 if (eapol_test.num_mppe_mismatch)
1614 ret = -4;
1615 if (ret)
1616 printf("FAILURE\n");
1617 else
1618 printf("SUCCESS\n");
1619
Sunil Ravia04bd252022-05-02 22:54:18 -07001620 crypto_unload();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621 os_program_deinit();
1622
1623 return ret;
1624}