1 /* $OpenBSD: tls_util.c,v 1.16 2023/05/14 07:26:25 op Exp $ */
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
5 * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 #include "tls_internal.h"
33 memdup(const void *in, size_t len)
37 if ((out = malloc(len)) == NULL)
44 tls_set_mem(char **dest, size_t *destlen, const void *src, size_t srclen)
50 if ((*dest = memdup(src, srclen)) == NULL)
58 tls_set_string(const char **dest, const char *src)
63 if ((*dest = strdup(src)) == NULL)
69 * Extract the host and port from a colon separated value. For a literal IPv6
70 * address the address must be contained with square braces. If a host and
71 * port are successfully extracted, the function will return 0 and the
72 * caller is responsible for freeing the host and port. If no port is found
73 * then the function will return 1, with both host and port being NULL.
74 * On memory allocation failure -1 will be returned.
77 tls_host_port(const char *hostport, char **host, char **port)
85 if ((s = strdup(hostport)) == NULL)
90 /* See if this is an IPv6 literal with square braces. */
93 if ((p = strchr(s, ']')) == NULL)
98 /* Find the port separator. */
99 if ((p = strchr(p, ':')) == NULL)
102 /* If there is another separator then we have issues. */
103 if (strchr(p + 1, ':') != NULL)
108 if (asprintf(host, "%s", h) == -1) {
112 if (asprintf(port, "%s", p) == -1) {
134 tls_password_cb(char *buf, int size, int rwflag, void *u)
142 memset(buf, 0, size);
146 if ((len = strlcpy(buf, u, size)) >= (size_t)size)
153 tls_load_file(const char *name, size_t *len, char *password)
156 EVP_PKEY *key = NULL;
167 if ((fd = open(name, O_RDONLY)) == -1)
170 /* Just load the file into memory without decryption */
171 if (password == NULL) {
172 if (fstat(fd, &st) != 0)
176 size = (size_t)st.st_size;
177 if ((buf = malloc(size)) == NULL)
179 n = read(fd, buf, size);
180 if (n < 0 || (size_t)n != size)
186 /* Or read the (possibly) encrypted key from file */
187 if ((fp = fdopen(fd, "r")) == NULL)
191 key = PEM_read_PrivateKey(fp, NULL, tls_password_cb, password);
196 /* Write unencrypted key to memory buffer */
197 if ((bio = BIO_new(BIO_s_mem())) == NULL)
199 if (!PEM_write_bio_PrivateKey(bio, key, NULL, NULL, 0, NULL, NULL))
201 if ((size = BIO_get_mem_data(bio, &data)) <= 0)
203 if ((buf = malloc(size)) == NULL)
205 memcpy(buf, data, size);
225 tls_unload_file(uint8_t *buf, size_t len)