33
44from jose .backends .pycrypto_backend import RSAKey
55from jose .backends .cryptography_backend import CryptographyRSAKey
6+ from jose .backends .rsa_backend import RSAKey as PurePythonRSAKey
67from jose .constants import ALGORITHMS
78from jose .exceptions import JOSEError , JWKError
89
@@ -185,3 +186,95 @@ def test_pycrypto_invalid_signature(self):
185186
186187 assert public_key .verify (msg , signature ) == True
187188 assert public_key .verify (msg , 1 ) == False
189+
190+
191+ class TestPythonRSA :
192+ def test_RSA_key (self ):
193+ PurePythonRSAKey (private_key , ALGORITHMS .RS256 )
194+
195+ def test_RSA_key_instance (self ):
196+ import rsa
197+ key = rsa .PublicKey (
198+ e = 65537 ,
199+ n = 26057131595212989515105618545799160306093557851986992545257129318694524535510983041068168825614868056510242030438003863929818932202262132630250203397069801217463517914103389095129323580576852108653940669240896817348477800490303630912852266209307160550655497615975529276169196271699168537716821419779900117025818140018436554173242441334827711966499484119233207097432165756707507563413323850255548329534279691658369466534587631102538061857114141268972476680597988266772849780811214198186940677291891818952682545840788356616771009013059992237747149380197028452160324144544057074406611859615973035412993832273216732343819 ,
200+ )
201+
202+ pubkey = PurePythonRSAKey (key , ALGORITHMS .RS256 )
203+ pem = pubkey .to_pem ()
204+ assert pem .startswith (b'-----BEGIN PUBLIC KEY-----' )
205+
206+ def test_invalid_algorithm (self ):
207+ with pytest .raises (JWKError ):
208+ PurePythonRSAKey (private_key , ALGORITHMS .ES256 )
209+
210+ with pytest .raises (JWKError ):
211+ PurePythonRSAKey ({'kty' : 'bla' }, ALGORITHMS .RS256 )
212+
213+ def test_RSA_jwk (self ):
214+ d = {
215+ "kty" : "RSA" ,
216+ "n" : "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw" ,
217+ "e" : "AQAB" ,
218+ }
219+ PurePythonRSAKey (d , ALGORITHMS .RS256 )
220+
221+ def test_string_secret (self ):
222+ key = 'secret'
223+ with pytest .raises (JOSEError ):
224+ PurePythonRSAKey (key , ALGORITHMS .RS256 )
225+
226+ def test_object (self ):
227+ key = object ()
228+ with pytest .raises (JOSEError ):
229+ PurePythonRSAKey (key , ALGORITHMS .RS256 )
230+
231+ def test_bad_cert (self ):
232+ key = '-----BEGIN CERTIFICATE-----'
233+ with pytest .raises (JOSEError ):
234+ PurePythonRSAKey (key , ALGORITHMS .RS256 )
235+
236+ def test_get_public_key (self ):
237+ key = PurePythonRSAKey (private_key , ALGORITHMS .RS256 )
238+ public_key = key .public_key ()
239+ public_key2 = public_key .public_key ()
240+ assert public_key == public_key2
241+
242+ key = RSAKey (private_key , ALGORITHMS .RS256 )
243+ public_key = key .public_key ()
244+ public_key2 = public_key .public_key ()
245+ assert public_key == public_key2
246+
247+ def test_to_pem (self ):
248+ key = PurePythonRSAKey (private_key , ALGORITHMS .RS256 )
249+ assert key .to_pem ().strip () == private_key .strip ()
250+
251+ key = RSAKey (private_key , ALGORITHMS .RS256 )
252+ assert key .to_pem ().strip () == private_key .strip ()
253+
254+ def test_signing_parity (self ):
255+ key1 = RSAKey (private_key , ALGORITHMS .RS256 )
256+ vkey1 = key1 .public_key ()
257+ key2 = PurePythonRSAKey (private_key , ALGORITHMS .RS256 )
258+ vkey2 = key2 .public_key ()
259+
260+ msg = b'test'
261+ sig1 = key1 .sign (msg )
262+ sig2 = key2 .sign (msg )
263+
264+ assert vkey1 .verify (msg , sig1 )
265+ assert vkey1 .verify (msg , sig2 )
266+ assert vkey2 .verify (msg , sig1 )
267+ assert vkey2 .verify (msg , sig2 )
268+
269+ # invalid signature
270+ assert not vkey2 .verify (msg , b'n' * 64 )
271+
272+ def test_pycrypto_invalid_signature (self ):
273+
274+ key = RSAKey (private_key , ALGORITHMS .RS256 )
275+ msg = b'test'
276+ signature = key .sign (msg )
277+ public_key = key .public_key ()
278+
279+ assert public_key .verify (msg , signature ) == True
280+ assert public_key .verify (msg , 1 ) == False
0 commit comments