server.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 agent
  5. import (
  6. "crypto/rsa"
  7. "encoding/binary"
  8. "fmt"
  9. "io"
  10. "log"
  11. "math/big"
  12. "github.com/gogits/gogs/modules/crypto/ssh"
  13. )
  14. // Server wraps an Agent and uses it to implement the agent side of
  15. // the SSH-agent, wire protocol.
  16. type server struct {
  17. agent Agent
  18. }
  19. func (s *server) processRequestBytes(reqData []byte) []byte {
  20. rep, err := s.processRequest(reqData)
  21. if err != nil {
  22. if err != errLocked {
  23. // TODO(hanwen): provide better logging interface?
  24. log.Printf("agent %d: %v", reqData[0], err)
  25. }
  26. return []byte{agentFailure}
  27. }
  28. if err == nil && rep == nil {
  29. return []byte{agentSuccess}
  30. }
  31. return ssh.Marshal(rep)
  32. }
  33. func marshalKey(k *Key) []byte {
  34. var record struct {
  35. Blob []byte
  36. Comment string
  37. }
  38. record.Blob = k.Marshal()
  39. record.Comment = k.Comment
  40. return ssh.Marshal(&record)
  41. }
  42. type agentV1IdentityMsg struct {
  43. Numkeys uint32 `sshtype:"2"`
  44. }
  45. type agentRemoveIdentityMsg struct {
  46. KeyBlob []byte `sshtype:"18"`
  47. }
  48. type agentLockMsg struct {
  49. Passphrase []byte `sshtype:"22"`
  50. }
  51. type agentUnlockMsg struct {
  52. Passphrase []byte `sshtype:"23"`
  53. }
  54. func (s *server) processRequest(data []byte) (interface{}, error) {
  55. switch data[0] {
  56. case agentRequestV1Identities:
  57. return &agentV1IdentityMsg{0}, nil
  58. case agentRemoveIdentity:
  59. var req agentRemoveIdentityMsg
  60. if err := ssh.Unmarshal(data, &req); err != nil {
  61. return nil, err
  62. }
  63. var wk wireKey
  64. if err := ssh.Unmarshal(req.KeyBlob, &wk); err != nil {
  65. return nil, err
  66. }
  67. return nil, s.agent.Remove(&Key{Format: wk.Format, Blob: req.KeyBlob})
  68. case agentRemoveAllIdentities:
  69. return nil, s.agent.RemoveAll()
  70. case agentLock:
  71. var req agentLockMsg
  72. if err := ssh.Unmarshal(data, &req); err != nil {
  73. return nil, err
  74. }
  75. return nil, s.agent.Lock(req.Passphrase)
  76. case agentUnlock:
  77. var req agentLockMsg
  78. if err := ssh.Unmarshal(data, &req); err != nil {
  79. return nil, err
  80. }
  81. return nil, s.agent.Unlock(req.Passphrase)
  82. case agentSignRequest:
  83. var req signRequestAgentMsg
  84. if err := ssh.Unmarshal(data, &req); err != nil {
  85. return nil, err
  86. }
  87. var wk wireKey
  88. if err := ssh.Unmarshal(req.KeyBlob, &wk); err != nil {
  89. return nil, err
  90. }
  91. k := &Key{
  92. Format: wk.Format,
  93. Blob: req.KeyBlob,
  94. }
  95. sig, err := s.agent.Sign(k, req.Data) // TODO(hanwen): flags.
  96. if err != nil {
  97. return nil, err
  98. }
  99. return &signResponseAgentMsg{SigBlob: ssh.Marshal(sig)}, nil
  100. case agentRequestIdentities:
  101. keys, err := s.agent.List()
  102. if err != nil {
  103. return nil, err
  104. }
  105. rep := identitiesAnswerAgentMsg{
  106. NumKeys: uint32(len(keys)),
  107. }
  108. for _, k := range keys {
  109. rep.Keys = append(rep.Keys, marshalKey(k)...)
  110. }
  111. return rep, nil
  112. case agentAddIdentity:
  113. return nil, s.insertIdentity(data)
  114. }
  115. return nil, fmt.Errorf("unknown opcode %d", data[0])
  116. }
  117. func (s *server) insertIdentity(req []byte) error {
  118. var record struct {
  119. Type string `sshtype:"17"`
  120. Rest []byte `ssh:"rest"`
  121. }
  122. if err := ssh.Unmarshal(req, &record); err != nil {
  123. return err
  124. }
  125. switch record.Type {
  126. case ssh.KeyAlgoRSA:
  127. var k rsaKeyMsg
  128. if err := ssh.Unmarshal(req, &k); err != nil {
  129. return err
  130. }
  131. priv := rsa.PrivateKey{
  132. PublicKey: rsa.PublicKey{
  133. E: int(k.E.Int64()),
  134. N: k.N,
  135. },
  136. D: k.D,
  137. Primes: []*big.Int{k.P, k.Q},
  138. }
  139. priv.Precompute()
  140. return s.agent.Add(AddedKey{PrivateKey: &priv, Comment: k.Comments})
  141. }
  142. return fmt.Errorf("not implemented: %s", record.Type)
  143. }
  144. // ServeAgent serves the agent protocol on the given connection. It
  145. // returns when an I/O error occurs.
  146. func ServeAgent(agent Agent, c io.ReadWriter) error {
  147. s := &server{agent}
  148. var length [4]byte
  149. for {
  150. if _, err := io.ReadFull(c, length[:]); err != nil {
  151. return err
  152. }
  153. l := binary.BigEndian.Uint32(length[:])
  154. if l > maxAgentResponseBytes {
  155. // We also cap requests.
  156. return fmt.Errorf("agent: request too large: %d", l)
  157. }
  158. req := make([]byte, l)
  159. if _, err := io.ReadFull(c, req); err != nil {
  160. return err
  161. }
  162. repData := s.processRequestBytes(req)
  163. if len(repData) > maxAgentResponseBytes {
  164. return fmt.Errorf("agent: reply too large: %d bytes", len(repData))
  165. }
  166. binary.BigEndian.PutUint32(length[:], uint32(len(repData)))
  167. if _, err := c.Write(length[:]); err != nil {
  168. return err
  169. }
  170. if _, err := c.Write(repData); err != nil {
  171. return err
  172. }
  173. }
  174. }