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