common.go 7.2 KB

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