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
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/autoconf.h>
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/process.h>
31 #include <freeradius-devel/rad_assert.h>
33 #ifdef HAVE_SYS_STAT_H
38 #ifdef HAVE_OPENSSL_RAND_H
39 #include <openssl/rand.h>
42 #ifdef HAVE_OPENSSL_OCSP_H
43 #include <openssl/ocsp.h>
47 static void record_init(record_t *buf);
48 static void record_close(record_t *buf);
49 static unsigned int record_plus(record_t *buf, const void *ptr,
51 static unsigned int record_minus(record_t *buf, void *ptr,
54 #ifdef PSK_MAX_IDENTITY_LEN
55 static unsigned int psk_server_callback(SSL *ssl, const char *identity,
56 unsigned char *psk, int max_psk_len)
59 fr_tls_server_conf_t *conf;
61 conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl,
62 FR_TLS_EX_INDEX_CONF);
66 * FIXME: Look up the PSK password based on the identity!
68 if (strcmp(identity, conf->psk_identity) != 0) {
72 psk_len = strlen(conf->psk_password);
73 if (psk_len > (2 * max_psk_len)) return 0;
75 return fr_hex2bin(conf->psk_password, psk, psk_len);
78 static unsigned int psk_client_callback(SSL *ssl, UNUSED const char *hint,
79 char *identity, unsigned int max_identity_len,
80 unsigned char *psk, unsigned int max_psk_len)
83 fr_tls_server_conf_t *conf;
85 conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl,
86 FR_TLS_EX_INDEX_CONF);
89 psk_len = strlen(conf->psk_password);
90 if (psk_len > (2 * max_psk_len)) return 0;
92 strlcpy(identity, conf->psk_identity, max_identity_len);
94 return fr_hex2bin(conf->psk_password, psk, psk_len);
99 tls_session_t *tls_new_client_session(fr_tls_server_conf_t *conf, int fd)
102 tls_session_t *ssn = NULL;
104 ssn = (tls_session_t *) malloc(sizeof(*ssn));
105 memset(ssn, 0, sizeof(*ssn));
107 ssn->ctx = conf->ctx;
108 ssn->ssl = SSL_new(ssn->ctx);
109 rad_assert(ssn->ssl != NULL);
112 * Add the message callback to identify what type of
113 * message/handshake is passed
115 SSL_set_msg_callback(ssn->ssl, cbtls_msg);
116 SSL_set_msg_callback_arg(ssn->ssl, ssn);
117 SSL_set_info_callback(ssn->ssl, cbtls_info);
120 * Always verify the peer certificate.
122 DEBUG2("Requiring Server certificate");
123 verify_mode = SSL_VERIFY_PEER;
124 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
125 SSL_set_verify(ssn->ssl, verify_mode, cbtls_verify);
127 SSL_set_ex_data(ssn->ssl, FR_TLS_EX_INDEX_CONF, (void *)conf);
128 SSL_set_fd(ssn->ssl, fd);
129 if (SSL_connect(ssn->ssl) <= 0) {
131 while ((err = ERR_get_error())) {
132 DEBUG("OpenSSL Err says %s",
133 ERR_error_string(err, NULL));
142 tls_session_t *tls_new_session(fr_tls_server_conf_t *conf, REQUEST *request,
145 tls_session_t *state = NULL;
151 * Manually flush the sessions every so often. If HALF
152 * of the session lifetime has passed since we last
153 * flushed, then flush it again.
155 * FIXME: Also do it every N sessions?
157 if (conf->session_cache_enable &&
158 ((conf->session_last_flushed + (conf->session_timeout * 1800)) <= request->timestamp)){
159 RDEBUG2("Flushing SSL sessions (of #%ld)",
160 SSL_CTX_sess_number(conf->ctx));
162 SSL_CTX_flush_sessions(conf->ctx, request->timestamp);
163 conf->session_last_flushed = request->timestamp;
166 if ((new_tls = SSL_new(conf->ctx)) == NULL) {
167 radlog(L_ERR, "SSL: Error creating new SSL: %s",
168 ERR_error_string(ERR_get_error(), NULL));
172 /* We use the SSL's "app_data" to indicate a call-back */
173 SSL_set_app_data(new_tls, NULL);
175 state = (tls_session_t *)malloc(sizeof(*state));
176 memset(state, 0, sizeof(*state));
179 state->ctx = conf->ctx;
180 state->ssl = new_tls;
183 * Initialize callbacks
185 state->record_init = record_init;
186 state->record_close = record_close;
187 state->record_plus = record_plus;
188 state->record_minus = record_minus;
191 * Create & hook the BIOs to handle the dirty side of the
192 * SSL. This is *very important* as we want to handle
193 * the transmission part. Now the only IO interface
194 * that SSL is aware of, is our defined BIO buffers.
196 * This means that all SSL IO is done to/from memory,
197 * and we can update those BIOs from the packets we've
200 state->into_ssl = BIO_new(BIO_s_mem());
201 state->from_ssl = BIO_new(BIO_s_mem());
202 SSL_set_bio(state->ssl, state->into_ssl, state->from_ssl);
205 * Add the message callback to identify what type of
206 * message/handshake is passed
208 SSL_set_msg_callback(new_tls, cbtls_msg);
209 SSL_set_msg_callback_arg(new_tls, state);
210 SSL_set_info_callback(new_tls, cbtls_info);
213 * In Server mode we only accept.
215 SSL_set_accept_state(state->ssl);
218 * Verify the peer certificate, if asked.
221 RDEBUG2("Requiring client certificate");
222 verify_mode = SSL_VERIFY_PEER;
223 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
224 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
226 SSL_set_verify(state->ssl, verify_mode, cbtls_verify);
228 SSL_set_ex_data(state->ssl, FR_TLS_EX_INDEX_CONF, (void *)conf);
229 state->length_flag = conf->include_length;
232 * We use default fragment size, unless the Framed-MTU
233 * tells us it's too big. Note that we do NOT account
234 * for the EAP-TLS headers if conf->fragment_size is
235 * large, because that config item looks to be confusing.
237 * i.e. it should REALLY be called MTU, and the code here
238 * should figure out what that means for TLS fragment size.
239 * asking the administrator to know the internal details
240 * of EAP-TLS in order to calculate fragment sizes is
243 state->offset = conf->fragment_size;
244 vp = pairfind(request->packet->vps, PW_FRAMED_MTU, 0);
245 if (vp && (vp->vp_integer > 100) && (vp->vp_integer < state->offset)) {
246 state->offset = vp->vp_integer;
249 if (conf->session_cache_enable) {
250 state->allow_session_resumption = 1; /* otherwise it's zero */
259 * Print out some text describing the error.
261 static int int_ssl_check(REQUEST *request, SSL *s, int ret, const char *text)
266 if ((l = ERR_get_error()) != 0) {
267 const char *p = ERR_error_string(l, NULL);
270 radlog(L_ERR, "SSL error %s", p);
273 vp = pairmake("Module-Failure-Message", p, T_OP_ADD);
274 if (vp) pairadd(&request->packet->vps, vp);
277 e = SSL_get_error(s, ret);
281 * These seem to be harmless and already "dealt
282 * with" by our non-blocking environment. NB:
283 * "ZERO_RETURN" is the clean "error"
284 * indicating a successfully closed SSL
285 * tunnel. We let this happen because our IO
286 * loop should not appear to have broken on
287 * this condition - and outside the IO loop, the
288 * "shutdown" state is checked.
290 * Don't print anything if we ignore the error.
293 case SSL_ERROR_WANT_READ:
294 case SSL_ERROR_WANT_WRITE:
295 case SSL_ERROR_WANT_X509_LOOKUP:
296 case SSL_ERROR_ZERO_RETURN:
300 * These seem to be indications of a genuine
301 * error that should result in the SSL tunnel
302 * being regarded as "dead".
304 case SSL_ERROR_SYSCALL:
305 radlog(L_ERR, "SSL: %s failed in a system call (%d), TLS session fails.",
310 radlog(L_ERR, "SSL: %s failed inside of TLS (%d), TLS session fails.",
316 * For any other errors that (a) exist, and (b)
317 * crop up - we need to interpret what to do with
318 * them - so "politely inform" the caller that
319 * the code needs updating here.
321 radlog(L_ERR, "SSL: FATAL SSL error ..... %d\n", e);
329 * We are the server, we always get the dirty data
330 * (Handshake data is also considered as dirty data)
331 * During handshake, since SSL API handles itself,
332 * After clean-up, dirty_out will be filled with
333 * the data required for handshaking. So we check
334 * if dirty_out is empty then we simply send it back.
335 * As of now, if handshake is successful, then we keep going,
338 * Fill the Bio with the dirty data to clean it
339 * Get the cleaned data from SSL, if it is not Handshake data
341 int tls_handshake_recv(REQUEST *request, tls_session_t *ssn)
345 err = BIO_write(ssn->into_ssl, ssn->dirty_in.data, ssn->dirty_in.used);
346 if (err != (int) ssn->dirty_in.used) {
347 RDEBUG("Failed writing %d to SSL BIO: %d", ssn->dirty_in.used,
349 record_init(&ssn->dirty_in);
352 record_init(&ssn->dirty_in);
354 err = SSL_read(ssn->ssl, ssn->clean_out.data + ssn->clean_out.used,
355 sizeof(ssn->clean_out.data) - ssn->clean_out.used);
357 ssn->clean_out.used += err;
361 if (!int_ssl_check(request, ssn->ssl, err, "SSL_read")) {
365 /* Some Extra STATE information for easy debugging */
366 if (SSL_is_init_finished(ssn->ssl)) {
367 DEBUG2("SSL Connection Established\n");
369 if (SSL_in_init(ssn->ssl)) {
370 DEBUG2("In SSL Handshake Phase\n");
372 if (SSL_in_before(ssn->ssl)) {
373 DEBUG2("Before SSL Handshake Phase\n");
375 if (SSL_in_accept_init(ssn->ssl)) {
376 DEBUG2("In SSL Accept mode \n");
378 if (SSL_in_connect_init(ssn->ssl)) {
379 DEBUG2("In SSL Connect mode \n");
382 err = BIO_ctrl_pending(ssn->from_ssl);
384 err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
385 sizeof(ssn->dirty_out.data));
387 ssn->dirty_out.used = err;
389 } else if (BIO_should_retry(ssn->from_ssl)) {
390 record_init(&ssn->dirty_in);
391 DEBUG2(" tls: Asking for more data in tunnel");
395 int_ssl_check(request, ssn->ssl, err, "BIO_read");
396 record_init(&ssn->dirty_in);
400 DEBUG2("SSL Application Data");
401 /* Its clean application data, do whatever we want */
402 record_init(&ssn->clean_out);
405 /* We are done with dirty_in, reinitialize it */
406 record_init(&ssn->dirty_in);
411 * Take clear-text user data, and encrypt it into the output buffer,
412 * to send to the client at the other end of the SSL connection.
414 int tls_handshake_send(REQUEST *request, tls_session_t *ssn)
419 * If there's un-encrypted data in 'clean_in', then write
420 * that data to the SSL session, and then call the BIO function
421 * to get that encrypted data from the SSL session, into
422 * a buffer which we can then package into an EAP packet.
424 * Based on Server's logic this clean_in is expected to
425 * contain the data to send to the client.
427 if (ssn->clean_in.used > 0) {
430 written = SSL_write(ssn->ssl, ssn->clean_in.data, ssn->clean_in.used);
431 record_minus(&ssn->clean_in, NULL, written);
433 /* Get the dirty data from Bio to send it */
434 err = BIO_read(ssn->from_ssl, ssn->dirty_out.data,
435 sizeof(ssn->dirty_out.data));
437 ssn->dirty_out.used = err;
439 int_ssl_check(request, ssn->ssl, err, "handshake_send");
446 void session_init(tls_session_t *ssn)
449 ssn->into_ssl = ssn->from_ssl = NULL;
450 record_init(&ssn->clean_in);
451 record_init(&ssn->clean_out);
452 record_init(&ssn->dirty_in);
453 record_init(&ssn->dirty_out);
455 memset(&ssn->info, 0, sizeof(ssn->info));
459 ssn->tls_msg_len = 0;
460 ssn->length_flag = 0;
462 ssn->free_opaque = NULL;
465 void session_close(tls_session_t *ssn)
467 SSL_set_quiet_shutdown(ssn->ssl, 1);
468 SSL_shutdown(ssn->ssl);
473 record_close(&ssn->clean_in);
474 record_close(&ssn->clean_out);
475 record_close(&ssn->dirty_in);
476 record_close(&ssn->dirty_out);
480 void session_free(void *ssn)
482 tls_session_t *sess = (tls_session_t *)ssn;
487 * Free any opaque TTLS or PEAP data.
489 if ((sess->opaque) && (sess->free_opaque)) {
490 sess->free_opaque(sess->opaque);
499 static void record_init(record_t *rec)
504 static void record_close(record_t *rec)
511 * Copy data to the intermediate buffer, before we send
514 static unsigned int record_plus(record_t *rec, const void *ptr,
517 unsigned int added = MAX_RECORD_SIZE - rec->used;
523 memcpy(rec->data + rec->used, ptr, added);
529 * Take data from the buffer, and give it to the caller.
531 static unsigned int record_minus(record_t *rec, void *ptr,
534 unsigned int taken = rec->used;
541 memcpy(ptr, rec->data, taken);
545 * This is pretty bad...
548 memmove(rec->data, rec->data + taken, rec->used);
552 void tls_session_information(tls_session_t *tls_session)
554 const char *str_write_p, *str_version, *str_content_type = "";
555 const char *str_details1 = "", *str_details2= "";
559 * Don't print this out in the normal course of
562 if (debug_flag == 0) {
566 str_write_p = tls_session->info.origin ? ">>>" : "<<<";
568 switch (tls_session->info.version)
571 str_version = "SSL 2.0";
574 str_version = "SSL 3.0 ";
577 str_version = "TLS 1.0 ";
580 str_version = "Unknown TLS version";
584 if (tls_session->info.version == SSL3_VERSION ||
585 tls_session->info.version == TLS1_VERSION) {
586 switch (tls_session->info.content_type) {
587 case SSL3_RT_CHANGE_CIPHER_SPEC:
588 str_content_type = "ChangeCipherSpec";
591 str_content_type = "Alert";
593 case SSL3_RT_HANDSHAKE:
594 str_content_type = "Handshake";
596 case SSL3_RT_APPLICATION_DATA:
597 str_content_type = "ApplicationData";
600 str_content_type = "UnknownContentType";
604 if (tls_session->info.content_type == SSL3_RT_ALERT) {
605 str_details1 = ", ???";
607 if (tls_session->info.record_len == 2) {
609 switch (tls_session->info.alert_level) {
610 case SSL3_AL_WARNING:
611 str_details1 = ", warning";
614 str_details1 = ", fatal";
618 str_details2 = " ???";
619 switch (tls_session->info.alert_description) {
620 case SSL3_AD_CLOSE_NOTIFY:
621 str_details2 = " close_notify";
623 case SSL3_AD_UNEXPECTED_MESSAGE:
624 str_details2 = " unexpected_message";
626 case SSL3_AD_BAD_RECORD_MAC:
627 str_details2 = " bad_record_mac";
629 case TLS1_AD_DECRYPTION_FAILED:
630 str_details2 = " decryption_failed";
632 case TLS1_AD_RECORD_OVERFLOW:
633 str_details2 = " record_overflow";
635 case SSL3_AD_DECOMPRESSION_FAILURE:
636 str_details2 = " decompression_failure";
638 case SSL3_AD_HANDSHAKE_FAILURE:
639 str_details2 = " handshake_failure";
641 case SSL3_AD_BAD_CERTIFICATE:
642 str_details2 = " bad_certificate";
644 case SSL3_AD_UNSUPPORTED_CERTIFICATE:
645 str_details2 = " unsupported_certificate";
647 case SSL3_AD_CERTIFICATE_REVOKED:
648 str_details2 = " certificate_revoked";
650 case SSL3_AD_CERTIFICATE_EXPIRED:
651 str_details2 = " certificate_expired";
653 case SSL3_AD_CERTIFICATE_UNKNOWN:
654 str_details2 = " certificate_unknown";
656 case SSL3_AD_ILLEGAL_PARAMETER:
657 str_details2 = " illegal_parameter";
659 case TLS1_AD_UNKNOWN_CA:
660 str_details2 = " unknown_ca";
662 case TLS1_AD_ACCESS_DENIED:
663 str_details2 = " access_denied";
665 case TLS1_AD_DECODE_ERROR:
666 str_details2 = " decode_error";
668 case TLS1_AD_DECRYPT_ERROR:
669 str_details2 = " decrypt_error";
671 case TLS1_AD_EXPORT_RESTRICTION:
672 str_details2 = " export_restriction";
674 case TLS1_AD_PROTOCOL_VERSION:
675 str_details2 = " protocol_version";
677 case TLS1_AD_INSUFFICIENT_SECURITY:
678 str_details2 = " insufficient_security";
680 case TLS1_AD_INTERNAL_ERROR:
681 str_details2 = " internal_error";
683 case TLS1_AD_USER_CANCELLED:
684 str_details2 = " user_canceled";
686 case TLS1_AD_NO_RENEGOTIATION:
687 str_details2 = " no_renegotiation";
693 if (tls_session->info.content_type == SSL3_RT_HANDSHAKE) {
694 str_details1 = "???";
696 if (tls_session->info.record_len > 0)
697 switch (tls_session->info.handshake_type)
699 case SSL3_MT_HELLO_REQUEST:
700 str_details1 = ", HelloRequest";
702 case SSL3_MT_CLIENT_HELLO:
703 str_details1 = ", ClientHello";
705 case SSL3_MT_SERVER_HELLO:
706 str_details1 = ", ServerHello";
708 case SSL3_MT_CERTIFICATE:
709 str_details1 = ", Certificate";
711 case SSL3_MT_SERVER_KEY_EXCHANGE:
712 str_details1 = ", ServerKeyExchange";
714 case SSL3_MT_CERTIFICATE_REQUEST:
715 str_details1 = ", CertificateRequest";
717 case SSL3_MT_SERVER_DONE:
718 str_details1 = ", ServerHelloDone";
720 case SSL3_MT_CERTIFICATE_VERIFY:
721 str_details1 = ", CertificateVerify";
723 case SSL3_MT_CLIENT_KEY_EXCHANGE:
724 str_details1 = ", ClientKeyExchange";
726 case SSL3_MT_FINISHED:
727 str_details1 = ", Finished";
733 snprintf(tls_session->info.info_description,
734 sizeof(tls_session->info.info_description),
735 "%s %s%s [length %04lx]%s%s\n",
736 str_write_p, str_version, str_content_type,
737 (unsigned long)tls_session->info.record_len,
738 str_details1, str_details2);
740 request = SSL_get_ex_data(tls_session->ssl, FR_TLS_EX_INDEX_REQUEST);
742 RDEBUG2("%s\n", tls_session->info.info_description);
745 static CONF_PARSER cache_config[] = {
746 { "enable", PW_TYPE_BOOLEAN,
747 offsetof(fr_tls_server_conf_t, session_cache_enable), NULL, "no" },
748 { "lifetime", PW_TYPE_INTEGER,
749 offsetof(fr_tls_server_conf_t, session_timeout), NULL, "24" },
750 { "max_entries", PW_TYPE_INTEGER,
751 offsetof(fr_tls_server_conf_t, session_cache_size), NULL, "255" },
752 { "name", PW_TYPE_STRING_PTR,
753 offsetof(fr_tls_server_conf_t, session_id_name), NULL, NULL},
754 { NULL, -1, 0, NULL, NULL } /* end the list */
757 static CONF_PARSER verify_config[] = {
758 { "tmpdir", PW_TYPE_STRING_PTR,
759 offsetof(fr_tls_server_conf_t, verify_tmp_dir), NULL, NULL},
760 { "client", PW_TYPE_STRING_PTR,
761 offsetof(fr_tls_server_conf_t, verify_client_cert_cmd), NULL, NULL},
762 { NULL, -1, 0, NULL, NULL } /* end the list */
765 #ifdef HAVE_OPENSSL_OCSP_H
766 static CONF_PARSER ocsp_config[] = {
767 { "enable", PW_TYPE_BOOLEAN,
768 offsetof(fr_tls_server_conf_t, ocsp_enable), NULL, "no"},
769 { "override_cert_url", PW_TYPE_BOOLEAN,
770 offsetof(fr_tls_server_conf_t, ocsp_override_url), NULL, "no"},
771 { "url", PW_TYPE_STRING_PTR,
772 offsetof(fr_tls_server_conf_t, ocsp_url), NULL, NULL },
773 { "use_nonce", PW_TYPE_BOOLEAN,
774 offsetof(fr_tls_server_conf_t, ocsp_use_nonce), NULL, "yes"},
775 { "timeout", PW_TYPE_INTEGER,
776 offsetof(fr_tls_server_conf_t, ocsp_timeout), NULL, "yes"},
777 { "softfail", PW_TYPE_BOOLEAN,
778 offsetof(fr_tls_server_conf_t, ocsp_softfail), NULL, "yes"},
779 { NULL, -1, 0, NULL, NULL } /* end the list */
783 static CONF_PARSER tls_server_config[] = {
784 { "rsa_key_exchange", PW_TYPE_BOOLEAN,
785 offsetof(fr_tls_server_conf_t, rsa_key), NULL, "no" },
786 { "dh_key_exchange", PW_TYPE_BOOLEAN,
787 offsetof(fr_tls_server_conf_t, dh_key), NULL, "yes" },
788 { "rsa_key_length", PW_TYPE_INTEGER,
789 offsetof(fr_tls_server_conf_t, rsa_key_length), NULL, "512" },
790 { "dh_key_length", PW_TYPE_INTEGER,
791 offsetof(fr_tls_server_conf_t, dh_key_length), NULL, "512" },
792 { "verify_depth", PW_TYPE_INTEGER,
793 offsetof(fr_tls_server_conf_t, verify_depth), NULL, "0" },
794 { "CA_path", PW_TYPE_FILENAME,
795 offsetof(fr_tls_server_conf_t, ca_path), NULL, NULL },
796 { "pem_file_type", PW_TYPE_BOOLEAN,
797 offsetof(fr_tls_server_conf_t, file_type), NULL, "yes" },
798 { "private_key_file", PW_TYPE_FILENAME,
799 offsetof(fr_tls_server_conf_t, private_key_file), NULL, NULL },
800 { "certificate_file", PW_TYPE_FILENAME,
801 offsetof(fr_tls_server_conf_t, certificate_file), NULL, NULL },
802 { "CA_file", PW_TYPE_FILENAME,
803 offsetof(fr_tls_server_conf_t, ca_file), NULL, NULL },
804 { "private_key_password", PW_TYPE_STRING_PTR,
805 offsetof(fr_tls_server_conf_t, private_key_password), NULL, NULL },
806 #ifdef PSK_MAX_IDENTITY_LEN
807 { "psk_identity", PW_TYPE_STRING_PTR,
808 offsetof(fr_tls_server_conf_t, psk_identity), NULL, NULL },
809 { "psk_hexphrase", PW_TYPE_STRING_PTR,
810 offsetof(fr_tls_server_conf_t, psk_password), NULL, NULL },
812 { "dh_file", PW_TYPE_STRING_PTR,
813 offsetof(fr_tls_server_conf_t, dh_file), NULL, NULL },
814 { "random_file", PW_TYPE_STRING_PTR,
815 offsetof(fr_tls_server_conf_t, random_file), NULL, NULL },
816 { "fragment_size", PW_TYPE_INTEGER,
817 offsetof(fr_tls_server_conf_t, fragment_size), NULL, "1024" },
818 { "include_length", PW_TYPE_BOOLEAN,
819 offsetof(fr_tls_server_conf_t, include_length), NULL, "yes" },
820 { "check_crl", PW_TYPE_BOOLEAN,
821 offsetof(fr_tls_server_conf_t, check_crl), NULL, "no"},
822 { "allow_expired_crl", PW_TYPE_BOOLEAN,
823 offsetof(fr_tls_server_conf_t, allow_expired_crl), NULL, NULL},
824 { "check_cert_cn", PW_TYPE_STRING_PTR,
825 offsetof(fr_tls_server_conf_t, check_cert_cn), NULL, NULL},
826 { "cipher_list", PW_TYPE_STRING_PTR,
827 offsetof(fr_tls_server_conf_t, cipher_list), NULL, NULL},
828 { "check_cert_issuer", PW_TYPE_STRING_PTR,
829 offsetof(fr_tls_server_conf_t, check_cert_issuer), NULL, NULL},
830 { "make_cert_command", PW_TYPE_STRING_PTR,
831 offsetof(fr_tls_server_conf_t, make_cert_command), NULL, NULL},
832 { "require_client_cert", PW_TYPE_BOOLEAN,
833 offsetof(fr_tls_server_conf_t, require_client_cert), NULL, NULL },
835 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
836 #ifndef OPENSSL_NO_ECDH
837 { "ecdh_curve", PW_TYPE_STRING_PTR,
838 offsetof(fr_tls_server_conf_t, ecdh_curve), NULL, "prime256v1"},
842 { "cache", PW_TYPE_SUBSECTION, 0, NULL, (const void *) cache_config },
844 { "verify", PW_TYPE_SUBSECTION, 0, NULL, (const void *) verify_config },
846 #ifdef HAVE_OPENSSL_OCSP_H
847 { "ocsp", PW_TYPE_SUBSECTION, 0, NULL, (const void *) ocsp_config },
850 { NULL, -1, 0, NULL, NULL } /* end the list */
854 static CONF_PARSER tls_client_config[] = {
855 { "rsa_key_exchange", PW_TYPE_BOOLEAN,
856 offsetof(fr_tls_server_conf_t, rsa_key), NULL, "no" },
857 { "dh_key_exchange", PW_TYPE_BOOLEAN,
858 offsetof(fr_tls_server_conf_t, dh_key), NULL, "yes" },
859 { "rsa_key_length", PW_TYPE_INTEGER,
860 offsetof(fr_tls_server_conf_t, rsa_key_length), NULL, "512" },
861 { "dh_key_length", PW_TYPE_INTEGER,
862 offsetof(fr_tls_server_conf_t, dh_key_length), NULL, "512" },
863 { "verify_depth", PW_TYPE_INTEGER,
864 offsetof(fr_tls_server_conf_t, verify_depth), NULL, "0" },
865 { "CA_path", PW_TYPE_FILENAME,
866 offsetof(fr_tls_server_conf_t, ca_path), NULL, NULL },
867 { "pem_file_type", PW_TYPE_BOOLEAN,
868 offsetof(fr_tls_server_conf_t, file_type), NULL, "yes" },
869 { "private_key_file", PW_TYPE_FILENAME,
870 offsetof(fr_tls_server_conf_t, private_key_file), NULL, NULL },
871 { "certificate_file", PW_TYPE_FILENAME,
872 offsetof(fr_tls_server_conf_t, certificate_file), NULL, NULL },
873 { "CA_file", PW_TYPE_FILENAME,
874 offsetof(fr_tls_server_conf_t, ca_file), NULL, NULL },
875 { "private_key_password", PW_TYPE_STRING_PTR,
876 offsetof(fr_tls_server_conf_t, private_key_password), NULL, NULL },
877 { "dh_file", PW_TYPE_STRING_PTR,
878 offsetof(fr_tls_server_conf_t, dh_file), NULL, NULL },
879 { "random_file", PW_TYPE_STRING_PTR,
880 offsetof(fr_tls_server_conf_t, random_file), NULL, NULL },
881 { "fragment_size", PW_TYPE_INTEGER,
882 offsetof(fr_tls_server_conf_t, fragment_size), NULL, "1024" },
883 { "include_length", PW_TYPE_BOOLEAN,
884 offsetof(fr_tls_server_conf_t, include_length), NULL, "yes" },
885 { "check_crl", PW_TYPE_BOOLEAN,
886 offsetof(fr_tls_server_conf_t, check_crl), NULL, "no"},
887 { "check_cert_cn", PW_TYPE_STRING_PTR,
888 offsetof(fr_tls_server_conf_t, check_cert_cn), NULL, NULL},
889 { "cipher_list", PW_TYPE_STRING_PTR,
890 offsetof(fr_tls_server_conf_t, cipher_list), NULL, NULL},
891 { "check_cert_issuer", PW_TYPE_STRING_PTR,
892 offsetof(fr_tls_server_conf_t, check_cert_issuer), NULL, NULL},
894 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
895 #ifndef OPENSSL_NO_ECDH
896 { "ecdh_curve", PW_TYPE_STRING_PTR,
897 offsetof(fr_tls_server_conf_t, ecdh_curve), NULL, "prime256v1"},
901 { NULL, -1, 0, NULL, NULL } /* end the list */
906 * TODO: Check for the type of key exchange * like conf->dh_key
908 static int load_dh_params(SSL_CTX *ctx, char *file)
913 if ((bio = BIO_new_file(file, "r")) == NULL) {
914 radlog(L_ERR, "rlm_eap_tls: Unable to open DH file - %s", file);
918 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
921 DEBUG2("WARNING: rlm_eap_tls: Unable to set DH parameters. DH cipher suites may not work!");
922 DEBUG2("WARNING: Fix this by running the OpenSSL command listed in eap.conf");
926 if (SSL_CTX_set_tmp_dh(ctx, dh) < 0) {
927 radlog(L_ERR, "rlm_eap_tls: Unable to set DH parameters");
938 * Generate ephemeral RSA keys.
940 static int generate_eph_rsa_key(SSL_CTX *ctx)
944 rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
946 if (!SSL_CTX_set_tmp_rsa(ctx, rsa)) {
947 radlog(L_ERR, "rlm_eap_tls: Couldn't set ephemeral RSA key");
957 * Print debugging messages, and free data.
959 * FIXME: Write sessions to some long-term storage, so that
960 * session resumption can still occur after the server
963 #define MAX_SESSION_SIZE (256)
965 static void cbtls_remove_session(UNUSED SSL_CTX *ctx, SSL_SESSION *sess)
968 char buffer[2 * MAX_SESSION_SIZE + 1];
970 size = sess->session_id_length;
971 if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;
973 fr_bin2hex(sess->session_id, buffer, size);
975 DEBUG2(" SSL: Removing session %s from the cache", buffer);
980 static int cbtls_new_session(UNUSED SSL *s, SSL_SESSION *sess)
983 char buffer[2 * MAX_SESSION_SIZE + 1];
985 size = sess->session_id_length;
986 if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;
988 fr_bin2hex(sess->session_id, buffer, size);
990 DEBUG2(" SSL: adding session %s to cache", buffer);
995 static SSL_SESSION *cbtls_get_session(UNUSED SSL *s,
996 unsigned char *data, int len,
1000 char buffer[2 * MAX_SESSION_SIZE + 1];
1003 if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;
1005 fr_bin2hex(data, buffer, size);
1007 DEBUG2(" SSL: Client requested nonexistent cached session %s",
1014 #ifdef HAVE_OPENSSL_OCSP_H
1016 * This function extracts the OCSP Responder URL
1017 * from an existing x509 certificate.
1019 static int ocsp_parse_cert_url(X509 *cert, char **phost, char **pport,
1020 char **ppath, int *pssl)
1024 AUTHORITY_INFO_ACCESS *aia;
1025 ACCESS_DESCRIPTION *ad;
1027 aia = X509_get_ext_d2i(cert, NID_info_access, NULL, NULL);
1029 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(aia); i++) {
1030 ad = sk_ACCESS_DESCRIPTION_value(aia, 0);
1031 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
1032 if (ad->location->type == GEN_URI) {
1033 if(OCSP_parse_url(ad->location->d.ia5->data,
1034 phost, pport, ppath, pssl))
1043 * This function sends a OCSP request to a defined OCSP responder
1044 * and checks the OCSP response for correctness.
1047 /* Maximum leeway in validity period: default 5 minutes */
1048 #define MAX_VALIDITY_PERIOD (5 * 60)
1050 static int ocsp_check(X509_STORE *store, X509 *issuer_cert, X509 *client_cert,
1051 fr_tls_server_conf_t *conf)
1053 OCSP_CERTID *certid;
1055 OCSP_RESPONSE *resp = NULL;
1056 OCSP_BASICRESP *bresp = NULL;
1061 long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
1062 BIO *cbio, *bio_out;
1065 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
1067 #if OPENSSL_VERSION_NUMBER >= 0x1000003f
1071 struct timeval when;
1075 * Create OCSP Request
1077 certid = OCSP_cert_to_id(NULL, client_cert, issuer_cert);
1078 req = OCSP_REQUEST_new();
1079 OCSP_request_add0_id(req, certid);
1080 if(conf->ocsp_use_nonce) {
1081 OCSP_request_add1_nonce(req, NULL, 8);
1085 * Send OCSP Request and get OCSP Response
1088 /* Get OCSP responder URL */
1089 if(conf->ocsp_override_url) {
1090 OCSP_parse_url(conf->ocsp_url, &host, &port, &path, &use_ssl);
1093 ocsp_parse_cert_url(client_cert, &host, &port, &path, &use_ssl);
1096 DEBUG2("[ocsp] --> Responder URL = http://%s:%s%s", host, port, path);
1098 /* Setup BIO socket to OCSP responder */
1099 cbio = BIO_new_connect(host);
1101 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
1103 BIO_set_conn_port(cbio, port);
1104 #if OPENSSL_VERSION_NUMBER < 0x1000003f
1105 BIO_do_connect(cbio);
1107 /* Send OCSP request and wait for response */
1108 resp = OCSP_sendreq_bio(cbio, path, req);
1110 radlog(L_ERR, "Error: Couldn't get OCSP response");
1115 if (conf->ocsp_timeout)
1116 BIO_set_nbio(cbio, 1);
1118 rc = BIO_do_connect(cbio);
1119 if ((rc <= 0) && ((!conf->ocsp_timeout) || !BIO_should_retry(cbio))) {
1120 radlog(L_ERR, "Error: Couldn't connect to OCSP responder");
1125 ctx = OCSP_sendreq_new(cbio, path, req, -1);
1127 radlog(L_ERR, "Error: Couldn't send OCSP request");
1132 gettimeofday(&when, NULL);
1133 when.tv_sec += conf->ocsp_timeout;
1136 rc = OCSP_sendreq_nbio(&resp, ctx);
1137 if (conf->ocsp_timeout) {
1138 gettimeofday(&now, NULL);
1139 if (!timercmp(&now, &when, <))
1142 } while ((rc == -1) && BIO_should_retry(cbio));
1144 if (conf->ocsp_timeout && (rc == -1) && BIO_should_retry(cbio)) {
1145 radlog(L_ERR, "Error: OCSP response timed out");
1150 OCSP_REQ_CTX_free(ctx);
1153 radlog(L_ERR, "Error: Couldn't get OCSP response");
1159 /* Verify OCSP response status */
1160 status = OCSP_response_status(resp);
1161 DEBUG2("[ocsp] --> Response status: %s",OCSP_response_status_str(status));
1162 if(status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
1163 radlog(L_ERR, "Error: OCSP response status: %s", OCSP_response_status_str(status));
1166 bresp = OCSP_response_get1_basic(resp);
1167 if(conf->ocsp_use_nonce && OCSP_check_nonce(req, bresp)!=1) {
1168 radlog(L_ERR, "Error: OCSP response has wrong nonce value");
1171 if(OCSP_basic_verify(bresp, NULL, store, 0)!=1){
1172 radlog(L_ERR, "Error: Couldn't verify OCSP basic response");
1176 /* Verify OCSP cert status */
1177 if(!OCSP_resp_find_status(bresp, certid, &status, &reason,
1178 &rev, &thisupd, &nextupd)) {
1179 radlog(L_ERR, "ERROR: No Status found.\n");
1183 if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage)) {
1184 BIO_puts(bio_out, "WARNING: Status times invalid.\n");
1185 ERR_print_errors(bio_out);
1188 BIO_puts(bio_out, "\tThis Update: ");
1189 ASN1_GENERALIZEDTIME_print(bio_out, thisupd);
1190 BIO_puts(bio_out, "\n");
1191 BIO_puts(bio_out, "\tNext Update: ");
1192 ASN1_GENERALIZEDTIME_print(bio_out, nextupd);
1193 BIO_puts(bio_out, "\n");
1196 case V_OCSP_CERTSTATUS_GOOD:
1197 DEBUG2("[oscp] --> Cert status: good");
1202 /* REVOKED / UNKNOWN */
1203 DEBUG2("[ocsp] --> Cert status: %s",OCSP_cert_status_str(status));
1205 DEBUG2("[ocsp] --> Reason: %s", OCSP_crl_reason_str(reason));
1206 BIO_puts(bio_out, "\tRevocation Time: ");
1207 ASN1_GENERALIZEDTIME_print(bio_out, rev);
1208 BIO_puts(bio_out, "\n");
1213 /* Free OCSP Stuff */
1214 OCSP_REQUEST_free(req);
1215 OCSP_RESPONSE_free(resp);
1220 OCSP_BASICRESP_free(bresp);
1224 DEBUG2("[ocsp] --> Certificate is valid!");
1227 if (conf->ocsp_softfail) {
1228 DEBUG2("[ocsp] --> Unable to check certificate; assuming valid.");
1229 DEBUG2("[ocsp] --> Warning! This may be insecure.");
1232 DEBUG2("[ocsp] --> Unable to check certificate; failing!");
1237 DEBUG2("[ocsp] --> Certificate has been expired/revoked!");
1243 #endif /* HAVE_OPENSSL_OCSP_H */
1246 * For creating certificate attributes.
1248 static const char *cert_attr_names[6][2] = {
1249 { "TLS-Client-Cert-Serial", "TLS-Cert-Serial" },
1250 { "TLS-Client-Cert-Expiration", "TLS-Cert-Expiration" },
1251 { "TLS-Client-Cert-Subject", "TLS-Cert-Subject" },
1252 { "TLS-Client-Cert-Issuer", "TLS-Cert-Issuer" },
1253 { "TLS-Client-Cert-Common-Name", "TLS-Cert-Common-Name" },
1254 { "TLS-Client-Cert-Subject-Alt-Name-Email", "TLS-Cert-Subject-Alt-Name-Email" }
1257 #define FR_TLS_SERIAL (0)
1258 #define FR_TLS_EXPIRATION (1)
1259 #define FR_TLS_SUBJECT (2)
1260 #define FR_TLS_ISSUER (3)
1261 #define FR_TLS_CN (4)
1262 #define FR_TLS_SAN_EMAIL (5)
1265 * Before trusting a certificate, you must make sure that the
1266 * certificate is 'valid'. There are several steps that your
1267 * application can take in determining if a certificate is
1268 * valid. Commonly used steps are:
1270 * 1.Verifying the certificate's signature, and verifying that
1271 * the certificate has been issued by a trusted Certificate
1274 * 2.Verifying that the certificate is valid for the present date
1275 * (i.e. it is being presented within its validity dates).
1277 * 3.Verifying that the certificate has not been revoked by its
1278 * issuing Certificate Authority, by checking with respect to a
1279 * Certificate Revocation List (CRL).
1281 * 4.Verifying that the credentials presented by the certificate
1282 * fulfill additional requirements specific to the application,
1283 * such as with respect to access control lists or with respect
1284 * to OCSP (Online Certificate Status Processing).
1286 * NOTE: This callback will be called multiple times based on the
1287 * depth of the root certificate chain
1289 int cbtls_verify(int ok, X509_STORE_CTX *ctx)
1291 char subject[1024]; /* Used for the subject name */
1292 char issuer[1024]; /* Used for the issuer name */
1293 char common_name[1024];
1298 int err, depth, lookup, loc;
1299 fr_tls_server_conf_t *conf;
1302 ASN1_INTEGER *sn = NULL;
1303 ASN1_TIME *asn_time = NULL;
1306 #ifdef HAVE_OPENSSL_OCSP_H
1307 X509_STORE *ocsp_store = NULL;
1311 client_cert = X509_STORE_CTX_get_current_cert(ctx);
1312 err = X509_STORE_CTX_get_error(ctx);
1313 depth = X509_STORE_CTX_get_error_depth(ctx);
1318 * Log client/issuing cert. If there's an error, log
1321 if ((lookup > 1) && !my_ok) lookup = 1;
1324 * Retrieve the pointer to the SSL of the connection currently treated
1325 * and the application specific data stored into the SSL object.
1327 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
1328 conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CONF);
1329 if (!conf) return 1;
1331 request = (REQUEST *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_REQUEST);
1333 if (!request) return 1; /* FIXME: outbound TLS */
1335 rad_assert(request != NULL);
1336 certs = (VALUE_PAIR **)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_CERTS);
1337 rad_assert(certs != NULL);
1338 identity = (char **)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_IDENTITY);
1339 #ifdef HAVE_OPENSSL_OCSP_H
1340 ocsp_store = (X509_STORE *)SSL_get_ex_data(ssl, FR_TLS_EX_INDEX_STORE);
1345 * Get the Serial Number
1348 sn = X509_get_serialNumber(client_cert);
1351 * For this next bit, we create the attributes *only* if
1352 * we're at the client or issuing certificate, AND we
1353 * have a user identity. i.e. we don't create the
1354 * attributes for RadSec connections.
1357 (lookup <= 1) && sn && ((size_t) sn->length < (sizeof(buf) / 2))) {
1361 for (i = 0; i < sn->length; i++) {
1362 sprintf(p, "%02x", (unsigned int)sn->data[i]);
1366 pairmake(cert_attr_names[FR_TLS_SERIAL][lookup], buf, T_OP_SET));
1371 * Get the Expiration Date
1374 asn_time = X509_get_notAfter(client_cert);
1375 if (identity && (lookup <= 1) && asn_time &&
1376 (asn_time->length < MAX_STRING_LEN)) {
1377 memcpy(buf, (char*) asn_time->data, asn_time->length);
1378 buf[asn_time->length] = '\0';
1380 pairmake(cert_attr_names[FR_TLS_EXPIRATION][lookup], buf, T_OP_SET));
1384 * Get the Subject & Issuer
1386 subject[0] = issuer[0] = '\0';
1387 X509_NAME_oneline(X509_get_subject_name(client_cert), subject,
1389 subject[sizeof(subject) - 1] = '\0';
1390 if (identity && (lookup <= 1) && subject[0] &&
1391 (strlen(subject) < MAX_STRING_LEN)) {
1393 pairmake(cert_attr_names[FR_TLS_SUBJECT][lookup], subject, T_OP_SET));
1396 X509_NAME_oneline(X509_get_issuer_name(ctx->current_cert), issuer,
1398 issuer[sizeof(issuer) - 1] = '\0';
1399 if (identity && (lookup <= 1) && issuer[0] &&
1400 (strlen(issuer) < MAX_STRING_LEN)) {
1402 pairmake(cert_attr_names[FR_TLS_ISSUER][lookup], issuer, T_OP_SET));
1406 * Get the Common Name
1408 X509_NAME_get_text_by_NID(X509_get_subject_name(client_cert),
1409 NID_commonName, common_name, sizeof(common_name));
1410 common_name[sizeof(common_name) - 1] = '\0';
1411 if (identity && (lookup <= 1) && common_name[0] &&
1412 (strlen(common_name) < MAX_STRING_LEN)) {
1414 pairmake(cert_attr_names[FR_TLS_CN][lookup], common_name, T_OP_SET));
1419 * Get the RFC822 Subject Alternative Name
1421 loc = X509_get_ext_by_NID(client_cert, NID_subject_alt_name, 0);
1422 if (lookup <= 1 && loc >= 0) {
1423 X509_EXTENSION *ext = NULL;
1424 GENERAL_NAMES *names = NULL;
1427 if ((ext = X509_get_ext(client_cert, loc)) &&
1428 (names = X509V3_EXT_d2i(ext))) {
1429 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1430 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1432 switch (name->type) {
1434 if (ASN1_STRING_length(name->d.rfc822Name) >= MAX_STRING_LEN)
1438 pairmake(cert_attr_names[FR_TLS_SAN_EMAIL][lookup],
1439 ASN1_STRING_data(name->d.rfc822Name), T_OP_SET));
1442 /* XXX TODO handle other SAN types */
1448 sk_GENERAL_NAME_free(names);
1450 #endif /* GEN_EMAIL */
1453 * If the CRL has expired, that might still be OK.
1456 (conf->allow_expired_crl) &&
1457 (err == X509_V_ERR_CRL_HAS_EXPIRED)) {
1459 X509_STORE_CTX_set_error( ctx, 0 );
1463 const char *p = X509_verify_cert_error_string(err);
1464 radlog(L_ERR,"--> verify error:num=%d:%s\n",err, p);
1465 radius_pairmake(request, &request->packet->vps,
1466 "Module-Failure-Message", p, T_OP_SET);
1470 switch (ctx->error) {
1472 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1473 radlog(L_ERR, "issuer= %s\n", issuer);
1475 case X509_V_ERR_CERT_NOT_YET_VALID:
1476 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1477 radlog(L_ERR, "notBefore=");
1479 ASN1_TIME_print(bio_err, X509_get_notBefore(ctx->current_cert));
1482 case X509_V_ERR_CERT_HAS_EXPIRED:
1483 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1484 radlog(L_ERR, "notAfter=");
1486 ASN1_TIME_print(bio_err, X509_get_notAfter(ctx->current_cert));
1492 * If we're at the actual client cert, apply additional
1497 * If the conf tells us to, check cert issuer
1498 * against the specified value and fail
1499 * verification if they don't match.
1501 if (conf->check_cert_issuer &&
1502 (strcmp(issuer, conf->check_cert_issuer) != 0)) {
1503 radlog(L_AUTH, "rlm_eap_tls: Certificate issuer (%s) does not match specified value (%s)!", issuer, conf->check_cert_issuer);
1508 * If the conf tells us to, check the CN in the
1509 * cert against xlat'ed value, but only if the
1510 * previous checks passed.
1512 if (my_ok && conf->check_cert_cn) {
1513 if (!radius_xlat(cn_str, sizeof(cn_str), conf->check_cert_cn, request, NULL)) {
1514 radlog(L_ERR, "rlm_eap_tls (%s): xlat failed.",
1515 conf->check_cert_cn);
1516 /* if this fails, fail the verification */
1519 RDEBUG2("checking certificate CN (%s) with xlat'ed value (%s)", common_name, cn_str);
1520 if (strcmp(cn_str, common_name) != 0) {
1521 radlog(L_AUTH, "rlm_eap_tls: Certificate CN (%s) does not match specified value (%s)!", common_name, cn_str);
1525 } /* check_cert_cn */
1527 #ifdef HAVE_OPENSSL_OCSP_H
1528 if (my_ok && conf->ocsp_enable){
1529 RDEBUG2("--> Starting OCSP Request");
1530 if(X509_STORE_CTX_get1_issuer(&issuer_cert, ctx, client_cert)!=1) {
1531 radlog(L_ERR, "Error: Couldn't get issuer_cert for %s", common_name);
1533 my_ok = ocsp_check(ocsp_store, issuer_cert, client_cert, conf);
1537 while (conf->verify_client_cert_cmd) {
1542 snprintf(filename, sizeof(filename), "%s/%s.client.XXXXXXXX",
1543 conf->verify_tmp_dir, progname);
1544 fd = mkstemp(filename);
1546 RDEBUG("Failed creating file in %s: %s",
1547 conf->verify_tmp_dir, strerror(errno));
1551 fp = fdopen(fd, "w");
1553 RDEBUG("Failed opening file %s: %s",
1554 filename, strerror(errno));
1558 if (!PEM_write_X509(fp, client_cert)) {
1560 RDEBUG("Failed writing certificate to file");
1565 if (!radius_pairmake(request, &request->packet->vps,
1566 "TLS-Client-Cert-Filename",
1567 filename, T_OP_SET)) {
1568 RDEBUG("Failed creating TLS-Client-Cert-Filename");
1573 RDEBUG("Verifying client certificate: %s",
1574 conf->verify_client_cert_cmd);
1575 if (radius_exec_program(conf->verify_client_cert_cmd,
1576 request, 1, NULL, 0,
1577 request->packet->vps,
1579 radlog(L_AUTH, "rlm_eap_tls: Certificate CN (%s) fails external verification!", common_name);
1582 RDEBUG("Client certificate CN %s passed external validation", common_name);
1593 if (debug_flag > 0) {
1594 RDEBUG2("chain-depth=%d, ", depth);
1595 RDEBUG2("error=%d", err);
1597 if (identity) RDEBUG2("--> User-Name = %s", *identity);
1598 RDEBUG2("--> BUF-Name = %s", common_name);
1599 RDEBUG2("--> subject = %s", subject);
1600 RDEBUG2("--> issuer = %s", issuer);
1601 RDEBUG2("--> verify return:%d", my_ok);
1607 #ifdef HAVE_OPENSSL_OCSP_H
1609 * Create Global X509 revocation store and use it to verify
1612 * - Load the trusted CAs
1613 * - Load the trusted issuer certificates
1615 static X509_STORE *init_revocation_store(fr_tls_server_conf_t *conf)
1617 X509_STORE *store = NULL;
1619 store = X509_STORE_new();
1621 /* Load the CAs we trust */
1622 if (conf->ca_file || conf->ca_path)
1623 if(!X509_STORE_load_locations(store, conf->ca_file, conf->ca_path)) {
1624 radlog(L_ERR, "rlm_eap: X509_STORE error %s", ERR_error_string(ERR_get_error(), NULL));
1625 radlog(L_ERR, "rlm_eap_tls: Error reading Trusted root CA list %s",conf->ca_file );
1629 #ifdef X509_V_FLAG_CRL_CHECK
1630 if (conf->check_crl)
1631 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK);
1635 #endif /* HAVE_OPENSSL_OCSP_H */
1637 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
1638 #ifndef OPENSSL_NO_ECDH
1639 static int set_ecdh_curve(SSL_CTX *ctx, const char *ecdh_curve)
1644 if (!ecdh_curve || !*ecdh_curve) return 0;
1646 nid = OBJ_sn2nid(ecdh_curve);
1648 radlog(L_ERR, "Unknown ecdh_curve \"%s\"", ecdh_curve);
1652 ecdh = EC_KEY_new_by_curve_name(nid);
1654 radlog(L_ERR, "Unable to create new curve \"%s\"", ecdh_curve);
1658 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
1660 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
1669 /* index we use to store cached session VPs
1670 * needs to be dynamic so we can supply a "free" function
1672 static int FR_TLS_EX_INDEX_VPS = -1;
1675 * DIE OPENSSL DIE DIE DIE
1677 * What a palaver, just to free some data attached the
1678 * session. We need to do this because the "remove" callback
1679 * is called when refcount > 0 sometimes, if another thread
1680 * is using the session
1682 static void sess_free_vps(UNUSED void *parent, void *data_ptr,
1683 UNUSED CRYPTO_EX_DATA *ad, UNUSED int idx,
1684 UNUSED long argl, UNUSED void *argp)
1686 VALUE_PAIR *vp = data_ptr;
1689 DEBUG2(" Freeing cached session VPs %p", vp);
1696 * Create Global context SSL and use it in every new session
1698 * - Load the trusted CAs
1699 * - Load the Private key & the certificate
1700 * - Set the Context options & Verify options
1702 static SSL_CTX *init_tls_ctx(fr_tls_server_conf_t *conf, int client)
1704 const SSL_METHOD *meth;
1706 X509_STORE *certstore;
1707 int verify_mode = SSL_VERIFY_NONE;
1708 int ctx_options = 0;
1712 * Add all the default ciphers and message digests
1713 * Create our context.
1716 SSL_load_error_strings();
1719 * SHA256 is in all versions of OpenSSL, but isn't
1720 * initialized by default. It's needed for WiMAX
1723 #ifdef HAVE_OPENSSL_EVP_SHA256
1724 EVP_add_digest(EVP_sha256());
1727 meth = TLSv1_method();
1728 ctx = SSL_CTX_new(meth);
1731 * Identify the type of certificates that needs to be loaded
1733 if (conf->file_type) {
1734 type = SSL_FILETYPE_PEM;
1736 type = SSL_FILETYPE_ASN1;
1740 * Set the password to load private key
1742 if (conf->private_key_password) {
1745 * We don't want to put the private key password in eap.conf, so check
1746 * for our special string which indicates we should get the password
1749 const char* special_string = "Apple:UseCertAdmin";
1750 if (strncmp(conf->private_key_password,
1752 strlen(special_string)) == 0)
1755 const long max_password_len = 128;
1756 snprintf(cmd, sizeof(cmd) - 1,
1757 "/usr/sbin/certadmin --get-private-key-passphrase \"%s\"",
1758 conf->private_key_file);
1760 DEBUG2("rlm_eap: Getting private key passphrase using command \"%s\"", cmd);
1762 FILE* cmd_pipe = popen(cmd, "r");
1764 radlog(L_ERR, "rlm_eap: %s command failed. Unable to get private_key_password", cmd);
1765 radlog(L_ERR, "rlm_eap: Error reading private_key_file %s", conf->private_key_file);
1769 free(conf->private_key_password);
1770 conf->private_key_password = malloc(max_password_len * sizeof(char));
1771 if (!conf->private_key_password) {
1772 radlog(L_ERR, "rlm_eap: Can't malloc space for private_key_password");
1773 radlog(L_ERR, "rlm_eap: Error reading private_key_file %s", conf->private_key_file);
1778 fgets(conf->private_key_password, max_password_len, cmd_pipe);
1781 /* Get rid of newline at end of password. */
1782 conf->private_key_password[strlen(conf->private_key_password) - 1] = '\0';
1783 DEBUG2("rlm_eap: Password from command = \"%s\"", conf->private_key_password);
1786 SSL_CTX_set_default_passwd_cb_userdata(ctx, conf->private_key_password);
1787 SSL_CTX_set_default_passwd_cb(ctx, cbtls_password);
1790 #ifdef PSK_MAX_IDENTITY_LEN
1791 if ((conf->psk_identity && !conf->psk_password) ||
1792 (!conf->psk_identity && conf->psk_password) ||
1793 (conf->psk_identity && !*conf->psk_identity) ||
1794 (conf->psk_password && !*conf->psk_password)) {
1795 radlog(L_ERR, "Invalid PSK Configuration: psk_identity or psk_password are empty");
1799 if (conf->psk_identity) {
1800 size_t psk_len, hex_len;
1801 char buffer[PSK_MAX_PSK_LEN];
1803 if (conf->certificate_file ||
1804 conf->private_key_password || conf->private_key_file ||
1805 conf->ca_file || conf->ca_path) {
1806 radlog(L_ERR, "When PSKs are used, No certificate configuration is permitted");
1811 SSL_CTX_set_psk_client_callback(ctx,
1812 psk_client_callback);
1814 SSL_CTX_set_psk_server_callback(ctx,
1815 psk_server_callback);
1818 psk_len = strlen(conf->psk_password);
1819 if (strlen(conf->psk_password) > (2 * PSK_MAX_PSK_LEN)) {
1820 radlog(L_ERR, "psk_hexphrase is too long (max %d)",
1825 hex_len = fr_hex2bin(conf->psk_password, buffer, psk_len);
1826 if (psk_len != (2 * hex_len)) {
1827 radlog(L_ERR, "psk_hexphrase is not all hex");
1834 client = client; /* -Wunused */
1838 * Load our keys and certificates
1840 * If certificates are of type PEM then we can make use
1841 * of cert chain authentication using openssl api call
1842 * SSL_CTX_use_certificate_chain_file. Please see how
1843 * the cert chain needs to be given in PEM from
1846 if (!conf->certificate_file) goto load_ca;
1848 if (type == SSL_FILETYPE_PEM) {
1849 if (!(SSL_CTX_use_certificate_chain_file(ctx, conf->certificate_file))) {
1850 radlog(L_ERR, "Error reading certificate file %s:%s",
1851 conf->certificate_file,
1852 ERR_error_string(ERR_get_error(), NULL));
1856 } else if (!(SSL_CTX_use_certificate_file(ctx, conf->certificate_file, type))) {
1857 radlog(L_ERR, "Error reading certificate file %s:%s",
1858 conf->certificate_file,
1859 ERR_error_string(ERR_get_error(), NULL));
1863 /* Load the CAs we trust */
1865 if (conf->ca_file || conf->ca_path) {
1866 if (!SSL_CTX_load_verify_locations(ctx, conf->ca_file, conf->ca_path)) {
1867 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
1868 radlog(L_ERR, "rlm_eap_tls: Error reading Trusted root CA list %s",conf->ca_file );
1872 if (conf->ca_file && *conf->ca_file) SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(conf->ca_file));
1874 if (conf->private_key_file) {
1875 if (!(SSL_CTX_use_PrivateKey_file(ctx, conf->private_key_file, type))) {
1876 radlog(L_ERR, "Failed reading private key file %s:%s",
1877 conf->private_key_file,
1878 ERR_error_string(ERR_get_error(), NULL));
1883 * Check if the loaded private key is the right one
1885 if (!SSL_CTX_check_private_key(ctx)) {
1886 radlog(L_ERR, "Private key does not match the certificate public key");
1891 #ifdef PSK_MAX_IDENTITY_LEN
1898 ctx_options |= SSL_OP_NO_SSLv2;
1899 ctx_options |= SSL_OP_NO_SSLv3;
1900 #ifdef SSL_OP_NO_TICKET
1901 ctx_options |= SSL_OP_NO_TICKET ;
1905 * SSL_OP_SINGLE_DH_USE must be used in order to prevent
1906 * small subgroup attacks and forward secrecy. Always
1909 * SSL_OP_SINGLE_DH_USE has an impact on the computer
1910 * time needed during negotiation, but it is not very
1913 ctx_options |= SSL_OP_SINGLE_DH_USE;
1916 * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS to work around issues
1917 * in Windows Vista client.
1918 * http://www.openssl.org/~bodo/tls-cbc.txt
1919 * http://www.nabble.com/(RADIATOR)-Radiator-Version-3.16-released-t2600070.html
1921 ctx_options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
1923 SSL_CTX_set_options(ctx, ctx_options);
1926 * TODO: Set the RSA & DH
1927 * SSL_CTX_set_tmp_rsa_callback(ctx, cbtls_rsa);
1928 * SSL_CTX_set_tmp_dh_callback(ctx, cbtls_dh);
1932 * set the message callback to identify the type of
1933 * message. For every new session, there can be a
1934 * different callback argument.
1936 * SSL_CTX_set_msg_callback(ctx, cbtls_msg);
1940 * Set eliptical curve crypto configuration.
1942 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
1943 #ifndef OPENSSL_NO_ECDH
1944 if (set_ecdh_curve(ctx, conf->ecdh_curve) < 0) {
1950 /* Set Info callback */
1951 SSL_CTX_set_info_callback(ctx, cbtls_info);
1954 * Callbacks, etc. for session resumption.
1956 if (conf->session_cache_enable) {
1957 SSL_CTX_sess_set_new_cb(ctx, cbtls_new_session);
1958 SSL_CTX_sess_set_get_cb(ctx, cbtls_get_session);
1959 SSL_CTX_sess_set_remove_cb(ctx, cbtls_remove_session);
1961 SSL_CTX_set_quiet_shutdown(ctx, 1);
1962 if (FR_TLS_EX_INDEX_VPS < 0)
1963 FR_TLS_EX_INDEX_VPS = SSL_SESSION_get_ex_new_index(0, NULL, NULL, NULL, sess_free_vps);
1967 * Check the certificates for revocation.
1969 #ifdef X509_V_FLAG_CRL_CHECK
1970 if (conf->check_crl) {
1971 certstore = SSL_CTX_get_cert_store(ctx);
1972 if (certstore == NULL) {
1973 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
1974 radlog(L_ERR, "rlm_eap_tls: Error reading Certificate Store");
1977 X509_STORE_set_flags(certstore, X509_V_FLAG_CRL_CHECK);
1983 * Always verify the peer certificate
1985 verify_mode |= SSL_VERIFY_PEER;
1986 verify_mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1987 verify_mode |= SSL_VERIFY_CLIENT_ONCE;
1988 SSL_CTX_set_verify(ctx, verify_mode, cbtls_verify);
1990 if (conf->verify_depth) {
1991 SSL_CTX_set_verify_depth(ctx, conf->verify_depth);
1994 /* Load randomness */
1995 if (!(RAND_load_file(conf->random_file, 1024*1024))) {
1996 radlog(L_ERR, "rlm_eap: SSL error %s", ERR_error_string(ERR_get_error(), NULL));
1997 radlog(L_ERR, "rlm_eap_tls: Error loading randomness");
2002 * Set the cipher list if we were told to
2004 if (conf->cipher_list) {
2005 if (!SSL_CTX_set_cipher_list(ctx, conf->cipher_list)) {
2006 radlog(L_ERR, "rlm_eap_tls: Error setting cipher list");
2012 * Setup session caching
2014 if (conf->session_cache_enable) {
2016 * Create a unique context Id per EAP-TLS configuration.
2018 if (conf->session_id_name) {
2019 snprintf(conf->session_context_id,
2020 sizeof(conf->session_context_id),
2022 conf->session_id_name);
2024 snprintf(conf->session_context_id,
2025 sizeof(conf->session_context_id),
2030 * Cache it, and DON'T auto-clear it.
2032 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | SSL_SESS_CACHE_NO_AUTO_CLEAR);
2034 SSL_CTX_set_session_id_context(ctx,
2035 (unsigned char *) conf->session_context_id,
2036 (unsigned int) strlen(conf->session_context_id));
2039 * Our timeout is in hours, this is in seconds.
2041 SSL_CTX_set_timeout(ctx, conf->session_timeout * 3600);
2044 * Set the maximum number of entries in the
2047 SSL_CTX_sess_set_cache_size(ctx, conf->session_cache_size);
2050 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
2057 void tls_server_conf_free(fr_tls_server_conf_t *conf)
2061 if (conf->cs) cf_section_parse_free(conf->cs, conf);
2063 if (conf->ctx) SSL_CTX_free(conf->ctx);
2065 #ifdef HAVE_OPENSSL_OCSP_H
2066 if (conf->ocsp_store) X509_STORE_free(conf->ocsp_store);
2067 conf->ocsp_store = NULL;
2070 memset(conf, 0, sizeof(*conf));
2075 fr_tls_server_conf_t *tls_server_conf_parse(CONF_SECTION *cs)
2077 fr_tls_server_conf_t *conf;
2079 conf = malloc(sizeof(*conf));
2081 radlog(L_ERR, "Out of memory");
2084 memset(conf, 0, sizeof(*conf));
2086 if (cf_section_parse(cs, conf, tls_server_config) < 0) {
2088 tls_server_conf_free(conf);
2093 * Save people from their own stupidity.
2095 if (conf->fragment_size < 100) conf->fragment_size = 100;
2098 * This magic makes the administrators life HUGELY easier
2099 * on initial deployments.
2101 * If the server starts up in debugging mode, AND the
2102 * bootstrap command is configured, AND it exists, AND
2103 * there is no server certificate
2105 if (conf->make_cert_command && (debug_flag >= 2)) {
2108 if ((stat(conf->make_cert_command, &buf) == 0) &&
2109 (stat(conf->certificate_file, &buf) < 0) &&
2110 (errno == ENOENT) &&
2111 (radius_exec_program(conf->make_cert_command, NULL, 1,
2112 NULL, 0, NULL, NULL, 0) != 0)) {
2117 if (!conf->private_key_file) {
2118 radlog(L_ERR, "TLS Server requires a private key file");
2122 if (!conf->certificate_file) {
2123 radlog(L_ERR, "TLS Server requires a certificate file");
2130 conf->ctx = init_tls_ctx(conf, 0);
2131 if (conf->ctx == NULL) {
2135 #ifdef HAVE_OPENSSL_OCSP_H
2137 * Initialize OCSP Revocation Store
2139 if (conf->ocsp_enable) {
2140 conf->ocsp_store = init_revocation_store(conf);
2141 if (conf->ocsp_store == NULL) goto error;
2143 #endif /*HAVE_OPENSSL_OCSP_H*/
2145 if (load_dh_params(conf->ctx, conf->dh_file) < 0) {
2149 if (generate_eph_rsa_key(conf->ctx) < 0) {
2153 if (conf->verify_tmp_dir) {
2154 if (chmod(conf->verify_tmp_dir, S_IRWXU) < 0) {
2155 radlog(L_ERR, "Failed changing permissions on %s: %s", conf->verify_tmp_dir, strerror(errno));
2160 if (conf->verify_client_cert_cmd && !conf->verify_tmp_dir) {
2161 radlog(L_ERR, "You MUST set the verify directory in order to use verify_client_cmd");
2168 fr_tls_server_conf_t *tls_client_conf_parse(CONF_SECTION *cs)
2170 fr_tls_server_conf_t *conf;
2172 conf = malloc(sizeof(*conf));
2174 radlog(L_ERR, "Out of memory");
2177 memset(conf, 0, sizeof(*conf));
2179 if (cf_section_parse(cs, conf, tls_client_config) < 0) {
2181 tls_server_conf_free(conf);
2186 * Save people from their own stupidity.
2188 if (conf->fragment_size < 100) conf->fragment_size = 100;
2193 conf->ctx = init_tls_ctx(conf, 1);
2194 if (conf->ctx == NULL) {
2198 if (load_dh_params(conf->ctx, conf->dh_file) < 0) {
2202 if (generate_eph_rsa_key(conf->ctx) < 0) {
2209 int tls_success(tls_session_t *ssn, REQUEST *request)
2211 VALUE_PAIR *vp, *vps = NULL;
2212 fr_tls_server_conf_t *conf;
2214 conf = (fr_tls_server_conf_t *)SSL_get_ex_data(ssn->ssl, FR_TLS_EX_INDEX_CONF);
2215 rad_assert(conf != NULL);
2218 * If there's no session resumption, delete the entry
2219 * from the cache. This means either it's disabled
2220 * globally for this SSL context, OR we were told to
2221 * disable it for this user.
2223 * This also means you can't turn it on just for one
2226 if ((!ssn->allow_session_resumption) ||
2227 (((vp = pairfind(request->config_items, 1127, 0)) != NULL) &&
2228 (vp->vp_integer == 0))) {
2229 SSL_CTX_remove_session(ssn->ctx,
2231 ssn->allow_session_resumption = 0;
2234 * If we're in a resumed session and it's
2237 if (SSL_session_reused(ssn->ssl)) {
2238 RDEBUG("FAIL: Forcibly stopping session resumption as it is not allowed.");
2243 * Else resumption IS allowed, so we store the
2244 * user data in the cache.
2246 } else if (!SSL_session_reused(ssn->ssl)) {
2248 char buffer[2 * MAX_SESSION_SIZE + 1];
2250 size = ssn->ssl->session->session_id_length;
2251 if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;
2253 fr_bin2hex(ssn->ssl->session->session_id, buffer, size);
2256 vp = paircopy2(request->reply->vps, PW_USER_NAME, 0);
2257 if (vp) pairadd(&vps, vp);
2259 vp = paircopy2(request->packet->vps, PW_STRIPPED_USER_NAME, 0);
2260 if (vp) pairadd(&vps, vp);
2262 vp = paircopy2(request->reply->vps, PW_CACHED_SESSION_POLICY, 0);
2263 if (vp) pairadd(&vps, vp);
2266 RDEBUG2("Saving session %s vps %p in the cache", buffer, vps);
2267 SSL_SESSION_set_ex_data(ssn->ssl->session,
2268 FR_TLS_EX_INDEX_VPS, vps);
2270 RDEBUG2("WARNING: No information to cache: session caching will be disabled for session %s", buffer);
2271 SSL_CTX_remove_session(ssn->ctx,
2276 * Else the session WAS allowed. Copy the cached
2281 char buffer[2 * MAX_SESSION_SIZE + 1];
2283 size = ssn->ssl->session->session_id_length;
2284 if (size > MAX_SESSION_SIZE) size = MAX_SESSION_SIZE;
2286 fr_bin2hex(ssn->ssl->session->session_id, buffer, size);
2289 vp = SSL_SESSION_get_ex_data(ssn->ssl->session,
2290 FR_TLS_EX_INDEX_VPS);
2292 RDEBUG("WARNING: No information in cached session %s", buffer);
2296 RDEBUG("Adding cached attributes for session %s vps %p to the reply:", buffer, vp);
2297 debug_pair_list(vp);
2298 pairadd(&request->reply->vps, paircopy(vp));
2301 * Mark the request as resumed.
2303 vp = pairmake("EAP-Session-Resumed", "1", T_OP_SET);
2304 if (vp) pairadd(&request->packet->vps, vp);
2312 void tls_fail(tls_session_t *ssn)
2315 * Force the session to NOT be cached.
2317 SSL_CTX_remove_session(ssn->ctx, ssn->ssl->session);
2320 fr_tls_status_t tls_application_data(tls_session_t *ssn,
2327 * Decrypt the complete record.
2329 err = BIO_write(ssn->into_ssl, ssn->dirty_in.data,
2330 ssn->dirty_in.used);
2331 if (err != (int) ssn->dirty_in.used) {
2332 record_init(&ssn->dirty_in);
2333 RDEBUG("Failed writing %d to SSL BIO: %d",
2334 ssn->dirty_in.used, err);
2339 * Clear the dirty buffer now that we are done with it
2340 * and init the clean_out buffer to store decrypted data
2342 record_init(&ssn->dirty_in);
2343 record_init(&ssn->clean_out);
2346 * Read (and decrypt) the tunneled data from the
2347 * SSL session, and put it into the decrypted
2350 err = SSL_read(ssn->ssl, ssn->clean_out.data,
2351 sizeof(ssn->clean_out.data));
2356 RDEBUG("SSL_read Error");
2358 code = SSL_get_error(ssn->ssl, err);
2360 case SSL_ERROR_WANT_READ:
2361 return FR_TLS_MORE_FRAGMENTS;
2362 DEBUG("Error in fragmentation logic: SSL_WANT_READ");
2365 case SSL_ERROR_WANT_WRITE:
2366 DEBUG("Error in fragmentation logic: SSL_WANT_WRITE");
2370 DEBUG("Error in fragmentation logic: ?");
2373 * FIXME: Call int_ssl_check?
2381 RDEBUG("WARNING: No data inside of the tunnel.");
2385 * Passed all checks, successfully decrypted data
2387 ssn->clean_out.used = err;
2394 * Acknowledge received is for one of the following messages sent earlier
2395 * 1. Handshake completed Message, so now send, EAP-Success
2396 * 2. Alert Message, now send, EAP-Failure
2397 * 3. Fragment Message, now send, next Fragment
2399 fr_tls_status_t tls_ack_handler(tls_session_t *ssn, REQUEST *request)
2401 RDEBUG2("Received TLS ACK");
2404 radlog_request(L_ERR, 0, request, "FAIL: Unexpected ACK received. Could not obtain session information.");
2405 return FR_TLS_INVALID;
2407 if (ssn->info.initialized == 0) {
2408 RDEBUG("No SSL info available. Waiting for more SSL data.");
2409 return FR_TLS_REQUEST;
2411 if ((ssn->info.content_type == handshake) &&
2412 (ssn->info.origin == 0)) {
2413 radlog_request(L_ERR, 0, request, "FAIL: ACK without earlier message.");
2414 return FR_TLS_INVALID;
2417 switch (ssn->info.content_type) {
2419 RDEBUG2("ACK alert");
2423 if ((ssn->info.handshake_type == finished) &&
2424 (ssn->dirty_out.used == 0)) {
2425 RDEBUG2("ACK handshake is finished");
2428 * From now on all the content is
2429 * application data set it here as nobody else
2432 ssn->info.content_type = application_data;
2433 return FR_TLS_SUCCESS;
2434 } /* else more data to send */
2436 RDEBUG2("ACK handshake fragment handler");
2437 /* Fragmentation handler, send next fragment */
2438 return FR_TLS_REQUEST;
2440 case application_data:
2441 RDEBUG2("ACK handshake fragment handler in application data");
2442 return FR_TLS_REQUEST;
2445 * For the rest of the conditions, switch over
2446 * to the default section below.
2449 RDEBUG2("ACK default");
2450 radlog_request(L_ERR, 0, request, "Invalid ACK received: %d",
2451 ssn->info.content_type);
2452 return FR_TLS_INVALID;
2456 #endif /* WITH_TLS */