3131import net .schmizz .sshj .userauth .keyprovider .FileKeyProvider ;
3232import net .schmizz .sshj .userauth .keyprovider .KeyFormat ;
3333import net .schmizz .sshj .userauth .password .PasswordFinder ;
34- import org .bouncycastle .asn1 .nist .NISTNamedCurves ;
35- import org .bouncycastle .asn1 .x9 .X9ECParameters ;
36- import org .bouncycastle .jce .spec .ECNamedCurveSpec ;
37- import org .bouncycastle .openssl .EncryptionException ;
3834import org .slf4j .Logger ;
3935import org .slf4j .LoggerFactory ;
4036
4743import java .security .KeyPair ;
4844import java .security .PrivateKey ;
4945import java .security .PublicKey ;
50- import java .security .spec .ECPrivateKeySpec ;
5146import java .security .spec .RSAPrivateCrtKeySpec ;
5247import java .util .Arrays ;
5348import java .util .HashMap ;
5449import java .util .Map ;
5550
5651/**
5752 * Reads a key file in the new OpenSSH format.
58- * The format is described in the following document: https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key
53+ * The format is described in the following document: <a href=" https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key">Key Protocol</a>
5954 */
6055public class OpenSSHKeyV1KeyFile extends BaseFileKeyProvider {
6156 private static final String BEGIN = "-----BEGIN " ;
@@ -244,7 +239,7 @@ private PlainBuffer decryptPrivateKey(final byte[] privateKey, final int private
244239 cipher .update (privateKey , 0 , privateKeyLength );
245240 } catch (final SSHRuntimeException e ) {
246241 final String message = String .format ("OpenSSH Private Key decryption failed with cipher [%s]" , cipherName );
247- throw new KeyDecryptionFailedException (new EncryptionException (message , e ));
242+ throw new KeyDecryptionFailedException (new IOException (message , e ));
248243 }
249244 final PlainBuffer decryptedPrivateKey = new PlainBuffer (privateKeyLength );
250245 decryptedPrivateKey .putRawBytes (privateKey , 0 , privateKeyLength );
@@ -343,7 +338,7 @@ private KeyPair readUnencrypted(final PlainBuffer keyBuffer, final PublicKey pub
343338 int checkInt1 = keyBuffer .readUInt32AsInt (); // uint32 checkint1
344339 int checkInt2 = keyBuffer .readUInt32AsInt (); // uint32 checkint2
345340 if (checkInt1 != checkInt2 ) {
346- throw new KeyDecryptionFailedException (new EncryptionException ("OpenSSH Private Key integer comparison failed" ));
341+ throw new KeyDecryptionFailedException (new IOException ("OpenSSH Private Key integer comparison failed" ));
347342 }
348343 // The private key section contains both the public key and the private key
349344 String keyType = keyBuffer .readString (); // string keytype
@@ -365,13 +360,13 @@ private KeyPair readUnencrypted(final PlainBuffer keyBuffer, final PublicKey pub
365360 kp = new KeyPair (publicKey , privateKey );
366361 break ;
367362 case ECDSA256 :
368- kp = new KeyPair (publicKey , createECDSAPrivateKey (kt , keyBuffer , "P-256" ));
363+ kp = new KeyPair (publicKey , createECDSAPrivateKey (kt , keyBuffer , ECDSACurve . SECP256R1 ));
369364 break ;
370365 case ECDSA384 :
371- kp = new KeyPair (publicKey , createECDSAPrivateKey (kt , keyBuffer , "P-384" ));
366+ kp = new KeyPair (publicKey , createECDSAPrivateKey (kt , keyBuffer , ECDSACurve . SECP384R1 ));
372367 break ;
373368 case ECDSA521 :
374- kp = new KeyPair (publicKey , createECDSAPrivateKey (kt , keyBuffer , "P-521" ));
369+ kp = new KeyPair (publicKey , createECDSAPrivateKey (kt , keyBuffer , ECDSACurve . SECP521R1 ));
375370 break ;
376371
377372 default :
@@ -388,13 +383,10 @@ private KeyPair readUnencrypted(final PlainBuffer keyBuffer, final PublicKey pub
388383 return kp ;
389384 }
390385
391- private PrivateKey createECDSAPrivateKey (KeyType kt , PlainBuffer buffer , String name ) throws GeneralSecurityException , Buffer .BufferException {
386+ private PrivateKey createECDSAPrivateKey (KeyType kt , PlainBuffer buffer , ECDSACurve ecdsaCurve ) throws GeneralSecurityException , Buffer .BufferException {
392387 kt .readPubKeyFromBuffer (buffer ); // Public key
393- BigInteger s = new BigInteger (1 , buffer .readBytes ());
394- X9ECParameters ecParams = NISTNamedCurves .getByName (name );
395- ECNamedCurveSpec ecCurveSpec = new ECNamedCurveSpec (name , ecParams .getCurve (), ecParams .getG (), ecParams .getN ());
396- ECPrivateKeySpec pks = new ECPrivateKeySpec (s , ecCurveSpec );
397- return SecurityUtils .getKeyFactory (KeyAlgorithm .ECDSA ).generatePrivate (pks );
388+ final BigInteger s = new BigInteger (1 , buffer .readBytes ());
389+ return ECDSAKeyFactory .getPrivateKey (s , ecdsaCurve );
398390 }
399391
400392 /**
0 commit comments