import libtls (LibreSSL 2.5.4)
[unleashed.git] / lib / libtls / tls_config.c
blob87c2166f9ea87eadc56ddd1a724cb94801b471a9
1 /* $OpenBSD: tls_config.c,v 1.36 2017/01/31 16:18:57 beck Exp $ */
2 /*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/stat.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #include <tls.h>
27 #include "tls_internal.h"
29 static int
30 set_string(const char **dest, const char *src)
32 free((char *)*dest);
33 *dest = NULL;
34 if (src != NULL)
35 if ((*dest = strdup(src)) == NULL)
36 return -1;
37 return 0;
40 static void *
41 memdup(const void *in, size_t len)
43 void *out;
45 if ((out = malloc(len)) == NULL)
46 return NULL;
47 memcpy(out, in, len);
48 return out;
51 static int
52 set_mem(char **dest, size_t *destlen, const void *src, size_t srclen)
54 free(*dest);
55 *dest = NULL;
56 *destlen = 0;
57 if (src != NULL)
58 if ((*dest = memdup(src, srclen)) == NULL)
59 return -1;
60 *destlen = srclen;
61 return 0;
64 static struct tls_keypair *
65 tls_keypair_new(void)
67 return calloc(1, sizeof(struct tls_keypair));
70 static int
71 tls_keypair_set_cert_file(struct tls_keypair *keypair, struct tls_error *error,
72 const char *cert_file)
74 return tls_config_load_file(error, "certificate", cert_file,
75 &keypair->cert_mem, &keypair->cert_len);
78 static int
79 tls_keypair_set_cert_mem(struct tls_keypair *keypair, const uint8_t *cert,
80 size_t len)
82 return set_mem(&keypair->cert_mem, &keypair->cert_len, cert, len);
85 static int
86 tls_keypair_set_key_file(struct tls_keypair *keypair, struct tls_error *error,
87 const char *key_file)
89 if (keypair->key_mem != NULL)
90 explicit_bzero(keypair->key_mem, keypair->key_len);
91 return tls_config_load_file(error, "key", key_file,
92 &keypair->key_mem, &keypair->key_len);
95 static int
96 tls_keypair_set_key_mem(struct tls_keypair *keypair, const uint8_t *key,
97 size_t len)
99 if (keypair->key_mem != NULL)
100 explicit_bzero(keypair->key_mem, keypair->key_len);
101 return set_mem(&keypair->key_mem, &keypair->key_len, key, len);
104 static int
105 tls_keypair_set_ocsp_staple_file(struct tls_keypair *keypair,
106 struct tls_error *error, const char *ocsp_file)
108 return tls_config_load_file(error, "ocsp", ocsp_file,
109 &keypair->ocsp_staple, &keypair->ocsp_staple_len);
112 static int
113 tls_keypair_set_ocsp_staple_mem(struct tls_keypair *keypair,
114 const uint8_t *staple, size_t len)
116 return set_mem(&keypair->ocsp_staple, &keypair->ocsp_staple_len, staple,
117 len);
120 static void
121 tls_keypair_clear(struct tls_keypair *keypair)
123 tls_keypair_set_cert_mem(keypair, NULL, 0);
124 tls_keypair_set_key_mem(keypair, NULL, 0);
127 static void
128 tls_keypair_free(struct tls_keypair *keypair)
130 if (keypair == NULL)
131 return;
133 tls_keypair_clear(keypair);
135 free(keypair->cert_mem);
136 free(keypair->key_mem);
137 free(keypair->ocsp_staple);
139 free(keypair);
143 tls_config_load_file(struct tls_error *error, const char *filetype,
144 const char *filename, char **buf, size_t *len)
146 struct stat st;
147 int fd = -1;
148 ssize_t n;
150 free(*buf);
151 *buf = NULL;
152 *len = 0;
154 if ((fd = open(filename, O_RDONLY)) == -1) {
155 tls_error_set(error, "failed to open %s file '%s'",
156 filetype, filename);
157 goto fail;
159 if (fstat(fd, &st) != 0) {
160 tls_error_set(error, "failed to stat %s file '%s'",
161 filetype, filename);
162 goto fail;
164 if (st.st_size < 0)
165 goto fail;
166 *len = (size_t)st.st_size;
167 if ((*buf = malloc(*len)) == NULL) {
168 tls_error_set(error, "failed to allocate buffer for "
169 "%s file", filetype);
170 goto fail;
172 n = read(fd, *buf, *len);
173 if (n < 0 || (size_t)n != *len) {
174 tls_error_set(error, "failed to read %s file '%s'",
175 filetype, filename);
176 goto fail;
178 close(fd);
179 return 0;
181 fail:
182 if (fd != -1)
183 close(fd);
184 if (*buf != NULL)
185 explicit_bzero(*buf, *len);
186 free(*buf);
187 *buf = NULL;
188 *len = 0;
190 return -1;
193 struct tls_config *
194 tls_config_new(void)
196 struct tls_config *config;
197 unsigned char sid[TLS_MAX_SESSION_ID_LENGTH];
199 if ((config = calloc(1, sizeof(*config))) == NULL)
200 return (NULL);
202 if ((config->keypair = tls_keypair_new()) == NULL)
203 goto err;
206 * Default configuration.
208 if (tls_config_set_dheparams(config, "none") != 0)
209 goto err;
210 if (tls_config_set_ecdhecurve(config, "auto") != 0)
211 goto err;
212 if (tls_config_set_ciphers(config, "secure") != 0)
213 goto err;
215 if (tls_config_set_protocols(config, TLS_PROTOCOLS_DEFAULT) != 0)
216 goto err;
217 if (tls_config_set_verify_depth(config, 6) != 0)
218 goto err;
221 * Set session ID context to a random value. For the simple case
222 * of a single process server this is good enough. For multiprocess
223 * servers the session ID needs to be set by the caller.
225 arc4random_buf(sid, sizeof(sid));
226 if (tls_config_set_session_id(config, sid, sizeof(sid)) != 0)
227 goto err;
228 config->ticket_keyrev = arc4random();
229 config->ticket_autorekey = 1;
231 tls_config_prefer_ciphers_server(config);
233 tls_config_verify(config);
235 return (config);
237 err:
238 tls_config_free(config);
239 return (NULL);
242 void
243 tls_config_free(struct tls_config *config)
245 struct tls_keypair *kp, *nkp;
247 if (config == NULL)
248 return;
250 for (kp = config->keypair; kp != NULL; kp = nkp) {
251 nkp = kp->next;
252 tls_keypair_free(kp);
255 free(config->error.msg);
257 free(config->alpn);
258 free((char *)config->ca_mem);
259 free((char *)config->ca_path);
260 free((char *)config->ciphers);
262 free(config);
265 static void
266 tls_config_keypair_add(struct tls_config *config, struct tls_keypair *keypair)
268 struct tls_keypair *kp;
270 kp = config->keypair;
271 while (kp->next != NULL)
272 kp = kp->next;
274 kp->next = keypair;
277 const char *
278 tls_config_error(struct tls_config *config)
280 return config->error.msg;
283 void
284 tls_config_clear_keys(struct tls_config *config)
286 struct tls_keypair *kp;
288 for (kp = config->keypair; kp != NULL; kp = kp->next)
289 tls_keypair_clear(kp);
291 tls_config_set_ca_mem(config, NULL, 0);
295 tls_config_parse_protocols(uint32_t *protocols, const char *protostr)
297 uint32_t proto, protos = 0;
298 char *s, *p, *q;
299 int negate;
301 if ((s = strdup(protostr)) == NULL)
302 return (-1);
304 q = s;
305 while ((p = strsep(&q, ",:")) != NULL) {
306 while (*p == ' ' || *p == '\t')
307 p++;
309 negate = 0;
310 if (*p == '!') {
311 negate = 1;
312 p++;
315 if (negate && protos == 0)
316 protos = TLS_PROTOCOLS_ALL;
318 proto = 0;
319 if (strcasecmp(p, "all") == 0 ||
320 strcasecmp(p, "legacy") == 0)
321 proto = TLS_PROTOCOLS_ALL;
322 else if (strcasecmp(p, "default") == 0 ||
323 strcasecmp(p, "secure") == 0)
324 proto = TLS_PROTOCOLS_DEFAULT;
325 if (strcasecmp(p, "tlsv1") == 0)
326 proto = TLS_PROTOCOL_TLSv1;
327 else if (strcasecmp(p, "tlsv1.0") == 0)
328 proto = TLS_PROTOCOL_TLSv1_0;
329 else if (strcasecmp(p, "tlsv1.1") == 0)
330 proto = TLS_PROTOCOL_TLSv1_1;
331 else if (strcasecmp(p, "tlsv1.2") == 0)
332 proto = TLS_PROTOCOL_TLSv1_2;
334 if (proto == 0) {
335 free(s);
336 return (-1);
339 if (negate)
340 protos &= ~proto;
341 else
342 protos |= proto;
345 *protocols = protos;
347 free(s);
349 return (0);
352 static int
353 tls_config_parse_alpn(struct tls_config *config, const char *alpn,
354 char **alpn_data, size_t *alpn_len)
356 size_t buf_len, i, len;
357 char *buf = NULL;
358 char *s = NULL;
359 char *p, *q;
361 free(*alpn_data);
362 *alpn_data = NULL;
363 *alpn_len = 0;
365 if ((buf_len = strlen(alpn) + 1) > 65535) {
366 tls_config_set_errorx(config, "alpn too large");
367 goto err;
370 if ((buf = malloc(buf_len)) == NULL) {
371 tls_config_set_errorx(config, "out of memory");
372 goto err;
375 if ((s = strdup(alpn)) == NULL) {
376 tls_config_set_errorx(config, "out of memory");
377 goto err;
380 i = 0;
381 q = s;
382 while ((p = strsep(&q, ",")) != NULL) {
383 if ((len = strlen(p)) == 0) {
384 tls_config_set_errorx(config,
385 "alpn protocol with zero length");
386 goto err;
388 if (len > 255) {
389 tls_config_set_errorx(config,
390 "alpn protocol too long");
391 goto err;
393 buf[i++] = len & 0xff;
394 memcpy(&buf[i], p, len);
395 i += len;
398 free(s);
400 *alpn_data = buf;
401 *alpn_len = buf_len;
403 return (0);
405 err:
406 free(buf);
407 free(s);
409 return (-1);
413 tls_config_set_alpn(struct tls_config *config, const char *alpn)
415 return tls_config_parse_alpn(config, alpn, &config->alpn,
416 &config->alpn_len);
419 static int
420 tls_config_add_keypair_file_internal(struct tls_config *config,
421 const char *cert_file, const char *key_file, const char *ocsp_file)
423 struct tls_keypair *keypair;
425 if ((keypair = tls_keypair_new()) == NULL)
426 return (-1);
427 if (tls_keypair_set_cert_file(keypair, &config->error, cert_file) != 0)
428 goto err;
429 if (tls_keypair_set_key_file(keypair, &config->error, key_file) != 0)
430 goto err;
431 if (ocsp_file != NULL &&
432 tls_keypair_set_ocsp_staple_file(keypair, &config->error,
433 ocsp_file) != 0)
434 goto err;
436 tls_config_keypair_add(config, keypair);
438 return (0);
440 err:
441 tls_keypair_free(keypair);
442 return (-1);
445 static int
446 tls_config_add_keypair_mem_internal(struct tls_config *config, const uint8_t *cert,
447 size_t cert_len, const uint8_t *key, size_t key_len,
448 const uint8_t *staple, size_t staple_len)
450 struct tls_keypair *keypair;
452 if ((keypair = tls_keypair_new()) == NULL)
453 return (-1);
454 if (tls_keypair_set_cert_mem(keypair, cert, cert_len) != 0)
455 goto err;
456 if (tls_keypair_set_key_mem(keypair, key, key_len) != 0)
457 goto err;
458 if (staple != NULL &&
459 tls_keypair_set_ocsp_staple_mem(keypair, staple, staple_len) != 0)
460 goto err;
462 tls_config_keypair_add(config, keypair);
464 return (0);
466 err:
467 tls_keypair_free(keypair);
468 return (-1);
472 tls_config_add_keypair_mem(struct tls_config *config, const uint8_t *cert,
473 size_t cert_len, const uint8_t *key, size_t key_len)
475 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
476 key_len, NULL, 0);
480 tls_config_add_keypair_file(struct tls_config *config,
481 const char *cert_file, const char *key_file)
483 return tls_config_add_keypair_file_internal(config, cert_file,
484 key_file, NULL);
488 tls_config_add_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert,
489 size_t cert_len, const uint8_t *key, size_t key_len, const uint8_t *staple,
490 size_t staple_len)
492 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
493 key_len, staple, staple_len);
497 tls_config_add_keypair_ocsp_file(struct tls_config *config,
498 const char *cert_file, const char *key_file, const char *ocsp_file)
500 return tls_config_add_keypair_file_internal(config, cert_file,
501 key_file, ocsp_file);
505 tls_config_set_ca_file(struct tls_config *config, const char *ca_file)
507 return tls_config_load_file(&config->error, "CA", ca_file,
508 &config->ca_mem, &config->ca_len);
512 tls_config_set_ca_path(struct tls_config *config, const char *ca_path)
514 return set_string(&config->ca_path, ca_path);
518 tls_config_set_ca_mem(struct tls_config *config, const uint8_t *ca, size_t len)
520 return set_mem(&config->ca_mem, &config->ca_len, ca, len);
524 tls_config_set_cert_file(struct tls_config *config, const char *cert_file)
526 return tls_keypair_set_cert_file(config->keypair, &config->error,
527 cert_file);
531 tls_config_set_cert_mem(struct tls_config *config, const uint8_t *cert,
532 size_t len)
534 return tls_keypair_set_cert_mem(config->keypair, cert, len);
538 tls_config_set_ciphers(struct tls_config *config, const char *ciphers)
540 SSL_CTX *ssl_ctx = NULL;
542 if (ciphers == NULL ||
543 strcasecmp(ciphers, "default") == 0 ||
544 strcasecmp(ciphers, "secure") == 0)
545 ciphers = TLS_CIPHERS_DEFAULT;
546 else if (strcasecmp(ciphers, "compat") == 0)
547 ciphers = TLS_CIPHERS_COMPAT;
548 else if (strcasecmp(ciphers, "legacy") == 0)
549 ciphers = TLS_CIPHERS_LEGACY;
550 else if (strcasecmp(ciphers, "all") == 0 ||
551 strcasecmp(ciphers, "insecure") == 0)
552 ciphers = TLS_CIPHERS_ALL;
554 if ((ssl_ctx = SSL_CTX_new(SSLv23_method())) == NULL) {
555 tls_config_set_errorx(config, "out of memory");
556 goto fail;
558 if (SSL_CTX_set_cipher_list(ssl_ctx, ciphers) != 1) {
559 tls_config_set_errorx(config, "no ciphers for '%s'", ciphers);
560 goto fail;
563 SSL_CTX_free(ssl_ctx);
564 return set_string(&config->ciphers, ciphers);
566 fail:
567 SSL_CTX_free(ssl_ctx);
568 return -1;
572 tls_config_set_dheparams(struct tls_config *config, const char *params)
574 int keylen;
576 if (params == NULL || strcasecmp(params, "none") == 0)
577 keylen = 0;
578 else if (strcasecmp(params, "auto") == 0)
579 keylen = -1;
580 else if (strcasecmp(params, "legacy") == 0)
581 keylen = 1024;
582 else {
583 tls_config_set_errorx(config, "invalid dhe param '%s'", params);
584 return (-1);
587 config->dheparams = keylen;
589 return (0);
593 tls_config_set_ecdhecurve(struct tls_config *config, const char *name)
595 int nid;
597 if (name == NULL || strcasecmp(name, "none") == 0)
598 nid = NID_undef;
599 else if (strcasecmp(name, "auto") == 0)
600 nid = -1;
601 else if ((nid = OBJ_txt2nid(name)) == NID_undef) {
602 tls_config_set_errorx(config, "invalid ecdhe curve '%s'", name);
603 return (-1);
606 config->ecdhecurve = nid;
608 return (0);
612 tls_config_set_key_file(struct tls_config *config, const char *key_file)
614 return tls_keypair_set_key_file(config->keypair, &config->error,
615 key_file);
619 tls_config_set_key_mem(struct tls_config *config, const uint8_t *key,
620 size_t len)
622 return tls_keypair_set_key_mem(config->keypair, key, len);
625 static int
626 tls_config_set_keypair_file_internal(struct tls_config *config,
627 const char *cert_file, const char *key_file, const char *ocsp_file)
629 if (tls_config_set_cert_file(config, cert_file) != 0)
630 return (-1);
631 if (tls_config_set_key_file(config, key_file) != 0)
632 return (-1);
633 if (tls_config_set_key_file(config, key_file) != 0)
634 return (-1);
635 if (ocsp_file != NULL &&
636 tls_config_set_ocsp_staple_file(config, ocsp_file) != 0)
637 return (-1);
639 return (0);
642 static int
643 tls_config_set_keypair_mem_internal(struct tls_config *config, const uint8_t *cert,
644 size_t cert_len, const uint8_t *key, size_t key_len,
645 const uint8_t *staple, size_t staple_len)
647 if (tls_config_set_cert_mem(config, cert, cert_len) != 0)
648 return (-1);
649 if (tls_config_set_key_mem(config, key, key_len) != 0)
650 return (-1);
651 if ((staple != NULL) &&
652 (tls_config_set_ocsp_staple_mem(config, staple, staple_len) != 0))
653 return (-1);
655 return (0);
659 tls_config_set_keypair_file(struct tls_config *config,
660 const char *cert_file, const char *key_file)
662 return tls_config_set_keypair_file_internal(config, cert_file, key_file,
663 NULL);
667 tls_config_set_keypair_mem(struct tls_config *config, const uint8_t *cert,
668 size_t cert_len, const uint8_t *key, size_t key_len)
670 return tls_config_set_keypair_mem_internal(config, cert, cert_len,
671 key, key_len, NULL, 0);
675 tls_config_set_keypair_ocsp_file(struct tls_config *config,
676 const char *cert_file, const char *key_file, const char *ocsp_file)
678 return tls_config_set_keypair_file_internal(config, cert_file, key_file,
679 ocsp_file);
683 tls_config_set_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert,
684 size_t cert_len, const uint8_t *key, size_t key_len,
685 const uint8_t *staple, size_t staple_len)
687 return tls_config_set_keypair_mem_internal(config, cert, cert_len,
688 key, key_len, staple, staple_len);
693 tls_config_set_protocols(struct tls_config *config, uint32_t protocols)
695 config->protocols = protocols;
697 return (0);
701 tls_config_set_verify_depth(struct tls_config *config, int verify_depth)
703 config->verify_depth = verify_depth;
705 return (0);
708 void
709 tls_config_prefer_ciphers_client(struct tls_config *config)
711 config->ciphers_server = 0;
714 void
715 tls_config_prefer_ciphers_server(struct tls_config *config)
717 config->ciphers_server = 1;
720 void
721 tls_config_insecure_noverifycert(struct tls_config *config)
723 config->verify_cert = 0;
726 void
727 tls_config_insecure_noverifyname(struct tls_config *config)
729 config->verify_name = 0;
732 void
733 tls_config_insecure_noverifytime(struct tls_config *config)
735 config->verify_time = 0;
738 void
739 tls_config_verify(struct tls_config *config)
741 config->verify_cert = 1;
742 config->verify_name = 1;
743 config->verify_time = 1;
746 void
747 tls_config_ocsp_require_stapling(struct tls_config *config)
749 config->ocsp_require_stapling = 1;
752 void
753 tls_config_verify_client(struct tls_config *config)
755 config->verify_client = 1;
758 void
759 tls_config_verify_client_optional(struct tls_config *config)
761 config->verify_client = 2;
765 tls_config_set_ocsp_staple_file(struct tls_config *config, const char *staple_file)
767 return tls_keypair_set_ocsp_staple_file(config->keypair, &config->error,
768 staple_file);
772 tls_config_set_ocsp_staple_mem(struct tls_config *config, const uint8_t *staple,
773 size_t len)
775 return tls_keypair_set_ocsp_staple_mem(config->keypair, staple, len);
779 tls_config_set_session_id(struct tls_config *config,
780 const unsigned char *session_id, size_t len)
782 if (len > TLS_MAX_SESSION_ID_LENGTH) {
783 tls_config_set_errorx(config, "session ID too large");
784 return (-1);
786 memset(config->session_id, 0, sizeof(config->session_id));
787 memcpy(config->session_id, session_id, len);
788 return (0);
792 tls_config_set_session_lifetime(struct tls_config *config, int lifetime)
794 if (lifetime > TLS_MAX_SESSION_TIMEOUT) {
795 tls_config_set_errorx(config, "session lifetime too large");
796 return (-1);
798 if (lifetime != 0 && lifetime < TLS_MIN_SESSION_TIMEOUT) {
799 tls_config_set_errorx(config, "session lifetime too small");
800 return (-1);
803 config->session_lifetime = lifetime;
804 return (0);
808 tls_config_add_ticket_key(struct tls_config *config, uint32_t keyrev,
809 unsigned char *key, size_t keylen)
811 struct tls_ticket_key newkey;
812 int i;
814 if (TLS_TICKET_KEY_SIZE != keylen ||
815 sizeof(newkey.aes_key) + sizeof(newkey.hmac_key) > keylen) {
816 tls_config_set_errorx(config,
817 "wrong amount of ticket key data");
818 return (-1);
821 keyrev = htonl(keyrev);
822 memset(&newkey, 0, sizeof(newkey));
823 memcpy(newkey.key_name, &keyrev, sizeof(keyrev));
824 memcpy(newkey.aes_key, key, sizeof(newkey.aes_key));
825 memcpy(newkey.hmac_key, key + sizeof(newkey.aes_key),
826 sizeof(newkey.hmac_key));
827 newkey.time = time(NULL);
829 for (i = 0; i < TLS_NUM_TICKETS; i++) {
830 struct tls_ticket_key *tk = &config->ticket_keys[i];
831 if (memcmp(newkey.key_name, tk->key_name,
832 sizeof(tk->key_name)) != 0)
833 continue;
835 /* allow re-entry of most recent key */
836 if (i == 0 && memcmp(newkey.aes_key, tk->aes_key,
837 sizeof(tk->aes_key)) == 0 && memcmp(newkey.hmac_key,
838 tk->hmac_key, sizeof(tk->hmac_key)) == 0)
839 return (0);
840 tls_config_set_errorx(config, "ticket key already present");
841 return (-1);
844 memmove(&config->ticket_keys[1], &config->ticket_keys[0],
845 sizeof(config->ticket_keys) - sizeof(config->ticket_keys[0]));
846 config->ticket_keys[0] = newkey;
848 config->ticket_autorekey = 0;
850 return (0);
854 tls_config_ticket_autorekey(struct tls_config *config)
856 unsigned char key[TLS_TICKET_KEY_SIZE];
857 int rv;
859 arc4random_buf(key, sizeof(key));
860 rv = tls_config_add_ticket_key(config, config->ticket_keyrev++, key,
861 sizeof(key));
862 config->ticket_autorekey = 1;
863 return (rv);