diff options
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | ext/openssl/ossl_asn1.c | 14 | ||||
-rw-r--r-- | test/openssl/test_asn1.rb | 12 |
3 files changed, 32 insertions, 3 deletions
@@ -1,3 +1,12 @@ +Thu Mar 29 07:45:36 2012 Martin Bosslet <Martin.Bosslet@googlemail.com> + + * ext/openssl/ossl_asn1.c: raise TypeError when trying to encode nil + values for Primitive instances. + * test/openssl/test_asn1.rb: Assert consistent behavior when + encoding nil values: Primitives raise TypeError, Constructives + raise NoMethodError. + Fixes [ruby-core:43009][Bug #6102] + Wed Mar 28 16:39:59 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> * process.c (obj2uid, obj2gid): allow strings as input user/group id. diff --git a/ext/openssl/ossl_asn1.c b/ext/openssl/ossl_asn1.c index adcb3263f0..18329bec28 100644 --- a/ext/openssl/ossl_asn1.c +++ b/ext/openssl/ossl_asn1.c @@ -149,11 +149,16 @@ num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai) ASN1_INTEGER * num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai) { - BIGNUM *bn = GetBNPtr(obj); + BIGNUM *bn; + + if (NIL_P(obj)) + ossl_raise(rb_eTypeError, "Can't convert nil into Integer"); - if (!(ai = BN_to_ASN1_INTEGER(bn, ai))) { + bn = GetBNPtr(obj); + + if (!(ai = BN_to_ASN1_INTEGER(bn, ai))) ossl_raise(eOSSLError, NULL); - } + return ai; } #endif @@ -219,6 +224,9 @@ static ID sivVALUE, sivTAG, sivTAG_CLASS, sivTAGGING, sivINFINITE_LENGTH, sivUNU static ASN1_BOOLEAN obj_to_asn1bool(VALUE obj) { + if (NIL_P(obj)) + ossl_raise(rb_eTypeError, "Can't convert nil into Boolean"); + #if OPENSSL_VERSION_NUMBER < 0x00907000L return RTEST(obj) ? 0xff : 0x100; #else diff --git a/test/openssl/test_asn1.rb b/test/openssl/test_asn1.rb index 09324761da..811fe387fa 100644 --- a/test/openssl/test_asn1.rb +++ b/test/openssl/test_asn1.rb @@ -198,6 +198,18 @@ class OpenSSL::TestASN1 < Test::Unit::TestCase encode_decode_test(OpenSSL::ASN1::Integer, [72, -127, -128, 128, -1, 0, 1, -(2**12345), 2**12345]) end + def test_encode_nil + m = OpenSSL::ASN1 + [ + m::Boolean, m::Integer, m::BitString, m::OctetString, + m::ObjectId, m::Enumerated, m::UTF8String, m::UTCTime, + m::GeneralizedTime, m::Sequence, m::Set + ].each do |klass| + #Primitives raise TypeError, Constructives NoMethodError + assert_raise(TypeError, NoMethodError) { klass.send(:new, nil).to_der } + end + end + def encode_decode_test(type, values) values.each do |v| assert_equal(v, OpenSSL::ASN1.decode(type.new(v).to_der).value) |