4

I have the following piece of code that extract a host leaf certificate and the certificate RSA public key:

c = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(c)
publickey=x509.get_pubkey()
m=publickey.get_modulus()

I tried to find functions to extract the public exponent of the RSA key but I could not find any. Can you help me figure out how can I extract the public exponent of RSA public key?

EDIT: if not possible by M2Crypt. Please, point any other way.

EDIT 2: when I tried to load the cert as DER as:

key = RSA.importKey(publickey.as_der())

I got this error:

('file() argument 1 must be encoded string without NULL bytes, not str',)

I want to avoid saving the certs in the local system. I just wan to extract the info such as modulus and exponent. What I understand from the error is that the importKey function takes the aregument as DER file not string. Can you help me find a workaround?

user2192774
  • 3,807
  • 17
  • 47
  • 62

1 Answers1

4

This code should give you the modulus and public exponent:

import Crypto.PublicKey.RSA

c = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(c)
publickey = x509.get_pubkey()
key = Crypto.PublicKey.RSA.importKey(publickey.as_der())

modulus = key.n
public_exponent = key.e
Jay Bosamiya
  • 3,011
  • 2
  • 14
  • 33
  • I do not want the modulus because I already got it from `mod=pk.get_modulus()` I want the public exponent which is normally named `e`. – user2192774 Jun 15 '15 at 19:58
  • It also did not work for me to extract the `n`. Are you sure that I send the `c` to `importKey`?? – user2192774 Jun 15 '15 at 20:02
  • I misread the question at first and assumed some things. Please see edited version of the code. – Jay Bosamiya Jun 15 '15 at 20:33
  • check EDIT 2 plz. thanx. – user2192774 Jun 15 '15 at 20:47
  • I am not sure what the `file()` error is stemming from. The code I am using for testing is http://www.hastebin.com/xajezajeyo.hs and it seems to work properly on my system. – Jay Bosamiya Jun 15 '15 at 21:07
  • I just copied your code and tested it. It does not work. `key = Crypto.PublicKey.RSA.importKey(publickey.as_der())` and the error is: `NameError: name 'Crypto' is not defined`. what is Crypto? I searched about a library with this name from pyCharm editor but seems no library with this name has been found. – user2192774 Jun 15 '15 at 21:46
  • For the `file()` error, the Exception statement I used is: except `Exception as ex: template = "An exception of type {0} occured. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) print message`. If you referred to the function importKey specification you will find that it taked der file as argument. – user2192774 Jun 15 '15 at 21:49
  • My bad, I wrote the wrong import (because I was working directly from an interpreter shell, all imports chained up). The correct import has been put into the latest edit. Also, Crypto is the pyCrypto library. It can be installed using `pip install pyCrypto`. BTW, corrected testing code is at http://www.hastebin.com/bowipeyani.hs – Jay Bosamiya Jun 16 '15 at 16:59
  • still can't run. When I run, I get: `ImportError: No module named Crypto.PublicKey.RSA`. I made sure that pyCrypto is installed. I tried `pip install pyCrypto` and I get: `Requirement already satisfied (use --upgrade to upgrade): pyCrypto in c:\python2 7\lib\site-packages`. Can't understand what is the reason? I am using pyCharm editor. – user2192774 Jun 16 '15 at 17:19