common.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. "crypto/dsa"
  7. "crypto/rsa"
  8. "math/big"
  9. "strconv"
  10. "sync"
  11. )
  12. // These are string constants in the SSH protocol.
  13. const (
  14. kexAlgoDH14SHA1 = "diffie-hellman-group14-sha1"
  15. hostAlgoRSA = "ssh-rsa"
  16. macSHA196 = "hmac-sha1-96"
  17. compressionNone = "none"
  18. serviceUserAuth = "ssh-userauth"
  19. serviceSSH = "ssh-connection"
  20. )
  21. var supportedKexAlgos = []string{kexAlgoDH14SHA1}
  22. var supportedHostKeyAlgos = []string{hostAlgoRSA}
  23. var supportedMACs = []string{macSHA196}
  24. var supportedCompressions = []string{compressionNone}
  25. // dhGroup is a multiplicative group suitable for implementing Diffie-Hellman key agreement.
  26. type dhGroup struct {
  27. g, p *big.Int
  28. }
  29. // dhGroup14 is the group called diffie-hellman-group14-sha1 in RFC 4253 and
  30. // Oakley Group 14 in RFC 3526.
  31. var dhGroup14 *dhGroup
  32. var dhGroup14Once sync.Once
  33. func initDHGroup14() {
  34. p, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF", 16)
  35. dhGroup14 = &dhGroup{
  36. g: new(big.Int).SetInt64(2),
  37. p: p,
  38. }
  39. }
  40. // UnexpectedMessageError results when the SSH message that we received didn't
  41. // match what we wanted.
  42. type UnexpectedMessageError struct {
  43. expected, got uint8
  44. }
  45. func (u UnexpectedMessageError) Error() string {
  46. return "ssh: unexpected message type " + strconv.Itoa(int(u.got)) + " (expected " + strconv.Itoa(int(u.expected)) + ")"
  47. }
  48. // ParseError results from a malformed SSH message.
  49. type ParseError struct {
  50. msgType uint8
  51. }
  52. func (p ParseError) Error() string {
  53. return "ssh: parse error in message type " + strconv.Itoa(int(p.msgType))
  54. }
  55. type handshakeMagics struct {
  56. clientVersion, serverVersion []byte
  57. clientKexInit, serverKexInit []byte
  58. }
  59. func findCommonAlgorithm(clientAlgos []string, serverAlgos []string) (commonAlgo string, ok bool) {
  60. for _, clientAlgo := range clientAlgos {
  61. for _, serverAlgo := range serverAlgos {
  62. if clientAlgo == serverAlgo {
  63. return clientAlgo, true
  64. }
  65. }
  66. }
  67. return
  68. }
  69. func findAgreedAlgorithms(transport *transport, clientKexInit, serverKexInit *kexInitMsg) (kexAlgo, hostKeyAlgo string, ok bool) {
  70. kexAlgo, ok = findCommonAlgorithm(clientKexInit.KexAlgos, serverKexInit.KexAlgos)
  71. if !ok {
  72. return
  73. }
  74. hostKeyAlgo, ok = findCommonAlgorithm(clientKexInit.ServerHostKeyAlgos, serverKexInit.ServerHostKeyAlgos)
  75. if !ok {
  76. return
  77. }
  78. transport.writer.cipherAlgo, ok = findCommonAlgorithm(clientKexInit.CiphersClientServer, serverKexInit.CiphersClientServer)
  79. if !ok {
  80. return
  81. }
  82. transport.reader.cipherAlgo, ok = findCommonAlgorithm(clientKexInit.CiphersServerClient, serverKexInit.CiphersServerClient)
  83. if !ok {
  84. return
  85. }
  86. transport.writer.macAlgo, ok = findCommonAlgorithm(clientKexInit.MACsClientServer, serverKexInit.MACsClientServer)
  87. if !ok {
  88. return
  89. }
  90. transport.reader.macAlgo, ok = findCommonAlgorithm(clientKexInit.MACsServerClient, serverKexInit.MACsServerClient)
  91. if !ok {
  92. return
  93. }
  94. transport.writer.compressionAlgo, ok = findCommonAlgorithm(clientKexInit.CompressionClientServer, serverKexInit.CompressionClientServer)
  95. if !ok {
  96. return
  97. }
  98. transport.reader.compressionAlgo, ok = findCommonAlgorithm(clientKexInit.CompressionServerClient, serverKexInit.CompressionServerClient)
  99. if !ok {
  100. return
  101. }
  102. ok = true
  103. return
  104. }
  105. // Cryptographic configuration common to both ServerConfig and ClientConfig.
  106. type CryptoConfig struct {
  107. // The allowed cipher algorithms. If unspecified then DefaultCipherOrder is
  108. // used.
  109. Ciphers []string
  110. }
  111. func (c *CryptoConfig) ciphers() []string {
  112. if c.Ciphers == nil {
  113. return DefaultCipherOrder
  114. }
  115. return c.Ciphers
  116. }
  117. // serialize a signed slice according to RFC 4254 6.6.
  118. func serializeSignature(algoname string, sig []byte) []byte {
  119. length := stringLength([]byte(algoname))
  120. length += stringLength(sig)
  121. ret := make([]byte, length)
  122. r := marshalString(ret, []byte(algoname))
  123. r = marshalString(r, sig)
  124. return ret
  125. }
  126. // serialize a *rsa.PublicKey or *dsa.PublicKey according to RFC 4253 6.6.
  127. func serializePublickey(key interface{}) []byte {
  128. algoname := algoName(key)
  129. switch key := key.(type) {
  130. case *rsa.PublicKey:
  131. e := new(big.Int).SetInt64(int64(key.E))
  132. length := stringLength([]byte(algoname))
  133. length += intLength(e)
  134. length += intLength(key.N)
  135. ret := make([]byte, length)
  136. r := marshalString(ret, []byte(algoname))
  137. r = marshalInt(r, e)
  138. marshalInt(r, key.N)
  139. return ret
  140. case *dsa.PublicKey:
  141. length := stringLength([]byte(algoname))
  142. length += intLength(key.P)
  143. length += intLength(key.Q)
  144. length += intLength(key.G)
  145. length += intLength(key.Y)
  146. ret := make([]byte, length)
  147. r := marshalString(ret, []byte(algoname))
  148. r = marshalInt(r, key.P)
  149. r = marshalInt(r, key.Q)
  150. r = marshalInt(r, key.G)
  151. marshalInt(r, key.Y)
  152. return ret
  153. }
  154. panic("unexpected key type")
  155. }
  156. func algoName(key interface{}) string {
  157. switch key.(type) {
  158. case *rsa.PublicKey:
  159. return "ssh-rsa"
  160. case *dsa.PublicKey:
  161. return "ssh-dss"
  162. }
  163. panic("unexpected key type")
  164. }
  165. // buildDataSignedForAuth returns the data that is signed in order to prove
  166. // posession of a private key. See RFC 4252, section 7.
  167. func buildDataSignedForAuth(sessionId []byte, req userAuthRequestMsg, algo, pubKey []byte) []byte {
  168. user := []byte(req.User)
  169. service := []byte(req.Service)
  170. method := []byte(req.Method)
  171. length := stringLength(sessionId)
  172. length += 1
  173. length += stringLength(user)
  174. length += stringLength(service)
  175. length += stringLength(method)
  176. length += 1
  177. length += stringLength(algo)
  178. length += stringLength(pubKey)
  179. ret := make([]byte, length)
  180. r := marshalString(ret, sessionId)
  181. r[0] = msgUserAuthRequest
  182. r = r[1:]
  183. r = marshalString(r, user)
  184. r = marshalString(r, service)
  185. r = marshalString(r, method)
  186. r[0] = 1
  187. r = r[1:]
  188. r = marshalString(r, algo)
  189. r = marshalString(r, pubKey)
  190. return ret
  191. }
  192. // safeString sanitises s according to RFC 4251, section 9.2.
  193. // All control characters except tab, carriage return and newline are
  194. // replaced by 0x20.
  195. func safeString(s string) string {
  196. out := []byte(s)
  197. for i, c := range out {
  198. if c < 0x20 && c != 0xd && c != 0xa && c != 0x9 {
  199. out[i] = 0x20
  200. }
  201. }
  202. return string(out)
  203. }