common.go 7.7 KB

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