| 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 |
/* this mirrors a private OpenSSL struct whose layout has stayed stable across |
| 41 |
* the supported OpenSSL versions, so breakage seems unlikely in current |
| 42 |
* releases, but it is still an unsupported internal dependency that could |
| 43 |
* change, especially in a future major version */ |
| 44 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 45 |
struct ssl_comp_st { |
| 46 |
int id; |
| 47 |
const char *name; |
| 48 |
COMP_METHOD *method; |
| 49 |
}; |
| 50 |
#endif /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 51 |
|
| 52 |
/* global OpenSSL initialization: compression, engine, entropy */ |
| 53 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 54 |
NOEXPORT void cb_new_auth(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 55 |
int idx, long argl, void *argp); |
| 56 |
#else /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 57 |
NOEXPORT int cb_new_auth(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 58 |
int idx, long argl, void *argp); |
| 59 |
#endif /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 60 |
#if OPENSSL_VERSION_NUMBER>=0x30000000L |
| 61 |
NOEXPORT int cb_dup_addr(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, |
| 62 |
void **from_d, int idx, long argl, void *argp); |
| 63 |
#elif OPENSSL_VERSION_NUMBER>=0x10100000L |
| 64 |
NOEXPORT int cb_dup_addr(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, |
| 65 |
void *from_d, int idx, long argl, void *argp); |
| 66 |
#else |
| 67 |
NOEXPORT int cb_dup_addr(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, |
| 68 |
void *from_d, int idx, long argl, void *argp); |
| 69 |
#endif |
| 70 |
NOEXPORT void cb_free_addr(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 71 |
int idx, long argl, void *argp); |
| 72 |
#ifndef OPENSSL_NO_COMP |
| 73 |
NOEXPORT int compression_set(GLOBAL_OPTIONS *); |
| 74 |
NOEXPORT void compression_list(void); |
| 75 |
#endif |
| 76 |
NOEXPORT int prng_init(GLOBAL_OPTIONS *); |
| 77 |
NOEXPORT int add_rand_file(GLOBAL_OPTIONS *, const char *); |
| 78 |
NOEXPORT void update_rand_file(const char *); |
| 79 |
|
| 80 |
int index_ssl_cli, index_ssl_ctx_opt; |
| 81 |
int index_session_authenticated, index_session_connect_address; |
| 82 |
|
| 83 |
#ifdef USE_FIPS |
| 84 |
|
| 85 |
int fips_default(void) { |
| 86 |
static int cache=-1; |
| 87 |
|
| 88 |
if(cache == -1) { |
| 89 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 90 |
cache=EVP_default_properties_is_fips_enabled(NULL); |
| 91 |
#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */ |
| 92 |
cache=FIPS_mode(); |
| 93 |
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ |
| 94 |
} |
| 95 |
return cache; |
| 96 |
} |
| 97 |
|
| 98 |
int fips_available(void) { /* either FIPS provider or container is available */ |
| 99 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 100 |
static OSSL_PROVIDER *fips=NULL; /* cache the success */ |
| 101 |
|
| 102 |
if(!fips) |
| 103 |
fips=OSSL_PROVIDER_try_load(NULL, "fips", 1); |
| 104 |
return fips && OSSL_PROVIDER_available(NULL, "fips"); |
| 105 |
#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ |
| 106 |
/* checking for OPENSSL_FIPS would be less precise, because it only |
| 107 |
* depends on the compiled, and not on the running OpenSSL version */ |
| 108 |
return strstr(OpenSSL_version(OPENSSL_VERSION), "-fips") != NULL || |
| 109 |
strstr(OpenSSL_version(OPENSSL_VERSION), "FIPS") != NULL; /* RHEL */ |
| 110 |
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ |
| 111 |
} |
| 112 |
|
| 113 |
#endif /* USE_FIPS */ |
| 114 |
|
| 115 |
/* initialize libcrypto before invoking API functions that require it */ |
| 116 |
void crypto_init(void) { |
| 117 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 118 |
OPENSSL_INIT_SETTINGS *conf; |
| 119 |
#endif /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 120 |
#ifdef USE_WIN32 |
| 121 |
TCHAR stunnel_exe_path[MAX_PATH]; |
| 122 |
LPTSTR c; |
| 123 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 124 |
char *stunnel_dir, *path; |
| 125 |
#endif /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 126 |
|
| 127 |
/* identify stunnel_exe_path */ |
| 128 |
GetModuleFileName(0, stunnel_exe_path, MAX_PATH); |
| 129 |
c=_tcsrchr(stunnel_exe_path, TEXT('\\')); /* last backslash */ |
| 130 |
if(c) { /* found */ |
| 131 |
*c=TEXT('\0'); /* truncate the program name */ |
| 132 |
c=_tcsrchr(stunnel_exe_path, TEXT('\\')); /* previous backslash */ |
| 133 |
if(c && !_tcscmp(c+1, TEXT("bin"))) |
| 134 |
*c=TEXT('\0'); /* truncate "bin" */ |
| 135 |
} |
| 136 |
|
| 137 |
/* set current working directory */ |
| 138 |
#ifndef _WIN32_WCE |
| 139 |
if(!SetCurrentDirectory(stunnel_exe_path)) { |
| 140 |
LPTSTR errmsg=str_tprintf(TEXT("Cannot set directory to %s"), |
| 141 |
stunnel_exe_path); |
| 142 |
message_box(errmsg, MB_ICONERROR); |
| 143 |
str_free(errmsg); |
| 144 |
exit(1); |
| 145 |
} |
| 146 |
/* try to enter the "config" subdirectory, ignore the result */ |
| 147 |
SetCurrentDirectory(TEXT("config")); |
| 148 |
#endif |
| 149 |
|
| 150 |
stunnel_dir=tstr2str(stunnel_exe_path); |
| 151 |
if(!stunnel_dir) /* fail-safe */ |
| 152 |
stunnel_dir=str_dup(".."); |
| 153 |
|
| 154 |
/* setup the environment */ |
| 155 |
path=str_printf("%s\\engines", stunnel_dir); |
| 156 |
_putenv_s("OPENSSL_ENGINES", path); |
| 157 |
str_free(path); |
| 158 |
path=str_printf("%s\\ossl-modules", stunnel_dir); |
| 159 |
_putenv_s("OPENSSL_MODULES", path); |
| 160 |
str_free(path); |
| 161 |
path=str_printf("%s\\config\\openssl.cnf", stunnel_dir); |
| 162 |
_putenv_s("OPENSSL_CONF", path); |
| 163 |
str_free(path); |
| 164 |
#endif /* USE_WIN32 */ |
| 165 |
|
| 166 |
/* initialize OpenSSL */ |
| 167 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 168 |
conf=OPENSSL_INIT_new(); |
| 169 |
#ifdef USE_WIN32 |
| 170 |
path=str_printf("%s\\config\\openssl.cnf", stunnel_dir); |
| 171 |
if(!OPENSSL_INIT_set_config_filename(conf, path)) { |
| 172 |
s_log(LOG_ERR, "Failed to set OpenSSL configuration file name"); |
| 173 |
} |
| 174 |
str_free(path); |
| 175 |
#endif /* USE_WIN32 */ |
| 176 |
OPENSSL_init_crypto( |
| 177 |
OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_LOAD_CONFIG, conf); |
| 178 |
OPENSSL_INIT_free(conf); |
| 179 |
#else /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 180 |
OPENSSL_config(NULL); |
| 181 |
SSL_load_error_strings(); |
| 182 |
SSL_library_init(); |
| 183 |
#endif /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 184 |
|
| 185 |
#ifdef USE_WIN32 |
| 186 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 187 |
/* setup the default search path for providers */ |
| 188 |
/* WARNING: OpenSSL needs to already be initialized */ |
| 189 |
path=str_printf("%s\\ossl-modules", stunnel_dir); |
| 190 |
if(!OSSL_PROVIDER_set_default_search_path(NULL, path)) { |
| 191 |
s_log(LOG_ERR, "Failed to set default ossl-modules path"); |
| 192 |
} |
| 193 |
str_free(path); |
| 194 |
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ |
| 195 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 196 |
str_free(stunnel_dir); |
| 197 |
#endif /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 198 |
#endif /* USE_WIN32 */ |
| 199 |
} |
| 200 |
|
| 201 |
/* release libcrypto resources at shutdown */ |
| 202 |
void crypto_cleanup(void) { |
| 203 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 204 |
OPENSSL_cleanup(); |
| 205 |
#endif |
| 206 |
} |
| 207 |
|
| 208 |
/* initialize libssl before parsing the configuration file */ |
| 209 |
int ssl_init(void) { |
| 210 |
index_ssl_cli=SSL_get_ex_new_index(0, NULL, |
| 211 |
NULL, NULL, NULL); |
| 212 |
index_ssl_ctx_opt=SSL_CTX_get_ex_new_index(0, NULL, |
| 213 |
NULL, NULL, NULL); |
| 214 |
index_session_authenticated=SSL_SESSION_get_ex_new_index(0, NULL, |
| 215 |
cb_new_auth, NULL, NULL); |
| 216 |
index_session_connect_address=SSL_SESSION_get_ex_new_index(0, NULL, |
| 217 |
NULL, cb_dup_addr, cb_free_addr); |
| 218 |
if(index_ssl_cli<0 || index_ssl_ctx_opt<0 || |
| 219 |
index_session_authenticated<0 || |
| 220 |
index_session_connect_address<0) { |
| 221 |
s_log(LOG_ERR, "Application specific data initialization failed"); |
| 222 |
return 1; |
| 223 |
} |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
/* release libssl resources at shutdown */ |
| 228 |
void ssl_cleanup(void) { |
| 229 |
/* no libssl cleanup is needed for now */ |
| 230 |
} |
| 231 |
|
| 232 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 233 |
NOEXPORT void cb_new_auth(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 234 |
int idx, long argl, void *argp) { |
| 235 |
#else /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 236 |
NOEXPORT int cb_new_auth(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 237 |
int idx, long argl, void *argp) { |
| 238 |
#endif /* OPENSSL_VERSION_NUMBER>=0x10100000L */ |
| 239 |
(void)parent; /* squash the unused parameter warning */ |
| 240 |
(void)ptr; /* squash the unused parameter warning */ |
| 241 |
(void)argl; /* squash the unused parameter warning */ |
| 242 |
(void)argp; /* squash the unused parameter warning */ |
| 243 |
if(!CRYPTO_set_ex_data(ad, idx, (void *)(-1))) |
| 244 |
ssl_error(NULL, "CRYPTO_set_ex_data"); |
| 245 |
#if OPENSSL_VERSION_NUMBER<0x10100000L |
| 246 |
return 1; /* success */ |
| 247 |
#endif /* OPENSSL_VERSION_NUMBER<0x10100000L */ |
| 248 |
} |
| 249 |
|
| 250 |
#if OPENSSL_VERSION_NUMBER>=0x30000000L |
| 251 |
NOEXPORT int cb_dup_addr(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, |
| 252 |
void **from_d, int idx, long argl, void *argp) { |
| 253 |
#elif OPENSSL_VERSION_NUMBER>=0x10100000L |
| 254 |
NOEXPORT int cb_dup_addr(CRYPTO_EX_DATA *to, const CRYPTO_EX_DATA *from, |
| 255 |
void *from_d, int idx, long argl, void *argp) { |
| 256 |
#else |
| 257 |
NOEXPORT int cb_dup_addr(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from, |
| 258 |
void *from_d, int idx, long argl, void *argp) { |
| 259 |
#endif |
| 260 |
SOCKADDR_UNION *src, *dst; |
| 261 |
socklen_t len; |
| 262 |
|
| 263 |
(void)to; /* squash the unused parameter warning */ |
| 264 |
(void)from; /* squash the unused parameter warning */ |
| 265 |
(void)idx; /* squash the unused parameter warning */ |
| 266 |
(void)argl; /* squash the unused parameter warning */ |
| 267 |
(void)argp; /* squash the unused parameter warning */ |
| 268 |
src=*(void **)from_d; |
| 269 |
len=addr_len(src); |
| 270 |
dst=str_alloc_detached((size_t)len); |
| 271 |
memcpy(dst, src, (size_t)len); |
| 272 |
*(void **)from_d=dst; |
| 273 |
return 1; |
| 274 |
} |
| 275 |
|
| 276 |
NOEXPORT void cb_free_addr(void *parent, void *ptr, CRYPTO_EX_DATA *ad, |
| 277 |
int idx, long argl, void *argp) { |
| 278 |
(void)parent; /* squash the unused parameter warning */ |
| 279 |
(void)ad; /* squash the unused parameter warning */ |
| 280 |
(void)idx; /* squash the unused parameter warning */ |
| 281 |
(void)argl; /* squash the unused parameter warning */ |
| 282 |
(void)argp; /* squash the unused parameter warning */ |
| 283 |
str_free(ptr); |
| 284 |
} |
| 285 |
|
| 286 |
int ssl_configure(GLOBAL_OPTIONS *global) { /* configure global TLS settings */ |
| 287 |
#ifdef USE_FIPS |
| 288 |
#if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 289 |
if(global->option.fips != |
| 290 |
(OSSL_PROVIDER_available(NULL, "fips") && |
| 291 |
EVP_default_properties_is_fips_enabled(NULL))) { |
| 292 |
if(global->option.fips) { /* need to enable */ |
| 293 |
if(!fips_available()) { |
| 294 |
ssl_error(NULL, "FIPS PROVIDER"); |
| 295 |
return 1; |
| 296 |
} |
| 297 |
if(!EVP_default_properties_enable_fips(NULL, 1)) { |
| 298 |
s_log(LOG_ERR, "Enabling FIPS provider failed"); |
| 299 |
return 1; |
| 300 |
} |
| 301 |
} else { /* need to disable */ |
| 302 |
if(fips_default()) { |
| 303 |
s_log(LOG_ERR, "Refusing to override 'fips=yes' default property"); |
| 304 |
return 1; |
| 305 |
} |
| 306 |
if(!EVP_default_properties_enable_fips(NULL, 0)) { |
| 307 |
s_log(LOG_ERR, "Disabling FIPS provider failed"); |
| 308 |
return 1; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
s_log(LOG_NOTICE, "FIPS provider %s", |
| 313 |
(OSSL_PROVIDER_available(NULL, "fips") && |
| 314 |
EVP_default_properties_is_fips_enabled(NULL)) ? |
| 315 |
"enabled" : "disabled"); |
| 316 |
#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ |
| 317 |
if(FIPS_mode()!=global->option.fips) { |
| 318 |
RAND_set_rand_method(NULL); /* reset RAND methods */ |
| 319 |
if(!FIPS_mode_set(global->option.fips)) { |
| 320 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 321 |
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); |
| 322 |
#else |
| 323 |
ERR_load_crypto_strings(); |
| 324 |
#endif |
| 325 |
ssl_error(NULL, "FIPS_mode_set"); |
| 326 |
return 1; |
| 327 |
} |
| 328 |
} |
| 329 |
s_log(LOG_NOTICE, "FIPS mode %s", |
| 330 |
global->option.fips ? "enabled" : "disabled"); |
| 331 |
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ |
| 332 |
#endif /* USE_FIPS */ |
| 333 |
|
| 334 |
#ifndef OPENSSL_NO_COMP |
| 335 |
if(compression_set(global)) |
| 336 |
return 1; |
| 337 |
compression_list(); |
| 338 |
#endif /* OPENSSL_NO_COMP */ |
| 339 |
if(prng_init(global)) |
| 340 |
return 1; |
| 341 |
|
| 342 |
#ifndef OPENSSL_NO_ENGINE |
| 343 |
ENGINE_load_builtin_engines(); |
| 344 |
#endif |
| 345 |
|
| 346 |
/* cryptographic algorithms can only be configured once, |
| 347 |
* after all the engines are initialized */ |
| 348 |
#if OPENSSL_VERSION_NUMBER>=0x10100000L |
| 349 |
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL); |
| 350 |
#else |
| 351 |
OpenSSL_add_all_algorithms(); |
| 352 |
#endif |
| 353 |
return 0; /* SUCCESS */ |
| 354 |
} |
| 355 |
|
| 356 |
#ifndef OPENSSL_NO_COMP |
| 357 |
|
| 358 |
#if OPENSSL_VERSION_NUMBER<0x10100000L |
| 359 |
|
| 360 |
NOEXPORT int COMP_get_type(const COMP_METHOD *meth) { |
| 361 |
return meth->type; |
| 362 |
} |
| 363 |
|
| 364 |
NOEXPORT const char *SSL_COMP_get0_name(const SSL_COMP *comp) { |
| 365 |
return comp->name; |
| 366 |
} |
| 367 |
|
| 368 |
NOEXPORT int SSL_COMP_get_id(const SSL_COMP *comp) { |
| 369 |
return comp->id; |
| 370 |
} |
| 371 |
|
| 372 |
NOEXPORT const char *COMP_get_name(const COMP_METHOD *meth) { |
| 373 |
return SSL_COMP_get_name(meth); |
| 374 |
} |
| 375 |
|
| 376 |
#endif /* OPENSSL_VERSION_NUMBER<0x10100000L */ |
| 377 |
|
| 378 |
/* the following function mostly re-implements the defunct |
| 379 |
* OpenSSL's SSL_COMP_add_compression_method() function */ |
| 380 |
NOEXPORT int add_compression_method(STACK_OF(SSL_COMP) *sk, |
| 381 |
int id, COMP_METHOD *meth) { |
| 382 |
SSL_COMP *comp; |
| 383 |
|
| 384 |
/* validate the requested compression method */ |
| 385 |
if(!meth || COMP_get_type(meth)==NID_undef) { |
| 386 |
s_log(LOG_ERR, "Compression type disabled in this OpenSSL build"); |
| 387 |
return 1; /* FAILED */ |
| 388 |
} |
| 389 |
|
| 390 |
/* build an SSL_COMP object and push it to the stack */ |
| 391 |
comp=OPENSSL_malloc(sizeof(SSL_COMP)); |
| 392 |
if(!comp) |
| 393 |
return 1; /* FAILED */ |
| 394 |
comp->id=id; |
| 395 |
comp->name=COMP_get_name(meth); /* SSL_COMP_add_compression_method() bug */ |
| 396 |
comp->method=meth; /* SSL_COMP_add_compression_method() bug */ |
| 397 |
if(!sk_SSL_COMP_push(sk, comp)) { |
| 398 |
OPENSSL_free(comp); |
| 399 |
s_log(LOG_ERR, "Failed to add compression method"); |
| 400 |
return 1; /* FAILED */ |
| 401 |
} |
| 402 |
return 0; /* SUCCESS */ |
| 403 |
} |
| 404 |
|
| 405 |
NOEXPORT int compression_set(GLOBAL_OPTIONS *global) { |
| 406 |
STACK_OF(SSL_COMP) *methods; |
| 407 |
int retval=0; |
| 408 |
|
| 409 |
methods=SSL_COMP_get_compression_methods(); |
| 410 |
if(!methods) /* make sure we have a valid stack */ |
| 411 |
return global->compression!=COMP_NONE; |
| 412 |
|
| 413 |
/* empty the current compression method stack */ |
| 414 |
while(sk_SSL_COMP_num(methods)) |
| 415 |
sk_SSL_COMP_pop(methods); |
| 416 |
|
| 417 |
/* add the requested compression methods */ |
| 418 |
switch(global->compression) { |
| 419 |
case COMP_DEFLATE: /* RFC 1951 on standard id 0x01 */ |
| 420 |
retval|=add_compression_method(methods, 0x01, COMP_zlib()); |
| 421 |
break; |
| 422 |
case COMP_ZLIB: /* RFC 1951 + historic/obsolete private id 0xe0 */ |
| 423 |
retval|=add_compression_method(methods, 0x01, COMP_zlib()); |
| 424 |
retval|=add_compression_method(methods, 0xe0, COMP_zlib()); |
| 425 |
break; |
| 426 |
#if OPENSSL_VERSION_NUMBER>=0x30200000L |
| 427 |
case COMP_ZSTD: /* RFC 8878 on stunnel's nonstandard id 0xe1 */ |
| 428 |
retval|=add_compression_method(methods, 0xe1, COMP_zstd()); |
| 429 |
break; |
| 430 |
case COMP_BROTLI: /* RFC 7932 on stunnel's nonstandard id 0xe2 */ |
| 431 |
retval|=add_compression_method(methods, 0xe2, COMP_brotli()); |
| 432 |
break; |
| 433 |
#endif |
| 434 |
case COMP_NONE: /* make static code analyzers happy */ |
| 435 |
break; |
| 436 |
} |
| 437 |
return retval; |
| 438 |
} |
| 439 |
|
| 440 |
NOEXPORT void compression_list(void) { |
| 441 |
STACK_OF(SSL_COMP) *methods; |
| 442 |
int num_methods, i; |
| 443 |
|
| 444 |
methods=SSL_COMP_get_compression_methods(); |
| 445 |
num_methods=sk_SSL_COMP_num(methods); |
| 446 |
if(!num_methods) { |
| 447 |
s_log(LOG_INFO, "Compression disabled"); |
| 448 |
return; |
| 449 |
} |
| 450 |
s_log(LOG_INFO, "Compression enabled: %d method%s", |
| 451 |
num_methods, num_methods==1 ? "" : "s"); |
| 452 |
for(i=0; i<num_methods; ++i) { |
| 453 |
SSL_COMP *comp; |
| 454 |
const char *name; |
| 455 |
|
| 456 |
comp=sk_SSL_COMP_value(methods, i); |
| 457 |
if(!comp) |
| 458 |
continue; |
| 459 |
name=SSL_COMP_get0_name(comp); |
| 460 |
/* see OpenSSL commit 847406923534dd791f73d0cda15d3f17f513f2a5 */ |
| 461 |
if(!name) |
| 462 |
name="name not available"; |
| 463 |
s_log(LOG_INFO, "Compression id 0x%02x: %s", |
| 464 |
SSL_COMP_get_id(comp), name); |
| 465 |
} |
| 466 |
} |
| 467 |
#endif /* OPENSSL_NO_COMP */ |
| 468 |
|
| 469 |
NOEXPORT int prng_init(GLOBAL_OPTIONS *global) { |
| 470 |
int totbytes=0; |
| 471 |
char filename[256]; |
| 472 |
const RAND_METHOD *meth=RAND_get_rand_method(); |
| 473 |
|
| 474 |
/* skip PRNG initialization when no seeding methods are available */ |
| 475 |
if(meth==NULL || meth->status==NULL || meth->add==NULL) { |
| 476 |
s_log(LOG_DEBUG, "No PRNG seeding methods"); |
| 477 |
return 0; /* success */ |
| 478 |
} |
| 479 |
|
| 480 |
if(RAND_status()) { |
| 481 |
s_log(LOG_DEBUG, "No PRNG seeding was required"); |
| 482 |
return 0; /* success */ |
| 483 |
} |
| 484 |
|
| 485 |
/* if they specify a rand file on the command line we |
| 486 |
assume that they really do want it, so try it first */ |
| 487 |
if(global->rand_file) { |
| 488 |
totbytes+=add_rand_file(global, global->rand_file); |
| 489 |
if(RAND_status()) |
| 490 |
return 0; /* success */ |
| 491 |
} |
| 492 |
|
| 493 |
/* try the $RANDFILE or $HOME/.rnd files */ |
| 494 |
filename[0]='\0'; |
| 495 |
RAND_file_name(filename, sizeof filename); |
| 496 |
if(filename[0]) { |
| 497 |
totbytes+=add_rand_file(global, filename); |
| 498 |
if(RAND_status()) |
| 499 |
return 0; /* success */ |
| 500 |
} |
| 501 |
|
| 502 |
#ifdef USE_WIN32 |
| 503 |
|
| 504 |
#if OPENSSL_VERSION_NUMBER<0x10100000L |
| 505 |
RAND_screen(); |
| 506 |
if(RAND_status()) { |
| 507 |
s_log(LOG_DEBUG, "Seeded PRNG with RAND_screen"); |
| 508 |
return 0; /* success */ |
| 509 |
} |
| 510 |
s_log(LOG_DEBUG, "RAND_screen failed to sufficiently seed PRNG"); |
| 511 |
#endif |
| 512 |
|
| 513 |
#else /* USE_WIN32 */ |
| 514 |
|
| 515 |
#ifndef OPENSSL_NO_EGD |
| 516 |
if(global->egd_sock) { |
| 517 |
int bytes=RAND_egd(global->egd_sock); |
| 518 |
if(bytes>=0) { |
| 519 |
s_log(LOG_DEBUG, "Snagged %d random bytes from EGD Socket %s", |
| 520 |
bytes, global->egd_sock); |
| 521 |
return 0; /* OpenSSL always gets what it needs or fails, |
| 522 |
so no need to check if seeded sufficiently */ |
| 523 |
} |
| 524 |
s_log(LOG_WARNING, "EGD Socket %s failed", global->egd_sock); |
| 525 |
} |
| 526 |
#endif |
| 527 |
|
| 528 |
#ifndef RANDOM_FILE |
| 529 |
/* try the good-old default /dev/urandom, if no RANDOM_FILE is defined */ |
| 530 |
totbytes+=add_rand_file(global, "/dev/urandom"); |
| 531 |
if(RAND_status()) |
| 532 |
return 0; /* success */ |
| 533 |
#endif |
| 534 |
|
| 535 |
#endif /* USE_WIN32 */ |
| 536 |
|
| 537 |
/* random file specified during configure */ |
| 538 |
s_log(LOG_ERR, "PRNG seeded with %d bytes total", totbytes); |
| 539 |
s_log(LOG_ERR, "PRNG was not seeded with enough random bytes"); |
| 540 |
return 1; /* FAILED */ |
| 541 |
} |
| 542 |
|
| 543 |
NOEXPORT int add_rand_file(GLOBAL_OPTIONS *global, const char *filename) { |
| 544 |
int readbytes; |
| 545 |
struct stat sb; |
| 546 |
|
| 547 |
if(stat(filename, &sb)) |
| 548 |
return 0; /* could not stat() file --> return 0 bytes */ |
| 549 |
|
| 550 |
readbytes=RAND_load_file(filename, global->random_bytes); |
| 551 |
if(readbytes<0) { |
| 552 |
ssl_error(NULL, "RAND_load_file"); |
| 553 |
s_log(LOG_INFO, "Cannot retrieve any random data from %s", |
| 554 |
filename); |
| 555 |
return 0; |
| 556 |
} |
| 557 |
s_log(LOG_DEBUG, "Snagged %d random bytes from %s", readbytes, filename); |
| 558 |
|
| 559 |
/* write new random data for future seeding if it's a regular file */ |
| 560 |
if(global->option.rand_write && S_ISREG(sb.st_mode)) |
| 561 |
update_rand_file(filename); |
| 562 |
|
| 563 |
return readbytes; |
| 564 |
} |
| 565 |
|
| 566 |
NOEXPORT void update_rand_file(const char *filename) { |
| 567 |
int writebytes; |
| 568 |
|
| 569 |
writebytes=RAND_write_file(filename); |
| 570 |
if(writebytes<0) { |
| 571 |
ssl_error(NULL, "RAND_write_file"); |
| 572 |
s_log(LOG_WARNING, "Failed to write strong random data to %s - " |
| 573 |
"may be a permissions or seeding problem", filename); |
| 574 |
return; |
| 575 |
} |
| 576 |
s_log(LOG_DEBUG, "Wrote %d new random bytes to %s", |
| 577 |
writebytes, filename); |
| 578 |
} |
| 579 |
|
| 580 |
/* end of ssl.c */ |