我正在通过 HTTP 从服务器下载 RSS 提要文件(例如 https://tools.ietf.org/dailydose/dailydose_atom.xml)。
首先,我必须通过 OpenSSL 连接到远程服务器,如所述here .
不安全的版本工作得很好,我可以连接并接收带有提要的 HTTP 应答:
bio = BIO_new_connect("www.tools.ietf.org:80");
if(bio == NULL)
{
/* Handle the failure */
}
if(BIO_do_connect(bio) <= 0)
{
/* Handle failed connection */
}
安全版本:
BIO * m_bio;
SSL_CTX * m_ctx;
SSL * m_ssl;
SSL_library_init();
m_ctx = SSL_CTX_new(SSLv23_client_method());
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
SSL_CTX_set_default_verify_paths(m_ctx);
m_bio = BIO_new_ssl_connect(m_ctx);
BIO_get_ssl(m_bio, &m_ssl);
SSL_set_mode(m_ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(m_bio, "www.tools.ietf.org:80");
if (BIO_do_connect(m_bio) <= 0)
{
printf("Error: %s\n", ERR_reason_error_string(ERR_get_error()));
throw std::runtime_error("FEEDREADER: Connection failed.");
}
if(SSL_get_verify_result(m_ssl) != X509_V_OK)
{
throw std::runtime_error("FEEDREADER: Verification failed.");
}
do_connect
失败并出现以下错误:
Error: unknown protocol
当我将 www.tools.ietf.org
替换为 http(s)://www.tools.ietf.org
出现另一个错误:
Error: bad hostname lookup
但主机名和 dns 适用于不安全的版本,所以有人可以帮我解决这个问题吗?
最佳答案
80 是默认的 HTTP 端口。 443 是默认的 HTTPS 端口。
bio = BIO_new_connect("www.tools.ietf.org:443");
BIO_set_conn_hostname(m_bio, "www.tools.ietf.org:443");
关于c++ - 安全 SSL 连接错误 : unknown protocol | bad hostname lookup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52540961/