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