c - 如何将 PFS 添加到用 c 和 openssl 编写的套接字服务器

标签 c sockets openssl

我尝试将 PFS(完全前向保密)添加到我的客户端-服务器应用程序中。

当我使用以下命令运行服务器时:

openssl s_server -key ./key.pem -cert ./cert.pem -accept 443 -cipher ECDHE-RSA-AES128-SHA -tls1_2

我可以通过以下 ctx 连接到我的客户:

SSL_CTX* initCTX() {
    SSL_METHOD *method;
    SSL_CTX *ctx;

    SSL_library_init();
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    method = TLSv1_2_client_method();
    ctx = SSL_CTX_new(method);

    if(ctx == NULL) {
            ERR_print_errors_fp(stderr);
            return NULL;
    }

    SSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA");

    return ctx;
}

当我使用以下 ctx 运行我的服务器应用程序时:

SSL_CTX* init_ssl_ctx() {
    SSL_METHOD const *method;
    SSL_CTX *ctx;

    SSL_library_init();
    OpenSSL_add_all_algorithms();
    SSL_load_error_strings();
    method = TLSv1_2_server_method();

    ctx = SSL_CTX_new(method);
    if(ctx == NULL) {
            ERR_print_errors_fp(stderr);
            abort();
    }
    SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
    SSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA");

    // ADDITIONAL CTX MODIFICATIONS TO ENABLE ECDHE

    SSL_CTX_use_certificate_file(ctx, "./cert.pem", SSL_FILETYPE_PEM);
    SSL_CTX_use_PrivateKey_file(ctx, "./key.pem", SSL_FILETYPE_PEM);
    return ctx;
}

并尝试与客户端连接,然后出现no shared cipher 错误。 私钥已使用 openssl genrsa 创建。

那么我的问题是:我如何修改 ctx 以添加 ECDHE 支持。我想我必须选择一条曲线,我可能需要为每个连接创建和交换 key 。

我还需要私钥文件吗?如果是——它有什么用?

最佳答案

其实我错过的是 Diffie-Hellman 参数和椭圆曲线 Diffie-Hellman 的配置。如果你不配置它们......

the PFS cipher suites will be silently ignored.

有关如何在 C 套接字服务器中配置和包含 Diffie-Hellman 参数和椭圆曲线 Diffie-Hellman 的更多信息和示例,请参见此处:http://wiki.openssl.org/index.php/Diffie-Hellman_parameters

关于c - 如何将 PFS 添加到用 c 和 openssl 编写的套接字服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23765090/

相关文章:

ruby - 如何在 Ruby 中以编程方式检测证书问题

c - 关于宏

node.js - Node.JS 应用程序出现 ERR_SERVER_ALREADY_LISTENING 错误,但仅限 10.2.1?

linux - 无法读取 s/mime 格式的 PKCS7 签名文件(大尺寸)

Android - 如何在长生命周期后台线程中运行套接字

c - 为什么这个 C 程序(使用套接字和文件处理)在文件末尾添加不需要的字符?

ssl.SSLError : [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl. c:645)

c - 我正在尝试在 Ubuntu 12.04.1 中进行系统调用。我在编译 hello.c 文件时遇到错误

c - Valgrind:有条件的跳跃或移动取决于未初始化的值

c - printf NULL 不带引号或非NULL,带引号的sql语句