Home | History | Annotate | Line # | Download | only in httpd
ssl-bozo.c revision 1.4.4.1
      1  1.4.4.1  msaitoh /*	$NetBSD: ssl-bozo.c,v 1.4.4.1 2014/07/09 15:21:21 msaitoh Exp $	*/
      2      1.2      tls 
      3  1.4.4.1  msaitoh /*	$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.4.4.1  msaitoh  * Copyright (c) 1997-2014 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.1      tls /* this code implements SSL for bozohttpd */
     34      1.1      tls 
     35  1.4.4.1  msaitoh #include <stdarg.h>
     36  1.4.4.1  msaitoh #include <stdio.h>
     37  1.4.4.1  msaitoh #include <syslog.h>
     38      1.1      tls #include <unistd.h>
     39      1.1      tls 
     40  1.4.4.1  msaitoh #include "bozohttpd.h"
     41  1.4.4.1  msaitoh 
     42  1.4.4.1  msaitoh #ifndef NO_SSL_SUPPORT
     43  1.4.4.1  msaitoh 
     44      1.1      tls #include <openssl/ssl.h>
     45      1.1      tls #include <openssl/err.h>
     46      1.1      tls 
     47  1.4.4.1  msaitoh #ifndef USE_ARG
     48  1.4.4.1  msaitoh #define USE_ARG(x)	/*LINTED*/(void)&(x)
     49  1.4.4.1  msaitoh #endif
     50  1.4.4.1  msaitoh 
     51  1.4.4.1  msaitoh /* this structure encapsulates the ssl info */
     52  1.4.4.1  msaitoh typedef struct sslinfo_t {
     53  1.4.4.1  msaitoh 	SSL_CTX			*ssl_context;
     54  1.4.4.1  msaitoh 	const SSL_METHOD	*ssl_method;
     55  1.4.4.1  msaitoh 	SSL			*bozossl;
     56  1.4.4.1  msaitoh 	char			*certificate_file;
     57  1.4.4.1  msaitoh 	char			*privatekey_file;
     58  1.4.4.1  msaitoh } sslinfo_t;
     59  1.4.4.1  msaitoh 
     60  1.4.4.1  msaitoh /*
     61  1.4.4.1  msaitoh  * bozo_ssl_err
     62  1.4.4.1  msaitoh  *
     63  1.4.4.1  msaitoh  * bozo_ssl_err works just like bozo_err except in addition to printing
     64  1.4.4.1  msaitoh  * the error provided by the caller at the point of error it pops and
     65  1.4.4.1  msaitoh  * prints all errors from the SSL error queue.
     66  1.4.4.1  msaitoh  */
     67  1.4.4.1  msaitoh BOZO_PRINTFLIKE(3, 4) BOZO_DEAD static void
     68  1.4.4.1  msaitoh bozo_ssl_err(bozohttpd_t *httpd, int code, const char *fmt, ...)
     69  1.4.4.1  msaitoh {
     70  1.4.4.1  msaitoh         va_list ap;
     71  1.4.4.1  msaitoh 
     72  1.4.4.1  msaitoh         va_start(ap, fmt);
     73  1.4.4.1  msaitoh         if (httpd->logstderr || isatty(STDERR_FILENO)) {
     74  1.4.4.1  msaitoh                 vfprintf(stderr, fmt, ap);
     75  1.4.4.1  msaitoh                 fputs("\n", stderr);
     76  1.4.4.1  msaitoh         } else
     77  1.4.4.1  msaitoh                 vsyslog(LOG_ERR, fmt, ap);
     78  1.4.4.1  msaitoh         va_end(ap);
     79  1.4.4.1  msaitoh 
     80  1.4.4.1  msaitoh 	unsigned int sslcode = ERR_get_error();
     81  1.4.4.1  msaitoh 	do {
     82  1.4.4.1  msaitoh 		static const char sslfmt[] = "SSL Error: %s:%s:%s";
     83  1.4.4.1  msaitoh 
     84  1.4.4.1  msaitoh 		if (httpd->logstderr || isatty(STDERR_FILENO)) {
     85  1.4.4.1  msaitoh 			fprintf(stderr, sslfmt,
     86  1.4.4.1  msaitoh 			    ERR_lib_error_string(sslcode),
     87  1.4.4.1  msaitoh 			    ERR_func_error_string(sslcode),
     88  1.4.4.1  msaitoh 			    ERR_reason_error_string(sslcode));
     89  1.4.4.1  msaitoh 		} else {
     90  1.4.4.1  msaitoh 			syslog(LOG_ERR, sslfmt,
     91  1.4.4.1  msaitoh 			    ERR_lib_error_string(sslcode),
     92  1.4.4.1  msaitoh 			    ERR_func_error_string(sslcode),
     93  1.4.4.1  msaitoh 			    ERR_reason_error_string(sslcode));
     94  1.4.4.1  msaitoh 		}
     95  1.4.4.1  msaitoh 	} while (0 != (sslcode = ERR_get_error()));
     96  1.4.4.1  msaitoh 	exit(code);
     97  1.4.4.1  msaitoh }
     98  1.4.4.1  msaitoh 
     99  1.4.4.1  msaitoh static BOZO_PRINTFLIKE(2, 0) int
    100  1.4.4.1  msaitoh bozo_ssl_printf(bozohttpd_t *httpd, const char * fmt, va_list ap)
    101  1.4.4.1  msaitoh {
    102  1.4.4.1  msaitoh 	sslinfo_t	*sslinfo;
    103  1.4.4.1  msaitoh 	char		*buf;
    104  1.4.4.1  msaitoh 	int		 nbytes;
    105  1.4.4.1  msaitoh 
    106  1.4.4.1  msaitoh 	sslinfo = httpd->sslinfo;
    107  1.4.4.1  msaitoh 	/* XXX we need more elegant/proper handling of SSL_write return */
    108  1.4.4.1  msaitoh 	if ((nbytes = vasprintf(&buf, fmt, ap)) != -1)
    109  1.4.4.1  msaitoh 		SSL_write(sslinfo->bozossl, buf, nbytes);
    110  1.4.4.1  msaitoh 
    111  1.4.4.1  msaitoh 	free(buf);
    112  1.4.4.1  msaitoh 
    113  1.4.4.1  msaitoh 	return nbytes;
    114  1.4.4.1  msaitoh }
    115  1.4.4.1  msaitoh 
    116  1.4.4.1  msaitoh static ssize_t
    117  1.4.4.1  msaitoh bozo_ssl_read(bozohttpd_t *httpd, int fd, void *buf, size_t nbytes)
    118  1.4.4.1  msaitoh {
    119  1.4.4.1  msaitoh 	sslinfo_t	*sslinfo;
    120  1.4.4.1  msaitoh 	ssize_t		 rbytes;
    121  1.4.4.1  msaitoh 
    122  1.4.4.1  msaitoh 	USE_ARG(fd);
    123  1.4.4.1  msaitoh 	sslinfo = httpd->sslinfo;
    124  1.4.4.1  msaitoh 	/* XXX we need elegant/proper handling of SSL_read return */
    125  1.4.4.1  msaitoh 	rbytes = (ssize_t)SSL_read(sslinfo->bozossl, buf, (int)nbytes);
    126  1.4.4.1  msaitoh 	if (rbytes < 1) {
    127  1.4.4.1  msaitoh 		if (SSL_get_error(sslinfo->bozossl, rbytes) ==
    128  1.4.4.1  msaitoh 				SSL_ERROR_WANT_READ)
    129  1.4.4.1  msaitoh 			bozo_warn(httpd, "SSL_ERROR_WANT_READ");
    130  1.4.4.1  msaitoh 		else
    131  1.4.4.1  msaitoh 			bozo_warn(httpd, "SSL_ERROR OTHER");
    132  1.4.4.1  msaitoh 	}
    133  1.4.4.1  msaitoh 
    134  1.4.4.1  msaitoh 	return rbytes;
    135  1.4.4.1  msaitoh }
    136  1.4.4.1  msaitoh 
    137  1.4.4.1  msaitoh static ssize_t
    138  1.4.4.1  msaitoh bozo_ssl_write(bozohttpd_t *httpd, int fd, const void *buf, size_t nbytes)
    139  1.4.4.1  msaitoh {
    140  1.4.4.1  msaitoh 	sslinfo_t	*sslinfo;
    141  1.4.4.1  msaitoh 	ssize_t		 wbytes;
    142  1.4.4.1  msaitoh 
    143  1.4.4.1  msaitoh 	USE_ARG(fd);
    144  1.4.4.1  msaitoh 	sslinfo = httpd->sslinfo;
    145  1.4.4.1  msaitoh 	/* XXX we need elegant/proper handling of SSL_write return */
    146  1.4.4.1  msaitoh 	wbytes = (ssize_t)SSL_write(sslinfo->bozossl, buf, (int)nbytes);
    147  1.4.4.1  msaitoh 
    148  1.4.4.1  msaitoh 	return wbytes;
    149  1.4.4.1  msaitoh }
    150      1.1      tls 
    151  1.4.4.1  msaitoh static int
    152  1.4.4.1  msaitoh bozo_ssl_flush(bozohttpd_t *httpd, FILE *fp)
    153  1.4.4.1  msaitoh {
    154  1.4.4.1  msaitoh 	USE_ARG(httpd);
    155  1.4.4.1  msaitoh 	USE_ARG(fp);
    156  1.4.4.1  msaitoh 	/* nothing to see here, move right along */
    157  1.4.4.1  msaitoh 	return 0;
    158  1.4.4.1  msaitoh }
    159      1.1      tls 
    160      1.1      tls void
    161  1.4.4.1  msaitoh bozo_ssl_init(bozohttpd_t *httpd)
    162      1.1      tls {
    163  1.4.4.1  msaitoh 	sslinfo_t	*sslinfo;
    164  1.4.4.1  msaitoh 
    165  1.4.4.1  msaitoh 	sslinfo = httpd->sslinfo;
    166  1.4.4.1  msaitoh 	if (sslinfo == NULL || !sslinfo->certificate_file)
    167      1.1      tls 		return;
    168      1.1      tls 	SSL_library_init();
    169      1.1      tls 	SSL_load_error_strings();
    170      1.1      tls 
    171  1.4.4.1  msaitoh 	sslinfo->ssl_method = SSLv23_server_method();
    172  1.4.4.1  msaitoh 	sslinfo->ssl_context = SSL_CTX_new(sslinfo->ssl_method);
    173      1.1      tls 
    174      1.1      tls 	/* XXX we need to learn how to check the SSL stack for more info */
    175  1.4.4.1  msaitoh 	if (NULL == sslinfo->ssl_context)
    176  1.4.4.1  msaitoh 		bozo_ssl_err(httpd, EXIT_FAILURE,
    177  1.4.4.1  msaitoh 		    "SSL context creation failed");
    178  1.4.4.1  msaitoh 
    179  1.4.4.1  msaitoh 	if (1 != SSL_CTX_use_certificate_chain_file(sslinfo->ssl_context,
    180  1.4.4.1  msaitoh 	    sslinfo->certificate_file))
    181  1.4.4.1  msaitoh 		bozo_ssl_err(httpd, EXIT_FAILURE,
    182  1.4.4.1  msaitoh 		    "Unable to use certificate file '%s'",
    183  1.4.4.1  msaitoh 		    sslinfo->certificate_file);
    184  1.4.4.1  msaitoh 
    185  1.4.4.1  msaitoh 	if (1 != SSL_CTX_use_PrivateKey_file(sslinfo->ssl_context,
    186  1.4.4.1  msaitoh 	    sslinfo->privatekey_file, SSL_FILETYPE_PEM))
    187  1.4.4.1  msaitoh 		bozo_ssl_err(httpd, EXIT_FAILURE,
    188  1.4.4.1  msaitoh 		    "Unable to use private key file '%s'",
    189  1.4.4.1  msaitoh 		    sslinfo->privatekey_file);
    190      1.1      tls 
    191      1.1      tls 	/* check consistency of key vs certificate */
    192  1.4.4.1  msaitoh 	if (!SSL_CTX_check_private_key(sslinfo->ssl_context))
    193  1.4.4.1  msaitoh 		bozo_ssl_err(httpd, EXIT_FAILURE,
    194  1.4.4.1  msaitoh 		    "Check private key failed");
    195      1.1      tls }
    196      1.1      tls 
    197      1.1      tls void
    198  1.4.4.1  msaitoh bozo_ssl_accept(bozohttpd_t *httpd)
    199      1.1      tls {
    200  1.4.4.1  msaitoh 	sslinfo_t	*sslinfo;
    201  1.4.4.1  msaitoh 
    202  1.4.4.1  msaitoh 	sslinfo = httpd->sslinfo;
    203  1.4.4.1  msaitoh 	if (sslinfo != NULL && sslinfo->ssl_context) {
    204  1.4.4.1  msaitoh 		sslinfo->bozossl = SSL_new(sslinfo->ssl_context);
    205  1.4.4.1  msaitoh 		SSL_set_rfd(sslinfo->bozossl, 0);
    206  1.4.4.1  msaitoh 		SSL_set_wfd(sslinfo->bozossl, 1);
    207  1.4.4.1  msaitoh 		SSL_accept(sslinfo->bozossl);
    208      1.1      tls 	}
    209      1.1      tls }
    210      1.1      tls 
    211      1.1      tls void
    212  1.4.4.1  msaitoh bozo_ssl_destroy(bozohttpd_t *httpd)
    213      1.1      tls {
    214  1.4.4.1  msaitoh 	sslinfo_t	*sslinfo;
    215  1.4.4.1  msaitoh 
    216  1.4.4.1  msaitoh 	sslinfo = httpd->sslinfo;
    217  1.4.4.1  msaitoh 	if (sslinfo && sslinfo->bozossl)
    218  1.4.4.1  msaitoh 		SSL_free(sslinfo->bozossl);
    219      1.1      tls }
    220      1.1      tls 
    221      1.1      tls void
    222  1.4.4.1  msaitoh bozo_ssl_set_opts(bozohttpd_t *httpd, const char *cert, const char *priv)
    223      1.1      tls {
    224  1.4.4.1  msaitoh 	sslinfo_t	*sslinfo;
    225  1.4.4.1  msaitoh 
    226  1.4.4.1  msaitoh 	if ((sslinfo = httpd->sslinfo) == NULL) {
    227  1.4.4.1  msaitoh 		sslinfo = bozomalloc(httpd, sizeof(*sslinfo));
    228  1.4.4.1  msaitoh 		if (sslinfo == NULL) {
    229  1.4.4.1  msaitoh 			bozo_err(httpd, 1, "sslinfo allocation failed");
    230  1.4.4.1  msaitoh 		}
    231  1.4.4.1  msaitoh 		httpd->sslinfo = sslinfo;
    232  1.4.4.1  msaitoh 	}
    233  1.4.4.1  msaitoh 	sslinfo->certificate_file = strdup(cert);
    234  1.4.4.1  msaitoh 	sslinfo->privatekey_file = strdup(priv);
    235  1.4.4.1  msaitoh 	debug((httpd, DEBUG_NORMAL, "using cert/priv files: %s & %s",
    236  1.4.4.1  msaitoh 		sslinfo->certificate_file,
    237  1.4.4.1  msaitoh 		sslinfo->privatekey_file));
    238  1.4.4.1  msaitoh 	if (!httpd->bindport) {
    239  1.4.4.1  msaitoh 		httpd->bindport = strdup("https");
    240  1.4.4.1  msaitoh 	}
    241      1.1      tls }
    242      1.1      tls 
    243  1.4.4.1  msaitoh #endif /* NO_SSL_SUPPORT */
    244      1.1      tls 
    245  1.4.4.1  msaitoh int
    246  1.4.4.1  msaitoh bozo_printf(bozohttpd_t *httpd, const char *fmt, ...)
    247  1.4.4.1  msaitoh {
    248  1.4.4.1  msaitoh 	va_list	args;
    249  1.4.4.1  msaitoh 	int	cc;
    250      1.1      tls 
    251  1.4.4.1  msaitoh 	va_start(args, fmt);
    252  1.4.4.1  msaitoh #ifndef NO_SSL_SUPPORT
    253  1.4.4.1  msaitoh 	if (httpd->sslinfo) {
    254  1.4.4.1  msaitoh 		cc = bozo_ssl_printf(httpd, fmt, args);
    255  1.4.4.1  msaitoh 		va_end(args);
    256  1.4.4.1  msaitoh 		return cc;
    257  1.4.4.1  msaitoh 	}
    258  1.4.4.1  msaitoh #endif
    259  1.4.4.1  msaitoh 	cc = vprintf(fmt, args);
    260  1.4.4.1  msaitoh 	va_end(args);
    261  1.4.4.1  msaitoh 	return cc;
    262      1.1      tls }
    263      1.1      tls 
    264  1.4.4.1  msaitoh ssize_t
    265  1.4.4.1  msaitoh bozo_read(bozohttpd_t *httpd, int fd, void *buf, size_t len)
    266      1.1      tls {
    267  1.4.4.1  msaitoh #ifndef NO_SSL_SUPPORT
    268  1.4.4.1  msaitoh 	if (httpd->sslinfo) {
    269  1.4.4.1  msaitoh 		return bozo_ssl_read(httpd, fd, buf, len);
    270      1.1      tls 	}
    271  1.4.4.1  msaitoh #endif
    272  1.4.4.1  msaitoh 	return read(fd, buf, len);
    273      1.1      tls }
    274      1.1      tls 
    275  1.4.4.1  msaitoh ssize_t
    276  1.4.4.1  msaitoh bozo_write(bozohttpd_t *httpd, int fd, const void *buf, size_t len)
    277      1.1      tls {
    278  1.4.4.1  msaitoh #ifndef NO_SSL_SUPPORT
    279  1.4.4.1  msaitoh 	if (httpd->sslinfo) {
    280  1.4.4.1  msaitoh 		return bozo_ssl_write(httpd, fd, buf, len);
    281  1.4.4.1  msaitoh 	}
    282  1.4.4.1  msaitoh #endif
    283  1.4.4.1  msaitoh 	return write(fd, buf, len);
    284      1.1      tls }
    285      1.1      tls 
    286  1.4.4.1  msaitoh int
    287  1.4.4.1  msaitoh bozo_flush(bozohttpd_t *httpd, FILE *fp)
    288      1.1      tls {
    289  1.4.4.1  msaitoh #ifndef NO_SSL_SUPPORT
    290  1.4.4.1  msaitoh 	if (httpd->sslinfo) {
    291  1.4.4.1  msaitoh 		return bozo_ssl_flush(httpd, fp);
    292  1.4.4.1  msaitoh 	}
    293  1.4.4.1  msaitoh #endif
    294  1.4.4.1  msaitoh 	return fflush(fp);
    295      1.1      tls }
    296