Blame


1 ebfc5784 2024-01-07 op /* $OpenBSD: tls_ocsp.c,v 1.24 2023/11/13 10:56:19 tb Exp $ */
2 f9ab77a8 2023-08-23 op /*
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>
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 <sys/types.h>
22 f9ab77a8 2023-08-23 op
23 f9ab77a8 2023-08-23 op #include <arpa/inet.h>
24 f9ab77a8 2023-08-23 op #include <netinet/in.h>
25 f9ab77a8 2023-08-23 op
26 f9ab77a8 2023-08-23 op #include <string.h>
27 f9ab77a8 2023-08-23 op
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>
31 f9ab77a8 2023-08-23 op
32 f9ab77a8 2023-08-23 op #include <tls.h>
33 f9ab77a8 2023-08-23 op #include "tls_internal.h"
34 f9ab77a8 2023-08-23 op
35 f9ab77a8 2023-08-23 op #define MAXAGE_SEC (14*24*60*60)
36 f9ab77a8 2023-08-23 op #define JITTER_SEC (60)
37 f9ab77a8 2023-08-23 op
38 f9ab77a8 2023-08-23 op /*
39 f9ab77a8 2023-08-23 op * State for request.
40 f9ab77a8 2023-08-23 op */
41 f9ab77a8 2023-08-23 op
42 f9ab77a8 2023-08-23 op static struct tls_ocsp *
43 f9ab77a8 2023-08-23 op tls_ocsp_new(void)
44 f9ab77a8 2023-08-23 op {
45 f9ab77a8 2023-08-23 op return (calloc(1, sizeof(struct tls_ocsp)));
46 f9ab77a8 2023-08-23 op }
47 f9ab77a8 2023-08-23 op
48 f9ab77a8 2023-08-23 op void
49 f9ab77a8 2023-08-23 op tls_ocsp_free(struct tls_ocsp *ocsp)
50 f9ab77a8 2023-08-23 op {
51 f9ab77a8 2023-08-23 op if (ocsp == NULL)
52 f9ab77a8 2023-08-23 op return;
53 f9ab77a8 2023-08-23 op
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);
57 f9ab77a8 2023-08-23 op
58 f9ab77a8 2023-08-23 op free(ocsp);
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_ocsp_asn1_parse_time(struct tls *ctx, ASN1_GENERALIZEDTIME *gt, time_t *gt_time)
63 f9ab77a8 2023-08-23 op {
64 f9ab77a8 2023-08-23 op struct tm tm;
65 f9ab77a8 2023-08-23 op
66 f9ab77a8 2023-08-23 op if (gt == NULL)
67 f9ab77a8 2023-08-23 op return -1;
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))
70 f9ab77a8 2023-08-23 op return -1;
71 ebfc5784 2024-01-07 op if (!ASN1_TIME_to_tm(gt, &tm))
72 ebfc5784 2024-01-07 op return -1;
73 f9ab77a8 2023-08-23 op if ((*gt_time = timegm(&tm)) == -1)
74 f9ab77a8 2023-08-23 op return -1;
75 f9ab77a8 2023-08-23 op return 0;
76 f9ab77a8 2023-08-23 op }
77 f9ab77a8 2023-08-23 op
78 f9ab77a8 2023-08-23 op static int
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)
82 f9ab77a8 2023-08-23 op {
83 f9ab77a8 2023-08-23 op struct tls_ocsp_result *info = NULL;
84 f9ab77a8 2023-08-23 op
85 f9ab77a8 2023-08-23 op free(ctx->ocsp->ocsp_result);
86 f9ab77a8 2023-08-23 op ctx->ocsp->ocsp_result = NULL;
87 f9ab77a8 2023-08-23 op
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");
90 f9ab77a8 2023-08-23 op return -1;
91 f9ab77a8 2023-08-23 op }
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);
100 f9ab77a8 2023-08-23 op } else {
101 f9ab77a8 2023-08-23 op info->result_msg = OCSP_crl_reason_str(info->crl_reason);
102 f9ab77a8 2023-08-23 op }
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");
108 f9ab77a8 2023-08-23 op goto err;
109 f9ab77a8 2023-08-23 op }
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");
114 f9ab77a8 2023-08-23 op goto err;
115 f9ab77a8 2023-08-23 op }
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");
120 f9ab77a8 2023-08-23 op goto err;
121 f9ab77a8 2023-08-23 op }
122 f9ab77a8 2023-08-23 op ctx->ocsp->ocsp_result = info;
123 f9ab77a8 2023-08-23 op return 0;
124 f9ab77a8 2023-08-23 op
125 f9ab77a8 2023-08-23 op err:
126 f9ab77a8 2023-08-23 op free(info);
127 f9ab77a8 2023-08-23 op return -1;
128 f9ab77a8 2023-08-23 op }
129 f9ab77a8 2023-08-23 op
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)
133 f9ab77a8 2023-08-23 op {
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;
140 f9ab77a8 2023-08-23 op
141 f9ab77a8 2023-08-23 op if ((issuer_name = X509_get_issuer_name(main_cert)) == NULL)
142 f9ab77a8 2023-08-23 op goto out;
143 f9ab77a8 2023-08-23 op
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);
148 f9ab77a8 2023-08-23 op goto out;
149 f9ab77a8 2023-08-23 op }
150 f9ab77a8 2023-08-23 op }
151 f9ab77a8 2023-08-23 op
152 f9ab77a8 2023-08-23 op if ((store = SSL_CTX_get_cert_store(ssl_ctx)) == NULL)
153 f9ab77a8 2023-08-23 op goto out;
154 f9ab77a8 2023-08-23 op if ((storectx = X509_STORE_CTX_new()) == NULL)
155 f9ab77a8 2023-08-23 op goto out;
156 f9ab77a8 2023-08-23 op if (X509_STORE_CTX_init(storectx, store, main_cert, extra_certs) != 1)
157 f9ab77a8 2023-08-23 op goto out;
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)
160 f9ab77a8 2023-08-23 op goto out;
161 f9ab77a8 2023-08-23 op
162 f9ab77a8 2023-08-23 op cid = OCSP_cert_to_id(NULL, main_cert, X509_OBJECT_get0_X509(obj));
163 f9ab77a8 2023-08-23 op
164 f9ab77a8 2023-08-23 op out:
165 f9ab77a8 2023-08-23 op X509_STORE_CTX_free(storectx);
166 f9ab77a8 2023-08-23 op X509_OBJECT_free(obj);
167 f9ab77a8 2023-08-23 op
168 f9ab77a8 2023-08-23 op return cid;
169 f9ab77a8 2023-08-23 op }
170 f9ab77a8 2023-08-23 op
171 f9ab77a8 2023-08-23 op struct tls_ocsp *
172 f9ab77a8 2023-08-23 op tls_ocsp_setup_from_peer(struct tls *ctx)
173 f9ab77a8 2023-08-23 op {
174 f9ab77a8 2023-08-23 op struct tls_ocsp *ocsp = NULL;
175 f9ab77a8 2023-08-23 op STACK_OF(OPENSSL_STRING) *ocsp_urls = NULL;
176 f9ab77a8 2023-08-23 op
177 f9ab77a8 2023-08-23 op if ((ocsp = tls_ocsp_new()) == NULL)
178 f9ab77a8 2023-08-23 op goto err;
179 f9ab77a8 2023-08-23 op
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");
185 f9ab77a8 2023-08-23 op goto err;
186 f9ab77a8 2023-08-23 op }
187 f9ab77a8 2023-08-23 op
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");
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 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");
197 f9ab77a8 2023-08-23 op goto err;
198 f9ab77a8 2023-08-23 op }
199 f9ab77a8 2023-08-23 op
200 f9ab77a8 2023-08-23 op X509_email_free(ocsp_urls);
201 f9ab77a8 2023-08-23 op return ocsp;
202 f9ab77a8 2023-08-23 op
203 f9ab77a8 2023-08-23 op err:
204 f9ab77a8 2023-08-23 op tls_ocsp_free(ocsp);
205 f9ab77a8 2023-08-23 op X509_email_free(ocsp_urls);
206 f9ab77a8 2023-08-23 op return NULL;
207 f9ab77a8 2023-08-23 op }
208 f9ab77a8 2023-08-23 op
209 f9ab77a8 2023-08-23 op static int
210 f9ab77a8 2023-08-23 op tls_ocsp_verify_response(struct tls *ctx, OCSP_RESPONSE *resp)
211 f9ab77a8 2023-08-23 op {
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;
219 f9ab77a8 2023-08-23 op
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");
222 f9ab77a8 2023-08-23 op goto err;
223 f9ab77a8 2023-08-23 op }
224 f9ab77a8 2023-08-23 op
225 f9ab77a8 2023-08-23 op /*
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.
228 f9ab77a8 2023-08-23 op */
229 f9ab77a8 2023-08-23 op flags = OCSP_TRUSTOTHER;
230 f9ab77a8 2023-08-23 op
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");
235 f9ab77a8 2023-08-23 op goto err;
236 f9ab77a8 2023-08-23 op }
237 f9ab77a8 2023-08-23 op
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));
243 f9ab77a8 2023-08-23 op goto err;
244 f9ab77a8 2023-08-23 op }
245 f9ab77a8 2023-08-23 op
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");
250 f9ab77a8 2023-08-23 op goto err;
251 f9ab77a8 2023-08-23 op }
252 f9ab77a8 2023-08-23 op
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");
256 f9ab77a8 2023-08-23 op goto err;
257 f9ab77a8 2023-08-23 op }
258 f9ab77a8 2023-08-23 op
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");
263 f9ab77a8 2023-08-23 op goto err;
264 f9ab77a8 2023-08-23 op }
265 f9ab77a8 2023-08-23 op
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)
268 f9ab77a8 2023-08-23 op goto err;
269 f9ab77a8 2023-08-23 op
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));
275 f9ab77a8 2023-08-23 op goto err;
276 f9ab77a8 2023-08-23 op }
277 f9ab77a8 2023-08-23 op ret = 0;
278 f9ab77a8 2023-08-23 op
279 f9ab77a8 2023-08-23 op err:
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);
283 f9ab77a8 2023-08-23 op return ret;
284 f9ab77a8 2023-08-23 op }
285 f9ab77a8 2023-08-23 op
286 f9ab77a8 2023-08-23 op /*
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.
290 f9ab77a8 2023-08-23 op */
291 f9ab77a8 2023-08-23 op static int
292 f9ab77a8 2023-08-23 op tls_ocsp_process_response_internal(struct tls *ctx, const unsigned char *response,
293 f9ab77a8 2023-08-23 op size_t size)
294 f9ab77a8 2023-08-23 op {
295 f9ab77a8 2023-08-23 op int ret;
296 f9ab77a8 2023-08-23 op OCSP_RESPONSE *resp;
297 f9ab77a8 2023-08-23 op
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");
303 f9ab77a8 2023-08-23 op return -1;
304 f9ab77a8 2023-08-23 op }
305 f9ab77a8 2023-08-23 op ret = tls_ocsp_verify_response(ctx, resp);
306 f9ab77a8 2023-08-23 op OCSP_RESPONSE_free(resp);
307 f9ab77a8 2023-08-23 op return ret;
308 f9ab77a8 2023-08-23 op }
309 f9ab77a8 2023-08-23 op
310 f9ab77a8 2023-08-23 op /* TLS handshake verification callback for stapled requests */
311 f9ab77a8 2023-08-23 op int
312 f9ab77a8 2023-08-23 op tls_ocsp_verify_cb(SSL *ssl, void *arg)
313 f9ab77a8 2023-08-23 op {
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;
317 f9ab77a8 2023-08-23 op
318 f9ab77a8 2023-08-23 op if ((ctx = SSL_get_app_data(ssl)) == NULL)
319 f9ab77a8 2023-08-23 op return -1;
320 f9ab77a8 2023-08-23 op
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");
325 f9ab77a8 2023-08-23 op return 0;
326 f9ab77a8 2023-08-23 op }
327 f9ab77a8 2023-08-23 op return 1;
328 f9ab77a8 2023-08-23 op }
329 f9ab77a8 2023-08-23 op
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)
332 f9ab77a8 2023-08-23 op return 0;
333 f9ab77a8 2023-08-23 op
334 f9ab77a8 2023-08-23 op if (ctx->config->verify_cert == 0 || ctx->config->verify_time == 0)
335 f9ab77a8 2023-08-23 op return 1;
336 f9ab77a8 2023-08-23 op
337 f9ab77a8 2023-08-23 op res = tls_ocsp_process_response_internal(ctx, raw, size);
338 f9ab77a8 2023-08-23 op
339 f9ab77a8 2023-08-23 op return (res == 0) ? 1 : 0;
340 f9ab77a8 2023-08-23 op }
341 f9ab77a8 2023-08-23 op
342 f9ab77a8 2023-08-23 op
343 f9ab77a8 2023-08-23 op /* Staple the OCSP information in ctx->ocsp to the server handshake. */
344 f9ab77a8 2023-08-23 op int
345 f9ab77a8 2023-08-23 op tls_ocsp_stapling_cb(SSL *ssl, void *arg)
346 f9ab77a8 2023-08-23 op {
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;
350 f9ab77a8 2023-08-23 op
351 f9ab77a8 2023-08-23 op if ((ctx = SSL_get_app_data(ssl)) == NULL)
352 f9ab77a8 2023-08-23 op goto err;
353 f9ab77a8 2023-08-23 op
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;
357 f9ab77a8 2023-08-23 op
358 f9ab77a8 2023-08-23 op if ((ocsp_staple = malloc(ctx->keypair->ocsp_staple_len)) == NULL)
359 f9ab77a8 2023-08-23 op goto err;
360 f9ab77a8 2023-08-23 op
361 f9ab77a8 2023-08-23 op memcpy(ocsp_staple, ctx->keypair->ocsp_staple,
362 f9ab77a8 2023-08-23 op ctx->keypair->ocsp_staple_len);
363 f9ab77a8 2023-08-23 op
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)
366 f9ab77a8 2023-08-23 op goto err;
367 f9ab77a8 2023-08-23 op
368 f9ab77a8 2023-08-23 op ret = SSL_TLSEXT_ERR_OK;
369 f9ab77a8 2023-08-23 op err:
370 f9ab77a8 2023-08-23 op if (ret != SSL_TLSEXT_ERR_OK)
371 f9ab77a8 2023-08-23 op free(ocsp_staple);
372 f9ab77a8 2023-08-23 op
373 f9ab77a8 2023-08-23 op return ret;
374 f9ab77a8 2023-08-23 op }
375 f9ab77a8 2023-08-23 op
376 f9ab77a8 2023-08-23 op /*
377 f9ab77a8 2023-08-23 op * Public API
378 f9ab77a8 2023-08-23 op */
379 f9ab77a8 2023-08-23 op
380 f9ab77a8 2023-08-23 op /* Retrieve OCSP URL from peer certificate, if present. */
381 f9ab77a8 2023-08-23 op const char *
382 f9ab77a8 2023-08-23 op tls_peer_ocsp_url(struct tls *ctx)
383 f9ab77a8 2023-08-23 op {
384 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
385 f9ab77a8 2023-08-23 op return NULL;
386 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_url;
387 f9ab77a8 2023-08-23 op }
388 f9ab77a8 2023-08-23 op
389 f9ab77a8 2023-08-23 op const char *
390 f9ab77a8 2023-08-23 op tls_peer_ocsp_result(struct tls *ctx)
391 f9ab77a8 2023-08-23 op {
392 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
393 f9ab77a8 2023-08-23 op return NULL;
394 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
395 f9ab77a8 2023-08-23 op return NULL;
396 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->result_msg;
397 f9ab77a8 2023-08-23 op }
398 f9ab77a8 2023-08-23 op
399 f9ab77a8 2023-08-23 op int
400 f9ab77a8 2023-08-23 op tls_peer_ocsp_response_status(struct tls *ctx)
401 f9ab77a8 2023-08-23 op {
402 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
403 f9ab77a8 2023-08-23 op return -1;
404 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
405 f9ab77a8 2023-08-23 op return -1;
406 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->response_status;
407 f9ab77a8 2023-08-23 op }
408 f9ab77a8 2023-08-23 op
409 f9ab77a8 2023-08-23 op int
410 f9ab77a8 2023-08-23 op tls_peer_ocsp_cert_status(struct tls *ctx)
411 f9ab77a8 2023-08-23 op {
412 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
413 f9ab77a8 2023-08-23 op return -1;
414 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
415 f9ab77a8 2023-08-23 op return -1;
416 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->cert_status;
417 f9ab77a8 2023-08-23 op }
418 f9ab77a8 2023-08-23 op
419 f9ab77a8 2023-08-23 op int
420 f9ab77a8 2023-08-23 op tls_peer_ocsp_crl_reason(struct tls *ctx)
421 f9ab77a8 2023-08-23 op {
422 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
423 f9ab77a8 2023-08-23 op return -1;
424 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
425 f9ab77a8 2023-08-23 op return -1;
426 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->crl_reason;
427 f9ab77a8 2023-08-23 op }
428 f9ab77a8 2023-08-23 op
429 f9ab77a8 2023-08-23 op time_t
430 f9ab77a8 2023-08-23 op tls_peer_ocsp_this_update(struct tls *ctx)
431 f9ab77a8 2023-08-23 op {
432 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
433 f9ab77a8 2023-08-23 op return -1;
434 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
435 f9ab77a8 2023-08-23 op return -1;
436 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->this_update;
437 f9ab77a8 2023-08-23 op }
438 f9ab77a8 2023-08-23 op
439 f9ab77a8 2023-08-23 op time_t
440 f9ab77a8 2023-08-23 op tls_peer_ocsp_next_update(struct tls *ctx)
441 f9ab77a8 2023-08-23 op {
442 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
443 f9ab77a8 2023-08-23 op return -1;
444 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
445 f9ab77a8 2023-08-23 op return -1;
446 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->next_update;
447 f9ab77a8 2023-08-23 op }
448 f9ab77a8 2023-08-23 op
449 f9ab77a8 2023-08-23 op time_t
450 f9ab77a8 2023-08-23 op tls_peer_ocsp_revocation_time(struct tls *ctx)
451 f9ab77a8 2023-08-23 op {
452 f9ab77a8 2023-08-23 op if (ctx->ocsp == NULL)
453 f9ab77a8 2023-08-23 op return -1;
454 f9ab77a8 2023-08-23 op if (ctx->ocsp->ocsp_result == NULL)
455 f9ab77a8 2023-08-23 op return -1;
456 f9ab77a8 2023-08-23 op return ctx->ocsp->ocsp_result->revocation_time;
457 f9ab77a8 2023-08-23 op }
458 f9ab77a8 2023-08-23 op
459 f9ab77a8 2023-08-23 op int
460 f9ab77a8 2023-08-23 op tls_ocsp_process_response(struct tls *ctx, const unsigned char *response,
461 f9ab77a8 2023-08-23 op size_t size)
462 f9ab77a8 2023-08-23 op {
463 f9ab77a8 2023-08-23 op if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0)
464 f9ab77a8 2023-08-23 op return -1;
465 f9ab77a8 2023-08-23 op return tls_ocsp_process_response_internal(ctx, response, size);
466 f9ab77a8 2023-08-23 op }