paramiko.kex_group1

Package paramiko :: Module kex_group1 |
[frames] | no frames]

# Source Code for Module paramiko.kex_group1

      1  # Copyright (C) 2003-2007  Robey Pointer <robeypointer@gmail.com>
      2  #
      3  # This file is part of paramiko.
      4  #
      5  # Paramiko is free software; you can redistribute it and/or modify it under the
      6  # terms of the GNU Lesser General Public License as published by the Free
      7  # Software Foundation; either version 2.1 of the License, or (at your option)
      8  # any later version.
      9  #
     10  # Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY
     11  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     12  # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
     13  # details.
     14  #
     15  # You should have received a copy of the GNU Lesser General Public License
     16  # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
     17  # 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
     18
     19  """
     20  Standard SSH key exchange ("kex" if you wanna sound cool).  Diffie-Hellman of
     21  1024 bit key halves, using a known "p" prime and "g" generator.
     22  """
     23
     24  from Crypto.Hash import SHA
     25
     26  from paramiko.common import *
     27  from paramiko import util
     28  from paramiko.message import Message
     29  from paramiko.ssh_exception import SSHException
     30
     31
     32  _MSG_KEXDH_INIT, _MSG_KEXDH_REPLY = range(30, 32)
     33
     34  # draft-ietf-secsh-transport-09.txt, page 17
     35  P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFFL
     36  G = 2
     37
     38
     39 -class KexGroup1(object):
     40
     41      name = 'diffie-hellman-group1-sha1'
     42
     43 -    def __init__(self, transport):
     44          self.transport = transport
     45          self.x = 0L
     46          self.e = 0L
     47          self.f = 0L
     48
     49 -    def start_kex(self):
     50          self._generate_x()
     51          if self.transport.server_mode:
     52              # compute f = g^x mod p, but don't send it yet
     53              self.f = pow(G, self.x, P)
     54              self.transport._expect_packet(_MSG_KEXDH_INIT)
     55              return
     56          # compute e = g^x mod p (where g=2), and send it
     57          self.e = pow(G, self.x, P)
     58          m = Message()
     59          m.add_byte(chr(_MSG_KEXDH_INIT))
     60          m.add_mpint(self.e)
     61          self.transport._send_message(m)
     62          self.transport._expect_packet(_MSG_KEXDH_REPLY)
     63
     64 -    def parse_next(self, ptype, m):
     65          if self.transport.server_mode and (ptype == _MSG_KEXDH_INIT):
     66              return self._parse_kexdh_init(m)
     67          elif not self.transport.server_mode and (ptype == _MSG_KEXDH_REPLY):
     68              return self._parse_kexdh_reply(m)
     69          raise SSHException('KexGroup1 asked to handle packet type %d' % ptype)
     70
     71
     72      ###  internals...
     73
     74
     75 -    def _generate_x(self):
     76          # generate an "x" (1 < x < q), where q is (p-1)/2.
     77          # p is a 128-byte (1024-bit) number, where the first 64 bits are 1.
     78          # therefore q can be approximated as a 2^1023.  we drop the subset of
     79          # potential x where the first 63 bits are 1, because some of those will be
     80          # larger than q (but this is a tiny tiny subset of potential x).
     81          while 1:
     82              x_bytes = self.transport.rng.read(128)
     83              x_bytes = chr(ord(x_bytes[0]) & 0x7f) + x_bytes[1:]
     84              if (x_bytes[:8] != '\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF') and \
     85                     (x_bytes[:8] != '\x00\x00\x00\x00\x00\x00\x00\x00'):
     86                  break
     87          self.x = util.inflate_long(x_bytes)
     88
     89 -    def _parse_kexdh_reply(self, m):
     90          # client mode
     91          host_key = m.get_string()
     92          self.f = m.get_mpint()
     93          if (self.f < 1) or (self.f > P - 1):
     94              raise SSHException('Server kex "f" is out of range')
     95          sig = m.get_string()
     96          K = pow(self.f, self.x, P)
     97          # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K)
     98          hm = Message()
     99          hm.add(self.transport.local_version, self.transport.remote_version,
    100                 self.transport.local_kex_init, self.transport.remote_kex_init)
    101          hm.add_string(host_key)
    102          hm.add_mpint(self.e)
    103          hm.add_mpint(self.f)
    104          hm.add_mpint(K)
    105          self.transport._set_K_H(K, SHA.new(str(hm)).digest())
    106          self.transport._verify_key(host_key, sig)
    107          self.transport._activate_outbound()
    108
    109 -    def _parse_kexdh_init(self, m):
    110          # server mode
    111          self.e = m.get_mpint()
    112          if (self.e < 1) or (self.e > P - 1):
    113              raise SSHException('Client kex "e" is out of range')
    114          K = pow(self.e, self.x, P)
    115          key = str(self.transport.get_server_key())
    116          # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K)
    117          hm = Message()
    118          hm.add(self.transport.remote_version, self.transport.local_version,
    119                 self.transport.remote_kex_init, self.transport.local_kex_init)
    120          hm.add_string(key)
    121          hm.add_mpint(self.e)
    122          hm.add_mpint(self.f)
    123          hm.add_mpint(K)
    124          H = SHA.new(str(hm)).digest()
    125          self.transport._set_K_H(K, H)
    126          # sign it
    127          sig = self.transport.get_server_key().sign_ssh_data(self.transport.rng, H)
    128          # send reply
    129          m = Message()
    130          m.add_byte(chr(_MSG_KEXDH_REPLY))
    131          m.add_string(key)
    132          m.add_mpint(self.f)
    133          m.add_string(str(sig))
    134          self.transport._send_message(m)
    135          self.transport._activate_outbound()
    136