@@ -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