我尝试将 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/