From a7305603a8a44683c017bb30fb40a5ea22edcde7 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Tue, 16 Nov 2010 01:49:18 -0500 Subject: Added SshRsaPrivateKey and SshRsaPublickey. These are two helper classes to create Rsa key objects from ssh keyfiles. --- ssh_rsa_key_util.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'ssh_rsa_key_util.py') 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' -- cgit v1.2.3-70-g09d2