diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libtls/tls_config.c | 79 |
1 files changed, 11 insertions, 68 deletions
diff --git a/src/lib/libtls/tls_config.c b/src/lib/libtls/tls_config.c index a348b826d5..6b47eeb8d1 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.20 2016/07/06 16:47:18 jsing Exp $ */ | 1 | /* $OpenBSD: tls_config.c,v 1.21 2016/07/07 14:09:03 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,13 +15,9 @@ | |||
| 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 | |||
| 20 | #include <ctype.h> | 18 | #include <ctype.h> |
| 21 | #include <errno.h> | 19 | #include <errno.h> |
| 22 | #include <fcntl.h> | ||
| 23 | #include <stdlib.h> | 20 | #include <stdlib.h> |
| 24 | #include <unistd.h> | ||
| 25 | 21 | ||
| 26 | #include <tls.h> | 22 | #include <tls.h> |
| 27 | #include "tls_internal.h" | 23 | #include "tls_internal.h" |
| @@ -61,53 +57,6 @@ set_mem(char **dest, size_t *destlen, const void *src, size_t srclen) | |||
| 61 | return 0; | 57 | return 0; |
| 62 | } | 58 | } |
| 63 | 59 | ||
| 64 | static int | ||
| 65 | load_file(struct tls_error *error, const char *filetype, const char *filename, | ||
| 66 | char **buf, size_t *len) | ||
| 67 | { | ||
| 68 | struct stat st; | ||
| 69 | int fd = -1; | ||
| 70 | |||
| 71 | free(*buf); | ||
| 72 | *buf = NULL; | ||
| 73 | *len = 0; | ||
| 74 | |||
| 75 | if ((fd = open(filename, O_RDONLY)) == -1) { | ||
| 76 | tls_error_set(error, "failed to open %s file '%s'", | ||
| 77 | filetype, filename); | ||
| 78 | goto fail; | ||
| 79 | } | ||
| 80 | if (fstat(fd, &st) != 0) { | ||
| 81 | tls_error_set(error, "failed to stat %s file '%s'", | ||
| 82 | filetype, filename); | ||
| 83 | goto fail; | ||
| 84 | } | ||
| 85 | *len = (size_t)st.st_size; | ||
| 86 | if ((*buf = malloc(*len)) == NULL) { | ||
| 87 | tls_error_set(error, "failed to allocate buffer for " | ||
| 88 | "%s file '%s'", filetype, filename); | ||
| 89 | goto fail; | ||
| 90 | } | ||
| 91 | if (read(fd, *buf, *len) != *len) { | ||
| 92 | tls_error_set(error, "failed to read %s file '%s'", | ||
| 93 | filetype, filename); | ||
| 94 | goto fail; | ||
| 95 | } | ||
| 96 | close(fd); | ||
| 97 | return 0; | ||
| 98 | |||
| 99 | fail: | ||
| 100 | if (fd != -1) | ||
| 101 | close(fd); | ||
| 102 | if (*buf != NULL) | ||
| 103 | explicit_bzero(*buf, *len); | ||
| 104 | free(*buf); | ||
| 105 | *buf = NULL; | ||
| 106 | *len = 0; | ||
| 107 | |||
| 108 | return -1; | ||
| 109 | } | ||
| 110 | |||
| 111 | static struct tls_keypair * | 60 | static struct tls_keypair * |
| 112 | tls_keypair_new() | 61 | tls_keypair_new() |
| 113 | { | 62 | { |
| @@ -115,11 +64,9 @@ tls_keypair_new() | |||
| 115 | } | 64 | } |
| 116 | 65 | ||
| 117 | static int | 66 | static int |
| 118 | tls_keypair_set_cert_file(struct tls_keypair *keypair, struct tls_error *error, | 67 | tls_keypair_set_cert_file(struct tls_keypair *keypair, const char *cert_file) |
| 119 | const char *cert_file) | ||
| 120 | { | 68 | { |
| 121 | return load_file(error, "certificate", cert_file, &keypair->cert_mem, | 69 | return set_string(&keypair->cert_file, cert_file); |
| 122 | &keypair->cert_len); | ||
| 123 | } | 70 | } |
| 124 | 71 | ||
| 125 | static int | 72 | static int |
| @@ -130,13 +77,9 @@ tls_keypair_set_cert_mem(struct tls_keypair *keypair, const uint8_t *cert, | |||
| 130 | } | 77 | } |
| 131 | 78 | ||
| 132 | static int | 79 | static int |
| 133 | tls_keypair_set_key_file(struct tls_keypair *keypair, struct tls_error *error, | 80 | tls_keypair_set_key_file(struct tls_keypair *keypair, const char *key_file) |
| 134 | const char *key_file) | ||
| 135 | { | 81 | { |
| 136 | if (keypair->key_mem != NULL) | 82 | return set_string(&keypair->key_file, key_file); |
| 137 | explicit_bzero(keypair->key_mem, keypair->key_len); | ||
| 138 | return load_file(error, "key", key_file, &keypair->key_mem, | ||
| 139 | &keypair->key_len); | ||
| 140 | } | 83 | } |
| 141 | 84 | ||
| 142 | static int | 85 | static int |
| @@ -163,7 +106,9 @@ tls_keypair_free(struct tls_keypair *keypair) | |||
| 163 | 106 | ||
| 164 | tls_keypair_clear(keypair); | 107 | tls_keypair_clear(keypair); |
| 165 | 108 | ||
| 109 | free((char *)keypair->cert_file); | ||
| 166 | free(keypair->cert_mem); | 110 | free(keypair->cert_mem); |
| 111 | free((char *)keypair->key_file); | ||
| 167 | free(keypair->key_mem); | 112 | free(keypair->key_mem); |
| 168 | 113 | ||
| 169 | free(keypair); | 114 | free(keypair); |
| @@ -221,6 +166,7 @@ tls_config_free(struct tls_config *config) | |||
| 221 | 166 | ||
| 222 | free(config->error.msg); | 167 | free(config->error.msg); |
| 223 | 168 | ||
| 169 | free((char *)config->ca_file); | ||
| 224 | free((char *)config->ca_mem); | 170 | free((char *)config->ca_mem); |
| 225 | free((char *)config->ca_path); | 171 | free((char *)config->ca_path); |
| 226 | free((char *)config->ciphers); | 172 | free((char *)config->ciphers); |
| @@ -306,8 +252,7 @@ tls_config_parse_protocols(uint32_t *protocols, const char *protostr) | |||
| 306 | int | 252 | int |
| 307 | tls_config_set_ca_file(struct tls_config *config, const char *ca_file) | 253 | tls_config_set_ca_file(struct tls_config *config, const char *ca_file) |
| 308 | { | 254 | { |
| 309 | return load_file(&config->error, "CA", ca_file, &config->ca_mem, | 255 | return set_string(&config->ca_file, ca_file); |
| 310 | &config->ca_len); | ||
| 311 | } | 256 | } |
| 312 | 257 | ||
| 313 | int | 258 | int |
| @@ -325,8 +270,7 @@ tls_config_set_ca_mem(struct tls_config *config, const uint8_t *ca, size_t len) | |||
| 325 | int | 270 | int |
| 326 | tls_config_set_cert_file(struct tls_config *config, const char *cert_file) | 271 | tls_config_set_cert_file(struct tls_config *config, const char *cert_file) |
| 327 | { | 272 | { |
| 328 | return tls_keypair_set_cert_file(config->keypair, &config->error, | 273 | return tls_keypair_set_cert_file(config->keypair, cert_file); |
| 329 | cert_file); | ||
| 330 | } | 274 | } |
| 331 | 275 | ||
| 332 | int | 276 | int |
| @@ -409,8 +353,7 @@ tls_config_set_ecdhecurve(struct tls_config *config, const char *name) | |||
| 409 | int | 353 | int |
| 410 | tls_config_set_key_file(struct tls_config *config, const char *key_file) | 354 | tls_config_set_key_file(struct tls_config *config, const char *key_file) |
| 411 | { | 355 | { |
| 412 | return tls_keypair_set_key_file(config->keypair, &config->error, | 356 | return tls_keypair_set_key_file(config->keypair, key_file); |
| 413 | key_file); | ||
| 414 | } | 357 | } |
| 415 | 358 | ||
| 416 | int | 359 | int |
