Skip to content

Commit 5113777

Browse files
committed
ssl: adjust "certificate verify failed" error on SSL_ERROR_SYSCALL
Enrich SSLError's message with the low-level certificate verification result, even if SSL_get_error() returns SSL_ERROR_SYSCALL. This is currently done on SSL_ERROR_SSL only. According to the man page of SSL_get_error(), SSL_ERROR_SYSCALL may be returned for "other errors, check the error queue for details". This apparently means we have to treat SSL_ERROR_SYSCALL, if errno is not set, as equivalent to SSL_ERROR_SSL.
1 parent 10833aa commit 5113777

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

ext/openssl/ossl_ssl.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,9 +1756,6 @@ ossl_start_ssl(VALUE self, int (*func)(SSL *), const char *funcname, VALUE opts)
17561756
int ret, ret2;
17571757
VALUE cb_state;
17581758
int nonblock = opts != Qfalse;
1759-
#if defined(SSL_R_CERTIFICATE_VERIFY_FAILED)
1760-
unsigned long err;
1761-
#endif
17621759

17631760
rb_ivar_set(self, ID_callback_state, Qnil);
17641761

@@ -1796,30 +1793,33 @@ ossl_start_ssl(VALUE self, int (*func)(SSL *), const char *funcname, VALUE opts)
17961793
continue;
17971794
#endif
17981795
if (errno) rb_sys_fail(funcname);
1799-
ossl_raise(eSSLError, "%s SYSCALL returned=%d errno=%d peeraddr=%"PRIsVALUE" state=%s",
1800-
funcname, ret2, errno, peeraddr_ip_str(self), SSL_state_string_long(ssl));
1801-
1796+
/* fallthrough */
1797+
default: {
1798+
VALUE error_append = Qnil;
18021799
#if defined(SSL_R_CERTIFICATE_VERIFY_FAILED)
1803-
case SSL_ERROR_SSL:
1804-
err = ERR_peek_last_error();
1805-
if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
1806-
ERR_GET_REASON(err) == SSL_R_CERTIFICATE_VERIFY_FAILED) {
1807-
const char *err_msg = ERR_reason_error_string(err),
1808-
*verify_msg = X509_verify_cert_error_string(SSL_get_verify_result(ssl));
1809-
if (!err_msg)
1810-
err_msg = "(null)";
1811-
if (!verify_msg)
1812-
verify_msg = "(null)";
1813-
ossl_clear_error(); /* let ossl_raise() not append message */
1814-
ossl_raise(eSSLError, "%s returned=%d errno=%d peeraddr=%"PRIsVALUE" state=%s: %s (%s)",
1815-
funcname, ret2, errno, peeraddr_ip_str(self), SSL_state_string_long(ssl),
1816-
err_msg, verify_msg);
1817-
}
1800+
unsigned long err = ERR_peek_last_error();
1801+
if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
1802+
ERR_GET_REASON(err) == SSL_R_CERTIFICATE_VERIFY_FAILED) {
1803+
const char *err_msg = ERR_reason_error_string(err),
1804+
*verify_msg = X509_verify_cert_error_string(SSL_get_verify_result(ssl));
1805+
if (!err_msg)
1806+
err_msg = "(null)";
1807+
if (!verify_msg)
1808+
verify_msg = "(null)";
1809+
ossl_clear_error(); /* let ossl_raise() not append message */
1810+
error_append = rb_sprintf(": %s (%s)", err_msg, verify_msg);
1811+
}
18181812
#endif
1819-
/* fallthrough */
1820-
default:
1821-
ossl_raise(eSSLError, "%s returned=%d errno=%d peeraddr=%"PRIsVALUE" state=%s",
1822-
funcname, ret2, errno, peeraddr_ip_str(self), SSL_state_string_long(ssl));
1813+
ossl_raise(eSSLError,
1814+
"%s%s returned=%d errno=%d peeraddr=%"PRIsVALUE" state=%s%"PRIsVALUE,
1815+
funcname,
1816+
ret2 == SSL_ERROR_SYSCALL ? " SYSCALL" : "",
1817+
ret2,
1818+
errno,
1819+
peeraddr_ip_str(self),
1820+
SSL_state_string_long(ssl),
1821+
error_append);
1822+
}
18231823
}
18241824
}
18251825

0 commit comments

Comments
 (0)