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

to_der on ASN1Data should convert ruby strings into java strings before encoding #265

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 10 additions & 10 deletions src/main/java/org/jruby/ext/openssl/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -1063,15 +1063,14 @@ else if ( obj instanceof ASN1GraphicString ) {
break;
}

if (taggedObj.getTagClass() == BERTags.APPLICATION) {
try {
final ASN1Sequence sequence = (ASN1Sequence) taggedObj.getBaseUniversal(false, SEQUENCE);
@SuppressWarnings("unchecked")
final RubyArray valArr = decodeObjects(context, ASN1, sequence.getObjects());
return ASN1.getClass("ASN1Data").newInstance(context, new IRubyObject[] { valArr, tag, tag_class }, Block.NULL_BLOCK);
} else {
IRubyObject val = decodeObject(context, ASN1, taggedObj.getBaseObject());
final RubyArray valArr = runtime.newArray(val);
return ASN1.getClass("ASN1Data").newInstance(context, new IRubyObject[] { valArr, tag, tag_class }, Block.NULL_BLOCK);
} catch (IllegalStateException e) {
IRubyObject val = decodeObject(context, ASN1, taggedObj.getBaseObject()).callMethod(context, "value");
return ASN1.getClass("ASN1Data").newInstance(context, new IRubyObject[] { val, tag, tag_class }, Block.NULL_BLOCK);
}
}

Expand Down Expand Up @@ -1397,12 +1396,13 @@ final ASN1TaggedObject toASN1TaggedObject(final ThreadContext context) {
vec.add( data );
}
return new DERTaggedObject(isExplicitTagging(), tag, new DERSequence(vec));
} else if (value instanceof ASN1Data) {
return new DERTaggedObject(isExplicitTagging(), tagClass, tag, ((ASN1Data) value).toASN1(context));
} else if (value instanceof RubyObject) {
return new DERTaggedObject(isExplicitTagging(), tagClass, tag, new DERGeneralString(value.asString().asJavaString()));
} else {
throw context.runtime.newTypeError("no implicit conversion of " + value.getMetaClass().getBaseName() + " into String");
}

if (!(value instanceof ASN1Data)) {
throw new UnsupportedOperationException("toASN1 " + inspect() + " value: " + value.inspect() + " (" + value.getMetaClass() + ")");
}
return new DERTaggedObject(isExplicitTagging(), tagClass, tag, ((ASN1Data) value).toASN1(context));
}

@JRubyMethod
Expand Down
32 changes: 31 additions & 1 deletion src/test/ruby/test_asn1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,36 @@ def test_null
}
end

def test_encode_asn1_data
ai = OpenSSL::ASN1::ASN1Data.new(i = "bla", 0, :APPLICATION)
ai2 = OpenSSL::ASN1.decode(ai.to_der)
assert_equal :APPLICATION, ai2.tag_class
assert_equal 0, ai2.tag
assert_equal i, ai2.value

ai = OpenSSL::ASN1::ASN1Data.new(i = "bla", 4, :UNIVERSAL)
ai2 = OpenSSL::ASN1.decode(ai.to_der)
assert_equal :UNIVERSAL, ai2.tag_class
assert_equal 4, ai2.tag
assert_equal i, ai2.value

ai = OpenSSL::ASN1::ASN1Data.new(i = ["bla"], 0, :APPLICATION)
ai2 = OpenSSL::ASN1.decode(ai.to_der)
assert_equal :APPLICATION, ai2.tag_class
assert_equal 0, ai2.tag
assert_equal "bla", ai2.value

ai = OpenSSL::ASN1::ASN1Data.new(i = ["bla", "bla"], 0, :APPLICATION)
ai2 = OpenSSL::ASN1.decode(ai.to_der)
assert_equal :APPLICATION, ai2.tag_class
assert_equal 0, ai2.tag
assert_equal "blabla", ai2.value

assert_raise(ArgumentError) { OpenSSL::ASN1::ASN1Data.new(1).to_der }
assert_raise("no implicit conversion of Integer into String") { OpenSSL::ASN1::ASN1Data.new(1, 0, :APPLICATION).to_der }
assert_raise("no implicit conversion of Integer into String") { OpenSSL::ASN1::ASN1Data.new(1, 0, :CONTEXT_SPECIFIC).to_der }
end

def test_encode_nil
#Primitives raise TypeError, Constructives NoMethodError

Expand Down Expand Up @@ -1161,7 +1191,7 @@ def test_decode
# This is from the upstream MRI tests, might be superseded by `test_bit_string_infinite_length`?
def test_bitstring
# TODO: Import Issue
# fails <nil> expected but was <0>
# fails <nil> expected but was <0>
#encode_decode_test B(%w{ 03 01 00 }), OpenSSL::ASN1::BitString.new(B(%w{}))
# TODO: Import Issue
# fails with <nil> expected but was <0>
Expand Down
Loading