关键词搜索

源码搜索 ×
×

漫话Redis源码之五

发布2021-11-21浏览682次

详情内容

说真的,看到openssl和tls, 我心好沉重:

  1. void tlsCleanup(void) {
  2. if (redis_tls_ctx) {
  3. SSL_CTX_free(redis_tls_ctx);
  4. redis_tls_ctx = NULL;
  5. }
  6. if (redis_tls_client_ctx) {
  7. SSL_CTX_free(redis_tls_client_ctx);
  8. redis_tls_client_ctx = NULL;
  9. }
  10. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  11. // unavailable on LibreSSL
  12. OPENSSL_cleanup();
  13. #endif
  14. }
  15. /* Callback for passing a keyfile password stored as an sds to OpenSSL */
  16. static int tlsPasswordCallback(char *buf, int size, int rwflag, void *u) {
  17. UNUSED(rwflag);
  18. const char *pass = u;
  19. size_t pass_len;
  20. if (!pass) return -1;
  21. pass_len = strlen(pass);
  22. if (pass_len > (size_t) size) return -1;
  23. memcpy(buf, pass, pass_len);
  24. return (int) pass_len;
  25. }
  26. /* Create a *base* SSL_CTX using the SSL configuration provided. The base context
  27. * includes everything that's common for both client-side and server-side connections.
  28. */
  29. static SSL_CTX *createSSLContext(redisTLSContextConfig *ctx_config, int protocols, int client) {
  30. const char *cert_file = client ? ctx_config->client_cert_file : ctx_config->cert_file;
  31. const char *key_file = client ? ctx_config->client_key_file : ctx_config->key_file;
  32. const char *key_file_pass = client ? ctx_config->client_key_file_pass : ctx_config->key_file_pass;
  33. char errbuf[256];
  34. SSL_CTX *ctx = NULL;
  35. ctx = SSL_CTX_new(SSLv23_method());
  36. SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
  37. #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
  38. SSL_CTX_set_options(ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
  39. #endif
  40. if (!(protocols & REDIS_TLS_PROTO_TLSv1))
  41. SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
  42. if (!(protocols & REDIS_TLS_PROTO_TLSv1_1))
  43. SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1);
  44. #ifdef SSL_OP_NO_TLSv1_2
  45. if (!(protocols & REDIS_TLS_PROTO_TLSv1_2))
  46. SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2);
  47. #endif
  48. #ifdef SSL_OP_NO_TLSv1_3
  49. if (!(protocols & REDIS_TLS_PROTO_TLSv1_3))
  50. SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3);
  51. #endif
  52. #ifdef SSL_OP_NO_COMPRESSION
  53. SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION);
  54. #endif
  55. SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
  56. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);
  57. SSL_CTX_set_default_passwd_cb(ctx, tlsPasswordCallback);
  58. SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *) key_file_pass);
  59. if (SSL_CTX_use_certificate_chain_file(ctx, cert_file) <= 0) {
  60. ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
  61. serverLog(LL_WARNING, "Failed to load certificate: %s: %s", cert_file, errbuf);
  62. goto error;
  63. }
  64. if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {
  65. ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
  66. serverLog(LL_WARNING, "Failed to load private key: %s: %s", key_file, errbuf);
  67. goto error;
  68. }
  69. if ((ctx_config->ca_cert_file || ctx_config->ca_cert_dir) &&
  70. SSL_CTX_load_verify_locations(ctx, ctx_config->ca_cert_file, ctx_config->ca_cert_dir) <= 0) {
  71. ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
  72. serverLog(LL_WARNING, "Failed to configure CA certificate(s) file/directory: %s", errbuf);
  73. goto error;
  74. }
  75. if (ctx_config->ciphers && !SSL_CTX_set_cipher_list(ctx, ctx_config->ciphers)) {
  76. serverLog(LL_WARNING, "Failed to configure ciphers: %s", ctx_config->ciphers);
  77. goto error;
  78. }
  79. #ifdef TLS1_3_VERSION
  80. if (ctx_config->ciphersuites && !SSL_CTX_set_ciphersuites(ctx, ctx_config->ciphersuites)) {
  81. serverLog(LL_WARNING, "Failed to configure ciphersuites: %s", ctx_config->ciphersuites);
  82. goto error;
  83. }
  84. #endif
  85. return ctx;
  86. error:
  87. if (ctx) SSL_CTX_free(ctx);
  88. return NULL;
  89. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载