2 * rlm_eap_tls.c contains the interfaces that are called from eap
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 USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
29 #ifdef HAVE_OPENSSL_RAND_H
30 #include <openssl/rand.h>
33 #ifdef HAVE_OPENSSL_EVP_H
34 #include <openssl/evp.h>
37 #include "rlm_eap_tls.h"
39 #ifdef HAVE_SYS_STAT_H
43 static CONF_PARSER module_config[] = {
44 { "tls", PW_TYPE_STRING_PTR,
45 offsetof(rlm_eap_tls_t, tls_conf_name), NULL, NULL },
47 { "virtual_server", PW_TYPE_STRING_PTR,
48 offsetof(rlm_eap_tls_t, virtual_server), NULL, NULL },
50 { NULL, -1, 0, NULL, NULL } /* end the list */
55 * Attach the EAP-TLS module.
57 static int eaptls_attach(CONF_SECTION *cs, void **instance)
62 * Parse the config file & get all the configured values
64 *instance = inst = talloc_zero(cs, rlm_eap_tls_t);
67 if (cf_section_parse(cs, inst, module_config) < 0) {
71 inst->tls_conf = eaptls_conf_parse(cs, "tls");
73 if (!inst->tls_conf) {
74 ERROR("rlm_eap_tls: Failed initializing SSL context");
83 * Send an initial eap-tls request to the peer, using the libeap functions.
85 static int eaptls_initiate(void *type_arg, eap_handler_t *handler)
90 REQUEST *request = handler->request;
95 handler->finished = false;
98 * EAP-TLS always requires a client certificate.
100 ssn = eaptls_session(inst->tls_conf, handler, true);
105 handler->opaque = ((void *)ssn);
106 handler->free_opaque = session_free;
109 * Set up type-specific information.
111 ssn->prf_label = "client EAP encryption";
114 * TLS session initialization is over. Now handle TLS
115 * related handshaking or application data.
117 status = eaptls_start(handler->eap_ds, ssn->peap_flag);
118 RDEBUG2("Start returned %d", status);
124 * The next stage to process the packet.
126 handler->stage = AUTHENTICATE;
132 * Do authentication, by letting EAP-TLS do most of the work.
134 static int mod_authenticate(void *type_arg, eap_handler_t *handler)
136 fr_tls_status_t status;
137 tls_session_t *tls_session = (tls_session_t *) handler->opaque;
138 REQUEST *request = handler->request;
143 RDEBUG2("Authenticate");
145 status = eaptls_process(handler);
146 RDEBUG2("eaptls_process returned %d\n", status);
149 * EAP-TLS handshake was successful, return an
150 * EAP-TLS-Success packet here.
152 * If a virtual server was configured, check that
153 * it accepts the certificates, too.
156 if (inst->virtual_server) {
160 /* create a fake request */
161 fake = request_alloc_fake(request);
162 rad_assert(!fake->packet->vps);
164 fake->packet->vps = paircopy(fake->packet, request->packet->vps);
166 /* set the virtual server to use */
167 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
168 fake->server = vp->vp_strvalue;
170 fake->server = inst->virtual_server;
173 RDEBUG("Processing EAP-TLS Certificate check:");
174 debug_pair_list(fake->packet->vps);
176 RDEBUG("server %s {", fake->server);
178 rad_virtual_server(fake);
180 RDEBUG("} # server %s", fake->server);
182 /* copy the reply vps back to our reply */
183 pairfilter(request->reply, &request->reply->vps,
184 &fake->reply->vps, 0, 0, TAG_ANY);
186 /* reject if virtual server didn't return accept */
187 if (fake->reply->code != PW_AUTHENTICATION_ACK) {
188 RDEBUG2("Certificates were rejected by the virtual server");
190 eaptls_fail(handler, 0);
200 * The TLS code is still working on the TLS
201 * exchange, and it's a valid TLS request.
208 * Handshake is done, proceed with decoding tunneled
212 RDEBUG2("Received unexpected tunneled data after successful handshake.");
214 if ((debug_flag > 2) && fr_log_fp) {
216 unsigned int data_len;
217 unsigned char buffer[1024];
219 data_len = (tls_session->record_minus)(&tls_session->dirty_in,
220 buffer, sizeof(buffer));
221 DEBUG(" Tunneled data (%u bytes)", data_len);
222 for (i = 0; i < data_len; i++) {
223 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, " %x: ", i);
224 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
226 fprintf(fr_log_fp, "%02x ", buffer[i]);
228 fprintf(fr_log_fp, "\n");
232 eaptls_fail(handler, 0);
237 * Anything else: fail.
239 * Also, remove the session from the cache so that
240 * the client can't re-use it.
243 tls_fail(tls_session);
249 * Success: Automatically return MPPE keys.
251 return eaptls_success(handler, 0);
255 * The module name should be the only globally exported symbol.
256 * That is, everything else should be 'static'.
258 rlm_eap_module_t rlm_eap_tls = {
260 eaptls_attach, /* attach */
261 eaptls_initiate, /* Start the initial request */
262 NULL, /* authorization */
263 mod_authenticate, /* authentication */