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; | |
e5c93010 FT |
274 | gnutls_datum_t sessid; |
275 | char *esessid; | |
6ca53b2e | 276 | |
7595e3a4 | 277 | headappheader(req, "X-Ash-Address", formathaddress((struct sockaddr *)&ssl->name, sizeof(sa))); |
a5f63653 | 278 | if(ssl->name.ss_family == AF_INET) |
6ca53b2e | 279 | headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in *)&ssl->name)->sin_port))); |
a5f63653 | 280 | else if(ssl->name.ss_family == AF_INET6) |
6ca53b2e | 281 | headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&ssl->name)->sin6_port))); |
6b84641a | 282 | salen = sizeof(sa); |
7595e3a4 FT |
283 | if(!getsockname(ssl->fd, (struct sockaddr *)&sa, &salen)) |
284 | headappheader(req, "X-Ash-Server-Address", formathaddress((struct sockaddr *)&sa, sizeof(sa))); | |
6ca53b2e FT |
285 | headappheader(req, "X-Ash-Server-Port", sprintf3("%i", ssl->port->sport)); |
286 | headappheader(req, "X-Ash-Protocol", "https"); | |
e5c93010 FT |
287 | if(gnutls_session_get_id2(ssl->sess, &sessid) == GNUTLS_E_SUCCESS) { |
288 | esessid = base64encode((void *)sessid.data, sessid.size); | |
289 | headappheader(req, "X-Ash-TLS-Session", esessid); | |
290 | free(esessid); | |
291 | } | |
6ca53b2e FT |
292 | return(0); |
293 | } | |
294 | ||
d1a6f43c FT |
295 | static int setcreds(gnutls_session_t sess) |
296 | { | |
297 | int i, o, u; | |
298 | struct sslport *pd; | |
299 | unsigned int ntype; | |
300 | char nambuf[256]; | |
301 | size_t namlen; | |
302 | ||
303 | pd = gnutls_session_get_ptr(sess); | |
304 | for(i = 0; 1; i++) { | |
305 | namlen = sizeof(nambuf); | |
306 | if(gnutls_server_name_get(sess, nambuf, &namlen, &ntype, i) != 0) | |
307 | break; | |
308 | if(ntype != GNUTLS_NAME_DNS) | |
309 | continue; | |
310 | for(o = 0; pd->ncreds[o] != NULL; o++) { | |
311 | for(u = 0; pd->ncreds[o]->names[u] != NULL; u++) { | |
312 | if(!strcmp(pd->ncreds[o]->names[u], nambuf)) { | |
313 | gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->ncreds[o]->creds); | |
314 | if(pd->clreq) | |
315 | gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST); | |
316 | return(0); | |
317 | } | |
318 | } | |
319 | } | |
320 | } | |
321 | gnutls_credentials_set(sess, GNUTLS_CRD_CERTIFICATE, pd->creds); | |
322 | if(pd->clreq) | |
323 | gnutls_certificate_server_set_request(sess, GNUTLS_CERT_REQUEST); | |
324 | return(0); | |
325 | } | |
326 | ||
6ca53b2e FT |
327 | static void servessl(struct muth *muth, va_list args) |
328 | { | |
329 | vavar(int, fd); | |
330 | vavar(struct sockaddr_storage, name); | |
331 | vavar(struct sslport *, pd); | |
332 | struct conn conn; | |
333 | struct sslconn ssl; | |
334 | gnutls_session_t sess; | |
335 | int ret; | |
1b77e192 | 336 | |
d8d4ed57 | 337 | numconn++; |
6ca53b2e FT |
338 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
339 | gnutls_init(&sess, GNUTLS_SERVER); | |
76d6825e | 340 | gnutls_priority_set(sess, pd->ciphers); |
d8d4ed57 FT |
341 | gnutls_db_set_retrieve_function(sess, sessdbfetch); |
342 | gnutls_db_set_store_function(sess, sessdbstore); | |
343 | gnutls_db_set_remove_function(sess, sessdbdel); | |
344 | gnutls_db_set_ptr(sess, NULL); | |
d1a6f43c | 345 | gnutls_session_set_ptr(sess, pd); |
1b77e192 | 346 | gnutls_handshake_set_post_client_hello_function(sess, setcreds); |
6ca53b2e FT |
347 | gnutls_transport_set_ptr(sess, (gnutls_transport_ptr_t)(intptr_t)fd); |
348 | while((ret = gnutls_handshake(sess)) != 0) { | |
349 | if((ret != GNUTLS_E_INTERRUPTED) && (ret != GNUTLS_E_AGAIN)) | |
350 | goto out; | |
351 | if(tlsblock(fd, sess, 60) <= 0) | |
352 | goto out; | |
353 | } | |
354 | memset(&conn, 0, sizeof(conn)); | |
355 | memset(&ssl, 0, sizeof(ssl)); | |
356 | conn.pdata = &ssl; | |
357 | conn.initreq = initreq; | |
358 | ssl.fd = fd; | |
359 | ssl.port = pd; | |
360 | ssl.name = name; | |
361 | ssl.sess = sess; | |
362 | bufinit(ssl.in); | |
a68db17d | 363 | serve(bioopen(&ssl, &iofuns), fd, &conn); |
6ca53b2e FT |
364 | |
365 | out: | |
366 | gnutls_deinit(sess); | |
367 | close(fd); | |
d8d4ed57 | 368 | numconn--; |
6ca53b2e FT |
369 | } |
370 | ||
371 | static void listenloop(struct muth *muth, va_list args) | |
372 | { | |
373 | vavar(struct sslport *, pd); | |
f24b7bb5 | 374 | int i, ns, n; |
6ca53b2e FT |
375 | struct sockaddr_storage name; |
376 | socklen_t namelen; | |
377 | ||
3ae56649 | 378 | fcntl(pd->fd, F_SETFL, fcntl(pd->fd, F_GETFL) | O_NONBLOCK); |
6ca53b2e FT |
379 | while(1) { |
380 | namelen = sizeof(name); | |
cac13158 FT |
381 | if(block(pd->fd, EV_READ, 0) == 0) |
382 | goto out; | |
f24b7bb5 FT |
383 | n = 0; |
384 | while(1) { | |
385 | ns = accept(pd->fd, (struct sockaddr *)&name, &namelen); | |
386 | if(ns < 0) { | |
387 | if(errno == EAGAIN) | |
388 | break; | |
51893006 FT |
389 | if(errno == ECONNABORTED) |
390 | continue; | |
f24b7bb5 FT |
391 | flog(LOG_ERR, "accept: %s", strerror(errno)); |
392 | goto out; | |
393 | } | |
394 | mustart(servessl, ns, name, pd); | |
395 | if(++n >= 100) | |
396 | break; | |
6ca53b2e | 397 | } |
6ca53b2e FT |
398 | } |
399 | ||
400 | out: | |
401 | close(pd->fd); | |
402 | free(pd); | |
8e9ec020 FT |
403 | for(i = 0; i < listeners.d; i++) { |
404 | if(listeners.b[i] == muth) | |
405 | bufdel(listeners, i); | |
406 | } | |
6ca53b2e FT |
407 | } |
408 | ||
2daf4411 FT |
409 | static gnutls_dh_params_t dhparams(void) |
410 | { | |
411 | static int inited = 0; | |
412 | static gnutls_dh_params_t pars; | |
413 | int ret; | |
414 | ||
415 | if(!inited) { | |
416 | if(((ret = gnutls_dh_params_init(&pars)) != 0) || | |
417 | ((ret = gnutls_dh_params_generate2(pars, 2048)) != 0)) { | |
418 | flog(LOG_ERR, "GnuTLS could not generate Diffie-Hellman parameters: %s", gnutls_strerror(ret)); | |
419 | exit(1); | |
420 | } | |
421 | inited = 1; | |
422 | } | |
423 | return(pars); | |
424 | } | |
425 | ||
6ca53b2e FT |
426 | static void init(void) |
427 | { | |
428 | static int inited = 0; | |
429 | int ret; | |
430 | ||
431 | if(inited) | |
432 | return; | |
433 | inited = 1; | |
434 | if((ret = gnutls_global_init()) != 0) { | |
435 | flog(LOG_ERR, "could not initialize GnuTLS: %s", gnutls_strerror(ret)); | |
436 | exit(1); | |
437 | } | |
6ca53b2e FT |
438 | } |
439 | ||
7a59f41f FT |
440 | /* This implementation seems somewhat ugly, but it's the way the |
441 | * GnuTLS implements the same thing internally, so it should probably | |
442 | * be interoperable, at least. */ | |
443 | static int readcrtchain(struct certbuffer *ret, struct charbuf *pem) | |
444 | { | |
445 | static char *headers[] = {"-----BEGIN CERTIFICATE", "-----BEGIN X509 CERTIFICATE"}; | |
446 | int i, rv; | |
447 | char *p, *p2, *f; | |
448 | gnutls_x509_crt_t crt; | |
449 | ||
450 | for(i = 0, p = NULL; i < sizeof(headers) / sizeof(*headers); i++) { | |
451 | f = memmem(pem->b, pem->d, headers[i], strlen(headers[i])); | |
2e90aa50 | 452 | if((f != NULL) && ((p == NULL) || (f < p))) |
7a59f41f FT |
453 | p = f; |
454 | } | |
455 | if(p == NULL) | |
456 | return(-GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); | |
457 | do { | |
458 | if((rv = gnutls_x509_crt_init(&crt)) < 0) | |
459 | goto error; | |
460 | 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) { | |
461 | gnutls_x509_crt_deinit(crt); | |
462 | goto error; | |
463 | } | |
464 | bufadd(*ret, crt); | |
465 | for(i = 0, p2 = NULL; i < sizeof(headers) / sizeof(*headers); i++) { | |
466 | f = memmem(p + 1, pem->d - (p + 1 - pem->b), headers[i], strlen(headers[i])); | |
2e90aa50 | 467 | if((f != NULL) && ((p2 == NULL) || (f < p2))) |
7a59f41f FT |
468 | p2 = f; |
469 | } | |
470 | } while((p = p2) != NULL); | |
471 | return(0); | |
472 | error: | |
473 | for(i = 0; i < ret->d; i++) | |
474 | gnutls_x509_crt_deinit(ret->b[i]); | |
475 | ret->d = 0; | |
476 | return(rv); | |
477 | } | |
478 | ||
479 | static struct namedcreds *readncreds(char *file, gnutls_x509_privkey_t defkey) | |
28b2e619 FT |
480 | { |
481 | int i, fd, ret; | |
482 | struct namedcreds *nc; | |
7a59f41f | 483 | struct certbuffer crts; |
28b2e619 FT |
484 | gnutls_x509_privkey_t key; |
485 | char cn[1024]; | |
486 | size_t cnl; | |
28b2e619 FT |
487 | struct charbuf keybuf; |
488 | struct charvbuf names; | |
489 | unsigned int type; | |
490 | ||
491 | bufinit(keybuf); | |
7a59f41f | 492 | bufinit(crts); |
28b2e619 FT |
493 | bufinit(names); |
494 | if((fd = open(file, O_RDONLY)) < 0) { | |
495 | flog(LOG_ERR, "ssl: %s: %s", file, strerror(errno)); | |
496 | exit(1); | |
497 | } | |
498 | while(1) { | |
499 | sizebuf(keybuf, keybuf.d + 1024); | |
500 | ret = read(fd, keybuf.b + keybuf.d, keybuf.s - keybuf.d); | |
501 | if(ret < 0) { | |
502 | flog(LOG_ERR, "ssl: reading from %s: %s", file, strerror(errno)); | |
503 | exit(1); | |
504 | } else if(ret == 0) { | |
505 | break; | |
506 | } | |
507 | keybuf.d += ret; | |
508 | } | |
509 | close(fd); | |
7a59f41f FT |
510 | if((ret = readcrtchain(&crts, &keybuf)) != 0) { |
511 | flog(LOG_ERR, "ssl: could not load certificate chain from %s: %s", file, gnutls_strerror(ret)); | |
28b2e619 FT |
512 | exit(1); |
513 | } | |
514 | cnl = sizeof(cn) - 1; | |
7a59f41f | 515 | if((ret = gnutls_x509_crt_get_dn_by_oid(crts.b[0], GNUTLS_OID_X520_COMMON_NAME, 0, 0, cn, &cnl)) != 0) { |
28b2e619 FT |
516 | flog(LOG_ERR, "ssl: could not read common name from %s: %s", file, gnutls_strerror(ret)); |
517 | exit(1); | |
518 | } | |
519 | cn[cnl] = 0; | |
520 | bufadd(names, sstrdup(cn)); | |
521 | for(i = 0; 1; i++) { | |
522 | cnl = sizeof(cn) - 1; | |
7a59f41f | 523 | if(gnutls_x509_crt_get_subject_alt_name2(crts.b[0], i, cn, &cnl, &type, NULL) < 0) |
28b2e619 FT |
524 | break; |
525 | cn[cnl] = 0; | |
526 | if(type == GNUTLS_SAN_DNSNAME) | |
527 | bufadd(names, sstrdup(cn)); | |
528 | } | |
529 | gnutls_x509_privkey_init(&key); | |
7a59f41f FT |
530 | if((ret = gnutls_x509_privkey_import(key, &(gnutls_datum_t){.data = (unsigned char *)keybuf.b, .size = keybuf.d}, GNUTLS_X509_FMT_PEM)) != 0) { |
531 | if(ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { | |
532 | gnutls_x509_privkey_deinit(key); | |
533 | key = defkey; | |
534 | } else { | |
535 | flog(LOG_ERR, "ssl: could not load key from %s: %s", file, gnutls_strerror(ret)); | |
536 | exit(1); | |
537 | } | |
28b2e619 FT |
538 | } |
539 | buffree(keybuf); | |
540 | bufadd(names, NULL); | |
541 | omalloc(nc); | |
542 | nc->names = names.b; | |
543 | gnutls_certificate_allocate_credentials(&nc->creds); | |
7a59f41f | 544 | if((ret = gnutls_certificate_set_x509_key(nc->creds, crts.b, crts.d, key)) != 0) { |
28b2e619 FT |
545 | flog(LOG_ERR, "ssl: could not use certificate from %s: %s", file, gnutls_strerror(ret)); |
546 | exit(1); | |
547 | } | |
2daf4411 | 548 | gnutls_certificate_set_dh_params(nc->creds, dhparams()); |
28b2e619 FT |
549 | return(nc); |
550 | } | |
551 | ||
7a59f41f | 552 | static void readncdir(struct ncredbuf *buf, char *dir, gnutls_x509_privkey_t defkey) |
26902200 FT |
553 | { |
554 | DIR *d; | |
555 | struct dirent *e; | |
556 | size_t es; | |
557 | ||
558 | if((d = opendir(dir)) == NULL) { | |
559 | flog(LOG_ERR, "ssl: could not read certificate directory %s: %s", dir, strerror(errno)); | |
560 | exit(1); | |
561 | } | |
562 | while((e = readdir(d)) != NULL) { | |
563 | if(e->d_name[0] == '.') | |
564 | continue; | |
565 | if((es = strlen(e->d_name)) <= 4) | |
566 | continue; | |
567 | if(strcmp(e->d_name + es - 4, ".crt")) | |
568 | continue; | |
7a59f41f | 569 | bufadd(*buf, readncreds(sprintf3("%s/%s", dir, e->d_name), defkey)); |
26902200 FT |
570 | } |
571 | closedir(d); | |
572 | } | |
573 | ||
6ca53b2e FT |
574 | void handlegnussl(int argc, char **argp, char **argv) |
575 | { | |
bcd711f0 | 576 | int i, ret, port, fd, clreq; |
6ca53b2e | 577 | gnutls_certificate_credentials_t creds; |
76d6825e | 578 | gnutls_priority_t ciphers; |
7a59f41f | 579 | gnutls_x509_privkey_t defkey; |
28b2e619 | 580 | struct ncredbuf ncreds; |
7a59f41f | 581 | struct charvbuf ncertf, ncertd; |
6ca53b2e | 582 | struct sslport *pd; |
76d6825e | 583 | char *crtfile, *keyfile, *perr; |
6ca53b2e FT |
584 | |
585 | init(); | |
586 | port = 443; | |
bcd711f0 | 587 | clreq = 0; |
28b2e619 | 588 | bufinit(ncreds); |
7a59f41f FT |
589 | bufinit(ncertf); |
590 | bufinit(ncertd); | |
6ca53b2e FT |
591 | gnutls_certificate_allocate_credentials(&creds); |
592 | keyfile = crtfile = NULL; | |
76d6825e | 593 | ciphers = NULL; |
6ca53b2e FT |
594 | for(i = 0; i < argc; i++) { |
595 | if(!strcmp(argp[i], "help")) { | |
596 | printf("ssl handler parameters:\n"); | |
597 | printf("\tcert=CERT-FILE [mandatory]\n"); | |
598 | printf("\t\tThe name of the file to read the certificate from.\n"); | |
599 | printf("\tkey=KEY-FILE [same as CERT-FILE]\n"); | |
600 | printf("\t\tThe name of the file to read the private key from.\n"); | |
76d6825e FT |
601 | printf("\tprio=PRIORITIES [NORMAL]\n"); |
602 | printf("\t\tCiphersuite priorities, as a GnuTLS priority string.\n"); | |
6ca53b2e FT |
603 | printf("\ttrust=CA-FILE [no default]\n"); |
604 | printf("\t\tThe name of a file to read trusted certificates from.\n"); | |
605 | printf("\t\tMay be given multiple times.\n"); | |
606 | printf("\tcrl=CRL-FILE [no default]\n"); | |
607 | printf("\t\tThe name of a file to read revocation lists from.\n"); | |
608 | printf("\t\tMay be given multiple times.\n"); | |
4094af22 FT |
609 | printf("\tncert=CERT-FILE [no default]\n"); |
610 | printf("\t\tThe name of a file to read a named certificate from,\n"); | |
611 | printf("\t\tfor use with SNI-enabled clients.\n"); | |
612 | printf("\t\tMay be given multiple times.\n"); | |
613 | printf("\tncertdir=DIR [no default]\n"); | |
614 | printf("\t\tRead all *.crt files in the given directory as if they\n"); | |
615 | printf("\t\twere given with `ncert' options.\n"); | |
616 | printf("\t\tMay be given multiple times.\n"); | |
6ca53b2e FT |
617 | printf("\tport=PORT [443]\n"); |
618 | printf("\t\tThe TCP port to listen on.\n"); | |
619 | printf("\n"); | |
620 | printf("\tAll X.509 data files must be PEM-encoded.\n"); | |
4094af22 FT |
621 | printf("\tIf any certificates were given with `ncert' options, they will be\n"); |
622 | printf("\tused if a client explicitly names one of them with a\n"); | |
623 | printf("\tserver-name indication. If a client indicates no server name,\n"); | |
624 | printf("\tor if a server-name indication does not match any given\n"); | |
625 | printf("\tcertificate, the certificate given with the `cert' option will\n"); | |
626 | printf("\tbe used instead.\n"); | |
6ca53b2e FT |
627 | exit(0); |
628 | } else if(!strcmp(argp[i], "cert")) { | |
629 | crtfile = argv[i]; | |
630 | } else if(!strcmp(argp[i], "key")) { | |
631 | keyfile = argv[i]; | |
76d6825e FT |
632 | } else if(!strcmp(argp[i], "prio")) { |
633 | if(ciphers != NULL) | |
634 | gnutls_priority_deinit(ciphers); | |
635 | ret = gnutls_priority_init(&ciphers, argv[i], (const char **)&perr); | |
636 | if(ret == GNUTLS_E_INVALID_REQUEST) { | |
637 | flog(LOG_ERR, "ssl: invalid cipher priority string, at `%s'", perr); | |
638 | exit(1); | |
639 | } else if(ret != 0) { | |
640 | flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret)); | |
641 | exit(1); | |
642 | } | |
6ca53b2e FT |
643 | } else if(!strcmp(argp[i], "trust")) { |
644 | if((ret = gnutls_certificate_set_x509_trust_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
645 | flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret)); | |
646 | exit(1); | |
647 | } | |
28b2e619 FT |
648 | for(i = 0; i < ncreds.d; i++) { |
649 | if((ret = gnutls_certificate_set_x509_trust_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
650 | flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret)); | |
651 | exit(1); | |
652 | } | |
653 | } | |
bcd711f0 | 654 | clreq = 1; |
6ca53b2e FT |
655 | } else if(!strcmp(argp[i], "crl")) { |
656 | if((ret = gnutls_certificate_set_x509_crl_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
657 | flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret)); | |
658 | exit(1); | |
659 | } | |
28b2e619 FT |
660 | for(i = 0; i < ncreds.d; i++) { |
661 | if((ret = gnutls_certificate_set_x509_crl_file(ncreds.b[i]->creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) { | |
662 | flog(LOG_ERR, "ssl: could not load CRL file `%s': %s", argv[i], gnutls_strerror(ret)); | |
663 | exit(1); | |
664 | } | |
665 | } | |
bcd711f0 | 666 | clreq = 1; |
6ca53b2e FT |
667 | } else if(!strcmp(argp[i], "port")) { |
668 | port = atoi(argv[i]); | |
28b2e619 | 669 | } else if(!strcmp(argp[i], "ncert")) { |
7a59f41f | 670 | bufadd(ncertf, argv[i]); |
26902200 | 671 | } else if(!strcmp(argp[i], "ncertdir")) { |
7a59f41f | 672 | bufadd(ncertd, argv[i]); |
6ca53b2e FT |
673 | } else { |
674 | flog(LOG_ERR, "unknown parameter `%s' to ssl handler", argp[i]); | |
675 | exit(1); | |
676 | } | |
677 | } | |
678 | if(crtfile == NULL) { | |
679 | flog(LOG_ERR, "ssl: needs certificate file at the very least"); | |
680 | exit(1); | |
681 | } | |
2daf4411 FT |
682 | if((fd = listensock6(port)) < 0) { |
683 | flog(LOG_ERR, "could not listen on IPv6 port (port %i): %s", port, strerror(errno)); | |
684 | exit(1); | |
685 | } | |
6ca53b2e FT |
686 | if(keyfile == NULL) |
687 | keyfile = crtfile; | |
688 | if((ret = gnutls_certificate_set_x509_key_file(creds, crtfile, keyfile, GNUTLS_X509_FMT_PEM)) != 0) { | |
689 | flog(LOG_ERR, "ssl: could not load certificate or key: %s", gnutls_strerror(ret)); | |
690 | exit(1); | |
691 | } | |
76d6825e FT |
692 | if((ciphers == NULL) && ((ret = gnutls_priority_init(&ciphers, "NORMAL", NULL)) != 0)) { |
693 | flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret)); | |
694 | exit(1); | |
695 | } | |
7a59f41f FT |
696 | if((ret = gnutls_certificate_get_x509_key(creds, 0, &defkey)) != 0) { |
697 | flog(LOG_ERR, "ssl: could not get default key: %s", gnutls_strerror(ret)); | |
698 | exit(1); | |
699 | } | |
700 | for(i = 0; i < ncertf.d; i++) | |
701 | bufadd(ncreds, readncreds(ncertf.b[i], defkey)); | |
702 | for(i = 0; i < ncertd.d; i++) | |
703 | readncdir(&ncreds, ncertd.b[i], defkey); | |
704 | buffree(ncertf); | |
705 | buffree(ncertd); | |
2daf4411 | 706 | gnutls_certificate_set_dh_params(creds, dhparams()); |
28b2e619 | 707 | bufadd(ncreds, NULL); |
6ca53b2e FT |
708 | omalloc(pd); |
709 | pd->fd = fd; | |
710 | pd->sport = port; | |
bcd711f0 | 711 | pd->clreq = clreq; |
6ca53b2e | 712 | pd->creds = creds; |
28b2e619 | 713 | pd->ncreds = ncreds.b; |
76d6825e | 714 | pd->ciphers = ciphers; |
cac13158 | 715 | bufadd(listeners, mustart(listenloop, pd)); |
aa06d1b3 | 716 | if((fd = listensock4(port)) < 0) { |
6ca53b2e | 717 | if(errno != EADDRINUSE) { |
aa06d1b3 | 718 | flog(LOG_ERR, "could not listen on IPv4 port (port %i): %s", port, strerror(errno)); |
6ca53b2e FT |
719 | exit(1); |
720 | } | |
721 | } else { | |
722 | omalloc(pd); | |
723 | pd->fd = fd; | |
724 | pd->sport = port; | |
725 | pd->creds = creds; | |
76d6825e FT |
726 | pd->ncreds = ncreds.b; |
727 | pd->ciphers = ciphers; | |
cac13158 | 728 | bufadd(listeners, mustart(listenloop, pd)); |
6ca53b2e FT |
729 | } |
730 | } | |
731 | ||
732 | #endif |