Skip to content

Commit ded2006

Browse files
samiponkanensamiponkanenssh
authored andcommitted
ssh: fixed TestClientAuthPublicKey() to use correct public key algorithm name with rsa-sha2-* signatures
1 parent 276d6d3 commit ded2006

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

ssh/client_auth_test.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,60 @@ func tryAuthBothSides(t *testing.T, config *ClientConfig, gssAPIWithMICConfig *G
104104
return err, serverAuthErrors
105105
}
106106

107+
type testAlgoSigner struct {
108+
signer Signer
109+
algo string
110+
}
111+
112+
func (tas *testAlgoSigner) SignWithAlgorithm(rand io.Reader, data []byte, algorithm string) (*Signature, error) {
113+
if as, ok := tas.signer.(AlgorithmSigner); ok {
114+
if algorithm == "" {
115+
algorithm = tas.algo
116+
}
117+
return as.SignWithAlgorithm(rand, data, algorithm)
118+
}
119+
return nil, errors.New("not an AlgorithmSigner")
120+
}
121+
122+
func (tas *testAlgoSigner) Sign(rand io.Reader, data []byte) (*Signature, error) {
123+
if as, ok := tas.signer.(AlgorithmSigner); ok {
124+
return as.SignWithAlgorithm(rand, data, tas.algo)
125+
}
126+
return nil, errors.New("not an AlgorithmSigner")
127+
}
128+
129+
func (tas *testAlgoSigner) PublicKey() PublicKey {
130+
return &testAlgoSignerPublickey{
131+
publickey: tas.signer.PublicKey(),
132+
algo: tas.algo,
133+
}
134+
}
135+
136+
type testAlgoSignerPublickey struct {
137+
publickey PublicKey
138+
algo string
139+
}
140+
141+
func (tp *testAlgoSignerPublickey) Type() string {
142+
return tp.algo
143+
}
144+
145+
func (tp *testAlgoSignerPublickey) Marshal() []byte {
146+
return tp.publickey.Marshal()
147+
}
148+
149+
func (tp *testAlgoSignerPublickey) Verify(data []byte, sig *Signature) error {
150+
return tp.publickey.Verify(data, sig)
151+
}
152+
107153
func TestClientAuthPublicKey(t *testing.T) {
108-
for _, s := range []string{"rsa", "rsa-sha2-256", "rsa-sha2-512"} {
154+
for _, s := range []Signer{testSigners["rsa"],
155+
&testAlgoSigner{signer: testSigners["rsa"], algo: "rsa-sha2-256"},
156+
&testAlgoSigner{signer: testSigners["rsa"], algo: "rsa-sha2-512"}} {
109157
config := &ClientConfig{
110158
User: "testuser",
111159
Auth: []AuthMethod{
112-
PublicKeys(testSigners[s]),
160+
PublicKeys(s),
113161
},
114162
HostKeyCallback: InsecureIgnoreHostKey(),
115163
}

ssh/testdata_test.go

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

1111
import (
1212
"crypto/rand"
13-
"errors"
1413
"fmt"
15-
"io"
1614

1715
"golang.org/x/crypto/ssh/testdata"
1816
)
@@ -23,32 +21,6 @@ var (
2321
testPublicKeys map[string]PublicKey
2422
)
2523

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-
5224
func init() {
5325
var err error
5426

@@ -68,13 +40,6 @@ func init() {
6840
testPublicKeys[t] = testSigners[t].PublicKey()
6941
}
7042

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-
7843
// Create a cert and sign it for use in tests.
7944
testCert := &Certificate{
8045
Nonce: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil

0 commit comments

Comments
 (0)