1 ebfc5784 2024-01-07 op /* $OpenBSD: tls_ocsp.c,v 1.24 2023/11/13 10:56:19 tb Exp $ */
3 f9ab77a8 2023-08-23 op * Copyright (c) 2015 Marko Kreen <markokr@gmail.com>
4 f9ab77a8 2023-08-23 op * Copyright (c) 2016 Bob Beck <beck@openbsd.org>
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.
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.
19 f9ab77a8 2023-08-23 op #include "config.h"
21 f9ab77a8 2023-08-23 op #include <sys/types.h>
23 f9ab77a8 2023-08-23 op #include <arpa/inet.h>
24 f9ab77a8 2023-08-23 op #include <netinet/in.h>
26 f9ab77a8 2023-08-23 op #include <string.h>
28 f9ab77a8 2023-08-23 op #include <openssl/err.h>
29 f9ab77a8 2023-08-23 op #include <openssl/ocsp.h>
30 f9ab77a8 2023-08-23 op #include <openssl/x509.h>
32 f9ab77a8 2023-08-23 op #include <tls.h>
33 f9ab77a8 2023-08-23 op #include "tls_internal.h"
35 f9ab77a8 2023-08-23 op #define MAXAGE_SEC (14*24*60*60)
36 f9ab77a8 2023-08-23 op #define JITTER_SEC (60)
39 f9ab77a8 2023-08-23 op * State for request.
42 f9ab77a8 2023-08-23 op static struct tls_ocsp *
43 f9ab77a8 2023-08-23 op tls_ocsp_new(void)
45 f9ab77a8 2023-08-23 op return (calloc(1, sizeof(struct tls_ocsp)));
49 f9ab77a8 2023-08-23 op tls_ocsp_free(struct tls_ocsp *ocsp)
51 f9ab77a8 2023-08-23 op if (ocsp == NULL)
54 f9ab77a8 2023-08-23 op X509_free(ocsp->main_cert);
55 f9ab77a8 2023-08-23 op free(ocsp->ocsp_result);
56 f9ab77a8 2023-08-23 op free(ocsp->ocsp_url);
62 f9ab77a8 2023-08-23 op tls_ocsp_asn1_parse_time(struct tls *ctx, ASN1_GENERALIZEDTIME *gt, time_t *gt_time)
66 f9ab77a8 2023-08-23 op if (gt == NULL)
68 f9ab77a8 2023-08-23 op /* RFC 6960 specifies that all times in OCSP must be GENERALIZEDTIME */
69 ebfc5784 2024-01-07 op if (!ASN1_GENERALIZEDTIME_check(gt))
71 ebfc5784 2024-01-07 op if (!ASN1_TIME_to_tm(gt, &tm))
73 f9ab77a8 2023-08-23 op if ((*gt_time = timegm(&tm)) == -1)
79 f9ab77a8 2023-08-23 op tls_ocsp_fill_info(struct tls *ctx, int response_status, int cert_status,
80 f9ab77a8 2023-08-23 op int crl_reason, ASN1_GENERALIZEDTIME *revtime,
81 f9ab77a8 2023-08-23 op ASN1_GENERALIZEDTIME *thisupd, ASN1_GENERALIZEDTIME *nextupd)
83 f9ab77a8 2023-08-23 op struct tls_ocsp_result *info = NULL;
85 f9ab77a8 2023-08-23 op free(ctx->ocsp->ocsp_result);
86 f9ab77a8 2023-08-23 op ctx->ocsp->ocsp_result = NULL;
88 f9ab77a8 2023-08-23 op if ((info = calloc(1, sizeof (struct tls_ocsp_result))) == NULL) {
89 f9ab77a8 2023-08-23 op tls_set_error(ctx, "calloc");
92 f9ab77a8 2023-08-23 op info->response_status = response_status;
93 f9ab77a8 2023-08-23 op info->cert_status = cert_status;
94 f9ab77a8 2023-08-23 op info->crl_reason = crl_reason;
95 f9ab77a8 2023-08-23 op if (info->response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
96 f9ab77a8 2023-08-23 op info->result_msg =
97 f9ab77a8 2023-08-23 op OCSP_response_status_str(info->response_status);
98 f9ab77a8 2023-08-23 op } else if (info->cert_status != V_OCSP_CERTSTATUS_REVOKED) {
99 f9ab77a8 2023-08-23 op info->result_msg = OCSP_cert_status_str(info->cert_status);
101 f9ab77a8 2023-08-23 op info->result_msg = OCSP_crl_reason_str(info->crl_reason);
103 f9ab77a8 2023-08-23 op info->revocation_time = info->this_update = info->next_update = -1;
104 f9ab77a8 2023-08-23 op if (revtime != NULL &&
105 f9ab77a8 2023-08-23 op tls_ocsp_asn1_parse_time(ctx, revtime, &info->revocation_time) != 0) {
106 f9ab77a8 2023-08-23 op tls_set_error(ctx,
107 f9ab77a8 2023-08-23 op "unable to parse revocation time in OCSP reply");
110 f9ab77a8 2023-08-23 op if (thisupd != NULL &&
111 f9ab77a8 2023-08-23 op tls_ocsp_asn1_parse_time(ctx, thisupd, &info->this_update) != 0) {
112 f9ab77a8 2023-08-23 op tls_set_error(ctx,
113 f9ab77a8 2023-08-23 op "unable to parse this update time in OCSP reply");
116 f9ab77a8 2023-08-23 op if (nextupd != NULL &&
117 f9ab77a8 2023-08-23 op tls_ocsp_asn1_parse_time(ctx, nextupd, &info->next_update) != 0) {
118 f9ab77a8 2023-08-23 op tls_set_error(ctx,
119 f9ab77a8 2023-08-23 op "unable to parse next update time in OCSP reply");
122 f9ab77a8 2023-08-23 op ctx->ocsp->ocsp_result = info;
130 f9ab77a8 2023-08-23 op static OCSP_CERTID *
131 f9ab77a8 2023-08-23 op tls_ocsp_get_certid(X509 *main_cert, STACK_OF(X509) *extra_certs,
132 f9ab77a8 2023-08-23 op SSL_CTX *ssl_ctx)
134 f9ab77a8 2023-08-23 op X509_NAME *issuer_name;
135 f9ab77a8 2023-08-23 op X509 *issuer;
136 f9ab77a8 2023-08-23 op X509_STORE_CTX *storectx = NULL;
137 f9ab77a8 2023-08-23 op X509_OBJECT *obj = NULL;
138 f9ab77a8 2023-08-23 op OCSP_CERTID *cid = NULL;
139 f9ab77a8 2023-08-23 op X509_STORE *store;
141 f9ab77a8 2023-08-23 op if ((issuer_name = X509_get_issuer_name(main_cert)) == NULL)
144 f9ab77a8 2023-08-23 op if (extra_certs != NULL) {
145 f9ab77a8 2023-08-23 op issuer = X509_find_by_subject(extra_certs, issuer_name);
146 f9ab77a8 2023-08-23 op if (issuer != NULL) {
147 f9ab77a8 2023-08-23 op cid = OCSP_cert_to_id(NULL, main_cert, issuer);
152 f9ab77a8 2023-08-23 op if ((store = SSL_CTX_get_cert_store(ssl_ctx)) == NULL)
154 f9ab77a8 2023-08-23 op if ((storectx = X509_STORE_CTX_new()) == NULL)
156 f9ab77a8 2023-08-23 op if (X509_STORE_CTX_init(storectx, store, main_cert, extra_certs) != 1)
158 f9ab77a8 2023-08-23 op if ((obj = X509_STORE_CTX_get_obj_by_subject(storectx, X509_LU_X509,
159 f9ab77a8 2023-08-23 op issuer_name)) == NULL)
162 f9ab77a8 2023-08-23 op cid = OCSP_cert_to_id(NULL, main_cert, X509_OBJECT_get0_X509(obj));
165 f9ab77a8 2023-08-23 op X509_STORE_CTX_free(storectx);
166 f9ab77a8 2023-08-23 op X509_OBJECT_free(obj);
171 f9ab77a8 2023-08-23 op struct tls_ocsp *
172 f9ab77a8 2023-08-23 op tls_ocsp_setup_from_peer(struct tls *ctx)
174 f9ab77a8 2023-08-23 op struct tls_ocsp *ocsp = NULL;
175 f9ab77a8 2023-08-23 op STACK_OF(OPENSSL_STRING) *ocsp_urls = NULL;
177 f9ab77a8 2023-08-23 op if ((ocsp = tls_ocsp_new()) == NULL)
180 f9ab77a8 2023-08-23 op /* steal state from ctx struct */
181 f9ab77a8 2023-08-23 op ocsp->main_cert = SSL_get_peer_certificate(ctx->ssl_conn);
182 f9ab77a8 2023-08-23 op ocsp->extra_certs = SSL_get_peer_cert_chain(ctx->ssl_conn);
183 f9ab77a8 2023-08-23 op if (ocsp->main_cert == NULL) {
184 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "no peer certificate for OCSP");
188 f9ab77a8 2023-08-23 op ocsp_urls = X509_get1_ocsp(ocsp->main_cert);
189 f9ab77a8 2023-08-23 op if (ocsp_urls == NULL) {
190 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "no OCSP URLs in peer certificate");
194 f9ab77a8 2023-08-23 op ocsp->ocsp_url = strdup(sk_OPENSSL_STRING_value(ocsp_urls, 0));
195 f9ab77a8 2023-08-23 op if (ocsp->ocsp_url == NULL) {
196 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "out of memory");
200 f9ab77a8 2023-08-23 op X509_email_free(ocsp_urls);
204 f9ab77a8 2023-08-23 op tls_ocsp_free(ocsp);
205 f9ab77a8 2023-08-23 op X509_email_free(ocsp_urls);
210 f9ab77a8 2023-08-23 op tls_ocsp_verify_response(struct tls *ctx, OCSP_RESPONSE *resp)
212 f9ab77a8 2023-08-23 op OCSP_BASICRESP *br = NULL;
213 f9ab77a8 2023-08-23 op ASN1_GENERALIZEDTIME *revtime = NULL, *thisupd = NULL, *nextupd = NULL;
214 f9ab77a8 2023-08-23 op OCSP_CERTID *cid = NULL;
215 f9ab77a8 2023-08-23 op STACK_OF(X509) *combined = NULL;
216 f9ab77a8 2023-08-23 op int response_status=0, cert_status=0, crl_reason=0;
217 f9ab77a8 2023-08-23 op int ret = -1;
218 f9ab77a8 2023-08-23 op unsigned long flags;
220 f9ab77a8 2023-08-23 op if ((br = OCSP_response_get1_basic(resp)) == NULL) {
221 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "cannot load ocsp reply");
226 f9ab77a8 2023-08-23 op * Skip validation of 'extra_certs' as this should be done
227 f9ab77a8 2023-08-23 op * already as part of main handshake.
229 f9ab77a8 2023-08-23 op flags = OCSP_TRUSTOTHER;
231 f9ab77a8 2023-08-23 op /* now verify */
232 f9ab77a8 2023-08-23 op if (OCSP_basic_verify(br, ctx->ocsp->extra_certs,
233 f9ab77a8 2023-08-23 op SSL_CTX_get_cert_store(ctx->ssl_ctx), flags) != 1) {
234 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ocsp verify failed");
238 f9ab77a8 2023-08-23 op /* signature OK, look inside */
239 f9ab77a8 2023-08-23 op response_status = OCSP_response_status(resp);
240 f9ab77a8 2023-08-23 op if (response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
241 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ocsp verify failed: response - %s",
242 f9ab77a8 2023-08-23 op OCSP_response_status_str(response_status));
246 f9ab77a8 2023-08-23 op cid = tls_ocsp_get_certid(ctx->ocsp->main_cert,
247 f9ab77a8 2023-08-23 op ctx->ocsp->extra_certs, ctx->ssl_ctx);
248 f9ab77a8 2023-08-23 op if (cid == NULL) {
249 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ocsp verify failed: no issuer cert");
253 f9ab77a8 2023-08-23 op if (OCSP_resp_find_status(br, cid, &cert_status, &crl_reason,
254 f9ab77a8 2023-08-23 op &revtime, &thisupd, &nextupd) != 1) {
255 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ocsp verify failed: no result for cert");
259 f9ab77a8 2023-08-23 op if (OCSP_check_validity(thisupd, nextupd, JITTER_SEC,
260 f9ab77a8 2023-08-23 op MAXAGE_SEC) != 1) {
261 f9ab77a8 2023-08-23 op tls_set_errorx(ctx,
262 f9ab77a8 2023-08-23 op "ocsp verify failed: ocsp response not current");
266 f9ab77a8 2023-08-23 op if (tls_ocsp_fill_info(ctx, response_status, cert_status,
267 f9ab77a8 2023-08-23 op crl_reason, revtime, thisupd, nextupd) != 0)
270 f9ab77a8 2023-08-23 op /* finally can look at status */
271 f9ab77a8 2023-08-23 op if (cert_status != V_OCSP_CERTSTATUS_GOOD && cert_status !=
272 f9ab77a8 2023-08-23 op V_OCSP_CERTSTATUS_UNKNOWN) {
273 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "ocsp verify failed: revoked cert - %s",
274 f9ab77a8 2023-08-23 op OCSP_crl_reason_str(crl_reason));
280 f9ab77a8 2023-08-23 op sk_X509_free(combined);
281 f9ab77a8 2023-08-23 op OCSP_CERTID_free(cid);
282 f9ab77a8 2023-08-23 op OCSP_BASICRESP_free(br);
287 f9ab77a8 2023-08-23 op * Process a raw OCSP response from an OCSP server request.
288 f9ab77a8 2023-08-23 op * OCSP details can then be retrieved with tls_peer_ocsp_* functions.
289 f9ab77a8 2023-08-23 op * returns 0 if certificate ok, -1 otherwise.
292 f9ab77a8 2023-08-23 op tls_ocsp_process_response_internal(struct tls *ctx, const unsigned char *response,
296 f9ab77a8 2023-08-23 op OCSP_RESPONSE *resp;
298 f9ab77a8 2023-08-23 op resp = d2i_OCSP_RESPONSE(NULL, &response, size);
299 f9ab77a8 2023-08-23 op if (resp == NULL) {
300 f9ab77a8 2023-08-23 op tls_ocsp_free(ctx->ocsp);
301 f9ab77a8 2023-08-23 op ctx->ocsp = NULL;
302 f9ab77a8 2023-08-23 op tls_set_error(ctx, "unable to parse OCSP response");
305 f9ab77a8 2023-08-23 op ret = tls_ocsp_verify_response(ctx, resp);
306 f9ab77a8 2023-08-23 op OCSP_RESPONSE_free(resp);
310 f9ab77a8 2023-08-23 op /* TLS handshake verification callback for stapled requests */
312 f9ab77a8 2023-08-23 op tls_ocsp_verify_cb(SSL *ssl, void *arg)
314 f9ab77a8 2023-08-23 op const unsigned char *raw = NULL;
315 f9ab77a8 2023-08-23 op int size, res = -1;
316 f9ab77a8 2023-08-23 op struct tls *ctx;
318 f9ab77a8 2023-08-23 op if ((ctx = SSL_get_app_data(ssl)) == NULL)
321 f9ab77a8 2023-08-23 op size = SSL_get_tlsext_status_ocsp_resp(ssl, &raw);
322 f9ab77a8 2023-08-23 op if (size <= 0) {
323 f9ab77a8 2023-08-23 op if (ctx->config->ocsp_require_stapling) {
324 f9ab77a8 2023-08-23 op tls_set_errorx(ctx, "no stapled OCSP response provided");
330 f9ab77a8 2023-08-23 op tls_ocsp_free(ctx->ocsp);
331 f9ab77a8 2023-08-23 op if ((ctx->ocsp = tls_ocsp_setup_from_peer(ctx)) == NULL)
334 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0 || ctx->config->verify_time == 0)
337 f9ab77a8 2023-08-23 op res = tls_ocsp_process_response_internal(ctx, raw, size);
339 f9ab77a8 2023-08-23 op return (res == 0) ? 1 : 0;
343 f9ab77a8 2023-08-23 op /* Staple the OCSP information in ctx->ocsp to the server handshake. */
345 f9ab77a8 2023-08-23 op tls_ocsp_stapling_cb(SSL *ssl, void *arg)
347 f9ab77a8 2023-08-23 op int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
348 f9ab77a8 2023-08-23 op unsigned char *ocsp_staple = NULL;
349 f9ab77a8 2023-08-23 op struct tls *ctx;
351 f9ab77a8 2023-08-23 op if ((ctx = SSL_get_app_data(ssl)) == NULL)
354 f9ab77a8 2023-08-23 op if (ctx->keypair == NULL || ctx->keypair->ocsp_staple == NULL ||
355 f9ab77a8 2023-08-23 op ctx->keypair->ocsp_staple_len == 0)
356 f9ab77a8 2023-08-23 op return SSL_TLSEXT_ERR_NOACK;
358 f9ab77a8 2023-08-23 op if ((ocsp_staple = malloc(ctx->keypair->ocsp_staple_len)) == NULL)
361 f9ab77a8 2023-08-23 op memcpy(ocsp_staple, ctx->keypair->ocsp_staple,
362 f9ab77a8 2023-08-23 op ctx->keypair->ocsp_staple_len);
364 f9ab77a8 2023-08-23 op if (SSL_set_tlsext_status_ocsp_resp(ctx->ssl_conn, ocsp_staple,
365 f9ab77a8 2023-08-23 op ctx->keypair->ocsp_staple_len) != 1)
368 f9ab77a8 2023-08-23 op ret = SSL_TLSEXT_ERR_OK;
370 f9ab77a8 2023-08-23 op if (ret != SSL_TLSEXT_ERR_OK)
371 f9ab77a8 2023-08-23 op free(ocsp_staple);
380 f9ab77a8 2023-08-23 op /* Retrieve OCSP URL from peer certificate, if present. */
382 f9ab77a8 2023-08-23 op tls_peer_ocsp_url(struct tls *ctx)
384 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
386 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_url;
390 f9ab77a8 2023-08-23 op tls_peer_ocsp_result(struct tls *ctx)
392 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
394 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
396 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->result_msg;
400 f9ab77a8 2023-08-23 op tls_peer_ocsp_response_status(struct tls *ctx)
402 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
404 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
406 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->response_status;
410 f9ab77a8 2023-08-23 op tls_peer_ocsp_cert_status(struct tls *ctx)
412 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
414 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
416 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->cert_status;
420 f9ab77a8 2023-08-23 op tls_peer_ocsp_crl_reason(struct tls *ctx)
422 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
424 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
426 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->crl_reason;
430 f9ab77a8 2023-08-23 op tls_peer_ocsp_this_update(struct tls *ctx)
432 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
434 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
436 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->this_update;
440 f9ab77a8 2023-08-23 op tls_peer_ocsp_next_update(struct tls *ctx)
442 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
444 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
446 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->next_update;
450 f9ab77a8 2023-08-23 op tls_peer_ocsp_revocation_time(struct tls *ctx)
452 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
454 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
456 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->revocation_time;
460 f9ab77a8 2023-08-23 op tls_ocsp_process_response(struct tls *ctx, const unsigned char *response,
463 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0)
465 f9ab77a8 2023-08-23 op return tls_ocsp_process_response_internal(ctx, response, size);