1 /* $OpenBSD: tls_config.c,v 1.58 2020/01/20 08:39:21 jsing Exp $ */
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.
29 #include "tls_internal.h"
31 static const char default_ca_file[] = TLS_DEFAULT_CA_FILE;
34 tls_default_ca_cert_file(void)
36 return default_ca_file;
40 tls_config_load_file(struct tls_error *error, const char *filetype,
41 const char *filename, char **buf, size_t *len)
51 if ((fd = open(filename, O_RDONLY)) == -1) {
52 tls_error_set(error, "failed to open %s file '%s'",
56 if (fstat(fd, &st) != 0) {
57 tls_error_set(error, "failed to stat %s file '%s'",
63 *len = (size_t)st.st_size;
64 if ((*buf = malloc(*len)) == NULL) {
65 tls_error_set(error, "failed to allocate buffer for "
69 n = read(fd, *buf, *len);
70 if (n < 0 || (size_t)n != *len) {
71 tls_error_set(error, "failed to read %s file '%s'",
89 tls_config_new_internal(void)
91 struct tls_config *config;
92 unsigned char sid[TLS_MAX_SESSION_ID_LENGTH];
94 if ((config = calloc(1, sizeof(*config))) == NULL)
97 if (pthread_mutex_init(&config->mutex, NULL) != 0)
100 config->refcount = 1;
101 config->session_fd = -1;
103 if ((config->keypair = tls_keypair_new()) == NULL)
107 * Default configuration.
109 if (tls_config_set_dheparams(config, "none") != 0)
111 if (tls_config_set_ecdhecurves(config, "default") != 0)
113 if (tls_config_set_ciphers(config, "secure") != 0)
116 if (tls_config_set_protocols(config, TLS_PROTOCOLS_DEFAULT) != 0)
118 if (tls_config_set_verify_depth(config, 6) != 0)
122 * Set session ID context to a random value. For the simple case
123 * of a single process server this is good enough. For multiprocess
124 * servers the session ID needs to be set by the caller.
126 arc4random_buf(sid, sizeof(sid));
127 if (tls_config_set_session_id(config, sid, sizeof(sid)) != 0)
129 config->ticket_keyrev = arc4random();
130 config->ticket_autorekey = 1;
132 tls_config_prefer_ciphers_server(config);
134 tls_config_verify(config);
139 tls_config_free(config);
146 if (tls_init() == -1)
149 return tls_config_new_internal();
153 tls_config_free(struct tls_config *config)
155 struct tls_keypair *kp, *nkp;
161 pthread_mutex_lock(&config->mutex);
162 refcount = --config->refcount;
163 pthread_mutex_unlock(&config->mutex);
168 for (kp = config->keypair; kp != NULL; kp = nkp) {
170 tls_keypair_free(kp);
173 free(config->error.msg);
176 free((char *)config->ca_mem);
177 free((char *)config->ca_path);
178 free((char *)config->ciphers);
179 free((char *)config->crl_mem);
180 free(config->ecdhecurves);
186 tls_config_keypair_add(struct tls_config *config, struct tls_keypair *keypair)
188 struct tls_keypair *kp;
190 kp = config->keypair;
191 while (kp->next != NULL)
198 tls_config_error(struct tls_config *config)
200 return config->error.msg;
204 tls_config_clear_keys(struct tls_config *config)
206 struct tls_keypair *kp;
208 for (kp = config->keypair; kp != NULL; kp = kp->next)
209 tls_keypair_clear_key(kp);
213 tls_config_parse_protocols(uint32_t *protocols, const char *protostr)
215 uint32_t proto, protos = 0;
219 if (protostr == NULL) {
220 *protocols = TLS_PROTOCOLS_DEFAULT;
224 if ((s = strdup(protostr)) == NULL)
228 while ((p = strsep(&q, ",:")) != NULL) {
229 while (*p == ' ' || *p == '\t')
238 if (negate && protos == 0)
239 protos = TLS_PROTOCOLS_ALL;
242 if (strcasecmp(p, "all") == 0 ||
243 strcasecmp(p, "legacy") == 0)
244 proto = TLS_PROTOCOLS_ALL;
245 else if (strcasecmp(p, "default") == 0 ||
246 strcasecmp(p, "secure") == 0)
247 proto = TLS_PROTOCOLS_DEFAULT;
248 if (strcasecmp(p, "tlsv1") == 0)
249 proto = TLS_PROTOCOL_TLSv1;
250 else if (strcasecmp(p, "tlsv1.0") == 0)
251 proto = TLS_PROTOCOL_TLSv1_0;
252 else if (strcasecmp(p, "tlsv1.1") == 0)
253 proto = TLS_PROTOCOL_TLSv1_1;
254 else if (strcasecmp(p, "tlsv1.2") == 0)
255 proto = TLS_PROTOCOL_TLSv1_2;
256 else if (strcasecmp(p, "tlsv1.3") == 0)
257 proto = TLS_PROTOCOL_TLSv1_3;
278 tls_config_parse_alpn(struct tls_config *config, const char *alpn,
279 char **alpn_data, size_t *alpn_len)
281 size_t buf_len, i, len;
290 if ((buf_len = strlen(alpn) + 1) > 65535) {
291 tls_config_set_errorx(config, "alpn too large");
295 if ((buf = malloc(buf_len)) == NULL) {
296 tls_config_set_errorx(config, "out of memory");
300 if ((s = strdup(alpn)) == NULL) {
301 tls_config_set_errorx(config, "out of memory");
307 while ((p = strsep(&q, ",")) != NULL) {
308 if ((len = strlen(p)) == 0) {
309 tls_config_set_errorx(config,
310 "alpn protocol with zero length");
314 tls_config_set_errorx(config,
315 "alpn protocol too long");
318 buf[i++] = len & 0xff;
319 memcpy(&buf[i], p, len);
338 tls_config_set_alpn(struct tls_config *config, const char *alpn)
340 return tls_config_parse_alpn(config, alpn, &config->alpn,
345 tls_config_add_keypair_file_internal(struct tls_config *config,
346 const char *cert_file, const char *key_file, const char *ocsp_file)
348 struct tls_keypair *keypair;
350 if ((keypair = tls_keypair_new()) == NULL)
352 if (tls_keypair_set_cert_file(keypair, &config->error, cert_file) != 0)
354 if (tls_keypair_set_key_file(keypair, &config->error, key_file) != 0)
356 if (ocsp_file != NULL &&
357 tls_keypair_set_ocsp_staple_file(keypair, &config->error,
361 tls_config_keypair_add(config, keypair);
366 tls_keypair_free(keypair);
371 tls_config_add_keypair_mem_internal(struct tls_config *config, const uint8_t *cert,
372 size_t cert_len, const uint8_t *key, size_t key_len,
373 const uint8_t *staple, size_t staple_len)
375 struct tls_keypair *keypair;
377 if ((keypair = tls_keypair_new()) == NULL)
379 if (tls_keypair_set_cert_mem(keypair, &config->error, cert, cert_len) != 0)
381 if (tls_keypair_set_key_mem(keypair, &config->error, key, key_len) != 0)
383 if (staple != NULL &&
384 tls_keypair_set_ocsp_staple_mem(keypair, &config->error, staple,
388 tls_config_keypair_add(config, keypair);
393 tls_keypair_free(keypair);
398 tls_config_add_keypair_mem(struct tls_config *config, const uint8_t *cert,
399 size_t cert_len, const uint8_t *key, size_t key_len)
401 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
406 tls_config_add_keypair_file(struct tls_config *config,
407 const char *cert_file, const char *key_file)
409 return tls_config_add_keypair_file_internal(config, cert_file,
414 tls_config_add_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert,
415 size_t cert_len, const uint8_t *key, size_t key_len, const uint8_t *staple,
418 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
419 key_len, staple, staple_len);
423 tls_config_add_keypair_ocsp_file(struct tls_config *config,
424 const char *cert_file, const char *key_file, const char *ocsp_file)
426 return tls_config_add_keypair_file_internal(config, cert_file,
427 key_file, ocsp_file);
431 tls_config_set_ca_file(struct tls_config *config, const char *ca_file)
433 return tls_config_load_file(&config->error, "CA", ca_file,
434 &config->ca_mem, &config->ca_len);
438 tls_config_set_ca_path(struct tls_config *config, const char *ca_path)
440 return tls_set_string(&config->ca_path, ca_path);
444 tls_config_set_ca_mem(struct tls_config *config, const uint8_t *ca, size_t len)
446 return tls_set_mem(&config->ca_mem, &config->ca_len, ca, len);
450 tls_config_set_cert_file(struct tls_config *config, const char *cert_file)
452 return tls_keypair_set_cert_file(config->keypair, &config->error,
457 tls_config_set_cert_mem(struct tls_config *config, const uint8_t *cert,
460 return tls_keypair_set_cert_mem(config->keypair, &config->error,
465 tls_config_set_ciphers(struct tls_config *config, const char *ciphers)
467 SSL_CTX *ssl_ctx = NULL;
469 if (ciphers == NULL ||
470 strcasecmp(ciphers, "default") == 0 ||
471 strcasecmp(ciphers, "secure") == 0)
472 ciphers = TLS_CIPHERS_DEFAULT;
473 else if (strcasecmp(ciphers, "compat") == 0)
474 ciphers = TLS_CIPHERS_COMPAT;
475 else if (strcasecmp(ciphers, "legacy") == 0)
476 ciphers = TLS_CIPHERS_LEGACY;
477 else if (strcasecmp(ciphers, "all") == 0 ||
478 strcasecmp(ciphers, "insecure") == 0)
479 ciphers = TLS_CIPHERS_ALL;
481 if ((ssl_ctx = SSL_CTX_new(SSLv23_method())) == NULL) {
482 tls_config_set_errorx(config, "out of memory");
485 if (SSL_CTX_set_cipher_list(ssl_ctx, ciphers) != 1) {
486 tls_config_set_errorx(config, "no ciphers for '%s'", ciphers);
490 SSL_CTX_free(ssl_ctx);
491 return tls_set_string(&config->ciphers, ciphers);
494 SSL_CTX_free(ssl_ctx);
499 tls_config_set_crl_file(struct tls_config *config, const char *crl_file)
501 return tls_config_load_file(&config->error, "CRL", crl_file,
502 &config->crl_mem, &config->crl_len);
506 tls_config_set_crl_mem(struct tls_config *config, const uint8_t *crl,
509 return tls_set_mem(&config->crl_mem, &config->crl_len, crl, len);
513 tls_config_set_dheparams(struct tls_config *config, const char *params)
517 if (params == NULL || strcasecmp(params, "none") == 0)
519 else if (strcasecmp(params, "auto") == 0)
521 else if (strcasecmp(params, "legacy") == 0)
524 tls_config_set_errorx(config, "invalid dhe param '%s'", params);
528 config->dheparams = keylen;
534 tls_config_set_ecdhecurve(struct tls_config *config, const char *curve)
537 strcasecmp(curve, "none") == 0 ||
538 strcasecmp(curve, "auto") == 0) {
539 curve = TLS_ECDHE_CURVES;
540 } else if (strchr(curve, ',') != NULL || strchr(curve, ':') != NULL) {
541 tls_config_set_errorx(config, "invalid ecdhe curve '%s'",
546 return tls_config_set_ecdhecurves(config, curve);
550 tls_config_set_ecdhecurves(struct tls_config *config, const char *curves)
552 int *curves_list = NULL, *curves_new;
553 size_t curves_num = 0;
559 free(config->ecdhecurves);
560 config->ecdhecurves = NULL;
561 config->ecdhecurves_len = 0;
563 if (curves == NULL || strcasecmp(curves, "default") == 0)
564 curves = TLS_ECDHE_CURVES;
566 if ((cs = strdup(curves)) == NULL) {
567 tls_config_set_errorx(config, "out of memory");
572 while ((p = strsep(&q, ",:")) != NULL) {
573 while (*p == ' ' || *p == '\t')
577 if (nid == NID_undef)
579 if (nid == NID_undef)
580 nid = EC_curve_nist2nid(p);
581 if (nid == NID_undef) {
582 tls_config_set_errorx(config,
583 "invalid ecdhe curve '%s'", p);
587 if ((curves_new = reallocarray(curves_list, curves_num + 1,
588 sizeof(int))) == NULL) {
589 tls_config_set_errorx(config, "out of memory");
592 curves_list = curves_new;
593 curves_list[curves_num] = nid;
597 config->ecdhecurves = curves_list;
598 config->ecdhecurves_len = curves_num;
611 tls_config_set_key_file(struct tls_config *config, const char *key_file)
613 return tls_keypair_set_key_file(config->keypair, &config->error,
618 tls_config_set_key_mem(struct tls_config *config, const uint8_t *key,
621 return tls_keypair_set_key_mem(config->keypair, &config->error,
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)
631 if (tls_config_set_key_file(config, key_file) != 0)
633 if (ocsp_file != NULL &&
634 tls_config_set_ocsp_staple_file(config, ocsp_file) != 0)
641 tls_config_set_keypair_mem_internal(struct tls_config *config, const uint8_t *cert,
642 size_t cert_len, const uint8_t *key, size_t key_len,
643 const uint8_t *staple, size_t staple_len)
645 if (tls_config_set_cert_mem(config, cert, cert_len) != 0)
647 if (tls_config_set_key_mem(config, key, key_len) != 0)
649 if ((staple != NULL) &&
650 (tls_config_set_ocsp_staple_mem(config, staple, staple_len) != 0))
657 tls_config_set_keypair_file(struct tls_config *config,
658 const char *cert_file, const char *key_file)
660 return tls_config_set_keypair_file_internal(config, cert_file, key_file,
665 tls_config_set_keypair_mem(struct tls_config *config, const uint8_t *cert,
666 size_t cert_len, const uint8_t *key, size_t key_len)
668 return tls_config_set_keypair_mem_internal(config, cert, cert_len,
669 key, key_len, NULL, 0);
673 tls_config_set_keypair_ocsp_file(struct tls_config *config,
674 const char *cert_file, const char *key_file, const char *ocsp_file)
676 return tls_config_set_keypair_file_internal(config, cert_file, key_file,
681 tls_config_set_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert,
682 size_t cert_len, const uint8_t *key, size_t key_len,
683 const uint8_t *staple, size_t staple_len)
685 return tls_config_set_keypair_mem_internal(config, cert, cert_len,
686 key, key_len, staple, staple_len);
691 tls_config_set_protocols(struct tls_config *config, uint32_t protocols)
693 config->protocols = protocols;
699 tls_config_set_session_fd(struct tls_config *config, int session_fd)
704 if (session_fd == -1) {
705 config->session_fd = session_fd;
709 if (fstat(session_fd, &sb) == -1) {
710 tls_config_set_error(config, "failed to stat session file");
713 if (!S_ISREG(sb.st_mode)) {
714 tls_config_set_errorx(config,
715 "session file is not a regular file");
719 if (sb.st_uid != getuid()) {
720 tls_config_set_errorx(config, "session file has incorrect "
721 "owner (uid %i != %i)", sb.st_uid, getuid());
724 mugo = sb.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO);
725 if (mugo != (S_IRUSR|S_IWUSR)) {
726 tls_config_set_errorx(config, "session file has incorrect "
727 "permissions (%o != 600)", mugo);
731 config->session_fd = session_fd;
737 tls_config_set_verify_depth(struct tls_config *config, int verify_depth)
739 config->verify_depth = verify_depth;
745 tls_config_prefer_ciphers_client(struct tls_config *config)
747 config->ciphers_server = 0;
751 tls_config_prefer_ciphers_server(struct tls_config *config)
753 config->ciphers_server = 1;
757 tls_config_insecure_noverifycert(struct tls_config *config)
759 config->verify_cert = 0;
763 tls_config_insecure_noverifyname(struct tls_config *config)
765 config->verify_name = 0;
769 tls_config_insecure_noverifytime(struct tls_config *config)
771 config->verify_time = 0;
775 tls_config_verify(struct tls_config *config)
777 config->verify_cert = 1;
778 config->verify_name = 1;
779 config->verify_time = 1;
783 tls_config_ocsp_require_stapling(struct tls_config *config)
785 config->ocsp_require_stapling = 1;
789 tls_config_verify_client(struct tls_config *config)
791 config->verify_client = 1;
795 tls_config_verify_client_optional(struct tls_config *config)
797 config->verify_client = 2;
801 tls_config_skip_private_key_check(struct tls_config *config)
803 config->skip_private_key_check = 1;
807 tls_config_set_ocsp_staple_file(struct tls_config *config, const char *staple_file)
809 return tls_keypair_set_ocsp_staple_file(config->keypair, &config->error,
814 tls_config_set_ocsp_staple_mem(struct tls_config *config, const uint8_t *staple,
817 return tls_keypair_set_ocsp_staple_mem(config->keypair, &config->error,
822 tls_config_set_session_id(struct tls_config *config,
823 const unsigned char *session_id, size_t len)
825 if (len > TLS_MAX_SESSION_ID_LENGTH) {
826 tls_config_set_errorx(config, "session ID too large");
829 memset(config->session_id, 0, sizeof(config->session_id));
830 memcpy(config->session_id, session_id, len);
835 tls_config_set_session_lifetime(struct tls_config *config, int lifetime)
837 if (lifetime > TLS_MAX_SESSION_TIMEOUT) {
838 tls_config_set_errorx(config, "session lifetime too large");
841 if (lifetime != 0 && lifetime < TLS_MIN_SESSION_TIMEOUT) {
842 tls_config_set_errorx(config, "session lifetime too small");
846 config->session_lifetime = lifetime;
851 tls_config_add_ticket_key(struct tls_config *config, uint32_t keyrev,
852 unsigned char *key, size_t keylen)
854 struct tls_ticket_key newkey;
857 if (TLS_TICKET_KEY_SIZE != keylen ||
858 sizeof(newkey.aes_key) + sizeof(newkey.hmac_key) > keylen) {
859 tls_config_set_errorx(config,
860 "wrong amount of ticket key data");
864 keyrev = htonl(keyrev);
865 memset(&newkey, 0, sizeof(newkey));
866 memcpy(newkey.key_name, &keyrev, sizeof(keyrev));
867 memcpy(newkey.aes_key, key, sizeof(newkey.aes_key));
868 memcpy(newkey.hmac_key, key + sizeof(newkey.aes_key),
869 sizeof(newkey.hmac_key));
870 newkey.time = time(NULL);
872 for (i = 0; i < TLS_NUM_TICKETS; i++) {
873 struct tls_ticket_key *tk = &config->ticket_keys[i];
874 if (memcmp(newkey.key_name, tk->key_name,
875 sizeof(tk->key_name)) != 0)
878 /* allow re-entry of most recent key */
879 if (i == 0 && memcmp(newkey.aes_key, tk->aes_key,
880 sizeof(tk->aes_key)) == 0 && memcmp(newkey.hmac_key,
881 tk->hmac_key, sizeof(tk->hmac_key)) == 0)
883 tls_config_set_errorx(config, "ticket key already present");
887 memmove(&config->ticket_keys[1], &config->ticket_keys[0],
888 sizeof(config->ticket_keys) - sizeof(config->ticket_keys[0]));
889 config->ticket_keys[0] = newkey;
891 config->ticket_autorekey = 0;
897 tls_config_ticket_autorekey(struct tls_config *config)
899 unsigned char key[TLS_TICKET_KEY_SIZE];
902 arc4random_buf(key, sizeof(key));
903 rv = tls_config_add_ticket_key(config, config->ticket_keyrev++, key,
905 config->ticket_autorekey = 1;