1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """
20 Useful functions used by the rest of paramiko.
21 """
22
23 from __future__ import generators
24
25 import array
26 from binascii import hexlify, unhexlify
27 import errno
28 import sys
29 import struct
30 import traceback
31 import threading
32 import logging
33
34 from paramiko.common import DEBUG, zero_byte, xffffffff, max_byte
35 from paramiko.py3compat import PY2, long, byte_ord, b, byte_chr
36 from paramiko.config import SSHConfig
37
38
40 """turns a normalized byte string into a long-int (adapted from Crypto.Util.number)"""
41 out = long(0)
42 negative = 0
43 if not always_positive and (len(s) > 0) and (byte_ord(s[0]) >= 0x80):
44 negative = 1
45 if len(s) % 4:
46 filler = zero_byte
47 if negative:
48 filler = max_byte
49
50
51 s = filler * (4 - len(s) % 4) + s
52 for i in range(0, len(s), 4):
53 out = (out << 32) + struct.unpack('>I', s[i:i+4])[0]
54 if negative:
55 out -= (long(1) << (8 * len(s)))
56 return out
57
58 deflate_zero = zero_byte if PY2 else 0
59 deflate_ff = max_byte if PY2 else 0xff
60
61
63 """turns a long-int into a normalized byte string (adapted from Crypto.Util.number)"""
64
65 s = bytes()
66 n = long(n)
67 while (n != 0) and (n != -1):
68 s = struct.pack('>I', n & xffffffff) + s
69 n >>= 32
70
71 for i in enumerate(s):
72 if (n == 0) and (i[1] != deflate_zero):
73 break
74 if (n == -1) and (i[1] != deflate_ff):
75 break
76 else:
77
78 i = (0,)
79 if n == 0:
80 s = zero_byte
81 else:
82 s = max_byte
83 s = s[i[0]:]
84 if add_sign_padding:
85 if (n == 0) and (byte_ord(s[0]) >= 0x80):
86 s = zero_byte + s
87 if (n == -1) and (byte_ord(s[0]) < 0x80):
88 s = max_byte + s
89 return s
90
91
101
102
107
108
110 return hexlify(s).upper()
111
112
115
116
118 out = ''
119 for c in s:
120 if (byte_ord(c) >= 32) and (byte_ord(c) <= 127):
121 out += c
122 else:
123 out += '%%%02X' % byte_ord(c)
124 return out
125
126
128 try:
129 return n.bitlength()
130 except AttributeError:
131 norm = deflate_long(n, False)
132 hbyte = byte_ord(norm[0])
133 if hbyte == 0:
134 return 1
135 bitlen = len(norm) * 8
136 while not (hbyte & 0x80):
137 hbyte <<= 1
138 bitlen -= 1
139 return bitlen
140
141
143 return ''.join(traceback.format_exception(*sys.exc_info())).split('\n')
144
145
147 """
148 Given a password, passphrase, or other human-source key, scramble it
149 through a secure hash into some keyworthy bytes. This specific algorithm
150 is used for encrypting/decrypting private key files.
151
152 :param function hash_alg: A function which creates a new hash object, such
153 as ``hashlib.sha256``.
154 :param salt: data to salt the hash with.
155 :type salt: byte string
156 :param str key: human-entered password or passphrase.
157 :param int nbytes: number of bytes to generate.
158 :return: Key data `str`
159 """
160 keydata = bytes()
161 digest = bytes()
162 if len(salt) > 8:
163 salt = salt[:8]
164 while nbytes > 0:
165 hash_obj = hash_alg()
166 if len(digest) > 0:
167 hash_obj.update(digest)
168 hash_obj.update(b(key))
169 hash_obj.update(salt)
170 digest = hash_obj.digest()
171 size = min(nbytes, len(digest))
172 keydata += digest[:size]
173 nbytes -= size
174 return keydata
175
176
178 """
179 Read a file of known SSH host keys, in the format used by openssh, and
180 return a compound dict of ``hostname -> keytype ->`` `PKey
181 <paramiko.pkey.PKey>`. The hostname may be an IP address or DNS name. The
182 keytype will be either ``"ssh-rsa"`` or ``"ssh-dss"``.
183
184 This type of file unfortunately doesn't exist on Windows, but on posix,
185 it will usually be stored in ``os.path.expanduser("~/.ssh/known_hosts")``.
186
187 Since 1.5.3, this is just a wrapper around `.HostKeys`.
188
189 :param str filename: name of the file to read host keys from
190 :return:
191 nested dict of `.PKey` objects, indexed by hostname and then keytype
192 """
193 from paramiko.hostkeys import HostKeys
194 return HostKeys(filename)
195
196
204
205
207 """
208 Provided only as a backward-compatible wrapper around `.SSHConfig`.
209 """
210 return config.lookup(hostname)
211
212
214
215 u1, u2, u3 = 1, 0, m
216 v1, v2, v3 = 0, 1, x
217
218 while v3 > 0:
219 q = u3 // v3
220 u1, v1 = v1, u1 - v1 * q
221 u2, v2 = v2, u2 - v2 * q
222 u3, v3 = v3, u3 - v3 * q
223 if u2 < 0:
224 u2 += m
225 return u2
226
227 _g_thread_ids = {}
228 _g_thread_counter = 0
229 _g_thread_lock = threading.Lock()
230
231
245
246
248 """send paramiko logs to a logfile, if they're not already going somewhere"""
249 l = logging.getLogger("paramiko")
250 if len(l.handlers) > 0:
251 return
252 l.setLevel(level)
253 f = open(filename, 'w')
254 lh = logging.StreamHandler(f)
255 lh.setFormatter(logging.Formatter('%(levelname)-.3s [%(asctime)s.%(msecs)03d] thr=%(_threadid)-3d %(name)s: %(message)s',
256 '%Y%m%d-%H:%M:%S'))
257 l.addHandler(lh)
258
259
260
265 _pfilter = PFilter()
266
267
269 l = logging.getLogger(name)
270 l.addFilter(_pfilter)
271 return l
272
273
275 """Retries function until it doesn't raise an EINTR error"""
276 while True:
277 try:
278 return function()
279 except EnvironmentError as e:
280 if e.errno != errno.EINTR:
281 raise
282
283
285 """Stateful counter for CTR mode crypto"""
287 self.blocksize = nbits / 8
288 self.overflow = overflow
289
290
291 if initial_value == 0:
292 self.value = array.array('c', max_byte * self.blocksize)
293 else:
294 x = deflate_long(initial_value - 1, add_sign_padding=False)
295 self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x)
296
298 """Increament the counter and return the new value"""
299 i = self.blocksize - 1
300 while i > -1:
301 c = self.value[i] = byte_chr((byte_ord(self.value[i]) + 1) % 256)
302 if c != zero_byte:
303 return self.value.tostring()
304 i -= 1
305
306 x = deflate_long(self.overflow, add_sign_padding=False)
307 self.value = array.array('c', zero_byte * (self.blocksize - len(x)) + x)
308 return self.value.tostring()
309
310 - def new(cls, nbits, initial_value=long(1), overflow=long(0)):
311 return cls(nbits, initial_value=initial_value, overflow=overflow)
312 new = classmethod(new)
313
314
316 if len(a) != len(b):
317 return False
318 res = 0
319
320 for i in (xrange if PY2 else range)(len(a)):
321 res |= byte_ord(a[i]) ^ byte_ord(b[i])
322 return res == 0
323