88
88
public class PKey {
89
89
90
90
public static KeyPair readPrivateKey (final byte [] input , final String type )
91
+ throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
92
+ return readPrivateKey ((ASN1Sequence ) new ASN1InputStream (input ).readObject (), type );
93
+ }
94
+
95
+ public static KeyPair readPrivateKey (final ASN1Sequence seq , final String type )
91
96
throws IOException , NoSuchAlgorithmException , InvalidKeySpecException {
92
97
KeySpec pubSpec ; KeySpec privSpec ;
93
- ASN1Sequence seq = (ASN1Sequence ) new ASN1InputStream (input ).readObject ();
94
98
if ( type .equals ("RSA" ) ) {
95
99
ASN1Integer mod = (ASN1Integer ) seq .getObjectAt (1 );
96
100
ASN1Integer pubExp = (ASN1Integer ) seq .getObjectAt (2 );
@@ -114,7 +118,7 @@ else if ( type.equals("DSA") ) {
114
118
pubSpec = new DSAPublicKeySpec (y .getValue (), p .getValue (), q .getValue (), g .getValue ());
115
119
}
116
120
else if ( type .equals ("EC" ) ) {
117
- return readECPrivateKey (input );
121
+ return readECPrivateKey (SecurityHelper . getKeyFactory ( "EC" ), seq );
118
122
}
119
123
else {
120
124
throw new IllegalStateException ("unsupported type: " + type );
@@ -123,29 +127,6 @@ else if ( type.equals("EC") ) {
123
127
return new KeyPair (fact .generatePublic (pubSpec ), fact .generatePrivate (privSpec ));
124
128
}
125
129
126
- // d2i_PrivateKey_bio
127
- public static KeyPair readPrivateKey (byte [] input ) throws IOException ,
128
- NoSuchAlgorithmException , InvalidKeySpecException {
129
- KeyPair key = null ;
130
- try {
131
- key = readRSAPrivateKey (input );
132
- }
133
- catch (NoSuchAlgorithmException e ) { throw e ; /* should not happen */ }
134
- catch (InvalidKeySpecException e ) {
135
- // ignore
136
- }
137
- if (key == null ) {
138
- try {
139
- key = readDSAPrivateKey (input );
140
- }
141
- catch (NoSuchAlgorithmException e ) { throw e ; /* should not happen */ }
142
- catch (InvalidKeySpecException e ) {
143
- // ignore
144
- }
145
- }
146
- return key ;
147
- }
148
-
149
130
// d2i_PUBKEY_bio
150
131
public static PublicKey readPublicKey (byte [] input ) throws IOException ,
151
132
NoSuchAlgorithmException , InvalidKeySpecException {
@@ -281,22 +262,26 @@ public static KeyPair readECPrivateKey(final byte[] input)
281
262
return readECPrivateKey (SecurityHelper .getKeyFactory ("EC" ), input );
282
263
}
283
264
284
- public static KeyPair readECPrivateKey (final KeyFactory ecFactory , final byte [] input )
265
+ public static KeyPair readECPrivateKey (final KeyFactory keyFactory , final byte [] input )
266
+ throws IOException , InvalidKeySpecException {
267
+ return readECPrivateKey (keyFactory , (ASN1Sequence ) ASN1Primitive .fromByteArray (input ));
268
+ }
269
+
270
+ public static KeyPair readECPrivateKey (final KeyFactory keyFactory , final ASN1Sequence input )
285
271
throws IOException , InvalidKeySpecException {
286
272
try {
287
- org .bouncycastle .asn1 .sec .ECPrivateKey pKey = org .bouncycastle .asn1 .sec .ECPrivateKey .getInstance (ASN1Primitive . fromByteArray ( input ) );
273
+ org .bouncycastle .asn1 .sec .ECPrivateKey pKey = org .bouncycastle .asn1 .sec .ECPrivateKey .getInstance (input );
288
274
AlgorithmIdentifier algId = new AlgorithmIdentifier (X9ObjectIdentifiers .id_ecPublicKey , pKey .getParametersObject ().toASN1Primitive ());
289
275
PrivateKeyInfo privInfo = new PrivateKeyInfo (algId , pKey .toASN1Primitive ());
290
276
SubjectPublicKeyInfo pubInfo = new SubjectPublicKeyInfo (algId , pKey .getPublicKey ().getBytes ());
291
277
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec (privInfo .getEncoded ());
292
278
X509EncodedKeySpec pubSpec = new X509EncodedKeySpec (pubInfo .getEncoded ());
293
- //KeyFactory fact = KeyFactory.getInstance("EC", provider);
294
279
295
- ECPrivateKey privateKey = (ECPrivateKey ) ecFactory .generatePrivate (privSpec );
280
+ ECPrivateKey privateKey = (ECPrivateKey ) keyFactory .generatePrivate (privSpec );
296
281
if ( algId .getParameters () instanceof ASN1ObjectIdentifier ) {
297
282
privateKey = ECPrivateKeyWithName .wrap (privateKey , (ASN1ObjectIdentifier ) algId .getParameters ());
298
283
}
299
- return new KeyPair (ecFactory .generatePublic (pubSpec ), privateKey );
284
+ return new KeyPair (keyFactory .generatePublic (pubSpec ), privateKey );
300
285
}
301
286
catch (ClassCastException ex ) {
302
287
throw new IOException ("wrong ASN.1 object found in stream" , ex );
0 commit comments