Skip to content

Commit 3f45150

Browse files
authored
Merge pull request #648 from rhenium/ky/error-additional-data
Include "additional data" message in OpenSSL errors
2 parents 283958a + 1c5bbdd commit 3f45150

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

ext/openssl/ossl.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,28 @@ VALUE
272272
ossl_make_error(VALUE exc, VALUE str)
273273
{
274274
unsigned long e;
275+
const char *data;
276+
int flags;
275277

276-
e = ERR_peek_last_error();
278+
if (NIL_P(str))
279+
str = rb_str_new(NULL, 0);
280+
281+
#ifdef HAVE_ERR_GET_ERROR_ALL
282+
e = ERR_peek_last_error_all(NULL, NULL, NULL, &data, &flags);
283+
#else
284+
e = ERR_peek_last_error_line_data(NULL, NULL, &data, &flags);
285+
#endif
277286
if (e) {
278-
const char *msg = ERR_reason_error_string(e);
287+
const char *msg = ERR_reason_error_string(e);
279288

280-
if (NIL_P(str)) {
281-
if (msg) str = rb_str_new_cstr(msg);
282-
}
283-
else {
284-
if (RSTRING_LEN(str)) rb_str_cat2(str, ": ");
285-
rb_str_cat2(str, msg ? msg : "(null)");
286-
}
287-
ossl_clear_error();
289+
if (RSTRING_LEN(str)) rb_str_cat_cstr(str, ": ");
290+
rb_str_cat_cstr(str, msg ? msg : "(null)");
291+
if (flags & ERR_TXT_STRING && data)
292+
rb_str_catf(str, " (%s)", data);
293+
ossl_clear_error();
288294
}
289295

290-
if (NIL_P(str)) str = rb_str_new(0, 0);
291-
return rb_exc_new3(exc, str);
296+
return rb_exc_new_str(exc, str);
292297
}
293298

294299
void

test/openssl/test_config.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,19 @@ def test_s_parse_format
9191
assert_equal('123baz456bar798', c['dollar']['qux'])
9292
assert_equal('123baz456bar798.123baz456bar798', c['dollar']['quxx'])
9393

94-
excn = assert_raise(OpenSSL::ConfigError) do
94+
assert_raise_with_message(OpenSSL::ConfigError, /error in line 1: variable has no value/) do
9595
OpenSSL::Config.parse("foo = $bar")
9696
end
97-
assert_equal("error in line 1: variable has no value", excn.message)
9897

99-
excn = assert_raise(OpenSSL::ConfigError) do
98+
assert_raise_with_message(OpenSSL::ConfigError, /error in line 1: no close brace/) do
10099
OpenSSL::Config.parse("foo = $(bar")
101100
end
102-
assert_equal("error in line 1: no close brace", excn.message)
103101

104-
excn = assert_raise(OpenSSL::ConfigError) do
102+
assert_raise_with_message(OpenSSL::ConfigError, /error in line 1: missing equal sign/) do
105103
OpenSSL::Config.parse("f o =b ar # no space in key")
106104
end
107-
assert_equal("error in line 1: missing equal sign", excn.message)
108105

109-
excn = assert_raise(OpenSSL::ConfigError) do
106+
assert_raise_with_message(OpenSSL::ConfigError, /error in line 7: missing close square bracket/) do
110107
OpenSSL::Config.parse(<<__EOC__)
111108
# comment 1 # comments
112109
@@ -117,7 +114,6 @@ def test_s_parse_format
117114
[third # section not terminated
118115
__EOC__
119116
end
120-
assert_equal("error in line 7: missing close square bracket", excn.message)
121117
end
122118

123119
def test_s_parse_include

test/openssl/test_ossl.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ def test_memcmp_timing
6060
assert_operator(a_b_time, :<, a_c_time * 10, "fixed_length_secure_compare timing test failed")
6161
assert_operator(a_c_time, :<, a_b_time * 10, "fixed_length_secure_compare timing test failed")
6262
end
63+
64+
def test_error_data
65+
# X509V3_EXT_nconf_nid() called from OpenSSL::X509::ExtensionFactory#create_ext is a function
66+
# that uses ERR_raise_data() to append additional information about the error.
67+
#
68+
# The generated message should look like:
69+
# "subjectAltName = IP:not.a.valid.ip.address: bad ip address (value=not.a.valid.ip.address)"
70+
ef = OpenSSL::X509::ExtensionFactory.new
71+
assert_raise_with_message(OpenSSL::X509::ExtensionError, /\(value=not.a.valid.ip.address\)/) {
72+
ef.create_ext("subjectAltName", "IP:not.a.valid.ip.address")
73+
}
74+
end
6375
end
6476

6577
end

0 commit comments

Comments
 (0)