Skip to content

Commit 6230c59

Browse files
committed
a simple resolution for handling subjectAltName multiple DNS: names
#102
1 parent aa9f9a2 commit 6230c59

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/main/java/org/jruby/ext/openssl/X509ExtensionFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.bouncycastle.asn1.DEROctetString;
4343
import org.bouncycastle.asn1.DLSequence;
4444
import org.bouncycastle.asn1.x509.GeneralName;
45+
import org.bouncycastle.asn1.x509.GeneralNames;
4546

4647
import org.jruby.Ruby;
4748
import org.jruby.RubyArray;
@@ -458,8 +459,13 @@ private ASN1Encodable parseIssuerAltName(final ThreadContext context, final Stri
458459

459460
private static ASN1Encodable parseSubjectAltName(final String valuex) throws IOException {
460461
if ( valuex.startsWith(DNS_) ) {
461-
final String dns = valuex.substring(DNS_.length());
462-
return new GeneralName(GeneralName.dNSName, dns);
462+
final String[] vals = valuex.split(",");
463+
final GeneralName[] names = new GeneralName[vals.length];
464+
for ( int i = 0; i < vals.length; i++ ) {
465+
final String dns = vals[i].substring(DNS_.length());
466+
names[i] = new GeneralName(GeneralName.dNSName, dns);
467+
}
468+
return new GeneralNames(names);
463469
}
464470
if ( valuex.startsWith(DNS_Name_) ) {
465471
final String dns = valuex.substring(DNS_Name_.length());

src/test/ruby/x509/test_x509ext.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,34 @@ def test_to_der_is_the_same_for_non_critical
126126
assert ext1.to_der != ext2.to_der
127127
end
128128

129+
def test_subject_alt_name_sign_to_pem
130+
domain_list = 'test.example.com,test2.example.com,example.com,www.example.com'
131+
132+
rsa_key = OpenSSL::PKey::RSA.new(2048)
133+
csr = OpenSSL::X509::Request.new
134+
csr.subject = OpenSSL::X509::Name.new [ ["C", 'AU'], ["ST", "NSW"], ["O", 'org'], ["CN", 'www.example.com'] ]
135+
csr.public_key = rsa_key.public_key
136+
137+
extensions = OpenSSL::ASN1::Set [ OpenSSL::ASN1::Sequence([ subject_alt_name(domain_list) ]) ]
138+
csr.add_attribute(OpenSSL::X509::Attribute.new('extReq', extensions))
139+
csr.add_attribute(OpenSSL::X509::Attribute.new('msExtReq', extensions))
140+
141+
csr.sign rsa_key, OpenSSL::Digest::SHA256.new
142+
143+
puts csr.to_text if $VERBOSE
144+
145+
csr = OpenSSL::X509::Request.new pem = csr.to_pem
146+
assert_equal 2, csr.attributes.length
147+
ext_set = csr.attributes.first.value ; seq = ext_set.first.value
148+
assert_equal 'subjectAltName', seq.first.value.first.value
149+
dns = seq.first.value.last.value
150+
assert dns =~ /test.example.com.*?test2.example.com.*?example.com.*?www.example.com/
151+
end
152+
153+
def subject_alt_name(domains)
154+
ef = OpenSSL::X509::ExtensionFactory.new
155+
ef.create_extension("subjectAltName", domains.split(',').map { |d| "DNS: #{d}" }.join(','))
156+
end
157+
private :subject_alt_name
158+
129159
end

0 commit comments

Comments
 (0)