【问题标题】:openssl SSL_get_verify_result returns error 20openssl SSL_get_verify_result 返回错误 20
【发布时间】:2015-03-14 06:28:44
【问题描述】:

我正在编写一个使用 SSL 连接的 C++ 程序。证书链检查使用:

openssl 验证 -CAfile test.pem private.pem

其中 test.pem 包含中间证书和根证书。我的测试程序不验证证书链。

if ( !SSL_CTX_load_verify_locations( ctx, "c:/Certs/test.pem", NULL ) ) {
    // Failure message and cleanup goes here.
}

SSL* ssl;
BIO* bio = BIO_new_ssl_connect( ctx );
BIO_get_ssl( bio, &ssl );
SSL_set_mode( ssl, SSL_MODE_AUTO_RETRY );

BIO_set_conn_hostname( bio, "url.com:https" );

if ( BIO_do_connect( bio ) <= 0 ) {
    // Failure message and cleanup goes here.
}

if ( SSL_get_verify_result( ssl ) != X509_V_OK ){
    // Here is where I get the error 20...
    // Free all resources and exit.
}

OpenSSL 文档将错误 20 描述为:

  1. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:无法获取本地颁发者证书。 找不到颁发者证书:如果颁发者证书 找不到不受信任的证书。

我需要帮助来确定问题以及解决方法。我确定我拥有的证书是正确的。

【问题讨论】:

    标签: c++ ssl https


    【解决方案1】:

    证书或证书链似乎不受信任。 您可以在尝试连接之前从 pem 文件中加载您自己的文件:

    int rc = SSL_CTX_load_verify_locations(ssl_context, file_name, NULL);
    if (rc != 1) { // verify authentication result
            g_warning("Load of certificates failed!: %s", X509_verify_cert_error_string(ERR_get_error()));
            return FALSE;
    }
    

    此外,您可以直接从内存中加载。

    这样的:

    char *chain_certs = "------- BEGIN CERTIFICAT...."; /// <<< YOUR CERTIFICATE CHAIN
    // Load chain of certs
    X509 *cacert=NULL;
    BIO *mem = BIO_new_mem_buf(chain_certs,strlen(chain_certs));
    X509_STORE *cert_store = SSL_CTX_get_cert_store(ssl_context);
    
    if(cert_store!=NULL){
        int index = 0;
        while ((cacert = PEM_read_bio_X509(mem, NULL, 0, NULL))!=NULL) {
            if(cacert) {
                g_debug("Our certificate name is %s", cacert->name);
                X509_STORE_add_cert(cert_store, cacert);
                X509_free(cacert);
                cacert=NULL;
            } /* Free immediately */
            index++;
        }
    }
    BIO_free(mem);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2021-07-08
      相关资源
      最近更新 更多