Skip to content

Commit 1b5d41f

Browse files
authored
Merge pull request #792 from rhenium/ky/asn1-obj-fix-eq
asn1: fix ObjectId#==
2 parents c96e666 + c67e16e commit 1b5d41f

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

ext/openssl/ossl_asn1.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,30 +1297,6 @@ ossl_asn1obj_get_ln(VALUE self)
12971297
return ret;
12981298
}
12991299

1300-
/*
1301-
* call-seq:
1302-
* oid == other_oid => true or false
1303-
*
1304-
* Returns +true+ if _other_oid_ is the same as _oid_
1305-
*/
1306-
static VALUE
1307-
ossl_asn1obj_eq(VALUE self, VALUE other)
1308-
{
1309-
VALUE valSelf, valOther;
1310-
int nidSelf, nidOther;
1311-
1312-
valSelf = ossl_asn1_get_value(self);
1313-
valOther = ossl_asn1_get_value(other);
1314-
1315-
if ((nidSelf = OBJ_txt2nid(StringValueCStr(valSelf))) == NID_undef)
1316-
ossl_raise(eASN1Error, "OBJ_txt2nid");
1317-
1318-
if ((nidOther = OBJ_txt2nid(StringValueCStr(valOther))) == NID_undef)
1319-
ossl_raise(eASN1Error, "OBJ_txt2nid");
1320-
1321-
return nidSelf == nidOther ? Qtrue : Qfalse;
1322-
}
1323-
13241300
static VALUE
13251301
asn1obj_get_oid_i(VALUE vobj)
13261302
{
@@ -1365,6 +1341,25 @@ ossl_asn1obj_get_oid(VALUE self)
13651341
return str;
13661342
}
13671343

1344+
/*
1345+
* call-seq:
1346+
* oid == other_oid => true or false
1347+
*
1348+
* Returns +true+ if _other_oid_ is the same as _oid_.
1349+
*/
1350+
static VALUE
1351+
ossl_asn1obj_eq(VALUE self, VALUE other)
1352+
{
1353+
VALUE oid1, oid2;
1354+
1355+
if (!rb_obj_is_kind_of(other, cASN1ObjectId))
1356+
return Qfalse;
1357+
1358+
oid1 = ossl_asn1obj_get_oid(self);
1359+
oid2 = ossl_asn1obj_get_oid(other);
1360+
return rb_str_equal(oid1, oid2);
1361+
}
1362+
13681363
#define OSSL_ASN1_IMPL_FACTORY_METHOD(klass) \
13691364
static VALUE ossl_asn1_##klass(int argc, VALUE *argv, VALUE self)\
13701365
{ return rb_funcall3(cASN1##klass, rb_intern("new"), argc, argv); }

test/openssl/test_asn1.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,9 @@ def test_object_identifier
331331
pend "OBJ_obj2txt() not working (LibreSSL?)" if $!.message =~ /OBJ_obj2txt/
332332
raise
333333
end
334+
end
334335

336+
def test_object_identifier_equality
335337
aki = [
336338
OpenSSL::ASN1::ObjectId.new("authorityKeyIdentifier"),
337339
OpenSSL::ASN1::ObjectId.new("X509v3 Authority Key Identifier"),
@@ -346,17 +348,22 @@ def test_object_identifier
346348

347349
aki.each do |a|
348350
aki.each do |b|
349-
assert a == b
351+
assert_equal true, a == b
350352
end
351353

352354
ski.each do |b|
353-
refute a == b
355+
assert_equal false, a == b
354356
end
355357
end
356358

357-
assert_raise(TypeError) {
358-
OpenSSL::ASN1::ObjectId.new("authorityKeyIdentifier") == nil
359-
}
359+
obj1 = OpenSSL::ASN1::ObjectId.new("1.2.34.56789.10")
360+
obj2 = OpenSSL::ASN1::ObjectId.new("1.2.34.56789.10")
361+
obj3 = OpenSSL::ASN1::ObjectId.new("1.2.34.56789.11")
362+
omit "OID 1.2.34.56789.10 is registered" if obj1.sn
363+
assert_equal true, obj1 == obj2
364+
assert_equal false, obj1 == obj3
365+
366+
assert_equal false, OpenSSL::ASN1::ObjectId.new("authorityKeyIdentifier") == nil
360367
end
361368

362369
def test_sequence

0 commit comments

Comments
 (0)