Blame


1 f9ab77a8 2023-08-23 op /* $OpenBSD: tls_util.c,v 1.16 2023/05/14 07:26:25 op Exp $ */
2 f9ab77a8 2023-08-23 op /*
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>
6 f9ab77a8 2023-08-23 op *
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.
10 f9ab77a8 2023-08-23 op *
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.
18 f9ab77a8 2023-08-23 op */
19 f9ab77a8 2023-08-23 op
20 f9ab77a8 2023-08-23 op #include "config.h"
21 f9ab77a8 2023-08-23 op
22 f9ab77a8 2023-08-23 op #include <sys/stat.h>
23 f9ab77a8 2023-08-23 op
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>
28 f9ab77a8 2023-08-23 op
29 f9ab77a8 2023-08-23 op #include "tls.h"
30 f9ab77a8 2023-08-23 op #include "tls_internal.h"
31 f9ab77a8 2023-08-23 op
32 f9ab77a8 2023-08-23 op static void *
33 f9ab77a8 2023-08-23 op memdup(const void *in, size_t len)
34 f9ab77a8 2023-08-23 op {
35 f9ab77a8 2023-08-23 op void *out;
36 f9ab77a8 2023-08-23 op
37 f9ab77a8 2023-08-23 op if ((out = malloc(len)) == NULL)
38 f9ab77a8 2023-08-23 op return NULL;
39 f9ab77a8 2023-08-23 op memcpy(out, in, len);
40 f9ab77a8 2023-08-23 op return out;
41 f9ab77a8 2023-08-23 op }
42 f9ab77a8 2023-08-23 op
43 f9ab77a8 2023-08-23 op int
44 f9ab77a8 2023-08-23 op tls_set_mem(char **dest, size_t *destlen, const void *src, size_t srclen)
45 f9ab77a8 2023-08-23 op {
46 f9ab77a8 2023-08-23 op free(*dest);
47 f9ab77a8 2023-08-23 op *dest = NULL;
48 f9ab77a8 2023-08-23 op *destlen = 0;
49 f9ab77a8 2023-08-23 op if (src != NULL) {
50 f9ab77a8 2023-08-23 op if ((*dest = memdup(src, srclen)) == NULL)
51 f9ab77a8 2023-08-23 op return -1;
52 f9ab77a8 2023-08-23 op *destlen = srclen;
53 f9ab77a8 2023-08-23 op }
54 f9ab77a8 2023-08-23 op return 0;
55 f9ab77a8 2023-08-23 op }
56 f9ab77a8 2023-08-23 op
57 f9ab77a8 2023-08-23 op int
58 f9ab77a8 2023-08-23 op tls_set_string(const char **dest, const char *src)
59 f9ab77a8 2023-08-23 op {
60 f9ab77a8 2023-08-23 op free((char *)*dest);
61 f9ab77a8 2023-08-23 op *dest = NULL;
62 f9ab77a8 2023-08-23 op if (src != NULL)
63 f9ab77a8 2023-08-23 op if ((*dest = strdup(src)) == NULL)
64 f9ab77a8 2023-08-23 op return -1;
65 f9ab77a8 2023-08-23 op return 0;
66 f9ab77a8 2023-08-23 op }
67 f9ab77a8 2023-08-23 op
68 f9ab77a8 2023-08-23 op /*
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.
75 f9ab77a8 2023-08-23 op */
76 f9ab77a8 2023-08-23 op int
77 f9ab77a8 2023-08-23 op tls_host_port(const char *hostport, char **host, char **port)
78 f9ab77a8 2023-08-23 op {
79 f9ab77a8 2023-08-23 op char *h, *p, *s;
80 f9ab77a8 2023-08-23 op int rv = 1;
81 f9ab77a8 2023-08-23 op
82 f9ab77a8 2023-08-23 op *host = NULL;
83 f9ab77a8 2023-08-23 op *port = NULL;
84 f9ab77a8 2023-08-23 op
85 f9ab77a8 2023-08-23 op if ((s = strdup(hostport)) == NULL)
86 f9ab77a8 2023-08-23 op goto err;
87 f9ab77a8 2023-08-23 op
88 f9ab77a8 2023-08-23 op h = p = s;
89 f9ab77a8 2023-08-23 op
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] == '[') {
92 f9ab77a8 2023-08-23 op h++;
93 f9ab77a8 2023-08-23 op if ((p = strchr(s, ']')) == NULL)
94 f9ab77a8 2023-08-23 op goto done;
95 f9ab77a8 2023-08-23 op *p++ = '\0';
96 f9ab77a8 2023-08-23 op }
97 f9ab77a8 2023-08-23 op
98 f9ab77a8 2023-08-23 op /* Find the port separator. */
99 f9ab77a8 2023-08-23 op if ((p = strchr(p, ':')) == NULL)
100 f9ab77a8 2023-08-23 op goto done;
101 f9ab77a8 2023-08-23 op
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)
104 f9ab77a8 2023-08-23 op goto done;
105 f9ab77a8 2023-08-23 op
106 f9ab77a8 2023-08-23 op *p++ = '\0';
107 f9ab77a8 2023-08-23 op
108 f9ab77a8 2023-08-23 op if (asprintf(host, "%s", h) == -1) {
109 f9ab77a8 2023-08-23 op *host = NULL;
110 f9ab77a8 2023-08-23 op goto err;
111 f9ab77a8 2023-08-23 op }
112 f9ab77a8 2023-08-23 op if (asprintf(port, "%s", p) == -1) {
113 f9ab77a8 2023-08-23 op *port = NULL;
114 f9ab77a8 2023-08-23 op goto err;
115 f9ab77a8 2023-08-23 op }
116 f9ab77a8 2023-08-23 op
117 f9ab77a8 2023-08-23 op rv = 0;
118 f9ab77a8 2023-08-23 op goto done;
119 f9ab77a8 2023-08-23 op
120 f9ab77a8 2023-08-23 op err:
121 f9ab77a8 2023-08-23 op free(*host);
122 f9ab77a8 2023-08-23 op *host = NULL;
123 f9ab77a8 2023-08-23 op free(*port);
124 f9ab77a8 2023-08-23 op *port = NULL;
125 f9ab77a8 2023-08-23 op rv = -1;
126 f9ab77a8 2023-08-23 op
127 f9ab77a8 2023-08-23 op done:
128 f9ab77a8 2023-08-23 op free(s);
129 f9ab77a8 2023-08-23 op
130 f9ab77a8 2023-08-23 op return (rv);
131 f9ab77a8 2023-08-23 op }
132 f9ab77a8 2023-08-23 op
133 f9ab77a8 2023-08-23 op int
134 f9ab77a8 2023-08-23 op tls_password_cb(char *buf, int size, int rwflag, void *u)
135 f9ab77a8 2023-08-23 op {
136 f9ab77a8 2023-08-23 op size_t len;
137 f9ab77a8 2023-08-23 op
138 f9ab77a8 2023-08-23 op if (size < 0)
139 f9ab77a8 2023-08-23 op return (0);
140 f9ab77a8 2023-08-23 op
141 f9ab77a8 2023-08-23 op if (u == NULL) {
142 f9ab77a8 2023-08-23 op memset(buf, 0, size);
143 f9ab77a8 2023-08-23 op return (0);
144 f9ab77a8 2023-08-23 op }
145 f9ab77a8 2023-08-23 op
146 f9ab77a8 2023-08-23 op if ((len = strlcpy(buf, u, size)) >= (size_t)size)
147 f9ab77a8 2023-08-23 op return (0);
148 f9ab77a8 2023-08-23 op
149 f9ab77a8 2023-08-23 op return (len);
150 f9ab77a8 2023-08-23 op }
151 f9ab77a8 2023-08-23 op
152 f9ab77a8 2023-08-23 op uint8_t *
153 f9ab77a8 2023-08-23 op tls_load_file(const char *name, size_t *len, char *password)
154 f9ab77a8 2023-08-23 op {
155 f9ab77a8 2023-08-23 op FILE *fp;
156 f9ab77a8 2023-08-23 op EVP_PKEY *key = NULL;
157 f9ab77a8 2023-08-23 op BIO *bio = NULL;
158 f9ab77a8 2023-08-23 op char *data;
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;
162 f9ab77a8 2023-08-23 op int fd = -1;
163 f9ab77a8 2023-08-23 op ssize_t n;
164 f9ab77a8 2023-08-23 op
165 f9ab77a8 2023-08-23 op *len = 0;
166 f9ab77a8 2023-08-23 op
167 f9ab77a8 2023-08-23 op if ((fd = open(name, O_RDONLY)) == -1)
168 f9ab77a8 2023-08-23 op return (NULL);
169 f9ab77a8 2023-08-23 op
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)
173 f9ab77a8 2023-08-23 op goto err;
174 f9ab77a8 2023-08-23 op if (st.st_size < 0)
175 f9ab77a8 2023-08-23 op goto err;
176 f9ab77a8 2023-08-23 op size = (size_t)st.st_size;
177 f9ab77a8 2023-08-23 op if ((buf = malloc(size)) == NULL)
178 f9ab77a8 2023-08-23 op goto err;
179 f9ab77a8 2023-08-23 op n = read(fd, buf, size);
180 f9ab77a8 2023-08-23 op if (n < 0 || (size_t)n != size)
181 f9ab77a8 2023-08-23 op goto err;
182 f9ab77a8 2023-08-23 op close(fd);
183 f9ab77a8 2023-08-23 op goto done;
184 f9ab77a8 2023-08-23 op }
185 f9ab77a8 2023-08-23 op
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)
188 f9ab77a8 2023-08-23 op goto err;
189 f9ab77a8 2023-08-23 op fd = -1;
190 f9ab77a8 2023-08-23 op
191 f9ab77a8 2023-08-23 op key = PEM_read_PrivateKey(fp, NULL, tls_password_cb, password);
192 f9ab77a8 2023-08-23 op fclose(fp);
193 f9ab77a8 2023-08-23 op if (key == NULL)
194 f9ab77a8 2023-08-23 op goto err;
195 f9ab77a8 2023-08-23 op
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)
198 f9ab77a8 2023-08-23 op goto err;
199 f9ab77a8 2023-08-23 op if (!PEM_write_bio_PrivateKey(bio, key, NULL, NULL, 0, NULL, NULL))
200 f9ab77a8 2023-08-23 op goto err;
201 f9ab77a8 2023-08-23 op if ((size = BIO_get_mem_data(bio, &data)) <= 0)
202 f9ab77a8 2023-08-23 op goto err;
203 f9ab77a8 2023-08-23 op if ((buf = malloc(size)) == NULL)
204 f9ab77a8 2023-08-23 op goto err;
205 f9ab77a8 2023-08-23 op memcpy(buf, data, size);
206 f9ab77a8 2023-08-23 op
207 f9ab77a8 2023-08-23 op BIO_free_all(bio);
208 f9ab77a8 2023-08-23 op EVP_PKEY_free(key);
209 f9ab77a8 2023-08-23 op
210 f9ab77a8 2023-08-23 op done:
211 f9ab77a8 2023-08-23 op *len = size;
212 f9ab77a8 2023-08-23 op return (buf);
213 f9ab77a8 2023-08-23 op
214 f9ab77a8 2023-08-23 op err:
215 f9ab77a8 2023-08-23 op if (fd != -1)
216 f9ab77a8 2023-08-23 op close(fd);
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);
220 f9ab77a8 2023-08-23 op
221 f9ab77a8 2023-08-23 op return (NULL);
222 f9ab77a8 2023-08-23 op }
223 f9ab77a8 2023-08-23 op
224 f9ab77a8 2023-08-23 op void
225 f9ab77a8 2023-08-23 op tls_unload_file(uint8_t *buf, size_t len)
226 f9ab77a8 2023-08-23 op {
227 f9ab77a8 2023-08-23 op freezero(buf, len);
228 f9ab77a8 2023-08-23 op }