import from HEAD:
[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., 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 "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 &&
336                     (prev_eap_ds->request->id == eap_ds->response->id)) {
337                         /*
338                          *      Run the ACK handler directly from here.
339                          */
340                         DEBUG2("rlm_eap_tls: Received EAP-TLS ACK message");
341                         return eaptls_ack_handler(handler);
342                 } else {
343                         radlog(L_ERR, "rlm_eap_tls: Received Invalid EAP-TLS ACK message");
344                         return EAPTLS_INVALID;
345                 }
346         }
347
348         /*
349          *      We send TLS_START, but do not receive it.
350          */
351         if (TLS_START(eaptls_packet->flags)) {
352                 radlog(L_ERR, "rlm_eap_tls:  Received unexpected EAP-TLS Start message");
353                 return EAPTLS_INVALID;
354         }
355
356         /*
357          *      The L bit (length included) is set to indicate the
358          *      presence of the four octet TLS Message Length field,
359          *      and MUST be set for the first fragment of a fragmented
360          *      TLS message or set of messages.
361          *
362          *      The M bit (more fragments) is set on all but the last
363          *      fragment.
364          *
365          *      The S bit (EAP-TLS start) is set in an EAP-TLS Start
366          *      message. This differentiates the EAP-TLS Start message
367          *      from a fragment acknowledgement.
368          */
369         if (TLS_LENGTH_INCLUDED(eaptls_packet->flags)) {
370                 if (TLS_MORE_FRAGMENTS(eaptls_packet->flags)) {
371                         /*
372                          * FIRST_FRAGMENT is identified
373                          * 1. If there is no previous EAP-response received.
374                          * 2. If EAP-response received, then its M bit not set.
375                          *      (It is because Last fragment will not have M bit set)
376                          */
377                         if (!prev_eap_ds ||
378                             (prev_eap_ds->response == NULL) ||
379                             (eaptls_prev == NULL) ||
380                             !TLS_MORE_FRAGMENTS(eaptls_prev->flags)) {
381
382                                 DEBUG2("rlm_eap_tls:  Received EAP-TLS First Fragment of the message");
383                                 return EAPTLS_FIRST_FRAGMENT;
384                         } else {
385
386                                 DEBUG2("rlm_eap_tls:  More Fragments with length included");
387                                 return EAPTLS_MORE_FRAGMENTS_WITH_LENGTH;
388                         }
389                 } else {
390
391                         DEBUG2("rlm_eap_tls:  Length Included");
392                         return EAPTLS_LENGTH_INCLUDED;
393                 }
394         }
395
396         if (TLS_MORE_FRAGMENTS(eaptls_packet->flags)) {
397                 DEBUG2("rlm_eap_tls:  More fragments to follow");
398                 return EAPTLS_MORE_FRAGMENTS;
399         }
400
401         /*
402          *      None of the flags are set, but it's still a valid
403          *      EAPTLS packet.
404          */
405         return EAPTLS_OK;
406 }
407
408 /*
409  * EAPTLS_PACKET
410  * code   =  EAP-code
411  * id     =  EAP-id
412  * length = code + id + length + flags + tlsdata
413  *        =  1   +  1 +   2    +  1    +  X
414  * length = EAP-length - 1(EAP-Type = 1 octet)
415  * flags  = EAP-typedata[0] (1 octet)
416  * dlen   = EAP-typedata[1-4] (4 octets), if L flag set
417  *        = length - 5(code+id+length+flags), otherwise
418  * data   = EAP-typedata[5-n], if L flag set
419  *        = EAP-typedata[1-n], otherwise
420  * packet = EAP-typedata (complete typedata)
421  *
422  * Points to consider during EAP-TLS data extraction
423  * 1. In the received packet, No data will be present incase of ACK-NAK
424  * 2. Incase if more fragments need to be received then ACK after retreiving this fragment.
425  *
426  *  RFC 2716 Section 4.2.  PPP EAP TLS Request Packet
427  *
428  *  0                   1                   2                   3
429  *  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
430  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
431  *  |     Code      |   Identifier  |            Length             |
432  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433  *  |     Type      |     Flags     |      TLS Message Length
434  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
435  *  |     TLS Message Length        |       TLS Data...
436  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
437  *
438  *  The Length field is two octets and indicates the length of the EAP
439  *  packet including the Code, Identifir, Length, Type, and TLS data
440  *  fields.
441  */
442 static EAPTLS_PACKET *eaptls_extract(EAP_DS *eap_ds, eaptls_status_t status)
443 {
444         EAPTLS_PACKET   *tlspacket;
445         uint32_t        data_len = 0;
446         uint32_t        len = 0;
447         uint8_t         *data = NULL;
448
449         if (status  == EAPTLS_INVALID)
450                 return NULL;
451
452         /*
453          *      The main EAP code & eaptls_verify() take care of
454          *      ensuring that the packet is OK, and that we can
455          *      extract the various fields we want.
456          *
457          *      e.g. a TLS packet with zero data is allowed as an ACK,
458          *      but we will never see it here, as we will simply
459          *      send another fragment, instead of trying to extract
460          *      the data.
461          *
462          *      MUST have TLS type octet, followed by flags, followed
463          *      by data.
464          */
465         assert(eap_ds->response->length > 2);
466
467         tlspacket = eaptls_alloc();
468         if (tlspacket == NULL) return NULL;
469
470         /*
471          *      Code & id for EAPTLS & EAP are same
472          *      but eaptls_length = eap_length - 1(EAP-Type = 1 octet)
473          *
474          *      length = code + id + length + type + tlsdata
475          *             =  1   +  1 +   2    +  1    +  X
476          */
477         tlspacket->code = eap_ds->response->code;
478         tlspacket->id = eap_ds->response->id;
479         tlspacket->length = eap_ds->response->length - 1; /* EAP type */
480         tlspacket->flags = eap_ds->response->type.data[0];
481
482         /*
483          *      A quick sanity check of the flags.  If we've been told
484          *      that there's a length, and there isn't one, then stop.
485          */
486         if (TLS_LENGTH_INCLUDED(tlspacket->flags) &&
487             (tlspacket->length < 5)) { /* flags + TLS message length */
488                 radlog(L_ERR, "rlm_eap_tls: Invalid EAP-TLS packet received.  (Length bit is set, but no length was found.)");
489                 eaptls_free(&tlspacket);
490                 return NULL;
491         }
492
493         /*
494          *      If the final TLS packet is larger than we can handle, die
495          *      now.
496          *
497          *      Likewise, if the EAP packet says N bytes, and the TLS
498          *      packet says there's fewer bytes, it's a problem.
499          *
500          *      FIXME: Try to ensure that the claimed length is
501          *      consistent across multiple TLS fragments.
502          */
503         if (TLS_LENGTH_INCLUDED(tlspacket->flags)) {
504                 memcpy(&data_len, &eap_ds->response->type.data[1], 4);
505                 data_len = ntohl(data_len);
506                 if (data_len > MAX_RECORD_SIZE) {
507                         radlog(L_ERR, "rlm_eap_tls: The EAP-TLS packet will contain more data than we can process.");
508                         eaptls_free(&tlspacket);
509                         return NULL;
510                 }
511
512 #if 0
513                 DEBUG2(" TLS: %d %d\n", data_len, tlspacket->length);
514
515                 if (data_len < tlspacket->length) {
516                         radlog(L_ERR, "rlm_eap_tls: EAP-TLS packet claims to be smaller than the encapsulating EAP packet.");
517                         eaptls_free(&tlspacket);
518                         return NULL;
519                 }
520 #endif
521         }
522
523         switch (status) {
524         /*
525          *      The TLS Message Length field is four octets, and
526          *      provides the total length of the TLS message or set of
527          *      messages that is being fragmented; this simplifies
528          *      buffer allocation.
529          *
530          *      Dynamic allocation of buffers as & when we know the
531          *      length should solve the problem.
532          */
533         case EAPTLS_FIRST_FRAGMENT:
534         case EAPTLS_LENGTH_INCLUDED:
535         case EAPTLS_MORE_FRAGMENTS_WITH_LENGTH:
536                 if (tlspacket->length < 5) { /* flags + TLS message length */
537                         radlog(L_ERR, "rlm_eap_tls: Invalid EAP-TLS packet received.  (Expected length, got none.)");
538                         eaptls_free(&tlspacket);
539                         return NULL;
540                 }
541
542                 /*
543                  *      Extract all the TLS fragments from the
544                  *      previous eap_ds Start appending this
545                  *      fragment to the above ds
546                  */
547                 memcpy(&data_len, &eap_ds->response->type.data[1], sizeof(uint32_t));
548                 data_len = ntohl(data_len);
549                 data = (eap_ds->response->type.data + 5/*flags+TLS-Length*/);
550                 len = eap_ds->response->type.length - 5/*flags+TLS-Length*/;
551
552                 /*
553                  *      Hmm... this should be an error, too.
554                  */
555                 if (data_len > len) {
556                         data_len = len;
557                 }
558                 break;
559
560                 /*
561                  *      Data length is implicit, from the EAP header.
562                  */
563         case EAPTLS_MORE_FRAGMENTS:
564         case EAPTLS_OK:
565                 data_len = eap_ds->response->type.length - 1/*flags*/;
566                 data = eap_ds->response->type.data + 1/*flags*/;
567                 break;
568
569         default:
570                 radlog(L_ERR, "rlm_eap_tls: Invalid EAP-TLS packet received");
571                 eaptls_free(&tlspacket);
572                 return NULL;
573         }
574
575         tlspacket->dlen = data_len;
576         if (data_len) {
577                 tlspacket->data = (unsigned char *)malloc(data_len);
578                 if (tlspacket->data == NULL) {
579                         radlog(L_ERR, "rlm_eap_tls: out of memory");
580                         eaptls_free(&tlspacket);
581                         return NULL;
582                 }
583                 memcpy(tlspacket->data, data, data_len);
584         }
585
586         return tlspacket;
587 }
588
589
590
591 /*
592  * To process the TLS,
593  *  INCOMING DATA:
594  *      1. EAP-TLS should get the compelete TLS data from the peer.
595  *      2. Store that data in a data structure with any other required info
596  *      3. Handle that data structure to the TLS module.
597  *      4. TLS module will perform its operations on the data and
598  *      handle back to EAP-TLS
599  *
600  *  OUTGOING DATA:
601  *      1. EAP-TLS if necessary will fragment it and send it to the
602  *      destination.
603  *
604  *      During EAP-TLS initialization, TLS Context object will be
605  *      initialized and stored.  For every new authentication
606  *      requests, TLS will open a new session object and that session
607  *      object should be maintained even after the session is
608  *      completed for session resumption. (Probably later as a feature
609  *      as we donot know who maintains these session objects ie,
610  *      SSL_CTX (internally) or TLS module(explicitly). If TLS module,
611  *      then how to let SSL API know about these sessions.)
612  */
613 static void eaptls_operation(EAPTLS_PACKET *eaptls_packet UNUSED,
614                              eaptls_status_t status, EAP_HANDLER *handler)
615 {
616         tls_session_t *tls_session;
617
618         tls_session = (tls_session_t *)handler->opaque;
619
620         if ((status == EAPTLS_MORE_FRAGMENTS) ||
621             (status == EAPTLS_MORE_FRAGMENTS_WITH_LENGTH) ||
622             (status == EAPTLS_FIRST_FRAGMENT)) {
623                 /*
624                  * Send the ACK.
625                  */
626                 eaptls_send_ack(handler->eap_ds, tls_session->peap_flag);
627         } else {
628                 /*
629                  *      We have the complete TLS-data or TLS-message.
630                  *
631                  *      Clean the dirty message.
632                  *
633                  *      Authenticate the user and send
634                  *      Success/Failure.
635                  *
636                  *      If more info
637                  *      is required then send another request.  */
638                 if (tls_handshake_recv(tls_session)) {
639                         /*
640                          *      FIXME: return success/fail.
641                          *
642                          *      TLS proper can decide what to do, then.
643                          */
644                         eaptls_request(handler->eap_ds, tls_session);
645                 } else {
646                         eaptls_fail(handler->eap_ds, tls_session->peap_flag);
647                 }
648         }
649         return;
650 }
651
652
653 /*
654  * In the actual authentication first verify the packet and then create the data structure
655  */
656 /*
657  * To process the TLS,
658  *  INCOMING DATA:
659  *      1. EAP-TLS should get the compelete TLS data from the peer.
660  *      2. Store that data in a data structure with any other required info
661  *      3. Hand this data structure to the TLS module.
662  *      4. TLS module will perform its operations on the data and hands back to EAP-TLS
663  *  OUTGOING DATA:
664  *      1. EAP-TLS if necessary will fragment it and send it to the destination.
665  *
666  *      During EAP-TLS initialization, TLS Context object will be
667  *      initialized and stored.  For every new authentication
668  *      requests, TLS will open a new session object and that
669  *      session object SHOULD be maintained even after the session
670  *      is completed, for session resumption. (Probably later as a
671  *      feature, as we do not know who maintains these session
672  *      objects ie, SSL_CTX (internally) or TLS module (explicitly). If
673  *      TLS module, then how to let SSL API know about these
674  *      sessions.)
675  */
676
677 /*
678  *      Process an EAP request
679  */
680 eaptls_status_t eaptls_process(EAP_HANDLER *handler)
681 {
682         tls_session_t *tls_session = (tls_session_t *) handler->opaque;
683         EAPTLS_PACKET   *tlspacket;
684         eaptls_status_t status;
685
686         DEBUG2("  rlm_eap_tls: processing TLS");
687
688         /* This case is when SSL generates Alert then we
689          * send that alert to the client and then send the EAP-Failure
690          */
691         status = eaptls_verify(handler);
692         DEBUG2("  eaptls_verify returned %d\n", status);
693
694         switch (status) {
695         default:
696         case EAPTLS_INVALID:
697         case EAPTLS_FAIL:
698
699                 /*
700                  *      Success means that we're done the initial
701                  *      handshake.  For TTLS, this means send stuff
702                  *      back to the client, and the client sends us
703                  *      more tunneled data.
704                  */
705         case EAPTLS_SUCCESS:
706                 return status;
707                 break;
708
709                 /*
710                  *      Normal TLS request, continue with the "get rest
711                  *      of fragments" phase.
712                  */
713         case EAPTLS_REQUEST:
714                 eaptls_request(handler->eap_ds, tls_session);
715                 return EAPTLS_HANDLED;
716                 break;
717
718                 /*
719                  *      The handshake is done, and we're in the "tunnel
720                  *      data" phase.
721                  */
722         case EAPTLS_OK:
723                 DEBUG2("  rlm_eap_tls: Done initial handshake");
724
725                 /*
726                  *      Get the rest of the fragments.
727                  */
728         case EAPTLS_FIRST_FRAGMENT:
729         case EAPTLS_MORE_FRAGMENTS:
730         case EAPTLS_LENGTH_INCLUDED:
731         case EAPTLS_MORE_FRAGMENTS_WITH_LENGTH:
732                 break;
733         }
734
735         /*
736          *      Extract the TLS packet from the buffer.
737          */
738         if ((tlspacket = eaptls_extract(handler->eap_ds, status)) == NULL)
739                 return EAPTLS_FAIL;
740
741         /*
742          *      Get the session struct from the handler
743          *
744          *      update the dirty_in buffer
745          *
746          *      NOTE: This buffer will contain partial data when M bit is set.
747          *
748          *      CAUTION while reinitializing this buffer, it should be
749          *      reinitialized only when this M bit is NOT set.
750          */
751         if (tlspacket->dlen !=
752             (tls_session->record_plus)(&tls_session->dirty_in, tlspacket->data, tlspacket->dlen)) {
753                 eaptls_free(&tlspacket);
754                 radlog(L_ERR, "rlm_eap_tls: Exceeded maximum record size");
755                 return EAPTLS_FAIL;
756         }
757
758         /*
759          *      SSL initalization is done.  Return.
760          *
761          *      The TLS data will be in the tls_session structure.
762          */
763         if (SSL_is_init_finished(tls_session->ssl)) {
764                 eaptls_free(&tlspacket);
765                 return EAPTLS_OK;
766         }
767
768         /*
769          *      Continue the handshake.
770          */
771         eaptls_operation(tlspacket, status, handler);
772
773         eaptls_free(&tlspacket);
774         return EAPTLS_HANDLED;
775 }
776
777
778 /*
779  *      compose the TLS reply packet in the EAP reply typedata
780  */
781 int eaptls_compose(EAP_DS *eap_ds, EAPTLS_PACKET *reply)
782 {
783         uint8_t *ptr;
784
785         /*
786          *      Don't set eap_ds->request->type.type, as the main EAP
787          *      handler will do that for us.  This allows the TLS
788          *      module to be called from TTLS & PEAP.
789          */
790
791         /*
792          *      When the EAP server receives an EAP-Response with the
793          *      M bit set, it MUST respond with an EAP-Request with
794          *      EAP-Type=EAP-TLS and no data. This serves as a
795          *      fragment ACK. The EAP peer MUST wait until it receives
796          *      the EAP-Request before sending another fragment.
797          *
798          *      In order to prevent errors in the processing of
799          *      fragments, the EAP server MUST use increment the
800          *      Identifier value for each fragment ACK contained
801          *      within an EAP-Request, and the peer MUST include this
802          *      Identifier value in the subsequent fragment contained
803          *      within an EAP- Reponse.
804          */
805         eap_ds->request->type.data = malloc(reply->length - TLS_HEADER_LEN + 1);
806         if (eap_ds->request->type.data == NULL) {
807                 radlog(L_ERR, "rlm_eap_tls: out of memory");
808                 return 0;
809         }
810
811         /* EAPTLS Header length is excluded while computing EAP typelen */
812         eap_ds->request->type.length = reply->length - TLS_HEADER_LEN;
813
814         ptr = eap_ds->request->type.data;
815         *ptr++ = (uint8_t)(reply->flags & 0xFF);
816
817         if (reply->dlen) memcpy(ptr, reply->data, reply->dlen);
818
819         switch (reply->code) {
820         case EAPTLS_ACK:
821         case EAPTLS_START:
822         case EAPTLS_REQUEST:
823                 eap_ds->request->code = PW_EAP_REQUEST;
824                 break;
825         case EAPTLS_SUCCESS:
826                 eap_ds->request->code = PW_EAP_SUCCESS;
827                 break;
828         case EAPTLS_FAIL:
829                 eap_ds->request->code = PW_EAP_FAILURE;
830                 break;
831         default:
832                 /* Should never enter here */
833                 eap_ds->request->code = PW_EAP_FAILURE;
834                 break;
835         }
836
837         return 1;
838 }