pyca / cryptography

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.
https://cryptography.io
Other
6.63k stars 1.52k forks source link

Python-cryptography with Openssl-3 FIPS #7722

Closed Pankul94 closed 1 year ago

Pankul94 commented 2 years ago

I am using Openssl-3.0.5 and have enabled the fips provider in the openssl.cnf making sure that openssl is FIPS 140-2 validated. When I try to calculate MD5 hash, I get an error which confirms that openssl is FIPS validated as MD5 is not allowed in FIPS mode.

image image

I built Python3.8 with the above mentioned installation of Openssl and I am reading about Python-cryptography and the use Openssl here: https://cryptography.io/en/latest/openssl/ where its mentioned that :

cryptography depends on the OpenSSL C library for all cryptographic operation

I assumed that the use of NON-FIPS validated algorithms will not be supported if I use the FIPS provider in Openssl but that is not the case as I am still able to calculate MD5 digest without any error unlike Openssl:

image

I want to use Openssl with the FIPS provider to provide all cryptographic algorithms in Python-cryptography, is there a way to enable this?

reaperhulk commented 2 years ago

cryptography does not load the OpenSSL conf by default, so you need to enable the FIPS provider in 3.0.x programmatically. You can do this by calling _enable_fips on the backend object (from cryptography.hazmat.backends.openssl.backend import backend). Note that this is not a public function, so we don't guarantee its stability (but we use it for our tests and it is how we verify that our test suite passes in FIPS mode).

FIPS support in cryptography will result in InternalError exceptions in a variety of places if you attempt to use disallowed algorithms, but should work without issue if you make compliant calls.

Pankul94 commented 2 years ago

Thanks for the quick reply. I tried enabling fips on the backend object and I am getting an error:

dev-dsk-pankulga-2b-1b336df9 % ./python3.8
Python 3.8.14 (default, Oct 19 2022, 18:13:27)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> ssl.OPENSSL_VERSION
'OpenSSL 3.0.5 5 Jul 2022'
>>> import cryptography
>>> from cryptography.hazmat.backends.openssl.backend import backend
>>> backend.activate_builtin_random()
>>> backend._is_fips_enabled()
False
>>> backend._enable_fips()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pankulga/.local/lib/python3.8/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 233, in _enable_fips
    self._binding._enable_fips()
  File "/home/pankulga/.local/lib/python3.8/site-packages/cryptography/hazmat/bindings/openssl/binding.py", line 143, in _enable_fips
    _openssl_assert(self.lib, self.lib._fips_provider != self.ffi.NULL)
  File "/home/pankulga/.local/lib/python3.8/site-packages/cryptography/hazmat/bindings/openssl/binding.py", line 90, in _openssl_assert
    raise InternalError(
cryptography.exceptions.InternalError: Unknown OpenSSL error. This error is commonly encountered when another library is not cleaning up the OpenSSL error stack. If you are using cryptography with another library that uses OpenSSL try disabling it before reporting a bug. Otherwise please file an issue at https://github.com/pyca/cryptography/issues with information on how to reproduce this. ([_OpenSSLErrorWithText(code=126615813, lib=15, reason=786693, reason_text=b'error:078C0105:common libcrypto routines::init fail')])

Since I am able to load the FIPS provider in Openssl, I believe its setup correctly. Is there any other configuration needed to enable this for python-cryptography

reaperhulk commented 2 years ago

init fail suggests that the FIPS provider is not happy with something in the conf file. You can see how we set up the FIPS module configuration in our CI (https://github.com/pyca/cryptography/blob/main/.github/workflows/build_openssl.sh#L29-L34). I'm afraid we can't offer much more guidance beyond that.

Pankul94 commented 2 years ago

I was searching for this error and found this: https://github.com/pyca/cryptography/issues/6392

I built my Openssl with just enable-fips flag, maybe this has something to do with the way Openssl is built. I am not sure on this, I will try something.

Also, looking at the stack-trace, the problem is that self.lib._fips_provider is NULL, so is it possible that cryptography is not able to find the fips_module i.e fips.so and I might have to expose OPENSSL_MODULES env variable?