| 1 |
/* |
| 2 |
* stunnel TLS offloading and load-balancing proxy |
| 3 |
* Copyright (C) 1998-2026 Michal Trojnara <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the |
| 7 |
* Free Software Foundation; either version 2 of the License, or (at your |
| 8 |
* option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, see <http://www.gnu.org/licenses>. |
| 17 |
* |
| 18 |
* Linking stunnel statically or dynamically with other modules is making |
| 19 |
* a combined work based on stunnel. Thus, the terms and conditions of |
| 20 |
* the GNU General Public License cover the whole combination. |
| 21 |
* |
| 22 |
* In addition, as a special exception, the copyright holder of stunnel |
| 23 |
* gives you permission to combine stunnel with free software programs or |
| 24 |
* libraries that are released under the GNU LGPL and with code included |
| 25 |
* in the standard release of OpenSSL under the OpenSSL License (or |
| 26 |
* modified versions of such code, with unchanged license). You may copy |
| 27 |
* and distribute such a system following the terms of the GNU GPL for |
| 28 |
* stunnel and the licenses of the other code concerned. |
| 29 |
* |
| 30 |
* Note that people who make modified versions of stunnel are not obligated |
| 31 |
* to grant this special exception for their modified versions; it is their |
| 32 |
* choice whether to do so. The GNU General Public License gives permission |
| 33 |
* to release a modified version without this exception; this exception |
| 34 |
* also makes it possible to release a modified version which carries |
| 35 |
* forward this exception. |
| 36 |
*/ |
| 37 |
|
| 38 |
#include "prototypes.h" |
| 39 |
|
| 40 |
#ifndef OPENSSL_NO_OCSP |
| 41 |
|
| 42 |
#define INVALID_TIME ((time_t)-1) |
| 43 |
#ifdef DEFINE_STACK_OF |
| 44 |
/* defined in openssl/safestack.h: |
| 45 |
* DEFINE_SPECIAL_STACK_OF(OPENSSL_STRING, char) */ |
| 46 |
#else /* DEFINE_STACK_OF */ |
| 47 |
#ifndef sk_OPENSSL_STRING_num |
| 48 |
#define sk_OPENSSL_STRING_num(st) sk_num(st) |
| 49 |
#endif /* sk_OPENSSL_STRING_num */ |
| 50 |
#ifndef sk_OPENSSL_STRING_value |
| 51 |
#define sk_OPENSSL_STRING_value(st, i) sk_value((st),(i)) |
| 52 |
#endif /* sk_OPENSSL_STRING_value */ |
| 53 |
#endif /* DEFINE_STACK_OF */ |
| 54 |
|
| 55 |
typedef struct { |
| 56 |
/* OCSP request and validation parameters */ |
| 57 |
int depth; |
| 58 |
int use_nonce; /* include nonce in OCSP requests */ |
| 59 |
int use_aia; /* use AuthorityInfoAccess to fetch the responder URL */ |
| 60 |
long leeway; |
| 61 |
unsigned long flags; |
| 62 |
char *url; |
| 63 |
STACK_OF(X509) *chain_to_verify; |
| 64 |
X509 *root_ca; |
| 65 |
OCSP_CERTID *cert_id; |
| 66 |
|
| 67 |
/* OCSP validation results */ |
| 68 |
int source_found; /* found either a conclusive stapling or a responder */ |
| 69 |
int callback_ctx_error; |
| 70 |
|
| 71 |
/* OCSP single request and result */ |
| 72 |
OCSP_REQUEST *request; |
| 73 |
OCSP_RESPONSE *response; |
| 74 |
ASN1_GENERALIZEDTIME *revoked_at, *this_update, *next_update; |
| 75 |
} OCSP_CTX; |
| 76 |
|
| 77 |
/**************************************** OCSP stapling callbacks */ |
| 78 |
|
| 79 |
NOEXPORT int ocsp_client_cb(SSL *, void *); |
| 80 |
#if OPENSSL_VERSION_NUMBER>=0x10002000L |
| 81 |
NOEXPORT int ocsp_server_cb(SSL *, void *); |
| 82 |
NOEXPORT int ocsp_set_stapling_response(CLI *); |
| 83 |
#endif /* OpenSSL version 1.0.2 or later */ |
| 84 |
|
| 85 |
/**************************************** OCSP utility functions */ |
| 86 |
|
| 87 |
NOEXPORT void ocsp_ctx_free(OCSP_CTX *); |
| 88 |
NOEXPORT void ocsp_ctx_cleanup(OCSP_CTX *); |
| 89 |
NOEXPORT int ocsp_verify(CLI *, OCSP_CTX *); |
| 90 |
NOEXPORT int check_aia(CLI *, SERVICE_OPTIONS *, OCSP_CTX *); |
| 91 |
NOEXPORT int ocsp_request(CLI *c, SERVICE_OPTIONS *, OCSP_CTX *); |
| 92 |
NOEXPORT int ocsp_get_response(SERVICE_OPTIONS *, OCSP_CTX *); |
| 93 |
NOEXPORT int ocsp_response_validate(CLI *, SERVICE_OPTIONS *, OCSP_CTX *); |
| 94 |
NOEXPORT void ocsp_ctx_setup_cert_id(OCSP_CTX *); |
| 95 |
NOEXPORT int ocsp_ctx_append_root_ca(SERVICE_OPTIONS *, OCSP_CTX *); |
| 96 |
NOEXPORT void log_time(const int, const char *, ASN1_GENERALIZEDTIME *); |
| 97 |
#if OPENSSL_VERSION_NUMBER>=0x10101000L |
| 98 |
NOEXPORT time_t time_t_get_asn1_time(const ASN1_TIME *); |
| 99 |
#endif /* OpenSSL version 1.1.1 or later */ |
| 100 |
|
| 101 |
/**************************************** OCSP initialization */ |
| 102 |
|
| 103 |
int ocsp_init(SERVICE_OPTIONS *section) { |
| 104 |
section->ocsp_response_lock=CRYPTO_THREAD_lock_new(); |
| 105 |
if(section->option.client) { |
| 106 |
if(!SSL_CTX_set_tlsext_status_cb(section->ctx, ocsp_client_cb)) { |
| 107 |
ssl_error(NULL, "OCSP: SSL_CTX_set_tlsext_status_cb"); |
| 108 |
return 1; /* FAILED */ |
| 109 |
} |
| 110 |
s_log(LOG_DEBUG, "OCSP: Client OCSP stapling enabled"); |
| 111 |
} else { |
| 112 |
#if OPENSSL_VERSION_NUMBER>=0x10002000L |
| 113 |
#ifndef OPENSSL_NO_PSK |
| 114 |
if(!section->psk_keys) { |
| 115 |
#endif /* !defined(OPENSSL_NO_PSK) */ |
| 116 |
if(SSL_CTX_set_tlsext_status_cb(section->ctx, ocsp_server_cb)) { |
| 117 |
ocsp_stapling(section); |
| 118 |
s_log(LOG_DEBUG, "OCSP: Server OCSP stapling enabled"); |
| 119 |
} else { |
| 120 |
s_log(LOG_NOTICE, "OCSP: Server OCSP stapling not supported"); |
| 121 |
} |
| 122 |
#ifndef OPENSSL_NO_PSK |
| 123 |
} else { |
| 124 |
s_log(LOG_NOTICE, "OCSP: Server OCSP stapling is incompatible with PSK"); |
| 125 |
} |
| 126 |
#endif /* !defined(OPENSSL_NO_PSK) */ |
| 127 |
#else /* OpenSSL version 1.0.2 or later */ |
| 128 |
s_log(LOG_NOTICE, "OCSP: Server OCSP stapling not supported"); |
| 129 |
#endif /* OpenSSL version 1.0.2 or later */ |
| 130 |
} |
| 131 |
|
| 132 |
return 0; /* OK */ |
| 133 |
} |
| 134 |
|
| 135 |
/* free all of the OCSP_CTX values */ |
| 136 |
NOEXPORT void ocsp_ctx_free(OCSP_CTX *ocsp) { |
| 137 |
ocsp_ctx_cleanup(ocsp); |
| 138 |
if(ocsp->chain_to_verify) { |
| 139 |
sk_X509_free(ocsp->chain_to_verify); |
| 140 |
ocsp->chain_to_verify=NULL; |
| 141 |
} |
| 142 |
if(ocsp->root_ca) { |
| 143 |
X509_free(ocsp->root_ca); |
| 144 |
ocsp->root_ca=NULL; |
| 145 |
} |
| 146 |
if(ocsp->cert_id) { |
| 147 |
OCSP_CERTID_free(ocsp->cert_id); |
| 148 |
ocsp->cert_id=NULL; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* free the OCSP_CTX values required to reuse it for a next request */ |
| 153 |
NOEXPORT void ocsp_ctx_cleanup(OCSP_CTX *ocsp) { |
| 154 |
if(ocsp->response) { |
| 155 |
OCSP_RESPONSE_free(ocsp->response); |
| 156 |
ocsp->response=NULL; |
| 157 |
} |
| 158 |
if(ocsp->request) { |
| 159 |
OCSP_REQUEST_free(ocsp->request); |
| 160 |
ocsp->request=NULL; |
| 161 |
} |
| 162 |
ocsp->revoked_at=NULL; |
| 163 |
ocsp->this_update=NULL; |
| 164 |
ocsp->next_update=NULL; |
| 165 |
} |
| 166 |
|
| 167 |
/**************************************** OCSP cleanup */ |
| 168 |
|
| 169 |
void ocsp_cleanup(SERVICE_OPTIONS *section) { |
| 170 |
if(section->ocsp_response_len) { |
| 171 |
str_free(section->ocsp_response_der); |
| 172 |
section->ocsp_response_len=0; |
| 173 |
} |
| 174 |
if(section->ocsp_response_lock) |
| 175 |
CRYPTO_THREAD_lock_free(section->ocsp_response_lock); |
| 176 |
} |
| 177 |
|
| 178 |
/**************************************** OCSP verify.c callback */ |
| 179 |
|
| 180 |
int ocsp_check(CLI *c, X509_STORE_CTX *callback_ctx) { |
| 181 |
OCSP_CTX ocsp; |
| 182 |
int ret=0; /* failed */ |
| 183 |
|
| 184 |
/* initial checks */ |
| 185 |
if(!c->opt->option.verify_chain) { |
| 186 |
s_log(LOG_INFO, "OCSP: Certificate chain verification disabled"); |
| 187 |
return 1; /* accept */ |
| 188 |
} |
| 189 |
if(c->opt->option.client && |
| 190 |
!X509_STORE_CTX_get_error_depth(callback_ctx) && |
| 191 |
!c->opt->stapling_cb_flag) { |
| 192 |
/* for client peer certificate verification, |
| 193 |
* tlsext_status_ocsp_resp is needed for oscp_verify_ssl() */ |
| 194 |
c->opt->verify_cb_flag=1; |
| 195 |
/* ocsp_verify() will be invoked from ocsp_client_cb() */ |
| 196 |
s_log(LOG_DEBUG, "OCSP: Waiting for OCSP stapling response"); |
| 197 |
return 1; /* accept */ |
| 198 |
} |
| 199 |
|
| 200 |
/* initialize the OCSP_CTX structure */ |
| 201 |
memset(&ocsp, 0, sizeof(OCSP_CTX)); |
| 202 |
ocsp.depth=X509_STORE_CTX_get_error_depth(callback_ctx); |
| 203 |
ocsp.use_nonce=c->opt->option.nonce; |
| 204 |
ocsp.use_aia=c->opt->option.aia; |
| 205 |
ocsp.leeway=60; /* allow for one minute leeway */ |
| 206 |
ocsp.flags=c->opt->ocsp_flags; |
| 207 |
ocsp.url=c->opt->ocsp_url; |
| 208 |
ocsp.source_found=0; |
| 209 |
ocsp.callback_ctx_error=X509_V_ERR_APPLICATION_VERIFICATION; |
| 210 |
|
| 211 |
/* get the client certificate chain */ |
| 212 |
ocsp.chain_to_verify=sk_X509_dup(X509_STORE_CTX_get0_chain(callback_ctx)); |
| 213 |
if(!ocsp.chain_to_verify) { |
| 214 |
s_log(LOG_ERR, "OCSP: sk_X509_dup"); |
| 215 |
goto cleanup; |
| 216 |
} |
| 217 |
ocsp_ctx_append_root_ca(c->opt, &ocsp); /* ignore failures */ |
| 218 |
|
| 219 |
ret=ocsp_verify(c, &ocsp); |
| 220 |
|
| 221 |
cleanup: |
| 222 |
if(!ret) |
| 223 |
X509_STORE_CTX_set_error(callback_ctx, ocsp.callback_ctx_error); |
| 224 |
ocsp_ctx_free(&ocsp); |
| 225 |
return ret; |
| 226 |
} |
| 227 |
|
| 228 |
/**************************************** OCSP stapling client callback */ |
| 229 |
|
| 230 |
/* |
| 231 |
* Returns 0 if the response is not acceptable (the handshake will fail) |
| 232 |
* or 1 if it is acceptable. |
| 233 |
*/ |
| 234 |
NOEXPORT int ocsp_client_cb(SSL *ssl, void *arg) { |
| 235 |
CLI *c; |
| 236 |
OCSP_CTX ocsp; |
| 237 |
int ret=0; /* failed */ |
| 238 |
|
| 239 |
(void)arg; /* squash the unused parameter warning */ |
| 240 |
s_log(LOG_DEBUG, "OCSP stapling: Client callback called"); |
| 241 |
|
| 242 |
c=SSL_get_ex_data(ssl, index_ssl_cli); |
| 243 |
|
| 244 |
/* initial checks */ |
| 245 |
if(!c->opt->option.verify_chain) { |
| 246 |
s_log(LOG_INFO, "OCSP: Certificate chain verification disabled"); |
| 247 |
return 1; /* accept */ |
| 248 |
} |
| 249 |
if(SSL_session_reused(ssl)) { |
| 250 |
s_log(LOG_DEBUG, "OCSP: Skipped OCSP stapling (previous session reused)"); |
| 251 |
return 1; /* accept: there is nothing we can do at session resumption */ |
| 252 |
} |
| 253 |
if(!c->opt->option.client) { /* just in case */ |
| 254 |
s_log(LOG_DEBUG, "OCSP: Client callback ignored on a server"); |
| 255 |
return 1; /* accept */ |
| 256 |
} |
| 257 |
if(!c->opt->verify_cb_flag) { |
| 258 |
/* for client peer certificate verification, |
| 259 |
* peer certificates are needed for oscp_verify_ssl() */ |
| 260 |
c->opt->stapling_cb_flag=1; |
| 261 |
/* ocsp_verify() will be invoked from ocsp_check() */ |
| 262 |
s_log(LOG_DEBUG, "OCSP: Waiting for OCSP peer certificates"); |
| 263 |
return 1; /* accept */ |
| 264 |
} |
| 265 |
|
| 266 |
/* initialize the OCSP_CTX structure */ |
| 267 |
memset(&ocsp, 0, sizeof(OCSP_CTX)); |
| 268 |
ocsp.depth=0; /* peer (leaf) certificate */ |
| 269 |
ocsp.use_nonce=c->opt->option.nonce; |
| 270 |
ocsp.use_aia=c->opt->option.aia; |
| 271 |
ocsp.leeway=60; /* allow for one minute leeway */ |
| 272 |
ocsp.flags=c->opt->ocsp_flags; |
| 273 |
ocsp.url=c->opt->ocsp_url; |
| 274 |
ocsp.source_found=0; |
| 275 |
ocsp.callback_ctx_error=0; |
| 276 |
|
| 277 |
/* get the client certificate chain */ |
| 278 |
ocsp.chain_to_verify=sk_X509_dup(SSL_get_peer_cert_chain(ssl)); |
| 279 |
if(!ocsp.chain_to_verify) { |
| 280 |
s_log(LOG_ERR, "OCSP: sk_X509_dup"); |
| 281 |
goto cleanup; |
| 282 |
} |
| 283 |
ocsp_ctx_append_root_ca(c->opt, &ocsp); /* ignore failures */ |
| 284 |
ret=ocsp_verify(c, &ocsp); |
| 285 |
|
| 286 |
cleanup: |
| 287 |
ocsp_ctx_free(&ocsp); |
| 288 |
return ret; |
| 289 |
} |
| 290 |
|
| 291 |
/**************************************** OCSP stapling server callback */ |
| 292 |
|
| 293 |
#if OPENSSL_VERSION_NUMBER>=0x10002000L |
| 294 |
|
| 295 |
/* |
| 296 |
* This is called when a client includes a certificate status request extension. |
| 297 |
* The response is either obtained from a cache, or from an OCSP responder. |
| 298 |
* Returns one of: |
| 299 |
* SSL_TLSEXT_ERR_OK - the OCSP response that has been set should be returned |
| 300 |
* SSL_TLSEXT_ERR_NOACK - the OCSP response should not be returned |
| 301 |
* SSL_TLSEXT_ERR_ALERT_FATAL - a fatal error has occurred |
| 302 |
*/ |
| 303 |
NOEXPORT int ocsp_server_cb(SSL *ssl, void *arg) { |
| 304 |
CLI *c; |
| 305 |
int ret; |
| 306 |
|
| 307 |
(void)arg; /* squash the unused parameter warning */ |
| 308 |
s_log(LOG_DEBUG, "OCSP stapling: Server callback called"); |
| 309 |
|
| 310 |
c=SSL_get_ex_data(ssl, index_ssl_cli); |
| 311 |
#ifdef USE_OS_THREADS /* use the stapling cached by a cron thread */ |
| 312 |
ret=ocsp_set_stapling_response(c); |
| 313 |
#else /* attempt to fetch a stapling each time */ |
| 314 |
ret=ocsp_stapling(c->opt); |
| 315 |
if(ret==SSL_TLSEXT_ERR_OK) |
| 316 |
ret=ocsp_set_stapling_response(c); |
| 317 |
#endif /* USE_OS_THREADS */ |
| 318 |
return ret; |
| 319 |
} |
| 320 |
|
| 321 |
/* |
| 322 |
* Validate the stapling cache and update it if needed. |
| 323 |
* Returns one of: |
| 324 |
* SSL_TLSEXT_ERR_OK - the OCSP response that has been set should be returned |
| 325 |
* SSL_TLSEXT_ERR_NOACK - the OCSP response should not be returned |
| 326 |
* SSL_TLSEXT_ERR_ALERT_FATAL - a fatal error has occurred |
| 327 |
*/ |
| 328 |
int ocsp_stapling(SERVICE_OPTIONS *opt) { |
| 329 |
OCSP_CTX ocsp; |
| 330 |
X509 *cert; |
| 331 |
STACK_OF(X509) *chain=NULL; |
| 332 |
unsigned char *response_der=NULL; |
| 333 |
const unsigned char *response_tmp; |
| 334 |
int response_len=0, ret=SSL_TLSEXT_ERR_ALERT_FATAL; |
| 335 |
int ocsp_status=V_OCSP_CERTSTATUS_UNKNOWN; |
| 336 |
|
| 337 |
/* initialize the OCSP_CTX structure */ |
| 338 |
memset(&ocsp, 0, sizeof(OCSP_CTX)); |
| 339 |
ocsp.depth=0; /* peer (leaf) certificate */ |
| 340 |
ocsp.use_nonce=0; /* disable nonce */ |
| 341 |
ocsp.use_aia=1; /* enable AIA */ |
| 342 |
ocsp.leeway=30; /* allow for 30 second leeway */ |
| 343 |
/* OCSP_basic_verify() returns success if the signer certificate |
| 344 |
* was found in a set of untrusted intermediate certificates */ |
| 345 |
ocsp.flags=OCSP_TRUSTOTHER; |
| 346 |
ocsp.url=NULL; /* to be set in check_aia() */ |
| 347 |
ocsp.source_found=0; |
| 348 |
ocsp.callback_ctx_error=0; |
| 349 |
|
| 350 |
/* get the server certificate chain */ |
| 351 |
cert=SSL_CTX_get0_certificate(opt->ctx); |
| 352 |
if(!cert) { |
| 353 |
s_log(LOG_ERR, "OCSP: SSL_get_certificate"); |
| 354 |
goto cleanup; |
| 355 |
} |
| 356 |
if(!SSL_CTX_get0_chain_certs(opt->ctx, &chain)) { |
| 357 |
s_log(LOG_ERR, "OCSP: SSL_CTX_get0_chain_certs"); |
| 358 |
goto cleanup; |
| 359 |
} |
| 360 |
if(chain) { |
| 361 |
ocsp.chain_to_verify=sk_X509_dup(chain); |
| 362 |
if(!ocsp.chain_to_verify) { |
| 363 |
s_log(LOG_ERR, "OCSP: sk_X509_dup"); |
| 364 |
goto cleanup; |
| 365 |
} |
| 366 |
} else { |
| 367 |
ocsp.chain_to_verify=sk_X509_new_null(); |
| 368 |
if(!ocsp.chain_to_verify) { |
| 369 |
s_log(LOG_ERR, "OCSP: sk_X509_new_null"); |
| 370 |
goto cleanup; |
| 371 |
} |
| 372 |
} |
| 373 |
/* insert the server certificate into the chain */ |
| 374 |
if (!sk_X509_unshift(ocsp.chain_to_verify, cert)) { |
| 375 |
s_log(LOG_ERR, "OCSP: sk_X509_unshift"); |
| 376 |
goto cleanup; |
| 377 |
} |
| 378 |
ocsp_ctx_append_root_ca(opt, &ocsp); /* ignore failures */ |
| 379 |
|
| 380 |
/* retrieve the cached response */ |
| 381 |
CRYPTO_THREAD_read_lock(opt->ocsp_response_lock); |
| 382 |
if(opt->ocsp_response_len) { |
| 383 |
response_len=opt->ocsp_response_len; |
| 384 |
response_der=str_alloc((size_t)response_len); |
| 385 |
memcpy(response_der, opt->ocsp_response_der, (size_t)response_len); |
| 386 |
} |
| 387 |
CRYPTO_THREAD_unlock(opt->ocsp_response_lock); |
| 388 |
|
| 389 |
if(response_len) { /* found a cached response */ |
| 390 |
/* decode */ |
| 391 |
response_tmp=response_der; |
| 392 |
ocsp.response=d2i_OCSP_RESPONSE(NULL, &response_tmp, response_len); |
| 393 |
|
| 394 |
/* validate */ |
| 395 |
ocsp_status=ocsp_response_validate(NULL, opt, &ocsp); |
| 396 |
|
| 397 |
/* cleanup */ |
| 398 |
ERR_clear_error(); /* silence any cached errors */ |
| 399 |
if(response_der) { |
| 400 |
str_free(response_der); |
| 401 |
response_der=NULL; |
| 402 |
} |
| 403 |
response_len=0; |
| 404 |
|
| 405 |
/* skip fetching if we have a conclusive status */ |
| 406 |
if(ocsp_status!=V_OCSP_CERTSTATUS_UNKNOWN) { |
| 407 |
s_log(LOG_DEBUG, "OCSP: Use the cached OCSP response"); |
| 408 |
ret=SSL_TLSEXT_ERR_OK; |
| 409 |
goto cleanup; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
/* invalidate the cache */ |
| 414 |
CRYPTO_THREAD_write_lock(opt->ocsp_response_lock); |
| 415 |
if(opt->ocsp_response_len) { |
| 416 |
opt->ocsp_response_len=0; |
| 417 |
str_free(opt->ocsp_response_der); |
| 418 |
opt->ocsp_response_der=NULL; |
| 419 |
} |
| 420 |
CRYPTO_THREAD_unlock(opt->ocsp_response_lock); |
| 421 |
|
| 422 |
/* try fetching response from the OCSP responder */ |
| 423 |
ocsp_status=check_aia(NULL, opt, &ocsp); |
| 424 |
if(ocsp_status==V_OCSP_CERTSTATUS_UNKNOWN) { /* no useful response */ |
| 425 |
s_log(LOG_INFO, "OCSP: No OCSP stapling response to send"); |
| 426 |
ret=SSL_TLSEXT_ERR_NOACK; |
| 427 |
goto cleanup; |
| 428 |
} |
| 429 |
|
| 430 |
/* encode */ |
| 431 |
response_len=i2d_OCSP_RESPONSE(ocsp.response, &response_der); |
| 432 |
|
| 433 |
/* update the cache */ |
| 434 |
if(ocsp.next_update) { |
| 435 |
/* cache the newly fetched OCSP response */ |
| 436 |
CRYPTO_THREAD_write_lock(opt->ocsp_response_lock); |
| 437 |
opt->ocsp_response_len=response_len; |
| 438 |
opt->ocsp_response_der=str_alloc_detached((size_t)response_len); |
| 439 |
memcpy(opt->ocsp_response_der, response_der, (size_t)response_len); |
| 440 |
CRYPTO_THREAD_unlock(opt->ocsp_response_lock); |
| 441 |
s_log(LOG_DEBUG, "OCSP: Response cached"); |
| 442 |
} |
| 443 |
|
| 444 |
OPENSSL_free(response_der); |
| 445 |
ret=SSL_TLSEXT_ERR_OK; |
| 446 |
|
| 447 |
cleanup: |
| 448 |
ocsp_ctx_free(&ocsp); |
| 449 |
return ret; |
| 450 |
} |
| 451 |
|
| 452 |
/* Set the stapling response based on our cache. |
| 453 |
* Returns one of: |
| 454 |
* SSL_TLSEXT_ERR_OK - the OCSP response that has been set should be returned |
| 455 |
* SSL_TLSEXT_ERR_NOACK - the OCSP response should not be returned |
| 456 |
* SSL_TLSEXT_ERR_ALERT_FATAL - a fatal error has occurred |
| 457 |
*/ |
| 458 |
NOEXPORT int ocsp_set_stapling_response(CLI *c) { |
| 459 |
int ret=SSL_TLSEXT_ERR_NOACK; |
| 460 |
|
| 461 |
if(!c->opt->ocsp_response_len) /* performance optimization */ |
| 462 |
return ret; /* return without locking */ |
| 463 |
|
| 464 |
CRYPTO_THREAD_read_lock(c->opt->ocsp_response_lock); |
| 465 |
if(c->opt->ocsp_response_len) { |
| 466 |
unsigned char *response_der= |
| 467 |
OPENSSL_malloc((size_t)c->opt->ocsp_response_len); |
| 468 |
|
| 469 |
memcpy(response_der, c->opt->ocsp_response_der, |
| 470 |
(size_t)c->opt->ocsp_response_len); |
| 471 |
/* SSL_set_tlsext_status_ocsp_resp requires *us* to allocate the |
| 472 |
* response with OPENSSL_malloc(), but it will free it for us */ |
| 473 |
SSL_set_tlsext_status_ocsp_resp(c->ssl, |
| 474 |
response_der, c->opt->ocsp_response_len); |
| 475 |
ret=SSL_TLSEXT_ERR_OK; |
| 476 |
} |
| 477 |
CRYPTO_THREAD_unlock(c->opt->ocsp_response_lock); |
| 478 |
if(ret==SSL_TLSEXT_ERR_OK) |
| 479 |
s_log(LOG_DEBUG, "OCSP stapling: OCSP response sent back"); |
| 480 |
return ret; |
| 481 |
} |
| 482 |
|
| 483 |
#endif /* OpenSSL version 1.0.2 or later */ |
| 484 |
|
| 485 |
/**************************************** OCSP utility functions */ |
| 486 |
|
| 487 |
/* |
| 488 |
* Issue an OCSP client-driven request and the validate response. |
| 489 |
* Returns the error code of X509_STORE_CTX. |
| 490 |
* Returns 0 if the response is not acceptable (the handshake will fail) |
| 491 |
* or 1 if it is acceptable. |
| 492 |
*/ |
| 493 |
NOEXPORT int ocsp_verify(CLI *c, OCSP_CTX *ocsp) { |
| 494 |
int ocsp_status=V_OCSP_CERTSTATUS_UNKNOWN; |
| 495 |
|
| 496 |
/* ignoring the root certificate */ |
| 497 |
if(ocsp->depth==sk_X509_num(ocsp->chain_to_verify)-1) { |
| 498 |
s_log(LOG_DEBUG, "OCSP: Ignoring the root certificate"); |
| 499 |
return 1; /* accept */ |
| 500 |
} |
| 501 |
|
| 502 |
if(!ocsp->depth) { /* peer (leaf) certificate */ |
| 503 |
const unsigned char *resp_der; |
| 504 |
long resp_der_len; |
| 505 |
|
| 506 |
if(c->opt->option.client) { /* no stapling on the server */ |
| 507 |
/* process the stapling response if available */ |
| 508 |
resp_der_len=SSL_get_tlsext_status_ocsp_resp(c->ssl, &resp_der); |
| 509 |
if(resp_der_len>0 && resp_der) { |
| 510 |
s_log(LOG_INFO, "OCSP: OCSP stapling response received"); |
| 511 |
ocsp->response=d2i_OCSP_RESPONSE(NULL, &resp_der, resp_der_len); |
| 512 |
/* validate */ |
| 513 |
ocsp_status=ocsp_response_validate(c, c->opt, ocsp); |
| 514 |
if(ocsp_status!=V_OCSP_CERTSTATUS_UNKNOWN) |
| 515 |
ocsp->source_found=1; /* conclusive stapling found */ |
| 516 |
} else { |
| 517 |
s_log(LOG_INFO, "OCSP: No OCSP stapling response received"); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if(ocsp_status==V_OCSP_CERTSTATUS_UNKNOWN && ocsp->url) { |
| 522 |
/* ocsp_request() from a statically configured responder URL */ |
| 523 |
s_log(LOG_NOTICE, "OCSP: Connecting the configured responder \"%s\"", |
| 524 |
ocsp->url); |
| 525 |
ocsp_status=ocsp_request(c, c->opt, ocsp); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
if(ocsp_status==V_OCSP_CERTSTATUS_UNKNOWN) |
| 530 |
/* ocsp_request() from AIA responders defined in the certificate */ |
| 531 |
ocsp_status=check_aia(c, c->opt, ocsp); |
| 532 |
|
| 533 |
if(!ocsp->source_found) /* to conclusive stapling or ocsp_request() */ |
| 534 |
return 1; /* accept */ |
| 535 |
if(ocsp_status==V_OCSP_CERTSTATUS_GOOD) { |
| 536 |
s_log(LOG_NOTICE, "OCSP: Accepted (good)"); |
| 537 |
return 1; /* accept */ |
| 538 |
} |
| 539 |
if(ocsp_status==V_OCSP_CERTSTATUS_REVOKED) { |
| 540 |
s_log(LOG_ERR, "OCSP: Rejected (revoked)"); |
| 541 |
return 0; /* reject */ |
| 542 |
} |
| 543 |
if(c->opt->option.ocsp_require) { |
| 544 |
s_log(LOG_ERR, "OCSP: Rejected (OCSPrequire = yes)"); |
| 545 |
return 0; /* reject */ |
| 546 |
} |
| 547 |
s_log(LOG_NOTICE, "OCSP: Accepted (OCSPrequire = no)"); |
| 548 |
return 1; /* accept */ |
| 549 |
} |
| 550 |
|
| 551 |
/* |
| 552 |
* OCSP AIA checks |
| 553 |
* Returns one of: |
| 554 |
* - V_OCSP_CERTSTATUS_GOOD |
| 555 |
* - V_OCSP_CERTSTATUS_REVOKED |
| 556 |
* - V_OCSP_CERTSTATUS_UNKNOWN |
| 557 |
*/ |
| 558 |
NOEXPORT int check_aia(CLI *c, SERVICE_OPTIONS *opt, OCSP_CTX *ocsp) { |
| 559 |
int ocsp_status=V_OCSP_CERTSTATUS_UNKNOWN; |
| 560 |
STACK_OF(OPENSSL_STRING) *aia; |
| 561 |
int i, num; |
| 562 |
|
| 563 |
if(!ocsp->use_aia) |
| 564 |
goto cleanup; |
| 565 |
aia=X509_get1_ocsp(sk_X509_value(ocsp->chain_to_verify, ocsp->depth)); |
| 566 |
if(!aia) { |
| 567 |
s_log(LOG_INFO, "OCSP: No AIA responder URL"); |
| 568 |
goto cleanup; |
| 569 |
} |
| 570 |
num=sk_OPENSSL_STRING_num(aia); |
| 571 |
if(!num) { |
| 572 |
s_log(LOG_INFO, "OCSP: Empty AIA responder URL list"); |
| 573 |
goto cleanup; |
| 574 |
} |
| 575 |
for(i=0; i<num; i++) { |
| 576 |
ocsp->url=sk_OPENSSL_STRING_value(aia, i); |
| 577 |
s_log(LOG_NOTICE, "OCSP: Connecting the AIA responder \"%s\"", ocsp->url); |
| 578 |
ocsp_status=ocsp_request(c, opt, ocsp); |
| 579 |
if(ocsp_status!=V_OCSP_CERTSTATUS_UNKNOWN) |
| 580 |
break; /* we received a definitive response */ |
| 581 |
} |
| 582 |
X509_email_free(aia); |
| 583 |
|
| 584 |
cleanup: |
| 585 |
return ocsp_status; |
| 586 |
} |
| 587 |
|
| 588 |
/* |
| 589 |
* OCSP request handling. |
| 590 |
* Returns one of: |
| 591 |
* - V_OCSP_CERTSTATUS_GOOD |
| 592 |
* - V_OCSP_CERTSTATUS_REVOKED |
| 593 |
* - V_OCSP_CERTSTATUS_UNKNOWN |
| 594 |
*/ |
| 595 |
NOEXPORT int ocsp_request(CLI *c, SERVICE_OPTIONS *opt, OCSP_CTX *ocsp) { |
| 596 |
int ocsp_status=V_OCSP_CERTSTATUS_UNKNOWN; |
| 597 |
|
| 598 |
/* prepare params for reuse */ |
| 599 |
ocsp_ctx_cleanup(ocsp); |
| 600 |
|
| 601 |
/* build request */ |
| 602 |
ocsp->source_found=1; /* ether AIA or a configured responder */ |
| 603 |
ocsp->request=OCSP_REQUEST_new(); |
| 604 |
if(!ocsp->request) { |
| 605 |
ssl_error(c, "OCSP: OCSP_REQUEST_new"); |
| 606 |
goto cleanup; |
| 607 |
} |
| 608 |
ocsp_ctx_setup_cert_id(ocsp); |
| 609 |
if(!ocsp->cert_id) |
| 610 |
goto cleanup; |
| 611 |
if(!OCSP_request_add0_id(ocsp->request, |
| 612 |
OCSP_CERTID_dup(ocsp->cert_id))) { |
| 613 |
ssl_error(c, "OCSP: OCSP_request_add0_id"); |
| 614 |
goto cleanup; |
| 615 |
} |
| 616 |
if(ocsp->use_nonce) { |
| 617 |
OCSP_request_add1_nonce(ocsp->request, NULL, -1); |
| 618 |
} |
| 619 |
|
| 620 |
/* send the request and get a response */ |
| 621 |
if(!ocsp_get_response(opt, ocsp)) { |
| 622 |
goto cleanup; |
| 623 |
} |
| 624 |
|
| 625 |
/* validate */ |
| 626 |
ocsp_status=ocsp_response_validate(NULL, opt, ocsp); |
| 627 |
if(ocsp_status==V_OCSP_CERTSTATUS_REVOKED) |
| 628 |
ocsp->callback_ctx_error=X509_V_ERR_CERT_REVOKED; |
| 629 |
|
| 630 |
cleanup: |
| 631 |
return ocsp_status; |
| 632 |
} |
| 633 |
|
| 634 |
#if defined(__GNUC__) && !defined(__clang__) |
| 635 |
#pragma GCC diagnostic push |
| 636 |
#pragma GCC diagnostic ignored "-Wclobbered" |
| 637 |
#endif |
| 638 |
/* |
| 639 |
* Send the OCSP request over HTTP and read the responder's reply. |
| 640 |
* A lightweight CLI structure is created only for socket handling. |
| 641 |
*/ |
| 642 |
NOEXPORT int ocsp_get_response(SERVICE_OPTIONS *opt, OCSP_CTX *ocsp) { |
| 643 |
BIO *bio=NULL; |
| 644 |
OCSP_REQ_CTX *req_ctx=NULL; |
| 645 |
char *host=NULL, *port=NULL, *path=NULL; |
| 646 |
SOCKADDR_UNION addr; |
| 647 |
int ssl, ret=0; |
| 648 |
CLI *c; /* TODO */ |
| 649 |
jmp_buf exception_buffer; /* TODO */ |
| 650 |
|
| 651 |
/* fake a CLI structure only for the socket API */ |
| 652 |
/* TODO: rewrite the network.c API: |
| 653 |
* - move c->fd, c->fds, and c->exception_pointer into a separate structure |
| 654 |
* - put this new structure inside CLI and here |
| 655 |
* - rewrite the network.c interface to use this structure */ |
| 656 |
c=str_alloc(sizeof(CLI)); |
| 657 |
c->fds=s_poll_alloc(); |
| 658 |
c->fd=INVALID_SOCKET; |
| 659 |
c->exception_pointer=&exception_buffer; |
| 660 |
if(setjmp(exception_buffer)) { |
| 661 |
s_poll_free(c->fds); |
| 662 |
str_free(c); |
| 663 |
return 0; |
| 664 |
} |
| 665 |
|
| 666 |
/* parse the OCSP URL */ |
| 667 |
if(!OCSP_parse_url(ocsp->url, &host, &port, &path, &ssl)) { |
| 668 |
s_log(LOG_ERR, "OCSP: Failed to parse the OCSP URL"); |
| 669 |
goto cleanup; |
| 670 |
} |
| 671 |
if(ssl) { |
| 672 |
s_log(LOG_ERR, "OCSP: TLS not supported for OCSP" |
| 673 |
" - an additional stunnel service needs to be defined"); |
| 674 |
goto cleanup; |
| 675 |
} |
| 676 |
if(!hostport2addr(&addr, host, port, 0)) { |
| 677 |
s_log(LOG_ERR, "OCSP: Failed to resolve the OCSP responder address"); |
| 678 |
goto cleanup; |
| 679 |
} |
| 680 |
|
| 681 |
/* connect specified OCSP responder */ |
| 682 |
c->fd=s_socket(addr.sa.sa_family, SOCK_STREAM, 0, 1, "OCSP: socket"); |
| 683 |
if(c->fd==INVALID_SOCKET) |
| 684 |
goto cleanup; |
| 685 |
if(s_connect(c, &addr, addr_len(&addr), opt->timeout_ocsp)) |
| 686 |
goto cleanup; |
| 687 |
bio=BIO_new_socket((int)c->fd, BIO_NOCLOSE); |
| 688 |
if(!bio) { |
| 689 |
ssl_error(c, "OCSP: BIO_new_socket"); |
| 690 |
goto cleanup; |
| 691 |
} |
| 692 |
s_log(LOG_DEBUG, "OCSP: Connected %s:%s", host, port); |
| 693 |
|
| 694 |
/* initialize an HTTP request with the POST method */ |
| 695 |
#if OPENSSL_VERSION_NUMBER>=0x10000000L |
| 696 |
req_ctx=OCSP_sendreq_new(bio, path, NULL, -1); |
| 697 |
#else /* OpenSSL version >= 1.0.0 */ |
| 698 |
/* there is no way to send the Host header with older OpenSSL versions */ |
| 699 |
req_ctx=OCSP_sendreq_new(bio, path, ocsp->request, -1); |
| 700 |
#endif /* OpenSSL version 1.0.0 or later */ |
| 701 |
if(!req_ctx) { |
| 702 |
ssl_error(c, "OCSP: OCSP_sendreq_new"); |
| 703 |
goto cleanup; |
| 704 |
} |
| 705 |
#if OPENSSL_VERSION_NUMBER>=0x10000000L |
| 706 |
/* add the HTTP headers */ |
| 707 |
if(!OCSP_REQ_CTX_add1_header(req_ctx, "Host", host)) { |
| 708 |
ssl_error(c, "OCSP: OCSP_REQ_CTX_add1_header"); |
| 709 |
goto cleanup; |
| 710 |
} |
| 711 |
if(!OCSP_REQ_CTX_add1_header(req_ctx, "User-Agent", "stunnel")) { |
| 712 |
ssl_error(c, "OCSP: OCSP_REQ_CTX_add1_header"); |
| 713 |
goto cleanup; |
| 714 |
} |
| 715 |
/* add the remaining HTTP headers and the OCSP request body */ |
| 716 |
if(!OCSP_REQ_CTX_set1_req(req_ctx, ocsp->request)) { |
| 717 |
ssl_error(c, "OCSP: OCSP_REQ_CTX_set1_req"); |
| 718 |
goto cleanup; |
| 719 |
} |
| 720 |
#endif /* OpenSSL version 1.0.0 or later */ |
| 721 |
|
| 722 |
/* OCSP protocol communication loop */ |
| 723 |
while(OCSP_sendreq_nbio(&ocsp->response, req_ctx)==-1) { |
| 724 |
s_poll_init(c->fds, 0); |
| 725 |
s_poll_add(c->fds, c->fd, BIO_should_read(bio), BIO_should_write(bio)); |
| 726 |
switch(s_poll_wait(c->fds, opt->timeout_busy, 0)) { |
| 727 |
case -1: |
| 728 |
sockerror("OCSP: s_poll_wait"); |
| 729 |
goto cleanup; |
| 730 |
case 0: |
| 731 |
s_log(LOG_INFO, "OCSP: s_poll_wait: TIMEOUTbusy exceeded"); |
| 732 |
goto cleanup; |
| 733 |
} |
| 734 |
} |
| 735 |
#if 0 |
| 736 |
s_log(LOG_DEBUG, "OCSP: context state: 0x%x", *(int *)req_ctx); |
| 737 |
#endif |
| 738 |
/* http://www.mail-archive.com/[email protected]/msg61691.html */ |
| 739 |
if(ocsp->response) { |
| 740 |
s_log(LOG_DEBUG, "OCSP: Response received"); |
| 741 |
ret=1; |
| 742 |
} else { |
| 743 |
if(ERR_peek_error()) |
| 744 |
ssl_error(c, "OCSP: OCSP_sendreq_nbio"); |
| 745 |
else /* OpenSSL error: OCSP_sendreq_nbio does not use OCSPerr */ |
| 746 |
s_log(LOG_ERR, "OCSP: OCSP_sendreq_nbio: OpenSSL internal error"); |
| 747 |
} |
| 748 |
|
| 749 |
cleanup: |
| 750 |
if(req_ctx) |
| 751 |
OCSP_REQ_CTX_free(req_ctx); |
| 752 |
if(bio) |
| 753 |
BIO_free_all(bio); |
| 754 |
if(c->fd!=INVALID_SOCKET) { |
| 755 |
closesocket(c->fd); |
| 756 |
c->fd=INVALID_SOCKET; /* avoid double close on cleanup */ |
| 757 |
} |
| 758 |
if(host) |
| 759 |
OPENSSL_free(host); |
| 760 |
if(port) |
| 761 |
OPENSSL_free(port); |
| 762 |
if(path) |
| 763 |
OPENSSL_free(path); |
| 764 |
s_poll_free(c->fds); /* TODO */ |
| 765 |
str_free(c); /* TODO */ |
| 766 |
return ret; |
| 767 |
} |
| 768 |
#if defined(__GNUC__) && !defined(__clang__) |
| 769 |
#pragma GCC diagnostic pop |
| 770 |
#endif |
| 771 |
|
| 772 |
/* |
| 773 |
* Validates the cached or fetched OCSP response. |
| 774 |
* Returns one of: |
| 775 |
* - V_OCSP_CERTSTATUS_GOOD |
| 776 |
* - V_OCSP_CERTSTATUS_REVOKED |
| 777 |
* - V_OCSP_CERTSTATUS_UNKNOWN |
| 778 |
*/ |
| 779 |
NOEXPORT int ocsp_response_validate(CLI *c, SERVICE_OPTIONS *opt, OCSP_CTX *ocsp) { |
| 780 |
int response_status, reason; |
| 781 |
OCSP_BASICRESP *basic_response=NULL; |
| 782 |
int ocsp_status=V_OCSP_CERTSTATUS_UNKNOWN; |
| 783 |
|
| 784 |
s_log(LOG_DEBUG, "OCSP: Validate the OCSP response"); |
| 785 |
if(!ocsp->response) { |
| 786 |
s_log(LOG_ERR, "OCSP: No OCSP response"); |
| 787 |
goto cleanup; |
| 788 |
} |
| 789 |
response_status=OCSP_response_status(ocsp->response); |
| 790 |
if(response_status!=OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 791 |
s_log(LOG_ERR, "OCSP: OCSP responder error: %d: %s", |
| 792 |
response_status, OCSP_response_status_str(response_status)); |
| 793 |
goto cleanup; |
| 794 |
} |
| 795 |
basic_response=OCSP_response_get1_basic(ocsp->response); |
| 796 |
if(!basic_response) { |
| 797 |
s_log(LOG_WARNING, "OCSP: OCSP_response_get1_basic"); |
| 798 |
goto cleanup; |
| 799 |
} |
| 800 |
if(ocsp->request && ocsp->use_nonce && |
| 801 |
OCSP_check_nonce(ocsp->request, basic_response)<=0) { |
| 802 |
s_log(LOG_ERR, "OCSP: Invalid or unsupported nonce"); |
| 803 |
goto cleanup; |
| 804 |
} |
| 805 |
if(OCSP_basic_verify(basic_response, ocsp->chain_to_verify, |
| 806 |
SSL_CTX_get_cert_store(opt->ctx), ocsp->flags)<=0) { |
| 807 |
ssl_error(c, "OCSP: OCSP_basic_verify"); |
| 808 |
goto cleanup; |
| 809 |
} |
| 810 |
ocsp_ctx_setup_cert_id(ocsp); |
| 811 |
if(!ocsp->cert_id) |
| 812 |
goto cleanup; |
| 813 |
if(!OCSP_resp_find_status(basic_response, ocsp->cert_id, &ocsp_status, &reason, |
| 814 |
&ocsp->revoked_at, &ocsp->this_update, &ocsp->next_update)) { |
| 815 |
s_log(LOG_WARNING, "OCSP: OCSP_resp_find_status"); |
| 816 |
goto cleanup; |
| 817 |
} |
| 818 |
s_log(LOG_INFO, "OCSP: Status: %s", OCSP_cert_status_str(ocsp_status)); |
| 819 |
log_time(LOG_INFO, "OCSP: This update", ocsp->this_update); |
| 820 |
if(ocsp->next_update) |
| 821 |
log_time(LOG_INFO, "OCSP: Next update", ocsp->next_update); |
| 822 |
if(!OCSP_check_validity(ocsp->this_update, ocsp->next_update, ocsp->leeway, -1)) { |
| 823 |
ssl_error(c, "OCSP: OCSP_check_validity"); |
| 824 |
ocsp_status=V_OCSP_CERTSTATUS_UNKNOWN; /* override an invalid response */ |
| 825 |
} |
| 826 |
switch(ocsp_status) { |
| 827 |
case V_OCSP_CERTSTATUS_GOOD: |
| 828 |
s_log(LOG_NOTICE, "OCSP: Certificate accepted"); |
| 829 |
break; |
| 830 |
case V_OCSP_CERTSTATUS_REVOKED: |
| 831 |
if(reason==-1) |
| 832 |
s_log(LOG_ERR, "OCSP: Certificate revoked"); |
| 833 |
else |
| 834 |
s_log(LOG_ERR, "OCSP: Certificate revoked: %d: %s", |
| 835 |
reason, OCSP_crl_reason_str(reason)); |
| 836 |
log_time(LOG_NOTICE, "OCSP: Revoked at", ocsp->revoked_at); |
| 837 |
break; |
| 838 |
case V_OCSP_CERTSTATUS_UNKNOWN: |
| 839 |
s_log(LOG_WARNING, "OCSP: Unknown verification status"); |
| 840 |
} |
| 841 |
|
| 842 |
cleanup: |
| 843 |
if(basic_response) |
| 844 |
OCSP_BASICRESP_free(basic_response); |
| 845 |
return ocsp_status; |
| 846 |
} |
| 847 |
|
| 848 |
/* |
| 849 |
* Create an OCSP_CERTID object from ocsp->chain_to_verify at ocsp->depth. |
| 850 |
*/ |
| 851 |
NOEXPORT void ocsp_ctx_setup_cert_id(OCSP_CTX *ocsp) { |
| 852 |
X509 *subject, *issuer=NULL; |
| 853 |
int chain_len; |
| 854 |
|
| 855 |
if(ocsp->cert_id) /* already set */ |
| 856 |
return; /* nothing to do */ |
| 857 |
chain_len=sk_X509_num(ocsp->chain_to_verify); |
| 858 |
if(ocsp->depth<0 || ocsp->depth>chain_len-1) { /* sanity check */ |
| 859 |
s_log(LOG_ERR, "OCSP: INTERNAL ERROR: Invalid verification depth"); |
| 860 |
return; |
| 861 |
} |
| 862 |
subject=sk_X509_value(ocsp->chain_to_verify, ocsp->depth); |
| 863 |
issuer=ocsp->depth==chain_len-1 ? subject /* root CA certificate */ : |
| 864 |
sk_X509_value(ocsp->chain_to_verify, ocsp->depth+1); |
| 865 |
/* if dgst is NULL then SHA1 is used */ |
| 866 |
ocsp->cert_id=OCSP_cert_to_id(NULL, subject, issuer); |
| 867 |
if(!ocsp->cert_id) |
| 868 |
s_log(LOG_ERR, "OCSP: Can't create an OCSP_CERTID object"); |
| 869 |
} |
| 870 |
|
| 871 |
#if OPENSSL_VERSION_NUMBER<0x10100000L |
| 872 |
#define X509_OBJECT_new() str_alloc(sizeof(X509_OBJECT)) |
| 873 |
#define X509_OBJECT_free(x) X509_OBJECT_free_contents(x); str_free(x) |
| 874 |
#define X509_OBJECT_get0_X509(x) ((x)->data.x509) |
| 875 |
#endif /* OpenSSL older than 1.1.0 */ |
| 876 |
|
| 877 |
NOEXPORT int ocsp_ctx_append_root_ca(SERVICE_OPTIONS *opt, |
| 878 |
OCSP_CTX *ocsp) { |
| 879 |
int chain_len; |
| 880 |
X509 *cert; |
| 881 |
X509_STORE_CTX *store_ctx=NULL; |
| 882 |
X509_OBJECT *obj=NULL; |
| 883 |
int ret=0; /* failure */ |
| 884 |
|
| 885 |
chain_len=sk_X509_num(ocsp->chain_to_verify); |
| 886 |
if(!chain_len) { /* empty chain */ |
| 887 |
s_log(LOG_ERR, "OCSP: Empty verification chain"); |
| 888 |
goto cleanup; |
| 889 |
} |
| 890 |
cert=sk_X509_value(ocsp->chain_to_verify, chain_len-1); |
| 891 |
store_ctx=X509_STORE_CTX_new(); |
| 892 |
if(!store_ctx) { |
| 893 |
s_log(LOG_ERR, "OCSP: X509_STORE_CTX_new"); |
| 894 |
goto cleanup; |
| 895 |
} |
| 896 |
if(!X509_STORE_CTX_init(store_ctx, |
| 897 |
SSL_CTX_get_cert_store(opt->ctx), NULL, NULL)) { |
| 898 |
s_log(LOG_ERR, "OCSP: X509_STORE_CTX_init"); |
| 899 |
goto cleanup; |
| 900 |
} |
| 901 |
obj=X509_OBJECT_new(); |
| 902 |
if(X509_STORE_get_by_subject(store_ctx, |
| 903 |
X509_LU_X509, X509_get_subject_name(cert), obj)>0) { |
| 904 |
goto success; /* the certificate is already trusted */ |
| 905 |
} |
| 906 |
if(X509_STORE_get_by_subject(store_ctx, |
| 907 |
X509_LU_X509, X509_get_issuer_name(cert), obj)<=0) { |
| 908 |
s_log(LOG_INFO, "OCSP: The root CA certificate was not found"); |
| 909 |
goto cleanup; |
| 910 |
} |
| 911 |
/* append the root CA certificate into the verified chain */ |
| 912 |
ocsp->root_ca=X509_dup(X509_OBJECT_get0_X509(obj)); |
| 913 |
if(!ocsp->root_ca) { |
| 914 |
s_log(LOG_ERR, "OCSP: X509_dup"); |
| 915 |
goto cleanup; |
| 916 |
} |
| 917 |
if(!sk_X509_push(ocsp->chain_to_verify, ocsp->root_ca)) { |
| 918 |
s_log(LOG_ERR, "OCSP: sk_X509_push"); |
| 919 |
goto cleanup; |
| 920 |
} |
| 921 |
|
| 922 |
success: |
| 923 |
ret=1; /* success: a trusted root CA certificate appended to the chain */ |
| 924 |
|
| 925 |
cleanup: |
| 926 |
if(obj) |
| 927 |
X509_OBJECT_free(obj); |
| 928 |
if(store_ctx) |
| 929 |
X509_STORE_CTX_free(store_ctx); |
| 930 |
return ret; |
| 931 |
} |
| 932 |
|
| 933 |
/* Logs the time structure in a human-readable format */ |
| 934 |
NOEXPORT void log_time(const int level, const char *txt, ASN1_GENERALIZEDTIME *t) { |
| 935 |
char *cp; |
| 936 |
BIO *bio; |
| 937 |
int n; |
| 938 |
#if OPENSSL_VERSION_NUMBER>=0x10101000L |
| 939 |
time_t posix_time; |
| 940 |
struct tm ts; |
| 941 |
#endif /* OpenSSL version 1.1.1 or later */ |
| 942 |
|
| 943 |
if(!t) |
| 944 |
return; |
| 945 |
bio=BIO_new(BIO_s_mem()); |
| 946 |
if(!bio) |
| 947 |
return; |
| 948 |
#if OPENSSL_VERSION_NUMBER>=0x10101000L |
| 949 |
posix_time=time_t_get_asn1_time(t); |
| 950 |
if(posix_time==INVALID_TIME) { |
| 951 |
BIO_free(bio); |
| 952 |
return; |
| 953 |
} |
| 954 |
safe_localtime(&ts, posix_time); |
| 955 |
BIO_printf(bio, "%04d.%02d.%02d %02d:%02d:%02d", |
| 956 |
ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, |
| 957 |
ts.tm_hour, ts.tm_min, ts.tm_sec); |
| 958 |
#else /* OpenSSL version 1.1.1 or later */ |
| 959 |
ASN1_TIME_print(bio, t); |
| 960 |
#endif /* OpenSSL version 1.1.1 or later */ |
| 961 |
|
| 962 |
n=BIO_pending(bio); |
| 963 |
cp=str_alloc((size_t)n+1); |
| 964 |
n=BIO_read(bio, cp, n); |
| 965 |
if(n<0) { |
| 966 |
BIO_free(bio); |
| 967 |
str_free(cp); |
| 968 |
return; |
| 969 |
} |
| 970 |
cp[n]='\0'; |
| 971 |
BIO_free(bio); |
| 972 |
s_log(level, "%s: %s", txt, cp); |
| 973 |
str_free(cp); |
| 974 |
} |
| 975 |
|
| 976 |
#if !defined(HAVE_TIMEGM) |
| 977 |
/* Alternate, portable, O(1), and re-entrant timegm(3) implementation. |
| 978 |
* See: https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html |
| 979 |
* This code uses a calendar algorithm attributed to Howard Hinnant. The key |
| 980 |
* idea is to shift the calendar year so that it begins on March 1. By making |
| 981 |
* February the last month of the year, all leap-day complexities are pushed to |
| 982 |
* the very end, eliminating the need for messy "if (is_leap_year)" checks. |
| 983 |
* This avoids loops that count years or months, array lookups for the number |
| 984 |
* of days in each month, and calls to standard library functions with side |
| 985 |
* effects (for example, setenv, getenv, and mktime). */ |
| 986 |
NOEXPORT time_t timegm_alt(const struct tm *tm) { |
| 987 |
int64_t year=(int64_t)tm->tm_year+1900; |
| 988 |
int64_t month=tm->tm_mon; |
| 989 |
int64_t days; |
| 990 |
int64_t secs; |
| 991 |
|
| 992 |
/* 0. Normalize month to 0..11 */ |
| 993 |
year+=month/12; |
| 994 |
month%=12; |
| 995 |
if(month < 0) { |
| 996 |
month+=12; |
| 997 |
--year; |
| 998 |
} |
| 999 |
|
| 1000 |
/* 1. Shift calendar to start on March 1st. |
| 1001 |
* This puts the leap day (Feb 29) at the very end of the shifted year. */ |
| 1002 |
if(month < 2) { |
| 1003 |
--year; |
| 1004 |
month+=10; /* Jan (0) -> 10, Feb (1) -> 11 */ |
| 1005 |
} else { |
| 1006 |
month-=2; /* Mar (2) -> 0, Apr (3) -> 1, ..., Dec (11) -> 9 */ |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* 2. Calculate days since the computational epoch 0000-03-01. |
| 1010 |
* This leap-year arithmetic is intended for AD dates only. */ |
| 1011 |
days=year*365 + year/4 - year/100 + year/400; |
| 1012 |
|
| 1013 |
/* 3. Add days passed in the current shifted year. |
| 1014 |
* The magic formula (153 * month + 2) / 5 perfectly distributes |
| 1015 |
* the 30- and 31-day months in the shifted calendar. */ |
| 1016 |
days+=(153*month + 2)/5; |
| 1017 |
|
| 1018 |
/* 4. Add the zero-based day offset within the month. */ |
| 1019 |
days+=tm->tm_mday - 1; |
| 1020 |
|
| 1021 |
/* 5. Subtract the number of days between 0000-03-01 and the |
| 1022 |
* Unix Epoch (1970-01-01), which is exactly 719468 days. */ |
| 1023 |
days-=719468; |
| 1024 |
|
| 1025 |
/* 6. Convert total days to seconds and add hours, minutes, and seconds */ |
| 1026 |
secs=days*86400; |
| 1027 |
secs+=(int64_t)tm->tm_hour*3600; |
| 1028 |
secs+=(int64_t)tm->tm_min*60; |
| 1029 |
secs+=tm->tm_sec; |
| 1030 |
|
| 1031 |
return (time_t)secs; |
| 1032 |
} |
| 1033 |
#endif |
| 1034 |
|
| 1035 |
#if OPENSSL_VERSION_NUMBER>=0x10101000L |
| 1036 |
/* Converts ASN1_TIME structure to time_t */ |
| 1037 |
NOEXPORT time_t time_t_get_asn1_time(const ASN1_TIME *s) { |
| 1038 |
struct tm tm; |
| 1039 |
|
| 1040 |
if ((!s) || (!ASN1_TIME_check(s))) { |
| 1041 |
return INVALID_TIME; |
| 1042 |
} |
| 1043 |
/* The ASN1_TIME_to_tm() function was added in OpenSSL 1.1.1 */ |
| 1044 |
if (ASN1_TIME_to_tm(s, &tm)) { |
| 1045 |
#if defined(HAVE_TIMEGM) |
| 1046 |
return timegm(&tm); |
| 1047 |
#else /* defined HAVE_TIMEGM */ |
| 1048 |
return timegm_alt(&tm); |
| 1049 |
#endif /* defined HAVE_TIMEGM */ |
| 1050 |
} else { |
| 1051 |
return INVALID_TIME; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
#endif /* OpenSSL version 1.1.0 or later */ |
| 1055 |
|
| 1056 |
#endif /* !defined(OPENSSL_NO_OCSP) */ |