I have signed my public key using Hashicorp's Vault, and saved the generated certificiate in ~/.ssh/id_rsa-cert.pub
It works just fine, I can do ssh -i ~/.ssh/id_rsa-cert.pub user@hostname
and it lets me in.
My problems start when I try to implement this in Go.
This is my code:
package main
import (
"encoding/base64"
"fmt"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"net"
"os"
)
func main() {
pubKey := "AAAAB3NzaC1yc2EAAAADAQABA..."
signedKey := "AAAAHHNzaC1yc2EtY2VydC..."
pubKeyBytes, err := base64.StdEncoding.DecodeString(pubKey)
if err != nil {
panic(err)
}
pk, err := ssh.ParsePublicKey(pubKeyBytes)
if err != nil {
panic(err)
}
fmt.Printf("%T
", pk)
signedKeyBytes, err := base64.StdEncoding.DecodeString(signedKey)
if err != nil {
panic(err)
}
fmt.Printf("%T
", pk)
sk, err := ssh.ParsePublicKey(signedKeyBytes)
if err != nil {
panic(err)
}
fmt.Printf("%T
", sk)
conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
panic(err)
}
sshAgent := agent.NewClient(conn)
signers, err := sshAgent.Signers()
if err != nil {
panic(err)
}
c := &ssh.Certificate{
Key: pk,
SignatureKey: sk,
}
signer, err := ssh.NewCertSigner(c, signers[0])
if err != nil {
panic(err)
}
auths := []ssh.AuthMethod{ssh.PublicKeys(signer)}
sshClient, err := ssh.Dial("tcp", "10.0.0.150:22", &ssh.ClientConfig{
User: "user1",
/*The signed key is signed against user2's public key, and should allow him to log in.
It works via command line; user2@localhost: ssh -i id_rsa-cert.pub user1@10.0.150*/
Auth: auths,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
fmt.Println(sshClient, err) /*This does not work*/
sshClient2, err := ssh.Dial("tcp", "10.0.0.150:22", &ssh.ClientConfig{
User: "user2",
/*User2 is allowed to connect with his basic keypair
user2@localhost: ssh user2@10.0.0.150*/
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(sshAgent.Signers),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
fmt.Println(sshClient2, err) /*This works fine*/
}
The errors I get are:
ssh: handshake failed: ssh: unable to authenticate, attempted methods [publickey none],
no supported methods remain
And the sshd logs from the server in question:
sshd[7149]: error: Unknown certificate type 0 sshd[7149]: error:
key_from_blob: can't parse cert data sshd[7149]: error:
userauth_pubkey: cannot decode key: ssh-rsa-cert-v01@openssh.com
What do I need to do here to get this to work?