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.
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.
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
20 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
21 * Copyright 2003 Alan DeKok <aland@freeradius.org>
22 * Copyright 2006 The FreeRADIUS server project
27 * TLS Packet Format in EAP
28 * --- ------ ------ -- ---
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 #include <freeradius-devel/ident.h>
44 #include <freeradius-devel/autoconf.h>
49 * Allocate a new TLS_PACKET
51 EAPTLS_PACKET *eaptls_alloc(void)
55 if ((rp = malloc(sizeof(EAPTLS_PACKET))) == NULL) {
56 radlog(L_ERR, "rlm_eap_tls: out of memory");
59 memset(rp, 0, sizeof(EAPTLS_PACKET));
66 void eaptls_free(EAPTLS_PACKET **eaptls_packet_ptr)
68 EAPTLS_PACKET *eaptls_packet;
70 if (!eaptls_packet_ptr) return;
71 eaptls_packet = *eaptls_packet_ptr;
72 if (eaptls_packet == NULL) return;
74 if (eaptls_packet->data) {
75 free(eaptls_packet->data);
76 eaptls_packet->data = NULL;
80 *eaptls_packet_ptr = NULL;
84 The S flag is set only within the EAP-TLS start message
85 sent from the EAP server to the peer.
87 int eaptls_start(EAP_DS *eap_ds, int peap_flag)
91 reply.code = EAPTLS_START;
92 reply.length = TLS_HEADER_LEN + 1/*flags*/;
94 reply.flags = peap_flag;
95 reply.flags = SET_START(reply.flags);
100 eaptls_compose(eap_ds, &reply);
105 int eaptls_success(EAP_HANDLER *handler, int peap_flag)
108 VALUE_PAIR *vp, *vps = NULL;
109 REQUEST *request = handler->request;
110 tls_session_t *tls_session = handler->opaque;
112 reply.code = EAPTLS_SUCCESS;
113 reply.length = TLS_HEADER_LEN;
114 reply.flags = peap_flag;
119 * If there's no session resumption, delete the entry
120 * from the cache. This means either it's disabled
121 * globally for this SSL context, OR we were told to
122 * disable it for this user.
124 * This also means you can't turn it on just for one
127 if ((!tls_session->allow_session_resumption) ||
128 (((vp = pairfind(request->config_items, 1127, 0)) != NULL) &&
129 (vp->vp_integer == 0))) {
130 SSL_CTX_remove_session(tls_session->ctx,
131 tls_session->ssl->session);
132 tls_session->allow_session_resumption = 0;
135 * If we're in a resumed session and it's
138 if (SSL_session_reused(tls_session->ssl)) {
139 RDEBUG("FAIL: Forcibly stopping session resumption as it is not allowed.");
140 return eaptls_fail(handler, peap_flag);
144 * Else resumption IS allowed, so we store the
145 * user data in the cache.
147 } else if (!SSL_session_reused(tls_session->ssl)) {
148 RDEBUG2("Saving response in the cache");
150 vp = paircopy2(request->reply->vps, PW_USER_NAME, 0);
153 vp = paircopy2(request->packet->vps, PW_STRIPPED_USER_NAME, 0);
157 SSL_SESSION_set_ex_data(tls_session->ssl->session,
158 eaptls_session_idx, vps);
160 RDEBUG2("WARNING: No information to cache: session caching will be disabled for this session.");
161 SSL_CTX_remove_session(tls_session->ctx,
162 tls_session->ssl->session);
166 * Else the session WAS allowed. Copy the cached
171 vp = SSL_SESSION_get_ex_data(tls_session->ssl->session,
174 RDEBUG("WARNING: No information in cached session!");
175 return eaptls_fail(handler, peap_flag);
177 RDEBUG("Adding cached attributes to the reply:");
179 pairadd(&request->reply->vps, paircopy(vp));
182 * Mark the request as resumed.
184 vp = pairmake("EAP-Session-Resumed", "1", T_OP_SET);
185 if (vp) pairadd(&request->packet->vps, vp);
190 * Call compose AFTER checking for cached data.
192 eaptls_compose(handler->eap_ds, &reply);
195 * Automatically generate MPPE keying material.
197 if (tls_session->prf_label) {
198 eaptls_gen_mppe_keys(&handler->request->reply->vps,
199 tls_session->ssl, tls_session->prf_label);
201 RDEBUG("WARNING: Not adding MPPE keys because there is no PRF label");
207 int eaptls_fail(EAP_HANDLER *handler, int peap_flag)
210 tls_session_t *tls_session = handler->opaque;
212 reply.code = EAPTLS_FAIL;
213 reply.length = TLS_HEADER_LEN;
214 reply.flags = peap_flag;
219 * Force the session to NOT be cached.
221 SSL_CTX_remove_session(tls_session->ctx, tls_session->ssl->session);
223 eaptls_compose(handler->eap_ds, &reply);
229 A single TLS record may be up to 16384 octets in length, but a TLS
230 message may span multiple TLS records, and a TLS certificate message
231 may in principle be as long as 16MB.
235 * Frame the Dirty data that needs to be send to the client in an
236 * EAP-Request. We always embed the TLS-length in all EAP-TLS
237 * packets that we send, for easy reference purpose. Handle
238 * fragmentation and sending the next fragment etc.
240 int eaptls_request(EAP_DS *eap_ds, tls_session_t *ssn)
245 unsigned int lbit = 0;
247 /* This value determines whether we set (L)ength flag for
248 EVERY packet we send and add corresponding
249 "TLS Message Length" field.
252 This means we include L flag and "TLS Msg Len" in EVERY
256 This means we include L flag and "TLS Msg Len" **ONLY**
257 in First packet of a fragment series. We do not use
260 Having L flag in every packet is prefered.
263 if (ssn->length_flag) {
266 if (ssn->fragment == 0) {
267 ssn->tls_msg_len = ssn->dirty_out.used;
270 reply.code = EAPTLS_REQUEST;
271 reply.flags = ssn->peap_flag;
273 /* Send data, NOT more than the FRAGMENT size */
274 if (ssn->dirty_out.used > ssn->offset) {
276 reply.flags = SET_MORE_FRAGMENTS(reply.flags);
277 /* Length MUST be included if it is the First Fragment */
278 if (ssn->fragment == 0) {
283 size = ssn->dirty_out.used;
287 reply.dlen = lbit + size;
288 reply.length = TLS_HEADER_LEN + 1/*flags*/ + reply.dlen;
290 reply.data = malloc(reply.dlen);
292 nlen = htonl(ssn->tls_msg_len);
293 memcpy(reply.data, &nlen, lbit);
294 reply.flags = SET_LENGTH_INCLUDED(reply.flags);
296 (ssn->record_minus)(&ssn->dirty_out, reply.data + lbit, size);
298 eaptls_compose(eap_ds, &reply);
306 * Acknowledge received is for one of the following messages sent earlier
307 * 1. Handshake completed Message, so now send, EAP-Success
308 * 2. Alert Message, now send, EAP-Failure
309 * 3. Fragment Message, now send, next Fragment
311 static eaptls_status_t eaptls_ack_handler(EAP_HANDLER *handler)
313 tls_session_t *tls_session;
314 REQUEST *request = handler->request;
316 tls_session = (tls_session_t *)handler->opaque;
317 if (tls_session == NULL){
318 radlog_request(L_ERR, 0, request, "FAIL: Unexpected ACK received. Could not obtain session information.");
321 if (tls_session->info.initialized == 0) {
322 RDEBUG("No SSL info available. Waiting for more SSL data.");
323 return EAPTLS_REQUEST;
325 if ((tls_session->info.content_type == handshake) &&
326 (tls_session->info.origin == 0)) {
327 radlog_request(L_ERR, 0, request, "FAIL: ACK without earlier message.");
331 switch (tls_session->info.content_type) {
333 RDEBUG2("ACK alert");
334 eaptls_fail(handler, tls_session->peap_flag);
338 if ((tls_session->info.handshake_type == finished) &&
339 (tls_session->dirty_out.used == 0)) {
340 RDEBUG2("ACK handshake is finished");
343 * From now on all the content is
344 * application data set it here as nobody else
347 tls_session->info.content_type = application_data;
348 return EAPTLS_SUCCESS;
349 } /* else more data to send */
351 RDEBUG2("ACK handshake fragment handler");
352 /* Fragmentation handler, send next fragment */
353 return EAPTLS_REQUEST;
355 case application_data:
356 RDEBUG2("ACK handshake fragment handler in application data");
357 return EAPTLS_REQUEST;
360 * For the rest of the conditions, switch over
361 * to the default section below.
364 RDEBUG2("ACK default");
365 radlog_request(L_ERR, 0, request, "Invalid ACK received: %d",
366 tls_session->info.content_type);
372 * Similarly, when the EAP server receives an EAP-Response with
373 * the M bit set, it MUST respond with an EAP-Request with
374 * EAP-Type=EAP-TLS and no data. This serves as a fragment ACK.
376 * In order to prevent errors in the processing of fragments, the
377 * EAP server MUST use increment the Identifier value for each
378 * fragment ACK contained within an EAP-Request, and the peer
379 * MUST include this Identifier value in the subsequent fragment
380 * contained within an EAP- Reponse.
382 * EAP server sends an ACK when it determines there are More
383 * fragments to receive to make the complete
384 * TLS-record/TLS-Message
386 static int eaptls_send_ack(EAP_DS *eap_ds, int peap_flag)
390 reply.code = EAPTLS_ACK;
391 reply.length = TLS_HEADER_LEN + 1/*flags*/;
392 reply.flags = peap_flag;
396 eaptls_compose(eap_ds, &reply);
402 * The S flag is set only within the EAP-TLS start message sent
403 * from the EAP server to the peer.
405 * Similarly, when the EAP server receives an EAP-Response with
406 * the M bit set, it MUST respond with an EAP-Request with
407 * EAP-Type=EAP-TLS and no data. This serves as a fragment
408 * ACK. The EAP peer MUST wait.
410 static eaptls_status_t eaptls_verify(EAP_HANDLER *handler)
412 EAP_DS *eap_ds = handler->eap_ds;
413 EAP_DS *prev_eap_ds = handler->prev_eapds;
414 eaptls_packet_t *eaptls_packet, *eaptls_prev = NULL;
415 REQUEST *request = handler->request;
418 * We don't check ANY of the input parameters. It's all
419 * code which works together, so if something is wrong,
420 * we SHOULD core dump.
422 * e.g. if eap_ds is NULL, of if eap_ds->response is
423 * NULL, of if it's NOT an EAP-Response, or if the packet
424 * is too short. See eap_validation()., in ../../eap.c
426 * Also, eaptype_select() takes care of selecting the
427 * appropriate type, so we don't need to check
428 * eap_ds->response->type.type == PW_EAP_TLS, or anything
431 eaptls_packet = (eaptls_packet_t *)eap_ds->response->type.data;
432 if (prev_eap_ds && prev_eap_ds->response)
433 eaptls_prev = (eaptls_packet_t *)prev_eap_ds->response->type.data;
438 * If there's no TLS data, or there's 1 byte of TLS data,
439 * with the flags set to zero, then it's an ACK.
441 * Find if this is a reply to the previous request sent
443 if ((eaptls_packet == NULL) ||
444 ((eap_ds->response->length == EAP_HEADER_LEN + 2) &&
445 ((eaptls_packet->flags & 0xc0) == 0x00))) {
449 * Un-comment this for TLS inside of TTLS/PEAP
451 RDEBUG2("Received EAP-TLS ACK message");
452 return eaptls_ack_handler(handler);
455 (prev_eap_ds->request->id == eap_ds->response->id)) {
457 * Run the ACK handler directly from here.
459 RDEBUG2("Received TLS ACK");
460 return eaptls_ack_handler(handler);
462 radlog_request(L_ERR, 0, request, "Received Invalid TLS ACK");
463 return EAPTLS_INVALID;
469 * We send TLS_START, but do not receive it.
471 if (TLS_START(eaptls_packet->flags)) {
472 RDEBUG("Received unexpected EAP-TLS Start message");
473 return EAPTLS_INVALID;
477 * The L bit (length included) is set to indicate the
478 * presence of the four octet TLS Message Length field,
479 * and MUST be set for the first fragment of a fragmented
480 * TLS message or set of messages.
482 * The M bit (more fragments) is set on all but the last
485 * The S bit (EAP-TLS start) is set in an EAP-TLS Start
486 * message. This differentiates the EAP-TLS Start message
487 * from a fragment acknowledgement.
489 if (TLS_LENGTH_INCLUDED(eaptls_packet->flags)) {
490 DEBUG2(" TLS Length %d",
491 eaptls_packet->data[2] * 256 | eaptls_packet->data[3]);
492 if (TLS_MORE_FRAGMENTS(eaptls_packet->flags)) {
494 * FIRST_FRAGMENT is identified
495 * 1. If there is no previous EAP-response received.
496 * 2. If EAP-response received, then its M bit not set.
497 * (It is because Last fragment will not have M bit set)
500 (prev_eap_ds->response == NULL) ||
501 (eaptls_prev == NULL) ||
502 !TLS_MORE_FRAGMENTS(eaptls_prev->flags)) {
504 RDEBUG2("Received EAP-TLS First Fragment of the message");
505 return EAPTLS_FIRST_FRAGMENT;
508 RDEBUG2("More Fragments with length included");
509 return EAPTLS_MORE_FRAGMENTS_WITH_LENGTH;
512 RDEBUG2("Length Included");
513 return EAPTLS_LENGTH_INCLUDED;
517 if (TLS_MORE_FRAGMENTS(eaptls_packet->flags)) {
518 RDEBUG2("More fragments to follow");
519 return EAPTLS_MORE_FRAGMENTS;
523 * None of the flags are set, but it's still a valid
533 * length = code + id + length + flags + tlsdata
534 * = 1 + 1 + 2 + 1 + X
535 * length = EAP-length - 1(EAP-Type = 1 octet)
536 * flags = EAP-typedata[0] (1 octet)
537 * dlen = EAP-typedata[1-4] (4 octets), if L flag set
538 * = length - 5(code+id+length+flags), otherwise
539 * data = EAP-typedata[5-n], if L flag set
540 * = EAP-typedata[1-n], otherwise
541 * packet = EAP-typedata (complete typedata)
543 * Points to consider during EAP-TLS data extraction
544 * 1. In the received packet, No data will be present incase of ACK-NAK
545 * 2. Incase if more fragments need to be received then ACK after retreiving this fragment.
547 * RFC 2716 Section 4.2. PPP EAP TLS Request Packet
550 * 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
551 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
552 * | Code | Identifier | Length |
553 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
554 * | Type | Flags | TLS Message Length
555 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
556 * | TLS Message Length | TLS Data...
557 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
559 * The Length field is two octets and indicates the length of the EAP
560 * packet including the Code, Identifir, Length, Type, and TLS data
563 static EAPTLS_PACKET *eaptls_extract(REQUEST *request, EAP_DS *eap_ds, eaptls_status_t status)
565 EAPTLS_PACKET *tlspacket;
566 uint32_t data_len = 0;
568 uint8_t *data = NULL;
570 if (status == EAPTLS_INVALID)
574 * The main EAP code & eaptls_verify() take care of
575 * ensuring that the packet is OK, and that we can
576 * extract the various fields we want.
578 * e.g. a TLS packet with zero data is allowed as an ACK,
579 * but we will never see it here, as we will simply
580 * send another fragment, instead of trying to extract
583 * MUST have TLS type octet, followed by flags, followed
586 assert(eap_ds->response->length > 2);
588 tlspacket = eaptls_alloc();
589 if (tlspacket == NULL) return NULL;
592 * Code & id for EAPTLS & EAP are same
593 * but eaptls_length = eap_length - 1(EAP-Type = 1 octet)
595 * length = code + id + length + type + tlsdata
596 * = 1 + 1 + 2 + 1 + X
598 tlspacket->code = eap_ds->response->code;
599 tlspacket->id = eap_ds->response->id;
600 tlspacket->length = eap_ds->response->length - 1; /* EAP type */
601 tlspacket->flags = eap_ds->response->type.data[0];
604 * A quick sanity check of the flags. If we've been told
605 * that there's a length, and there isn't one, then stop.
607 if (TLS_LENGTH_INCLUDED(tlspacket->flags) &&
608 (tlspacket->length < 5)) { /* flags + TLS message length */
609 RDEBUG("Invalid EAP-TLS packet received. (Length bit is set, but no length was found.)");
610 eaptls_free(&tlspacket);
615 * If the final TLS packet is larger than we can handle, die
618 * Likewise, if the EAP packet says N bytes, and the TLS
619 * packet says there's fewer bytes, it's a problem.
621 * FIXME: Try to ensure that the claimed length is
622 * consistent across multiple TLS fragments.
624 if (TLS_LENGTH_INCLUDED(tlspacket->flags)) {
625 memcpy(&data_len, &eap_ds->response->type.data[1], 4);
626 data_len = ntohl(data_len);
627 if (data_len > MAX_RECORD_SIZE) {
628 RDEBUG("The EAP-TLS packet will contain more data than we can process.");
629 eaptls_free(&tlspacket);
634 DEBUG2(" TLS: %d %d\n", data_len, tlspacket->length);
636 if (data_len < tlspacket->length) {
637 RDEBUG("EAP-TLS packet claims to be smaller than the encapsulating EAP packet.");
638 eaptls_free(&tlspacket);
646 * The TLS Message Length field is four octets, and
647 * provides the total length of the TLS message or set of
648 * messages that is being fragmented; this simplifies
651 * Dynamic allocation of buffers as & when we know the
652 * length should solve the problem.
654 case EAPTLS_FIRST_FRAGMENT:
655 case EAPTLS_LENGTH_INCLUDED:
656 case EAPTLS_MORE_FRAGMENTS_WITH_LENGTH:
657 if (tlspacket->length < 5) { /* flags + TLS message length */
658 RDEBUG("Invalid EAP-TLS packet received. (Expected length, got none.)");
659 eaptls_free(&tlspacket);
664 * Extract all the TLS fragments from the
665 * previous eap_ds Start appending this
666 * fragment to the above ds
668 memcpy(&data_len, &eap_ds->response->type.data[1], sizeof(uint32_t));
669 data_len = ntohl(data_len);
670 data = (eap_ds->response->type.data + 5/*flags+TLS-Length*/);
671 len = eap_ds->response->type.length - 5/*flags+TLS-Length*/;
674 * Hmm... this should be an error, too.
676 if (data_len > len) {
682 * Data length is implicit, from the EAP header.
684 case EAPTLS_MORE_FRAGMENTS:
686 data_len = eap_ds->response->type.length - 1/*flags*/;
687 data = eap_ds->response->type.data + 1/*flags*/;
691 RDEBUG("Invalid EAP-TLS packet received");
692 eaptls_free(&tlspacket);
696 tlspacket->dlen = data_len;
698 tlspacket->data = (unsigned char *)malloc(data_len);
699 if (tlspacket->data == NULL) {
700 RDEBUG("out of memory");
701 eaptls_free(&tlspacket);
704 memcpy(tlspacket->data, data, data_len);
713 * To process the TLS,
715 * 1. EAP-TLS should get the compelete TLS data from the peer.
716 * 2. Store that data in a data structure with any other required info
717 * 3. Handle that data structure to the TLS module.
718 * 4. TLS module will perform its operations on the data and
719 * handle back to EAP-TLS
722 * 1. EAP-TLS if necessary will fragment it and send it to the
725 * During EAP-TLS initialization, TLS Context object will be
726 * initialized and stored. For every new authentication
727 * requests, TLS will open a new session object and that session
728 * object should be maintained even after the session is
729 * completed for session resumption. (Probably later as a feature
730 * as we donot know who maintains these session objects ie,
731 * SSL_CTX (internally) or TLS module(explicitly). If TLS module,
732 * then how to let SSL API know about these sessions.)
734 static eaptls_status_t eaptls_operation(eaptls_status_t status,
735 EAP_HANDLER *handler)
737 tls_session_t *tls_session;
739 tls_session = (tls_session_t *)handler->opaque;
741 if ((status == EAPTLS_MORE_FRAGMENTS) ||
742 (status == EAPTLS_MORE_FRAGMENTS_WITH_LENGTH) ||
743 (status == EAPTLS_FIRST_FRAGMENT)) {
747 eaptls_send_ack(handler->eap_ds, tls_session->peap_flag);
748 return EAPTLS_HANDLED;
753 * We have the complete TLS-data or TLS-message.
755 * Clean the dirty message.
757 * Authenticate the user and send
761 * is required then send another request.
763 if (!tls_handshake_recv(tls_session)) {
764 DEBUG2("TLS receive handshake failed during operation");
765 eaptls_fail(handler, tls_session->peap_flag);
770 * FIXME: return success/fail.
772 * TLS proper can decide what to do, then.
774 if (tls_session->dirty_out.used > 0) {
775 eaptls_request(handler->eap_ds, tls_session);
776 return EAPTLS_HANDLED;
780 * If there is no data to send i.e
781 * dirty_out.used <=0 and if the SSL
782 * handshake is finished, then return a
786 if (SSL_is_init_finished(tls_session->ssl)) {
788 * Init is finished. The rest is
791 tls_session->info.content_type = application_data;
792 return EAPTLS_SUCCESS;
796 * Who knows what happened...
798 DEBUG2("TLS failed during operation");
804 * In the actual authentication first verify the packet and then create the data structure
807 * To process the TLS,
809 * 1. EAP-TLS should get the compelete TLS data from the peer.
810 * 2. Store that data in a data structure with any other required info
811 * 3. Hand this data structure to the TLS module.
812 * 4. TLS module will perform its operations on the data and hands back to EAP-TLS
814 * 1. EAP-TLS if necessary will fragment it and send it to the destination.
816 * During EAP-TLS initialization, TLS Context object will be
817 * initialized and stored. For every new authentication
818 * requests, TLS will open a new session object and that
819 * session object SHOULD be maintained even after the session
820 * is completed, for session resumption. (Probably later as a
821 * feature, as we do not know who maintains these session
822 * objects ie, SSL_CTX (internally) or TLS module (explicitly). If
823 * TLS module, then how to let SSL API know about these
828 * Process an EAP request
830 eaptls_status_t eaptls_process(EAP_HANDLER *handler)
832 tls_session_t *tls_session = (tls_session_t *) handler->opaque;
833 EAPTLS_PACKET *tlspacket;
834 eaptls_status_t status;
835 REQUEST *request = handler->request;
837 RDEBUG2("processing EAP-TLS");
839 /* This case is when SSL generates Alert then we
840 * send that alert to the client and then send the EAP-Failure
842 status = eaptls_verify(handler);
843 RDEBUG2("eaptls_verify returned %d\n", status);
851 * Success means that we're done the initial
852 * handshake. For TTLS, this means send stuff
853 * back to the client, and the client sends us
854 * more tunneled data.
861 * Normal TLS request, continue with the "get rest
862 * of fragments" phase.
865 eaptls_request(handler->eap_ds, tls_session);
866 return EAPTLS_HANDLED;
870 * The handshake is done, and we're in the "tunnel
874 RDEBUG2("Done initial handshake");
877 * Get the rest of the fragments.
879 case EAPTLS_FIRST_FRAGMENT:
880 case EAPTLS_MORE_FRAGMENTS:
881 case EAPTLS_LENGTH_INCLUDED:
882 case EAPTLS_MORE_FRAGMENTS_WITH_LENGTH:
887 * Extract the TLS packet from the buffer.
889 if ((tlspacket = eaptls_extract(request, handler->eap_ds, status)) == NULL)
893 * Get the session struct from the handler
895 * update the dirty_in buffer
897 * NOTE: This buffer will contain partial data when M bit is set.
899 * CAUTION while reinitializing this buffer, it should be
900 * reinitialized only when this M bit is NOT set.
902 if (tlspacket->dlen !=
903 (tls_session->record_plus)(&tls_session->dirty_in, tlspacket->data, tlspacket->dlen)) {
904 eaptls_free(&tlspacket);
905 RDEBUG("Exceeded maximum record size");
912 eaptls_free(&tlspacket);
915 * SSL initalization is done. Return.
917 * The TLS data will be in the tls_session structure.
919 if (SSL_is_init_finished(tls_session->ssl)) {
923 * The initialization may be finished, but if
924 * there more fragments coming, then send ACK,
925 * and get the caller to continue the
928 if ((status == EAPTLS_MORE_FRAGMENTS) ||
929 (status == EAPTLS_MORE_FRAGMENTS_WITH_LENGTH) ||
930 (status == EAPTLS_FIRST_FRAGMENT)) {
934 eaptls_send_ack(handler->eap_ds,
935 tls_session->peap_flag);
936 RDEBUG2("Init is done, but tunneled data is fragmented");
937 return EAPTLS_HANDLED;
941 * Decrypt the complete record.
943 BIO_write(tls_session->into_ssl, tls_session->dirty_in.data,
944 tls_session->dirty_in.used);
947 * Clear the dirty buffer now that we are done with it
948 * and init the clean_out buffer to store decrypted data
950 (tls_session->record_init)(&tls_session->dirty_in);
951 (tls_session->record_init)(&tls_session->clean_out);
954 * Read (and decrypt) the tunneled data from the
955 * SSL session, and put it into the decrypted
958 err = SSL_read(tls_session->ssl, tls_session->clean_out.data,
959 sizeof(tls_session->clean_out.data));
962 RDEBUG("SSL_read Error");
964 switch (SSL_get_error(tls_session->ssl, err)) {
965 case SSL_ERROR_WANT_READ:
966 case SSL_ERROR_WANT_WRITE:
967 RDEBUG("Error in fragmentation logic");
971 * FIXME: Call int_ssl_check?
979 RDEBUG("WARNING: No data inside of the tunnel.");
983 * Passed all checks, successfully decrypted data
985 tls_session->clean_out.used = err;
991 * Continue the handshake.
993 return eaptls_operation(status, handler);
998 * compose the TLS reply packet in the EAP reply typedata
1000 int eaptls_compose(EAP_DS *eap_ds, EAPTLS_PACKET *reply)
1005 * Don't set eap_ds->request->type.type, as the main EAP
1006 * handler will do that for us. This allows the TLS
1007 * module to be called from TTLS & PEAP.
1011 * When the EAP server receives an EAP-Response with the
1012 * M bit set, it MUST respond with an EAP-Request with
1013 * EAP-Type=EAP-TLS and no data. This serves as a
1014 * fragment ACK. The EAP peer MUST wait until it receives
1015 * the EAP-Request before sending another fragment.
1017 * In order to prevent errors in the processing of
1018 * fragments, the EAP server MUST use increment the
1019 * Identifier value for each fragment ACK contained
1020 * within an EAP-Request, and the peer MUST include this
1021 * Identifier value in the subsequent fragment contained
1022 * within an EAP- Reponse.
1024 eap_ds->request->type.data = malloc(reply->length - TLS_HEADER_LEN + 1);
1025 if (eap_ds->request->type.data == NULL) {
1026 radlog(L_ERR, "out of memory");
1030 /* EAPTLS Header length is excluded while computing EAP typelen */
1031 eap_ds->request->type.length = reply->length - TLS_HEADER_LEN;
1033 ptr = eap_ds->request->type.data;
1034 *ptr++ = (uint8_t)(reply->flags & 0xFF);
1036 if (reply->dlen) memcpy(ptr, reply->data, reply->dlen);
1038 switch (reply->code) {
1041 case EAPTLS_REQUEST:
1042 eap_ds->request->code = PW_EAP_REQUEST;
1044 case EAPTLS_SUCCESS:
1045 eap_ds->request->code = PW_EAP_SUCCESS;
1048 eap_ds->request->code = PW_EAP_FAILURE;
1051 /* Should never enter here */
1052 eap_ds->request->code = PW_EAP_FAILURE;