Skip to content

Commit 9c1aa6a

Browse files
drakkangopherbot
authored andcommitted
ssh/test: reset the random source before capturing a recording
If a recording file exists but is invalid for any reason, the random source may have already been used, resulting in a recording that cannot be replayed. Change-Id: Ib81aaf163f5783fede2c14a0ac10a8d2af6019c6 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/664917 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Filippo Valsorda <filippo@golang.org> Auto-Submit: Nicola Murino <nicola.murino@gmail.com>
1 parent 8819902 commit 9c1aa6a

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

ssh/test/recording_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"time"
2222

2323
"golang.org/x/crypto/internal/testenv"
24-
"golang.org/x/crypto/sha3"
2524
"golang.org/x/crypto/ssh"
2625
"golang.org/x/crypto/ssh/testdata"
2726
)
@@ -169,6 +168,8 @@ func (test *clientTest) run(t *testing.T, write bool) {
169168
var clientConn net.Conn
170169
var recordingConn *recordingConn
171170

171+
setDeterministicRandomSource(&test.config.Config)
172+
172173
if write {
173174
// We store the username used when we record the connection so we can
174175
// reuse the same username when running tests.
@@ -238,7 +239,6 @@ func recordingsClientConfig() *ssh.ClientConfig {
238239
if config.KeyExchanges[0] == "mlkem768x25519-sha256" {
239240
config.KeyExchanges = config.KeyExchanges[1:]
240241
}
241-
config.Rand = sha3.NewShake128()
242242
config.Auth = []ssh.AuthMethod{
243243
ssh.PublicKeys(testSigners["rsa"]),
244244
}

ssh/test/recording_server_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"time"
1919

2020
"golang.org/x/crypto/internal/testenv"
21-
"golang.org/x/crypto/sha3"
2221
"golang.org/x/crypto/ssh"
2322
"golang.org/x/crypto/ssh/testdata"
2423
)
@@ -132,6 +131,8 @@ func (test *serverTest) run(t *testing.T, write bool) {
132131
var serverConn net.Conn
133132
var recordingConn *recordingConn
134133

134+
setDeterministicRandomSource(&test.config.Config)
135+
135136
if write {
136137
var err error
137138
recordingConn, err = test.connFromCommand(t)
@@ -211,9 +212,6 @@ func (test *serverTest) run(t *testing.T, write bool) {
211212

212213
func recordingsServerConfig() *ssh.ServerConfig {
213214
config := &ssh.ServerConfig{
214-
Config: ssh.Config{
215-
Rand: sha3.NewShake128(),
216-
},
217215
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
218216
return nil, nil
219217
},

ssh/test/recording_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"text/template"
2424
"time"
2525

26+
"golang.org/x/crypto/sha3"
2627
"golang.org/x/crypto/ssh"
2728
)
2829

@@ -412,6 +413,15 @@ func writeFile(path string, contents []byte) {
412413
}
413414
}
414415

416+
// setDeterministicRandomSource sets a deterministic random source for the
417+
// provided ssh.Config. It is intended solely for use in test cases, as
418+
// deterministic randomness is insecure and should never be used in production
419+
// environments. A deterministic random source is required to enable consistent
420+
// testing against recorded session files.
421+
func setDeterministicRandomSource(config *ssh.Config) {
422+
config.Rand = sha3.NewShake128()
423+
}
424+
415425
func TestMain(m *testing.M) {
416426
flag.Usage = func() {
417427
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args)

0 commit comments

Comments
 (0)