Blob


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 */
19 #include "config.h"
21 #include <sys/types.h>
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
26 #include <string.h>
28 #include <openssl/err.h>
29 #include <openssl/ocsp.h>
30 #include <openssl/x509.h>
32 #include <tls.h>
33 #include "tls_internal.h"
35 #define MAXAGE_SEC (14*24*60*60)
36 #define JITTER_SEC (60)
38 /*
39 * State for request.
40 */
42 static struct tls_ocsp *
43 tls_ocsp_new(void)
44 {
45 return (calloc(1, sizeof(struct tls_ocsp)));
46 }
48 void
49 tls_ocsp_free(struct tls_ocsp *ocsp)
50 {
51 if (ocsp == NULL)
52 return;
54 X509_free(ocsp->main_cert);
55 free(ocsp->ocsp_result);
56 free(ocsp->ocsp_url);
58 free(ocsp);
59 }
61 static int
62 tls_ocsp_asn1_parse_time(struct tls *ctx, ASN1_GENERALIZEDTIME *gt, time_t *gt_time)
63 {
64 struct tm tm;
66 if (gt == NULL)
67 return -1;
68 /* RFC 6960 specifies that all times in OCSP must be GENERALIZEDTIME */
69 if (!ASN1_GENERALIZEDTIME_check(gt))
70 return -1;
71 if (!ASN1_TIME_to_tm(gt, &tm))
72 return -1;
73 if ((*gt_time = timegm(&tm)) == -1)
74 return -1;
75 return 0;
76 }
78 static int
79 tls_ocsp_fill_info(struct tls *ctx, int response_status, int cert_status,
80 int crl_reason, ASN1_GENERALIZEDTIME *revtime,
81 ASN1_GENERALIZEDTIME *thisupd, ASN1_GENERALIZEDTIME *nextupd)
82 {
83 struct tls_ocsp_result *info = NULL;
85 free(ctx->ocsp->ocsp_result);
86 ctx->ocsp->ocsp_result = NULL;
88 if ((info = calloc(1, sizeof (struct tls_ocsp_result))) == NULL) {
89 tls_set_error(ctx, "calloc");
90 return -1;
91 }
92 info->response_status = response_status;
93 info->cert_status = cert_status;
94 info->crl_reason = crl_reason;
95 if (info->response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
96 info->result_msg =
97 OCSP_response_status_str(info->response_status);
98 } else if (info->cert_status != V_OCSP_CERTSTATUS_REVOKED) {
99 info->result_msg = OCSP_cert_status_str(info->cert_status);
100 } else {
101 info->result_msg = OCSP_crl_reason_str(info->crl_reason);
103 info->revocation_time = info->this_update = info->next_update = -1;
104 if (revtime != NULL &&
105 tls_ocsp_asn1_parse_time(ctx, revtime, &info->revocation_time) != 0) {
106 tls_set_error(ctx,
107 "unable to parse revocation time in OCSP reply");
108 goto err;
110 if (thisupd != NULL &&
111 tls_ocsp_asn1_parse_time(ctx, thisupd, &info->this_update) != 0) {
112 tls_set_error(ctx,
113 "unable to parse this update time in OCSP reply");
114 goto err;
116 if (nextupd != NULL &&
117 tls_ocsp_asn1_parse_time(ctx, nextupd, &info->next_update) != 0) {
118 tls_set_error(ctx,
119 "unable to parse next update time in OCSP reply");
120 goto err;
122 ctx->ocsp->ocsp_result = info;
123 return 0;
125 err:
126 free(info);
127 return -1;
130 static OCSP_CERTID *
131 tls_ocsp_get_certid(X509 *main_cert, STACK_OF(X509) *extra_certs,
132 SSL_CTX *ssl_ctx)
134 X509_NAME *issuer_name;
135 X509 *issuer;
136 X509_STORE_CTX *storectx = NULL;
137 X509_OBJECT *obj = NULL;
138 OCSP_CERTID *cid = NULL;
139 X509_STORE *store;
141 if ((issuer_name = X509_get_issuer_name(main_cert)) == NULL)
142 goto out;
144 if (extra_certs != NULL) {
145 issuer = X509_find_by_subject(extra_certs, issuer_name);
146 if (issuer != NULL) {
147 cid = OCSP_cert_to_id(NULL, main_cert, issuer);
148 goto out;
152 if ((store = SSL_CTX_get_cert_store(ssl_ctx)) == NULL)
153 goto out;
154 if ((storectx = X509_STORE_CTX_new()) == NULL)
155 goto out;
156 if (X509_STORE_CTX_init(storectx, store, main_cert, extra_certs) != 1)
157 goto out;
158 if ((obj = X509_STORE_CTX_get_obj_by_subject(storectx, X509_LU_X509,
159 issuer_name)) == NULL)
160 goto out;
162 cid = OCSP_cert_to_id(NULL, main_cert, X509_OBJECT_get0_X509(obj));
164 out:
165 X509_STORE_CTX_free(storectx);
166 X509_OBJECT_free(obj);
168 return cid;
171 struct tls_ocsp *
172 tls_ocsp_setup_from_peer(struct tls *ctx)
174 struct tls_ocsp *ocsp = NULL;
175 STACK_OF(OPENSSL_STRING) *ocsp_urls = NULL;
177 if ((ocsp = tls_ocsp_new()) == NULL)
178 goto err;
180 /* steal state from ctx struct */
181 ocsp->main_cert = SSL_get_peer_certificate(ctx->ssl_conn);
182 ocsp->extra_certs = SSL_get_peer_cert_chain(ctx->ssl_conn);
183 if (ocsp->main_cert == NULL) {
184 tls_set_errorx(ctx, "no peer certificate for OCSP");
185 goto err;
188 ocsp_urls = X509_get1_ocsp(ocsp->main_cert);
189 if (ocsp_urls == NULL) {
190 tls_set_errorx(ctx, "no OCSP URLs in peer certificate");
191 goto err;
194 ocsp->ocsp_url = strdup(sk_OPENSSL_STRING_value(ocsp_urls, 0));
195 if (ocsp->ocsp_url == NULL) {
196 tls_set_errorx(ctx, "out of memory");
197 goto err;
200 X509_email_free(ocsp_urls);
201 return ocsp;
203 err:
204 tls_ocsp_free(ocsp);
205 X509_email_free(ocsp_urls);
206 return NULL;
209 static int
210 tls_ocsp_verify_response(struct tls *ctx, OCSP_RESPONSE *resp)
212 OCSP_BASICRESP *br = NULL;
213 ASN1_GENERALIZEDTIME *revtime = NULL, *thisupd = NULL, *nextupd = NULL;
214 OCSP_CERTID *cid = NULL;
215 STACK_OF(X509) *combined = NULL;
216 int response_status=0, cert_status=0, crl_reason=0;
217 int ret = -1;
218 unsigned long flags;
220 if ((br = OCSP_response_get1_basic(resp)) == NULL) {
221 tls_set_errorx(ctx, "cannot load ocsp reply");
222 goto err;
225 /*
226 * Skip validation of 'extra_certs' as this should be done
227 * already as part of main handshake.
228 */
229 flags = OCSP_TRUSTOTHER;
231 /* now verify */
232 if (OCSP_basic_verify(br, ctx->ocsp->extra_certs,
233 SSL_CTX_get_cert_store(ctx->ssl_ctx), flags) != 1) {
234 tls_set_errorx(ctx, "ocsp verify failed");
235 goto err;
238 /* signature OK, look inside */
239 response_status = OCSP_response_status(resp);
240 if (response_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
241 tls_set_errorx(ctx, "ocsp verify failed: response - %s",
242 OCSP_response_status_str(response_status));
243 goto err;
246 cid = tls_ocsp_get_certid(ctx->ocsp->main_cert,
247 ctx->ocsp->extra_certs, ctx->ssl_ctx);
248 if (cid == NULL) {
249 tls_set_errorx(ctx, "ocsp verify failed: no issuer cert");
250 goto err;
253 if (OCSP_resp_find_status(br, cid, &cert_status, &crl_reason,
254 &revtime, &thisupd, &nextupd) != 1) {
255 tls_set_errorx(ctx, "ocsp verify failed: no result for cert");
256 goto err;
259 if (OCSP_check_validity(thisupd, nextupd, JITTER_SEC,
260 MAXAGE_SEC) != 1) {
261 tls_set_errorx(ctx,
262 "ocsp verify failed: ocsp response not current");
263 goto err;
266 if (tls_ocsp_fill_info(ctx, response_status, cert_status,
267 crl_reason, revtime, thisupd, nextupd) != 0)
268 goto err;
270 /* finally can look at status */
271 if (cert_status != V_OCSP_CERTSTATUS_GOOD && cert_status !=
272 V_OCSP_CERTSTATUS_UNKNOWN) {
273 tls_set_errorx(ctx, "ocsp verify failed: revoked cert - %s",
274 OCSP_crl_reason_str(crl_reason));
275 goto err;
277 ret = 0;
279 err:
280 sk_X509_free(combined);
281 OCSP_CERTID_free(cid);
282 OCSP_BASICRESP_free(br);
283 return ret;
286 /*
287 * Process a raw OCSP response from an OCSP server request.
288 * OCSP details can then be retrieved with tls_peer_ocsp_* functions.
289 * returns 0 if certificate ok, -1 otherwise.
290 */
291 static int
292 tls_ocsp_process_response_internal(struct tls *ctx, const unsigned char *response,
293 size_t size)
295 int ret;
296 OCSP_RESPONSE *resp;
298 resp = d2i_OCSP_RESPONSE(NULL, &response, size);
299 if (resp == NULL) {
300 tls_ocsp_free(ctx->ocsp);
301 ctx->ocsp = NULL;
302 tls_set_error(ctx, "unable to parse OCSP response");
303 return -1;
305 ret = tls_ocsp_verify_response(ctx, resp);
306 OCSP_RESPONSE_free(resp);
307 return ret;
310 /* TLS handshake verification callback for stapled requests */
311 int
312 tls_ocsp_verify_cb(SSL *ssl, void *arg)
314 const unsigned char *raw = NULL;
315 int size, res = -1;
316 struct tls *ctx;
318 if ((ctx = SSL_get_app_data(ssl)) == NULL)
319 return -1;
321 size = SSL_get_tlsext_status_ocsp_resp(ssl, &raw);
322 if (size <= 0) {
323 if (ctx->config->ocsp_require_stapling) {
324 tls_set_errorx(ctx, "no stapled OCSP response provided");
325 return 0;
327 return 1;
330 tls_ocsp_free(ctx->ocsp);
331 if ((ctx->ocsp = tls_ocsp_setup_from_peer(ctx)) == NULL)
332 return 0;
334 if (ctx->config->verify_cert == 0 || ctx->config->verify_time == 0)
335 return 1;
337 res = tls_ocsp_process_response_internal(ctx, raw, size);
339 return (res == 0) ? 1 : 0;
343 /* Staple the OCSP information in ctx->ocsp to the server handshake. */
344 int
345 tls_ocsp_stapling_cb(SSL *ssl, void *arg)
347 int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
348 unsigned char *ocsp_staple = NULL;
349 struct tls *ctx;
351 if ((ctx = SSL_get_app_data(ssl)) == NULL)
352 goto err;
354 if (ctx->keypair == NULL || ctx->keypair->ocsp_staple == NULL ||
355 ctx->keypair->ocsp_staple_len == 0)
356 return SSL_TLSEXT_ERR_NOACK;
358 if ((ocsp_staple = malloc(ctx->keypair->ocsp_staple_len)) == NULL)
359 goto err;
361 memcpy(ocsp_staple, ctx->keypair->ocsp_staple,
362 ctx->keypair->ocsp_staple_len);
364 if (SSL_set_tlsext_status_ocsp_resp(ctx->ssl_conn, ocsp_staple,
365 ctx->keypair->ocsp_staple_len) != 1)
366 goto err;
368 ret = SSL_TLSEXT_ERR_OK;
369 err:
370 if (ret != SSL_TLSEXT_ERR_OK)
371 free(ocsp_staple);
373 return ret;
376 /*
377 * Public API
378 */
380 /* Retrieve OCSP URL from peer certificate, if present. */
381 const char *
382 tls_peer_ocsp_url(struct tls *ctx)
384 if (ctx->ocsp == NULL)
385 return NULL;
386 return ctx->ocsp->ocsp_url;
389 const char *
390 tls_peer_ocsp_result(struct tls *ctx)
392 if (ctx->ocsp == NULL)
393 return NULL;
394 if (ctx->ocsp->ocsp_result == NULL)
395 return NULL;
396 return ctx->ocsp->ocsp_result->result_msg;
399 int
400 tls_peer_ocsp_response_status(struct tls *ctx)
402 if (ctx->ocsp == NULL)
403 return -1;
404 if (ctx->ocsp->ocsp_result == NULL)
405 return -1;
406 return ctx->ocsp->ocsp_result->response_status;
409 int
410 tls_peer_ocsp_cert_status(struct tls *ctx)
412 if (ctx->ocsp == NULL)
413 return -1;
414 if (ctx->ocsp->ocsp_result == NULL)
415 return -1;
416 return ctx->ocsp->ocsp_result->cert_status;
419 int
420 tls_peer_ocsp_crl_reason(struct tls *ctx)
422 if (ctx->ocsp == NULL)
423 return -1;
424 if (ctx->ocsp->ocsp_result == NULL)
425 return -1;
426 return ctx->ocsp->ocsp_result->crl_reason;
429 time_t
430 tls_peer_ocsp_this_update(struct tls *ctx)
432 if (ctx->ocsp == NULL)
433 return -1;
434 if (ctx->ocsp->ocsp_result == NULL)
435 return -1;
436 return ctx->ocsp->ocsp_result->this_update;
439 time_t
440 tls_peer_ocsp_next_update(struct tls *ctx)
442 if (ctx->ocsp == NULL)
443 return -1;
444 if (ctx->ocsp->ocsp_result == NULL)
445 return -1;
446 return ctx->ocsp->ocsp_result->next_update;
449 time_t
450 tls_peer_ocsp_revocation_time(struct tls *ctx)
452 if (ctx->ocsp == NULL)
453 return -1;
454 if (ctx->ocsp->ocsp_result == NULL)
455 return -1;
456 return ctx->ocsp->ocsp_result->revocation_time;
459 int
460 tls_ocsp_process_response(struct tls *ctx, const unsigned char *response,
461 size_t size)
463 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0)
464 return -1;
465 return tls_ocsp_process_response_internal(ctx, response, size);