cert_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd
  5. // +build aix darwin dragonfly freebsd linux netbsd openbsd
  6. package test
  7. import (
  8. "bytes"
  9. "crypto/rand"
  10. "testing"
  11. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  12. )
  13. // Test both logging in with a cert, and also that the certificate presented by an OpenSSH host can be validated correctly
  14. func TestCertLogin(t *testing.T) {
  15. s := newServer(t)
  16. defer s.Shutdown()
  17. // Use a key different from the default.
  18. clientKey := testSigners["dsa"]
  19. caAuthKey := testSigners["ecdsa"]
  20. cert := &ssh.Certificate{
  21. Key: clientKey.PublicKey(),
  22. ValidPrincipals: []string{username()},
  23. CertType: ssh.UserCert,
  24. ValidBefore: ssh.CertTimeInfinity,
  25. }
  26. if err := cert.SignCert(rand.Reader, caAuthKey); err != nil {
  27. t.Fatalf("SetSignature: %v", err)
  28. }
  29. certSigner, err := ssh.NewCertSigner(cert, clientKey)
  30. if err != nil {
  31. t.Fatalf("NewCertSigner: %v", err)
  32. }
  33. conf := &ssh.ClientConfig{
  34. User: username(),
  35. HostKeyCallback: (&ssh.CertChecker{
  36. IsHostAuthority: func(pk ssh.PublicKey, addr string) bool {
  37. return bytes.Equal(pk.Marshal(), testPublicKeys["ca"].Marshal())
  38. },
  39. }).CheckHostKey,
  40. }
  41. conf.Auth = append(conf.Auth, ssh.PublicKeys(certSigner))
  42. for _, test := range []struct {
  43. addr string
  44. succeed bool
  45. }{
  46. {addr: "host.example.com:22", succeed: true},
  47. {addr: "host.example.com:10000", succeed: true}, // non-standard port must be OK
  48. {addr: "host.example.com", succeed: false}, // port must be specified
  49. {addr: "host.ex4mple.com:22", succeed: false}, // wrong host
  50. } {
  51. client, err := s.TryDialWithAddr(conf, test.addr)
  52. // Always close client if opened successfully
  53. if err == nil {
  54. client.Close()
  55. }
  56. // Now evaluate whether the test failed or passed
  57. if test.succeed {
  58. if err != nil {
  59. t.Fatalf("TryDialWithAddr: %v", err)
  60. }
  61. } else {
  62. if err == nil {
  63. t.Fatalf("TryDialWithAddr, unexpected success")
  64. }
  65. }
  66. }
  67. }