首页 > 解决方案 > openSSL SSL_CTX_load_verify_locations 失败

问题描述

在 Linux Debian 上,考虑以下 C/C++ 片段:

TRACE << "TLSv1_2_client_method() started";
// Set up a SSL_CTX object, which will tell our BIO object how to do its work
const SSL_METHOD* method = TLSv1_2_client_method();
TRACE << "TLSv1_2_client_method() ended";

if (method == NULL) {
    ERROR << "SSL_METHOD is null";
    return false;
}

TRACE << "SSL_CTX_new() started";
SSL_CTX* ctx = SSL_CTX_new(method);
TRACE << "SSL_CTX_new() ended";
if (ctx == NULL) {
    ERROR << "SSL CTX is null";
    return false;
}

TRACE << "SSL_CTX_set_verify() started";
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
TRACE << "SSL_CTX_set_verify() ended";

TRACE << "SSL_CTX_set_verify_depth() started";
SSL_CTX_set_verify_depth(ctx, 4);
TRACE << "SSL_CTX_set_verify_depth() ended";

TRACE << "SSL_CTX_load_verify_locations() started";
if (!SSL_CTX_load_verify_locations(ctx, CA_CERTIFICATE_FILE, NULL)) {
    ERROR << "SSL_CTX_load_verify_locations failed";
    SSL_CTX_free(ctx);
    return false;
}
TRACE << "SSL_CTX_load_verify_locations() ended";

这个函数已经运行了很长时间,但是前一天SSL_CTX_load_verify_locations返回了 0,因此函数返回了。

现在来自 openSSL 的文档:它只能在“操作失败,因为 CAfile 和 CApath 为 NULL 或指定位置之一的处理失败。检查错误堆栈以找出原因”时返回 0。

让我们把这句话分成几部分:

我感兴趣的是上面的可能

此功能每分钟定期运行。日志文件中充满了此错误消息“SSL_CTX_load_verify_locations failed”。因此,确实可能会发生“指定位置之一的处理失败”。

但!杀死应用程序,然后重新启动它后,该功能再次按预期工作,没有任何错误。

在这种情况下,什么会导致 SSL_CTX_load_verify_locations 失败?显然与验证本身无关。(因为它在我重新启动应用程序后立即自行修复)

标签: opensslcertificatessl-certificate

解决方案


推荐阅读