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