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