Skip to content

Commit

Permalink
adjust X.509 value handling to parse subjectAltName recursively (jrub…
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jul 28, 2017
1 parent 34c777c commit 5f167cb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jruby/ext/openssl/X509Extension.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ final byte[] getRealValueEncoded() throws IOException {
if ( value instanceof byte[] ) return (byte[]) value;
if ( value instanceof RubyString ) return ((RubyString) value).getBytes();
if ( value instanceof String ) return ByteList.plain((String) value);

if ( value instanceof ASN1OctetString ) { // initialize
return ((ASN1OctetString) value).getOctets();
}
Expand Down Expand Up @@ -536,7 +536,7 @@ public RubyString value(final ThreadContext context) {
for ( int i = 0; i < names.length; i++ ) {
boolean other = formatGeneralName(names[i], val, false);
if ( i < names.length - 1 ) {
if ( other ) val.append(';'); else val.append(',');
if ( other ) val.append(';'); else val.append(',').append(' ');
}
}
return runtime.newString( val );
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/org/jruby/ext/openssl/X509ExtensionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ else if (id.equals("2.5.29.37")) { // extendedKeyUsage
}
}
catch (IOException e) {
OpenSSL.debugStackTrace(e);
throw newExtensionError(runtime, "Unable to create extension: " + e.getMessage());
}
return newExtension(runtime, objectId, value, critical.isNil() ? null : critical.isTrue());
Expand Down Expand Up @@ -518,8 +519,19 @@ private static ASN1Encodable parseSubjectAltName(final String valuex) throws IOE
return new GeneralName(GeneralName.registeredID, rid);
}
if ( valuex.startsWith(email_) ) {
final String mail = valuex.substring(email_.length());
return new GeneralName(GeneralName.rfc822Name, mail);
final String[] vals = valuex.split(",");
final GeneralName[] names = new GeneralName[vals.length];
for ( int i = 0; i < vals.length; i++ ) {
if (vals[i].startsWith(email_)) {
String mail = vals[i].substring(email_.length());
names[i] = new GeneralName(GeneralName.rfc822Name, mail);
}
else {
ASN1Encodable name = parseSubjectAltName(vals[i]);
names[i] = name instanceof GeneralNames ? ((GeneralNames) name).getNames()[0] : (GeneralName) name;
}
}
return new GeneralNames(names);
}
if ( valuex.startsWith("IP:") || valuex.startsWith("IP Address:") ) {
final int idx = valuex.charAt(2) == ':' ? 3 : 11;
Expand Down
9 changes: 9 additions & 0 deletions src/test/ruby/x509/test_x509ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ def test_subject_alt_name_sign_to_pem
assert dns =~ /test.example.com.*?test2.example.com.*?example.com.*?www.example.com/
end

def test_subject_alt_name_sequence
extensions = OpenSSL::X509::ExtensionFactory.new
ext = extensions.create_extension("subjectAltName", "email:[email protected],DNS:a.b.com,email:[email protected]")
assert_equal 'subjectAltName', ext.oid
assert_equal 'email:[email protected], DNS:a.b.com, email:[email protected]', ext.value
mri_der = "0,\x06\x03U\x1D\x11\x04%0#\x81\v[email protected]\x82\aa.b.com\x81\v[email protected]"
assert_equal mri_der, ext.to_der
end

def subject_alt_name(domains)
ef = OpenSSL::X509::ExtensionFactory.new
ef.create_extension("subjectAltName", domains.split(',').map { |d| "DNS: #{d}" }.join(','))
Expand Down

0 comments on commit 5f167cb

Please sign in to comment.