问题
浏览这些不同的 OpenSSL 3.0 文档
- https://security.stackexchange.com/questions/34791/openssl-vs-fips-enabled-openssl
- https://github.com/openssl/openssl/blob/master/README-FIPS.md
- https://www.openssl.org/docs/man3.0/man7/fips_module.html
- https://en.wikipedia.org/wiki/FIPS_140-2
我设法拼凑出一个在 OpenSSL 中启用 FIPS 模式的解决方案,并牢记以下目标:
重点是,一旦在 OpenSSL 中启用 FIPS 模式,通过 OpenSSL 执行的所有加密操作(包括 C++ Boost.Asio 用于 SSL/TLS 连接的加密操作)都将使用 FIPS 批准的算法,确保整个过程都符合 FIPS 标准。 Boost C++ 应用程序。
enableFIPS()
这是为 Boost C++ 启用 OpenSSL FIPS 模式的有效函数吗?
例子
主程序
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <openssl/provider.h>
bool PrintErrorSSL(const std::string& err)
{
std::cerr << err << std::endl;
unsigned long err_code;
while ((err_code = ERR_get_error()) != 0)
{
char err_msg[256];
ERR_error_string_n(err_code, err_msg, sizeof(err_msg));
std::cerr << "OpenSSL error: " << err_msg << std::endl;
}
// FIPS mode not enabled.
return false;
}
bool enableFIPS()
{
std::string ConfigPath;
std::string TestPath = "C:/Projects/sys-openssl-fips/Test/";
#ifdef _WIN32
ConfigPath = TestPath + "openssl-fips.cnf";
#else
ConfigPath = TestPath + "openssl-fips-linux.cnf";
#endif
if (!OSSL_PROVIDER_set_default_search_path(nullptr, TestPath.c_str()))
{
return PrintErrorSSL("OSSL_PROVIDER_set_default_search_path() Failed");
}
if (!OSSL_LIB_CTX_load_config(nullptr, ConfigPath.c_str()))
{
return PrintErrorSSL("OSSL_LIB_CTX_load_config() Failed");
}
if (!OSSL_PROVIDER_available(nullptr, "fips") || !OSSL_PROVIDER_available(nullptr, "base"))
{
return PrintErrorSSL("OSSL_PROVIDER_available() Failed");
}
if (!EVP_default_properties_is_fips_enabled(nullptr))
{
return PrintErrorSSL("EVP_default_properties_is_fips_enabled() Failed");
}
// FIPS mode enabled.
return true;
}
int main(int argc, char* argv[])
{
if (enableFIPS())
{
using boost::asio::ip::tcp;
boost::asio::io_context io_context;
boost::asio::ssl::context ssl_context(boost::asio::ssl::context::tlsv12);
boost::asio::ssl::stream<tcp::socket> socket(io_context, ssl_context);
tcp::resolver resolver(io_context);
auto endpoints = resolver.resolve("www.google.com", "443");
boost::asio::connect(socket.lowest_layer(), endpoints);
socket.handshake(boost::asio::ssl::stream_base::client);
std::string request = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
boost::asio::write(socket, boost::asio::buffer(request));
boost::asio::streambuf response;
boost::system::error_code ec;
boost::asio::read_until(socket, response, "\r\n", ec);
std::istream response_stream(&response);
std::string http_status;
std::getline(response_stream, http_status);
if (http_status.find("200 OK") != std::string::npos)
return 0;
}
return 1;
}
openssl-fips.cnf
config_diagnostics = 1
openssl_conf = openssl_init
[fips_sect]
activate = 1
conditional-errors = 1
security-checks = 1
module-mac = D9:AB:CC:37:8A:4C:06:BF:E9:E3:A8:F7:B9:B5:02:48:58:71:76:EB:5E:71:8A:0F:87:AA:52:46:7D:60:B0:EB
[openssl_init]
providers = provider_sect
alg_section = algorithm_sect
[provider_sect]
fips = fips_sect
base = base_sect
[base_sect]
activate = 1
[algorithm_sect]
default_properties = fips=yes
输出(Windows)
OpenSSL FIPS 模式:关闭
项目将利用这些 openssl静态库提供的非 FIPS 批准的算法
- libssl - 实现传输层安全 (TLS) 协议。
- libcrypto - 实现加密算法。
OpenSSL FIPS 模式:开
项目将利用这些 openssl共享库提供的经 FIPS 批准的算法
- fips.so(在 Linux 上)
- fips.dll(在 Windows 上)
使用Process Explorer加载fips.dll
: