1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // IMPLEMENTATION NOTE: To avoid a package loop, this file is in three places:
6 // ssh/, ssh/agent, and ssh/test/. It should be kept in sync across all three
15 "golang.org/x/crypto/ssh"
16 "golang.org/x/crypto/ssh/testdata"
20 testPrivateKeys map[string]interface{}
21 testSigners map[string]ssh.Signer
22 testPublicKeys map[string]ssh.PublicKey
28 n := len(testdata.PEMBytes)
29 testPrivateKeys = make(map[string]interface{}, n)
30 testSigners = make(map[string]ssh.Signer, n)
31 testPublicKeys = make(map[string]ssh.PublicKey, n)
32 for t, k := range testdata.PEMBytes {
33 testPrivateKeys[t], err = ssh.ParseRawPrivateKey(k)
35 panic(fmt.Sprintf("Unable to parse test key %s: %v", t, err))
37 testSigners[t], err = ssh.NewSignerFromKey(testPrivateKeys[t])
39 panic(fmt.Sprintf("Unable to create signer for test key %s: %v", t, err))
41 testPublicKeys[t] = testSigners[t].PublicKey()
44 // Create a cert and sign it for use in tests.
45 testCert := &ssh.Certificate{
46 Nonce: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
47 ValidPrincipals: []string{"gopher1", "gopher2"}, // increases test coverage
48 ValidAfter: 0, // unix epoch
49 ValidBefore: ssh.CertTimeInfinity, // The end of currently representable time.
50 Reserved: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
51 Key: testPublicKeys["ecdsa"],
52 SignatureKey: testPublicKeys["rsa"],
53 Permissions: ssh.Permissions{
54 CriticalOptions: map[string]string{},
55 Extensions: map[string]string{},
58 testCert.SignCert(rand.Reader, testSigners["rsa"])
59 testPrivateKeys["cert"] = testPrivateKeys["ecdsa"]
60 testSigners["cert"], err = ssh.NewCertSigner(testCert, testSigners["ecdsa"])
62 panic(fmt.Sprintf("Unable to create certificate signer: %v", err))