]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tls.c
32d4c756de5cb915fdef0d993516350e843f2c20
[bacula/bacula] / bacula / src / lib / tls.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2005-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
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.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  * tls.c TLS support functions
18  *
19  * Author: Landon Fuller <landonf@threerings.net>
20  *
21  * This file was contributed to the Bacula project by Landon Fuller
22  * and Three Rings Design, Inc.
23  *
24  * Three Rings Design, Inc. has been granted a perpetual, worldwide,
25  * non-exclusive, no-charge, royalty-free, irrevocable copyright
26  * license to reproduce, prepare derivative works of, publicly
27  * display, publicly perform, sublicense, and distribute the original
28  * work contributed by Three Rings Design, Inc. and its employees to
29  * the Bacula project in source or object form.
30  *
31  * If you wish to license contributions from Three Rings Design, Inc,
32  * under an alternate open source license please contact
33  * Landon Fuller <landonf@threerings.net>.
34  */
35
36
37 #include "bacula.h"
38 #include <assert.h>
39
40
41 #ifdef HAVE_TLS /* Is TLS enabled? */
42
43 #ifdef HAVE_OPENSSL /* How about OpenSSL? */
44
45 /* No anonymous ciphers, no <128 bit ciphers, no export ciphers, no MD5 ciphers */
46 #define TLS_DEFAULT_CIPHERS "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"
47
48 /* TLS Context Structure */
49 struct TLS_Context {
50    SSL_CTX *openssl;
51    CRYPTO_PEM_PASSWD_CB *pem_callback;
52    const void *pem_userdata;
53    bool tls_enable;
54    bool tls_require;
55 };
56
57 struct TLS_Connection {
58    SSL *openssl;
59 };
60
61 /*
62  * OpenSSL certificate verification callback.
63  * OpenSSL has already performed internal certificate verification.
64  * We just report any errors that occured.
65  */
66 static int openssl_verify_peer(int ok, X509_STORE_CTX *store)
67 {
68    if (!ok) {
69       X509 *cert = X509_STORE_CTX_get_current_cert(store);
70       int depth = X509_STORE_CTX_get_error_depth(store);
71       int err = X509_STORE_CTX_get_error(store);
72       char issuer[256];
73       char subject[256];
74
75       X509_NAME_oneline(X509_get_issuer_name(cert), issuer, 256);
76       X509_NAME_oneline(X509_get_subject_name(cert), subject, 256);
77
78       Jmsg5(NULL, M_ERROR, 0, _("Error with certificate at depth: %d, issuer = %s,"
79             " subject = %s, ERR=%d:%s\n"), depth, issuer,
80               subject, err, X509_verify_cert_error_string(err));
81
82    }
83
84    return ok;
85 }
86
87 /* Dispatch user PEM encryption callbacks */
88 static int tls_pem_callback_dispatch (char *buf, int size, int rwflag, void *userdata)
89 {
90    TLS_CONTEXT *ctx = (TLS_CONTEXT *)userdata;
91    return (ctx->pem_callback(buf, size, ctx->pem_userdata));
92 }
93
94 /*
95  * Create a new TLS_CONTEXT instance.
96  *  Returns: Pointer to TLS_CONTEXT instance on success
97  *           NULL on failure;
98  */
99 TLS_CONTEXT *new_tls_context(const char *ca_certfile, const char *ca_certdir,
100                              const char *certfile, const char *keyfile,
101                              CRYPTO_PEM_PASSWD_CB *pem_callback,
102                              const void *pem_userdata, const char *dhfile,
103                              bool verify_peer)
104 {
105    TLS_CONTEXT *ctx;
106    BIO *bio;
107    DH *dh;
108
109    ctx = (TLS_CONTEXT *)malloc(sizeof(TLS_CONTEXT));
110
111    /* Allocate our OpenSSL TLSv1 Context */
112    ctx->openssl = SSL_CTX_new(TLSv1_method());
113
114    if (!ctx->openssl) {
115       openssl_post_errors(M_FATAL, _("Error initializing SSL context"));
116       goto err;
117    }
118
119    /* Set up pem encryption callback */
120    if (pem_callback) {
121       ctx->pem_callback = pem_callback;
122       ctx->pem_userdata = pem_userdata;
123    } else {
124       ctx->pem_callback = crypto_default_pem_callback;
125       ctx->pem_userdata = NULL;
126    }
127    SSL_CTX_set_default_passwd_cb(ctx->openssl, tls_pem_callback_dispatch);
128    SSL_CTX_set_default_passwd_cb_userdata(ctx->openssl, (void *) ctx);
129
130    /*
131     * Set certificate verification paths. This requires that at least one
132     * value be non-NULL
133     */
134    if (ca_certfile || ca_certdir) {
135       if (!SSL_CTX_load_verify_locations(ctx->openssl, ca_certfile, ca_certdir)) {
136          openssl_post_errors(M_FATAL, _("Error loading certificate verification stores"));
137          goto err;
138       }
139    } else if (verify_peer) {
140       /* At least one CA is required for peer verification */
141       Jmsg0(NULL, M_ERROR, 0, _("Either a certificate file or a directory must be"
142                          " specified as a verification store\n"));
143       goto err;
144    }
145
146    /*
147     * Load our certificate file, if available. This file may also contain a
148     * private key, though this usage is somewhat unusual.
149     */
150    if (certfile) {
151       if (!SSL_CTX_use_certificate_chain_file(ctx->openssl, certfile)) {
152          openssl_post_errors(M_FATAL, _("Error loading certificate file"));
153          goto err;
154       }
155    }
156
157    /* Load our private key. */
158    if (keyfile) {
159       if (!SSL_CTX_use_PrivateKey_file(ctx->openssl, keyfile, SSL_FILETYPE_PEM)) {
160          openssl_post_errors(M_FATAL, _("Error loading private key"));
161          goto err;
162       }
163    }
164
165    /* Load Diffie-Hellman Parameters. */
166    if (dhfile) {
167       if (!(bio = BIO_new_file(dhfile, "r"))) {
168          openssl_post_errors(M_FATAL, _("Unable to open DH parameters file"));
169          goto err;
170       }
171       dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
172       BIO_free(bio);
173       if (!dh) {
174          openssl_post_errors(M_FATAL, _("Unable to load DH parameters from specified file"));
175          goto err;
176       }
177       if (!SSL_CTX_set_tmp_dh(ctx->openssl, dh)) {
178          openssl_post_errors(M_FATAL, _("Failed to set TLS Diffie-Hellman parameters"));
179          DH_free(dh);
180          goto err;
181       }
182       /* Enable Single-Use DH for Ephemeral Keying */
183       SSL_CTX_set_options(ctx->openssl, SSL_OP_SINGLE_DH_USE);
184    }
185
186    if (SSL_CTX_set_cipher_list(ctx->openssl, TLS_DEFAULT_CIPHERS) != 1) {
187       Jmsg0(NULL, M_ERROR, 0,
188              _("Error setting cipher list, no valid ciphers available\n"));
189       goto err;
190    }
191
192    /* Verify Peer Certificate */
193    if (verify_peer) {
194       /* SSL_VERIFY_FAIL_IF_NO_PEER_CERT has no effect in client mode */
195       SSL_CTX_set_verify(ctx->openssl,
196                          SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
197                          openssl_verify_peer);
198    }
199
200    return ctx;
201
202 err:
203    /* Clean up after ourselves */
204    if(ctx->openssl) {
205       SSL_CTX_free(ctx->openssl);
206    }
207    free(ctx);
208    return NULL;
209 }
210
211 /*
212  * Free TLS_CONTEXT instance
213  */
214 void free_tls_context(TLS_CONTEXT *ctx)
215 {
216    SSL_CTX_free(ctx->openssl);
217    free(ctx);
218 }
219
220 bool get_tls_require(TLS_CONTEXT *ctx)
221 {
222    return ctx->tls_require;
223 }
224
225 bool get_tls_enable(TLS_CONTEXT *ctx)
226 {
227    return ctx->tls_enable;
228 }
229
230
231 /*
232  * Verifies a list of common names against the certificate
233  * commonName attribute.
234  *  Returns: true on success
235  *           false on failure
236  */
237 bool tls_postconnect_verify_cn(JCR *jcr, TLS_CONNECTION *tls, alist *verify_list)
238 {
239    SSL *ssl = tls->openssl;
240    X509 *cert;
241    X509_NAME *subject;
242    bool auth_success = false;
243    char data[256];
244
245    /* Check if peer provided a certificate */
246    if (!(cert = SSL_get_peer_certificate(ssl))) {
247       Qmsg0(jcr, M_ERROR, 0, _("Peer failed to present a TLS certificate\n"));
248       return false;
249    }
250
251    if ((subject = X509_get_subject_name(cert)) != NULL) {
252       if (X509_NAME_get_text_by_NID(subject, NID_commonName, data, sizeof(data)) > 0) {
253          char *cn;
254          /* NULL terminate data */
255          data[255] = 0;
256
257          /* Try all the CNs in the list */
258          foreach_alist(cn, verify_list) {
259             if (strcasecmp(data, cn) == 0) {
260                auth_success = true;
261             }
262          }
263       }
264    }
265
266    X509_free(cert);
267    return auth_success;
268 }
269
270 /*
271  * Verifies a peer's hostname against the subjectAltName and commonName
272  * attributes.
273  *  Returns: true on success
274  *           false on failure
275  */
276 bool tls_postconnect_verify_host(JCR *jcr, TLS_CONNECTION *tls, const char *host)
277 {
278    SSL *ssl = tls->openssl;
279    X509 *cert;
280    X509_NAME *subject;
281    bool auth_success = false;
282    int extensions;
283    int i, j;
284    const char *pval, *phost;
285
286    int cnLastPos = -1;
287    X509_NAME_ENTRY *neCN;
288    ASN1_STRING *asn1CN;
289
290    /* Check if peer provided a certificate */
291    if (!(cert = SSL_get_peer_certificate(ssl))) {
292       Qmsg1(jcr, M_ERROR, 0,
293             _("Peer %s failed to present a TLS certificate\n"), host);
294       Dmsg1(250, _("Peer %s failed to present a TLS certificate\n"), host);
295       return false;
296    }
297
298    /* Check subjectAltName extensions first */
299    if ((extensions = X509_get_ext_count(cert)) > 0) {
300       for (i = 0; i < extensions; i++) {
301          X509_EXTENSION *ext;
302          const char *extname;
303
304          ext = X509_get_ext(cert, i);
305          extname = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext)));
306
307          if (strcmp(extname, "subjectAltName") == 0) {
308 #ifdef HAVE_OPENSSLv1
309             const X509V3_EXT_METHOD *method;
310 #else
311             X509V3_EXT_METHOD *method;
312 #endif
313             STACK_OF(CONF_VALUE) *val;
314             CONF_VALUE *nval;
315             void *extstr = NULL;
316 #if (OPENSSL_VERSION_NUMBER >= 0x0090800FL)
317             const unsigned char *ext_value_data;
318 #else
319             unsigned char *ext_value_data;
320 #endif
321
322             /* Get x509 extension method structure */
323             if (!(method = X509V3_EXT_get(ext))) {
324                break;
325             }
326
327             ext_value_data = ext->value->data;
328
329 #if (OPENSSL_VERSION_NUMBER > 0x00907000L)
330             if (method->it) {
331                /* New style ASN1 */
332
333                /* Decode ASN1 item in data */
334                extstr = ASN1_item_d2i(NULL, &ext_value_data, ext->value->length,
335                                       ASN1_ITEM_ptr(method->it));
336             } else {
337                /* Old style ASN1 */
338
339                /* Decode ASN1 item in data */
340                extstr = method->d2i(NULL, &ext_value_data, ext->value->length);
341             }
342
343 #else
344             extstr = method->d2i(NULL, &ext_value_data, ext->value->length);
345 #endif
346
347             /* Iterate through to find the dNSName field(s) */
348             val = method->i2v(method, extstr, NULL);
349
350             /* dNSName shortname is "DNS" */
351             Dmsg0(250, "Check DNS name\n");
352             for (j = 0; j < sk_CONF_VALUE_num(val); j++) {
353                nval = sk_CONF_VALUE_value(val, j);
354                if (strcmp(nval->name, "DNS") == 0) {
355                   if (strncasecmp(nval->value, "*.", 2) == 0) {
356                      Dmsg0(250, "Wildcard Certificate\n");
357                      pval = strstr(nval->value, ".");
358                      phost = strstr(host, ".");
359                      if (pval && phost && (strcasecmp(pval, phost) == 0)) {
360                         auth_success = true;
361                         goto success;
362                      }
363                   } else if (strcasecmp(nval->value, host) == 0) {
364                      auth_success = true;
365                      goto success;
366                   }
367                   Dmsg2(250, "No DNS name match. Host=%s cert=%s\n", host, nval->value);
368                }
369             }
370          }
371       }
372    }
373
374    /* Try verifying against the subject name */
375    if (!auth_success) {
376       Dmsg0(250, "Check subject name name\n");
377       if ((subject = X509_get_subject_name(cert)) != NULL) {
378          /* Loop through all CNs */
379          for (;;) {
380             cnLastPos = X509_NAME_get_index_by_NID(subject, NID_commonName, cnLastPos);
381             if (cnLastPos == -1) {
382                break;
383             }
384             neCN = X509_NAME_get_entry(subject, cnLastPos);
385             asn1CN = X509_NAME_ENTRY_get_data(neCN);
386             if (strncasecmp((const char*)asn1CN->data, "*.", 2) == 0) {
387                /* wildcard certificate */
388                Dmsg0(250, "Wildcard Certificate\n");
389                pval = strstr((const char*)asn1CN->data, ".");
390                phost = strstr(host, ".");
391                if (pval && phost && (strcasecmp(pval, phost) == 0)) {
392                   auth_success = true;
393                   goto success;
394                }
395             } else if (strcasecmp((const char*)asn1CN->data, host) == 0) {
396                auth_success = true;
397                break;
398             }
399             Dmsg2(250, "No subject name match. Host=%s cert=%s\n", host, (const char*)asn1CN->data);
400          }
401       }
402    }
403
404 success:
405    X509_free(cert);
406    return auth_success;
407 }
408
409 /*
410  * Create a new TLS_CONNECTION instance.
411  *
412  * Returns: Pointer to TLS_CONNECTION instance on success
413  *          NULL on failure;
414  */
415 TLS_CONNECTION *new_tls_connection(TLS_CONTEXT *ctx, int fd)
416 {
417    BIO *bio;
418
419    /*
420     * Create a new BIO and assign the fd.
421     * The caller will remain responsible for closing the associated fd
422     */
423    bio = BIO_new(BIO_s_socket());
424    if (!bio) {
425       /* Not likely, but never say never */
426       openssl_post_errors(M_FATAL, _("Error creating file descriptor-based BIO"));
427       return NULL; /* Nothing allocated, nothing to clean up */
428    }
429    BIO_set_fd(bio, fd, BIO_NOCLOSE);
430
431    /* Allocate our new tls connection */
432    TLS_CONNECTION *tls = (TLS_CONNECTION *)malloc(sizeof(TLS_CONNECTION));
433
434    /* Create the SSL object and attach the socket BIO */
435    if ((tls->openssl = SSL_new(ctx->openssl)) == NULL) {
436       /* Not likely, but never say never */
437       openssl_post_errors(M_FATAL, _("Error creating new SSL object"));
438       goto err;
439    }
440
441    SSL_set_bio(tls->openssl, bio, bio);
442
443    /* Non-blocking partial writes */
444    SSL_set_mode(tls->openssl, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
445
446    return tls;
447
448 err:
449    /* Clean up */
450    BIO_free(bio);
451    SSL_free(tls->openssl);
452    free(tls);
453    return NULL;
454 }
455
456 /*
457  * Free TLS_CONNECTION instance
458  */
459 void free_tls_connection(TLS_CONNECTION *tls)
460 {
461    SSL_free(tls->openssl);
462    free(tls);
463 }
464
465 /* Does all the manual labor for tls_bsock_accept() and tls_bsock_connect() */
466 static inline bool openssl_bsock_session_start(BSOCK *bsock, bool server)
467 {
468    TLS_CONNECTION *tls = bsock->tls;
469    int err;
470    int fdmax, flags;
471    int stat = true;
472    fd_set fdset;
473    struct timeval tv;
474
475    /* Zero the fdset, we'll set our fd prior to each invocation of select() */
476    FD_ZERO(&fdset);
477    fdmax = bsock->m_fd + 1;
478
479    /* Ensure that socket is non-blocking */
480    flags = bsock->set_nonblocking();
481
482    /* start timer */
483    bsock->timer_start = watchdog_time;
484    bsock->clear_timed_out();
485    bsock->set_killable(false);
486
487    for (;;) {
488       if (server) {
489          err = SSL_accept(tls->openssl);
490       } else {
491          err = SSL_connect(tls->openssl);
492       }
493
494       /* Handle errors */
495       switch (SSL_get_error(tls->openssl, err)) {
496       case SSL_ERROR_NONE:
497          stat = true;
498          goto cleanup;
499       case SSL_ERROR_ZERO_RETURN:
500          /* TLS connection was cleanly shut down */
501          openssl_post_errors(bsock->get_jcr(), M_FATAL, _("Connect failure"));
502          stat = false;
503          goto cleanup;
504       case SSL_ERROR_WANT_READ:
505          /* If we timeout of a select, this will be unset */
506          FD_SET((unsigned) bsock->m_fd, &fdset);
507          /* Set our timeout */
508          tv.tv_sec = 10;
509          tv.tv_usec = 0;
510          /* Block until we can read */
511          select(fdmax, &fdset, NULL, NULL, &tv);
512          break;
513       case SSL_ERROR_WANT_WRITE:
514          /* If we timeout of a select, this will be unset */
515          FD_SET((unsigned) bsock->m_fd, &fdset);
516          /* Set our timeout */
517          tv.tv_sec = 10;
518          tv.tv_usec = 0;
519          /* Block until we can write */
520          select(fdmax, NULL, &fdset, NULL, &tv);
521          break;
522       default:
523          /* Socket Error Occurred */
524          openssl_post_errors(bsock->get_jcr(), M_FATAL, _("Connect failure"));
525          stat = false;
526          goto cleanup;
527       }
528
529       if (bsock->is_timed_out()) {
530          goto cleanup;
531       }
532    }
533
534 cleanup:
535    /* Restore saved flags */
536    bsock->restore_blocking(flags);
537    /* Clear timer */
538    bsock->timer_start = 0;
539    bsock->set_killable(true);
540
541    return stat;
542 }
543
544 /*
545  * Initiates a TLS connection with the server.
546  *  Returns: true on success
547  *           false on failure
548  */
549 bool tls_bsock_connect(BSOCK *bsock)
550 {
551    /* SSL_connect(bsock->tls) */
552    return openssl_bsock_session_start(bsock, false);
553 }
554
555 /*
556  * Listens for a TLS connection from a client.
557  *  Returns: true on success
558  *           false on failure
559  */
560 bool tls_bsock_accept(BSOCK *bsock)
561 {
562    /* SSL_accept(bsock->tls) */
563    return openssl_bsock_session_start(bsock, true);
564 }
565
566 /*
567  * Shutdown TLS_CONNECTION instance
568  */
569 void tls_bsock_shutdown(BSOCK *bsock)
570 {
571    /*
572     * SSL_shutdown must be called twice to fully complete the process -
573     * The first time to initiate the shutdown handshake, and the second to
574     * receive the peer's reply.
575     *
576     * In addition, if the underlying socket is blocking, SSL_shutdown()
577     * will not return until the current stage of the shutdown process has
578     * completed or an error has occured. By setting the socket blocking
579     * we can avoid the ugly for()/switch()/select() loop.
580     */
581    int err;
582
583    btimer_t *tid;
584
585    /* Set socket blocking for shutdown */
586    bsock->set_blocking();
587
588    tid = start_bsock_timer(bsock, 60 * 2);
589    err = SSL_shutdown(bsock->tls->openssl);
590    stop_bsock_timer(tid);
591    if (err == 0) {
592       /* Complete shutdown */
593       tid = start_bsock_timer(bsock, 60 * 2);
594       err = SSL_shutdown(bsock->tls->openssl);
595       stop_bsock_timer(tid);
596    }
597
598
599    switch (SSL_get_error(bsock->tls->openssl, err)) {
600    case SSL_ERROR_NONE:
601       break;
602    case SSL_ERROR_ZERO_RETURN:
603       /* TLS connection was shut down on us via a TLS protocol-level closure */
604       openssl_post_errors(bsock->get_jcr(), M_ERROR, _("TLS shutdown failure."));
605       break;
606    default:
607       /* Socket Error Occurred */
608       openssl_post_errors(bsock->get_jcr(), M_ERROR, _("TLS shutdown failure."));
609       break;
610    }
611 }
612
613 /* Does all the manual labor for tls_bsock_readn() and tls_bsock_writen() */
614 static inline int openssl_bsock_readwrite(BSOCK *bsock, char *ptr, int nbytes, bool write)
615 {
616    TLS_CONNECTION *tls = bsock->tls;
617    int fdmax, flags;
618    fd_set fdset;
619    struct timeval tv;
620    int nleft = 0;
621    int nwritten = 0;
622
623    /* Zero the fdset, we'll set our fd prior to each invocation of select() */
624    FD_ZERO(&fdset);
625    fdmax = bsock->m_fd + 1;
626
627    /* Ensure that socket is non-blocking */
628    flags = bsock->set_nonblocking();
629
630    /* start timer */
631    bsock->timer_start = watchdog_time;
632    bsock->clear_timed_out();
633    bsock->set_killable(false);
634
635    nleft = nbytes;
636
637    while (nleft > 0) {
638       if (write) {
639          nwritten = SSL_write(tls->openssl, ptr, nleft);
640       } else {
641          nwritten = SSL_read(tls->openssl, ptr, nleft);
642       }
643
644       /* Handle errors */
645       switch (SSL_get_error(tls->openssl, nwritten)) {
646       case SSL_ERROR_NONE:
647          nleft -= nwritten;
648          if (nleft) {
649             ptr += nwritten;
650          }
651          break;
652
653       case SSL_ERROR_SYSCALL:
654          if (nwritten == -1) {
655             if (errno == EINTR) {
656                continue;
657             }
658             if (errno == EAGAIN) {
659                bmicrosleep(0, 20000); /* try again in 20 ms */
660                continue;
661             }
662          }
663          openssl_post_errors(bsock->get_jcr(), M_FATAL, _("TLS read/write failure."));
664          goto cleanup;
665
666       case SSL_ERROR_WANT_READ:
667          /* If we timeout on a select, this will be unset */
668          FD_SET((unsigned)bsock->m_fd, &fdset);
669          tv.tv_sec = 10;
670          tv.tv_usec = 0;
671          /* Block until we can read */
672          select(fdmax, &fdset, NULL, NULL, &tv);
673          break;
674
675       case SSL_ERROR_WANT_WRITE:
676          /* If we timeout on a select, this will be unset */
677          FD_SET((unsigned)bsock->m_fd, &fdset);
678          tv.tv_sec = 10;
679          tv.tv_usec = 0;
680          /* Block until we can write */
681          select(fdmax, NULL, &fdset, NULL, &tv);
682          break;
683
684       case SSL_ERROR_ZERO_RETURN:
685          /* TLS connection was cleanly shut down */
686          /* Fall through wanted */
687       default:
688          /* Socket Error Occured */
689          openssl_post_errors(bsock->get_jcr(), M_FATAL, _("TLS read/write failure."));
690          goto cleanup;
691       }
692
693       /* Everything done? */
694       if (nleft == 0) {
695          goto cleanup;
696       }
697
698       /* Timeout/Termination, let's take what we can get */
699       if (bsock->is_timed_out() || bsock->is_terminated()) {
700          goto cleanup;
701       }
702    }
703
704 cleanup:
705    /* Restore saved flags */
706    bsock->restore_blocking(flags);
707
708    /* Clear timer */
709    bsock->timer_start = 0;
710    bsock->set_killable(true);
711    return nbytes - nleft;
712 }
713
714
715 int tls_bsock_writen(BSOCK *bsock, char *ptr, int32_t nbytes)
716 {
717    /* SSL_write(bsock->tls->openssl, ptr, nbytes) */
718    return openssl_bsock_readwrite(bsock, ptr, nbytes, true);
719 }
720
721 int tls_bsock_readn(BSOCK *bsock, char *ptr, int32_t nbytes)
722 {
723    /* SSL_read(bsock->tls->openssl, ptr, nbytes) */
724    return openssl_bsock_readwrite(bsock, ptr, nbytes, false);
725 }
726
727 #else /* HAVE_OPENSSL */
728 # error No TLS implementation available.
729 #endif /* !HAVE_OPENSSL */
730
731
732 #else     /* TLS NOT enabled, dummy routines substituted */
733
734
735 /* Dummy routines */
736 TLS_CONTEXT *new_tls_context(const char *ca_certfile, const char *ca_certdir,
737                              const char *certfile, const char *keyfile,
738                              CRYPTO_PEM_PASSWD_CB *pem_callback,
739                              const void *pem_userdata, const char *dhfile,
740                              bool verify_peer)
741 {
742    return NULL;
743 }
744 void free_tls_context(TLS_CONTEXT *ctx) { }
745
746 void tls_bsock_shutdown(BSOCK *bsock) { }
747
748 void free_tls_connection(TLS_CONNECTION *tls) { }
749
750 bool get_tls_require(TLS_CONTEXT *ctx)
751 {
752    return false;
753 }
754
755 bool get_tls_enable(TLS_CONTEXT *ctx)
756 {
757    return false;
758 }
759
760 #endif /* HAVE_TLS */