summaryrefslogtreecommitdiff
path: root/paramiko/ber.py
diff options
context:
space:
mode:
authorRobey Pointer <robey@lag.net>2004-04-05 10:16:31 +0000
committerRobey Pointer <robey@lag.net>2004-04-05 10:16:31 +0000
commit01bf5477a04cbb34974aae92f6c5965572800b63 (patch)
treedd7c8c79f2676305e0bad8531e54620863824540 /paramiko/ber.py
parent8fafd1aa1716bdf50575c4c3f98e46a2c7e73f8c (diff)
downloadparamiko-01bf5477a04cbb34974aae92f6c5965572800b63.tar.gz
[project @ Arch-1:robey@lag.net--2003-public%secsh--dev--1.0--patch-37]
can now generate rsa keys (not dss yet) added functionality to ber to create ber streams. added some common methods to PKey to allow dumping the key to base64 (the format used by openssh for public key files and host key lists), and a factory for creating a key from a private key file, and a common way to save private keys. RSAKey luckily didn't have to change that much. also added a factory method to RSAKey to generate a new key.
Diffstat (limited to 'paramiko/ber.py')
-rw-r--r--paramiko/ber.py44
1 files changed, 36 insertions, 8 deletions
diff --git a/paramiko/ber.py b/paramiko/ber.py
index f32237f4..dc04c1a5 100644
--- a/paramiko/ber.py
+++ b/paramiko/ber.py
@@ -37,7 +37,7 @@ class BER(object):
return self.content
def __repr__(self):
- return 'BER(' + repr(self.content) + ')'
+ return 'BER(\'' + repr(self.content) + '\')'
def decode(self):
return self.decode_next()
@@ -52,10 +52,10 @@ class BER(object):
id = 0
while self.idx < len(self.content):
t = ord(self.content[self.idx])
+ self.idx += 1
+ id = (id << 7) | (t & 0x7f)
if not (t & 0x80):
break
- id = (id << 7) | (t & 0x7f)
- self.idx += 1
if self.idx >= len(self.content):
return None
# now fetch length
@@ -67,11 +67,8 @@ class BER(object):
t = size & 0x7f
if self.idx + t > len(self.content):
return None
- size = 0
- while t > 0:
- size = (size << 8) | ord(self.content[self.idx])
- self.idx += 1
- t -= 1
+ size = self.inflate_long(self.content[self.idx : self.idx + t], True)
+ self.idx += t
if self.idx + size > len(self.content):
# can't fit
return None
@@ -98,3 +95,34 @@ class BER(object):
out.append(x)
decode_sequence = staticmethod(decode_sequence)
+ def encode_tlv(self, id, val):
+ # FIXME: support id > 31 someday
+ self.content += chr(id)
+ if len(val) > 0x7f:
+ lenstr = util.deflate_long(len(val))
+ self.content += chr(0x80 + len(lenstr)) + lenstr
+ else:
+ self.content += chr(len(val))
+ self.content += val
+
+ def encode(self, x):
+ if type(x) is bool:
+ if x:
+ self.encode_tlv(1, '\xff')
+ else:
+ self.encode_tlv(1, '\x00')
+ elif (type(x) is int) or (type(x) is long):
+ self.encode_tlv(2, util.deflate_long(x))
+ elif type(x) is str:
+ self.encode_tlv(4, x)
+ elif (type(x) is list) or (type(x) is tuple):
+ self.encode_tlv(30, self.encode_sequence(x))
+ else:
+ raise BERException('Unknown type for encoding: %s' % repr(type(x)))
+
+ def encode_sequence(data):
+ b = BER()
+ for item in data:
+ b.encode(item)
+ return str(b)
+ encode_sequence = staticmethod(encode_sequence)