diff options
| author | jsing <> | 2016-08-13 13:05:51 +0000 |
|---|---|---|
| committer | jsing <> | 2016-08-13 13:05:51 +0000 |
| commit | c4deab91c7a1a342aaede40e7173d65352b9141c (patch) | |
| tree | dbfa584d89ca8f2821053defe496bba86e955463 /src/lib/libtls/tls_config.c | |
| parent | 55635d1508f076c656e20a5a0fe6f1cba7636f16 (diff) | |
| download | openbsd-c4deab91c7a1a342aaede40e7173d65352b9141c.tar.gz openbsd-c4deab91c7a1a342aaede40e7173d65352b9141c.tar.bz2 openbsd-c4deab91c7a1a342aaede40e7173d65352b9141c.zip | |
Load CA, certificate and key files into memory when the appropriate
tls_config_set_*_file() function is called. This allows us to immediately
propagate useful error messages, play more nicely with privsep/pledge and
have a single code path. Instead of always loading the default CA when
tls_config_new() is called, defer and only load the default CA when
tls_configure() is invoked, if a CA has not already been specified.
ok beck@ bluhm@
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libtls/tls_config.c | 81 |
1 files changed, 68 insertions, 13 deletions
diff --git a/src/lib/libtls/tls_config.c b/src/lib/libtls/tls_config.c index e690b9ee76..cd2a04cdd6 100644 --- a/src/lib/libtls/tls_config.c +++ b/src/lib/libtls/tls_config.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls_config.c,v 1.25 2016/08/12 15:10:59 jsing Exp $ */ | 1 | /* $OpenBSD: tls_config.c,v 1.26 2016/08/13 13:05:51 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -15,9 +15,13 @@ | |||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | */ | 16 | */ |
| 17 | 17 | ||
| 18 | #include <sys/stat.h> | ||
| 19 | |||
| 18 | #include <ctype.h> | 20 | #include <ctype.h> |
| 19 | #include <errno.h> | 21 | #include <errno.h> |
| 22 | #include <fcntl.h> | ||
| 20 | #include <stdlib.h> | 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> | ||
| 21 | 25 | ||
| 22 | #include <tls.h> | 26 | #include <tls.h> |
| 23 | #include "tls_internal.h" | 27 | #include "tls_internal.h" |
| @@ -64,9 +68,11 @@ tls_keypair_new() | |||
| 64 | } | 68 | } |
| 65 | 69 | ||
| 66 | static int | 70 | static int |
| 67 | tls_keypair_set_cert_file(struct tls_keypair *keypair, const char *cert_file) | 71 | tls_keypair_set_cert_file(struct tls_keypair *keypair, struct tls_error *error, |
| 72 | const char *cert_file) | ||
| 68 | { | 73 | { |
| 69 | return set_string(&keypair->cert_file, cert_file); | 74 | return tls_config_load_file(error, "certificate", cert_file, |
| 75 | &keypair->cert_mem, &keypair->cert_len); | ||
| 70 | } | 76 | } |
| 71 | 77 | ||
| 72 | static int | 78 | static int |
| @@ -77,9 +83,13 @@ tls_keypair_set_cert_mem(struct tls_keypair *keypair, const uint8_t *cert, | |||
| 77 | } | 83 | } |
| 78 | 84 | ||
| 79 | static int | 85 | static int |
| 80 | tls_keypair_set_key_file(struct tls_keypair *keypair, const char *key_file) | 86 | tls_keypair_set_key_file(struct tls_keypair *keypair, struct tls_error *error, |
| 87 | const char *key_file) | ||
| 81 | { | 88 | { |
| 82 | return set_string(&keypair->key_file, 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); | ||
| 83 | } | 93 | } |
| 84 | 94 | ||
| 85 | static int | 95 | static int |
| @@ -106,14 +116,59 @@ tls_keypair_free(struct tls_keypair *keypair) | |||
| 106 | 116 | ||
| 107 | tls_keypair_clear(keypair); | 117 | tls_keypair_clear(keypair); |
| 108 | 118 | ||
| 109 | free((char *)keypair->cert_file); | ||
| 110 | free(keypair->cert_mem); | 119 | free(keypair->cert_mem); |
| 111 | free((char *)keypair->key_file); | ||
| 112 | free(keypair->key_mem); | 120 | free(keypair->key_mem); |
| 113 | 121 | ||
| 114 | free(keypair); | 122 | free(keypair); |
| 115 | } | 123 | } |
| 116 | 124 | ||
| 125 | int | ||
| 126 | tls_config_load_file(struct tls_error *error, const char *filetype, | ||
| 127 | const char *filename, char **buf, size_t *len) | ||
| 128 | { | ||
| 129 | struct stat st; | ||
| 130 | int fd = -1; | ||
| 131 | |||
| 132 | free(*buf); | ||
| 133 | *buf = NULL; | ||
| 134 | *len = 0; | ||
| 135 | |||
| 136 | if ((fd = open(filename, O_RDONLY)) == -1) { | ||
| 137 | tls_error_set(error, "failed to open %s file '%s'", | ||
| 138 | filetype, filename); | ||
| 139 | goto fail; | ||
| 140 | } | ||
| 141 | if (fstat(fd, &st) != 0) { | ||
| 142 | tls_error_set(error, "failed to stat %s file '%s'", | ||
| 143 | filetype, filename); | ||
| 144 | goto fail; | ||
| 145 | } | ||
| 146 | *len = (size_t)st.st_size; | ||
| 147 | if ((*buf = malloc(*len)) == NULL) { | ||
| 148 | tls_error_set(error, "failed to allocate buffer for " | ||
| 149 | "%s file", filetype); | ||
| 150 | goto fail; | ||
| 151 | } | ||
| 152 | if (read(fd, *buf, *len) != *len) { | ||
| 153 | tls_error_set(error, "failed to read %s file '%s'", | ||
| 154 | filetype, filename); | ||
| 155 | goto fail; | ||
| 156 | } | ||
| 157 | close(fd); | ||
| 158 | return 0; | ||
| 159 | |||
| 160 | fail: | ||
| 161 | if (fd != -1) | ||
| 162 | close(fd); | ||
| 163 | if (*buf != NULL) | ||
| 164 | explicit_bzero(*buf, *len); | ||
| 165 | free(*buf); | ||
| 166 | *buf = NULL; | ||
| 167 | *len = 0; | ||
| 168 | |||
| 169 | return -1; | ||
| 170 | } | ||
| 171 | |||
| 117 | struct tls_config * | 172 | struct tls_config * |
| 118 | tls_config_new(void) | 173 | tls_config_new(void) |
| 119 | { | 174 | { |
| @@ -128,8 +183,6 @@ tls_config_new(void) | |||
| 128 | /* | 183 | /* |
| 129 | * Default configuration. | 184 | * Default configuration. |
| 130 | */ | 185 | */ |
| 131 | if (tls_config_set_ca_file(config, _PATH_SSL_CA_FILE) != 0) | ||
| 132 | goto err; | ||
| 133 | if (tls_config_set_dheparams(config, "none") != 0) | 186 | if (tls_config_set_dheparams(config, "none") != 0) |
| 134 | goto err; | 187 | goto err; |
| 135 | if (tls_config_set_ecdhecurve(config, "auto") != 0) | 188 | if (tls_config_set_ecdhecurve(config, "auto") != 0) |
| @@ -167,7 +220,6 @@ tls_config_free(struct tls_config *config) | |||
| 167 | free(config->error.msg); | 220 | free(config->error.msg); |
| 168 | 221 | ||
| 169 | free(config->alpn); | 222 | free(config->alpn); |
| 170 | free((char *)config->ca_file); | ||
| 171 | free((char *)config->ca_mem); | 223 | free((char *)config->ca_mem); |
| 172 | free((char *)config->ca_path); | 224 | free((char *)config->ca_path); |
| 173 | free((char *)config->ciphers); | 225 | free((char *)config->ciphers); |
| @@ -319,7 +371,8 @@ tls_config_set_alpn(struct tls_config *config, const char *alpn) | |||
| 319 | int | 371 | int |
| 320 | tls_config_set_ca_file(struct tls_config *config, const char *ca_file) | 372 | tls_config_set_ca_file(struct tls_config *config, const char *ca_file) |
| 321 | { | 373 | { |
| 322 | return set_string(&config->ca_file, ca_file); | 374 | return tls_config_load_file(&config->error, "CA", ca_file, |
| 375 | &config->ca_mem, &config->ca_len); | ||
| 323 | } | 376 | } |
| 324 | 377 | ||
| 325 | int | 378 | int |
| @@ -337,7 +390,8 @@ tls_config_set_ca_mem(struct tls_config *config, const uint8_t *ca, size_t len) | |||
| 337 | int | 390 | int |
| 338 | tls_config_set_cert_file(struct tls_config *config, const char *cert_file) | 391 | tls_config_set_cert_file(struct tls_config *config, const char *cert_file) |
| 339 | { | 392 | { |
| 340 | return tls_keypair_set_cert_file(config->keypair, cert_file); | 393 | return tls_keypair_set_cert_file(config->keypair, &config->error, |
| 394 | cert_file); | ||
| 341 | } | 395 | } |
| 342 | 396 | ||
| 343 | int | 397 | int |
| @@ -424,7 +478,8 @@ tls_config_set_ecdhecurve(struct tls_config *config, const char *name) | |||
| 424 | int | 478 | int |
| 425 | tls_config_set_key_file(struct tls_config *config, const char *key_file) | 479 | tls_config_set_key_file(struct tls_config *config, const char *key_file) |
| 426 | { | 480 | { |
| 427 | return tls_keypair_set_key_file(config->keypair, key_file); | 481 | return tls_keypair_set_key_file(config->keypair, &config->error, |
| 482 | key_file); | ||
| 428 | } | 483 | } |
| 429 | 484 | ||
| 430 | int | 485 | int |
