gpt4 book ai didi

python - 如何为Python打包的libcrypto和libssl启用FIPS模式?

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:15 30 4
gpt4 key购买 nike

我有一个python应用程序,它与python、Libcrypto和LibSSL共享对象打包在一起应用程序是用Openssl Fips模块2.0构建的Python的请求模块和urllib3使用这些共享对象来发出TLS请求。
我在构建应用程序的环境中启用了OPENSSL_FIPS标志现在,如果想检查当我将共享对象从开发环境中取出并放到另一台计算机中时,它们是否启用了fips模式,我该如何做?
如何检查fips模式是否已启用如果不是,如何为这些共享对象启用fips模式?
可能有帮助的其他详细信息:
OpenSSL版本:1.0.2H(从源代码构建)
FIPS模块:2.0.12(从源代码构建)
蟒蛇:3.6
操作系统:ubuntu 16.04 LTS
如果需要其他细节,请告诉我。
谢谢!

最佳答案

我已经使用常规标志构建了openssl fips模块(例如:no asm,shared,some olgent ciphers disabled):

[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> ls ssl/build/bin ssl/build/lib
ssl/build/bin:
c_rehash openssl

ssl/build/lib:
engines libcrypto.a libcrypto.so libcrypto.so.1.0.0 libssl.a libssl.so libssl.so.1.0.0 pkgconfig

开始玩了一点:
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> ssl/build/bin/openssl version
OpenSSL 1.0.2h-fips 3 May 2016 (Library: OpenSSL 1.0.2g 1 Mar 2016)

请注意“(库:OpenSSL 1.0.2g 2016年3月1日)”部分这个(存在)表明openssl可执行文件是正常的(预期的版本),但它使用了错误的libcrypto(它是默认安装在系统上的libcrypto-under/lib下的,并且通常不是用fips支持构建的)。它必须加载我们的库,这可以通过设置LD_LIBRARY_PATH来完成(同样的行为也可以通过在构建OpenSSL时设置env var来实现,该操作本来可以在OpenSSL可执行文件中设置rpath,但我忘记了,我不想再构建它):
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> LD_LIBRARY_PATH=ssl/build/lib ssl/build/bin/openssl version
OpenSSL 1.0.2h-fips 3 May 2016

现在,安装成功了,让我们进入openssl_fips env var:
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> LD_LIBRARY_PATH=ssl/build/lib ssl/build/bin/openssl md5 ./code.py
MD5(./code.py)= d41d8cd98f00b204e9800998ecf8427e
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> LD_LIBRARY_PATH=ssl/build/lib ssl/build/bin/openssl sha1 ./code.py
SHA1(./code.py)= da39a3ee5e6b4b0d3255bfef95601890afd80709
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> OPENSSL_FIPS=1 LD_LIBRARY_PATH=ssl/build/lib ssl/build/bin/openssl sha1 ./code.py
SHA1(./code.py)= da39a3ee5e6b4b0d3255bfef95601890afd80709
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> OPENSSL_FIPS=1 LD_LIBRARY_PATH=ssl/build/lib ssl/build/bin/openssl md5 ./code.py
Error setting digest md5
139778679649944:error:060A80A3:digital envelope routines:FIPS_DIGESTINIT:disabled for fips:fips_md.c:180:

从上面可以看出,md5哈希行为受OPENSSL_FIPS env var的影响(当FIPS mode为on时,不允许使用它)。
笔记:
最有可能的是,较新的openssl fips版本也会禁用sha1,因为它被认为是弱的,所以不变量应该切换到sha2散列函数家族中的一个(例如sha256)或者更好的sha3(较旧的openssl版本可能没有它)。
从我的pov来看,这有点太过严格,因为可能有些情况下,为了不关心安全性的目的,需要使用散列算法,而且仍然需要使用更复杂(也更耗时)的允许算法。
由于openssl_fips env var是在openssl可执行级别处理的,它将被绕过(因为libcrypto将被直接使用),因此对于当前的情况没有任何用处,因此我们必须深入研究。以下是在加载的libcrypto实例中控制fips模式的函数:
[OpenSSL]: FIPS mode()
[OpenSSL]: FIPS mode set()
它们将用于读取/写入FIPS模式。为了测试是否真的设置了fips模式,将使用md5散列(从上面的示例)。
代码.py:
#!/usr/bin/env python3


import sys
import ssl
import ctypes


libcrypto = ctypes.CDLL("libcrypto.so.1.0.0")

fips_mode = libcrypto.FIPS_mode
fips_mode.argtypes = []
fips_mode.restype = ctypes.c_int

fips_mode_set = libcrypto.FIPS_mode_set
fips_mode_set.argtypes = [ctypes.c_int]
fips_mode_set.restype = ctypes.c_int

text = b""


if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
print("OPENSSL_VERSION: {:s}".format(ssl.OPENSSL_VERSION))
enable_fips = len(sys.argv) > 1

print("FIPS_mode(): {:d}".format(fips_mode()))
if enable_fips:
print("FIPS_mode_set(1): {:d}".format(fips_mode_set(1)))
print("FIPS_mode(): {:d}".format(fips_mode()))

import hashlib
print("SHA1: {:s}".format(hashlib.sha1(text).hexdigest()))
print("MD5: {:s}".format(hashlib.md5(text).hexdigest()))

笔记:
[Python 3]: ctypes - A foreign function library for Python中指定的两个函数设置argtypes和restype
md5哈希算法在python级别由 [Python 3]: hashlib - Secure hashes and message digests提供。
重要提示: import hashlib语句位于设置fips模式之后(而不是在文件的开头,因为它应该在文件的开头),因为hashlib在导入时执行一些缓存,所以它在导入时捕获fips值,并且不关心它是否在之后更改。
输出:
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> LD_LIBRARY_PATH=ssl/build/lib ./code.py
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux

OPENSSL_VERSION: OpenSSL 1.0.2h-fips 3 May 2016
FIPS_mode(): 0
FIPS_mode(): 0
SHA1: da39a3ee5e6b4b0d3255bfef95601890afd80709
MD5: d41d8cd98f00b204e9800998ecf8427e
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> LD_LIBRARY_PATH=ssl/build/lib ./code.py 1
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux

OPENSSL_VERSION: OpenSSL 1.0.2h-fips 3 May 2016
FIPS_mode(): 0
FIPS_mode_set(1): 1
FIPS_mode(): 1
SHA1: da39a3ee5e6b4b0d3255bfef95601890afd80709
fips_md.c(149): OpenSSL internal error, assertion failed: Digest Final previous FIPS forbidden algorithm error ignored
Aborted (core dumped)

如图所示,通过cTypes设置fips模式,确实可以设置它。
我不知道为什么它会出现segfaults,但是md5相关的代码只用于测试目的,所以在生产中不需要它。
我记得在某些LNX版本(可能是基于RH的)上,也可以通过编辑一些条目(在/proc?下)来设置FIPS模式(对于系统是全局的)。,但我记不起来了。
更优雅的方法是为这两个函数公开Python包装器检查 [Python]: Issue 27592: FIPS_mode() and FIPS_mode_set() functions in Python (ssl),我还提交了python 3.4的修补程序(在那里它们被ssl模块公开),但是基于以下参数被拒绝(其中前2个参数是相关的):
FIPS是一个不好的标准
OpenSSL将放弃对它的支持
它打破了一般性
您可以将它应用到Python3.6中(我认为它不会工作ootb,因为行号很可能会改变),而且(显然)您必须从源代码构建Python。
底线:
FIPS工作和FIPS验证之间有很大的区别,因为我相信您已经阅读了 [OpenSSL]: User Guide for the OpenSSL FIPS Object Module v2.0
[AskUbuntu]: Enable FIPS 140-2 in ubuntu还可能包含一些有用的信息
@编辑0:
这让我很吃惊,您在 [SO]: Not able to call FIPS_mode_set() of libcrypto.so with Python ctypes [duplicate]上遇到的行为也可能与加载的libcrypto错误有关(从一开始就检查 openssl version测试w/wo ld_library_路径)。不支持fips的openssl仍将导出这两个函数,但它们都只返回0。
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q049320993]> ./code.py 1
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux

OPENSSL_VERSION: OpenSSL 1.0.2g 1 Mar 2016
FIPS_mode(): 0
FIPS_mode_set(1): 0
FIPS_mode(): 0
SHA1: da39a3ee5e6b4b0d3255bfef95601890afd80709
MD5: d41d8cd98f00b204e9800998ecf8427e

因此,请确保通过指定ld_library_path加载正确的库!(还有其他方法,但这是最直接的方法)。

关于python - 如何为Python打包的libcrypto和libssl启用FIPS模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49320993/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com