【问题标题】:AC certificate not trusted during handshake握手期间 AC 证书不受信任
【发布时间】:2022-07-07 14:46:04
【问题描述】:

我有两个与 QSslSocket(协议 TLS)通信的应用程序(一个客户端和一个服务器)。 我为我的服务器创建了一个密钥和一个证书。我用 AC 签署证书。 (我也创建了 AC)。

#create AC
$ openssl genrsa -des3 -out ca.key
$ openssl req -new -x509 -days 365 -key ca.key -out ca.crt

#create server key
$ openssl genrsa -des3 -out server.key

#create server certificate (sign by AC)
$ openssl req -key server.key -new -out server.csr
$ openssl x509 -days 365 -req -in server.csr -out server.crt -CA ca.crt -CAkey ca.key -CAcreateserial

我希望我的客户验证服务器的证书。 我的客户:

bool MyClient::Connect()
{
    if(MySocket == nullptr)
    {
        MySocket = new QSslSocket();
    }

    //connect signal
    connect(MySocket, SIGNAL (sslErrors (QList<QSslError>)), this, SLOT (GererErreurs (QList<QSslError>)));
    ...

    MySocket->setProtocol(QSsl::TlsV1_2);

    QString path = "path/to/certificateSSL/";
    QSslConfiguration configuration = MySocket->sslConfiguration();

    QString ca = "ca.cert";
    if(configuration.addCaCertificates(chemin+ca) )
    {
        qDebug()<<"> CA OK";
    }

    MySocket->setSslConfiguration(configuration);

    MySocket->setPeerVerifyMode(QSslSocket::VerifyPeer);
    MySocket->setPeerVerifyName("myHostname");
    
    MySocket->abort();

    MySocket->connectToHostEncrypted(ServerAdress, static_cast<quint16> (PortServeur));

    if (!MySocket->waitForEncrypted(Timeout * 1000))
    {
        qDebug()<<("Error");
        return false;
    }

    qDebug()<<("Connexion client/serveur encrypted");

    ...
}

我的服务器:

void MyServeur::incomingConnection(qintptr descriptionSocket)
{
    MySocket = new QSslSocket(this);
    MySocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);

    // signal connection
    ...
    

    QString path = "path/to/certificatsSSL/";
    QSslConfiguration configuration = Soquette->sslConfiguration();

    chargePrivateKey(path, configuration);
    if (!configuration.privateKey().isNull())
    {
        qDebug()<<"> Private key OK";
    }

    QString ca = "ca.cert";
    if(configuration.addCaCertificates(chemin+ca) )
    {
        qDebug() << "> CA OK";
    }
    
    chargeCertificate(path, configuration);
    if (!configuration.localCertificate().isNull())
    {
        qDebug() << "> server certificate OK";
    }

    MySocket->setSslConfiguration(configuration);

    MySocket->setPeerVerifyMode(QSslSocket::VerifyNone);

    MySocket->startServerEncryption();

    if (MySocket->waitForReadyRead (TIMEOUT_SOCKET * 1000) == false)
    {
        qDebug()<<"Notification de connexion cryptée du logiciel non-reçue";
        return;
    }

    qDebug()<<"Connection encrypted";
}

void MyServer::chargePrivateKey(const QString &chemin, QSslConfiguration &conf)
{
    QString serverKey = "server.key";
    QFile   fileKey(chemin + serverKey);
    if (!fileKey.open(QIODevice::ReadOnly))
    {
        qDebug() << "error" << chemin + serverKey;
        return;
    }
    QSslKey key(&fileKey, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, "password");
    fileKey.close();
    conf.setPrivateKey(key);
}

void MyServer::chargeCertificat(const QString &chemin, QSslConfiguration &conf)
{
    QString serverCRT =  "server-signe.cert";
    QFile fileCertificat( chemin+serverCRT);
    if( ! fileCertificat.open( QIODevice::ReadOnly ) )
    {
        qDebug() << "Error"<<chemin+serverCRT ;
        return;
    }
    QSslCertificate certificate(&fileCertificat, QSsl::Pem);
    fileCertificat.close();
    conf.setLocalCertificate( certificate );
}

在握手过程中,我遇到了错误:

QSslError::CertificateUntrusted : 为此目的不信任根 CA 证书

我在 MacOS 上 我已在密钥库中添加了我的 AC,并且 AC 证书是“可靠的”

我在 Windows 或 Linux 上没有问题。

【问题讨论】:

    标签: qt openssl ssl-certificate tls1.2 qsslsocket


    【解决方案1】:

    证书必须是V3 版本,并且还需要指定一些必填字段。

    对于 iOS/macOS,底层机制是 SecureTransport。这有我们需要使用的其他强化措施。示例 - 我们不能使用“CN”进行域验证,而必须使用“SAN”(稍后会详细介绍)。我相信,这意味着我们需要一个 V3 x509 证书而不是默认的 OpenSSL 生成 V1 证书(同样,稍后会更多)。 Mac/iOS 也需要 AKI 和 EKU(我相信)。

    您可以在本文中阅读更多详细信息:https://github.com/kindid/QtSSLCustomCACerts

    【讨论】:

      猜你喜欢
      • 2012-07-05
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 2016-06-15
      • 2016-01-15
      • 2019-05-20
      相关资源
      最近更新 更多