1 /*
2 * EAP peer method: EAP-FAST (RFC 4851)
3 * Copyright (c) 2004-2008, 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
9 #include "includes.h"
10
11 #include "common.h"
12 #include "crypto/tls.h"
13 #include "crypto/sha1.h"
14 #include "eap_common/eap_tlv_common.h"
15 #include "eap_i.h"
16 #include "eap_tls_common.h"
17 #include "eap_config.h"
18 #include "eap_fast_pac.h"
19
20 #ifdef EAP_FAST_DYNAMIC
21 #include "eap_fast_pac.c"
22 #endif /* EAP_FAST_DYNAMIC */
23
24 /* TODO:
25 * - test session resumption and enable it if it interoperates
26 * - password change (pending mschapv2 packet; replay decrypted packet)
27 */
28
29
30 static void eap_fast_deinit(struct eap_sm *sm, void *priv);
31
32
33 struct eap_fast_data {
34 struct eap_ssl_data ssl;
35
36 int fast_version;
37
38 const struct eap_method *phase2_method;
39 void *phase2_priv;
40 int phase2_success;
41
42 struct eap_method_type phase2_type;
43 struct eap_method_type *phase2_types;
44 size_t num_phase2_types;
45 int resuming; /* starting a resumed session */
46 struct eap_fast_key_block_provisioning *key_block_p;
47 #define EAP_FAST_PROV_UNAUTH 1
48 #define EAP_FAST_PROV_AUTH 2
49 int provisioning_allowed; /* Allowed PAC provisioning modes */
50 int provisioning; /* doing PAC provisioning (not the normal auth) */
51 int anon_provisioning; /* doing anonymous (unauthenticated)
52 * provisioning */
53 int session_ticket_used;
54
55 u8 key_data[EAP_FAST_KEY_LEN];
56 u8 *session_id;
57 size_t id_len;
58 u8 emsk[EAP_EMSK_LEN];
59 int success;
60
61 struct eap_fast_pac *pac;
62 struct eap_fast_pac *current_pac;
63 size_t max_pac_list_len;
64 int use_pac_binary_format;
65
66 u8 simck[EAP_FAST_SIMCK_LEN];
67 int simck_idx;
68
69 struct wpabuf *pending_phase2_req;
70 };
71
72
eap_fast_session_ticket_cb(void * ctx,const u8 * ticket,size_t len,const u8 * client_random,const u8 * server_random,u8 * master_secret)73 static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
74 const u8 *client_random,
75 const u8 *server_random,
76 u8 *master_secret)
77 {
78 struct eap_fast_data *data = ctx;
79
80 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
81
82 if (client_random == NULL || server_random == NULL ||
83 master_secret == NULL) {
84 wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
85 "back to full TLS handshake");
86 data->session_ticket_used = 0;
87 if (data->provisioning_allowed) {
88 wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
89 "new PAC-Key");
90 data->provisioning = 1;
91 data->current_pac = NULL;
92 }
93 return 0;
94 }
95
96 wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
97
98 if (data->current_pac == NULL) {
99 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
100 "using SessionTicket");
101 data->session_ticket_used = 0;
102 return 0;
103 }
104
105 eap_fast_derive_master_secret(data->current_pac->pac_key,
106 server_random, client_random,
107 master_secret);
108
109 data->session_ticket_used = 1;
110
111 return 1;
112 }
113
114
eap_fast_parse_phase1(struct eap_fast_data * data,const char * phase1)115 static int eap_fast_parse_phase1(struct eap_fast_data *data,
116 const char *phase1)
117 {
118 const char *pos;
119
120 pos = os_strstr(phase1, "fast_provisioning=");
121 if (pos) {
122 data->provisioning_allowed = atoi(pos + 18);
123 wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
124 "mode: %d", data->provisioning_allowed);
125 }
126
127 pos = os_strstr(phase1, "fast_max_pac_list_len=");
128 if (pos) {
129 data->max_pac_list_len = atoi(pos + 22);
130 if (data->max_pac_list_len == 0)
131 data->max_pac_list_len = 1;
132 wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
133 (unsigned long) data->max_pac_list_len);
134 }
135
136 pos = os_strstr(phase1, "fast_pac_format=binary");
137 if (pos) {
138 data->use_pac_binary_format = 1;
139 wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
140 "list");
141 }
142
143 return 0;
144 }
145
146
eap_fast_init(struct eap_sm * sm)147 static void * eap_fast_init(struct eap_sm *sm)
148 {
149 struct eap_fast_data *data;
150 struct eap_peer_config *config = eap_get_config(sm);
151
152 if (config == NULL)
153 return NULL;
154
155 data = os_zalloc(sizeof(*data));
156 if (data == NULL)
157 return NULL;
158 data->fast_version = EAP_FAST_VERSION;
159 data->max_pac_list_len = 10;
160
161 if (config->phase1 && eap_fast_parse_phase1(data, config->phase1) < 0) {
162 eap_fast_deinit(sm, data);
163 return NULL;
164 }
165
166 if (eap_peer_select_phase2_methods(config, "auth=",
167 &data->phase2_types,
168 &data->num_phase2_types) < 0) {
169 eap_fast_deinit(sm, data);
170 return NULL;
171 }
172
173 data->phase2_type.vendor = EAP_VENDOR_IETF;
174 data->phase2_type.method = EAP_TYPE_NONE;
175
176 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_FAST)) {
177 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
178 eap_fast_deinit(sm, data);
179 return NULL;
180 }
181
182 if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
183 eap_fast_session_ticket_cb,
184 data) < 0) {
185 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
186 "callback");
187 eap_fast_deinit(sm, data);
188 return NULL;
189 }
190
191 /*
192 * The local RADIUS server in a Cisco AP does not seem to like empty
193 * fragments before data, so disable that workaround for CBC.
194 * TODO: consider making this configurable
195 */
196 if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
197 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
198 "workarounds");
199 }
200
201 if (!config->pac_file) {
202 wpa_printf(MSG_INFO, "EAP-FAST: No PAC file configured");
203 eap_fast_deinit(sm, data);
204 return NULL;
205 }
206
207 if (data->use_pac_binary_format &&
208 eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
209 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
210 eap_fast_deinit(sm, data);
211 return NULL;
212 }
213
214 if (!data->use_pac_binary_format &&
215 eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
216 wpa_printf(MSG_INFO, "EAP-FAST: Failed to load PAC file");
217 eap_fast_deinit(sm, data);
218 return NULL;
219 }
220 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
221
222 if (data->pac == NULL && !data->provisioning_allowed) {
223 wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
224 "provisioning disabled");
225 eap_fast_deinit(sm, data);
226 return NULL;
227 }
228
229 return data;
230 }
231
232
eap_fast_deinit(struct eap_sm * sm,void * priv)233 static void eap_fast_deinit(struct eap_sm *sm, void *priv)
234 {
235 struct eap_fast_data *data = priv;
236 struct eap_fast_pac *pac, *prev;
237
238 if (data == NULL)
239 return;
240 if (data->phase2_priv && data->phase2_method)
241 data->phase2_method->deinit(sm, data->phase2_priv);
242 os_free(data->phase2_types);
243 os_free(data->key_block_p);
244 eap_peer_tls_ssl_deinit(sm, &data->ssl);
245
246 pac = data->pac;
247 prev = NULL;
248 while (pac) {
249 prev = pac;
250 pac = pac->next;
251 eap_fast_free_pac(prev);
252 }
253 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
254 os_memset(data->emsk, 0, EAP_EMSK_LEN);
255 os_free(data->session_id);
256 wpabuf_free(data->pending_phase2_req);
257 os_free(data);
258 }
259
260
eap_fast_derive_msk(struct eap_fast_data * data)261 static int eap_fast_derive_msk(struct eap_fast_data *data)
262 {
263 eap_fast_derive_eap_msk(data->simck, data->key_data);
264 eap_fast_derive_eap_emsk(data->simck, data->emsk);
265 data->success = 1;
266 return 0;
267 }
268
269
eap_fast_derive_key_auth(struct eap_sm * sm,struct eap_fast_data * data)270 static int eap_fast_derive_key_auth(struct eap_sm *sm,
271 struct eap_fast_data *data)
272 {
273 u8 *sks;
274
275 /* RFC 4851, Section 5.1:
276 * Extra key material after TLS key_block: session_key_seed[40]
277 */
278
279 sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
280 EAP_FAST_SKS_LEN);
281 if (sks == NULL) {
282 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
283 "session_key_seed");
284 return -1;
285 }
286
287 /*
288 * RFC 4851, Section 5.2:
289 * S-IMCK[0] = session_key_seed
290 */
291 wpa_hexdump_key(MSG_DEBUG,
292 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
293 sks, EAP_FAST_SKS_LEN);
294 data->simck_idx = 0;
295 os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
296 os_free(sks);
297 return 0;
298 }
299
300
eap_fast_derive_key_provisioning(struct eap_sm * sm,struct eap_fast_data * data)301 static int eap_fast_derive_key_provisioning(struct eap_sm *sm,
302 struct eap_fast_data *data)
303 {
304 os_free(data->key_block_p);
305 data->key_block_p = (struct eap_fast_key_block_provisioning *)
306 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
307 "key expansion",
308 sizeof(*data->key_block_p));
309 if (data->key_block_p == NULL) {
310 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
311 return -1;
312 }
313 /*
314 * RFC 4851, Section 5.2:
315 * S-IMCK[0] = session_key_seed
316 */
317 wpa_hexdump_key(MSG_DEBUG,
318 "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
319 data->key_block_p->session_key_seed,
320 sizeof(data->key_block_p->session_key_seed));
321 data->simck_idx = 0;
322 os_memcpy(data->simck, data->key_block_p->session_key_seed,
323 EAP_FAST_SIMCK_LEN);
324 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
325 data->key_block_p->server_challenge,
326 sizeof(data->key_block_p->server_challenge));
327 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
328 data->key_block_p->client_challenge,
329 sizeof(data->key_block_p->client_challenge));
330 return 0;
331 }
332
333
eap_fast_derive_keys(struct eap_sm * sm,struct eap_fast_data * data)334 static int eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
335 {
336 int res;
337
338 if (data->anon_provisioning)
339 res = eap_fast_derive_key_provisioning(sm, data);
340 else
341 res = eap_fast_derive_key_auth(sm, data);
342 return res;
343 }
344
345
eap_fast_init_phase2_method(struct eap_sm * sm,struct eap_fast_data * data)346 static int eap_fast_init_phase2_method(struct eap_sm *sm,
347 struct eap_fast_data *data)
348 {
349 data->phase2_method =
350 eap_peer_get_eap_method(data->phase2_type.vendor,
351 data->phase2_type.method);
352 if (data->phase2_method == NULL)
353 return -1;
354
355 if (data->key_block_p) {
356 sm->auth_challenge = data->key_block_p->server_challenge;
357 sm->peer_challenge = data->key_block_p->client_challenge;
358 }
359 sm->init_phase2 = 1;
360 data->phase2_priv = data->phase2_method->init(sm);
361 sm->init_phase2 = 0;
362 sm->auth_challenge = NULL;
363 sm->peer_challenge = NULL;
364
365 return data->phase2_priv == NULL ? -1 : 0;
366 }
367
368
eap_fast_select_phase2_method(struct eap_fast_data * data,u8 type)369 static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
370 {
371 size_t i;
372
373 /* TODO: TNC with anonymous provisioning; need to require both
374 * completed MSCHAPv2 and TNC */
375
376 if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
377 wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
378 "during unauthenticated provisioning; reject phase2"
379 " type %d", type);
380 return -1;
381 }
382
383 #ifdef EAP_TNC
384 if (type == EAP_TYPE_TNC) {
385 data->phase2_type.vendor = EAP_VENDOR_IETF;
386 data->phase2_type.method = EAP_TYPE_TNC;
387 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
388 "vendor %d method %d for TNC",
389 data->phase2_type.vendor,
390 data->phase2_type.method);
391 return 0;
392 }
393 #endif /* EAP_TNC */
394
395 for (i = 0; i < data->num_phase2_types; i++) {
396 if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
397 data->phase2_types[i].method != type)
398 continue;
399
400 data->phase2_type.vendor = data->phase2_types[i].vendor;
401 data->phase2_type.method = data->phase2_types[i].method;
402 wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
403 "vendor %d method %d",
404 data->phase2_type.vendor,
405 data->phase2_type.method);
406 break;
407 }
408
409 if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
410 return -1;
411
412 return 0;
413 }
414
415
eap_fast_phase2_request(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,struct eap_hdr * hdr,struct wpabuf ** resp)416 static int eap_fast_phase2_request(struct eap_sm *sm,
417 struct eap_fast_data *data,
418 struct eap_method_ret *ret,
419 struct eap_hdr *hdr,
420 struct wpabuf **resp)
421 {
422 size_t len = be_to_host16(hdr->length);
423 u8 *pos;
424 struct eap_method_ret iret;
425 struct eap_peer_config *config = eap_get_config(sm);
426 struct wpabuf msg;
427
428 if (len <= sizeof(struct eap_hdr)) {
429 wpa_printf(MSG_INFO, "EAP-FAST: too short "
430 "Phase 2 request (len=%lu)", (unsigned long) len);
431 return -1;
432 }
433 pos = (u8 *) (hdr + 1);
434 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
435 if (*pos == EAP_TYPE_IDENTITY) {
436 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
437 return 0;
438 }
439
440 if (data->phase2_priv && data->phase2_method &&
441 *pos != data->phase2_type.method) {
442 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
443 "deinitialize previous method");
444 data->phase2_method->deinit(sm, data->phase2_priv);
445 data->phase2_method = NULL;
446 data->phase2_priv = NULL;
447 data->phase2_type.vendor = EAP_VENDOR_IETF;
448 data->phase2_type.method = EAP_TYPE_NONE;
449 }
450
451 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
452 data->phase2_type.method == EAP_TYPE_NONE &&
453 eap_fast_select_phase2_method(data, *pos) < 0) {
454 if (eap_peer_tls_phase2_nak(data->phase2_types,
455 data->num_phase2_types,
456 hdr, resp))
457 return -1;
458 return 0;
459 }
460
461 if ((data->phase2_priv == NULL &&
462 eap_fast_init_phase2_method(sm, data) < 0) ||
463 data->phase2_method == NULL) {
464 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
465 "Phase 2 EAP method %d", *pos);
466 ret->methodState = METHOD_DONE;
467 ret->decision = DECISION_FAIL;
468 return -1;
469 }
470
471 os_memset(&iret, 0, sizeof(iret));
472 wpabuf_set(&msg, hdr, len);
473 *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
474 &msg);
475 if (*resp == NULL ||
476 (iret.methodState == METHOD_DONE &&
477 iret.decision == DECISION_FAIL)) {
478 ret->methodState = METHOD_DONE;
479 ret->decision = DECISION_FAIL;
480 } else if ((iret.methodState == METHOD_DONE ||
481 iret.methodState == METHOD_MAY_CONT) &&
482 (iret.decision == DECISION_UNCOND_SUCC ||
483 iret.decision == DECISION_COND_SUCC)) {
484 data->phase2_success = 1;
485 }
486
487 if (*resp == NULL && config &&
488 (config->pending_req_identity || config->pending_req_password ||
489 config->pending_req_otp || config->pending_req_new_password)) {
490 wpabuf_free(data->pending_phase2_req);
491 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
492 } else if (*resp == NULL)
493 return -1;
494
495 return 0;
496 }
497
498
eap_fast_tlv_nak(int vendor_id,int tlv_type)499 static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
500 {
501 struct wpabuf *buf;
502 struct eap_tlv_nak_tlv *nak;
503 buf = wpabuf_alloc(sizeof(*nak));
504 if (buf == NULL)
505 return NULL;
506 nak = wpabuf_put(buf, sizeof(*nak));
507 nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
508 nak->length = host_to_be16(6);
509 nak->vendor_id = host_to_be32(vendor_id);
510 nak->nak_type = host_to_be16(tlv_type);
511 return buf;
512 }
513
514
eap_fast_tlv_result(int status,int intermediate)515 static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
516 {
517 struct wpabuf *buf;
518 struct eap_tlv_intermediate_result_tlv *result;
519 buf = wpabuf_alloc(sizeof(*result));
520 if (buf == NULL)
521 return NULL;
522 wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
523 intermediate ? "Intermediate " : "", status);
524 result = wpabuf_put(buf, sizeof(*result));
525 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
526 (intermediate ?
527 EAP_TLV_INTERMEDIATE_RESULT_TLV :
528 EAP_TLV_RESULT_TLV));
529 result->length = host_to_be16(2);
530 result->status = host_to_be16(status);
531 return buf;
532 }
533
534
eap_fast_tlv_pac_ack(void)535 static struct wpabuf * eap_fast_tlv_pac_ack(void)
536 {
537 struct wpabuf *buf;
538 struct eap_tlv_result_tlv *res;
539 struct eap_tlv_pac_ack_tlv *ack;
540
541 buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
542 if (buf == NULL)
543 return NULL;
544
545 wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
546 ack = wpabuf_put(buf, sizeof(*ack));
547 ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
548 EAP_TLV_TYPE_MANDATORY);
549 ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
550 ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
551 ack->pac_len = host_to_be16(2);
552 ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
553
554 return buf;
555 }
556
557
eap_fast_process_eap_payload_tlv(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 * eap_payload_tlv,size_t eap_payload_tlv_len)558 static struct wpabuf * eap_fast_process_eap_payload_tlv(
559 struct eap_sm *sm, struct eap_fast_data *data,
560 struct eap_method_ret *ret,
561 u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
562 {
563 struct eap_hdr *hdr;
564 struct wpabuf *resp = NULL;
565
566 if (eap_payload_tlv_len < sizeof(*hdr)) {
567 wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
568 "Payload TLV (len=%lu)",
569 (unsigned long) eap_payload_tlv_len);
570 return NULL;
571 }
572
573 hdr = (struct eap_hdr *) eap_payload_tlv;
574 if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
575 wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
576 "EAP Payload TLV");
577 return NULL;
578 }
579
580 if (hdr->code != EAP_CODE_REQUEST) {
581 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
582 "Phase 2 EAP header", hdr->code);
583 return NULL;
584 }
585
586 if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
587 wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
588 "failed");
589 return NULL;
590 }
591
592 return eap_fast_tlv_eap_payload(resp);
593 }
594
595
eap_fast_validate_crypto_binding(struct eap_tlv_crypto_binding_tlv * _bind)596 static int eap_fast_validate_crypto_binding(
597 struct eap_tlv_crypto_binding_tlv *_bind)
598 {
599 wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
600 "Received Version %d SubType %d",
601 _bind->version, _bind->received_version, _bind->subtype);
602 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
603 _bind->nonce, sizeof(_bind->nonce));
604 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
605 _bind->compound_mac, sizeof(_bind->compound_mac));
606
607 if (_bind->version != EAP_FAST_VERSION ||
608 _bind->received_version != EAP_FAST_VERSION ||
609 _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
610 wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
611 "Crypto-Binding TLV: Version %d "
612 "Received Version %d SubType %d",
613 _bind->version, _bind->received_version,
614 _bind->subtype);
615 return -1;
616 }
617
618 return 0;
619 }
620
621
eap_fast_write_crypto_binding(struct eap_tlv_crypto_binding_tlv * rbind,struct eap_tlv_crypto_binding_tlv * _bind,const u8 * cmk)622 static void eap_fast_write_crypto_binding(
623 struct eap_tlv_crypto_binding_tlv *rbind,
624 struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
625 {
626 rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
627 EAP_TLV_CRYPTO_BINDING_TLV);
628 rbind->length = host_to_be16(sizeof(*rbind) -
629 sizeof(struct eap_tlv_hdr));
630 rbind->version = EAP_FAST_VERSION;
631 rbind->received_version = _bind->version;
632 rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
633 os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
634 inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
635 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
636 rbind->compound_mac);
637
638 wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
639 "Received Version %d SubType %d",
640 rbind->version, rbind->received_version, rbind->subtype);
641 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
642 rbind->nonce, sizeof(rbind->nonce));
643 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
644 rbind->compound_mac, sizeof(rbind->compound_mac));
645 }
646
647
eap_fast_get_phase2_key(struct eap_sm * sm,struct eap_fast_data * data,u8 * isk,size_t isk_len)648 static int eap_fast_get_phase2_key(struct eap_sm *sm,
649 struct eap_fast_data *data,
650 u8 *isk, size_t isk_len)
651 {
652 u8 *key;
653 size_t key_len;
654
655 os_memset(isk, 0, isk_len);
656
657 if (data->phase2_method == NULL || data->phase2_priv == NULL) {
658 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
659 "available");
660 return -1;
661 }
662
663 if (data->phase2_method->isKeyAvailable == NULL ||
664 data->phase2_method->getKey == NULL)
665 return 0;
666
667 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
668 (key = data->phase2_method->getKey(sm, data->phase2_priv,
669 &key_len)) == NULL) {
670 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
671 "from Phase 2");
672 return -1;
673 }
674
675 if (key_len > isk_len)
676 key_len = isk_len;
677 if (key_len == 32 &&
678 data->phase2_method->vendor == EAP_VENDOR_IETF &&
679 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
680 /*
681 * EAP-FAST uses reverse order for MS-MPPE keys when deriving
682 * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
683 * ISK for EAP-FAST cryptobinding.
684 */
685 os_memcpy(isk, key + 16, 16);
686 os_memcpy(isk + 16, key, 16);
687 } else
688 os_memcpy(isk, key, key_len);
689 os_free(key);
690
691 return 0;
692 }
693
694
eap_fast_get_cmk(struct eap_sm * sm,struct eap_fast_data * data,u8 * cmk)695 static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
696 u8 *cmk)
697 {
698 u8 isk[32], imck[60];
699
700 wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
701 "calculation", data->simck_idx + 1);
702
703 /*
704 * RFC 4851, Section 5.2:
705 * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
706 * MSK[j], 60)
707 * S-IMCK[j] = first 40 octets of IMCK[j]
708 * CMK[j] = last 20 octets of IMCK[j]
709 */
710
711 if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
712 return -1;
713 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
714 sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
715 "Inner Methods Compound Keys",
716 isk, sizeof(isk), imck, sizeof(imck));
717 data->simck_idx++;
718 os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
719 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
720 data->simck, EAP_FAST_SIMCK_LEN);
721 os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
722 wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
723 cmk, EAP_FAST_CMK_LEN);
724
725 return 0;
726 }
727
728
eap_fast_write_pac_request(u8 * pos,u16 pac_type)729 static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
730 {
731 struct eap_tlv_hdr *pac;
732 struct eap_tlv_request_action_tlv *act;
733 struct eap_tlv_pac_type_tlv *type;
734
735 act = (struct eap_tlv_request_action_tlv *) pos;
736 act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
737 act->length = host_to_be16(2);
738 act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
739
740 pac = (struct eap_tlv_hdr *) (act + 1);
741 pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
742 pac->length = host_to_be16(sizeof(*type));
743
744 type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
745 type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
746 type->length = host_to_be16(2);
747 type->pac_type = host_to_be16(pac_type);
748
749 return (u8 *) (type + 1);
750 }
751
752
eap_fast_process_crypto_binding(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,struct eap_tlv_crypto_binding_tlv * _bind,size_t bind_len)753 static struct wpabuf * eap_fast_process_crypto_binding(
754 struct eap_sm *sm, struct eap_fast_data *data,
755 struct eap_method_ret *ret,
756 struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
757 {
758 struct wpabuf *resp;
759 u8 *pos;
760 u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
761 int res;
762 size_t len;
763
764 if (eap_fast_validate_crypto_binding(_bind) < 0)
765 return NULL;
766
767 if (eap_fast_get_cmk(sm, data, cmk) < 0)
768 return NULL;
769
770 /* Validate received Compound MAC */
771 os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
772 os_memset(_bind->compound_mac, 0, sizeof(cmac));
773 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
774 "MAC calculation", (u8 *) _bind, bind_len);
775 hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
776 _bind->compound_mac);
777 res = os_memcmp_const(cmac, _bind->compound_mac, sizeof(cmac));
778 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
779 cmac, sizeof(cmac));
780 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
781 _bind->compound_mac, sizeof(cmac));
782 if (res != 0) {
783 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
784 os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
785 return NULL;
786 }
787
788 /*
789 * Compound MAC was valid, so authentication succeeded. Reply with
790 * crypto binding to allow server to complete authentication.
791 */
792
793 len = sizeof(struct eap_tlv_crypto_binding_tlv);
794 resp = wpabuf_alloc(len);
795 if (resp == NULL)
796 return NULL;
797
798 if (!data->anon_provisioning && data->phase2_success &&
799 eap_fast_derive_msk(data) < 0) {
800 wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
801 ret->methodState = METHOD_DONE;
802 ret->decision = DECISION_FAIL;
803 data->phase2_success = 0;
804 wpabuf_free(resp);
805 return NULL;
806 }
807
808 if (!data->anon_provisioning && data->phase2_success) {
809 os_free(data->session_id);
810 data->session_id = eap_peer_tls_derive_session_id(
811 sm, &data->ssl, EAP_TYPE_FAST, &data->id_len);
812 if (data->session_id) {
813 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Derived Session-Id",
814 data->session_id, data->id_len);
815 } else {
816 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to derive "
817 "Session-Id");
818 wpabuf_free(resp);
819 return NULL;
820 }
821 }
822
823 pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
824 eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
825 pos, _bind, cmk);
826
827 return resp;
828 }
829
830
eap_fast_parse_pac_tlv(struct eap_fast_pac * entry,int type,u8 * pos,size_t len,int * pac_key_found)831 static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
832 u8 *pos, size_t len, int *pac_key_found)
833 {
834 switch (type & 0x7fff) {
835 case PAC_TYPE_PAC_KEY:
836 wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
837 if (len != EAP_FAST_PAC_KEY_LEN) {
838 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
839 "length %lu", (unsigned long) len);
840 break;
841 }
842 *pac_key_found = 1;
843 os_memcpy(entry->pac_key, pos, len);
844 break;
845 case PAC_TYPE_PAC_OPAQUE:
846 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
847 entry->pac_opaque = pos;
848 entry->pac_opaque_len = len;
849 break;
850 case PAC_TYPE_PAC_INFO:
851 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
852 entry->pac_info = pos;
853 entry->pac_info_len = len;
854 break;
855 default:
856 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
857 type);
858 break;
859 }
860 }
861
862
eap_fast_process_pac_tlv(struct eap_fast_pac * entry,u8 * pac,size_t pac_len)863 static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
864 u8 *pac, size_t pac_len)
865 {
866 struct pac_tlv_hdr *hdr;
867 u8 *pos;
868 size_t left, len;
869 int type, pac_key_found = 0;
870
871 pos = pac;
872 left = pac_len;
873
874 while (left > sizeof(*hdr)) {
875 hdr = (struct pac_tlv_hdr *) pos;
876 type = be_to_host16(hdr->type);
877 len = be_to_host16(hdr->len);
878 pos += sizeof(*hdr);
879 left -= sizeof(*hdr);
880 if (len > left) {
881 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
882 "(type=%d len=%lu left=%lu)",
883 type, (unsigned long) len,
884 (unsigned long) left);
885 return -1;
886 }
887
888 eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
889
890 pos += len;
891 left -= len;
892 }
893
894 if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
895 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
896 "all the required fields");
897 return -1;
898 }
899
900 return 0;
901 }
902
903
eap_fast_parse_pac_info(struct eap_fast_pac * entry,int type,u8 * pos,size_t len)904 static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
905 u8 *pos, size_t len)
906 {
907 u16 pac_type;
908 u32 lifetime;
909 struct os_time now;
910
911 switch (type & 0x7fff) {
912 case PAC_TYPE_CRED_LIFETIME:
913 if (len != 4) {
914 wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
915 "Invalid CRED_LIFETIME length - ignored",
916 pos, len);
917 return 0;
918 }
919
920 /*
921 * This is not currently saved separately in PAC files since
922 * the server can automatically initiate PAC update when
923 * needed. Anyway, the information is available from PAC-Info
924 * dump if it is needed for something in the future.
925 */
926 lifetime = WPA_GET_BE32(pos);
927 os_get_time(&now);
928 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
929 "(%d days)",
930 lifetime, (lifetime - (u32) now.sec) / 86400);
931 break;
932 case PAC_TYPE_A_ID:
933 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
934 pos, len);
935 entry->a_id = pos;
936 entry->a_id_len = len;
937 break;
938 case PAC_TYPE_I_ID:
939 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
940 pos, len);
941 entry->i_id = pos;
942 entry->i_id_len = len;
943 break;
944 case PAC_TYPE_A_ID_INFO:
945 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
946 pos, len);
947 entry->a_id_info = pos;
948 entry->a_id_info_len = len;
949 break;
950 case PAC_TYPE_PAC_TYPE:
951 /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
952 if (len != 2) {
953 wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
954 "length %lu (expected 2)",
955 (unsigned long) len);
956 wpa_hexdump_ascii(MSG_DEBUG,
957 "EAP-FAST: PAC-Info - PAC-Type",
958 pos, len);
959 return -1;
960 }
961 pac_type = WPA_GET_BE16(pos);
962 if (pac_type != PAC_TYPE_TUNNEL_PAC &&
963 pac_type != PAC_TYPE_USER_AUTHORIZATION &&
964 pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
965 wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
966 "%d", pac_type);
967 return -1;
968 }
969
970 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
971 pac_type);
972 entry->pac_type = pac_type;
973 break;
974 default:
975 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
976 "type %d", type);
977 break;
978 }
979
980 return 0;
981 }
982
983
eap_fast_process_pac_info(struct eap_fast_pac * entry)984 static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
985 {
986 struct pac_tlv_hdr *hdr;
987 u8 *pos;
988 size_t left, len;
989 int type;
990
991 /* RFC 5422, Section 4.2.4 */
992
993 /* PAC-Type defaults to Tunnel PAC (Type 1) */
994 entry->pac_type = PAC_TYPE_TUNNEL_PAC;
995
996 pos = entry->pac_info;
997 left = entry->pac_info_len;
998 while (left > sizeof(*hdr)) {
999 hdr = (struct pac_tlv_hdr *) pos;
1000 type = be_to_host16(hdr->type);
1001 len = be_to_host16(hdr->len);
1002 pos += sizeof(*hdr);
1003 left -= sizeof(*hdr);
1004 if (len > left) {
1005 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
1006 "(type=%d len=%lu left=%lu)",
1007 type, (unsigned long) len,
1008 (unsigned long) left);
1009 return -1;
1010 }
1011
1012 if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
1013 return -1;
1014
1015 pos += len;
1016 left -= len;
1017 }
1018
1019 if (entry->a_id == NULL || entry->a_id_info == NULL) {
1020 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
1021 "all the required fields");
1022 return -1;
1023 }
1024
1025 return 0;
1026 }
1027
1028
eap_fast_process_pac(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 * pac,size_t pac_len)1029 static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
1030 struct eap_fast_data *data,
1031 struct eap_method_ret *ret,
1032 u8 *pac, size_t pac_len)
1033 {
1034 struct eap_peer_config *config = eap_get_config(sm);
1035 struct eap_fast_pac entry;
1036
1037 os_memset(&entry, 0, sizeof(entry));
1038 if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
1039 eap_fast_process_pac_info(&entry))
1040 return NULL;
1041
1042 eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
1043 eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
1044 if (data->use_pac_binary_format)
1045 eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
1046 else
1047 eap_fast_save_pac(sm, data->pac, config->pac_file);
1048
1049 if (data->provisioning) {
1050 if (data->anon_provisioning) {
1051 /*
1052 * Unauthenticated provisioning does not provide keying
1053 * material and must end with an EAP-Failure.
1054 * Authentication will be done separately after this.
1055 */
1056 data->success = 0;
1057 ret->decision = DECISION_FAIL;
1058 } else {
1059 /*
1060 * Server may or may not allow authenticated
1061 * provisioning also for key generation.
1062 */
1063 ret->decision = DECISION_COND_SUCC;
1064 }
1065 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1066 "- Provisioning completed successfully");
1067 sm->expected_failure = 1;
1068 } else {
1069 /*
1070 * This is PAC refreshing, i.e., normal authentication that is
1071 * expected to be completed with an EAP-Success. However,
1072 * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
1073 * after protected success exchange in case of EAP-Fast
1074 * provisioning, so we better use DECISION_COND_SUCC here
1075 * instead of DECISION_UNCOND_SUCC.
1076 */
1077 wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
1078 "- PAC refreshing completed successfully");
1079 ret->decision = DECISION_COND_SUCC;
1080 }
1081 ret->methodState = METHOD_DONE;
1082 return eap_fast_tlv_pac_ack();
1083 }
1084
1085
eap_fast_parse_decrypted(struct wpabuf * decrypted,struct eap_fast_tlv_parse * tlv,struct wpabuf ** resp)1086 static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
1087 struct eap_fast_tlv_parse *tlv,
1088 struct wpabuf **resp)
1089 {
1090 int mandatory, tlv_type, res;
1091 size_t len;
1092 u8 *pos, *end;
1093
1094 os_memset(tlv, 0, sizeof(*tlv));
1095
1096 /* Parse TLVs from the decrypted Phase 2 data */
1097 pos = wpabuf_mhead(decrypted);
1098 end = pos + wpabuf_len(decrypted);
1099 while (pos + 4 < end) {
1100 mandatory = pos[0] & 0x80;
1101 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
1102 pos += 2;
1103 len = WPA_GET_BE16(pos);
1104 pos += 2;
1105 if (len > (size_t) (end - pos)) {
1106 wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
1107 return -1;
1108 }
1109 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
1110 "TLV type %d length %u%s",
1111 tlv_type, (unsigned int) len,
1112 mandatory ? " (mandatory)" : "");
1113
1114 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
1115 if (res == -2)
1116 break;
1117 if (res < 0) {
1118 if (mandatory) {
1119 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
1120 "mandatory TLV type %d", tlv_type);
1121 *resp = eap_fast_tlv_nak(0, tlv_type);
1122 break;
1123 } else {
1124 wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
1125 "unknown optional TLV type %d",
1126 tlv_type);
1127 }
1128 }
1129
1130 pos += len;
1131 }
1132
1133 return 0;
1134 }
1135
1136
eap_fast_encrypt_response(struct eap_sm * sm,struct eap_fast_data * data,struct wpabuf * resp,u8 identifier,struct wpabuf ** out_data)1137 static int eap_fast_encrypt_response(struct eap_sm *sm,
1138 struct eap_fast_data *data,
1139 struct wpabuf *resp,
1140 u8 identifier, struct wpabuf **out_data)
1141 {
1142 if (resp == NULL)
1143 return 0;
1144
1145 wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
1146 resp);
1147 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1148 data->fast_version, identifier,
1149 resp, out_data)) {
1150 wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
1151 "frame");
1152 }
1153 wpabuf_free(resp);
1154
1155 return 0;
1156 }
1157
1158
eap_fast_pac_request(void)1159 static struct wpabuf * eap_fast_pac_request(void)
1160 {
1161 struct wpabuf *tmp;
1162 u8 *pos, *pos2;
1163
1164 tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
1165 sizeof(struct eap_tlv_request_action_tlv) +
1166 sizeof(struct eap_tlv_pac_type_tlv));
1167 if (tmp == NULL)
1168 return NULL;
1169
1170 pos = wpabuf_put(tmp, 0);
1171 pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
1172 wpabuf_put(tmp, pos2 - pos);
1173 return tmp;
1174 }
1175
1176
eap_fast_process_decrypted(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 identifier,struct wpabuf * decrypted,struct wpabuf ** out_data)1177 static int eap_fast_process_decrypted(struct eap_sm *sm,
1178 struct eap_fast_data *data,
1179 struct eap_method_ret *ret,
1180 u8 identifier,
1181 struct wpabuf *decrypted,
1182 struct wpabuf **out_data)
1183 {
1184 struct wpabuf *resp = NULL, *tmp;
1185 struct eap_fast_tlv_parse tlv;
1186 int failed = 0;
1187
1188 if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
1189 return 0;
1190 if (resp)
1191 return eap_fast_encrypt_response(sm, data, resp,
1192 identifier, out_data);
1193
1194 if (tlv.result == EAP_TLV_RESULT_FAILURE) {
1195 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1196 return eap_fast_encrypt_response(sm, data, resp,
1197 identifier, out_data);
1198 }
1199
1200 if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
1201 resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
1202 return eap_fast_encrypt_response(sm, data, resp,
1203 identifier, out_data);
1204 }
1205
1206 if (tlv.crypto_binding) {
1207 tmp = eap_fast_process_crypto_binding(sm, data, ret,
1208 tlv.crypto_binding,
1209 tlv.crypto_binding_len);
1210 if (tmp == NULL)
1211 failed = 1;
1212 else
1213 resp = wpabuf_concat(resp, tmp);
1214 }
1215
1216 if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
1217 tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
1218 EAP_TLV_RESULT_SUCCESS, 1);
1219 resp = wpabuf_concat(resp, tmp);
1220 }
1221
1222 if (tlv.eap_payload_tlv) {
1223 tmp = eap_fast_process_eap_payload_tlv(
1224 sm, data, ret, tlv.eap_payload_tlv,
1225 tlv.eap_payload_tlv_len);
1226 resp = wpabuf_concat(resp, tmp);
1227 }
1228
1229 if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
1230 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
1231 "acknowledging success");
1232 failed = 1;
1233 } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
1234 tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
1235 tlv.pac_len);
1236 resp = wpabuf_concat(resp, tmp);
1237 }
1238
1239 if (data->current_pac == NULL && data->provisioning &&
1240 !data->anon_provisioning && !tlv.pac &&
1241 (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
1242 tlv.result == EAP_TLV_RESULT_SUCCESS)) {
1243 /*
1244 * Need to request Tunnel PAC when using authenticated
1245 * provisioning.
1246 */
1247 wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
1248 tmp = eap_fast_pac_request();
1249 resp = wpabuf_concat(resp, tmp);
1250 }
1251
1252 if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
1253 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
1254 resp = wpabuf_concat(tmp, resp);
1255 } else if (failed) {
1256 tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
1257 resp = wpabuf_concat(tmp, resp);
1258 }
1259
1260 if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
1261 tlv.crypto_binding && data->phase2_success) {
1262 if (data->anon_provisioning) {
1263 wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
1264 "provisioning completed successfully.");
1265 ret->methodState = METHOD_DONE;
1266 ret->decision = DECISION_FAIL;
1267 sm->expected_failure = 1;
1268 } else {
1269 wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
1270 "completed successfully.");
1271 if (data->provisioning)
1272 ret->methodState = METHOD_MAY_CONT;
1273 else
1274 ret->methodState = METHOD_DONE;
1275 ret->decision = DECISION_UNCOND_SUCC;
1276 }
1277 }
1278
1279 if (resp == NULL) {
1280 wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
1281 "empty response packet");
1282 resp = wpabuf_alloc(1);
1283 }
1284
1285 return eap_fast_encrypt_response(sm, data, resp, identifier,
1286 out_data);
1287 }
1288
1289
eap_fast_decrypt(struct eap_sm * sm,struct eap_fast_data * data,struct eap_method_ret * ret,u8 identifier,const struct wpabuf * in_data,struct wpabuf ** out_data)1290 static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
1291 struct eap_method_ret *ret, u8 identifier,
1292 const struct wpabuf *in_data,
1293 struct wpabuf **out_data)
1294 {
1295 struct wpabuf *in_decrypted;
1296 int res;
1297
1298 wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
1299 " Phase 2", (unsigned long) wpabuf_len(in_data));
1300
1301 if (data->pending_phase2_req) {
1302 wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
1303 "skip decryption and use old data");
1304 /* Clear TLS reassembly state. */
1305 eap_peer_tls_reset_input(&data->ssl);
1306
1307 in_decrypted = data->pending_phase2_req;
1308 data->pending_phase2_req = NULL;
1309 goto continue_req;
1310 }
1311
1312 if (wpabuf_len(in_data) == 0) {
1313 /* Received TLS ACK - requesting more fragments */
1314 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
1315 data->fast_version,
1316 identifier, NULL, out_data);
1317 }
1318
1319 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
1320 if (res)
1321 return res;
1322
1323 continue_req:
1324 wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
1325 in_decrypted);
1326
1327 if (wpabuf_len(in_decrypted) < 4) {
1328 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
1329 "TLV frame (len=%lu)",
1330 (unsigned long) wpabuf_len(in_decrypted));
1331 wpabuf_free(in_decrypted);
1332 return -1;
1333 }
1334
1335 res = eap_fast_process_decrypted(sm, data, ret, identifier,
1336 in_decrypted, out_data);
1337
1338 wpabuf_free(in_decrypted);
1339
1340 return res;
1341 }
1342
1343
eap_fast_get_a_id(const u8 * buf,size_t len,size_t * id_len)1344 static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
1345 {
1346 const u8 *a_id;
1347 const struct pac_tlv_hdr *hdr;
1348
1349 /*
1350 * Parse authority identity (A-ID) from the EAP-FAST/Start. This
1351 * supports both raw A-ID and one inside an A-ID TLV.
1352 */
1353 a_id = buf;
1354 *id_len = len;
1355 if (len > sizeof(*hdr)) {
1356 int tlen;
1357 hdr = (const struct pac_tlv_hdr *) buf;
1358 tlen = be_to_host16(hdr->len);
1359 if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
1360 sizeof(*hdr) + tlen <= len) {
1361 wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
1362 "(Start)");
1363 a_id = (const u8 *) (hdr + 1);
1364 *id_len = tlen;
1365 }
1366 }
1367 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
1368
1369 return a_id;
1370 }
1371
1372
eap_fast_select_pac(struct eap_fast_data * data,const u8 * a_id,size_t a_id_len)1373 static void eap_fast_select_pac(struct eap_fast_data *data,
1374 const u8 *a_id, size_t a_id_len)
1375 {
1376 data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
1377 PAC_TYPE_TUNNEL_PAC);
1378 if (data->current_pac == NULL) {
1379 /*
1380 * Tunnel PAC was not available for this A-ID. Try to use
1381 * Machine Authentication PAC, if one is available.
1382 */
1383 data->current_pac = eap_fast_get_pac(
1384 data->pac, a_id, a_id_len,
1385 PAC_TYPE_MACHINE_AUTHENTICATION);
1386 }
1387
1388 if (data->current_pac) {
1389 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
1390 "(PAC-Type %d)", data->current_pac->pac_type);
1391 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
1392 data->current_pac->a_id_info,
1393 data->current_pac->a_id_info_len);
1394 }
1395 }
1396
1397
eap_fast_use_pac_opaque(struct eap_sm * sm,struct eap_fast_data * data,struct eap_fast_pac * pac)1398 static int eap_fast_use_pac_opaque(struct eap_sm *sm,
1399 struct eap_fast_data *data,
1400 struct eap_fast_pac *pac)
1401 {
1402 u8 *tlv;
1403 size_t tlv_len, olen;
1404 struct eap_tlv_hdr *ehdr;
1405
1406 olen = pac->pac_opaque_len;
1407 tlv_len = sizeof(*ehdr) + olen;
1408 tlv = os_malloc(tlv_len);
1409 if (tlv) {
1410 ehdr = (struct eap_tlv_hdr *) tlv;
1411 ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
1412 ehdr->length = host_to_be16(olen);
1413 os_memcpy(ehdr + 1, pac->pac_opaque, olen);
1414 }
1415 if (tlv == NULL ||
1416 tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1417 TLS_EXT_PAC_OPAQUE,
1418 tlv, tlv_len) < 0) {
1419 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
1420 "extension");
1421 os_free(tlv);
1422 return -1;
1423 }
1424 os_free(tlv);
1425
1426 return 0;
1427 }
1428
1429
eap_fast_clear_pac_opaque_ext(struct eap_sm * sm,struct eap_fast_data * data)1430 static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
1431 struct eap_fast_data *data)
1432 {
1433 if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
1434 TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
1435 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
1436 "TLS extension");
1437 return -1;
1438 }
1439 return 0;
1440 }
1441
1442
eap_fast_set_provisioning_ciphers(struct eap_sm * sm,struct eap_fast_data * data)1443 static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
1444 struct eap_fast_data *data)
1445 {
1446 u8 ciphers[5];
1447 int count = 0;
1448
1449 if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
1450 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
1451 "provisioning TLS cipher suites");
1452 ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
1453 }
1454
1455 if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
1456 wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
1457 "provisioning TLS cipher suites");
1458 ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
1459 ciphers[count++] = TLS_CIPHER_AES128_SHA;
1460 ciphers[count++] = TLS_CIPHER_RC4_SHA;
1461 }
1462
1463 ciphers[count++] = TLS_CIPHER_NONE;
1464
1465 if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
1466 ciphers)) {
1467 wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
1468 "cipher suites for provisioning");
1469 return -1;
1470 }
1471
1472 return 0;
1473 }
1474
1475
eap_fast_process_start(struct eap_sm * sm,struct eap_fast_data * data,u8 flags,const u8 * pos,size_t left)1476 static int eap_fast_process_start(struct eap_sm *sm,
1477 struct eap_fast_data *data, u8 flags,
1478 const u8 *pos, size_t left)
1479 {
1480 const u8 *a_id;
1481 size_t a_id_len;
1482
1483 /* EAP-FAST Version negotiation (section 3.1) */
1484 wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
1485 flags & EAP_TLS_VERSION_MASK, data->fast_version);
1486 if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
1487 data->fast_version = flags & EAP_TLS_VERSION_MASK;
1488 wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
1489 data->fast_version);
1490
1491 a_id = eap_fast_get_a_id(pos, left, &a_id_len);
1492 eap_fast_select_pac(data, a_id, a_id_len);
1493
1494 if (data->resuming && data->current_pac) {
1495 wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
1496 "do not add PAC-Opaque to TLS ClientHello");
1497 if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1498 return -1;
1499 } else if (data->current_pac) {
1500 /*
1501 * PAC found for the A-ID and we are not resuming an old
1502 * session, so add PAC-Opaque extension to ClientHello.
1503 */
1504 if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
1505 return -1;
1506 } else {
1507 /* No PAC found, so we must provision one. */
1508 if (!data->provisioning_allowed) {
1509 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
1510 "provisioning disabled");
1511 return -1;
1512 }
1513 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
1514 "starting provisioning");
1515 if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
1516 eap_fast_clear_pac_opaque_ext(sm, data) < 0)
1517 return -1;
1518 data->provisioning = 1;
1519 }
1520
1521 return 0;
1522 }
1523
1524
eap_fast_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)1525 static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
1526 struct eap_method_ret *ret,
1527 const struct wpabuf *reqData)
1528 {
1529 const struct eap_hdr *req;
1530 size_t left;
1531 int res;
1532 u8 flags, id;
1533 struct wpabuf *resp;
1534 const u8 *pos;
1535 struct eap_fast_data *data = priv;
1536 struct wpabuf msg;
1537
1538 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
1539 reqData, &left, &flags);
1540 if (pos == NULL)
1541 return NULL;
1542
1543 req = wpabuf_head(reqData);
1544 id = req->identifier;
1545
1546 if (flags & EAP_TLS_FLAGS_START) {
1547 if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
1548 return NULL;
1549
1550 left = 0; /* A-ID is not used in further packet processing */
1551 }
1552
1553 wpabuf_set(&msg, pos, left);
1554
1555 resp = NULL;
1556 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1557 !data->resuming) {
1558 /* Process tunneled (encrypted) phase 2 data. */
1559 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1560 if (res < 0) {
1561 ret->methodState = METHOD_DONE;
1562 ret->decision = DECISION_FAIL;
1563 /*
1564 * Ack possible Alert that may have caused failure in
1565 * decryption.
1566 */
1567 res = 1;
1568 }
1569 } else {
1570 /* Continue processing TLS handshake (phase 1). */
1571 res = eap_peer_tls_process_helper(sm, &data->ssl,
1572 EAP_TYPE_FAST,
1573 data->fast_version, id, &msg,
1574 &resp);
1575
1576 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1577 char cipher[80];
1578 wpa_printf(MSG_DEBUG,
1579 "EAP-FAST: TLS done, proceed to Phase 2");
1580 if (data->provisioning &&
1581 (!(data->provisioning_allowed &
1582 EAP_FAST_PROV_AUTH) ||
1583 tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
1584 cipher, sizeof(cipher)) < 0 ||
1585 os_strstr(cipher, "ADH-") ||
1586 os_strstr(cipher, "anon"))) {
1587 wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
1588 "anonymous (unauthenticated) "
1589 "provisioning");
1590 data->anon_provisioning = 1;
1591 } else
1592 data->anon_provisioning = 0;
1593 data->resuming = 0;
1594 if (eap_fast_derive_keys(sm, data) < 0) {
1595 wpa_printf(MSG_DEBUG,
1596 "EAP-FAST: Could not derive keys");
1597 ret->methodState = METHOD_DONE;
1598 ret->decision = DECISION_FAIL;
1599 wpabuf_free(resp);
1600 return NULL;
1601 }
1602 }
1603
1604 if (res == 2) {
1605 /*
1606 * Application data included in the handshake message.
1607 */
1608 wpabuf_free(data->pending_phase2_req);
1609 data->pending_phase2_req = resp;
1610 resp = NULL;
1611 res = eap_fast_decrypt(sm, data, ret, id, &msg, &resp);
1612 }
1613 }
1614
1615 if (res == 1) {
1616 wpabuf_free(resp);
1617 return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
1618 data->fast_version);
1619 }
1620
1621 return resp;
1622 }
1623
1624
1625 #if 0 /* FIX */
1626 static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
1627 {
1628 struct eap_fast_data *data = priv;
1629 return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
1630 }
1631
1632
1633 static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
1634 {
1635 struct eap_fast_data *data = priv;
1636 os_free(data->key_block_p);
1637 data->key_block_p = NULL;
1638 wpabuf_free(data->pending_phase2_req);
1639 data->pending_phase2_req = NULL;
1640 }
1641
1642
1643 static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
1644 {
1645 struct eap_fast_data *data = priv;
1646 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1647 os_free(data);
1648 return NULL;
1649 }
1650 os_memset(data->key_data, 0, EAP_FAST_KEY_LEN);
1651 os_memset(data->emsk, 0, EAP_EMSK_LEN);
1652 os_free(data->session_id);
1653 data->session_id = NULL;
1654 if (data->phase2_priv && data->phase2_method &&
1655 data->phase2_method->init_for_reauth)
1656 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1657 data->phase2_success = 0;
1658 data->resuming = 1;
1659 data->provisioning = 0;
1660 data->anon_provisioning = 0;
1661 data->simck_idx = 0;
1662 return priv;
1663 }
1664 #endif
1665
1666
eap_fast_get_status(struct eap_sm * sm,void * priv,char * buf,size_t buflen,int verbose)1667 static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
1668 size_t buflen, int verbose)
1669 {
1670 struct eap_fast_data *data = priv;
1671 int len, ret;
1672
1673 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1674 if (data->phase2_method) {
1675 ret = os_snprintf(buf + len, buflen - len,
1676 "EAP-FAST Phase2 method=%s\n",
1677 data->phase2_method->name);
1678 if (os_snprintf_error(buflen - len, ret))
1679 return len;
1680 len += ret;
1681 }
1682 return len;
1683 }
1684
1685
eap_fast_isKeyAvailable(struct eap_sm * sm,void * priv)1686 static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
1687 {
1688 struct eap_fast_data *data = priv;
1689 return data->success;
1690 }
1691
1692
eap_fast_getKey(struct eap_sm * sm,void * priv,size_t * len)1693 static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
1694 {
1695 struct eap_fast_data *data = priv;
1696 u8 *key;
1697
1698 if (!data->success)
1699 return NULL;
1700
1701 key = os_malloc(EAP_FAST_KEY_LEN);
1702 if (key == NULL)
1703 return NULL;
1704
1705 *len = EAP_FAST_KEY_LEN;
1706 os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
1707
1708 return key;
1709 }
1710
1711
eap_fast_get_session_id(struct eap_sm * sm,void * priv,size_t * len)1712 static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1713 {
1714 struct eap_fast_data *data = priv;
1715 u8 *id;
1716
1717 if (!data->success)
1718 return NULL;
1719
1720 id = os_malloc(data->id_len);
1721 if (id == NULL)
1722 return NULL;
1723
1724 *len = data->id_len;
1725 os_memcpy(id, data->session_id, data->id_len);
1726
1727 return id;
1728 }
1729
1730
eap_fast_get_emsk(struct eap_sm * sm,void * priv,size_t * len)1731 static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1732 {
1733 struct eap_fast_data *data = priv;
1734 u8 *key;
1735
1736 if (!data->success)
1737 return NULL;
1738
1739 key = os_malloc(EAP_EMSK_LEN);
1740 if (key == NULL)
1741 return NULL;
1742
1743 *len = EAP_EMSK_LEN;
1744 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1745
1746 return key;
1747 }
1748
1749
eap_peer_fast_register(void)1750 int eap_peer_fast_register(void)
1751 {
1752 struct eap_method *eap;
1753 int ret;
1754
1755 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1756 EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
1757 if (eap == NULL)
1758 return -1;
1759
1760 eap->init = eap_fast_init;
1761 eap->deinit = eap_fast_deinit;
1762 eap->process = eap_fast_process;
1763 eap->isKeyAvailable = eap_fast_isKeyAvailable;
1764 eap->getKey = eap_fast_getKey;
1765 eap->getSessionId = eap_fast_get_session_id;
1766 eap->get_status = eap_fast_get_status;
1767 #if 0
1768 eap->has_reauth_data = eap_fast_has_reauth_data;
1769 eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
1770 eap->init_for_reauth = eap_fast_init_for_reauth;
1771 #endif
1772 eap->get_emsk = eap_fast_get_emsk;
1773
1774 ret = eap_peer_method_register(eap);
1775 if (ret)
1776 eap_peer_method_free(eap);
1777 return ret;
1778 }
1779