* add sshkey.Generate function that returns an sshkey.Pair to be used with openssh. * add cmd/ssh-keygen/main.go for testing purposes * add a test calling ssh.ParsePrivateKey & ssh.ParseAuthorizedKey (which is very similar to what openssh would do to read a keypair) The wrapping of the keys should be handled by crypto/x509.MarshalPKCS8PrivateKey & x/crypto/ssh.NewPublicKey which does not work for ed25519 and dsa. x509.MarshalPKCS8PrivateKey marshals ed25519 keys but the keys did not work with openssh. x509.MarshalPKCS8PrivateKey does not handle dsa keys. So I had to 'wrap' those manually by reading the code of the openssh package. Note that ssh.NewPublicKey works with any keytype. I should probably do a PR to ssh to have a NewPrivateKey & Marshalling funcs
41 lines
803 B
Go
41 lines
803 B
Go
package sshkey
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
func TestGeneratePair_parseable(t *testing.T) {
|
|
tests := []struct {
|
|
t Algorithm
|
|
}{
|
|
{DSA},
|
|
{RSA},
|
|
{ECDSA},
|
|
{ED25519},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.t.String(), func(t *testing.T) {
|
|
got, err := GeneratePair(tt.t, nil, 0)
|
|
if err != nil {
|
|
t.Errorf("GeneratePair() error = %v", err)
|
|
return
|
|
}
|
|
|
|
privateKey, err := ssh.ParsePrivateKey(got.Private)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
publicKey, _, _, _, err := ssh.ParseAuthorizedKey(got.Public)
|
|
if err != nil {
|
|
t.Fatalf("%v: %s", err, got.Public)
|
|
}
|
|
if diff := cmp.Diff(privateKey.PublicKey().Marshal(), publicKey.Marshal()); diff != "" {
|
|
t.Fatalf("wrong public key: %s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|