Skip to content

Commit ab4f6d8

Browse files
samiponkanensamiponkanenssh
authored andcommitted
ssh: unit tests for rsa-sha2-256 and rsa-sha2-512 signatures
1 parent a0e406f commit ab4f6d8

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

ssh/client_auth_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,17 @@ func tryAuthBothSides(t *testing.T, config *ClientConfig, gssAPIWithMICConfig *G
105105
}
106106

107107
func TestClientAuthPublicKey(t *testing.T) {
108-
config := &ClientConfig{
109-
User: "testuser",
110-
Auth: []AuthMethod{
111-
PublicKeys(testSigners["rsa"]),
112-
},
113-
HostKeyCallback: InsecureIgnoreHostKey(),
114-
}
115-
if err := tryAuth(t, config); err != nil {
116-
t.Fatalf("unable to dial remote side: %s", err)
108+
for _, s := range []string{"rsa", "rsa-sha2-256", "rsa-sha2-512"} {
109+
config := &ClientConfig{
110+
User: "testuser",
111+
Auth: []AuthMethod{
112+
PublicKeys(testSigners[s]),
113+
},
114+
HostKeyCallback: InsecureIgnoreHostKey(),
115+
}
116+
if err := tryAuth(t, config); err != nil {
117+
t.Fatalf("unable to dial remote side: %s", err)
118+
}
117119
}
118120
}
119121

ssh/testdata_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ package ssh
1010

1111
import (
1212
"crypto/rand"
13+
"errors"
1314
"fmt"
15+
"io"
1416

1517
"golang.org/x/crypto/ssh/testdata"
1618
)
@@ -21,6 +23,32 @@ var (
2123
testPublicKeys map[string]PublicKey
2224
)
2325

26+
type testAlgoSigner struct {
27+
signer Signer
28+
algo string
29+
}
30+
31+
func (tas *testAlgoSigner) SignWithAlgorithm(rand io.Reader, data []byte, algorithm string) (*Signature, error) {
32+
if as, ok := tas.signer.(AlgorithmSigner); ok {
33+
if algorithm == "" {
34+
algorithm = tas.algo
35+
}
36+
return as.SignWithAlgorithm(rand, data, algorithm)
37+
}
38+
return nil, errors.New("not an AlgorithmSigner")
39+
}
40+
41+
func (tas *testAlgoSigner) Sign(rand io.Reader, data []byte) (*Signature, error) {
42+
if as, ok := tas.signer.(AlgorithmSigner); ok {
43+
return as.SignWithAlgorithm(rand, data, tas.algo)
44+
}
45+
return nil, errors.New("not an AlgorithmSigner")
46+
}
47+
48+
func (tas *testAlgoSigner) PublicKey() PublicKey {
49+
return tas.signer.PublicKey()
50+
}
51+
2452
func init() {
2553
var err error
2654

@@ -40,6 +68,13 @@ func init() {
4068
testPublicKeys[t] = testSigners[t].PublicKey()
4169
}
4270

71+
// Create rsa-sha2-256 and rsa-sha2-512 signers
72+
for _, t := range []string{"rsa-sha2-256", "rsa-sha2-512"} {
73+
testPrivateKeys[t] = testPrivateKeys["rsa"]
74+
testSigners[t] = &testAlgoSigner{signer: testSigners["rsa"], algo: t}
75+
testPublicKeys[t] = testSigners[t].PublicKey()
76+
}
77+
4378
// Create a cert and sign it for use in tests.
4479
testCert := &Certificate{
4580
Nonce: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil

0 commit comments

Comments
 (0)