我需要与需要本地证书(.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(插槽 printSslErrors 和 printSslErrors2)中得到任何 ssl 错误。
如果我取消注释这 3 行中的任何一行:
//QSslConfiguration::setDefaultConfiguration(sslConf);
//QSslSocket::addDefaultCaCertificate(certs.first());
//loginRequest.setSslConfiguration(sslConf);
我在 wireshark 中什么都没有(SYN、ACK 和 FIN tcp 消息很少,但没有 http 或 ssl 流量)。QNetworkAccessManager 和 QNetworkReply 仍然没有错误,所以我不知道出了什么问题。
有没有机会让 Qt 接受我的本地证书,或者可能有一些面向 3d 方的 qt 库来帮助我?
PS:顺便说一句 - ssl 和 https 几天前工作得很好,在服务器被更改为需要客户端证书之前。
PPS:如果有任何区别,证书是自签名的。我还尝试将它(p12 文件)“安装”到系统中,Chrome 和 IE7 都可以使用它并与服务器通信。