gpt4 book ai didi

c++ - openssl SSL_get_verify_result 返回错误 20

转载 作者:太空狗 更新时间:2023-10-29 23:15:42 29 4
gpt4 key购买 nike

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

openssl verify -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:无法获取本地颁发者证书。找不到颁发者证书:如果颁发者证书找不到不受信任的证书。

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

最佳答案

证书或证书链似乎不受信任。在尝试连接之前,您可以从 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);

关于c++ - openssl SSL_get_verify_result 返回错误 20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970611/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com