testdata_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.
  4. // IMPLEMENTATION NOTE: To avoid a package loop, this file is in three places:
  5. // ssh/, ssh/agent, and ssh/test/. It should be kept in sync across all three
  6. // instances.
  7. package ssh
  8. import (
  9. "crypto/rand"
  10. "fmt"
  11. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh/testdata"
  12. )
  13. var (
  14. testPrivateKeys map[string]interface{}
  15. testSigners map[string]Signer
  16. testPublicKeys map[string]PublicKey
  17. )
  18. func init() {
  19. var err error
  20. n := len(testdata.PEMBytes)
  21. testPrivateKeys = make(map[string]interface{}, n)
  22. testSigners = make(map[string]Signer, n)
  23. testPublicKeys = make(map[string]PublicKey, n)
  24. for t, k := range testdata.PEMBytes {
  25. testPrivateKeys[t], err = ParseRawPrivateKey(k)
  26. if err != nil {
  27. panic(fmt.Sprintf("Unable to parse test key %s: %v", t, err))
  28. }
  29. testSigners[t], err = NewSignerFromKey(testPrivateKeys[t])
  30. if v, ok := testSigners[t].(*rsaSigner); ok {
  31. switch t {
  32. case "rsa-sha2-256":
  33. testSigners[t] = &rsaSigner{v, SigAlgoRSASHA2256}
  34. case "rsa-sha2-512":
  35. testSigners[t] = &rsaSigner{v, SigAlgoRSASHA2512}
  36. }
  37. }
  38. if err != nil {
  39. panic(fmt.Sprintf("Unable to create signer for test key %s: %v", t, err))
  40. }
  41. testPublicKeys[t] = testSigners[t].PublicKey()
  42. }
  43. // Create a cert and sign it for use in tests.
  44. testCert := &Certificate{
  45. Nonce: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
  46. ValidPrincipals: []string{"gopher1", "gopher2"}, // increases test coverage
  47. ValidAfter: 0, // unix epoch
  48. ValidBefore: CertTimeInfinity, // The end of currently representable time.
  49. Reserved: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil
  50. Key: testPublicKeys["ecdsa"],
  51. SignatureKey: testPublicKeys["rsa"],
  52. Permissions: Permissions{
  53. CriticalOptions: map[string]string{},
  54. Extensions: map[string]string{},
  55. },
  56. }
  57. testCert.SignCert(rand.Reader, testSigners["rsa"])
  58. testPrivateKeys["cert"] = testPrivateKeys["ecdsa"]
  59. testSigners["cert"], err = NewCertSigner(testCert, testSigners["ecdsa"])
  60. if err != nil {
  61. panic(fmt.Sprintf("Unable to create certificate signer: %v", err))
  62. }
  63. }