Reputation: 19312
I am trying to parse a public key using go
's ssh
lib as follows:
var testPublicKey = `ssh-rsa AAAAB3NzaC1yc...DEdfU= pkaramol@MacBookPro`
pubKey, err := ssh.ParsePublicKey(([]byte(testPublicKey)))
if err != nil {
log.Fatal(err)
}
But I am getting the following error:
ssh: short read
I am aware of this question but it addresses a different issue (reading a public key from a file and loading to authorized keys list)
My goal is to end up with an *rsa.PublicKey
type to be used in this function.
Upvotes: 2
Views: 1178
Reputation: 4204
There's a difference between the wire and disk format for the key. From the doc:
ParsePublicKey parses an SSH public key formatted for use in the SSH wire protocol according to RFC 4253
So you need to marshal it to the wire format.
package main
import (
"log"
"golang.org/x/crypto/ssh"
)
func parse(in []byte) error {
// ParseAuthorizedKeys parses a public key from an authorized_keys file used in OpenSSH
pk, _, _, _, err := ssh.ParseAuthorizedKey(in)
if err != nil {
return err
}
// pk.Marshal() Marshal returns the serialized key data in SSH wire format, with the name prefix
// ssh.ParsePublicKey is used to unmarshal the returned data
res, err := ssh.ParsePublicKey(pk.Marshal())
if err != nil {
return err
}
log.Println(res)
return nil
}
func main() {
key := "ssh-rsa AAAAB3NzaC1yc...DEdfU= pkaramol@MacBookPro"
if err := parse([]byte(key)); err != nil {
log.Println(err)
}
}
Upvotes: 3