Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x509name: fix handling of X509_NAME_{oneline,print_ex}() return value #211

Merged
merged 3 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions ext/openssl/ossl_x509name.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,31 @@ ossl_x509name_to_s_old(VALUE self)
{
X509_NAME *name;
char *buf;
VALUE str;

GetX509Name(self, name);
buf = X509_NAME_oneline(name, NULL, 0);
str = rb_str_new2(buf);
OPENSSL_free(buf);
if (!buf)
ossl_raise(eX509NameError, "X509_NAME_oneline");
return ossl_buf2str(buf, rb_long2int(strlen(buf)));
}

return str;
static VALUE
x509name_print(VALUE self, unsigned long iflag)
{
X509_NAME *name;
BIO *out;
int ret;

GetX509Name(self, name);
out = BIO_new(BIO_s_mem());
if (!out)
ossl_raise(eX509NameError, NULL);
ret = X509_NAME_print_ex(out, name, 0, iflag);
if (ret < 0 || iflag == XN_FLAG_COMPAT && ret == 0) {
BIO_free(out);
ossl_raise(eX509NameError, "X509_NAME_print_ex");
}
return ossl_membio2str(out);
}

/*
Expand All @@ -264,25 +281,12 @@ ossl_x509name_to_s_old(VALUE self)
static VALUE
ossl_x509name_to_s(int argc, VALUE *argv, VALUE self)
{
X509_NAME *name;
VALUE flag, str;
BIO *out;
unsigned long iflag;

rb_scan_args(argc, argv, "01", &flag);
if (NIL_P(flag))
rb_check_arity(argc, 0, 1);
/* name.to_s(nil) was allowed */
if (!argc || NIL_P(argv[0]))
return ossl_x509name_to_s_old(self);
else iflag = NUM2ULONG(flag);
if (!(out = BIO_new(BIO_s_mem())))
ossl_raise(eX509NameError, NULL);
GetX509Name(self, name);
if (!X509_NAME_print_ex(out, name, 0, iflag)){
BIO_free(out);
ossl_raise(eX509NameError, NULL);
}
str = ossl_membio2str(out);

return str;
else
return x509name_print(self, NUM2ULONG(argv[0]));
}

/*
Expand Down
30 changes: 29 additions & 1 deletion test/test_x509name.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# coding: US-ASCII
# coding: ASCII-8BIT
# frozen_string_literal: false
require_relative 'utils'

Expand Down Expand Up @@ -322,6 +322,34 @@ def test_add_entry_street
assert_equal("Namiki", ary[5][1])
end

def test_to_s
dn = [
["DC", "org"],
["DC", "ruby-lang"],
["CN", "フー, バー"],
]
name = OpenSSL::X509::Name.new
dn.each { |x| name.add_entry(*x) }

assert_equal "/DC=org/DC=ruby-lang/" \
"CN=\\xE3\\x83\\x95\\xE3\\x83\\xBC, \\xE3\\x83\\x90\\xE3\\x83\\xBC",
name.to_s
# OpenSSL escapes characters with MSB by default
assert_equal \
"CN=\\E3\\83\\95\\E3\\83\\BC\\, \\E3\\83\\90\\E3\\83\\BC," \
"DC=ruby-lang,DC=org",
name.to_s(OpenSSL::X509::Name::RFC2253)
assert_equal "DC = org, DC = ruby-lang, " \
"CN = \"\\E3\\83\\95\\E3\\83\\BC, \\E3\\83\\90\\E3\\83\\BC\"",
name.to_s(OpenSSL::X509::Name::ONELINE)

empty = OpenSSL::X509::Name.new
assert_equal "", empty.to_s
assert_equal "", empty.to_s(OpenSSL::X509::Name::COMPAT)
assert_equal "", empty.to_s(OpenSSL::X509::Name::RFC2253)
assert_equal "", empty.to_s(OpenSSL::X509::Name::ONELINE)
end

def test_equals2
n1 = OpenSSL::X509::Name.parse 'CN=a'
n2 = OpenSSL::X509::Name.parse 'CN=a'
Expand Down