AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78286395
Accepted
vengy
vengy
Asked: 2024-04-07 09:48:55 +0800 CST2024-04-07 09:48:55 +0800 CST 2024-04-07 09:48:55 +0800 CST

为 Boost C++ 启用 OpenSSL FIPS 模式

  • 772

问题

浏览这些不同的 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:

系统fips

boost
  • 1 1 个回答
  • 20 Views

1 个回答

  • Voted
  1. Best Answer
    sehe
    2024-04-08T03:31:56+08:002024-04-08T03:31:56+08:00

    看起来很接近。

    我想说你必须小心使用特定的构造函数asio::ssl::context。它看起来enableFIPS主要设置默认值,所以也许为了安全起见,您还可以手动实例化 SSL_CTX* 并使用https://live.boost.org/doc/libs/1_84_0/doc/html/boost_asio/reference/ssl__contextasio::ssl::context从中构造/context/overload2.html

    除此之外,我始终保证合规性测试。当然,您可以从一些内部单元/集成测试开始,检查是否主动拒绝不正确的配置。

    • 1

相关问题

  • 在 C++03 编译器上使用移动模拟将 boost::unique_lock 作为返回值从函数中移出是否安全?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve