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