Skip to content

Commit ddb4e80

Browse files
committed
ssh: remove custom contains, use slices.Contains
Change-Id: If4784469e7285675bdd51399a76bdc16f0036a2e Reviewed-on: https://go-review.googlesource.com/c/crypto/+/703635 Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Sean Liao <sean@liao.dev> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent f4d47b0 commit ddb4e80

File tree

6 files changed

+27
-31
lines changed

6 files changed

+27
-31
lines changed

ssh/client_auth.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io"
12+
"slices"
1213
"strings"
1314
)
1415

@@ -83,7 +84,7 @@ func (c *connection) clientAuthenticate(config *ClientConfig) error {
8384
// success
8485
return nil
8586
} else if ok == authFailure {
86-
if m := auth.method(); !contains(tried, m) {
87+
if m := auth.method(); !slices.Contains(tried, m) {
8788
tried = append(tried, m)
8889
}
8990
}
@@ -97,7 +98,7 @@ func (c *connection) clientAuthenticate(config *ClientConfig) error {
9798
findNext:
9899
for _, a := range config.Auth {
99100
candidateMethod := a.method()
100-
if contains(tried, candidateMethod) {
101+
if slices.Contains(tried, candidateMethod) {
101102
continue
102103
}
103104
for _, meth := range methods {
@@ -117,15 +118,6 @@ func (c *connection) clientAuthenticate(config *ClientConfig) error {
117118
return fmt.Errorf("ssh: unable to authenticate, attempted methods %v, no supported methods remain", tried)
118119
}
119120

120-
func contains(list []string, e string) bool {
121-
for _, s := range list {
122-
if s == e {
123-
return true
124-
}
125-
}
126-
return false
127-
}
128-
129121
// An AuthMethod represents an instance of an RFC 4252 authentication method.
130122
type AuthMethod interface {
131123
// auth authenticates user over transport t.
@@ -255,7 +247,7 @@ func pickSignatureAlgorithm(signer Signer, extensions map[string][]byte) (MultiA
255247
// Fallback to use if there is no "server-sig-algs" extension or a
256248
// common algorithm cannot be found. We use the public key format if the
257249
// MultiAlgorithmSigner supports it, otherwise we return an error.
258-
if !contains(as.Algorithms(), underlyingAlgo(keyFormat)) {
250+
if !slices.Contains(as.Algorithms(), underlyingAlgo(keyFormat)) {
259251
return "", fmt.Errorf("ssh: no common public key signature algorithm, server only supports %q for key type %q, signer only supports %v",
260252
underlyingAlgo(keyFormat), keyFormat, as.Algorithms())
261253
}
@@ -284,7 +276,7 @@ func pickSignatureAlgorithm(signer Signer, extensions map[string][]byte) (MultiA
284276
// Filter algorithms based on those supported by MultiAlgorithmSigner.
285277
var keyAlgos []string
286278
for _, algo := range algorithmsForKeyFormat(keyFormat) {
287-
if contains(as.Algorithms(), underlyingAlgo(algo)) {
279+
if slices.Contains(as.Algorithms(), underlyingAlgo(algo)) {
288280
keyAlgos = append(keyAlgos, algo)
289281
}
290282
}
@@ -334,7 +326,7 @@ func (cb publicKeyCallback) auth(session []byte, user string, c packetConn, rand
334326
// the key try to use the obtained algorithm as if "server-sig-algs" had
335327
// not been implemented if supported from the algorithm signer.
336328
if !ok && idx < origSignersLen && isRSACert(algo) && algo != CertAlgoRSAv01 {
337-
if contains(as.Algorithms(), KeyAlgoRSA) {
329+
if slices.Contains(as.Algorithms(), KeyAlgoRSA) {
338330
// We retry using the compat algorithm after all signers have
339331
// been tried normally.
340332
signers = append(signers, &multiAlgorithmSigner{
@@ -385,7 +377,7 @@ func (cb publicKeyCallback) auth(session []byte, user string, c packetConn, rand
385377
// contain the "publickey" method, do not attempt to authenticate with any
386378
// other keys. According to RFC 4252 Section 7, the latter can occur when
387379
// additional authentication methods are required.
388-
if success == authSuccess || !contains(methods, cb.method()) {
380+
if success == authSuccess || !slices.Contains(methods, cb.method()) {
389381
return success, methods, err
390382
}
391383
}
@@ -434,7 +426,7 @@ func confirmKeyAck(key PublicKey, c packetConn) (bool, error) {
434426
// servers send the key type instead. OpenSSH allows any algorithm
435427
// that matches the public key, so we do the same.
436428
// https://github.com/openssh/openssh-portable/blob/86bdd385/sshconnect2.c#L709
437-
if !contains(algorithmsForKeyFormat(key.Type()), msg.Algo) {
429+
if !slices.Contains(algorithmsForKeyFormat(key.Type()), msg.Algo) {
438430
return false, nil
439431
}
440432
if !bytes.Equal(msg.PubKey, pubKey) {

ssh/client_auth_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net"
1515
"os"
1616
"runtime"
17+
"slices"
1718
"strings"
1819
"testing"
1920
)
@@ -1214,7 +1215,7 @@ func (cb configurablePublicKeyCallback) auth(session []byte, user string, c pack
12141215
if err != nil {
12151216
return authFailure, nil, err
12161217
}
1217-
if success == authSuccess || !contains(methods, cb.method()) {
1218+
if success == authSuccess || !slices.Contains(methods, cb.method()) {
12181219
return success, methods, err
12191220
}
12201221

ssh/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func keyFormatForAlgorithm(sigAlgo string) string {
345345
// algorithms.
346346
func isRSA(algo string) bool {
347347
algos := algorithmsForKeyFormat(KeyAlgoRSA)
348-
return contains(algos, underlyingAlgo(algo))
348+
return slices.Contains(algos, underlyingAlgo(algo))
349349
}
350350

351351
func isRSACert(algo string) bool {
@@ -544,7 +544,7 @@ func (c *Config) SetDefaults() {
544544
if kexAlgoMap[k] != nil {
545545
// Ignore the KEX if we have no kexAlgoMap definition.
546546
kexs = append(kexs, k)
547-
if k == KeyExchangeCurve25519 && !contains(c.KeyExchanges, keyExchangeCurve25519LibSSH) {
547+
if k == KeyExchangeCurve25519 && !slices.Contains(c.KeyExchanges, keyExchangeCurve25519LibSSH) {
548548
kexs = append(kexs, keyExchangeCurve25519LibSSH)
549549
}
550550
}

ssh/handshake.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"log"
1212
"net"
13+
"slices"
1314
"strings"
1415
"sync"
1516
)
@@ -527,7 +528,7 @@ func (t *handshakeTransport) sendKexInit() error {
527528
switch s := k.(type) {
528529
case MultiAlgorithmSigner:
529530
for _, algo := range algorithmsForKeyFormat(keyFormat) {
530-
if contains(s.Algorithms(), underlyingAlgo(algo)) {
531+
if slices.Contains(s.Algorithms(), underlyingAlgo(algo)) {
531532
msg.ServerHostKeyAlgos = append(msg.ServerHostKeyAlgos, algo)
532533
}
533534
}
@@ -679,7 +680,7 @@ func (t *handshakeTransport) enterKeyExchange(otherInitPacket []byte) error {
679680
return err
680681
}
681682

682-
if t.sessionID == nil && ((isClient && contains(serverInit.KexAlgos, kexStrictServer)) || (!isClient && contains(clientInit.KexAlgos, kexStrictClient))) {
683+
if t.sessionID == nil && ((isClient && slices.Contains(serverInit.KexAlgos, kexStrictServer)) || (!isClient && slices.Contains(clientInit.KexAlgos, kexStrictClient))) {
683684
t.strictMode = true
684685
if err := t.conn.setStrictMode(); err != nil {
685686
return err
@@ -736,7 +737,7 @@ func (t *handshakeTransport) enterKeyExchange(otherInitPacket []byte) error {
736737
// On the server side, after the first SSH_MSG_NEWKEYS, send a SSH_MSG_EXT_INFO
737738
// message with the server-sig-algs extension if the client supports it. See
738739
// RFC 8308, Sections 2.4 and 3.1, and [PROTOCOL], Section 1.9.
739-
if !isClient && firstKeyExchange && contains(clientInit.KexAlgos, "ext-info-c") {
740+
if !isClient && firstKeyExchange && slices.Contains(clientInit.KexAlgos, "ext-info-c") {
740741
supportedPubKeyAuthAlgosList := strings.Join(t.publicKeyAuthAlgorithms, ",")
741742
extInfo := &extInfoMsg{
742743
NumExtensions: 2,
@@ -790,7 +791,7 @@ func (a algorithmSignerWrapper) SignWithAlgorithm(rand io.Reader, data []byte, a
790791
func pickHostKey(hostKeys []Signer, algo string) AlgorithmSigner {
791792
for _, k := range hostKeys {
792793
if s, ok := k.(MultiAlgorithmSigner); ok {
793-
if !contains(s.Algorithms(), underlyingAlgo(algo)) {
794+
if !slices.Contains(s.Algorithms(), underlyingAlgo(algo)) {
794795
continue
795796
}
796797
}

ssh/keys.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"fmt"
2828
"io"
2929
"math/big"
30+
"slices"
3031
"strings"
3132

3233
"golang.org/x/crypto/ssh/internal/bcrypt_pbkdf"
@@ -409,11 +410,11 @@ func NewSignerWithAlgorithms(signer AlgorithmSigner, algorithms []string) (Multi
409410
}
410411

411412
for _, algo := range algorithms {
412-
if !contains(supportedAlgos, algo) {
413+
if !slices.Contains(supportedAlgos, algo) {
413414
return nil, fmt.Errorf("ssh: algorithm %q is not supported for key type %q",
414415
algo, signer.PublicKey().Type())
415416
}
416-
if !contains(signerAlgos, algo) {
417+
if !slices.Contains(signerAlgos, algo) {
417418
return nil, fmt.Errorf("ssh: algorithm %q is restricted for the provided signer", algo)
418419
}
419420
}
@@ -500,7 +501,7 @@ func (r *rsaPublicKey) Marshal() []byte {
500501

501502
func (r *rsaPublicKey) Verify(data []byte, sig *Signature) error {
502503
supportedAlgos := algorithmsForKeyFormat(r.Type())
503-
if !contains(supportedAlgos, sig.Format) {
504+
if !slices.Contains(supportedAlgos, sig.Format) {
504505
return fmt.Errorf("ssh: signature type %s for key type %s", sig.Format, r.Type())
505506
}
506507
hash := hashFuncs[sig.Format]
@@ -1126,7 +1127,7 @@ func (s *wrappedSigner) SignWithAlgorithm(rand io.Reader, data []byte, algorithm
11261127
algorithm = s.pubKey.Type()
11271128
}
11281129

1129-
if !contains(s.Algorithms(), algorithm) {
1130+
if !slices.Contains(s.Algorithms(), algorithm) {
11301131
return nil, fmt.Errorf("ssh: unsupported signature algorithm %q for key format %q", algorithm, s.pubKey.Type())
11311132
}
11321133

ssh/server.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"net"
13+
"slices"
1314
"strings"
1415
)
1516

@@ -246,7 +247,7 @@ func NewServerConn(c net.Conn, config *ServerConfig) (*ServerConn, <-chan NewCha
246247
fullConf.PublicKeyAuthAlgorithms = defaultPubKeyAuthAlgos
247248
} else {
248249
for _, algo := range fullConf.PublicKeyAuthAlgorithms {
249-
if !contains(SupportedAlgorithms().PublicKeyAuths, algo) && !contains(InsecureAlgorithms().PublicKeyAuths, algo) {
250+
if !slices.Contains(SupportedAlgorithms().PublicKeyAuths, algo) && !slices.Contains(InsecureAlgorithms().PublicKeyAuths, algo) {
250251
c.Close()
251252
return nil, nil, nil, fmt.Errorf("ssh: unsupported public key authentication algorithm %s", algo)
252253
}
@@ -631,7 +632,7 @@ userAuthLoop:
631632
return nil, parseError(msgUserAuthRequest)
632633
}
633634
algo := string(algoBytes)
634-
if !contains(config.PublicKeyAuthAlgorithms, underlyingAlgo(algo)) {
635+
if !slices.Contains(config.PublicKeyAuthAlgorithms, underlyingAlgo(algo)) {
635636
authErr = fmt.Errorf("ssh: algorithm %q not accepted", algo)
636637
break
637638
}
@@ -695,7 +696,7 @@ userAuthLoop:
695696
// ssh-rsa-cert-v01@openssh.com algorithm with ssh-rsa public
696697
// key type. The algorithm and public key type must be
697698
// consistent: both must be certificate algorithms, or neither.
698-
if !contains(algorithmsForKeyFormat(pubKey.Type()), algo) {
699+
if !slices.Contains(algorithmsForKeyFormat(pubKey.Type()), algo) {
699700
authErr = fmt.Errorf("ssh: public key type %q not compatible with selected algorithm %q",
700701
pubKey.Type(), algo)
701702
break
@@ -705,7 +706,7 @@ userAuthLoop:
705706
// algorithm name that corresponds to algo with
706707
// sig.Format. This is usually the same, but
707708
// for certs, the names differ.
708-
if !contains(config.PublicKeyAuthAlgorithms, sig.Format) {
709+
if !slices.Contains(config.PublicKeyAuthAlgorithms, sig.Format) {
709710
authErr = fmt.Errorf("ssh: algorithm %q not accepted", sig.Format)
710711
break
711712
}

0 commit comments

Comments
 (0)