knownhosts_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright 2017 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. package knownhosts
  5. import (
  6. "bytes"
  7. "fmt"
  8. "net"
  9. "reflect"
  10. "testing"
  11. "golang.org/x/crypto/ssh"
  12. )
  13. const edKeyStr = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGBAarftlLeoyf+v+nVchEZII/vna2PCV8FaX4vsF5BX"
  14. const alternateEdKeyStr = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIXffBYeYL+WVzVru8npl5JHt2cjlr4ornFTWzoij9sx"
  15. const ecKeyStr = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNLCu01+wpXe3xB5olXCN4SqU2rQu0qjSRKJO4Bg+JRCPU+ENcgdA5srTU8xYDz/GEa4dzK5ldPw4J/gZgSXCMs="
  16. var ecKey, alternateEdKey, edKey ssh.PublicKey
  17. var testAddr = &net.TCPAddr{
  18. IP: net.IP{198, 41, 30, 196},
  19. Port: 22,
  20. }
  21. var testAddr6 = &net.TCPAddr{
  22. IP: net.IP{198, 41, 30, 196,
  23. 1, 2, 3, 4,
  24. 1, 2, 3, 4,
  25. 1, 2, 3, 4,
  26. },
  27. Port: 22,
  28. }
  29. func init() {
  30. var err error
  31. ecKey, _, _, _, err = ssh.ParseAuthorizedKey([]byte(ecKeyStr))
  32. if err != nil {
  33. panic(err)
  34. }
  35. edKey, _, _, _, err = ssh.ParseAuthorizedKey([]byte(edKeyStr))
  36. if err != nil {
  37. panic(err)
  38. }
  39. alternateEdKey, _, _, _, err = ssh.ParseAuthorizedKey([]byte(alternateEdKeyStr))
  40. if err != nil {
  41. panic(err)
  42. }
  43. }
  44. func testDB(t *testing.T, s string) *hostKeyDB {
  45. db := newHostKeyDB()
  46. if err := db.Read(bytes.NewBufferString(s), "testdb"); err != nil {
  47. t.Fatalf("Read: %v", err)
  48. }
  49. return db
  50. }
  51. func TestRevoked(t *testing.T) {
  52. db := testDB(t, "\n\n@revoked * "+edKeyStr+"\n")
  53. want := &RevokedError{
  54. Revoked: KnownKey{
  55. Key: edKey,
  56. Filename: "testdb",
  57. Line: 3,
  58. },
  59. }
  60. if err := db.check("", &net.TCPAddr{
  61. Port: 42,
  62. }, edKey); err == nil {
  63. t.Fatal("no error for revoked key")
  64. } else if !reflect.DeepEqual(want, err) {
  65. t.Fatalf("got %#v, want %#v", want, err)
  66. }
  67. }
  68. func TestHostAuthority(t *testing.T) {
  69. for _, m := range []struct {
  70. authorityFor string
  71. address string
  72. good bool
  73. }{
  74. {authorityFor: "localhost", address: "localhost:22", good: true},
  75. {authorityFor: "localhost", address: "localhost", good: false},
  76. {authorityFor: "localhost", address: "localhost:1234", good: false},
  77. {authorityFor: "[localhost]:1234", address: "localhost:1234", good: true},
  78. {authorityFor: "[localhost]:1234", address: "localhost:22", good: false},
  79. {authorityFor: "[localhost]:1234", address: "localhost", good: false},
  80. } {
  81. db := testDB(t, `@cert-authority `+m.authorityFor+` `+edKeyStr)
  82. if ok := db.IsHostAuthority(db.lines[0].knownKey.Key, m.address); ok != m.good {
  83. t.Errorf("IsHostAuthority: authority %s, address %s, wanted good = %v, got good = %v",
  84. m.authorityFor, m.address, m.good, ok)
  85. }
  86. }
  87. }
  88. func TestBracket(t *testing.T) {
  89. db := testDB(t, `[git.eclipse.org]:29418,[198.41.30.196]:29418 `+edKeyStr)
  90. if err := db.check("git.eclipse.org:29418", &net.TCPAddr{
  91. IP: net.IP{198, 41, 30, 196},
  92. Port: 29418,
  93. }, edKey); err != nil {
  94. t.Errorf("got error %v, want none", err)
  95. }
  96. if err := db.check("git.eclipse.org:29419", &net.TCPAddr{
  97. Port: 42,
  98. }, edKey); err == nil {
  99. t.Fatalf("no error for unknown address")
  100. } else if ke, ok := err.(*KeyError); !ok {
  101. t.Fatalf("got type %T, want *KeyError", err)
  102. } else if len(ke.Want) > 0 {
  103. t.Fatalf("got Want %v, want []", ke.Want)
  104. }
  105. }
  106. func TestNewKeyType(t *testing.T) {
  107. str := fmt.Sprintf("%s %s", testAddr, edKeyStr)
  108. db := testDB(t, str)
  109. if err := db.check("", testAddr, ecKey); err == nil {
  110. t.Fatalf("no error for unknown address")
  111. } else if ke, ok := err.(*KeyError); !ok {
  112. t.Fatalf("got type %T, want *KeyError", err)
  113. } else if len(ke.Want) == 0 {
  114. t.Fatalf("got empty KeyError.Want")
  115. }
  116. }
  117. func TestSameKeyType(t *testing.T) {
  118. str := fmt.Sprintf("%s %s", testAddr, edKeyStr)
  119. db := testDB(t, str)
  120. if err := db.check("", testAddr, alternateEdKey); err == nil {
  121. t.Fatalf("no error for unknown address")
  122. } else if ke, ok := err.(*KeyError); !ok {
  123. t.Fatalf("got type %T, want *KeyError", err)
  124. } else if len(ke.Want) == 0 {
  125. t.Fatalf("got empty KeyError.Want")
  126. } else if got, want := ke.Want[0].Key.Marshal(), edKey.Marshal(); !bytes.Equal(got, want) {
  127. t.Fatalf("got key %q, want %q", got, want)
  128. }
  129. }
  130. func TestIPAddress(t *testing.T) {
  131. str := fmt.Sprintf("%s %s", testAddr, edKeyStr)
  132. db := testDB(t, str)
  133. if err := db.check("", testAddr, edKey); err != nil {
  134. t.Errorf("got error %q, want none", err)
  135. }
  136. }
  137. func TestIPv6Address(t *testing.T) {
  138. str := fmt.Sprintf("%s %s", testAddr6, edKeyStr)
  139. db := testDB(t, str)
  140. if err := db.check("", testAddr6, edKey); err != nil {
  141. t.Errorf("got error %q, want none", err)
  142. }
  143. }
  144. func TestBasic(t *testing.T) {
  145. str := fmt.Sprintf("#comment\n\nserver.org,%s %s\notherhost %s", testAddr, edKeyStr, ecKeyStr)
  146. db := testDB(t, str)
  147. if err := db.check("server.org:22", testAddr, edKey); err != nil {
  148. t.Errorf("got error %q, want none", err)
  149. }
  150. want := KnownKey{
  151. Key: edKey,
  152. Filename: "testdb",
  153. Line: 3,
  154. }
  155. if err := db.check("server.org:22", testAddr, ecKey); err == nil {
  156. t.Errorf("succeeded, want KeyError")
  157. } else if ke, ok := err.(*KeyError); !ok {
  158. t.Errorf("got %T, want *KeyError", err)
  159. } else if len(ke.Want) != 1 {
  160. t.Errorf("got %v, want 1 entry", ke)
  161. } else if !reflect.DeepEqual(ke.Want[0], want) {
  162. t.Errorf("got %v, want %v", ke.Want[0], want)
  163. }
  164. }
  165. func TestNegate(t *testing.T) {
  166. str := fmt.Sprintf("%s,!server.org %s", testAddr, edKeyStr)
  167. db := testDB(t, str)
  168. if err := db.check("server.org:22", testAddr, ecKey); err == nil {
  169. t.Errorf("succeeded")
  170. } else if ke, ok := err.(*KeyError); !ok {
  171. t.Errorf("got error type %T, want *KeyError", err)
  172. } else if len(ke.Want) != 0 {
  173. t.Errorf("got expected keys %d (first of type %s), want []", len(ke.Want), ke.Want[0].Key.Type())
  174. }
  175. }
  176. func TestWildcard(t *testing.T) {
  177. str := fmt.Sprintf("server*.domain %s", edKeyStr)
  178. db := testDB(t, str)
  179. want := &KeyError{
  180. Want: []KnownKey{{
  181. Filename: "testdb",
  182. Line: 1,
  183. Key: edKey,
  184. }},
  185. }
  186. got := db.check("server.domain:22", &net.TCPAddr{}, ecKey)
  187. if !reflect.DeepEqual(got, want) {
  188. t.Errorf("got %s, want %s", got, want)
  189. }
  190. }
  191. func TestLine(t *testing.T) {
  192. for in, want := range map[string]string{
  193. "server.org": "server.org " + edKeyStr,
  194. "server.org:22": "server.org " + edKeyStr,
  195. "server.org:23": "[server.org]:23 " + edKeyStr,
  196. "[c629:1ec4:102:304:102:304:102:304]:22": "[c629:1ec4:102:304:102:304:102:304] " + edKeyStr,
  197. "[c629:1ec4:102:304:102:304:102:304]:23": "[c629:1ec4:102:304:102:304:102:304]:23 " + edKeyStr,
  198. } {
  199. if got := Line([]string{in}, edKey); got != want {
  200. t.Errorf("Line(%q) = %q, want %q", in, got, want)
  201. }
  202. }
  203. }
  204. func TestWildcardMatch(t *testing.T) {
  205. for _, c := range []struct {
  206. pat, str string
  207. want bool
  208. }{
  209. {"a?b", "abb", true},
  210. {"ab", "abc", false},
  211. {"abc", "ab", false},
  212. {"a*b", "axxxb", true},
  213. {"a*b", "axbxb", true},
  214. {"a*b", "axbxbc", false},
  215. {"a*?", "axbxc", true},
  216. {"a*b*", "axxbxxxxxx", true},
  217. {"a*b*c", "axxbxxxxxxc", true},
  218. {"a*b*?", "axxbxxxxxxc", true},
  219. {"a*b*z", "axxbxxbxxxz", true},
  220. {"a*b*z", "axxbxxzxxxz", true},
  221. {"a*b*z", "axxbxxzxxx", false},
  222. } {
  223. got := wildcardMatch([]byte(c.pat), []byte(c.str))
  224. if got != c.want {
  225. t.Errorf("wildcardMatch(%q, %q) = %v, want %v", c.pat, c.str, got, c.want)
  226. }
  227. }
  228. }
  229. // TODO(hanwen): test coverage for certificates.
  230. const testHostname = "hostname"
  231. // generated with keygen -H -f
  232. const encodedTestHostnameHash = "|1|IHXZvQMvTcZTUU29+2vXFgx8Frs=|UGccIWfRVDwilMBnA3WJoRAC75Y="
  233. func TestHostHash(t *testing.T) {
  234. testHostHash(t, testHostname, encodedTestHostnameHash)
  235. }
  236. func TestHashList(t *testing.T) {
  237. encoded := HashHostname(testHostname)
  238. testHostHash(t, testHostname, encoded)
  239. }
  240. func testHostHash(t *testing.T, hostname, encoded string) {
  241. typ, salt, hash, err := decodeHash(encoded)
  242. if err != nil {
  243. t.Fatalf("decodeHash: %v", err)
  244. }
  245. if got := encodeHash(typ, salt, hash); got != encoded {
  246. t.Errorf("got encoding %s want %s", got, encoded)
  247. }
  248. if typ != sha1HashType {
  249. t.Fatalf("got hash type %q, want %q", typ, sha1HashType)
  250. }
  251. got := hashHost(hostname, salt)
  252. if !bytes.Equal(got, hash) {
  253. t.Errorf("got hash %x want %x", got, hash)
  254. }
  255. }
  256. func TestNormalize(t *testing.T) {
  257. for in, want := range map[string]string{
  258. "127.0.0.1:22": "127.0.0.1",
  259. "[127.0.0.1]:22": "127.0.0.1",
  260. "[127.0.0.1]:23": "[127.0.0.1]:23",
  261. "127.0.0.1:23": "[127.0.0.1]:23",
  262. "[a.b.c]:22": "a.b.c",
  263. "[abcd:abcd:abcd:abcd]": "[abcd:abcd:abcd:abcd]",
  264. "[abcd:abcd:abcd:abcd]:22": "[abcd:abcd:abcd:abcd]",
  265. "[abcd:abcd:abcd:abcd]:23": "[abcd:abcd:abcd:abcd]:23",
  266. } {
  267. got := Normalize(in)
  268. if got != want {
  269. t.Errorf("Normalize(%q) = %q, want %q", in, got, want)
  270. }
  271. }
  272. }
  273. func TestHashedHostkeyCheck(t *testing.T) {
  274. str := fmt.Sprintf("%s %s", HashHostname(testHostname), edKeyStr)
  275. db := testDB(t, str)
  276. if err := db.check(testHostname+":22", testAddr, edKey); err != nil {
  277. t.Errorf("check(%s): %v", testHostname, err)
  278. }
  279. want := &KeyError{
  280. Want: []KnownKey{{
  281. Filename: "testdb",
  282. Line: 1,
  283. Key: edKey,
  284. }},
  285. }
  286. if got := db.check(testHostname+":22", testAddr, alternateEdKey); !reflect.DeepEqual(got, want) {
  287. t.Errorf("got error %v, want %v", got, want)
  288. }
  289. }