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