0

I am trying to ssh using sshj java library to an ec2 redhat instance with the ppk file,I am getting InvalidKeySpecException . Where as i am able to successfully ssh to other machines with same ppk. I guess i am missing some configuration in the ec2 instance any help will be appreciated .

Note : I am able to do ssh to the machine in question via putty.

below is the stack trace i am getting in the application:

net.schmizz.sshj.transport.TransportException: java.security.spec.InvalidKeySpecException: key spec not recognised
        at net.schmizz.sshj.transport.TransportException$1.chain(TransportException.java:33)
        at net.schmizz.sshj.transport.TransportException$1.chain(TransportException.java:27)
        at net.schmizz.concurrent.Promise.deliverError(Promise.java:95)
        at net.schmizz.concurrent.Event.deliverError(Event.java:74)
        at net.schmizz.concurrent.ErrorDeliveryUtil.alertEvents(ErrorDeliveryUtil.java:34)
        at net.schmizz.sshj.transport.KeyExchanger.notifyError(KeyExchanger.java:386)
        at net.schmizz.sshj.transport.TransportImpl.die(TransportImpl.java:600)
        at net.schmizz.sshj.transport.Reader.run(Reader.java:67)
Caused by: net.schmizz.sshj.common.SSHException: java.security.spec.InvalidKeySpecException: key spec not recognised
        at net.schmizz.sshj.common.SSHException$1.chain(SSHException.java:36)
        at net.schmizz.sshj.common.SSHException$1.chain(SSHException.java:29)
        at net.schmizz.sshj.transport.TransportImpl.die(TransportImpl.java:595)
        ... 1 common frames omitted
Caused by: net.schmizz.sshj.common.SSHRuntimeException: java.security.spec.InvalidKeySpecException: key spec not recognised
        at net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:466)
        at net.schmizz.sshj.transport.kex.AbstractDHG.next(AbstractDHG.java:66)
        at net.schmizz.sshj.transport.KeyExchanger.handle(KeyExchanger.java:358)
        at net.schmizz.sshj.transport.TransportImpl.handle(TransportImpl.java:503)
        at net.schmizz.sshj.transport.Decoder.decode(Decoder.java:102)
        at net.schmizz.sshj.transport.Decoder.received(Decoder.java:170)
        at net.schmizz.sshj.transport.Reader.run(Reader.java:59)
Caused by: java.security.GeneralSecurityException: java.security.spec.InvalidKeySpecException: key spec not recognised
        at net.schmizz.sshj.common.ECDSAVariationsAdapter.readPubKeyFromBuffer(ECDSAVariationsAdapter.java:92)
        at net.schmizz.sshj.common.KeyType$3.readPubKeyFromBuffer(KeyType.java:113)
        at net.schmizz.sshj.common.Buffer.readPublicKey(Buffer.java:464)
        ... 6 common frames omitted
Caused by: java.security.spec.InvalidKeySpecException: key spec not recognised
        at org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(Unknown Source)
        at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi.engineGeneratePublic(Unknown Source)
        at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
        at net.schmizz.sshj.common.ECDSAVariationsAdapter.readPubKeyFromBuffer(ECDSAVariationsAdapter.java:90)
    ... 8 common frames omitted
sadath pasha
  • 49
  • 11

2 Answers2

0

The ppk file is a Putty private key file, which is not compatible with the OpenSSH formats that are supported by SSHJ. You can convert your ppk file to be used by SSHJ using the following command: puttygen <keyfile>.ppk -O private-openssh -o <outfile>

Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
  • I was trying to follow your suggestion, but it just opens the Putty key generator UI. Is this what is expected? I assume not, but if so what should I do with the dialog since I am not trying to generate a key? – Chris94 Mar 20 '21 at 15:27
0

This one was pretty tricky, the sshj library uses the ssh-rsa algorithm. So we need to add the ssh-rsa algorithm in ec2 instance. steps:

  1. Edit the file /etc/ssh/sshd_config.
  2. Add the entry HostKeyAlgorithms ssh-rsa

it started working after that.

sadath pasha
  • 49
  • 11