xref: /openbsd-src/lib/libtls/tls_ocsp.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 /*	$OpenBSD: tls_ocsp.c,v 1.23 2023/05/14 07:26:25 op Exp $ */
2 /*
3  * Copyright (c) 2015 Marko Kreen <markokr@gmail.com>
4  * Copyright (c) 2016 Bob Beck <beck@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
23 
24 #include <string.h>
25 
26 #include <openssl/err.h>
27 #include <openssl/ocsp.h>
28 #include <openssl/x509.h>
29 
30 #include <tls.h>
31 #include "tls_internal.h"
32 
33 #define MAXAGE_SEC (14*24*60*60)
34 #define JITTER_SEC (60)
35 
36 /*
37  * State for request.
38  */
39 
40 static struct tls_ocsp *
41 tls_ocsp_new(void)
42 {
43 	return (calloc(1, sizeof(struct tls_ocsp)));
44 }
45 
46 void
47 tls_ocsp_free(struct tls_ocsp *ocsp)
48 {
49 	if (ocsp == NULL)
50 		return;
51 
52 	X509_free(ocsp->main_cert);
53 	free(ocsp->ocsp_result);
54 	free(ocsp->ocsp_url);
55 
56 	free(ocsp);
57 }
58 
59 static int
60 tls_ocsp_asn1_parse_time(struct tls *ctx, ASN1_GENERALIZEDTIME *gt, time_t *gt_time)
61 {
62 	struct tm tm;
63 
64 	if (gt == NULL)
65 		return -1;
66 	/* RFC 6960 specifies that all times in OCSP must be GENERALIZEDTIME */
67 	if (ASN1_time_parse(gt->data, gt->length, &tm,
68 		V_ASN1_GENERALIZEDTIME) == -1)
69 		return -1;
70 	if ((*gt_time = timegm(&tm)) == -1)
71 		return -1;
72 	return 0;
73 }
74 
75 static int
76 tls_ocsp_fill_info(struct tls *ctx, int response_status, int cert_status,
77     int crl_reason, ASN1_GENERALIZEDTIME *revtime,
78     ASN1_GENERALIZEDTIME *thisupd, ASN1_GENERALIZEDTIME *nextupd)
79 {
80 	struct tls_ocsp_result *info = NULL;
81 
82 	free(ctx->ocsp->ocsp_result);
83 	ctx->ocsp->ocsp_result = NULL;
84 
85 	if ((info = calloc(1, sizeof (struct tls_ocsp_result))) == NULL) {
86 		tls_set_error(ctx, "calloc");
87 		return -1;
88 	}
89 	info->response_status = response_status;
90 	info->cert_status = cert_status;
91 	info->crl_reason = crl_reason;
92 	if (info->response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
93 		info->result_msg =
94 		    OCSP_response_status_str(info->response_status);
95 	} else if (info->cert_status != V_OCSP_CERTSTATUS_REVOKED) {
96 		info->result_msg = OCSP_cert_status_str(info->cert_status);
97 	} else {
98 		info->result_msg = OCSP_crl_reason_str(info->crl_reason);
99 	}
100 	info->revocation_time = info->this_update = info->next_update = -1;
101 	if (revtime != NULL &&
102 	    tls_ocsp_asn1_parse_time(ctx, revtime, &info->revocation_time) != 0) {
103 		tls_set_error(ctx,
104 		    "unable to parse revocation time in OCSP reply");
105 		goto err;
106 	}
107 	if (thisupd != NULL &&
108 	    tls_ocsp_asn1_parse_time(ctx, thisupd, &info->this_update) != 0) {
109 		tls_set_error(ctx,
110 		    "unable to parse this update time in OCSP reply");
111 		goto err;
112 	}
113 	if (nextupd != NULL &&
114 	    tls_ocsp_asn1_parse_time(ctx, nextupd, &info->next_update) != 0) {
115 		tls_set_error(ctx,
116 		    "unable to parse next update time in OCSP reply");
117 		goto err;
118 	}
119 	ctx->ocsp->ocsp_result = info;
120 	return 0;
121 
122  err:
123 	free(info);
124 	return -1;
125 }
126 
127 static OCSP_CERTID *
128 tls_ocsp_get_certid(X509 *main_cert, STACK_OF(X509) *extra_certs,
129     SSL_CTX *ssl_ctx)
130 {
131 	X509_NAME *issuer_name;
132 	X509 *issuer;
133 	X509_STORE_CTX *storectx = NULL;
134 	X509_OBJECT *obj = NULL;
135 	OCSP_CERTID *cid = NULL;
136 	X509_STORE *store;
137 
138 	if ((issuer_name = X509_get_issuer_name(main_cert)) == NULL)
139 		goto out;
140 
141 	if (extra_certs != NULL) {
142 		issuer = X509_find_by_subject(extra_certs, issuer_name);
143 		if (issuer != NULL) {
144 			cid = OCSP_cert_to_id(NULL, main_cert, issuer);
145 			goto out;
146 		}
147 	}
148 
149 	if ((store = SSL_CTX_get_cert_store(ssl_ctx)) == NULL)
150 		goto out;
151 	if ((storectx = X509_STORE_CTX_new()) == NULL)
152 		goto out;
153 	if (X509_STORE_CTX_init(storectx, store, main_cert, extra_certs) != 1)
154 		goto out;
155 	if ((obj = X509_STORE_CTX_get_obj_by_subject(storectx, X509_LU_X509,
156 	    issuer_name)) == NULL)
157 		goto out;
158 
159 	cid = OCSP_cert_to_id(NULL, main_cert, X509_OBJECT_get0_X509(obj));
160 
161  out:
162 	X509_STORE_CTX_free(storectx);
163 	X509_OBJECT_free(obj);
164 
165 	return cid;
166 }
167 
168 struct tls_ocsp *
169 tls_ocsp_setup_from_peer(struct tls *ctx)
170 {
171 	struct tls_ocsp *ocsp = NULL;
172 	STACK_OF(OPENSSL_STRING) *ocsp_urls = NULL;
173 
174 	if ((ocsp = tls_ocsp_new()) == NULL)
175 		goto err;
176 
177 	/* steal state from ctx struct */
178 	ocsp->main_cert = SSL_get_peer_certificate(ctx->ssl_conn);
179 	ocsp->extra_certs = SSL_get_peer_cert_chain(ctx->ssl_conn);
180 	if (ocsp->main_cert == NULL) {
181 		tls_set_errorx(ctx, "no peer certificate for OCSP");
182 		goto err;
183 	}
184 
185 	ocsp_urls = X509_get1_ocsp(ocsp->main_cert);
186 	if (ocsp_urls == NULL) {
187 		tls_set_errorx(ctx, "no OCSP URLs in peer certificate");
188 		goto err;
189 	}
190 
191 	ocsp->ocsp_url = strdup(sk_OPENSSL_STRING_value(ocsp_urls, 0));
192 	if (ocsp->ocsp_url == NULL) {
193 		tls_set_errorx(ctx, "out of memory");
194 		goto err;
195 	}
196 
197 	X509_email_free(ocsp_urls);
198 	return ocsp;
199 
200  err:
201 	tls_ocsp_free(ocsp);
202 	X509_email_free(ocsp_urls);
203 	return NULL;
204 }
205 
206 static int
207 tls_ocsp_verify_response(struct tls *ctx, OCSP_RESPONSE *resp)
208 {
209 	OCSP_BASICRESP *br = NULL;
210 	ASN1_GENERALIZEDTIME *revtime = NULL, *thisupd = NULL, *nextupd = NULL;
211 	OCSP_CERTID *cid = NULL;
212 	STACK_OF(X509) *combined = NULL;
213 	int response_status=0, cert_status=0, crl_reason=0;
214 	int ret = -1;
215 	unsigned long flags;
216 
217 	if ((br = OCSP_response_get1_basic(resp)) == NULL) {
218 		tls_set_errorx(ctx, "cannot load ocsp reply");
219 		goto err;
220 	}
221 
222 	/*
223 	 * Skip validation of 'extra_certs' as this should be done
224 	 * already as part of main handshake.
225 	 */
226 	flags = OCSP_TRUSTOTHER;
227 
228 	/* now verify */
229 	if (OCSP_basic_verify(br, ctx->ocsp->extra_certs,
230 		SSL_CTX_get_cert_store(ctx->ssl_ctx), flags) != 1) {
231 		tls_set_errorx(ctx, "ocsp verify failed");
232 		goto err;
233 	}
234 
235 	/* signature OK, look inside */
236 	response_status = OCSP_response_status(resp);
237 	if (response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
238 		tls_set_errorx(ctx, "ocsp verify failed: response - %s",
239 		    OCSP_response_status_str(response_status));
240 		goto err;
241 	}
242 
243 	cid = tls_ocsp_get_certid(ctx->ocsp->main_cert,
244 	    ctx->ocsp->extra_certs, ctx->ssl_ctx);
245 	if (cid == NULL) {
246 		tls_set_errorx(ctx, "ocsp verify failed: no issuer cert");
247 		goto err;
248 	}
249 
250 	if (OCSP_resp_find_status(br, cid, &cert_status, &crl_reason,
251 	    &revtime, &thisupd, &nextupd) != 1) {
252 		tls_set_errorx(ctx, "ocsp verify failed: no result for cert");
253 		goto err;
254 	}
255 
256 	if (OCSP_check_validity(thisupd, nextupd, JITTER_SEC,
257 	    MAXAGE_SEC) != 1) {
258 		tls_set_errorx(ctx,
259 		    "ocsp verify failed: ocsp response not current");
260 		goto err;
261 	}
262 
263 	if (tls_ocsp_fill_info(ctx, response_status, cert_status,
264 	    crl_reason, revtime, thisupd, nextupd) != 0)
265 		goto err;
266 
267 	/* finally can look at status */
268 	if (cert_status != V_OCSP_CERTSTATUS_GOOD && cert_status !=
269 	    V_OCSP_CERTSTATUS_UNKNOWN) {
270 		tls_set_errorx(ctx, "ocsp verify failed: revoked cert - %s",
271 			       OCSP_crl_reason_str(crl_reason));
272 		goto err;
273 	}
274 	ret = 0;
275 
276  err:
277 	sk_X509_free(combined);
278 	OCSP_CERTID_free(cid);
279 	OCSP_BASICRESP_free(br);
280 	return ret;
281 }
282 
283 /*
284  * Process a raw OCSP response from an OCSP server request.
285  * OCSP details can then be retrieved with tls_peer_ocsp_* functions.
286  * returns 0 if certificate ok, -1 otherwise.
287  */
288 static int
289 tls_ocsp_process_response_internal(struct tls *ctx, const unsigned char *response,
290     size_t size)
291 {
292 	int ret;
293 	OCSP_RESPONSE *resp;
294 
295 	resp = d2i_OCSP_RESPONSE(NULL, &response, size);
296 	if (resp == NULL) {
297 		tls_ocsp_free(ctx->ocsp);
298 		ctx->ocsp = NULL;
299 		tls_set_error(ctx, "unable to parse OCSP response");
300 		return -1;
301 	}
302 	ret = tls_ocsp_verify_response(ctx, resp);
303 	OCSP_RESPONSE_free(resp);
304 	return ret;
305 }
306 
307 /* TLS handshake verification callback for stapled requests */
308 int
309 tls_ocsp_verify_cb(SSL *ssl, void *arg)
310 {
311 	const unsigned char *raw = NULL;
312 	int size, res = -1;
313 	struct tls *ctx;
314 
315 	if ((ctx = SSL_get_app_data(ssl)) == NULL)
316 		return -1;
317 
318 	size = SSL_get_tlsext_status_ocsp_resp(ssl, &raw);
319 	if (size <= 0) {
320 		if (ctx->config->ocsp_require_stapling) {
321 			tls_set_errorx(ctx, "no stapled OCSP response provided");
322 			return 0;
323 		}
324 		return 1;
325 	}
326 
327 	tls_ocsp_free(ctx->ocsp);
328 	if ((ctx->ocsp = tls_ocsp_setup_from_peer(ctx)) == NULL)
329 		return 0;
330 
331 	if (ctx->config->verify_cert == 0 || ctx->config->verify_time == 0)
332 		return 1;
333 
334 	res = tls_ocsp_process_response_internal(ctx, raw, size);
335 
336 	return (res == 0) ? 1 : 0;
337 }
338 
339 
340 /* Staple the OCSP information in ctx->ocsp to the server handshake. */
341 int
342 tls_ocsp_stapling_cb(SSL *ssl, void *arg)
343 {
344 	int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
345 	unsigned char *ocsp_staple = NULL;
346 	struct tls *ctx;
347 
348 	if ((ctx = SSL_get_app_data(ssl)) == NULL)
349 		goto err;
350 
351 	if (ctx->keypair == NULL || ctx->keypair->ocsp_staple == NULL ||
352 	    ctx->keypair->ocsp_staple_len == 0)
353 		return SSL_TLSEXT_ERR_NOACK;
354 
355 	if ((ocsp_staple = malloc(ctx->keypair->ocsp_staple_len)) == NULL)
356 		goto err;
357 
358 	memcpy(ocsp_staple, ctx->keypair->ocsp_staple,
359 	    ctx->keypair->ocsp_staple_len);
360 
361 	if (SSL_set_tlsext_status_ocsp_resp(ctx->ssl_conn, ocsp_staple,
362 	    ctx->keypair->ocsp_staple_len) != 1)
363 		goto err;
364 
365 	ret = SSL_TLSEXT_ERR_OK;
366  err:
367 	if (ret != SSL_TLSEXT_ERR_OK)
368 		free(ocsp_staple);
369 
370 	return ret;
371 }
372 
373 /*
374  * Public API
375  */
376 
377 /* Retrieve OCSP URL from peer certificate, if present. */
378 const char *
379 tls_peer_ocsp_url(struct tls *ctx)
380 {
381 	if (ctx->ocsp == NULL)
382 		return NULL;
383 	return ctx->ocsp->ocsp_url;
384 }
385 
386 const char *
387 tls_peer_ocsp_result(struct tls *ctx)
388 {
389 	if (ctx->ocsp == NULL)
390 		return NULL;
391 	if (ctx->ocsp->ocsp_result == NULL)
392 		return NULL;
393 	return ctx->ocsp->ocsp_result->result_msg;
394 }
395 
396 int
397 tls_peer_ocsp_response_status(struct tls *ctx)
398 {
399 	if (ctx->ocsp == NULL)
400 		return -1;
401 	if (ctx->ocsp->ocsp_result == NULL)
402 		return -1;
403 	return ctx->ocsp->ocsp_result->response_status;
404 }
405 
406 int
407 tls_peer_ocsp_cert_status(struct tls *ctx)
408 {
409 	if (ctx->ocsp == NULL)
410 		return -1;
411 	if (ctx->ocsp->ocsp_result == NULL)
412 		return -1;
413 	return ctx->ocsp->ocsp_result->cert_status;
414 }
415 
416 int
417 tls_peer_ocsp_crl_reason(struct tls *ctx)
418 {
419 	if (ctx->ocsp == NULL)
420 		return -1;
421 	if (ctx->ocsp->ocsp_result == NULL)
422 		return -1;
423 	return ctx->ocsp->ocsp_result->crl_reason;
424 }
425 
426 time_t
427 tls_peer_ocsp_this_update(struct tls *ctx)
428 {
429 	if (ctx->ocsp == NULL)
430 		return -1;
431 	if (ctx->ocsp->ocsp_result == NULL)
432 		return -1;
433 	return ctx->ocsp->ocsp_result->this_update;
434 }
435 
436 time_t
437 tls_peer_ocsp_next_update(struct tls *ctx)
438 {
439 	if (ctx->ocsp == NULL)
440 		return -1;
441 	if (ctx->ocsp->ocsp_result == NULL)
442 		return -1;
443 	return ctx->ocsp->ocsp_result->next_update;
444 }
445 
446 time_t
447 tls_peer_ocsp_revocation_time(struct tls *ctx)
448 {
449 	if (ctx->ocsp == NULL)
450 		return -1;
451 	if (ctx->ocsp->ocsp_result == NULL)
452 		return -1;
453 	return ctx->ocsp->ocsp_result->revocation_time;
454 }
455 
456 int
457 tls_ocsp_process_response(struct tls *ctx, const unsigned char *response,
458     size_t size)
459 {
460 	if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0)
461 		return -1;
462 	return tls_ocsp_process_response_internal(ctx, response, size);
463 }
464