说真的,看到openssl和tls, 我心好沉重:
- void tlsCleanup(void) {
- if (redis_tls_ctx) {
- SSL_CTX_free(redis_tls_ctx);
- redis_tls_ctx = NULL;
- }
- if (redis_tls_client_ctx) {
- SSL_CTX_free(redis_tls_client_ctx);
- redis_tls_client_ctx = NULL;
- }
-
- #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
- // unavailable on LibreSSL
- OPENSSL_cleanup();
- #endif
- }
-
- /* Callback for passing a keyfile password stored as an sds to OpenSSL */
- static int tlsPasswordCallback(char *buf, int size, int rwflag, void *u) {
- UNUSED(rwflag);
-
- const char *pass = u;
- size_t pass_len;
-
- if (!pass) return -1;
- pass_len = strlen(pass);
- if (pass_len > (size_t) size) return -1;
- memcpy(buf, pass, pass_len);
-
- return (int) pass_len;
- }
-
- /* Create a *base* SSL_CTX using the SSL configuration provided. The base context
- * includes everything that's common for both client-side and server-side connections.
- */
- static SSL_CTX *createSSLContext(redisTLSContextConfig *ctx_config, int protocols, int client) {
- const char *cert_file = client ? ctx_config->client_cert_file : ctx_config->cert_file;
- const char *key_file = client ? ctx_config->client_key_file : ctx_config->key_file;
- const char *key_file_pass = client ? ctx_config->client_key_file_pass : ctx_config->key_file_pass;
- char errbuf[256];
- SSL_CTX *ctx = NULL;
-
- ctx = SSL_CTX_new(SSLv23_method());
-
- SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
-
- #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
- SSL_CTX_set_options(ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
- #endif
-
- if (!(protocols & REDIS_TLS_PROTO_TLSv1))
- SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
- if (!(protocols & REDIS_TLS_PROTO_TLSv1_1))
- SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1);
- #ifdef SSL_OP_NO_TLSv1_2
- if (!(protocols & REDIS_TLS_PROTO_TLSv1_2))
- SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2);
- #endif
- #ifdef SSL_OP_NO_TLSv1_3
- if (!(protocols & REDIS_TLS_PROTO_TLSv1_3))
- SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3);
- #endif
-
- #ifdef SSL_OP_NO_COMPRESSION
- SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION);
- #endif
-
- SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
- SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
-
- SSL_CTX_set_default_passwd_cb(ctx, tlsPasswordCallback);
- SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *) key_file_pass);
-
- if (SSL_CTX_use_certificate_chain_file(ctx, cert_file) <= 0) {
- ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
- serverLog(LL_WARNING, "Failed to load certificate: %s: %s", cert_file, errbuf);
- goto error;
- }
-
- if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
- ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
- serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf);
- goto error;
- }
-
- if ((ctx_config->ca_cert_file || ctx_config->ca_cert_dir) &&
- SSL_CTX_load_verify_locations(ctx, ctx_config->ca_cert_file, ctx_config->ca_cert_dir) <= 0) {
- ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
- serverLog(LL_WARNING, "Failed to configure CA certificate(s) file/directory: %s", errbuf);
- goto error;
- }
-
- if (ctx_config->ciphers && !SSL_CTX_set_cipher_list(ctx, ctx_config->ciphers)) {
- serverLog(LL_WARNING, "Failed to configure ciphers: %s", ctx_config->ciphers);
- goto error;
- }
-
- #ifdef TLS1_3_VERSION
- if (ctx_config->ciphersuites && !SSL_CTX_set_ciphersuites(ctx, ctx_config->ciphersuites)) {
- serverLog(LL_WARNING, "Failed to configure ciphersuites: %s", ctx_config->ciphersuites);
- goto error;
- }
- #endif
-
- return ctx;
-
- error:
- if (ctx) SSL_CTX_free(ctx);
- return NULL;
- }