我需要与需要本地证书(.crt 文件)的服务器交换数据。
我试试这个:
loginRequest = QNetworkRequest(QUrl("https://somesite.com/login"));
QSslConfiguration sslConf = loginRequest.sslConfiguration();
QList<QSslCertificate> certs = QSslCertificate::fromPath(Preferences::certificatePath());
qDebug() << certs.first().issuerInfo(QSslCertificate::Organization); // prints name
sslConf.setLocalCertificate(certs.first());
qDebug() << "is valid " << sslConf.localCertificate().isValid(); // true
qDebug() << "is null " << sslConf.localCertificate().isNull(); // false
qDebug() << "protocol " << sslConf.protocol(); // 0
sslConf.setProtocol(QSsl::SslV3); // i also tried Qssl::AnyProtocol
qDebug() << "protocol " << sslConf.protocol(); // 0
// if i uncomment these i expect everithing to work
//QSslConfiguration::setDefaultConfiguration(sslConf);
//QSslSocket::addDefaultCaCertificate(certs.first());
//loginRequest.setSslConfiguration(sslConf);
QObject::connect(connectionManager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(printSslErrors2(QNetworkReply*,QList<QSslError>)));
m_reply = connectionManager->get(loginRequest);
QObject::connect(m_reply, SIGNAL(readyRead()), this, SLOT(getCookie()));
QObject::connect(m_reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(printSslErrors(QList<QSslError>)));
当此代码执行时,我在 WireShark 中收到以下消息(过滤器:tcp && ssl && ip.addr == my_addr):
Client Hello
ServerHello, Certificate
Server Key Exchange, Certificate request, Server Hello Done
Alert (level: Warning, Description: no certificate), client key exchange, change cipher spec, encrypted handshake message
Alert (level: Fatal, Description: Handshake failure)
这是预期的 - 应用证书的代码被注释掉,但奇怪的是 - 我没有从 QNetworkAccessManager 和 QNetworkReply 中收到任何 ssl 错误(插槽 printSslErrors 和 printSslErrors2)。
如果我取消注释这三行中的任何一行:
//QSslConfiguration::setDefaultConfiguration(sslConf);
//QSslSocket::addDefaultCaCertificate(certs.first());
//loginRequest.setSslConfiguration(sslConf);
我在wireshark 中什么也没有得到(很少有SYN、ACK 和FIN tcp 消息,但没有http 或ssl 流量)。另外,QNetworkAccessManager 和 QNetworkReply 仍然没有错误,所以我不知道出了什么问题。
有没有机会让 Qt 接受我的本地证书,或者可能有一些 3d 方面向 qt 的库来帮助我?
P.S.:顺便说一句 - 几天前,在服务器更改为需要客户端证书之前,ssl 和 https 工作得很好。
P.P.S.:如果有任何区别,证书是自签名的。我还尝试将它(p12 文件)“安装”到系统中,Chrome 和 IE7 都能够使用它并与服务器通信。