Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle newer M2Crypto #507

Closed
wants to merge 2 commits into from
Closed

handle newer M2Crypto #507

wants to merge 2 commits into from

Conversation

sthen
Copy link

@sthen sthen commented Dec 20, 2023

As of M2Crypto 0.40.0, "import M2Crypto" no longer pulls in everything - from their release notes:

  • Excessively complicated and error-prone __init__py has been cleaned and import M2Crypto doesn’t include everything anymore. Imports should specified as for example with from M2Crypto import foo.

resulting in the test failures shown below. Attached PR adds the imports which seem to be needed to fix this.

=================================== FAILURES ===================================
_________________ TestParsePEMKey.test_key_parse_using_openssl _________________

self = <unit_tests.test_tlslite_utils_keyfactory.TestParsePEMKey testMethod=test_key_parse_using_openssl>

    @unittest.skipUnless(cryptomath.m2cryptoLoaded, "requires M2Crypto")
    def test_key_parse_using_openssl(self):
    
        # XXX doesn't handle files without newlines
        # old version of M2Crypto return a Null, in Python3 it raises exception
        if M2Crypto.version_info >= (0, 28, 0):
>           exp = M2Crypto.EVP.EVPError
E           AttributeError: module 'M2Crypto' has no attribute 'EVP'

unit_tests/test_tlslite_utils_keyfactory.py:177: AttributeError
__ TestRSADecrypt.test_negative_11_bytes_long_with_null_truncated_ciphertext ___

self = <tlslite.utils.openssl_rsakey.OpenSSL_RSAKey object at 0x73e741dcc70>
method = <built-in function rsa_private_encrypt>
param = bytearray(b'\x96\x13f!\xfa\xf3mR\x90\xb1k\xd2b\x95\xde\'\xf8\x95\xd1\xfa\xa5\x1c\x80\r\xaf\xces\xd0\x01\xd6\x07\x96\xc...\x99\xd7\xc8\x80S\x1d\x06\xf2_p\x02\xe1\xaa\xf1r\'e\x86],+\x90,w6\xac\xd2{\xc6\xcb\xd3\xe3\x8bV\x0e.\xec\xf7\xd4\xb5v')
err_msg = 'Bad parameters to private key operation'

    def _call_m2crypto(self, method, param, err_msg):
        try:
>           return bytearray(method(self.rsa, bytes(param), m2.no_padding))
E           SystemError: <built-in function rsa_private_encrypt> returned NULL without setting an exception

tlslite/utils/openssl_rsakey.py:86: SystemError

During handling of the above exception, another exception occurred:

self = <unit_tests.test_tlslite_utils_rsakey.TestRSADecrypt testMethod=test_negative_11_bytes_long_with_null_truncated_ciphertext>

        def test_negative_11_bytes_long_with_null_truncated_ciphertext(self):
            # same as test_negative_11_bytes_long_with_null_padded_ciphertext
            # but with the zero bytes at the beginning removed
            ciphertext = a2b_hex(remove_whitespace("""
    96136621faf36d5290b16bd26295de27f895d1faa51c800dafce73d001d60796cd4e2ac3f
    a2162131d859cd9da5a0c8a42281d9a63e5f353971b72e36b5722e4ac444d77f892a5443deb
    3dca49fa732fe855727196e23c26eeac55eeced8267a209ebc0f92f4656d64a6c13f7f7ce54
    4ebeb0f668fe3a6c0f189e4bcd5ea12b73cf63e0c8350ee130dd62f01e5c97a1e13f52fde96
    a9a1bc9936ce734fdd61f27b18216f1d6de87f49cf4f2ea821fb8efd1f92cdad529baf7e31a
    ff9bff4074f2cad2b4243dd15a711adcf7de900851fbd6bcb53dac399d7c880531d06f25f70
    02e1aaf1722765865d2c2b902c7736acd27bc6cbd3e38b560e2eecf7d4b576
    """))
            self.assertEqual(len(ciphertext), numBytes(self.pub_key.n)-1)
    
            # sanity check that the decrypted ciphertext is invalid
            dec = self.priv_key._raw_private_key_op_bytes(b"\x00" + ciphertext)
            self.assertEqual(dec[0:3], b'\x00\x02\x00')
            self.assertNotEqual(dec[-12:-11], b'\x00')
    
            plaintext = a2b_hex(remove_whitespace("ba27b1842e7c21c0e7ef6a"))
    
>           msg = self.priv_key.decrypt(ciphertext)

unit_tests/test_tlslite_utils_rsakey.py:2108: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tlslite/utils/rsakey.py:447: in decrypt
    dec_bytes = self._raw_private_key_op_bytes(encBytes)
tlslite/utils/openssl_rsakey.py:73: in _raw_private_key_op_bytes
    return self._call_m2crypto(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <tlslite.utils.openssl_rsakey.OpenSSL_RSAKey object at 0x73e741dcc70>
method = <built-in function rsa_private_encrypt>
param = bytearray(b'\x96\x13f!\xfa\xf3mR\x90\xb1k\xd2b\x95\xde\'\xf8\x95\xd1\xfa\xa5\x1c\x80\r\xaf\xces\xd0\x01\xd6\x07\x96\xc...\x99\xd7\xc8\x80S\x1d\x06\xf2_p\x02\xe1\xaa\xf1r\'e\x86],+\x90,w6\xac\xd2{\xc6\xcb\xd3\xe3\x8bV\x0e.\xec\xf7\xd4\xb5v')
err_msg = 'Bad parameters to private key operation'

    def _call_m2crypto(self, method, param, err_msg):
        try:
            return bytearray(method(self.rsa, bytes(param), m2.no_padding))
>       except M2Crypto.RSA.RSAError:
E       AttributeError: module 'M2Crypto' has no attribute 'RSA'

tlslite/utils/openssl_rsakey.py:87: AttributeError

This change is Reviewable

@tomato42
Copy link
Member

I think the code has to be changed to reference RSA instead of M2Crypto.RSA

@sthen
Copy link
Author

sthen commented Dec 20, 2023

ah, perhaps the change to tlslite/utils/openssl_rsakey.py is not actually needed then, things still work if I only have the unit_tests/test_tlslite_utils_keyfactory.py patch..

@tomato42
Copy link
Member

the version_info is also a problem... It should be fixed, but a new version isn't released probably: https://gitlab.com/m2crypto/m2crypto/-/merge_requests/315

@sthen
Copy link
Author

sthen commented Dec 20, 2023

ah yes, sorry I forgot about version_info, I had backported the commit from that merge request to the version I was testing with..

@tomato42
Copy link
Member

tomato42 commented Jan 2, 2024

will you update the PR to include workaround for it?

@tomato42
Copy link
Member

fixed in #508

@tomato42 tomato42 closed this Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants