Commit | Line | Data |
---|---|---|
6ca53b2e FT |
1 | /* |
2 | ashd - A Sane HTTP Daemon | |
3 | Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | ||
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation, either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include <stdlib.h> | |
20 | #include <unistd.h> | |
21 | #include <string.h> | |
22 | #include <fcntl.h> | |
26902200 | 23 | #include <dirent.h> |
6ca53b2e FT |
24 | #include <sys/socket.h> |
25 | #include <netinet/in.h> | |
26 | #include <arpa/inet.h> | |
27 | #include <errno.h> | |
28 | ||
29 | #ifdef HAVE_CONFIG_H | |
30 | #include <config.h> | |
31 | #endif | |
32 | #include <utils.h> | |
33 | #include <mt.h> | |
34 | #include <mtio.h> | |
35 | #include <req.h> | |
36 | #include <log.h> | |
fc253e2f | 37 | #include <bufio.h> |
6ca53b2e FT |
38 | |
39 | #include "htparser.h" | |
40 | ||
41 | #ifdef HAVE_GNUTLS | |
42 | ||
43 | #include <gnutls/gnutls.h> | |
28b2e619 FT |
44 | #include <gnutls/x509.h> |
45 | ||
46 | struct namedcreds { | |
47 | char **names; | |
48 | gnutls_certificate_credentials_t creds; | |
49 | }; | |
50 | ||
51 | struct ncredbuf { | |
52 | struct namedcreds **b; | |
53 | size_t s, d; | |
54 | }; | |
6ca53b2e FT |
55 | |
56 | struct sslport { | |
bcd711f0 | 57 | int fd, sport, clreq; |
6ca53b2e | 58 | gnutls_certificate_credentials_t creds; |
76d6825e | 59 | gnutls_priority_t ciphers; |
28b2e619 | 60 | struct namedcreds **ncreds; |
6ca53b2e FT |
61 | }; |
62 | ||
63 | struct sslconn { | |
64 | int fd; | |
65 | struct sslport *port; | |
66 | struct sockaddr_storage name; | |
67 | gnutls_session_t sess; | |
68 | struct charbuf in; | |
69 | }; | |
70 | ||
d8d4ed57 FT |
71 | struct savedsess { |
72 | struct savedsess *next, *prev; | |
73 | gnutls_datum_t key, value; | |
74 | }; | |
75 | ||
7a59f41f FT |
76 | struct certbuffer { |
77 | gnutls_x509_crt_t *b; | |
78 | size_t s, d; | |
79 | }; | |
80 | ||
d8d4ed57 FT |
81 | static int numconn = 0, numsess = 0; |
82 | static struct btree *sessidx = NULL; | |
83 | static struct savedsess *sesslistf = NULL, *sesslistl = NULL; | |
84 | ||
85 | static int sesscmp(void *ap, void *bp) | |
86 | { | |
87 | struct savedsess *a = ap, *b = bp; | |
88 | ||
89 | if(a->key.size != b->key.size) | |
90 | return(a->key.size - b->key.size); | |
91 | return(memcmp(a->key.data, b->key.data, a->key.size)); | |
92 | } | |
93 | ||
94 | static gnutls_datum_t sessdbfetch(void *uudata, gnutls_datum_t key) | |
95 | { | |
96 | struct savedsess *sess, lkey; | |
97 | gnutls_datum_t ret; | |
98 | ||
99 | memset(&ret, 0, sizeof(ret)); | |
100 | lkey.key = key; | |
101 | if((sess = btreeget(sessidx, &lkey, sesscmp)) == NULL) | |
102 | return(ret); | |
103 | ret.data = memcpy(gnutls_malloc(ret.size = sess->value.size), sess->value.data, sess->value.size); | |
104 | return(ret); | |
105 | } | |
106 | ||
107 | static void freesess(struct savedsess *sess) | |
108 | { | |
109 | bbtreedel(&sessidx, sess, sesscmp); | |
110 | if(sess->next) | |
111 | sess->next->prev = sess->prev; | |
112 | if(sess->prev) | |
113 | sess->prev->next = sess->next; | |
114 | if(sess == sesslistf) | |
115 | sesslistf = sess->next; | |
116 | if(sess == sesslistl) | |
117 | sesslistl = sess->prev; | |
118 | free(sess->key.data); | |
119 | free(sess->value.data); | |
120 | free(sess); | |
121 | numsess--; | |
122 | } | |
123 | ||
124 | static int sessdbdel(void *uudata, gnutls_datum_t key) | |
125 | { | |
126 | struct savedsess *sess, lkey; | |
127 | ||
128 | lkey.key = key; | |
129 | if((sess = btreeget(sessidx, &lkey, sesscmp)) == NULL) | |
130 | return(-1); | |
131 | freesess(sess); | |
132 | return(0); | |
133 | } | |
134 | ||
135 | static void cleansess(void) | |
136 | { | |
137 | while(numsess > (max(numconn, 1) * 100)) | |
138 | freesess(sesslistl); | |
139 | } | |
140 | ||
141 | static int sessdbstore(void *uudata, gnutls_datum_t key, gnutls_datum_t value) | |
142 | { | |
143 | static int cc = 0; | |
144 | struct savedsess *sess, lkey; | |
145 | ||
146 | if((value.data == NULL) || (value.size == 0)) { | |
147 | sessdbdel(NULL, key); | |
148 | return(0); | |
149 | } | |
150 | lkey.key = key; | |
151 | if((sess = btreeget(sessidx, &lkey, sesscmp)) == NULL) { | |
152 | omalloc(sess); | |
153 | sess->key.data = memcpy(smalloc(sess->key.size = key.size), key.data, key.size); | |
154 | sess->value.data = memcpy(smalloc(sess->value.size = value.size), value.data, value.size); | |
155 | bbtreeput(&sessidx, sess, sesscmp); | |
156 | sess->prev = NULL; | |
157 | sess->next = sesslistf; | |
158 | if(sesslistf) | |
159 | sesslistf->prev = sess; | |
160 | sesslistf = sess; | |
161 | if(sesslistl == NULL) | |
162 | sesslistl = sess; | |
163 | numsess++; | |
164 | } else { | |
165 | free(sess->value.data); | |
166 | sess->value.data = memcpy(smalloc(sess->value.size = value.size), value.data, value.size); | |
167 | if(sess != sesslistf) { | |
168 | if(sess->next) | |
169 | sess->next->prev = sess->prev; | |
170 | if(sess->prev) | |
171 | sess->prev->next = sess->next; | |
172 | if(sess == sesslistl) | |
173 | sesslistl = sess->prev; | |
174 | sess->prev = NULL; | |
175 | sess->next = sesslistf; | |
176 | if(sesslistf) | |
177 | sesslistf->prev = sess; | |
178 | sesslistf = sess; | |
179 | } | |
180 | } | |
181 | if(cc++ > 100) { | |
182 | cleansess(); | |
183 | cc = 0; | |
184 | } | |
185 | return(0); | |
186 | } | |
187 | ||
6ca53b2e FT |
188 | static int tlsblock(int fd, gnutls_session_t sess, time_t to) |
189 | { | |
190 | if(gnutls_record_get_direction(sess)) | |
191 | return(block(fd, EV_WRITE, to)); | |
192 | else | |
193 | return(block(fd, EV_READ, to)); | |
194 | } | |
195 | ||
fc253e2f | 196 | static ssize_t sslread(void *cookie, void *buf, size_t len) |
6ca53b2e FT |
197 | { |
198 | struct sslconn *ssl = cookie; | |
199 | ssize_t xf; | |
200 | int ret; | |
201 | ||
202 | while(ssl->in.d == 0) { | |
203 | sizebuf(ssl->in, ssl->in.d + 1024); | |
204 | ret = gnutls_record_recv(ssl->sess, ssl->in.b + ssl->in.d, ssl->in.s - ssl->in.d); | |
205 | if((ret == GNUTLS_E_INTERRUPTED) || (ret == GNUTLS_E_AGAIN)) { | |
206 | if(tlsblock(ssl->fd, ssl->sess, 60) == 0) { | |
207 | errno = ETIMEDOUT; | |
208 | return(-1); | |
209 | } | |
210 | } else if(ret < 0) { | |
211 | errno = EIO; | |
212 | return(-1); | |
213 | } else if(ret == 0) { | |
214 | return(0); | |
215 | } else { | |
216 | ssl->in.d += ret; | |
217 | } | |
218 | } | |
219 | xf = min(ssl->in.d, len); | |
220 | memcpy(buf, ssl->in.b, xf); | |
221 | memmove(ssl->in.b, ssl->in.b + xf, ssl->in.d -= xf); | |
222 | return(xf); | |
223 | } | |
224 | ||
fc253e2f | 225 | static ssize_t sslwrite(void *cookie, const void *buf, size_t len) |
6ca53b2e FT |
226 | { |
227 | struct sslconn *ssl = cookie; | |
228 | int ret; | |
229 | size_t off; | |
230 | ||
231 | off = 0; | |
232 | while(off < len) { | |
233 | ret = gnutls_record_send(ssl->sess, buf + off, len - off); | |
234 | if((ret == GNUTLS_E_INTERRUPTED) || (ret == GNUTLS_E_AGAIN)) { | |
235 | if(tlsblock(ssl->fd, ssl->sess, 60) == 0) { | |
236 | if(off == 0) { | |
237 | errno = ETIMEDOUT; | |
238 | return(-1); | |
239 | } | |
240 | return(off); | |
241 | } | |
242 | } else if(ret < 0) { | |
243 | if(off == 0) { | |
244 | errno = EIO; | |
245 | return(-1); | |
246 | } | |
247 | return(off); | |
248 | } else { | |
249 | off += ret; | |
250 | } | |
251 | } | |
252 | return(off); | |
253 | } | |
254 | ||
255 | static int sslclose(void *cookie) | |
256 | { | |
5ba5dd0b FT |
257 | struct sslconn *ssl = cookie; |
258 | ||
259 | buffree(ssl->in); | |
6ca53b2e FT |
260 | return(0); |
261 | } | |
262 | ||
fc253e2f | 263 | static struct bufioops iofuns = { |
6ca53b2e FT |
264 | .read = sslread, |
265 | .write = sslwrite, | |
266 | .close = sslclose, | |
267 | }; | |
268 | ||
269 | static int initreq(struct conn *conn, struct hthead *req) | |
270 | { | |
271 | struct sslconn *ssl = conn->pdata; | |
6b84641a FT |
272 | struct sockaddr_storage sa; |
273 | socklen_t salen; | |
6ca53b2e | 274 | |
7595e3a4 | 275 | headappheader(req, "X-Ash-Address", formathaddress((struct sockaddr *)&ssl->name, sizeof(sa))); |
a5f63653 | 276 | if(ssl->name.ss_family == AF_INET) |
6ca53b2e | 277 | headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&ssl->name)->sin_port))); |
a5f63653 | 278 | else if(ssl->name.ss_family == AF_INET6) |
6ca53b2e | 279 | headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&ssl->name)->sin6_port))); |
6b84641a | 280 | salen = sizeof(sa); |
7595e3a4 FT |
281 | if(!getsockname(ssl->fd, (struct sockaddr *)&sa, &salen)) |
282 | headappheader(req, "X-Ash-Server-Address", formathaddress((struct sockaddr *)&sa, sizeof(sa))); | |
6ca53b2e FT |
283 | headappheader(req, "X-Ash-Server-Port", sprintf3("%i", ssl->port->sport)); |
284 | headappheader(req, "X-Ash-Protocol", "https"); | |
285 | return(0); | |
286 | } | |
287 | ||
288 | static void servessl(struct muth *muth, va_list args) | |
289 | { | |
290 | vavar(int, fd); | |
291 | vavar(struct sockaddr_storage, name); | |
292 | vavar(struct sslport *, pd); | |
293 | struct conn conn; | |
294 | struct sslconn ssl; | |
295 | gnutls_session_t sess; | |
296 | int ret; | |
6ca53b2e | 297 | |
1b77e192 FT |
298 | int setcreds(gnutls_session_t sess) |
299 | { | |
28b2e619 | 300 | int i, o, u; |
1b77e192 FT |
301 | unsigned int ntype; |
302 | char nambuf[256]; | |
303 | size_t namlen; | |
304 | ||
305 | for(i = 0; 1; i++) { | |
306 | namlen = sizeof(nambuf); | |
307 | if(gnutls_server_name_get(sess, nambuf, &namlen, &ntype, i) != 0) | |
308 | break; | |
309 | if(ntype != GNUTLS_NAME_DNS) | |
310 | continue; | |
28b2e619 FT |
311 | for(o = 0; pd->ncreds[o] != NULL; o++) { |
312 | for(u = 0; pd->ncreds[o]->names[u] != NULL; u++) { | |
313 | if(!strcmp(pd->ncreds[o]->names[u], nambuf)) { | |
314 | gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->ncreds[o]->creds); | |
bcd711f0 FT |
315 | if(pd->clreq) |
316 | gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST); | |
28b2e619 FT |
317 | return(0); |
318 | } | |
319 | } | |
320 | } | |
1b77e192 FT |
321 | } |
322 | gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->creds); | |
bcd711f0 FT |
323 | if(pd->clreq) |
324 | gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST); | |
1b77e192 FT |
325 | return(0); |
326 | } | |
327 | ||
d8d4ed57 | 328 | numconn++; |
6ca53b2e FT |
329 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
330 | gnutls_init(&sess, GNUTLS_SERVER); | |
76d6825e | 331 | gnutls_priority_set(sess, pd->ciphers); |
d8d4ed57 FT |
332 | gnutls_db_set_retrieve_function(sess, sessdbfetch); |
333 | gnutls_db_set_store_function(sess, sessdbstore); | |
334 | gnutls_db_set_remove_function(sess, sessdbdel); | |
335 | gnutls_db_set_ptr(sess, NULL); | |
1b77e192 | 336 | gnutls_handshake_set_post_client_hello_function(sess, setcreds); |
6ca53b2e FT |
337 | gnutls_transport_set_ptr(sess, (gnutls_transport_ptr_t)(intptr_t)fd); |
338 | while((ret = gnutls_handshake(sess)) != 0) { | |
339 | if((ret != GNUTLS_E_INTERRUPTED) && (ret != GNUTLS_E_AGAIN)) | |
340 | goto out; | |
341 | if(tlsblock(fd, sess, 60) <= 0) | |
342 | goto out; | |
343 | } | |
344 | memset(&conn, 0, sizeof(conn)); | |
345 | memset(&ssl, 0, sizeof(ssl)); | |
346 | conn.pdata = &ssl; | |
347 | conn.initreq = initreq; | |
348 | ssl.fd = fd; | |
349 | ssl.port = pd; | |
350 | ssl.name = name; | |
351 | ssl.sess = sess; | |
352 | bufinit(ssl.in); | |
a68db17d | 353 | serve(bioopen(&ssl, &iofuns), fd, &conn); |
6ca53b2e FT |
354 | |
355 | out: | |
356 | gnutls_deinit(sess); | |
357 | close(fd); | |
d8d4ed57 | 358 | numconn--; |
6ca53b2e FT |
359 | } |
360 | ||
361 | static void listenloop(struct muth *muth, va_list args) | |
362 | { | |
363 | vavar(struct sslport *, pd); | |
f24b7bb5 | 364 | int i, ns, n; |
6ca53b2e FT |
365 | struct sockaddr_storage name; |
366 | socklen_t namelen; | |
367 | ||
3ae56649 | 368 | fcntl(pd->fd, F_SETFL, fcntl(pd->fd, F_GETFL) | O_NONBLOCK); |
6ca53b2e FT |
369 | while(1) { |
370 | namelen = sizeof(name); | |
cac13158 FT |
371 | if(block(pd->fd, EV_READ, 0) == 0) |
372 | goto out; | |
f24b7bb5 FT |
373 | n = 0; |
374 | while(1) { | |
375 | ns = accept(pd->fd, (struct sockaddr *)&name, &namelen); | |
376 | if(ns < 0) { | |
377 | if(errno == EAGAIN) | |
378 | break; | |
51893006 FT |
379 | if(errno == ECONNABORTED) |
380 | continue; | |
f24b7bb5 FT |
381 | flog(LOG_ERR, "accept: %s", strerror(errno)); |
382 | goto out; | |
383 | } | |
384 | mustart(servessl, ns, name, pd); | |
385 | if(++n >= 100) | |
386 | break; | |
6ca53b2e | 387 | } |
6ca53b2e FT |
388 | } |
389 | ||
390 | out: | |
391 | close(pd->fd); | |
392 | free(pd); | |
8e9ec020 FT |
393 | for(i = 0; i < listeners.d; i++) { |
394 | if(listeners.b[i] == muth) | |
395 | bufdel(listeners, i); | |
396 | } | |
6ca53b2e FT |
397 | } |
398 | ||
2daf4411 FT |
399 | static gnutls_dh_params_t dhparams(void) |
400 | { | |
401 | static int inited = 0; | |
402 | static gnutls_dh_params_t pars; | |
403 | int ret; | |
404 | ||
405 | if(!inited) { | |
406 | if(((ret = gnutls_dh_params_init(&pars)) != 0) || | |
407 | ((ret = gnutls_dh_params_generate2(pars, 2048)) != 0)) { | |
408 | flog(LOG_ERR, "GnuTLS could not generate Diffie-Hellman parameters: %s", gnutls_strerror(ret)); | |
409 | exit(1); | |
410 | } | |
411 | inited = 1; | |
412 | } | |
413 | return(pars); | |
414 | } | |
415 | ||
6ca53b2e FT |
416 | static void init(void) |
417 | { | |
418 | static int inited = 0; | |
419 | int ret; | |
420 | ||
421 | if(inited) | |
422 | return; | |
423 | inited = 1; | |
424 | if((ret = gnutls_global_init()) != 0) { | |
425 | flog(LOG_ERR, "could not initialize GnuTLS: %s", gnutls_strerror(ret)); | |
426 | exit(1); | |
427 | } | |
6ca53b2e FT |
428 | } |
429 | ||
7a59f41f FT |
430 | /* This implementation seems somewhat ugly, but it's the way the |
431 | * GnuTLS implements the same thing internally, so it should probably | |
432 | * be interoperable, at least. */ | |
433 | static int readcrtchain(struct certbuffer *ret, struct charbuf *pem) | |
434 | { | |
435 | static char *headers[] = {"-----BEGIN CERTIFICATE", "-----BEGIN X509 CERTIFICATE"}; | |
436 | int i, rv; | |
437 | char *p, *p2, *f; | |
438 | gnutls_x509_crt_t crt; | |
439 | ||
440 | for(i = 0, p = NULL; i < sizeof(headers) / sizeof(*headers); i++) { | |
441 | f = memmem(pem->b, pem->d, headers[i], strlen(headers[i])); | |
2e90aa50 | 442 | if((f != NULL) && ((p == NULL) || (f < p))) |
7a59f41f FT |
443 | p = f; |
444 | } | |
445 | if(p == NULL) | |
446 | return(-GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); | |
447 | do { | |
448 | if((rv = gnutls_x509_crt_init(&crt)) < 0) | |
449 | goto error; | |
450 | if((rv = gnutls_x509_crt_import(crt, &(gnutls_datum_t){.data = (unsigned char *)p, .size = pem->d - (p - pem->b)}, GNUTLS_X509_FMT_PEM)) < 0) { | |
451 | gnutls_x509_crt_deinit(crt); | |
452 | goto error; | |
453 | } | |
454 | bufadd(*ret, crt); | |
455 | for(i = 0, p2 = NULL; i < sizeof(headers) / sizeof(*headers); i++) { | |
456 | f = memmem(p + 1, pem->d - (p + 1 - pem->b), headers[i], strlen(headers[i])); | |
2e90aa50 | 457 | if((f != NULL) && ((p2 == NULL) || (f < p2))) |
7a59f41f FT |
458 | p2 = f; |
459 | } | |
460 | } while((p = p2) != NULL); | |
461 | return(0); | |
462 | error: | |
463 | for(i = 0; i < ret->d; i++) | |
464 | gnutls_x509_crt_deinit(ret->b[i]); | |
465 | ret->d = 0; | |
466 | return(rv); | |
467 | } | |
468 | ||
469 | static struct namedcreds *readncreds(char *file, gnutls_x509_privkey_t defkey) | |
28b2e619 FT |
470 | { |
471 | int i, fd, ret; | |
472 | struct namedcreds *nc; | |
7a59f41f | 473 | struct certbuffer crts; |
28b2e619 FT |
474 | gnutls_x509_privkey_t key; |
475 | char cn[1024]; | |
476 | size_t cnl; | |
28b2e619 FT |
477 | struct charbuf keybuf; |
478 | struct charvbuf names; | |
479 | unsigned int type; | |
480 | ||
481 | bufinit(keybuf); | |
7a59f41f | 482 | bufinit(crts); |
28b2e619 FT |
483 | bufinit(names); |
484 | if((fd = open(file, O_RDONLY)) < 0) { | |
485 | flog(LOG_ERR, "ssl: %s: %s", file, strerror(errno)); | |
486 | exit(1); | |
487 | } | |
488 | while(1) { | |
489 | sizebuf(keybuf, keybuf.d + 1024); | |
490 | ret = read(fd, keybuf.b + keybuf.d, keybuf.s - keybuf.d); | |
491 | if(ret < 0) { | |
492 | flog(LOG_ERR, "ssl: reading from %s: %s", file, strerror(errno)); | |
493 | exit(1); | |
494 | } else if(ret == 0) { | |
495 | break; | |
496 | } | |
497 | keybuf.d += ret; | |
498 | } | |
499 | close(fd); | |
7a59f41f FT |
500 | if((ret = readcrtchain(&crts, &keybuf)) != 0) { |
501 | flog(LOG_ERR, "ssl: could not load certificate chain from %s: %s", file, gnutls_strerror(ret)); | |
28b2e619 FT |
502 | exit(1); |
503 | } | |
504 | cnl = sizeof(cn) - 1; | |
7a59f41f | 505 | if((ret = gnutls_x509_crt_get_dn_by_oid(crts.b[0], GNUTLS_OID_X520_COMMON_NAME, 0, 0, cn, &cnl)) != 0) { |
28b2e619 FT |
506 | flog(LOG_ERR, "ssl: could not read common name from %s: %s", file, gnutls_strerror(ret)); |
507 | exit(1); | |
508 | } | |
509 | cn[cnl] = 0; | |
510 | bufadd(names, sstrdup(cn)); | |
511 | for(i = 0; 1; i++) { | |
512 | cnl = sizeof(cn) - 1; | |
7a59f41f | 513 | if(gnutls_x509_crt_get_subject_alt_name2(crts.b[0], i, cn, &cnl, &type, NULL) < 0) |
28b2e619 FT |
514 | break; |
515 | cn[cnl] = 0; | |
516 | if(type == GNUTLS_SAN_DNSNAME) | |
517 | bufadd(names, sstrdup(cn)); | |
518 | } | |
519 | gnutls_x509_privkey_init(&key); | |
7a59f41f FT |
520 | if((ret = gnutls_x509_privkey_import(key, &(gnutls_datum_t){.data = (unsigned char *)keybuf.b, .size = keybuf.d}, GNUTLS_X509_FMT_PEM)) != 0) { |
521 | if(ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { | |
522 | gnutls_x509_privkey_deinit(key); | |
523 | key = defkey; | |
524 | } else { | |
525 | flog(LOG_ERR, "ssl: could not load key from %s: %s", file, gnutls_strerror(ret)); | |
526 | exit(1); | |
527 | } | |
28b2e619 FT |
528 | } |
529 | buffree(keybuf); | |
530 | bufadd(names, NULL); | |
531 | omalloc(nc); | |
532 | nc->names = names.b; | |
533 | gnutls_certificate_allocate_credentials(&nc->creds); | |
7a59f41f | 534 | if((ret = gnutls_certificate_set_x509_key(nc->creds, crts.b, crts.d, key)) != 0) { |
28b2e619 FT |
535 | flog(LOG_ERR, "ssl: could not use certificate from %s: %s", file, gnutls_strerror(ret)); |
536 | exit(1); | |
537 | } | |
2daf4411 | 538 | gnutls_certificate_set_dh_params(nc->creds, dhparams()); |
28b2e619 FT |
539 | return(nc); |
540 | } | |
541 | ||
7a59f41f | 542 | static void readncdir(struct ncredbuf *buf, char *dir, gnutls_x509_privkey_t defkey) |
26902200 FT |
543 | { |
544 | DIR *d; | |
545 | struct dirent *e; | |
546 | size_t es; | |
547 | ||
548 | if((d = opendir(dir)) == NULL) { | |
549 | flog(LOG_ERR, "ssl: could not read certificate directory %s: %s", dir, strerror(errno)); | |
550 | exit(1); | |
551 | } | |
552 | while((e = readdir(d)) != NULL) { | |
553 | if(e->d_name[0] == '.') | |
554 | continue; | |
555 | if((es = strlen(e->d_name)) <= 4) | |
556 | continue; | |
557 | if(strcmp(e->d_name + es - 4, ".crt")) | |
558 | continue; | |
7a59f41f | 559 | bufadd(*buf, readncreds(sprintf3("%s/%s", dir, e->d_name), defkey)); |
26902200 FT |
560 | } |
561 | closedir(d); | |
562 | } | |
563 | ||
6ca53b2e FT |
564 | void handlegnussl(int argc, char **argp, char **argv) |
565 | { | |
bcd711f0 | 566 | int i, ret, port, fd, clreq; |
6ca53b2e | 567 | gnutls_certificate_credentials_t creds; |
76d6825e | 568 | gnutls_priority_t ciphers; |
7a59f41f | 569 | gnutls_x509_privkey_t defkey; |
28b2e619 | 570 | struct ncredbuf ncreds; |
7a59f41f | 571 | struct charvbuf ncertf, ncertd; |
6ca53b2e | 572 | struct sslport *pd; |
76d6825e | 573 | char *crtfile, *keyfile, *perr; |
6ca53b2e FT |
574 | |
575 | init(); | |
576 | port = 443; | |
bcd711f0 | 577 | clreq = 0; |
28b2e619 | 578 | bufinit(ncreds); |
7a59f41f FT |
579 | bufinit(ncertf); |
580 | bufinit(ncertd); | |
6ca53b2e FT |
581 | gnutls_certificate_allocate_credentials(&creds); |
582 | keyfile = crtfile = NULL; | |
76d6825e | 583 | ciphers = NULL; |
6ca53b2e FT |
584 | for(i = 0; i < argc; i++) { |
585 | if(!strcmp(argp[i], "help")) { | |
586 | printf("ssl handler parameters:\n"); | |
587 | printf("\tcert=CERT-FILE [mandatory]\n"); | |
588 | printf("\t\tThe name of the file to read the certificate from.\n"); | |
589 | printf("\tkey=KEY-FILE [same as CERT-FILE]\n"); | |
590 | printf("\t\tThe name of the file to read the private key from.\n"); | |
76d6825e FT |
591 | printf("\tprio=PRIORITIES [NORMAL]\n"); |
592 | printf("\t\tCiphersuite priorities, as a GnuTLS priority string.\n"); | |
6ca53b2e FT |
593 | printf("\ttrust=CA-FILE [no default]\n"); |
594 | printf("\t\tThe name of a file to read trusted certificates from.\n"); | |
595 | printf("\t\tMay be given multiple times.\n"); | |
596 | printf("\tcrl=CRL-FILE [no default]\n"); | |
597 | printf("\t\tThe name of a file to read revocation lists from.\n"); | |
598 | printf("\t\tMay be given multiple times.\n"); | |
4094af22 FT |
599 | printf("\tncert=CERT-FILE [no default]\n"); |
600 | printf("\t\tThe name of a file to read a named certificate from,\n"); | |
601 | printf("\t\tfor use with SNI-enabled clients.\n"); | |
602 | printf("\t\tMay be given multiple times.\n"); | |
603 | printf("\tncertdir=DIR [no default]\n"); | |
604 | printf("\t\tRead all *.crt files in the given directory as if they\n"); | |
605 | printf("\t\twere given with `ncert' options.\n"); | |
606 | printf("\t\tMay be given multiple times.\n"); | |
6ca53b2e FT |
607 | printf("\tport=PORT [443]\n"); |
608 | printf("\t\tThe TCP port to listen on.\n"); | |
609 | printf("\n"); | |
610 | printf("\tAll X.509 data files must be PEM-encoded.\n"); | |
4094af22 FT |
611 | printf("\tIf any certificates were given with `ncert' options, they will be\n"); |
612 | printf("\tused if a client explicitly names one of them with a\n"); | |
613 | printf("\tserver-name indication. If a client indicates no server name,\n"); | |
614 | printf("\tor if a server-name indication does not match any given\n"); | |
615 | printf("\tcertificate, the certificate given with the `cert' option will\n"); | |
616 | printf("\tbe used instead.\n"); | |
6ca53b2e FT |
617 | exit(0); |
618 | } else if(!strcmp(argp[i], "cert")) { | |
619 | crtfile = argv[i]; | |
620 | } else if(!strcmp(argp[i], "key")) { | |
621 | keyfile = argv[i]; | |
76d6825e FT |
622 | } else if(!strcmp(argp[i], "prio")) { |
623 | if(ciphers != NULL) | |
624 | gnutls_priority_deinit(ciphers); | |
625 | ret = gnutls_priority_init(&ciphers, argv[i], (const char **)&perr); | |
626 | if(ret == GNUTLS_E_INVALID_REQUEST) { | |
627 | flog(LOG_ERR, "ssl: invalid cipher priority string, at `%s'", perr); | |
628 | exit(1); | |
629 | } else if(ret != 0) { | |
630 | flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret)); | |
631 | exit(1); | |
632 | } | |
6ca53b2e FT |
633 | } else if(!strcmp(argp[i], "trust")) { |
634 | if((ret = gnutls_certificate_set_x509_trust_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
635 | flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret)); | |
636 | exit(1); | |
637 | } | |
28b2e619 FT |
638 | for(i = 0; i < ncreds.d; i++) { |
639 | if((ret = gnutls_certificate_set_x509_trust_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
640 | flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret)); | |
641 | exit(1); | |
642 | } | |
643 | } | |
bcd711f0 | 644 | clreq = 1; |
6ca53b2e FT |
645 | } else if(!strcmp(argp[i], "crl")) { |
646 | if((ret = gnutls_certificate_set_x509_crl_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
647 | flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret)); | |
648 | exit(1); | |
649 | } | |
28b2e619 FT |
650 | for(i = 0; i < ncreds.d; i++) { |
651 | if((ret = gnutls_certificate_set_x509_crl_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
652 | flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret)); | |
653 | exit(1); | |
654 | } | |
655 | } | |
bcd711f0 | 656 | clreq = 1; |
6ca53b2e FT |
657 | } else if(!strcmp(argp[i], "port")) { |
658 | port = atoi(argv[i]); | |
28b2e619 | 659 | } else if(!strcmp(argp[i], "ncert")) { |
7a59f41f | 660 | bufadd(ncertf, argv[i]); |
26902200 | 661 | } else if(!strcmp(argp[i], "ncertdir")) { |
7a59f41f | 662 | bufadd(ncertd, argv[i]); |
6ca53b2e FT |
663 | } else { |
664 | flog(LOG_ERR, "unknown parameter `%s' to ssl handler", argp[i]); | |
665 | exit(1); | |
666 | } | |
667 | } | |
668 | if(crtfile == NULL) { | |
669 | flog(LOG_ERR, "ssl: needs certificate file at the very least"); | |
670 | exit(1); | |
671 | } | |
2daf4411 FT |
672 | if((fd = listensock6(port)) < 0) { |
673 | flog(LOG_ERR, "could not listen on IPv6 port (port %i): %s", port, strerror(errno)); | |
674 | exit(1); | |
675 | } | |
6ca53b2e FT |
676 | if(keyfile == NULL) |
677 | keyfile = crtfile; | |
678 | if((ret = gnutls_certificate_set_x509_key_file(creds, crtfile, keyfile, GNUTLS_X509_FMT_PEM)) != 0) { | |
679 | flog(LOG_ERR, "ssl: could not load certificate or key: %s", gnutls_strerror(ret)); | |
680 | exit(1); | |
681 | } | |
76d6825e FT |
682 | if((ciphers == NULL) && ((ret = gnutls_priority_init(&ciphers, "NORMAL", NULL)) != 0)) { |
683 | flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret)); | |
684 | exit(1); | |
685 | } | |
7a59f41f FT |
686 | if((ret = gnutls_certificate_get_x509_key(creds, 0, &defkey)) != 0) { |
687 | flog(LOG_ERR, "ssl: could not get default key: %s", gnutls_strerror(ret)); | |
688 | exit(1); | |
689 | } | |
690 | for(i = 0; i < ncertf.d; i++) | |
691 | bufadd(ncreds, readncreds(ncertf.b[i], defkey)); | |
692 | for(i = 0; i < ncertd.d; i++) | |
693 | readncdir(&ncreds, ncertd.b[i], defkey); | |
694 | buffree(ncertf); | |
695 | buffree(ncertd); | |
2daf4411 | 696 | gnutls_certificate_set_dh_params(creds, dhparams()); |
28b2e619 | 697 | bufadd(ncreds, NULL); |
6ca53b2e FT |
698 | omalloc(pd); |
699 | pd->fd = fd; | |
700 | pd->sport = port; | |
bcd711f0 | 701 | pd->clreq = clreq; |
6ca53b2e | 702 | pd->creds = creds; |
28b2e619 | 703 | pd->ncreds = ncreds.b; |
76d6825e | 704 | pd->ciphers = ciphers; |
cac13158 | 705 | bufadd(listeners, mustart(listenloop, pd)); |
aa06d1b3 | 706 | if((fd = listensock4(port)) < 0) { |
6ca53b2e | 707 | if(errno != EADDRINUSE) { |
aa06d1b3 | 708 | flog(LOG_ERR, "could not listen on IPv4 port (port %i): %s", port, strerror(errno)); |
6ca53b2e FT |
709 | exit(1); |
710 | } | |
711 | } else { | |
712 | omalloc(pd); | |
713 | pd->fd = fd; | |
714 | pd->sport = port; | |
715 | pd->creds = creds; | |
76d6825e FT |
716 | pd->ncreds = ncreds.b; |
717 | pd->ciphers = ciphers; | |
cac13158 | 718 | bufadd(listeners, mustart(listenloop, pd)); |
6ca53b2e FT |
719 | } |
720 | } | |
721 | ||
722 | #endif |