0008-S2S-TLS-OpenSSL-Postpone-verification-of-TLS-session.patch 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. From 1a63a5e98b1dfe3a6e31dd6206585a1476f7d242 Mon Sep 17 00:00:00 2001
  2. From: Alexander Barton <alex@barton.de>
  3. Date: Tue, 2 Jan 2024 22:02:46 +0100
  4. Subject: [PATCH 08/20] S2S-TLS/OpenSSL: Postpone verification of TLS session
  5. right before server handshake
  6. The verify callback in OpenSSL is called pretty early, and at that time
  7. it is not possible yet to check which connection it belongs to, and some
  8. connections may have relaxed requirements.
  9. So always return success in the Verify_openssl() callback, and postpone
  10. validation of the TLS session until starting the server handshake in
  11. cb_connserver_login_ssl(), when we know which server this connection
  12. belongs to and which options (like "SSLVerify") are in effect.
  13. The code doing this was already present in cb_connserver_login_ssl(),
  14. but this patch adds a more prominent comment to the function.
  15. (cherry picked from commit 3db3b47fc7172a69b7d99d66eddb07a323dc6e74)
  16. ---
  17. src/ngircd/conn-ssl.c | 21 +++++++++++++++------
  18. src/ngircd/conn.c | 7 +++++++
  19. 2 files changed, 22 insertions(+), 6 deletions(-)
  20. --- a/src/ngircd/conn-ssl.c
  21. +++ b/src/ngircd/conn-ssl.c
  22. @@ -211,14 +211,23 @@
  23. static int
  24. Verify_openssl(int preverify_ok, X509_STORE_CTX * ctx)
  25. {
  26. - int err;
  27. -
  28. +#ifdef DEBUG
  29. if (!preverify_ok) {
  30. - err = X509_STORE_CTX_get_error(ctx);
  31. - Log(LOG_ERR, "Certificate validation failed: %s",
  32. - X509_verify_cert_error_string(err));
  33. + int err = X509_STORE_CTX_get_error(ctx);
  34. + LogDebug("Certificate validation failed: %s",
  35. + X509_verify_cert_error_string(err));
  36. }
  37. - return preverify_ok;
  38. +#else
  39. + (void)preverify_ok;
  40. + (void)ctx;
  41. +#endif
  42. +
  43. + /* Always(!) return success as we have to deal with invalid
  44. + * (self-signed, expired, ...) client certificates and with invalid
  45. + * server certificates when "SSLVerify" is disabled, which we don't
  46. + * know at this stage. Therefore we postpone this check, it will be
  47. + * (and has to be!) handled in cb_connserver_login_ssl(). */
  48. + return 1;
  49. }
  50. #endif
  51. --- a/src/ngircd/conn.c
  52. +++ b/src/ngircd/conn.c
  53. @@ -2539,6 +2539,13 @@
  54. /**
  55. * IO callback for new outgoing SSL-enabled server connections.
  56. *
  57. + * IMPORTANT: The SSL session has been validated before, but all errors have
  58. + * been ignored so far! The reason for this is that the generic SSL code has no
  59. + * idea if the new session actually belongs to a server, as this only becomes
  60. + * clear when the remote peer sends its PASS command (and we have to handle
  61. + * invalid client certificates!). Therefore, it is important to check the
  62. + * status of the SSL session first before continuing the server handshake here!
  63. + *
  64. * @param sock Socket descriptor.
  65. * @param unused (ignored IO specification)
  66. */