python3 - 2to3 + miscell obvious tweaks
[sfa.git] / keyconvert / keyconvert.py
index af12b1f..2effcb6 100755 (executable)
@@ -4,7 +4,22 @@ import sys
 import base64
 import struct
 import binascii
-from M2Crypto import RSA, DSA
+from M2Crypto import RSA, DSA, m2
+
+
+###### Workaround for bug in m2crypto-0.18 (on Fedora 8)
+class RSA_pub_fix(RSA.RSA_pub):
+    def save_key_bio(self, bio, *args, **kw):
+        return self.save_pub_key_bio(bio)
+
+def rsa_new_pub_key(couple):
+    (e, n)=couple
+    rsa = m2.rsa_new()
+    m2.rsa_set_e(rsa, e)
+    m2.rsa_set_n(rsa, n)
+    return RSA_pub_fix(rsa, 1)
+######
+#rsa_new_pub_key = RSA.new_pub_key
 
 
 def decode_key(fname):
@@ -78,7 +93,7 @@ def convert(fin, fout):
 
     if key_type == "ssh-rsa":
         e, n = ret[1:]
-        rsa = RSA.new_pub_key((e, n))
+        rsa = rsa_new_pub_key((e, n))
         rsa.save_pem(fout)
 
     elif key_type == "ssh-dss":
@@ -88,12 +103,12 @@ def convert(fin, fout):
         dsa.save_pub_key(fout)
         # FIXME: This is wrong.
         # M2Crypto doesn't allow us to set the public key parameter
-        raise(Exception, "DSA keys are not supported yet: M2Crypto doesn't allow us to set the public key parameter")
+        raise Exception("DSA keys are not supported yet: M2Crypto doesn't allow us to set the public key parameter")
 
 
 if __name__ == "__main__":
     if len(sys.argv) != 3:
-        print "Usage: %s <input-file> <output-file>"
+        print("Usage: %s <input-file> <output-file>")
         sys.exit(1)
 
     fin = sys.argv[1]