This repository has been archived by the owner on Jul 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
eap.c
2134 lines (1880 loc) · 58.8 KB
/
eap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* EAP peer state machines (RFC 4137)
* Copyright (c) 2004-2006, Jouni Malinen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*
* This file implements the Peer State Machine as defined in RFC 4137. The used
* states and state transitions match mostly with the RFC. However, there are
* couple of additional transitions for working around small issues noticed
* during testing. These exceptions are explained in comments within the
* functions in this file. The method functions, m.func(), are similar to the
* ones used in RFC 4137, but some small changes have used here to optimize
* operations and to add functionality needed for fast re-authentication
* (session resumption).
*/
#include "includes.h"
#include "common.h"
#include "eap_i.h"
#include "config_ssid.h"
#include "tls.h"
#include "crypto.h"
#include "pcsc_funcs.h"
#include "wpa_ctrl.h"
#include "state_machine.h"
#define STATE_MACHINE_DATA struct eap_sm
#define STATE_MACHINE_DEBUG_PREFIX "EAP"
#define EAP_MAX_AUTH_ROUNDS 50
static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
EapType method);
static u8 * eap_sm_buildNak(struct eap_sm *sm, int id, size_t *len);
static void eap_sm_processIdentity(struct eap_sm *sm, const u8 *req);
static void eap_sm_processNotify(struct eap_sm *sm, const u8 *req);
static u8 * eap_sm_buildNotify(int id, size_t *len);
static void eap_sm_parseEapReq(struct eap_sm *sm, const u8 *req, size_t len);
#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
static const char * eap_sm_method_state_txt(EapMethodState state);
static const char * eap_sm_decision_txt(EapDecision decision);
#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
static Boolean eapol_get_bool(struct eap_sm *sm, enum eapol_bool_var var)
{
return sm->eapol_cb->get_bool(sm->eapol_ctx, var);
}
static void eapol_set_bool(struct eap_sm *sm, enum eapol_bool_var var,
Boolean value)
{
sm->eapol_cb->set_bool(sm->eapol_ctx, var, value);
}
static unsigned int eapol_get_int(struct eap_sm *sm, enum eapol_int_var var)
{
return sm->eapol_cb->get_int(sm->eapol_ctx, var);
}
static void eapol_set_int(struct eap_sm *sm, enum eapol_int_var var,
unsigned int value)
{
sm->eapol_cb->set_int(sm->eapol_ctx, var, value);
}
static u8 * eapol_get_eapReqData(struct eap_sm *sm, size_t *len)
{
return sm->eapol_cb->get_eapReqData(sm->eapol_ctx, len);
}
static void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
{
if (sm->m == NULL || sm->eap_method_priv == NULL)
return;
wpa_printf(MSG_DEBUG, "EAP: deinitialize previously used EAP method "
"(%d, %s) at %s", sm->selectedMethod, sm->m->name, txt);
sm->m->deinit(sm, sm->eap_method_priv);
sm->eap_method_priv = NULL;
sm->m = NULL;
}
/*
* This state initializes state machine variables when the machine is
* activated (portEnabled = TRUE). This is also used when re-starting
* authentication (eapRestart == TRUE).
*/
SM_STATE(EAP, INITIALIZE)
{
SM_ENTRY(EAP, INITIALIZE);
if (sm->fast_reauth && sm->m && sm->m->has_reauth_data &&
sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
wpa_printf(MSG_DEBUG, "EAP: maintaining EAP method data for "
"fast reauthentication");
sm->m->deinit_for_reauth(sm, sm->eap_method_priv);
} else {
eap_deinit_prev_method(sm, "INITIALIZE");
}
sm->selectedMethod = EAP_TYPE_NONE;
sm->methodState = METHOD_NONE;
sm->allowNotifications = TRUE;
sm->decision = DECISION_FAIL;
eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
eapol_set_bool(sm, EAPOL_eapSuccess, FALSE);
eapol_set_bool(sm, EAPOL_eapFail, FALSE);
os_free(sm->eapKeyData);
sm->eapKeyData = NULL;
sm->eapKeyAvailable = FALSE;
eapol_set_bool(sm, EAPOL_eapRestart, FALSE);
sm->lastId = -1; /* new session - make sure this does not match with
* the first EAP-Packet */
/*
* RFC 4137 does not reset eapResp and eapNoResp here. However, this
* seemed to be able to trigger cases where both were set and if EAPOL
* state machine uses eapNoResp first, it may end up not sending a real
* reply correctly. This occurred when the workaround in FAIL state set
* eapNoResp = TRUE.. Maybe that workaround needs to be fixed to do
* something else(?)
*/
eapol_set_bool(sm, EAPOL_eapResp, FALSE);
eapol_set_bool(sm, EAPOL_eapNoResp, FALSE);
sm->num_rounds = 0;
}
/*
* This state is reached whenever service from the lower layer is interrupted
* or unavailable (portEnabled == FALSE). Immediate transition to INITIALIZE
* occurs when the port becomes enabled.
*/
SM_STATE(EAP, DISABLED)
{
SM_ENTRY(EAP, DISABLED);
sm->num_rounds = 0;
}
/*
* The state machine spends most of its time here, waiting for something to
* happen. This state is entered unconditionally from INITIALIZE, DISCARD, and
* SEND_RESPONSE states.
*/
SM_STATE(EAP, IDLE)
{
SM_ENTRY(EAP, IDLE);
}
/*
* This state is entered when an EAP packet is received (eapReq == TRUE) to
* parse the packet header.
*/
SM_STATE(EAP, RECEIVED)
{
const u8 *eapReqData;
size_t eapReqDataLen;
SM_ENTRY(EAP, RECEIVED);
eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen);
/* parse rxReq, rxSuccess, rxFailure, reqId, reqMethod */
eap_sm_parseEapReq(sm, eapReqData, eapReqDataLen);
sm->num_rounds++;
}
/*
* This state is entered when a request for a new type comes in. Either the
* correct method is started, or a Nak response is built.
*/
SM_STATE(EAP, GET_METHOD)
{
int reinit;
EapType method;
SM_ENTRY(EAP, GET_METHOD);
if (sm->reqMethod == EAP_TYPE_EXPANDED)
method = sm->reqVendorMethod;
else
method = sm->reqMethod;
if (!eap_sm_allowMethod(sm, sm->reqVendor, method)) {
wpa_printf(MSG_DEBUG, "EAP: vendor %u method %u not allowed",
sm->reqVendor, method);
goto nak;
}
/*
* RFC 4137 does not define specific operation for fast
* re-authentication (session resumption). The design here is to allow
* the previously used method data to be maintained for
* re-authentication if the method support session resumption.
* Otherwise, the previously used method data is freed and a new method
* is allocated here.
*/
if (sm->fast_reauth &&
sm->m && sm->m->vendor == sm->reqVendor &&
sm->m->method == method &&
sm->m->has_reauth_data &&
sm->m->has_reauth_data(sm, sm->eap_method_priv)) {
wpa_printf(MSG_DEBUG, "EAP: Using previous method data"
" for fast re-authentication");
reinit = 1;
} else {
eap_deinit_prev_method(sm, "GET_METHOD");
reinit = 0;
}
sm->selectedMethod = sm->reqMethod;
if (sm->m == NULL)
sm->m = eap_sm_get_eap_methods(sm->reqVendor, method);
if (!sm->m) {
wpa_printf(MSG_DEBUG, "EAP: Could not find selected method: "
"vendor %d method %d",
sm->reqVendor, method);
goto nak;
}
wpa_printf(MSG_DEBUG, "EAP: Initialize selected EAP method: "
"vendor %u method %u (%s)",
sm->reqVendor, method, sm->m->name);
if (reinit)
sm->eap_method_priv = sm->m->init_for_reauth(
sm, sm->eap_method_priv);
else
sm->eap_method_priv = sm->m->init(sm);
if (sm->eap_method_priv == NULL) {
struct wpa_ssid *config = eap_get_config(sm);
wpa_msg(sm->msg_ctx, MSG_INFO,
"EAP: Failed to initialize EAP method: vendor %u "
"method %u (%s)",
sm->reqVendor, method, sm->m->name);
sm->m = NULL;
sm->methodState = METHOD_NONE;
sm->selectedMethod = EAP_TYPE_NONE;
if (sm->reqMethod == EAP_TYPE_TLS && config &&
(config->pending_req_pin ||
config->pending_req_passphrase)) {
/*
* Return without generating Nak in order to allow
* entering of PIN code or passphrase to retry the
* current EAP packet.
*/
wpa_printf(MSG_DEBUG, "EAP: Pending PIN/passphrase "
"request - skip Nak");
return;
}
goto nak;
}
sm->methodState = METHOD_INIT;
wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_METHOD
"EAP vendor %u method %u (%s) selected",
sm->reqVendor, method, sm->m->name);
return;
nak:
os_free(sm->eapRespData);
sm->eapRespData = NULL;
sm->eapRespData = eap_sm_buildNak(sm, sm->reqId, &sm->eapRespDataLen);
}
/*
* The method processing happens here. The request from the authenticator is
* processed, and an appropriate response packet is built.
*/
SM_STATE(EAP, METHOD)
{
u8 *eapReqData;
size_t eapReqDataLen;
struct eap_method_ret ret;
SM_ENTRY(EAP, METHOD);
if (sm->m == NULL) {
wpa_printf(MSG_WARNING, "EAP::METHOD - method not selected");
return;
}
eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen);
/*
* Get ignore, methodState, decision, allowNotifications, and
* eapRespData. RFC 4137 uses three separate method procedure (check,
* process, and buildResp) in this state. These have been combined into
* a single function call to m->process() in order to optimize EAP
* method implementation interface a bit. These procedures are only
* used from within this METHOD state, so there is no need to keep
* these as separate C functions.
*
* The RFC 4137 procedures return values as follows:
* ignore = m.check(eapReqData)
* (methodState, decision, allowNotifications) = m.process(eapReqData)
* eapRespData = m.buildResp(reqId)
*/
os_memset(&ret, 0, sizeof(ret));
ret.ignore = sm->ignore;
ret.methodState = sm->methodState;
ret.decision = sm->decision;
ret.allowNotifications = sm->allowNotifications;
os_free(sm->eapRespData);
sm->eapRespData = NULL;
sm->eapRespData = sm->m->process(sm, sm->eap_method_priv, &ret,
eapReqData, eapReqDataLen,
&sm->eapRespDataLen);
wpa_printf(MSG_DEBUG, "EAP: method process -> ignore=%s "
"methodState=%s decision=%s",
ret.ignore ? "TRUE" : "FALSE",
eap_sm_method_state_txt(ret.methodState),
eap_sm_decision_txt(ret.decision));
sm->ignore = ret.ignore;
if (sm->ignore)
return;
sm->methodState = ret.methodState;
sm->decision = ret.decision;
sm->allowNotifications = ret.allowNotifications;
if (sm->m->isKeyAvailable && sm->m->getKey &&
sm->m->isKeyAvailable(sm, sm->eap_method_priv)) {
os_free(sm->eapKeyData);
sm->eapKeyData = sm->m->getKey(sm, sm->eap_method_priv,
&sm->eapKeyDataLen);
}
}
/*
* This state signals the lower layer that a response packet is ready to be
* sent.
*/
SM_STATE(EAP, SEND_RESPONSE)
{
SM_ENTRY(EAP, SEND_RESPONSE);
os_free(sm->lastRespData);
if (sm->eapRespData) {
if (sm->workaround)
os_memcpy(sm->last_md5, sm->req_md5, 16);
sm->lastId = sm->reqId;
sm->lastRespData = os_malloc(sm->eapRespDataLen);
if (sm->lastRespData) {
os_memcpy(sm->lastRespData, sm->eapRespData,
sm->eapRespDataLen);
sm->lastRespDataLen = sm->eapRespDataLen;
}
eapol_set_bool(sm, EAPOL_eapResp, TRUE);
} else
sm->lastRespData = NULL;
eapol_set_bool(sm, EAPOL_eapReq, FALSE);
eapol_set_int(sm, EAPOL_idleWhile, sm->ClientTimeout);
}
/*
* This state signals the lower layer that the request was discarded, and no
* response packet will be sent at this time.
*/
SM_STATE(EAP, DISCARD)
{
SM_ENTRY(EAP, DISCARD);
eapol_set_bool(sm, EAPOL_eapReq, FALSE);
eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
}
/*
* Handles requests for Identity method and builds a response.
*/
SM_STATE(EAP, IDENTITY)
{
const u8 *eapReqData;
size_t eapReqDataLen;
SM_ENTRY(EAP, IDENTITY);
eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen);
eap_sm_processIdentity(sm, eapReqData);
os_free(sm->eapRespData);
sm->eapRespData = NULL;
sm->eapRespData = eap_sm_buildIdentity(sm, sm->reqId,
&sm->eapRespDataLen, 0);
}
/*
* Handles requests for Notification method and builds a response.
*/
SM_STATE(EAP, NOTIFICATION)
{
const u8 *eapReqData;
size_t eapReqDataLen;
SM_ENTRY(EAP, NOTIFICATION);
eapReqData = eapol_get_eapReqData(sm, &eapReqDataLen);
eap_sm_processNotify(sm, eapReqData);
os_free(sm->eapRespData);
sm->eapRespData = NULL;
sm->eapRespData = eap_sm_buildNotify(sm->reqId, &sm->eapRespDataLen);
}
/*
* This state retransmits the previous response packet.
*/
SM_STATE(EAP, RETRANSMIT)
{
SM_ENTRY(EAP, RETRANSMIT);
os_free(sm->eapRespData);
if (sm->lastRespData) {
sm->eapRespData = os_malloc(sm->lastRespDataLen);
if (sm->eapRespData) {
os_memcpy(sm->eapRespData, sm->lastRespData,
sm->lastRespDataLen);
sm->eapRespDataLen = sm->lastRespDataLen;
}
} else
sm->eapRespData = NULL;
}
/*
* This state is entered in case of a successful completion of authentication
* and state machine waits here until port is disabled or EAP authentication is
* restarted.
*/
SM_STATE(EAP, SUCCESS)
{
SM_ENTRY(EAP, SUCCESS);
if (sm->eapKeyData != NULL)
sm->eapKeyAvailable = TRUE;
eapol_set_bool(sm, EAPOL_eapSuccess, TRUE);
/*
* RFC 4137 does not clear eapReq here, but this seems to be required
* to avoid processing the same request twice when state machine is
* initialized.
*/
eapol_set_bool(sm, EAPOL_eapReq, FALSE);
/*
* RFC 4137 does not set eapNoResp here, but this seems to be required
* to get EAPOL Supplicant backend state machine into SUCCESS state. In
* addition, either eapResp or eapNoResp is required to be set after
* processing the received EAP frame.
*/
eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
"EAP authentication completed successfully");
}
/*
* This state is entered in case of a failure and state machine waits here
* until port is disabled or EAP authentication is restarted.
*/
SM_STATE(EAP, FAILURE)
{
SM_ENTRY(EAP, FAILURE);
eapol_set_bool(sm, EAPOL_eapFail, TRUE);
/*
* RFC 4137 does not clear eapReq here, but this seems to be required
* to avoid processing the same request twice when state machine is
* initialized.
*/
eapol_set_bool(sm, EAPOL_eapReq, FALSE);
/*
* RFC 4137 does not set eapNoResp here. However, either eapResp or
* eapNoResp is required to be set after processing the received EAP
* frame.
*/
eapol_set_bool(sm, EAPOL_eapNoResp, TRUE);
wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
"EAP authentication failed");
}
static int eap_success_workaround(struct eap_sm *sm, int reqId, int lastId)
{
/*
* At least Microsoft IAS and Meetinghouse Aegis seem to be sending
* EAP-Success/Failure with lastId + 1 even though RFC 3748 and
* RFC 4137 require that reqId == lastId. In addition, it looks like
* Ringmaster v2.1.2.0 would be using lastId + 2 in EAP-Success.
*
* Accept this kind of Id if EAP workarounds are enabled. These are
* unauthenticated plaintext messages, so this should have minimal
* security implications (bit easier to fake EAP-Success/Failure).
*/
if (sm->workaround && (reqId == ((lastId + 1) & 0xff) ||
reqId == ((lastId + 2) & 0xff))) {
wpa_printf(MSG_DEBUG, "EAP: Workaround for unexpected "
"identifier field in EAP Success: "
"reqId=%d lastId=%d (these are supposed to be "
"same)", reqId, lastId);
return 1;
}
wpa_printf(MSG_DEBUG, "EAP: EAP-Success Id mismatch - reqId=%d "
"lastId=%d", reqId, lastId);
return 0;
}
/*
* RFC 4137 - Appendix A.1: EAP Peer State Machine - State transitions
*/
SM_STEP(EAP)
{
int duplicate;
if (eapol_get_bool(sm, EAPOL_eapRestart) &&
eapol_get_bool(sm, EAPOL_portEnabled))
SM_ENTER_GLOBAL(EAP, INITIALIZE);
else if (!eapol_get_bool(sm, EAPOL_portEnabled) || sm->force_disabled)
SM_ENTER_GLOBAL(EAP, DISABLED);
else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
/* RFC 4137 does not place any limit on number of EAP messages
* in an authentication session. However, some error cases have
* ended up in a state were EAP messages were sent between the
* peer and server in a loop (e.g., TLS ACK frame in both
* direction). Since this is quite undesired outcome, limit the
* total number of EAP round-trips and abort authentication if
* this limit is exceeded.
*/
if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
wpa_msg(sm->msg_ctx, MSG_INFO, "EAP: more than %d "
"authentication rounds - abort",
EAP_MAX_AUTH_ROUNDS);
sm->num_rounds++;
SM_ENTER_GLOBAL(EAP, FAILURE);
}
} else switch (sm->EAP_state) {
case EAP_INITIALIZE:
SM_ENTER(EAP, IDLE);
break;
case EAP_DISABLED:
if (eapol_get_bool(sm, EAPOL_portEnabled) &&
!sm->force_disabled)
SM_ENTER(EAP, INITIALIZE);
break;
case EAP_IDLE:
/*
* The first three transitions are from RFC 4137. The last two
* are local additions to handle special cases with LEAP and
* PEAP server not sending EAP-Success in some cases.
*/
if (eapol_get_bool(sm, EAPOL_eapReq))
SM_ENTER(EAP, RECEIVED);
else if ((eapol_get_bool(sm, EAPOL_altAccept) &&
sm->decision != DECISION_FAIL) ||
(eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
sm->decision == DECISION_UNCOND_SUCC))
SM_ENTER(EAP, SUCCESS);
else if (eapol_get_bool(sm, EAPOL_altReject) ||
(eapol_get_int(sm, EAPOL_idleWhile) == 0 &&
sm->decision != DECISION_UNCOND_SUCC) ||
(eapol_get_bool(sm, EAPOL_altAccept) &&
sm->methodState != METHOD_CONT &&
sm->decision == DECISION_FAIL))
SM_ENTER(EAP, FAILURE);
else if (sm->selectedMethod == EAP_TYPE_LEAP &&
sm->leap_done && sm->decision != DECISION_FAIL &&
sm->methodState == METHOD_DONE)
SM_ENTER(EAP, SUCCESS);
else if (sm->selectedMethod == EAP_TYPE_PEAP &&
sm->peap_done && sm->decision != DECISION_FAIL &&
sm->methodState == METHOD_DONE)
SM_ENTER(EAP, SUCCESS);
break;
case EAP_RECEIVED:
duplicate = (sm->reqId == sm->lastId) && sm->rxReq;
if (sm->workaround && duplicate &&
os_memcmp(sm->req_md5, sm->last_md5, 16) != 0) {
/*
* RFC 4137 uses (reqId == lastId) as the only
* verification for duplicate EAP requests. However,
* this misses cases where the AS is incorrectly using
* the same id again; and unfortunately, such
* implementations exist. Use MD5 hash as an extra
* verification for the packets being duplicate to
* workaround these issues.
*/
wpa_printf(MSG_DEBUG, "EAP: AS used the same Id again,"
" but EAP packets were not identical");
wpa_printf(MSG_DEBUG, "EAP: workaround - assume this "
"is not a duplicate packet");
duplicate = 0;
}
/*
* Two special cases below for LEAP are local additions to work
* around odd LEAP behavior (EAP-Success in the middle of
* authentication and then swapped roles). Other transitions
* are based on RFC 4137.
*/
if (sm->rxSuccess && sm->decision != DECISION_FAIL &&
(sm->reqId == sm->lastId ||
eap_success_workaround(sm, sm->reqId, sm->lastId)))
SM_ENTER(EAP, SUCCESS);
else if (sm->methodState != METHOD_CONT &&
((sm->rxFailure &&
sm->decision != DECISION_UNCOND_SUCC) ||
(sm->rxSuccess && sm->decision == DECISION_FAIL &&
(sm->selectedMethod != EAP_TYPE_LEAP ||
sm->methodState != METHOD_MAY_CONT))) &&
(sm->reqId == sm->lastId ||
eap_success_workaround(sm, sm->reqId, sm->lastId)))
SM_ENTER(EAP, FAILURE);
else if (sm->rxReq && duplicate)
SM_ENTER(EAP, RETRANSMIT);
else if (sm->rxReq && !duplicate &&
sm->reqMethod == EAP_TYPE_NOTIFICATION &&
sm->allowNotifications)
SM_ENTER(EAP, NOTIFICATION);
else if (sm->rxReq && !duplicate &&
sm->selectedMethod == EAP_TYPE_NONE &&
sm->reqMethod == EAP_TYPE_IDENTITY)
SM_ENTER(EAP, IDENTITY);
else if (sm->rxReq && !duplicate &&
sm->selectedMethod == EAP_TYPE_NONE &&
sm->reqMethod != EAP_TYPE_IDENTITY &&
sm->reqMethod != EAP_TYPE_NOTIFICATION)
SM_ENTER(EAP, GET_METHOD);
else if (sm->rxReq && !duplicate &&
sm->reqMethod == sm->selectedMethod &&
sm->methodState != METHOD_DONE)
SM_ENTER(EAP, METHOD);
else if (sm->selectedMethod == EAP_TYPE_LEAP &&
(sm->rxSuccess || sm->rxResp))
SM_ENTER(EAP, METHOD);
else
SM_ENTER(EAP, DISCARD);
break;
case EAP_GET_METHOD:
if (sm->selectedMethod == sm->reqMethod)
SM_ENTER(EAP, METHOD);
else
SM_ENTER(EAP, SEND_RESPONSE);
break;
case EAP_METHOD:
if (sm->ignore)
SM_ENTER(EAP, DISCARD);
else
SM_ENTER(EAP, SEND_RESPONSE);
break;
case EAP_SEND_RESPONSE:
SM_ENTER(EAP, IDLE);
break;
case EAP_DISCARD:
SM_ENTER(EAP, IDLE);
break;
case EAP_IDENTITY:
SM_ENTER(EAP, SEND_RESPONSE);
break;
case EAP_NOTIFICATION:
SM_ENTER(EAP, SEND_RESPONSE);
break;
case EAP_RETRANSMIT:
SM_ENTER(EAP, SEND_RESPONSE);
break;
case EAP_SUCCESS:
break;
case EAP_FAILURE:
break;
}
}
static Boolean eap_sm_allowMethod(struct eap_sm *sm, int vendor,
EapType method)
{
struct wpa_ssid *config = eap_get_config(sm);
if (!wpa_config_allowed_eap_method(config, vendor, method)) {
wpa_printf(MSG_DEBUG, "EAP: configuration does not allow: "
"vendor %u method %u", vendor, method);
return FALSE;
}
if (eap_sm_get_eap_methods(vendor, method))
return TRUE;
wpa_printf(MSG_DEBUG, "EAP: not included in build: "
"vendor %u method %u", vendor, method);
return FALSE;
}
static u8 * eap_sm_build_expanded_nak(struct eap_sm *sm, int id, size_t *len,
const struct eap_method *methods,
size_t count)
{
struct wpa_ssid *config = eap_get_config(sm);
struct eap_hdr *resp;
u8 *pos;
int found = 0;
const struct eap_method *m;
wpa_printf(MSG_DEBUG, "EAP: Building expanded EAP-Nak");
/* RFC 3748 - 5.3.2: Expanded Nak */
*len = sizeof(struct eap_hdr) + 8;
resp = os_malloc(*len + 8 * (count + 1));
if (resp == NULL)
return NULL;
resp->code = EAP_CODE_RESPONSE;
resp->identifier = id;
pos = (u8 *) (resp + 1);
*pos++ = EAP_TYPE_EXPANDED;
WPA_PUT_BE24(pos, EAP_VENDOR_IETF);
pos += 3;
WPA_PUT_BE32(pos, EAP_TYPE_NAK);
pos += 4;
for (m = methods; m; m = m->next) {
if (sm->reqVendor == m->vendor &&
sm->reqVendorMethod == m->method)
continue; /* do not allow the current method again */
if (wpa_config_allowed_eap_method(config, m->vendor,
m->method)) {
wpa_printf(MSG_DEBUG, "EAP: allowed type: "
"vendor=%u method=%u",
m->vendor, m->method);
*pos++ = EAP_TYPE_EXPANDED;
WPA_PUT_BE24(pos, m->vendor);
pos += 3;
WPA_PUT_BE32(pos, m->method);
pos += 4;
(*len) += 8;
found++;
}
}
if (!found) {
wpa_printf(MSG_DEBUG, "EAP: no more allowed methods");
*pos++ = EAP_TYPE_EXPANDED;
WPA_PUT_BE24(pos, EAP_VENDOR_IETF);
pos += 3;
WPA_PUT_BE32(pos, EAP_TYPE_NONE);
pos += 4;
(*len) += 8;
}
resp->length = host_to_be16(*len);
return (u8 *) resp;
}
static u8 * eap_sm_buildNak(struct eap_sm *sm, int id, size_t *len)
{
struct wpa_ssid *config = eap_get_config(sm);
struct eap_hdr *resp;
u8 *pos;
int found = 0, expanded_found = 0;
size_t count;
const struct eap_method *methods, *m;
wpa_printf(MSG_DEBUG, "EAP: Building EAP-Nak (requested type %u "
"vendor=%u method=%u not allowed)", sm->reqMethod,
sm->reqVendor, sm->reqVendorMethod);
methods = eap_peer_get_methods(&count);
if (methods == NULL)
return NULL;
if (sm->reqMethod == EAP_TYPE_EXPANDED)
return eap_sm_build_expanded_nak(sm, id, len, methods, count);
/* RFC 3748 - 5.3.1: Legacy Nak */
*len = sizeof(struct eap_hdr) + 1;
resp = os_malloc(*len + count + 1);
if (resp == NULL)
return NULL;
resp->code = EAP_CODE_RESPONSE;
resp->identifier = id;
pos = (u8 *) (resp + 1);
*pos++ = EAP_TYPE_NAK;
for (m = methods; m; m = m->next) {
if (m->vendor == EAP_VENDOR_IETF && m->method == sm->reqMethod)
continue; /* do not allow the current method again */
if (wpa_config_allowed_eap_method(config, m->vendor,
m->method)) {
if (m->vendor != EAP_VENDOR_IETF) {
if (expanded_found)
continue;
expanded_found = 1;
*pos++ = EAP_TYPE_EXPANDED;
} else
*pos++ = m->method;
(*len)++;
found++;
}
}
if (!found) {
*pos = EAP_TYPE_NONE;
(*len)++;
}
wpa_hexdump(MSG_DEBUG, "EAP: allowed methods",
((u8 *) (resp + 1)) + 1, found);
resp->length = host_to_be16(*len);
return (u8 *) resp;
}
static void eap_sm_processIdentity(struct eap_sm *sm, const u8 *req)
{
const struct eap_hdr *hdr = (const struct eap_hdr *) req;
const u8 *pos = (const u8 *) (hdr + 1);
pos++;
wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
"EAP authentication started");
/*
* RFC 3748 - 5.1: Identity
* Data field may contain a displayable message in UTF-8. If this
* includes NUL-character, only the data before that should be
* displayed. Some EAP implementasitons may piggy-back additional
* options after the NUL.
*/
/* TODO: could save displayable message so that it can be shown to the
* user in case of interaction is required */
wpa_hexdump_ascii(MSG_DEBUG, "EAP: EAP-Request Identity data",
pos, be_to_host16(hdr->length) - 5);
}
#ifdef PCSC_FUNCS
static int eap_sm_imsi_identity(struct eap_sm *sm, struct wpa_ssid *ssid)
{
int aka = 0;
char imsi[100];
size_t imsi_len;
struct eap_method_type *m = ssid->eap_methods;
int i;
imsi_len = sizeof(imsi);
if (scard_get_imsi(sm->scard_ctx, imsi, &imsi_len)) {
wpa_printf(MSG_WARNING, "Failed to get IMSI from SIM");
return -1;
}
wpa_hexdump_ascii(MSG_DEBUG, "IMSI", (u8 *) imsi, imsi_len);
for (i = 0; m && (m[i].vendor != EAP_VENDOR_IETF ||
m[i].method != EAP_TYPE_NONE); i++) {
if (m[i].vendor == EAP_VENDOR_IETF &&
m[i].method == EAP_TYPE_AKA) {
aka = 1;
break;
}
}
os_free(ssid->identity);
ssid->identity = os_malloc(1 + imsi_len);
if (ssid->identity == NULL) {
wpa_printf(MSG_WARNING, "Failed to allocate buffer for "
"IMSI-based identity");
return -1;
}
ssid->identity[0] = aka ? '0' : '1';
os_memcpy(ssid->identity + 1, imsi, imsi_len);
ssid->identity_len = 1 + imsi_len;
return 0;
}
#endif /* PCSC_FUNCS */
static int eap_sm_set_scard_pin(struct eap_sm *sm, struct wpa_ssid *ssid)
{
#ifdef PCSC_FUNCS
if (scard_set_pin(sm->scard_ctx, ssid->pin)) {
/*
* Make sure the same PIN is not tried again in order to avoid
* blocking SIM.
*/
os_free(ssid->pin);
ssid->pin = NULL;
wpa_printf(MSG_WARNING, "PIN validation failed");
eap_sm_request_pin(sm);
return -1;
}
return 0;
#else /* PCSC_FUNCS */
return -1;
#endif /* PCSC_FUNCS */
}
static int eap_sm_get_scard_identity(struct eap_sm *sm, struct wpa_ssid *ssid)
{
#ifdef PCSC_FUNCS
if (eap_sm_set_scard_pin(sm, ssid))
return -1;
return eap_sm_imsi_identity(sm, ssid);
#else /* PCSC_FUNCS */
return -1;
#endif /* PCSC_FUNCS */
}
/**
* eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
* @sm: Pointer to EAP state machine allocated with eap_sm_init()
* @id: EAP identifier for the packet
* @len: Pointer to a variable that will be set to the length of the response
* @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
* Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
* failure
*
* This function allocates and builds an EAP-Identity/Response packet for the
* current network. The caller is responsible for freeing the returned data.
*/
u8 * eap_sm_buildIdentity(struct eap_sm *sm, int id, size_t *len,
int encrypted)
{
struct wpa_ssid *config = eap_get_config(sm);
struct eap_hdr *resp;
u8 *pos;
const u8 *identity;
size_t identity_len;
if (config == NULL) {
wpa_printf(MSG_WARNING, "EAP: buildIdentity: configuration "
"was not available");
return NULL;
}
if (sm->m && sm->m->get_identity &&
(identity = sm->m->get_identity(sm, sm->eap_method_priv,
&identity_len)) != NULL) {
wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
"identity", identity, identity_len);
} else if (!encrypted && config->anonymous_identity) {
identity = config->anonymous_identity;
identity_len = config->anonymous_identity_len;
wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
identity, identity_len);
} else {
identity = config->identity;
identity_len = config->identity_len;
wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
identity, identity_len);
}
if (identity == NULL) {
wpa_printf(MSG_WARNING, "EAP: buildIdentity: identity "
"configuration was not available");
if (config->pcsc) {
if (eap_sm_get_scard_identity(sm, config) < 0)
return NULL;
identity = config->identity;
identity_len = config->identity_len;
wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
"IMSI", identity, identity_len);
} else {
eap_sm_request_identity(sm);
return NULL;
}
} else if (config->pcsc) {
if (eap_sm_set_scard_pin(sm, config) < 0)
return NULL;
}
*len = sizeof(struct eap_hdr) + 1 + identity_len;
resp = os_malloc(*len);
if (resp == NULL)
return NULL;
resp->code = EAP_CODE_RESPONSE;
resp->identifier = id;
resp->length = host_to_be16(*len);
pos = (u8 *) (resp + 1);