Skip to content

Commit

Permalink
[fix] respect @unused_bits in BitString (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Jan 9, 2025
1 parent 976a3f5 commit ea06253
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/main/java/org/jruby/ext/openssl/ASN1.java
Original file line number Diff line number Diff line change
Expand Up @@ -1678,13 +1678,10 @@ private ASN1Encodable toASN1Primitive(final ThreadContext context) {
}
if ( type == DERBitString.class ) {
final byte[] data = val.asString().getBytes();
int padBits = 0; // padBits < 8 && padBits >= 0
for (int i = (data.length - 1); i > -1; i--) {
int b = Byte.toUnsignedInt(data[i]);
if (b != 0) {
padBits = Integer.numberOfTrailingZeros(b);
break;
}
int padBits = 0;
IRubyObject unused_bits = getInstanceVariable("@unused_bits");
if (unused_bits != null) {
padBits = unused_bits.convertToInteger("to_i").getIntValue();
}
return new DERBitString(data, padBits);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/ruby/test_asn1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,16 @@ def test_bitstring
#}
end

def test_bit_string_unused_length
asn = "\x00\x04".b
asn.prepend "\x03#{asn.bytesize.chr}"
bs = OpenSSL::ASN1.decode(asn)
assert_equal asn, bs.to_der

bs.unused_bits = 6
assert_equal "\x03\x02\x06\x00", bs.to_der
end

def test_bit_string_infinite_length
begin
content = [ OpenSSL::ASN1::BitString.new("\x01"), OpenSSL::ASN1::EndOfContent.new() ]
Expand Down

0 comments on commit ea06253

Please sign in to comment.