Qt: QSslCertificate::fromPath 没有找到我的文件,但是 QFileInfo 找到了

Qt: QSslCertificate::fromPath doesn't find my file, but QFileInfo finds it

我的问题是在下面的代码中,"QSslCertificate::fromPath" 没有找到我指定的文件,但是当我用下面的 fileExists 函数检查它时,它告诉我文件毕竟存在.只有当我尝试 运行 我的应用程序在不同于我的开发 PC 的 PC 上时才会出现此问题。我在 Windows 10 64 位上开发,测试 PC 是 Windows 7 64 位。我使用 QT 5.4.0。我做错了什么?

void MyClass::init()
{

    // ... some other init code

    QLatin1String rootCApath = QLatin1String("./ssl/rootCA.crt");
    if (fileExists(rootCApath))
        log("File exists"); // This is just a log function for displaying messages in the GUI.
    else
        log("File doesn't exist");

    static QList<QSslCertificate> cert = QSslCertificate::fromPath(rootCApath);
    if(cert.size()==1)
    {
        ssl_configuration.setCaCertificates(cert);
        m_webSocket.setSslConfiguration(ssl_configuration);
    }
    else
    {
        QString s = "Server certificate not found. Size is " + QString::number(cert.size());
        log(s);
    }
}

bool MyClass::fileExists(QString path) {
    QFileInfo check_file(path);
    // check if file exists and if yes: Is it really a file and no directory?
    return check_file.exists() && check_file.isFile();
}

编辑:当我将证书读入 QByteArray 并将其传递给它时,我的连接函数在 Windows 7 PC 上没有任何作用,而在我的 Windows 10 上开发者 PC 一切正常。

还有另一台 PC 始终给我 TLS 握手错误,无论我做什么。 3个PC,3个结果,好郁闷

您的代码看起来不错,在我的 linux 系统上运行没有任何问题。您应该检查相对路径或简单地尝试通过 QFileInfo 设置证书:

QLatin1String rootCApath = QLatin1String("./ssl/rootCA.crt");
QFileInfo temp(rootCApath);
QString tempssl;
QDir dir ;//try to set dir also if this does not work dir("/home/foo/yourapppath/");  

tempssl=dir.relativeFilePath("./ssl/rootCA.crt");   
static QList<QSslCertificate> cert = QSslCertificate::fromPath(tempssl);

if(cert.size()==1)
{
    ssl_configuration.setCaCertificates(cert);
    m_webSocket.setSslConfiguration(ssl_configuration);
}
else
{
    QString s = "Server certificate not found. Size is " + QString::number(cert.size());
    log(s);
}

我在我的案例中找到了解决方案:我没有将 OpenSSL 库 libeay32.dll 和 ssleay32.dll 添加到我的 exe 所在的目录。