【问题标题】:OpenSSL API's crashing while using SSL_get_certificate()使用 SSL_get_certificate() 时 OpenSSL API 崩溃
【发布时间】:2014-09-04 09:17:54
【问题描述】:

以下命令打印提到的主机的 SHA1 指纹

openssl s_client -connect 主机名:端口 | openssl x509 -fingerprint -noout

要使用 c++ 执行相同的操作,我使用的是 ssl API

#include"openssl/ssl.h"
#include"openssl/bio.h"
#include "openssl/err.h"

#include<iostream>

using namespace std;

int main()
{    
    SSL_CTX * ctx = SSL_CTX_new(SSLv23_client_method());
    SSL * ssl;
    BIO * bio;

    SSL_library_init();

    bio = BIO_new_ssl_connect(ctx);

    BIO_set_conn_hostname(bio, "hostname:port");//Correct hostname and port is used

    if(BIO_do_connect(bio) <= 0)
    {
        cout << "success";
    }
    BIO_get_ssl(bio, & ssl);

    X509 *x509 = NULL;

    x509 = SSL_get_certificate( ssl );//Crashing point

    return 1;
}

应用程序崩溃

x509 = SSL_get_certificate(ssl);

知道为什么会崩溃吗?

【问题讨论】:

  • 另请参阅 OpenSSL wiki 上的示例 TLS Client

标签: c++ ssl openssl


【解决方案1】:

从手册页引用:

BIO_do_connect() 尝试连接提供的 BIO。如果连接建立成功,则返回 1。如果无法建立连接,则返回零或负值,...

但是,您的代码会检查零值或负值,然后错误地打印“成功”并继续。因此,BIO_get_ssl(bio, &amp; ssl); 很可能会失败并留下 ssl 指向 NULL,从而导致下一行崩溃。

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 2014-05-16
    • 2013-02-11
    • 1970-01-01
    相关资源
    最近更新 更多