Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA Supplicant - test code |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | * |
| 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 Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 16 | #include "utils/ext_password.h" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 17 | #include "common/version.h" |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 18 | #include "crypto/crypto.h" |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 19 | #include "crypto/tls.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 20 | #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 Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 25 | #include "utils/base64.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 26 | #include "rsn_supp/wpa.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 27 | #include "wpa_supplicant_i.h" |
| 28 | #include "radius/radius.h" |
| 29 | #include "radius/radius_client.h" |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 30 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 31 | #include "ctrl_iface.h" |
| 32 | #include "pcsc_funcs.h" |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 33 | #include "wpas_glue.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 34 | |
| 35 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 36 | const struct wpa_driver_ops *const wpa_drivers[] = { NULL }; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 37 | |
| 38 | |
| 39 | struct extra_radius_attr { |
| 40 | u8 type; |
| 41 | char syntax; |
| 42 | char *data; |
| 43 | struct extra_radius_attr *next; |
| 44 | }; |
| 45 | |
| 46 | struct 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 Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 52 | int req_eap_key_name; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 53 | |
| 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 Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 60 | /* last received EAP Response from Authentication Server */ |
| 61 | struct wpabuf *last_eap_radius; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 62 | |
| 63 | u8 authenticator_pmk[PMK_LEN]; |
| 64 | size_t authenticator_pmk_len; |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 65 | u8 authenticator_eap_key_name[256]; |
| 66 | size_t authenticator_eap_key_name_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 67 | 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 Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 77 | |
| 78 | FILE *server_cert_file; |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 79 | |
| 80 | const char *pcsc_reader; |
| 81 | const char *pcsc_pin; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 82 | |
| 83 | unsigned int ctrl_iface:1; |
| 84 | unsigned int id_req_sent:1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static struct eapol_test_data eapol_test; |
| 88 | |
| 89 | |
| 90 | static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx); |
| 91 | |
| 92 | |
| 93 | static 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 | |
| 104 | static 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 Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 110 | char buf[RADIUS_MAX_ATTR_LEN + 1]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 111 | |
| 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 Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 126 | if ((len & 1) || (len / 2) > RADIUS_MAX_ATTR_LEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 127 | 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 | |
| 155 | static 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 | |
| 167 | static struct extra_radius_attr * |
| 168 | find_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 | |
| 179 | static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e, |
| 180 | const u8 *eap, size_t len) |
| 181 | { |
| 182 | struct radius_msg *msg; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 183 | char buf[RADIUS_MAX_ATTR_LEN + 1]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 184 | 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 Shmidt | b97e428 | 2016-02-08 10:16:07 -0800 | [diff] [blame] | 198 | radius_msg_make_authenticator(msg); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 199 | |
| 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 Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 223 | 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 Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 230 | 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 Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 263 | 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 Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 270 | 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 Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 304 | if (radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr) |
| 305 | < 0) |
| 306 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 307 | return; |
| 308 | |
| 309 | fail: |
| 310 | radius_msg_free(msg); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf, |
| 315 | size_t len) |
| 316 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 317 | 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 | |
| 327 | static void eapol_test_set_config_blob(void *ctx, |
| 328 | struct wpa_config_blob *blob) |
| 329 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 330 | struct eapol_test_data *e = ctx; |
| 331 | wpa_config_set_blob(e->wpa_s->conf, blob); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | static const struct wpa_config_blob * |
| 336 | eapol_test_get_config_blob(void *ctx, const char *name) |
| 337 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 338 | struct eapol_test_data *e = ctx; |
| 339 | return wpa_config_get_blob(e->wpa_s->conf, name); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
| 343 | static void eapol_test_eapol_done_cb(void *ctx) |
| 344 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 345 | struct eapol_test_data *e = ctx; |
| 346 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 347 | printf("WPA: EAPOL processing complete\n"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 348 | wpa_supplicant_cancel_auth_timeout(e->wpa_s); |
| 349 | wpa_supplicant_set_state(e->wpa_s, WPA_COMPLETED); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | static 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 | |
| 362 | static int eapol_test_compare_pmk(struct eapol_test_data *e) |
| 363 | { |
| 364 | u8 pmk[PMK_LEN]; |
| 365 | int ret = 1; |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 366 | const u8 *sess_id; |
| 367 | size_t sess_id_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 368 | |
| 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 Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 396 | 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 Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 422 | static void eapol_sm_cb(struct eapol_sm *eapol, enum eapol_supp_result result, |
| 423 | void *ctx) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 424 | { |
| 425 | struct eapol_test_data *e = ctx; |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 426 | printf("eapol_sm_cb: result=%d\n", result); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 427 | e->id_req_sent = 0; |
| 428 | if (e->ctrl_iface) |
| 429 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 430 | 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 Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 440 | static void eapol_test_write_cert(FILE *f, const char *subject, |
| 441 | const struct wpabuf *cert) |
| 442 | { |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 443 | char *encoded; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 444 | |
| 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 Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 454 | #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG) |
| 455 | static 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 Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 484 | if (os_snprintf_error(buflen, len)) { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 485 | 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 Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 502 | static void eapol_test_cert_cb(void *ctx, struct tls_cert_data *cert, |
| 503 | const char *cert_hash) |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 504 | { |
| 505 | struct eapol_test_data *e = ctx; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 506 | int i; |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 507 | |
| 508 | wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT |
| 509 | "depth=%d subject='%s'%s%s", |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 510 | cert->depth, cert->subject, |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 511 | cert_hash ? " hash=" : "", |
| 512 | cert_hash ? cert_hash : ""); |
| 513 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 514 | if (cert->cert) { |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 515 | char *cert_hex; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 516 | size_t len = wpabuf_len(cert->cert) * 2 + 1; |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 517 | cert_hex = os_malloc(len); |
| 518 | if (cert_hex) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 519 | wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert->cert), |
| 520 | wpabuf_len(cert->cert)); |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 521 | wpa_msg_ctrl(e->wpa_s, MSG_INFO, |
| 522 | WPA_EVENT_EAP_PEER_CERT |
| 523 | "depth=%d subject='%s' cert=%s", |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 524 | cert->depth, cert->subject, cert_hex); |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 525 | os_free(cert_hex); |
| 526 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 527 | |
| 528 | if (e->server_cert_file) |
| 529 | eapol_test_write_cert(e->server_cert_file, |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 530 | cert->subject, cert->cert); |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 531 | } |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 532 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 533 | 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 Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 539 | static 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 Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 570 | static 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 Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 579 | static 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 Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 584 | struct wpa_sm_ctx *wctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 585 | |
| 586 | ctx = os_zalloc(sizeof(*ctx)); |
| 587 | if (ctx == NULL) { |
| 588 | printf("Failed to allocate EAPOL context.\n"); |
| 589 | return -1; |
| 590 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 591 | ctx->ctx = e; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 592 | 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 Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 605 | ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 606 | ctx->eap_param_needed = eapol_test_eap_param_needed; |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 607 | ctx->cert_cb = eapol_test_cert_cb; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 608 | ctx->cert_in_cb = 1; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 609 | ctx->set_anon_id = eapol_test_set_anon_id; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 610 | |
| 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 Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 618 | 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 Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 637 | 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 Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 643 | eapol_conf.external_sim = wpa_s->conf->external_sim; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 644 | 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 Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 648 | eapol_sm_notify_portValid(wpa_s->eapol, false); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 649 | /* 802.1X::portControl = Auto */ |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 650 | eapol_sm_notify_portEnabled(wpa_s->eapol, true); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | |
| 656 | static 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 Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 661 | wpa_sm_deinit(wpa_s->wpa); |
| 662 | wpa_s->wpa = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 663 | radius_client_deinit(e->radius); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 664 | wpabuf_free(e->last_eap_radius); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 665 | 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); |
| 673 | os_free(e->radius_conf->auth_server); |
| 674 | } |
| 675 | os_free(e->radius_conf); |
| 676 | e->radius_conf = NULL; |
| 677 | scard_deinit(wpa_s->scard); |
Jouni Malinen | f3f8d3c | 2021-02-05 00:28:17 +0200 | [diff] [blame] | 678 | wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface); |
| 679 | wpa_s->ctrl_iface = NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 680 | |
| 681 | ext_password_deinit(wpa_s->ext_pw); |
| 682 | wpa_s->ext_pw = NULL; |
| 683 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 684 | wpa_config_free(wpa_s->conf); |
| 685 | |
| 686 | p = e->extra_attrs; |
| 687 | while (p) { |
| 688 | prev = p; |
| 689 | p = p->next; |
| 690 | os_free(prev); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | |
| 695 | static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx) |
| 696 | { |
| 697 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 698 | u8 buf[100], *pos; |
| 699 | struct ieee802_1x_hdr *hdr; |
| 700 | struct eap_hdr *eap; |
| 701 | |
| 702 | hdr = (struct ieee802_1x_hdr *) buf; |
| 703 | hdr->version = EAPOL_VERSION; |
| 704 | hdr->type = IEEE802_1X_TYPE_EAP_PACKET; |
| 705 | hdr->length = htons(5); |
| 706 | |
| 707 | eap = (struct eap_hdr *) (hdr + 1); |
| 708 | eap->code = EAP_CODE_REQUEST; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 709 | if (os_get_random((u8 *) &eap->identifier, sizeof(eap->identifier)) < 0) |
| 710 | eap->identifier = os_random() & 0xff; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 711 | eap->length = htons(5); |
| 712 | pos = (u8 *) (eap + 1); |
| 713 | *pos = EAP_TYPE_IDENTITY; |
| 714 | |
| 715 | printf("Sending fake EAP-Request-Identity\n"); |
| 716 | eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf, |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 717 | sizeof(*hdr) + 5, FRAME_ENCRYPTION_UNKNOWN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | |
| 721 | static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx) |
| 722 | { |
| 723 | struct eapol_test_data *e = eloop_ctx; |
| 724 | printf("EAPOL test timed out\n"); |
| 725 | e->auth_timed_out = 1; |
| 726 | eloop_terminate(); |
| 727 | } |
| 728 | |
| 729 | |
| 730 | static char *eap_type_text(u8 type) |
| 731 | { |
| 732 | switch (type) { |
| 733 | case EAP_TYPE_IDENTITY: return "Identity"; |
| 734 | case EAP_TYPE_NOTIFICATION: return "Notification"; |
| 735 | case EAP_TYPE_NAK: return "Nak"; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 736 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 737 | case EAP_TYPE_MD5: return "MD5"; |
| 738 | case EAP_TYPE_OTP: return "OTP"; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 739 | case EAP_TYPE_GTC: return "GTC"; |
| 740 | case EAP_TYPE_TLS: return "TLS"; |
| 741 | case EAP_TYPE_LEAP: return "LEAP"; |
| 742 | case EAP_TYPE_SIM: return "SIM"; |
| 743 | case EAP_TYPE_TTLS: return "TTLS"; |
| 744 | case EAP_TYPE_AKA: return "AKA"; |
| 745 | case EAP_TYPE_PEAP: return "PEAP"; |
| 746 | case EAP_TYPE_MSCHAPV2: return "MSCHAPv2"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 747 | case EAP_TYPE_FAST: return "FAST"; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 748 | case EAP_TYPE_PAX: return "PAX"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 749 | case EAP_TYPE_PSK: return "PSK"; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 750 | case EAP_TYPE_SAKE: return "SAKE"; |
| 751 | case EAP_TYPE_IKEV2: return "IKEv2"; |
| 752 | case EAP_TYPE_AKA_PRIME: return "AKA-PRIME"; |
| 753 | case EAP_TYPE_GPSK: return "GPSK"; |
| 754 | case EAP_TYPE_PWD: return "PWD"; |
| 755 | case EAP_TYPE_EKE: return "EKE"; |
| 756 | case EAP_TYPE_TEAP: return "TEAP"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 757 | default: return "Unknown"; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | |
| 762 | static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e) |
| 763 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 764 | struct wpabuf *eap; |
| 765 | const struct eap_hdr *hdr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 766 | int eap_type = -1; |
| 767 | char buf[64]; |
| 768 | struct radius_msg *msg; |
| 769 | |
| 770 | if (e->last_recv_radius == NULL) |
| 771 | return; |
| 772 | |
| 773 | msg = e->last_recv_radius; |
| 774 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 775 | eap = radius_msg_get_eap(msg); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 776 | if (!eap) { |
| 777 | /* RFC 3579, Chap. 2.6.3: |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 778 | * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message |
| 779 | * attribute */ |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 780 | wpa_printf(MSG_DEBUG, |
| 781 | "could not extract EAP-Message from RADIUS message"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 782 | wpabuf_free(e->last_eap_radius); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 783 | e->last_eap_radius = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 787 | if (wpabuf_len(eap) < sizeof(*hdr)) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 788 | wpa_printf(MSG_DEBUG, |
| 789 | "too short EAP packet received from authentication server"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 790 | wpabuf_free(eap); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 791 | return; |
| 792 | } |
| 793 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 794 | if (wpabuf_len(eap) > sizeof(*hdr)) |
| 795 | eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 796 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 797 | hdr = wpabuf_head(eap); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 798 | switch (hdr->code) { |
| 799 | case EAP_CODE_REQUEST: |
| 800 | os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)", |
| 801 | eap_type >= 0 ? eap_type_text(eap_type) : "??", |
| 802 | eap_type); |
| 803 | break; |
| 804 | case EAP_CODE_RESPONSE: |
| 805 | os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)", |
| 806 | eap_type >= 0 ? eap_type_text(eap_type) : "??", |
| 807 | eap_type); |
| 808 | break; |
| 809 | case EAP_CODE_SUCCESS: |
| 810 | os_strlcpy(buf, "EAP Success", sizeof(buf)); |
| 811 | /* LEAP uses EAP Success within an authentication, so must not |
| 812 | * stop here with eloop_terminate(); */ |
| 813 | break; |
| 814 | case EAP_CODE_FAILURE: |
| 815 | os_strlcpy(buf, "EAP Failure", sizeof(buf)); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 816 | if (e->ctrl_iface) |
| 817 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 818 | eloop_terminate(); |
| 819 | break; |
| 820 | default: |
| 821 | os_strlcpy(buf, "unknown EAP code", sizeof(buf)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 822 | wpa_hexdump_buf(MSG_DEBUG, "Decapsulated EAP packet", eap); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 823 | break; |
| 824 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 825 | buf[sizeof(buf) - 1] = '\0'; |
| 826 | wpa_printf(MSG_DEBUG, |
| 827 | "decapsulated EAP packet (code=%d id=%d len=%d) from RADIUS server: %s", |
| 828 | hdr->code, hdr->identifier, be_to_host16(hdr->length), |
| 829 | buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 830 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 831 | wpabuf_free(e->last_eap_radius); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 832 | e->last_eap_radius = eap; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 833 | |
| 834 | { |
| 835 | struct ieee802_1x_hdr *dot1x; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 836 | dot1x = os_malloc(sizeof(*dot1x) + wpabuf_len(eap)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 837 | assert(dot1x != NULL); |
| 838 | dot1x->version = EAPOL_VERSION; |
| 839 | dot1x->type = IEEE802_1X_TYPE_EAP_PACKET; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 840 | dot1x->length = htons(wpabuf_len(eap)); |
| 841 | os_memcpy((u8 *) (dot1x + 1), wpabuf_head(eap), |
| 842 | wpabuf_len(eap)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 843 | eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 844 | (u8 *) dot1x, |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 845 | sizeof(*dot1x) + wpabuf_len(eap), |
| 846 | FRAME_ENCRYPTION_UNKNOWN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 847 | os_free(dot1x); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | |
| 852 | static void ieee802_1x_get_keys(struct eapol_test_data *e, |
| 853 | struct radius_msg *msg, struct radius_msg *req, |
| 854 | const u8 *shared_secret, |
| 855 | size_t shared_secret_len) |
| 856 | { |
| 857 | struct radius_ms_mppe_keys *keys; |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 858 | u8 *buf; |
| 859 | size_t len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 860 | |
| 861 | keys = radius_msg_get_ms_keys(msg, req, shared_secret, |
| 862 | shared_secret_len); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 863 | if (keys && !keys->send && !keys->recv) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 864 | os_free(keys); |
| 865 | keys = radius_msg_get_cisco_keys(msg, req, shared_secret, |
| 866 | shared_secret_len); |
| 867 | } |
| 868 | |
| 869 | if (keys) { |
| 870 | if (keys->send) { |
| 871 | wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)", |
| 872 | keys->send, keys->send_len); |
| 873 | } |
| 874 | if (keys->recv) { |
| 875 | wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)", |
| 876 | keys->recv, keys->recv_len); |
| 877 | e->authenticator_pmk_len = |
| 878 | keys->recv_len > PMK_LEN ? PMK_LEN : |
| 879 | keys->recv_len; |
| 880 | os_memcpy(e->authenticator_pmk, keys->recv, |
| 881 | e->authenticator_pmk_len); |
| 882 | if (e->authenticator_pmk_len == 16 && keys->send && |
| 883 | keys->send_len == 16) { |
| 884 | /* MS-CHAP-v2 derives 16 octet keys */ |
| 885 | wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key " |
| 886 | "to extend PMK to 32 octets"); |
| 887 | os_memcpy(e->authenticator_pmk + |
| 888 | e->authenticator_pmk_len, |
| 889 | keys->send, keys->send_len); |
| 890 | e->authenticator_pmk_len += keys->send_len; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | os_free(keys->send); |
| 895 | os_free(keys->recv); |
| 896 | os_free(keys); |
| 897 | } |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 898 | |
| 899 | if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len, |
| 900 | NULL) == 0) { |
| 901 | os_memcpy(e->authenticator_eap_key_name, buf, len); |
| 902 | e->authenticator_eap_key_name_len = len; |
| 903 | } else { |
| 904 | e->authenticator_eap_key_name_len = 0; |
| 905 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | |
| 909 | /* Process the RADIUS frames from Authentication Server */ |
| 910 | static RadiusRxResult |
| 911 | ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req, |
| 912 | const u8 *shared_secret, size_t shared_secret_len, |
| 913 | void *data) |
| 914 | { |
| 915 | struct eapol_test_data *e = data; |
| 916 | struct radius_hdr *hdr = radius_msg_get_hdr(msg); |
| 917 | |
| 918 | /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be |
| 919 | * present when packet contains an EAP-Message attribute */ |
| 920 | if (hdr->code == RADIUS_CODE_ACCESS_REJECT && |
| 921 | radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL, |
| 922 | 0) < 0 && |
| 923 | radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 924 | wpa_printf(MSG_DEBUG, |
| 925 | "Allowing RADIUS Access-Reject without Message-Authenticator since it does not include EAP-Message"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 926 | } else if (radius_msg_verify(msg, shared_secret, shared_secret_len, |
| 927 | req, 1)) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 928 | wpa_printf(MSG_INFO, |
| 929 | "Incoming RADIUS packet did not have correct Message-Authenticator - dropped"); |
| 930 | return RADIUS_RX_INVALID_AUTHENTICATOR; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT && |
| 934 | hdr->code != RADIUS_CODE_ACCESS_REJECT && |
| 935 | hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 936 | wpa_printf(MSG_INFO, "Unknown RADIUS message code"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 937 | return RADIUS_RX_UNKNOWN; |
| 938 | } |
| 939 | |
| 940 | e->radius_identifier = -1; |
| 941 | wpa_printf(MSG_DEBUG, "RADIUS packet matching with station"); |
| 942 | |
| 943 | radius_msg_free(e->last_recv_radius); |
| 944 | e->last_recv_radius = msg; |
| 945 | |
| 946 | switch (hdr->code) { |
| 947 | case RADIUS_CODE_ACCESS_ACCEPT: |
| 948 | e->radius_access_accept_received = 1; |
| 949 | ieee802_1x_get_keys(e, msg, req, shared_secret, |
| 950 | shared_secret_len); |
| 951 | break; |
| 952 | case RADIUS_CODE_ACCESS_REJECT: |
| 953 | e->radius_access_reject_received = 1; |
| 954 | break; |
| 955 | } |
| 956 | |
| 957 | ieee802_1x_decapsulate_radius(e); |
| 958 | |
| 959 | if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT && |
| 960 | e->eapol_test_num_reauths < 0) || |
| 961 | hdr->code == RADIUS_CODE_ACCESS_REJECT) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 962 | if (!e->ctrl_iface) |
| 963 | eloop_terminate(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | return RADIUS_RX_QUEUED; |
| 967 | } |
| 968 | |
| 969 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 970 | static int driver_get_ssid(void *priv, u8 *ssid) |
| 971 | { |
| 972 | ssid[0] = 0; |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | |
| 977 | static int driver_get_bssid(void *priv, u8 *bssid) |
| 978 | { |
| 979 | struct eapol_test_data *e = priv; |
| 980 | |
| 981 | if (e->ctrl_iface && !e->id_req_sent) { |
| 982 | eloop_register_timeout(0, 0, send_eap_request_identity, |
| 983 | e->wpa_s, NULL); |
| 984 | e->id_req_sent = 1; |
| 985 | } |
| 986 | |
| 987 | os_memset(bssid, 0, ETH_ALEN); |
| 988 | bssid[5] = 1; |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | |
| 993 | static int driver_get_capa(void *priv, struct wpa_driver_capa *capa) |
| 994 | { |
| 995 | os_memset(capa, 0, sizeof(*capa)); |
| 996 | capa->flags = WPA_DRIVER_FLAGS_WIRED; |
| 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | struct wpa_driver_ops eapol_test_drv_ops = { |
| 1002 | .name = "test", |
| 1003 | .get_ssid = driver_get_ssid, |
| 1004 | .get_bssid = driver_get_bssid, |
| 1005 | .get_capa = driver_get_capa, |
| 1006 | }; |
| 1007 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1008 | static void wpa_init_conf(struct eapol_test_data *e, |
| 1009 | struct wpa_supplicant *wpa_s, const char *authsrv, |
| 1010 | int port, const char *secret, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1011 | const char *cli_addr, const char *ifname) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1012 | { |
| 1013 | struct hostapd_radius_server *as; |
| 1014 | int res; |
| 1015 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1016 | wpa_s->driver = &eapol_test_drv_ops; |
| 1017 | wpa_s->drv_priv = e; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1018 | wpa_s->bssid[5] = 1; |
| 1019 | os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN); |
| 1020 | e->own_ip_addr.s_addr = htonl((127 << 24) | 1); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1021 | os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1022 | |
| 1023 | e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers)); |
| 1024 | assert(e->radius_conf != NULL); |
| 1025 | e->radius_conf->num_auth_servers = 1; |
| 1026 | as = os_zalloc(sizeof(struct hostapd_radius_server)); |
| 1027 | assert(as != NULL); |
| 1028 | #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA) |
| 1029 | { |
| 1030 | int a[4]; |
| 1031 | u8 *pos; |
| 1032 | sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]); |
| 1033 | pos = (u8 *) &as->addr.u.v4; |
| 1034 | *pos++ = a[0]; |
| 1035 | *pos++ = a[1]; |
| 1036 | *pos++ = a[2]; |
| 1037 | *pos++ = a[3]; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 1038 | as->addr.af = AF_INET; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1039 | } |
| 1040 | #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */ |
Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 1041 | if (hostapd_parse_ip_addr(authsrv, &as->addr) < 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1042 | wpa_printf(MSG_ERROR, "Invalid IP address '%s'", |
| 1043 | authsrv); |
| 1044 | assert(0); |
| 1045 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1046 | #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1047 | as->port = port; |
| 1048 | as->shared_secret = (u8 *) os_strdup(secret); |
| 1049 | as->shared_secret_len = os_strlen(secret); |
| 1050 | e->radius_conf->auth_server = as; |
| 1051 | e->radius_conf->auth_servers = as; |
| 1052 | e->radius_conf->msg_dumps = 1; |
| 1053 | if (cli_addr) { |
| 1054 | if (hostapd_parse_ip_addr(cli_addr, |
| 1055 | &e->radius_conf->client_addr) == 0) |
| 1056 | e->radius_conf->force_client_addr = 1; |
| 1057 | else { |
| 1058 | wpa_printf(MSG_ERROR, "Invalid IP address '%s'", |
| 1059 | cli_addr); |
| 1060 | assert(0); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | e->radius = radius_client_init(wpa_s, e->radius_conf); |
| 1065 | assert(e->radius != NULL); |
| 1066 | |
| 1067 | res = radius_client_register(e->radius, RADIUS_AUTH, |
| 1068 | ieee802_1x_receive_auth, e); |
| 1069 | assert(res == 0); |
| 1070 | } |
| 1071 | |
| 1072 | |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1073 | static int scard_test(struct eapol_test_data *e) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1074 | { |
| 1075 | struct scard_data *scard; |
| 1076 | size_t len; |
| 1077 | char imsi[20]; |
| 1078 | unsigned char _rand[16]; |
| 1079 | #ifdef PCSC_FUNCS |
| 1080 | unsigned char sres[4]; |
| 1081 | unsigned char kc[8]; |
| 1082 | #endif /* PCSC_FUNCS */ |
| 1083 | #define num_triplets 5 |
| 1084 | unsigned char rand_[num_triplets][16]; |
| 1085 | unsigned char sres_[num_triplets][4]; |
| 1086 | unsigned char kc_[num_triplets][8]; |
| 1087 | int i, res; |
| 1088 | size_t j; |
| 1089 | |
| 1090 | #define AKA_RAND_LEN 16 |
| 1091 | #define AKA_AUTN_LEN 16 |
| 1092 | #define AKA_AUTS_LEN 14 |
| 1093 | #define RES_MAX_LEN 16 |
| 1094 | #define IK_LEN 16 |
| 1095 | #define CK_LEN 16 |
| 1096 | unsigned char aka_rand[AKA_RAND_LEN]; |
| 1097 | unsigned char aka_autn[AKA_AUTN_LEN]; |
| 1098 | unsigned char aka_auts[AKA_AUTS_LEN]; |
| 1099 | unsigned char aka_res[RES_MAX_LEN]; |
| 1100 | size_t aka_res_len; |
| 1101 | unsigned char aka_ik[IK_LEN]; |
| 1102 | unsigned char aka_ck[CK_LEN]; |
| 1103 | |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1104 | scard = scard_init(e->pcsc_reader); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1105 | if (scard == NULL) |
| 1106 | return -1; |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1107 | if (scard_set_pin(scard, e->pcsc_pin)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1108 | wpa_printf(MSG_WARNING, "PIN validation failed"); |
| 1109 | scard_deinit(scard); |
| 1110 | return -1; |
| 1111 | } |
| 1112 | |
| 1113 | len = sizeof(imsi); |
| 1114 | if (scard_get_imsi(scard, imsi, &len)) |
| 1115 | goto failed; |
| 1116 | wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len); |
| 1117 | /* NOTE: Permanent Username: 1 | IMSI */ |
| 1118 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1119 | wpa_printf(MSG_DEBUG, "SCARD: MNC length %d", |
| 1120 | scard_get_mnc_len(scard)); |
| 1121 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1122 | os_memset(_rand, 0, sizeof(_rand)); |
| 1123 | if (scard_gsm_auth(scard, _rand, sres, kc)) |
| 1124 | goto failed; |
| 1125 | |
| 1126 | os_memset(_rand, 0xff, sizeof(_rand)); |
| 1127 | if (scard_gsm_auth(scard, _rand, sres, kc)) |
| 1128 | goto failed; |
| 1129 | |
| 1130 | for (i = 0; i < num_triplets; i++) { |
| 1131 | os_memset(rand_[i], i, sizeof(rand_[i])); |
| 1132 | if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i])) |
| 1133 | goto failed; |
| 1134 | } |
| 1135 | |
| 1136 | for (i = 0; i < num_triplets; i++) { |
| 1137 | printf("1"); |
| 1138 | for (j = 0; j < len; j++) |
| 1139 | printf("%c", imsi[j]); |
| 1140 | printf(","); |
| 1141 | for (j = 0; j < 16; j++) |
| 1142 | printf("%02X", rand_[i][j]); |
| 1143 | printf(","); |
| 1144 | for (j = 0; j < 4; j++) |
| 1145 | printf("%02X", sres_[i][j]); |
| 1146 | printf(","); |
| 1147 | for (j = 0; j < 8; j++) |
| 1148 | printf("%02X", kc_[i][j]); |
| 1149 | printf("\n"); |
| 1150 | } |
| 1151 | |
| 1152 | wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication"); |
| 1153 | |
| 1154 | /* seq 39 (0x28) */ |
| 1155 | os_memset(aka_rand, 0xaa, 16); |
| 1156 | os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf" |
| 1157 | "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16); |
| 1158 | |
| 1159 | res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len, |
| 1160 | aka_ik, aka_ck, aka_auts); |
| 1161 | if (res == 0) { |
| 1162 | wpa_printf(MSG_DEBUG, "UMTS auth completed successfully"); |
| 1163 | wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len); |
| 1164 | wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN); |
| 1165 | wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN); |
| 1166 | } else if (res == -2) { |
| 1167 | wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization " |
| 1168 | "failure"); |
| 1169 | wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN); |
| 1170 | } else { |
| 1171 | wpa_printf(MSG_DEBUG, "UMTS auth failed"); |
| 1172 | } |
| 1173 | |
| 1174 | failed: |
| 1175 | scard_deinit(scard); |
| 1176 | |
| 1177 | return 0; |
| 1178 | #undef num_triplets |
| 1179 | } |
| 1180 | |
| 1181 | |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1182 | static int scard_get_triplets(struct eapol_test_data *e, int argc, char *argv[]) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1183 | { |
| 1184 | struct scard_data *scard; |
| 1185 | size_t len; |
| 1186 | char imsi[20]; |
| 1187 | unsigned char _rand[16]; |
| 1188 | unsigned char sres[4]; |
| 1189 | unsigned char kc[8]; |
| 1190 | int num_triplets; |
| 1191 | int i; |
| 1192 | size_t j; |
| 1193 | |
| 1194 | if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) { |
| 1195 | printf("invalid parameters for sim command\n"); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | |
| 1199 | if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) { |
| 1200 | /* disable debug output */ |
| 1201 | wpa_debug_level = 99; |
| 1202 | } |
| 1203 | |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1204 | scard = scard_init(e->pcsc_reader); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1205 | if (scard == NULL) { |
| 1206 | printf("Failed to open smartcard connection\n"); |
| 1207 | return -1; |
| 1208 | } |
| 1209 | if (scard_set_pin(scard, argv[0])) { |
| 1210 | wpa_printf(MSG_WARNING, "PIN validation failed"); |
| 1211 | scard_deinit(scard); |
| 1212 | return -1; |
| 1213 | } |
| 1214 | |
| 1215 | len = sizeof(imsi); |
| 1216 | if (scard_get_imsi(scard, imsi, &len)) { |
| 1217 | scard_deinit(scard); |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
| 1221 | for (i = 0; i < num_triplets; i++) { |
| 1222 | os_memset(_rand, i, sizeof(_rand)); |
| 1223 | if (scard_gsm_auth(scard, _rand, sres, kc)) |
| 1224 | break; |
| 1225 | |
| 1226 | /* IMSI:Kc:SRES:RAND */ |
| 1227 | for (j = 0; j < len; j++) |
| 1228 | printf("%c", imsi[j]); |
| 1229 | printf(":"); |
| 1230 | for (j = 0; j < 8; j++) |
| 1231 | printf("%02X", kc[j]); |
| 1232 | printf(":"); |
| 1233 | for (j = 0; j < 4; j++) |
| 1234 | printf("%02X", sres[j]); |
| 1235 | printf(":"); |
| 1236 | for (j = 0; j < 16; j++) |
| 1237 | printf("%02X", _rand[j]); |
| 1238 | printf("\n"); |
| 1239 | } |
| 1240 | |
| 1241 | scard_deinit(scard); |
| 1242 | |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | static void eapol_test_terminate(int sig, void *signal_ctx) |
| 1248 | { |
| 1249 | struct wpa_supplicant *wpa_s = signal_ctx; |
| 1250 | wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig); |
| 1251 | eloop_terminate(); |
| 1252 | } |
| 1253 | |
| 1254 | |
| 1255 | static void usage(void) |
| 1256 | { |
| 1257 | printf("usage:\n" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1258 | "eapol_test [-enWSv] -c<conf> [-a<AS IP>] [-p<AS port>] " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1259 | "[-s<AS secret>]\\\n" |
| 1260 | " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1261 | " [-M<client MAC address>] [-o<server cert file] \\\n" |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1262 | " [-N<attr spec>] [-R<PC/SC reader>] " |
| 1263 | "[-P<PC/SC PIN>] \\\n" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1264 | " [-A<client IP>] [-i<ifname>] [-T<ctrl_iface>]\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1265 | "eapol_test scard\n" |
| 1266 | "eapol_test sim <PIN> <num triplets> [debug]\n" |
| 1267 | "\n"); |
| 1268 | printf("options:\n" |
| 1269 | " -c<conf> = configuration file\n" |
| 1270 | " -a<AS IP> = IP address of the authentication server, " |
| 1271 | "default 127.0.0.1\n" |
| 1272 | " -p<AS port> = UDP port of the authentication server, " |
| 1273 | "default 1812\n" |
| 1274 | " -s<AS secret> = shared secret with the authentication " |
| 1275 | "server, default 'radius'\n" |
| 1276 | " -A<client IP> = IP address of the client, default: select " |
| 1277 | "automatically\n" |
| 1278 | " -r<count> = number of re-authentications\n" |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 1279 | " -e = Request EAP-Key-Name\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1280 | " -W = wait for a control interface monitor before starting\n" |
| 1281 | " -S = save configuration after authentication\n" |
| 1282 | " -n = no MPPE keys expected\n" |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1283 | " -v = show version\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1284 | " -t<timeout> = sets timeout in seconds (default: 30 s)\n" |
| 1285 | " -C<Connect-Info> = RADIUS Connect-Info (default: " |
| 1286 | "CONNECT 11Mbps 802.11b)\n" |
| 1287 | " -M<client MAC address> = Set own MAC address " |
| 1288 | "(Calling-Station-Id,\n" |
| 1289 | " default: 02:00:00:00:00:01)\n" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1290 | " -o<server cert file> = Write received server certificate\n" |
| 1291 | " chain to the specified file\n" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1292 | " -N<attr spec> = send arbitrary attribute specified by:\n" |
| 1293 | " attr_id:syntax:value or attr_id\n" |
| 1294 | " attr_id - number id of the attribute\n" |
| 1295 | " syntax - one of: s, d, x\n" |
| 1296 | " s = string\n" |
| 1297 | " d = integer\n" |
| 1298 | " x = octet string\n" |
| 1299 | " value - attribute value.\n" |
| 1300 | " When only attr_id is specified, NULL will be used as " |
| 1301 | "value.\n" |
| 1302 | " Multiple attributes can be specified by using the " |
| 1303 | "option several times.\n"); |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | int main(int argc, char *argv[]) |
| 1308 | { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1309 | struct wpa_global global; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1310 | struct wpa_supplicant wpa_s; |
| 1311 | int c, ret = 1, wait_for_monitor = 0, save_config = 0; |
| 1312 | char *as_addr = "127.0.0.1"; |
| 1313 | int as_port = 1812; |
| 1314 | char *as_secret = "radius"; |
| 1315 | char *cli_addr = NULL; |
| 1316 | char *conf = NULL; |
| 1317 | int timeout = 30; |
| 1318 | char *pos; |
| 1319 | struct extra_radius_attr *p = NULL, *p1; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1320 | const char *ifname = "test"; |
| 1321 | const char *ctrl_iface = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1322 | |
| 1323 | if (os_program_init()) |
| 1324 | return -1; |
| 1325 | |
| 1326 | hostapd_logger_register_cb(hostapd_logger_cb); |
| 1327 | |
| 1328 | os_memset(&eapol_test, 0, sizeof(eapol_test)); |
| 1329 | eapol_test.connect_info = "CONNECT 11Mbps 802.11b"; |
| 1330 | os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN); |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1331 | eapol_test.pcsc_pin = "1234"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1332 | |
| 1333 | wpa_debug_level = 0; |
| 1334 | wpa_debug_show_keys = 1; |
| 1335 | |
| 1336 | for (;;) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1337 | c = getopt(argc, argv, "a:A:c:C:ei:M:nN:o:p:P:r:R:s:St:T:vW"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1338 | if (c < 0) |
| 1339 | break; |
| 1340 | switch (c) { |
| 1341 | case 'a': |
| 1342 | as_addr = optarg; |
| 1343 | break; |
| 1344 | case 'A': |
| 1345 | cli_addr = optarg; |
| 1346 | break; |
| 1347 | case 'c': |
| 1348 | conf = optarg; |
| 1349 | break; |
| 1350 | case 'C': |
| 1351 | eapol_test.connect_info = optarg; |
| 1352 | break; |
Dmitry Shmidt | 5a1480c | 2014-05-12 09:46:02 -0700 | [diff] [blame] | 1353 | case 'e': |
| 1354 | eapol_test.req_eap_key_name = 1; |
| 1355 | break; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1356 | case 'i': |
| 1357 | ifname = optarg; |
| 1358 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1359 | case 'M': |
| 1360 | if (hwaddr_aton(optarg, eapol_test.own_addr)) { |
| 1361 | usage(); |
| 1362 | return -1; |
| 1363 | } |
| 1364 | break; |
| 1365 | case 'n': |
| 1366 | eapol_test.no_mppe_keys++; |
| 1367 | break; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1368 | case 'o': |
| 1369 | if (eapol_test.server_cert_file) |
| 1370 | fclose(eapol_test.server_cert_file); |
| 1371 | eapol_test.server_cert_file = fopen(optarg, "w"); |
| 1372 | if (eapol_test.server_cert_file == NULL) { |
| 1373 | printf("Could not open '%s' for writing\n", |
| 1374 | optarg); |
| 1375 | return -1; |
| 1376 | } |
| 1377 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1378 | case 'p': |
| 1379 | as_port = atoi(optarg); |
| 1380 | break; |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1381 | case 'P': |
| 1382 | eapol_test.pcsc_pin = optarg; |
| 1383 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1384 | case 'r': |
| 1385 | eapol_test.eapol_test_num_reauths = atoi(optarg); |
| 1386 | break; |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1387 | case 'R': |
| 1388 | eapol_test.pcsc_reader = optarg; |
Dmitry Shmidt | d30ac60 | 2014-06-30 09:54:22 -0700 | [diff] [blame] | 1389 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1390 | case 's': |
| 1391 | as_secret = optarg; |
| 1392 | break; |
| 1393 | case 'S': |
| 1394 | save_config++; |
| 1395 | break; |
| 1396 | case 't': |
| 1397 | timeout = atoi(optarg); |
| 1398 | break; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1399 | case 'T': |
| 1400 | ctrl_iface = optarg; |
| 1401 | eapol_test.ctrl_iface = 1; |
| 1402 | break; |
| 1403 | case 'v': |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1404 | printf("eapol_test v%s\n", VERSION_STR); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1405 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1406 | case 'W': |
| 1407 | wait_for_monitor++; |
| 1408 | break; |
| 1409 | case 'N': |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1410 | p1 = os_zalloc(sizeof(*p1)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1411 | if (p1 == NULL) |
| 1412 | break; |
| 1413 | if (!p) |
| 1414 | eapol_test.extra_attrs = p1; |
| 1415 | else |
| 1416 | p->next = p1; |
| 1417 | p = p1; |
| 1418 | |
| 1419 | p->type = atoi(optarg); |
| 1420 | pos = os_strchr(optarg, ':'); |
| 1421 | if (pos == NULL) { |
| 1422 | p->syntax = 'n'; |
| 1423 | p->data = NULL; |
| 1424 | break; |
| 1425 | } |
| 1426 | |
| 1427 | pos++; |
| 1428 | if (pos[0] == '\0' || pos[1] != ':') { |
| 1429 | printf("Incorrect format of attribute " |
| 1430 | "specification\n"); |
| 1431 | break; |
| 1432 | } |
| 1433 | |
| 1434 | p->syntax = pos[0]; |
| 1435 | p->data = pos + 2; |
| 1436 | break; |
| 1437 | default: |
| 1438 | usage(); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | if (argc > optind && os_strcmp(argv[optind], "scard") == 0) { |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1444 | return scard_test(&eapol_test); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | if (argc > optind && os_strcmp(argv[optind], "sim") == 0) { |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1448 | return scard_get_triplets(&eapol_test, argc - optind - 1, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1449 | &argv[optind + 1]); |
| 1450 | } |
| 1451 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1452 | if (conf == NULL && !ctrl_iface) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1453 | usage(); |
| 1454 | printf("Configuration file is required.\n"); |
| 1455 | return -1; |
| 1456 | } |
| 1457 | |
| 1458 | if (eap_register_methods()) { |
| 1459 | wpa_printf(MSG_ERROR, "Failed to register EAP methods"); |
| 1460 | return -1; |
| 1461 | } |
| 1462 | |
| 1463 | if (eloop_init()) { |
| 1464 | wpa_printf(MSG_ERROR, "Failed to initialize event loop"); |
| 1465 | return -1; |
| 1466 | } |
| 1467 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1468 | os_memset(&global, 0, sizeof(global)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1469 | os_memset(&wpa_s, 0, sizeof(wpa_s)); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1470 | wpa_s.global = &global; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1471 | eapol_test.wpa_s = &wpa_s; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1472 | dl_list_init(&wpa_s.bss); |
| 1473 | dl_list_init(&wpa_s.bss_id); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1474 | if (conf) |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1475 | wpa_s.conf = wpa_config_read(conf, NULL, false); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1476 | else |
| 1477 | wpa_s.conf = wpa_config_alloc_empty(ctrl_iface, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1478 | if (wpa_s.conf == NULL) { |
| 1479 | printf("Failed to parse configuration file '%s'.\n", conf); |
| 1480 | return -1; |
| 1481 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1482 | if (!ctrl_iface && wpa_s.conf->ssid == NULL) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1483 | printf("No networks defined.\n"); |
| 1484 | return -1; |
| 1485 | } |
| 1486 | |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 1487 | if (eapol_test.pcsc_reader) { |
| 1488 | os_free(wpa_s.conf->pcsc_reader); |
| 1489 | wpa_s.conf->pcsc_reader = os_strdup(eapol_test.pcsc_reader); |
| 1490 | } |
| 1491 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1492 | wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1493 | cli_addr, ifname); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1494 | wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s); |
| 1495 | if (wpa_s.ctrl_iface == NULL) { |
| 1496 | printf("Failed to initialize control interface '%s'.\n" |
| 1497 | "You may have another eapol_test process already " |
| 1498 | "running or the file was\n" |
| 1499 | "left by an unclean termination of eapol_test in " |
| 1500 | "which case you will need\n" |
| 1501 | "to manually remove this file before starting " |
| 1502 | "eapol_test again.\n", |
| 1503 | wpa_s.conf->ctrl_interface); |
| 1504 | return -1; |
| 1505 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1506 | if (wpa_s.conf->ssid && |
| 1507 | wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1508 | return -1; |
| 1509 | |
| 1510 | if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid)) |
| 1511 | return -1; |
| 1512 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1513 | if (wpas_init_ext_pw(&wpa_s) < 0) |
| 1514 | return -1; |
| 1515 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1516 | if (wait_for_monitor) |
| 1517 | wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface); |
| 1518 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1519 | if (!ctrl_iface) { |
| 1520 | eloop_register_timeout(timeout, 0, eapol_test_timeout, |
| 1521 | &eapol_test, NULL); |
| 1522 | eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, |
| 1523 | NULL); |
| 1524 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1525 | eloop_register_signal_terminate(eapol_test_terminate, &wpa_s); |
| 1526 | eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s); |
| 1527 | eloop_run(); |
| 1528 | |
| 1529 | eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL); |
| 1530 | eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL); |
| 1531 | |
| 1532 | if (eapol_test_compare_pmk(&eapol_test) == 0 || |
| 1533 | eapol_test.no_mppe_keys) |
| 1534 | ret = 0; |
| 1535 | if (eapol_test.auth_timed_out) |
| 1536 | ret = -2; |
| 1537 | if (eapol_test.radius_access_reject_received) |
| 1538 | ret = -3; |
| 1539 | |
| 1540 | if (save_config) |
| 1541 | wpa_config_write(conf, wpa_s.conf); |
| 1542 | |
| 1543 | test_eapol_clean(&eapol_test, &wpa_s); |
| 1544 | |
| 1545 | eap_peer_unregister_methods(); |
| 1546 | #ifdef CONFIG_AP |
| 1547 | eap_server_unregister_methods(); |
| 1548 | #endif /* CONFIG_AP */ |
| 1549 | |
| 1550 | eloop_destroy(); |
| 1551 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1552 | if (eapol_test.server_cert_file) |
| 1553 | fclose(eapol_test.server_cert_file); |
| 1554 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1555 | printf("MPPE keys OK: %d mismatch: %d\n", |
| 1556 | eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch); |
| 1557 | if (eapol_test.num_mppe_mismatch) |
| 1558 | ret = -4; |
| 1559 | if (ret) |
| 1560 | printf("FAILURE\n"); |
| 1561 | else |
| 1562 | printf("SUCCESS\n"); |
| 1563 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1564 | crypto_unload(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1565 | os_program_deinit(); |
| 1566 | |
| 1567 | return ret; |
| 1568 | } |