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