agent.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2012 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. // References
  6. // PROTOCOL.agent: http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.agent
  7. import (
  8. "encoding/base64"
  9. "errors"
  10. "fmt"
  11. "io"
  12. )
  13. // See PROTOCOL.agent, section 3.
  14. const (
  15. // 3.2 Requests from client to agent for protocol 2 key operations
  16. agentRequestIdentities = 11
  17. agentSignRequest = 13
  18. agentAddIdentity = 17
  19. agentRemoveIdentity = 18
  20. agentRemoveAllIdentities = 19
  21. agentAddIdConstrained = 25
  22. // 3.3 Key-type independent requests from client to agent
  23. agentAddSmartcardKey = 20
  24. agentRemoveSmartcardKey = 21
  25. agentLock = 22
  26. agentUnlock = 23
  27. agentAddSmartcardKeyConstrained = 26
  28. // 3.4 Generic replies from agent to client
  29. agentFailure = 5
  30. agentSuccess = 6
  31. // 3.6 Replies from agent to client for protocol 2 key operations
  32. agentIdentitiesAnswer = 12
  33. agentSignResponse = 14
  34. // 3.7 Key constraint identifiers
  35. agentConstrainLifetime = 1
  36. agentConstrainConfirm = 2
  37. )
  38. // Agent messages:
  39. // These structures mirror the wire format of the corresponding ssh agent
  40. // messages found in PROTOCOL.agent.
  41. type failureAgentMsg struct{}
  42. type successAgentMsg struct{}
  43. // See PROTOCOL.agent, section 2.5.2.
  44. type requestIdentitiesAgentMsg struct{}
  45. // See PROTOCOL.agent, section 2.5.2.
  46. type identitiesAnswerAgentMsg struct {
  47. NumKeys uint32
  48. Keys []byte `ssh:"rest"`
  49. }
  50. // See PROTOCOL.agent, section 2.6.2.
  51. type signRequestAgentMsg struct {
  52. KeyBlob []byte
  53. Data []byte
  54. Flags uint32
  55. }
  56. // See PROTOCOL.agent, section 2.6.2.
  57. type signResponseAgentMsg struct {
  58. SigBlob []byte
  59. }
  60. // AgentKey represents a protocol 2 key as defined in PROTOCOL.agent,
  61. // section 2.5.2.
  62. type AgentKey struct {
  63. blob []byte
  64. Comment string
  65. }
  66. // String returns the storage form of an agent key with the format, base64
  67. // encoded serialized key, and the comment if it is not empty.
  68. func (ak *AgentKey) String() string {
  69. algo, _, ok := parseString(ak.blob)
  70. if !ok {
  71. return "malformed key"
  72. }
  73. algoName := string(algo)
  74. b64EncKey := base64.StdEncoding.EncodeToString(ak.blob)
  75. comment := ""
  76. if ak.Comment != "" {
  77. comment = " " + ak.Comment
  78. }
  79. return fmt.Sprintf("%s %s%s", algoName, b64EncKey, comment)
  80. }
  81. // Key returns an agent's public key as a *rsa.PublicKey, *dsa.PublicKey, or
  82. // *OpenSSHCertV01.
  83. func (ak *AgentKey) Key() (interface{}, error) {
  84. if key, _, ok := parsePubKey(ak.blob); ok {
  85. return key, nil
  86. }
  87. return nil, errors.New("ssh: failed to parse key blob")
  88. }
  89. func parseAgentKey(in []byte) (out *AgentKey, rest []byte, ok bool) {
  90. ak := new(AgentKey)
  91. if ak.blob, in, ok = parseString(in); !ok {
  92. return
  93. }
  94. comment, in, ok := parseString(in)
  95. if !ok {
  96. return
  97. }
  98. ak.Comment = string(comment)
  99. return ak, in, true
  100. }
  101. // AgentClient provides a means to communicate with an ssh agent process based
  102. // on the protocol described in PROTOCOL.agent?rev=1.6. It contains an
  103. // embedded io.ReadWriter that is typically represented by using a *net.UnixConn.
  104. type AgentClient struct {
  105. io.ReadWriter
  106. }
  107. func (ac *AgentClient) sendRequest(req []byte) error {
  108. msg := make([]byte, stringLength(req))
  109. marshalString(msg, req)
  110. if _, err := ac.Write(msg); err != nil {
  111. return err
  112. }
  113. return nil
  114. }
  115. func (ac *AgentClient) readResponse() ([]byte, error) {
  116. var respSizeBuf [4]byte
  117. if _, err := io.ReadFull(ac, respSizeBuf[:]); err != nil {
  118. return nil, err
  119. }
  120. respSize, _, ok := parseUint32(respSizeBuf[:])
  121. if !ok {
  122. return nil, errors.New("ssh: failure to parse response size")
  123. }
  124. buf := make([]byte, respSize)
  125. if _, err := io.ReadFull(ac, buf); err != nil {
  126. return nil, err
  127. }
  128. return buf, nil
  129. }
  130. // RequestIdentities queries the agent for protocol 2 keys as defined in
  131. // PROTOCOL.agent section 2.5.2.
  132. func (ac *AgentClient) RequestIdentities() ([]*AgentKey, error) {
  133. req := marshal(agentRequestIdentities, requestIdentitiesAgentMsg{})
  134. if err := ac.sendRequest(req); err != nil {
  135. return nil, err
  136. }
  137. resp, err := ac.readResponse()
  138. if err != nil {
  139. return nil, err
  140. }
  141. switch msg := decodeAgentMsg(resp).(type) {
  142. case *identitiesAnswerAgentMsg:
  143. keys := make([]*AgentKey, msg.NumKeys)
  144. data := msg.Keys[:]
  145. for i := uint32(0); i < msg.NumKeys; i++ {
  146. var key *AgentKey
  147. var ok bool
  148. if key, data, ok = parseAgentKey(data); !ok {
  149. return nil, ParseError{agentIdentitiesAnswer}
  150. }
  151. keys[i] = key
  152. }
  153. return keys, nil
  154. case *failureAgentMsg:
  155. return nil, errors.New("ssh: failed to list keys.")
  156. case ParseError, UnexpectedMessageError:
  157. return nil, msg.(error)
  158. }
  159. return nil, UnexpectedMessageError{agentIdentitiesAnswer, resp[0]}
  160. }
  161. // SignRequest requests the signing of data by the agent using a protocol 2 key
  162. // as defined in PROTOCOL.agent section 2.6.2. Supported key types include
  163. // *rsa.PublicKey, *dsa.PublicKey, *OpenSSHCertV01.
  164. func (ac *AgentClient) SignRequest(key interface{}, data []byte) ([]byte, error) {
  165. req := marshal(agentSignRequest, signRequestAgentMsg{
  166. KeyBlob: serializePublickey(key),
  167. Data: data,
  168. })
  169. if err := ac.sendRequest(req); err != nil {
  170. return nil, err
  171. }
  172. resp, err := ac.readResponse()
  173. if err != nil {
  174. return nil, err
  175. }
  176. switch msg := decodeAgentMsg(resp).(type) {
  177. case *signResponseAgentMsg:
  178. return msg.SigBlob, nil
  179. case *failureAgentMsg:
  180. return nil, errors.New("ssh: failed to sign challenge")
  181. case ParseError, UnexpectedMessageError:
  182. return nil, msg.(error)
  183. }
  184. return nil, UnexpectedMessageError{agentSignResponse, resp[0]}
  185. }
  186. func decodeAgentMsg(packet []byte) interface{} {
  187. if len(packet) < 1 {
  188. return ParseError{0}
  189. }
  190. var msg interface{}
  191. switch packet[0] {
  192. case agentFailure:
  193. msg = new(failureAgentMsg)
  194. case agentSuccess:
  195. msg = new(successAgentMsg)
  196. case agentIdentitiesAnswer:
  197. msg = new(identitiesAnswerAgentMsg)
  198. case agentSignResponse:
  199. msg = new(signResponseAgentMsg)
  200. default:
  201. return UnexpectedMessageError{0, packet[0]}
  202. }
  203. if err := unmarshal(msg, packet, packet[0]); err != nil {
  204. return err
  205. }
  206. return msg
  207. }