def get_modulus_exponent_from_cert_and_pkey_pemfile(cert_path='/tmp/xmppwebid_cert.pem', key_path='/tmp/xmppwebid_key.key'): """Get the modulus and exponent of RSA key from a PEM Certificate and key files m2.rsa_get_e(rsa.rsa) return something like '\x00\x00\x00\x03\x01\x00\x01' so to get the decimal value (65537), two crufty methods :param cert_path: certificate path :type cert_path: string :param key_path: key path :type key_path: string :return: tuple(modulus, exponent) :rtype: tuple (hex, int) .. TODO:: replace the exponent method with something cleaner """ cert=X509.load_cert(cert_path) pubkey = cert.get_pubkey() modulus = pubkey.get_modulus() pkey_rsa = RSA.load_key(key_path) e = m2.rsa_get_e(pkey_rsa.rsa) # exponent = int(eval(repr(e[-3:]).replace('\\x', '')),16) exponent = int(''.join(["%2.2d" % ord(x) for x in e[-3:]]),16) return modulus, exponent
def __getattr__(self, name): if name == 'e': return m2.rsa_get_e(self.rsa) elif name == 'n': return m2.rsa_get_n(self.rsa) else: raise AttributeError
def get_modulus_exponent_from_cert_and_pkey_pemfile( cert_path='/tmp/xmppwebid_cert.pem', key_path='/tmp/xmppwebid_key.key'): """Get the modulus and exponent of RSA key from a PEM Certificate and key files m2.rsa_get_e(rsa.rsa) return something like '\x00\x00\x00\x03\x01\x00\x01' so to get the decimal value (65537), two crufty methods :param cert_path: certificate path :type cert_path: string :param key_path: key path :type key_path: string :return: tuple(modulus, exponent) :rtype: tuple (hex, int) .. TODO:: replace the exponent method with something cleaner """ cert = X509.load_cert(cert_path) pubkey = cert.get_pubkey() modulus = pubkey.get_modulus() pkey_rsa = RSA.load_key(key_path) e = m2.rsa_get_e(pkey_rsa.rsa) # exponent = int(eval(repr(e[-3:]).replace('\\x', '')),16) exponent = int(''.join(["%2.2d" % ord(x) for x in e[-3:]]), 16) return modulus, exponent
def __getattr__(self, name): # type: (str) -> bytes if name == 'e': return m2.rsa_get_e(self.rsa) elif name == 'n': return m2.rsa_get_n(self.rsa) else: raise AttributeError
def test_set_e(self): rsa = m2.rsa_new() m2.rsa_set_e(rsa, b'\000\000\000\003\001\000\001') n = m2.rsa_get_n(rsa) e = m2.rsa_get_e(rsa) self.assertEqual(e, b'\000\000\000\003\001\000\001') self.assertEqual(n, b'\x00\x00\x00\x00')
def parse_der(der): cert = X509.load_cert_der_string(der) #log.debug('PARSING CERTIFICATE %s', cert.get_subject().as_text()) rsa = cert.get_pubkey().get_rsa() e = mpi_to_num(m2.rsa_get_e(rsa.rsa)) n = mpi_to_num(m2.rsa_get_n(rsa.rsa)) return (n, e)
def test_set_n_then_set_e(self): rsa = m2.rsa_new() m2.rsa_set_n(rsa, b'\000\000\000\004\020\011\006\006') m2.rsa_set_e(rsa, b'\000\000\000\003\001\000\001') n = m2.rsa_get_n(rsa) e = m2.rsa_get_e(rsa) self.assertEqual(e, b'\000\000\000\003\001\000\001') self.assertEqual(n, b'\000\000\000\004\020\011\006\006')
def get_modulus_exponent_from_cert_file(cert_path='/tmp/xmpp_foaf_cert.pem'): """ Get the modulus and exponent of RSA Public Key from a PEM Certificate file m2.rsa_get_e(rsa.rsa) return something like '\x00\x00\x00\x03\x01\x00\x01' so to get the decimal value (65537), two crufty methods @param cert_path: certificate path @type cert_path: string @return: tuple(modulus, exponent) @rtype: tuple (hex, int) @TODO: replace the exponent method with something cleaner """ cert=X509.load_cert(cert_path) pubkey = cert.get_pubkey() modulus = pubkey.get_modulus() pk_rsa = pubkey.get_rsa() e = m2.rsa_get_e(rsa.rsa) # exponent = int(eval(repr(e[-3:]).replace('\\x', '')),16) exponent = int(''.join(["%2.2d" % ord(x) for x in e[-3:]]),16) return modulus, exponent
def pub(self): # type: () -> Tuple[bytes, bytes] assert self.check_key(), 'key is not initialised' return m2.rsa_get_e(self.rsa), m2.rsa_get_n(self.rsa)
def pub(self): assert self.check_key(), 'key is not initialised' return m2.rsa_get_e(self.rsa), m2.rsa_get_n(self.rsa)