| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include "conf.h" |
| 30 |
#include "privs.h" |
| 31 |
#include "mod_tls.h" |
| 32 |
#include "json.h" |
| 33 |
|
| 34 |
#define MOD_TLS_REDIS_VERSION "mod_tls_redis/0.1" |
| 35 |
|
| 36 |
|
| 37 |
#if PROFTPD_VERSION_NUMBER < 0x0001030605 |
| 38 |
# error "ProFTPD 1.3.6rc5 or later required" |
| 39 |
#endif |
| 40 |
|
| 41 |
module tls_redis_module; |
| 42 |
|
| 43 |
|
| 44 |
static pr_redis_t *sess_redis = NULL; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#ifndef TLS_MAX_SSL_SESSION_SIZE |
| 52 |
# define TLS_MAX_SSL_SESSION_SIZE 1024 * 10 |
| 53 |
#endif |
| 54 |
|
| 55 |
struct sesscache_entry { |
| 56 |
uint32_t expires; |
| 57 |
unsigned int sess_datalen; |
| 58 |
unsigned char sess_data[TLS_MAX_SSL_SESSION_SIZE]; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
#define SESS_CACHE_JSON_KEY_EXPIRES "expires" |
| 63 |
#define SESS_CACHE_JSON_KEY_DATA "data" |
| 64 |
#define SESS_CACHE_JSON_KEY_DATA_LENGTH "data_len" |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
struct sesscache_large_entry { |
| 73 |
time_t expires; |
| 74 |
unsigned int sess_id_len; |
| 75 |
const unsigned char *sess_id; |
| 76 |
unsigned int sess_datalen; |
| 77 |
const unsigned char *sess_data; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
struct sesscache_key { |
| 84 |
const char *key; |
| 85 |
const char *desc; |
| 86 |
}; |
| 87 |
|
| 88 |
static struct sesscache_key sesscache_keys[] = { |
| 89 |
{ "cache_hits", "Cache lifetime hits" }, |
| 90 |
{ "cache_misses", "Cache lifetime misses" }, |
| 91 |
{ "cache_stores", "Cache lifetime sessions stored" }, |
| 92 |
{ "cache_deletes", "Cache lifetime sessions deleted" }, |
| 93 |
{ "cache_errors", "Cache lifetime errors handling sessions in cache" }, |
| 94 |
{ "cache_exceeds", "Cache lifetime sessions exceeding max entry size" }, |
| 95 |
{ "cache_max_sess_len", "Largest session exceeding max entry size" }, |
| 96 |
{ NULL, NULL } |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
#define SESSCACHE_KEY_HITS 0 |
| 101 |
#define SESSCACHE_KEY_MISSES 1 |
| 102 |
#define SESSCACHE_KEY_STORES 2 |
| 103 |
#define SESSCACHE_KEY_DELETES 3 |
| 104 |
#define SESSCACHE_KEY_ERRORS 4 |
| 105 |
#define SESSCACHE_KEY_EXCEEDS 5 |
| 106 |
#define SESSCACHE_KEY_MAX_LEN 6 |
| 107 |
|
| 108 |
static tls_sess_cache_t sess_cache; |
| 109 |
static array_header *sesscache_sess_list = NULL; |
| 110 |
|
| 111 |
#if defined(PR_USE_OPENSSL_OCSP) |
| 112 |
static pr_redis_t *ocsp_redis = NULL; |
| 113 |
|
| 114 |
|
| 115 |
# ifndef TLS_MAX_OCSP_RESPONSE_SIZE |
| 116 |
# define TLS_MAX_OCSP_RESPONSE_SIZE 1024 * 4 |
| 117 |
# endif |
| 118 |
|
| 119 |
struct ocspcache_entry { |
| 120 |
time_t age; |
| 121 |
unsigned int fingerprint_len; |
| 122 |
char fingerprint[EVP_MAX_MD_SIZE]; |
| 123 |
unsigned int resp_derlen; |
| 124 |
unsigned char resp_der[TLS_MAX_OCSP_RESPONSE_SIZE]; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
#define OCSP_CACHE_JSON_KEY_AGE "expires" |
| 129 |
#define OCSP_CACHE_JSON_KEY_RESPONSE "response" |
| 130 |
#define OCSP_CACHE_JSON_KEY_RESPONSE_LENGTH "response_len" |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
struct ocspcache_large_entry { |
| 139 |
time_t age; |
| 140 |
unsigned int fingerprint_len; |
| 141 |
char *fingerprint; |
| 142 |
unsigned int resp_derlen; |
| 143 |
unsigned char *resp_der; |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
struct ocspcache_key { |
| 150 |
const char *key; |
| 151 |
const char *desc; |
| 152 |
}; |
| 153 |
|
| 154 |
static struct ocspcache_key ocspcache_keys[] = { |
| 155 |
{ "cache_hits", "Cache lifetime hits" }, |
| 156 |
{ "cache_misses", "Cache lifetime misses" }, |
| 157 |
{ "cache_stores", "Cache lifetime responses stored" }, |
| 158 |
{ "cache_deletes", "Cache lifetime responses deleted" }, |
| 159 |
{ "cache_errors", "Cache lifetime errors handling responses in cache" }, |
| 160 |
{ "cache_exceeds", "Cache lifetime responses exceeding max entry size" }, |
| 161 |
{ "cache_max_resp_len", "Largest response exceeding max entry size" }, |
| 162 |
{ NULL, NULL } |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
#define OCSPCACHE_KEY_HITS 0 |
| 167 |
#define OCSPCACHE_KEY_MISSES 1 |
| 168 |
#define OCSPCACHE_KEY_STORES 2 |
| 169 |
#define OCSPCACHE_KEY_DELETES 3 |
| 170 |
#define OCSPCACHE_KEY_ERRORS 4 |
| 171 |
#define OCSPCACHE_KEY_EXCEEDS 5 |
| 172 |
#define OCSPCACHE_KEY_MAX_LEN 6 |
| 173 |
|
| 174 |
static tls_ocsp_cache_t ocsp_cache; |
| 175 |
static array_header *ocspcache_resp_list = NULL; |
| 176 |
#endif |
| 177 |
|
| 178 |
static const char *trace_channel = "tls.redis"; |
| 179 |
|
| 180 |
static int sess_cache_close(tls_sess_cache_t *); |
| 181 |
#if defined(PR_USE_OPENSSL_OCSP) |
| 182 |
static int ocsp_cache_close(tls_ocsp_cache_t *); |
| 183 |
#endif |
| 184 |
static int tls_redis_sess_init(void); |
| 185 |
|
| 186 |
static const char *redis_get_errors(void) { |
| 187 |
unsigned int count = 0; |
| 188 |
unsigned long error_code; |
| 189 |
BIO *bio = NULL; |
| 190 |
char *data = NULL; |
| 191 |
long datalen; |
| 192 |
const char *error_data = NULL, *str = "(unknown)"; |
| 193 |
int error_flags = 0; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags); |
| 200 |
if (error_code) { |
| 201 |
bio = BIO_new(BIO_s_mem()); |
| 202 |
} |
| 203 |
|
| 204 |
while (error_code) { |
| 205 |
pr_signals_handle(); |
| 206 |
|
| 207 |
if (error_flags & ERR_TXT_STRING) { |
| 208 |
BIO_printf(bio, "\n (%u) %s [%s]", ++count, |
| 209 |
ERR_error_string(error_code, NULL), error_data); |
| 210 |
|
| 211 |
} else { |
| 212 |
BIO_printf(bio, "\n (%u) %s", ++count, |
| 213 |
ERR_error_string(error_code, NULL)); |
| 214 |
} |
| 215 |
|
| 216 |
error_data = NULL; |
| 217 |
error_flags = 0; |
| 218 |
error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags); |
| 219 |
} |
| 220 |
|
| 221 |
datalen = BIO_get_mem_data(bio, &data); |
| 222 |
if (data) { |
| 223 |
data[datalen] = '\0'; |
| 224 |
str = pstrdup(permanent_pool, data); |
| 225 |
} |
| 226 |
|
| 227 |
if (bio != NULL) { |
| 228 |
BIO_free(bio); |
| 229 |
} |
| 230 |
|
| 231 |
return str; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static int sess_cache_get_json_key(pool *p, const unsigned char *sess_id, |
| 240 |
unsigned int sess_id_len, void **key, size_t *keysz) { |
| 241 |
pr_json_object_t *json; |
| 242 |
char *sess_id_hex, *json_text; |
| 243 |
|
| 244 |
sess_id_hex = pr_str_bin2hex(p, sess_id, sess_id_len, 0); |
| 245 |
json = pr_json_object_alloc(p); |
| 246 |
(void) pr_json_object_set_string(p, json, "id", sess_id_hex); |
| 247 |
|
| 248 |
json_text = pr_json_object_to_text(p, json, ""); |
| 249 |
(void) pr_json_object_free(json); |
| 250 |
|
| 251 |
|
| 252 |
*keysz = strlen(json_text) + 1; |
| 253 |
*key = pstrndup(p, json_text, *keysz - 1); |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
static int sess_cache_get_key(pool *p, const unsigned char *sess_id, |
| 259 |
unsigned int sess_id_len, void **key, size_t *keysz) { |
| 260 |
int res; |
| 261 |
|
| 262 |
res = sess_cache_get_json_key(p, sess_id, sess_id_len, key, keysz); |
| 263 |
if (res < 0) { |
| 264 |
pr_trace_msg(trace_channel, 3, |
| 265 |
"error constructing cache JSON lookup key for session ID (%lu bytes)", |
| 266 |
(unsigned long) keysz); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
static int entry_get_json_number(pool *p, pr_json_object_t *json, |
| 274 |
const char *key, double *val, const char *text) { |
| 275 |
if (pr_json_object_get_number(p, json, key, val) < 0) { |
| 276 |
if (errno == EEXIST) { |
| 277 |
pr_trace_msg(trace_channel, 3, |
| 278 |
"ignoring non-number '%s' JSON field in '%s'", key, text); |
| 279 |
|
| 280 |
} else { |
| 281 |
tls_log(MOD_TLS_REDIS_VERSION |
| 282 |
": missing required '%s' JSON field in '%s'", key, text); |
| 283 |
} |
| 284 |
|
| 285 |
(void) pr_json_object_free(json); |
| 286 |
errno = EINVAL; |
| 287 |
return -1; |
| 288 |
} |
| 289 |
|
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
static int entry_get_json_string(pool *p, pr_json_object_t *json, |
| 294 |
const char *key, char **val, const char *text) { |
| 295 |
if (pr_json_object_get_string(p, json, key, val) < 0) { |
| 296 |
if (errno == EEXIST) { |
| 297 |
pr_trace_msg(trace_channel, 3, |
| 298 |
"ignoring non-string '%s' JSON field in '%s'", key, text); |
| 299 |
|
| 300 |
} else { |
| 301 |
tls_log(MOD_TLS_REDIS_VERSION |
| 302 |
": missing required '%s' JSON field in '%s'", key, text); |
| 303 |
} |
| 304 |
|
| 305 |
(void) pr_json_object_free(json); |
| 306 |
errno = EINVAL; |
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
static int sess_cache_entry_decode_json(pool *p, void *value, size_t valuesz, |
| 314 |
struct sesscache_entry *se) { |
| 315 |
int res; |
| 316 |
pr_json_object_t *json; |
| 317 |
const char *key; |
| 318 |
char *entry, *text; |
| 319 |
double number = 0; |
| 320 |
|
| 321 |
entry = value; |
| 322 |
if (pr_json_text_validate(p, entry) == FALSE) { |
| 323 |
tls_log(MOD_TLS_REDIS_VERSION |
| 324 |
": unable to decode invalid JSON session cache entry: '%s'", entry); |
| 325 |
errno = EINVAL; |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
json = pr_json_object_from_text(p, entry); |
| 330 |
if (json == NULL) { |
| 331 |
return -1; |
| 332 |
} |
| 333 |
|
| 334 |
key = SESS_CACHE_JSON_KEY_EXPIRES; |
| 335 |
res = entry_get_json_number(p, json, key, &number, entry); |
| 336 |
if (res < 0) { |
| 337 |
int xerrno = errno; |
| 338 |
|
| 339 |
(void) pr_json_object_free(json); |
| 340 |
errno = xerrno; |
| 341 |
return -1; |
| 342 |
} |
| 343 |
se->expires = (uint32_t) number; |
| 344 |
|
| 345 |
key = SESS_CACHE_JSON_KEY_DATA; |
| 346 |
res = entry_get_json_string(p, json, key, &text, entry); |
| 347 |
if (res == 0) { |
| 348 |
pool *tmp_pool = NULL; |
| 349 |
int have_padding = FALSE; |
| 350 |
char *base64_data; |
| 351 |
size_t base64_datalen; |
| 352 |
unsigned char *data; |
| 353 |
|
| 354 |
tmp_pool = make_sub_pool(p); |
| 355 |
pr_pool_tag(tmp_pool, "TLS Redis session cache base64 data pool"); |
| 356 |
|
| 357 |
base64_data = text; |
| 358 |
base64_datalen = strlen(base64_data); |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
if (base64_data[base64_datalen-1] == '=') { |
| 367 |
have_padding = TRUE; |
| 368 |
} |
| 369 |
|
| 370 |
data = palloc(tmp_pool, base64_datalen); |
| 371 |
res = EVP_DecodeBlock(data, (unsigned char *) base64_data, |
| 372 |
(int) base64_datalen); |
| 373 |
if (res <= 0) { |
| 374 |
|
| 375 |
pr_trace_msg(trace_channel, 5, |
| 376 |
"error base64-decoding session data in '%s', rejecting", entry); |
| 377 |
(void) pr_json_object_free(json); |
| 378 |
|
| 379 |
destroy_pool(tmp_pool); |
| 380 |
errno = EINVAL; |
| 381 |
return -1; |
| 382 |
} |
| 383 |
|
| 384 |
if (have_padding == TRUE) { |
| 385 |
|
| 386 |
if (data[res-1] == '\0') { |
| 387 |
res -= 1; |
| 388 |
|
| 389 |
if (data[res-1] == '\0') { |
| 390 |
res -= 1; |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if (res <= TLS_MAX_SSL_SESSION_SIZE) { |
| 396 |
|
| 397 |
memcpy(se->sess_data, data, res); |
| 398 |
|
| 399 |
} else { |
| 400 |
tls_log(MOD_TLS_REDIS_VERSION |
| 401 |
": decoded JSON session cache entry too large, ignoring"); |
| 402 |
(void) pr_json_object_free(json); |
| 403 |
|
| 404 |
destroy_pool(tmp_pool); |
| 405 |
errno = EINVAL; |
| 406 |
return -1; |
| 407 |
} |
| 408 |
|
| 409 |
destroy_pool(tmp_pool); |
| 410 |
|
| 411 |
} else { |
| 412 |
int xerrno = errno; |
| 413 |
|
| 414 |
(void) pr_json_object_free(json); |
| 415 |
errno = xerrno; |
| 416 |
return -1; |
| 417 |
} |
| 418 |
|
| 419 |
key = SESS_CACHE_JSON_KEY_DATA_LENGTH; |
| 420 |
res = entry_get_json_number(p, json, key, &number, entry); |
| 421 |
if (res < 0) { |
| 422 |
int xerrno = errno; |
| 423 |
|
| 424 |
(void) pr_json_object_free(json); |
| 425 |
errno = xerrno; |
| 426 |
return -1; |
| 427 |
} |
| 428 |
se->sess_datalen = (unsigned int) number; |
| 429 |
|
| 430 |
(void) pr_json_object_free(json); |
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
static int sess_cache_redis_entry_get(pool *p, const unsigned char *sess_id, |
| 435 |
unsigned int sess_id_len, struct sesscache_entry *se) { |
| 436 |
int res; |
| 437 |
void *key = NULL, *value = NULL; |
| 438 |
size_t keysz = 0, valuesz = 0; |
| 439 |
|
| 440 |
res = sess_cache_get_key(p, sess_id, sess_id_len, &key, &keysz); |
| 441 |
if (res < 0) { |
| 442 |
pr_trace_msg(trace_channel, 1, |
| 443 |
"unable to get cache entry: error getting cache key: %s", |
| 444 |
strerror(errno)); |
| 445 |
|
| 446 |
return -1; |
| 447 |
} |
| 448 |
|
| 449 |
value = pr_redis_kget(p, sess_redis, &tls_redis_module, (const char *) key, |
| 450 |
keysz, &valuesz); |
| 451 |
if (value == NULL) { |
| 452 |
pr_trace_msg(trace_channel, 3, |
| 453 |
"no matching Redis entry found for session ID (%lu bytes)", |
| 454 |
(unsigned long) keysz); |
| 455 |
errno = ENOENT; |
| 456 |
return -1; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
res = sess_cache_entry_decode_json(p, value, valuesz, se); |
| 461 |
if (res == 0) { |
| 462 |
time_t now; |
| 463 |
|
| 464 |
|
| 465 |
time(&now); |
| 466 |
|
| 467 |
if (se->expires <= now) { |
| 468 |
pr_trace_msg(trace_channel, 4, |
| 469 |
"ignoring expired cached session data (expires %lu <= now %lu)", |
| 470 |
(unsigned long) se->expires, (unsigned long) now); |
| 471 |
errno = EPERM; |
| 472 |
return -1; |
| 473 |
} |
| 474 |
|
| 475 |
pr_trace_msg(trace_channel, 9, "retrieved JSON session data from cache"); |
| 476 |
} |
| 477 |
|
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
static int sess_cache_redis_entry_delete(pool *p, const unsigned char *sess_id, |
| 482 |
unsigned int sess_id_len) { |
| 483 |
int res; |
| 484 |
void *key = NULL; |
| 485 |
size_t keysz = 0; |
| 486 |
|
| 487 |
res = sess_cache_get_key(p, sess_id, sess_id_len, &key, &keysz); |
| 488 |
if (res < 0) { |
| 489 |
pr_trace_msg(trace_channel, 1, |
| 490 |
"unable to remove cache entry: error getting cache key: %s", |
| 491 |
strerror(errno)); |
| 492 |
|
| 493 |
return -1; |
| 494 |
} |
| 495 |
|
| 496 |
res = pr_redis_kremove(sess_redis, &tls_redis_module, (const char *) key, |
| 497 |
keysz); |
| 498 |
if (res < 0) { |
| 499 |
int xerrno = errno; |
| 500 |
|
| 501 |
pr_trace_msg(trace_channel, 2, |
| 502 |
"unable to remove Redis entry for session ID (%lu bytes): %s", |
| 503 |
(unsigned long) keysz, strerror(xerrno)); |
| 504 |
|
| 505 |
errno = xerrno; |
| 506 |
return -1; |
| 507 |
} |
| 508 |
|
| 509 |
return 0; |
| 510 |
} |
| 511 |
|
| 512 |
static int sess_cache_entry_encode_json(pool *p, void **value, size_t *valuesz, |
| 513 |
struct sesscache_entry *se) { |
| 514 |
pr_json_object_t *json; |
| 515 |
pool *tmp_pool; |
| 516 |
char *base64_data = NULL, *json_text; |
| 517 |
|
| 518 |
json = pr_json_object_alloc(p); |
| 519 |
(void) pr_json_object_set_number(p, json, SESS_CACHE_JSON_KEY_EXPIRES, |
| 520 |
(double) se->expires); |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
tmp_pool = make_sub_pool(p); |
| 526 |
base64_data = pcalloc(tmp_pool, se->sess_datalen * 2); |
| 527 |
|
| 528 |
EVP_EncodeBlock((unsigned char *) base64_data, se->sess_data, |
| 529 |
(int) se->sess_datalen); |
| 530 |
(void) pr_json_object_set_string(p, json, SESS_CACHE_JSON_KEY_DATA, |
| 531 |
base64_data); |
| 532 |
(void) pr_json_object_set_number(p, json, SESS_CACHE_JSON_KEY_DATA_LENGTH, |
| 533 |
(double) se->sess_datalen); |
| 534 |
destroy_pool(tmp_pool); |
| 535 |
|
| 536 |
json_text = pr_json_object_to_text(p, json, ""); |
| 537 |
(void) pr_json_object_free(json); |
| 538 |
|
| 539 |
|
| 540 |
if (pr_json_text_validate(p, json_text) == FALSE) { |
| 541 |
pr_trace_msg(trace_channel, 1, "invalid JSON emitted: '%s'", json_text); |
| 542 |
errno = EINVAL; |
| 543 |
return -1; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
*valuesz = strlen(json_text) + 1; |
| 548 |
*value = pstrndup(p, json_text, *valuesz - 1); |
| 549 |
|
| 550 |
return 0; |
| 551 |
} |
| 552 |
|
| 553 |
static int sess_cache_redis_entry_set(pool *p, const unsigned char *sess_id, |
| 554 |
unsigned int sess_id_len, struct sesscache_entry *se) { |
| 555 |
int res, xerrno = 0; |
| 556 |
void *key = NULL, *value = NULL; |
| 557 |
size_t keysz = 0, valuesz = 0; |
| 558 |
|
| 559 |
|
| 560 |
res = sess_cache_entry_encode_json(p, &value, &valuesz, se); |
| 561 |
if (res < 0) { |
| 562 |
xerrno = errno; |
| 563 |
|
| 564 |
pr_trace_msg(trace_channel, 4, "error JSON encoding session data: %s", |
| 565 |
strerror(xerrno)); |
| 566 |
|
| 567 |
errno = xerrno; |
| 568 |
return -1; |
| 569 |
} |
| 570 |
|
| 571 |
res = sess_cache_get_key(p, sess_id, sess_id_len, &key, &keysz); |
| 572 |
xerrno = errno; |
| 573 |
if (res < 0) { |
| 574 |
pr_trace_msg(trace_channel, 1, |
| 575 |
"unable to set cache entry: error getting cache key: %s", |
| 576 |
strerror(xerrno)); |
| 577 |
|
| 578 |
errno = xerrno; |
| 579 |
return -1; |
| 580 |
} |
| 581 |
|
| 582 |
res = pr_redis_kset(sess_redis, &tls_redis_module, (const char *) key, |
| 583 |
keysz, value, valuesz, se->expires); |
| 584 |
xerrno = errno; |
| 585 |
|
| 586 |
if (res < 0) { |
| 587 |
pr_trace_msg(trace_channel, 2, |
| 588 |
"unable to add Redis entry for session ID (%lu bytes): %s", |
| 589 |
(unsigned long) keysz, strerror(xerrno)); |
| 590 |
|
| 591 |
errno = xerrno; |
| 592 |
return -1; |
| 593 |
} |
| 594 |
|
| 595 |
pr_trace_msg(trace_channel, 9, "stored JSON session data in cache"); |
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
static int sess_cache_open(tls_sess_cache_t *cache, char *info, long timeout) { |
| 600 |
config_rec *c; |
| 601 |
|
| 602 |
cache->cache_pool = make_sub_pool(permanent_pool); |
| 603 |
pr_pool_tag(cache->cache_pool, MOD_TLS_REDIS_VERSION); |
| 604 |
|
| 605 |
pr_trace_msg(trace_channel, 9, "opening Redis cache %p (info '%s')", |
| 606 |
cache, info ? info : "(none)"); |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
c = find_config(main_server->conf, CONF_PARAM, "RedisEngine", FALSE); |
| 614 |
if (c != NULL) { |
| 615 |
int engine; |
| 616 |
|
| 617 |
engine = *((int *) c->argv[0]); |
| 618 |
if (engine == FALSE) { |
| 619 |
pr_trace_msg(trace_channel, 2, "%s", |
| 620 |
"Redis support disabled (see RedisEngine directive)"); |
| 621 |
errno = EPERM; |
| 622 |
return -1; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
sess_redis = pr_redis_conn_new(cache->cache_pool, &tls_redis_module, 0); |
| 627 |
if (sess_redis == NULL) { |
| 628 |
pr_trace_msg(trace_channel, 2, |
| 629 |
"error connecting to Redis: %s", strerror(errno)); |
| 630 |
errno = EPERM; |
| 631 |
return -1; |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
if (pr_redis_conn_set_namespace(sess_redis, &tls_redis_module, |
| 636 |
"mod_tls_redis.sessions.", 23) < 0) { |
| 637 |
pr_trace_msg(trace_channel, 2, |
| 638 |
"error setting Redis namespace prefix: %s", strerror(errno)); |
| 639 |
} |
| 640 |
|
| 641 |
cache->cache_timeout = timeout; |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
static int sess_cache_close(tls_sess_cache_t *cache) { |
| 646 |
pr_trace_msg(trace_channel, 9, "closing Redis session cache %p", cache); |
| 647 |
|
| 648 |
if (cache != NULL && |
| 649 |
cache->cache_pool != NULL) { |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
if (sesscache_sess_list != NULL) { |
| 657 |
register unsigned int i; |
| 658 |
struct sesscache_large_entry *entries; |
| 659 |
|
| 660 |
entries = sesscache_sess_list->elts; |
| 661 |
for (i = 0; i < sesscache_sess_list->nelts; i++) { |
| 662 |
struct sesscache_large_entry *entry; |
| 663 |
|
| 664 |
entry = &(entries[i]); |
| 665 |
if (entry->expires > 0) { |
| 666 |
pr_memscrub((void *) entry->sess_data, entry->sess_datalen); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
clear_array(sesscache_sess_list); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
static int sess_cache_add_large_sess(tls_sess_cache_t *cache, |
| 678 |
const unsigned char *sess_id, unsigned int sess_id_len, time_t expires, |
| 679 |
SSL_SESSION *sess, int sess_len) { |
| 680 |
struct sesscache_large_entry *entry = NULL; |
| 681 |
|
| 682 |
if (sess_len > TLS_MAX_SSL_SESSION_SIZE) { |
| 683 |
int res; |
| 684 |
const char *exceeds_key = sesscache_keys[SESSCACHE_KEY_EXCEEDS].key, |
| 685 |
*max_len_key = sesscache_keys[SESSCACHE_KEY_MAX_LEN].key; |
| 686 |
void *value = NULL; |
| 687 |
size_t valuesz = 0; |
| 688 |
pool *tmp_pool; |
| 689 |
|
| 690 |
res = pr_redis_incr(sess_redis, &tls_redis_module, exceeds_key, 1, NULL); |
| 691 |
if (res < 0) { |
| 692 |
pr_trace_msg(trace_channel, 2, |
| 693 |
"error incrementing '%s' value: %s", exceeds_key, strerror(errno)); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
tmp_pool = make_sub_pool(cache->cache_pool); |
| 701 |
value = pr_redis_get(tmp_pool, sess_redis, &tls_redis_module, max_len_key, |
| 702 |
&valuesz); |
| 703 |
if (value != NULL) { |
| 704 |
uint64_t max_len; |
| 705 |
|
| 706 |
memcpy(&max_len, value, valuesz); |
| 707 |
if ((uint64_t) sess_len > max_len) { |
| 708 |
if (pr_redis_set(sess_redis, &tls_redis_module, max_len_key, &max_len, |
| 709 |
sizeof(max_len), 0) < 0) { |
| 710 |
pr_trace_msg(trace_channel, 2, |
| 711 |
"error setting '%s' value: %s", max_len_key, strerror(errno)); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
} else { |
| 716 |
pr_trace_msg(trace_channel, 2, |
| 717 |
"error getting '%s' value: %s", max_len_key, strerror(errno)); |
| 718 |
} |
| 719 |
|
| 720 |
destroy_pool(tmp_pool); |
| 721 |
} |
| 722 |
|
| 723 |
if (sesscache_sess_list != NULL) { |
| 724 |
register unsigned int i; |
| 725 |
struct sesscache_large_entry *entries; |
| 726 |
time_t now; |
| 727 |
int ok = FALSE; |
| 728 |
|
| 729 |
|
| 730 |
entries = sesscache_sess_list->elts; |
| 731 |
time(&now); |
| 732 |
for (i = 0; i < sesscache_sess_list->nelts; i++) { |
| 733 |
entry = &(entries[i]); |
| 734 |
|
| 735 |
if (entry->expires <= now) { |
| 736 |
|
| 737 |
entry->expires = 0; |
| 738 |
pr_memscrub((void *) entry->sess_data, entry->sess_datalen); |
| 739 |
|
| 740 |
ok = TRUE; |
| 741 |
break; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
if (!ok) { |
| 746 |
|
| 747 |
entry = push_array(sesscache_sess_list); |
| 748 |
} |
| 749 |
|
| 750 |
} else { |
| 751 |
sesscache_sess_list = make_array(cache->cache_pool, 1, |
| 752 |
sizeof(struct sesscache_large_entry)); |
| 753 |
entry = push_array(sesscache_sess_list); |
| 754 |
} |
| 755 |
|
| 756 |
entry->expires = expires; |
| 757 |
entry->sess_id_len = sess_id_len; |
| 758 |
entry->sess_id = palloc(cache->cache_pool, sess_id_len); |
| 759 |
memcpy((unsigned char *) entry->sess_id, sess_id, sess_id_len); |
| 760 |
entry->sess_datalen = sess_len; |
| 761 |
entry->sess_data = palloc(cache->cache_pool, sess_len); |
| 762 |
i2d_SSL_SESSION(sess, (unsigned char **) &(entry->sess_data)); |
| 763 |
|
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
static int sess_cache_add(tls_sess_cache_t *cache, const unsigned char *sess_id, |
| 768 |
unsigned int sess_id_len, time_t expires, SSL_SESSION *sess) { |
| 769 |
struct sesscache_entry entry; |
| 770 |
int sess_len; |
| 771 |
unsigned char *ptr; |
| 772 |
time_t now; |
| 773 |
|
| 774 |
time(&now); |
| 775 |
pr_trace_msg(trace_channel, 9, |
| 776 |
"adding session to Redis cache %p (expires = %lu, now = %lu)", cache, |
| 777 |
(unsigned long) expires, (unsigned long) now); |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
sess_len = i2d_SSL_SESSION(sess, NULL); |
| 784 |
if (sess_len > TLS_MAX_SSL_SESSION_SIZE) { |
| 785 |
pr_trace_msg(trace_channel, 2, |
| 786 |
"length of serialized SSL session data (%d) exceeds maximum size (%u), " |
| 787 |
"unable to add to shared Redis, adding to list", sess_len, |
| 788 |
TLS_MAX_SSL_SESSION_SIZE); |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
return sess_cache_add_large_sess(cache, sess_id, sess_id_len, expires, |
| 796 |
sess, sess_len); |
| 797 |
} |
| 798 |
|
| 799 |
entry.expires = expires; |
| 800 |
entry.sess_datalen = sess_len; |
| 801 |
ptr = entry.sess_data; |
| 802 |
i2d_SSL_SESSION(sess, &ptr); |
| 803 |
|
| 804 |
if (sess_cache_redis_entry_set(cache->cache_pool, sess_id, sess_id_len, |
| 805 |
&entry) < 0) { |
| 806 |
pr_trace_msg(trace_channel, 2, |
| 807 |
"error adding session to Redis: %s", strerror(errno)); |
| 808 |
|
| 809 |
|
| 810 |
return sess_cache_add_large_sess(cache, sess_id, sess_id_len, expires, |
| 811 |
sess, sess_len); |
| 812 |
|
| 813 |
} else { |
| 814 |
const char *key = sesscache_keys[SESSCACHE_KEY_STORES].key; |
| 815 |
|
| 816 |
if (pr_redis_incr(sess_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 817 |
pr_trace_msg(trace_channel, 2, |
| 818 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
return 0; |
| 823 |
} |
| 824 |
|
| 825 |
static SSL_SESSION *sess_cache_get(tls_sess_cache_t *cache, |
| 826 |
const unsigned char *sess_id, unsigned int sess_id_len) { |
| 827 |
struct sesscache_entry entry; |
| 828 |
time_t now; |
| 829 |
SSL_SESSION *sess = NULL; |
| 830 |
|
| 831 |
pr_trace_msg(trace_channel, 9, "getting session from Redis cache %p", cache); |
| 832 |
|
| 833 |
|
| 834 |
if (sesscache_sess_list != NULL) { |
| 835 |
register unsigned int i; |
| 836 |
struct sesscache_large_entry *entries; |
| 837 |
|
| 838 |
entries = sesscache_sess_list->elts; |
| 839 |
for (i = 0; i < sesscache_sess_list->nelts; i++) { |
| 840 |
struct sesscache_large_entry *large_entry; |
| 841 |
|
| 842 |
large_entry = &(entries[i]); |
| 843 |
if (large_entry->expires > 0 && |
| 844 |
large_entry->sess_id_len == sess_id_len && |
| 845 |
memcmp(large_entry->sess_id, sess_id, |
| 846 |
large_entry->sess_id_len) == 0) { |
| 847 |
|
| 848 |
now = time(NULL); |
| 849 |
if (large_entry->expires > now) { |
| 850 |
TLS_D2I_SSL_SESSION_CONST unsigned char *ptr; |
| 851 |
|
| 852 |
ptr = large_entry->sess_data; |
| 853 |
sess = d2i_SSL_SESSION(NULL, &ptr, large_entry->sess_datalen); |
| 854 |
if (sess == NULL) { |
| 855 |
pr_trace_msg(trace_channel, 2, |
| 856 |
"error retrieving session from cache: %s", redis_get_errors()); |
| 857 |
|
| 858 |
} else { |
| 859 |
break; |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
if (sess != NULL) { |
| 867 |
return sess; |
| 868 |
} |
| 869 |
|
| 870 |
if (sess_cache_redis_entry_get(cache->cache_pool, sess_id, sess_id_len, |
| 871 |
&entry) < 0) { |
| 872 |
return NULL; |
| 873 |
} |
| 874 |
|
| 875 |
now = time(NULL); |
| 876 |
if (entry.expires > now) { |
| 877 |
TLS_D2I_SSL_SESSION_CONST unsigned char *ptr; |
| 878 |
|
| 879 |
ptr = entry.sess_data; |
| 880 |
sess = d2i_SSL_SESSION(NULL, &ptr, entry.sess_datalen); |
| 881 |
if (sess != NULL) { |
| 882 |
const char *key = sesscache_keys[SESSCACHE_KEY_HITS].key; |
| 883 |
|
| 884 |
if (pr_redis_incr(sess_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 885 |
pr_trace_msg(trace_channel, 2, |
| 886 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 887 |
} |
| 888 |
|
| 889 |
} else { |
| 890 |
const char *key = sesscache_keys[SESSCACHE_KEY_ERRORS].key; |
| 891 |
|
| 892 |
pr_trace_msg(trace_channel, 2, |
| 893 |
"error retrieving session from cache: %s", redis_get_errors()); |
| 894 |
|
| 895 |
if (pr_redis_incr(sess_redis, &tls_redis_module, key, 1, |
| 896 |
NULL) < 0) { |
| 897 |
pr_trace_msg(trace_channel, 2, |
| 898 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 899 |
} |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
if (sess == NULL) { |
| 904 |
const char *key = sesscache_keys[SESSCACHE_KEY_MISSES].key; |
| 905 |
|
| 906 |
if (pr_redis_incr(sess_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 907 |
pr_trace_msg(trace_channel, 2, |
| 908 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 909 |
} |
| 910 |
|
| 911 |
errno = ENOENT; |
| 912 |
} |
| 913 |
|
| 914 |
return sess; |
| 915 |
} |
| 916 |
|
| 917 |
static int sess_cache_delete(tls_sess_cache_t *cache, |
| 918 |
const unsigned char *sess_id, unsigned int sess_id_len) { |
| 919 |
const char *key = sesscache_keys[SESSCACHE_KEY_DELETES].key; |
| 920 |
int res; |
| 921 |
|
| 922 |
pr_trace_msg(trace_channel, 9, "removing session from Redis cache %p", cache); |
| 923 |
|
| 924 |
|
| 925 |
if (sesscache_sess_list != NULL) { |
| 926 |
register unsigned int i; |
| 927 |
struct sesscache_large_entry *entries; |
| 928 |
|
| 929 |
entries = sesscache_sess_list->elts; |
| 930 |
for (i = 0; i < sesscache_sess_list->nelts; i++) { |
| 931 |
struct sesscache_large_entry *entry; |
| 932 |
|
| 933 |
entry = &(entries[i]); |
| 934 |
if (entry->sess_id_len == sess_id_len && |
| 935 |
memcmp(entry->sess_id, sess_id, entry->sess_id_len) == 0) { |
| 936 |
|
| 937 |
pr_memscrub((void *) entry->sess_data, entry->sess_datalen); |
| 938 |
entry->expires = 0; |
| 939 |
return 0; |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
res = sess_cache_redis_entry_delete(cache->cache_pool, sess_id, sess_id_len); |
| 945 |
if (res < 0) { |
| 946 |
return -1; |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
if (pr_redis_incr(sess_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 952 |
pr_trace_msg(trace_channel, 2, |
| 953 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 954 |
} |
| 955 |
|
| 956 |
return res; |
| 957 |
} |
| 958 |
|
| 959 |
static int sess_cache_clear(tls_sess_cache_t *cache) { |
| 960 |
register unsigned int i; |
| 961 |
int res = 0; |
| 962 |
|
| 963 |
if (sess_redis == NULL) { |
| 964 |
pr_trace_msg(trace_channel, 9, "missing required Redis connection"); |
| 965 |
errno = EINVAL; |
| 966 |
return -1; |
| 967 |
} |
| 968 |
|
| 969 |
pr_trace_msg(trace_channel, 9, "clearing Redis session cache %p", cache); |
| 970 |
|
| 971 |
if (sesscache_sess_list != NULL) { |
| 972 |
struct sesscache_large_entry *entries; |
| 973 |
|
| 974 |
entries = sesscache_sess_list->elts; |
| 975 |
for (i = 0; i < sesscache_sess_list->nelts; i++) { |
| 976 |
struct sesscache_large_entry *entry; |
| 977 |
|
| 978 |
entry = &(entries[i]); |
| 979 |
entry->expires = 0; |
| 980 |
pr_memscrub((void *) entry->sess_data, entry->sess_datalen); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
return res; |
| 987 |
} |
| 988 |
|
| 989 |
static int sess_cache_remove(tls_sess_cache_t *cache) { |
| 990 |
int res; |
| 991 |
|
| 992 |
pr_trace_msg(trace_channel, 9, "removing Redis session cache %p", cache); |
| 993 |
|
| 994 |
res = sess_cache_clear(cache); |
| 995 |
|
| 996 |
|
| 997 |
return res; |
| 998 |
} |
| 999 |
|
| 1000 |
static int sess_cache_status(tls_sess_cache_t *cache, |
| 1001 |
void (*statusf)(void *, const char *, ...), void *arg, int flags) { |
| 1002 |
register unsigned int i; |
| 1003 |
pool *tmp_pool; |
| 1004 |
|
| 1005 |
pr_trace_msg(trace_channel, 9, "checking Redis session cache %p", cache); |
| 1006 |
|
| 1007 |
tmp_pool = make_sub_pool(permanent_pool); |
| 1008 |
|
| 1009 |
statusf(arg, "%s", "Redis SSL session cache provided by " |
| 1010 |
MOD_TLS_REDIS_VERSION); |
| 1011 |
statusf(arg, "%s", ""); |
| 1012 |
statusf(arg, "Redis server: "); |
| 1013 |
|
| 1014 |
for (i = 0; sesscache_keys[i].key != NULL; i++) { |
| 1015 |
const char *key, *desc; |
| 1016 |
void *value = NULL; |
| 1017 |
size_t valuesz = 0; |
| 1018 |
|
| 1019 |
key = sesscache_keys[i].key; |
| 1020 |
desc = sesscache_keys[i].desc; |
| 1021 |
|
| 1022 |
value = pr_redis_get(tmp_pool, sess_redis, &tls_redis_module, key, |
| 1023 |
&valuesz); |
| 1024 |
if (value != NULL) { |
| 1025 |
uint64_t num = 0; |
| 1026 |
memcpy(&num, value, valuesz); |
| 1027 |
statusf(arg, "%s: %lu", desc, (unsigned long) num); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
#if 0 |
| 1034 |
if (flags & TLS_SESS_CACHE_STATUS_FL_SHOW_SESSIONS) { |
| 1035 |
statusf(arg, "%s", ""); |
| 1036 |
statusf(arg, "%s", "Cached sessions:"); |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
for (i = 0; i < 0; i++) { |
| 1051 |
struct sesscache_entry *entry; |
| 1052 |
|
| 1053 |
pr_signals_handle(); |
| 1054 |
|
| 1055 |
|
| 1056 |
if (entry->expires > 0) { |
| 1057 |
SSL_SESSION *sess; |
| 1058 |
TLS_D2I_SSL_SESSION_CONST unsigned char *ptr; |
| 1059 |
time_t ts; |
| 1060 |
|
| 1061 |
ptr = entry->sess_data; |
| 1062 |
sess = d2i_SSL_SESSION(NULL, &ptr, entry->sess_datalen); |
| 1063 |
if (sess == NULL) { |
| 1064 |
pr_log_pri(PR_LOG_NOTICE, MOD_TLS_REDIS_VERSION |
| 1065 |
": error retrieving session from cache: %s", redis_get_errors()); |
| 1066 |
continue; |
| 1067 |
} |
| 1068 |
|
| 1069 |
statusf(arg, "%s", " -----BEGIN SSL SESSION PARAMETERS-----"); |
| 1070 |
|
| 1071 |
|
| 1072 |
if (sess->session_id_length > 0) { |
| 1073 |
char *sess_id_str; |
| 1074 |
|
| 1075 |
sess_id_str = pr_str2hex(tmp_pool, sess->session_id, |
| 1076 |
sess->session_id_length, PR_STR_FL_HEX_USE_UC); |
| 1077 |
|
| 1078 |
statusf(arg, " Session ID: %s", sess_id_str); |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (sess->sid_ctx_length > 0) { |
| 1082 |
char *sid_ctx_str; |
| 1083 |
|
| 1084 |
sid_ctx_str = pr_str2hex(tmp_pool, sess->sid_ctx, |
| 1085 |
sess->sid_ctx_length, PR_STR_FL_HEX_USE_UC); |
| 1086 |
|
| 1087 |
statusf(arg, " Session ID Context: %s", sid_ctx_str); |
| 1088 |
} |
| 1089 |
|
| 1090 |
switch (sess->ssl_version) { |
| 1091 |
case SSL3_VERSION: |
| 1092 |
statusf(arg, " Protocol: %s", "SSLv3"); |
| 1093 |
break; |
| 1094 |
|
| 1095 |
case TLS1_VERSION: |
| 1096 |
statusf(arg, " Protocol: %s", "TLSv1"); |
| 1097 |
break; |
| 1098 |
|
| 1099 |
#if defined(TLS1_1_VERSION) |
| 1100 |
case TLS1_1_VERSION: |
| 1101 |
statusf(arg, " Protocol: %s", "TLSv1.1"); |
| 1102 |
break; |
| 1103 |
#endif |
| 1104 |
|
| 1105 |
#if defined(TLS1_2_VERSION) |
| 1106 |
case TLS1_2_VERSION: |
| 1107 |
statusf(arg, " Protocol: %s", "TLSv1.2"); |
| 1108 |
break; |
| 1109 |
#endif |
| 1110 |
|
| 1111 |
#if defined(TLS1_3_VERSION) |
| 1112 |
case TLS1_3_VERSION: |
| 1113 |
statusf(arg, " Protocol: %s", "TLSv1.3"); |
| 1114 |
break; |
| 1115 |
#endif |
| 1116 |
|
| 1117 |
default: |
| 1118 |
statusf(arg, " Protocol: %s", "unknown"); |
| 1119 |
} |
| 1120 |
|
| 1121 |
ts = SSL_SESSION_get_time(sess); |
| 1122 |
statusf(arg, " Started: %s", pr_strtime3(tmp_pool, ts, FALSE)); |
| 1123 |
ts = entry->expires; |
| 1124 |
statusf(arg, " Expires: %s (%u secs)", |
| 1125 |
pr_strtime3(tmp_pool, ts, FALSE), SSL_SESSION_get_timeout(sess)); |
| 1126 |
|
| 1127 |
SSL_SESSION_free(sess); |
| 1128 |
statusf(arg, "%s", " -----END SSL SESSION PARAMETERS-----"); |
| 1129 |
statusf(arg, "%s", ""); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
#endif |
| 1134 |
|
| 1135 |
destroy_pool(tmp_pool); |
| 1136 |
return 0; |
| 1137 |
} |
| 1138 |
|
| 1139 |
#if defined(PR_USE_OPENSSL_OCSP) |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
static int ocsp_cache_get_json_key(pool *p, const char *fingerprint, |
| 1146 |
void **key, size_t *keysz) { |
| 1147 |
pr_json_object_t *json; |
| 1148 |
char *json_text; |
| 1149 |
|
| 1150 |
json = pr_json_object_alloc(p); |
| 1151 |
(void) pr_json_object_set_string(p, json, "fingerprint", fingerprint); |
| 1152 |
|
| 1153 |
json_text = pr_json_object_to_text(p, json, ""); |
| 1154 |
(void) pr_json_object_free(json); |
| 1155 |
|
| 1156 |
|
| 1157 |
*keysz = strlen(json_text) + 1; |
| 1158 |
*key = pstrndup(p, json_text, *keysz - 1); |
| 1159 |
|
| 1160 |
return 0; |
| 1161 |
} |
| 1162 |
|
| 1163 |
static int ocsp_cache_get_key(pool *p, const char *fingerprint, void **key, |
| 1164 |
size_t *keysz) { |
| 1165 |
int res; |
| 1166 |
|
| 1167 |
res = ocsp_cache_get_json_key(p, fingerprint, key, keysz); |
| 1168 |
if (res < 0) { |
| 1169 |
pr_trace_msg(trace_channel, 3, |
| 1170 |
"error constructing ocsp cache JSON lookup key for fingerprint '%s'", |
| 1171 |
fingerprint); |
| 1172 |
return -1; |
| 1173 |
} |
| 1174 |
|
| 1175 |
return 0; |
| 1176 |
} |
| 1177 |
|
| 1178 |
static int ocsp_cache_entry_decode_json(pool *p, void *value, size_t valuesz, |
| 1179 |
struct ocspcache_entry *oe) { |
| 1180 |
int res; |
| 1181 |
pr_json_object_t *json; |
| 1182 |
const char *key; |
| 1183 |
char *entry, *text; |
| 1184 |
double number = 0; |
| 1185 |
|
| 1186 |
entry = value; |
| 1187 |
if (pr_json_text_validate(p, entry) == FALSE) { |
| 1188 |
tls_log(MOD_TLS_REDIS_VERSION |
| 1189 |
": unable to decode invalid JSON ocsp cache entry: '%s'", entry); |
| 1190 |
errno = EINVAL; |
| 1191 |
return -1; |
| 1192 |
} |
| 1193 |
|
| 1194 |
json = pr_json_object_from_text(p, entry); |
| 1195 |
if (json == NULL) { |
| 1196 |
return -1; |
| 1197 |
} |
| 1198 |
|
| 1199 |
key = OCSP_CACHE_JSON_KEY_AGE; |
| 1200 |
res = entry_get_json_number(p, json, key, &number, entry); |
| 1201 |
if (res < 0) { |
| 1202 |
int xerrno = errno; |
| 1203 |
|
| 1204 |
(void) pr_json_object_free(json); |
| 1205 |
errno = xerrno; |
| 1206 |
return -1; |
| 1207 |
} |
| 1208 |
oe->age = (uint32_t) number; |
| 1209 |
|
| 1210 |
key = OCSP_CACHE_JSON_KEY_RESPONSE; |
| 1211 |
res = entry_get_json_string(p, json, key, &text, entry); |
| 1212 |
if (res == 0) { |
| 1213 |
pool *tmp_pool = NULL; |
| 1214 |
int have_padding = FALSE; |
| 1215 |
char *base64_data; |
| 1216 |
size_t base64_datalen; |
| 1217 |
unsigned char *data; |
| 1218 |
|
| 1219 |
tmp_pool = make_sub_pool(p); |
| 1220 |
pr_pool_tag(tmp_pool, "TLS Redis OCSP cache base64 data pool"); |
| 1221 |
|
| 1222 |
base64_data = text; |
| 1223 |
base64_datalen = strlen(base64_data); |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
if (base64_data[base64_datalen-1] == '=') { |
| 1232 |
have_padding = TRUE; |
| 1233 |
} |
| 1234 |
|
| 1235 |
data = palloc(tmp_pool, base64_datalen); |
| 1236 |
res = EVP_DecodeBlock(data, (unsigned char *) base64_data, |
| 1237 |
(int) base64_datalen); |
| 1238 |
if (res <= 0) { |
| 1239 |
|
| 1240 |
pr_trace_msg(trace_channel, 5, |
| 1241 |
"error base64-decoding OCSP data in '%s', rejecting", entry); |
| 1242 |
(void) pr_json_object_free(json); |
| 1243 |
|
| 1244 |
destroy_pool(tmp_pool); |
| 1245 |
errno = EINVAL; |
| 1246 |
return -1; |
| 1247 |
} |
| 1248 |
|
| 1249 |
if (have_padding == TRUE) { |
| 1250 |
|
| 1251 |
if (data[res-1] == '\0') { |
| 1252 |
res -= 1; |
| 1253 |
|
| 1254 |
if (data[res-1] == '\0') { |
| 1255 |
res -= 1; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
if (res <= TLS_MAX_OCSP_RESPONSE_SIZE) { |
| 1261 |
|
| 1262 |
memcpy(oe->resp_der, data, res); |
| 1263 |
|
| 1264 |
} else { |
| 1265 |
tls_log(MOD_TLS_REDIS_VERSION |
| 1266 |
": decoded JSON OCSP cache entry too large, ignoring"); |
| 1267 |
(void) pr_json_object_free(json); |
| 1268 |
|
| 1269 |
destroy_pool(tmp_pool); |
| 1270 |
errno = EINVAL; |
| 1271 |
return -1; |
| 1272 |
} |
| 1273 |
|
| 1274 |
destroy_pool(tmp_pool); |
| 1275 |
|
| 1276 |
} else { |
| 1277 |
int xerrno = errno; |
| 1278 |
|
| 1279 |
(void) pr_json_object_free(json); |
| 1280 |
errno = xerrno; |
| 1281 |
return -1; |
| 1282 |
} |
| 1283 |
|
| 1284 |
key = OCSP_CACHE_JSON_KEY_RESPONSE_LENGTH; |
| 1285 |
res = entry_get_json_number(p, json, key, &number, entry); |
| 1286 |
if (res < 0) { |
| 1287 |
int xerrno = errno; |
| 1288 |
|
| 1289 |
(void) pr_json_object_free(json); |
| 1290 |
errno = xerrno; |
| 1291 |
return -1; |
| 1292 |
} |
| 1293 |
oe->resp_derlen = (unsigned int) number; |
| 1294 |
|
| 1295 |
(void) pr_json_object_free(json); |
| 1296 |
return 0; |
| 1297 |
} |
| 1298 |
|
| 1299 |
static int ocsp_cache_redis_entry_get(pool *p, const char *fingerprint, |
| 1300 |
struct ocspcache_entry *oe) { |
| 1301 |
int res; |
| 1302 |
void *key = NULL, *value = NULL; |
| 1303 |
size_t keysz = 0, valuesz = 0; |
| 1304 |
|
| 1305 |
res = ocsp_cache_get_key(p, fingerprint, &key, &keysz); |
| 1306 |
if (res < 0) { |
| 1307 |
pr_trace_msg(trace_channel, 1, |
| 1308 |
"unable to get ocsp cache entry: error getting cache key: %s", |
| 1309 |
strerror(errno)); |
| 1310 |
|
| 1311 |
return -1; |
| 1312 |
} |
| 1313 |
|
| 1314 |
value = pr_redis_kget(p, ocsp_redis, &tls_redis_module, (const char *) key, |
| 1315 |
keysz, &valuesz); |
| 1316 |
if (value == NULL) { |
| 1317 |
pr_trace_msg(trace_channel, 3, |
| 1318 |
"no matching Redis entry found for fingerprint '%s'", fingerprint); |
| 1319 |
errno = ENOENT; |
| 1320 |
return -1; |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
res = ocsp_cache_entry_decode_json(p, value, valuesz, oe); |
| 1325 |
if (res == 0) { |
| 1326 |
pr_trace_msg(trace_channel, 9, "retrieved JSON response data from cache"); |
| 1327 |
} |
| 1328 |
|
| 1329 |
return 0; |
| 1330 |
} |
| 1331 |
|
| 1332 |
static int ocsp_cache_redis_entry_delete(pool *p, const char *fingerprint) { |
| 1333 |
int res; |
| 1334 |
void *key = NULL; |
| 1335 |
size_t keysz = 0; |
| 1336 |
|
| 1337 |
res = ocsp_cache_get_key(p, fingerprint, &key, &keysz); |
| 1338 |
if (res < 0) { |
| 1339 |
pr_trace_msg(trace_channel, 1, |
| 1340 |
"unable to remove ocsp cache entry: error getting cache key: %s", |
| 1341 |
strerror(errno)); |
| 1342 |
|
| 1343 |
return -1; |
| 1344 |
} |
| 1345 |
|
| 1346 |
res = pr_redis_kremove(ocsp_redis, &tls_redis_module, (const char *) key, |
| 1347 |
keysz); |
| 1348 |
if (res < 0) { |
| 1349 |
int xerrno = errno; |
| 1350 |
|
| 1351 |
pr_trace_msg(trace_channel, 2, |
| 1352 |
"unable to remove Redis entry for fingerpring '%s': %s", fingerprint, |
| 1353 |
strerror(xerrno)); |
| 1354 |
|
| 1355 |
errno = xerrno; |
| 1356 |
return -1; |
| 1357 |
} |
| 1358 |
|
| 1359 |
return 0; |
| 1360 |
} |
| 1361 |
|
| 1362 |
static int ocsp_cache_entry_encode_json(pool *p, void **value, size_t *valuesz, |
| 1363 |
struct ocspcache_entry *oe) { |
| 1364 |
pr_json_object_t *json; |
| 1365 |
pool *tmp_pool; |
| 1366 |
char *base64_data = NULL, *json_text; |
| 1367 |
|
| 1368 |
json = pr_json_object_alloc(p); |
| 1369 |
(void) pr_json_object_set_number(p, json, OCSP_CACHE_JSON_KEY_AGE, |
| 1370 |
(double) oe->age); |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
tmp_pool = make_sub_pool(p); |
| 1376 |
base64_data = pcalloc(tmp_pool, (oe->resp_derlen * 2) + 1); |
| 1377 |
|
| 1378 |
EVP_EncodeBlock((unsigned char *) base64_data, oe->resp_der, |
| 1379 |
(int) oe->resp_derlen); |
| 1380 |
(void) pr_json_object_set_string(p, json, OCSP_CACHE_JSON_KEY_RESPONSE, |
| 1381 |
base64_data); |
| 1382 |
(void) pr_json_object_set_number(p, json, OCSP_CACHE_JSON_KEY_RESPONSE_LENGTH, |
| 1383 |
(double) oe->resp_derlen); |
| 1384 |
destroy_pool(tmp_pool); |
| 1385 |
|
| 1386 |
json_text = pr_json_object_to_text(p, json, ""); |
| 1387 |
(void) pr_json_object_free(json); |
| 1388 |
|
| 1389 |
|
| 1390 |
if (pr_json_text_validate(p, json_text) == FALSE) { |
| 1391 |
pr_trace_msg(trace_channel, 1, "invalid JSON emitted: '%s'", json_text); |
| 1392 |
errno = EINVAL; |
| 1393 |
return -1; |
| 1394 |
} |
| 1395 |
|
| 1396 |
|
| 1397 |
*valuesz = strlen(json_text) + 1; |
| 1398 |
*value = pstrndup(p, json_text, *valuesz - 1); |
| 1399 |
|
| 1400 |
return 0; |
| 1401 |
} |
| 1402 |
|
| 1403 |
static int ocsp_cache_redis_entry_set(pool *p, const char *fingerprint, |
| 1404 |
struct ocspcache_entry *oe) { |
| 1405 |
int res, xerrno = 0; |
| 1406 |
void *key = NULL, *value = NULL; |
| 1407 |
size_t keysz = 0, valuesz = 0; |
| 1408 |
|
| 1409 |
|
| 1410 |
res = ocsp_cache_entry_encode_json(p, &value, &valuesz, oe); |
| 1411 |
if (res < 0) { |
| 1412 |
xerrno = errno; |
| 1413 |
|
| 1414 |
pr_trace_msg(trace_channel, 4, "error JSON encoding OCSP response data: %s", |
| 1415 |
strerror(xerrno)); |
| 1416 |
|
| 1417 |
errno = xerrno; |
| 1418 |
return -1; |
| 1419 |
} |
| 1420 |
|
| 1421 |
res = ocsp_cache_get_key(p, fingerprint, &key, &keysz); |
| 1422 |
xerrno = errno; |
| 1423 |
if (res < 0) { |
| 1424 |
pr_trace_msg(trace_channel, 1, |
| 1425 |
"unable to set ocsp cache entry: error getting cache key: %s", |
| 1426 |
strerror(xerrno)); |
| 1427 |
|
| 1428 |
errno = xerrno; |
| 1429 |
return -1; |
| 1430 |
} |
| 1431 |
|
| 1432 |
res = pr_redis_kset(ocsp_redis, &tls_redis_module, (const char *) key, keysz, |
| 1433 |
value, valuesz, 0); |
| 1434 |
xerrno = errno; |
| 1435 |
|
| 1436 |
if (res < 0) { |
| 1437 |
pr_trace_msg(trace_channel, 2, |
| 1438 |
"unable to add Redis entry for fingerprint '%s': %s", fingerprint, |
| 1439 |
strerror(xerrno)); |
| 1440 |
|
| 1441 |
errno = xerrno; |
| 1442 |
return -1; |
| 1443 |
} |
| 1444 |
|
| 1445 |
pr_trace_msg(trace_channel, 9, "stored OCSP JSON response data in cache"); |
| 1446 |
return 0; |
| 1447 |
} |
| 1448 |
|
| 1449 |
static int ocsp_cache_open(tls_ocsp_cache_t *cache, char *info) { |
| 1450 |
config_rec *c; |
| 1451 |
|
| 1452 |
pr_trace_msg(trace_channel, 9, "opening Redis cache %p (info '%s')", |
| 1453 |
cache, info ? info : "(none)"); |
| 1454 |
|
| 1455 |
cache->cache_pool = make_sub_pool(permanent_pool); |
| 1456 |
pr_pool_tag(cache->cache_pool, MOD_TLS_REDIS_VERSION); |
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
c = find_config(main_server->conf, CONF_PARAM, "RedisEngine", FALSE); |
| 1464 |
if (c != NULL) { |
| 1465 |
int engine; |
| 1466 |
|
| 1467 |
engine = *((int *) c->argv[0]); |
| 1468 |
if (engine == FALSE) { |
| 1469 |
pr_trace_msg(trace_channel, 2, "%s", |
| 1470 |
"Redis support disabled (see RedisEngine directive)"); |
| 1471 |
errno = EPERM; |
| 1472 |
return -1; |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
ocsp_redis = pr_redis_conn_new(cache->cache_pool, &tls_redis_module, 0); |
| 1477 |
if (ocsp_redis == NULL) { |
| 1478 |
pr_trace_msg(trace_channel, 2, |
| 1479 |
"error connecting to Redis: %s", strerror(errno)); |
| 1480 |
errno = EPERM; |
| 1481 |
return -1; |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
if (pr_redis_conn_set_namespace(ocsp_redis, &tls_redis_module, |
| 1486 |
"mod_tls_redis.ocsp.", 19) < 0) { |
| 1487 |
pr_trace_msg(trace_channel, 2, |
| 1488 |
"error setting Redis namespace prefix: %s", strerror(errno)); |
| 1489 |
} |
| 1490 |
|
| 1491 |
return 0; |
| 1492 |
} |
| 1493 |
|
| 1494 |
static int ocsp_cache_close(tls_ocsp_cache_t *cache) { |
| 1495 |
pr_trace_msg(trace_channel, 9, "closing Redis ocsp cache %p", cache); |
| 1496 |
|
| 1497 |
if (cache != NULL && |
| 1498 |
cache->cache_pool != NULL) { |
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
if (ocspcache_resp_list != NULL) { |
| 1506 |
register unsigned int i; |
| 1507 |
struct ocspcache_large_entry *entries; |
| 1508 |
|
| 1509 |
entries = ocspcache_resp_list->elts; |
| 1510 |
for (i = 0; i < ocspcache_resp_list->nelts; i++) { |
| 1511 |
struct ocspcache_large_entry *entry; |
| 1512 |
|
| 1513 |
entry = &(entries[i]); |
| 1514 |
pr_memscrub(entry->resp_der, entry->resp_derlen); |
| 1515 |
entry->resp_derlen = 0; |
| 1516 |
pr_memscrub(entry->fingerprint, entry->fingerprint_len); |
| 1517 |
entry->fingerprint_len = 0; |
| 1518 |
entry->age = 0; |
| 1519 |
} |
| 1520 |
|
| 1521 |
clear_array(ocspcache_resp_list); |
| 1522 |
} |
| 1523 |
} |
| 1524 |
|
| 1525 |
return 0; |
| 1526 |
} |
| 1527 |
|
| 1528 |
static int ocsp_cache_add_large_resp(tls_ocsp_cache_t *cache, |
| 1529 |
const char *fingerprint, OCSP_RESPONSE *resp, time_t resp_age) { |
| 1530 |
struct ocspcache_large_entry *entry = NULL; |
| 1531 |
int resp_derlen; |
| 1532 |
unsigned char *ptr; |
| 1533 |
|
| 1534 |
resp_derlen = i2d_OCSP_RESPONSE(resp, NULL); |
| 1535 |
if (resp_derlen > TLS_MAX_OCSP_RESPONSE_SIZE) { |
| 1536 |
const char *exceeds_key = ocspcache_keys[OCSPCACHE_KEY_EXCEEDS].key, |
| 1537 |
*max_len_key = ocspcache_keys[OCSPCACHE_KEY_MAX_LEN].key; |
| 1538 |
void *value = NULL; |
| 1539 |
size_t valuesz = 0; |
| 1540 |
pool *tmp_pool; |
| 1541 |
|
| 1542 |
if (pr_redis_incr(ocsp_redis, &tls_redis_module, exceeds_key, 1, |
| 1543 |
NULL) < 0) { |
| 1544 |
pr_trace_msg(trace_channel, 2, |
| 1545 |
"error incrementing '%s' value: %s", exceeds_key, strerror(errno)); |
| 1546 |
} |
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
tmp_pool = make_sub_pool(cache->cache_pool); |
| 1553 |
value = pr_redis_get(tmp_pool, ocsp_redis, &tls_redis_module, max_len_key, |
| 1554 |
&valuesz); |
| 1555 |
if (value != NULL) { |
| 1556 |
uint64_t max_len; |
| 1557 |
|
| 1558 |
memcpy(&max_len, value, valuesz); |
| 1559 |
if ((uint64_t) resp_derlen > max_len) { |
| 1560 |
if (pr_redis_set(ocsp_redis, &tls_redis_module, max_len_key, &max_len, |
| 1561 |
sizeof(max_len), 0) < 0) { |
| 1562 |
pr_trace_msg(trace_channel, 2, |
| 1563 |
"error setting '%s' value: %s", max_len_key, strerror(errno)); |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
} else { |
| 1568 |
pr_trace_msg(trace_channel, 2, |
| 1569 |
"error getting '%s' value: %s", max_len_key, strerror(errno)); |
| 1570 |
} |
| 1571 |
|
| 1572 |
destroy_pool(tmp_pool); |
| 1573 |
} |
| 1574 |
|
| 1575 |
if (ocspcache_resp_list != NULL) { |
| 1576 |
register unsigned int i; |
| 1577 |
struct ocspcache_large_entry *entries; |
| 1578 |
time_t now; |
| 1579 |
int ok = FALSE; |
| 1580 |
|
| 1581 |
|
| 1582 |
entries = ocspcache_resp_list->elts; |
| 1583 |
time(&now); |
| 1584 |
for (i = 0; i < ocspcache_resp_list->nelts; i++) { |
| 1585 |
entry = &(entries[i]); |
| 1586 |
|
| 1587 |
if (entry->age > (now - 3600)) { |
| 1588 |
|
| 1589 |
entry->age = 0; |
| 1590 |
pr_memscrub(entry->resp_der, entry->resp_derlen); |
| 1591 |
entry->resp_derlen = 0; |
| 1592 |
pr_memscrub(entry->fingerprint, entry->fingerprint_len); |
| 1593 |
entry->fingerprint_len = 0; |
| 1594 |
|
| 1595 |
ok = TRUE; |
| 1596 |
break; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (!ok) { |
| 1601 |
|
| 1602 |
entry = push_array(ocspcache_resp_list); |
| 1603 |
} |
| 1604 |
|
| 1605 |
} else { |
| 1606 |
ocspcache_resp_list = make_array(cache->cache_pool, 1, |
| 1607 |
sizeof(struct ocspcache_large_entry)); |
| 1608 |
entry = push_array(ocspcache_resp_list); |
| 1609 |
} |
| 1610 |
|
| 1611 |
entry->age = resp_age; |
| 1612 |
entry->fingerprint_len = strlen(fingerprint); |
| 1613 |
entry->fingerprint = pstrdup(cache->cache_pool, fingerprint); |
| 1614 |
entry->resp_derlen = resp_derlen; |
| 1615 |
entry->resp_der = ptr = palloc(cache->cache_pool, resp_derlen); |
| 1616 |
i2d_OCSP_RESPONSE(resp, &ptr); |
| 1617 |
|
| 1618 |
return 0; |
| 1619 |
} |
| 1620 |
|
| 1621 |
static int ocsp_cache_add(tls_ocsp_cache_t *cache, const char *fingerprint, |
| 1622 |
OCSP_RESPONSE *resp, time_t resp_age) { |
| 1623 |
struct ocspcache_entry entry; |
| 1624 |
int resp_derlen; |
| 1625 |
unsigned char *ptr; |
| 1626 |
|
| 1627 |
pr_trace_msg(trace_channel, 9, "adding response to Redis ocsp cache %p", |
| 1628 |
cache); |
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
resp_derlen = i2d_OCSP_RESPONSE(resp, NULL); |
| 1635 |
if (resp_derlen > TLS_MAX_OCSP_RESPONSE_SIZE) { |
| 1636 |
pr_trace_msg(trace_channel, 2, |
| 1637 |
"length of serialized OCSP response data (%d) exceeds maximum size (%u), " |
| 1638 |
"unable to add to shared Redis, adding to list", resp_derlen, |
| 1639 |
TLS_MAX_OCSP_RESPONSE_SIZE); |
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
return ocsp_cache_add_large_resp(cache, fingerprint, resp, resp_age); |
| 1647 |
} |
| 1648 |
|
| 1649 |
entry.age = resp_age; |
| 1650 |
entry.resp_derlen = resp_derlen; |
| 1651 |
ptr = entry.resp_der; |
| 1652 |
i2d_OCSP_RESPONSE(resp, &ptr); |
| 1653 |
|
| 1654 |
if (ocsp_cache_redis_entry_set(cache->cache_pool, fingerprint, &entry) < 0) { |
| 1655 |
pr_trace_msg(trace_channel, 2, |
| 1656 |
"error adding response to Redis: %s", strerror(errno)); |
| 1657 |
|
| 1658 |
|
| 1659 |
return ocsp_cache_add_large_resp(cache, fingerprint, resp, resp_age); |
| 1660 |
|
| 1661 |
} else { |
| 1662 |
const char *key = ocspcache_keys[OCSPCACHE_KEY_STORES].key; |
| 1663 |
|
| 1664 |
if (pr_redis_incr(ocsp_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 1665 |
pr_trace_msg(trace_channel, 2, |
| 1666 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 1667 |
} |
| 1668 |
} |
| 1669 |
|
| 1670 |
return 0; |
| 1671 |
} |
| 1672 |
|
| 1673 |
static OCSP_RESPONSE *ocsp_cache_get(tls_ocsp_cache_t *cache, |
| 1674 |
const char *fingerprint, time_t *resp_age) { |
| 1675 |
struct ocspcache_entry entry; |
| 1676 |
OCSP_RESPONSE *resp = NULL; |
| 1677 |
size_t fingerprint_len; |
| 1678 |
const unsigned char *ptr; |
| 1679 |
|
| 1680 |
pr_trace_msg(trace_channel, 9, "getting response from Redis ocsp cache %p", |
| 1681 |
cache); |
| 1682 |
|
| 1683 |
fingerprint_len = strlen(fingerprint); |
| 1684 |
|
| 1685 |
|
| 1686 |
if (ocspcache_resp_list != NULL) { |
| 1687 |
register unsigned int i; |
| 1688 |
struct ocspcache_large_entry *entries; |
| 1689 |
|
| 1690 |
entries = ocspcache_resp_list->elts; |
| 1691 |
for (i = 0; i < ocspcache_resp_list->nelts; i++) { |
| 1692 |
struct ocspcache_large_entry *large_entry; |
| 1693 |
|
| 1694 |
large_entry = &(entries[i]); |
| 1695 |
if (large_entry->fingerprint_len > 0 && |
| 1696 |
large_entry->fingerprint_len == fingerprint_len && |
| 1697 |
memcmp(large_entry->fingerprint, fingerprint, fingerprint_len) == 0) { |
| 1698 |
ptr = large_entry->resp_der; |
| 1699 |
resp = d2i_OCSP_RESPONSE(NULL, &ptr, large_entry->resp_derlen); |
| 1700 |
if (resp == NULL) { |
| 1701 |
pr_trace_msg(trace_channel, 2, |
| 1702 |
"error retrieving response from ocsp cache: %s", |
| 1703 |
redis_get_errors()); |
| 1704 |
|
| 1705 |
} else { |
| 1706 |
*resp_age = large_entry->age; |
| 1707 |
break; |
| 1708 |
} |
| 1709 |
} |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
if (resp) { |
| 1714 |
return resp; |
| 1715 |
} |
| 1716 |
|
| 1717 |
if (ocsp_cache_redis_entry_get(cache->cache_pool, fingerprint, &entry) < 0) { |
| 1718 |
return NULL; |
| 1719 |
} |
| 1720 |
|
| 1721 |
ptr = entry.resp_der; |
| 1722 |
resp = d2i_OCSP_RESPONSE(NULL, &ptr, entry.resp_derlen); |
| 1723 |
if (resp != NULL) { |
| 1724 |
const char *key = ocspcache_keys[OCSPCACHE_KEY_HITS].key; |
| 1725 |
|
| 1726 |
*resp_age = entry.age; |
| 1727 |
|
| 1728 |
if (pr_redis_incr(ocsp_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 1729 |
pr_trace_msg(trace_channel, 2, |
| 1730 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 1731 |
} |
| 1732 |
|
| 1733 |
} else { |
| 1734 |
const char *key = ocspcache_keys[OCSPCACHE_KEY_ERRORS].key; |
| 1735 |
|
| 1736 |
pr_trace_msg(trace_channel, 2, |
| 1737 |
"error retrieving response from ocsp cache: %s", redis_get_errors()); |
| 1738 |
|
| 1739 |
if (pr_redis_incr(ocsp_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 1740 |
pr_trace_msg(trace_channel, 2, |
| 1741 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 1742 |
} |
| 1743 |
} |
| 1744 |
|
| 1745 |
if (resp == NULL) { |
| 1746 |
const char *key = ocspcache_keys[OCSPCACHE_KEY_MISSES].key; |
| 1747 |
|
| 1748 |
if (pr_redis_incr(ocsp_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 1749 |
pr_trace_msg(trace_channel, 2, |
| 1750 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 1751 |
} |
| 1752 |
|
| 1753 |
errno = ENOENT; |
| 1754 |
} |
| 1755 |
|
| 1756 |
return resp; |
| 1757 |
} |
| 1758 |
|
| 1759 |
static int ocsp_cache_delete(tls_ocsp_cache_t *cache, |
| 1760 |
const char *fingerprint) { |
| 1761 |
const char *key = ocspcache_keys[OCSPCACHE_KEY_DELETES].key; |
| 1762 |
int res; |
| 1763 |
size_t fingerprint_len; |
| 1764 |
|
| 1765 |
pr_trace_msg(trace_channel, 9, "deleting response from Redis ocsp cache %p", |
| 1766 |
cache); |
| 1767 |
|
| 1768 |
fingerprint_len = strlen(fingerprint); |
| 1769 |
|
| 1770 |
|
| 1771 |
if (ocspcache_resp_list != NULL) { |
| 1772 |
register unsigned int i; |
| 1773 |
struct ocspcache_large_entry *entries; |
| 1774 |
|
| 1775 |
entries = ocspcache_resp_list->elts; |
| 1776 |
for (i = 0; i < ocspcache_resp_list->nelts; i++) { |
| 1777 |
struct ocspcache_large_entry *entry; |
| 1778 |
|
| 1779 |
entry = &(entries[i]); |
| 1780 |
if (entry->fingerprint_len == fingerprint_len && |
| 1781 |
memcmp(entry->fingerprint, fingerprint, fingerprint_len) == 0) { |
| 1782 |
|
| 1783 |
pr_memscrub(entry->resp_der, entry->resp_derlen); |
| 1784 |
entry->resp_derlen = 0; |
| 1785 |
pr_memscrub(entry->fingerprint, entry->fingerprint_len); |
| 1786 |
entry->fingerprint_len = 0; |
| 1787 |
entry->age = 0; |
| 1788 |
|
| 1789 |
return 0; |
| 1790 |
} |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
res = ocsp_cache_redis_entry_delete(cache->cache_pool, fingerprint); |
| 1795 |
if (res < 0) { |
| 1796 |
return -1; |
| 1797 |
} |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
if (pr_redis_incr(ocsp_redis, &tls_redis_module, key, 1, NULL) < 0) { |
| 1802 |
pr_trace_msg(trace_channel, 2, |
| 1803 |
"error incrementing '%s' value: %s", key, strerror(errno)); |
| 1804 |
} |
| 1805 |
|
| 1806 |
return res; |
| 1807 |
} |
| 1808 |
|
| 1809 |
static int ocsp_cache_clear(tls_ocsp_cache_t *cache) { |
| 1810 |
register unsigned int i; |
| 1811 |
int res = 0; |
| 1812 |
|
| 1813 |
if (ocsp_redis == NULL) { |
| 1814 |
pr_trace_msg(trace_channel, 9, "missing required Redis connection"); |
| 1815 |
errno = EINVAL; |
| 1816 |
return -1; |
| 1817 |
} |
| 1818 |
|
| 1819 |
pr_trace_msg(trace_channel, 9, "clearing Redis ocsp cache %p", cache); |
| 1820 |
|
| 1821 |
if (ocspcache_resp_list != NULL) { |
| 1822 |
struct ocspcache_large_entry *entries; |
| 1823 |
|
| 1824 |
entries = ocspcache_resp_list->elts; |
| 1825 |
for (i = 0; i < ocspcache_resp_list->nelts; i++) { |
| 1826 |
struct ocspcache_large_entry *entry; |
| 1827 |
|
| 1828 |
entry = &(entries[i]); |
| 1829 |
entry->age = 0; |
| 1830 |
pr_memscrub(entry->resp_der, entry->resp_derlen); |
| 1831 |
entry->resp_derlen = 0; |
| 1832 |
pr_memscrub(entry->fingerprint, entry->fingerprint_len); |
| 1833 |
entry->fingerprint_len = 0; |
| 1834 |
} |
| 1835 |
} |
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
return res; |
| 1840 |
} |
| 1841 |
|
| 1842 |
static int ocsp_cache_remove(tls_ocsp_cache_t *cache) { |
| 1843 |
int res; |
| 1844 |
|
| 1845 |
pr_trace_msg(trace_channel, 9, "removing Redis ocsp cache %p", cache); |
| 1846 |
|
| 1847 |
res = ocsp_cache_clear(cache); |
| 1848 |
|
| 1849 |
|
| 1850 |
return res; |
| 1851 |
} |
| 1852 |
|
| 1853 |
static int ocsp_cache_status(tls_ocsp_cache_t *cache, |
| 1854 |
void (*statusf)(void *, const char *, ...), void *arg, int flags) { |
| 1855 |
register unsigned int i; |
| 1856 |
pool *tmp_pool; |
| 1857 |
|
| 1858 |
pr_trace_msg(trace_channel, 9, "checking Redis ocsp cache %p", cache); |
| 1859 |
|
| 1860 |
tmp_pool = make_sub_pool(permanent_pool); |
| 1861 |
|
| 1862 |
statusf(arg, "%s", "Redis OCSP response cache provided by " |
| 1863 |
MOD_TLS_REDIS_VERSION); |
| 1864 |
statusf(arg, "%s", ""); |
| 1865 |
statusf(arg, "Redis server: "); |
| 1866 |
|
| 1867 |
for (i = 0; ocspcache_keys[i].key != NULL; i++) { |
| 1868 |
const char *key, *desc; |
| 1869 |
void *value = NULL; |
| 1870 |
size_t valuesz = 0; |
| 1871 |
|
| 1872 |
key = ocspcache_keys[i].key; |
| 1873 |
desc = ocspcache_keys[i].desc; |
| 1874 |
|
| 1875 |
value = pr_redis_get(tmp_pool, ocsp_redis, &tls_redis_module, key, |
| 1876 |
&valuesz); |
| 1877 |
if (value != NULL) { |
| 1878 |
uint64_t num = 0; |
| 1879 |
memcpy(&num, value, valuesz); |
| 1880 |
statusf(arg, "%s: %lu", desc, (unsigned long) num); |
| 1881 |
} |
| 1882 |
} |
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
destroy_pool(tmp_pool); |
| 1887 |
return 0; |
| 1888 |
} |
| 1889 |
#endif |
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
#if defined(PR_SHARED_MODULE) |
| 1895 |
static void tls_redis_mod_unload_ev(const void *event_data, void *user_data) { |
| 1896 |
if (strcmp("mod_tls_redis.c", (const char *) event_data) == 0) { |
| 1897 |
pr_event_unregister(&tls_redis_module, NULL, NULL); |
| 1898 |
tls_sess_cache_unregister("redis"); |
| 1899 |
# if defined(PR_USE_OPENSSL_OCSP) |
| 1900 |
tls_ocsp_cache_unregister("redis"); |
| 1901 |
# endif |
| 1902 |
|
| 1903 |
if (sess_redis != NULL) { |
| 1904 |
(void) pr_redis_conn_destroy(sess_redis); |
| 1905 |
sess_redis = NULL; |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (ocsp_redis != NULL) { |
| 1909 |
(void) pr_redis_conn_destroy(ocsp_redis); |
| 1910 |
ocsp_redis = NULL; |
| 1911 |
} |
| 1912 |
} |
| 1913 |
} |
| 1914 |
#endif |
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
static int tls_redis_init(void) { |
| 1920 |
#if defined(PR_SHARED_MODULE) |
| 1921 |
pr_event_register(&tls_redis_module, "core.module-unload", |
| 1922 |
tls_redis_mod_unload_ev, NULL); |
| 1923 |
#endif |
| 1924 |
|
| 1925 |
|
| 1926 |
memset(&sess_cache, 0, sizeof(sess_cache)); |
| 1927 |
|
| 1928 |
sess_cache.cache_name = "redis"; |
| 1929 |
pr_pool_tag(sess_cache.cache_pool, MOD_TLS_REDIS_VERSION); |
| 1930 |
|
| 1931 |
sess_cache.open = sess_cache_open; |
| 1932 |
sess_cache.close = sess_cache_close; |
| 1933 |
sess_cache.add = sess_cache_add; |
| 1934 |
sess_cache.get = sess_cache_get; |
| 1935 |
sess_cache.delete = sess_cache_delete; |
| 1936 |
sess_cache.clear = sess_cache_clear; |
| 1937 |
sess_cache.remove = sess_cache_remove; |
| 1938 |
sess_cache.status = sess_cache_status; |
| 1939 |
|
| 1940 |
#ifdef SSL_SESS_CACHE_NO_INTERNAL |
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
sess_cache.cache_mode = SSL_SESS_CACHE_NO_INTERNAL; |
| 1946 |
#endif |
| 1947 |
|
| 1948 |
#if defined(PR_USE_OPENSSL_OCSP) |
| 1949 |
|
| 1950 |
memset(&ocsp_cache, 0, sizeof(ocsp_cache)); |
| 1951 |
|
| 1952 |
ocsp_cache.cache_name = "redis"; |
| 1953 |
pr_pool_tag(ocsp_cache.cache_pool, MOD_TLS_REDIS_VERSION); |
| 1954 |
|
| 1955 |
ocsp_cache.open = ocsp_cache_open; |
| 1956 |
ocsp_cache.close = ocsp_cache_close; |
| 1957 |
ocsp_cache.add = ocsp_cache_add; |
| 1958 |
ocsp_cache.get = ocsp_cache_get; |
| 1959 |
ocsp_cache.delete = ocsp_cache_delete; |
| 1960 |
ocsp_cache.clear = ocsp_cache_clear; |
| 1961 |
ocsp_cache.remove = ocsp_cache_remove; |
| 1962 |
ocsp_cache.status = ocsp_cache_status; |
| 1963 |
#endif |
| 1964 |
|
| 1965 |
#if defined(PR_USE_REDIS) |
| 1966 |
if (tls_sess_cache_register("redis", &sess_cache) < 0) { |
| 1967 |
pr_log_debug(DEBUG1, MOD_TLS_REDIS_VERSION |
| 1968 |
": notice: error registering 'redis' SSL session cache: %s", |
| 1969 |
strerror(errno)); |
| 1970 |
return -1; |
| 1971 |
} |
| 1972 |
|
| 1973 |
# if defined(PR_USE_OPENSSL_OCSP) |
| 1974 |
if (tls_ocsp_cache_register("redis", &ocsp_cache) < 0) { |
| 1975 |
pr_log_debug(DEBUG1, MOD_TLS_REDIS_VERSION |
| 1976 |
": notice: error registering 'redis' OCSP response cache: %s", |
| 1977 |
strerror(errno)); |
| 1978 |
return -1; |
| 1979 |
} |
| 1980 |
# endif |
| 1981 |
|
| 1982 |
#else |
| 1983 |
pr_log_debug(DEBUG1, MOD_TLS_REDIS_VERSION |
| 1984 |
": unable to register 'redis' SSL session cache: Redis support not enabled"); |
| 1985 |
# if defined(PR_USE_OPENSSL_OCSP) |
| 1986 |
pr_log_debug(DEBUG1, MOD_TLS_REDIS_VERSION |
| 1987 |
": unable to register 'redis' OCSP response cache: Redis support not enabled"); |
| 1988 |
# endif |
| 1989 |
#endif |
| 1990 |
|
| 1991 |
return 0; |
| 1992 |
} |
| 1993 |
|
| 1994 |
static int tls_redis_sess_init(void) { |
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
#if 0 |
| 2000 |
if (sess_redis != NULL) { |
| 2001 |
if (pr_redis_conn_clone(session.pool, sess_redis) < 0) { |
| 2002 |
tls_log(MOD_TLS_REDIS_VERSION |
| 2003 |
": error resetting Redis handle: %s", strerror(errno)); |
| 2004 |
} |
| 2005 |
} |
| 2006 |
|
| 2007 |
#if defined(PR_USE_OPENSSL_OCSP) |
| 2008 |
if (ocsp_redis != NULL) { |
| 2009 |
if (pr_redis_conn_clone(session.pool, ocsp_redis) < 0) { |
| 2010 |
tls_log(MOD_TLS_REDIS_VERSION |
| 2011 |
": error resetting Redis handle: %s", strerror(errno)); |
| 2012 |
} |
| 2013 |
} |
| 2014 |
#endif |
| 2015 |
#endif |
| 2016 |
|
| 2017 |
return 0; |
| 2018 |
} |
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
module tls_redis_module = { |
| 2024 |
NULL, NULL, |
| 2025 |
|
| 2026 |
|
| 2027 |
0x20, |
| 2028 |
|
| 2029 |
|
| 2030 |
"tls_redis", |
| 2031 |
|
| 2032 |
|
| 2033 |
NULL, |
| 2034 |
|
| 2035 |
|
| 2036 |
NULL, |
| 2037 |
|
| 2038 |
|
| 2039 |
NULL, |
| 2040 |
|
| 2041 |
|
| 2042 |
tls_redis_init, |
| 2043 |
|
| 2044 |
|
| 2045 |
tls_redis_sess_init, |
| 2046 |
|
| 2047 |
|
| 2048 |
MOD_TLS_REDIS_VERSION |
| 2049 |
}; |