2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * tls.c TLS support functions
22 * Author: Landon Fuller <landonf@threerings.net>
24 * This file was contributed to the Bacula project by Landon Fuller
25 * and Three Rings Design, Inc.
27 * Three Rings Design, Inc. has been granted a perpetual, worldwide,
28 * non-exclusive, no-charge, royalty-free, irrevocable copyright
29 * license to reproduce, prepare derivative works of, publicly
30 * display, publicly perform, sublicense, and distribute the original
31 * work contributed by Three Rings Design, Inc. and its employees to
32 * the Bacula project in source or object form.
34 * If you wish to license contributions from Three Rings Design, Inc,
35 * under an alternate open source license please contact
36 * Landon Fuller <landonf@threerings.net>.
44 #ifdef HAVE_TLS /* Is TLS enabled? */
46 #ifdef HAVE_OPENSSL /* How about OpenSSL? */
48 #include "openssl-compat.h"
50 /* No anonymous ciphers, no <128 bit ciphers, no export ciphers, no MD5 ciphers */
51 #define TLS_DEFAULT_CIPHERS "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"
53 /* TLS Context Structure */
56 CRYPTO_PEM_PASSWD_CB *pem_callback;
57 const void *pem_userdata;
62 struct TLS_Connection {
64 pthread_mutex_t wlock; /* make openssl_bsock_readwrite() atomic when writing */
65 pthread_mutex_t rwlock; /* only one SSL_read() or SSL_write() at a time */
69 * OpenSSL certificate verification callback.
70 * OpenSSL has already performed internal certificate verification.
71 * We just report any errors that occured.
73 static int openssl_verify_peer(int ok, X509_STORE_CTX *store)
76 X509 *cert = X509_STORE_CTX_get_current_cert(store);
77 int depth = X509_STORE_CTX_get_error_depth(store);
78 int err = X509_STORE_CTX_get_error(store);
82 X509_NAME_oneline(X509_get_issuer_name(cert), issuer, 256);
83 X509_NAME_oneline(X509_get_subject_name(cert), subject, 256);
85 Jmsg5(NULL, M_ERROR, 0, _("Error with certificate at depth: %d, issuer = %s,"
86 " subject = %s, ERR=%d:%s\n"), depth, issuer,
87 subject, err, X509_verify_cert_error_string(err));
94 /* Dispatch user PEM encryption callbacks */
95 static int tls_pem_callback_dispatch (char *buf, int size, int rwflag, void *userdata)
97 TLS_CONTEXT *ctx = (TLS_CONTEXT *)userdata;
98 return (ctx->pem_callback(buf, size, ctx->pem_userdata));
102 * Create a new TLS_CONTEXT instance.
103 * Returns: Pointer to TLS_CONTEXT instance on success
106 TLS_CONTEXT *new_tls_context(const char *ca_certfile, const char *ca_certdir,
107 const char *certfile, const char *keyfile,
108 CRYPTO_PEM_PASSWD_CB *pem_callback,
109 const void *pem_userdata, const char *dhfile,
116 ctx = (TLS_CONTEXT *)malloc(sizeof(TLS_CONTEXT));
118 /* Allocate our OpenSSL TLS Context */
119 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
120 /* Allows SSLv3, TLSv1, TLSv1.1 and TLSv1.2 protocols */
121 ctx->openssl = SSL_CTX_new(TLS_method());
124 /* Allows most all protocols */
125 ctx->openssl = SSL_CTX_new(SSLv23_method());
129 /* Use SSL_OP_ALL to turn on all "rather harmless" workarounds that
132 SSL_CTX_set_options(ctx->openssl, SSL_OP_ALL);
134 /* Now disable old broken SSLv3 and SSLv2 protocols */
135 SSL_CTX_set_options(ctx->openssl, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
138 openssl_post_errors(M_FATAL, _("Error initializing SSL context"));
142 /* Set up pem encryption callback */
144 ctx->pem_callback = pem_callback;
145 ctx->pem_userdata = pem_userdata;
147 ctx->pem_callback = crypto_default_pem_callback;
148 ctx->pem_userdata = NULL;
150 SSL_CTX_set_default_passwd_cb(ctx->openssl, tls_pem_callback_dispatch);
151 SSL_CTX_set_default_passwd_cb_userdata(ctx->openssl, (void *) ctx);
154 * Set certificate verification paths. This requires that at least one
157 if (ca_certfile || ca_certdir) {
158 if (!SSL_CTX_load_verify_locations(ctx->openssl, ca_certfile, ca_certdir)) {
159 openssl_post_errors(M_FATAL, _("Error loading certificate verification stores"));
162 } else if (verify_peer) {
163 /* At least one CA is required for peer verification */
164 Jmsg0(NULL, M_ERROR, 0, _("Either a certificate file or a directory must be"
165 " specified as a verification store\n"));
170 * Load our certificate file, if available. This file may also contain a
171 * private key, though this usage is somewhat unusual.
174 if (!SSL_CTX_use_certificate_chain_file(ctx->openssl, certfile)) {
175 openssl_post_errors(M_FATAL, _("Error loading certificate file"));
180 /* Load our private key. */
182 if (!SSL_CTX_use_PrivateKey_file(ctx->openssl, keyfile, SSL_FILETYPE_PEM)) {
183 openssl_post_errors(M_FATAL, _("Error loading private key"));
188 /* Load Diffie-Hellman Parameters. */
190 if (!(bio = BIO_new_file(dhfile, "r"))) {
191 openssl_post_errors(M_FATAL, _("Unable to open DH parameters file"));
194 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
197 openssl_post_errors(M_FATAL, _("Unable to load DH parameters from specified file"));
200 if (!SSL_CTX_set_tmp_dh(ctx->openssl, dh)) {
201 openssl_post_errors(M_FATAL, _("Failed to set TLS Diffie-Hellman parameters"));
205 /* Enable Single-Use DH for Ephemeral Keying */
206 SSL_CTX_set_options(ctx->openssl, SSL_OP_SINGLE_DH_USE);
209 if (SSL_CTX_set_cipher_list(ctx->openssl, TLS_DEFAULT_CIPHERS) != 1) {
210 Jmsg0(NULL, M_ERROR, 0,
211 _("Error setting cipher list, no valid ciphers available\n"));
215 /* Verify Peer Certificate */
217 /* SSL_VERIFY_FAIL_IF_NO_PEER_CERT has no effect in client mode */
218 SSL_CTX_set_verify(ctx->openssl,
219 SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
220 openssl_verify_peer);
226 /* Clean up after ourselves */
228 SSL_CTX_free(ctx->openssl);
235 * Free TLS_CONTEXT instance
237 void free_tls_context(TLS_CONTEXT *ctx)
239 SSL_CTX_free(ctx->openssl);
243 bool get_tls_require(TLS_CONTEXT *ctx)
245 return ctx->tls_require;
248 bool get_tls_enable(TLS_CONTEXT *ctx)
250 return ctx->tls_enable;
255 * Verifies a list of common names against the certificate
256 * commonName attribute.
257 * Returns: true on success
260 bool tls_postconnect_verify_cn(JCR *jcr, TLS_CONNECTION *tls, alist *verify_list)
262 SSL *ssl = tls->openssl;
265 bool auth_success = false;
268 /* Check if peer provided a certificate */
269 if (!(cert = SSL_get_peer_certificate(ssl))) {
270 Qmsg0(jcr, M_ERROR, 0, _("Peer failed to present a TLS certificate\n"));
274 if ((subject = X509_get_subject_name(cert)) != NULL) {
275 if (X509_NAME_get_text_by_NID(subject, NID_commonName, data, sizeof(data)) > 0) {
277 /* NULL terminate data */
280 /* Try all the CNs in the list */
281 foreach_alist(cn, verify_list) {
282 if (strcasecmp(data, cn) == 0) {
294 * Verifies a peer's hostname against the subjectAltName and commonName
296 * Returns: true on success
299 bool tls_postconnect_verify_host(JCR *jcr, TLS_CONNECTION *tls, const char *host)
301 SSL *ssl = tls->openssl;
304 bool auth_success = false;
307 const char *pval, *phost;
310 X509_NAME_ENTRY *neCN;
313 /* Check if peer provided a certificate */
314 if (!(cert = SSL_get_peer_certificate(ssl))) {
315 Qmsg1(jcr, M_ERROR, 0,
316 _("Peer %s failed to present a TLS certificate\n"), host);
317 Dmsg1(250, _("Peer %s failed to present a TLS certificate\n"), host);
321 /* Check subjectAltName extensions first */
322 if ((extensions = X509_get_ext_count(cert)) > 0) {
323 for (i = 0; i < extensions; i++) {
327 ext = X509_get_ext(cert, i);
328 extname = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext)));
330 if (strcmp(extname, "subjectAltName") == 0) {
331 #ifdef HAVE_OPENSSLv1
332 const X509V3_EXT_METHOD *method;
334 X509V3_EXT_METHOD *method;
336 STACK_OF(CONF_VALUE) *val;
339 const unsigned char *ext_value_data;
341 /* Get x509 extension method structure */
342 if (!(method = X509V3_EXT_get(ext))) {
346 ext_value_data = ext->value->data;
351 /* Decode ASN1 item in data */
352 extstr = ASN1_item_d2i(NULL, &ext_value_data, ext->value->length,
353 ASN1_ITEM_ptr(method->it));
357 /* Decode ASN1 item in data */
358 extstr = method->d2i(NULL, &ext_value_data, ext->value->length);
361 /* Iterate through to find the dNSName field(s) */
362 val = method->i2v(method, extstr, NULL);
364 /* dNSName shortname is "DNS" */
365 Dmsg0(250, "Check DNS name\n");
366 for (j = 0; j < sk_CONF_VALUE_num(val); j++) {
367 nval = sk_CONF_VALUE_value(val, j);
368 if (strcmp(nval->name, "DNS") == 0) {
369 if (strncasecmp(nval->value, "*.", 2) == 0) {
370 Dmsg0(250, "Wildcard Certificate\n");
371 pval = strstr(nval->value, ".");
372 phost = strstr(host, ".");
373 if (pval && phost && (strcasecmp(pval, phost) == 0)) {
377 } else if (strcasecmp(nval->value, host) == 0) {
381 Dmsg2(250, "No DNS name match. Host=%s cert=%s\n", host, nval->value);
388 /* Try verifying against the subject name */
390 Dmsg0(250, "Check subject name name\n");
391 if ((subject = X509_get_subject_name(cert)) != NULL) {
392 /* Loop through all CNs */
394 cnLastPos = X509_NAME_get_index_by_NID(subject, NID_commonName, cnLastPos);
395 if (cnLastPos == -1) {
398 neCN = X509_NAME_get_entry(subject, cnLastPos);
399 asn1CN = X509_NAME_ENTRY_get_data(neCN);
400 if (strncasecmp((const char*)asn1CN->data, "*.", 2) == 0) {
401 /* wildcard certificate */
402 Dmsg0(250, "Wildcard Certificate\n");
403 pval = strstr((const char*)asn1CN->data, ".");
404 phost = strstr(host, ".");
405 if (pval && phost && (strcasecmp(pval, phost) == 0)) {
409 } else if (strcasecmp((const char*)asn1CN->data, host) == 0) {
413 Dmsg2(250, "No subject name match. Host=%s cert=%s\n", host, (const char*)asn1CN->data);
424 * Create a new TLS_CONNECTION instance.
426 * Returns: Pointer to TLS_CONNECTION instance on success
429 TLS_CONNECTION *new_tls_connection(TLS_CONTEXT *ctx, int fd)
434 * Create a new BIO and assign the fd.
435 * The caller will remain responsible for closing the associated fd
437 bio = BIO_new(BIO_s_socket());
439 /* Not likely, but never say never */
440 openssl_post_errors(M_FATAL, _("Error creating file descriptor-based BIO"));
441 return NULL; /* Nothing allocated, nothing to clean up */
443 BIO_set_fd(bio, fd, BIO_NOCLOSE);
445 /* Allocate our new tls connection */
446 TLS_CONNECTION *tls = (TLS_CONNECTION *)malloc(sizeof(TLS_CONNECTION));
448 /* Create the SSL object and attach the socket BIO */
449 if ((tls->openssl = SSL_new(ctx->openssl)) == NULL) {
450 /* Not likely, but never say never */
451 openssl_post_errors(M_FATAL, _("Error creating new SSL object"));
455 SSL_set_bio(tls->openssl, bio, bio);
457 /* Non-blocking partial writes */
458 SSL_set_mode(tls->openssl, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
460 pthread_mutex_init(&tls->wlock, NULL);
461 pthread_mutex_init(&tls->rwlock, NULL);
468 SSL_free(tls->openssl);
474 * Free TLS_CONNECTION instance
476 void free_tls_connection(TLS_CONNECTION *tls)
478 pthread_mutex_destroy(&tls->rwlock);
479 pthread_mutex_destroy(&tls->wlock);
480 SSL_free(tls->openssl);
484 /* Does all the manual labor for tls_bsock_accept() and tls_bsock_connect() */
485 static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server)
487 TLS_CONNECTION *tls = bsock->tls;
492 /* Ensure that socket is non-blocking */
493 flags = bsock->set_nonblocking();
496 bsock->timer_start = watchdog_time;
497 bsock->clear_timed_out();
498 bsock->set_killable(false);
502 err = SSL_accept(tls->openssl);
504 err = SSL_connect(tls->openssl);
508 switch (SSL_get_error(tls->openssl, err)) {
512 case SSL_ERROR_ZERO_RETURN:
513 /* TLS connection was cleanly shut down */
514 openssl_post_errors(bsock->get_jcr(), M_FATAL, _("Connect failure"));
517 case SSL_ERROR_WANT_READ:
518 /* Block until we can read */
519 fd_wait_data(bsock->m_fd, WAIT_READ, 10, 0);
521 case SSL_ERROR_WANT_WRITE:
522 /* Block until we can write */
523 fd_wait_data(bsock->m_fd, WAIT_WRITE, 10, 0);
526 /* Socket Error Occurred */
527 openssl_post_errors(bsock->get_jcr(), M_FATAL, _("Connect failure"));
532 if (bsock->is_timed_out()) {
538 /* Restore saved flags */
539 bsock->restore_blocking(flags);
541 bsock->timer_start = 0;
542 bsock->set_killable(true);
548 * Initiates a TLS connection with the server.
549 * Returns: true on success
552 bool tls_bsock_connect(BSOCK *bsock)
554 /* SSL_connect(bsock->tls) */
555 return openssl_bsock_session_start(bsock, false);
559 * Listens for a TLS connection from a client.
560 * Returns: true on success
563 bool tls_bsock_accept(BSOCK *bsock)
565 /* SSL_accept(bsock->tls) */
566 return openssl_bsock_session_start(bsock, true);
570 * Shutdown TLS_CONNECTION instance
572 void tls_bsock_shutdown(BSOCK *bsock)
575 * SSL_shutdown must be called twice to fully complete the process -
576 * The first time to initiate the shutdown handshake, and the second to
577 * receive the peer's reply.
579 * In addition, if the underlying socket is blocking, SSL_shutdown()
580 * will not return until the current stage of the shutdown process has
581 * completed or an error has occured. By setting the socket blocking
582 * we can avoid the ugly for()/switch()/select() loop.
588 /* Set socket blocking for shutdown */
589 bsock->set_blocking();
591 tid = start_bsock_timer(bsock, 60 * 2);
592 err = SSL_shutdown(bsock->tls->openssl);
593 stop_bsock_timer(tid);
595 /* Complete shutdown */
596 tid = start_bsock_timer(bsock, 60 * 2);
597 err = SSL_shutdown(bsock->tls->openssl);
598 stop_bsock_timer(tid);
602 switch (SSL_get_error(bsock->tls->openssl, err)) {
605 case SSL_ERROR_ZERO_RETURN:
606 /* TLS connection was shut down on us via a TLS protocol-level closure */
607 openssl_post_errors(bsock->get_jcr(), M_ERROR, _("TLS shutdown failure."));
610 /* Socket Error Occurred */
611 openssl_post_errors(bsock->get_jcr(), M_ERROR, _("TLS shutdown failure."));
616 /* Does all the manual labor for tls_bsock_readn() and tls_bsock_writen() */
617 static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, bool write)
619 TLS_CONNECTION *tls = bsock->tls;
624 /* Ensure that socket is non-blocking */
625 flags = bsock->set_nonblocking();
628 bsock->timer_start = watchdog_time;
629 bsock->clear_timed_out();
630 bsock->set_killable(false);
635 pthread_mutex_lock(&tls->wlock);
639 pthread_mutex_lock(&tls->rwlock);
641 nwritten = SSL_write(tls->openssl, ptr, nleft);
643 nwritten = SSL_read(tls->openssl, ptr, nleft);
645 pthread_mutex_unlock(&tls->rwlock);
648 switch (SSL_get_error(tls->openssl, nwritten)) {
656 case SSL_ERROR_SYSCALL:
657 if (nwritten == -1) {
658 if (errno == EINTR) {
661 if (errno == EAGAIN) {
662 bmicrosleep(0, 20000); /* try again in 20 ms */
666 openssl_post_errors(bsock->get_jcr(), M_FATAL, _("TLS read/write failure."));
669 case SSL_ERROR_WANT_READ:
670 /* Block until we can read */
671 fd_wait_data(bsock->m_fd, WAIT_READ, 10, 0);
674 case SSL_ERROR_WANT_WRITE:
675 /* Block until we can read */
676 fd_wait_data(bsock->m_fd, WAIT_WRITE, 10, 0);
679 case SSL_ERROR_ZERO_RETURN:
680 /* TLS connection was cleanly shut down */
681 /* Fall through wanted */
683 /* Socket Error Occured */
684 openssl_post_errors(bsock->get_jcr(), M_FATAL, _("TLS read/write failure."));
688 /* Everything done? */
693 /* Timeout/Termination, let's take what we can get */
694 if (bsock->is_timed_out() || bsock->is_terminated()) {
701 pthread_mutex_unlock(&tls->wlock);
703 /* Restore saved flags */
704 bsock->restore_blocking(flags);
707 bsock->timer_start = 0;
708 bsock->set_killable(true);
709 return nbytes - nleft;
713 int tls_bsock_writen(BSOCK *bsock, char *ptr, int32_t nbytes)
715 /* SSL_write(bsock->tls->openssl, ptr, nbytes) */
716 return openssl_bsock_readwrite(bsock, ptr, nbytes, true);
719 int tls_bsock_readn(BSOCK *bsock, char *ptr, int32_t nbytes)
721 /* SSL_read(bsock->tls->openssl, ptr, nbytes) */
722 return openssl_bsock_readwrite(bsock, ptr, nbytes, false);
725 /* test if 4 bytes can be read without "blocking" */
726 bool tls_bsock_probe(BSOCK *bsock)
729 return SSL_peek(bsock->tls->openssl, &pktsiz, sizeof(pktsiz))==sizeof(pktsiz);
733 #else /* HAVE_OPENSSL */
734 # error No TLS implementation available.
735 #endif /* !HAVE_OPENSSL */
738 #else /* TLS NOT enabled, dummy routines substituted */
742 TLS_CONTEXT *new_tls_context(const char *ca_certfile, const char *ca_certdir,
743 const char *certfile, const char *keyfile,
744 CRYPTO_PEM_PASSWD_CB *pem_callback,
745 const void *pem_userdata, const char *dhfile,
750 void free_tls_context(TLS_CONTEXT *ctx) { }
752 void tls_bsock_shutdown(BSOCK *bsock) { }
754 void free_tls_connection(TLS_CONNECTION *tls) { }
756 bool get_tls_require(TLS_CONTEXT *ctx)
761 bool get_tls_enable(TLS_CONTEXT *ctx)
766 #endif /* HAVE_TLS */