Skip to content

Commit 00c0390

Browse files
committed
make generated RSA keys have the d property too
1 parent ea854f1 commit 00c0390

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

tlslite/utils/compat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def compatAscii2Bytes(val):
3838
return bytes(val, 'ascii')
3939
return val
4040

41+
def compat_b2a(val):
42+
"""Convert an ASCII bytes string to string."""
43+
return str(val, 'ascii')
44+
4145
def raw_input(s):
4246
return input(s)
4347

@@ -141,6 +145,10 @@ def compatAscii2Bytes(val):
141145
"""Convert ASCII string to bytes."""
142146
return val
143147

148+
def compat_b2a(val):
149+
"""Convert an ASCII bytes string to string."""
150+
return str(val)
151+
144152
# So, python 2.6 requires strings, python 3 requires 'bytes',
145153
# and python 2.7 can handle bytearrays...
146154
def compatHMAC(x): return compat26Str(x)

tlslite/utils/openssl_rsakey.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
from .rsakey import *
99
from .python_rsakey import Python_RSAKey
10-
from .compat import compatAscii2Bytes
11-
import sys
10+
from .compat import compatAscii2Bytes, compat_b2a
1211

1312
#copied from M2Crypto.util.py, so when we load the local copy of m2
1413
#we can still use it
@@ -119,6 +118,9 @@ def f():pass
119118
key.rsa = m2.rsa_generate_key(bits, 3, f)
120119
key._hasPrivateKey = True
121120
key.key_type = key_type
121+
b64_key = compat_b2a(key.write())
122+
py_key = Python_RSAKey.parsePEM(b64_key)
123+
key.d = py_key.d
122124
return key
123125

124126
@staticmethod
@@ -171,10 +173,7 @@ def f():pass
171173
else:
172174
raise SyntaxError()
173175
if key._hasPrivateKey:
174-
if sys.version_info < (3, 0):
175-
b64_key = str(key.write())
176-
else:
177-
b64_key = str(key.write(), "ascii")
176+
b64_key = compat_b2a(key.write())
178177
py_key = Python_RSAKey.parsePEM(b64_key)
179178
key.d = py_key.d
180179
return key

unit_tests/test_tlslite_utils_rsakey.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from tlslite.utils.python_rsakey import Python_RSAKey
1414
from tlslite.utils.cryptomath import *
1515
from tlslite.errors import *
16-
from tlslite.utils.keyfactory import parsePEMKey
16+
from tlslite.utils.keyfactory import parsePEMKey, generateRSAKey
1717
from tlslite.utils.compat import a2b_hex, remove_whitespace
1818
try:
1919
import mock
@@ -1229,6 +1229,7 @@ def m(leght):
12291229
signed = self.rsa.RSASSA_PSS_sign(mHash, 'sha512', 10)
12301230
self.assertEqual(signed, intendedS)
12311231

1232+
12321233
class TestEncryptDecrypt(unittest.TestCase):
12331234
n = int("a8d68acd413c5e195d5ef04e1b4faaf242365cb450196755e92e1215ba59802aa"
12341235
"fbadbf2564dd550956abb54f8b1c917844e5f36195d1088c600e07cada5c080ed"
@@ -1267,6 +1268,12 @@ def test_invalid_init(self):
12671268
with self.assertRaises(ValueError):
12681269
Python_RSAKey(self.n, self.e, self.d, self.p)
12691270

1271+
def test_with_generated_key(self):
1272+
key = generateRSAKey(1024)
1273+
1274+
txt = bytearray(b"test string")
1275+
self.assertEqual(txt, key.decrypt(key.encrypt(txt)))
1276+
12701277

12711278
class TestRSAPKCS1(unittest.TestCase):
12721279
n = int("a8d68acd413c5e195d5ef04e1b4faaf242365cb450196755e92e1215ba59802aa"

0 commit comments

Comments
 (0)