diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-01-22 15:41:40 +0100 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2016-01-22 15:41:40 +0100 |
commit | 4bc9733b78cd115a742b9486ab11ccbdcb9ca001 (patch) | |
tree | 53df23ee4826dcfa00cc835cd437df6b8acb4d2f /rsa/key.py | |
parent | f1c55540438fb88dbbc30c9c3bf7c4d82f5d59a2 (diff) | |
download | rsa-git-4bc9733b78cd115a742b9486ab11ccbdcb9ca001.tar.gz |
Fix #12 Allow pickling of keys.
Pickling is now possible, with the added note that one should never
unpickle from an untrusted or unauthenticated source.
Diffstat (limited to 'rsa/key.py')
-rw-r--r-- | rsa/key.py | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -23,6 +23,14 @@ Loading and saving keys requires the pyasn1 module. This module is imported as late as possible, such that other functionality will remain working in absence of pyasn1. +.. note:: + + Storing public and private keys via the `pickle` module is possible. + However, it is insecure to load a key from an untrusted source. + The pickle module is not secure against erroneous or maliciously + constructed data. Never unpickle data received from an untrusted + or unauthenticated source. + """ import logging @@ -154,6 +162,14 @@ class PublicKey(AbstractKey): def __repr__(self): return 'PublicKey(%i, %i)' % (self.n, self.e) + def __getstate__(self): + """Returns the key as tuple for pickling.""" + return self.n, self.e + + def __setstate__(self, state): + """Sets the key from tuple.""" + self.n, self.e = state + def __eq__(self, other): if other is None: return False @@ -337,6 +353,14 @@ class PrivateKey(AbstractKey): def __repr__(self): return 'PrivateKey(%(n)i, %(e)i, %(d)i, %(p)i, %(q)i)' % self + def __getstate__(self): + """Returns the key as tuple for pickling.""" + return self.n, self.e, self.d, self.p, self.q, self.exp1, self.exp2, self.coef + + def __setstate__(self, state): + """Sets the key from tuple.""" + self.n, self.e, self.d, self.p, self.q, self.exp1, self.exp2, self.coef = state + def __eq__(self, other): if other is None: return False |