2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
42 #include <gnutls/gnutls.h>
43 #include <gnutls/x509.h>
47 gnutls_certificate_credentials_t creds;
51 struct namedcreds **b;
58 gnutls_certificate_credentials_t creds;
59 struct namedcreds **ncreds;
65 struct sockaddr_storage name;
66 gnutls_session_t sess;
70 static gnutls_dh_params_t dhparams;
72 static int tlsblock(int fd, gnutls_session_t sess, time_t to)
74 if(gnutls_record_get_direction(sess))
75 return(block(fd, EV_WRITE, to));
77 return(block(fd, EV_READ, to));
80 static ssize_t sslread(void *cookie, char *buf, size_t len)
82 struct sslconn *ssl = cookie;
86 while(ssl->in.d == 0) {
87 sizebuf(ssl->in, ssl->in.d + 1024);
88 ret = gnutls_record_recv(ssl->sess, ssl->in.b + ssl->in.d, ssl->in.s - ssl->in.d);
89 if((ret == GNUTLS_E_INTERRUPTED) || (ret == GNUTLS_E_AGAIN)) {
90 if(tlsblock(ssl->fd, ssl->sess, 60) == 0) {
103 xf = min(ssl->in.d, len);
104 memcpy(buf, ssl->in.b, xf);
105 memmove(ssl->in.b, ssl->in.b + xf, ssl->in.d -= xf);
109 static ssize_t sslwrite(void *cookie, const char *buf, size_t len)
111 struct sslconn *ssl = cookie;
117 ret = gnutls_record_send(ssl->sess, buf + off, len - off);
118 if((ret == GNUTLS_E_INTERRUPTED) || (ret == GNUTLS_E_AGAIN)) {
119 if(tlsblock(ssl->fd, ssl->sess, 60) == 0) {
139 static int sslclose(void *cookie)
144 static cookie_io_functions_t iofuns = {
150 static int initreq(struct conn *conn, struct hthead *req)
152 struct sslconn *ssl = conn->pdata;
153 struct sockaddr_storage sa;
157 headappheader(req, "X-Ash-Address", formathaddress((struct sockaddr *)&ssl->name, sizeof(sa)));
158 if(ssl->name.ss_family == AF_INET) {
159 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET, &((struct sockaddr_in *)&ssl->name)->sin_addr, nmbuf, sizeof(nmbuf)));
160 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&ssl->name)->sin_port)));
161 } else if(ssl->name.ss_family == AF_INET6) {
162 headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&ssl->name)->sin6_addr, nmbuf, sizeof(nmbuf)));
163 headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&ssl->name)->sin6_port)));
166 if(!getsockname(ssl->fd, (struct sockaddr *)&sa, &salen))
167 headappheader(req, "X-Ash-Server-Address", formathaddress((struct sockaddr *)&sa, sizeof(sa)));
168 headappheader(req, "X-Ash-Server-Port", sprintf3("%i", ssl->port->sport));
169 headappheader(req, "X-Ash-Protocol", "https");
173 static void servessl(struct muth *muth, va_list args)
176 vavar(struct sockaddr_storage, name);
177 vavar(struct sslport *, pd);
180 gnutls_session_t sess;
184 int setcreds(gnutls_session_t sess)
192 namlen = sizeof(nambuf);
193 if(gnutls_server_name_get(sess, nambuf, &namlen, &ntype, i) != 0)
195 if(ntype != GNUTLS_NAME_DNS)
197 for(o = 0; pd->ncreds[o] != NULL; o++) {
198 for(u = 0; pd->ncreds[o]->names[u] != NULL; u++) {
199 if(!strcmp(pd->ncreds[o]->names[u], nambuf)) {
200 gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->ncreds[o]->creds);
201 gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST);
207 gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->creds);
208 gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST);
212 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
213 gnutls_init(&sess, GNUTLS_SERVER);
214 gnutls_set_default_priority(sess);
215 gnutls_handshake_set_post_client_hello_function(sess, setcreds);
216 gnutls_transport_set_ptr(sess, (gnutls_transport_ptr_t)(intptr_t)fd);
217 while((ret = gnutls_handshake(sess)) != 0) {
218 if((ret != GNUTLS_E_INTERRUPTED) && (ret != GNUTLS_E_AGAIN))
220 if(tlsblock(fd, sess, 60) <= 0)
223 memset(&conn, 0, sizeof(conn));
224 memset(&ssl, 0, sizeof(ssl));
226 conn.initreq = initreq;
232 in = fopencookie(&ssl, "r+", iofuns);
240 static void listenloop(struct muth *muth, va_list args)
242 vavar(struct sslport *, pd);
244 struct sockaddr_storage name;
248 namelen = sizeof(name);
249 block(pd->fd, EV_READ, 0);
250 ns = accept(pd->fd, (struct sockaddr *)&name, &namelen);
252 flog(LOG_ERR, "accept: %s", strerror(errno));
255 mustart(servessl, ns, name, pd);
263 static void init(void)
265 static int inited = 0;
271 if((ret = gnutls_global_init()) != 0) {
272 flog(LOG_ERR, "could not initialize GnuTLS: %s", gnutls_strerror(ret));
275 if(((ret = gnutls_dh_params_init(&dhparams)) != 0) ||
276 ((ret = gnutls_dh_params_generate2(dhparams, 2048)) != 0)) {
277 flog(LOG_ERR, "GnuTLS could not generate Diffie-Hellman parameters: %s", gnutls_strerror(ret));
282 static struct namedcreds *readncreds(char *file)
285 struct namedcreds *nc;
286 gnutls_x509_crt_t crt;
287 gnutls_x509_privkey_t key;
291 struct charbuf keybuf;
292 struct charvbuf names;
297 if((fd = open(file, O_RDONLY)) < 0) {
298 flog(LOG_ERR, "ssl: %s: %s", file, strerror(errno));
302 sizebuf(keybuf, keybuf.d + 1024);
303 ret = read(fd, keybuf.b + keybuf.d, keybuf.s - keybuf.d);
305 flog(LOG_ERR, "ssl: reading from %s: %s", file, strerror(errno));
307 } else if(ret == 0) {
313 d.data = (unsigned char *)keybuf.b;
315 gnutls_x509_crt_init(&crt);
316 if((ret = gnutls_x509_crt_import(crt, &d, GNUTLS_X509_FMT_PEM)) != 0) {
317 flog(LOG_ERR, "ssl: could not load certificate from %s: %s", file, gnutls_strerror(ret));
320 cnl = sizeof(cn) - 1;
321 if((ret = gnutls_x509_crt_get_dn_by_oid(crt, GNUTLS_OID_X520_COMMON_NAME, 0, 0, cn, &cnl)) != 0) {
322 flog(LOG_ERR, "ssl: could not read common name from %s: %s", file, gnutls_strerror(ret));
326 bufadd(names, sstrdup(cn));
328 cnl = sizeof(cn) - 1;
329 if(gnutls_x509_crt_get_subject_alt_name2(crt, i, cn, &cnl, &type, NULL) < 0)
332 if(type == GNUTLS_SAN_DNSNAME)
333 bufadd(names, sstrdup(cn));
335 gnutls_x509_privkey_init(&key);
336 if((ret = gnutls_x509_privkey_import(key, &d, GNUTLS_X509_FMT_PEM)) != 0) {
337 flog(LOG_ERR, "ssl: could not load key from %s: %s", file, gnutls_strerror(ret));
344 gnutls_certificate_allocate_credentials(&nc->creds);
345 if((ret = gnutls_certificate_set_x509_key(nc->creds, &crt, 1, key)) != 0) {
346 flog(LOG_ERR, "ssl: could not use certificate from %s: %s", file, gnutls_strerror(ret));
349 gnutls_certificate_set_dh_params(nc->creds, dhparams);
353 static void readncdir(struct ncredbuf *buf, char *dir)
359 if((d = opendir(dir)) == NULL) {
360 flog(LOG_ERR, "ssl: could not read certificate directory %s: %s", dir, strerror(errno));
363 while((e = readdir(d)) != NULL) {
364 if(e->d_name[0] == '.')
366 if((es = strlen(e->d_name)) <= 4)
368 if(strcmp(e->d_name + es - 4, ".crt"))
370 bufadd(*buf, readncreds(sprintf3("%s/%s", dir, e->d_name)));
375 void handlegnussl(int argc, char **argp, char **argv)
377 int i, ret, port, fd;
378 gnutls_certificate_credentials_t creds;
379 struct ncredbuf ncreds;
381 char *crtfile, *keyfile;
386 gnutls_certificate_allocate_credentials(&creds);
387 keyfile = crtfile = NULL;
388 for(i = 0; i < argc; i++) {
389 if(!strcmp(argp[i], "help")) {
390 printf("ssl handler parameters:\n");
391 printf("\tcert=CERT-FILE [mandatory]\n");
392 printf("\t\tThe name of the file to read the certificate from.\n");
393 printf("\tkey=KEY-FILE [same as CERT-FILE]\n");
394 printf("\t\tThe name of the file to read the private key from.\n");
395 printf("\ttrust=CA-FILE [no default]\n");
396 printf("\t\tThe name of a file to read trusted certificates from.\n");
397 printf("\t\tMay be given multiple times.\n");
398 printf("\tcrl=CRL-FILE [no default]\n");
399 printf("\t\tThe name of a file to read revocation lists from.\n");
400 printf("\t\tMay be given multiple times.\n");
401 printf("\tncert=CERT-FILE [no default]\n");
402 printf("\t\tThe name of a file to read a named certificate from,\n");
403 printf("\t\tfor use with SNI-enabled clients.\n");
404 printf("\t\tMay be given multiple times.\n");
405 printf("\tncertdir=DIR [no default]\n");
406 printf("\t\tRead all *.crt files in the given directory as if they\n");
407 printf("\t\twere given with `ncert' options.\n");
408 printf("\t\tMay be given multiple times.\n");
409 printf("\tport=PORT [443]\n");
410 printf("\t\tThe TCP port to listen on.\n");
412 printf("\tAll X.509 data files must be PEM-encoded.\n");
413 printf("\tIf any certificates were given with `ncert' options, they will be\n");
414 printf("\tused if a client explicitly names one of them with a\n");
415 printf("\tserver-name indication. If a client indicates no server name,\n");
416 printf("\tor if a server-name indication does not match any given\n");
417 printf("\tcertificate, the certificate given with the `cert' option will\n");
418 printf("\tbe used instead.\n");
420 } else if(!strcmp(argp[i], "cert")) {
422 } else if(!strcmp(argp[i], "key")) {
424 } else if(!strcmp(argp[i], "trust")) {
425 if((ret = gnutls_certificate_set_x509_trust_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
426 flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret));
429 for(i = 0; i < ncreds.d; i++) {
430 if((ret = gnutls_certificate_set_x509_trust_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
431 flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret));
435 } else if(!strcmp(argp[i], "crl")) {
436 if((ret = gnutls_certificate_set_x509_crl_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
437 flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret));
440 for(i = 0; i < ncreds.d; i++) {
441 if((ret = gnutls_certificate_set_x509_crl_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
442 flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret));
446 } else if(!strcmp(argp[i], "port")) {
447 port = atoi(argv[i]);
448 } else if(!strcmp(argp[i], "ncert")) {
449 bufadd(ncreds, readncreds(argv[i]));
450 } else if(!strcmp(argp[i], "ncertdir")) {
451 readncdir(&ncreds, argv[i]);
453 flog(LOG_ERR, "unknown parameter `%s' to ssl handler", argp[i]);
457 if(crtfile == NULL) {
458 flog(LOG_ERR, "ssl: needs certificate file at the very least");
463 if((ret = gnutls_certificate_set_x509_key_file(creds, crtfile, keyfile, GNUTLS_X509_FMT_PEM)) != 0) {
464 flog(LOG_ERR, "ssl: could not load certificate or key: %s", gnutls_strerror(ret));
467 gnutls_certificate_set_dh_params(creds, dhparams);
468 if((fd = listensock6(port)) < 0) {
469 flog(LOG_ERR, "could not listen on IPv6 port (port %i): %s", port, strerror(errno));
472 bufadd(ncreds, NULL);
477 pd->ncreds = ncreds.b;
478 mustart(listenloop, pd);
479 if((fd = listensock6(port)) < 0) {
480 if(errno != EADDRINUSE) {
481 flog(LOG_ERR, "could not listen on IPv6 port (port %i): %s", port, strerror(errno));
489 mustart(listenloop, pd);