ssh: support for marshaling keys using the OpenSSH format
This adds methods to marshal private keys, encrypted and unencrypted to the OpenSSH format. Fixes golang/go#37132 Change-Id: I1a95301f789ce04858e6b147748c6e8b7700384b Reviewed-on: https://go-review.googlesource.com/c/crypto/+/218620 Run-TryBot: Roland Shoemaker <roland@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org> Auto-Submit: Roland Shoemaker <roland@golang.org>
This commit is contained in:
committed by
Gopher Robot
parent
c5370d2cc6
commit
d359caa4a3
@@ -281,6 +281,74 @@ func TestMarshalParsePublicKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalPrivateKey(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
}{
|
||||
{"rsa-openssh-format"},
|
||||
{"ed25519"},
|
||||
{"p256-openssh-format"},
|
||||
{"p384-openssh-format"},
|
||||
{"p521-openssh-format"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
expected, ok := testPrivateKeys[tt.name]
|
||||
if !ok {
|
||||
t.Fatalf("cannot find key %s", tt.name)
|
||||
}
|
||||
|
||||
block, err := MarshalPrivateKey(expected, "test@golang.org")
|
||||
if err != nil {
|
||||
t.Fatalf("cannot marshal %s: %v", tt.name, err)
|
||||
}
|
||||
|
||||
key, err := ParseRawPrivateKey(pem.EncodeToMemory(block))
|
||||
if err != nil {
|
||||
t.Fatalf("cannot parse %s: %v", tt.name, err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expected, key) {
|
||||
t.Errorf("unexpected marshaled key %s", tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalPrivateKeyWithPassphrase(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
}{
|
||||
{"rsa-openssh-format"},
|
||||
{"ed25519"},
|
||||
{"p256-openssh-format"},
|
||||
{"p384-openssh-format"},
|
||||
{"p521-openssh-format"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
expected, ok := testPrivateKeys[tt.name]
|
||||
if !ok {
|
||||
t.Fatalf("cannot find key %s", tt.name)
|
||||
}
|
||||
|
||||
block, err := MarshalPrivateKeyWithPassphrase(expected, "test@golang.org", []byte("test-passphrase"))
|
||||
if err != nil {
|
||||
t.Fatalf("cannot marshal %s: %v", tt.name, err)
|
||||
}
|
||||
|
||||
key, err := ParseRawPrivateKeyWithPassphrase(pem.EncodeToMemory(block), []byte("test-passphrase"))
|
||||
if err != nil {
|
||||
t.Fatalf("cannot parse %s: %v", tt.name, err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expected, key) {
|
||||
t.Errorf("unexpected marshaled key %s", tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type testAuthResult struct {
|
||||
pubKey PublicKey
|
||||
options []string
|
||||
|
||||
Reference in New Issue
Block a user