xref: /openbsd/lib/libtls/tls_ocsp.c (revision 510d2225)
1 /*	$OpenBSD: tls_ocsp.c,v 1.24 2023/11/13 10:56:19 tb 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_GENERALIZEDTIME_check(gt))
68 		return -1;
69 	if (!ASN1_TIME_to_tm(gt, &tm))
70 		return -1;
71 	if ((*gt_time = timegm(&tm)) == -1)
72 		return -1;
73 	return 0;
74 }
75 
76 static int
77 tls_ocsp_fill_info(struct tls *ctx, int response_status, int cert_status,
78     int crl_reason, ASN1_GENERALIZEDTIME *revtime,
79     ASN1_GENERALIZEDTIME *thisupd, ASN1_GENERALIZEDTIME *nextupd)
80 {
81 	struct tls_ocsp_result *info = NULL;
82 
83 	free(ctx->ocsp->ocsp_result);
84 	ctx->ocsp->ocsp_result = NULL;
85 
86 	if ((info = calloc(1, sizeof (struct tls_ocsp_result))) == NULL) {
87 		tls_set_error(ctx, "calloc");
88 		return -1;
89 	}
90 	info->response_status = response_status;
91 	info->cert_status = cert_status;
92 	info->crl_reason = crl_reason;
93 	if (info->response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
94 		info->result_msg =
95 		    OCSP_response_status_str(info->response_status);
96 	} else if (info->cert_status != V_OCSP_CERTSTATUS_REVOKED) {
97 		info->result_msg = OCSP_cert_status_str(info->cert_status);
98 	} else {
99 		info->result_msg = OCSP_crl_reason_str(info->crl_reason);
100 	}
101 	info->revocation_time = info->this_update = info->next_update = -1;
102 	if (revtime != NULL &&
103 	    tls_ocsp_asn1_parse_time(ctx, revtime, &info->revocation_time) != 0) {
104 		tls_set_error(ctx,
105 		    "unable to parse revocation time in OCSP reply");
106 		goto err;
107 	}
108 	if (thisupd != NULL &&
109 	    tls_ocsp_asn1_parse_time(ctx, thisupd, &info->this_update) != 0) {
110 		tls_set_error(ctx,
111 		    "unable to parse this update time in OCSP reply");
112 		goto err;
113 	}
114 	if (nextupd != NULL &&
115 	    tls_ocsp_asn1_parse_time(ctx, nextupd, &info->next_update) != 0) {
116 		tls_set_error(ctx,
117 		    "unable to parse next update time in OCSP reply");
118 		goto err;
119 	}
120 	ctx->ocsp->ocsp_result = info;
121 	return 0;
122 
123  err:
124 	free(info);
125 	return -1;
126 }
127 
128 static OCSP_CERTID *
129 tls_ocsp_get_certid(X509 *main_cert, STACK_OF(X509) *extra_certs,
130     SSL_CTX *ssl_ctx)
131 {
132 	X509_NAME *issuer_name;
133 	X509 *issuer;
134 	X509_STORE_CTX *storectx = NULL;
135 	X509_OBJECT *obj = NULL;
136 	OCSP_CERTID *cid = NULL;
137 	X509_STORE *store;
138 
139 	if ((issuer_name = X509_get_issuer_name(main_cert)) == NULL)
140 		goto out;
141 
142 	if (extra_certs != NULL) {
143 		issuer = X509_find_by_subject(extra_certs, issuer_name);
144 		if (issuer != NULL) {
145 			cid = OCSP_cert_to_id(NULL, main_cert, issuer);
146 			goto out;
147 		}
148 	}
149 
150 	if ((store = SSL_CTX_get_cert_store(ssl_ctx)) == NULL)
151 		goto out;
152 	if ((storectx = X509_STORE_CTX_new()) == NULL)
153 		goto out;
154 	if (X509_STORE_CTX_init(storectx, store, main_cert, extra_certs) != 1)
155 		goto out;
156 	if ((obj = X509_STORE_CTX_get_obj_by_subject(storectx, X509_LU_X509,
157 	    issuer_name)) == NULL)
158 		goto out;
159 
160 	cid = OCSP_cert_to_id(NULL, main_cert, X509_OBJECT_get0_X509(obj));
161 
162  out:
163 	X509_STORE_CTX_free(storectx);
164 	X509_OBJECT_free(obj);
165 
166 	return cid;
167 }
168 
169 struct tls_ocsp *
170 tls_ocsp_setup_from_peer(struct tls *ctx)
171 {
172 	struct tls_ocsp *ocsp = NULL;
173 	STACK_OF(OPENSSL_STRING) *ocsp_urls = NULL;
174 
175 	if ((ocsp = tls_ocsp_new()) == NULL)
176 		goto err;
177 
178 	/* steal state from ctx struct */
179 	ocsp->main_cert = SSL_get_peer_certificate(ctx->ssl_conn);
180 	ocsp->extra_certs = SSL_get_peer_cert_chain(ctx->ssl_conn);
181 	if (ocsp->main_cert == NULL) {
182 		tls_set_errorx(ctx, "no peer certificate for OCSP");
183 		goto err;
184 	}
185 
186 	ocsp_urls = X509_get1_ocsp(ocsp->main_cert);
187 	if (ocsp_urls == NULL) {
188 		tls_set_errorx(ctx, "no OCSP URLs in peer certificate");
189 		goto err;
190 	}
191 
192 	ocsp->ocsp_url = strdup(sk_OPENSSL_STRING_value(ocsp_urls, 0));
193 	if (ocsp->ocsp_url == NULL) {
194 		tls_set_errorx(ctx, "out of memory");
195 		goto err;
196 	}
197 
198 	X509_email_free(ocsp_urls);
199 	return ocsp;
200 
201  err:
202 	tls_ocsp_free(ocsp);
203 	X509_email_free(ocsp_urls);
204 	return NULL;
205 }
206 
207 static int
208 tls_ocsp_verify_response(struct tls *ctx, OCSP_RESPONSE *resp)
209 {
210 	OCSP_BASICRESP *br = NULL;
211 	ASN1_GENERALIZEDTIME *revtime = NULL, *thisupd = NULL, *nextupd = NULL;
212 	OCSP_CERTID *cid = NULL;
213 	STACK_OF(X509) *combined = NULL;
214 	int response_status=0, cert_status=0, crl_reason=0;
215 	int ret = -1;
216 	unsigned long flags;
217 
218 	if ((br = OCSP_response_get1_basic(resp)) == NULL) {
219 		tls_set_errorx(ctx, "cannot load ocsp reply");
220 		goto err;
221 	}
222 
223 	/*
224 	 * Skip validation of 'extra_certs' as this should be done
225 	 * already as part of main handshake.
226 	 */
227 	flags = OCSP_TRUSTOTHER;
228 
229 	/* now verify */
230 	if (OCSP_basic_verify(br, ctx->ocsp->extra_certs,
231 		SSL_CTX_get_cert_store(ctx->ssl_ctx), flags) != 1) {
232 		tls_set_errorx(ctx, "ocsp verify failed");
233 		goto err;
234 	}
235 
236 	/* signature OK, look inside */
237 	response_status = OCSP_response_status(resp);
238 	if (response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
239 		tls_set_errorx(ctx, "ocsp verify failed: response - %s",
240 		    OCSP_response_status_str(response_status));
241 		goto err;
242 	}
243 
244 	cid = tls_ocsp_get_certid(ctx->ocsp->main_cert,
245 	    ctx->ocsp->extra_certs, ctx->ssl_ctx);
246 	if (cid == NULL) {
247 		tls_set_errorx(ctx, "ocsp verify failed: no issuer cert");
248 		goto err;
249 	}
250 
251 	if (OCSP_resp_find_status(br, cid, &cert_status, &crl_reason,
252 	    &revtime, &thisupd, &nextupd) != 1) {
253 		tls_set_errorx(ctx, "ocsp verify failed: no result for cert");
254 		goto err;
255 	}
256 
257 	if (OCSP_check_validity(thisupd, nextupd, JITTER_SEC,
258 	    MAXAGE_SEC) != 1) {
259 		tls_set_errorx(ctx,
260 		    "ocsp verify failed: ocsp response not current");
261 		goto err;
262 	}
263 
264 	if (tls_ocsp_fill_info(ctx, response_status, cert_status,
265 	    crl_reason, revtime, thisupd, nextupd) != 0)
266 		goto err;
267 
268 	/* finally can look at status */
269 	if (cert_status != V_OCSP_CERTSTATUS_GOOD && cert_status !=
270 	    V_OCSP_CERTSTATUS_UNKNOWN) {
271 		tls_set_errorx(ctx, "ocsp verify failed: revoked cert - %s",
272 			       OCSP_crl_reason_str(crl_reason));
273 		goto err;
274 	}
275 	ret = 0;
276 
277  err:
278 	sk_X509_free(combined);
279 	OCSP_CERTID_free(cid);
280 	OCSP_BASICRESP_free(br);
281 	return ret;
282 }
283 
284 /*
285  * Process a raw OCSP response from an OCSP server request.
286  * OCSP details can then be retrieved with tls_peer_ocsp_* functions.
287  * returns 0 if certificate ok, -1 otherwise.
288  */
289 static int
290 tls_ocsp_process_response_internal(struct tls *ctx, const unsigned char *response,
291     size_t size)
292 {
293 	int ret;
294 	OCSP_RESPONSE *resp;
295 
296 	resp = d2i_OCSP_RESPONSE(NULL, &response, size);
297 	if (resp == NULL) {
298 		tls_ocsp_free(ctx->ocsp);
299 		ctx->ocsp = NULL;
300 		tls_set_error(ctx, "unable to parse OCSP response");
301 		return -1;
302 	}
303 	ret = tls_ocsp_verify_response(ctx, resp);
304 	OCSP_RESPONSE_free(resp);
305 	return ret;
306 }
307 
308 /* TLS handshake verification callback for stapled requests */
309 int
310 tls_ocsp_verify_cb(SSL *ssl, void *arg)
311 {
312 	const unsigned char *raw = NULL;
313 	int size, res = -1;
314 	struct tls *ctx;
315 
316 	if ((ctx = SSL_get_app_data(ssl)) == NULL)
317 		return -1;
318 
319 	size = SSL_get_tlsext_status_ocsp_resp(ssl, &raw);
320 	if (size <= 0) {
321 		if (ctx->config->ocsp_require_stapling) {
322 			tls_set_errorx(ctx, "no stapled OCSP response provided");
323 			return 0;
324 		}
325 		return 1;
326 	}
327 
328 	tls_ocsp_free(ctx->ocsp);
329 	if ((ctx->ocsp = tls_ocsp_setup_from_peer(ctx)) == NULL)
330 		return 0;
331 
332 	if (ctx->config->verify_cert == 0 || ctx->config->verify_time == 0)
333 		return 1;
334 
335 	res = tls_ocsp_process_response_internal(ctx, raw, size);
336 
337 	return (res == 0) ? 1 : 0;
338 }
339 
340 
341 /* Staple the OCSP information in ctx->ocsp to the server handshake. */
342 int
343 tls_ocsp_stapling_cb(SSL *ssl, void *arg)
344 {
345 	int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
346 	unsigned char *ocsp_staple = NULL;
347 	struct tls *ctx;
348 
349 	if ((ctx = SSL_get_app_data(ssl)) == NULL)
350 		goto err;
351 
352 	if (ctx->keypair == NULL || ctx->keypair->ocsp_staple == NULL ||
353 	    ctx->keypair->ocsp_staple_len == 0)
354 		return SSL_TLSEXT_ERR_NOACK;
355 
356 	if ((ocsp_staple = malloc(ctx->keypair->ocsp_staple_len)) == NULL)
357 		goto err;
358 
359 	memcpy(ocsp_staple, ctx->keypair->ocsp_staple,
360 	    ctx->keypair->ocsp_staple_len);
361 
362 	if (SSL_set_tlsext_status_ocsp_resp(ctx->ssl_conn, ocsp_staple,
363 	    ctx->keypair->ocsp_staple_len) != 1)
364 		goto err;
365 
366 	ret = SSL_TLSEXT_ERR_OK;
367  err:
368 	if (ret != SSL_TLSEXT_ERR_OK)
369 		free(ocsp_staple);
370 
371 	return ret;
372 }
373 
374 /*
375  * Public API
376  */
377 
378 /* Retrieve OCSP URL from peer certificate, if present. */
379 const char *
380 tls_peer_ocsp_url(struct tls *ctx)
381 {
382 	if (ctx->ocsp == NULL)
383 		return NULL;
384 	return ctx->ocsp->ocsp_url;
385 }
386 
387 const char *
388 tls_peer_ocsp_result(struct tls *ctx)
389 {
390 	if (ctx->ocsp == NULL)
391 		return NULL;
392 	if (ctx->ocsp->ocsp_result == NULL)
393 		return NULL;
394 	return ctx->ocsp->ocsp_result->result_msg;
395 }
396 
397 int
398 tls_peer_ocsp_response_status(struct tls *ctx)
399 {
400 	if (ctx->ocsp == NULL)
401 		return -1;
402 	if (ctx->ocsp->ocsp_result == NULL)
403 		return -1;
404 	return ctx->ocsp->ocsp_result->response_status;
405 }
406 
407 int
408 tls_peer_ocsp_cert_status(struct tls *ctx)
409 {
410 	if (ctx->ocsp == NULL)
411 		return -1;
412 	if (ctx->ocsp->ocsp_result == NULL)
413 		return -1;
414 	return ctx->ocsp->ocsp_result->cert_status;
415 }
416 
417 int
418 tls_peer_ocsp_crl_reason(struct tls *ctx)
419 {
420 	if (ctx->ocsp == NULL)
421 		return -1;
422 	if (ctx->ocsp->ocsp_result == NULL)
423 		return -1;
424 	return ctx->ocsp->ocsp_result->crl_reason;
425 }
426 
427 time_t
428 tls_peer_ocsp_this_update(struct tls *ctx)
429 {
430 	if (ctx->ocsp == NULL)
431 		return -1;
432 	if (ctx->ocsp->ocsp_result == NULL)
433 		return -1;
434 	return ctx->ocsp->ocsp_result->this_update;
435 }
436 
437 time_t
438 tls_peer_ocsp_next_update(struct tls *ctx)
439 {
440 	if (ctx->ocsp == NULL)
441 		return -1;
442 	if (ctx->ocsp->ocsp_result == NULL)
443 		return -1;
444 	return ctx->ocsp->ocsp_result->next_update;
445 }
446 
447 time_t
448 tls_peer_ocsp_revocation_time(struct tls *ctx)
449 {
450 	if (ctx->ocsp == NULL)
451 		return -1;
452 	if (ctx->ocsp->ocsp_result == NULL)
453 		return -1;
454 	return ctx->ocsp->ocsp_result->revocation_time;
455 }
456 
457 int
458 tls_ocsp_process_response(struct tls *ctx, const unsigned char *response,
459     size_t size)
460 {
461 	if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0)
462 		return -1;
463 	return tls_ocsp_process_response_internal(ctx, response, size);
464 }
465