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