1 /* $OpenBSD: tls_ocsp.c,v 1.24 2023/11/13 10:56:19 tb Exp $ */
3 * Copyright (c) 2015 Marko Kreen <markokr@gmail.com>
4 * Copyright (c) 2016 Bob Beck <beck@openbsd.org>
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.
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.
21 #include <sys/types.h>
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
28 #include <openssl/err.h>
29 #include <openssl/ocsp.h>
30 #include <openssl/x509.h>
33 #include "tls_internal.h"
35 #define MAXAGE_SEC (14*24*60*60)
36 #define JITTER_SEC (60)
42 static struct tls_ocsp *
45 return (calloc(1, sizeof(struct tls_ocsp)));
49 tls_ocsp_free(struct tls_ocsp *ocsp)
54 X509_free(ocsp->main_cert);
55 free(ocsp->ocsp_result);
62 tls_ocsp_asn1_parse_time(struct tls *ctx, ASN1_GENERALIZEDTIME *gt, time_t *gt_time)
68 /* RFC 6960 specifies that all times in OCSP must be GENERALIZEDTIME */
69 if (!ASN1_GENERALIZEDTIME_check(gt))
71 if (!ASN1_TIME_to_tm(gt, &tm))
73 if ((*gt_time = timegm(&tm)) == -1)
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)
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");
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) {
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);
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) {
107 "unable to parse revocation time in OCSP reply");
110 if (thisupd != NULL &&
111 tls_ocsp_asn1_parse_time(ctx, thisupd, &info->this_update) != 0) {
113 "unable to parse this update time in OCSP reply");
116 if (nextupd != NULL &&
117 tls_ocsp_asn1_parse_time(ctx, nextupd, &info->next_update) != 0) {
119 "unable to parse next update time in OCSP reply");
122 ctx->ocsp->ocsp_result = info;
131 tls_ocsp_get_certid(X509 *main_cert, STACK_OF(X509) *extra_certs,
134 X509_NAME *issuer_name;
136 X509_STORE_CTX *storectx = NULL;
137 X509_OBJECT *obj = NULL;
138 OCSP_CERTID *cid = NULL;
141 if ((issuer_name = X509_get_issuer_name(main_cert)) == NULL)
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);
152 if ((store = SSL_CTX_get_cert_store(ssl_ctx)) == NULL)
154 if ((storectx = X509_STORE_CTX_new()) == NULL)
156 if (X509_STORE_CTX_init(storectx, store, main_cert, extra_certs) != 1)
158 if ((obj = X509_STORE_CTX_get_obj_by_subject(storectx, X509_LU_X509,
159 issuer_name)) == NULL)
162 cid = OCSP_cert_to_id(NULL, main_cert, X509_OBJECT_get0_X509(obj));
165 X509_STORE_CTX_free(storectx);
166 X509_OBJECT_free(obj);
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)
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");
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");
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");
200 X509_email_free(ocsp_urls);
205 X509_email_free(ocsp_urls);
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;
220 if ((br = OCSP_response_get1_basic(resp)) == NULL) {
221 tls_set_errorx(ctx, "cannot load ocsp reply");
226 * Skip validation of 'extra_certs' as this should be done
227 * already as part of main handshake.
229 flags = OCSP_TRUSTOTHER;
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");
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));
246 cid = tls_ocsp_get_certid(ctx->ocsp->main_cert,
247 ctx->ocsp->extra_certs, ctx->ssl_ctx);
249 tls_set_errorx(ctx, "ocsp verify failed: no issuer cert");
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");
259 if (OCSP_check_validity(thisupd, nextupd, JITTER_SEC,
262 "ocsp verify failed: ocsp response not current");
266 if (tls_ocsp_fill_info(ctx, response_status, cert_status,
267 crl_reason, revtime, thisupd, nextupd) != 0)
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));
280 sk_X509_free(combined);
281 OCSP_CERTID_free(cid);
282 OCSP_BASICRESP_free(br);
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.
292 tls_ocsp_process_response_internal(struct tls *ctx, const unsigned char *response,
298 resp = d2i_OCSP_RESPONSE(NULL, &response, size);
300 tls_ocsp_free(ctx->ocsp);
302 tls_set_error(ctx, "unable to parse OCSP response");
305 ret = tls_ocsp_verify_response(ctx, resp);
306 OCSP_RESPONSE_free(resp);
310 /* TLS handshake verification callback for stapled requests */
312 tls_ocsp_verify_cb(SSL *ssl, void *arg)
314 const unsigned char *raw = NULL;
318 if ((ctx = SSL_get_app_data(ssl)) == NULL)
321 size = SSL_get_tlsext_status_ocsp_resp(ssl, &raw);
323 if (ctx->config->ocsp_require_stapling) {
324 tls_set_errorx(ctx, "no stapled OCSP response provided");
330 tls_ocsp_free(ctx->ocsp);
331 if ((ctx->ocsp = tls_ocsp_setup_from_peer(ctx)) == NULL)
334 if (ctx->config->verify_cert == 0 || ctx->config->verify_time == 0)
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. */
345 tls_ocsp_stapling_cb(SSL *ssl, void *arg)
347 int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
348 unsigned char *ocsp_staple = NULL;
351 if ((ctx = SSL_get_app_data(ssl)) == NULL)
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)
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)
368 ret = SSL_TLSEXT_ERR_OK;
370 if (ret != SSL_TLSEXT_ERR_OK)
380 /* Retrieve OCSP URL from peer certificate, if present. */
382 tls_peer_ocsp_url(struct tls *ctx)
384 if (ctx->ocsp == NULL)
386 return ctx->ocsp->ocsp_url;
390 tls_peer_ocsp_result(struct tls *ctx)
392 if (ctx->ocsp == NULL)
394 if (ctx->ocsp->ocsp_result == NULL)
396 return ctx->ocsp->ocsp_result->result_msg;
400 tls_peer_ocsp_response_status(struct tls *ctx)
402 if (ctx->ocsp == NULL)
404 if (ctx->ocsp->ocsp_result == NULL)
406 return ctx->ocsp->ocsp_result->response_status;
410 tls_peer_ocsp_cert_status(struct tls *ctx)
412 if (ctx->ocsp == NULL)
414 if (ctx->ocsp->ocsp_result == NULL)
416 return ctx->ocsp->ocsp_result->cert_status;
420 tls_peer_ocsp_crl_reason(struct tls *ctx)
422 if (ctx->ocsp == NULL)
424 if (ctx->ocsp->ocsp_result == NULL)
426 return ctx->ocsp->ocsp_result->crl_reason;
430 tls_peer_ocsp_this_update(struct tls *ctx)
432 if (ctx->ocsp == NULL)
434 if (ctx->ocsp->ocsp_result == NULL)
436 return ctx->ocsp->ocsp_result->this_update;
440 tls_peer_ocsp_next_update(struct tls *ctx)
442 if (ctx->ocsp == NULL)
444 if (ctx->ocsp->ocsp_result == NULL)
446 return ctx->ocsp->ocsp_result->next_update;
450 tls_peer_ocsp_revocation_time(struct tls *ctx)
452 if (ctx->ocsp == NULL)
454 if (ctx->ocsp->ocsp_result == NULL)
456 return ctx->ocsp->ocsp_result->revocation_time;
460 tls_ocsp_process_response(struct tls *ctx, const unsigned char *response,
463 if ((ctx->state & TLS_HANDSHAKE_COMPLETE) == 0)
465 return tls_ocsp_process_response_internal(ctx, response, size);