I generated a SSH RSA keypair with the crypto/ssh
package. However, when I try to use it via a script in Go I'm getting the error:
unable to connect: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain
When I connect to the remote device via CLI, it connects successfully:
ssh -i ~/.ssh/test_key_1 username@172.22.4.1
Could I possibly be using the package incorrectly?
Note: Private key does NOT have a passphrase.
package main
import (
"golang.org/x/crypto/ssh"
"io/ioutil"
"log"
)
func main() {
privateKeyFile := "/Users/username/.ssh/test_key_1"
remoteIP := "172.22.4.1:22"
user := "username"
privateKeyBytes, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
log.Fatal(err)
}
key, err := ssh.ParsePrivateKey(privateKeyBytes)
if err != nil {
log.Fatal(err)
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(key),
},
// using InsecureIgnoreHostKey() for testing purposes
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err := ssh.Dial("tcp", remoteIP, config)
if err != nil {
log.Fatalf("unable to connect: %v", err)
}
defer client.Close()
fmt.Println("Success!")
}
After the long process of component isolation I was finally able to verify why my Key Pairs weren't authenticating. It's due to a custom connivence package I was using that is generating slightly off Public Keys.
I've post on an open issue he had:
https://github.com/ianmcmahon/encoding_ssh/issues/1
In short:
The Public Key that was created using the EncodePublicKey()
function is as below: (truncated for brevity)
ssh-rsa AAAAB3NzaC1yc2EAAAAEAAEAAQAAAgC2u3I/nbN9jcWDV..
However when running ssh-keygen -y -f id_rsa
the below is created:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC2u3I/nbN9jcWDV...
Notice how the bits in bold are slightly different. This causes SSH authentication to not work.