aboutsummaryrefslogtreecommitdiffstats
path: root/ssh_rsa_key_util.py
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2010-11-16 01:49:18 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2010-11-16 01:49:18 -0500
commita7305603a8a44683c017bb30fb40a5ea22edcde7 (patch)
tree952471499cc52e28dd5699057f60dbfd9d9b43df /ssh_rsa_key_util.py
parent20860ea10a8c05170d1bb3a0c99a6da4768f32d9 (diff)
downloadalias-a7305603a8a44683c017bb30fb40a5ea22edcde7.tar.gz
Added SshRsaPrivateKey and SshRsaPublickey.
These are two helper classes to create Rsa key objects from ssh keyfiles.
Diffstat (limited to 'ssh_rsa_key_util.py')
-rw-r--r--ssh_rsa_key_util.py39
1 files changed, 38 insertions, 1 deletions
diff --git a/ssh_rsa_key_util.py b/ssh_rsa_key_util.py
index d77577f..c25a112 100644
--- a/ssh_rsa_key_util.py
+++ b/ssh_rsa_key_util.py
@@ -1,7 +1,8 @@
import base64
import struct
import filecmp
-from keyczar import util
+from keyczar import util, keys
+from Crypto.PublicKey import RSA
# need pyasn for DER parsing and generating
from pyasn1.type import univ
@@ -155,10 +156,46 @@ def write_rsa_pri(filename, n, e, d, p, q, e1, e2, c):
""".format('\n'.join(chopped))
file(filename, 'w').write(content)
+
+class SshRsaPublicKey(keys.RsaPublicKey):
+ @staticmethod
+ def Read(keyfile):
+ (n, e, host) = read_rsa_pub(keyfile)
+ params = {'modulus' : util.PadBytes(util.BigIntToBytes(n), 1),
+ 'publicExponent' : util.PadBytes(util.BigIntToBytes(e), 1)}
+ pubkey = RSA.construct((util.BytesToLong(params['modulus']),
+ util.BytesToLong(params['publicExponent'])))
+ return keys.RsaPublicKey(params, pubkey)
+
+class SshRsaPrivateKey(keys.RsaPrivateKey):
+ @staticmethod
+ def Read(keyfile):
+ (n, e, d, p, q, e1, e2, c) = read_rsa_pri(keyfile)
+ params = {'modulus' : util.PadBytes(util.BigIntToBytes(n), 1),
+ 'publicExponent' : util.PadBytes(util.BigIntToBytes(e), 1)}
+ pubkey = RSA.construct((util.BytesToLong(params['modulus']),
+ util.BytesToLong(params['publicExponent'])))
+ pub = keys.RsaPublicKey(params,pubkey)
+ params = {'privateExponent': util.PadBytes(util.BigIntToBytes(d),1),
+ 'primeP': util.PadBytes(util.BigIntToBytes(p),1),
+ 'primeQ': util.PadBytes(util.BigIntToBytes(q),1),
+ 'primeExponentP': util.PadBytes(util.BigIntToBytes(e1),1),
+ 'primeExponentQ': util.PadBytes(util.BigIntToBytes(e2),1),
+ 'crtCoefficient': util.PadBytes(util.BigIntToBytes(c),1),
+ }
+ key = RSA.construct((util.BytesToLong(pub.params['modulus']),
+ util.BytesToLong(pub.params['publicExponent']),
+ util.BytesToLong(params['privateExponent']),
+ util.BytesToLong(params['primeQ']),
+ util.BytesToLong(params['primeP']),
+ util.BytesToLong(params['crtCoefficient'])))
+ return keys.RsaPrivateKey(params, pub, key)
+
if __name__ == '__main__' :
ssh_keys_directory='/home/guillaume/.ssh/'
print 'Testing public key reading...'
(n,e,host)=read_rsa_pub(ssh_keys_directory + 'id_rsa.pub')
+ test = SshRsaPublicKey.Read(ssh_keys_directory + 'id_rsa.pub')
write_rsa_pub(ssh_keys_directory + 'id_rsa_test.pub',n,e,host)
if filecmp.cmp(ssh_keys_directory + 'id_rsa.pub',ssh_keys_directory + 'id_rsa_test.pub'):
print 'test succesful'