common.go 6.8 KB

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