|
15 | 15 | import json |
16 | 16 | import os |
17 | 17 | import pickle |
| 18 | +import warnings |
18 | 19 |
|
19 | 20 | from cryptography.hazmat.primitives.asymmetric import rsa |
20 | 21 | import pytest # type: ignore |
@@ -174,3 +175,37 @@ def test_pickle(self): |
174 | 175 |
|
175 | 176 | assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID] |
176 | 177 | assert isinstance(signer._key, rsa.RSAPrivateKey) |
| 178 | + |
| 179 | + |
| 180 | +@pytest.mark.skipif(rsa is None, reason="rsa library is not installed") |
| 181 | +class TestRSATransparency(object): |
| 182 | + @classmethod |
| 183 | + def setup_class(cls): |
| 184 | + import rsa |
| 185 | + (cls.pub_key_rsa, cls.priv_key_rsa) = rsa.newkeys(512) |
| 186 | + cls.message = b"test message" |
| 187 | + |
| 188 | + @pytest.mark.filterwarnings("ignore::DeprecationWarning") |
| 189 | + def test_rsa_key_signing_and_verification(self): |
| 190 | + # Test RSASigner with rsa.key.PrivateKey |
| 191 | + signer = _cryptography_rsa.RSASigner(self.priv_key_rsa) |
| 192 | + signature = signer.sign(self.message) |
| 193 | + assert isinstance(signer._key, rsa.RSAPrivateKey) |
| 194 | + |
| 195 | + # Test RSAVerifier with rsa.key.PublicKey |
| 196 | + verifier = _cryptography_rsa.RSAVerifier(self.pub_key_rsa) |
| 197 | + assert verifier.verify(self.message, signature) is True |
| 198 | + assert isinstance(verifier._pubkey, rsa.RSAPublicKey) |
| 199 | + |
| 200 | + def test_rsa_key_warning(self): |
| 201 | + # Reset the global flag to ensure the warning is issued |
| 202 | + _cryptography_rsa._RSA_DEPRECATION_WARNED = False |
| 203 | + with pytest.warns(DeprecationWarning) as record: |
| 204 | + _cryptography_rsa.RSASigner(self.priv_key_rsa) |
| 205 | + # duplicates should be ignored |
| 206 | + _cryptography_rsa.RSAVerifier(self.pub_key_rsa) |
| 207 | + _cryptography_rsa.RSASigner(self.priv_key_rsa) |
| 208 | + _cryptography_rsa.RSAVerifier(self.pub_key_rsa) |
| 209 | + |
| 210 | + assert len(record) == 1 |
| 211 | + assert "The 'rsa' library is deprecated" in str(record[0].message) |
0 commit comments