gpt4 book ai didi

qt - 客户端应用程序如何使用 Qt 中的自签名证书连接到 SSL 服务器?

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:17 50 4
gpt4 key购买 nike

我想通过我的客户端应用程序与一个使用 ssl 和端口 995 的 POP3 服务器通信服务器的证书是自签名的,在运行应用程序时收到的错误是:

The certificate is self-signed, and untrusted

部分代码为:

socket = new QSslSocket(this);
QFile certfile("D:\\hani\\cert\\localhost.localdomain.pem");
Q_ASSERT(certfile.open(QIODevice::ReadOnly));
QList<QSslCertificate> certList;
QSslCertificate cert(&certfile,QSsl::Pem);
certList.append(cert);
socket->addCaCertificate(cert);
socket->setCaCertificates(certList);
QList<QSslCertificate> serverCert = socket->caCertificates();

我能做什么?

最佳答案

不要,让我重复一遍,不要调用 ignoreSslErrors()。它完全违背了 SSL/TLS 的目的。有非常可以安全调用它的特殊情况,但这(自签名证书)不是特殊情况。

以下准备运行的最小代码显示了如何安全地接受服务器自签名证书。不要快捷方式。

司机:

int main(int argc, char** argv) {
QCoreApplication app(argc, argv);
QTextStream log(stdout);
DummyClient dummy(log);
QObject::connect(&dummy, SIGNAL(done()), &app, SLOT(quit()));
return app.exec();
}

DummyClient 类:

/*
* Show how to safely authenticate a TLS server which uses a self-signed certificate.
* Warning: No error handling to keep the code short.
*/
class DummyClient : public QObject {
Q_OBJECT
public:
DummyClient(QTextStream& log)
: _log(log),
_sock(new QSslSocket(this)) {
connect(_sock, SIGNAL(encrypted()), this, SLOT(onEncrypted()));
connect(_sock, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(onSslErrors(QList<QSslError>)));
connect(_sock, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(onErrors(QAbstractSocket::SocketError)));

// Trust store: which CAs or self-signed certs we are going to trust.
//
// We use setCaCertificates() instead than QSslSocket::addCaCertificates()
// because we don't want to trust the ~200 default CAs.
QList<QSslCertificate> trustedCas = QSslCertificate::fromPath("server-cert.pem");
if (trustedCas.empty()) {
qFatal("Error: no trusted Cas");
}
_sock->setCaCertificates(trustedCas);

bool mutualAuth = false;
if (mutualAuth) {
// Our identity
_sock->setPrivateKey("client-key.pem");
_sock->setLocalCertificate("client-cert.pem");
}

_log << "Connecting" << endl;
// Note: serverName must match the cert CN or alternative name.
Qstring serverName = "myserver.example.org";
_sock->connectToHostEncrypted(serverName, 995);
}

signals:
void done();

private slots:
void onEncrypted() {
_log << "onEncrypted" << endl;

/* Everything is good. Start communicating. */

emit done();
}

void onSslErrors(QList<QSslError> errors) {
QSslError first = errors.takeFirst();
_log << "onSslErrors: " << first.errorString() << endl;

/* Something went wrong in the TLS handshake. Inform the user and quit! */

emit done();
}

void onErrors(QAbstractSocket::SocketError) {
_log << "onErrors: " << _sock->errorString() << endl;
emit done();
}

private:
QTextStream& _log;
QSslSocket* _sock;
};

关于qt - 客户端应用程序如何使用 Qt 中的自签名证书连接到 SSL 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18810788/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com