Home | Trees | Indices | Help |
---|
|
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 3840 41 name = 'diffie-hellman-group1-sha1' 42 4813750 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)6365 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 7476 # 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 self.transport.randpool.stir() 83 x_bytes = self.transport.randpool.get_bytes(128) 84 x_bytes = chr(ord(x_bytes[0]) & 0x7f) + x_bytes[1:] 85 if (x_bytes[:8] != '\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF') and \ 86 (x_bytes[:8] != '\x00\x00\x00\x00\x00\x00\x00\x00'): 87 break 88 self.x = util.inflate_long(x_bytes)8991 # client mode 92 host_key = m.get_string() 93 self.f = m.get_mpint() 94 if (self.f < 1) or (self.f > P - 1): 95 raise SSHException('Server kex "f" is out of range') 96 sig = m.get_string() 97 K = pow(self.f, self.x, P) 98 # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) 99 hm = Message() 100 hm.add(self.transport.local_version, self.transport.remote_version, 101 self.transport.local_kex_init, self.transport.remote_kex_init) 102 hm.add_string(host_key) 103 hm.add_mpint(self.e) 104 hm.add_mpint(self.f) 105 hm.add_mpint(K) 106 self.transport._set_K_H(K, SHA.new(str(hm)).digest()) 107 self.transport._verify_key(host_key, sig) 108 self.transport._activate_outbound()109111 # server mode 112 self.e = m.get_mpint() 113 if (self.e < 1) or (self.e > P - 1): 114 raise SSHException('Client kex "e" is out of range') 115 K = pow(self.e, self.x, P) 116 key = str(self.transport.get_server_key()) 117 # okay, build up the hash H of (V_C || V_S || I_C || I_S || K_S || e || f || K) 118 hm = Message() 119 hm.add(self.transport.remote_version, self.transport.local_version, 120 self.transport.remote_kex_init, self.transport.local_kex_init) 121 hm.add_string(key) 122 hm.add_mpint(self.e) 123 hm.add_mpint(self.f) 124 hm.add_mpint(K) 125 H = SHA.new(str(hm)).digest() 126 self.transport._set_K_H(K, H) 127 # sign it 128 sig = self.transport.get_server_key().sign_ssh_data(self.transport.randpool, H) 129 # send reply 130 m = Message() 131 m.add_byte(chr(_MSG_KEXDH_REPLY)) 132 m.add_string(key) 133 m.add_mpint(self.f) 134 m.add_string(str(sig)) 135 self.transport._send_message(m) 136 self.transport._activate_outbound()
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Sun Nov 1 22:14:17 2009 | http://epydoc.sourceforge.net |