1 /* $OpenBSD: tls_config.c,v 1.56 2019/04/04 15:09:09 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.
19 #define NO_REDEF_POSIX_FUNCTIONS
33 #include "tls_internal.h"
35 static const char default_ca_file[] = "/etc/ssl/cert.pem";
38 tls_default_ca_cert_file(void)
40 return default_ca_file;
44 tls_config_load_file(struct tls_error *error, const char *filetype,
45 const char *filename, char **buf, size_t *len)
55 if ((fd = open(filename, O_RDONLY)) == -1) {
56 tls_error_set(error, "failed to open %s file '%s'",
60 if (fstat(fd, &st) != 0) {
61 tls_error_set(error, "failed to stat %s file '%s'",
67 *len = (size_t)st.st_size;
68 if ((*buf = malloc(*len)) == NULL) {
69 tls_error_set(error, "failed to allocate buffer for "
73 n = read(fd, *buf, *len);
74 if (n < 0 || (size_t)n != *len) {
75 tls_error_set(error, "failed to read %s file '%s'",
93 tls_config_new_internal(void)
95 struct tls_config *config;
96 unsigned char sid[TLS_MAX_SESSION_ID_LENGTH];
98 if ((config = calloc(1, sizeof(*config))) == NULL)
101 if (pthread_mutex_init(&config->mutex, NULL) != 0)
104 config->refcount = 1;
105 config->session_fd = -1;
107 if ((config->keypair = tls_keypair_new()) == NULL)
111 * Default configuration.
113 if (tls_config_set_dheparams(config, "none") != 0)
115 if (tls_config_set_ecdhecurves(config, "default") != 0)
117 if (tls_config_set_ciphers(config, "secure") != 0)
120 if (tls_config_set_protocols(config, TLS_PROTOCOLS_DEFAULT) != 0)
122 if (tls_config_set_verify_depth(config, 6) != 0)
126 * Set session ID context to a random value. For the simple case
127 * of a single process server this is good enough. For multiprocess
128 * servers the session ID needs to be set by the caller.
130 arc4random_buf(sid, sizeof(sid));
131 if (tls_config_set_session_id(config, sid, sizeof(sid)) != 0)
133 config->ticket_keyrev = arc4random();
134 config->ticket_autorekey = 1;
136 tls_config_prefer_ciphers_server(config);
138 tls_config_verify(config);
143 tls_config_free(config);
150 if (tls_init() == -1)
153 return tls_config_new_internal();
157 tls_config_free(struct tls_config *config)
159 struct tls_keypair *kp, *nkp;
165 pthread_mutex_lock(&config->mutex);
166 refcount = --config->refcount;
167 pthread_mutex_unlock(&config->mutex);
172 for (kp = config->keypair; kp != NULL; kp = nkp) {
174 tls_keypair_free(kp);
177 free(config->error.msg);
180 free((char *)config->ca_mem);
181 free((char *)config->ca_path);
182 free((char *)config->ciphers);
183 free((char *)config->crl_mem);
184 free(config->ecdhecurves);
190 tls_config_keypair_add(struct tls_config *config, struct tls_keypair *keypair)
192 struct tls_keypair *kp;
194 kp = config->keypair;
195 while (kp->next != NULL)
202 tls_config_error(struct tls_config *config)
204 return config->error.msg;
208 tls_config_clear_keys(struct tls_config *config)
210 struct tls_keypair *kp;
212 for (kp = config->keypair; kp != NULL; kp = kp->next)
213 tls_keypair_clear_key(kp);
217 tls_config_parse_protocols(uint32_t *protocols, const char *protostr)
219 uint32_t proto, protos = 0;
223 if (protostr == NULL) {
224 *protocols = TLS_PROTOCOLS_DEFAULT;
228 if ((s = strdup(protostr)) == NULL)
232 while ((p = strsep(&q, ",:")) != NULL) {
233 while (*p == ' ' || *p == '\t')
242 if (negate && protos == 0)
243 protos = TLS_PROTOCOLS_ALL;
246 if (strcasecmp(p, "all") == 0 ||
247 strcasecmp(p, "legacy") == 0)
248 proto = TLS_PROTOCOLS_ALL;
249 else if (strcasecmp(p, "default") == 0 ||
250 strcasecmp(p, "secure") == 0)
251 proto = TLS_PROTOCOLS_DEFAULT;
252 if (strcasecmp(p, "tlsv1") == 0)
253 proto = TLS_PROTOCOL_TLSv1;
254 else if (strcasecmp(p, "tlsv1.0") == 0)
255 proto = TLS_PROTOCOL_TLSv1_0;
256 else if (strcasecmp(p, "tlsv1.1") == 0)
257 proto = TLS_PROTOCOL_TLSv1_1;
258 else if (strcasecmp(p, "tlsv1.2") == 0)
259 proto = TLS_PROTOCOL_TLSv1_2;
280 tls_config_parse_alpn(struct tls_config *config, const char *alpn,
281 char **alpn_data, size_t *alpn_len)
283 size_t buf_len, i, len;
292 if ((buf_len = strlen(alpn) + 1) > 65535) {
293 tls_config_set_errorx(config, "alpn too large");
297 if ((buf = malloc(buf_len)) == NULL) {
298 tls_config_set_errorx(config, "out of memory");
302 if ((s = strdup(alpn)) == NULL) {
303 tls_config_set_errorx(config, "out of memory");
309 while ((p = strsep(&q, ",")) != NULL) {
310 if ((len = strlen(p)) == 0) {
311 tls_config_set_errorx(config,
312 "alpn protocol with zero length");
316 tls_config_set_errorx(config,
317 "alpn protocol too long");
320 buf[i++] = len & 0xff;
321 memcpy(&buf[i], p, len);
340 tls_config_set_alpn(struct tls_config *config, const char *alpn)
342 return tls_config_parse_alpn(config, alpn, &config->alpn,
347 tls_config_add_keypair_file_internal(struct tls_config *config,
348 const char *cert_file, const char *key_file, const char *ocsp_file)
350 struct tls_keypair *keypair;
352 if ((keypair = tls_keypair_new()) == NULL)
354 if (tls_keypair_set_cert_file(keypair, &config->error, cert_file) != 0)
356 if (tls_keypair_set_key_file(keypair, &config->error, key_file) != 0)
358 if (ocsp_file != NULL &&
359 tls_keypair_set_ocsp_staple_file(keypair, &config->error,
363 tls_config_keypair_add(config, keypair);
368 tls_keypair_free(keypair);
373 tls_config_add_keypair_mem_internal(struct tls_config *config, const uint8_t *cert,
374 size_t cert_len, const uint8_t *key, size_t key_len,
375 const uint8_t *staple, size_t staple_len)
377 struct tls_keypair *keypair;
379 if ((keypair = tls_keypair_new()) == NULL)
381 if (tls_keypair_set_cert_mem(keypair, &config->error, cert, cert_len) != 0)
383 if (tls_keypair_set_key_mem(keypair, &config->error, key, key_len) != 0)
385 if (staple != NULL &&
386 tls_keypair_set_ocsp_staple_mem(keypair, &config->error, staple,
390 tls_config_keypair_add(config, keypair);
395 tls_keypair_free(keypair);
400 tls_config_add_keypair_mem(struct tls_config *config, const uint8_t *cert,
401 size_t cert_len, const uint8_t *key, size_t key_len)
403 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
408 tls_config_add_keypair_file(struct tls_config *config,
409 const char *cert_file, const char *key_file)
411 return tls_config_add_keypair_file_internal(config, cert_file,
416 tls_config_add_keypair_ocsp_mem(struct tls_config *config, const uint8_t *cert,
417 size_t cert_len, const uint8_t *key, size_t key_len, const uint8_t *staple,
420 return tls_config_add_keypair_mem_internal(config, cert, cert_len, key,
421 key_len, staple, staple_len);
425 tls_config_add_keypair_ocsp_file(struct tls_config *config,
426 const char *cert_file, const char *key_file, const char *ocsp_file)
428 return tls_config_add_keypair_file_internal(config, cert_file,
429 key_file, ocsp_file);
433 tls_config_set_ca_file(struct tls_config *config, const char *ca_file)
435 return tls_config_load_file(&config->error, "CA", ca_file,
436 &config->ca_mem, &config->ca_len);
440 tls_config_set_ca_path(struct tls_config *config, const char *ca_path)
442 return tls_set_string(&config->ca_path, ca_path);
446 tls_config_set_ca_mem(struct tls_config *config, const uint8_t *ca, size_t len)
448 return tls_set_mem(&config->ca_mem, &config->ca_len, ca, len);
452 tls_config_set_cert_file(struct tls_config *config, const char *cert_file)
454 return tls_keypair_set_cert_file(config->keypair, &config->error,
459 tls_config_set_cert_mem(struct tls_config *config, const uint8_t *cert,
462 return tls_keypair_set_cert_mem(config->keypair, &config->error,
467 tls_config_set_ciphers(struct tls_config *config, const char *ciphers)
469 SSL_CTX *ssl_ctx = NULL;
471 if (ciphers == NULL ||
472 strcasecmp(ciphers, "default") == 0 ||
473 strcasecmp(ciphers, "secure") == 0)
474 ciphers = TLS_CIPHERS_DEFAULT;
475 else if (strcasecmp(ciphers, "compat") == 0)
476 ciphers = TLS_CIPHERS_COMPAT;
477 else if (strcasecmp(ciphers, "legacy") == 0)
478 ciphers = TLS_CIPHERS_LEGACY;
479 else if (strcasecmp(ciphers, "all") == 0 ||
480 strcasecmp(ciphers, "insecure") == 0)
481 ciphers = TLS_CIPHERS_ALL;
483 if ((ssl_ctx = SSL_CTX_new(SSLv23_method())) == NULL) {
484 tls_config_set_errorx(config, "out of memory");
487 if (SSL_CTX_set_cipher_list(ssl_ctx, ciphers) != 1) {
488 tls_config_set_errorx(config, "no ciphers for '%s'", ciphers);
492 SSL_CTX_free(ssl_ctx);
493 return tls_set_string(&config->ciphers, ciphers);
496 SSL_CTX_free(ssl_ctx);
501 tls_config_set_crl_file(struct tls_config *config, const char *crl_file)
503 return tls_config_load_file(&config->error, "CRL", crl_file,
504 &config->crl_mem, &config->crl_len);
508 tls_config_set_crl_mem(struct tls_config *config, const uint8_t *crl,
511 return tls_set_mem(&config->crl_mem, &config->crl_len, crl, len);
515 tls_config_set_dheparams(struct tls_config *config, const char *params)
519 if (params == NULL || strcasecmp(params, "none") == 0)
521 else if (strcasecmp(params, "auto") == 0)
523 else if (strcasecmp(params, "legacy") == 0)
526 tls_config_set_errorx(config, "invalid dhe param '%s'", params);
530 config->dheparams = keylen;
536 tls_config_set_ecdhecurve(struct tls_config *config, const char *curve)
539 strcasecmp(curve, "none") == 0 ||
540 strcasecmp(curve, "auto") == 0) {
541 curve = TLS_ECDHE_CURVES;
542 } else if (strchr(curve, ',') != NULL || strchr(curve, ':') != NULL) {
543 tls_config_set_errorx(config, "invalid ecdhe curve '%s'",
548 return tls_config_set_ecdhecurves(config, curve);
552 tls_config_set_ecdhecurves(struct tls_config *config, const char *curves)
554 int *curves_list = NULL, *curves_new;
555 size_t curves_num = 0;
561 free(config->ecdhecurves);
562 config->ecdhecurves = NULL;
563 config->ecdhecurves_len = 0;
565 if (curves == NULL || strcasecmp(curves, "default") == 0)
566 curves = TLS_ECDHE_CURVES;
568 if ((cs = strdup(curves)) == NULL) {
569 tls_config_set_errorx(config, "out of memory");
574 while ((p = strsep(&q, ",:")) != NULL) {
575 while (*p == ' ' || *p == '\t')
579 if (nid == NID_undef)
581 if (nid == NID_undef)
582 nid = EC_curve_nist2nid(p);
583 if (nid == NID_undef) {
584 tls_config_set_errorx(config,
585 "invalid ecdhe curve '%s'", p);
589 if ((curves_new = reallocarray(curves_list, curves_num + 1,
590 sizeof(int))) == NULL) {
591 tls_config_set_errorx(config, "out of memory");
594 curves_list = curves_new;
595 curves_list[curves_num] = nid;
599 config->ecdhecurves = curves_list;
600 config->ecdhecurves_len = curves_num;
613 tls_config_set_key_file(struct tls_config *config, const char *key_file)
615 return tls_keypair_set_key_file(config->keypair, &config->error,
620 tls_config_set_key_mem(struct tls_config *config, const uint8_t *key,
623 return tls_keypair_set_key_mem(config->keypair, &config->error,
628 tls_config_set_keypair_file_internal(struct tls_config *config,
629 const char *cert_file, const char *key_file, const char *ocsp_file)
631 if (tls_config_set_cert_file(config, cert_file) != 0)
633 if (tls_config_set_key_file(config, key_file) != 0)
635 if (ocsp_file != NULL &&
636 tls_config_set_ocsp_staple_file(config, ocsp_file) != 0)
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)
649 if (tls_config_set_key_mem(config, key, key_len) != 0)
651 if ((staple != NULL) &&
652 (tls_config_set_ocsp_staple_mem(config, staple, staple_len) != 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,
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,
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;
701 tls_config_set_session_fd(struct tls_config *config, int session_fd)
706 if (session_fd == -1) {
707 config->session_fd = session_fd;
711 if (fstat(session_fd, &sb) == -1) {
712 tls_config_set_error(config, "failed to stat session file");
715 if (!S_ISREG(sb.st_mode)) {
716 tls_config_set_errorx(config,
717 "session file is not a regular file");
721 if (sb.st_uid != getuid()) {
722 tls_config_set_errorx(config, "session file has incorrect "
723 "owner (uid %i != %i)", sb.st_uid, getuid());
726 mugo = sb.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO);
727 if (mugo != (S_IRUSR|S_IWUSR)) {
728 tls_config_set_errorx(config, "session file has incorrect "
729 "permissions (%o != 600)", mugo);
733 config->session_fd = session_fd;
739 tls_config_set_verify_depth(struct tls_config *config, int verify_depth)
741 config->verify_depth = verify_depth;
747 tls_config_prefer_ciphers_client(struct tls_config *config)
749 config->ciphers_server = 0;
753 tls_config_prefer_ciphers_server(struct tls_config *config)
755 config->ciphers_server = 1;
759 tls_config_insecure_noverifycert(struct tls_config *config)
761 config->verify_cert = 0;
765 tls_config_insecure_noverifyname(struct tls_config *config)
767 config->verify_name = 0;
771 tls_config_insecure_noverifytime(struct tls_config *config)
773 config->verify_time = 0;
777 tls_config_verify(struct tls_config *config)
779 config->verify_cert = 1;
780 config->verify_name = 1;
781 config->verify_time = 1;
785 tls_config_ocsp_require_stapling(struct tls_config *config)
787 config->ocsp_require_stapling = 1;
791 tls_config_verify_client(struct tls_config *config)
793 config->verify_client = 1;
797 tls_config_verify_client_optional(struct tls_config *config)
799 config->verify_client = 2;
803 tls_config_skip_private_key_check(struct tls_config *config)
805 config->skip_private_key_check = 1;
809 tls_config_set_ocsp_staple_file(struct tls_config *config, const char *staple_file)
811 return tls_keypair_set_ocsp_staple_file(config->keypair, &config->error,
816 tls_config_set_ocsp_staple_mem(struct tls_config *config, const uint8_t *staple,
819 return tls_keypair_set_ocsp_staple_mem(config->keypair, &config->error,
824 tls_config_set_session_id(struct tls_config *config,
825 const unsigned char *session_id, size_t len)
827 if (len > TLS_MAX_SESSION_ID_LENGTH) {
828 tls_config_set_errorx(config, "session ID too large");
831 memset(config->session_id, 0, sizeof(config->session_id));
832 memcpy(config->session_id, session_id, len);
837 tls_config_set_session_lifetime(struct tls_config *config, int lifetime)
839 if (lifetime > TLS_MAX_SESSION_TIMEOUT) {
840 tls_config_set_errorx(config, "session lifetime too large");
843 if (lifetime != 0 && lifetime < TLS_MIN_SESSION_TIMEOUT) {
844 tls_config_set_errorx(config, "session lifetime too small");
848 config->session_lifetime = lifetime;
853 tls_config_add_ticket_key(struct tls_config *config, uint32_t keyrev,
854 unsigned char *key, size_t keylen)
856 struct tls_ticket_key newkey;
859 if (TLS_TICKET_KEY_SIZE != keylen ||
860 sizeof(newkey.aes_key) + sizeof(newkey.hmac_key) > keylen) {
861 tls_config_set_errorx(config,
862 "wrong amount of ticket key data");
866 keyrev = htonl(keyrev);
867 memset(&newkey, 0, sizeof(newkey));
868 memcpy(newkey.key_name, &keyrev, sizeof(keyrev));
869 memcpy(newkey.aes_key, key, sizeof(newkey.aes_key));
870 memcpy(newkey.hmac_key, key + sizeof(newkey.aes_key),
871 sizeof(newkey.hmac_key));
872 newkey.time = time(NULL);
874 for (i = 0; i < TLS_NUM_TICKETS; i++) {
875 struct tls_ticket_key *tk = &config->ticket_keys[i];
876 if (memcmp(newkey.key_name, tk->key_name,
877 sizeof(tk->key_name)) != 0)
880 /* allow re-entry of most recent key */
881 if (i == 0 && memcmp(newkey.aes_key, tk->aes_key,
882 sizeof(tk->aes_key)) == 0 && memcmp(newkey.hmac_key,
883 tk->hmac_key, sizeof(tk->hmac_key)) == 0)
885 tls_config_set_errorx(config, "ticket key already present");
889 memmove(&config->ticket_keys[1], &config->ticket_keys[0],
890 sizeof(config->ticket_keys) - sizeof(config->ticket_keys[0]));
891 config->ticket_keys[0] = newkey;
893 config->ticket_autorekey = 0;
899 tls_config_ticket_autorekey(struct tls_config *config)
901 unsigned char key[TLS_TICKET_KEY_SIZE];
904 arc4random_buf(key, sizeof(key));
905 rv = tls_config_add_ticket_key(config, config->ticket_keyrev++, key,
907 config->ticket_autorekey = 1;