diff options
author | Robey Pointer <robey@lag.net> | 2004-01-04 09:29:13 +0000 |
---|---|---|
committer | Robey Pointer <robey@lag.net> | 2004-01-04 09:29:13 +0000 |
commit | 988c6abda08dd7380da37cfc74b9642437afe1ae (patch) | |
tree | 64ef726eb8fdf17e01f832a8487c7a2776939067 /paramiko/ber.py | |
parent | 3a8887a42083dda796f50e1e9b32f625abcb5d5a (diff) | |
download | paramiko-988c6abda08dd7380da37cfc74b9642437afe1ae.tar.gz |
[project @ Arch-1:robey@lag.net--2003-public%secsh--dev--1.0--patch-20]
more docs, and password-protected key files can now be read
lots more documentation, some of it moved out of the README file, which is
now much smaller and less rambling.
repr(Transport) now reports the number of bits used in the cipher.
cleaned up BER to use util functions, and throw a proper exception (the new
BERException) on error. it doesn't ever have to be a full BER decoder, but
it can at least comb its hair and tuck in its shirt.
lots of stuff added to PKey.read_private_key_file so it can try to decode
password-protected key files. right now it only understands "DES-EDE3-CBC"
format, but this is the only format i've seen openssh make so far. if the
key is password-protected, but no password was given, a new exception
(PasswordRequiredException) is raised so an outer layer can ask for a password
and try again.
Diffstat (limited to 'paramiko/ber.py')
-rw-r--r-- | paramiko/ber.py | 46 |
1 files changed, 8 insertions, 38 deletions
diff --git a/paramiko/ber.py b/paramiko/ber.py index 7fe1dd09..ed7db223 100644 --- a/paramiko/ber.py +++ b/paramiko/ber.py @@ -1,45 +1,15 @@ #!/usr/bin/python -import struct +import struct, util -def inflate_long(s, always_positive=0): - "turns a normalized byte string into a long-int (adapted from Crypto.Util.number)" - out = 0L - if len(s) % 4: - filler = '\x00' - if not always_positive and (ord(s[0]) >= 0x80): - # negative - filler = '\xff' - s = filler * (4 - len(s) % 4) + s - # FIXME: this doesn't actually handle negative. - # luckily ssh never uses negative bignums. - for i in range(0, len(s), 4): - out = (out << 32) + struct.unpack('>I', s[i:i+4])[0] - return out - -def deflate_long(n, add_sign_padding=1): - "turns a long-int into a normalized byte string (adapted from Crypto.Util.number)" - # after much testing, this algorithm was deemed to be the fastest - s = '' - n = long(n) - while n > 0: - s = struct.pack('>I', n & 0xffffffffL) + s - n = n >> 32 - # strip off leading zeros - for i in enumerate(s): - if i[1] != '\000': - break - else: - # only happens when n == 0 - s = '\000' - i = (0,) - s = s[i[0]:] - if (ord(s[0]) >= 0x80) and add_sign_padding: - s = '\x00' + s - return s +class BERException (Exception): + pass class BER(object): + """ + Robey's tiny little attempt at a BER decoder. + """ def __init__(self, content=''): self.content = content @@ -95,10 +65,10 @@ class BER(object): return self.decode_sequence(data) elif id == 2: # int - return inflate_long(data) + return util.inflate_long(data) else: # 1: boolean (00 false, otherwise true) - raise Exception('Unknown ber encoding type %d (robey is lazy)' % id) + raise BERException('Unknown ber encoding type %d (robey is lazy)' % id) def decode_sequence(data): out = [] |