0013-S2S-TLS-GnuTLS-Fix-handling-of-certificate-informati.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. From 4384e600b9d709551deb8e2c93754f5af1a24161 Mon Sep 17 00:00:00 2001
  2. From: Alexander Barton <alex@barton.de>
  3. Date: Fri, 5 Jan 2024 22:29:40 +0100
  4. Subject: [PATCH 13/20] S2S-TLS/GnuTLS: Fix handling of certificate information
  5. for incoming connections
  6. Show proper certificate information for incoming connections, too, and
  7. not "peer did not present a certificate", regardless if the client sent
  8. a certificate or not.
  9. This change is for GnuTLS and similar to what was implemented in commit
  10. for OpenSSL in "S2S-TLS/OpenSSL: Fix handling of certificate information
  11. for incoming connections".
  12. (cherry picked from commit 509ff6032686662328f4ecb0c5c287a34e929c53)
  13. ---
  14. src/ngircd/conn-ssl.c | 75 ++++++++++++++++++++++++-------------------
  15. 1 file changed, 42 insertions(+), 33 deletions(-)
  16. --- a/src/ngircd/conn-ssl.c
  17. +++ b/src/ngircd/conn-ssl.c
  18. @@ -667,6 +667,7 @@
  19. #ifdef HAVE_LIBGNUTLS
  20. int err;
  21. + (void)s;
  22. err = gnutls_init(&c->ssl_state.gnutls_session, GNUTLS_CLIENT);
  23. if (err) {
  24. Log(LOG_ERR, "Failed to initialize new SSL session: %s",
  25. @@ -916,29 +917,8 @@
  26. gnutls_cipher_get_name(cipher),
  27. gnutls_mac_get_name(gnutls_mac_get(sess)));
  28. cred = gnutls_auth_get_type(c->ssl_state.gnutls_session);
  29. - if (cred == GNUTLS_CRD_CERTIFICATE && connect) {
  30. + if (cred == GNUTLS_CRD_CERTIFICATE) {
  31. cert_seen = true;
  32. - int verify =
  33. - gnutls_certificate_verify_peers2(c->
  34. - ssl_state.gnutls_session,
  35. - &status);
  36. - if (verify < 0) {
  37. - Log(LOG_ERR,
  38. - "gnutls_certificate_verify_peers2 failed: %s",
  39. - gnutls_strerror(verify));
  40. - goto done_cn_validation;
  41. - } else if (status) {
  42. - gnutls_datum_t out;
  43. -
  44. - if (gnutls_certificate_verification_status_print
  45. - (status, gnutls_certificate_type_get(sess), &out,
  46. - 0) == GNUTLS_E_SUCCESS) {
  47. - Log(LOG_ERR,
  48. - "Certificate validation failed: %s",
  49. - out.data);
  50. - gnutls_free(out.data);
  51. - }
  52. - }
  53. gnutls_x509_crt_t cert;
  54. unsigned cert_list_size;
  55. @@ -962,17 +942,46 @@
  56. gnutls_strerror(err));
  57. goto done_cn_validation;
  58. }
  59. - err = gnutls_x509_crt_check_hostname(cert, c->host);
  60. - if (err == 0)
  61. - Log(LOG_ERR,
  62. - "Failed to verify the hostname, expected \"%s\"",
  63. - c->host);
  64. - else
  65. - cert_ok = verify == 0 && status == 0;
  66. -
  67. - snprintf(msg, sizeof(msg), "%svalid peer certificate",
  68. - cert_ok ? "" : "in");
  69. - LogGnuTLS_CertInfo(cert_ok ? LOG_DEBUG : LOG_ERR, cert, msg);
  70. +
  71. + if (connect) {
  72. + int verify =
  73. + gnutls_certificate_verify_peers2(c->
  74. + ssl_state.gnutls_session,
  75. + &status);
  76. + if (verify < 0) {
  77. + Log(LOG_ERR,
  78. + "gnutls_certificate_verify_peers2 failed: %s",
  79. + gnutls_strerror(verify));
  80. + goto done_cn_validation;
  81. + } else if (status) {
  82. + gnutls_datum_t out;
  83. +
  84. + if (gnutls_certificate_verification_status_print
  85. + (status, gnutls_certificate_type_get(sess), &out,
  86. + 0) == GNUTLS_E_SUCCESS) {
  87. + Log(LOG_ERR,
  88. + "Certificate validation failed: %s",
  89. + out.data);
  90. + gnutls_free(out.data);
  91. + }
  92. + }
  93. +
  94. + err = gnutls_x509_crt_check_hostname(cert, c->host);
  95. + if (err == 0)
  96. + Log(LOG_ERR,
  97. + "Failed to verify the hostname, expected \"%s\"",
  98. + c->host);
  99. + else
  100. + cert_ok = verify == 0 && status == 0;
  101. +
  102. + snprintf(msg, sizeof(msg), "Got %svalid server certificate",
  103. + cert_ok ? "" : "in");
  104. + LogGnuTLS_CertInfo(LOG_INFO, cert, msg);
  105. + } else {
  106. + /* Incoming connection. Please see comments for OpenSSL! */
  107. + LogGnuTLS_CertInfo(LOG_INFO, cert,
  108. + "Got unchecked peer certificate");
  109. + }
  110. gnutls_x509_crt_deinit(cert);
  111. done_cn_validation: