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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2001 hereUare Communications, Inc. <raghud@hereuare.com>
21 * Copyright 2003 Alan DeKok <aland@freeradius.org>
27 #ifdef HAVE_OPENSSL_RAND_H
28 #include <openssl/rand.h>
31 static CONF_PARSER module_config[] = {
32 { "rsa_key_exchange", PW_TYPE_BOOLEAN,
33 offsetof(EAP_TLS_CONF, rsa_key), NULL, "no" },
34 { "dh_key_exchange", PW_TYPE_BOOLEAN,
35 offsetof(EAP_TLS_CONF, dh_key), NULL, "yes" },
36 { "rsa_key_length", PW_TYPE_INTEGER,
37 offsetof(EAP_TLS_CONF, rsa_key_length), NULL, "512" },
38 { "dh_key_length", PW_TYPE_INTEGER,
39 offsetof(EAP_TLS_CONF, dh_key_length), NULL, "512" },
40 { "verify_depth", PW_TYPE_INTEGER,
41 offsetof(EAP_TLS_CONF, verify_depth), NULL, "0" },
42 { "CA_path", PW_TYPE_STRING_PTR,
43 offsetof(EAP_TLS_CONF, ca_path), NULL, NULL },
44 { "pem_file_type", PW_TYPE_BOOLEAN,
45 offsetof(EAP_TLS_CONF, file_type), NULL, "yes" },
46 { "private_key_file", PW_TYPE_STRING_PTR,
47 offsetof(EAP_TLS_CONF, private_key_file), NULL, NULL },
48 { "certificate_file", PW_TYPE_STRING_PTR,
49 offsetof(EAP_TLS_CONF, certificate_file), NULL, NULL },
50 { "CA_file", PW_TYPE_STRING_PTR,
51 offsetof(EAP_TLS_CONF, ca_file), NULL, NULL },
52 { "private_key_password", PW_TYPE_STRING_PTR,
53 offsetof(EAP_TLS_CONF, private_key_password), NULL, NULL },
54 { "dh_file", PW_TYPE_STRING_PTR,
55 offsetof(EAP_TLS_CONF, dh_file), NULL, NULL },
56 { "random_file", PW_TYPE_STRING_PTR,
57 offsetof(EAP_TLS_CONF, random_file), NULL, NULL },
58 { "fragment_size", PW_TYPE_INTEGER,
59 offsetof(EAP_TLS_CONF, fragment_size), NULL, "1024" },
60 { "include_length", PW_TYPE_BOOLEAN,
61 offsetof(EAP_TLS_CONF, include_length), NULL, "yes" },
63 { NULL, -1, 0, NULL, NULL } /* end the list */
68 * TODO: Check for the type of key exchange * like conf->dh_key
70 static int load_dh_params(SSL_CTX *ctx, char *file)
75 if ((bio = BIO_new_file(file, "r")) == NULL) {
76 radlog(L_ERR, "rlm_eap_tls: Unable to open DH file - %s", file);
80 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
82 if (SSL_CTX_set_tmp_dh(ctx, dh) < 0) {
83 radlog(L_ERR, "rlm_eap_tls: Unable to set DH parameters");
93 * Generte ephemeral RSA keys.
95 static int generate_eph_rsa_key(SSL_CTX *ctx)
99 rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
101 if (!SSL_CTX_set_tmp_rsa(ctx, rsa)) {
102 radlog(L_ERR, "rlm_eap_tls: Couldn't set RSA key");
112 * Create Global context SSL and use it in every new session
114 * - Load the trusted CAs
115 * - Load the Private key & the certificate
116 * - Set the Context options & Verify options
118 static SSL_CTX *init_tls_ctx(EAP_TLS_CONF *conf)
127 * Add all the default ciphers and message digests
128 * Create our context.
131 SSL_load_error_strings();
133 meth = TLSv1_method();
134 ctx = SSL_CTX_new(meth);
137 * Identify the type of certificates that needs to be loaded
139 if (conf->file_type) {
140 type = SSL_FILETYPE_PEM;
142 type = SSL_FILETYPE_ASN1;
145 /* Load the CAs we trust */
146 if (!(SSL_CTX_load_verify_locations(ctx, conf->ca_file, conf->ca_path)) ||
147 (!SSL_CTX_set_default_verify_paths(ctx))) {
148 ERR_print_errors_fp(stderr);
149 radlog(L_ERR, "rlm_eap_tls: Error reading Trusted root CA list");
152 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(conf->ca_file));
155 * Set the password to load private key
157 if (conf->private_key_password) {
158 SSL_CTX_set_default_passwd_cb_userdata(ctx, conf->private_key_password);
159 SSL_CTX_set_default_passwd_cb(ctx, cbtls_password);
162 /* Load our keys and certificates*/
163 if (!(SSL_CTX_use_certificate_file(ctx, conf->certificate_file, type))) {
164 ERR_print_errors_fp(stderr);
165 radlog(L_ERR, "rlm_eap_tls: Error reading certificate file");
169 if (!(SSL_CTX_use_PrivateKey_file(ctx, conf->private_key_file, type))) {
170 ERR_print_errors_fp(stderr);
171 radlog(L_ERR, "rlm_eap_tls: Error reading private key file");
176 * Check if the loaded private key is the right one
178 if (!SSL_CTX_check_private_key(ctx)) {
179 radlog(L_ERR, "rlm_eap_tls: Private key does not match the certificate public key");
186 ctx_options |= SSL_OP_NO_SSLv2;
187 ctx_options |= SSL_OP_NO_SSLv3;
190 * SSL_OP_SINGLE_DH_USE must be used in order to prevent
191 * small subgroup attacks and forward secrecy. Always
194 * SSL_OP_SINGLE_DH_USE has an impact on the computer
195 * time needed during negotiation, but it is not very
198 ctx_options |= SSL_OP_SINGLE_DH_USE;
199 SSL_CTX_set_options(ctx, ctx_options);
202 * TODO: Set the RSA & DH
203 * SSL_CTX_set_tmp_rsa_callback(ctx, cbtls_rsa);
204 * SSL_CTX_set_tmp_dh_callback(ctx, cbtls_dh);
208 * set the message callback to identify the type of
209 * message. For every new session, there can be a
210 * different callback argument.
212 * SSL_CTX_set_msg_callback(ctx, cbtls_msg);
215 /* Set Info callback */
216 SSL_CTX_set_info_callback(ctx, cbtls_info);
220 * Always verify the peer certificate
222 verify_mode |= SSL_VERIFY_PEER;
223 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
224 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
225 SSL_CTX_set_verify(ctx, verify_mode, cbtls_verify);
227 if (conf->verify_depth) {
228 SSL_CTX_set_verify_depth(ctx, conf->verify_depth);
231 /* Load randomness */
232 if (!(RAND_load_file(conf->random_file, 1024*1024))) {
233 ERR_print_errors_fp(stderr);
234 radlog(L_ERR, "rlm_eap_tls: Error loading randomness");
243 * Detach the EAP-TLS module.
245 static int eaptls_detach(void *arg)
250 inst = (eap_tls_t *) arg;
254 if (conf->dh_file) free(conf->dh_file);
255 conf->dh_file = NULL;
256 if (conf->certificate_file) free(conf->certificate_file);
257 conf->certificate_file = NULL;
258 if (conf->private_key_file) free(conf->private_key_file);
259 conf->private_key_file = NULL;
260 if (conf->private_key_password) free(conf->private_key_password);
261 conf->private_key_password = NULL;
262 if (conf->random_file) free(conf->random_file);
263 conf->random_file = NULL;
269 if (inst->ctx) SSL_CTX_free(inst->ctx);
279 * Attach the EAP-TLS module.
281 static int eaptls_attach(CONF_SECTION *cs, void **instance)
286 /* Store all these values in the data structure for later references */
287 inst = (eap_tls_t *)malloc(sizeof(*inst));
289 radlog(L_ERR, "rlm_eap_tls: out of memory");
292 memset(inst, 0, sizeof(*inst));
295 * Parse the config file & get all the configured values
297 conf = (EAP_TLS_CONF *)malloc(sizeof(*conf));
299 radlog(L_ERR, "rlm_eap_tls: out of memory");
302 memset(conf, 0, sizeof(*conf));
305 if (cf_section_parse(cs, conf, module_config) < 0) {
314 inst->ctx = init_tls_ctx(conf);
315 if (inst->ctx == NULL) {
320 if (load_dh_params(inst->ctx, conf->dh_file) < 0) {
324 if (generate_eph_rsa_key(inst->ctx) < 0) {
336 * Send an initial eap-tls request to the peer.
338 * Frame eap reply packet.
339 * len = header + type + tls_typedata
340 * tls_typedata = flags(Start (S) bit set, and no data)
342 * Once having received the peer's Identity, the EAP server MUST
343 * respond with an EAP-TLS/Start packet, which is an
344 * EAP-Request packet with EAP-Type=EAP-TLS, the Start (S) bit
345 * set, and no data. The EAP-TLS conversation will then begin,
346 * with the peer sending an EAP-Response packet with
347 * EAP-Type = EAP-TLS. The data field of that packet will
350 * Fragment length is Framed-MTU - 4.
352 * http://mail.frascone.com/pipermail/public/eap/2003-July/001426.html
354 static int eaptls_initiate(void *type_arg, EAP_HANDLER *handler)
360 int client_cert = TRUE;
362 inst = (eap_tls_t *)type_arg;
365 * If we're TTLS or PEAP, then do NOT require a client
368 * FIXME: This should be more configurable.
370 if (handler->eap_type != PW_EAP_TLS) {
371 vp = pairfind(handler->request->config_items,
372 PW_EAP_TLS_REQUIRE_CLIENT_CERT);
376 client_cert = vp->lvalue;
381 * Every new session is started only from EAP-TLS-START.
382 * Before Sending EAP-TLS-START, open a new SSL session.
383 * Create all the required data structures & store them
384 * in Opaque. So that we can use these data structures
385 * when we get the response
387 ssn = eaptls_new_session(inst->ctx, client_cert);
393 * Create a structure for all the items required to be
394 * verified for each client and set that as opaque data
397 * NOTE: If we want to set each item sepearately then
398 * this index should be global.
400 SSL_set_ex_data(ssn->ssl, 0, (void *)handler->identity);
402 ssn->length_flag = inst->conf->include_length;
405 * We set a default fragment size, unless the Framed-MTU
406 * tells us it's too big.
408 ssn->offset = inst->conf->fragment_size;
409 vp = pairfind(handler->request->packet->vps, PW_FRAMED_MTU);
410 if (vp && ((vp->lvalue - 4) < ssn->offset)) {
411 ssn->offset = vp->lvalue - 4;
414 handler->opaque = ((void *)ssn);
415 handler->free_opaque = session_free;
417 DEBUG2(" rlm_eap_tls: Initiate");
420 * PEAP-specific breakage.
422 if (handler->eap_type == PW_EAP_PEAP) {
423 ssn->peap_flag = 0x02;
427 * TLS session initialization is over. *Now handle TLS
428 * related handshaking or application data.
430 status = eaptls_start(handler->eap_ds, ssn->peap_flag);
431 DEBUG2(" rlm_eap_tls: Start returned %d", status);
436 * The next stage to process the packet.
438 handler->stage = AUTHENTICATE;
444 * Do authentication, by letting EAP-TLS do most of the work.
446 static int eaptls_authenticate(void *arg, EAP_HANDLER *handler)
448 eaptls_status_t status;
449 tls_session_t *tls_session = (tls_session_t *) handler->opaque;
451 DEBUG2(" rlm_eap_tls: Authenticate");
453 status = eaptls_process(handler);
454 DEBUG2(" eaptls_process returned %d\n", status);
457 * EAP-TLS handshake was successful, return an
458 * EAP-TLS-Success packet here.
464 * The TLS code is still working on the TLS
465 * exchange, and it's a valid TLS request.
472 * Handshake is done, proceed with decoding tunneled
476 DEBUG2(" rlm_eap_tls: Received unexpected tunneled data after successful handshake.");
477 eaptls_fail(handler->eap_ds, 0);
482 * Anything else: fail.
489 * Success: Return MPPE keys.
491 eaptls_success(handler->eap_ds, 0);
492 eaptls_gen_mppe_keys(&handler->request->reply->vps,
494 "client EAP encryption");
499 * The module name should be the only globally exported symbol.
500 * That is, everything else should be 'static'.
502 EAP_TYPE rlm_eap_tls = {
504 eaptls_attach, /* attach */
505 eaptls_initiate, /* Start the initial request */
506 NULL, /* authorization */
507 eaptls_authenticate, /* authentication */
508 eaptls_detach /* detach */