Skip to content

Commit 1e00c00

Browse files
committed
[refactor] check expected ASN type explicitly
1 parent 2e2b423 commit 1e00c00

File tree

1 file changed

+9
-6
lines changed
  • src/main/java/org/jruby/ext/openssl/impl

1 file changed

+9
-6
lines changed

src/main/java/org/jruby/ext/openssl/impl/PKey.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ public static KeyPair readRSAPrivateKey(final byte[] input)
178178

179179
public static KeyPair readRSAPrivateKey(final KeyFactory rsaFactory, final byte[] input)
180180
throws IOException, InvalidKeySpecException {
181-
ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
182-
if ( seq.size() == 9 ) {
181+
ASN1Sequence seq;
182+
ASN1Primitive obj = new ASN1InputStream(input).readObject();
183+
if (obj instanceof ASN1Sequence && (seq = (ASN1Sequence) obj).size() == 9) {
183184
BigInteger mod = ((ASN1Integer) seq.getObjectAt(1)).getValue();
184185
BigInteger pubexp = ((ASN1Integer) seq.getObjectAt(2)).getValue();
185186
BigInteger privexp = ((ASN1Integer) seq.getObjectAt(3)).getValue();
@@ -203,8 +204,9 @@ public static PublicKey readRSAPublicKey(final byte[] input)
203204

204205
public static PublicKey readRSAPublicKey(final KeyFactory rsaFactory, final byte[] input)
205206
throws IOException, InvalidKeySpecException {
206-
ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
207-
if ( seq.size() == 2 ) {
207+
ASN1Sequence seq;
208+
ASN1Primitive obj = new ASN1InputStream(input).readObject();
209+
if (obj instanceof ASN1Sequence && (seq = (ASN1Sequence) obj).size() == 2) {
208210
BigInteger mod = ((ASN1Integer) seq.getObjectAt(0)).getValue();
209211
BigInteger pubexp = ((ASN1Integer) seq.getObjectAt(1)).getValue();
210212
return rsaFactory.generatePublic(new RSAPublicKeySpec(mod, pubexp));
@@ -220,8 +222,9 @@ public static KeyPair readDSAPrivateKey(final byte[] input)
220222

221223
public static KeyPair readDSAPrivateKey(final KeyFactory dsaFactory, final byte[] input)
222224
throws IOException, InvalidKeySpecException {
223-
ASN1Sequence seq = (ASN1Sequence) new ASN1InputStream(input).readObject();
224-
if ( seq.size() == 6 ) {
225+
ASN1Sequence seq;
226+
ASN1Primitive obj = new ASN1InputStream(input).readObject();
227+
if (obj instanceof ASN1Sequence && (seq = (ASN1Sequence) obj).size() == 6) {
225228
BigInteger p = ((ASN1Integer) seq.getObjectAt(1)).getValue();
226229
BigInteger q = ((ASN1Integer) seq.getObjectAt(2)).getValue();
227230
BigInteger g = ((ASN1Integer) seq.getObjectAt(3)).getValue();

0 commit comments

Comments
 (0)