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
26 #include <freeradius-devel/ident.h>
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"
40 #ifdef HAVE_SYS_STAT_H
45 * Detach the EAP-TLS module.
47 static int eaptls_detach(void *arg)
49 rlm_eap_tls_t *inst = (rlm_eap_tls_t *) arg;
58 * Attach the EAP-TLS module.
60 static int eaptls_attach(CONF_SECTION *cs, void **instance)
65 * Parse the config file & get all the configured values
67 inst = rad_malloc(sizeof(*inst));
69 radlog(L_ERR, "rlm_eap_tls: out of memory");
72 memset(inst, 0, sizeof(*inst));
74 if (cf_section_parse(cs, inst, module_config) < 0) {
79 inst->tls_conf = eaptls_conf_parse(cs, "tls");
81 if (!inst->tls_conf) {
82 radlog(L_ERR, "rlm_eap_tls: Failed initializing SSL context");
94 * Send an initial eap-tls request to the peer, using the libeap functions.
96 static int eaptls_initiate(void *type_arg, EAP_HANDLER *handler)
101 REQUEST *request = handler->request;
106 handler->finished = FALSE;
109 * EAP-TLS always requires a client certificate.
111 ssn = eaptls_session(inst->tls_conf, handler, TRUE);
116 handler->opaque = ((void *)ssn);
117 handler->free_opaque = session_free;
120 * Set up type-specific information.
122 ssn->prf_label = "client EAP encryption";
125 * TLS session initialization is over. Now handle TLS
126 * related handshaking or application data.
128 status = eaptls_start(handler->eap_ds, ssn->peap_flag);
129 RDEBUG2("Start returned %d", status);
135 * The next stage to process the packet.
137 handler->stage = AUTHENTICATE;
143 * Do authentication, by letting EAP-TLS do most of the work.
145 static int eaptls_authenticate(void *type_arg, EAP_HANDLER *handler)
147 fr_tls_status_t status;
148 tls_session_t *tls_session = (tls_session_t *) handler->opaque;
149 REQUEST *request = handler->request;
154 RDEBUG2("Authenticate");
156 status = eaptls_process(handler);
157 RDEBUG2("eaptls_process returned %d\n", status);
160 * EAP-TLS handshake was successful, return an
161 * EAP-TLS-Success packet here.
163 * If a virtual server was configured, check that
164 * it accepts the certificates, too.
167 if (inst->virtual_server) {
171 /* create a fake request */
172 fake = request_alloc_fake(request);
173 rad_assert(fake->packet->vps == NULL);
175 fake->packet->vps = paircopy(request->packet->vps);
177 /* set the virtual server to use */
178 if ((vp = pairfind(request->config_items, PW_VIRTUAL_SERVER, 0, TAG_ANY)) != NULL) {
179 fake->server = vp->vp_strvalue;
181 fake->server = inst->virtual_server;
184 RDEBUG("Processing EAP-TLS Certificate check:");
185 debug_pair_list(fake->packet->vps);
187 RDEBUG("server %s {", fake->server);
189 rad_virtual_server(fake);
191 RDEBUG("} # server %s", fake->server);
193 /* copy the reply vps back to our reply */
194 pairadd(&request->reply->vps, fake->reply->vps);
195 fake->reply->vps = NULL;
197 /* reject if virtual server didn't return accept */
198 if (fake->reply->code != PW_AUTHENTICATION_ACK) {
199 RDEBUG2("Certifictes were rejected by the virtual server");
201 eaptls_fail(handler, 0);
211 * The TLS code is still working on the TLS
212 * exchange, and it's a valid TLS request.
219 * Handshake is done, proceed with decoding tunneled
223 RDEBUG2("Received unexpected tunneled data after successful handshake.");
225 if ((debug_flag > 2) && fr_log_fp) {
227 unsigned int data_len;
228 unsigned char buffer[1024];
230 data_len = (tls_session->record_minus)(&tls_session->dirty_in,
231 buffer, sizeof(buffer));
232 log_debug(" Tunneled data (%u bytes)\n", data_len);
233 for (i = 0; i < data_len; i++) {
234 if ((i & 0x0f) == 0x00) fprintf(fr_log_fp, " %x: ", i);
235 if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
237 fprintf(fr_log_fp, "%02x ", buffer[i]);
239 fprintf(fr_log_fp, "\n");
243 eaptls_fail(handler, 0);
248 * Anything else: fail.
250 * Also, remove the session from the cache so that
251 * the client can't re-use it.
254 tls_fail(tls_session);
260 * Success: Automatically return MPPE keys.
262 return eaptls_success(handler, 0);
266 * The module name should be the only globally exported symbol.
267 * That is, everything else should be 'static'.
269 EAP_TYPE rlm_eap_tls = {
271 eaptls_attach, /* attach */
272 eaptls_initiate, /* Start the initial request */
273 NULL, /* authorization */
274 eaptls_authenticate, /* authentication */
275 eaptls_detach /* detach */