Home | History | Annotate | Download | only in eap_server
      1 /*
      2  * hostapd / EAP-PSK (RFC 4764) 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  * Note: EAP-PSK is an EAP authentication method and as such, completely
      9  * different from WPA-PSK. This file is not needed for WPA-PSK functionality.
     10  */
     11 
     12 #include "includes.h"
     13 
     14 #include "common.h"
     15 #include "crypto/aes_wrap.h"
     16 #include "crypto/random.h"
     17 #include "eap_common/eap_psk_common.h"
     18 #include "eap_server/eap_i.h"
     19 
     20 
     21 struct eap_psk_data {
     22 	enum { PSK_1, PSK_3, SUCCESS, FAILURE } state;
     23 	u8 rand_s[EAP_PSK_RAND_LEN];
     24 	u8 rand_p[EAP_PSK_RAND_LEN];
     25 	u8 *id_p;
     26 	size_t id_p_len;
     27 	u8 ak[EAP_PSK_AK_LEN], kdk[EAP_PSK_KDK_LEN], tek[EAP_PSK_TEK_LEN];
     28 	u8 msk[EAP_MSK_LEN];
     29 	u8 emsk[EAP_EMSK_LEN];
     30 };
     31 
     32 
     33 static void * eap_psk_init(struct eap_sm *sm)
     34 {
     35 	struct eap_psk_data *data;
     36 
     37 	data = os_zalloc(sizeof(*data));
     38 	if (data == NULL)
     39 		return NULL;
     40 	data->state = PSK_1;
     41 
     42 	return data;
     43 }
     44 
     45 
     46 static void eap_psk_reset(struct eap_sm *sm, void *priv)
     47 {
     48 	struct eap_psk_data *data = priv;
     49 	os_free(data->id_p);
     50 	os_free(data);
     51 }
     52 
     53 
     54 static struct wpabuf * eap_psk_build_1(struct eap_sm *sm,
     55 				       struct eap_psk_data *data, u8 id)
     56 {
     57 	struct wpabuf *req;
     58 	struct eap_psk_hdr_1 *psk;
     59 
     60 	wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-1 (sending)");
     61 
     62 	if (random_get_bytes(data->rand_s, EAP_PSK_RAND_LEN)) {
     63 		wpa_printf(MSG_ERROR, "EAP-PSK: Failed to get random data");
     64 		data->state = FAILURE;
     65 		return NULL;
     66 	}
     67 	wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_S (server rand)",
     68 		    data->rand_s, EAP_PSK_RAND_LEN);
     69 
     70 	req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
     71 			    sizeof(*psk) + sm->server_id_len,
     72 			    EAP_CODE_REQUEST, id);
     73 	if (req == NULL) {
     74 		wpa_printf(MSG_ERROR, "EAP-PSK: Failed to allocate memory "
     75 			   "request");
     76 		data->state = FAILURE;
     77 		return NULL;
     78 	}
     79 
     80 	psk = wpabuf_put(req, sizeof(*psk));
     81 	psk->flags = EAP_PSK_FLAGS_SET_T(0); /* T=0 */
     82 	os_memcpy(psk->rand_s, data->rand_s, EAP_PSK_RAND_LEN);
     83 	wpabuf_put_data(req, sm->server_id, sm->server_id_len);
     84 
     85 	return req;
     86 }
     87 
     88 
     89 static struct wpabuf * eap_psk_build_3(struct eap_sm *sm,
     90 				       struct eap_psk_data *data, u8 id)
     91 {
     92 	struct wpabuf *req;
     93 	struct eap_psk_hdr_3 *psk;
     94 	u8 *buf, *pchannel, nonce[16];
     95 	size_t buflen;
     96 
     97 	wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-3 (sending)");
     98 
     99 	req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
    100 			    sizeof(*psk) + 4 + 16 + 1, EAP_CODE_REQUEST, id);
    101 	if (req == NULL) {
    102 		wpa_printf(MSG_ERROR, "EAP-PSK: Failed to allocate memory "
    103 			   "request");
    104 		data->state = FAILURE;
    105 		return NULL;
    106 	}
    107 
    108 	psk = wpabuf_put(req, sizeof(*psk));
    109 	psk->flags = EAP_PSK_FLAGS_SET_T(2); /* T=2 */
    110 	os_memcpy(psk->rand_s, data->rand_s, EAP_PSK_RAND_LEN);
    111 
    112 	/* MAC_S = OMAC1-AES-128(AK, ID_S||RAND_P) */
    113 	buflen = sm->server_id_len + EAP_PSK_RAND_LEN;
    114 	buf = os_malloc(buflen);
    115 	if (buf == NULL)
    116 		goto fail;
    117 
    118 	os_memcpy(buf, sm->server_id, sm->server_id_len);
    119 	os_memcpy(buf + sm->server_id_len, data->rand_p, EAP_PSK_RAND_LEN);
    120 	if (omac1_aes_128(data->ak, buf, buflen, psk->mac_s)) {
    121 		os_free(buf);
    122 		goto fail;
    123 	}
    124 	os_free(buf);
    125 
    126 	if (eap_psk_derive_keys(data->kdk, data->rand_p, data->tek, data->msk,
    127 				data->emsk))
    128 		goto fail;
    129 	wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: TEK", data->tek, EAP_PSK_TEK_LEN);
    130 	wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: MSK", data->msk, EAP_MSK_LEN);
    131 	wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: EMSK", data->emsk, EAP_EMSK_LEN);
    132 
    133 	os_memset(nonce, 0, sizeof(nonce));
    134 	pchannel = wpabuf_put(req, 4 + 16 + 1);
    135 	os_memcpy(pchannel, nonce + 12, 4);
    136 	os_memset(pchannel + 4, 0, 16); /* Tag */
    137 	pchannel[4 + 16] = EAP_PSK_R_FLAG_DONE_SUCCESS << 6;
    138 	wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (plaintext)",
    139 		    pchannel, 4 + 16 + 1);
    140 	if (aes_128_eax_encrypt(data->tek, nonce, sizeof(nonce),
    141 				wpabuf_head(req), 22,
    142 				pchannel + 4 + 16, 1, pchannel + 4))
    143 		goto fail;
    144 	wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (encrypted)",
    145 		    pchannel, 4 + 16 + 1);
    146 
    147 	return req;
    148 
    149 fail:
    150 	wpabuf_free(req);
    151 	data->state = FAILURE;
    152 	return NULL;
    153 }
    154 
    155 
    156 static struct wpabuf * eap_psk_buildReq(struct eap_sm *sm, void *priv, u8 id)
    157 {
    158 	struct eap_psk_data *data = priv;
    159 
    160 	switch (data->state) {
    161 	case PSK_1:
    162 		return eap_psk_build_1(sm, data, id);
    163 	case PSK_3:
    164 		return eap_psk_build_3(sm, data, id);
    165 	default:
    166 		wpa_printf(MSG_DEBUG, "EAP-PSK: Unknown state %d in buildReq",
    167 			   data->state);
    168 		break;
    169 	}
    170 	return NULL;
    171 }
    172 
    173 
    174 static Boolean eap_psk_check(struct eap_sm *sm, void *priv,
    175 			     struct wpabuf *respData)
    176 {
    177 	struct eap_psk_data *data = priv;
    178 	size_t len;
    179 	u8 t;
    180 	const u8 *pos;
    181 
    182 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
    183 	if (pos == NULL || len < 1) {
    184 		wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
    185 		return TRUE;
    186 	}
    187 	t = EAP_PSK_FLAGS_GET_T(*pos);
    188 
    189 	wpa_printf(MSG_DEBUG, "EAP-PSK: received frame: T=%d", t);
    190 
    191 	if (data->state == PSK_1 && t != 1) {
    192 		wpa_printf(MSG_DEBUG, "EAP-PSK: Expected PSK-2 - "
    193 			   "ignore T=%d", t);
    194 		return TRUE;
    195 	}
    196 
    197 	if (data->state == PSK_3 && t != 3) {
    198 		wpa_printf(MSG_DEBUG, "EAP-PSK: Expected PSK-4 - "
    199 			   "ignore T=%d", t);
    200 		return TRUE;
    201 	}
    202 
    203 	if ((t == 1 && len < sizeof(struct eap_psk_hdr_2)) ||
    204 	    (t == 3 && len < sizeof(struct eap_psk_hdr_4))) {
    205 		wpa_printf(MSG_DEBUG, "EAP-PSK: Too short frame");
    206 		return TRUE;
    207 	}
    208 
    209 	return FALSE;
    210 }
    211 
    212 
    213 static void eap_psk_process_2(struct eap_sm *sm,
    214 			      struct eap_psk_data *data,
    215 			      struct wpabuf *respData)
    216 {
    217 	const struct eap_psk_hdr_2 *resp;
    218 	u8 *pos, mac[EAP_PSK_MAC_LEN], *buf;
    219 	size_t left, buflen;
    220 	int i;
    221 	const u8 *cpos;
    222 
    223 	if (data->state != PSK_1)
    224 		return;
    225 
    226 	wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-2");
    227 
    228 	cpos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData,
    229 				&left);
    230 	if (cpos == NULL || left < sizeof(*resp)) {
    231 		wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
    232 		return;
    233 	}
    234 	resp = (const struct eap_psk_hdr_2 *) cpos;
    235 	cpos = (const u8 *) (resp + 1);
    236 	left -= sizeof(*resp);
    237 
    238 	os_free(data->id_p);
    239 	data->id_p = os_malloc(left);
    240 	if (data->id_p == NULL) {
    241 		wpa_printf(MSG_INFO, "EAP-PSK: Failed to allocate memory for "
    242 			   "ID_P");
    243 		return;
    244 	}
    245 	os_memcpy(data->id_p, cpos, left);
    246 	data->id_p_len = left;
    247 	wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-PSK: ID_P",
    248 			  data->id_p, data->id_p_len);
    249 
    250 	if (eap_user_get(sm, data->id_p, data->id_p_len, 0) < 0) {
    251 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-PSK: unknown ID_P",
    252 				  data->id_p, data->id_p_len);
    253 		data->state = FAILURE;
    254 		return;
    255 	}
    256 
    257 	for (i = 0;
    258 	     i < EAP_MAX_METHODS &&
    259 		     (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
    260 		      sm->user->methods[i].method != EAP_TYPE_NONE);
    261 	     i++) {
    262 		if (sm->user->methods[i].vendor == EAP_VENDOR_IETF &&
    263 		    sm->user->methods[i].method == EAP_TYPE_PSK)
    264 			break;
    265 	}
    266 
    267 	if (i >= EAP_MAX_METHODS ||
    268 	    sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
    269 	    sm->user->methods[i].method != EAP_TYPE_PSK) {
    270 		wpa_hexdump_ascii(MSG_DEBUG,
    271 				  "EAP-PSK: EAP-PSK not enabled for ID_P",
    272 				  data->id_p, data->id_p_len);
    273 		data->state = FAILURE;
    274 		return;
    275 	}
    276 
    277 	if (sm->user->password == NULL ||
    278 	    sm->user->password_len != EAP_PSK_PSK_LEN) {
    279 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-PSK: invalid password in "
    280 				  "user database for ID_P",
    281 				  data->id_p, data->id_p_len);
    282 		data->state = FAILURE;
    283 		return;
    284 	}
    285 	if (eap_psk_key_setup(sm->user->password, data->ak, data->kdk)) {
    286 		data->state = FAILURE;
    287 		return;
    288 	}
    289 	wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: AK", data->ak, EAP_PSK_AK_LEN);
    290 	wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: KDK", data->kdk, EAP_PSK_KDK_LEN);
    291 
    292 	wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_P (client rand)",
    293 		    resp->rand_p, EAP_PSK_RAND_LEN);
    294 	os_memcpy(data->rand_p, resp->rand_p, EAP_PSK_RAND_LEN);
    295 
    296 	/* MAC_P = OMAC1-AES-128(AK, ID_P||ID_S||RAND_S||RAND_P) */
    297 	buflen = data->id_p_len + sm->server_id_len + 2 * EAP_PSK_RAND_LEN;
    298 	buf = os_malloc(buflen);
    299 	if (buf == NULL) {
    300 		data->state = FAILURE;
    301 		return;
    302 	}
    303 	os_memcpy(buf, data->id_p, data->id_p_len);
    304 	pos = buf + data->id_p_len;
    305 	os_memcpy(pos, sm->server_id, sm->server_id_len);
    306 	pos += sm->server_id_len;
    307 	os_memcpy(pos, data->rand_s, EAP_PSK_RAND_LEN);
    308 	pos += EAP_PSK_RAND_LEN;
    309 	os_memcpy(pos, data->rand_p, EAP_PSK_RAND_LEN);
    310 	if (omac1_aes_128(data->ak, buf, buflen, mac)) {
    311 		os_free(buf);
    312 		data->state = FAILURE;
    313 		return;
    314 	}
    315 	os_free(buf);
    316 	wpa_hexdump(MSG_DEBUG, "EAP-PSK: MAC_P", resp->mac_p, EAP_PSK_MAC_LEN);
    317 	if (os_memcmp(mac, resp->mac_p, EAP_PSK_MAC_LEN) != 0) {
    318 		wpa_printf(MSG_INFO, "EAP-PSK: Invalid MAC_P");
    319 		wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: Expected MAC_P",
    320 			    mac, EAP_PSK_MAC_LEN);
    321 		data->state = FAILURE;
    322 		return;
    323 	}
    324 
    325 	data->state = PSK_3;
    326 }
    327 
    328 
    329 static void eap_psk_process_4(struct eap_sm *sm,
    330 			      struct eap_psk_data *data,
    331 			      struct wpabuf *respData)
    332 {
    333 	const struct eap_psk_hdr_4 *resp;
    334 	u8 *decrypted, nonce[16];
    335 	size_t left;
    336 	const u8 *pos, *tag;
    337 
    338 	if (data->state != PSK_3)
    339 		return;
    340 
    341 	wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-4");
    342 
    343 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &left);
    344 	if (pos == NULL || left < sizeof(*resp)) {
    345 		wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
    346 		return;
    347 	}
    348 	resp = (const struct eap_psk_hdr_4 *) pos;
    349 	pos = (const u8 *) (resp + 1);
    350 	left -= sizeof(*resp);
    351 
    352 	wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: Encrypted PCHANNEL", pos, left);
    353 
    354 	if (left < 4 + 16 + 1) {
    355 		wpa_printf(MSG_INFO, "EAP-PSK: Too short PCHANNEL data in "
    356 			   "PSK-4 (len=%lu, expected 21)",
    357 			   (unsigned long) left);
    358 		return;
    359 	}
    360 
    361 	if (pos[0] == 0 && pos[1] == 0 && pos[2] == 0 && pos[3] == 0) {
    362 		wpa_printf(MSG_DEBUG, "EAP-PSK: Nonce did not increase");
    363 		return;
    364 	}
    365 
    366 	os_memset(nonce, 0, 12);
    367 	os_memcpy(nonce + 12, pos, 4);
    368 	pos += 4;
    369 	left -= 4;
    370 	tag = pos;
    371 	pos += 16;
    372 	left -= 16;
    373 
    374 	decrypted = os_malloc(left);
    375 	if (decrypted == NULL)
    376 		return;
    377 	os_memcpy(decrypted, pos, left);
    378 
    379 	if (aes_128_eax_decrypt(data->tek, nonce, sizeof(nonce),
    380 				wpabuf_head(respData), 22, decrypted, left,
    381 				tag)) {
    382 		wpa_printf(MSG_WARNING, "EAP-PSK: PCHANNEL decryption failed");
    383 		os_free(decrypted);
    384 		data->state = FAILURE;
    385 		return;
    386 	}
    387 	wpa_hexdump(MSG_DEBUG, "EAP-PSK: Decrypted PCHANNEL message",
    388 		    decrypted, left);
    389 
    390 	/* Verify R flag */
    391 	switch (decrypted[0] >> 6) {
    392 	case EAP_PSK_R_FLAG_CONT:
    393 		wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - CONT - unsupported");
    394 		data->state = FAILURE;
    395 		break;
    396 	case EAP_PSK_R_FLAG_DONE_SUCCESS:
    397 		wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - DONE_SUCCESS");
    398 		data->state = SUCCESS;
    399 		break;
    400 	case EAP_PSK_R_FLAG_DONE_FAILURE:
    401 		wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - DONE_FAILURE");
    402 		data->state = FAILURE;
    403 		break;
    404 	}
    405 	os_free(decrypted);
    406 }
    407 
    408 
    409 static void eap_psk_process(struct eap_sm *sm, void *priv,
    410 			    struct wpabuf *respData)
    411 {
    412 	struct eap_psk_data *data = priv;
    413 	const u8 *pos;
    414 	size_t len;
    415 
    416 	if (sm->user == NULL || sm->user->password == NULL) {
    417 		wpa_printf(MSG_INFO, "EAP-PSK: Plaintext password not "
    418 			   "configured");
    419 		data->state = FAILURE;
    420 		return;
    421 	}
    422 
    423 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
    424 	if (pos == NULL || len < 1)
    425 		return;
    426 
    427 	switch (EAP_PSK_FLAGS_GET_T(*pos)) {
    428 	case 1:
    429 		eap_psk_process_2(sm, data, respData);
    430 		break;
    431 	case 3:
    432 		eap_psk_process_4(sm, data, respData);
    433 		break;
    434 	}
    435 }
    436 
    437 
    438 static Boolean eap_psk_isDone(struct eap_sm *sm, void *priv)
    439 {
    440 	struct eap_psk_data *data = priv;
    441 	return data->state == SUCCESS || data->state == FAILURE;
    442 }
    443 
    444 
    445 static u8 * eap_psk_getKey(struct eap_sm *sm, void *priv, size_t *len)
    446 {
    447 	struct eap_psk_data *data = priv;
    448 	u8 *key;
    449 
    450 	if (data->state != SUCCESS)
    451 		return NULL;
    452 
    453 	key = os_malloc(EAP_MSK_LEN);
    454 	if (key == NULL)
    455 		return NULL;
    456 	os_memcpy(key, data->msk, EAP_MSK_LEN);
    457 	*len = EAP_MSK_LEN;
    458 
    459 	return key;
    460 }
    461 
    462 
    463 static u8 * eap_psk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
    464 {
    465 	struct eap_psk_data *data = priv;
    466 	u8 *key;
    467 
    468 	if (data->state != SUCCESS)
    469 		return NULL;
    470 
    471 	key = os_malloc(EAP_EMSK_LEN);
    472 	if (key == NULL)
    473 		return NULL;
    474 	os_memcpy(key, data->emsk, EAP_EMSK_LEN);
    475 	*len = EAP_EMSK_LEN;
    476 
    477 	return key;
    478 }
    479 
    480 
    481 static Boolean eap_psk_isSuccess(struct eap_sm *sm, void *priv)
    482 {
    483 	struct eap_psk_data *data = priv;
    484 	return data->state == SUCCESS;
    485 }
    486 
    487 
    488 int eap_server_psk_register(void)
    489 {
    490 	struct eap_method *eap;
    491 	int ret;
    492 
    493 	eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
    494 				      EAP_VENDOR_IETF, EAP_TYPE_PSK, "PSK");
    495 	if (eap == NULL)
    496 		return -1;
    497 
    498 	eap->init = eap_psk_init;
    499 	eap->reset = eap_psk_reset;
    500 	eap->buildReq = eap_psk_buildReq;
    501 	eap->check = eap_psk_check;
    502 	eap->process = eap_psk_process;
    503 	eap->isDone = eap_psk_isDone;
    504 	eap->getKey = eap_psk_getKey;
    505 	eap->isSuccess = eap_psk_isSuccess;
    506 	eap->get_emsk = eap_psk_get_emsk;
    507 
    508 	ret = eap_server_method_register(eap);
    509 	if (ret)
    510 		eap_server_method_free(eap);
    511 	return ret;
    512 }
    513