2 * hostapd / EAP-PSK (RFC 4764) server
3 * Copyright (c) 2005-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * Note: EAP-PSK is an EAP authentication method and as such, completely
15 * different from WPA-PSK. This file is not needed for WPA-PSK functionality.
21 #include "crypto/aes_wrap.h"
22 #include "crypto/random.h"
23 #include "eap_common/eap_psk_common.h"
24 #include "eap_server/eap_i.h"
28 enum { PSK_1, PSK_3, SUCCESS, FAILURE } state;
29 u8 rand_s[EAP_PSK_RAND_LEN];
30 u8 rand_p[EAP_PSK_RAND_LEN];
32 size_t id_p_len, id_s_len;
33 u8 ak[EAP_PSK_AK_LEN], kdk[EAP_PSK_KDK_LEN], tek[EAP_PSK_TEK_LEN];
35 u8 emsk[EAP_EMSK_LEN];
39 static void * eap_psk_init(struct eap_sm *sm)
41 struct eap_psk_data *data;
43 data = os_zalloc(sizeof(*data));
47 data->id_s = (u8 *) "hostapd";
54 static void eap_psk_reset(struct eap_sm *sm, void *priv)
56 struct eap_psk_data *data = priv;
62 static struct wpabuf * eap_psk_build_1(struct eap_sm *sm,
63 struct eap_psk_data *data, u8 id)
66 struct eap_psk_hdr_1 *psk;
68 wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-1 (sending)");
70 if (random_get_bytes(data->rand_s, EAP_PSK_RAND_LEN)) {
71 wpa_printf(MSG_ERROR, "EAP-PSK: Failed to get random data");
72 data->state = FAILURE;
75 wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_S (server rand)",
76 data->rand_s, EAP_PSK_RAND_LEN);
78 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
79 sizeof(*psk) + data->id_s_len,
80 EAP_CODE_REQUEST, id);
82 wpa_printf(MSG_ERROR, "EAP-PSK: Failed to allocate memory "
84 data->state = FAILURE;
88 psk = wpabuf_put(req, sizeof(*psk));
89 psk->flags = EAP_PSK_FLAGS_SET_T(0); /* T=0 */
90 os_memcpy(psk->rand_s, data->rand_s, EAP_PSK_RAND_LEN);
91 wpabuf_put_data(req, data->id_s, data->id_s_len);
97 static struct wpabuf * eap_psk_build_3(struct eap_sm *sm,
98 struct eap_psk_data *data, u8 id)
101 struct eap_psk_hdr_3 *psk;
102 u8 *buf, *pchannel, nonce[16];
105 wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-3 (sending)");
107 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
108 sizeof(*psk) + 4 + 16 + 1, EAP_CODE_REQUEST, id);
110 wpa_printf(MSG_ERROR, "EAP-PSK: Failed to allocate memory "
112 data->state = FAILURE;
116 psk = wpabuf_put(req, sizeof(*psk));
117 psk->flags = EAP_PSK_FLAGS_SET_T(2); /* T=2 */
118 os_memcpy(psk->rand_s, data->rand_s, EAP_PSK_RAND_LEN);
120 /* MAC_S = OMAC1-AES-128(AK, ID_S||RAND_P) */
121 buflen = data->id_s_len + EAP_PSK_RAND_LEN;
122 buf = os_malloc(buflen);
126 os_memcpy(buf, data->id_s, data->id_s_len);
127 os_memcpy(buf + data->id_s_len, data->rand_p, EAP_PSK_RAND_LEN);
128 if (omac1_aes_128(data->ak, buf, buflen, psk->mac_s)) {
134 if (eap_psk_derive_keys(data->kdk, data->rand_p, data->tek, data->msk,
137 wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: TEK", data->tek, EAP_PSK_TEK_LEN);
138 wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: MSK", data->msk, EAP_MSK_LEN);
139 wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: EMSK", data->emsk, EAP_EMSK_LEN);
141 os_memset(nonce, 0, sizeof(nonce));
142 pchannel = wpabuf_put(req, 4 + 16 + 1);
143 os_memcpy(pchannel, nonce + 12, 4);
144 os_memset(pchannel + 4, 0, 16); /* Tag */
145 pchannel[4 + 16] = EAP_PSK_R_FLAG_DONE_SUCCESS << 6;
146 wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (plaintext)",
147 pchannel, 4 + 16 + 1);
148 if (aes_128_eax_encrypt(data->tek, nonce, sizeof(nonce),
149 wpabuf_head(req), 22,
150 pchannel + 4 + 16, 1, pchannel + 4))
152 wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (encrypted)",
153 pchannel, 4 + 16 + 1);
159 data->state = FAILURE;
164 static struct wpabuf * eap_psk_buildReq(struct eap_sm *sm, void *priv, u8 id)
166 struct eap_psk_data *data = priv;
168 switch (data->state) {
170 return eap_psk_build_1(sm, data, id);
172 return eap_psk_build_3(sm, data, id);
174 wpa_printf(MSG_DEBUG, "EAP-PSK: Unknown state %d in buildReq",
182 static Boolean eap_psk_check(struct eap_sm *sm, void *priv,
183 struct wpabuf *respData)
185 struct eap_psk_data *data = priv;
190 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
191 if (pos == NULL || len < 1) {
192 wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
195 t = EAP_PSK_FLAGS_GET_T(*pos);
197 wpa_printf(MSG_DEBUG, "EAP-PSK: received frame: T=%d", t);
199 if (data->state == PSK_1 && t != 1) {
200 wpa_printf(MSG_DEBUG, "EAP-PSK: Expected PSK-2 - "
205 if (data->state == PSK_3 && t != 3) {
206 wpa_printf(MSG_DEBUG, "EAP-PSK: Expected PSK-4 - "
211 if ((t == 1 && len < sizeof(struct eap_psk_hdr_2)) ||
212 (t == 3 && len < sizeof(struct eap_psk_hdr_4))) {
213 wpa_printf(MSG_DEBUG, "EAP-PSK: Too short frame");
221 static void eap_psk_process_2(struct eap_sm *sm,
222 struct eap_psk_data *data,
223 struct wpabuf *respData)
225 const struct eap_psk_hdr_2 *resp;
226 u8 *pos, mac[EAP_PSK_MAC_LEN], *buf;
231 if (data->state != PSK_1)
234 wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-2");
236 cpos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData,
238 if (cpos == NULL || left < sizeof(*resp)) {
239 wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
242 resp = (const struct eap_psk_hdr_2 *) cpos;
243 cpos = (const u8 *) (resp + 1);
244 left -= sizeof(*resp);
247 data->id_p = os_malloc(left);
248 if (data->id_p == NULL) {
249 wpa_printf(MSG_INFO, "EAP-PSK: Failed to allocate memory for "
253 os_memcpy(data->id_p, cpos, left);
254 data->id_p_len = left;
255 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-PSK: ID_P",
256 data->id_p, data->id_p_len);
258 if (eap_user_get(sm, data->id_p, data->id_p_len, 0) < 0) {
259 wpa_hexdump_ascii(MSG_DEBUG, "EAP-PSK: unknown ID_P",
260 data->id_p, data->id_p_len);
261 data->state = FAILURE;
266 i < EAP_MAX_METHODS &&
267 (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
268 sm->user->methods[i].method != EAP_TYPE_NONE);
270 if (sm->user->methods[i].vendor == EAP_VENDOR_IETF &&
271 sm->user->methods[i].method == EAP_TYPE_PSK)
275 if (i >= EAP_MAX_METHODS ||
276 sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
277 sm->user->methods[i].method != EAP_TYPE_PSK) {
278 wpa_hexdump_ascii(MSG_DEBUG,
279 "EAP-PSK: EAP-PSK not enabled for ID_P",
280 data->id_p, data->id_p_len);
281 data->state = FAILURE;
285 if (sm->user->password == NULL ||
286 sm->user->password_len != EAP_PSK_PSK_LEN) {
287 wpa_hexdump_ascii(MSG_DEBUG, "EAP-PSK: invalid password in "
288 "user database for ID_P",
289 data->id_p, data->id_p_len);
290 data->state = FAILURE;
293 if (eap_psk_key_setup(sm->user->password, data->ak, data->kdk)) {
294 data->state = FAILURE;
297 wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: AK", data->ak, EAP_PSK_AK_LEN);
298 wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: KDK", data->kdk, EAP_PSK_KDK_LEN);
300 wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_P (client rand)",
301 resp->rand_p, EAP_PSK_RAND_LEN);
302 os_memcpy(data->rand_p, resp->rand_p, EAP_PSK_RAND_LEN);
304 /* MAC_P = OMAC1-AES-128(AK, ID_P||ID_S||RAND_S||RAND_P) */
305 buflen = data->id_p_len + data->id_s_len + 2 * EAP_PSK_RAND_LEN;
306 buf = os_malloc(buflen);
308 data->state = FAILURE;
311 os_memcpy(buf, data->id_p, data->id_p_len);
312 pos = buf + data->id_p_len;
313 os_memcpy(pos, data->id_s, data->id_s_len);
314 pos += data->id_s_len;
315 os_memcpy(pos, data->rand_s, EAP_PSK_RAND_LEN);
316 pos += EAP_PSK_RAND_LEN;
317 os_memcpy(pos, data->rand_p, EAP_PSK_RAND_LEN);
318 if (omac1_aes_128(data->ak, buf, buflen, mac)) {
320 data->state = FAILURE;
324 wpa_hexdump(MSG_DEBUG, "EAP-PSK: MAC_P", resp->mac_p, EAP_PSK_MAC_LEN);
325 if (os_memcmp(mac, resp->mac_p, EAP_PSK_MAC_LEN) != 0) {
326 wpa_printf(MSG_INFO, "EAP-PSK: Invalid MAC_P");
327 wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: Expected MAC_P",
328 mac, EAP_PSK_MAC_LEN);
329 data->state = FAILURE;
337 static void eap_psk_process_4(struct eap_sm *sm,
338 struct eap_psk_data *data,
339 struct wpabuf *respData)
341 const struct eap_psk_hdr_4 *resp;
342 u8 *decrypted, nonce[16];
346 if (data->state != PSK_3)
349 wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-4");
351 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &left);
352 if (pos == NULL || left < sizeof(*resp)) {
353 wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
356 resp = (const struct eap_psk_hdr_4 *) pos;
357 pos = (const u8 *) (resp + 1);
358 left -= sizeof(*resp);
360 wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: Encrypted PCHANNEL", pos, left);
362 if (left < 4 + 16 + 1) {
363 wpa_printf(MSG_INFO, "EAP-PSK: Too short PCHANNEL data in "
364 "PSK-4 (len=%lu, expected 21)",
365 (unsigned long) left);
369 if (pos[0] == 0 && pos[1] == 0 && pos[2] == 0 && pos[3] == 0) {
370 wpa_printf(MSG_DEBUG, "EAP-PSK: Nonce did not increase");
374 os_memset(nonce, 0, 12);
375 os_memcpy(nonce + 12, pos, 4);
382 decrypted = os_malloc(left);
383 if (decrypted == NULL)
385 os_memcpy(decrypted, pos, left);
387 if (aes_128_eax_decrypt(data->tek, nonce, sizeof(nonce),
388 wpabuf_head(respData), 22, decrypted, left,
390 wpa_printf(MSG_WARNING, "EAP-PSK: PCHANNEL decryption failed");
392 data->state = FAILURE;
395 wpa_hexdump(MSG_DEBUG, "EAP-PSK: Decrypted PCHANNEL message",
399 switch (decrypted[0] >> 6) {
400 case EAP_PSK_R_FLAG_CONT:
401 wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - CONT - unsupported");
402 data->state = FAILURE;
404 case EAP_PSK_R_FLAG_DONE_SUCCESS:
405 wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - DONE_SUCCESS");
406 data->state = SUCCESS;
408 case EAP_PSK_R_FLAG_DONE_FAILURE:
409 wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - DONE_FAILURE");
410 data->state = FAILURE;
417 static void eap_psk_process(struct eap_sm *sm, void *priv,
418 struct wpabuf *respData)
420 struct eap_psk_data *data = priv;
424 if (sm->user == NULL || sm->user->password == NULL) {
425 wpa_printf(MSG_INFO, "EAP-PSK: Plaintext password not "
427 data->state = FAILURE;
431 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
432 if (pos == NULL || len < 1)
435 switch (EAP_PSK_FLAGS_GET_T(*pos)) {
437 eap_psk_process_2(sm, data, respData);
440 eap_psk_process_4(sm, data, respData);
446 static Boolean eap_psk_isDone(struct eap_sm *sm, void *priv)
448 struct eap_psk_data *data = priv;
449 return data->state == SUCCESS || data->state == FAILURE;
453 static u8 * eap_psk_getKey(struct eap_sm *sm, void *priv, size_t *len)
455 struct eap_psk_data *data = priv;
458 if (data->state != SUCCESS)
461 key = os_malloc(EAP_MSK_LEN);
464 os_memcpy(key, data->msk, EAP_MSK_LEN);
471 static u8 * eap_psk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
473 struct eap_psk_data *data = priv;
476 if (data->state != SUCCESS)
479 key = os_malloc(EAP_EMSK_LEN);
482 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
489 static Boolean eap_psk_isSuccess(struct eap_sm *sm, void *priv)
491 struct eap_psk_data *data = priv;
492 return data->state == SUCCESS;
496 int eap_server_psk_register(void)
498 struct eap_method *eap;
501 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
502 EAP_VENDOR_IETF, EAP_TYPE_PSK, "PSK");
506 eap->init = eap_psk_init;
507 eap->reset = eap_psk_reset;
508 eap->buildReq = eap_psk_buildReq;
509 eap->check = eap_psk_check;
510 eap->process = eap_psk_process;
511 eap->isDone = eap_psk_isDone;
512 eap->getKey = eap_psk_getKey;
513 eap->isSuccess = eap_psk_isSuccess;
514 eap->get_emsk = eap_psk_get_emsk;
516 ret = eap_server_method_register(eap);
518 eap_server_method_free(eap);