Blame


1 ebfc5784 2024-01-07 op /* $OpenBSD: tls_conninfo.c,v 1.24 2023/11/13 10:51:49 tb Exp $ */
2 f9ab77a8 2023-08-23 op /*
3 f9ab77a8 2023-08-23 op * Copyright (c) 2015 Joel Sing <jsing@openbsd.org>
4 f9ab77a8 2023-08-23 op * Copyright (c) 2015 Bob Beck <beck@openbsd.org>
5 f9ab77a8 2023-08-23 op *
6 f9ab77a8 2023-08-23 op * Permission to use, copy, modify, and distribute this software for any
7 f9ab77a8 2023-08-23 op * purpose with or without fee is hereby granted, provided that the above
8 f9ab77a8 2023-08-23 op * copyright notice and this permission notice appear in all copies.
9 f9ab77a8 2023-08-23 op *
10 f9ab77a8 2023-08-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 f9ab77a8 2023-08-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 f9ab77a8 2023-08-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 f9ab77a8 2023-08-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 f9ab77a8 2023-08-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 f9ab77a8 2023-08-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 f9ab77a8 2023-08-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 f9ab77a8 2023-08-23 op */
18 f9ab77a8 2023-08-23 op
19 f9ab77a8 2023-08-23 op #include "config.h"
20 f9ab77a8 2023-08-23 op
21 f9ab77a8 2023-08-23 op #include <stdio.h>
22 f9ab77a8 2023-08-23 op #include <string.h>
23 f9ab77a8 2023-08-23 op
24 f9ab77a8 2023-08-23 op #include <openssl/x509.h>
25 f9ab77a8 2023-08-23 op
26 f9ab77a8 2023-08-23 op #include <tls.h>
27 f9ab77a8 2023-08-23 op #include "tls_internal.h"
28 f9ab77a8 2023-08-23 op
29 f9ab77a8 2023-08-23 op int ASN1_time_tm_clamp_notafter(struct tm *tm);
30 f9ab77a8 2023-08-23 op
31 f9ab77a8 2023-08-23 op int
32 f9ab77a8 2023-08-23 op tls_hex_string(const unsigned char *in, size_t inlen, char **out,
33 f9ab77a8 2023-08-23 op size_t *outlen)
34 f9ab77a8 2023-08-23 op {
35 f9ab77a8 2023-08-23 op static const char hex[] = "0123456789abcdef";
36 f9ab77a8 2023-08-23 op size_t i, len;
37 f9ab77a8 2023-08-23 op char *p;
38 f9ab77a8 2023-08-23 op
39 f9ab77a8 2023-08-23 op if (outlen != NULL)
40 f9ab77a8 2023-08-23 op *outlen = 0;
41 f9ab77a8 2023-08-23 op
42 f9ab77a8 2023-08-23 op if (inlen >= SIZE_MAX)
43 f9ab77a8 2023-08-23 op return (-1);
44 f9ab77a8 2023-08-23 op if ((*out = reallocarray(NULL, inlen + 1, 2)) == NULL)
45 f9ab77a8 2023-08-23 op return (-1);
46 f9ab77a8 2023-08-23 op
47 f9ab77a8 2023-08-23 op p = *out;
48 f9ab77a8 2023-08-23 op len = 0;
49 f9ab77a8 2023-08-23 op for (i = 0; i < inlen; i++) {
50 f9ab77a8 2023-08-23 op p[len++] = hex[(in[i] >> 4) & 0x0f];
51 f9ab77a8 2023-08-23 op p[len++] = hex[in[i] & 0x0f];
52 f9ab77a8 2023-08-23 op }
53 f9ab77a8 2023-08-23 op p[len++] = 0;
54 f9ab77a8 2023-08-23 op
55 f9ab77a8 2023-08-23 op if (outlen != NULL)
56 f9ab77a8 2023-08-23 op *outlen = len;
57 f9ab77a8 2023-08-23 op
58 f9ab77a8 2023-08-23 op return (0);
59 f9ab77a8 2023-08-23 op }
60 f9ab77a8 2023-08-23 op
61 f9ab77a8 2023-08-23 op static int
62 f9ab77a8 2023-08-23 op tls_get_peer_cert_hash(struct tls *ctx, char **hash)
63 f9ab77a8 2023-08-23 op {
64 f9ab77a8 2023-08-23 op *hash = NULL;
65 f9ab77a8 2023-08-23 op if (ctx->ssl_peer_cert == NULL)
66 f9ab77a8 2023-08-23 op return (0);
67 f9ab77a8 2023-08-23 op
68 f9ab77a8 2023-08-23 op if (tls_cert_hash(ctx->ssl_peer_cert, hash) == -1) {
69 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "unable to compute peer certificate hash - out of memory");
70 f9ab77a8 2023-08-23 op *hash = NULL;
71 f9ab77a8 2023-08-23 op return -1;
72 f9ab77a8 2023-08-23 op }
73 f9ab77a8 2023-08-23 op return 0;
74 f9ab77a8 2023-08-23 op }
75 f9ab77a8 2023-08-23 op
76 f9ab77a8 2023-08-23 op static int
77 f9ab77a8 2023-08-23 op tls_get_peer_cert_issuer(struct tls *ctx, char **issuer)
78 f9ab77a8 2023-08-23 op {
79 f9ab77a8 2023-08-23 op X509_NAME *name = NULL;
80 f9ab77a8 2023-08-23 op
81 f9ab77a8 2023-08-23 op *issuer = NULL;
82 f9ab77a8 2023-08-23 op if (ctx->ssl_peer_cert == NULL)
83 f9ab77a8 2023-08-23 op return (-1);
84 f9ab77a8 2023-08-23 op if ((name = X509_get_issuer_name(ctx->ssl_peer_cert)) == NULL)
85 f9ab77a8 2023-08-23 op return (-1);
86 f9ab77a8 2023-08-23 op *issuer = X509_NAME_oneline(name, 0, 0);
87 f9ab77a8 2023-08-23 op if (*issuer == NULL)
88 f9ab77a8 2023-08-23 op return (-1);
89 f9ab77a8 2023-08-23 op return (0);
90 f9ab77a8 2023-08-23 op }
91 f9ab77a8 2023-08-23 op
92 f9ab77a8 2023-08-23 op static int
93 f9ab77a8 2023-08-23 op tls_get_peer_cert_subject(struct tls *ctx, char **subject)
94 f9ab77a8 2023-08-23 op {
95 f9ab77a8 2023-08-23 op X509_NAME *name = NULL;
96 f9ab77a8 2023-08-23 op
97 f9ab77a8 2023-08-23 op *subject = NULL;
98 f9ab77a8 2023-08-23 op if (ctx->ssl_peer_cert == NULL)
99 f9ab77a8 2023-08-23 op return (-1);
100 f9ab77a8 2023-08-23 op if ((name = X509_get_subject_name(ctx->ssl_peer_cert)) == NULL)
101 f9ab77a8 2023-08-23 op return (-1);
102 f9ab77a8 2023-08-23 op *subject = X509_NAME_oneline(name, 0, 0);
103 f9ab77a8 2023-08-23 op if (*subject == NULL)
104 f9ab77a8 2023-08-23 op return (-1);
105 f9ab77a8 2023-08-23 op return (0);
106 f9ab77a8 2023-08-23 op }
107 f9ab77a8 2023-08-23 op
108 f9ab77a8 2023-08-23 op static int
109 f9ab77a8 2023-08-23 op tls_get_peer_cert_times(struct tls *ctx, time_t *notbefore,
110 f9ab77a8 2023-08-23 op time_t *notafter)
111 f9ab77a8 2023-08-23 op {
112 f9ab77a8 2023-08-23 op struct tm before_tm, after_tm;
113 f9ab77a8 2023-08-23 op ASN1_TIME *before, *after;
114 f9ab77a8 2023-08-23 op
115 f9ab77a8 2023-08-23 op if (ctx->ssl_peer_cert == NULL)
116 f9ab77a8 2023-08-23 op return (-1);
117 f9ab77a8 2023-08-23 op
118 f9ab77a8 2023-08-23 op if ((before = X509_get_notBefore(ctx->ssl_peer_cert)) == NULL)
119 f9ab77a8 2023-08-23 op goto err;
120 f9ab77a8 2023-08-23 op if ((after = X509_get_notAfter(ctx->ssl_peer_cert)) == NULL)
121 f9ab77a8 2023-08-23 op goto err;
122 ebfc5784 2024-01-07 op if (!ASN1_TIME_to_tm(before, &before_tm))
123 f9ab77a8 2023-08-23 op goto err;
124 ebfc5784 2024-01-07 op if (!ASN1_TIME_to_tm(after, &after_tm))
125 f9ab77a8 2023-08-23 op goto err;
126 f9ab77a8 2023-08-23 op if (!ASN1_time_tm_clamp_notafter(&after_tm))
127 f9ab77a8 2023-08-23 op goto err;
128 f9ab77a8 2023-08-23 op if ((*notbefore = timegm(&before_tm)) == -1)
129 f9ab77a8 2023-08-23 op goto err;
130 f9ab77a8 2023-08-23 op if ((*notafter = timegm(&after_tm)) == -1)
131 f9ab77a8 2023-08-23 op goto err;
132 f9ab77a8 2023-08-23 op
133 f9ab77a8 2023-08-23 op return (0);
134 f9ab77a8 2023-08-23 op
135 f9ab77a8 2023-08-23 op err:
136 f9ab77a8 2023-08-23 op return (-1);
137 f9ab77a8 2023-08-23 op }
138 f9ab77a8 2023-08-23 op
139 f9ab77a8 2023-08-23 op static int
140 f9ab77a8 2023-08-23 op tls_get_peer_cert_info(struct tls *ctx)
141 f9ab77a8 2023-08-23 op {
142 f9ab77a8 2023-08-23 op if (ctx->ssl_peer_cert == NULL)
143 f9ab77a8 2023-08-23 op return (0);
144 f9ab77a8 2023-08-23 op
145 f9ab77a8 2023-08-23 op if (tls_get_peer_cert_hash(ctx, &ctx->conninfo->hash) == -1)
146 f9ab77a8 2023-08-23 op goto err;
147 f9ab77a8 2023-08-23 op if (tls_get_peer_cert_subject(ctx, &ctx->conninfo->subject) == -1)
148 f9ab77a8 2023-08-23 op goto err;
149 f9ab77a8 2023-08-23 op if (tls_get_peer_cert_issuer(ctx, &ctx->conninfo->issuer) == -1)
150 f9ab77a8 2023-08-23 op goto err;
151 f9ab77a8 2023-08-23 op if (tls_get_peer_cert_times(ctx, &ctx->conninfo->notbefore,
152 f9ab77a8 2023-08-23 op &ctx->conninfo->notafter) == -1)
153 f9ab77a8 2023-08-23 op goto err;
154 f9ab77a8 2023-08-23 op
155 f9ab77a8 2023-08-23 op return (0);
156 f9ab77a8 2023-08-23 op
157 f9ab77a8 2023-08-23 op err:
158 f9ab77a8 2023-08-23 op return (-1);
159 f9ab77a8 2023-08-23 op }
160 f9ab77a8 2023-08-23 op
161 f9ab77a8 2023-08-23 op static int
162 f9ab77a8 2023-08-23 op tls_conninfo_alpn_proto(struct tls *ctx)
163 f9ab77a8 2023-08-23 op {
164 f9ab77a8 2023-08-23 op const unsigned char *p;
165 f9ab77a8 2023-08-23 op unsigned int len;
166 f9ab77a8 2023-08-23 op
167 f9ab77a8 2023-08-23 op free(ctx->conninfo->alpn);
168 f9ab77a8 2023-08-23 op ctx->conninfo->alpn = NULL;
169 f9ab77a8 2023-08-23 op
170 f9ab77a8 2023-08-23 op SSL_get0_alpn_selected(ctx->ssl_conn, &p, &len);
171 f9ab77a8 2023-08-23 op if (len > 0) {
172 f9ab77a8 2023-08-23 op if ((ctx->conninfo->alpn = malloc(len + 1)) == NULL)
173 f9ab77a8 2023-08-23 op return (-1);
174 f9ab77a8 2023-08-23 op memcpy(ctx->conninfo->alpn, p, len);
175 f9ab77a8 2023-08-23 op ctx->conninfo->alpn[len] = '\0';
176 f9ab77a8 2023-08-23 op }
177 f9ab77a8 2023-08-23 op
178 f9ab77a8 2023-08-23 op return (0);
179 f9ab77a8 2023-08-23 op }
180 f9ab77a8 2023-08-23 op
181 f9ab77a8 2023-08-23 op static int
182 f9ab77a8 2023-08-23 op tls_conninfo_cert_pem(struct tls *ctx)
183 f9ab77a8 2023-08-23 op {
184 f9ab77a8 2023-08-23 op int i, rv = -1;
185 f9ab77a8 2023-08-23 op BIO *membio = NULL;
186 f9ab77a8 2023-08-23 op BUF_MEM *bptr = NULL;
187 f9ab77a8 2023-08-23 op
188 f9ab77a8 2023-08-23 op if (ctx->ssl_peer_cert == NULL)
189 f9ab77a8 2023-08-23 op return 0;
190 f9ab77a8 2023-08-23 op if ((membio = BIO_new(BIO_s_mem()))== NULL)
191 f9ab77a8 2023-08-23 op goto err;
192 f9ab77a8 2023-08-23 op
193 f9ab77a8 2023-08-23 op /*
194 f9ab77a8 2023-08-23 op * We have to write the peer cert out separately, because
195 f9ab77a8 2023-08-23 op * the certificate chain may or may not contain it.
196 f9ab77a8 2023-08-23 op */
197 f9ab77a8 2023-08-23 op if (!PEM_write_bio_X509(membio, ctx->ssl_peer_cert))
198 f9ab77a8 2023-08-23 op goto err;
199 f9ab77a8 2023-08-23 op for (i = 0; i < sk_X509_num(ctx->ssl_peer_chain); i++) {
200 f9ab77a8 2023-08-23 op X509 *chaincert = sk_X509_value(ctx->ssl_peer_chain, i);
201 f9ab77a8 2023-08-23 op if (chaincert != ctx->ssl_peer_cert &&
202 f9ab77a8 2023-08-23 op !PEM_write_bio_X509(membio, chaincert))
203 f9ab77a8 2023-08-23 op goto err;
204 f9ab77a8 2023-08-23 op }
205 f9ab77a8 2023-08-23 op
206 f9ab77a8 2023-08-23 op BIO_get_mem_ptr(membio, &bptr);
207 f9ab77a8 2023-08-23 op free(ctx->conninfo->peer_cert);
208 f9ab77a8 2023-08-23 op ctx->conninfo->peer_cert_len = 0;
209 f9ab77a8 2023-08-23 op if ((ctx->conninfo->peer_cert = malloc(bptr->length)) == NULL)
210 f9ab77a8 2023-08-23 op goto err;
211 f9ab77a8 2023-08-23 op ctx->conninfo->peer_cert_len = bptr->length;
212 f9ab77a8 2023-08-23 op memcpy(ctx->conninfo->peer_cert, bptr->data,
213 f9ab77a8 2023-08-23 op ctx->conninfo->peer_cert_len);
214 f9ab77a8 2023-08-23 op
215 f9ab77a8 2023-08-23 op /* BIO_free() will kill BUF_MEM - because we have not set BIO_NOCLOSE */
216 f9ab77a8 2023-08-23 op rv = 0;
217 f9ab77a8 2023-08-23 op err:
218 f9ab77a8 2023-08-23 op BIO_free(membio);
219 f9ab77a8 2023-08-23 op return rv;
220 f9ab77a8 2023-08-23 op }
221 f9ab77a8 2023-08-23 op
222 f9ab77a8 2023-08-23 op static int
223 f9ab77a8 2023-08-23 op tls_conninfo_session(struct tls *ctx)
224 f9ab77a8 2023-08-23 op {
225 f9ab77a8 2023-08-23 op ctx->conninfo->session_resumed = SSL_session_reused(ctx->ssl_conn);
226 f9ab77a8 2023-08-23 op
227 f9ab77a8 2023-08-23 op return 0;
228 f9ab77a8 2023-08-23 op }
229 f9ab77a8 2023-08-23 op
230 f9ab77a8 2023-08-23 op int
231 f9ab77a8 2023-08-23 op tls_conninfo_populate(struct tls *ctx)
232 f9ab77a8 2023-08-23 op {
233 f9ab77a8 2023-08-23 op const char *tmp;
234 f9ab77a8 2023-08-23 op
235 f9ab77a8 2023-08-23 op tls_conninfo_free(ctx->conninfo);
236 f9ab77a8 2023-08-23 op
237 f9ab77a8 2023-08-23 op if ((ctx->conninfo = calloc(1, sizeof(struct tls_conninfo))) == NULL) {
238 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "out of memory");
239 f9ab77a8 2023-08-23 op goto err;
240 f9ab77a8 2023-08-23 op }
241 f9ab77a8 2023-08-23 op
242 f9ab77a8 2023-08-23 op if (tls_conninfo_alpn_proto(ctx) == -1)
243 f9ab77a8 2023-08-23 op goto err;
244 f9ab77a8 2023-08-23 op
245 f9ab77a8 2023-08-23 op if ((tmp = SSL_get_cipher(ctx->ssl_conn)) == NULL)
246 f9ab77a8 2023-08-23 op goto err;
247 f9ab77a8 2023-08-23 op if ((ctx->conninfo->cipher = strdup(tmp)) == NULL)
248 f9ab77a8 2023-08-23 op goto err;
249 f9ab77a8 2023-08-23 op ctx->conninfo->cipher_strength = SSL_get_cipher_bits(ctx->ssl_conn, NULL);
250 f9ab77a8 2023-08-23 op
251 f9ab77a8 2023-08-23 op if (ctx->servername != NULL) {
252 f9ab77a8 2023-08-23 op if ((ctx->conninfo->servername =
253 f9ab77a8 2023-08-23 op strdup(ctx->servername)) == NULL)
254 f9ab77a8 2023-08-23 op goto err;
255 f9ab77a8 2023-08-23 op }
256 f9ab77a8 2023-08-23 op
257 f9ab77a8 2023-08-23 op if ((tmp = SSL_get_version(ctx->ssl_conn)) == NULL)
258 f9ab77a8 2023-08-23 op goto err;
259 f9ab77a8 2023-08-23 op if ((ctx->conninfo->version = strdup(tmp)) == NULL)
260 f9ab77a8 2023-08-23 op goto err;
261 f9ab77a8 2023-08-23 op
262 f9ab77a8 2023-08-23 op if (tls_get_peer_cert_info(ctx) == -1)
263 f9ab77a8 2023-08-23 op goto err;
264 f9ab77a8 2023-08-23 op
265 f9ab77a8 2023-08-23 op if (tls_conninfo_cert_pem(ctx) == -1)
266 f9ab77a8 2023-08-23 op goto err;
267 f9ab77a8 2023-08-23 op
268 f9ab77a8 2023-08-23 op if (tls_conninfo_session(ctx) == -1)
269 f9ab77a8 2023-08-23 op goto err;
270 f9ab77a8 2023-08-23 op
271 f9ab77a8 2023-08-23 op return (0);
272 f9ab77a8 2023-08-23 op
273 f9ab77a8 2023-08-23 op err:
274 f9ab77a8 2023-08-23 op tls_conninfo_free(ctx->conninfo);
275 f9ab77a8 2023-08-23 op ctx->conninfo = NULL;
276 f9ab77a8 2023-08-23 op
277 f9ab77a8 2023-08-23 op return (-1);
278 f9ab77a8 2023-08-23 op }
279 f9ab77a8 2023-08-23 op
280 f9ab77a8 2023-08-23 op void
281 f9ab77a8 2023-08-23 op tls_conninfo_free(struct tls_conninfo *conninfo)
282 f9ab77a8 2023-08-23 op {
283 f9ab77a8 2023-08-23 op if (conninfo == NULL)
284 f9ab77a8 2023-08-23 op return;
285 f9ab77a8 2023-08-23 op
286 f9ab77a8 2023-08-23 op free(conninfo->alpn);
287 f9ab77a8 2023-08-23 op free(conninfo->cipher);
288 f9ab77a8 2023-08-23 op free(conninfo->servername);
289 f9ab77a8 2023-08-23 op free(conninfo->version);
290 f9ab77a8 2023-08-23 op
291 f9ab77a8 2023-08-23 op free(conninfo->hash);
292 f9ab77a8 2023-08-23 op free(conninfo->issuer);
293 f9ab77a8 2023-08-23 op free(conninfo->subject);
294 f9ab77a8 2023-08-23 op
295 f9ab77a8 2023-08-23 op free(conninfo->peer_cert);
296 f9ab77a8 2023-08-23 op
297 f9ab77a8 2023-08-23 op free(conninfo);
298 f9ab77a8 2023-08-23 op }
299 f9ab77a8 2023-08-23 op
300 f9ab77a8 2023-08-23 op const char *
301 f9ab77a8 2023-08-23 op tls_conn_alpn_selected(struct tls *ctx)
302 f9ab77a8 2023-08-23 op {
303 f9ab77a8 2023-08-23 op if (ctx->conninfo == NULL)
304 f9ab77a8 2023-08-23 op return (NULL);
305 f9ab77a8 2023-08-23 op return (ctx->conninfo->alpn);
306 f9ab77a8 2023-08-23 op }
307 f9ab77a8 2023-08-23 op
308 f9ab77a8 2023-08-23 op const char *
309 f9ab77a8 2023-08-23 op tls_conn_cipher(struct tls *ctx)
310 f9ab77a8 2023-08-23 op {
311 f9ab77a8 2023-08-23 op if (ctx->conninfo == NULL)
312 f9ab77a8 2023-08-23 op return (NULL);
313 f9ab77a8 2023-08-23 op return (ctx->conninfo->cipher);
314 f9ab77a8 2023-08-23 op }
315 f9ab77a8 2023-08-23 op
316 f9ab77a8 2023-08-23 op int
317 f9ab77a8 2023-08-23 op tls_conn_cipher_strength(struct tls *ctx)
318 f9ab77a8 2023-08-23 op {
319 f9ab77a8 2023-08-23 op if (ctx->conninfo == NULL)
320 f9ab77a8 2023-08-23 op return (0);
321 f9ab77a8 2023-08-23 op return (ctx->conninfo->cipher_strength);
322 f9ab77a8 2023-08-23 op }
323 f9ab77a8 2023-08-23 op
324 f9ab77a8 2023-08-23 op const char *
325 f9ab77a8 2023-08-23 op tls_conn_servername(struct tls *ctx)
326 f9ab77a8 2023-08-23 op {
327 f9ab77a8 2023-08-23 op if (ctx->conninfo == NULL)
328 f9ab77a8 2023-08-23 op return (NULL);
329 f9ab77a8 2023-08-23 op return (ctx->conninfo->servername);
330 f9ab77a8 2023-08-23 op }
331 f9ab77a8 2023-08-23 op
332 f9ab77a8 2023-08-23 op int
333 f9ab77a8 2023-08-23 op tls_conn_session_resumed(struct tls *ctx)
334 f9ab77a8 2023-08-23 op {
335 f9ab77a8 2023-08-23 op if (ctx->conninfo == NULL)
336 f9ab77a8 2023-08-23 op return (0);
337 f9ab77a8 2023-08-23 op return (ctx->conninfo->session_resumed);
338 f9ab77a8 2023-08-23 op }
339 f9ab77a8 2023-08-23 op
340 f9ab77a8 2023-08-23 op const char *
341 f9ab77a8 2023-08-23 op tls_conn_version(struct tls *ctx)
342 f9ab77a8 2023-08-23 op {
343 f9ab77a8 2023-08-23 op if (ctx->conninfo == NULL)
344 f9ab77a8 2023-08-23 op return (NULL);
345 f9ab77a8 2023-08-23 op return (ctx->conninfo->version);
346 f9ab77a8 2023-08-23 op }