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