amazon-ec2sshj

InvalidKeySpecException while doing ssh with sshj using ppk


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

Solution

  • 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.