gpt4 book ai didi

encryption - ssl_accept上的“no shared cipher”,为什么?

转载 作者:行者123 更新时间:2023-12-04 23:40:03 25 4
gpt4 key购买 nike

用谷歌搜索了很多,没有找到以下问题的答案:
创建了服务器代码和客户端代码,但是得到了

error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher



在服务器上执行 SSL_connect时。

下面的代码仅缩小到与SSL/套接字相关的函数调用的顺序。在适用的情况下已应用了错误处理代码,以确保 SSL_accept/ SSL_connect之前的调用不会返回任何失败代码。我还省略了初始化方法。

我不知道这是否重要,但是我在本地主机上同时运行服务器和客户端。

可能存在明显的错误,但是我对OpenSSL还是很陌生。

客户端代码(参数: hostnamecertificate_chain_fileca_certificate_file):
SSL_library_init();             // <<< To clarify my initialization
OpenSSL_add_all_algorithms(); // <<< To clarify my initialization
SSL_load_error_strings(); // <<< To clarify my initialization
ERR_load_crypto_strings(); // <<< To clarify my initialization (2)
OpenSSL_add_all_ciphers(); // <<< To clarify my initialization (2)
SSL_METHOD const * method = SSLv23_method(); // <<< Updated method
SSL_CTX * ctx = SSL_CTX_new(method);
SSL_CTX_use_certificate_chain_file(ctx, certificate_chain_file));
const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION; // <<< Added
SSL_CTX_set_options(ctx, flags); // <<< Added
SSL_CTX_load_verify_locations(ctx, ca_certificate_file, NULL));
struct hostent * host = gethostbyname(hostname);
int client_sd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(6789);
server.sin_addr.s_addr = *(long *) (host->h_addr);
connect(client_sd, (struct sockaddr *) &server, sizeof(server));
SSL * ssl = SSL_new(ctx);
SSL_set_fd(ssl, client_sd);
const char * const preferred_ciphers = "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4";
SSL_set_cipher_list(ssl, preferred_ciphers); // <<< Added
SSL_set_tlsext_host_name(ssl, hostname); // <<< Added
mydata_t mydata;
mydata_index_client = SSL_get_ex_new_index(0, (void *) "mydata index", NULL, NULL, NULL);
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback_client);
SSL_CTX_set_verify_depth(ctx, 1);
mydata.verify_depth = 0;
SSL_set_ex_data(ssl, mydata_index_client, &mydata);
int connection_result = SSL_connect(ssl);
if (connection_result < 0)
{
// Comes in here and ERR_get_error indicates
// error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
}
else if (connection_result == 0)
{
}
else if (connection_result == 1)
{
}
else
{
}

服务器代码(参数: certificate_chain_fileca_certificate_file):
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
SSL_METHOD const * method = SSLv23_method();
SSL_CTX * ctx = SSL_CTX_new(method);
SSL_CTX_use_certificate_chain_file(ctx, certificate_chain_file); //Contains only root CA
SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *) private_key_file_password);
SSL_CTX_set_default_passwd_cb(ctx, pem_passwd_cb);
SSL_CTX_load_verify_locations(ctx, ca_certificate_file, NULL);
struct sockaddr_in addr;
int server_sd = create_socket(addr, 6789);
bind(server_sd, (struct sockaddr *) &addr, sizeof(addr));
listen(server_sd, max_nr_of_simultaneous_connections);
sockaddr_in client;
client.sin_family = AF_INET;
socklen_t c_len = sizeof(client);
int client_sd = accept(server_sd, (sockaddr *) &client, &c_len);
char remote_addr[INET_ADDRSTRLEN];
inet_ntop(client.sin_family, &(client.sin_addr), remote_addr, INET_ADDRSTRLEN);
SSL * ssl = SSL_new(ctx);
SSL_set_fd(ssl, client_sd);
const char * const preferred_ciphers = "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4";
SSL_set_cipher_list(ssl, preferred_ciphers); // <<< Added
STACK_OF(X509_NAME) * cert_names = SSL_load_client_CA_file(certificate_chain_file);
if (cert_names != NULL)
{
SSL_CTX_set_client_CA_list(ctx, cert_names);
}
mydata_t mydata;
mydata_index_server = SSL_get_ex_new_index(0, (void *) "mydata index", NULL, NULL, NULL);
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback_server);
SSL_CTX_set_verify_depth(ctx, 1);
mydata.verify_depth = 1;
SSL_set_ex_data(ssl, mydata_index_server, &mydata);
int accept_result = SSL_accept(ssl);
if (accept_result == 0)
{
}
else if (accept_result < 0)
{
// Comes in here and ERR_get_error indicates
// error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher
}

编辑:
jww,我已经尝试过您在下面提出的建议。但是,没有任何进展;我仍然得到相同的错误输出。
这是我创建证书的方式:

openssl-ca.cnf
HOME            = .
RANDFILE = $ENV::HOME/.rnd

####################################################################
[ ca ]
default_ca = CA_default # The default ca section

####################################################################
[ CA_default ]
default_days = 1000 # how long to certify for
default_crl_days = 30 # how long before next CRL
default_md = sha256 # use public key default MD
preserve = no # keep passed DN ordering

x509_extensions = ca_extensions # The extensions to add to the cert

email_in_dn = no # Don't concat the email in the DN
copy_extensions = copy # Required to copy SANs from CSR to cert

base_dir = .
certificate = $base_dir/certs/ca_fromweb.cert.pem # The CA certifcate
private_key = $base_dir/private/ca.key.pem # The CA private key
new_certs_dir = $base_dir # Location for new certs after signing
database = $base_dir/index2.txt # Database index file
serial = $base_dir/serial2.txt # The current serial number

unique_subject = no # Set to 'no' to allow creation of
# several certificates with same subject.

####################################################################
[ req ]
default_bits = 4096
default_keyfile = ./private/ca.key.pem
distinguished_name = ca_distinguished_name
x509_extensions = ca_extensions
string_mask = utf8only

####################################################################
[ ca_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = SE

stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = Östergötland

localityName = Locality Name (eg, city)
localityName_default =

organizationName = Organization Name (eg, company)
organizationName_default =

organizationalUnitName = Organizational Unit (eg, division)
organizationalUnitName_default =

commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_default =

emailAddress = Email Address
emailAddress_default =

####################################################################
[ ca_extensions ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always, issuer
basicConstraints = critical, CA:true
keyUsage = keyCertSign, cRLSign

####################################################################
[ signing_policy ]
countryName = optional
stateOrProvinceName = optional
localityName = optional
organizationName = optional
organizationalUnitName = optional
commonName = supplied
emailAddress = optional

####################################################################
[ signing_req ]
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer

basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment

openssl-server.cnf
HOME            = .
RANDFILE = $ENV::HOME/.rnd

####################################################################
[ req ]
default_bits = 2048
default_keyfile = ./intermediate/private/my.example.com.key.pem
distinguished_name = server_distinguished_name
req_extensions = server_req_extensions
string_mask = utf8only

####################################################################
[ server_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = SE

stateOrProvinceName = State or Province Name (full name)
stateOrProvinceName_default = Östergötland

localityName = Locality Name (eg, city)
localityName_default = Linköping

organizationName = Organization Name (eg, company)
organizationName_default =

commonName = Common Name (e.g. server FQDN or YOUR name)
commonName_default =

emailAddress = Email Address
emailAddress_default =

####################################################################
[ server_req_extensions ]
subjectKeyIdentifier = hash
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
subjectAltName = @alternate_names
nsComment = "OpenSSL Generated Certificate"

####################################################################
[ alternate_names ]
DNS.1 = my.example.com

命令
 touch index.txt
echo 1000 > serial
openssl genrsa -aes256 -out ca.key.pem 4096
chmod 400 private/ca.key.pem
openssl req -config openssl-ca.cnf -key ca.key.pem -new -x509 -days 7300 -sha256 -extensions ca_extensions -out ca.cert.pem
chmod 444 ca.cert.pem

openssl genrsa -aes256 -out server.key.pem 4096
openssl req -config openssl-server.cnf -new -sha256 -key server.key.pem -out my.example.com.csr.pem
openssl ca -config openssl-ca.cnf -policy signing_policy -extensions signing_req -out my.example.com.cert.pem -infiles my.example.com.csr.pem
chmod 444 my.example.com.cert.pem
cat ca.cert.pem > ca_chain.cert.pem

编辑2:也尝试了
 ERR_load_crypto_strings();      // <<< To clarify my initialization (2)
OpenSSL_add_all_ciphers(); // <<< To clarify my initialization (2)

参见顶部。结果相同。

“不幸的是,所有初始化函数都返回一个无用的值(例如,始终为1)或为void函数。无法确定是否发生了故障。”-( https://wiki.openssl.org/index.php/Library_Initialization)很烂!

最佳答案

“no shared cipher” at ssl_accept, why?



可能有几个原因。以下是一些建议,具体取决于您遇到的问题。我怀疑一个或多个是您问题的答案。

Client:

SSL_METHOD const * method = SSLv3_client_method();


和:

Server:

SSL_METHOD const * method = SSLv23_method();


您应该首先设置“TLS 1.0及更高版本”。您可以使用以下方法在客户端和服务器上执行此操作。它来自OpenSSL Wiki和 SLL/TLS Client示例。
const SSL_METHOD* method = SSLv23_method();
if(method == NULL) handleFailure();

ctx = SSL_CTX_new(method);
if(ctx == NULL) handleFailure();

...

/* Cannot fail ??? */
const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
SSL_CTX_set_options(ctx, flags);

您的客户也应该使用 Server Name Indication (SNI)。客户端将其与 SSL_set_tlsext_host_name一起使用。 SNI是TLS扩展,它是您希望使用“TLS 1.0及更高版本”的部分原因。

客户端和服务器都希望使用 "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4"这样的密码套件列表。使用 SSL_CTX_set_cipher_listSSL_set_cipher_list设置它们。它避免了来自浏览器的 “Your connection to website is encrypted with obsolete cryptography”警告。

如果您正在使用基于椭圆曲线的证书,则需要使用命名曲线。另请参阅堆栈溢出上的 boost asio with ECDSA certificate issue和OpenSSL Wiki上的 Elliptic Curve Cryptography | Named Curves

确保初始化OpenSSL库。如果未正确初始化库,则将没有可用的密码,并且可能导致“没有共享密码”。另请参阅堆栈溢出上的 Openssl SSL_CTX_new(SSLv3_method()) returns NULL和OpenSSL Wiki上的 Library Initialization

如果要创建自己的证书,请确保将主机名放在“使用者备用名称”(SAN)中。主机名总是放在SAN中。如果它存在于CN中,那么它也必须存在于SAN中(在这种情况下,您必须列出两次)。有关更多规则和原因,请参见 How do you sign Certificate Signing Request with your Certification AuthorityHow to create a self-signed certificate with openssl?

关于encryption - ssl_accept上的“no shared cipher”,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454338/

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