@@ -88,6 +88,7 @@ public static KeyPair readPrivateKey(final Type type, final byte[] input)
88
88
}
89
89
90
90
private static PrivateKeyInfo mockPrivateKeyInfo (final Type type , final byte [] input ) throws IOException {
91
+ assert type != null ;
91
92
return new PrivateKeyInfo (null , new ASN1InputStream (input ).readObject ());
92
93
}
93
94
@@ -273,20 +274,21 @@ public static KeyPair readECPrivateKey(final KeyFactory keyFactory, final byte[]
273
274
public static KeyPair readECPrivateKey (final KeyFactory keyFactory , final PrivateKeyInfo keyInfo )
274
275
throws IOException , InvalidKeySpecException {
275
276
try {
276
- org .bouncycastle .asn1 .sec .ECPrivateKey key = org .bouncycastle .asn1 .sec .ECPrivateKey .getInstance (keyInfo .parsePrivateKey ());
277
- AlgorithmIdentifier algId = keyInfo .getPrivateKeyAlgorithm ();
278
- // NOTE: should only happen when using mockPrivateKeyInfo(Type, byte[])
279
- if (algId == null ) algId = new AlgorithmIdentifier (X9ObjectIdentifiers .id_ecPublicKey );
277
+ ASN1Sequence seq = ASN1Sequence .getInstance (keyInfo .parsePrivateKey ());
280
278
281
- SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo (algId , key .getPublicKey ().getBytes ());
282
- PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec (keyInfo .getEncoded ());
283
- X509EncodedKeySpec pubSpec = new X509EncodedKeySpec (pubInfo .getEncoded ());
279
+ org .bouncycastle .asn1 .sec .ECPrivateKey key = org .bouncycastle .asn1 .sec .ECPrivateKey .getInstance (seq );
280
+ AlgorithmIdentifier algId = keyInfo .getPrivateKeyAlgorithm ();
281
+ if (algId == null ) { // mockPrivateKeyInfo
282
+ algId = new AlgorithmIdentifier (X9ObjectIdentifiers .id_ecPublicKey , key .getParameters ());
283
+ }
284
+ final SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo (algId , key .getPublicKey ().getBytes ());
285
+ final PrivateKeyInfo privInfo = new PrivateKeyInfo (algId , key );
284
286
285
- ECPrivateKey privateKey = (ECPrivateKey ) keyFactory .generatePrivate (privSpec );
287
+ ECPrivateKey privateKey = (ECPrivateKey ) keyFactory .generatePrivate (new PKCS8EncodedKeySpec ( privInfo . getEncoded ()) );
286
288
if (algId .getParameters () instanceof ASN1ObjectIdentifier ) {
287
289
privateKey = ECPrivateKeyWithName .wrap (privateKey , (ASN1ObjectIdentifier ) algId .getParameters ());
288
290
}
289
- return new KeyPair (keyFactory .generatePublic (pubSpec ), privateKey );
291
+ return new KeyPair (keyFactory .generatePublic (new X509EncodedKeySpec ( pubInfo . getEncoded ()) ), privateKey );
290
292
}
291
293
catch (ClassCastException ex ) {
292
294
throw new IOException ("wrong ASN.1 object found in stream" , ex );
0 commit comments