【发布时间】: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