PageRenderTime 67ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/desktop/core/ext-py/Twisted/twisted/conch/ssh/transport.py

https://github.com/jcrobak/hue
Python | 1362 lines | 1195 code | 38 blank | 129 comment | 16 complexity | e1f291c6967d42a312eec423600194d3 MD5 | raw file
  1. # -*- test-case-name: twisted.conch.test.test_transport -*-
  2. #
  3. # Copyright (c) 2001-2008 Twisted Matrix Laboratories.
  4. # See LICENSE for details.
  5. """
  6. The lowest level SSH protocol. This handles the key negotiation, the
  7. encryption and the compression. The transport layer is described in
  8. RFC 4253.
  9. Maintainer: Paul Swartz
  10. """
  11. # base library imports
  12. import struct
  13. import md5
  14. import sha
  15. import zlib
  16. import array
  17. # external library imports
  18. from Crypto import Util
  19. from Crypto.Cipher import XOR
  20. # twisted imports
  21. from twisted.internet import protocol, defer
  22. from twisted.conch import error
  23. from twisted.python import log, randbytes
  24. # sibling imports
  25. from twisted.conch.ssh import keys
  26. from twisted.conch.ssh.common import NS, getNS, MP, getMP, _MPpow, ffs
  27. class SSHTransportBase(protocol.Protocol):
  28. """
  29. Protocol supporting basic SSH functionality: sending/receiving packets
  30. and message dispatch. To connect to or run a server, you must use
  31. SSHClientTransport or SSHServerTransport.
  32. @ivar protocolVersion: A string representing the version of the SSH
  33. protocol we support. Currently defaults to '2.0'.
  34. @ivar version: A string representing the version of the server or client.
  35. Currently defaults to 'Twisted'.
  36. @ivar comment: An optional string giving more information about the
  37. server or client.
  38. @ivar supportedCiphers: A list of strings representing the encryption
  39. algorithms supported, in order from most-preferred to least.
  40. @ivar supportedMACs: A list of strings representing the message
  41. authentication codes (hashes) supported, in order from most-preferred
  42. to least. Both this and supportedCiphers can include 'none' to use
  43. no encryption or authentication, but that must be done manually,
  44. @ivar supportedKeyExchanges: A list of strings representing the
  45. key exchanges supported, in order from most-preferred to least.
  46. @ivar supportedPublicKeys: A list of strings representing the
  47. public key types supported, in order from most-preferred to least.
  48. @ivar supportedCompressions: A list of strings representing compression
  49. types supported, from most-preferred to least.
  50. @ivar supportedLanguages: A list of strings representing languages
  51. supported, from most-preferred to least.
  52. @ivar isClient: A boolean indicating whether this is a client or server.
  53. @ivar gotVersion: A boolean indicating whether we have receieved the
  54. version string from the other side.
  55. @ivar buf: Data we've received but hasn't been parsed into a packet.
  56. @ivar outgoingPacketSequence: the sequence number of the next packet we
  57. will send.
  58. @ivar incomingPacketSequence: the sequence number of the next packet we
  59. are expecting from the other side.
  60. @ivar outgoingCompression: an object supporting the .compress(str) and
  61. .flush() methods, or None if there is no outgoing compression. Used to
  62. compress outgoing data.
  63. @ivar outgoingCompressionType: A string representing the outgoing
  64. compression type.
  65. @ivar incomingCompression: an object supporting the .decompress(str)
  66. method, or None if there is no incoming compression. Used to
  67. decompress incoming data.
  68. @ivar incomingCompressionType: A string representing the incoming
  69. compression type.
  70. @ivar ourVersionString: the version string that we sent to the other side.
  71. Used in the key exchange.
  72. @ivar otherVersionString: the version string sent by the other side. Used
  73. in the key exchange.
  74. @ivar ourKexInitPayload: the MSG_KEXINIT payload we sent. Used in the key
  75. exchange.
  76. @ivar otherKexInitPayload: the MSG_KEXINIT payload we received. Used in
  77. the key exchange
  78. @ivar sessionID: a string that is unique to this SSH session. Created as
  79. part of the key exchange, sessionID is used to generate the various
  80. encryption and authentication keys.
  81. @ivar service: an SSHService instance, or None. If it's set to an object,
  82. it's the currently running service.
  83. @ivar kexAlg: the agreed-upon key exchange algorithm.
  84. @ivar keyAlg: the agreed-upon public key type for the key exchange.
  85. @ivar currentEncryptions: an SSHCiphers instance. It represents the
  86. current encryption and authentication options for the transport.
  87. @ivar nextEncryptions: an SSHCiphers instance. Held here until the
  88. MSG_NEWKEYS messages are exchanged, when nextEncryptions is
  89. transitioned to currentEncryptions.
  90. @ivar first: the first bytes of the next packet. In order to avoid
  91. decrypting data twice, the first bytes are decrypted and stored until
  92. the whole packet is available.
  93. """
  94. protocolVersion = '2.0'
  95. version = 'Twisted'
  96. comment = ''
  97. ourVersionString = ('SSH-' + protocolVersion + '-' + version + ' '
  98. + comment).strip()
  99. supportedCiphers = ['aes256-ctr', 'aes256-cbc', 'aes192-ctr', 'aes192-cbc',
  100. 'aes128-ctr', 'aes128-cbc', 'cast128-ctr',
  101. 'cast128-cbc', 'blowfish-ctr', 'blowfish-cbc',
  102. '3des-ctr', '3des-cbc'] # ,'none']
  103. supportedMACs = ['hmac-sha1', 'hmac-md5'] # , 'none']
  104. # both of the above support 'none', but for security are disabled by
  105. # default. to enable them, subclass this class and add it, or do:
  106. # SSHTransportBase.supportedCiphers.append('none')
  107. supportedKeyExchanges = ['diffie-hellman-group-exchange-sha1',
  108. 'diffie-hellman-group1-sha1']
  109. supportedPublicKeys = ['ssh-rsa', 'ssh-dss']
  110. supportedCompressions = ['none', 'zlib']
  111. supportedLanguages = ()
  112. isClient = False
  113. gotVersion = False
  114. buf = ''
  115. outgoingPacketSequence = 0
  116. incomingPacketSequence = 0
  117. outgoingCompression = None
  118. incomingCompression = None
  119. sessionID = None
  120. service = None
  121. def connectionLost(self, reason):
  122. if self.service:
  123. self.service.serviceStopped()
  124. if hasattr(self, 'avatar'):
  125. self.logoutFunction()
  126. log.msg('connection lost')
  127. def connectionMade(self):
  128. """
  129. Called when the connection is made to the other side. We sent our
  130. version and the MSG_KEXINIT packet.
  131. """
  132. self.transport.write('%s\r\n' % (self.ourVersionString,))
  133. self.currentEncryptions = SSHCiphers('none', 'none', 'none', 'none')
  134. self.currentEncryptions.setKeys('', '', '', '', '', '')
  135. self.sendKexInit()
  136. def sendKexInit(self):
  137. self.ourKexInitPayload = (chr(MSG_KEXINIT) +
  138. randbytes.secureRandom(16) +
  139. NS(','.join(self.supportedKeyExchanges)) +
  140. NS(','.join(self.supportedPublicKeys)) +
  141. NS(','.join(self.supportedCiphers)) +
  142. NS(','.join(self.supportedCiphers)) +
  143. NS(','.join(self.supportedMACs)) +
  144. NS(','.join(self.supportedMACs)) +
  145. NS(','.join(self.supportedCompressions)) +
  146. NS(','.join(self.supportedCompressions)) +
  147. NS(','.join(self.supportedLanguages)) +
  148. NS(','.join(self.supportedLanguages)) +
  149. '\000' + '\000\000\000\000')
  150. self.sendPacket(MSG_KEXINIT, self.ourKexInitPayload[1:])
  151. def sendPacket(self, messageType, payload):
  152. """
  153. Sends a packet. If it's been set up, compress the data, encrypt it,
  154. and authenticate it before sending.
  155. @param messageType: The type of the packet; generally one of the
  156. MSG_* values.
  157. @type messageType: C{int}
  158. @param payload: The payload for the message.
  159. @type payload: C{str}
  160. """
  161. payload = chr(messageType) + payload
  162. if self.outgoingCompression:
  163. payload = (self.outgoingCompression.compress(payload)
  164. + self.outgoingCompression.flush(2))
  165. bs = self.currentEncryptions.encBlockSize
  166. # 4 for the packet length and 1 for the padding length
  167. totalSize = 5 + len(payload)
  168. lenPad = bs - (totalSize % bs)
  169. if lenPad < 4:
  170. lenPad = lenPad + bs
  171. packet = (struct.pack('!LB',
  172. totalSize + lenPad - 4, lenPad) +
  173. payload + randbytes.secureRandom(lenPad))
  174. encPacket = (
  175. self.currentEncryptions.encrypt(packet) +
  176. self.currentEncryptions.makeMAC(
  177. self.outgoingPacketSequence, packet))
  178. self.transport.write(encPacket)
  179. self.outgoingPacketSequence += 1
  180. def getPacket(self):
  181. """
  182. Try to return a decrypted, authenticated, and decompressed packet
  183. out of the buffer. If there is not enough data, return None.
  184. @rtype: C{str}/C{None}
  185. """
  186. bs = self.currentEncryptions.decBlockSize
  187. ms = self.currentEncryptions.verifyDigestSize
  188. if len(self.buf) < bs: return # not enough data
  189. if not hasattr(self, 'first'):
  190. first = self.currentEncryptions.decrypt(self.buf[:bs])
  191. else:
  192. first = self.first
  193. del self.first
  194. packetLen, paddingLen = struct.unpack('!LB', first[:5])
  195. if packetLen > 1048576: # 1024 ** 2
  196. self.sendDisconnect(DISCONNECT_PROTOCOL_ERROR,
  197. 'bad packet length %s' % packetLen)
  198. return
  199. if len(self.buf) < packetLen + 4 + ms:
  200. self.first = first
  201. return # not enough packet
  202. if(packetLen + 4) % bs != 0:
  203. self.sendDisconnect(
  204. DISCONNECT_PROTOCOL_ERROR,
  205. 'bad packet mod (%i%%%i == %i)' % (packetLen + 4, bs,
  206. (packetLen + 4) % bs))
  207. return
  208. encData, self.buf = self.buf[:4 + packetLen], self.buf[4 + packetLen:]
  209. packet = first + self.currentEncryptions.decrypt(encData[bs:])
  210. if len(packet) != 4 + packetLen:
  211. self.sendDisconnect(DISCONNECT_PROTOCOL_ERROR,
  212. 'bad decryption')
  213. return
  214. if ms:
  215. macData, self.buf = self.buf[:ms], self.buf[ms:]
  216. if not self.currentEncryptions.verify(self.incomingPacketSequence,
  217. packet, macData):
  218. self.sendDisconnect(DISCONNECT_MAC_ERROR, 'bad MAC')
  219. return
  220. payload = packet[5:-paddingLen]
  221. if self.incomingCompression:
  222. try:
  223. payload = self.incomingCompression.decompress(payload)
  224. except: # bare except, because who knows what kind of errors
  225. # decompression can raise
  226. log.err()
  227. self.sendDisconnect(DISCONNECT_COMPRESSION_ERROR,
  228. 'compression error')
  229. return
  230. self.incomingPacketSequence += 1
  231. return payload
  232. def dataReceived(self, data):
  233. """
  234. First, check for the version string (SSH-2.0-*). After that has been
  235. received, this method adds data to the buffer, and pulls out any
  236. packets.
  237. @type data: C{str}
  238. """
  239. self.buf = self.buf + data
  240. if not self.gotVersion:
  241. if self.buf.find('\n', self.buf.find('SSH-')) == -1:
  242. return
  243. lines = self.buf.split('\n')
  244. for p in lines:
  245. if p.startswith('SSH-'):
  246. self.gotVersion = True
  247. self.otherVersionString = p.strip()
  248. if p.split('-')[1] not in ('1.99', '2.0'): # bad version
  249. self.sendDisconnect(
  250. DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
  251. 'bad version ' + p.split('-')[1])
  252. return
  253. i = lines.index(p)
  254. self.buf = '\n'.join(lines[i + 1:])
  255. packet = self.getPacket()
  256. while packet:
  257. messageNum = ord(packet[0])
  258. self.dispatchMessage(messageNum, packet[1:])
  259. packet = self.getPacket()
  260. def dispatchMessage(self, messageNum, payload):
  261. """
  262. Send a received message to the appropriate method.
  263. @type messageNum: C{int}
  264. @type payload: c{str}
  265. """
  266. if messageNum < 50 and messageNum in messages:
  267. messageType = messages[messageNum][4:]
  268. f = getattr(self, 'ssh_%s' % messageType, None)
  269. if f is not None:
  270. f(payload)
  271. else:
  272. log.msg("couldn't handle %s" % messageType)
  273. log.msg(repr(payload))
  274. self.sendUnimplemented()
  275. elif self.service:
  276. log.callWithLogger(self.service, self.service.packetReceived,
  277. messageNum, payload)
  278. else:
  279. log.msg("couldn't handle %s" % messageNum)
  280. log.msg(repr(payload))
  281. self.sendUnimplemented()
  282. def ssh_KEXINIT(self, packet):
  283. """
  284. Called when we receive a MSG_KEXINIT message. Payload::
  285. bytes[16] cookie
  286. string keyExchangeAlgorithms
  287. string keyAlgorithms
  288. string incomingEncryptions
  289. string outgoingEncryptions
  290. string incomingAuthentications
  291. string outgoingAuthentications
  292. string incomingCompressions
  293. string outgoingCompressions
  294. string incomingLanguages
  295. string outgoingLanguages
  296. bool firstPacketFollows
  297. unit32 0 (reserved)
  298. Starts setting up the key exchange, keys, encryptions, and
  299. authentications. Extended by ssh_KEXINIT in SSHServerTransport and
  300. SSHClientTransport.
  301. """
  302. self.otherKexInitPayload = chr(MSG_KEXINIT) + packet
  303. #cookie = packet[: 16] # taking this is useless
  304. k = getNS(packet[16:], 10)
  305. strings, rest = k[:-1], k[-1]
  306. (kexAlgs, keyAlgs, encCS, encSC, macCS, macSC, compCS, compSC, langCS,
  307. langSC) = [s.split(',') for s in strings]
  308. # these are the server directions
  309. outs = [encSC, macSC, compSC]
  310. ins = [encCS, macSC, compCS]
  311. if self.isClient:
  312. outs, ins = ins, outs # switch directions
  313. server = (self.supportedKeyExchanges, self.supportedPublicKeys,
  314. self.supportedCiphers, self.supportedCiphers,
  315. self.supportedMACs, self.supportedMACs,
  316. self.supportedCompressions, self.supportedCompressions)
  317. client = (kexAlgs, keyAlgs, outs[0], ins[0], outs[1], ins[1],
  318. outs[2], ins[2])
  319. if self.isClient:
  320. server, client = client, server
  321. self.kexAlg = ffs(client[0], server[0])
  322. self.keyAlg = ffs(client[1], server[1])
  323. self.nextEncryptions = SSHCiphers(
  324. ffs(client[2], server[2]),
  325. ffs(client[3], server[3]),
  326. ffs(client[4], server[4]),
  327. ffs(client[5], server[5]))
  328. self.outgoingCompressionType = ffs(client[6], server[6])
  329. self.incomingCompressionType = ffs(client[7], server[7])
  330. if None in (self.kexAlg, self.keyAlg, self.outgoingCompressionType,
  331. self.incomingCompressionType):
  332. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  333. "couldn't match all kex parts")
  334. return
  335. if None in self.nextEncryptions.__dict__.values():
  336. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  337. "couldn't match all kex parts")
  338. return
  339. log.msg('kex alg, key alg: %s %s' % (self.kexAlg, self.keyAlg))
  340. log.msg('outgoing: %s %s %s' % (self.nextEncryptions.outCipType,
  341. self.nextEncryptions.outMACType,
  342. self.outgoingCompressionType))
  343. log.msg('incoming: %s %s %s' % (self.nextEncryptions.inCipType,
  344. self.nextEncryptions.inMACType,
  345. self.incomingCompressionType))
  346. return kexAlgs, keyAlgs, rest # for SSHServerTransport to use
  347. def ssh_DISCONNECT(self, packet):
  348. """
  349. Called when we receive a MSG_DISCONNECT message. Payload::
  350. long code
  351. string description
  352. This means that the other side has disconnected. Pass the message up
  353. and disconnect ourselves.
  354. """
  355. reasonCode = struct.unpack('>L', packet[: 4])[0]
  356. description, foo = getNS(packet[4:])
  357. self.receiveError(reasonCode, description)
  358. self.transport.loseConnection()
  359. def ssh_IGNORE(self, packet):
  360. """
  361. Called when we receieve a MSG_IGNORE message. No payload.
  362. This means nothing; we simply return.
  363. """
  364. def ssh_UNIMPLEMENTED(self, packet):
  365. """
  366. Called when we receieve a MSG_UNIMPLEMENTED message. Payload::
  367. long packet
  368. This means that the other side did not implement one of our packets.
  369. """
  370. seqnum, = struct.unpack('>L', packet)
  371. self.receiveUnimplemented(seqnum)
  372. def ssh_DEBUG(self, packet):
  373. """
  374. Called when we receieve a MSG_DEBUG message. Payload::
  375. bool alwaysDisplay
  376. string message
  377. string language
  378. This means the other side has passed along some debugging info.
  379. """
  380. alwaysDisplay = bool(packet[0])
  381. message, lang, foo = getNS(packet[1:], 2)
  382. self.receiveDebug(alwaysDisplay, message, lang)
  383. def setService(self, service):
  384. """
  385. Set our service to service and start it running. If we were
  386. running a service previously, stop it first.
  387. @type service: C{SSHService}
  388. """
  389. log.msg('starting service %s' % service.name)
  390. if self.service:
  391. self.service.serviceStopped()
  392. self.service = service
  393. service.transport = self
  394. self.service.serviceStarted()
  395. def sendDebug(self, message, alwaysDisplay=False, language=''):
  396. """
  397. Send a debug message to the other side.
  398. @param message: the message to send.
  399. @type message: C{str}
  400. @param alwaysDisplay: if True, tell the other side to always
  401. display this message.
  402. @type alwaysDisplay: C{bool}
  403. @param language: optionally, the language the message is in.
  404. @type language: C{str}
  405. """
  406. self.sendPacket(MSG_DEBUG, chr(alwaysDisplay) + NS(message) +
  407. NS(language))
  408. def sendIgnore(self, message):
  409. """
  410. Send a message that will be ignored by the other side. This is
  411. useful to fool attacks based on guessing packet sizes in the
  412. encrypted stream.
  413. @param message: data to send with the message
  414. @type message: C{str}
  415. """
  416. self.sendPacket(MSG_IGNORE, NS(message))
  417. def sendUnimplemented(self):
  418. """
  419. Send a message to the other side that the last packet was not
  420. understood.
  421. """
  422. seqnum = self.incomingPacketSequence
  423. self.sendPacket(MSG_UNIMPLEMENTED, struct.pack('!L', seqnum))
  424. def sendDisconnect(self, reason, desc):
  425. """
  426. Send a disconnect message to the other side and then disconnect.
  427. @param reason: the reason for the disconnect. Should be one of the
  428. DISCONNECT_* values.
  429. @type reason: C{int}
  430. @param desc: a descrption of the reason for the disconnection.
  431. @type desc: C{str}
  432. """
  433. self.sendPacket(
  434. MSG_DISCONNECT, struct.pack('>L', reason) + NS(desc) + NS(''))
  435. log.msg('Disconnecting with error, code %s\nreason: %s' % (reason,
  436. desc))
  437. self.transport.loseConnection()
  438. def _getKey(self, c, sharedSecret, exchangeHash):
  439. """
  440. Get one of the keys for authentication/encryption.
  441. @type c: C{str}
  442. @type sharedSecret: C{str}
  443. @type exchangeHash: C{str}
  444. """
  445. k1 = sha.new(sharedSecret + exchangeHash + c + self.sessionID)
  446. k1 = k1.digest()
  447. k2 = sha.new(sharedSecret + exchangeHash + k1).digest()
  448. return k1 + k2
  449. def _keySetup(self, sharedSecret, exchangeHash):
  450. """
  451. Set up the keys for the connection and sends MSG_NEWKEYS when
  452. finished,
  453. @param sharedSecret: a secret string agreed upon using a Diffie-
  454. Hellman exchange, so it is only shared between
  455. the server and the client.
  456. @type sharedSecret: C{str}
  457. @param exchangeHash: A hash of various data known by both sides.
  458. @type exchangeHash: C{str}
  459. """
  460. if not self.sessionID:
  461. self.sessionID = exchangeHash
  462. initIVCS = self._getKey('A', sharedSecret, exchangeHash)
  463. initIVSC = self._getKey('B', sharedSecret, exchangeHash)
  464. encKeyCS = self._getKey('C', sharedSecret, exchangeHash)
  465. encKeySC = self._getKey('D', sharedSecret, exchangeHash)
  466. integKeyCS = self._getKey('E', sharedSecret, exchangeHash)
  467. integKeySC = self._getKey('F', sharedSecret, exchangeHash)
  468. outs = [initIVSC, encKeySC, integKeySC]
  469. ins = [initIVCS, encKeyCS, integKeyCS]
  470. if self.isClient: # reverse for the client
  471. log.msg('REVERSE')
  472. outs, ins = ins, outs
  473. self.nextEncryptions.setKeys(outs[0], outs[1], ins[0], ins[1],
  474. outs[2], ins[2])
  475. self.sendPacket(MSG_NEWKEYS, '')
  476. def isEncrypted(self, direction="out"):
  477. """
  478. Return True if the connection is encrypted in the given direction.
  479. Direction must be one of ["out", "in", "both"].
  480. """
  481. if direction == "out":
  482. return self.currentEncryptions.outCipType != 'none'
  483. elif direction == "in":
  484. return self.currentEncryptions.inCipType != 'none'
  485. elif direction == "both":
  486. return self.isEncrypted("in") and self.isEncrypted("out")
  487. else:
  488. raise TypeError('direction must be "out", "in", or "both"')
  489. def isVerified(self, direction="out"):
  490. """
  491. Return True if the connecction is verified/authenticated in the
  492. given direction. Direction must be one of ["out", "in", "both"].
  493. """
  494. if direction == "out":
  495. return self.currentEncryptions.outMACType != 'none'
  496. elif direction == "in":
  497. return self.currentEncryptions.inMACType != 'none'
  498. elif direction == "both":
  499. return self.isVerified("in")and self.isVerified("out")
  500. else:
  501. raise TypeError('direction must be "out", "in", or "both"')
  502. def loseConnection(self):
  503. """
  504. Lose the connection to the other side, sending a
  505. DISCONNECT_CONNECTION_LOST message.
  506. """
  507. self.sendDisconnect(DISCONNECT_CONNECTION_LOST,
  508. "user closed connection")
  509. # client methods
  510. def receiveError(self, reasonCode, description):
  511. """
  512. Called when we receive a disconnect error message from the other
  513. side.
  514. @param reasonCode: the reason for the disconnect, one of the
  515. DISCONNECT_ values.
  516. @type reasonCode: C{int}
  517. @param description: a human-readable description of the
  518. disconnection.
  519. @type description: C{str}
  520. """
  521. log.msg('Got remote error, code %s\nreason: %s' % (reasonCode,
  522. description))
  523. def receiveUnimplemented(self, seqnum):
  524. """
  525. Called when we receive an unimplemented packet message from the other
  526. side.
  527. @param seqnum: the sequence number that was not understood.
  528. @type seqnum: C{int}
  529. """
  530. log.msg('other side unimplemented packet #%s' % seqnum)
  531. def receiveDebug(self, alwaysDisplay, message, lang):
  532. """
  533. Called when we receive a debug message from the other side.
  534. @param alwaysDisplay: if True, this message should always be
  535. displayed.
  536. @type alwaysDisplay: C{bool}
  537. @param message: the debug message
  538. @type message: C{str}
  539. @param lang: optionally the language the message is in.
  540. @type lang: C{str}
  541. """
  542. if alwaysDisplay:
  543. log.msg('Remote Debug Message: %s' % message)
  544. class SSHServerTransport(SSHTransportBase):
  545. """
  546. SSHServerTransport implements the server side of the SSH protocol.
  547. @ivar isClient: since we are never the client, this is always False.
  548. @ivar ignoreNextPacket: if True, ignore the next key exchange packet. This
  549. is set when the client sends a guessed key exchange packet but with
  550. an incorrect guess.
  551. @ivar dhGexRequest: the KEX_DH_GEX_REQUEST(_OLD) that the client sent.
  552. The key generation needs this to be stored.
  553. @ivar g: the Diffie-Hellman group generator.
  554. @ivar p: the Diffie-Hellman group prime.
  555. """
  556. isClient = False
  557. ignoreNextPacket = 0
  558. def ssh_KEXINIT(self, packet):
  559. """
  560. Called when we receive a MSG_KEXINIT message. For a description
  561. of the packet, see SSHTransportBase.ssh_KEXINIT(). Additionally,
  562. this method checks if a guessed key exchange packet was sent. If
  563. it was sent, and it guessed incorrectly, the next key exchange
  564. packet MUST be ignored.
  565. """
  566. retval = SSHTransportBase.ssh_KEXINIT(self, packet)
  567. if not retval: # disconnected
  568. return
  569. else:
  570. kexAlgs, keyAlgs, rest = retval
  571. if ord(rest[0]): # first_kex_packet_follows
  572. if (kexAlgs[0] != self.supportedKeyExchanges[0] or
  573. keyAlgs[0] != self.supportedPublicKeys[0]):
  574. self.ignoreNextPacket = True # guess was wrong
  575. def ssh_KEX_DH_GEX_REQUEST_OLD(self, packet):
  576. """
  577. This represents two different key exchange methods that share the
  578. same integer value.
  579. KEXDH_INIT (for diffie-hellman-group1-sha1 exchanges) payload::
  580. integer e (the client's Diffie-Hellman public key)
  581. We send the KEXDH_REPLY with our host key and signature.
  582. KEX_DH_GEX_REQUEST_OLD (for diffie-hellman-group-exchange-sha1)
  583. payload::
  584. integer ideal (ideal size for the Diffie-Hellman prime)
  585. We send the KEX_DH_GEX_GROUP message with the group that is
  586. closest in size to ideal.
  587. If we were told to ignore the next key exchange packet by
  588. ssh_KEXINIT, drop it on the floor and return.
  589. """
  590. if self.ignoreNextPacket:
  591. self.ignoreNextPacket = 0
  592. return
  593. if self.kexAlg == 'diffie-hellman-group1-sha1':
  594. # this is really KEXDH_INIT
  595. clientDHpublicKey, foo = getMP(packet)
  596. y = Util.number.getRandomNumber(512, randbytes.secureRandom)
  597. serverDHpublicKey = _MPpow(DH_GENERATOR, y, DH_PRIME)
  598. sharedSecret = _MPpow(clientDHpublicKey, y, DH_PRIME)
  599. h = sha.new()
  600. h.update(NS(self.otherVersionString))
  601. h.update(NS(self.ourVersionString))
  602. h.update(NS(self.otherKexInitPayload))
  603. h.update(NS(self.ourKexInitPayload))
  604. h.update(NS(self.factory.publicKeys[self.keyAlg].blob()))
  605. h.update(MP(clientDHpublicKey))
  606. h.update(serverDHpublicKey)
  607. h.update(sharedSecret)
  608. exchangeHash = h.digest()
  609. self.sendPacket(
  610. MSG_KEXDH_REPLY,
  611. NS(self.factory.publicKeys[self.keyAlg].blob()) +
  612. serverDHpublicKey +
  613. NS(self.factory.privateKeys[self.keyAlg].sign(exchangeHash)))
  614. self._keySetup(sharedSecret, exchangeHash)
  615. elif self.kexAlg == 'diffie-hellman-group-exchange-sha1':
  616. self.dhGexRequest = packet
  617. ideal = struct.unpack('>L', packet)[0]
  618. self.g, self.p = self.factory.getDHPrime(ideal)
  619. self.sendPacket(MSG_KEX_DH_GEX_GROUP, MP(self.p) + MP(self.g))
  620. else:
  621. raise error.ConchError('bad kexalg: %s' % self.kexAlg)
  622. def ssh_KEX_DH_GEX_REQUEST(self, packet):
  623. """
  624. Called when we receive a MSG_KEX_DH_GEX_REQUEST message. Payload::
  625. integer minimum
  626. integer ideal
  627. integer maximum
  628. The client is asking for a Diffie-Hellman group between minimum and
  629. maximum size, and close to ideal if possible. We reply with a
  630. MSG_KEX_DH_GEX_GROUP message.
  631. If we were told to ignore the next key exchange packekt by
  632. ssh_KEXINIT, drop it on the floor and return.
  633. """
  634. if self.ignoreNextPacket:
  635. self.ignoreNextPacket = 0
  636. return
  637. self.dhGexRequest = packet
  638. min, ideal, max = struct.unpack('>3L', packet)
  639. self.g, self.p = self.factory.getDHPrime(ideal)
  640. self.sendPacket(MSG_KEX_DH_GEX_GROUP, MP(self.p) + MP(self.g))
  641. def ssh_KEX_DH_GEX_INIT(self, packet):
  642. """
  643. Called when we get a MSG_KEX_DH_GEX_INIT message. Payload::
  644. integer e (client DH public key)
  645. We send the MSG_KEX_DH_GEX_REPLY message with our host key and
  646. signature.
  647. """
  648. clientDHpublicKey, foo = getMP(packet)
  649. # TODO: we should also look at the value they send to us and reject
  650. # insecure values of f (if g==2 and f has a single '1' bit while the
  651. # rest are '0's, then they must have used a small y also).
  652. # TODO: This could be computed when self.p is set up
  653. # or do as openssh does and scan f for a single '1' bit instead
  654. pSize = Util.number.size(self.p)
  655. y = Util.number.getRandomNumber(pSize, randbytes.secureRandom)
  656. serverDHpublicKey = _MPpow(self.g, y, self.p)
  657. sharedSecret = _MPpow(clientDHpublicKey, y, self.p)
  658. h = sha.new()
  659. h.update(NS(self.otherVersionString))
  660. h.update(NS(self.ourVersionString))
  661. h.update(NS(self.otherKexInitPayload))
  662. h.update(NS(self.ourKexInitPayload))
  663. h.update(NS(self.factory.publicKeys[self.keyAlg].blob()))
  664. h.update(self.dhGexRequest)
  665. h.update(MP(self.p))
  666. h.update(MP(self.g))
  667. h.update(MP(clientDHpublicKey))
  668. h.update(serverDHpublicKey)
  669. h.update(sharedSecret)
  670. exchangeHash = h.digest()
  671. self.sendPacket(
  672. MSG_KEX_DH_GEX_REPLY,
  673. NS(self.factory.publicKeys[self.keyAlg].blob()) +
  674. serverDHpublicKey +
  675. NS(self.factory.privateKeys[self.keyAlg].sign(exchangeHash)))
  676. self._keySetup(sharedSecret, exchangeHash)
  677. def ssh_NEWKEYS(self, packet):
  678. """
  679. Called when we get a MSG_NEWKEYS message. No payload.
  680. When we get this, the keys have been set on both sides, and we
  681. start using them to encrypt and authenticate the connection.
  682. """
  683. log.msg('NEW KEYS')
  684. if packet != '':
  685. self.sendDisconnect(DISCONNECT_PROTOCOL_ERROR,
  686. "NEWKEYS takes no data")
  687. return
  688. self.currentEncryptions = self.nextEncryptions
  689. if self.outgoingCompressionType == 'zlib':
  690. self.outgoingCompression = zlib.compressobj(6)
  691. if self.incomingCompressionType == 'zlib':
  692. self.incomingCompression = zlib.decompressobj()
  693. def ssh_SERVICE_REQUEST(self, packet):
  694. """
  695. Called when we get a MSG_SERVICE_REQUEST message. Payload::
  696. string serviceName
  697. The client has requested a service. If we can start the service,
  698. start it; otherwise, disconnect with
  699. DISCONNECT_SERVICE_NOT_AVAILABLE.
  700. """
  701. service, rest = getNS(packet)
  702. cls = self.factory.getService(self, service)
  703. if not cls:
  704. self.sendDisconnect(DISCONNECT_SERVICE_NOT_AVAILABLE,
  705. "don't have service %s" % service)
  706. return
  707. else:
  708. self.sendPacket(MSG_SERVICE_ACCEPT, NS(service))
  709. self.setService(cls())
  710. class SSHClientTransport(SSHTransportBase):
  711. """
  712. SSHClientTransport implements the client side of the SSH protocol.
  713. @ivar isClient: since we are always the client, this is always True.
  714. @ivar _gotNewKeys: if we receive a MSG_NEWKEYS message before we are
  715. ready to transition to the new keys, this is set to True so we
  716. can transition when the keys are ready locally.
  717. @ivar x: our Diffie-Hellman private key.
  718. @ivar e: our Diffie-Hellman public key.
  719. @ivar g: the Diffie-Hellman group generator.
  720. @ivar p: the Diffie-Hellman group prime
  721. @ivar instance: the SSHService object we are requesting.
  722. """
  723. isClient = True
  724. def connectionMade(self):
  725. """
  726. Called when the connection is started with the server. Just sets
  727. up a private instance variable.
  728. """
  729. SSHTransportBase.connectionMade(self)
  730. self._gotNewKeys = 0
  731. def ssh_KEXINIT(self, packet):
  732. """
  733. Called when we receive a MSG_KEXINIT message. For a description
  734. of the packet, see SSHTransportBase.ssh_KEXINIT(). Additionally,
  735. this method sends the first key exchange packet. If the agreed-upon
  736. exchange is diffie-hellman-group1-sha1, generate a public key
  737. and send it in a MSG_KEXDH_INIT message. If the exchange is
  738. diffie-hellman-group-exchange-sha1, ask for a 2048 bit group with a
  739. MSG_KEX_DH_GEX_REQUEST_OLD message.
  740. """
  741. if SSHTransportBase.ssh_KEXINIT(self, packet) is None:
  742. return # we disconnected
  743. if self.kexAlg == 'diffie-hellman-group1-sha1':
  744. self.x = Util.number.getRandomNumber(512, randbytes.secureRandom)
  745. self.e = _MPpow(DH_GENERATOR, self.x, DH_PRIME)
  746. self.sendPacket(MSG_KEXDH_INIT, self.e)
  747. elif self.kexAlg == 'diffie-hellman-group-exchange-sha1':
  748. self.sendPacket(MSG_KEX_DH_GEX_REQUEST_OLD, '\x00\x00\x08\x00')
  749. else:
  750. raise error.ConchError("somehow, the kexAlg has been set "
  751. "to something we don't support")
  752. def ssh_KEX_DH_GEX_GROUP(self, packet):
  753. """
  754. This handles two different message which share an integer value.
  755. If the key exchange is diffie-hellman-group1-sha1, this is
  756. MSG_KEXDH_REPLY. Payload::
  757. string serverHostKey
  758. integer f (server Diffie-Hellman public key)
  759. string signature
  760. We verify the host key by calling verifyHostKey, then continue in
  761. _continueKEXDH_REPLY.
  762. If the key exchange is diffie-hellman-group-exchange-sha1, this is
  763. MSG_KEX_DH_GEX_GROUP. Payload::
  764. string g (group generator)
  765. string p (group prime)
  766. We generate a Diffie-Hellman public key and send it in a
  767. MSG_KEX_DH_GEX_INIT message.
  768. """
  769. if self.kexAlg == 'diffie-hellman-group1-sha1':
  770. # actually MSG_KEXDH_REPLY
  771. pubKey, packet = getNS(packet)
  772. f, packet = getMP(packet)
  773. signature, packet = getNS(packet)
  774. fingerprint = ':'.join([ch.encode('hex') for ch in
  775. md5.new(pubKey).digest()])
  776. d = self.verifyHostKey(pubKey, fingerprint)
  777. d.addCallback(self._continueKEXDH_REPLY, pubKey, f, signature)
  778. d.addErrback(
  779. lambda unused: self.sendDisconnect(
  780. DISCONNECT_HOST_KEY_NOT_VERIFIABLE, 'bad host key'))
  781. return d
  782. else:
  783. self.p, rest = getMP(packet)
  784. self.g, rest = getMP(rest)
  785. self.x = Util.number.getRandomNumber(320, randbytes.secureRandom)
  786. self.e = _MPpow(self.g, self.x, self.p)
  787. self.sendPacket(MSG_KEX_DH_GEX_INIT, self.e)
  788. def _continueKEXDH_REPLY(self, ignored, pubKey, f, signature):
  789. """
  790. The host key has been verified, so we generate the keys.
  791. @param pubKey: the public key blob for the server's public key.
  792. @type pubKey: C{str}
  793. @param f: the server's Diffie-Hellman public key.
  794. @type f: C{long}
  795. @param signature: the server's signature, verifying that it has the
  796. correct private key.
  797. @type signature: C{str}
  798. """
  799. serverKey = keys.Key.fromString(pubKey)
  800. sharedSecret = _MPpow(f, self.x, DH_PRIME)
  801. h = sha.new()
  802. h.update(NS(self.ourVersionString))
  803. h.update(NS(self.otherVersionString))
  804. h.update(NS(self.ourKexInitPayload))
  805. h.update(NS(self.otherKexInitPayload))
  806. h.update(NS(pubKey))
  807. h.update(self.e)
  808. h.update(MP(f))
  809. h.update(sharedSecret)
  810. exchangeHash = h.digest()
  811. if not serverKey.verify(signature, exchangeHash):
  812. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  813. 'bad signature')
  814. return
  815. self._keySetup(sharedSecret, exchangeHash)
  816. def ssh_KEX_DH_GEX_REPLY(self, packet):
  817. """
  818. Called when we receieve a MSG_KEX_DH_GEX_REPLY message. Payload::
  819. string server host key
  820. integer f (server DH public key)
  821. We verify the host key by calling verifyHostKey, then continue in
  822. _continueGEX_REPLY.
  823. """
  824. pubKey, packet = getNS(packet)
  825. f, packet = getMP(packet)
  826. signature, packet = getNS(packet)
  827. fingerprint = ':'.join(map(lambda c: '%02x'%ord(c),
  828. md5.new(pubKey).digest()))
  829. d = self.verifyHostKey(pubKey, fingerprint)
  830. d.addCallback(self._continueGEX_REPLY, pubKey, f, signature)
  831. d.addErrback(
  832. lambda unused: self.sendDisconnect(
  833. DISCONNECT_HOST_KEY_NOT_VERIFIABLE, 'bad host key'))
  834. return d
  835. def _continueGEX_REPLY(self, ignored, pubKey, f, signature):
  836. """
  837. The host key has been verified, so we generate the keys.
  838. @param pubKey: the public key blob for the server's public key.
  839. @type pubKey: C{str}
  840. @param f: the server's Diffie-Hellman public key.
  841. @type f: C{long}
  842. @param signature: the server's signature, verifying that it has the
  843. correct private key.
  844. @type signature: C{str}
  845. """
  846. serverKey = keys.Key.fromString(pubKey)
  847. sharedSecret = _MPpow(f, self.x, self.p)
  848. h = sha.new()
  849. h.update(NS(self.ourVersionString))
  850. h.update(NS(self.otherVersionString))
  851. h.update(NS(self.ourKexInitPayload))
  852. h.update(NS(self.otherKexInitPayload))
  853. h.update(NS(pubKey))
  854. h.update('\x00\x00\x08\x00')
  855. h.update(MP(self.p))
  856. h.update(MP(self.g))
  857. h.update(self.e)
  858. h.update(MP(f))
  859. h.update(sharedSecret)
  860. exchangeHash = h.digest()
  861. if not serverKey.verify(signature, exchangeHash):
  862. self.sendDisconnect(DISCONNECT_KEY_EXCHANGE_FAILED,
  863. 'bad signature')
  864. return
  865. self._keySetup(sharedSecret, exchangeHash)
  866. def _keySetup(self, sharedSecret, exchangeHash):
  867. """
  868. See SSHTransportBase._keySetup().
  869. """
  870. SSHTransportBase._keySetup(self, sharedSecret, exchangeHash)
  871. if self._gotNewKeys:
  872. self.ssh_NEWKEYS('')
  873. def ssh_NEWKEYS(self, packet):
  874. """
  875. Called when we receieve a MSG_NEWKEYS message. No payload.
  876. If we've finished setting up our own keys, start using them.
  877. Otherwise, remeber that we've receieved this message.
  878. """
  879. if packet != '':
  880. self.sendDisconnect(DISCONNECT_PROTOCOL_ERROR,
  881. "NEWKEYS takes no data")
  882. return
  883. if not self.nextEncryptions.encBlockSize:
  884. self._gotNewKeys = 1
  885. return
  886. log.msg('NEW KEYS')
  887. self.currentEncryptions = self.nextEncryptions
  888. if self.outgoingCompressionType == 'zlib':
  889. self.outgoingCompression = zlib.compressobj(6)
  890. if self.incomingCompressionType == 'zlib':
  891. self.incomingCompression = zlib.decompressobj()
  892. self.connectionSecure()
  893. def ssh_SERVICE_ACCEPT(self, packet):
  894. """
  895. Called when we receieve a MSG_SERVICE_ACCEPT message. Payload::
  896. string service name
  897. Start the service we requested.
  898. """
  899. name = getNS(packet)[0]
  900. if name != self.instance.name:
  901. self.sendDisconnect(
  902. DISCONNECT_PROTOCOL_ERROR,
  903. "received accept for service we did not request")
  904. self.setService(self.instance)
  905. def requestService(self, instance):
  906. """
  907. Request that a service be run over this transport.
  908. @type instance: subclass of L{twisted.conch.ssh.service.SSHService}
  909. """
  910. self.sendPacket(MSG_SERVICE_REQUEST, NS(instance.name))
  911. self.instance = instance
  912. # client methods
  913. def verifyHostKey(self, hostKey, fingerprint):
  914. """
  915. Returns a Deferred that gets a callback if it is a valid key, or
  916. an errback if not.
  917. @type hostKey: C{str}
  918. @type fingerprint: C{str}
  919. @rtype: L{twisted.internet.defer.Deferred}
  920. """
  921. # return if it's good
  922. return defer.fail(NotImplementedError())
  923. def connectionSecure(self):
  924. """
  925. Called when the encryption has been set up. Generally,
  926. requestService() is called to run another service over the transport.
  927. """
  928. raise NotImplementedError()
  929. class _DummyCipher:
  930. """
  931. A cipher for the none encryption method.
  932. @ivar block_size: the block size of the encryption. In the case of the
  933. none cipher, this is 8 bytes.
  934. """
  935. block_size = 8
  936. def encrypt(self, x):
  937. return x
  938. decrypt = encrypt
  939. class SSHCiphers:
  940. """
  941. SSHCiphers represents all the encryption operations that need to occur
  942. to encrypt and authenticate the SSH connection.
  943. @cvar cipherMap: A dictionary mapping SSH encryption names to 3-tuples of
  944. (<Crypto.Cipher.* name>, <block size>, <counter mode>)
  945. @cvar macMap: A dictionary mapping SSH MAC names to hash modules.
  946. @ivar outCipType: the string type of the outgoing cipher.
  947. @ivar inCipType: the string type of the incoming cipher.
  948. @ivar outMACType: the string type of the incoming MAC.
  949. @ivar inMACType: the string type of the incoming MAC.
  950. @ivar encBlockSize: the block size of the outgoing cipher.
  951. @ivar decBlockSize: the block size of the incoming cipher.
  952. @ivar verifyDigestSize: the size of the incoming MAC.
  953. @ivar outMAC: a tuple of (<hash module>, <inner key>, <outer key>,
  954. <digest size>) representing the outgoing MAC.
  955. @ivar inMAc: see outMAC, but for the incoming MAC.
  956. """
  957. cipherMap = {
  958. '3des-cbc':('DES3', 24, 0),
  959. 'blowfish-cbc':('Blowfish', 16,0 ),
  960. 'aes256-cbc':('AES', 32, 0),
  961. 'aes192-cbc':('AES', 24, 0),
  962. 'aes128-cbc':('AES', 16, 0),
  963. 'cast128-cbc':('CAST', 16, 0),
  964. 'aes128-ctr':('AES', 16, 1),
  965. 'aes192-ctr':('AES', 24, 1),
  966. 'aes256-ctr':('AES', 32, 1),
  967. '3des-ctr':('DES3', 24, 1),
  968. 'blowfish-ctr':('Blowfish', 16, 1),
  969. 'cast128-ctr':('CAST', 16, 1),
  970. 'none':(None, 0, 0),
  971. }
  972. macMap = {
  973. 'hmac-sha1': sha,
  974. 'hmac-md5': md5,
  975. 'none':None
  976. }
  977. def __init__(self, outCip, inCip, outMac, inMac):
  978. self.outCipType = outCip
  979. self.inCipType = inCip
  980. self.outMACType = outMac
  981. self.inMACType = inMac
  982. self.encBlockSize = 0
  983. self.decBlockSize = 0
  984. self.verifyDigestSize = 0
  985. self.outMAC = (None, '', '', 0)
  986. self.inMAC = (None, '', '', 0)
  987. def setKeys(self, outIV, outKey, inIV, inKey, outInteg, inInteg):
  988. """
  989. Set up the ciphers and hashes using the given keys,
  990. @param outIV: the outgoing initialization vector
  991. @param outKey: the outgoing encryption key
  992. @param inIV: the incoming initialization vector
  993. @param inKey: the incoming encryption key
  994. @param outInteg: the outgoing integrity key
  995. @param inInteg: the incoming integrity key.
  996. """
  997. o = self._getCipher(self.outCipType, outIV, outKey)
  998. self.encrypt = o.encrypt
  999. self.encBlockSize = o.block_size
  1000. o = self._getCipher(self.inCipType, inIV, inKey)
  1001. self.decrypt = o.decrypt
  1002. self.decBlockSize = o.block_size
  1003. self.outMAC = self._getMAC(self.outMACType, outInteg)
  1004. self.inMAC = self._getMAC(self.inMACType, inInteg)
  1005. if self.inMAC:
  1006. self.verifyDigestSize = self.inMAC[3]
  1007. def _getCipher(self, cip, iv, key):
  1008. """
  1009. Creates an initialized cipher object.
  1010. @param cip: the name of the cipher: maps into Crypto.Cipher.*
  1011. @param iv: the initialzation vector
  1012. @param key: the encryption key
  1013. """
  1014. modName, keySize, counterMode = self.cipherMap[cip]
  1015. if not modName: # no cipher
  1016. return _DummyCipher()
  1017. mod = __import__('Crypto.Cipher.%s'%modName, {}, {}, 'x')
  1018. if counterMode:
  1019. return mod.new(key[:keySize], mod.MODE_CTR, iv[:mod.block_size],
  1020. counter=_Counter(iv, mod.block_size))
  1021. else:
  1022. return mod.new(key[:keySize], mod.MODE_CBC, iv[:mod.block_size])
  1023. def _getMAC(self, mac, key):
  1024. """
  1025. Gets a 4-tuple representing the message authentication code.
  1026. (<hash module>, <inner hash value>, <outer hash value>,
  1027. <digest size>)
  1028. @param mac: a key mapping into macMap
  1029. @type mac: C{str}
  1030. @param key: the MAC key.
  1031. @type key: C{str}
  1032. """
  1033. mod = self.macMap[mac]
  1034. if not mod:
  1035. return (None, '', '', 0)
  1036. #if not hasattr(mod, 'digest_size'):
  1037. # ds = len(mod.new().digest())
  1038. #else:
  1039. ds = mod.digest_size
  1040. key = key[:ds] + '\x00' * (64 - ds)
  1041. i = XOR.new('\x36').encrypt(key)
  1042. o = XOR.new('\x5c').encrypt(key)
  1043. return mod, i, o, ds
  1044. def encrypt(self, blocks):
  1045. """
  1046. Encrypt blocks. Overridden by the encrypt method of a
  1047. Crypto.Cipher.* object in setKeys().
  1048. @type blocks: C{str}
  1049. """
  1050. raise NotImplementedError()
  1051. def decrypt(self, blocks):
  1052. """
  1053. Decrypt blocks. See encrypt().
  1054. @type blocks: C{str}
  1055. """
  1056. raise NotImplementedError()
  1057. def makeMAC(self, seqid, data):
  1058. """
  1059. Create a message authentication code (MAC) for the given packet using
  1060. the outgoing MAC values.
  1061. @param seqid: the sequence ID of the outgoing packet
  1062. @type seqid: C{int}
  1063. @param data: the data to create a MAC for
  1064. @type data: C{str}
  1065. @rtype: C{str}
  1066. """
  1067. if not self.outMAC[0]:
  1068. return ''
  1069. data = struct.pack('>L', seqid) + data
  1070. mod, i, o, ds = self.outMAC
  1071. inner = mod.new(i + data)
  1072. outer = mod.new(o + inner.digest())
  1073. return outer.digest()
  1074. def verify(self, seqid, data, mac):
  1075. """
  1076. Verify an incoming MAC using the incoming MAC values. Return True
  1077. if the MAC is valid.
  1078. @param seqid: the sequence ID of the incoming packet
  1079. @type seqid: C{int}
  1080. @param data: the packet data to verify
  1081. @type data: C{str}
  1082. @param mac: the MAC sent with the packet
  1083. @type mac: C{str}
  1084. @rtype: C{bool}
  1085. """
  1086. if not self.inMAC[0]:
  1087. return mac == ''
  1088. data = struct.pack('>L', seqid) + data
  1089. mod, i, o, ds = self.inMAC
  1090. inner = mod.new(i + data)
  1091. outer = mod.new(o + inner.digest())
  1092. return mac == outer.digest()
  1093. class _Counter:
  1094. """
  1095. Stateful counter which returns results packed in a byte string
  1096. """
  1097. def __init__(self, initialVector, blockSize):
  1098. """
  1099. @type initialVector: C{str}
  1100. @param initialVector: A byte string representing the initial counter
  1101. value.
  1102. @type blockSize: C{int}
  1103. @param blockSize: The length of the output buffer, as well as the
  1104. number of bytes at the beginning of C{initialVector} to consider.
  1105. """
  1106. initialVector = initialVector[:blockSize]
  1107. self.count = getMP('\xff\xff\xff\xff' + initialVector)[0]
  1108. self.blockSize = blockSize
  1109. self.count = Util.number.long_to_bytes(self.count - 1)
  1110. self.count = '\x00' * (self.blockSize - len(self.count)) + self.count
  1111. self.count = array.array('c', self.count)
  1112. self.len = len(self.count) - 1
  1113. def __call__(self):
  1114. """
  1115. Increment the counter and return the new value.
  1116. """
  1117. i = self.len
  1118. while i > -1:
  1119. self.count[i] = n = chr((ord(self.count[i]) + 1) % 256)
  1120. if n == '\x00':
  1121. i -= 1
  1122. else:
  1123. return self.count.tostring()
  1124. self.count = array.array('c', '\x00' * self.blockSize)
  1125. return self.count.tostring()
  1126. # Diffie-Hellman primes from Oakley Group 2 [RFC 2409]
  1127. DH_PRIME = long('17976931348623159077083915679378745319786029604875601170644'
  1128. '442368419718021615851936894783379586492554150218056548598050364644054819923'
  1129. '9100050792877003355816639