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