]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/tls.c
crypto: remove support for ancient openssl
[bacula/bacula] / bacula / src / lib / tls.c
index 42c542aee4a14424191fd4244f2869ad1cdd9302..992265b38ad22069f4274a3cac048175bdd3dc92 100644 (file)
@@ -1,17 +1,20 @@
 /*
-   Bacula® - The Network Backup Solution
+   Bacula(R) - The Network Backup Solution
 
-   Copyright (C) 2005-2014 Free Software Foundation Europe e.V.
+   Copyright (C) 2000-2017 Kern Sibbald
 
-   The main author of Bacula is Kern Sibbald, with contributions from many
-   others, a complete list can be found in the file AUTHORS.
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
 
    You may use this file and others of this release according to the
    license defined in the LICENSE file, which includes the Affero General
    Public License, v3.0 ("AGPLv3") and some additional permissions and
    terms pursuant to its AGPLv3 Section 7.
 
-   Bacula® is a registered trademark of Kern Sibbald.
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
 */
 /*
  * tls.c TLS support functions
@@ -56,6 +59,8 @@ struct TLS_Context {
 
 struct TLS_Connection {
    SSL *openssl;
+   pthread_mutex_t wlock;  /* make openssl_bsock_readwrite() atomic when writing */
+   pthread_mutex_t rwlock; /* only one SSL_read() or SSL_write() at a time */
 };
 
 /*
@@ -113,13 +118,10 @@ TLS_CONTEXT *new_tls_context(const char *ca_certfile, const char *ca_certdir,
    /* Allows SSLv3, TLSv1, TLSv1.1 and TLSv1.2 protocols */
    ctx->openssl = SSL_CTX_new(TLS_method());
 
-#elif (OPENSSL_VERSION_NUMBER >= 0x10000000L)
+#else
    /* Allows most all protocols */
    ctx->openssl = SSL_CTX_new(SSLv23_method());
 
-#else
-   /* Older method only understands TLSv1 */
-   ctx->openssl = SSL_CTX_new(TLSv1_method());
 #endif
 
    /* Use SSL_OP_ALL to turn on all "rather harmless" workarounds that
@@ -332,11 +334,7 @@ bool tls_postconnect_verify_host(JCR *jcr, TLS_CONNECTION *tls, const char *host
             STACK_OF(CONF_VALUE) *val;
             CONF_VALUE *nval;
             void *extstr = NULL;
-#if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
             const unsigned char *ext_value_data;
-#else
-            unsigned char *ext_value_data;
-#endif
 
             /* Get x509 extension method structure */
             if (!(method = X509V3_EXT_get(ext))) {
@@ -345,7 +343,6 @@ bool tls_postconnect_verify_host(JCR *jcr, TLS_CONNECTION *tls, const char *host
 
             ext_value_data = ext->value->data;
 
-#if (OPENSSL_VERSION_NUMBER > 0x00907000L)
             if (method->it) {
                /* New style ASN1 */
 
@@ -359,10 +356,6 @@ bool tls_postconnect_verify_host(JCR *jcr, TLS_CONNECTION *tls, const char *host
                extstr = method->d2i(NULL, &ext_value_data, ext->value->length);
             }
 
-#else
-            extstr = method->d2i(NULL, &ext_value_data, ext->value->length);
-#endif
-
             /* Iterate through to find the dNSName field(s) */
             val = method->i2v(method, extstr, NULL);
 
@@ -462,6 +455,9 @@ TLS_CONNECTION *new_tls_connection(TLS_CONTEXT *ctx, int fd)
    /* Non-blocking partial writes */
    SSL_set_mode(tls->openssl, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
 
+   pthread_mutex_init(&tls->wlock, NULL);
+   pthread_mutex_init(&tls->rwlock, NULL);
+
    return tls;
 
 err:
@@ -477,6 +473,8 @@ err:
  */
 void free_tls_connection(TLS_CONNECTION *tls)
 {
+   pthread_mutex_destroy(&tls->rwlock);
+   pthread_mutex_destroy(&tls->wlock);
    SSL_free(tls->openssl);
    free(tls);
 }
@@ -486,14 +484,8 @@ static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server)
 {
    TLS_CONNECTION *tls = bsock->tls;
    int err;
-   int fdmax, flags;
+   int flags;
    int stat = true;
-   fd_set fdset;
-   struct timeval tv;
-
-   /* Zero the fdset, we'll set our fd prior to each invocation of select() */
-   FD_ZERO(&fdset);
-   fdmax = bsock->m_fd + 1;
 
    /* Ensure that socket is non-blocking */
    flags = bsock->set_nonblocking();
@@ -521,22 +513,12 @@ static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server)
          stat = false;
          goto cleanup;
       case SSL_ERROR_WANT_READ:
-         /* If we timeout of a select, this will be unset */
-         FD_SET((unsigned) bsock->m_fd, &fdset);
-         /* Set our timeout */
-         tv.tv_sec = 10;
-         tv.tv_usec = 0;
          /* Block until we can read */
-         select(fdmax, &fdset, NULL, NULL, &tv);
+         fd_wait_data(bsock->m_fd, WAIT_READ, 10, 0);
          break;
       case SSL_ERROR_WANT_WRITE:
-         /* If we timeout of a select, this will be unset */
-         FD_SET((unsigned) bsock->m_fd, &fdset);
-         /* Set our timeout */
-         tv.tv_sec = 10;
-         tv.tv_usec = 0;
          /* Block until we can write */
-         select(fdmax, NULL, &fdset, NULL, &tv);
+         fd_wait_data(bsock->m_fd, WAIT_WRITE, 10, 0);
          break;
       default:
          /* Socket Error Occurred */
@@ -633,16 +615,10 @@ void tls_bsock_shutdown(BSOCK *bsock)
 static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, bool write)
 {
    TLS_CONNECTION *tls = bsock->tls;
-   int fdmax, flags;
-   fd_set fdset;
-   struct timeval tv;
+   int flags;
    int nleft = 0;
    int nwritten = 0;
 
-   /* Zero the fdset, we'll set our fd prior to each invocation of select() */
-   FD_ZERO(&fdset);
-   fdmax = bsock->m_fd + 1;
-
    /* Ensure that socket is non-blocking */
    flags = bsock->set_nonblocking();
 
@@ -653,12 +629,18 @@ static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, b
 
    nleft = nbytes;
 
+   if (write) {
+      pthread_mutex_lock(&tls->wlock);
+   }
    while (nleft > 0) {
+
+      pthread_mutex_lock(&tls->rwlock);
       if (write) {
          nwritten = SSL_write(tls->openssl, ptr, nleft);
       } else {
          nwritten = SSL_read(tls->openssl, ptr, nleft);
       }
+      pthread_mutex_unlock(&tls->rwlock);
 
       /* Handle errors */
       switch (SSL_get_error(tls->openssl, nwritten)) {
@@ -683,21 +665,13 @@ static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, b
          goto cleanup;
 
       case SSL_ERROR_WANT_READ:
-         /* If we timeout on a select, this will be unset */
-         FD_SET((unsigned)bsock->m_fd, &fdset);
-         tv.tv_sec = 10;
-         tv.tv_usec = 0;
          /* Block until we can read */
-         select(fdmax, &fdset, NULL, NULL, &tv);
+         fd_wait_data(bsock->m_fd, WAIT_READ, 10, 0);
          break;
 
       case SSL_ERROR_WANT_WRITE:
-         /* If we timeout on a select, this will be unset */
-         FD_SET((unsigned)bsock->m_fd, &fdset);
-         tv.tv_sec = 10;
-         tv.tv_usec = 0;
-         /* Block until we can write */
-         select(fdmax, NULL, &fdset, NULL, &tv);
+         /* Block until we can read */
+         fd_wait_data(bsock->m_fd, WAIT_WRITE, 10, 0);
          break;
 
       case SSL_ERROR_ZERO_RETURN:
@@ -721,6 +695,9 @@ static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, b
    }
 
 cleanup:
+   if (write) {
+      pthread_mutex_unlock(&tls->wlock);
+   }
    /* Restore saved flags */
    bsock->restore_blocking(flags);
 
@@ -743,6 +720,14 @@ int tls_bsock_readn(BSOCK *bsock, char *ptr, int32_t nbytes)
    return openssl_bsock_readwrite(bsock, ptr, nbytes, false);
 }
 
+/* test if 4 bytes can be read without "blocking" */
+bool tls_bsock_probe(BSOCK *bsock)
+{
+   int32_t pktsiz;
+   return SSL_peek(bsock->tls->openssl, &pktsiz, sizeof(pktsiz))==sizeof(pktsiz);
+}
+
+
 #else /* HAVE_OPENSSL */
 # error No TLS implementation available.
 #endif /* !HAVE_OPENSSL */