]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/eap_peap.c
This commit was generated by cvs2svn to compensate for changes in r165071,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / eap_peap.c
1 /*
2  * WPA Supplicant / EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-07.txt)
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "common.h"
20 #include "eap_i.h"
21 #include "eap_tls_common.h"
22 #include "wpa_supplicant.h"
23 #include "config_ssid.h"
24 #include "tls.h"
25 #include "eap_tlv.h"
26
27
28 /* Maximum supported PEAP version
29  * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
30  * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
31  * 2 = draft-josefsson-ppext-eap-tls-eap-07.txt
32  */
33 #define EAP_PEAP_VERSION 1
34
35
36 static void eap_peap_deinit(struct eap_sm *sm, void *priv);
37
38
39 struct eap_peap_data {
40         struct eap_ssl_data ssl;
41
42         int peap_version, force_peap_version, force_new_label;
43
44         const struct eap_method *phase2_method;
45         void *phase2_priv;
46         int phase2_success;
47
48         u8 phase2_type;
49         u8 *phase2_types;
50         size_t num_phase2_types;
51
52         int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
53                                  * EAP-Success
54                                  * 1 = reply with tunneled EAP-Success to inner
55                                  * EAP-Success and expect AS to send outer
56                                  * (unencrypted) EAP-Success after this
57                                  * 2 = reply with PEAP/TLS ACK to inner
58                                  * EAP-Success and expect AS to send outer
59                                  * (unencrypted) EAP-Success after this */
60         int resuming; /* starting a resumed session */
61         u8 *key_data;
62
63         u8 *pending_phase2_req;
64         size_t pending_phase2_req_len;
65 };
66
67
68 static void * eap_peap_init(struct eap_sm *sm)
69 {
70         struct eap_peap_data *data;
71         struct wpa_ssid *config = eap_get_config(sm);
72
73         data = malloc(sizeof(*data));
74         if (data == NULL)
75                 return NULL;
76         sm->peap_done = FALSE;
77         memset(data, 0, sizeof(*data));
78         data->peap_version = EAP_PEAP_VERSION;
79         data->force_peap_version = -1;
80         data->peap_outer_success = 2;
81
82         if (config && config->phase1) {
83                 char *pos = strstr(config->phase1, "peapver=");
84                 if (pos) {
85                         data->force_peap_version = atoi(pos + 8);
86                         data->peap_version = data->force_peap_version;
87                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version "
88                                    "%d", data->force_peap_version);
89                 }
90
91                 if (strstr(config->phase1, "peaplabel=1")) {
92                         data->force_new_label = 1;
93                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for "
94                                    "key derivation");
95                 }
96
97                 if (strstr(config->phase1, "peap_outer_success=0")) {
98                         data->peap_outer_success = 0;
99                         wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate "
100                                    "authentication on tunneled EAP-Success");
101                 } else if (strstr(config->phase1, "peap_outer_success=1")) {
102                         data->peap_outer_success = 1;
103                         wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled "
104                                    "EAP-Success after receiving tunneled "
105                                    "EAP-Success");
106                 } else if (strstr(config->phase1, "peap_outer_success=2")) {
107                         data->peap_outer_success = 2;
108                         wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK "
109                                    "after receiving tunneled EAP-Success");
110                 }
111         }
112
113         if (config && config->phase2) {
114                 char *start, *pos, *buf;
115                 u8 method, *methods = NULL, *_methods;
116                 size_t num_methods = 0;
117                 start = buf = strdup(config->phase2);
118                 if (buf == NULL) {
119                         eap_peap_deinit(sm, data);
120                         return NULL;
121                 }
122                 while (start && *start != '\0') {
123                         pos = strstr(start, "auth=");
124                         if (pos == NULL)
125                                 break;
126                         if (start != pos && *(pos - 1) != ' ') {
127                                 start = pos + 5;
128                                 continue;
129                         }
130
131                         start = pos + 5;
132                         pos = strchr(start, ' ');
133                         if (pos)
134                                 *pos++ = '\0';
135                         method = eap_get_phase2_type(start);
136                         if (method == EAP_TYPE_NONE) {
137                                 wpa_printf(MSG_ERROR, "EAP-PEAP: Unsupported "
138                                            "Phase2 method '%s'", start);
139                         } else {
140                                 num_methods++;
141                                 _methods = realloc(methods, num_methods);
142                                 if (_methods == NULL) {
143                                         free(methods);
144                                         eap_peap_deinit(sm, data);
145                                         return NULL;
146                                 }
147                                 methods = _methods;
148                                 methods[num_methods - 1] = method;
149                         }
150
151                         start = pos;
152                 }
153                 free(buf);
154                 data->phase2_types = methods;
155                 data->num_phase2_types = num_methods;
156         }
157         if (data->phase2_types == NULL) {
158                 data->phase2_types =
159                         eap_get_phase2_types(config, &data->num_phase2_types);
160         }
161         if (data->phase2_types == NULL) {
162                 wpa_printf(MSG_ERROR, "EAP-PEAP: No Phase2 method available");
163                 eap_peap_deinit(sm, data);
164                 return NULL;
165         }
166         wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Phase2 EAP types",
167                     data->phase2_types, data->num_phase2_types);
168         data->phase2_type = EAP_TYPE_NONE;
169
170         if (eap_tls_ssl_init(sm, &data->ssl, config)) {
171                 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
172                 eap_peap_deinit(sm, data);
173                 return NULL;
174         }
175
176         return data;
177 }
178
179
180 static void eap_peap_deinit(struct eap_sm *sm, void *priv)
181 {
182         struct eap_peap_data *data = priv;
183         if (data == NULL)
184                 return;
185         if (data->phase2_priv && data->phase2_method)
186                 data->phase2_method->deinit(sm, data->phase2_priv);
187         free(data->phase2_types);
188         eap_tls_ssl_deinit(sm, &data->ssl);
189         free(data->key_data);
190         free(data->pending_phase2_req);
191         free(data);
192 }
193
194
195 static int eap_peap_encrypt(struct eap_sm *sm, struct eap_peap_data *data,
196                             int id, const u8 *plain, size_t plain_len,
197                             u8 **out_data, size_t *out_len)
198 {
199         int res;
200         u8 *pos;
201         struct eap_hdr *resp;
202
203         /* TODO: add support for fragmentation, if needed. This will need to
204          * add TLS Message Length field, if the frame is fragmented.
205          * Note: Microsoft IAS did not seem to like TLS Message Length with
206          * PEAP/MSCHAPv2. */
207         resp = malloc(sizeof(struct eap_hdr) + 2 + data->ssl.tls_out_limit);
208         if (resp == NULL)
209                 return -1;
210
211         resp->code = EAP_CODE_RESPONSE;
212         resp->identifier = id;
213
214         pos = (u8 *) (resp + 1);
215         *pos++ = EAP_TYPE_PEAP;
216         *pos++ = data->peap_version;
217
218         res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
219                                      plain, plain_len,
220                                      pos, data->ssl.tls_out_limit);
221         if (res < 0) {
222                 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt Phase 2 "
223                            "data");
224                 free(resp);
225                 return -1;
226         }
227
228         *out_len = sizeof(struct eap_hdr) + 2 + res;
229         resp->length = host_to_be16(*out_len);
230         *out_data = (u8 *) resp;
231         return 0;
232 }
233
234
235 static int eap_peap_phase2_nak(struct eap_sm *sm,
236                                struct eap_peap_data *data,
237                                struct eap_hdr *hdr,
238                                u8 **resp, size_t *resp_len)
239 {
240         struct eap_hdr *resp_hdr;
241         u8 *pos = (u8 *) (hdr + 1);
242
243         wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: Nak type=%d", *pos);
244         wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Allowed Phase2 EAP types",
245                     data->phase2_types, data->num_phase2_types);
246         *resp_len = sizeof(struct eap_hdr) + 1 + data->num_phase2_types;
247         *resp = malloc(*resp_len);
248         if (*resp == NULL)
249                 return -1;
250
251         resp_hdr = (struct eap_hdr *) (*resp);
252         resp_hdr->code = EAP_CODE_RESPONSE;
253         resp_hdr->identifier = hdr->identifier;
254         resp_hdr->length = host_to_be16(*resp_len);
255         pos = (u8 *) (resp_hdr + 1);
256         *pos++ = EAP_TYPE_NAK;
257         memcpy(pos, data->phase2_types, data->num_phase2_types);
258
259         return 0;
260 }
261
262
263 static int eap_peap_phase2_request(struct eap_sm *sm,
264                                    struct eap_peap_data *data,
265                                    struct eap_method_ret *ret,
266                                    const struct eap_hdr *req,
267                                    struct eap_hdr *hdr,
268                                    u8 **resp, size_t *resp_len)
269 {
270         size_t len = be_to_host16(hdr->length);
271         u8 *pos;
272         struct eap_method_ret iret;
273         struct wpa_ssid *config = eap_get_config(sm);
274
275         if (len <= sizeof(struct eap_hdr)) {
276                 wpa_printf(MSG_INFO, "EAP-PEAP: too short "
277                            "Phase 2 request (len=%lu)", (unsigned long) len);
278                 return -1;
279         }
280         pos = (u8 *) (hdr + 1);
281         wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
282         switch (*pos) {
283         case EAP_TYPE_IDENTITY:
284                 *resp = eap_sm_buildIdentity(sm, req->identifier, resp_len, 1);
285                 break;
286         case EAP_TYPE_TLV:
287                 memset(&iret, 0, sizeof(iret));
288                 if (eap_tlv_process(sm, &iret, hdr, resp, resp_len)) {
289                         ret->methodState = METHOD_DONE;
290                         ret->decision = DECISION_FAIL;
291                         return -1;
292                 }
293                 if (iret.methodState == METHOD_DONE ||
294                     iret.methodState == METHOD_MAY_CONT) {
295                         ret->methodState = iret.methodState;
296                         ret->decision = iret.decision;
297                         data->phase2_success = 1;
298                 }
299                 break;
300         default:
301                 if (data->phase2_type == EAP_TYPE_NONE) {
302                         int i;
303                         for (i = 0; i < data->num_phase2_types; i++) {
304                                 if (data->phase2_types[i] != *pos)
305                                         continue;
306
307                                 data->phase2_type = *pos;
308                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
309                                            "Phase 2 EAP method %d",
310                                            data->phase2_type);
311                                 break;
312                         }
313                 }
314                 if (*pos != data->phase2_type || *pos == EAP_TYPE_NONE) {
315                         if (eap_peap_phase2_nak(sm, data, hdr, resp, resp_len))
316                                 return -1;
317                         return 0;
318                 }
319
320                 if (data->phase2_priv == NULL) {
321                         data->phase2_method = eap_sm_get_eap_methods(*pos);
322                         if (data->phase2_method) {
323                                 sm->init_phase2 = 1;
324                                 data->phase2_priv =
325                                         data->phase2_method->init(sm);
326                                 sm->init_phase2 = 0;
327                         }
328                 }
329                 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
330                         wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
331                                    "Phase 2 EAP method %d", *pos);
332                         ret->methodState = METHOD_DONE;
333                         ret->decision = DECISION_FAIL;
334                         return -1;
335                 }
336                 memset(&iret, 0, sizeof(iret));
337                 *resp = data->phase2_method->process(sm, data->phase2_priv,
338                                                      &iret, (u8 *) hdr, len,
339                                                      resp_len);
340                 if ((iret.methodState == METHOD_DONE ||
341                      iret.methodState == METHOD_MAY_CONT) &&
342                     (iret.decision == DECISION_UNCOND_SUCC ||
343                      iret.decision == DECISION_COND_SUCC)) {
344                         data->phase2_success = 1;
345                 }
346                 break;
347         }
348
349         if (*resp == NULL &&
350             (config->pending_req_identity || config->pending_req_password ||
351              config->pending_req_otp || config->pending_req_new_password)) {
352                 free(data->pending_phase2_req);
353                 data->pending_phase2_req = malloc(len);
354                 if (data->pending_phase2_req) {
355                         memcpy(data->pending_phase2_req, hdr, len);
356                         data->pending_phase2_req_len = len;
357                 }
358         }
359
360         return 0;
361 }
362
363
364 static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
365                             struct eap_method_ret *ret,
366                             const struct eap_hdr *req,
367                             const u8 *in_data, size_t in_len,
368                             u8 **out_data, size_t *out_len)
369 {
370         u8 *in_decrypted;
371         int buf_len, len_decrypted, len, skip_change = 0;
372         struct eap_hdr *hdr, *rhdr;
373         u8 *resp = NULL;
374         size_t resp_len;
375         const u8 *msg;
376         size_t msg_len;
377         int need_more_input;
378
379         wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
380                    " Phase 2", (unsigned long) in_len);
381
382         if (data->pending_phase2_req) {
383                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
384                            "skip decryption and use old data");
385                 /* Clear TLS reassembly state. */
386                 free(data->ssl.tls_in);
387                 data->ssl.tls_in = NULL;
388                 data->ssl.tls_in_len = 0;
389                 data->ssl.tls_in_left = 0;
390                 data->ssl.tls_in_total = 0;
391                 in_decrypted = data->pending_phase2_req;
392                 data->pending_phase2_req = NULL;
393                 len_decrypted = data->pending_phase2_req_len;
394                 skip_change = 1;
395                 goto continue_req;
396         }
397
398         msg = eap_tls_data_reassemble(sm, &data->ssl, in_data, in_len,
399                                       &msg_len, &need_more_input);
400         if (msg == NULL)
401                 return need_more_input ? 1 : -1;
402
403         if (in_len == 0 && sm->workaround && data->phase2_success) {
404                 /*
405                  * Cisco ACS seems to be using TLS ACK to terminate
406                  * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
407                  */
408                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
409                            "expected data - acknowledge with TLS ACK since "
410                            "Phase 2 has been completed");
411                 ret->decision = DECISION_COND_SUCC;
412                 ret->methodState = METHOD_DONE;
413                 return 1;
414         }
415
416         buf_len = in_len;
417         if (data->ssl.tls_in_total > buf_len)
418                 buf_len = data->ssl.tls_in_total;
419         in_decrypted = malloc(buf_len);
420         if (in_decrypted == NULL) {
421                 free(data->ssl.tls_in);
422                 data->ssl.tls_in = NULL;
423                 data->ssl.tls_in_len = 0;
424                 wpa_printf(MSG_WARNING, "EAP-PEAP: failed to allocate memory "
425                            "for decryption");
426                 return -1;
427         }
428
429         len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
430                                                msg, msg_len,
431                                                in_decrypted, buf_len);
432         free(data->ssl.tls_in);
433         data->ssl.tls_in = NULL;
434         data->ssl.tls_in_len = 0;
435         if (len_decrypted < 0) {
436                 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to decrypt Phase 2 "
437                            "data");
438                 free(in_decrypted);
439                 return 0;
440         }
441
442 continue_req:
443         wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP", in_decrypted,
444                     len_decrypted);
445
446         hdr = (struct eap_hdr *) in_decrypted;
447         if (len_decrypted == 5 && hdr->code == EAP_CODE_REQUEST &&
448             be_to_host16(hdr->length) == 5 &&
449             in_decrypted[4] == EAP_TYPE_IDENTITY) {
450                 /* At least FreeRADIUS seems to send full EAP header with
451                  * EAP Request Identity */
452                 skip_change = 1;
453         }
454         if (len_decrypted >= 5 && hdr->code == EAP_CODE_REQUEST &&
455             in_decrypted[4] == EAP_TYPE_TLV) {
456                 skip_change = 1;
457         }
458
459         if (data->peap_version == 0 && !skip_change) {
460                 struct eap_hdr *nhdr = malloc(sizeof(struct eap_hdr) +
461                                               len_decrypted);
462                 if (nhdr == NULL) {
463                         free(in_decrypted);
464                         return 0;
465                 }
466                 memcpy((u8 *) (nhdr + 1), in_decrypted, len_decrypted);
467                 free(in_decrypted);
468                 nhdr->code = req->code;
469                 nhdr->identifier = req->identifier;
470                 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
471                                             len_decrypted);
472
473                 len_decrypted += sizeof(struct eap_hdr);
474                 in_decrypted = (u8 *) nhdr;
475         }
476         hdr = (struct eap_hdr *) in_decrypted;
477         if (len_decrypted < sizeof(*hdr)) {
478                 free(in_decrypted);
479                 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
480                            "EAP frame (len=%d)", len_decrypted);
481                 return 0;
482         }
483         len = be_to_host16(hdr->length);
484         if (len > len_decrypted) {
485                 free(in_decrypted);
486                 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
487                            "Phase 2 EAP frame (len=%d hdr->length=%d)",
488                            len_decrypted, len);
489                 return 0;
490         }
491         if (len < len_decrypted) {
492                 wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
493                            "shorter length than full decrypted data (%d < %d)",
494                            len, len_decrypted);
495                 if (sm->workaround && len == 4 && len_decrypted == 5 &&
496                     in_decrypted[4] == EAP_TYPE_IDENTITY) {
497                         /* Radiator 3.9 seems to set Phase 2 EAP header to use
498                          * incorrect length for the EAP-Request Identity
499                          * packet, so fix the inner header to interoperate..
500                          * This was fixed in 2004-06-23 patch for Radiator and
501                          * this workaround can be removed at some point. */
502                         wpa_printf(MSG_INFO, "EAP-PEAP: workaround -> replace "
503                                    "Phase 2 EAP header len (%d) with real "
504                                    "decrypted len (%d)", len, len_decrypted);
505                         len = len_decrypted;
506                         hdr->length = host_to_be16(len);
507                 }
508         }
509         wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
510                    "identifier=%d length=%d", hdr->code, hdr->identifier, len);
511         switch (hdr->code) {
512         case EAP_CODE_REQUEST:
513                 if (eap_peap_phase2_request(sm, data, ret, req, hdr,
514                                             &resp, &resp_len)) {
515                         free(in_decrypted);
516                         wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
517                                    "processing failed");
518                         return 0;
519                 }
520                 break;
521         case EAP_CODE_SUCCESS:
522                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
523                 if (data->peap_version == 1) {
524                         /* EAP-Success within TLS tunnel is used to indicate
525                          * shutdown of the TLS channel. The authentication has
526                          * been completed. */
527                         wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
528                                    "EAP-Success within TLS tunnel - "
529                                    "authentication completed");
530                         ret->decision = DECISION_UNCOND_SUCC;
531                         ret->methodState = METHOD_DONE;
532                         data->phase2_success = 1;
533                         if (data->peap_outer_success == 2) {
534                                 free(in_decrypted);
535                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
536                                            "to finish authentication");
537                                 return 1;
538                         } else if (data->peap_outer_success == 1) {
539                                 /* Reply with EAP-Success within the TLS
540                                  * channel to complete the authentication. */
541                                 resp_len = sizeof(struct eap_hdr);
542                                 resp = malloc(resp_len);
543                                 if (resp) {
544                                         memset(resp, 0, resp_len);
545                                         rhdr = (struct eap_hdr *) resp;
546                                         rhdr->code = EAP_CODE_SUCCESS;
547                                         rhdr->identifier = hdr->identifier;
548                                         rhdr->length = host_to_be16(resp_len);
549                                 }
550                         } else {
551                                 /* No EAP-Success expected for Phase 1 (outer,
552                                  * unencrypted auth), so force EAP state
553                                  * machine to SUCCESS state. */
554                                 sm->peap_done = TRUE;
555                         }
556                 } else {
557                         /* FIX: ? */
558                 }
559                 break;
560         case EAP_CODE_FAILURE:
561                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
562                 ret->decision = DECISION_FAIL;
563                 ret->methodState = METHOD_MAY_CONT;
564                 ret->allowNotifications = FALSE;
565                 /* Reply with EAP-Failure within the TLS channel to complete
566                  * failure reporting. */
567                 resp_len = sizeof(struct eap_hdr);
568                 resp = malloc(resp_len);
569                 if (resp) {
570                         memset(resp, 0, resp_len);
571                         rhdr = (struct eap_hdr *) resp;
572                         rhdr->code = EAP_CODE_FAILURE;
573                         rhdr->identifier = hdr->identifier;
574                         rhdr->length = host_to_be16(resp_len);
575                 }
576                 break;
577         default:
578                 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
579                            "Phase 2 EAP header", hdr->code);
580                 break;
581         }
582
583         free(in_decrypted);
584
585         if (resp) {
586                 u8 *resp_pos;
587                 size_t resp_send_len;
588                 int skip_change = 0;
589
590                 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 data",
591                                 resp, resp_len);
592                 /* PEAP version changes */
593                 if (resp_len >= 5 && resp[0] == EAP_CODE_RESPONSE &&
594                     resp[4] == EAP_TYPE_TLV)
595                         skip_change = 1;
596                 if (data->peap_version == 0 && !skip_change) {
597                         resp_pos = resp + sizeof(struct eap_hdr);
598                         resp_send_len = resp_len - sizeof(struct eap_hdr);
599                 } else {
600                         resp_pos = resp;
601                         resp_send_len = resp_len;
602                 }
603
604                 if (eap_peap_encrypt(sm, data, req->identifier,
605                                      resp_pos, resp_send_len,
606                                      out_data, out_len)) {
607                         wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
608                                    "a Phase 2 frame");
609                 }
610                 free(resp);
611         }
612
613         return 0;
614 }
615
616
617 static u8 * eap_peap_process(struct eap_sm *sm, void *priv,
618                              struct eap_method_ret *ret,
619                              const u8 *reqData, size_t reqDataLen,
620                              size_t *respDataLen)
621 {
622         const struct eap_hdr *req;
623         size_t left;
624         int res;
625         u8 flags, *resp, id;
626         const u8 *pos;
627         struct eap_peap_data *data = priv;
628
629         pos = eap_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
630                                    reqData, reqDataLen, &left, &flags);
631         if (pos == NULL)
632                 return NULL;
633         req = (const struct eap_hdr *) reqData;
634         id = req->identifier;
635
636         if (flags & EAP_TLS_FLAGS_START) {
637                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
638                            "ver=%d)", flags & EAP_PEAP_VERSION_MASK,
639                         data->peap_version);
640                 if ((flags & EAP_PEAP_VERSION_MASK) < data->peap_version)
641                         data->peap_version = flags & EAP_PEAP_VERSION_MASK;
642                 if (data->force_peap_version >= 0 &&
643                     data->force_peap_version != data->peap_version) {
644                         wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
645                                    "forced PEAP version %d",
646                                    data->force_peap_version);
647                         ret->methodState = METHOD_DONE;
648                         ret->decision = DECISION_FAIL;
649                         ret->allowNotifications = FALSE;
650                         return NULL;
651                 }
652                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
653                            data->peap_version);
654                 left = 0; /* make sure that this frame is empty, even though it
655                            * should always be, anyway */
656         }
657
658         resp = NULL;
659         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
660             !data->resuming) {
661                 res = eap_peap_decrypt(sm, data, ret, req, pos, left,
662                                        &resp, respDataLen);
663         } else {
664                 res = eap_tls_process_helper(sm, &data->ssl, EAP_TYPE_PEAP,
665                                              data->peap_version, id, pos, left,
666                                              &resp, respDataLen);
667
668                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
669                         char *label;
670                         wpa_printf(MSG_DEBUG,
671                                    "EAP-PEAP: TLS done, proceed to Phase 2");
672                         free(data->key_data);
673                         /* draft-josefsson-ppext-eap-tls-eap-05.txt
674                          * specifies that PEAPv1 would use "client PEAP
675                          * encryption" as the label. However, most existing
676                          * PEAPv1 implementations seem to be using the old
677                          * label, "client EAP encryption", instead. Use the old
678                          * label by default, but allow it to be configured with
679                          * phase1 parameter peaplabel=1. */
680                         if (data->peap_version > 1 || data->force_new_label)
681                                 label = "client PEAP encryption";
682                         else
683                                 label = "client EAP encryption";
684                         wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
685                                    "key derivation", label);
686                         data->key_data =
687                                 eap_tls_derive_key(sm, &data->ssl, label,
688                                                    EAP_TLS_KEY_LEN);
689                         if (data->key_data) {
690                                 wpa_hexdump_key(MSG_DEBUG, 
691                                                 "EAP-PEAP: Derived key",
692                                                 data->key_data,
693                                                 EAP_TLS_KEY_LEN);
694                         } else {
695                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
696                                            "derive key");
697                         }
698
699                         if (sm->workaround && data->resuming) {
700                                 /*
701                                  * At least few RADIUS servers (Aegis v1.1.6;
702                                  * but not v1.1.4; and Cisco ACS) seem to be
703                                  * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
704                                  * ACS) session resumption with outer
705                                  * EAP-Success. This does not seem to follow
706                                  * draft-josefsson-pppext-eap-tls-eap-05.txt
707                                  * section 4.2, so only allow this if EAP
708                                  * workarounds are enabled.
709                                  */
710                                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
711                                            "allow outer EAP-Success to "
712                                            "terminate PEAP resumption");
713                                 ret->decision = DECISION_COND_SUCC;
714                                 data->phase2_success = 1;
715                         }
716
717                         data->resuming = 0;
718                 }
719         }
720
721         if (ret->methodState == METHOD_DONE) {
722                 ret->allowNotifications = FALSE;
723         }
724
725         if (res == 1) {
726                 return eap_tls_build_ack(&data->ssl, respDataLen, id,
727                                          EAP_TYPE_PEAP, data->peap_version);
728         }
729
730         return resp;
731 }
732
733
734 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
735 {
736         struct eap_peap_data *data = priv;
737         return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
738                 data->phase2_success;
739 }
740
741
742 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
743 {
744         struct eap_peap_data *data = priv;
745         free(data->pending_phase2_req);
746         data->pending_phase2_req = NULL;
747 }
748
749
750 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
751 {
752         struct eap_peap_data *data = priv;
753         free(data->key_data);
754         data->key_data = NULL;
755         if (eap_tls_reauth_init(sm, &data->ssl)) {
756                 free(data);
757                 return NULL;
758         }
759         data->phase2_success = 0;
760         data->resuming = 1;
761         sm->peap_done = FALSE;
762         return priv;
763 }
764
765
766 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
767                                size_t buflen, int verbose)
768 {
769         struct eap_peap_data *data = priv;
770         int len;
771
772         len = eap_tls_status(sm, &data->ssl, buf, buflen, verbose);
773         if (data->phase2_method) {
774                 len += snprintf(buf + len, buflen - len,
775                                 "EAP-PEAPv%d Phase2 method=%s\n",
776                                 data->peap_version, data->phase2_method->name);
777         }
778         return len;
779 }
780
781
782 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
783 {
784         struct eap_peap_data *data = priv;
785         return data->key_data != NULL && data->phase2_success;
786 }
787
788
789 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
790 {
791         struct eap_peap_data *data = priv;
792         u8 *key;
793
794         if (data->key_data == NULL || !data->phase2_success)
795                 return NULL;
796
797         key = malloc(EAP_TLS_KEY_LEN);
798         if (key == NULL)
799                 return NULL;
800
801         *len = EAP_TLS_KEY_LEN;
802         memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
803
804         return key;
805 }
806
807
808 const struct eap_method eap_method_peap =
809 {
810         .method = EAP_TYPE_PEAP,
811         .name = "PEAP",
812         .init = eap_peap_init,
813         .deinit = eap_peap_deinit,
814         .process = eap_peap_process,
815         .isKeyAvailable = eap_peap_isKeyAvailable,
816         .getKey = eap_peap_getKey,
817         .get_status = eap_peap_get_status,
818         .has_reauth_data = eap_peap_has_reauth_data,
819         .deinit_for_reauth = eap_peap_deinit_for_reauth,
820         .init_for_reauth = eap_peap_init_for_reauth,
821 };