Skip to content

Commit 7b2fdb8

Browse files
committed
x509name: fix handling of X509_NAME_{oneline,print_ex}() return value
X509_NAME_print_ex() behaves differently depending on the passed flags. When XN_FLAG_COMPAT is specified, it returns either 1 on success or 0 on error. Otherwise, it returns the byte size written or -1 on error. This means 0 return is not necessarily an error. Also, X509_NAME_oneline() return value needs to be checked as it may fail with a NULL return. Fixes: #200
1 parent 49c9d3f commit 7b2fdb8

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

ext/openssl/ossl_x509name.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,27 +239,27 @@ ossl_x509name_to_s_old(VALUE self)
239239
{
240240
X509_NAME *name;
241241
char *buf;
242-
VALUE str;
243242

244243
GetX509Name(self, name);
245244
buf = X509_NAME_oneline(name, NULL, 0);
246-
str = rb_str_new2(buf);
247-
OPENSSL_free(buf);
248-
249-
return str;
245+
if (!buf)
246+
ossl_raise(eX509NameError, "X509_NAME_oneline");
247+
return ossl_buf2str(buf, rb_long2int(strlen(buf)));
250248
}
251249

252250
static VALUE
253251
x509name_print(VALUE self, unsigned long iflag)
254252
{
255253
X509_NAME *name;
256254
BIO *out;
255+
int ret;
257256

258257
GetX509Name(self, name);
259258
out = BIO_new(BIO_s_mem());
260259
if (!out)
261260
ossl_raise(eX509NameError, NULL);
262-
if (!X509_NAME_print_ex(out, name, 0, iflag)) {
261+
ret = X509_NAME_print_ex(out, name, 0, iflag);
262+
if (ret < 0 || iflag == XN_FLAG_COMPAT && ret == 0) {
263263
BIO_free(out);
264264
ossl_raise(eX509NameError, "X509_NAME_print_ex");
265265
}

test/test_x509name.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,34 @@ def test_add_entry_street
322322
assert_equal("Namiki", ary[5][1])
323323
end
324324

325+
def test_to_s
326+
dn = [
327+
["DC", "org"],
328+
["DC", "ruby-lang"],
329+
["CN", "フー, バー"],
330+
]
331+
name = OpenSSL::X509::Name.new
332+
dn.each { |x| name.add_entry(*x) }
333+
334+
assert_equal "/DC=org/DC=ruby-lang/" \
335+
"CN=\\xE3\\x83\\x95\\xE3\\x83\\xBC, \\xE3\\x83\\x90\\xE3\\x83\\xBC",
336+
name.to_s
337+
# OpenSSL escapes characters with MSB by default
338+
assert_equal \
339+
"CN=\\E3\\83\\95\\E3\\83\\BC\\, \\E3\\83\\90\\E3\\83\\BC," \
340+
"DC=ruby-lang,DC=org",
341+
name.to_s(OpenSSL::X509::Name::RFC2253)
342+
assert_equal "DC = org, DC = ruby-lang, " \
343+
"CN = \"\\E3\\83\\95\\E3\\83\\BC, \\E3\\83\\90\\E3\\83\\BC\"",
344+
name.to_s(OpenSSL::X509::Name::ONELINE)
345+
346+
empty = OpenSSL::X509::Name.new
347+
assert_equal "", empty.to_s
348+
assert_equal "", empty.to_s(OpenSSL::X509::Name::COMPAT)
349+
assert_equal "", empty.to_s(OpenSSL::X509::Name::RFC2253)
350+
assert_equal "", empty.to_s(OpenSSL::X509::Name::ONELINE)
351+
end
352+
325353
def test_equals2
326354
n1 = OpenSSL::X509::Name.parse 'CN=a'
327355
n2 = OpenSSL::X509::Name.parse 'CN=a'

0 commit comments

Comments
 (0)