Skip to content

Commit

Permalink
make m2crypto backed public key pickleable
Browse files Browse the repository at this point in the history
  • Loading branch information
tomato42 committed Feb 2, 2022
1 parent 38dbe7d commit 30f416b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tlslite/utils/openssl_rsakey.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def password_callback(v, prompt1='Enter private key passphrase:',

if m2cryptoLoaded:
import M2Crypto
# the methods like rsa_get_e and rsa_get_n are native, so pylint can't find
# them, ignore those errors
#pylint: disable=no-member

class OpenSSL_RSAKey(RSAKey):
def __init__(self, n=0, e=0, key_type="rsa"):
Expand All @@ -43,6 +46,22 @@ def __init__(self, n=0, e=0, key_type="rsa"):
m2.rsa_set_e(self.rsa, numberToMPI(e))
self.key_type = key_type

def __getstate__(self):
if not self.rsa:
return (self.key_type, )
return (self.key_type,
mpiToNumber(m2.rsa_get_e(self.rsa)),
mpiToNumber(m2.rsa_get_n(self.rsa)))

def __setstate__(self, state):
self.rsa = None
self._hasPrivateKey = False
self.key_type = state[0]
if len(state) > 1:
self.rsa = m2.rsa_new()
m2.rsa_set_e(self.rsa, numberToMPI(state[1]))
m2.rsa_set_n(self.rsa, numberToMPI(state[2]))

def __del__(self):
if self.rsa:
m2.rsa_free(self.rsa)
Expand Down

0 comments on commit 30f416b

Please sign in to comment.