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 33 : static void * eap_psk_init(struct eap_sm *sm)
34 : {
35 : struct eap_psk_data *data;
36 :
37 33 : data = os_zalloc(sizeof(*data));
38 33 : if (data == NULL)
39 0 : return NULL;
40 33 : data->state = PSK_1;
41 :
42 33 : return data;
43 : }
44 :
45 :
46 33 : static void eap_psk_reset(struct eap_sm *sm, void *priv)
47 : {
48 33 : struct eap_psk_data *data = priv;
49 33 : os_free(data->id_p);
50 33 : bin_clear_free(data, sizeof(*data));
51 33 : }
52 :
53 :
54 33 : 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 33 : wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-1 (sending)");
61 :
62 33 : 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 33 : wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_S (server rand)",
68 33 : data->rand_s, EAP_PSK_RAND_LEN);
69 :
70 66 : req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
71 33 : sizeof(*psk) + sm->server_id_len,
72 : EAP_CODE_REQUEST, id);
73 33 : 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 33 : psk = wpabuf_put(req, sizeof(*psk));
81 33 : psk->flags = EAP_PSK_FLAGS_SET_T(0); /* T=0 */
82 33 : os_memcpy(psk->rand_s, data->rand_s, EAP_PSK_RAND_LEN);
83 33 : wpabuf_put_data(req, sm->server_id, sm->server_id_len);
84 :
85 33 : return req;
86 : }
87 :
88 :
89 32 : 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 32 : wpa_printf(MSG_DEBUG, "EAP-PSK: PSK-3 (sending)");
98 :
99 32 : req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PSK,
100 : sizeof(*psk) + 4 + 16 + 1, EAP_CODE_REQUEST, id);
101 32 : 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 32 : psk = wpabuf_put(req, sizeof(*psk));
109 32 : psk->flags = EAP_PSK_FLAGS_SET_T(2); /* T=2 */
110 32 : 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 32 : buflen = sm->server_id_len + EAP_PSK_RAND_LEN;
114 32 : buf = os_malloc(buflen);
115 32 : if (buf == NULL)
116 0 : goto fail;
117 :
118 32 : os_memcpy(buf, sm->server_id, sm->server_id_len);
119 32 : os_memcpy(buf + sm->server_id_len, data->rand_p, EAP_PSK_RAND_LEN);
120 32 : if (omac1_aes_128(data->ak, buf, buflen, psk->mac_s)) {
121 0 : os_free(buf);
122 0 : goto fail;
123 : }
124 32 : os_free(buf);
125 :
126 32 : if (eap_psk_derive_keys(data->kdk, data->rand_p, data->tek, data->msk,
127 32 : data->emsk))
128 0 : goto fail;
129 32 : wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: TEK", data->tek, EAP_PSK_TEK_LEN);
130 32 : wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: MSK", data->msk, EAP_MSK_LEN);
131 32 : wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: EMSK", data->emsk, EAP_EMSK_LEN);
132 :
133 32 : os_memset(nonce, 0, sizeof(nonce));
134 32 : pchannel = wpabuf_put(req, 4 + 16 + 1);
135 32 : os_memcpy(pchannel, nonce + 12, 4);
136 32 : os_memset(pchannel + 4, 0, 16); /* Tag */
137 32 : pchannel[4 + 16] = EAP_PSK_R_FLAG_DONE_SUCCESS << 6;
138 32 : wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (plaintext)",
139 : pchannel, 4 + 16 + 1);
140 64 : if (aes_128_eax_encrypt(data->tek, nonce, sizeof(nonce),
141 32 : wpabuf_head(req), 22,
142 : pchannel + 4 + 16, 1, pchannel + 4))
143 0 : goto fail;
144 32 : wpa_hexdump(MSG_DEBUG, "EAP-PSK: PCHANNEL (encrypted)",
145 : pchannel, 4 + 16 + 1);
146 :
147 32 : return req;
148 :
149 : fail:
150 0 : wpabuf_free(req);
151 0 : data->state = FAILURE;
152 0 : return NULL;
153 : }
154 :
155 :
156 65 : static struct wpabuf * eap_psk_buildReq(struct eap_sm *sm, void *priv, u8 id)
157 : {
158 65 : struct eap_psk_data *data = priv;
159 :
160 65 : switch (data->state) {
161 : case PSK_1:
162 33 : return eap_psk_build_1(sm, data, id);
163 : case PSK_3:
164 32 : 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 65 : static Boolean eap_psk_check(struct eap_sm *sm, void *priv,
175 : struct wpabuf *respData)
176 : {
177 65 : struct eap_psk_data *data = priv;
178 : size_t len;
179 : u8 t;
180 : const u8 *pos;
181 :
182 65 : pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
183 65 : if (pos == NULL || len < 1) {
184 0 : wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
185 0 : return TRUE;
186 : }
187 65 : t = EAP_PSK_FLAGS_GET_T(*pos);
188 :
189 65 : wpa_printf(MSG_DEBUG, "EAP-PSK: received frame: T=%d", t);
190 :
191 65 : 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 65 : 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 65 : if ((t == 1 && len < sizeof(struct eap_psk_hdr_2)) ||
204 32 : (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 65 : return FALSE;
210 : }
211 :
212 :
213 33 : 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 33 : if (data->state != PSK_1)
224 1 : return;
225 :
226 33 : wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-2");
227 :
228 33 : cpos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData,
229 : &left);
230 33 : if (cpos == NULL || left < sizeof(*resp)) {
231 0 : wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
232 0 : return;
233 : }
234 33 : resp = (const struct eap_psk_hdr_2 *) cpos;
235 33 : cpos = (const u8 *) (resp + 1);
236 33 : left -= sizeof(*resp);
237 :
238 33 : os_free(data->id_p);
239 33 : data->id_p = os_malloc(left);
240 33 : 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 33 : os_memcpy(data->id_p, cpos, left);
246 33 : data->id_p_len = left;
247 66 : wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-PSK: ID_P",
248 33 : data->id_p, data->id_p_len);
249 :
250 33 : 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 66 : for (i = 0;
258 33 : i < EAP_MAX_METHODS &&
259 66 : (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
260 33 : sm->user->methods[i].method != EAP_TYPE_NONE);
261 0 : i++) {
262 66 : if (sm->user->methods[i].vendor == EAP_VENDOR_IETF &&
263 33 : sm->user->methods[i].method == EAP_TYPE_PSK)
264 33 : break;
265 : }
266 :
267 66 : if (i >= EAP_MAX_METHODS ||
268 66 : sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
269 33 : 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 66 : if (sm->user->password == NULL ||
278 33 : 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 33 : if (eap_psk_key_setup(sm->user->password, data->ak, data->kdk)) {
286 0 : data->state = FAILURE;
287 0 : return;
288 : }
289 33 : wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: AK", data->ak, EAP_PSK_AK_LEN);
290 33 : wpa_hexdump_key(MSG_DEBUG, "EAP-PSK: KDK", data->kdk, EAP_PSK_KDK_LEN);
291 :
292 33 : wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: RAND_P (client rand)",
293 33 : resp->rand_p, EAP_PSK_RAND_LEN);
294 33 : 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 33 : buflen = data->id_p_len + sm->server_id_len + 2 * EAP_PSK_RAND_LEN;
298 33 : buf = os_malloc(buflen);
299 33 : if (buf == NULL) {
300 0 : data->state = FAILURE;
301 0 : return;
302 : }
303 33 : os_memcpy(buf, data->id_p, data->id_p_len);
304 33 : pos = buf + data->id_p_len;
305 33 : os_memcpy(pos, sm->server_id, sm->server_id_len);
306 33 : pos += sm->server_id_len;
307 33 : os_memcpy(pos, data->rand_s, EAP_PSK_RAND_LEN);
308 33 : pos += EAP_PSK_RAND_LEN;
309 33 : os_memcpy(pos, data->rand_p, EAP_PSK_RAND_LEN);
310 33 : if (omac1_aes_128(data->ak, buf, buflen, mac)) {
311 0 : os_free(buf);
312 0 : data->state = FAILURE;
313 0 : return;
314 : }
315 33 : os_free(buf);
316 33 : wpa_hexdump(MSG_DEBUG, "EAP-PSK: MAC_P", resp->mac_p, EAP_PSK_MAC_LEN);
317 33 : if (os_memcmp_const(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 32 : data->state = PSK_3;
326 : }
327 :
328 :
329 32 : 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 32 : if (data->state != PSK_3)
339 0 : return;
340 :
341 32 : wpa_printf(MSG_DEBUG, "EAP-PSK: Received PSK-4");
342 :
343 32 : pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &left);
344 32 : if (pos == NULL || left < sizeof(*resp)) {
345 0 : wpa_printf(MSG_INFO, "EAP-PSK: Invalid frame");
346 0 : return;
347 : }
348 32 : resp = (const struct eap_psk_hdr_4 *) pos;
349 32 : pos = (const u8 *) (resp + 1);
350 32 : left -= sizeof(*resp);
351 :
352 32 : wpa_hexdump(MSG_MSGDUMP, "EAP-PSK: Encrypted PCHANNEL", pos, left);
353 :
354 32 : 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 32 : 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 32 : os_memset(nonce, 0, 12);
367 32 : os_memcpy(nonce + 12, pos, 4);
368 32 : pos += 4;
369 32 : left -= 4;
370 32 : tag = pos;
371 32 : pos += 16;
372 32 : left -= 16;
373 :
374 32 : decrypted = os_malloc(left);
375 32 : if (decrypted == NULL)
376 0 : return;
377 32 : os_memcpy(decrypted, pos, left);
378 :
379 64 : if (aes_128_eax_decrypt(data->tek, nonce, sizeof(nonce),
380 32 : 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 32 : wpa_hexdump(MSG_DEBUG, "EAP-PSK: Decrypted PCHANNEL message",
388 : decrypted, left);
389 :
390 : /* Verify R flag */
391 32 : 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 32 : wpa_printf(MSG_DEBUG, "EAP-PSK: R flag - DONE_SUCCESS");
398 32 : data->state = SUCCESS;
399 32 : 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 32 : os_free(decrypted);
406 : }
407 :
408 :
409 65 : static void eap_psk_process(struct eap_sm *sm, void *priv,
410 : struct wpabuf *respData)
411 : {
412 65 : struct eap_psk_data *data = priv;
413 : const u8 *pos;
414 : size_t len;
415 :
416 65 : 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 65 : pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PSK, respData, &len);
424 65 : if (pos == NULL || len < 1)
425 0 : return;
426 :
427 65 : switch (EAP_PSK_FLAGS_GET_T(*pos)) {
428 : case 1:
429 33 : eap_psk_process_2(sm, data, respData);
430 33 : break;
431 : case 3:
432 32 : eap_psk_process_4(sm, data, respData);
433 32 : break;
434 : }
435 : }
436 :
437 :
438 66 : static Boolean eap_psk_isDone(struct eap_sm *sm, void *priv)
439 : {
440 66 : struct eap_psk_data *data = priv;
441 66 : return data->state == SUCCESS || data->state == FAILURE;
442 : }
443 :
444 :
445 33 : static u8 * eap_psk_getKey(struct eap_sm *sm, void *priv, size_t *len)
446 : {
447 33 : struct eap_psk_data *data = priv;
448 : u8 *key;
449 :
450 33 : if (data->state != SUCCESS)
451 1 : return NULL;
452 :
453 32 : key = os_malloc(EAP_MSK_LEN);
454 32 : if (key == NULL)
455 0 : return NULL;
456 32 : os_memcpy(key, data->msk, EAP_MSK_LEN);
457 32 : *len = EAP_MSK_LEN;
458 :
459 32 : return key;
460 : }
461 :
462 :
463 5 : static u8 * eap_psk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
464 : {
465 5 : struct eap_psk_data *data = priv;
466 : u8 *key;
467 :
468 5 : if (data->state != SUCCESS)
469 0 : return NULL;
470 :
471 5 : key = os_malloc(EAP_EMSK_LEN);
472 5 : if (key == NULL)
473 0 : return NULL;
474 5 : os_memcpy(key, data->emsk, EAP_EMSK_LEN);
475 5 : *len = EAP_EMSK_LEN;
476 :
477 5 : return key;
478 : }
479 :
480 :
481 34 : static Boolean eap_psk_isSuccess(struct eap_sm *sm, void *priv)
482 : {
483 34 : struct eap_psk_data *data = priv;
484 34 : return data->state == SUCCESS;
485 : }
486 :
487 :
488 33 : static u8 * eap_psk_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
489 : {
490 33 : struct eap_psk_data *data = priv;
491 : u8 *id;
492 :
493 33 : if (data->state != SUCCESS)
494 1 : return NULL;
495 :
496 32 : *len = 1 + 2 * EAP_PSK_RAND_LEN;
497 32 : id = os_malloc(*len);
498 32 : if (id == NULL)
499 0 : return NULL;
500 :
501 32 : id[0] = EAP_TYPE_PSK;
502 32 : os_memcpy(id + 1, data->rand_p, EAP_PSK_RAND_LEN);
503 32 : os_memcpy(id + 1 + EAP_PSK_RAND_LEN, data->rand_s, EAP_PSK_RAND_LEN);
504 32 : wpa_hexdump(MSG_DEBUG, "EAP-PSK: Derived Session-Id", id, *len);
505 :
506 32 : return id;
507 : }
508 :
509 :
510 10 : int eap_server_psk_register(void)
511 : {
512 : struct eap_method *eap;
513 : int ret;
514 :
515 10 : eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
516 : EAP_VENDOR_IETF, EAP_TYPE_PSK, "PSK");
517 10 : if (eap == NULL)
518 0 : return -1;
519 :
520 10 : eap->init = eap_psk_init;
521 10 : eap->reset = eap_psk_reset;
522 10 : eap->buildReq = eap_psk_buildReq;
523 10 : eap->check = eap_psk_check;
524 10 : eap->process = eap_psk_process;
525 10 : eap->isDone = eap_psk_isDone;
526 10 : eap->getKey = eap_psk_getKey;
527 10 : eap->isSuccess = eap_psk_isSuccess;
528 10 : eap->get_emsk = eap_psk_get_emsk;
529 10 : eap->getSessionId = eap_psk_get_session_id;
530 :
531 10 : ret = eap_server_method_register(eap);
532 10 : if (ret)
533 0 : eap_server_method_free(eap);
534 10 : return ret;
535 : }
|