client_auth_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright 2011 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 ssh
  5. import (
  6. "bytes"
  7. "crypto/dsa"
  8. "io"
  9. "io/ioutil"
  10. "math/big"
  11. "strings"
  12. "testing"
  13. _ "crypto/sha1"
  14. )
  15. // private key for mock server
  16. const testServerPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
  17. MIIEpAIBAAKCAQEA19lGVsTqIT5iiNYRgnoY1CwkbETW5cq+Rzk5v/kTlf31XpSU
  18. 70HVWkbTERECjaYdXM2gGcbb+sxpq6GtXf1M3kVomycqhxwhPv4Cr6Xp4WT/jkFx
  19. 9z+FFzpeodGJWjOH6L2H5uX1Cvr9EDdQp9t9/J32/qBFntY8GwoUI/y/1MSTmMiF
  20. tupdMODN064vd3gyMKTwrlQ8tZM6aYuyOPsutLlUY7M5x5FwMDYvnPDSeyT/Iw0z
  21. s3B+NCyqeeMd2T7YzQFnRATj0M7rM5LoSs7DVqVriOEABssFyLj31PboaoLhOKgc
  22. qoM9khkNzr7FHVvi+DhYM2jD0DwvqZLN6NmnLwIDAQABAoIBAQCGVj+kuSFOV1lT
  23. +IclQYA6bM6uY5mroqcSBNegVxCNhWU03BxlW//BE9tA/+kq53vWylMeN9mpGZea
  24. riEMIh25KFG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40. gqnBycHj6AhEycjda75cs+0zybZvN4x65KZHOGW/O/7OAWEcZP5TPb3zf9ned3Hl
  41. NsZoFj52ponUM6+99A2CmezFCN16c4mbA//luWF+k3VVqR6BpkrhKw==
  42. -----END RSA PRIVATE KEY-----`
  43. const testClientPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
  44. MIIBOwIBAAJBALdGZxkXDAjsYk10ihwU6Id2KeILz1TAJuoq4tOgDWxEEGeTrcld
  45. r/ZwVaFzjWzxaf6zQIJbfaSEAhqD5yo72+sCAwEAAQJBAK8PEVU23Wj8mV0QjwcJ
  46. tZ4GcTUYQL7cF4+ezTCE9a1NrGnCP2RuQkHEKxuTVrxXt+6OF15/1/fuXnxKjmJC
  47. nxkCIQDaXvPPBi0c7vAxGwNY9726x01/dNbHCE0CBtcotobxpwIhANbbQbh3JHVW
  48. 2haQh4fAG5mhesZKAGcxTyv4mQ7uMSQdAiAj+4dzMpJWdSzQ+qGHlHMIBvVHLkqB
  49. y2VdEyF7DPCZewIhAI7GOI/6LDIFOvtPo6Bj2nNmyQ1HU6k/LRtNIXi4c9NJAiAr
  50. rrxx26itVhJmcvoUhOjwuzSlP2bE5VHAvkGB352YBg==
  51. -----END RSA PRIVATE KEY-----`
  52. // keychain implements the ClientKeyring interface
  53. type keychain struct {
  54. keys []Signer
  55. }
  56. func (k *keychain) Key(i int) (PublicKey, error) {
  57. if i < 0 || i >= len(k.keys) {
  58. return nil, nil
  59. }
  60. return k.keys[i].PublicKey(), nil
  61. }
  62. func (k *keychain) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
  63. return k.keys[i].Sign(rand, data)
  64. }
  65. func (k *keychain) add(key Signer) {
  66. k.keys = append(k.keys, key)
  67. }
  68. func (k *keychain) loadPEM(file string) error {
  69. buf, err := ioutil.ReadFile(file)
  70. if err != nil {
  71. return err
  72. }
  73. key, err := ParsePrivateKey(buf)
  74. if err != nil {
  75. return err
  76. }
  77. k.add(key)
  78. return nil
  79. }
  80. // password implements the ClientPassword interface
  81. type password string
  82. func (p password) Password(user string) (string, error) {
  83. return string(p), nil
  84. }
  85. type keyboardInteractive map[string]string
  86. func (cr *keyboardInteractive) Challenge(user string, instruction string, questions []string, echos []bool) ([]string, error) {
  87. var answers []string
  88. for _, q := range questions {
  89. answers = append(answers, (*cr)[q])
  90. }
  91. return answers, nil
  92. }
  93. // reused internally by tests
  94. var (
  95. rsaKey Signer
  96. dsaKey Signer
  97. clientKeychain = new(keychain)
  98. clientPassword = password("tiger")
  99. serverConfig = &ServerConfig{
  100. PasswordCallback: func(conn *ServerConn, user, pass string) bool {
  101. return user == "testuser" && pass == string(clientPassword)
  102. },
  103. PublicKeyCallback: func(conn *ServerConn, user, algo string, pubkey []byte) bool {
  104. key, _ := clientKeychain.Key(0)
  105. expected := MarshalPublicKey(key)
  106. algoname := key.PublicKeyAlgo()
  107. return user == "testuser" && algo == algoname && bytes.Equal(pubkey, expected)
  108. },
  109. KeyboardInteractiveCallback: func(conn *ServerConn, user string, client ClientKeyboardInteractive) bool {
  110. ans, err := client.Challenge("user",
  111. "instruction",
  112. []string{"question1", "question2"},
  113. []bool{true, true})
  114. if err != nil {
  115. return false
  116. }
  117. ok := user == "testuser" && ans[0] == "answer1" && ans[1] == "answer2"
  118. client.Challenge("user", "motd", nil, nil)
  119. return ok
  120. },
  121. }
  122. )
  123. func init() {
  124. var err error
  125. rsaKey, err = ParsePrivateKey([]byte(testServerPrivateKey))
  126. if err != nil {
  127. panic("unable to set private key: " + err.Error())
  128. }
  129. rawDSAKey := new(dsa.PrivateKey)
  130. // taken from crypto/dsa/dsa_test.go
  131. rawDSAKey.P, _ = new(big.Int).SetString("A9B5B793FB4785793D246BAE77E8FF63CA52F442DA763C440259919FE1BC1D6065A9350637A04F75A2F039401D49F08E066C4D275A5A65DA5684BC563C14289D7AB8A67163BFBF79D85972619AD2CFF55AB0EE77A9002B0EF96293BDD0F42685EBB2C66C327079F6C98000FBCB79AACDE1BC6F9D5C7B1A97E3D9D54ED7951FEF", 16)
  132. rawDSAKey.Q, _ = new(big.Int).SetString("E1D3391245933D68A0714ED34BBCB7A1F422B9C1", 16)
  133. rawDSAKey.G, _ = new(big.Int).SetString("634364FC25248933D01D1993ECABD0657CC0CB2CEED7ED2E3E8AECDFCDC4A25C3B15E9E3B163ACA2984B5539181F3EFF1A5E8903D71D5B95DA4F27202B77D2C44B430BB53741A8D59A8F86887525C9F2A6A5980A195EAA7F2FF910064301DEF89D3AA213E1FAC7768D89365318E370AF54A112EFBA9246D9158386BA1B4EEFDA", 16)
  134. rawDSAKey.Y, _ = new(big.Int).SetString("32969E5780CFE1C849A1C276D7AEB4F38A23B591739AA2FE197349AEEBD31366AEE5EB7E6C6DDB7C57D02432B30DB5AA66D9884299FAA72568944E4EEDC92EA3FBC6F39F53412FBCC563208F7C15B737AC8910DBC2D9C9B8C001E72FDC40EB694AB1F06A5A2DBD18D9E36C66F31F566742F11EC0A52E9F7B89355C02FB5D32D2", 16)
  135. rawDSAKey.X, _ = new(big.Int).SetString("5078D4D29795CBE76D3AACFE48C9AF0BCDBEE91A", 16)
  136. dsaKey, err = NewSignerFromKey(rawDSAKey)
  137. if err != nil {
  138. panic("NewSignerFromKey: " + err.Error())
  139. }
  140. clientKeychain.add(rsaKey)
  141. serverConfig.AddHostKey(rsaKey)
  142. }
  143. // newMockAuthServer creates a new Server bound to
  144. // the loopback interface. The server exits after
  145. // processing one handshake.
  146. func newMockAuthServer(t *testing.T) string {
  147. l, err := Listen("tcp", "127.0.0.1:0", serverConfig)
  148. if err != nil {
  149. t.Fatalf("unable to newMockAuthServer: %s", err)
  150. }
  151. go func() {
  152. defer l.Close()
  153. c, err := l.Accept()
  154. if err != nil {
  155. t.Errorf("Unable to accept incoming connection: %v", err)
  156. return
  157. }
  158. if err := c.Handshake(); err != nil {
  159. // not Errorf because this is expected to
  160. // fail for some tests.
  161. t.Logf("Handshaking error: %v", err)
  162. return
  163. }
  164. defer c.Close()
  165. }()
  166. return l.Addr().String()
  167. }
  168. func TestClientAuthPublicKey(t *testing.T) {
  169. config := &ClientConfig{
  170. User: "testuser",
  171. Auth: []ClientAuth{
  172. ClientAuthKeyring(clientKeychain),
  173. },
  174. }
  175. c, err := Dial("tcp", newMockAuthServer(t), config)
  176. if err != nil {
  177. t.Fatalf("unable to dial remote side: %s", err)
  178. }
  179. c.Close()
  180. }
  181. func TestClientAuthPassword(t *testing.T) {
  182. config := &ClientConfig{
  183. User: "testuser",
  184. Auth: []ClientAuth{
  185. ClientAuthPassword(clientPassword),
  186. },
  187. }
  188. c, err := Dial("tcp", newMockAuthServer(t), config)
  189. if err != nil {
  190. t.Fatalf("unable to dial remote side: %s", err)
  191. }
  192. c.Close()
  193. }
  194. func TestClientAuthWrongPassword(t *testing.T) {
  195. wrongPw := password("wrong")
  196. config := &ClientConfig{
  197. User: "testuser",
  198. Auth: []ClientAuth{
  199. ClientAuthPassword(wrongPw),
  200. ClientAuthKeyring(clientKeychain),
  201. },
  202. }
  203. c, err := Dial("tcp", newMockAuthServer(t), config)
  204. if err != nil {
  205. t.Fatalf("unable to dial remote side: %s", err)
  206. }
  207. c.Close()
  208. }
  209. func TestClientAuthKeyboardInteractive(t *testing.T) {
  210. answers := keyboardInteractive(map[string]string{
  211. "question1": "answer1",
  212. "question2": "answer2",
  213. })
  214. config := &ClientConfig{
  215. User: "testuser",
  216. Auth: []ClientAuth{
  217. ClientAuthKeyboardInteractive(&answers),
  218. },
  219. }
  220. c, err := Dial("tcp", newMockAuthServer(t), config)
  221. if err != nil {
  222. t.Fatalf("unable to dial remote side: %s", err)
  223. }
  224. c.Close()
  225. }
  226. func TestClientAuthWrongKeyboardInteractive(t *testing.T) {
  227. answers := keyboardInteractive(map[string]string{
  228. "question1": "answer1",
  229. "question2": "WRONG",
  230. })
  231. config := &ClientConfig{
  232. User: "testuser",
  233. Auth: []ClientAuth{
  234. ClientAuthKeyboardInteractive(&answers),
  235. },
  236. }
  237. c, err := Dial("tcp", newMockAuthServer(t), config)
  238. if err == nil {
  239. c.Close()
  240. t.Fatalf("wrong answers should not have authenticated with KeyboardInteractive")
  241. }
  242. }
  243. // the mock server will only authenticate ssh-rsa keys
  244. func TestClientAuthInvalidPublicKey(t *testing.T) {
  245. kc := new(keychain)
  246. kc.add(dsaKey)
  247. config := &ClientConfig{
  248. User: "testuser",
  249. Auth: []ClientAuth{
  250. ClientAuthKeyring(kc),
  251. },
  252. }
  253. c, err := Dial("tcp", newMockAuthServer(t), config)
  254. if err == nil {
  255. c.Close()
  256. t.Fatalf("dsa private key should not have authenticated with rsa public key")
  257. }
  258. }
  259. // the client should authenticate with the second key
  260. func TestClientAuthRSAandDSA(t *testing.T) {
  261. kc := new(keychain)
  262. kc.add(dsaKey)
  263. kc.add(rsaKey)
  264. config := &ClientConfig{
  265. User: "testuser",
  266. Auth: []ClientAuth{
  267. ClientAuthKeyring(kc),
  268. },
  269. }
  270. c, err := Dial("tcp", newMockAuthServer(t), config)
  271. if err != nil {
  272. t.Fatalf("client could not authenticate with rsa key: %v", err)
  273. }
  274. c.Close()
  275. }
  276. func TestClientHMAC(t *testing.T) {
  277. kc := new(keychain)
  278. kc.add(rsaKey)
  279. for _, mac := range DefaultMACOrder {
  280. config := &ClientConfig{
  281. User: "testuser",
  282. Auth: []ClientAuth{
  283. ClientAuthKeyring(kc),
  284. },
  285. Crypto: CryptoConfig{
  286. MACs: []string{mac},
  287. },
  288. }
  289. c, err := Dial("tcp", newMockAuthServer(t), config)
  290. if err != nil {
  291. t.Fatalf("client could not authenticate with mac algo %s: %v", mac, err)
  292. }
  293. c.Close()
  294. }
  295. }
  296. // issue 4285.
  297. func TestClientUnsupportedCipher(t *testing.T) {
  298. kc := new(keychain)
  299. config := &ClientConfig{
  300. User: "testuser",
  301. Auth: []ClientAuth{
  302. ClientAuthKeyring(kc),
  303. },
  304. Crypto: CryptoConfig{
  305. Ciphers: []string{"aes128-cbc"}, // not currently supported
  306. },
  307. }
  308. c, err := Dial("tcp", newMockAuthServer(t), config)
  309. if err == nil {
  310. t.Errorf("expected no ciphers in common")
  311. c.Close()
  312. }
  313. }
  314. func TestClientUnsupportedKex(t *testing.T) {
  315. kc := new(keychain)
  316. config := &ClientConfig{
  317. User: "testuser",
  318. Auth: []ClientAuth{
  319. ClientAuthKeyring(kc),
  320. },
  321. Crypto: CryptoConfig{
  322. KeyExchanges: []string{"diffie-hellman-group-exchange-sha256"}, // not currently supported
  323. },
  324. }
  325. c, err := Dial("tcp", newMockAuthServer(t), config)
  326. if err == nil || !strings.Contains(err.Error(), "no common algorithms") {
  327. t.Errorf("got %v, expected 'no common algorithms'", err)
  328. c.Close()
  329. }
  330. }