DEBUG -> RDEBUG
[freeradius.git] / src / modules / rlm_eap / libeap / eap_tls.c
1 /*
2  * eap_tls.c
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
21  * Copyright 2003  Alan DeKok <aland@freeradius.org>
22  * Copyright 2006  The FreeRADIUS server project
23  */
24
25 /*
26  *
27  *  TLS Packet Format in EAP
28  *  --- ------ ------ -- ---
29  * 0                   1                   2                   3
30  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
31  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32  * |     Code      |   Identifier  |            Length             |
33  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  * |     Type      |     Flags     |      TLS Message Length
35  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36  * |     TLS Message Length        |       TLS Data...
37  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38  *
39  */
40
41 #include <freeradius-devel/ident.h>
42 RCSID("$Id$")
43
44 #include <freeradius-devel/autoconf.h>
45 #include <assert.h>
46 #include "eap_tls.h"
47
48 /*
49  *      Allocate a new TLS_PACKET
50  */
51 EAPTLS_PACKET *eaptls_alloc(void)
52 {
53         EAPTLS_PACKET   *rp;
54
55         if ((rp = malloc(sizeof(EAPTLS_PACKET))) == NULL) {
56                 radlog(L_ERR, "rlm_eap_tls: out of memory");
57                 return NULL;
58         }
59         memset(rp, 0, sizeof(EAPTLS_PACKET));
60         return rp;
61 }
62
63 /*
64  *      Free EAPTLS_PACKET
65  */
66 void eaptls_free(EAPTLS_PACKET **eaptls_packet_ptr)
67 {
68         EAPTLS_PACKET *eaptls_packet;
69
70         if (!eaptls_packet_ptr) return;
71         eaptls_packet = *eaptls_packet_ptr;
72         if (eaptls_packet == NULL) return;
73
74         if (eaptls_packet->data) {
75                 free(eaptls_packet->data);
76                 eaptls_packet->data = NULL;
77         }
78
79         free(eaptls_packet);
80         *eaptls_packet_ptr = NULL;
81 }
82
83 /*
84    The S flag is set only within the EAP-TLS start message
85    sent from the EAP server to the peer.
86 */
87 int eaptls_start(EAP_DS *eap_ds, int peap_flag)
88 {
89         EAPTLS_PACKET   reply;
90
91         reply.code = EAPTLS_START;
92         reply.length = TLS_HEADER_LEN + 1/*flags*/;
93
94         reply.flags = peap_flag;
95         reply.flags = SET_START(reply.flags);
96
97         reply.data = NULL;
98         reply.dlen = 0;
99
100         eaptls_compose(eap_ds, &reply);
101
102         return 1;
103 }
104
105 int eaptls_success(EAP_DS *eap_ds, int peap_flag)
106 {
107         EAPTLS_PACKET   reply;
108
109         reply.code = EAPTLS_SUCCESS;
110         reply.length = TLS_HEADER_LEN;
111         reply.flags = peap_flag;
112         reply.data = NULL;
113         reply.dlen = 0;
114
115         eaptls_compose(eap_ds, &reply);
116
117         return 1;
118 }
119
120 int eaptls_fail(EAP_DS *eap_ds, int peap_flag)
121 {
122         EAPTLS_PACKET   reply;
123
124         reply.code = EAPTLS_FAIL;
125         reply.length = TLS_HEADER_LEN;
126         reply.flags = peap_flag;
127         reply.data = NULL;
128         reply.dlen = 0;
129
130         eaptls_compose(eap_ds, &reply);
131
132         return 1;
133 }
134
135 /*
136    A single TLS record may be up to 16384 octets in length, but a TLS
137    message may span multiple TLS records, and a TLS certificate message
138    may in principle be as long as 16MB.
139 */
140
141 /*
142  *      Frame the Dirty data that needs to be send to the client in an
143  *      EAP-Request.  We always embed the TLS-length in all EAP-TLS
144  *      packets that we send, for easy reference purpose.  Handle
145  *      fragmentation and sending the next fragment etc.
146  */
147 int eaptls_request(EAP_DS *eap_ds, tls_session_t *ssn)
148 {
149         EAPTLS_PACKET   reply;
150         unsigned int    size;
151         unsigned int    nlen;
152         unsigned int    lbit = 0;
153
154         /* This value determines whether we set (L)ength flag for
155                 EVERY packet we send and add corresponding
156                 "TLS Message Length" field.
157
158         length_flag = TRUE;
159                 This means we include L flag and "TLS Msg Len" in EVERY
160                 packet we send out.
161
162         length_flag = FALSE;
163                 This means we include L flag and "TLS Msg Len" **ONLY**
164                 in First packet of a fragment series. We do not use
165                 it anywhere else.
166
167                 Having L flag in every packet is prefered.
168
169         */
170         if (ssn->length_flag) {
171                 lbit = 4;
172         }
173         if (ssn->fragment == 0) {
174                 ssn->tls_msg_len = ssn->dirty_out.used;
175         }
176
177         reply.code = EAPTLS_REQUEST;
178         reply.flags = ssn->peap_flag;
179
180         /* Send data, NOT more than the FRAGMENT size */
181         if (ssn->dirty_out.used > ssn->offset) {
182                 size = ssn->offset;
183                 reply.flags = SET_MORE_FRAGMENTS(reply.flags);
184                 /* Length MUST be included if it is the First Fragment */
185                 if (ssn->fragment == 0) {
186                         lbit = 4;
187                 }
188                 ssn->fragment = 1;
189         } else {
190                 size = ssn->dirty_out.used;
191                 ssn->fragment = 0;
192         }
193
194         reply.dlen = lbit + size;
195         reply.length = TLS_HEADER_LEN + 1/*flags*/ + reply.dlen;
196
197         reply.data = malloc(reply.dlen);
198         if (lbit) {
199                 nlen = htonl(ssn->tls_msg_len);
200                 memcpy(reply.data, &nlen, lbit);
201                 reply.flags = SET_LENGTH_INCLUDED(reply.flags);
202         }
203         (ssn->record_minus)(&ssn->dirty_out, reply.data + lbit, size);
204
205         eaptls_compose(eap_ds, &reply);
206         free(reply.data);
207         reply.data = NULL;
208
209         return 1;
210 }
211
212 /*
213  * Acknowledge received is for one of the following messages sent earlier
214  * 1. Handshake completed Message, so now send, EAP-Success
215  * 2. Alert Message, now send, EAP-Failure
216  * 3. Fragment Message, now send, next Fragment
217  */
218 static eaptls_status_t eaptls_ack_handler(EAP_HANDLER *handler)
219 {
220         tls_session_t *tls_session;
221         REQUEST *request = handler->request;
222
223         tls_session = (tls_session_t *)handler->opaque;
224         if (tls_session == NULL){
225                 radlog_request(L_ERR, 0, request, "Unexpected ACK received");
226                 return EAPTLS_FAIL;
227         }
228         if (tls_session->info.initialized == 0) {
229                 RDEBUG("No SSL info available. Waiting for more SSL data.");
230                 return EAPTLS_REQUEST;
231         }
232         if (tls_session->info.origin == 0) {
233                 radlog_request(L_ERR, 0, request, "Unexpected ACK received");
234                 return EAPTLS_FAIL;
235         }
236
237         switch (tls_session->info.content_type) {
238         case alert:
239                 RDEBUG2("ACK alert");
240                 eaptls_fail(handler->eap_ds, tls_session->peap_flag);
241                 return EAPTLS_FAIL;
242
243         case handshake:
244                 if ((tls_session->info.handshake_type == finished) &&
245                     (tls_session->dirty_out.used == 0)) {
246                         RDEBUG2("ACK handshake is finished");
247                         return EAPTLS_SUCCESS;
248                 } /* else more data to send */
249
250                 RDEBUG2("ACK handshake fragment handler");
251                 /* Fragmentation handler, send next fragment */
252                 return EAPTLS_REQUEST;
253
254                 /*
255                  *      For the rest of the conditions, switch over
256                  *      to the default section below.
257                  */
258         default:
259                 RDEBUG2("ACK default");
260                 radlog_request(L_ERR, 0, request, "Invalid ACK received: %d",
261                        tls_session->info.content_type);
262                 return EAPTLS_FAIL;
263         }
264 }
265
266 /*
267  *      Similarly, when the EAP server receives an EAP-Response with
268  *      the M bit set, it MUST respond with an EAP-Request with
269  *      EAP-Type=EAP-TLS and no data. This serves as a fragment ACK.
270  *
271  *      In order to prevent errors in the processing of fragments, the
272  *      EAP server MUST use increment the Identifier value for each
273  *      fragment ACK contained within an EAP-Request, and the peer
274  *      MUST include this Identifier value in the subsequent fragment
275  *      contained within an EAP- Reponse.
276  *
277  *      EAP server sends an ACK when it determines there are More
278  *      fragments to receive to make the complete
279  *      TLS-record/TLS-Message
280  */
281 static int eaptls_send_ack(EAP_DS *eap_ds, int peap_flag)
282 {
283         EAPTLS_PACKET   reply;
284
285         reply.code = EAPTLS_ACK;
286         reply.length = TLS_HEADER_LEN + 1/*flags*/;
287         reply.flags = peap_flag;
288         reply.data = NULL;
289         reply.dlen = 0;
290
291         eaptls_compose(eap_ds, &reply);
292
293         return 1;
294 }
295
296 /*
297  *      The S flag is set only within the EAP-TLS start message sent
298  *      from the EAP server to the peer.
299  *
300  *      Similarly, when the EAP server receives an EAP-Response with
301  *      the M bit set, it MUST respond with an EAP-Request with
302  *      EAP-Type=EAP-TLS and no data. This serves as a fragment
303  *      ACK. The EAP peer MUST wait.
304  */
305 static eaptls_status_t eaptls_verify(EAP_HANDLER *handler)
306 {
307         EAP_DS *eap_ds = handler->eap_ds;
308         EAP_DS *prev_eap_ds = handler->prev_eapds;
309         eaptls_packet_t *eaptls_packet, *eaptls_prev = NULL;
310         REQUEST *request = handler->request;
311
312         /*
313          *      We don't check ANY of the input parameters.  It's all
314          *      code which works together, so if something is wrong,
315          *      we SHOULD core dump.
316          *
317          *      e.g. if eap_ds is NULL, of if eap_ds->response is
318          *      NULL, of if it's NOT an EAP-Response, or if the packet
319          *      is too short.  See eap_validation()., in ../../eap.c
320          *
321          *      Also, eaptype_select() takes care of selecting the
322          *      appropriate type, so we don't need to check
323          *      eap_ds->response->type.type == PW_EAP_TLS, or anything
324          *      else.
325          */
326         eaptls_packet = (eaptls_packet_t *)eap_ds->response->type.data;
327         if (prev_eap_ds && prev_eap_ds->response)
328                 eaptls_prev = (eaptls_packet_t *)prev_eap_ds->response->type.data;
329
330         /*
331          *      check for ACK
332          *
333          *      If there's no TLS data, or there's 1 byte of TLS data,
334          *      with the flags set to zero, then it's an ACK.
335          *
336          *      Find if this is a reply to the previous request sent
337          */
338         if ((eaptls_packet == NULL) ||
339             ((eap_ds->response->length == EAP_HEADER_LEN + 2) &&
340              ((eaptls_packet->flags & 0xc0) == 0x00))) {
341
342 #if 0
343                 /*
344                  *      Un-comment this for TLS inside of TTLS/PEAP
345                  */
346                 RDEBUG2("Received EAP-TLS ACK message");
347                 return eaptls_ack_handler(handler);
348 #else
349                 if (prev_eap_ds->request->id == eap_ds->response->id) {
350                         /*
351                          *      Run the ACK handler directly from here.
352                          */
353                         RDEBUG2("Received TLS ACK");
354                         return eaptls_ack_handler(handler);
355                 } else {
356                         radlog_request(L_ERR, 0, request, "Received Invalid TLS ACK");
357                         return EAPTLS_INVALID;
358                 }
359 #endif
360         }
361
362         /*
363          *      We send TLS_START, but do not receive it.
364          */
365         if (TLS_START(eaptls_packet->flags)) {
366                 radlog(L_ERR, "rlm_eap_tls:  Received unexpected EAP-TLS Start message");
367                 return EAPTLS_INVALID;
368         }
369
370         /*
371          *      The L bit (length included) is set to indicate the
372          *      presence of the four octet TLS Message Length field,
373          *      and MUST be set for the first fragment of a fragmented
374          *      TLS message or set of messages.
375          *
376          *      The M bit (more fragments) is set on all but the last
377          *      fragment.
378          *
379          *      The S bit (EAP-TLS start) is set in an EAP-TLS Start
380          *      message. This differentiates the EAP-TLS Start message
381          *      from a fragment acknowledgement.
382          */
383         if (TLS_LENGTH_INCLUDED(eaptls_packet->flags)) {
384                 DEBUG2("  TLS Length %d",
385                        eaptls_packet->data[2] * 256 | eaptls_packet->data[3]);
386                 if (TLS_MORE_FRAGMENTS(eaptls_packet->flags)) {
387                         /*
388                          * FIRST_FRAGMENT is identified
389                          * 1. If there is no previous EAP-response received.
390                          * 2. If EAP-response received, then its M bit not set.
391                          *      (It is because Last fragment will not have M bit set)
392                          */
393                         if (!prev_eap_ds ||
394                             (prev_eap_ds->response == NULL) ||
395                             (eaptls_prev == NULL) ||
396                             !TLS_MORE_FRAGMENTS(eaptls_prev->flags)) {
397
398                                 DEBUG2("rlm_eap_tls:  Received EAP-TLS First Fragment of the message");
399                                 return EAPTLS_FIRST_FRAGMENT;
400                         } else {
401
402                                 DEBUG2("rlm_eap_tls:  More Fragments with length included");
403                                 return EAPTLS_MORE_FRAGMENTS_WITH_LENGTH;
404                         }
405                 } else {
406                         DEBUG2("rlm_eap_tls:  Length Included");
407                         return EAPTLS_LENGTH_INCLUDED;
408                 }
409         }
410
411         if (TLS_MORE_FRAGMENTS(eaptls_packet->flags)) {
412                 DEBUG2("rlm_eap_tls:  More fragments to follow");
413                 return EAPTLS_MORE_FRAGMENTS;
414         }
415
416         /*
417          *      None of the flags are set, but it's still a valid
418          *      EAPTLS packet.
419          */
420         return EAPTLS_OK;
421 }
422
423 /*
424  * EAPTLS_PACKET
425  * code   =  EAP-code
426  * id     =  EAP-id
427  * length = code + id + length + flags + tlsdata
428  *        =  1   +  1 +   2    +  1    +  X
429  * length = EAP-length - 1(EAP-Type = 1 octet)
430  * flags  = EAP-typedata[0] (1 octet)
431  * dlen   = EAP-typedata[1-4] (4 octets), if L flag set
432  *        = length - 5(code+id+length+flags), otherwise
433  * data   = EAP-typedata[5-n], if L flag set
434  *        = EAP-typedata[1-n], otherwise
435  * packet = EAP-typedata (complete typedata)
436  *
437  * Points to consider during EAP-TLS data extraction
438  * 1. In the received packet, No data will be present incase of ACK-NAK
439  * 2. Incase if more fragments need to be received then ACK after retreiving this fragment.
440  *
441  *  RFC 2716 Section 4.2.  PPP EAP TLS Request Packet
442  *
443  *  0                   1                   2                   3
444  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
445  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
446  *  |     Code      |   Identifier  |            Length             |
447  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
448  *  |     Type      |     Flags     |      TLS Message Length
449  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
450  *  |     TLS Message Length        |       TLS Data...
451  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
452  *
453  *  The Length field is two octets and indicates the length of the EAP
454  *  packet including the Code, Identifir, Length, Type, and TLS data
455  *  fields.
456  */
457 static EAPTLS_PACKET *eaptls_extract(EAP_DS *eap_ds, eaptls_status_t status)
458 {
459         EAPTLS_PACKET   *tlspacket;
460         uint32_t        data_len = 0;
461         uint32_t        len = 0;
462         uint8_t         *data = NULL;
463
464         if (status  == EAPTLS_INVALID)
465                 return NULL;
466
467         /*
468          *      The main EAP code & eaptls_verify() take care of
469          *      ensuring that the packet is OK, and that we can
470          *      extract the various fields we want.
471          *
472          *      e.g. a TLS packet with zero data is allowed as an ACK,
473          *      but we will never see it here, as we will simply
474          *      send another fragment, instead of trying to extract
475          *      the data.
476          *
477          *      MUST have TLS type octet, followed by flags, followed
478          *      by data.
479          */
480         assert(eap_ds->response->length > 2);
481
482         tlspacket = eaptls_alloc();
483         if (tlspacket == NULL) return NULL;
484
485         /*
486          *      Code & id for EAPTLS & EAP are same
487          *      but eaptls_length = eap_length - 1(EAP-Type = 1 octet)
488          *
489          *      length = code + id + length + type + tlsdata
490          *             =  1   +  1 +   2    +  1    +  X
491          */
492         tlspacket->code = eap_ds->response->code;
493         tlspacket->id = eap_ds->response->id;
494         tlspacket->length = eap_ds->response->length - 1; /* EAP type */
495         tlspacket->flags = eap_ds->response->type.data[0];
496
497         /*
498          *      A quick sanity check of the flags.  If we've been told
499          *      that there's a length, and there isn't one, then stop.
500          */
501         if (TLS_LENGTH_INCLUDED(tlspacket->flags) &&
502             (tlspacket->length < 5)) { /* flags + TLS message length */
503                 radlog(L_ERR, "rlm_eap_tls: Invalid EAP-TLS packet received.  (Length bit is set, but no length was found.)");
504                 eaptls_free(&tlspacket);
505                 return NULL;
506         }
507
508         /*
509          *      If the final TLS packet is larger than we can handle, die
510          *      now.
511          *
512          *      Likewise, if the EAP packet says N bytes, and the TLS
513          *      packet says there's fewer bytes, it's a problem.
514          *
515          *      FIXME: Try to ensure that the claimed length is
516          *      consistent across multiple TLS fragments.
517          */
518         if (TLS_LENGTH_INCLUDED(tlspacket->flags)) {
519                 memcpy(&data_len, &eap_ds->response->type.data[1], 4);
520                 data_len = ntohl(data_len);
521                 if (data_len > MAX_RECORD_SIZE) {
522                         radlog(L_ERR, "rlm_eap_tls: The EAP-TLS packet will contain more data than we can process.");
523                         eaptls_free(&tlspacket);
524                         return NULL;
525                 }
526
527 #if 0
528                 DEBUG2(" TLS: %d %d\n", data_len, tlspacket->length);
529
530                 if (data_len < tlspacket->length) {
531                         radlog(L_ERR, "rlm_eap_tls: EAP-TLS packet claims to be smaller than the encapsulating EAP packet.");
532                         eaptls_free(&tlspacket);
533                         return NULL;
534                 }
535 #endif
536         }
537
538         switch (status) {
539         /*
540          *      The TLS Message Length field is four octets, and
541          *      provides the total length of the TLS message or set of
542          *      messages that is being fragmented; this simplifies
543          *      buffer allocation.
544          *
545          *      Dynamic allocation of buffers as & when we know the
546          *      length should solve the problem.
547          */
548         case EAPTLS_FIRST_FRAGMENT:
549         case EAPTLS_LENGTH_INCLUDED:
550         case EAPTLS_MORE_FRAGMENTS_WITH_LENGTH:
551                 if (tlspacket->length < 5) { /* flags + TLS message length */
552                         radlog(L_ERR, "rlm_eap_tls: Invalid EAP-TLS packet received.  (Expected length, got none.)");
553                         eaptls_free(&tlspacket);
554                         return NULL;
555                 }
556
557                 /*
558                  *      Extract all the TLS fragments from the
559                  *      previous eap_ds Start appending this
560                  *      fragment to the above ds
561                  */
562                 memcpy(&data_len, &eap_ds->response->type.data[1], sizeof(uint32_t));
563                 data_len = ntohl(data_len);
564                 data = (eap_ds->response->type.data + 5/*flags+TLS-Length*/);
565                 len = eap_ds->response->type.length - 5/*flags+TLS-Length*/;
566
567                 /*
568                  *      Hmm... this should be an error, too.
569                  */
570                 if (data_len > len) {
571                         data_len = len;
572                 }
573                 break;
574
575                 /*
576                  *      Data length is implicit, from the EAP header.
577                  */
578         case EAPTLS_MORE_FRAGMENTS:
579         case EAPTLS_OK:
580                 data_len = eap_ds->response->type.length - 1/*flags*/;
581                 data = eap_ds->response->type.data + 1/*flags*/;
582                 break;
583
584         default:
585                 radlog(L_ERR, "rlm_eap_tls: Invalid EAP-TLS packet received");
586                 eaptls_free(&tlspacket);
587                 return NULL;
588         }
589
590         tlspacket->dlen = data_len;
591         if (data_len) {
592                 tlspacket->data = (unsigned char *)malloc(data_len);
593                 if (tlspacket->data == NULL) {
594                         radlog(L_ERR, "rlm_eap_tls: out of memory");
595                         eaptls_free(&tlspacket);
596                         return NULL;
597                 }
598                 memcpy(tlspacket->data, data, data_len);
599         }
600
601         return tlspacket;
602 }
603
604
605
606 /*
607  * To process the TLS,
608  *  INCOMING DATA:
609  *      1. EAP-TLS should get the compelete TLS data from the peer.
610  *      2. Store that data in a data structure with any other required info
611  *      3. Handle that data structure to the TLS module.
612  *      4. TLS module will perform its operations on the data and
613  *      handle back to EAP-TLS
614  *
615  *  OUTGOING DATA:
616  *      1. EAP-TLS if necessary will fragment it and send it to the
617  *      destination.
618  *
619  *      During EAP-TLS initialization, TLS Context object will be
620  *      initialized and stored.  For every new authentication
621  *      requests, TLS will open a new session object and that session
622  *      object should be maintained even after the session is
623  *      completed for session resumption. (Probably later as a feature
624  *      as we donot know who maintains these session objects ie,
625  *      SSL_CTX (internally) or TLS module(explicitly). If TLS module,
626  *      then how to let SSL API know about these sessions.)
627  */
628 static void eaptls_operation(EAPTLS_PACKET *eaptls_packet UNUSED,
629                              eaptls_status_t status, EAP_HANDLER *handler)
630 {
631         tls_session_t *tls_session;
632
633         tls_session = (tls_session_t *)handler->opaque;
634
635         if ((status == EAPTLS_MORE_FRAGMENTS) ||
636             (status == EAPTLS_MORE_FRAGMENTS_WITH_LENGTH) ||
637             (status == EAPTLS_FIRST_FRAGMENT)) {
638                 /*
639                  * Send the ACK.
640                  */
641                 eaptls_send_ack(handler->eap_ds, tls_session->peap_flag);
642         } else {
643                 int rcode;
644
645                 /*
646                  *      We have the complete TLS-data or TLS-message.
647                  *
648                  *      Clean the dirty message.
649                  *
650                  *      Authenticate the user and send
651                  *      Success/Failure.
652                  *
653                  *      If more info
654                  *      is required then send another request.
655                  */
656                 rcode = tls_handshake_recv(tls_session);
657                 if (rcode == 1) {
658                         /*
659                          *      FIXME: return success/fail.
660                          *
661                          *      TLS proper can decide what to do, then.
662                          */
663                         eaptls_request(handler->eap_ds, tls_session);
664
665                         /*
666                          *      TLS returns 0 or 1.
667                          *      anything else is application-specific.
668                          *
669                          *      In our code, this means "session
670                          *      resumption was OK".
671                          */
672                 } else if (rcode == 0xea) {
673                         /*
674                          *      FIXME: hard-code key based on EAP type.
675                          *      Also, this code is duplicated all over
676                          *      the place...
677                          */
678                         eaptls_success(handler->eap_ds, 0);
679                         eaptls_gen_mppe_keys(&handler->request->reply->vps,
680                                              tls_session->ssl,
681                                              "client EAP encryption");
682
683                 } else {
684                         eaptls_fail(handler->eap_ds, tls_session->peap_flag);
685
686                 }
687         }
688         return;
689 }
690
691
692 /*
693  * In the actual authentication first verify the packet and then create the data structure
694  */
695 /*
696  * To process the TLS,
697  *  INCOMING DATA:
698  *      1. EAP-TLS should get the compelete TLS data from the peer.
699  *      2. Store that data in a data structure with any other required info
700  *      3. Hand this data structure to the TLS module.
701  *      4. TLS module will perform its operations on the data and hands back to EAP-TLS
702  *  OUTGOING DATA:
703  *      1. EAP-TLS if necessary will fragment it and send it to the destination.
704  *
705  *      During EAP-TLS initialization, TLS Context object will be
706  *      initialized and stored.  For every new authentication
707  *      requests, TLS will open a new session object and that
708  *      session object SHOULD be maintained even after the session
709  *      is completed, for session resumption. (Probably later as a
710  *      feature, as we do not know who maintains these session
711  *      objects ie, SSL_CTX (internally) or TLS module (explicitly). If
712  *      TLS module, then how to let SSL API know about these
713  *      sessions.)
714  */
715
716 /*
717  *      Process an EAP request
718  */
719 eaptls_status_t eaptls_process(EAP_HANDLER *handler)
720 {
721         tls_session_t *tls_session = (tls_session_t *) handler->opaque;
722         EAPTLS_PACKET   *tlspacket;
723         eaptls_status_t status;
724         REQUEST *request = handler->request;
725
726         RDEBUG2("processing EAP-TLS");
727
728         /* This case is when SSL generates Alert then we
729          * send that alert to the client and then send the EAP-Failure
730          */
731         status = eaptls_verify(handler);
732         RDEBUG2("eaptls_verify returned %d\n", status);
733
734         switch (status) {
735         default:
736         case EAPTLS_INVALID:
737         case EAPTLS_FAIL:
738
739                 /*
740                  *      Success means that we're done the initial
741                  *      handshake.  For TTLS, this means send stuff
742                  *      back to the client, and the client sends us
743                  *      more tunneled data.
744                  */
745         case EAPTLS_SUCCESS:
746                 return status;
747                 break;
748
749                 /*
750                  *      Normal TLS request, continue with the "get rest
751                  *      of fragments" phase.
752                  */
753         case EAPTLS_REQUEST:
754                 eaptls_request(handler->eap_ds, tls_session);
755                 return EAPTLS_HANDLED;
756                 break;
757
758                 /*
759                  *      The handshake is done, and we're in the "tunnel
760                  *      data" phase.
761                  */
762         case EAPTLS_OK:
763                 RDEBUG2("Done initial handshake");
764
765                 /*
766                  *      Get the rest of the fragments.
767                  */
768         case EAPTLS_FIRST_FRAGMENT:
769         case EAPTLS_MORE_FRAGMENTS:
770         case EAPTLS_LENGTH_INCLUDED:
771         case EAPTLS_MORE_FRAGMENTS_WITH_LENGTH:
772                 break;
773         }
774
775         /*
776          *      Extract the TLS packet from the buffer.
777          */
778         if ((tlspacket = eaptls_extract(handler->eap_ds, status)) == NULL)
779                 return EAPTLS_FAIL;
780
781         /*
782          *      Get the session struct from the handler
783          *
784          *      update the dirty_in buffer
785          *
786          *      NOTE: This buffer will contain partial data when M bit is set.
787          *
788          *      CAUTION while reinitializing this buffer, it should be
789          *      reinitialized only when this M bit is NOT set.
790          */
791         if (tlspacket->dlen !=
792             (tls_session->record_plus)(&tls_session->dirty_in, tlspacket->data, tlspacket->dlen)) {
793                 eaptls_free(&tlspacket);
794                 radlog(L_ERR, "rlm_eap_tls: Exceeded maximum record size");
795                 return EAPTLS_FAIL;
796         }
797
798         /*
799          *      SSL initalization is done.  Return.
800          *
801          *      The TLS data will be in the tls_session structure.
802          */
803         if (SSL_is_init_finished(tls_session->ssl)) {
804                 eaptls_free(&tlspacket);
805                 return EAPTLS_OK;
806         }
807
808         /*
809          *      Continue the handshake.
810          */
811         eaptls_operation(tlspacket, status, handler);
812
813         eaptls_free(&tlspacket);
814         return EAPTLS_HANDLED;
815 }
816
817
818 /*
819  *      compose the TLS reply packet in the EAP reply typedata
820  */
821 int eaptls_compose(EAP_DS *eap_ds, EAPTLS_PACKET *reply)
822 {
823         uint8_t *ptr;
824
825         /*
826          *      Don't set eap_ds->request->type.type, as the main EAP
827          *      handler will do that for us.  This allows the TLS
828          *      module to be called from TTLS & PEAP.
829          */
830
831         /*
832          *      When the EAP server receives an EAP-Response with the
833          *      M bit set, it MUST respond with an EAP-Request with
834          *      EAP-Type=EAP-TLS and no data. This serves as a
835          *      fragment ACK. The EAP peer MUST wait until it receives
836          *      the EAP-Request before sending another fragment.
837          *
838          *      In order to prevent errors in the processing of
839          *      fragments, the EAP server MUST use increment the
840          *      Identifier value for each fragment ACK contained
841          *      within an EAP-Request, and the peer MUST include this
842          *      Identifier value in the subsequent fragment contained
843          *      within an EAP- Reponse.
844          */
845         eap_ds->request->type.data = malloc(reply->length - TLS_HEADER_LEN + 1);
846         if (eap_ds->request->type.data == NULL) {
847                 radlog(L_ERR, "rlm_eap_tls: out of memory");
848                 return 0;
849         }
850
851         /* EAPTLS Header length is excluded while computing EAP typelen */
852         eap_ds->request->type.length = reply->length - TLS_HEADER_LEN;
853
854         ptr = eap_ds->request->type.data;
855         *ptr++ = (uint8_t)(reply->flags & 0xFF);
856
857         if (reply->dlen) memcpy(ptr, reply->data, reply->dlen);
858
859         switch (reply->code) {
860         case EAPTLS_ACK:
861         case EAPTLS_START:
862         case EAPTLS_REQUEST:
863                 eap_ds->request->code = PW_EAP_REQUEST;
864                 break;
865         case EAPTLS_SUCCESS:
866                 eap_ds->request->code = PW_EAP_SUCCESS;
867                 break;
868         case EAPTLS_FAIL:
869                 eap_ds->request->code = PW_EAP_FAILURE;
870                 break;
871         default:
872                 /* Should never enter here */
873                 eap_ds->request->code = PW_EAP_FAILURE;
874                 break;
875         }
876
877         return 1;
878 }