ssl-bozo.c revision 1.17 1 /* $NetBSD: ssl-bozo.c,v 1.17 2014/07/17 06:14:46 mrg Exp $ */
2
3 /* $eterna: ssl-bozo.c,v 1.15 2011/11/18 09:21:15 mrg Exp $ */
4
5 /*
6 * Copyright (c) 1997-2014 Matthew R. Green
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer and
16 * dedication in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 /* this code implements SSL for bozohttpd */
34
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <syslog.h>
38 #include <unistd.h>
39
40 #include "bozohttpd.h"
41
42 #ifndef NO_SSL_SUPPORT
43
44 #include <openssl/ssl.h>
45 #include <openssl/err.h>
46
47 #ifndef USE_ARG
48 #define USE_ARG(x) /*LINTED*/(void)&(x)
49 #endif
50
51 /* this structure encapsulates the ssl info */
52 typedef struct sslinfo_t {
53 SSL_CTX *ssl_context;
54 const SSL_METHOD *ssl_method;
55 SSL *bozossl;
56 char *certificate_file;
57 char *privatekey_file;
58 } sslinfo_t;
59
60 /*
61 * bozo_ssl_err
62 *
63 * bozo_ssl_err works just like bozo_err except in addition to printing
64 * the error provided by the caller at the point of error it pops and
65 * prints all errors from the SSL error queue.
66 */
67 BOZO_PRINTFLIKE(3, 4) BOZO_DEAD static void
68 bozo_ssl_err(bozohttpd_t *httpd, int code, const char *fmt, ...)
69 {
70 va_list ap;
71
72 va_start(ap, fmt);
73 if (httpd->logstderr || isatty(STDERR_FILENO)) {
74 vfprintf(stderr, fmt, ap);
75 fputs("\n", stderr);
76 } else
77 vsyslog(LOG_ERR, fmt, ap);
78 va_end(ap);
79
80 unsigned int sslcode = ERR_get_error();
81 do {
82 static const char sslfmt[] = "SSL Error: %s:%s:%s";
83
84 if (httpd->logstderr || isatty(STDERR_FILENO)) {
85 fprintf(stderr, sslfmt,
86 ERR_lib_error_string(sslcode),
87 ERR_func_error_string(sslcode),
88 ERR_reason_error_string(sslcode));
89 } else {
90 syslog(LOG_ERR, sslfmt,
91 ERR_lib_error_string(sslcode),
92 ERR_func_error_string(sslcode),
93 ERR_reason_error_string(sslcode));
94 }
95 } while (0 != (sslcode = ERR_get_error()));
96 exit(code);
97 }
98
99 static BOZO_PRINTFLIKE(2, 0) int
100 bozo_ssl_printf(bozohttpd_t *httpd, const char * fmt, va_list ap)
101 {
102 const sslinfo_t *sslinfo = httpd->sslinfo;
103 char *buf;
104 int nbytes;
105
106 /* XXX we need more elegant/proper handling of SSL_write return */
107 if ((nbytes = vasprintf(&buf, fmt, ap)) != -1)
108 SSL_write(sslinfo->bozossl, buf, nbytes);
109
110 free(buf);
111
112 return nbytes;
113 }
114
115 static ssize_t
116 bozo_ssl_read(bozohttpd_t *httpd, int fd, void *buf, size_t nbytes)
117 {
118 const sslinfo_t *sslinfo = httpd->sslinfo;
119 ssize_t rbytes;
120
121 USE_ARG(fd);
122 /* XXX we need elegant/proper handling of SSL_read return */
123 rbytes = (ssize_t)SSL_read(sslinfo->bozossl, buf, (int)nbytes);
124 if (rbytes < 1) {
125 if (SSL_get_error(sslinfo->bozossl, rbytes) ==
126 SSL_ERROR_WANT_READ)
127 bozo_warn(httpd, "SSL_ERROR_WANT_READ");
128 else
129 bozo_warn(httpd, "SSL_ERROR OTHER");
130 }
131
132 return rbytes;
133 }
134
135 static ssize_t
136 bozo_ssl_write(bozohttpd_t *httpd, int fd, const void *buf, size_t nbytes)
137 {
138 const sslinfo_t *sslinfo = httpd->sslinfo;
139 ssize_t wbytes;
140
141 USE_ARG(fd);
142 /* XXX we need elegant/proper handling of SSL_write return */
143 wbytes = (ssize_t)SSL_write(sslinfo->bozossl, buf, (int)nbytes);
144
145 return wbytes;
146 }
147
148 static int
149 bozo_ssl_flush(bozohttpd_t *httpd, FILE *fp)
150 {
151 USE_ARG(httpd);
152 USE_ARG(fp);
153 /* nothing to see here, move right along */
154 return 0;
155 }
156
157 void
158 bozo_ssl_init(bozohttpd_t *httpd)
159 {
160 sslinfo_t *sslinfo = httpd->sslinfo;
161
162 if (sslinfo == NULL || !sslinfo->certificate_file)
163 return;
164 SSL_library_init();
165 SSL_load_error_strings();
166
167 sslinfo->ssl_method = SSLv23_server_method();
168 sslinfo->ssl_context = SSL_CTX_new(sslinfo->ssl_method);
169
170 /* XXX we need to learn how to check the SSL stack for more info */
171 if (NULL == sslinfo->ssl_context)
172 bozo_ssl_err(httpd, EXIT_FAILURE,
173 "SSL context creation failed");
174
175 if (1 != SSL_CTX_use_certificate_chain_file(sslinfo->ssl_context,
176 sslinfo->certificate_file))
177 bozo_ssl_err(httpd, EXIT_FAILURE,
178 "Unable to use certificate file '%s'",
179 sslinfo->certificate_file);
180
181 if (1 != SSL_CTX_use_PrivateKey_file(sslinfo->ssl_context,
182 sslinfo->privatekey_file, SSL_FILETYPE_PEM))
183 bozo_ssl_err(httpd, EXIT_FAILURE,
184 "Unable to use private key file '%s'",
185 sslinfo->privatekey_file);
186
187 /* check consistency of key vs certificate */
188 if (!SSL_CTX_check_private_key(sslinfo->ssl_context))
189 bozo_ssl_err(httpd, EXIT_FAILURE,
190 "Check private key failed");
191 }
192
193 void
194 bozo_ssl_accept(bozohttpd_t *httpd)
195 {
196 sslinfo_t *sslinfo = httpd->sslinfo;
197
198 if (sslinfo != NULL && sslinfo->ssl_context) {
199 sslinfo->bozossl = SSL_new(sslinfo->ssl_context);
200 SSL_set_rfd(sslinfo->bozossl, 0);
201 SSL_set_wfd(sslinfo->bozossl, 1);
202 SSL_accept(sslinfo->bozossl);
203 }
204 }
205
206 void
207 bozo_ssl_destroy(bozohttpd_t *httpd)
208 {
209 const sslinfo_t *sslinfo = httpd->sslinfo;
210
211 if (sslinfo && sslinfo->bozossl)
212 SSL_free(sslinfo->bozossl);
213 }
214
215 void
216 bozo_ssl_set_opts(bozohttpd_t *httpd, const char *cert, const char *priv)
217 {
218 sslinfo_t *sslinfo = httpd->sslinfo;
219
220 if (sslinfo == NULL) {
221 sslinfo = bozomalloc(httpd, sizeof(*sslinfo));
222 if (sslinfo == NULL) {
223 bozo_err(httpd, 1, "sslinfo allocation failed");
224 }
225 httpd->sslinfo = sslinfo;
226 }
227 sslinfo->certificate_file = strdup(cert);
228 sslinfo->privatekey_file = strdup(priv);
229 debug((httpd, DEBUG_NORMAL, "using cert/priv files: %s & %s",
230 sslinfo->certificate_file,
231 sslinfo->privatekey_file));
232 if (!httpd->bindport)
233 httpd->bindport = strdup("https");
234 }
235
236 #endif /* NO_SSL_SUPPORT */
237
238 int
239 bozo_printf(bozohttpd_t *httpd, const char *fmt, ...)
240 {
241 va_list args;
242 int cc;
243
244 va_start(args, fmt);
245 #ifndef NO_SSL_SUPPORT
246 if (httpd->sslinfo)
247 cc = bozo_ssl_printf(httpd, fmt, args);
248 else
249 #endif
250 cc = vprintf(fmt, args);
251 va_end(args);
252 return cc;
253 }
254
255 ssize_t
256 bozo_read(bozohttpd_t *httpd, int fd, void *buf, size_t len)
257 {
258 #ifndef NO_SSL_SUPPORT
259 if (httpd->sslinfo)
260 return bozo_ssl_read(httpd, fd, buf, len);
261 #endif
262 return read(fd, buf, len);
263 }
264
265 ssize_t
266 bozo_write(bozohttpd_t *httpd, int fd, const void *buf, size_t len)
267 {
268 #ifndef NO_SSL_SUPPORT
269 if (httpd->sslinfo)
270 return bozo_ssl_write(httpd, fd, buf, len);
271 #endif
272 return write(fd, buf, len);
273 }
274
275 int
276 bozo_flush(bozohttpd_t *httpd, FILE *fp)
277 {
278 #ifndef NO_SSL_SUPPORT
279 if (httpd->sslinfo)
280 return bozo_ssl_flush(httpd, fp);
281 #endif
282 return fflush(fp);
283 }
284