Qt:QSslCertificate :: fromPath找不到我的文件,但QFileInfo找不到它

时间:2017-01-23 11:17:24

标签: c++ qt ssl certificate

我遇到的问题是,在下面的代码中,“QSslCertificate :: fromPath”找不到我指定的文件,但是当我用下面的fileExists-function检查它时,它告诉我该文件毕竟存在。当我尝试在不同于我的开发PC的PC上运行我的应用程序时,才会出现此问题。我在Windows 10 64 Bit上开发,测试PC是Windows 7 64 Bit。我使用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 Developer PC上,一切仍然正常。

另外还有另一台PC,无论我做什么,都会一直给我TLS握手错误。 3台电脑,3个结果,令人沮丧。

2 个答案:

答案 0 :(得分:0)

您的代码看起来很好,并且在我的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);
}

答案 1 :(得分:0)

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