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 #include <freeradius-devel/autoconf.h>
31 #ifdef HAVE_OPENSSL_RAND_H
32 #include <openssl/rand.h>
35 #include "rlm_eap_tls.h"
37 #ifdef HAVE_SYS_STAT_H
41 static CONF_PARSER module_config[] = {
42 { "rsa_key_exchange", PW_TYPE_BOOLEAN,
43 offsetof(EAP_TLS_CONF, rsa_key), NULL, "no" },
44 { "dh_key_exchange", PW_TYPE_BOOLEAN,
45 offsetof(EAP_TLS_CONF, dh_key), NULL, "yes" },
46 { "rsa_key_length", PW_TYPE_INTEGER,
47 offsetof(EAP_TLS_CONF, rsa_key_length), NULL, "512" },
48 { "dh_key_length", PW_TYPE_INTEGER,
49 offsetof(EAP_TLS_CONF, dh_key_length), NULL, "512" },
50 { "verify_depth", PW_TYPE_INTEGER,
51 offsetof(EAP_TLS_CONF, verify_depth), NULL, "0" },
52 { "CA_path", PW_TYPE_FILENAME,
53 offsetof(EAP_TLS_CONF, ca_path), NULL, NULL },
54 { "pem_file_type", PW_TYPE_BOOLEAN,
55 offsetof(EAP_TLS_CONF, file_type), NULL, "yes" },
56 { "private_key_file", PW_TYPE_FILENAME,
57 offsetof(EAP_TLS_CONF, private_key_file), NULL, NULL },
58 { "certificate_file", PW_TYPE_FILENAME,
59 offsetof(EAP_TLS_CONF, certificate_file), NULL, NULL },
60 { "CA_file", PW_TYPE_FILENAME,
61 offsetof(EAP_TLS_CONF, ca_file), NULL, NULL },
62 { "private_key_password", PW_TYPE_STRING_PTR,
63 offsetof(EAP_TLS_CONF, private_key_password), NULL, NULL },
64 { "dh_file", PW_TYPE_STRING_PTR,
65 offsetof(EAP_TLS_CONF, dh_file), NULL, NULL },
66 { "random_file", PW_TYPE_STRING_PTR,
67 offsetof(EAP_TLS_CONF, random_file), NULL, NULL },
68 { "fragment_size", PW_TYPE_INTEGER,
69 offsetof(EAP_TLS_CONF, fragment_size), NULL, "1024" },
70 { "include_length", PW_TYPE_BOOLEAN,
71 offsetof(EAP_TLS_CONF, include_length), NULL, "yes" },
72 { "check_crl", PW_TYPE_BOOLEAN,
73 offsetof(EAP_TLS_CONF, check_crl), NULL, "no"},
74 { "check_cert_cn", PW_TYPE_STRING_PTR,
75 offsetof(EAP_TLS_CONF, check_cert_cn), NULL, NULL},
76 { "cipher_list", PW_TYPE_STRING_PTR,
77 offsetof(EAP_TLS_CONF, cipher_list), NULL, NULL},
78 { "check_cert_issuer", PW_TYPE_STRING_PTR,
79 offsetof(EAP_TLS_CONF, check_cert_issuer), NULL, NULL},
80 { "make_cert_command", PW_TYPE_STRING_PTR,
81 offsetof(EAP_TLS_CONF, make_cert_command), NULL, NULL},
83 { NULL, -1, 0, NULL, NULL } /* end the list */
88 * TODO: Check for the type of key exchange * like conf->dh_key
90 static int load_dh_params(SSL_CTX *ctx, char *file)
95 if ((bio = BIO_new_file(file, "r")) == NULL) {
96 radlog(L_ERR, "rlm_eap_tls: Unable to open DH file - %s", file);
100 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
103 radlog(L_INFO, "WARNING: rlm_eap_tls: Unable to set DH parameters. DH cipher suites may not work!");
104 DEBUG2("WARNING: Fix this by running the OpenSSL command listed in eap.conf");
108 if (SSL_CTX_set_tmp_dh(ctx, dh) < 0) {
109 radlog(L_ERR, "rlm_eap_tls: Unable to set DH parameters");
119 * Before trusting a certificate, you must make sure that the
120 * certificate is 'valid'. There are several steps that your
121 * application can take in determining if a certificate is
122 * valid. Commonly used steps are:
124 * 1.Verifying the certificate's signature, and verifying that
125 * the certificate has been issued by a trusted Certificate
128 * 2.Verifying that the certificate is valid for the present date
129 * (i.e. it is being presented within its validity dates).
131 * 3.Verifying that the certificate has not been revoked by its
132 * issuing Certificate Authority, by checking with respect to a
133 * Certificate Revocation List (CRL).
135 * 4.Verifying that the credentials presented by the certificate
136 * fulfill additional requirements specific to the application,
137 * such as with respect to access control lists or with respect
138 * to OCSP (Online Certificate Status Processing).
140 * NOTE: This callback will be called multiple times based on the
141 * depth of the root certificate chain
143 static int cbtls_verify(int ok, X509_STORE_CTX *ctx)
145 char subject[1024]; /* Used for the subject name */
146 char issuer[1024]; /* Used for the issuer name */
147 char common_name[1024];
149 EAP_HANDLER *handler = NULL;
156 client_cert = X509_STORE_CTX_get_current_cert(ctx);
157 err = X509_STORE_CTX_get_error(ctx);
158 depth = X509_STORE_CTX_get_error_depth(ctx);
161 radlog(L_ERR,"--> verify error:num=%d:%s\n",err,
162 X509_verify_cert_error_string(err));
167 * Retrieve the pointer to the SSL of the connection currently treated
168 * and the application specific data stored into the SSL object.
170 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
171 handler = (EAP_HANDLER *)SSL_get_ex_data(ssl, 0);
172 conf = (EAP_TLS_CONF *)SSL_get_ex_data(ssl, 1);
175 * Get the Subject & Issuer
177 subject[0] = issuer[0] = '\0';
178 X509_NAME_oneline(X509_get_subject_name(client_cert), subject,
180 X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer,
183 subject[sizeof(subject) - 1] = '\0';
184 issuer[sizeof(issuer) - 1] = '\0';
187 * Get the Common Name
189 X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
190 NID_commonName, common_name, sizeof(common_name));
191 common_name[sizeof(common_name) - 1] = '\0';
193 switch (ctx->error) {
195 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
196 radlog(L_ERR, "issuer= %s\n", issuer);
198 case X509_V_ERR_CERT_NOT_YET_VALID:
199 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
200 radlog(L_ERR, "notBefore=");
202 ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
205 case X509_V_ERR_CERT_HAS_EXPIRED:
206 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
207 radlog(L_ERR, "notAfter=");
209 ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
215 * If we're at the actual client cert, apply additional
220 * If the conf tells us to, check cert issuer
221 * against the specified value and fail
222 * verification if they don't match.
224 if (conf->check_cert_issuer &&
225 (strcmp(issuer, conf->check_cert_issuer) != 0)) {
226 radlog(L_AUTH, "rlm_eap_tls: Certificate issuer (%s) does not match specified value (%s)!", issuer, conf->check_cert_issuer);
231 * If the conf tells us to, check the CN in the
232 * cert against xlat'ed value, but only if the
233 * previous checks passed.
235 if (my_ok && conf->check_cert_cn) {
236 if (!radius_xlat(cn_str, sizeof(cn_str), conf->check_cert_cn, handler->request, NULL)) {
237 radlog(L_ERR, "rlm_eap_tls (%s): xlat failed.",
238 conf->check_cert_cn);
239 /* if this fails, fail the verification */
242 DEBUG2(" rlm_eap_tls: checking certificate CN (%s) with xlat'ed value (%s)", common_name, cn_str);
243 if (strcmp(cn_str, common_name) != 0) {
244 radlog(L_AUTH, "rlm_eap_tls: Certificate CN (%s) does not match specified value (%s)!", common_name, cn_str);
248 } /* check_cert_cn */
251 if (debug_flag > 0) {
252 radlog(L_INFO, "chain-depth=%d, ", depth);
253 radlog(L_INFO, "error=%d", err);
255 radlog(L_INFO, "--> User-Name = %s", handler->identity);
256 radlog(L_INFO, "--> BUF-Name = %s", common_name);
257 radlog(L_INFO, "--> subject = %s", subject);
258 radlog(L_INFO, "--> issuer = %s", issuer);
259 radlog(L_INFO, "--> verify return:%d", my_ok);
266 * Create Global context SSL and use it in every new session
268 * - Load the trusted CAs
269 * - Load the Private key & the certificate
270 * - Set the Context options & Verify options
272 static SSL_CTX *init_tls_ctx(EAP_TLS_CONF *conf)
276 X509_STORE *certstore;
277 int verify_mode = SSL_VERIFY_NONE;
282 * Add all the default ciphers and message digests
283 * Create our context.
286 SSL_load_error_strings();
288 meth = TLSv1_method();
289 ctx = SSL_CTX_new(meth);
292 * Identify the type of certificates that needs to be loaded
294 if (conf->file_type) {
295 type = SSL_FILETYPE_PEM;
297 type = SSL_FILETYPE_ASN1;
301 * Set the password to load private key
303 if (conf->private_key_password) {
304 SSL_CTX_set_default_passwd_cb_userdata(ctx, conf->private_key_password);
305 SSL_CTX_set_default_passwd_cb(ctx, cbtls_password);
309 * Load our keys and certificates
311 * If certificates are of type PEM then we can make use
312 * of cert chain authentication using openssl api call
313 * SSL_CTX_use_certificate_chain_file. Please see how
314 * the cert chain needs to be given in PEM from
317 if (type == SSL_FILETYPE_PEM) {
318 radlog(L_INFO, "rlm_eap_tls: Loading the certificate file as a chain");
319 if (!(SSL_CTX_use_certificate_chain_file(ctx, conf->certificate_file))) {
320 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
321 radlog(L_ERR, "rlm_eap_tls: Error reading certificate file %s", conf->certificate_file);
325 } else if (!(SSL_CTX_use_certificate_file(ctx, conf->certificate_file, type))) {
326 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
327 radlog(L_ERR, "rlm_eap_tls: Error reading certificate file %s", conf->certificate_file);
331 /* Load the CAs we trust */
332 if (!SSL_CTX_load_verify_locations(ctx, conf->ca_file, conf->ca_path)) {
333 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
334 radlog(L_ERR, "rlm_eap_tls: Error reading Trusted root CA list %s",conf->ca_file );
337 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(conf->ca_file));
338 if (!(SSL_CTX_use_PrivateKey_file(ctx, conf->private_key_file, type))) {
339 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
340 radlog(L_ERR, "rlm_eap_tls: Error reading private key file %s", conf->private_key_file);
345 * Check if the loaded private key is the right one
347 if (!SSL_CTX_check_private_key(ctx)) {
348 radlog(L_ERR, "rlm_eap_tls: Private key does not match the certificate public key");
355 ctx_options |= SSL_OP_NO_SSLv2;
356 ctx_options |= SSL_OP_NO_SSLv3;
359 * SSL_OP_SINGLE_DH_USE must be used in order to prevent
360 * small subgroup attacks and forward secrecy. Always
363 * SSL_OP_SINGLE_DH_USE has an impact on the computer
364 * time needed during negotiation, but it is not very
367 ctx_options |= SSL_OP_SINGLE_DH_USE;
370 * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS to work around issues
371 * in Windows Vista client.
372 * http://www.openssl.org/~bodo/tls-cbc.txt
373 * http://www.nabble.com/(RADIATOR)-Radiator-Version-3.16-released-t2600070.html
375 ctx_options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
377 SSL_CTX_set_options(ctx, ctx_options);
380 * TODO: Set the RSA & DH
381 * SSL_CTX_set_tmp_rsa_callback(ctx, cbtls_rsa);
382 * SSL_CTX_set_tmp_dh_callback(ctx, cbtls_dh);
386 * set the message callback to identify the type of
387 * message. For every new session, there can be a
388 * different callback argument.
390 * SSL_CTX_set_msg_callback(ctx, cbtls_msg);
393 /* Set Info callback */
394 SSL_CTX_set_info_callback(ctx, cbtls_info);
397 * Check the certificates for revocation.
399 #ifdef X509_V_FLAG_CRL_CHECK
400 if (conf->check_crl) {
401 certstore = SSL_CTX_get_cert_store(ctx);
402 if (certstore == NULL) {
403 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
404 radlog(L_ERR, "rlm_eap_tls: Error reading Certificate Store");
407 X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK);
413 * Always verify the peer certificate
415 verify_mode |= SSL_VERIFY_PEER;
416 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
417 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
418 SSL_CTX_set_verify(ctx, verify_mode, cbtls_verify);
420 if (conf->verify_depth) {
421 SSL_CTX_set_verify_depth(ctx, conf->verify_depth);
424 /* Load randomness */
425 if (!(RAND_load_file(conf->random_file, 1024*1024))) {
426 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
427 radlog(L_ERR, "rlm_eap_tls: Error loading randomness");
432 * Set the cipher list if we were told to
434 if (conf->cipher_list) {
435 if (!SSL_CTX_set_cipher_list(ctx, conf->cipher_list)) {
436 radlog(L_ERR, "rlm_eap_tls: Error setting cipher list");
446 * Detach the EAP-TLS module.
448 static int eaptls_detach(void *arg)
453 inst = (eap_tls_t *) arg;
457 memset(conf, 0, sizeof(*conf));
462 if (inst->ctx) SSL_CTX_free(inst->ctx);
472 * Attach the EAP-TLS module.
474 static int eaptls_attach(CONF_SECTION *cs, void **instance)
479 /* Store all these values in the data structure for later references */
480 inst = (eap_tls_t *)malloc(sizeof(*inst));
482 radlog(L_ERR, "rlm_eap_tls: out of memory");
485 memset(inst, 0, sizeof(*inst));
488 * Parse the config file & get all the configured values
490 conf = (EAP_TLS_CONF *)malloc(sizeof(*conf));
493 radlog(L_ERR, "rlm_eap_tls: out of memory");
496 memset(conf, 0, sizeof(*conf));
499 if (cf_section_parse(cs, conf, module_config) < 0) {
505 * The EAP RFC's say 1020, but we're less picky.
507 if (conf->fragment_size < 100) {
508 radlog(L_ERR, "rlm_eap_tls: Fragment size is too small.");
514 * The maximum size for a RADIUS packet is 4096,
515 * minus the header (20), Message-Authenticator (18),
516 * and State (18), etc. results in about 4000 bytes of data
517 * that can be devoted *solely* to EAP.
519 if (conf->fragment_size > 4000) {
520 radlog(L_ERR, "rlm_eap_tls: Fragment size is too large.");
526 * Account for the EAP header (4), and the EAP-TLS header
527 * (6), as per Section 4.2 of RFC 2716. What's left is
528 * the maximum amount of data we read from a TLS buffer.
530 conf->fragment_size -= 10;
533 * This magic makes the administrators life HUGELY easier
534 * on initial deployments.
536 * If the server starts up in debugging mode, AND the
537 * bootstrap command is configured, AND it exists, AND
538 * there is no server certificate
540 if (conf->make_cert_command && (debug_flag >= 2)) {
543 if ((stat(conf->make_cert_command, &buf) == 0) &&
544 (stat(conf->certificate_file, &buf) < 0) &&
546 (radius_exec_program(conf->make_cert_command, NULL, 1,
547 NULL, 0, NULL, NULL, 0) != 0)) {
557 inst->ctx = init_tls_ctx(conf);
558 if (inst->ctx == NULL) {
563 if (load_dh_params(inst->ctx, conf->dh_file) < 0) {
575 * Send an initial eap-tls request to the peer.
577 * Frame eap reply packet.
578 * len = header + type + tls_typedata
579 * tls_typedata = flags(Start (S) bit set, and no data)
581 * Once having received the peer's Identity, the EAP server MUST
582 * respond with an EAP-TLS/Start packet, which is an
583 * EAP-Request packet with EAP-Type=EAP-TLS, the Start (S) bit
584 * set, and no data. The EAP-TLS conversation will then begin,
585 * with the peer sending an EAP-Response packet with
586 * EAP-Type = EAP-TLS. The data field of that packet will
589 * Fragment length is Framed-MTU - 4.
591 * http://mail.frascone.com/pipermail/public/eap/2003-July/001426.html
593 static int eaptls_initiate(void *type_arg, EAP_HANDLER *handler)
599 int client_cert = TRUE;
602 inst = (eap_tls_t *)type_arg;
605 * If we're TTLS or PEAP, then do NOT require a client
608 * FIXME: This should be more configurable.
610 if (handler->eap_type != PW_EAP_TLS) {
611 vp = pairfind(handler->request->config_items,
612 PW_EAP_TLS_REQUIRE_CLIENT_CERT);
616 client_cert = vp->vp_integer;
621 * Every new session is started only from EAP-TLS-START.
622 * Before Sending EAP-TLS-START, open a new SSL session.
623 * Create all the required data structures & store them
624 * in Opaque. So that we can use these data structures
625 * when we get the response
627 ssn = eaptls_new_session(inst->ctx, client_cert);
633 * Verify the peer certificate, if asked.
636 DEBUG2(" rlm_eap_tls: Requiring client certificate");
637 verify_mode = SSL_VERIFY_PEER;
638 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
639 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
641 SSL_set_verify(ssn->ssl, verify_mode, cbtls_verify);
644 * Create a structure for all the items required to be
645 * verified for each client and set that as opaque data
648 * NOTE: If we want to set each item sepearately then
649 * this index should be global.
651 SSL_set_ex_data(ssn->ssl, 0, (void *)handler);
652 SSL_set_ex_data(ssn->ssl, 1, (void *)inst->conf);
654 ssn->length_flag = inst->conf->include_length;
657 * We use default fragment size, unless the Framed-MTU
658 * tells us it's too big. Note that we do NOT account
659 * for the EAP-TLS headers if conf->fragment_size is
660 * large, because that config item looks to be confusing.
662 * i.e. it should REALLY be called MTU, and the code here
663 * should figure out what that means for TLS fragment size.
664 * asking the administrator to know the internal details
665 * of EAP-TLS in order to calculate fragment sizes is
668 ssn->offset = inst->conf->fragment_size;
669 vp = pairfind(handler->request->packet->vps, PW_FRAMED_MTU);
670 if (vp && ((vp->vp_integer - 14) < ssn->offset)) {
672 * Discount the Framed-MTU by:
674 * 4 : EAP header (code + id + length)
675 * 1 : EAP type == EAP-TLS
677 * 4 : EAP-TLS Message length
678 * (even if conf->include_length == 0,
683 ssn->offset = vp->vp_integer - 14;
686 handler->opaque = ((void *)ssn);
687 handler->free_opaque = session_free;
689 DEBUG2(" rlm_eap_tls: Initiate");
692 * PEAP-specific breakage.
694 if (handler->eap_type == PW_EAP_PEAP) {
696 * As it is a poorly designed protocol, PEAP uses
697 * bits in the TLS header to indicate PEAP
698 * version numbers. For now, we only support
699 * PEAP version 0, so it doesn't matter too much.
700 * However, if we support later versions of PEAP,
701 * we will need this flag to indicate which
702 * version we're currently dealing with.
704 ssn->peap_flag = 0x00;
707 * PEAP version 0 requires 'include_length = no',
708 * so rather than hoping the user figures it out,
711 ssn->length_flag = 0;
715 * TLS session initialization is over. Now handle TLS
716 * related handshaking or application data.
718 status = eaptls_start(handler->eap_ds, ssn->peap_flag);
719 DEBUG2(" rlm_eap_tls: Start returned %d", status);
724 * The next stage to process the packet.
726 handler->stage = AUTHENTICATE;
732 * Do authentication, by letting EAP-TLS do most of the work.
734 static int eaptls_authenticate(void *arg UNUSED, EAP_HANDLER *handler)
736 eaptls_status_t status;
737 tls_session_t *tls_session = (tls_session_t *) handler->opaque;
739 DEBUG2(" rlm_eap_tls: Authenticate");
741 status = eaptls_process(handler);
742 DEBUG2(" eaptls_process returned %d\n", status);
745 * EAP-TLS handshake was successful, return an
746 * EAP-TLS-Success packet here.
752 * The TLS code is still working on the TLS
753 * exchange, and it's a valid TLS request.
760 * Handshake is done, proceed with decoding tunneled
764 DEBUG2(" rlm_eap_tls: Received unexpected tunneled data after successful handshake.");
766 if (debug_flag > 2) {
768 unsigned int data_len;
769 unsigned char buffer[1024];
771 data_len = (tls_session->record_minus)(&tls_session->dirty_in,
772 buffer, sizeof(buffer));
773 log_debug(" Tunneled data (%u bytes)\n", data_len);
774 for (i = 0; i < data_len; i++) {
775 if ((i & 0x0f) == 0x00) printf(" %x: ", i);
776 if ((i & 0x0f) == 0x0f) printf("\n");
778 printf("%02x ", buffer[i]);
784 eaptls_fail(handler->eap_ds, 0);
789 * Anything else: fail.
796 * Success: Return MPPE keys.
798 eaptls_success(handler->eap_ds, 0);
799 eaptls_gen_mppe_keys(&handler->request->reply->vps,
801 "client EAP encryption");
806 * The module name should be the only globally exported symbol.
807 * That is, everything else should be 'static'.
809 EAP_TYPE rlm_eap_tls = {
811 eaptls_attach, /* attach */
812 eaptls_initiate, /* Start the initial request */
813 NULL, /* authorization */
814 eaptls_authenticate, /* authentication */
815 eaptls_detach /* detach */