Skip to content

Commit 5f167cb

Browse files
committed
adjust X.509 value handling to parse subjectAltName recursively (#134)
1 parent 34c777c commit 5f167cb

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ final byte[] getRealValueEncoded() throws IOException {
207207
if ( value instanceof byte[] ) return (byte[]) value;
208208
if ( value instanceof RubyString ) return ((RubyString) value).getBytes();
209209
if ( value instanceof String ) return ByteList.plain((String) value);
210-
210+
211211
if ( value instanceof ASN1OctetString ) { // initialize
212212
return ((ASN1OctetString) value).getOctets();
213213
}
@@ -536,7 +536,7 @@ public RubyString value(final ThreadContext context) {
536536
for ( int i = 0; i < names.length; i++ ) {
537537
boolean other = formatGeneralName(names[i], val, false);
538538
if ( i < names.length - 1 ) {
539-
if ( other ) val.append(';'); else val.append(',');
539+
if ( other ) val.append(';'); else val.append(',').append(' ');
540540
}
541541
}
542542
return runtime.newString( val );

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ else if (id.equals("2.5.29.37")) { // extendedKeyUsage
209209
}
210210
}
211211
catch (IOException e) {
212+
OpenSSL.debugStackTrace(e);
212213
throw newExtensionError(runtime, "Unable to create extension: " + e.getMessage());
213214
}
214215
return newExtension(runtime, objectId, value, critical.isNil() ? null : critical.isTrue());
@@ -518,8 +519,19 @@ private static ASN1Encodable parseSubjectAltName(final String valuex) throws IOE
518519
return new GeneralName(GeneralName.registeredID, rid);
519520
}
520521
if ( valuex.startsWith(email_) ) {
521-
final String mail = valuex.substring(email_.length());
522-
return new GeneralName(GeneralName.rfc822Name, mail);
522+
final String[] vals = valuex.split(",");
523+
final GeneralName[] names = new GeneralName[vals.length];
524+
for ( int i = 0; i < vals.length; i++ ) {
525+
if (vals[i].startsWith(email_)) {
526+
String mail = vals[i].substring(email_.length());
527+
names[i] = new GeneralName(GeneralName.rfc822Name, mail);
528+
}
529+
else {
530+
ASN1Encodable name = parseSubjectAltName(vals[i]);
531+
names[i] = name instanceof GeneralNames ? ((GeneralNames) name).getNames()[0] : (GeneralName) name;
532+
}
533+
}
534+
return new GeneralNames(names);
523535
}
524536
if ( valuex.startsWith("IP:") || valuex.startsWith("IP Address:") ) {
525537
final int idx = valuex.charAt(2) == ':' ? 3 : 11;

src/test/ruby/x509/test_x509ext.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ def test_subject_alt_name_sign_to_pem
150150
assert dns =~ /test.example.com.*?test2.example.com.*?example.com.*?www.example.com/
151151
end
152152

153+
def test_subject_alt_name_sequence
154+
extensions = OpenSSL::X509::ExtensionFactory.new
155+
ext = extensions.create_extension("subjectAltName", "email:[email protected],DNS:a.b.com,email:[email protected]")
156+
assert_equal 'subjectAltName', ext.oid
157+
assert_equal 'email:[email protected], DNS:a.b.com, email:[email protected]', ext.value
158+
mri_der = "0,\x06\x03U\x1D\x11\x04%0#\x81\v[email protected]\x82\aa.b.com\x81\v[email protected]"
159+
assert_equal mri_der, ext.to_der
160+
end
161+
153162
def subject_alt_name(domains)
154163
ef = OpenSSL::X509::ExtensionFactory.new
155164
ef.create_extension("subjectAltName", domains.split(',').map { |d| "DNS: #{d}" }.join(','))

0 commit comments

Comments
 (0)