transport.py 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. # -*- test-case-name: twisted.conch.test.test_transport -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. The lowest level SSH protocol. This handles the key negotiation, the
  6. encryption and the compression. The transport layer is described in
  7. RFC 4253.
  8. Maintainer: Paul Swartz
  9. """
  10. from __future__ import absolute_import, division
  11. import binascii
  12. import hmac
  13. import struct
  14. import zlib
  15. from hashlib import md5, sha1, sha256, sha384, sha512
  16. from cryptography.exceptions import UnsupportedAlgorithm
  17. from cryptography.hazmat.backends import default_backend
  18. from cryptography.hazmat.primitives.ciphers import algorithms, modes, Cipher
  19. from cryptography.hazmat.primitives.asymmetric import ec
  20. from twisted.internet import protocol, defer
  21. from twisted.python import log, randbytes
  22. from twisted.python.compat import networkString, iterbytes, _bytesChr as chr
  23. # This import is needed if SHA256 hashing is used.
  24. #from twisted.python.compat import nativeString
  25. from twisted.conch.ssh import address, keys, _kex
  26. from twisted.conch.ssh.common import (
  27. NS, getNS, MP, getMP, _MPpow, ffs, int_from_bytes
  28. )
  29. def _getRandomNumber(random, bits):
  30. """
  31. Generate a random number in the range [0, 2 ** bits).
  32. @type random: L{callable}
  33. @param random: A callable taking a count of bytes and returning that many
  34. random bytes.
  35. @type bits: L{int}
  36. @param bits: The number of bits in the result.
  37. @rtype: L{int} or L{long}
  38. @return: The newly generated random number.
  39. @raise ValueError: if C{bits} is not a multiple of 8.
  40. """
  41. if bits % 8:
  42. raise ValueError("bits (%d) must be a multiple of 8" % (bits,))
  43. return int_from_bytes(random(bits // 8), 'big')
  44. def _generateX(random, bits):
  45. """
  46. Generate a new value for the private key x.
  47. From RFC 2631, section 2.2::
  48. X9.42 requires that the private key x be in the interval
  49. [2, (q - 2)]. x should be randomly generated in this interval.
  50. @type random: L{callable}
  51. @param random: A callable taking a count of bytes and returning that many
  52. random bytes.
  53. @type bits: L{int}
  54. @param bits: The size of the key to generate, in bits.
  55. @rtype: L{int}
  56. @return: A suitable 'x' value.
  57. """
  58. while True:
  59. x = _getRandomNumber(random, bits)
  60. if 2 <= x <= (2 ** bits) - 2:
  61. return x
  62. class _MACParams(tuple):
  63. """
  64. L{_MACParams} represents the parameters necessary to compute SSH MAC
  65. (Message Authenticate Codes).
  66. L{_MACParams} is a L{tuple} subclass to maintain compatibility with older
  67. versions of the code. The elements of a L{_MACParams} are::
  68. 0. The digest object used for the MAC
  69. 1. The inner pad ("ipad") string
  70. 2. The outer pad ("opad") string
  71. 3. The size of the digest produced by the digest object
  72. L{_MACParams} is also an object lesson in why tuples are a bad type for
  73. public APIs.
  74. @ivar key: The HMAC key which will be used.
  75. """
  76. class SSHCiphers:
  77. """
  78. SSHCiphers represents all the encryption operations that need to occur
  79. to encrypt and authenticate the SSH connection.
  80. @cvar cipherMap: A dictionary mapping SSH encryption names to 3-tuples of
  81. (<cryptography.hazmat.primitives.interfaces.CipherAlgorithm>,
  82. <block size>, <cryptography.hazmat.primitives.interfaces.Mode>)
  83. @cvar macMap: A dictionary mapping SSH MAC names to hash modules.
  84. @ivar outCipType: the string type of the outgoing cipher.
  85. @ivar inCipType: the string type of the incoming cipher.
  86. @ivar outMACType: the string type of the incoming MAC.
  87. @ivar inMACType: the string type of the incoming MAC.
  88. @ivar encBlockSize: the block size of the outgoing cipher.
  89. @ivar decBlockSize: the block size of the incoming cipher.
  90. @ivar verifyDigestSize: the size of the incoming MAC.
  91. @ivar outMAC: a tuple of (<hash module>, <inner key>, <outer key>,
  92. <digest size>) representing the outgoing MAC.
  93. @ivar inMAc: see outMAC, but for the incoming MAC.
  94. """
  95. cipherMap = {
  96. b'3des-cbc': (algorithms.TripleDES, 24, modes.CBC),
  97. b'blowfish-cbc': (algorithms.Blowfish, 16, modes.CBC),
  98. b'aes256-cbc': (algorithms.AES, 32, modes.CBC),
  99. b'aes192-cbc': (algorithms.AES, 24, modes.CBC),
  100. b'aes128-cbc': (algorithms.AES, 16, modes.CBC),
  101. b'cast128-cbc': (algorithms.CAST5, 16, modes.CBC),
  102. b'aes128-ctr': (algorithms.AES, 16, modes.CTR),
  103. b'aes192-ctr': (algorithms.AES, 24, modes.CTR),
  104. b'aes256-ctr': (algorithms.AES, 32, modes.CTR),
  105. b'3des-ctr': (algorithms.TripleDES, 24, modes.CTR),
  106. b'blowfish-ctr': (algorithms.Blowfish, 16, modes.CTR),
  107. b'cast128-ctr': (algorithms.CAST5, 16, modes.CTR),
  108. b'none': (None, 0, modes.CBC),
  109. }
  110. macMap = {
  111. b'hmac-sha2-512': sha512,
  112. b'hmac-sha2-384': sha384,
  113. b'hmac-sha2-256': sha256,
  114. b'hmac-sha1': sha1,
  115. b'hmac-md5': md5,
  116. b'none': None
  117. }
  118. def __init__(self, outCip, inCip, outMac, inMac):
  119. self.outCipType = outCip
  120. self.inCipType = inCip
  121. self.outMACType = outMac
  122. self.inMACType = inMac
  123. self.encBlockSize = 0
  124. self.decBlockSize = 0
  125. self.verifyDigestSize = 0
  126. self.outMAC = (None, b'', b'', 0)
  127. self.inMAC = (None, b'', b'', 0)
  128. def setKeys(self, outIV, outKey, inIV, inKey, outInteg, inInteg):
  129. """
  130. Set up the ciphers and hashes using the given keys,
  131. @param outIV: the outgoing initialization vector
  132. @param outKey: the outgoing encryption key
  133. @param inIV: the incoming initialization vector
  134. @param inKey: the incoming encryption key
  135. @param outInteg: the outgoing integrity key
  136. @param inInteg: the incoming integrity key.
  137. """
  138. o = self._getCipher(self.outCipType, outIV, outKey)
  139. self.encryptor = o.encryptor()
  140. self.encBlockSize = o.algorithm.block_size // 8
  141. o = self._getCipher(self.inCipType, inIV, inKey)
  142. self.decryptor = o.decryptor()
  143. self.decBlockSize = o.algorithm.block_size // 8
  144. self.outMAC = self._getMAC(self.outMACType, outInteg)
  145. self.inMAC = self._getMAC(self.inMACType, inInteg)
  146. if self.inMAC:
  147. self.verifyDigestSize = self.inMAC[3]
  148. def _getCipher(self, cip, iv, key):
  149. """
  150. Creates an initialized cipher object.
  151. @param cip: the name of the cipher, maps into cipherMap
  152. @param iv: the initialzation vector
  153. @param key: the encryption key
  154. @return: the cipher object.
  155. """
  156. algorithmClass, keySize, modeClass = self.cipherMap[cip]
  157. if algorithmClass is None:
  158. return _DummyCipher()
  159. return Cipher(
  160. algorithmClass(key[:keySize]),
  161. modeClass(iv[:algorithmClass.block_size // 8]),
  162. backend=default_backend(),
  163. )
  164. def _getMAC(self, mac, key):
  165. """
  166. Gets a 4-tuple representing the message authentication code.
  167. (<hash module>, <inner hash value>, <outer hash value>,
  168. <digest size>)
  169. @type mac: L{bytes}
  170. @param mac: a key mapping into macMap
  171. @type key: L{bytes}
  172. @param key: the MAC key.
  173. @rtype: L{bytes}
  174. @return: The MAC components.
  175. """
  176. mod = self.macMap[mac]
  177. if not mod:
  178. return (None, b'', b'', 0)
  179. # With stdlib we can only get attributes fron an instantiated object.
  180. hashObject = mod()
  181. digestSize = hashObject.digest_size
  182. blockSize = hashObject.block_size
  183. # Truncation here appears to contravene RFC 2104, section 2. However,
  184. # implementing the hashing behavior prescribed by the RFC breaks
  185. # interoperability with OpenSSH (at least version 5.5p1).
  186. key = key[:digestSize] + (b'\x00' * (blockSize - digestSize))
  187. i = key.translate(hmac.trans_36)
  188. o = key.translate(hmac.trans_5C)
  189. result = _MACParams((mod, i, o, digestSize))
  190. result.key = key
  191. return result
  192. def encrypt(self, blocks):
  193. """
  194. Encrypt some data.
  195. @type blocks: L{bytes}
  196. @param blocks: The data to encrypt.
  197. @rtype: L{bytes}
  198. @return: The encrypted data.
  199. """
  200. return self.encryptor.update(blocks)
  201. def decrypt(self, blocks):
  202. """
  203. Decrypt some data.
  204. @type blocks: L{bytes}
  205. @param blocks: The data to decrypt.
  206. @rtype: L{bytes}
  207. @return: The decrypted data.
  208. """
  209. return self.decryptor.update(blocks)
  210. def makeMAC(self, seqid, data):
  211. """
  212. Create a message authentication code (MAC) for the given packet using
  213. the outgoing MAC values.
  214. @type seqid: L{int}
  215. @param seqid: The sequence ID of the outgoing packet.
  216. @type data: L{bytes}
  217. @param data: The data to create a MAC for.
  218. @rtype: L{str}
  219. @return: The serialized MAC.
  220. """
  221. if not self.outMAC[0]:
  222. return b''
  223. data = struct.pack('>L', seqid) + data
  224. return hmac.HMAC(self.outMAC.key, data, self.outMAC[0]).digest()
  225. def verify(self, seqid, data, mac):
  226. """
  227. Verify an incoming MAC using the incoming MAC values.
  228. @type seqid: L{int}
  229. @param seqid: The sequence ID of the incoming packet.
  230. @type data: L{bytes}
  231. @param data: The packet data to verify.
  232. @type mac: L{bytes}
  233. @param mac: The MAC sent with the packet.
  234. @rtype: L{bool}
  235. @return: C{True} if the MAC is valid.
  236. """
  237. if not self.inMAC[0]:
  238. return mac == b''
  239. data = struct.pack('>L', seqid) + data
  240. outer = hmac.HMAC(self.inMAC.key, data, self.inMAC[0]).digest()
  241. return mac == outer
  242. def _getSupportedCiphers():
  243. """
  244. Build a list of ciphers that are supported by the backend in use.
  245. @return: a list of supported ciphers.
  246. @rtype: L{list} of L{str}
  247. """
  248. supportedCiphers = []
  249. cs = [b'aes256-ctr', b'aes256-cbc', b'aes192-ctr', b'aes192-cbc',
  250. b'aes128-ctr', b'aes128-cbc', b'cast128-ctr', b'cast128-cbc',
  251. b'blowfish-ctr', b'blowfish-cbc', b'3des-ctr', b'3des-cbc']
  252. for cipher in cs:
  253. algorithmClass, keySize, modeClass = SSHCiphers.cipherMap[cipher]
  254. try:
  255. Cipher(
  256. algorithmClass(b' ' * keySize),
  257. modeClass(b' ' * (algorithmClass.block_size // 8)),
  258. backend=default_backend(),
  259. ).encryptor()
  260. except UnsupportedAlgorithm:
  261. pass
  262. else:
  263. supportedCiphers.append(cipher)
  264. return supportedCiphers
  265. class SSHTransportBase(protocol.Protocol):
  266. """
  267. Protocol supporting basic SSH functionality: sending/receiving packets
  268. and message dispatch. To connect to or run a server, you must use
  269. SSHClientTransport or SSHServerTransport.
  270. @ivar protocolVersion: A string representing the version of the SSH
  271. protocol we support. Currently defaults to '2.0'.
  272. @ivar version: A string representing the version of the server or client.
  273. Currently defaults to 'Twisted'.
  274. @ivar comment: An optional string giving more information about the
  275. server or client.
  276. @ivar supportedCiphers: A list of strings representing the encryption
  277. algorithms supported, in order from most-preferred to least.
  278. @ivar supportedMACs: A list of strings representing the message
  279. authentication codes (hashes) supported, in order from most-preferred
  280. to least. Both this and supportedCiphers can include 'none' to use
  281. no encryption or authentication, but that must be done manually,
  282. @ivar supportedKeyExchanges: A list of strings representing the
  283. key exchanges supported, in order from most-preferred to least.
  284. @ivar supportedPublicKeys: A list of strings representing the
  285. public key types supported, in order from most-preferred to least.
  286. @ivar supportedCompressions: A list of strings representing compression
  287. types supported, from most-preferred to least.
  288. @ivar supportedLanguages: A list of strings representing languages
  289. supported, from most-preferred to least.
  290. @ivar supportedVersions: A container of strings representing supported ssh
  291. protocol version numbers.
  292. @ivar isClient: A boolean indicating whether this is a client or server.
  293. @ivar gotVersion: A boolean indicating whether we have received the
  294. version string from the other side.
  295. @ivar buf: Data we've received but hasn't been parsed into a packet.
  296. @ivar outgoingPacketSequence: the sequence number of the next packet we
  297. will send.
  298. @ivar incomingPacketSequence: the sequence number of the next packet we
  299. are expecting from the other side.
  300. @ivar outgoingCompression: an object supporting the .compress(str) and
  301. .flush() methods, or None if there is no outgoing compression. Used to
  302. compress outgoing data.
  303. @ivar outgoingCompressionType: A string representing the outgoing
  304. compression type.
  305. @ivar incomingCompression: an object supporting the .decompress(str)
  306. method, or None if there is no incoming compression. Used to
  307. decompress incoming data.
  308. @ivar incomingCompressionType: A string representing the incoming
  309. compression type.
  310. @ivar ourVersionString: the version string that we sent to the other side.
  311. Used in the key exchange.
  312. @ivar otherVersionString: the version string sent by the other side. Used
  313. in the key exchange.
  314. @ivar ourKexInitPayload: the MSG_KEXINIT payload we sent. Used in the key
  315. exchange.
  316. @ivar otherKexInitPayload: the MSG_KEXINIT payload we received. Used in
  317. the key exchange
  318. @ivar sessionID: a string that is unique to this SSH session. Created as
  319. part of the key exchange, sessionID is used to generate the various
  320. encryption and authentication keys.
  321. @ivar service: an SSHService instance, or None. If it's set to an object,
  322. it's the currently running service.
  323. @ivar kexAlg: the agreed-upon key exchange algorithm.
  324. @ivar keyAlg: the agreed-upon public key type for the key exchange.
  325. @ivar currentEncryptions: an SSHCiphers instance. It represents the
  326. current encryption and authentication options for the transport.
  327. @ivar nextEncryptions: an SSHCiphers instance. Held here until the
  328. MSG_NEWKEYS messages are exchanged, when nextEncryptions is
  329. transitioned to currentEncryptions.
  330. @ivar first: the first bytes of the next packet. In order to avoid
  331. decrypting data twice, the first bytes are decrypted and stored until
  332. the whole packet is available.
  333. @ivar _keyExchangeState: The current protocol state with respect to key
  334. exchange. This is either C{_KEY_EXCHANGE_NONE} if no key exchange is
  335. in progress (and returns to this value after any key exchange
  336. completqes), C{_KEY_EXCHANGE_REQUESTED} if this side of the connection
  337. initiated a key exchange, and C{_KEY_EXCHANGE_PROGRESSING} if the other
  338. side of the connection initiated a key exchange. C{_KEY_EXCHANGE_NONE}
  339. is the initial value (however SSH connections begin with key exchange,
  340. so it will quickly change to another state).
  341. @ivar _blockedByKeyExchange: Whenever C{_keyExchangeState} is not
  342. C{_KEY_EXCHANGE_NONE}, this is a C{list} of pending messages which were
  343. passed to L{sendPacket} but could not be sent because it is not legal
  344. to send them while a key exchange is in progress. When the key
  345. exchange completes, another attempt is made to send these messages.
  346. """
  347. protocolVersion = b'2.0'
  348. version = b'Twisted'
  349. comment = b''
  350. ourVersionString = (b'SSH-' + protocolVersion + b'-' + version + b' '
  351. + comment).strip()
  352. # L{None} is supported as cipher and hmac. For security they are disabled
  353. # by default. To enable them, subclass this class and add it, or do:
  354. # SSHTransportBase.supportedCiphers.append('none')
  355. # List ordered by preference.
  356. supportedCiphers = _getSupportedCiphers()
  357. supportedMACs = [
  358. b'hmac-sha2-512',
  359. b'hmac-sha2-384',
  360. b'hmac-sha2-256',
  361. b'hmac-sha1',
  362. b'hmac-md5',
  363. # `none`,
  364. ]
  365. supportedKeyExchanges = _kex.getSupportedKeyExchanges()
  366. supportedPublicKeys = []
  367. # Add the supported EC keys, and change the name from ecdh* to ecdsa*
  368. for eckey in supportedKeyExchanges:
  369. if eckey.find(b'ecdh') != -1:
  370. supportedPublicKeys += [eckey.replace(b'ecdh', b'ecdsa')]
  371. supportedPublicKeys += [b'ssh-rsa', b'ssh-dss']
  372. supportedCompressions = [b'none', b'zlib']
  373. supportedLanguages = ()
  374. supportedVersions = (b'1.99', b'2.0')
  375. isClient = False
  376. gotVersion = False
  377. buf = b''
  378. outgoingPacketSequence = 0
  379. incomingPacketSequence = 0
  380. outgoingCompression = None
  381. incomingCompression = None
  382. sessionID = None
  383. service = None
  384. # There is no key exchange activity in progress.
  385. _KEY_EXCHANGE_NONE = '_KEY_EXCHANGE_NONE'
  386. # Key exchange is in progress and we started it.
  387. _KEY_EXCHANGE_REQUESTED = '_KEY_EXCHANGE_REQUESTED'
  388. # Key exchange is in progress and both sides have sent KEXINIT messages.
  389. _KEY_EXCHANGE_PROGRESSING = '_KEY_EXCHANGE_PROGRESSING'
  390. # There is a fourth conceptual state not represented here: KEXINIT received
  391. # but not sent. Since we always send a KEXINIT as soon as we get it, we
  392. # can't ever be in that state.
  393. # The current key exchange state.
  394. _keyExchangeState = _KEY_EXCHANGE_NONE
  395. _blockedByKeyExchange = None
  396. def connectionLost(self, reason):
  397. """
  398. When the underlying connection is closed, stop the running service (if
  399. any), and log out the avatar (if any).
  400. @type reason: L{twisted.python.failure.Failure}
  401. @param reason: The cause of the connection being closed.
  402. """
  403. if self.service:
  404. self.service.serviceStopped()
  405. if hasattr(self, 'avatar'):
  406. self.logoutFunction()
  407. log.msg('connection lost')
  408. def connectionMade(self):
  409. """
  410. Called when the connection is made to the other side. We sent our
  411. version and the MSG_KEXINIT packet.
  412. """
  413. self.transport.write(self.ourVersionString + b'\r\n')
  414. self.currentEncryptions = SSHCiphers(b'none', b'none', b'none',
  415. b'none')
  416. self.currentEncryptions.setKeys(b'', b'', b'', b'', b'', b'')
  417. self.sendKexInit()
  418. def sendKexInit(self):
  419. """
  420. Send a I{KEXINIT} message to initiate key exchange or to respond to a
  421. key exchange initiated by the peer.
  422. @raise RuntimeError: If a key exchange has already been started and it
  423. is not appropriate to send a I{KEXINIT} message at this time.
  424. @return: L{None}
  425. """
  426. if self._keyExchangeState != self._KEY_EXCHANGE_NONE:
  427. raise RuntimeError(
  428. "Cannot send KEXINIT while key exchange state is %r" % (
  429. self._keyExchangeState,))
  430. self.ourKexInitPayload = b''.join([
  431. chr(MSG_KEXINIT),
  432. randbytes.secureRandom(16),
  433. NS(b','.join(self.supportedKeyExchanges)),
  434. NS(b','.join(self.supportedPublicKeys)),
  435. NS(b','.join(self.supportedCiphers)),
  436. NS(b','.join(self.supportedCiphers)),
  437. NS(b','.join(self.supportedMACs)),
  438. NS(b','.join(self.supportedMACs)),
  439. NS(b','.join(self.supportedCompressions)),
  440. NS(b','.join(self.supportedCompressions)),
  441. NS(b','.join(self.supportedLanguages)),
  442. NS(b','.join(self.supportedLanguages)),
  443. b'\000\000\000\000\000'])
  444. self.sendPacket(MSG_KEXINIT, self.ourKexInitPayload[1:])
  445. self._keyExchangeState = self._KEY_EXCHANGE_REQUESTED
  446. self._blockedByKeyExchange = []
  447. def _allowedKeyExchangeMessageType(self, messageType):
  448. """
  449. Determine if the given message type may be sent while key exchange is
  450. in progress.
  451. @param messageType: The type of message
  452. @type messageType: L{int}
  453. @return: C{True} if the given type of message may be sent while key
  454. exchange is in progress, C{False} if it may not.
  455. @rtype: L{bool}
  456. @see: U{http://tools.ietf.org/html/rfc4253#section-7.1}
  457. """
  458. # Written somewhat peculularly to reflect the way the specification
  459. # defines the allowed message types.
  460. if 1 <= messageType <= 19:
  461. return messageType not in (MSG_SERVICE_REQUEST, MSG_SERVICE_ACCEPT)
  462. if 20 <= messageType <= 29:
  463. return messageType not in (MSG_KEXINIT,)
  464. return 30 <= messageType <= 49
  465. def sendPacket(self, messageType, payload):
  466. """
  467. Sends a packet. If it's been set up, compress the data, encrypt it,
  468. and authenticate it before sending. If key exchange is in progress and
  469. the message is not part of key exchange, queue it to be sent later.
  470. @param messageType: The type of the packet; generally one of the
  471. MSG_* values.
  472. @type messageType: L{int}
  473. @param payload: The payload for the message.
  474. @type payload: L{str}
  475. """
  476. if self._keyExchangeState != self._KEY_EXCHANGE_NONE:
  477. if not self._allowedKeyExchangeMessageType(messageType):
  478. self._blockedByKeyExchange.append((messageType, payload))
  479. return
  480. payload = chr(messageType) + payload
  481. if self.outgoingCompression:
  482. payload = (self.outgoingCompression.compress(payload)
  483. + self.outgoingCompression.flush(2))
  484. bs = self.currentEncryptions.encBlockSize
  485. # 4 for the packet length and 1 for the padding length
  486. totalSize = 5 + len(payload)
  487. lenPad = bs - (totalSize % bs)
  488. if lenPad < 4:
  489. lenPad = lenPad + bs
  490. packet = (struct.pack('!LB',
  491. totalSize + lenPad - 4, lenPad) +
  492. payload + randbytes.secureRandom(lenPad))
  493. encPacket = (
  494. self.currentEncryptions.encrypt(packet) +
  495. self.currentEncryptions.makeMAC(
  496. self.outgoingPacketSequence, packet))
  497. self.transport.write(encPacket)
  498. self.outgoingPacketSequence += 1
  499. def getPacket(self):
  500. """
  501. Try to return a decrypted, authenticated, and decompressed packet
  502. out of the buffer. If there is not enough data, return None.
  503. @rtype: L{str} or L{None}
  504. @return: The decoded packet, if any.
  505. """
  506. bs = self.currentEncryptions.decBlockSize
  507. ms = self.currentEncryptions.verifyDigestSize
  508. if len(self.buf) < bs:
  509. # Not enough data for a block
  510. return
  511. if not hasattr(self, 'first'):
  512. first = self.currentEncryptions.decrypt(self.buf[:bs])
  513. else:
  514. first = self.first
  515. del self.first
  516. packetLen, paddingLen = struct.unpack('!LB', first[:5])
  517. if packetLen > 1048576: # 1024 ** 2
  518. self.sendDisconnect(
  519. DISCONNECT_PROTOCOL_ERROR,
  520. networkString('bad packet length %s' % (packetLen,)))
  521. return
  522. if len(self.buf) < packetLen + 4 + ms:
  523. # Not enough data for a packet
  524. self.first = first
  525. return
  526. if (packetLen + 4) % bs != 0:
  527. self.sendDisconnect(
  528. DISCONNECT_PROTOCOL_ERROR,
  529. networkString(
  530. 'bad packet mod (%i%%%i == %i)' % (
  531. packetLen + 4, bs,(packetLen + 4) % bs)))
  532. return
  533. encData, self.buf = self.buf[:4 + packetLen], self.buf[4 + packetLen:]
  534. packet = first + self.currentEncryptions.decrypt(encData[bs:])
  535. if len(packet) != 4 + packetLen:
  536. self.sendDisconnect(DISCONNECT_PROTOCOL_ERROR,
  537. b'bad decryption')
  538. return
  539. if ms:
  540. macData, self.buf = self.buf[:ms], self.buf[ms:]
  541. if not self.currentEncryptions.verify(self.incomingPacketSequence,
  542. packet, macData):
  543. self.sendDisconnect(DISCONNECT_MAC_ERROR, b'bad MAC')
  544. return
  545. payload = packet[5:-paddingLen]
  546. if self.incomingCompression:
  547. try:
  548. payload = self.incomingCompression.decompress(payload)
  549. except:
  550. # Tolerate any errors in decompression
  551. log.err()
  552. self.sendDisconnect(DISCONNECT_COMPRESSION_ERROR,
  553. b'compression error')
  554. return
  555. self.incomingPacketSequence += 1
  556. return payload
  557. def _unsupportedVersionReceived(self, remoteVersion):
  558. """
  559. Called when an unsupported version of the ssh protocol is received from
  560. the remote endpoint.
  561. @param remoteVersion: remote ssh protocol version which is unsupported
  562. by us.
  563. @type remoteVersion: L{str}
  564. """
  565. self.sendDisconnect(DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
  566. b'bad version ' + remoteVersion)
  567. def dataReceived(self, data):
  568. """
  569. First, check for the version string (SSH-2.0-*). After that has been
  570. received, this method adds data to the buffer, and pulls out any
  571. packets.
  572. @type data: L{bytes}
  573. @param data: The data that was received.
  574. """
  575. self.buf = self.buf + data
  576. if not self.gotVersion:
  577. if self.buf.find(b'\n', self.buf.find(b'SSH-')) == -1:
  578. return
  579. lines = self.buf.split(b'\n')
  580. for p in lines:
  581. if p.startswith(b'SSH-'):
  582. self.gotVersion = True
  583. self.otherVersionString = p.strip()
  584. remoteVersion = p.split(b'-')[1]
  585. if remoteVersion not in self.supportedVersions:
  586. self._unsupportedVersionReceived(remoteVersion)
  587. return
  588. i = lines.index(p)
  589. self.buf = b'\n'.join(lines[i + 1:])
  590. packet = self.getPacket()
  591. while packet:
  592. messageNum = ord(packet[0:1])
  593. self.dispatchMessage(messageNum, packet[1:])
  594. packet = self.getPacket()
  595. def dispatchMessage(self, messageNum, payload):
  596. """
  597. Send a received message to the appropriate method.
  598. @type messageNum: L{int}
  599. @param messageNum: The message number.
  600. @type payload: L{bytes}
  601. @param payload: The message payload.
  602. """
  603. if messageNum < 50 and messageNum in messages:
  604. messageType = messages[messageNum][4:]
  605. f = getattr(self, 'ssh_%s' % (messageType,), None)
  606. if f is not None:
  607. f(payload)
  608. else:
  609. log.msg("couldn't handle %s" % messageType)
  610. log.msg(repr(payload))
  611. self.sendUnimplemented()
  612. elif self.service:
  613. log.callWithLogger(self.service, self.service.packetReceived,
  614. messageNum, payload)
  615. else:
  616. log.msg("couldn't handle %s" % messageNum)
  617. log.msg(repr(payload))
  618. self.sendUnimplemented()
  619. def getPeer(self):
  620. """
  621. Returns an L{SSHTransportAddress} corresponding to the other (peer)
  622. side of this transport.
  623. @return: L{SSHTransportAddress} for the peer
  624. @rtype: L{SSHTransportAddress}
  625. @since: 12.1
  626. """
  627. return address.SSHTransportAddress(self.transport.getPeer())
  628. def getHost(self):
  629. """
  630. Returns an L{SSHTransportAddress} corresponding to the this side of
  631. transport.
  632. @return: L{SSHTransportAddress} for the peer
  633. @rtype: L{SSHTransportAddress}
  634. @since: 12.1
  635. """
  636. return address.SSHTransportAddress(self.transport.getHost())
  637. @property
  638. def kexAlg(self):
  639. """
  640. The key exchange algorithm name agreed between client and server.
  641. """
  642. return self._kexAlg
  643. @kexAlg.setter
  644. def kexAlg(self, value):
  645. """
  646. Set the key exchange algorithm name.
  647. """
  648. self._kexAlg = value
  649. # Client-initiated rekeying looks like this:
  650. #
  651. # C> MSG_KEXINIT
  652. # S> MSG_KEXINIT
  653. # C> MSG_KEX_DH_GEX_REQUEST or MSG_KEXDH_INIT
  654. # S> MSG_KEX_DH_GEX_GROUP or MSG_KEXDH_REPLY
  655. # C> MSG_KEX_DH_GEX_INIT or --
  656. # S> MSG_KEX_DH_GEX_REPLY or --
  657. # C> MSG_NEWKEYS
  658. # S> MSG_NEWKEYS
  659. #
  660. # Server-initiated rekeying is the same, only the first two messages are
  661. # switched.
  662. def ssh_KEXINIT(self, packet):
  663. """
  664. Called when we receive a MSG_KEXINIT message. Payload::
  665. bytes[16] cookie
  666. string keyExchangeAlgorithms
  667. string keyAlgorithms
  668. string incomingEncryptions
  669. string outgoingEncryptions
  670. string incomingAuthentications
  671. string outgoingAuthentications
  672. string incomingCompressions
  673. string outgoingCompressions
  674. string incomingLanguages
  675. string outgoingLanguages
  676. bool firstPacketFollows
  677. unit32 0 (reserved)
  678. Starts setting up the key exchange, keys, encryptions, and
  679. authentications. Extended by ssh_KEXINIT in SSHServerTransport and
  680. SSHClientTransport.
  681. @type packet: L{bytes}
  682. @param packet: The message data.
  683. @return: A L{tuple} of negotiated key exchange algorithms, key
  684. algorithms, and unhandled data, or L{None} if something went wrong.
  685. """
  686. self.otherKexInitPayload = chr(MSG_KEXINIT) + packet
  687. # This is useless to us:
  688. # cookie = packet[: 16]
  689. k = getNS(packet[16:], 10)
  690. strings, rest = k[:-1], k[-1]
  691. (kexAlgs, keyAlgs, encCS, encSC, macCS, macSC, compCS, compSC, langCS,
  692. langSC) = [s.split(b',') for s in strings]
  693. # These are the server directions
  694. outs = [encSC, macSC, compSC]
  695. ins = [encCS, macSC, compCS]
  696. if self.isClient:
  697. outs, ins = ins, outs # Switch directions
  698. server = (self.supportedKeyExchanges, self.supportedPublicKeys,
  699. self.supportedCiphers, self.supportedCiphers,
  700. self.supportedMACs, self.supportedMACs,
  701. self.supportedCompressions, self.supportedCompressions)
  702. client = (kexAlgs, keyAlgs, outs[0], ins[0], outs[1], ins[1],
  703. outs[2], ins[2])
  704. if self.isClient:
  705. server, client = client, server
  706. self.kexAlg = ffs(client[0], server[0])
  707. self.keyAlg = ffs(client[1], server[1])
  708. self.nextEncryptions = SSHCiphers(
  709. ffs(client[2], server[2]),
  710. ffs(client[3], server[3]),
  711. ffs(client[4], server[4]),
  712. ffs(client[5], server[5]))
  713. self.outgoingCompressionType = ffs(client[6], server[6])
  714. self.incomingCompressionType = ffs(client[7], server[7])
  715. if None in (self.kexAlg, self.keyAlg, self.outgoingCompressionType,
  716. self.incomingCompressionType):
  717. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  718. b"couldn't match all kex parts")
  719. return
  720. if None in self.nextEncryptions.__dict__.values():
  721. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  722. b"couldn't match all kex parts")
  723. return
  724. log.msg('kex alg, key alg: %r %r' % (self.kexAlg, self.keyAlg))
  725. log.msg('outgoing: %r %r %r' % (self.nextEncryptions.outCipType,
  726. self.nextEncryptions.outMACType,
  727. self.outgoingCompressionType))
  728. log.msg('incoming: %r %r %r' % (self.nextEncryptions.inCipType,
  729. self.nextEncryptions.inMACType,
  730. self.incomingCompressionType))
  731. if self._keyExchangeState == self._KEY_EXCHANGE_REQUESTED:
  732. self._keyExchangeState = self._KEY_EXCHANGE_PROGRESSING
  733. else:
  734. self.sendKexInit()
  735. return kexAlgs, keyAlgs, rest # For SSHServerTransport to use
  736. def ssh_DISCONNECT(self, packet):
  737. """
  738. Called when we receive a MSG_DISCONNECT message. Payload::
  739. long code
  740. string description
  741. This means that the other side has disconnected. Pass the message up
  742. and disconnect ourselves.
  743. @type packet: L{bytes}
  744. @param packet: The message data.
  745. """
  746. reasonCode = struct.unpack('>L', packet[: 4])[0]
  747. description, foo = getNS(packet[4:])
  748. self.receiveError(reasonCode, description)
  749. self.transport.loseConnection()
  750. def ssh_IGNORE(self, packet):
  751. """
  752. Called when we receive a MSG_IGNORE message. No payload.
  753. This means nothing; we simply return.
  754. @type packet: L{bytes}
  755. @param packet: The message data.
  756. """
  757. def ssh_UNIMPLEMENTED(self, packet):
  758. """
  759. Called when we receive a MSG_UNIMPLEMENTED message. Payload::
  760. long packet
  761. This means that the other side did not implement one of our packets.
  762. @type packet: L{bytes}
  763. @param packet: The message data.
  764. """
  765. seqnum, = struct.unpack('>L', packet)
  766. self.receiveUnimplemented(seqnum)
  767. def ssh_DEBUG(self, packet):
  768. """
  769. Called when we receive a MSG_DEBUG message. Payload::
  770. bool alwaysDisplay
  771. string message
  772. string language
  773. This means the other side has passed along some debugging info.
  774. @type packet: L{bytes}
  775. @param packet: The message data.
  776. """
  777. alwaysDisplay = bool(packet[0])
  778. message, lang, foo = getNS(packet[1:], 2)
  779. self.receiveDebug(alwaysDisplay, message, lang)
  780. def setService(self, service):
  781. """
  782. Set our service to service and start it running. If we were
  783. running a service previously, stop it first.
  784. @type service: C{SSHService}
  785. @param service: The service to attach.
  786. """
  787. log.msg('starting service %r' % (service.name,))
  788. if self.service:
  789. self.service.serviceStopped()
  790. self.service = service
  791. service.transport = self
  792. self.service.serviceStarted()
  793. def sendDebug(self, message, alwaysDisplay=False, language=b''):
  794. """
  795. Send a debug message to the other side.
  796. @param message: the message to send.
  797. @type message: L{str}
  798. @param alwaysDisplay: if True, tell the other side to always
  799. display this message.
  800. @type alwaysDisplay: L{bool}
  801. @param language: optionally, the language the message is in.
  802. @type language: L{str}
  803. """
  804. self.sendPacket(MSG_DEBUG, chr(alwaysDisplay) + NS(message) +
  805. NS(language))
  806. def sendIgnore(self, message):
  807. """
  808. Send a message that will be ignored by the other side. This is
  809. useful to fool attacks based on guessing packet sizes in the
  810. encrypted stream.
  811. @param message: data to send with the message
  812. @type message: L{str}
  813. """
  814. self.sendPacket(MSG_IGNORE, NS(message))
  815. def sendUnimplemented(self):
  816. """
  817. Send a message to the other side that the last packet was not
  818. understood.
  819. """
  820. seqnum = self.incomingPacketSequence
  821. self.sendPacket(MSG_UNIMPLEMENTED, struct.pack('!L', seqnum))
  822. def sendDisconnect(self, reason, desc):
  823. """
  824. Send a disconnect message to the other side and then disconnect.
  825. @param reason: the reason for the disconnect. Should be one of the
  826. DISCONNECT_* values.
  827. @type reason: L{int}
  828. @param desc: a descrption of the reason for the disconnection.
  829. @type desc: L{str}
  830. """
  831. self.sendPacket(
  832. MSG_DISCONNECT, struct.pack('>L', reason) + NS(desc) + NS(b''))
  833. log.msg('Disconnecting with error, code %s\nreason: %s' % (reason,
  834. desc))
  835. self.transport.loseConnection()
  836. def _getKey(self, c, sharedSecret, exchangeHash):
  837. """
  838. Get one of the keys for authentication/encryption.
  839. @type c: L{bytes}
  840. @param c: The letter identifying which key this is.
  841. @type sharedSecret: L{bytes}
  842. @param sharedSecret: The shared secret K.
  843. @type exchangeHash: L{bytes}
  844. @param exchangeHash: The hash H from key exchange.
  845. @rtype: L{bytes}
  846. @return: The derived key.
  847. """
  848. hashProcessor = _kex.getHashProcessor(self.kexAlg)
  849. k1 = hashProcessor(sharedSecret + exchangeHash + c + self.sessionID)
  850. k1 = k1.digest()
  851. k2 = hashProcessor(sharedSecret + exchangeHash + k1).digest()
  852. return k1 + k2
  853. def _keySetup(self, sharedSecret, exchangeHash):
  854. """
  855. Set up the keys for the connection and sends MSG_NEWKEYS when
  856. finished,
  857. @param sharedSecret: a secret string agreed upon using a Diffie-
  858. Hellman exchange, so it is only shared between
  859. the server and the client.
  860. @type sharedSecret: L{str}
  861. @param exchangeHash: A hash of various data known by both sides.
  862. @type exchangeHash: L{str}
  863. """
  864. if not self.sessionID:
  865. self.sessionID = exchangeHash
  866. initIVCS = self._getKey(b'A', sharedSecret, exchangeHash)
  867. initIVSC = self._getKey(b'B', sharedSecret, exchangeHash)
  868. encKeyCS = self._getKey(b'C', sharedSecret, exchangeHash)
  869. encKeySC = self._getKey(b'D', sharedSecret, exchangeHash)
  870. integKeyCS = self._getKey(b'E', sharedSecret, exchangeHash)
  871. integKeySC = self._getKey(b'F', sharedSecret, exchangeHash)
  872. outs = [initIVSC, encKeySC, integKeySC]
  873. ins = [initIVCS, encKeyCS, integKeyCS]
  874. if self.isClient: # Reverse for the client
  875. log.msg('REVERSE')
  876. outs, ins = ins, outs
  877. self.nextEncryptions.setKeys(outs[0], outs[1], ins[0], ins[1],
  878. outs[2], ins[2])
  879. self.sendPacket(MSG_NEWKEYS, b'')
  880. def _newKeys(self):
  881. """
  882. Called back by a subclass once a I{MSG_NEWKEYS} message has been
  883. received. This indicates key exchange has completed and new encryption
  884. and compression parameters should be adopted. Any messages which were
  885. queued during key exchange will also be flushed.
  886. """
  887. log.msg('NEW KEYS')
  888. self.currentEncryptions = self.nextEncryptions
  889. if self.outgoingCompressionType == b'zlib':
  890. self.outgoingCompression = zlib.compressobj(6)
  891. if self.incomingCompressionType == b'zlib':
  892. self.incomingCompression = zlib.decompressobj()
  893. self._keyExchangeState = self._KEY_EXCHANGE_NONE
  894. messages = self._blockedByKeyExchange
  895. self._blockedByKeyExchange = None
  896. for (messageType, payload) in messages:
  897. self.sendPacket(messageType, payload)
  898. def isEncrypted(self, direction="out"):
  899. """
  900. Check if the connection is encrypted in the given direction.
  901. @type direction: L{str}
  902. @param direction: The direction: one of 'out', 'in', or 'both'.
  903. @rtype: L{bool}
  904. @return: C{True} if it is encrypted.
  905. """
  906. if direction == "out":
  907. return self.currentEncryptions.outCipType != b'none'
  908. elif direction == "in":
  909. return self.currentEncryptions.inCipType != b'none'
  910. elif direction == "both":
  911. return self.isEncrypted("in") and self.isEncrypted("out")
  912. else:
  913. raise TypeError('direction must be "out", "in", or "both"')
  914. def isVerified(self, direction="out"):
  915. """
  916. Check if the connection is verified/authentication in the given direction.
  917. @type direction: L{str}
  918. @param direction: The direction: one of 'out', 'in', or 'both'.
  919. @rtype: L{bool}
  920. @return: C{True} if it is verified.
  921. """
  922. if direction == "out":
  923. return self.currentEncryptions.outMACType != b'none'
  924. elif direction == "in":
  925. return self.currentEncryptions.inMACType != b'none'
  926. elif direction == "both":
  927. return self.isVerified("in") and self.isVerified("out")
  928. else:
  929. raise TypeError('direction must be "out", "in", or "both"')
  930. def loseConnection(self):
  931. """
  932. Lose the connection to the other side, sending a
  933. DISCONNECT_CONNECTION_LOST message.
  934. """
  935. self.sendDisconnect(DISCONNECT_CONNECTION_LOST,
  936. b"user closed connection")
  937. # Client methods
  938. def receiveError(self, reasonCode, description):
  939. """
  940. Called when we receive a disconnect error message from the other
  941. side.
  942. @param reasonCode: the reason for the disconnect, one of the
  943. DISCONNECT_ values.
  944. @type reasonCode: L{int}
  945. @param description: a human-readable description of the
  946. disconnection.
  947. @type description: L{str}
  948. """
  949. log.msg('Got remote error, code %s\nreason: %s' % (reasonCode,
  950. description))
  951. def receiveUnimplemented(self, seqnum):
  952. """
  953. Called when we receive an unimplemented packet message from the other
  954. side.
  955. @param seqnum: the sequence number that was not understood.
  956. @type seqnum: L{int}
  957. """
  958. log.msg('other side unimplemented packet #%s' % (seqnum,))
  959. def receiveDebug(self, alwaysDisplay, message, lang):
  960. """
  961. Called when we receive a debug message from the other side.
  962. @param alwaysDisplay: if True, this message should always be
  963. displayed.
  964. @type alwaysDisplay: L{bool}
  965. @param message: the debug message
  966. @type message: L{str}
  967. @param lang: optionally the language the message is in.
  968. @type lang: L{str}
  969. """
  970. if alwaysDisplay:
  971. log.msg('Remote Debug Message: %s' % (message,))
  972. class SSHServerTransport(SSHTransportBase):
  973. """
  974. SSHServerTransport implements the server side of the SSH protocol.
  975. @ivar isClient: since we are never the client, this is always False.
  976. @ivar ignoreNextPacket: if True, ignore the next key exchange packet. This
  977. is set when the client sends a guessed key exchange packet but with
  978. an incorrect guess.
  979. @ivar dhGexRequest: the KEX_DH_GEX_REQUEST(_OLD) that the client sent.
  980. The key generation needs this to be stored.
  981. @ivar g: the Diffie-Hellman group generator.
  982. @ivar p: the Diffie-Hellman group prime.
  983. """
  984. isClient = False
  985. ignoreNextPacket = 0
  986. def ssh_KEXINIT(self, packet):
  987. """
  988. Called when we receive a MSG_KEXINIT message. For a description
  989. of the packet, see SSHTransportBase.ssh_KEXINIT(). Additionally,
  990. this method checks if a guessed key exchange packet was sent. If
  991. it was sent, and it guessed incorrectly, the next key exchange
  992. packet MUST be ignored.
  993. """
  994. retval = SSHTransportBase.ssh_KEXINIT(self, packet)
  995. if not retval: # Disconnected
  996. return
  997. else:
  998. kexAlgs, keyAlgs, rest = retval
  999. if ord(rest[0:1]): # Flag first_kex_packet_follows?
  1000. if (kexAlgs[0] != self.supportedKeyExchanges[0] or
  1001. keyAlgs[0] != self.supportedPublicKeys[0]):
  1002. self.ignoreNextPacket = True # Guess was wrong
  1003. def _ssh_KEX_ECDH_INIT(self, packet):
  1004. """
  1005. Called from L{ssh_KEX_DH_GEX_REQUEST_OLD} to handle
  1006. elliptic curve key exchanges.
  1007. Payload::
  1008. string client Elliptic Curve Diffie-Hellman public key
  1009. Just like L{_ssh_KEXDH_INIT} this message type is also not dispatched
  1010. directly. Extra check to determine if this is really KEX_ECDH_INIT
  1011. is required.
  1012. First we load the host's public/private keys.
  1013. Then we generate the ECDH public/private keypair for the given curve.
  1014. With that we generate the shared secret key.
  1015. Then we compute the hash to sign and send back to the client
  1016. Along with the server's public key and the ECDH public key.
  1017. @type packet: L{bytes}
  1018. @param packet: The message data.
  1019. @return: None.
  1020. """
  1021. # Get the raw client public key.
  1022. pktPub, packet = getNS(packet)
  1023. # Get the host's public and private keys
  1024. pubHostKey = self.factory.publicKeys[self.keyAlg]
  1025. privHostKey = self.factory.privateKeys[self.keyAlg]
  1026. # Get the curve instance
  1027. try:
  1028. curve = keys._curveTable[b'ecdsa' + self.kexAlg[4:]]
  1029. except KeyError:
  1030. raise UnsupportedAlgorithm('unused-key')
  1031. # Generate the private key
  1032. ecPriv = ec.generate_private_key(curve, default_backend())
  1033. # Get the public key
  1034. ecPub = ecPriv.public_key()
  1035. encPub = ecPub.public_numbers().encode_point()
  1036. # Take the provided public key and transform it into
  1037. # a format for the cryptography module
  1038. theirECPub = ec.EllipticCurvePublicNumbers.from_encoded_point(
  1039. curve, pktPub).public_key(default_backend())
  1040. # We need to convert to hex,
  1041. # so we can convert to an int
  1042. # so we can make it a multiple precision int.
  1043. sharedSecret = MP(
  1044. int(
  1045. binascii.hexlify(
  1046. ecPriv.exchange(ec.ECDH(), theirECPub)), 16))
  1047. # Finish update and digest
  1048. h = _kex.getHashProcessor(self.kexAlg)()
  1049. h.update(NS(self.otherVersionString))
  1050. h.update(NS(self.ourVersionString))
  1051. h.update(NS(self.otherKexInitPayload))
  1052. h.update(NS(self.ourKexInitPayload))
  1053. h.update(NS(pubHostKey.blob()))
  1054. h.update(NS(pktPub))
  1055. h.update(NS(encPub))
  1056. h.update(sharedSecret)
  1057. exchangeHash = h.digest()
  1058. self.sendPacket(
  1059. MSG_KEXDH_REPLY,
  1060. NS(pubHostKey.blob()) + NS(encPub) +
  1061. NS(privHostKey.sign(exchangeHash)))
  1062. self._keySetup(sharedSecret, exchangeHash)
  1063. def _ssh_KEXDH_INIT(self, packet):
  1064. """
  1065. Called to handle the beginning of a non-group key exchange.
  1066. Unlike other message types, this is not dispatched automatically. It
  1067. is called from C{ssh_KEX_DH_GEX_REQUEST_OLD} because an extra check is
  1068. required to determine if this is really a KEXDH_INIT message or if it
  1069. is a KEX_DH_GEX_REQUEST_OLD message.
  1070. The KEXDH_INIT payload::
  1071. integer e (the client's Diffie-Hellman public key)
  1072. We send the KEXDH_REPLY with our host key and signature.
  1073. @type packet: L{bytes}
  1074. @param packet: The message data.
  1075. """
  1076. clientDHpublicKey, foo = getMP(packet)
  1077. y = _getRandomNumber(randbytes.secureRandom, 512)
  1078. self.g, self.p = _kex.getDHGeneratorAndPrime(self.kexAlg)
  1079. serverDHpublicKey = _MPpow(self.g, y, self.p)
  1080. sharedSecret = _MPpow(clientDHpublicKey, y, self.p)
  1081. h = sha1()
  1082. h.update(NS(self.otherVersionString))
  1083. h.update(NS(self.ourVersionString))
  1084. h.update(NS(self.otherKexInitPayload))
  1085. h.update(NS(self.ourKexInitPayload))
  1086. h.update(NS(self.factory.publicKeys[self.keyAlg].blob()))
  1087. h.update(MP(clientDHpublicKey))
  1088. h.update(serverDHpublicKey)
  1089. h.update(sharedSecret)
  1090. exchangeHash = h.digest()
  1091. self.sendPacket(
  1092. MSG_KEXDH_REPLY,
  1093. NS(self.factory.publicKeys[self.keyAlg].blob()) +
  1094. serverDHpublicKey +
  1095. NS(self.factory.privateKeys[self.keyAlg].sign(exchangeHash)))
  1096. self._keySetup(sharedSecret, exchangeHash)
  1097. def ssh_KEX_DH_GEX_REQUEST_OLD(self, packet):
  1098. """
  1099. This represents different key exchange methods that share the same
  1100. integer value. If the message is determined to be a KEXDH_INIT,
  1101. L{_ssh_KEXDH_INIT} is called to handle it. If it is a KEX_ECDH_INIT,
  1102. L{_ssh_KEX_ECDH_INIT} is called.
  1103. Otherwise, for KEX_DH_GEX_REQUEST_OLD payload::
  1104. integer ideal (ideal size for the Diffie-Hellman prime)
  1105. We send the KEX_DH_GEX_GROUP message with the group that is
  1106. closest in size to ideal.
  1107. If we were told to ignore the next key exchange packet by ssh_KEXINIT,
  1108. drop it on the floor and return.
  1109. @type packet: L{bytes}
  1110. @param packet: The message data.
  1111. """
  1112. if self.ignoreNextPacket:
  1113. self.ignoreNextPacket = 0
  1114. return
  1115. # KEXDH_INIT, KEX_ECDH_INIT, and KEX_DH_GEX_REQUEST_OLD
  1116. # have the same value, so use another cue
  1117. # to decide what kind of message the peer sent us.
  1118. if _kex.isFixedGroup(self.kexAlg):
  1119. return self._ssh_KEXDH_INIT(packet)
  1120. elif _kex.isEllipticCurve(self.kexAlg):
  1121. return self._ssh_KEX_ECDH_INIT(packet)
  1122. else:
  1123. self.dhGexRequest = packet
  1124. ideal = struct.unpack('>L', packet)[0]
  1125. self.g, self.p = self.factory.getDHPrime(ideal)
  1126. self.sendPacket(MSG_KEX_DH_GEX_GROUP, MP(self.p) + MP(self.g))
  1127. def ssh_KEX_DH_GEX_REQUEST(self, packet):
  1128. """
  1129. Called when we receive a MSG_KEX_DH_GEX_REQUEST message. Payload::
  1130. integer minimum
  1131. integer ideal
  1132. integer maximum
  1133. The client is asking for a Diffie-Hellman group between minimum and
  1134. maximum size, and close to ideal if possible. We reply with a
  1135. MSG_KEX_DH_GEX_GROUP message.
  1136. If we were told to ignore the next key exchange packet by ssh_KEXINIT,
  1137. drop it on the floor and return.
  1138. @type packet: L{bytes}
  1139. @param packet: The message data.
  1140. """
  1141. if self.ignoreNextPacket:
  1142. self.ignoreNextPacket = 0
  1143. return
  1144. self.dhGexRequest = packet
  1145. min, ideal, max = struct.unpack('>3L', packet)
  1146. self.g, self.p = self.factory.getDHPrime(ideal)
  1147. self.sendPacket(MSG_KEX_DH_GEX_GROUP, MP(self.p) + MP(self.g))
  1148. def ssh_KEX_DH_GEX_INIT(self, packet):
  1149. """
  1150. Called when we get a MSG_KEX_DH_GEX_INIT message. Payload::
  1151. integer e (client DH public key)
  1152. We send the MSG_KEX_DH_GEX_REPLY message with our host key and
  1153. signature.
  1154. @type packet: L{bytes}
  1155. @param packet: The message data.
  1156. """
  1157. clientDHpublicKey, foo = getMP(packet)
  1158. # TODO: we should also look at the value they send to us and reject
  1159. # insecure values of f (if g==2 and f has a single '1' bit while the
  1160. # rest are '0's, then they must have used a small y also).
  1161. # TODO: This could be computed when self.p is set up
  1162. # or do as openssh does and scan f for a single '1' bit instead
  1163. pSize = self.p.bit_length()
  1164. y = _getRandomNumber(randbytes.secureRandom, pSize)
  1165. serverDHpublicKey = _MPpow(self.g, y, self.p)
  1166. sharedSecret = _MPpow(clientDHpublicKey, y, self.p)
  1167. h = _kex.getHashProcessor(self.kexAlg)()
  1168. h.update(NS(self.otherVersionString))
  1169. h.update(NS(self.ourVersionString))
  1170. h.update(NS(self.otherKexInitPayload))
  1171. h.update(NS(self.ourKexInitPayload))
  1172. h.update(NS(self.factory.publicKeys[self.keyAlg].blob()))
  1173. h.update(self.dhGexRequest)
  1174. h.update(MP(self.p))
  1175. h.update(MP(self.g))
  1176. h.update(MP(clientDHpublicKey))
  1177. h.update(serverDHpublicKey)
  1178. h.update(sharedSecret)
  1179. exchangeHash = h.digest()
  1180. self.sendPacket(
  1181. MSG_KEX_DH_GEX_REPLY,
  1182. NS(self.factory.publicKeys[self.keyAlg].blob()) +
  1183. serverDHpublicKey +
  1184. NS(self.factory.privateKeys[self.keyAlg].sign(exchangeHash)))
  1185. self._keySetup(sharedSecret, exchangeHash)
  1186. def ssh_NEWKEYS(self, packet):
  1187. """
  1188. Called when we get a MSG_NEWKEYS message. No payload.
  1189. When we get this, the keys have been set on both sides, and we
  1190. start using them to encrypt and authenticate the connection.
  1191. @type packet: L{bytes}
  1192. @param packet: The message data.
  1193. """
  1194. if packet != b'':
  1195. self.sendDisconnect(DISCONNECT_PROTOCOL_ERROR,
  1196. b"NEWKEYS takes no data")
  1197. return
  1198. self._newKeys()
  1199. def ssh_SERVICE_REQUEST(self, packet):
  1200. """
  1201. Called when we get a MSG_SERVICE_REQUEST message. Payload::
  1202. string serviceName
  1203. The client has requested a service. If we can start the service,
  1204. start it; otherwise, disconnect with
  1205. DISCONNECT_SERVICE_NOT_AVAILABLE.
  1206. @type packet: L{bytes}
  1207. @param packet: The message data.
  1208. """
  1209. service, rest = getNS(packet)
  1210. cls = self.factory.getService(self, service)
  1211. if not cls:
  1212. self.sendDisconnect(DISCONNECT_SERVICE_NOT_AVAILABLE,
  1213. b"don't have service " + service)
  1214. return
  1215. else:
  1216. self.sendPacket(MSG_SERVICE_ACCEPT, NS(service))
  1217. self.setService(cls())
  1218. class SSHClientTransport(SSHTransportBase):
  1219. """
  1220. SSHClientTransport implements the client side of the SSH protocol.
  1221. @ivar isClient: since we are always the client, this is always True.
  1222. @ivar _gotNewKeys: if we receive a MSG_NEWKEYS message before we are
  1223. ready to transition to the new keys, this is set to True so we
  1224. can transition when the keys are ready locally.
  1225. @ivar x: our Diffie-Hellman private key.
  1226. @ivar e: our Diffie-Hellman public key.
  1227. @ivar g: the Diffie-Hellman group generator.
  1228. @ivar p: the Diffie-Hellman group prime
  1229. @ivar instance: the SSHService object we are requesting.
  1230. @ivar _dhMinimalGroupSize: Minimal acceptable group size advertised by the
  1231. client in MSG_KEX_DH_GEX_REQUEST.
  1232. @type _dhMinimalGroupSize: int
  1233. @ivar _dhMaximalGroupSize: Maximal acceptable group size advertised by the
  1234. client in MSG_KEX_DH_GEX_REQUEST.
  1235. @type _dhMaximalGroupSize: int
  1236. @ivar _dhPreferredGroupSize: Preferred group size advertised by the client
  1237. in MSG_KEX_DH_GEX_REQUEST.
  1238. @type _dhPreferredGroupSize: int
  1239. """
  1240. isClient = True
  1241. # Recommended minimal and maximal values from RFC 4419, 3.
  1242. _dhMinimalGroupSize = 1024
  1243. _dhMaximalGroupSize = 8192
  1244. # FIXME: https://twistedmatrix.com/trac/ticket/8103
  1245. # This may need to be more dynamic; compare kexgex_client in
  1246. # OpenSSH.
  1247. _dhPreferredGroupSize = 2048
  1248. def connectionMade(self):
  1249. """
  1250. Called when the connection is started with the server. Just sets
  1251. up a private instance variable.
  1252. """
  1253. SSHTransportBase.connectionMade(self)
  1254. self._gotNewKeys = 0
  1255. def ssh_KEXINIT(self, packet):
  1256. """
  1257. Called when we receive a MSG_KEXINIT message. For a description
  1258. of the packet, see SSHTransportBase.ssh_KEXINIT(). Additionally,
  1259. this method sends the first key exchange packet.
  1260. If the agreed-upon exchange is ECDH, generate a key pair for the
  1261. corresponding curve and send the public key.
  1262. If the agreed-upon exchange has a fixed prime/generator group,
  1263. generate a public key and send it in a MSG_KEXDH_INIT message.
  1264. Otherwise, ask for a 2048 bit group with a MSG_KEX_DH_GEX_REQUEST
  1265. message.
  1266. """
  1267. if SSHTransportBase.ssh_KEXINIT(self, packet) is None:
  1268. # Connection was disconnected while doing base processing.
  1269. # Maybe no common protocols were agreed.
  1270. return
  1271. # Are we using ECDH?
  1272. if _kex.isEllipticCurve(self.kexAlg):
  1273. # Find the base curve info
  1274. self.curve = keys._curveTable[b'ecdsa' + self.kexAlg[4:]]
  1275. # Generate the keys
  1276. self.ecPriv = ec.generate_private_key(self.curve,
  1277. default_backend())
  1278. self.ecPub = self.ecPriv.public_key()
  1279. # DH_GEX_REQUEST_OLD is the same number we need.
  1280. self.sendPacket(
  1281. MSG_KEX_DH_GEX_REQUEST_OLD,
  1282. NS(self.ecPub.public_numbers().encode_point()))
  1283. elif _kex.isFixedGroup(self.kexAlg):
  1284. # We agreed on a fixed group key exchange algorithm.
  1285. self.x = _generateX(randbytes.secureRandom, 512)
  1286. self.g, self.p = _kex.getDHGeneratorAndPrime(self.kexAlg)
  1287. self.e = _MPpow(self.g, self.x, self.p)
  1288. self.sendPacket(MSG_KEXDH_INIT, self.e)
  1289. else:
  1290. # We agreed on a dynamic group. Tell the server what range of
  1291. # group sizes we accept, and what size we prefer; the server
  1292. # will then select a group.
  1293. self.sendPacket(
  1294. MSG_KEX_DH_GEX_REQUEST,
  1295. struct.pack(
  1296. '!LLL',
  1297. self._dhMinimalGroupSize,
  1298. self._dhPreferredGroupSize,
  1299. self._dhMaximalGroupSize,
  1300. ))
  1301. def _ssh_KEX_ECDH_REPLY(self, packet):
  1302. """
  1303. Called to handle a reply to a ECDH exchange message(KEX_ECDH_INIT).
  1304. Like the handler for I{KEXDH_INIT}, this message type has an
  1305. overlapping value. This method is called from C{ssh_KEX_DH_GEX_GROUP}
  1306. if that method detects a non-group key exchange is in progress.
  1307. Payload::
  1308. string serverHostKey
  1309. string server Elliptic Curve Diffie-Hellman public key
  1310. string signature
  1311. We verify the host key and continue if it passes verificiation.
  1312. Otherwise raise an exception and return.
  1313. @type packet: L{bytes}
  1314. @param packet: The message data.
  1315. @return: A deferred firing when key exchange is complete.
  1316. """
  1317. def _continue_KEX_ECDH_REPLY(ignored, hostKey, pubKey, signature):
  1318. # Save off the host public key.
  1319. theirECHost = hostKey
  1320. # Take the provided public key and transform it into a format
  1321. # for the cryptography module
  1322. theirECPub = ec.EllipticCurvePublicNumbers.from_encoded_point(
  1323. self.curve, pubKey).public_key(
  1324. default_backend())
  1325. # We need to convert to hex,
  1326. # so we can convert to an int
  1327. # so we can make a multiple precision int.
  1328. sharedSecret = MP(
  1329. int(
  1330. binascii.hexlify(
  1331. self.ecPriv.exchange(ec.ECDH(), theirECPub)), 16))
  1332. h = _kex.getHashProcessor(self.kexAlg)()
  1333. h.update(NS(self.ourVersionString))
  1334. h.update(NS(self.otherVersionString))
  1335. h.update(NS(self.ourKexInitPayload))
  1336. h.update(NS(self.otherKexInitPayload))
  1337. h.update(NS(theirECHost))
  1338. h.update(NS(self.ecPub.public_numbers().encode_point()))
  1339. h.update(NS(pubKey))
  1340. h.update(sharedSecret)
  1341. exchangeHash = h.digest()
  1342. if not keys.Key.fromString(theirECHost).verify(
  1343. signature, exchangeHash):
  1344. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  1345. b'bad signature')
  1346. else:
  1347. self._keySetup(sharedSecret, exchangeHash)
  1348. # Get the host public key,
  1349. # the raw ECDH public key bytes and the signature
  1350. hostKey, pubKey, signature, packet = getNS(packet, 3)
  1351. # Easier to comment this out for now than to update all of the tests.
  1352. #fingerprint = nativeString(base64.b64encode(
  1353. # sha256(hostKey).digest()))
  1354. fingerprint = b':'.join(
  1355. [binascii.hexlify(ch) for ch in iterbytes(md5(hostKey).digest())])
  1356. d = self.verifyHostKey(hostKey, fingerprint)
  1357. d.addCallback(_continue_KEX_ECDH_REPLY, hostKey, pubKey, signature)
  1358. d.addErrback(
  1359. lambda unused: self.sendDisconnect(
  1360. DISCONNECT_HOST_KEY_NOT_VERIFIABLE, b'bad host key'))
  1361. return d
  1362. def _ssh_KEXDH_REPLY(self, packet):
  1363. """
  1364. Called to handle a reply to a non-group key exchange message
  1365. (KEXDH_INIT).
  1366. Like the handler for I{KEXDH_INIT}, this message type has an
  1367. overlapping value. This method is called from C{ssh_KEX_DH_GEX_GROUP}
  1368. if that method detects a non-group key exchange is in progress.
  1369. Payload::
  1370. string serverHostKey
  1371. integer f (server Diffie-Hellman public key)
  1372. string signature
  1373. We verify the host key by calling verifyHostKey, then continue in
  1374. _continueKEXDH_REPLY.
  1375. @type packet: L{bytes}
  1376. @param packet: The message data.
  1377. @return: A deferred firing when key exchange is complete.
  1378. """
  1379. pubKey, packet = getNS(packet)
  1380. f, packet = getMP(packet)
  1381. signature, packet = getNS(packet)
  1382. fingerprint = b':'.join([binascii.hexlify(ch) for ch in
  1383. iterbytes(md5(pubKey).digest())])
  1384. d = self.verifyHostKey(pubKey, fingerprint)
  1385. d.addCallback(self._continueKEXDH_REPLY, pubKey, f, signature)
  1386. d.addErrback(
  1387. lambda unused: self.sendDisconnect(
  1388. DISCONNECT_HOST_KEY_NOT_VERIFIABLE, b'bad host key'))
  1389. return d
  1390. def ssh_KEX_DH_GEX_GROUP(self, packet):
  1391. """
  1392. This handles different messages which share an integer value.
  1393. If the key exchange does not have a fixed prime/generator group,
  1394. we generate a Diffie-Hellman public key and send it in a
  1395. MSG_KEX_DH_GEX_INIT message.
  1396. Payload::
  1397. string g (group generator)
  1398. string p (group prime)
  1399. @type packet: L{bytes}
  1400. @param packet: The message data.
  1401. """
  1402. if _kex.isFixedGroup(self.kexAlg):
  1403. return self._ssh_KEXDH_REPLY(packet)
  1404. elif _kex.isEllipticCurve(self.kexAlg):
  1405. return self._ssh_KEX_ECDH_REPLY(packet)
  1406. else:
  1407. self.p, rest = getMP(packet)
  1408. self.g, rest = getMP(rest)
  1409. self.x = _generateX(randbytes.secureRandom, 320)
  1410. self.e = _MPpow(self.g, self.x, self.p)
  1411. self.sendPacket(MSG_KEX_DH_GEX_INIT, self.e)
  1412. def _continueKEXDH_REPLY(self, ignored, pubKey, f, signature):
  1413. """
  1414. The host key has been verified, so we generate the keys.
  1415. @param ignored: Ignored.
  1416. @param pubKey: the public key blob for the server's public key.
  1417. @type pubKey: L{str}
  1418. @param f: the server's Diffie-Hellman public key.
  1419. @type f: L{long}
  1420. @param signature: the server's signature, verifying that it has the
  1421. correct private key.
  1422. @type signature: L{str}
  1423. """
  1424. serverKey = keys.Key.fromString(pubKey)
  1425. sharedSecret = _MPpow(f, self.x, self.p)
  1426. h = sha1()
  1427. h.update(NS(self.ourVersionString))
  1428. h.update(NS(self.otherVersionString))
  1429. h.update(NS(self.ourKexInitPayload))
  1430. h.update(NS(self.otherKexInitPayload))
  1431. h.update(NS(pubKey))
  1432. h.update(self.e)
  1433. h.update(MP(f))
  1434. h.update(sharedSecret)
  1435. exchangeHash = h.digest()
  1436. if not serverKey.verify(signature, exchangeHash):
  1437. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  1438. b'bad signature')
  1439. return
  1440. self._keySetup(sharedSecret, exchangeHash)
  1441. def ssh_KEX_DH_GEX_REPLY(self, packet):
  1442. """
  1443. Called when we receive a MSG_KEX_DH_GEX_REPLY message. Payload::
  1444. string server host key
  1445. integer f (server DH public key)
  1446. We verify the host key by calling verifyHostKey, then continue in
  1447. _continueGEX_REPLY.
  1448. @type packet: L{bytes}
  1449. @param packet: The message data.
  1450. @return: A deferred firing once key exchange is complete.
  1451. """
  1452. pubKey, packet = getNS(packet)
  1453. f, packet = getMP(packet)
  1454. signature, packet = getNS(packet)
  1455. fingerprint = b':'.join(
  1456. [binascii.hexlify(c) for c in iterbytes(md5(pubKey).digest())])
  1457. d = self.verifyHostKey(pubKey, fingerprint)
  1458. d.addCallback(self._continueGEX_REPLY, pubKey, f, signature)
  1459. d.addErrback(
  1460. lambda unused: self.sendDisconnect(
  1461. DISCONNECT_HOST_KEY_NOT_VERIFIABLE, b'bad host key'))
  1462. return d
  1463. def _continueGEX_REPLY(self, ignored, pubKey, f, signature):
  1464. """
  1465. The host key has been verified, so we generate the keys.
  1466. @param ignored: Ignored.
  1467. @param pubKey: the public key blob for the server's public key.
  1468. @type pubKey: L{str}
  1469. @param f: the server's Diffie-Hellman public key.
  1470. @type f: L{long}
  1471. @param signature: the server's signature, verifying that it has the
  1472. correct private key.
  1473. @type signature: L{str}
  1474. """
  1475. serverKey = keys.Key.fromString(pubKey)
  1476. sharedSecret = _MPpow(f, self.x, self.p)
  1477. h = _kex.getHashProcessor(self.kexAlg)()
  1478. h.update(NS(self.ourVersionString))
  1479. h.update(NS(self.otherVersionString))
  1480. h.update(NS(self.ourKexInitPayload))
  1481. h.update(NS(self.otherKexInitPayload))
  1482. h.update(NS(pubKey))
  1483. h.update(struct.pack(
  1484. '!LLL',
  1485. self._dhMinimalGroupSize,
  1486. self._dhPreferredGroupSize,
  1487. self._dhMaximalGroupSize,
  1488. ))
  1489. h.update(MP(self.p))
  1490. h.update(MP(self.g))
  1491. h.update(self.e)
  1492. h.update(MP(f))
  1493. h.update(sharedSecret)
  1494. exchangeHash = h.digest()
  1495. if not serverKey.verify(signature, exchangeHash):
  1496. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  1497. b'bad signature')
  1498. return
  1499. self._keySetup(sharedSecret, exchangeHash)
  1500. def _keySetup(self, sharedSecret, exchangeHash):
  1501. """
  1502. See SSHTransportBase._keySetup().
  1503. """
  1504. SSHTransportBase._keySetup(self, sharedSecret, exchangeHash)
  1505. if self._gotNewKeys:
  1506. self.ssh_NEWKEYS(b'')
  1507. def ssh_NEWKEYS(self, packet):
  1508. """
  1509. Called when we receive a MSG_NEWKEYS message. No payload.
  1510. If we've finished setting up our own keys, start using them.
  1511. Otherwise, remember that we've received this message.
  1512. @type packet: L{bytes}
  1513. @param packet: The message data.
  1514. """
  1515. if packet != b'':
  1516. self.sendDisconnect(DISCONNECT_PROTOCOL_ERROR,
  1517. b"NEWKEYS takes no data")
  1518. return
  1519. if not self.nextEncryptions.encBlockSize:
  1520. self._gotNewKeys = 1
  1521. return
  1522. self._newKeys()
  1523. self.connectionSecure()
  1524. def ssh_SERVICE_ACCEPT(self, packet):
  1525. """
  1526. Called when we receive a MSG_SERVICE_ACCEPT message. Payload::
  1527. string service name
  1528. Start the service we requested.
  1529. @type packet: L{bytes}
  1530. @param packet: The message data.
  1531. """
  1532. if packet == b'':
  1533. log.msg('got SERVICE_ACCEPT without payload')
  1534. else:
  1535. name = getNS(packet)[0]
  1536. if name != self.instance.name:
  1537. self.sendDisconnect(
  1538. DISCONNECT_PROTOCOL_ERROR,
  1539. b"received accept for service we did not request")
  1540. self.setService(self.instance)
  1541. def requestService(self, instance):
  1542. """
  1543. Request that a service be run over this transport.
  1544. @type instance: subclass of L{twisted.conch.ssh.service.SSHService}
  1545. @param instance: The service to run.
  1546. """
  1547. self.sendPacket(MSG_SERVICE_REQUEST, NS(instance.name))
  1548. self.instance = instance
  1549. # Client methods
  1550. def verifyHostKey(self, hostKey, fingerprint):
  1551. """
  1552. Returns a Deferred that gets a callback if it is a valid key, or
  1553. an errback if not.
  1554. @type hostKey: L{bytes}
  1555. @param hostKey: The host key to verify.
  1556. @type fingerprint: L{bytes}
  1557. @param fingerprint: The fingerprint of the key.
  1558. @return: A deferred firing with C{True} if the key is valid.
  1559. """
  1560. return defer.fail(NotImplementedError())
  1561. def connectionSecure(self):
  1562. """
  1563. Called when the encryption has been set up. Generally,
  1564. requestService() is called to run another service over the transport.
  1565. """
  1566. raise NotImplementedError()
  1567. class _NullEncryptionContext(object):
  1568. """
  1569. An encryption context that does not actually encrypt anything.
  1570. """
  1571. def update(self, data):
  1572. """
  1573. 'Encrypt' new data by doing nothing.
  1574. @type data: L{bytes}
  1575. @param data: The data to 'encrypt'.
  1576. @rtype: L{bytes}
  1577. @return: The 'encrypted' data.
  1578. """
  1579. return data
  1580. class _DummyAlgorithm(object):
  1581. """
  1582. An encryption algorithm that does not actually encrypt anything.
  1583. """
  1584. block_size = 64
  1585. class _DummyCipher(object):
  1586. """
  1587. A cipher for the none encryption method.
  1588. @ivar block_size: the block size of the encryption. In the case of the
  1589. none cipher, this is 8 bytes.
  1590. """
  1591. algorithm = _DummyAlgorithm()
  1592. def encryptor(self):
  1593. """
  1594. Construct a noop encryptor.
  1595. @return: The encryptor.
  1596. """
  1597. return _NullEncryptionContext()
  1598. def decryptor(self):
  1599. """
  1600. Construct a noop decryptor.
  1601. @return: The decryptor.
  1602. """
  1603. return _NullEncryptionContext()
  1604. DH_GENERATOR, DH_PRIME = _kex.getDHGeneratorAndPrime(
  1605. b'diffie-hellman-group14-sha1')
  1606. MSG_DISCONNECT = 1
  1607. MSG_IGNORE = 2
  1608. MSG_UNIMPLEMENTED = 3
  1609. MSG_DEBUG = 4
  1610. MSG_SERVICE_REQUEST = 5
  1611. MSG_SERVICE_ACCEPT = 6
  1612. MSG_KEXINIT = 20
  1613. MSG_NEWKEYS = 21
  1614. MSG_KEXDH_INIT = 30
  1615. MSG_KEXDH_REPLY = 31
  1616. MSG_KEX_DH_GEX_REQUEST_OLD = 30
  1617. MSG_KEX_DH_GEX_REQUEST = 34
  1618. MSG_KEX_DH_GEX_GROUP = 31
  1619. MSG_KEX_DH_GEX_INIT = 32
  1620. MSG_KEX_DH_GEX_REPLY = 33
  1621. DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT = 1
  1622. DISCONNECT_PROTOCOL_ERROR = 2
  1623. DISCONNECT_KEY_EXCHANGE_FAILED = 3
  1624. DISCONNECT_RESERVED = 4
  1625. DISCONNECT_MAC_ERROR = 5
  1626. DISCONNECT_COMPRESSION_ERROR = 6
  1627. DISCONNECT_SERVICE_NOT_AVAILABLE = 7
  1628. DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED = 8
  1629. DISCONNECT_HOST_KEY_NOT_VERIFIABLE = 9
  1630. DISCONNECT_CONNECTION_LOST = 10
  1631. DISCONNECT_BY_APPLICATION = 11
  1632. DISCONNECT_TOO_MANY_CONNECTIONS = 12
  1633. DISCONNECT_AUTH_CANCELLED_BY_USER = 13
  1634. DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE = 14
  1635. DISCONNECT_ILLEGAL_USER_NAME = 15
  1636. messages = {}
  1637. for name, value in list(globals().items()):
  1638. # Avoid legacy messages which overlap with never ones
  1639. if name.startswith('MSG_') and not name.startswith('MSG_KEXDH_'):
  1640. messages[value] = name
  1641. # Check for regressions (#5352)
  1642. if 'MSG_KEXDH_INIT' in messages or 'MSG_KEXDH_REPLY' in messages:
  1643. raise RuntimeError(
  1644. "legacy SSH mnemonics should not end up in messages dict")