2 * Copyright (c) 2015 Marko Kreen <markokr@gmail.com>
3 * Copyright (c) 2016 Bob Beck <beck@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
23 #include <openssl/err.h>
24 #include <openssl/ocsp.h>
25 #include <openssl/x509.h>
28 #include "tls_internal.h"
30 #define MAXAGE_SEC (14*24*60*60)
31 #define JITTER_SEC (60)
37 static struct tls_ocsp
*
40 return (calloc(1, sizeof(struct tls_ocsp
)));
44 tls_ocsp_free(struct tls_ocsp
*ocsp
)
49 X509_free(ocsp
->main_cert
);
50 ocsp
->main_cert
= NULL
;
51 free(ocsp
->ocsp_result
);
52 ocsp
->ocsp_result
= NULL
;
54 ocsp
->ocsp_url
= NULL
;
59 tls_ocsp_asn1_parse_time(struct tls
*ctx
, ASN1_GENERALIZEDTIME
*gt
, time_t *gt_time
)
65 /* RFC 6960 specifies that all times in OCSP must be GENERALIZEDTIME */
66 if (ASN1_time_parse(gt
->data
, gt
->length
, &tm
,
67 V_ASN1_GENERALIZEDTIME
) == -1)
69 if ((*gt_time
= timegm(&tm
)) == -1)
75 tls_ocsp_fill_info(struct tls
*ctx
, int response_status
, int cert_status
,
76 int crl_reason
, ASN1_GENERALIZEDTIME
*revtime
,
77 ASN1_GENERALIZEDTIME
*thisupd
, ASN1_GENERALIZEDTIME
*nextupd
)
79 struct tls_ocsp_result
*info
= NULL
;
81 free(ctx
->ocsp
->ocsp_result
);
82 ctx
->ocsp
->ocsp_result
= NULL
;
84 if ((info
= calloc(1, sizeof (struct tls_ocsp_result
))) == NULL
) {
85 tls_set_error(ctx
, "calloc");
88 info
->response_status
= response_status
;
89 info
->cert_status
= cert_status
;
90 info
->crl_reason
= crl_reason
;
91 if (info
->response_status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
93 OCSP_response_status_str(info
->response_status
);
94 } else if (info
->cert_status
!= V_OCSP_CERTSTATUS_REVOKED
) {
95 info
->result_msg
= OCSP_cert_status_str(info
->cert_status
);
97 info
->result_msg
= OCSP_crl_reason_str(info
->crl_reason
);
99 info
->revocation_time
= info
->this_update
= info
->next_update
= -1;
100 if (revtime
!= NULL
&&
101 tls_ocsp_asn1_parse_time(ctx
, revtime
, &info
->revocation_time
) != 0) {
103 "unable to parse revocation time in OCSP reply");
106 if (thisupd
!= NULL
&&
107 tls_ocsp_asn1_parse_time(ctx
, thisupd
, &info
->this_update
) != 0) {
109 "unable to parse this update time in OCSP reply");
112 if (nextupd
!= NULL
&&
113 tls_ocsp_asn1_parse_time(ctx
, nextupd
, &info
->next_update
) != 0) {
115 "unable to parse next update time in OCSP reply");
118 ctx
->ocsp
->ocsp_result
= info
;
126 tls_ocsp_get_certid(X509
*main_cert
, STACK_OF(X509
) *extra_certs
,
129 X509_NAME
*issuer_name
;
131 X509_STORE_CTX storectx
;
133 OCSP_CERTID
*cid
= NULL
;
136 if ((issuer_name
= X509_get_issuer_name(main_cert
)) == NULL
)
139 if (extra_certs
!= NULL
) {
140 issuer
= X509_find_by_subject(extra_certs
, issuer_name
);
142 return OCSP_cert_to_id(NULL
, main_cert
, issuer
);
145 if ((store
= SSL_CTX_get_cert_store(ssl_ctx
)) == NULL
)
147 if (X509_STORE_CTX_init(&storectx
, store
, main_cert
, extra_certs
) != 1)
149 if (X509_STORE_get_by_subject(&storectx
, X509_LU_X509
, issuer_name
,
151 cid
= OCSP_cert_to_id(NULL
, main_cert
, tmpobj
.data
.x509
);
152 X509_OBJECT_free_contents(&tmpobj
);
154 X509_STORE_CTX_cleanup(&storectx
);
159 tls_ocsp_setup_from_peer(struct tls
*ctx
)
161 struct tls_ocsp
*ocsp
= NULL
;
162 STACK_OF(OPENSSL_STRING
) *ocsp_urls
= NULL
;
164 if ((ocsp
= tls_ocsp_new()) == NULL
)
167 /* steal state from ctx struct */
168 ocsp
->main_cert
= SSL_get_peer_certificate(ctx
->ssl_conn
);
169 ocsp
->extra_certs
= SSL_get_peer_cert_chain(ctx
->ssl_conn
);
170 if (ocsp
->main_cert
== NULL
) {
171 tls_set_errorx(ctx
, "no peer certificate for OCSP");
175 ocsp_urls
= X509_get1_ocsp(ocsp
->main_cert
);
176 if (ocsp_urls
== NULL
) {
177 tls_set_errorx(ctx
, "no OCSP URLs in peer certificate");
181 ocsp
->ocsp_url
= strdup(sk_OPENSSL_STRING_value(ocsp_urls
, 0));
182 if (ocsp
->ocsp_url
== NULL
) {
183 tls_set_errorx(ctx
, "out of memory");
187 X509_email_free(ocsp_urls
);
192 X509_email_free(ocsp_urls
);
197 tls_ocsp_verify_response(struct tls
*ctx
, OCSP_RESPONSE
*resp
)
199 OCSP_BASICRESP
*br
= NULL
;
200 ASN1_GENERALIZEDTIME
*revtime
= NULL
, *thisupd
= NULL
, *nextupd
= NULL
;
201 OCSP_CERTID
*cid
= NULL
;
202 STACK_OF(X509
) *combined
= NULL
;
203 int response_status
=0, cert_status
=0, crl_reason
=0;
207 if ((br
= OCSP_response_get1_basic(resp
)) == NULL
) {
208 tls_set_errorx(ctx
, "cannot load ocsp reply");
213 * Skip validation of 'extra_certs' as this should be done
214 * already as part of main handshake.
216 flags
= OCSP_TRUSTOTHER
;
219 if (OCSP_basic_verify(br
, ctx
->ocsp
->extra_certs
,
220 SSL_CTX_get_cert_store(ctx
->ssl_ctx
), flags
) != 1) {
221 tls_set_error(ctx
, "ocsp verify failed");
225 /* signature OK, look inside */
226 response_status
= OCSP_response_status(resp
);
227 if (response_status
!= OCSP_RESPONSE_STATUS_SUCCESSFUL
) {
228 tls_set_errorx(ctx
, "ocsp verify failed: response - %s",
229 OCSP_response_status_str(response_status
));
233 cid
= tls_ocsp_get_certid(ctx
->ocsp
->main_cert
,
234 ctx
->ocsp
->extra_certs
, ctx
->ssl_ctx
);
236 tls_set_errorx(ctx
, "ocsp verify failed: no issuer cert");
240 if (OCSP_resp_find_status(br
, cid
, &cert_status
, &crl_reason
,
241 &revtime
, &thisupd
, &nextupd
) != 1) {
242 tls_set_errorx(ctx
, "ocsp verify failed: no result for cert");
246 if (OCSP_check_validity(thisupd
, nextupd
, JITTER_SEC
,
249 "ocsp verify failed: ocsp response not current");
253 if (tls_ocsp_fill_info(ctx
, response_status
, cert_status
,
254 crl_reason
, revtime
, thisupd
, nextupd
) != 0)
257 /* finally can look at status */
258 if (cert_status
!= V_OCSP_CERTSTATUS_GOOD
&& cert_status
!=
259 V_OCSP_CERTSTATUS_UNKNOWN
) {
260 tls_set_errorx(ctx
, "ocsp verify failed: revoked cert - %s",
261 OCSP_crl_reason_str(crl_reason
));
267 sk_X509_free(combined
);
268 OCSP_CERTID_free(cid
);
269 OCSP_BASICRESP_free(br
);
274 * Process a raw OCSP response from an OCSP server request.
275 * OCSP details can then be retrieved with tls_peer_ocsp_* functions.
276 * returns 0 if certificate ok, -1 otherwise.
279 tls_ocsp_process_response_internal(struct tls
*ctx
, const unsigned char *response
,
285 resp
= d2i_OCSP_RESPONSE(NULL
, &response
, size
);
287 tls_ocsp_free(ctx
->ocsp
);
289 tls_set_error(ctx
, "unable to parse OCSP response");
292 ret
= tls_ocsp_verify_response(ctx
, resp
);
293 OCSP_RESPONSE_free(resp
);
297 /* TLS handshake verification callback for stapled requests */
299 tls_ocsp_verify_cb(SSL
*ssl
, void *arg
)
301 const unsigned char *raw
= NULL
;
305 if ((ctx
= SSL_get_app_data(ssl
)) == NULL
)
308 size
= SSL_get_tlsext_status_ocsp_resp(ssl
, &raw
);
310 if (ctx
->config
->ocsp_require_stapling
) {
311 tls_set_errorx(ctx
, "no stapled OCSP response provided");
317 tls_ocsp_free(ctx
->ocsp
);
318 if ((ctx
->ocsp
= tls_ocsp_setup_from_peer(ctx
)) == NULL
)
321 if (ctx
->config
->verify_cert
== 0 || ctx
->config
->verify_time
== 0)
324 res
= tls_ocsp_process_response_internal(ctx
, raw
, size
);
326 return (res
== 0) ? 1 : 0;
330 /* Staple the OCSP information in ctx->ocsp to the server handshake. */
332 tls_ocsp_stapling_cb(SSL
*ssl
, void *arg
)
334 int ret
= SSL_TLSEXT_ERR_ALERT_FATAL
;
335 unsigned char *ocsp_staple
= NULL
;
338 if ((ctx
= SSL_get_app_data(ssl
)) == NULL
)
341 if (ctx
->keypair
== NULL
|| ctx
->keypair
->ocsp_staple
== NULL
||
342 ctx
->keypair
->ocsp_staple_len
== 0)
343 return SSL_TLSEXT_ERR_NOACK
;
345 if ((ocsp_staple
= malloc(ctx
->keypair
->ocsp_staple_len
)) == NULL
)
348 memcpy(ocsp_staple
, ctx
->keypair
->ocsp_staple
,
349 ctx
->keypair
->ocsp_staple_len
);
351 if (SSL_set_tlsext_status_ocsp_resp(ctx
->ssl_conn
, ocsp_staple
,
352 ctx
->keypair
->ocsp_staple_len
) != 1)
355 ret
= SSL_TLSEXT_ERR_OK
;
357 if (ret
!= SSL_TLSEXT_ERR_OK
)
367 /* Retrieve OCSP URL from peer certificate, if present. */
369 tls_peer_ocsp_url(struct tls
*ctx
)
371 if (ctx
->ocsp
== NULL
)
373 return ctx
->ocsp
->ocsp_url
;
377 tls_peer_ocsp_result(struct tls
*ctx
)
379 if (ctx
->ocsp
== NULL
)
381 if (ctx
->ocsp
->ocsp_result
== NULL
)
383 return ctx
->ocsp
->ocsp_result
->result_msg
;
387 tls_peer_ocsp_response_status(struct tls
*ctx
)
389 if (ctx
->ocsp
== NULL
)
391 if (ctx
->ocsp
->ocsp_result
== NULL
)
393 return ctx
->ocsp
->ocsp_result
->response_status
;
397 tls_peer_ocsp_cert_status(struct tls
*ctx
)
399 if (ctx
->ocsp
== NULL
)
401 if (ctx
->ocsp
->ocsp_result
== NULL
)
403 return ctx
->ocsp
->ocsp_result
->cert_status
;
407 tls_peer_ocsp_crl_reason(struct tls
*ctx
)
409 if (ctx
->ocsp
== NULL
)
411 if (ctx
->ocsp
->ocsp_result
== NULL
)
413 return ctx
->ocsp
->ocsp_result
->crl_reason
;
417 tls_peer_ocsp_this_update(struct tls
*ctx
)
419 if (ctx
->ocsp
== NULL
)
421 if (ctx
->ocsp
->ocsp_result
== NULL
)
423 return ctx
->ocsp
->ocsp_result
->this_update
;
427 tls_peer_ocsp_next_update(struct tls
*ctx
)
429 if (ctx
->ocsp
== NULL
)
431 if (ctx
->ocsp
->ocsp_result
== NULL
)
433 return ctx
->ocsp
->ocsp_result
->next_update
;
437 tls_peer_ocsp_revocation_time(struct tls
*ctx
)
439 if (ctx
->ocsp
== NULL
)
441 if (ctx
->ocsp
->ocsp_result
== NULL
)
443 return ctx
->ocsp
->ocsp_result
->revocation_time
;
447 tls_ocsp_process_response(struct tls
*ctx
, const unsigned char *response
,
450 if ((ctx
->state
& TLS_HANDSHAKE_COMPLETE
) == 0)
452 return tls_ocsp_process_response_internal(ctx
, response
, size
);