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 static CONF_PARSER module_config[] = {
38 { "rsa_key_exchange", PW_TYPE_BOOLEAN,
39 offsetof(EAP_TLS_CONF, rsa_key), NULL, "no" },
40 { "dh_key_exchange", PW_TYPE_BOOLEAN,
41 offsetof(EAP_TLS_CONF, dh_key), NULL, "yes" },
42 { "rsa_key_length", PW_TYPE_INTEGER,
43 offsetof(EAP_TLS_CONF, rsa_key_length), NULL, "512" },
44 { "dh_key_length", PW_TYPE_INTEGER,
45 offsetof(EAP_TLS_CONF, dh_key_length), NULL, "512" },
46 { "verify_depth", PW_TYPE_INTEGER,
47 offsetof(EAP_TLS_CONF, verify_depth), NULL, "0" },
48 { "CA_path", PW_TYPE_FILENAME,
49 offsetof(EAP_TLS_CONF, ca_path), NULL, NULL },
50 { "pem_file_type", PW_TYPE_BOOLEAN,
51 offsetof(EAP_TLS_CONF, file_type), NULL, "yes" },
52 { "private_key_file", PW_TYPE_FILENAME,
53 offsetof(EAP_TLS_CONF, private_key_file), NULL, NULL },
54 { "certificate_file", PW_TYPE_FILENAME,
55 offsetof(EAP_TLS_CONF, certificate_file), NULL, NULL },
56 { "CA_file", PW_TYPE_FILENAME,
57 offsetof(EAP_TLS_CONF, ca_file), NULL, NULL },
58 { "private_key_password", PW_TYPE_STRING_PTR,
59 offsetof(EAP_TLS_CONF, private_key_password), NULL, NULL },
60 { "dh_file", PW_TYPE_STRING_PTR,
61 offsetof(EAP_TLS_CONF, dh_file), NULL, NULL },
62 { "random_file", PW_TYPE_STRING_PTR,
63 offsetof(EAP_TLS_CONF, random_file), NULL, NULL },
64 { "fragment_size", PW_TYPE_INTEGER,
65 offsetof(EAP_TLS_CONF, fragment_size), NULL, "1024" },
66 { "include_length", PW_TYPE_BOOLEAN,
67 offsetof(EAP_TLS_CONF, include_length), NULL, "yes" },
68 { "check_crl", PW_TYPE_BOOLEAN,
69 offsetof(EAP_TLS_CONF, check_crl), NULL, "no"},
70 { "check_cert_cn", PW_TYPE_STRING_PTR,
71 offsetof(EAP_TLS_CONF, check_cert_cn), NULL, NULL},
72 { "cipher_list", PW_TYPE_STRING_PTR,
73 offsetof(EAP_TLS_CONF, cipher_list), NULL, NULL},
74 { "check_cert_issuer", PW_TYPE_STRING_PTR,
75 offsetof(EAP_TLS_CONF, check_cert_issuer), NULL, NULL},
77 { NULL, -1, 0, NULL, NULL } /* end the list */
82 * TODO: Check for the type of key exchange * like conf->dh_key
84 static int load_dh_params(SSL_CTX *ctx, char *file)
89 if ((bio = BIO_new_file(file, "r")) == NULL) {
90 radlog(L_ERR, "rlm_eap_tls: Unable to open DH file - %s", file);
94 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
96 if (SSL_CTX_set_tmp_dh(ctx, dh) < 0) {
97 radlog(L_ERR, "rlm_eap_tls: Unable to set DH parameters");
107 * Generte ephemeral RSA keys.
109 static int generate_eph_rsa_key(SSL_CTX *ctx)
113 rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
115 if (!SSL_CTX_set_tmp_rsa(ctx, rsa)) {
116 radlog(L_ERR, "rlm_eap_tls: Couldn't set RSA key");
126 * Before trusting a certificate, you must make sure that the
127 * certificate is 'valid'. There are several steps that your
128 * application can take in determining if a certificate is
129 * valid. Commonly used steps are:
131 * 1.Verifying the certificate's signature, and verifying that
132 * the certificate has been issued by a trusted Certificate
135 * 2.Verifying that the certificate is valid for the present date
136 * (i.e. it is being presented within its validity dates).
138 * 3.Verifying that the certificate has not been revoked by its
139 * issuing Certificate Authority, by checking with respect to a
140 * Certificate Revocation List (CRL).
142 * 4.Verifying that the credentials presented by the certificate
143 * fulfill additional requirements specific to the application,
144 * such as with respect to access control lists or with respect
145 * to OCSP (Online Certificate Status Processing).
147 * NOTE: This callback will be called multiple times based on the
148 * depth of the root certificate chain
150 static int cbtls_verify(int ok, X509_STORE_CTX *ctx)
152 char subject[1024]; /* Used for the subject name */
153 char issuer[1024]; /* Used for the issuer name */
154 char common_name[1024];
156 EAP_HANDLER *handler = NULL;
163 client_cert = X509_STORE_CTX_get_current_cert(ctx);
164 err = X509_STORE_CTX_get_error(ctx);
165 depth = X509_STORE_CTX_get_error_depth(ctx);
168 radlog(L_ERR,"--> verify error:num=%d:%s\n",err,
169 X509_verify_cert_error_string(err));
174 * Retrieve the pointer to the SSL of the connection currently treated
175 * and the application specific data stored into the SSL object.
177 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
178 handler = (EAP_HANDLER *)SSL_get_ex_data(ssl, 0);
179 conf = (EAP_TLS_CONF *)SSL_get_ex_data(ssl, 1);
182 * Get the Subject & Issuer
184 subject[0] = issuer[0] = '\0';
185 X509_NAME_oneline(X509_get_subject_name(client_cert), subject,
187 X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer,
190 subject[sizeof(subject) - 1] = '\0';
191 issuer[sizeof(issuer) - 1] = '\0';
194 * Get the Common Name
196 X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
197 NID_commonName, common_name, sizeof(common_name));
198 common_name[sizeof(common_name) - 1] = '\0';
200 switch (ctx->error) {
202 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
203 radlog(L_ERR, "issuer= %s\n", issuer);
205 case X509_V_ERR_CERT_NOT_YET_VALID:
206 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
207 radlog(L_ERR, "notBefore=");
209 ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
212 case X509_V_ERR_CERT_HAS_EXPIRED:
213 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
214 radlog(L_ERR, "notAfter=");
216 ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
222 * If we're at the actual client cert, apply additional
227 * If the conf tells us to, check cert issuer
228 * against the specified value and fail
229 * verification if they don't match.
231 if (conf->check_cert_issuer &&
232 (strcmp(issuer, conf->check_cert_issuer) != 0)) {
233 radlog(L_AUTH, "rlm_eap_tls: Certificate issuer (%s) does not match specified value (%s)!", issuer, conf->check_cert_issuer);
238 * If the conf tells us to, check the CN in the
239 * cert against xlat'ed value, but only if the
240 * previous checks passed.
242 if (my_ok && conf->check_cert_cn) {
243 if (!radius_xlat(cn_str, sizeof(cn_str), conf->check_cert_cn, handler->request, NULL)) {
244 radlog(L_ERR, "rlm_eap_tls (%s): xlat failed.",
245 conf->check_cert_cn);
246 /* if this fails, fail the verification */
249 DEBUG2(" rlm_eap_tls: checking certificate CN (%s) with xlat'ed value (%s)", common_name, cn_str);
250 if (strcmp(cn_str, common_name) != 0) {
251 radlog(L_AUTH, "rlm_eap_tls: Certificate CN (%s) does not match specified value (%s)!", common_name, cn_str);
255 } /* check_cert_cn */
258 if (debug_flag > 0) {
259 radlog(L_INFO, "chain-depth=%d, ", depth);
260 radlog(L_INFO, "error=%d", err);
262 radlog(L_INFO, "--> User-Name = %s", handler->identity);
263 radlog(L_INFO, "--> BUF-Name = %s", common_name);
264 radlog(L_INFO, "--> subject = %s", subject);
265 radlog(L_INFO, "--> issuer = %s", issuer);
266 radlog(L_INFO, "--> verify return:%d", my_ok);
273 * Create Global context SSL and use it in every new session
275 * - Load the trusted CAs
276 * - Load the Private key & the certificate
277 * - Set the Context options & Verify options
279 static SSL_CTX *init_tls_ctx(EAP_TLS_CONF *conf)
283 X509_STORE *certstore;
284 int verify_mode = SSL_VERIFY_NONE;
289 * Add all the default ciphers and message digests
290 * Create our context.
293 SSL_load_error_strings();
295 meth = TLSv1_method();
296 ctx = SSL_CTX_new(meth);
299 * Identify the type of certificates that needs to be loaded
301 if (conf->file_type) {
302 type = SSL_FILETYPE_PEM;
304 type = SSL_FILETYPE_ASN1;
308 * Set the password to load private key
310 if (conf->private_key_password) {
311 SSL_CTX_set_default_passwd_cb_userdata(ctx, conf->private_key_password);
312 SSL_CTX_set_default_passwd_cb(ctx, cbtls_password);
316 * Load our keys and certificates
318 * If certificates are of type PEM then we can make use
319 * of cert chain authentication using openssl api call
320 * SSL_CTX_use_certificate_chain_file. Please see how
321 * the cert chain needs to be given in PEM from
324 if (type == SSL_FILETYPE_PEM) {
325 radlog(L_INFO, "rlm_eap_tls: Loading the certificate file as a chain");
326 if (!(SSL_CTX_use_certificate_chain_file(ctx, conf->certificate_file))) {
327 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
328 radlog(L_ERR, "rlm_eap_tls: Error reading certificate file");
332 } else if (!(SSL_CTX_use_certificate_file(ctx, conf->certificate_file, type))) {
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 certificate file");
338 /* Load the CAs we trust */
339 if (!SSL_CTX_load_verify_locations(ctx, conf->ca_file, conf->ca_path)) {
340 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
341 radlog(L_ERR, "rlm_eap_tls: Error reading Trusted root CA list");
344 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(conf->ca_file));
345 if (!(SSL_CTX_use_PrivateKey_file(ctx, conf->private_key_file, type))) {
346 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
347 radlog(L_ERR, "rlm_eap_tls: Error reading private key file");
352 * Check if the loaded private key is the right one
354 if (!SSL_CTX_check_private_key(ctx)) {
355 radlog(L_ERR, "rlm_eap_tls: Private key does not match the certificate public key");
362 ctx_options |= SSL_OP_NO_SSLv2;
363 ctx_options |= SSL_OP_NO_SSLv3;
366 * SSL_OP_SINGLE_DH_USE must be used in order to prevent
367 * small subgroup attacks and forward secrecy. Always
370 * SSL_OP_SINGLE_DH_USE has an impact on the computer
371 * time needed during negotiation, but it is not very
374 ctx_options |= SSL_OP_SINGLE_DH_USE;
375 SSL_CTX_set_options(ctx, ctx_options);
378 * TODO: Set the RSA & DH
379 * SSL_CTX_set_tmp_rsa_callback(ctx, cbtls_rsa);
380 * SSL_CTX_set_tmp_dh_callback(ctx, cbtls_dh);
384 * set the message callback to identify the type of
385 * message. For every new session, there can be a
386 * different callback argument.
388 * SSL_CTX_set_msg_callback(ctx, cbtls_msg);
391 /* Set Info callback */
392 SSL_CTX_set_info_callback(ctx, cbtls_info);
395 * Check the certificates for revocation.
397 #ifdef X509_V_FLAG_CRL_CHECK
398 if (conf->check_crl) {
399 certstore = SSL_CTX_get_cert_store(ctx);
400 if (certstore == NULL) {
401 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
402 radlog(L_ERR, "rlm_eap_tls: Error reading Certificate Store");
405 X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK);
411 * Always verify the peer certificate
413 verify_mode |= SSL_VERIFY_PEER;
414 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
415 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
416 SSL_CTX_set_verify(ctx, verify_mode, cbtls_verify);
418 if (conf->verify_depth) {
419 SSL_CTX_set_verify_depth(ctx, conf->verify_depth);
422 /* Load randomness */
423 if (!(RAND_load_file(conf->random_file, 1024*1024))) {
424 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
425 radlog(L_ERR, "rlm_eap_tls: Error loading randomness");
430 * Set the cipher list if we were told to
432 if (conf->cipher_list) {
433 if (!SSL_CTX_set_cipher_list(ctx, conf->cipher_list)) {
434 radlog(L_ERR, "rlm_eap_tls: Error setting cipher list");
444 * Detach the EAP-TLS module.
446 static int eaptls_detach(void *arg)
451 inst = (eap_tls_t *) arg;
457 free(conf->certificate_file);
458 free(conf->private_key_file);
459 free(conf->private_key_password);
461 free(conf->random_file);
463 free(conf->check_cert_cn);
464 free(conf->cipher_list);
465 free(conf->check_cert_issuer);
467 memset(conf, 0, sizeof(*conf));
472 if (inst->ctx) SSL_CTX_free(inst->ctx);
482 * Attach the EAP-TLS module.
484 static int eaptls_attach(CONF_SECTION *cs, void **instance)
489 /* Store all these values in the data structure for later references */
490 inst = (eap_tls_t *)malloc(sizeof(*inst));
492 radlog(L_ERR, "rlm_eap_tls: out of memory");
495 memset(inst, 0, sizeof(*inst));
498 * Parse the config file & get all the configured values
500 conf = (EAP_TLS_CONF *)malloc(sizeof(*conf));
502 radlog(L_ERR, "rlm_eap_tls: out of memory");
505 memset(conf, 0, sizeof(*conf));
508 if (cf_section_parse(cs, conf, module_config) < 0) {
517 inst->ctx = init_tls_ctx(conf);
518 if (inst->ctx == NULL) {
523 if (load_dh_params(inst->ctx, conf->dh_file) < 0) {
527 if (generate_eph_rsa_key(inst->ctx) < 0) {
539 * Send an initial eap-tls request to the peer.
541 * Frame eap reply packet.
542 * len = header + type + tls_typedata
543 * tls_typedata = flags(Start (S) bit set, and no data)
545 * Once having received the peer's Identity, the EAP server MUST
546 * respond with an EAP-TLS/Start packet, which is an
547 * EAP-Request packet with EAP-Type=EAP-TLS, the Start (S) bit
548 * set, and no data. The EAP-TLS conversation will then begin,
549 * with the peer sending an EAP-Response packet with
550 * EAP-Type = EAP-TLS. The data field of that packet will
553 * Fragment length is Framed-MTU - 4.
555 * http://mail.frascone.com/pipermail/public/eap/2003-July/001426.html
557 static int eaptls_initiate(void *type_arg, EAP_HANDLER *handler)
563 int client_cert = TRUE;
566 inst = (eap_tls_t *)type_arg;
569 * If we're TTLS or PEAP, then do NOT require a client
572 * FIXME: This should be more configurable.
574 if (handler->eap_type != PW_EAP_TLS) {
575 vp = pairfind(handler->request->config_items,
576 PW_EAP_TLS_REQUIRE_CLIENT_CERT);
580 client_cert = vp->lvalue;
585 * Every new session is started only from EAP-TLS-START.
586 * Before Sending EAP-TLS-START, open a new SSL session.
587 * Create all the required data structures & store them
588 * in Opaque. So that we can use these data structures
589 * when we get the response
591 ssn = eaptls_new_session(inst->ctx, client_cert);
597 * Verify the peer certificate, if asked.
600 DEBUG2(" rlm_eap_tls: Requiring client certificate");
601 verify_mode = SSL_VERIFY_PEER;
602 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
603 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
605 SSL_set_verify(ssn->ssl, verify_mode, cbtls_verify);
608 * Create a structure for all the items required to be
609 * verified for each client and set that as opaque data
612 * NOTE: If we want to set each item sepearately then
613 * this index should be global.
615 SSL_set_ex_data(ssn->ssl, 0, (void *)handler);
616 SSL_set_ex_data(ssn->ssl, 1, (void *)inst->conf);
618 ssn->length_flag = inst->conf->include_length;
621 * We use default fragment size, unless the Framed-MTU
622 * tells us it's too big. Note that we do NOT account
623 * for the EAP-TLS headers if conf->fragment_size is
624 * large, because that config item looks to be confusing.
626 * i.e. it should REALLY be called MTU, and the code here
627 * should figure out what that means for TLS fragment size.
628 * asking the administrator to know the internal details
629 * of EAP-TLS in order to calculate fragment sizes is
632 ssn->offset = inst->conf->fragment_size;
633 vp = pairfind(handler->request->packet->vps, PW_FRAMED_MTU);
634 if (vp && ((vp->lvalue - 14) < ssn->offset)) {
636 * Discount the Framed-MTU by:
638 * 4 : EAP header (code + id + length)
639 * 1 : EAP type == EAP-TLS
641 * 4 : EAP-TLS Message length
642 * (even if conf->include_length == 0,
647 ssn->offset = vp->lvalue - 14;
650 handler->opaque = ((void *)ssn);
651 handler->free_opaque = session_free;
653 DEBUG2(" rlm_eap_tls: Initiate");
656 * PEAP-specific breakage.
658 if (handler->eap_type == PW_EAP_PEAP) {
660 * As it is a poorly designed protocol, PEAP uses
661 * bits in the TLS header to indicate PEAP
662 * version numbers. For now, we only support
663 * PEAP version 0, so it doesn't matter too much.
664 * However, if we support later versions of PEAP,
665 * we will need this flag to indicate which
666 * version we're currently dealing with.
668 ssn->peap_flag = 0x00;
671 * PEAP version 0 requires 'include_length = no',
672 * so rather than hoping the user figures it out,
675 ssn->length_flag = 0;
679 * TLS session initialization is over. Now handle TLS
680 * related handshaking or application data.
682 status = eaptls_start(handler->eap_ds, ssn->peap_flag);
683 DEBUG2(" rlm_eap_tls: Start returned %d", status);
688 * The next stage to process the packet.
690 handler->stage = AUTHENTICATE;
696 * Do authentication, by letting EAP-TLS do most of the work.
698 static int eaptls_authenticate(void *arg UNUSED, EAP_HANDLER *handler)
700 eaptls_status_t status;
701 tls_session_t *tls_session = (tls_session_t *) handler->opaque;
703 DEBUG2(" rlm_eap_tls: Authenticate");
705 status = eaptls_process(handler);
706 DEBUG2(" eaptls_process returned %d\n", status);
709 * EAP-TLS handshake was successful, return an
710 * EAP-TLS-Success packet here.
716 * The TLS code is still working on the TLS
717 * exchange, and it's a valid TLS request.
724 * Handshake is done, proceed with decoding tunneled
728 DEBUG2(" rlm_eap_tls: Received unexpected tunneled data after successful handshake.");
730 if (debug_flag > 2) {
732 unsigned int data_len;
733 unsigned char buffer[1024];
735 data_len = (tls_session->record_minus)(&tls_session->dirty_in,
736 buffer, sizeof(buffer));
737 log_debug(" Tunneled data (%u bytes)\n", data_len);
738 for (i = 0; i < data_len; i++) {
739 if ((i & 0x0f) == 0x00) printf(" %x: ", i);
740 if ((i & 0x0f) == 0x0f) printf("\n");
742 printf("%02x ", buffer[i]);
748 eaptls_fail(handler->eap_ds, 0);
753 * Anything else: fail.
760 * Success: Return MPPE keys.
762 eaptls_success(handler->eap_ds, 0);
763 eaptls_gen_mppe_keys(&handler->request->reply->vps,
765 "client EAP encryption");
770 * The module name should be the only globally exported symbol.
771 * That is, everything else should be 'static'.
773 EAP_TYPE rlm_eap_tls = {
775 eaptls_attach, /* attach */
776 eaptls_initiate, /* Start the initial request */
777 NULL, /* authorization */
778 eaptls_authenticate, /* authentication */
779 eaptls_detach /* detach */