1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-07-01 03:22:48 -05:00

Migrate all Python scripts to Python 3.

Most of them are now _mandatory_ P3 scripts, because I'm tired of
maintaining everything to be compatible with both versions.

The current exceptions are gdb.py (which has to live with whatever gdb
gives it), and kh2reg.py (which is actually designed for other people
to use, and some of them might still be stuck on P2 for the moment).
This commit is contained in:
Simon Tatham
2020-03-04 21:23:49 +00:00
parent cdffb995df
commit 2ec2b796ed
15 changed files with 116 additions and 93 deletions

View File

@ -4,14 +4,21 @@
# The idea of this is that you can use it to manually construct key
# exchange sequences of interesting kinds, for testing purposes.
import struct, random
import sys
import struct
import random
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
def tobytes(s):
return s if isinstance(s, bytes) else s.encode('ASCII')
def boolean(b):
return "\1" if b else "\0"
return b"\1" if b else b"\0"
def byte(b):
assert 0 <= b < 0x100
return chr(b)
return bytes([b])
def uint32(u):
assert 0 <= u < 0x100000000
@ -22,25 +29,24 @@ def uint64(u):
return struct.pack(">L", u)
def string(s):
return uint32(len(s)) + s
return uint32(len(s)) + tobytes(s)
def mpint(m):
s = ""
lastbyte = 0
s = []
while m > 0:
lastbyte = m & 0xFF
s = chr(lastbyte) + s
s.append(m & 0xFF)
m >>= 8
if lastbyte & 0x80:
s = "\0" + s
return string(s)
if len(s) > 0 and (s[-1] & 0x80):
s.append(0)
s.reverse()
return string(bytes(s))
def name_list(ns):
s = ""
for n in ns:
assert "," not in n
if s != "":
s += ","
s = b""
for n in map(tobytes, ns):
assert b"," not in n
if s != b"":
s += b","
s += n
return string(s)
@ -52,7 +58,7 @@ def ssh_rsa_signature_blob(signature):
def greeting(string):
# Greeting at the start of an SSH connection.
return string + "\r\n"
return tobytes(string) + b"\r\n"
# Packet types.
SSH2_MSG_DISCONNECT = 1
@ -122,8 +128,5 @@ def decode_uint32(s):
def read_clearpkt(fh):
length_field = fh.read(4)
s = fh.read(decode_uint32(length_field))
import sys
padlen = ord(s[0])
s = s[1:-padlen]
msgtype = ord(s[0])
return msgtype, s[1:]
padlen, msgtype = s[:2]
return msgtype, s[2:-padlen]

View File

@ -1,4 +1,4 @@
#! /usr/bin/env python
#!/usr/bin/env python3
# Convert OpenSSH known_hosts and known_hosts2 files to "new format" PuTTY
# host keys.

View File

@ -1,9 +1,11 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import sys
import string
from collections import namedtuple
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
class Multiprecision(object):
def __init__(self, target, minval, maxval, words):
self.target = target
@ -89,21 +91,21 @@ class Multiprecision(object):
for i in range(nwords):
srcpos = i * self.target.bits + start
maxbits = min(self.target.bits, start + bits - srcpos)
wordindex = srcpos / self.target.bits
wordindex = srcpos // self.target.bits
if srcpos % self.target.bits == 0:
word = self.getword(srcpos / self.target.bits)
word = self.getword(srcpos // self.target.bits)
elif (wordindex+1 >= len(self.words) or
srcpos % self.target.bits + maxbits < self.target.bits):
word = self.target.new_value(
"(%%s) >> %d" % (srcpos % self.target.bits),
self.getword(srcpos / self.target.bits))
self.getword(srcpos // self.target.bits))
else:
word = self.target.new_value(
"((%%s) >> %d) | ((%%s) << %d)" % (
srcpos % self.target.bits,
self.target.bits - (srcpos % self.target.bits)),
self.getword(srcpos / self.target.bits),
self.getword(srcpos / self.target.bits + 1))
self.getword(srcpos // self.target.bits),
self.getword(srcpos // self.target.bits + 1))
if maxbits < self.target.bits and maxbits < bits:
word = self.target.new_value(
"(%%s) & ((((BignumInt)1) << %d)-1)" % maxbits,
@ -127,11 +129,11 @@ class CodegenTarget(object):
self.valindex = 0
self.stmts = []
self.generators = {}
self.bv_words = (130 + self.bits - 1) / self.bits
self.bv_words = (130 + self.bits - 1) // self.bits
self.carry_index = 0
def nwords(self, maxval):
return (maxval.bit_length() + self.bits - 1) / self.bits
return (maxval.bit_length() + self.bits - 1) // self.bits
def stmt(self, stmt, needed=False):
index = len(self.stmts)
@ -149,7 +151,7 @@ class CodegenTarget(object):
return name
def bigval_input(self, name, bits):
words = (bits + self.bits - 1) / self.bits
words = (bits + self.bits - 1) // self.bits
# Expect not to require an entire extra word
assert words == self.bv_words

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# Example Python script to synthesise the server end of an SSH key exchange.
@ -27,6 +27,8 @@
import sys, random
from encodelib import *
assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
# A random Diffie-Hellman group, taken from an SSH server I made a
# test connection to.
groupgen = 5
@ -37,7 +39,7 @@ rsaexp = 0x10001
rsamod = 0xB98FE0C0BEE1E05B35FDDF5517B3E29D8A9A6A7834378B6783A19536968968F755E341B5822CAE15B465DECB80EE4116CF8D22DB5A6C85444A68D0D45D9D42008329619BE3CAC3B192EF83DD2B75C4BB6B567E11B841073BACE92108DA7E97E543ED7F032F454F7AC3C6D3F27DB34BC9974A85C7963C546662AE300A61CBABEE274481FD041C41D0145704F5FA9C77A5A442CD7A64827BB0F21FB56FDE388B596A20D7A7D1C5F22DA96C6C2171D90A673DABC66596CD99499D75AD82FEFDBE04DEC2CC7E1414A95388F668591B3F4D58249F80489646ED2C318E77D4F4E37EE8A588E79F2960620E6D28BF53653F1C974C91845F0BABFE5D167E1CA7044EE20D
# 16 bytes of random data for the start of KEXINIT.
cookie = "".join([chr(random.randint(0,255)) for i in range(16)])
cookie = bytes(random.randint(0,255) for i in range(16))
def expect(var, expr):
expected_val = eval(expr)
@ -46,12 +48,12 @@ def expect(var, expr):
expr, repr(expected_val), repr(var)))
sys.exit(1)
sys.stdout.write(greeting("SSH-2.0-Example KEX synthesis"))
sys.stdout.buffer.write(greeting("SSH-2.0-Example KEX synthesis"))
greeting = sys.stdin.readline()
expect(greeting[:8], '"SSH-2.0-"')
greeting = sys.stdin.buffer.readline()
expect(greeting[:8].decode("ASCII"), '"SSH-2.0-"')
sys.stdout.write(
sys.stdout.buffer.write(
clearpkt(SSH2_MSG_KEXINIT,
cookie,
name_list(("diffie-hellman-group-exchange-sha256",)), # kex
@ -66,27 +68,27 @@ sys.stdout.write(
name_list(()), # server->client languages
boolean(False), # first kex packet does not follow
uint32(0)))
sys.stdout.flush()
sys.stdout.buffer.flush()
intype, inpkt = read_clearpkt(sys.stdin)
intype, inpkt = read_clearpkt(sys.stdin.buffer)
expect(intype, "SSH2_MSG_KEXINIT")
intype, inpkt = read_clearpkt(sys.stdin)
intype, inpkt = read_clearpkt(sys.stdin.buffer)
expect(intype, "SSH2_MSG_KEX_DH_GEX_REQUEST")
expect(inpkt, "uint32(0x400) + uint32(0x400) + uint32(0x2000)")
sys.stdout.write(
sys.stdout.buffer.write(
clearpkt(SSH2_MSG_KEX_DH_GEX_GROUP,
mpint(group),
mpint(groupgen)))
sys.stdout.flush()
sys.stdout.buffer.flush()
intype, inpkt = read_clearpkt(sys.stdin)
intype, inpkt = read_clearpkt(sys.stdin.buffer)
expect(intype, "SSH2_MSG_KEX_DH_GEX_INIT")
sys.stdout.write(
sys.stdout.buffer.write(
clearpkt(SSH2_MSG_KEX_DH_GEX_REPLY,
ssh_rsa_key_blob(rsaexp, rsamod),
mpint(random.randint(2, group-2)),
ssh_rsa_signature_blob(random.randint(2, rsamod-2))))
sys.stdout.flush()
sys.stdout.buffer.flush()