我正在寫一個接受SSL連接的服務器。我已經生成的自簽名證書:沒有可用自簽名證書的對等證書
openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem -subj "/CN=myhostname"
SSL_accept
失敗,此消息:
140121764049248:error:1408A0C1:SSL routines:ssl3_get_client_hello:no shared cipher:s3_srvr.c:1417:
這裏是服務器代碼的一部分:
sslCtx = (SSL_CTX_new(SSLv23_server_method());
if (!sslCtx){
ERR_print_errors_fp(stderr);
throw runtime_error("SSL_CTX_new failed");
}
ssl = SSL_new(sslCtx);
if (!ssl)
throw runtime_error("SSL_new failed");
if (SSL_CTX_use_PrivateKey_file(sslCtx, keyFile.c_str(),
SSL_FILETYPE_PEM) != 1)
throw runtime_error("Unable to load private key file" + keyFile);
if (SSL_CTX_use_certificate_file(sslCtx, certFile.c_str(),
SSL_FILETYPE_PEM) != 1)
throw runtime_error("Unable to load certificate file" + certFile);
if (SSL_set_fd(ssl, socket) != 1)
throw runtime_error("SSL_set_fd failed");
if (SSL_accept(ssl) != 1){
ERR_print_errors_fp(stderr);
throw runtime_error("SSL_accept failed");
}
我試圖測試服務器:
openssl s_client -cipher RSA -connect myhostname:33221 -tls1 -CApath . -servername myhostname
並得到了
CONNECTED(00000003)
139898773520408:error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:s3_pkt.c:1487:SSL alert number 40
139898773520408:error:1409E0E5:SSL routines:ssl3_write_bytes:ssl handshake failure:s3_pkt.c:656:
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 7 bytes and written 0 bytes
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
Protocol : TLSv1
Cipher : 0000
Session-ID:
Session-ID-ctx:
Master-Key:
Key-Arg : None
PSK identity: None
PSK identity hint: None
SRP username: None
Start Time: 1496232544
Timeout : 7200 (sec)
Verify return code: 0 (ok)
---
我使用的是OpenSSL 1.0.2g。
***'CN = myhostname' ***可能是錯誤的。主機名始終在* SAN *中。如果它存在於* CN *中,那麼它也必須存在於* SAN *中(在這種情況下,您必須列出它兩次)。有關更多規則和原因,請參閱[如何使用您的證書頒發機構簽署證書籤名請求](http://stackoverflow.com/a/21340898/608639)和[如何使用openssl創建自簽名證書?]( http://stackoverflow.com/q/10175812/608639)您還需要將自簽名證書放入適當的信任庫中。 – jww
請提供您用於連接服務器的URL,併發布'openssl s_client -connect: -tls1 -servername | openssl x509 -text -noout'。另請參閱OpenSSL wiki上的[TLS客戶端](https://wiki.openssl.org/index.php/SSL/TLS_Client)。它向您展示瞭如何在客戶端上下文中配置SSL_CTX。它大部分也適用於服務器。另外,這可能會給你帶來麻煩:'if(SSL_accept(ssl)!= 1)',特別是在非阻塞套接字上。我似乎記得非阻塞套接字經常返回'-1',並且它不是錯誤。 –
jww