client_auth.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. "errors"
  7. "fmt"
  8. "io"
  9. )
  10. // authenticate authenticates with the remote server. See RFC 4252.
  11. func (c *ClientConn) authenticate(session []byte) error {
  12. // initiate user auth session
  13. if err := c.writePacket(marshal(msgServiceRequest, serviceRequestMsg{serviceUserAuth})); err != nil {
  14. return err
  15. }
  16. packet, err := c.readPacket()
  17. if err != nil {
  18. return err
  19. }
  20. var serviceAccept serviceAcceptMsg
  21. if err := unmarshal(&serviceAccept, packet, msgServiceAccept); err != nil {
  22. return err
  23. }
  24. // during the authentication phase the client first attempts the "none" method
  25. // then any untried methods suggested by the server.
  26. tried, remain := make(map[string]bool), make(map[string]bool)
  27. for auth := ClientAuth(new(noneAuth)); auth != nil; {
  28. ok, methods, err := auth.auth(session, c.config.User, c.transport, c.config.rand())
  29. if err != nil {
  30. return err
  31. }
  32. if ok {
  33. // success
  34. return nil
  35. }
  36. tried[auth.method()] = true
  37. delete(remain, auth.method())
  38. for _, meth := range methods {
  39. if tried[meth] {
  40. // if we've tried meth already, skip it.
  41. continue
  42. }
  43. remain[meth] = true
  44. }
  45. auth = nil
  46. for _, a := range c.config.Auth {
  47. if remain[a.method()] {
  48. auth = a
  49. break
  50. }
  51. }
  52. }
  53. return fmt.Errorf("ssh: unable to authenticate, attempted methods %v, no supported methods remain", keys(tried))
  54. }
  55. func keys(m map[string]bool) (s []string) {
  56. for k, _ := range m {
  57. s = append(s, k)
  58. }
  59. return
  60. }
  61. // A ClientAuth represents an instance of an RFC 4252 authentication method.
  62. type ClientAuth interface {
  63. // auth authenticates user over transport t.
  64. // Returns true if authentication is successful.
  65. // If authentication is not successful, a []string of alternative
  66. // method names is returned.
  67. auth(session []byte, user string, t *transport, rand io.Reader) (bool, []string, error)
  68. // method returns the RFC 4252 method name.
  69. method() string
  70. }
  71. // "none" authentication, RFC 4252 section 5.2.
  72. type noneAuth int
  73. func (n *noneAuth) auth(session []byte, user string, t *transport, rand io.Reader) (bool, []string, error) {
  74. if err := t.writePacket(marshal(msgUserAuthRequest, userAuthRequestMsg{
  75. User: user,
  76. Service: serviceSSH,
  77. Method: "none",
  78. })); err != nil {
  79. return false, nil, err
  80. }
  81. return handleAuthResponse(t)
  82. }
  83. func (n *noneAuth) method() string {
  84. return "none"
  85. }
  86. // "password" authentication, RFC 4252 Section 8.
  87. type passwordAuth struct {
  88. ClientPassword
  89. }
  90. func (p *passwordAuth) auth(session []byte, user string, t *transport, rand io.Reader) (bool, []string, error) {
  91. type passwordAuthMsg struct {
  92. User string
  93. Service string
  94. Method string
  95. Reply bool
  96. Password string
  97. }
  98. pw, err := p.Password(user)
  99. if err != nil {
  100. return false, nil, err
  101. }
  102. if err := t.writePacket(marshal(msgUserAuthRequest, passwordAuthMsg{
  103. User: user,
  104. Service: serviceSSH,
  105. Method: "password",
  106. Reply: false,
  107. Password: pw,
  108. })); err != nil {
  109. return false, nil, err
  110. }
  111. return handleAuthResponse(t)
  112. }
  113. func (p *passwordAuth) method() string {
  114. return "password"
  115. }
  116. // A ClientPassword implements access to a client's passwords.
  117. type ClientPassword interface {
  118. // Password returns the password to use for user.
  119. Password(user string) (password string, err error)
  120. }
  121. // ClientAuthPassword returns a ClientAuth using password authentication.
  122. func ClientAuthPassword(impl ClientPassword) ClientAuth {
  123. return &passwordAuth{impl}
  124. }
  125. // ClientKeyring implements access to a client key ring.
  126. type ClientKeyring interface {
  127. // Key returns the i'th *rsa.Publickey or *dsa.Publickey, or nil if
  128. // no key exists at i.
  129. Key(i int) (key interface{}, err error)
  130. // Sign returns a signature of the given data using the i'th key
  131. // and the supplied random source.
  132. Sign(i int, rand io.Reader, data []byte) (sig []byte, err error)
  133. }
  134. // "publickey" authentication, RFC 4252 Section 7.
  135. type publickeyAuth struct {
  136. ClientKeyring
  137. }
  138. type publickeyAuthMsg struct {
  139. User string
  140. Service string
  141. Method string
  142. // HasSig indicates to the reciver packet that the auth request is signed and
  143. // should be used for authentication of the request.
  144. HasSig bool
  145. Algoname string
  146. Pubkey string
  147. // Sig is defined as []byte so marshal will exclude it during validateKey
  148. Sig []byte `ssh:"rest"`
  149. }
  150. func (p *publickeyAuth) auth(session []byte, user string, t *transport, rand io.Reader) (bool, []string, error) {
  151. // Authentication is performed in two stages. The first stage sends an
  152. // enquiry to test if each key is acceptable to the remote. The second
  153. // stage attempts to authenticate with the valid keys obtained in the
  154. // first stage.
  155. var index int
  156. // a map of public keys to their index in the keyring
  157. validKeys := make(map[int]interface{})
  158. for {
  159. key, err := p.Key(index)
  160. if err != nil {
  161. return false, nil, err
  162. }
  163. if key == nil {
  164. // no more keys in the keyring
  165. break
  166. }
  167. if ok, err := p.validateKey(key, user, t); ok {
  168. validKeys[index] = key
  169. } else {
  170. if err != nil {
  171. return false, nil, err
  172. }
  173. }
  174. index++
  175. }
  176. // methods that may continue if this auth is not successful.
  177. var methods []string
  178. for i, key := range validKeys {
  179. pubkey := serializePublickey(key)
  180. algoname := algoName(key)
  181. sign, err := p.Sign(i, rand, buildDataSignedForAuth(session, userAuthRequestMsg{
  182. User: user,
  183. Service: serviceSSH,
  184. Method: p.method(),
  185. }, []byte(algoname), pubkey))
  186. if err != nil {
  187. return false, nil, err
  188. }
  189. // manually wrap the serialized signature in a string
  190. s := serializeSignature(algoname, sign)
  191. sig := make([]byte, stringLength(len(s)))
  192. marshalString(sig, s)
  193. msg := publickeyAuthMsg{
  194. User: user,
  195. Service: serviceSSH,
  196. Method: p.method(),
  197. HasSig: true,
  198. Algoname: algoname,
  199. Pubkey: string(pubkey),
  200. Sig: sig,
  201. }
  202. p := marshal(msgUserAuthRequest, msg)
  203. if err := t.writePacket(p); err != nil {
  204. return false, nil, err
  205. }
  206. success, methods, err := handleAuthResponse(t)
  207. if err != nil {
  208. return false, nil, err
  209. }
  210. if success {
  211. return success, methods, err
  212. }
  213. }
  214. return false, methods, nil
  215. }
  216. // validateKey validates the key provided it is acceptable to the server.
  217. func (p *publickeyAuth) validateKey(key interface{}, user string, t *transport) (bool, error) {
  218. pubkey := serializePublickey(key)
  219. algoname := algoName(key)
  220. msg := publickeyAuthMsg{
  221. User: user,
  222. Service: serviceSSH,
  223. Method: p.method(),
  224. HasSig: false,
  225. Algoname: algoname,
  226. Pubkey: string(pubkey),
  227. }
  228. if err := t.writePacket(marshal(msgUserAuthRequest, msg)); err != nil {
  229. return false, err
  230. }
  231. return p.confirmKeyAck(key, t)
  232. }
  233. func (p *publickeyAuth) confirmKeyAck(key interface{}, t *transport) (bool, error) {
  234. pubkey := serializePublickey(key)
  235. algoname := algoName(key)
  236. for {
  237. packet, err := t.readPacket()
  238. if err != nil {
  239. return false, err
  240. }
  241. switch packet[0] {
  242. case msgUserAuthBanner:
  243. // TODO(gpaul): add callback to present the banner to the user
  244. case msgUserAuthPubKeyOk:
  245. msg := decode(packet).(*userAuthPubKeyOkMsg)
  246. if msg.Algo != algoname || msg.PubKey != string(pubkey) {
  247. return false, nil
  248. }
  249. return true, nil
  250. case msgUserAuthFailure:
  251. return false, nil
  252. default:
  253. return false, UnexpectedMessageError{msgUserAuthSuccess, packet[0]}
  254. }
  255. }
  256. panic("unreachable")
  257. }
  258. func (p *publickeyAuth) method() string {
  259. return "publickey"
  260. }
  261. // ClientAuthKeyring returns a ClientAuth using public key authentication.
  262. func ClientAuthKeyring(impl ClientKeyring) ClientAuth {
  263. return &publickeyAuth{impl}
  264. }
  265. // handleAuthResponse returns whether the preceding authentication request succeeded
  266. // along with a list of remaining authentication methods to try next and
  267. // an error if an unexpected response was received.
  268. func handleAuthResponse(t *transport) (bool, []string, error) {
  269. for {
  270. packet, err := t.readPacket()
  271. if err != nil {
  272. return false, nil, err
  273. }
  274. switch packet[0] {
  275. case msgUserAuthBanner:
  276. // TODO: add callback to present the banner to the user
  277. case msgUserAuthFailure:
  278. msg := decode(packet).(*userAuthFailureMsg)
  279. return false, msg.Methods, nil
  280. case msgUserAuthSuccess:
  281. return true, nil, nil
  282. case msgDisconnect:
  283. return false, nil, io.EOF
  284. default:
  285. return false, nil, UnexpectedMessageError{msgUserAuthSuccess, packet[0]}
  286. }
  287. }
  288. panic("unreachable")
  289. }
  290. // ClientAuthKeyring returns a ClientAuth using public key authentication via
  291. // an agent.
  292. func ClientAuthAgent(agent *AgentClient) ClientAuth {
  293. return ClientAuthKeyring(&agentKeyring{agent: agent})
  294. }
  295. // agentKeyring implements ClientKeyring.
  296. type agentKeyring struct {
  297. agent *AgentClient
  298. keys []*AgentKey
  299. }
  300. func (kr *agentKeyring) Key(i int) (key interface{}, err error) {
  301. if kr.keys == nil {
  302. if kr.keys, err = kr.agent.RequestIdentities(); err != nil {
  303. return
  304. }
  305. }
  306. if i >= len(kr.keys) {
  307. return
  308. }
  309. return kr.keys[i].Key()
  310. }
  311. func (kr *agentKeyring) Sign(i int, rand io.Reader, data []byte) (sig []byte, err error) {
  312. var key interface{}
  313. if key, err = kr.Key(i); err != nil {
  314. return
  315. }
  316. if key == nil {
  317. return nil, errors.New("ssh: key index out of range")
  318. }
  319. if sig, err = kr.agent.SignRequest(key, data); err != nil {
  320. return
  321. }
  322. // Unmarshal the signature.
  323. var ok bool
  324. if _, sig, ok = parseString(sig); !ok {
  325. return nil, errors.New("ssh: malformed signature response from agent")
  326. }
  327. if sig, _, ok = parseString(sig); !ok {
  328. return nil, errors.New("ssh: malformed signature response from agent")
  329. }
  330. return sig, nil
  331. }