Coverage for /pythoncovmergedfiles/medio/medio/src/paramiko/paramiko/kex_group14.py: 26%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com>
2# Copyright (C) 2013 Torsten Landschoff <torsten@debian.org>
3#
4# This file is part of paramiko.
5#
6# Paramiko is free software; you can redistribute it and/or modify it under the
7# terms of the GNU Lesser General Public License as published by the Free
8# Software Foundation; either version 2.1 of the License, or (at your option)
9# any later version.
10#
11# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY
12# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14# details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20"""
21Standard SSH key exchange ("kex" if you wanna sound cool). Diffie-Hellman of
221024 bit key halves, using a known "p" prime and "g" generator.
23"""
25import os
26from hashlib import sha256
28from paramiko import util
29from paramiko.common import byte_chr, byte_mask, max_byte, zero_byte
30from paramiko.message import Message
31from paramiko.ssh_exception import SSHException
33_MSG_KEXDH_INIT, _MSG_KEXDH_REPLY = range(30, 32)
34c_MSG_KEXDH_INIT, c_MSG_KEXDH_REPLY = [byte_chr(c) for c in range(30, 32)]
36b7fffffffffffffff = byte_chr(0x7F) + max_byte * 7
37b0000000000000000 = zero_byte * 8
40class KexGroup14SHA256:
41 # http://tools.ietf.org/html/rfc3526#section-3
42 P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF # noqa
43 G = 2
45 name = "diffie-hellman-group14-sha256"
46 hash_algo = sha256
48 def __init__(self, transport):
49 self.transport = transport
50 self.x = 0
51 self.e = 0
52 self.f = 0
54 def start_kex(self):
55 self._generate_x()
56 if self.transport.server_mode:
57 # compute f = g^x mod p, but don't send it yet
58 self.f = pow(self.G, self.x, self.P)
59 self.transport._expect_packet(_MSG_KEXDH_INIT)
60 return
61 # compute e = g^x mod p (where g=2), and send it
62 self.e = pow(self.G, self.x, self.P)
63 m = Message()
64 m.add_byte(c_MSG_KEXDH_INIT)
65 m.add_mpint(self.e)
66 self.transport._send_message(m)
67 self.transport._expect_packet(_MSG_KEXDH_REPLY)
69 def parse_next(self, ptype, m):
70 if self.transport.server_mode and (ptype == _MSG_KEXDH_INIT):
71 return self._parse_kexdh_init(m)
72 elif not self.transport.server_mode and (ptype == _MSG_KEXDH_REPLY):
73 return self._parse_kexdh_reply(m)
74 msg = "KexGroup14SHA256 asked to handle packet type {:d}"
75 raise SSHException(msg.format(ptype))
77 # ...internals...
79 def _generate_x(self):
80 # generate an "x" (1 < x < q), where q is (p-1)/2.
81 # p is a 128-byte (1024-bit) number, where the first 64 bits are 1.
82 # therefore q can be approximated as a 2^1023. we drop the subset of
83 # potential x where the first 63 bits are 1, because some of those
84 # will be larger than q (but this is a tiny tiny subset of
85 # potential x).
86 while 1:
87 x_bytes = os.urandom(128)
88 x_bytes = byte_mask(x_bytes[0], 0x7F) + x_bytes[1:]
89 if (
90 x_bytes[:8] != b7fffffffffffffff
91 and x_bytes[:8] != b0000000000000000
92 ):
93 break
94 self.x = util.inflate_long(x_bytes)
96 def _parse_kexdh_reply(self, m):
97 # client mode
98 host_key = m.get_string()
99 self.f = m.get_mpint()
100 if (self.f < 1) or (self.f > self.P - 1):
101 raise SSHException('Server kex "f" is out of range')
102 sig = m.get_binary()
103 K = pow(self.f, self.x, self.P)
104 # okay, build up the hash H of
105 # (V_C || V_S || I_C || I_S || K_S || e || f || K)
106 hm = Message()
107 hm.add(
108 self.transport.local_version,
109 self.transport.remote_version,
110 self.transport.local_kex_init,
111 self.transport.remote_kex_init,
112 )
113 hm.add_string(host_key)
114 hm.add_mpint(self.e)
115 hm.add_mpint(self.f)
116 hm.add_mpint(K)
117 self.transport._set_K_H(K, self.hash_algo(hm.asbytes()).digest())
118 self.transport._verify_key(host_key, sig)
119 self.transport._activate_outbound()
121 def _parse_kexdh_init(self, m):
122 # server mode
123 self.e = m.get_mpint()
124 if (self.e < 1) or (self.e > self.P - 1):
125 raise SSHException('Client kex "e" is out of range')
126 K = pow(self.e, self.x, self.P)
127 key = self.transport.get_server_key().asbytes()
128 # okay, build up the hash H of
129 # (V_C || V_S || I_C || I_S || K_S || e || f || K)
130 hm = Message()
131 hm.add(
132 self.transport.remote_version,
133 self.transport.local_version,
134 self.transport.remote_kex_init,
135 self.transport.local_kex_init,
136 )
137 hm.add_string(key)
138 hm.add_mpint(self.e)
139 hm.add_mpint(self.f)
140 hm.add_mpint(K)
141 H = self.hash_algo(hm.asbytes()).digest()
142 self.transport._set_K_H(K, H)
143 # sign it
144 sig = self.transport.get_server_key().sign_ssh_data(
145 H, self.transport.host_key_type
146 )
147 # send reply
148 m = Message()
149 m.add_byte(c_MSG_KEXDH_REPLY)
150 m.add_string(key)
151 m.add_mpint(self.f)
152 m.add_string(sig)
153 self.transport._send_message(m)
154 self.transport._activate_outbound()