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