From 491f1eb30dc61a978a9deef5627862117e4f125f Mon Sep 17 00:00:00 2001 From: Justin Stoller Date: Tue, 26 Sep 2023 21:13:33 -0700 Subject: [PATCH] Replace methods removed in BC 1.75 Several methods and classes were deprecated in BC 1.70 and many of those in the org.bouncycastle.asn1.* were removed in 1.75. This commit moves away from removed method and classes so BouncyCastle can be updated to 1.76. --- src/main/java/org/jruby/ext/openssl/ASN1.java | 37 +++++++++---------- .../org/jruby/ext/openssl/X509Extension.java | 4 +- .../java/org/jruby/ext/openssl/impl/PKey.java | 1 - 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jruby/ext/openssl/ASN1.java b/src/main/java/org/jruby/ext/openssl/ASN1.java index fbe08bb9..f2bb0cde 100644 --- a/src/main/java/org/jruby/ext/openssl/ASN1.java +++ b/src/main/java/org/jruby/ext/openssl/ASN1.java @@ -1072,23 +1072,22 @@ else if ( obj instanceof DERBMPString ) { return ASN1.getClass("ObjectId").newInstance(context, runtime.newString(objId), Block.NULL_BLOCK); } - if ( obj instanceof ASN1ApplicationSpecific ) { // TODO this will likely break in BC version > 1.71 - final ASN1ApplicationSpecific appSpecific = (ASN1ApplicationSpecific) obj; - IRubyObject tag = runtime.newFixnum( appSpecific.getApplicationTag() ); - IRubyObject tag_class = runtime.newSymbol("APPLICATION"); - final ASN1Sequence sequence = (ASN1Sequence) appSpecific.getObject(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); - } - - if ( obj instanceof ASN1TaggedObject ) { + if (obj instanceof ASN1TaggedObject) { final ASN1TaggedObject taggedObj = (ASN1TaggedObject) obj; - IRubyObject val = decodeObject(context, ASN1, taggedObj.getBaseObject()); - IRubyObject tag = runtime.newFixnum( taggedObj.getTagNo() ); - IRubyObject tag_class = runtime.newSymbol("CONTEXT_SPECIFIC"); - final RubyArray valArr = runtime.newArray(val); - return ASN1.getClass("ASN1Data").newInstance(context, new IRubyObject[] { valArr, tag, tag_class }, Block.NULL_BLOCK); + if (taggedObj.getTagClass() == BERTags.APPLICATION) { + IRubyObject tag = runtime.newFixnum( taggedObj.getTagNo() ); + IRubyObject tag_class = runtime.newSymbol("APPLICATION"); + 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()); + IRubyObject tag = runtime.newFixnum( taggedObj.getTagNo() ); + IRubyObject tag_class = runtime.newSymbol("CONTEXT_SPECIFIC"); + final RubyArray valArr = runtime.newArray(val); + return ASN1.getClass("ASN1Data").newInstance(context, new IRubyObject[] { valArr, tag, tag_class }, Block.NULL_BLOCK); + } } if ( obj instanceof ASN1Sequence ) { @@ -1696,13 +1695,13 @@ ASN1Encodable toASN1(final ThreadContext context) { } if ( type == DERGeneralString.class ) { - return DERGeneralString.getInstance( val.asString().getBytes() ); + return new DERGeneralString( val.asString().toString() ); } if ( type == DERVisibleString.class ) { - return DERVisibleString.getInstance( val.asString().getBytes() ); + return new DERVisibleString( val.asString().toString() ); } if ( type == DERNumericString.class ) { - return DERNumericString.getInstance( val.asString().getBytes() ); + return new DERNumericString( val.asString().toString() ); } if ( val instanceof RubyString ) { diff --git a/src/main/java/org/jruby/ext/openssl/X509Extension.java b/src/main/java/org/jruby/ext/openssl/X509Extension.java index c199d05b..b50f0ca5 100644 --- a/src/main/java/org/jruby/ext/openssl/X509Extension.java +++ b/src/main/java/org/jruby/ext/openssl/X509Extension.java @@ -38,6 +38,7 @@ import org.bouncycastle.asn1.ASN1EncodableVector; import org.bouncycastle.asn1.ASN1Encoding; import org.bouncycastle.asn1.ASN1Integer; +import org.bouncycastle.asn1.ASN1IA5String; import org.bouncycastle.asn1.ASN1Object; import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.asn1.ASN1OctetString; @@ -46,7 +47,6 @@ import org.bouncycastle.asn1.ASN1String; import org.bouncycastle.asn1.ASN1TaggedObject; import org.bouncycastle.asn1.BERTags; -import org.bouncycastle.asn1.DERIA5String; import org.bouncycastle.asn1.DEROctetString; import org.bouncycastle.asn1.DERUniversalString; import org.bouncycastle.asn1.DLSequence; @@ -620,7 +620,7 @@ private static boolean formatGeneralName(final GeneralName name, final ByteList case GeneralName.uniformResourceIdentifier: if ( ! tagged ) out.append('U').append('R').append('I'). append(':'); - val = DERIA5String.getInstance(obj).getString(); + val = ASN1IA5String.getInstance(obj).getString(); out.append( ByteList.plain(val) ); break; case GeneralName.directoryName: diff --git a/src/main/java/org/jruby/ext/openssl/impl/PKey.java b/src/main/java/org/jruby/ext/openssl/impl/PKey.java index d54d5817..d2534ade 100644 --- a/src/main/java/org/jruby/ext/openssl/impl/PKey.java +++ b/src/main/java/org/jruby/ext/openssl/impl/PKey.java @@ -67,7 +67,6 @@ import org.bouncycastle.asn1.DLSequence; import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; -import org.bouncycastle.asn1.sec.ECPrivateKeyStructure; import org.bouncycastle.asn1.x509.AlgorithmIdentifier; import org.bouncycastle.asn1.x509.DSAParameter; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;