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