htparser: Fixed SSL client-address formatting.
[ashd.git] / src / ssl-gnutls.c
CommitLineData
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
45struct namedcreds {
46 char **names;
47 gnutls_certificate_credentials_t creds;
48};
49
50struct ncredbuf {
51 struct namedcreds **b;
52 size_t s, d;
53};
6ca53b2e
FT
54
55struct 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
63struct 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
71struct savedsess {
72 struct savedsess *next, *prev;
73 gnutls_datum_t key, value;
74};
75
76static int numconn = 0, numsess = 0;
77static struct btree *sessidx = NULL;
78static struct savedsess *sesslistf = NULL, *sesslistl = NULL;
79
80static 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
89static 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
102static 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
119static 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
130static void cleansess(void)
131{
132 while(numsess > (max(numconn, 1) * 100))
133 freesess(sesslistl);
134}
135
136static 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
183static 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
191static 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
220static 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
250static int sslclose(void *cookie)
251{
5ba5dd0b
FT
252 struct sslconn *ssl = cookie;
253
254 buffree(ssl->in);
6ca53b2e
FT
255 return(0);
256}
257
258static cookie_io_functions_t iofuns = {
259 .read = sslread,
260 .write = sslwrite,
261 .close = sslclose,
262};
263
264static 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 269
7595e3a4 270 headappheader(req, "X-Ash-Address", formathaddress((struct sockaddr *)&ssl->name, sizeof(sa)));
a5f63653 271 if(ssl->name.ss_family == AF_INET)
6ca53b2e 272 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&ssl->name)->sin_port)));
a5f63653 273 else if(ssl->name.ss_family == AF_INET6)
6ca53b2e 274 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&ssl->name)->sin6_port)));
6b84641a 275 salen = sizeof(sa);
7595e3a4
FT
276 if(!getsockname(ssl->fd, (struct sockaddr *)&sa, &salen))
277 headappheader(req, "X-Ash-Server-Address", formathaddress((struct sockaddr *)&sa, sizeof(sa)));
6ca53b2e
FT
278 headappheader(req, "X-Ash-Server-Port", sprintf3("%i", ssl->port->sport));
279 headappheader(req, "X-Ash-Protocol", "https");
280 return(0);
281}
282
283static void servessl(struct muth *muth, va_list args)
284{
285 vavar(int, fd);
286 vavar(struct sockaddr_storage, name);
287 vavar(struct sslport *, pd);
288 struct conn conn;
289 struct sslconn ssl;
290 gnutls_session_t sess;
291 int ret;
292 FILE *in;
293
1b77e192
FT
294 int setcreds(gnutls_session_t sess)
295 {
28b2e619 296 int i, o, u;
1b77e192
FT
297 unsigned int ntype;
298 char nambuf[256];
299 size_t namlen;
300
301 for(i = 0; 1; i++) {
302 namlen = sizeof(nambuf);
303 if(gnutls_server_name_get(sess, nambuf, &namlen, &ntype, i) != 0)
304 break;
305 if(ntype != GNUTLS_NAME_DNS)
306 continue;
28b2e619
FT
307 for(o = 0; pd->ncreds[o] != NULL; o++) {
308 for(u = 0; pd->ncreds[o]->names[u] != NULL; u++) {
309 if(!strcmp(pd->ncreds[o]->names[u], nambuf)) {
310 gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->ncreds[o]->creds);
311 gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST);
312 return(0);
313 }
314 }
315 }
1b77e192
FT
316 }
317 gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->creds);
318 gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST);
319 return(0);
320 }
321
d8d4ed57 322 numconn++;
6ca53b2e
FT
323 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
324 gnutls_init(&sess, GNUTLS_SERVER);
76d6825e 325 gnutls_priority_set(sess, pd->ciphers);
d8d4ed57
FT
326 gnutls_db_set_retrieve_function(sess, sessdbfetch);
327 gnutls_db_set_store_function(sess, sessdbstore);
328 gnutls_db_set_remove_function(sess, sessdbdel);
329 gnutls_db_set_ptr(sess, NULL);
1b77e192 330 gnutls_handshake_set_post_client_hello_function(sess, setcreds);
6ca53b2e
FT
331 gnutls_transport_set_ptr(sess, (gnutls_transport_ptr_t)(intptr_t)fd);
332 while((ret = gnutls_handshake(sess)) != 0) {
333 if((ret != GNUTLS_E_INTERRUPTED) && (ret != GNUTLS_E_AGAIN))
334 goto out;
335 if(tlsblock(fd, sess, 60) <= 0)
336 goto out;
337 }
338 memset(&conn, 0, sizeof(conn));
339 memset(&ssl, 0, sizeof(ssl));
340 conn.pdata = &ssl;
341 conn.initreq = initreq;
342 ssl.fd = fd;
343 ssl.port = pd;
344 ssl.name = name;
345 ssl.sess = sess;
346 bufinit(ssl.in);
347 in = fopencookie(&ssl, "r+", iofuns);
348 serve(in, &conn);
349
350out:
351 gnutls_deinit(sess);
352 close(fd);
d8d4ed57 353 numconn--;
6ca53b2e
FT
354}
355
356static void listenloop(struct muth *muth, va_list args)
357{
358 vavar(struct sslport *, pd);
f24b7bb5 359 int i, ns, n;
6ca53b2e
FT
360 struct sockaddr_storage name;
361 socklen_t namelen;
362
3ae56649 363 fcntl(pd->fd, F_SETFL, fcntl(pd->fd, F_GETFL) | O_NONBLOCK);
6ca53b2e
FT
364 while(1) {
365 namelen = sizeof(name);
cac13158
FT
366 if(block(pd->fd, EV_READ, 0) == 0)
367 goto out;
f24b7bb5
FT
368 n = 0;
369 while(1) {
370 ns = accept(pd->fd, (struct sockaddr *)&name, &namelen);
371 if(ns < 0) {
372 if(errno == EAGAIN)
373 break;
51893006
FT
374 if(errno == ECONNABORTED)
375 continue;
f24b7bb5
FT
376 flog(LOG_ERR, "accept: %s", strerror(errno));
377 goto out;
378 }
379 mustart(servessl, ns, name, pd);
380 if(++n >= 100)
381 break;
6ca53b2e 382 }
6ca53b2e
FT
383 }
384
385out:
386 close(pd->fd);
387 free(pd);
8e9ec020
FT
388 for(i = 0; i < listeners.d; i++) {
389 if(listeners.b[i] == muth)
390 bufdel(listeners, i);
391 }
6ca53b2e
FT
392}
393
2daf4411
FT
394static gnutls_dh_params_t dhparams(void)
395{
396 static int inited = 0;
397 static gnutls_dh_params_t pars;
398 int ret;
399
400 if(!inited) {
401 if(((ret = gnutls_dh_params_init(&pars)) != 0) ||
402 ((ret = gnutls_dh_params_generate2(pars, 2048)) != 0)) {
403 flog(LOG_ERR, "GnuTLS could not generate Diffie-Hellman parameters: %s", gnutls_strerror(ret));
404 exit(1);
405 }
406 inited = 1;
407 }
408 return(pars);
409}
410
6ca53b2e
FT
411static void init(void)
412{
413 static int inited = 0;
414 int ret;
415
416 if(inited)
417 return;
418 inited = 1;
419 if((ret = gnutls_global_init()) != 0) {
420 flog(LOG_ERR, "could not initialize GnuTLS: %s", gnutls_strerror(ret));
421 exit(1);
422 }
6ca53b2e
FT
423}
424
28b2e619
FT
425static struct namedcreds *readncreds(char *file)
426{
427 int i, fd, ret;
428 struct namedcreds *nc;
429 gnutls_x509_crt_t crt;
430 gnutls_x509_privkey_t key;
431 char cn[1024];
432 size_t cnl;
433 gnutls_datum_t d;
434 struct charbuf keybuf;
435 struct charvbuf names;
436 unsigned int type;
437
438 bufinit(keybuf);
439 bufinit(names);
440 if((fd = open(file, O_RDONLY)) < 0) {
441 flog(LOG_ERR, "ssl: %s: %s", file, strerror(errno));
442 exit(1);
443 }
444 while(1) {
445 sizebuf(keybuf, keybuf.d + 1024);
446 ret = read(fd, keybuf.b + keybuf.d, keybuf.s - keybuf.d);
447 if(ret < 0) {
448 flog(LOG_ERR, "ssl: reading from %s: %s", file, strerror(errno));
449 exit(1);
450 } else if(ret == 0) {
451 break;
452 }
453 keybuf.d += ret;
454 }
455 close(fd);
456 d.data = (unsigned char *)keybuf.b;
457 d.size = keybuf.d;
458 gnutls_x509_crt_init(&crt);
459 if((ret = gnutls_x509_crt_import(crt, &d, GNUTLS_X509_FMT_PEM)) != 0) {
460 flog(LOG_ERR, "ssl: could not load certificate from %s: %s", file, gnutls_strerror(ret));
461 exit(1);
462 }
463 cnl = sizeof(cn) - 1;
464 if((ret = gnutls_x509_crt_get_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0, 0, cn, &cnl)) != 0) {
465 flog(LOG_ERR, "ssl: could not read common name from %s: %s", file, gnutls_strerror(ret));
466 exit(1);
467 }
468 cn[cnl] = 0;
469 bufadd(names, sstrdup(cn));
470 for(i = 0; 1; i++) {
471 cnl = sizeof(cn) - 1;
472 if(gnutls_x509_crt_get_subject_alt_name2(crt, i, cn, &cnl, &type, NULL) < 0)
473 break;
474 cn[cnl] = 0;
475 if(type == GNUTLS_SAN_DNSNAME)
476 bufadd(names, sstrdup(cn));
477 }
478 gnutls_x509_privkey_init(&key);
479 if((ret = gnutls_x509_privkey_import(key, &d, GNUTLS_X509_FMT_PEM)) != 0) {
480 flog(LOG_ERR, "ssl: could not load key from %s: %s", file, gnutls_strerror(ret));
481 exit(1);
482 }
483 buffree(keybuf);
484 bufadd(names, NULL);
485 omalloc(nc);
486 nc->names = names.b;
487 gnutls_certificate_allocate_credentials(&nc->creds);
488 if((ret = gnutls_certificate_set_x509_key(nc->creds, &crt, 1, key)) != 0) {
489 flog(LOG_ERR, "ssl: could not use certificate from %s: %s", file, gnutls_strerror(ret));
490 exit(1);
491 }
2daf4411 492 gnutls_certificate_set_dh_params(nc->creds, dhparams());
28b2e619
FT
493 return(nc);
494}
495
26902200
FT
496static void readncdir(struct ncredbuf *buf, char *dir)
497{
498 DIR *d;
499 struct dirent *e;
500 size_t es;
501
502 if((d = opendir(dir)) == NULL) {
503 flog(LOG_ERR, "ssl: could not read certificate directory %s: %s", dir, strerror(errno));
504 exit(1);
505 }
506 while((e = readdir(d)) != NULL) {
507 if(e->d_name[0] == '.')
508 continue;
509 if((es = strlen(e->d_name)) <= 4)
510 continue;
511 if(strcmp(e->d_name + es - 4, ".crt"))
512 continue;
513 bufadd(*buf, readncreds(sprintf3("%s/%s", dir, e->d_name)));
514 }
515 closedir(d);
516}
517
6ca53b2e
FT
518void handlegnussl(int argc, char **argp, char **argv)
519{
520 int i, ret, port, fd;
521 gnutls_certificate_credentials_t creds;
76d6825e 522 gnutls_priority_t ciphers;
28b2e619 523 struct ncredbuf ncreds;
6ca53b2e 524 struct sslport *pd;
76d6825e 525 char *crtfile, *keyfile, *perr;
6ca53b2e
FT
526
527 init();
528 port = 443;
28b2e619 529 bufinit(ncreds);
6ca53b2e
FT
530 gnutls_certificate_allocate_credentials(&creds);
531 keyfile = crtfile = NULL;
76d6825e 532 ciphers = NULL;
6ca53b2e
FT
533 for(i = 0; i < argc; i++) {
534 if(!strcmp(argp[i], "help")) {
535 printf("ssl handler parameters:\n");
536 printf("\tcert=CERT-FILE [mandatory]\n");
537 printf("\t\tThe name of the file to read the certificate from.\n");
538 printf("\tkey=KEY-FILE [same as CERT-FILE]\n");
539 printf("\t\tThe name of the file to read the private key from.\n");
76d6825e
FT
540 printf("\tprio=PRIORITIES [NORMAL]\n");
541 printf("\t\tCiphersuite priorities, as a GnuTLS priority string.\n");
6ca53b2e
FT
542 printf("\ttrust=CA-FILE [no default]\n");
543 printf("\t\tThe name of a file to read trusted certificates from.\n");
544 printf("\t\tMay be given multiple times.\n");
545 printf("\tcrl=CRL-FILE [no default]\n");
546 printf("\t\tThe name of a file to read revocation lists from.\n");
547 printf("\t\tMay be given multiple times.\n");
4094af22
FT
548 printf("\tncert=CERT-FILE [no default]\n");
549 printf("\t\tThe name of a file to read a named certificate from,\n");
550 printf("\t\tfor use with SNI-enabled clients.\n");
551 printf("\t\tMay be given multiple times.\n");
552 printf("\tncertdir=DIR [no default]\n");
553 printf("\t\tRead all *.crt files in the given directory as if they\n");
554 printf("\t\twere given with `ncert' options.\n");
555 printf("\t\tMay be given multiple times.\n");
6ca53b2e
FT
556 printf("\tport=PORT [443]\n");
557 printf("\t\tThe TCP port to listen on.\n");
558 printf("\n");
559 printf("\tAll X.509 data files must be PEM-encoded.\n");
4094af22
FT
560 printf("\tIf any certificates were given with `ncert' options, they will be\n");
561 printf("\tused if a client explicitly names one of them with a\n");
562 printf("\tserver-name indication. If a client indicates no server name,\n");
563 printf("\tor if a server-name indication does not match any given\n");
564 printf("\tcertificate, the certificate given with the `cert' option will\n");
565 printf("\tbe used instead.\n");
6ca53b2e
FT
566 exit(0);
567 } else if(!strcmp(argp[i], "cert")) {
568 crtfile = argv[i];
569 } else if(!strcmp(argp[i], "key")) {
570 keyfile = argv[i];
76d6825e
FT
571 } else if(!strcmp(argp[i], "prio")) {
572 if(ciphers != NULL)
573 gnutls_priority_deinit(ciphers);
574 ret = gnutls_priority_init(&ciphers, argv[i], (const char **)&perr);
575 if(ret == GNUTLS_E_INVALID_REQUEST) {
576 flog(LOG_ERR, "ssl: invalid cipher priority string, at `%s'", perr);
577 exit(1);
578 } else if(ret != 0) {
579 flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret));
580 exit(1);
581 }
6ca53b2e
FT
582 } else if(!strcmp(argp[i], "trust")) {
583 if((ret = gnutls_certificate_set_x509_trust_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
584 flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret));
585 exit(1);
586 }
28b2e619
FT
587 for(i = 0; i < ncreds.d; i++) {
588 if((ret = gnutls_certificate_set_x509_trust_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
589 flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret));
590 exit(1);
591 }
592 }
6ca53b2e
FT
593 } else if(!strcmp(argp[i], "crl")) {
594 if((ret = gnutls_certificate_set_x509_crl_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
595 flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret));
596 exit(1);
597 }
28b2e619
FT
598 for(i = 0; i < ncreds.d; i++) {
599 if((ret = gnutls_certificate_set_x509_crl_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
600 flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret));
601 exit(1);
602 }
603 }
6ca53b2e
FT
604 } else if(!strcmp(argp[i], "port")) {
605 port = atoi(argv[i]);
28b2e619
FT
606 } else if(!strcmp(argp[i], "ncert")) {
607 bufadd(ncreds, readncreds(argv[i]));
26902200
FT
608 } else if(!strcmp(argp[i], "ncertdir")) {
609 readncdir(&ncreds, argv[i]);
6ca53b2e
FT
610 } else {
611 flog(LOG_ERR, "unknown parameter `%s' to ssl handler", argp[i]);
612 exit(1);
613 }
614 }
615 if(crtfile == NULL) {
616 flog(LOG_ERR, "ssl: needs certificate file at the very least");
617 exit(1);
618 }
2daf4411
FT
619 if((fd = listensock6(port)) < 0) {
620 flog(LOG_ERR, "could not listen on IPv6 port (port %i): %s", port, strerror(errno));
621 exit(1);
622 }
6ca53b2e
FT
623 if(keyfile == NULL)
624 keyfile = crtfile;
625 if((ret = gnutls_certificate_set_x509_key_file(creds, crtfile, keyfile, GNUTLS_X509_FMT_PEM)) != 0) {
626 flog(LOG_ERR, "ssl: could not load certificate or key: %s", gnutls_strerror(ret));
627 exit(1);
628 }
76d6825e
FT
629 if((ciphers == NULL) && ((ret = gnutls_priority_init(&ciphers, "NORMAL", NULL)) != 0)) {
630 flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret));
631 exit(1);
632 }
2daf4411 633 gnutls_certificate_set_dh_params(creds, dhparams());
28b2e619 634 bufadd(ncreds, NULL);
6ca53b2e
FT
635 omalloc(pd);
636 pd->fd = fd;
637 pd->sport = port;
638 pd->creds = creds;
28b2e619 639 pd->ncreds = ncreds.b;
76d6825e 640 pd->ciphers = ciphers;
cac13158 641 bufadd(listeners, mustart(listenloop, pd));
aa06d1b3 642 if((fd = listensock4(port)) < 0) {
6ca53b2e 643 if(errno != EADDRINUSE) {
aa06d1b3 644 flog(LOG_ERR, "could not listen on IPv4 port (port %i): %s", port, strerror(errno));
6ca53b2e
FT
645 exit(1);
646 }
647 } else {
648 omalloc(pd);
649 pd->fd = fd;
650 pd->sport = port;
651 pd->creds = creds;
76d6825e
FT
652 pd->ncreds = ncreds.b;
653 pd->ciphers = ciphers;
cac13158 654 bufadd(listeners, mustart(listenloop, pd));
6ca53b2e
FT
655 }
656}
657
658#endif