c++ - 安全 SSL 连接错误 : unknown protocol | bad hostname lookup

标签 c++ http openssl

我正在通过 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/

相关文章:

javascript - Angular $http 中的重复参数

如果还使用了 EVP_PKEY_free,C++ OpenSSL RSA_free 会给出段错误

command-line - openssl命令行验证签名

c++ - 具有 float 的 C++ 程序的输出是否可预测?

c++ - 取消引用函数参数中的引用

c++ - undefined reference (模板)

linux - 如何使用公钥在 Ubuntu 中使用 openssl 加密 XML 文件?

c++ - 无符号类型按位移位的 RHS 操作数类型

android - 如何从android访问本地主机?

java - HttpURL连接 "An unexpected error occurred."