QNetworkRequest with ssl local certificate

好久不见. 提交于 2019-12-04 15:46:34

Complete shot in the dark and goes on the assumption that Qt may in fact be reporting an error but you're not getting the signal.

You're connecting signals from your connectionManager to this have you included the Q_OBJECT macro in the header for this?

Also examine the output when you run your application as Qt may report issues connecting the signals/slots if that is indeed the case here.

DarkWalker

SOLUTION, Part I: I mostly solved this (the lack of connection), there were 2 reason:

1st - the apache server actually require private key (for some unknown reason, found it [here][1]), how to add private key:

QFile x(Preferences::certificateKeyPath()); 
x.open(QIODevice::ReadOnly);
pKey = QSslKey(x.readAll(),QSsl::Rsa);
QSslError error1(QSslError::SelfSignedCertificate, certs.first());
QSslError error2(QSslError::CertificateUntrusted, certs.first());
QList<QSslError> expectedSslErrors;
expectedSslErrors.append(error1);
expectedSslErrors.append(error2);

2d - the certificate I had was not very 'good'. I dont know what it really means or why it was not working, but when I got new certificate from server admin and added private key the handshake succeeded.

I still dont know how to catch sslErrors (for example to show user that his certificate is not working), but it is a good start

SOLUTION, Part II:

Solved the last part of the question (kina a woraround). It seems QNetworkReply not emitting SslErrors is a bug (or at least it does not work all the time or for all web-sites), found it [in Qt bug tracker][2]. And the workaround also from there: sinse we cant get SslErrors, we have to try and get smth else - [error][3], for example. It does not give detailed information about what have actually happend, but better than nothing. For me error code 6 - "the SSL/TLS handshake failed and the encrypted channel could not be established. The sslErrors() signal should have been emitted." is perfect (i dont care for anything else):

 QObject::connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(handleSslErrors(QNetworkReply::NetworkError)));

The important part: if the user has wrong certificate and/or key - the signal is emited. But it is also emited if certificate and key are correct. It seems auth might still be not perfect, but you can easily shut it down with

QObject::connect(m_reply, SIGNAL(sslErrors(QList<QSslError>)), 
                  this, SLOT(printSslErrors(QList<QSslError>)));

Conclusion it seems they have fixed a lot of SSL bugs in Qt 4.8, so I hope release will be soon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!