1 /* 2 * hostapd / EAP-PAX (RFC 4746) server 3 * Copyright (c) 2005-2007, Jouni Malinen <j (at) w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "includes.h" 10 11 #include "common.h" 12 #include "crypto/random.h" 13 #include "eap_server/eap_i.h" 14 #include "eap_common/eap_pax_common.h" 15 16 /* 17 * Note: only PAX_STD subprotocol is currently supported 18 * 19 * TODO: Add support with PAX_SEC with the mandatory to implement ciphersuite 20 * (HMAC_SHA1_128, IANA DH Group 14 (2048 bits), RSA-PKCS1-V1_5) and 21 * recommended ciphersuite (HMAC_SHA256_128, IANA DH Group 15 (3072 bits), 22 * RSAES-OAEP). 23 */ 24 25 struct eap_pax_data { 26 enum { PAX_STD_1, PAX_STD_3, SUCCESS, FAILURE } state; 27 u8 mac_id; 28 union { 29 u8 e[2 * EAP_PAX_RAND_LEN]; 30 struct { 31 u8 x[EAP_PAX_RAND_LEN]; /* server rand */ 32 u8 y[EAP_PAX_RAND_LEN]; /* client rand */ 33 } r; 34 } rand; 35 u8 ak[EAP_PAX_AK_LEN]; 36 u8 mk[EAP_PAX_MK_LEN]; 37 u8 ck[EAP_PAX_CK_LEN]; 38 u8 ick[EAP_PAX_ICK_LEN]; 39 u8 mid[EAP_PAX_MID_LEN]; 40 int keys_set; 41 char *cid; 42 size_t cid_len; 43 }; 44 45 46 static void * eap_pax_init(struct eap_sm *sm) 47 { 48 struct eap_pax_data *data; 49 50 data = os_zalloc(sizeof(*data)); 51 if (data == NULL) 52 return NULL; 53 data->state = PAX_STD_1; 54 /* 55 * TODO: make this configurable once EAP_PAX_HMAC_SHA256_128 is 56 * supported 57 */ 58 data->mac_id = EAP_PAX_MAC_HMAC_SHA1_128; 59 60 return data; 61 } 62 63 64 static void eap_pax_reset(struct eap_sm *sm, void *priv) 65 { 66 struct eap_pax_data *data = priv; 67 os_free(data->cid); 68 bin_clear_free(data, sizeof(*data)); 69 } 70 71 72 static struct wpabuf * eap_pax_build_std_1(struct eap_sm *sm, 73 struct eap_pax_data *data, u8 id) 74 { 75 struct wpabuf *req; 76 struct eap_pax_hdr *pax; 77 u8 *pos; 78 79 wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-1 (sending)"); 80 81 if (random_get_bytes(data->rand.r.x, EAP_PAX_RAND_LEN)) { 82 wpa_printf(MSG_ERROR, "EAP-PAX: Failed to get random data"); 83 data->state = FAILURE; 84 return NULL; 85 } 86 87 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PAX, 88 sizeof(*pax) + 2 + EAP_PAX_RAND_LEN + 89 EAP_PAX_ICV_LEN, EAP_CODE_REQUEST, id); 90 if (req == NULL) { 91 wpa_printf(MSG_ERROR, "EAP-PAX: Failed to allocate memory " 92 "request"); 93 data->state = FAILURE; 94 return NULL; 95 } 96 97 pax = wpabuf_put(req, sizeof(*pax)); 98 pax->op_code = EAP_PAX_OP_STD_1; 99 pax->flags = 0; 100 pax->mac_id = data->mac_id; 101 pax->dh_group_id = EAP_PAX_DH_GROUP_NONE; 102 pax->public_key_id = EAP_PAX_PUBLIC_KEY_NONE; 103 104 wpabuf_put_be16(req, EAP_PAX_RAND_LEN); 105 wpabuf_put_data(req, data->rand.r.x, EAP_PAX_RAND_LEN); 106 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: A = X (server rand)", 107 data->rand.r.x, EAP_PAX_RAND_LEN); 108 109 pos = wpabuf_put(req, EAP_PAX_MAC_LEN); 110 eap_pax_mac(data->mac_id, (u8 *) "", 0, 111 wpabuf_mhead(req), wpabuf_len(req) - EAP_PAX_ICV_LEN, 112 NULL, 0, NULL, 0, pos); 113 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", pos, EAP_PAX_ICV_LEN); 114 115 return req; 116 } 117 118 119 static struct wpabuf * eap_pax_build_std_3(struct eap_sm *sm, 120 struct eap_pax_data *data, u8 id) 121 { 122 struct wpabuf *req; 123 struct eap_pax_hdr *pax; 124 u8 *pos; 125 126 wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-3 (sending)"); 127 128 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PAX, 129 sizeof(*pax) + 2 + EAP_PAX_MAC_LEN + 130 EAP_PAX_ICV_LEN, EAP_CODE_REQUEST, id); 131 if (req == NULL) { 132 wpa_printf(MSG_ERROR, "EAP-PAX: Failed to allocate memory " 133 "request"); 134 data->state = FAILURE; 135 return NULL; 136 } 137 138 pax = wpabuf_put(req, sizeof(*pax)); 139 pax->op_code = EAP_PAX_OP_STD_3; 140 pax->flags = 0; 141 pax->mac_id = data->mac_id; 142 pax->dh_group_id = EAP_PAX_DH_GROUP_NONE; 143 pax->public_key_id = EAP_PAX_PUBLIC_KEY_NONE; 144 145 wpabuf_put_be16(req, EAP_PAX_MAC_LEN); 146 pos = wpabuf_put(req, EAP_PAX_MAC_LEN); 147 eap_pax_mac(data->mac_id, data->ck, EAP_PAX_CK_LEN, 148 data->rand.r.y, EAP_PAX_RAND_LEN, 149 (u8 *) data->cid, data->cid_len, NULL, 0, pos); 150 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: MAC_CK(B, CID)", 151 pos, EAP_PAX_MAC_LEN); 152 153 /* Optional ADE could be added here, if needed */ 154 155 pos = wpabuf_put(req, EAP_PAX_MAC_LEN); 156 eap_pax_mac(data->mac_id, data->ick, EAP_PAX_ICK_LEN, 157 wpabuf_mhead(req), wpabuf_len(req) - EAP_PAX_ICV_LEN, 158 NULL, 0, NULL, 0, pos); 159 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", pos, EAP_PAX_ICV_LEN); 160 161 return req; 162 } 163 164 165 static struct wpabuf * eap_pax_buildReq(struct eap_sm *sm, void *priv, u8 id) 166 { 167 struct eap_pax_data *data = priv; 168 169 switch (data->state) { 170 case PAX_STD_1: 171 return eap_pax_build_std_1(sm, data, id); 172 case PAX_STD_3: 173 return eap_pax_build_std_3(sm, data, id); 174 default: 175 wpa_printf(MSG_DEBUG, "EAP-PAX: Unknown state %d in buildReq", 176 data->state); 177 break; 178 } 179 return NULL; 180 } 181 182 183 static Boolean eap_pax_check(struct eap_sm *sm, void *priv, 184 struct wpabuf *respData) 185 { 186 struct eap_pax_data *data = priv; 187 struct eap_pax_hdr *resp; 188 const u8 *pos; 189 size_t len, mlen; 190 u8 icvbuf[EAP_PAX_ICV_LEN], *icv; 191 192 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PAX, respData, &len); 193 if (pos == NULL || len < sizeof(*resp)) { 194 wpa_printf(MSG_INFO, "EAP-PAX: Invalid frame"); 195 return TRUE; 196 } 197 198 mlen = sizeof(struct eap_hdr) + 1 + len; 199 resp = (struct eap_pax_hdr *) pos; 200 201 wpa_printf(MSG_DEBUG, "EAP-PAX: received frame: op_code 0x%x " 202 "flags 0x%x mac_id 0x%x dh_group_id 0x%x " 203 "public_key_id 0x%x", 204 resp->op_code, resp->flags, resp->mac_id, resp->dh_group_id, 205 resp->public_key_id); 206 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: received payload", 207 (u8 *) (resp + 1), len - sizeof(*resp) - EAP_PAX_ICV_LEN); 208 209 if (data->state == PAX_STD_1 && 210 resp->op_code != EAP_PAX_OP_STD_2) { 211 wpa_printf(MSG_DEBUG, "EAP-PAX: Expected PAX_STD-2 - " 212 "ignore op %d", resp->op_code); 213 return TRUE; 214 } 215 216 if (data->state == PAX_STD_3 && 217 resp->op_code != EAP_PAX_OP_ACK) { 218 wpa_printf(MSG_DEBUG, "EAP-PAX: Expected PAX-ACK - " 219 "ignore op %d", resp->op_code); 220 return TRUE; 221 } 222 223 if (resp->op_code != EAP_PAX_OP_STD_2 && 224 resp->op_code != EAP_PAX_OP_ACK) { 225 wpa_printf(MSG_DEBUG, "EAP-PAX: Unknown op_code 0x%x", 226 resp->op_code); 227 } 228 229 if (data->mac_id != resp->mac_id) { 230 wpa_printf(MSG_DEBUG, "EAP-PAX: Expected MAC ID 0x%x, " 231 "received 0x%x", data->mac_id, resp->mac_id); 232 return TRUE; 233 } 234 235 if (resp->dh_group_id != EAP_PAX_DH_GROUP_NONE) { 236 wpa_printf(MSG_INFO, "EAP-PAX: Expected DH Group ID 0x%x, " 237 "received 0x%x", EAP_PAX_DH_GROUP_NONE, 238 resp->dh_group_id); 239 return TRUE; 240 } 241 242 if (resp->public_key_id != EAP_PAX_PUBLIC_KEY_NONE) { 243 wpa_printf(MSG_INFO, "EAP-PAX: Expected Public Key ID 0x%x, " 244 "received 0x%x", EAP_PAX_PUBLIC_KEY_NONE, 245 resp->public_key_id); 246 return TRUE; 247 } 248 249 if (resp->flags & EAP_PAX_FLAGS_MF) { 250 /* TODO: add support for reassembling fragments */ 251 wpa_printf(MSG_INFO, "EAP-PAX: fragmentation not supported"); 252 return TRUE; 253 } 254 255 if (resp->flags & EAP_PAX_FLAGS_CE) { 256 wpa_printf(MSG_INFO, "EAP-PAX: Unexpected CE flag"); 257 return TRUE; 258 } 259 260 if (data->keys_set) { 261 if (len - sizeof(*resp) < EAP_PAX_ICV_LEN) { 262 wpa_printf(MSG_INFO, "EAP-PAX: No ICV in the packet"); 263 return TRUE; 264 } 265 icv = wpabuf_mhead_u8(respData) + mlen - EAP_PAX_ICV_LEN; 266 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", icv, EAP_PAX_ICV_LEN); 267 eap_pax_mac(data->mac_id, data->ick, EAP_PAX_ICK_LEN, 268 wpabuf_mhead(respData), 269 wpabuf_len(respData) - EAP_PAX_ICV_LEN, 270 NULL, 0, NULL, 0, icvbuf); 271 if (os_memcmp_const(icvbuf, icv, EAP_PAX_ICV_LEN) != 0) { 272 wpa_printf(MSG_INFO, "EAP-PAX: Invalid ICV"); 273 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: Expected ICV", 274 icvbuf, EAP_PAX_ICV_LEN); 275 return TRUE; 276 } 277 } 278 279 return FALSE; 280 } 281 282 283 static void eap_pax_process_std_2(struct eap_sm *sm, 284 struct eap_pax_data *data, 285 struct wpabuf *respData) 286 { 287 struct eap_pax_hdr *resp; 288 u8 mac[EAP_PAX_MAC_LEN], icvbuf[EAP_PAX_ICV_LEN]; 289 const u8 *pos; 290 size_t len, left, cid_len; 291 int i; 292 293 if (data->state != PAX_STD_1) 294 return; 295 296 wpa_printf(MSG_DEBUG, "EAP-PAX: Received PAX_STD-2"); 297 298 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PAX, respData, &len); 299 if (pos == NULL || len < sizeof(*resp) + EAP_PAX_ICV_LEN) 300 return; 301 302 resp = (struct eap_pax_hdr *) pos; 303 pos = (u8 *) (resp + 1); 304 left = len - sizeof(*resp); 305 306 if (left < 2 + EAP_PAX_RAND_LEN || 307 WPA_GET_BE16(pos) != EAP_PAX_RAND_LEN) { 308 wpa_printf(MSG_INFO, "EAP-PAX: Too short PAX_STD-2 (B)"); 309 return; 310 } 311 pos += 2; 312 left -= 2; 313 os_memcpy(data->rand.r.y, pos, EAP_PAX_RAND_LEN); 314 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: Y (client rand)", 315 data->rand.r.y, EAP_PAX_RAND_LEN); 316 pos += EAP_PAX_RAND_LEN; 317 left -= EAP_PAX_RAND_LEN; 318 319 if (left < 2 || (size_t) 2 + WPA_GET_BE16(pos) > left) { 320 wpa_printf(MSG_INFO, "EAP-PAX: Too short PAX_STD-2 (CID)"); 321 return; 322 } 323 cid_len = WPA_GET_BE16(pos); 324 if (cid_len > 1500) { 325 wpa_printf(MSG_INFO, "EAP-PAX: Too long CID"); 326 return; 327 } 328 data->cid_len = cid_len; 329 os_free(data->cid); 330 data->cid = os_malloc(data->cid_len); 331 if (data->cid == NULL) { 332 wpa_printf(MSG_INFO, "EAP-PAX: Failed to allocate memory for " 333 "CID"); 334 return; 335 } 336 os_memcpy(data->cid, pos + 2, data->cid_len); 337 pos += 2 + data->cid_len; 338 left -= 2 + data->cid_len; 339 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-PAX: CID", 340 (u8 *) data->cid, data->cid_len); 341 342 if (left < 2 + EAP_PAX_MAC_LEN || 343 WPA_GET_BE16(pos) != EAP_PAX_MAC_LEN) { 344 wpa_printf(MSG_INFO, "EAP-PAX: Too short PAX_STD-2 (MAC_CK)"); 345 return; 346 } 347 pos += 2; 348 left -= 2; 349 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: MAC_CK(A, B, CID)", 350 pos, EAP_PAX_MAC_LEN); 351 352 if (eap_user_get(sm, (u8 *) data->cid, data->cid_len, 0) < 0) { 353 wpa_hexdump_ascii(MSG_DEBUG, "EAP-PAX: unknown CID", 354 (u8 *) data->cid, data->cid_len); 355 data->state = FAILURE; 356 return; 357 } 358 359 for (i = 0; 360 i < EAP_MAX_METHODS && 361 (sm->user->methods[i].vendor != EAP_VENDOR_IETF || 362 sm->user->methods[i].method != EAP_TYPE_NONE); 363 i++) { 364 if (sm->user->methods[i].vendor == EAP_VENDOR_IETF && 365 sm->user->methods[i].method == EAP_TYPE_PAX) 366 break; 367 } 368 369 if (i >= EAP_MAX_METHODS || 370 sm->user->methods[i].vendor != EAP_VENDOR_IETF || 371 sm->user->methods[i].method != EAP_TYPE_PAX) { 372 wpa_hexdump_ascii(MSG_DEBUG, 373 "EAP-PAX: EAP-PAX not enabled for CID", 374 (u8 *) data->cid, data->cid_len); 375 data->state = FAILURE; 376 return; 377 } 378 379 if (sm->user->password == NULL || 380 sm->user->password_len != EAP_PAX_AK_LEN) { 381 wpa_hexdump_ascii(MSG_DEBUG, "EAP-PAX: invalid password in " 382 "user database for CID", 383 (u8 *) data->cid, data->cid_len); 384 data->state = FAILURE; 385 return; 386 } 387 os_memcpy(data->ak, sm->user->password, EAP_PAX_AK_LEN); 388 389 if (eap_pax_initial_key_derivation(data->mac_id, data->ak, 390 data->rand.e, data->mk, data->ck, 391 data->ick, data->mid) < 0) { 392 wpa_printf(MSG_INFO, "EAP-PAX: Failed to complete initial " 393 "key derivation"); 394 data->state = FAILURE; 395 return; 396 } 397 data->keys_set = 1; 398 399 eap_pax_mac(data->mac_id, data->ck, EAP_PAX_CK_LEN, 400 data->rand.r.x, EAP_PAX_RAND_LEN, 401 data->rand.r.y, EAP_PAX_RAND_LEN, 402 (u8 *) data->cid, data->cid_len, mac); 403 if (os_memcmp_const(mac, pos, EAP_PAX_MAC_LEN) != 0) { 404 wpa_printf(MSG_INFO, "EAP-PAX: Invalid MAC_CK(A, B, CID) in " 405 "PAX_STD-2"); 406 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: Expected MAC_CK(A, B, CID)", 407 mac, EAP_PAX_MAC_LEN); 408 data->state = FAILURE; 409 return; 410 } 411 412 pos += EAP_PAX_MAC_LEN; 413 left -= EAP_PAX_MAC_LEN; 414 415 if (left < EAP_PAX_ICV_LEN) { 416 wpa_printf(MSG_INFO, "EAP-PAX: Too short ICV (%lu) in " 417 "PAX_STD-2", (unsigned long) left); 418 return; 419 } 420 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", pos, EAP_PAX_ICV_LEN); 421 eap_pax_mac(data->mac_id, data->ick, EAP_PAX_ICK_LEN, 422 wpabuf_head(respData), 423 wpabuf_len(respData) - EAP_PAX_ICV_LEN, NULL, 0, NULL, 0, 424 icvbuf); 425 if (os_memcmp_const(icvbuf, pos, EAP_PAX_ICV_LEN) != 0) { 426 wpa_printf(MSG_INFO, "EAP-PAX: Invalid ICV in PAX_STD-2"); 427 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: Expected ICV", 428 icvbuf, EAP_PAX_ICV_LEN); 429 return; 430 } 431 pos += EAP_PAX_ICV_LEN; 432 left -= EAP_PAX_ICV_LEN; 433 434 if (left > 0) { 435 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ignored extra payload", 436 pos, left); 437 } 438 439 data->state = PAX_STD_3; 440 } 441 442 443 static void eap_pax_process_ack(struct eap_sm *sm, 444 struct eap_pax_data *data, 445 struct wpabuf *respData) 446 { 447 if (data->state != PAX_STD_3) 448 return; 449 450 wpa_printf(MSG_DEBUG, "EAP-PAX: Received PAX-ACK - authentication " 451 "completed successfully"); 452 data->state = SUCCESS; 453 } 454 455 456 static void eap_pax_process(struct eap_sm *sm, void *priv, 457 struct wpabuf *respData) 458 { 459 struct eap_pax_data *data = priv; 460 struct eap_pax_hdr *resp; 461 const u8 *pos; 462 size_t len; 463 464 if (sm->user == NULL || sm->user->password == NULL) { 465 wpa_printf(MSG_INFO, "EAP-PAX: Plaintext password not " 466 "configured"); 467 data->state = FAILURE; 468 return; 469 } 470 471 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PAX, respData, &len); 472 if (pos == NULL || len < sizeof(*resp)) 473 return; 474 475 resp = (struct eap_pax_hdr *) pos; 476 477 switch (resp->op_code) { 478 case EAP_PAX_OP_STD_2: 479 eap_pax_process_std_2(sm, data, respData); 480 break; 481 case EAP_PAX_OP_ACK: 482 eap_pax_process_ack(sm, data, respData); 483 break; 484 } 485 } 486 487 488 static Boolean eap_pax_isDone(struct eap_sm *sm, void *priv) 489 { 490 struct eap_pax_data *data = priv; 491 return data->state == SUCCESS || data->state == FAILURE; 492 } 493 494 495 static u8 * eap_pax_getKey(struct eap_sm *sm, void *priv, size_t *len) 496 { 497 struct eap_pax_data *data = priv; 498 u8 *key; 499 500 if (data->state != SUCCESS) 501 return NULL; 502 503 key = os_malloc(EAP_MSK_LEN); 504 if (key == NULL) 505 return NULL; 506 507 *len = EAP_MSK_LEN; 508 eap_pax_kdf(data->mac_id, data->mk, EAP_PAX_MK_LEN, 509 "Master Session Key", data->rand.e, 2 * EAP_PAX_RAND_LEN, 510 EAP_MSK_LEN, key); 511 512 return key; 513 } 514 515 516 static u8 * eap_pax_get_emsk(struct eap_sm *sm, void *priv, size_t *len) 517 { 518 struct eap_pax_data *data = priv; 519 u8 *key; 520 521 if (data->state != SUCCESS) 522 return NULL; 523 524 key = os_malloc(EAP_EMSK_LEN); 525 if (key == NULL) 526 return NULL; 527 528 *len = EAP_EMSK_LEN; 529 eap_pax_kdf(data->mac_id, data->mk, EAP_PAX_MK_LEN, 530 "Extended Master Session Key", 531 data->rand.e, 2 * EAP_PAX_RAND_LEN, 532 EAP_EMSK_LEN, key); 533 534 return key; 535 } 536 537 538 static Boolean eap_pax_isSuccess(struct eap_sm *sm, void *priv) 539 { 540 struct eap_pax_data *data = priv; 541 return data->state == SUCCESS; 542 } 543 544 545 static u8 * eap_pax_get_session_id(struct eap_sm *sm, void *priv, size_t *len) 546 { 547 struct eap_pax_data *data = priv; 548 u8 *sid; 549 550 if (data->state != SUCCESS) 551 return NULL; 552 553 sid = os_malloc(1 + EAP_PAX_MID_LEN); 554 if (sid == NULL) 555 return NULL; 556 557 *len = 1 + EAP_PAX_MID_LEN; 558 sid[0] = EAP_TYPE_PAX; 559 os_memcpy(sid + 1, data->mid, EAP_PAX_MID_LEN); 560 561 return sid; 562 } 563 564 565 int eap_server_pax_register(void) 566 { 567 struct eap_method *eap; 568 569 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION, 570 EAP_VENDOR_IETF, EAP_TYPE_PAX, "PAX"); 571 if (eap == NULL) 572 return -1; 573 574 eap->init = eap_pax_init; 575 eap->reset = eap_pax_reset; 576 eap->buildReq = eap_pax_buildReq; 577 eap->check = eap_pax_check; 578 eap->process = eap_pax_process; 579 eap->isDone = eap_pax_isDone; 580 eap->getKey = eap_pax_getKey; 581 eap->isSuccess = eap_pax_isSuccess; 582 eap->get_emsk = eap_pax_get_emsk; 583 eap->getSessionId = eap_pax_get_session_id; 584 585 return eap_server_method_register(eap); 586 } 587