@@ -766,6 +766,15 @@ private static ASN1ObjectIdentifier toObjectID(final String oid, final boolean s
766766 }
767767 }
768768
769+ static ASN1ObjectIdentifier oid (final Ruby runtime , final IRubyObject value ) {
770+ final String oid = value .convertToString ().toString ();
771+ try {
772+ return getObjectID (runtime , oid );
773+ } catch (IllegalArgumentException e ) {
774+ throw newASN1Error (runtime , "invalid OBJECT ID " + oid + ": " + e .getMessage ());
775+ }
776+ }
777+
769778 static String shortName (final Ruby runtime , final ASN1ObjectIdentifier oid ) {
770779 final String name = oid2name (runtime , oid , true );
771780 return name == null ? oid .getId () : name ;
@@ -920,16 +929,16 @@ public static RubyString ln(final ThreadContext context, final IRubyObject self)
920929
921930 @ JRubyMethod
922931 public static RubyString oid (final ThreadContext context , final IRubyObject self ) {
923- final Ruby runtime = context .runtime ;
924- return runtime .newString ( getObjectID (runtime , self .callMethod (context , "value" ).toString ()).getId () );
932+ return context .runtime .newString (ASN1 .oid (context .runtime , self .callMethod (context , "value" )).getId ());
925933 }
926934
927935 @ JRubyMethod (name = "==" )
928936 public static IRubyObject eq (final ThreadContext context , final IRubyObject self , final IRubyObject other ) {
929- if (!other .getMetaClass ().equals (_ASN1 (context .runtime ).getClass ("ObjectId" ))) {
930- return context .runtime .getFalse ();
937+ final Ruby runtime = context .runtime ;
938+ if (!other .getMetaClass ().equals (_ASN1 (runtime ).getClass ("ObjectId" ))) {
939+ return runtime .getFalse ();
931940 }
932- return self . callMethod (context , "value" ).op_eqq (context , other . callMethod (context , "value" ));
941+ return oid (context , self ).op_eqq (context , oid (context , other ));
933942 }
934943
935944 private static RubyString name (final ThreadContext context , IRubyObject value ,
@@ -2063,15 +2072,13 @@ static void initializeImpl(final ThreadContext context,
20632072 final String baseName = self .getMetaClass ().getRealClass ().getBaseName ();
20642073 switch (baseName ) {
20652074 case "ObjectId" :
2066- final String name ;
2067- try {
2068- name = oid2Sym ( runtime , getObjectID (runtime , value .toString ()), true );
2069- }
2070- catch (IllegalArgumentException e ) {
2071- // e.g. in case of nil "string not an OID"
2072- throw newASN1Error (runtime , e .getMessage ());
2075+ if ( value instanceof RubyString ) {
2076+ try {
2077+ final String name = oid2Sym ( runtime , getObjectID (runtime , value .toString ()), true );
2078+ if ( name != null ) value = runtime .newString (name );
2079+ }
2080+ catch (IllegalArgumentException ignored ) {}
20732081 }
2074- if ( name != null ) value = runtime .newString (name );
20752082 break ;
20762083 case "BitString" :
20772084 self .setInstanceVariable ("@unused_bits" , runtime .newFixnum (0 ));
@@ -2155,11 +2162,7 @@ private ASN1Encodable toASN1Primitive(final ThreadContext context) {
21552162
21562163 final IRubyObject val = value (context );
21572164 if ( type == ASN1ObjectIdentifier .class ) {
2158- try {
2159- return getObjectID (context .runtime , val .toString ());
2160- } catch (IllegalArgumentException e ) {
2161- throw newASN1Error (context .runtime , e .getMessage ());
2162- }
2165+ return oid (context .runtime , val );
21632166 }
21642167 if ( type == DERNull .class || type == ASN1Null .class ) {
21652168 return DERNull .INSTANCE ;
0 commit comments