44from jose .backends .pycrypto_backend import RSAKey
55from jose .backends .cryptography_backend import CryptographyRSAKey
66from jose .constants import ALGORITHMS
7- from jose .exceptions import JOSEError
7+ from jose .exceptions import JOSEError , JWKError
88
99from Crypto .PublicKey import RSA
1010
1515if sys .version_info > (3 ,):
1616 long = int
1717
18- private_key = """-----BEGIN RSA PRIVATE KEY-----
18+ private_key = b """-----BEGIN RSA PRIVATE KEY-----
1919MIIJKwIBAAKCAgEAtSKfSeI0fukRIX38AHlKB1YPpX8PUYN2JdvfM+XjNmLfU1M7
20204N0VmdzIX95sneQGO9kC2xMIE+AIlt52Yf/KgBZggAlS9Y0Vx8DsSL2HvOjguAdX
2121ir3vYLvAyyHin/mUisJOqccFKChHKjnk0uXy/38+1r17/cYTp76brKpU1I4kM20M
@@ -110,6 +110,13 @@ def test_RSA_key_instance(self):
110110 pem = pubkey .to_pem ()
111111 assert pem .startswith (b'-----BEGIN PUBLIC KEY-----' )
112112
113+ def test_invalid_algorithm (self ):
114+ with pytest .raises (JWKError ):
115+ CryptographyRSAKey (private_key , ALGORITHMS .ES256 )
116+
117+ with pytest .raises (JWKError ):
118+ CryptographyRSAKey ({'kty' : 'bla' }, ALGORITHMS .RS256 )
119+
113120 def test_RSA_jwk (self ):
114121 d = {
115122 "kty" : "RSA" ,
@@ -133,14 +140,29 @@ def test_bad_cert(self):
133140 with pytest .raises (JOSEError ):
134141 CryptographyRSAKey (key , ALGORITHMS .RS256 )
135142
143+ def test_get_public_key (self ):
144+ key = CryptographyRSAKey (private_key , ALGORITHMS .RS256 )
145+ public_key = key .public_key ()
146+ public_key2 = public_key .public_key ()
147+ assert public_key == public_key2
148+
149+ key = RSAKey (private_key , ALGORITHMS .RS256 )
150+ public_key = key .public_key ()
151+ public_key2 = public_key .public_key ()
152+ assert public_key == public_key2
153+
154+ def test_to_pem (self ):
155+ key = CryptographyRSAKey (private_key , ALGORITHMS .RS256 )
156+ assert key .to_pem ().strip () == private_key .strip ()
157+
158+ key = RSAKey (private_key , ALGORITHMS .RS256 )
159+ assert key .to_pem ().strip () == private_key .strip ()
160+
136161 def test_signing_parity (self ):
137162 key1 = RSAKey (private_key , ALGORITHMS .RS256 )
138- public_key = key1 .public_key ().to_pem ()
139- vkey1 = RSAKey (public_key , ALGORITHMS .RS256 )
163+ vkey1 = key1 .public_key ()
140164 key2 = CryptographyRSAKey (private_key , ALGORITHMS .RS256 )
141- vkey2 = CryptographyRSAKey (public_key , ALGORITHMS .RS256 )
142-
143- assert key2 .public_key ().to_pem () == public_key
165+ vkey2 = key2 .public_key ()
144166
145167 msg = b'test'
146168 sig1 = key1 .sign (msg )
@@ -153,3 +175,13 @@ def test_signing_parity(self):
153175
154176 # invalid signature
155177 assert not vkey2 .verify (msg , b'n' * 64 )
178+
179+ def test_pycrypto_invalid_signature (self ):
180+
181+ key = RSAKey (private_key , ALGORITHMS .RS256 )
182+ msg = b'test'
183+ signature = key .sign (msg )
184+ public_key = key .public_key ()
185+
186+ assert public_key .verify (msg , signature ) == True
187+ assert public_key .verify (msg , 1 ) == False
0 commit comments