ssh_gss_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package ssh
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestParseGSSAPIPayload(t *testing.T) {
  7. payload := []byte{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x06, 0x09,
  8. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x01, 0x02, 0x02}
  9. res, err := parseGSSAPIPayload(payload)
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. if ok := res.OIDS[0].Equal(krb5Mesh); !ok {
  14. t.Fatalf("got %v, want %v", res, krb5Mesh)
  15. }
  16. }
  17. func TestBuildMIC(t *testing.T) {
  18. sessionID := []byte{134, 180, 134, 194, 62, 145, 171, 82, 119, 149, 254, 196, 125, 173, 177, 145, 187, 85, 53,
  19. 183, 44, 150, 219, 129, 166, 195, 19, 33, 209, 246, 175, 121}
  20. username := "testuser"
  21. service := "ssh-connection"
  22. authMethod := "gssapi-with-mic"
  23. expected := []byte{0, 0, 0, 32, 134, 180, 134, 194, 62, 145, 171, 82, 119, 149, 254, 196, 125, 173, 177, 145, 187, 85, 53, 183, 44, 150, 219, 129, 166, 195, 19, 33, 209, 246, 175, 121, 50, 0, 0, 0, 8, 116, 101, 115, 116, 117, 115, 101, 114, 0, 0, 0, 14, 115, 115, 104, 45, 99, 111, 110, 110, 101, 99, 116, 105, 111, 110, 0, 0, 0, 15, 103, 115, 115, 97, 112, 105, 45, 119, 105, 116, 104, 45, 109, 105, 99}
  24. result := buildMIC(string(sessionID), username, service, authMethod)
  25. if string(result) != string(expected) {
  26. t.Fatalf("buildMic: got %v, want %v", result, expected)
  27. }
  28. }
  29. type exchange struct {
  30. outToken string
  31. expectedToken string
  32. }
  33. type FakeClient struct {
  34. exchanges []*exchange
  35. round int
  36. mic []byte
  37. maxRound int
  38. }
  39. func (f *FakeClient) InitSecContext(target string, token []byte, isGSSDelegCreds bool) (outputToken []byte, needContinue bool, err error) {
  40. if token == nil {
  41. if f.exchanges[f.round].expectedToken != "" {
  42. err = fmt.Errorf("got empty token, want %q", f.exchanges[f.round].expectedToken)
  43. } else {
  44. outputToken = []byte(f.exchanges[f.round].outToken)
  45. }
  46. } else {
  47. if string(token) != string(f.exchanges[f.round].expectedToken) {
  48. err = fmt.Errorf("got %q, want token %q", token, f.exchanges[f.round].expectedToken)
  49. } else {
  50. outputToken = []byte(f.exchanges[f.round].outToken)
  51. }
  52. }
  53. f.round++
  54. needContinue = f.round < f.maxRound
  55. return
  56. }
  57. func (f *FakeClient) GetMIC(micField []byte) ([]byte, error) {
  58. return f.mic, nil
  59. }
  60. func (f *FakeClient) DeleteSecContext() error {
  61. return nil
  62. }
  63. type FakeServer struct {
  64. exchanges []*exchange
  65. round int
  66. expectedMIC []byte
  67. srcName string
  68. maxRound int
  69. }
  70. func (f *FakeServer) AcceptSecContext(token []byte) (outputToken []byte, srcName string, needContinue bool, err error) {
  71. if token == nil {
  72. if f.exchanges[f.round].expectedToken != "" {
  73. err = fmt.Errorf("got empty token, want %q", f.exchanges[f.round].expectedToken)
  74. } else {
  75. outputToken = []byte(f.exchanges[f.round].outToken)
  76. }
  77. } else {
  78. if string(token) != string(f.exchanges[f.round].expectedToken) {
  79. err = fmt.Errorf("got %q, want token %q", token, f.exchanges[f.round].expectedToken)
  80. } else {
  81. outputToken = []byte(f.exchanges[f.round].outToken)
  82. }
  83. }
  84. f.round++
  85. needContinue = f.round < f.maxRound
  86. srcName = f.srcName
  87. return
  88. }
  89. func (f *FakeServer) VerifyMIC(micField []byte, micToken []byte) error {
  90. if string(micToken) != string(f.expectedMIC) {
  91. return fmt.Errorf("got MICToken %q, want %q", micToken, f.expectedMIC)
  92. }
  93. return nil
  94. }
  95. func (f *FakeServer) DeleteSecContext() error {
  96. return nil
  97. }