Skip to content

Commit 307db49

Browse files
authored
Merge pull request #211 from rhenium/ky/x509name-to-s-empty
x509name: fix handling of X509_NAME_{oneline,print_ex}() return value
2 parents a1e8aac + 7b2fdb8 commit 307db49

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

ext/openssl/ossl_x509name.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,31 @@ 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);
245+
if (!buf)
246+
ossl_raise(eX509NameError, "X509_NAME_oneline");
247+
return ossl_buf2str(buf, rb_long2int(strlen(buf)));
248+
}
248249

249-
return str;
250+
static VALUE
251+
x509name_print(VALUE self, unsigned long iflag)
252+
{
253+
X509_NAME *name;
254+
BIO *out;
255+
int ret;
256+
257+
GetX509Name(self, name);
258+
out = BIO_new(BIO_s_mem());
259+
if (!out)
260+
ossl_raise(eX509NameError, NULL);
261+
ret = X509_NAME_print_ex(out, name, 0, iflag);
262+
if (ret < 0 || iflag == XN_FLAG_COMPAT && ret == 0) {
263+
BIO_free(out);
264+
ossl_raise(eX509NameError, "X509_NAME_print_ex");
265+
}
266+
return ossl_membio2str(out);
250267
}
251268

252269
/*
@@ -264,25 +281,12 @@ ossl_x509name_to_s_old(VALUE self)
264281
static VALUE
265282
ossl_x509name_to_s(int argc, VALUE *argv, VALUE self)
266283
{
267-
X509_NAME *name;
268-
VALUE flag, str;
269-
BIO *out;
270-
unsigned long iflag;
271-
272-
rb_scan_args(argc, argv, "01", &flag);
273-
if (NIL_P(flag))
284+
rb_check_arity(argc, 0, 1);
285+
/* name.to_s(nil) was allowed */
286+
if (!argc || NIL_P(argv[0]))
274287
return ossl_x509name_to_s_old(self);
275-
else iflag = NUM2ULONG(flag);
276-
if (!(out = BIO_new(BIO_s_mem())))
277-
ossl_raise(eX509NameError, NULL);
278-
GetX509Name(self, name);
279-
if (!X509_NAME_print_ex(out, name, 0, iflag)){
280-
BIO_free(out);
281-
ossl_raise(eX509NameError, NULL);
282-
}
283-
str = ossl_membio2str(out);
284-
285-
return str;
288+
else
289+
return x509name_print(self, NUM2ULONG(argv[0]));
286290
}
287291

288292
/*

test/test_x509name.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# coding: US-ASCII
1+
# coding: ASCII-8BIT
22
# frozen_string_literal: false
33
require_relative 'utils'
44

@@ -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)