Home | History | Annotate | Line # | Download | only in httpd
ssl-bozo.c revision 1.18.2.1.4.1
      1  1.18.2.1.4.1   martin /*	$NetBSD: ssl-bozo.c,v 1.18.2.1.4.1 2018/11/24 17:23:20 martin Exp $	*/
      2           1.2      tls 
      3          1.13      mrg /*	$eterna: ssl-bozo.c,v 1.15 2011/11/18 09:21:15 mrg Exp $	*/
      4           1.1      tls 
      5           1.1      tls /*
      6  1.18.2.1.4.1   martin  * Copyright (c) 1997-2018 Matthew R. Green
      7           1.1      tls  * All rights reserved.
      8           1.1      tls  *
      9           1.1      tls  * Redistribution and use in source and binary forms, with or without
     10           1.1      tls  * modification, are permitted provided that the following conditions
     11           1.1      tls  * are met:
     12           1.1      tls  * 1. Redistributions of source code must retain the above copyright
     13           1.1      tls  *    notice, this list of conditions and the following disclaimer.
     14           1.1      tls  * 2. Redistributions in binary form must reproduce the above copyright
     15           1.1      tls  *    notice, this list of conditions and the following disclaimer and
     16           1.1      tls  *    dedication in the documentation and/or other materials provided
     17           1.1      tls  *    with the distribution.
     18           1.1      tls  *
     19           1.1      tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20           1.1      tls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21           1.1      tls  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22           1.1      tls  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23           1.1      tls  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24           1.1      tls  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25           1.1      tls  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26           1.1      tls  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27           1.1      tls  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28           1.1      tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29           1.1      tls  * SUCH DAMAGE.
     30           1.1      tls  *
     31           1.1      tls  */
     32           1.1      tls 
     33          1.18      mrg /* this code implements SSL and backend IO for bozohttpd */
     34           1.1      tls 
     35           1.7      mrg #include <stdarg.h>
     36           1.7      mrg #include <stdio.h>
     37  1.18.2.1.4.1   martin #include <string.h>
     38           1.8      mrg #include <syslog.h>
     39           1.7      mrg #include <unistd.h>
     40           1.7      mrg 
     41           1.7      mrg #include "bozohttpd.h"
     42           1.7      mrg 
     43           1.1      tls #ifndef NO_SSL_SUPPORT
     44           1.1      tls 
     45           1.1      tls #include <openssl/ssl.h>
     46           1.1      tls #include <openssl/err.h>
     47           1.1      tls 
     48           1.7      mrg #ifndef USE_ARG
     49           1.7      mrg #define USE_ARG(x)	/*LINTED*/(void)&(x)
     50           1.7      mrg #endif
     51           1.7      mrg 
     52      1.18.2.1   martin #ifndef BOZO_SSL_CIPHERS
     53      1.18.2.1   martin #define BOZO_SSL_CIPHERS 					\
     54      1.18.2.1   martin 	"AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:"		\
     55      1.18.2.1   martin 	"AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:"		\
     56      1.18.2.1   martin 	"AES:"							\
     57      1.18.2.1   martin 	"-SHA:"							\
     58      1.18.2.1   martin 	"!aNULL:!eNULL:"					\
     59      1.18.2.1   martin 	"!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"			\
     60      1.18.2.1   martin 	"!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:"		\
     61      1.18.2.1   martin 	"!KRB5-DES-CBC3-SHA"
     62      1.18.2.1   martin #endif
     63      1.18.2.1   martin 
     64      1.18.2.1   martin #ifndef BOZO_SSL_OPTIONS
     65      1.18.2.1   martin #define BOZO_SSL_OPTIONS					\
     66  1.18.2.1.4.1   martin 	((long)(SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1))
     67      1.18.2.1   martin #endif
     68      1.18.2.1   martin 
     69      1.18.2.1   martin   /* this structure encapsulates the ssl info */
     70      1.18.2.1   martin 
     71           1.7      mrg /* this structure encapsulates the ssl info */
     72           1.7      mrg typedef struct sslinfo_t {
     73           1.7      mrg 	SSL_CTX			*ssl_context;
     74           1.7      mrg 	const SSL_METHOD	*ssl_method;
     75           1.7      mrg 	SSL			*bozossl;
     76           1.7      mrg 	char			*certificate_file;
     77           1.7      mrg 	char			*privatekey_file;
     78      1.18.2.1   martin 	char			*ciphers;
     79           1.7      mrg } sslinfo_t;
     80           1.7      mrg 
     81           1.8      mrg /*
     82          1.18      mrg  * bozo_clear_ssl_queue:  print the contents of the SSL error queue
     83           1.8      mrg  */
     84          1.18      mrg static void
     85          1.18      mrg bozo_clear_ssl_queue(bozohttpd_t *httpd)
     86           1.8      mrg {
     87          1.18      mrg 	unsigned long sslcode = ERR_get_error();
     88           1.8      mrg 
     89           1.8      mrg 	do {
     90          1.11  hannken 		static const char sslfmt[] = "SSL Error: %s:%s:%s";
     91           1.8      mrg 
     92           1.8      mrg 		if (httpd->logstderr || isatty(STDERR_FILENO)) {
     93           1.8      mrg 			fprintf(stderr, sslfmt,
     94           1.8      mrg 			    ERR_lib_error_string(sslcode),
     95           1.8      mrg 			    ERR_func_error_string(sslcode),
     96           1.8      mrg 			    ERR_reason_error_string(sslcode));
     97           1.8      mrg 		} else {
     98           1.8      mrg 			syslog(LOG_ERR, sslfmt,
     99           1.8      mrg 			    ERR_lib_error_string(sslcode),
    100           1.8      mrg 			    ERR_func_error_string(sslcode),
    101           1.8      mrg 			    ERR_reason_error_string(sslcode));
    102           1.8      mrg 		}
    103           1.8      mrg 	} while (0 != (sslcode = ERR_get_error()));
    104          1.18      mrg }
    105          1.18      mrg 
    106          1.18      mrg /*
    107      1.18.2.1   martin  * bozo_ssl_warn works just like bozowarn, plus the SSL error queue
    108          1.18      mrg  */
    109          1.18      mrg BOZO_PRINTFLIKE(2, 3) static void
    110          1.18      mrg bozo_ssl_warn(bozohttpd_t *httpd, const char *fmt, ...)
    111          1.18      mrg {
    112          1.18      mrg 	va_list ap;
    113          1.18      mrg 
    114          1.18      mrg 	va_start(ap, fmt);
    115          1.18      mrg 	if (httpd->logstderr || isatty(STDERR_FILENO)) {
    116          1.18      mrg 		vfprintf(stderr, fmt, ap);
    117          1.18      mrg 		fputs("\n", stderr);
    118          1.18      mrg 	} else
    119          1.18      mrg 		vsyslog(LOG_ERR, fmt, ap);
    120          1.18      mrg 	va_end(ap);
    121          1.18      mrg 
    122          1.18      mrg 	bozo_clear_ssl_queue(httpd);
    123          1.18      mrg }
    124          1.18      mrg 
    125          1.18      mrg 
    126          1.18      mrg /*
    127      1.18.2.1   martin  * bozo_ssl_err works just like bozoerr, plus the SSL error queue
    128          1.18      mrg  */
    129          1.18      mrg BOZO_PRINTFLIKE(3, 4) BOZO_DEAD static void
    130          1.18      mrg bozo_ssl_err(bozohttpd_t *httpd, int code, const char *fmt, ...)
    131          1.18      mrg {
    132          1.18      mrg 	va_list ap;
    133          1.18      mrg 
    134          1.18      mrg 	va_start(ap, fmt);
    135          1.18      mrg 	if (httpd->logstderr || isatty(STDERR_FILENO)) {
    136          1.18      mrg 		vfprintf(stderr, fmt, ap);
    137          1.18      mrg 		fputs("\n", stderr);
    138          1.18      mrg 	} else
    139          1.18      mrg 		vsyslog(LOG_ERR, fmt, ap);
    140          1.18      mrg 	va_end(ap);
    141          1.18      mrg 
    142          1.18      mrg 	bozo_clear_ssl_queue(httpd);
    143           1.8      mrg 	exit(code);
    144           1.8      mrg }
    145           1.8      mrg 
    146          1.18      mrg /*
    147          1.18      mrg  * bozo_check_error_queue:  print warnings if the error isn't expected
    148          1.18      mrg  */
    149          1.18      mrg static void
    150          1.18      mrg bozo_check_error_queue(bozohttpd_t *httpd, const char *tag, int ret)
    151          1.18      mrg {
    152          1.18      mrg 	if (ret > 0)
    153          1.18      mrg 		return;
    154          1.18      mrg 
    155          1.18      mrg 	const sslinfo_t *sslinfo = httpd->sslinfo;
    156          1.18      mrg 	const int sslerr = SSL_get_error(sslinfo->bozossl, ret);
    157          1.18      mrg 
    158          1.18      mrg 	if (sslerr != SSL_ERROR_ZERO_RETURN &&
    159          1.18      mrg 	    sslerr != SSL_ERROR_SYSCALL &&
    160          1.18      mrg 	    sslerr != SSL_ERROR_NONE)
    161          1.18      mrg 		bozo_ssl_warn(httpd, "%s: SSL_ERROR %d", tag, sslerr);
    162          1.18      mrg }
    163          1.18      mrg 
    164          1.15    joerg static BOZO_PRINTFLIKE(2, 0) int
    165           1.8      mrg bozo_ssl_printf(bozohttpd_t *httpd, const char * fmt, va_list ap)
    166           1.7      mrg {
    167          1.18      mrg 	char	*buf;
    168          1.18      mrg 	int	 nbytes;
    169           1.7      mrg 
    170          1.18      mrg 	if ((nbytes = vasprintf(&buf, fmt, ap)) != -1)  {
    171          1.18      mrg 		const sslinfo_t *sslinfo = httpd->sslinfo;
    172          1.18      mrg 		int ret = SSL_write(sslinfo->bozossl, buf, nbytes);
    173          1.18      mrg 		bozo_check_error_queue(httpd, "write", ret);
    174          1.18      mrg 	}
    175           1.7      mrg 
    176           1.7      mrg 	free(buf);
    177           1.7      mrg 
    178           1.7      mrg 	return nbytes;
    179           1.7      mrg }
    180           1.7      mrg 
    181           1.7      mrg static ssize_t
    182           1.7      mrg bozo_ssl_read(bozohttpd_t *httpd, int fd, void *buf, size_t nbytes)
    183           1.7      mrg {
    184          1.17      mrg 	const sslinfo_t *sslinfo = httpd->sslinfo;
    185          1.18      mrg 	int	ret;
    186           1.7      mrg 
    187           1.7      mrg 	USE_ARG(fd);
    188          1.18      mrg 	ret = SSL_read(sslinfo->bozossl, buf, (int)nbytes);
    189          1.18      mrg 	bozo_check_error_queue(httpd, "read", ret);
    190           1.7      mrg 
    191          1.18      mrg 	return (ssize_t)ret;
    192           1.7      mrg }
    193           1.7      mrg 
    194           1.7      mrg static ssize_t
    195           1.7      mrg bozo_ssl_write(bozohttpd_t *httpd, int fd, const void *buf, size_t nbytes)
    196           1.7      mrg {
    197          1.17      mrg 	const sslinfo_t *sslinfo = httpd->sslinfo;
    198          1.18      mrg 	int	ret;
    199           1.7      mrg 
    200           1.7      mrg 	USE_ARG(fd);
    201          1.18      mrg 	ret = SSL_write(sslinfo->bozossl, buf, (int)nbytes);
    202          1.18      mrg 	bozo_check_error_queue(httpd, "write", ret);
    203           1.1      tls 
    204          1.18      mrg 	return (ssize_t)ret;
    205           1.7      mrg }
    206           1.1      tls 
    207           1.1      tls void
    208           1.7      mrg bozo_ssl_init(bozohttpd_t *httpd)
    209           1.1      tls {
    210          1.17      mrg 	sslinfo_t *sslinfo = httpd->sslinfo;
    211      1.18.2.1   martin 	long options;
    212           1.7      mrg 
    213           1.7      mrg 	if (sslinfo == NULL || !sslinfo->certificate_file)
    214           1.1      tls 		return;
    215           1.1      tls 	SSL_library_init();
    216           1.1      tls 	SSL_load_error_strings();
    217           1.1      tls 
    218           1.7      mrg 	sslinfo->ssl_method = SSLv23_server_method();
    219           1.7      mrg 	sslinfo->ssl_context = SSL_CTX_new(sslinfo->ssl_method);
    220           1.1      tls 
    221           1.8      mrg 	if (NULL == sslinfo->ssl_context)
    222           1.8      mrg 		bozo_ssl_err(httpd, EXIT_FAILURE,
    223           1.8      mrg 		    "SSL context creation failed");
    224           1.8      mrg 
    225      1.18.2.1   martin 	options = SSL_CTX_set_options(sslinfo->ssl_context,
    226      1.18.2.1   martin 	    BOZO_SSL_OPTIONS);
    227      1.18.2.1   martin 	if ((options & BOZO_SSL_OPTIONS) != BOZO_SSL_OPTIONS)
    228      1.18.2.1   martin 		bozo_ssl_err(httpd, EXIT_FAILURE,
    229      1.18.2.1   martin 		    "Error setting ssl options requested %#lx, got %#lx",
    230      1.18.2.1   martin 		    BOZO_SSL_OPTIONS, options);
    231      1.18.2.1   martin 
    232      1.18.2.1   martin 	if (!SSL_CTX_set_cipher_list(sslinfo->ssl_context,
    233      1.18.2.1   martin 	    sslinfo->ciphers ? sslinfo->ciphers : BOZO_SSL_CIPHERS))
    234      1.18.2.1   martin 		bozo_ssl_err(httpd, EXIT_FAILURE,
    235      1.18.2.1   martin 		    "Error setting cipher list '%s'", sslinfo->ciphers);
    236      1.18.2.1   martin 
    237          1.14    elric 	if (1 != SSL_CTX_use_certificate_chain_file(sslinfo->ssl_context,
    238          1.14    elric 	    sslinfo->certificate_file))
    239           1.8      mrg 		bozo_ssl_err(httpd, EXIT_FAILURE,
    240           1.8      mrg 		    "Unable to use certificate file '%s'",
    241           1.8      mrg 		    sslinfo->certificate_file);
    242           1.8      mrg 
    243           1.8      mrg 	if (1 != SSL_CTX_use_PrivateKey_file(sslinfo->ssl_context,
    244           1.8      mrg 	    sslinfo->privatekey_file, SSL_FILETYPE_PEM))
    245           1.8      mrg 		bozo_ssl_err(httpd, EXIT_FAILURE,
    246           1.8      mrg 		    "Unable to use private key file '%s'",
    247           1.8      mrg 		    sslinfo->privatekey_file);
    248           1.1      tls 
    249           1.1      tls 	/* check consistency of key vs certificate */
    250           1.7      mrg 	if (!SSL_CTX_check_private_key(sslinfo->ssl_context))
    251           1.8      mrg 		bozo_ssl_err(httpd, EXIT_FAILURE,
    252           1.8      mrg 		    "Check private key failed");
    253           1.1      tls }
    254           1.1      tls 
    255          1.18      mrg /*
    256          1.18      mrg  * returns non-zero for failure
    257          1.18      mrg  */
    258          1.18      mrg int
    259           1.7      mrg bozo_ssl_accept(bozohttpd_t *httpd)
    260           1.1      tls {
    261          1.17      mrg 	sslinfo_t *sslinfo = httpd->sslinfo;
    262           1.7      mrg 
    263          1.18      mrg 	if (sslinfo == NULL || !sslinfo->ssl_context)
    264          1.18      mrg 		return 0;
    265          1.18      mrg 
    266          1.18      mrg 	sslinfo->bozossl = SSL_new(sslinfo->ssl_context);
    267          1.18      mrg 	if (sslinfo->bozossl == NULL)
    268      1.18.2.1   martin 		bozoerr(httpd, 1, "SSL_new failed");
    269          1.18      mrg 
    270          1.18      mrg 	SSL_set_rfd(sslinfo->bozossl, 0);
    271          1.18      mrg 	SSL_set_wfd(sslinfo->bozossl, 1);
    272          1.18      mrg 
    273          1.18      mrg 	const int ret = SSL_accept(sslinfo->bozossl);
    274          1.18      mrg 	bozo_check_error_queue(httpd, "accept", ret);
    275          1.18      mrg 
    276          1.18      mrg 	return ret != 1;
    277           1.1      tls }
    278           1.1      tls 
    279           1.1      tls void
    280           1.7      mrg bozo_ssl_destroy(bozohttpd_t *httpd)
    281           1.1      tls {
    282          1.17      mrg 	const sslinfo_t *sslinfo = httpd->sslinfo;
    283           1.7      mrg 
    284           1.7      mrg 	if (sslinfo && sslinfo->bozossl)
    285           1.7      mrg 		SSL_free(sslinfo->bozossl);
    286           1.1      tls }
    287           1.1      tls 
    288      1.18.2.1   martin static sslinfo_t *
    289      1.18.2.1   martin bozo_get_sslinfo(bozohttpd_t *httpd)
    290      1.18.2.1   martin {
    291      1.18.2.1   martin 	sslinfo_t *sslinfo;
    292      1.18.2.1   martin 	if (httpd->sslinfo)
    293      1.18.2.1   martin 		return httpd->sslinfo;
    294      1.18.2.1   martin 	sslinfo = bozomalloc(httpd, sizeof(*sslinfo));
    295      1.18.2.1   martin 	if (sslinfo == NULL)
    296      1.18.2.1   martin 		bozoerr(httpd, 1, "sslinfo allocation failed");
    297      1.18.2.1   martin 	memset(sslinfo, 0, sizeof(*sslinfo));
    298      1.18.2.1   martin 	return httpd->sslinfo = sslinfo;
    299      1.18.2.1   martin }
    300      1.18.2.1   martin 
    301           1.1      tls void
    302           1.7      mrg bozo_ssl_set_opts(bozohttpd_t *httpd, const char *cert, const char *priv)
    303           1.1      tls {
    304      1.18.2.1   martin 	sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
    305           1.7      mrg 
    306      1.18.2.1   martin 	sslinfo->certificate_file = bozostrdup(httpd, NULL, cert);
    307      1.18.2.1   martin 	sslinfo->privatekey_file = bozostrdup(httpd, NULL, priv);
    308           1.7      mrg 	debug((httpd, DEBUG_NORMAL, "using cert/priv files: %s & %s",
    309      1.18.2.1   martin 	    sslinfo->certificate_file,
    310      1.18.2.1   martin 	    sslinfo->privatekey_file));
    311          1.17      mrg 	if (!httpd->bindport)
    312      1.18.2.1   martin 		httpd->bindport = bozostrdup(httpd, NULL, "https");
    313      1.18.2.1   martin }
    314      1.18.2.1   martin 
    315      1.18.2.1   martin void
    316      1.18.2.1   martin bozo_ssl_set_ciphers(bozohttpd_t *httpd, const char *ciphers)
    317      1.18.2.1   martin {
    318      1.18.2.1   martin 	sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
    319      1.18.2.1   martin 
    320      1.18.2.1   martin 	sslinfo->ciphers = bozostrdup(httpd, NULL, ciphers);
    321      1.18.2.1   martin 	debug((httpd, DEBUG_NORMAL, "using ciphers: %s", sslinfo->ciphers));
    322           1.1      tls }
    323           1.1      tls 
    324           1.7      mrg #endif /* NO_SSL_SUPPORT */
    325           1.7      mrg 
    326  1.18.2.1.4.1   martin /*
    327  1.18.2.1.4.1   martin  * These functions are always present, so that caller code can simply
    328  1.18.2.1.4.1   martin  * use bozo_*() for IO, regardless of SSL.
    329  1.18.2.1.4.1   martin  */
    330           1.7      mrg int
    331           1.7      mrg bozo_printf(bozohttpd_t *httpd, const char *fmt, ...)
    332           1.1      tls {
    333           1.7      mrg 	va_list	args;
    334           1.7      mrg 	int	cc;
    335           1.1      tls 
    336           1.7      mrg 	va_start(args, fmt);
    337           1.7      mrg #ifndef NO_SSL_SUPPORT
    338          1.17      mrg 	if (httpd->sslinfo)
    339           1.7      mrg 		cc = bozo_ssl_printf(httpd, fmt, args);
    340          1.17      mrg 	else
    341           1.7      mrg #endif
    342          1.17      mrg 		cc = vprintf(fmt, args);
    343           1.7      mrg 	va_end(args);
    344           1.7      mrg 	return cc;
    345           1.1      tls }
    346           1.1      tls 
    347           1.7      mrg ssize_t
    348           1.7      mrg bozo_read(bozohttpd_t *httpd, int fd, void *buf, size_t len)
    349           1.1      tls {
    350           1.7      mrg #ifndef NO_SSL_SUPPORT
    351          1.17      mrg 	if (httpd->sslinfo)
    352           1.7      mrg 		return bozo_ssl_read(httpd, fd, buf, len);
    353           1.7      mrg #endif
    354           1.7      mrg 	return read(fd, buf, len);
    355           1.1      tls }
    356           1.1      tls 
    357           1.7      mrg ssize_t
    358           1.7      mrg bozo_write(bozohttpd_t *httpd, int fd, const void *buf, size_t len)
    359           1.1      tls {
    360           1.7      mrg #ifndef NO_SSL_SUPPORT
    361          1.17      mrg 	if (httpd->sslinfo)
    362           1.7      mrg 		return bozo_ssl_write(httpd, fd, buf, len);
    363           1.7      mrg #endif
    364           1.7      mrg 	return write(fd, buf, len);
    365           1.1      tls }
    366           1.1      tls 
    367           1.7      mrg int
    368           1.7      mrg bozo_flush(bozohttpd_t *httpd, FILE *fp)
    369           1.1      tls {
    370           1.7      mrg #ifndef NO_SSL_SUPPORT
    371          1.17      mrg 	if (httpd->sslinfo)
    372          1.18      mrg 		return 0;
    373           1.7      mrg #endif
    374           1.7      mrg 	return fflush(fp);
    375           1.1      tls }
    376