cert_test.go 2.1 KB

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