Qt QSslSocket "证书是自签名的,不可信的"
Qt QSslSocket "The certificate is self-signed, and untrusted"
我想连接服务器与QSslSocket和服务器上我得到soketSslError "证书是自签名的,不受信任的",但我不明白为什么我有这个错误。
第一步是使用openssl为服务器和客户端生成文件
$openssl req -new -newkey rsa:1024 -keyout ca.key -x509 -days 500 -out ca.crt
$openssl req -new -newkey rsa:1024 -keyout client01.key -out client01.csr
$openssl ca -config ca.config -in client01.csr -out client01.crt -batch
c++ server/client
在服务器:启动服务器
if (listen(QHostAddress::Any,this->connectingPort)) {
std::cout<<"Server start on port: "<<this->connectingPort<<std::endl;
return true;
} else {
std::cout<<"Cant start server. "<<errorString().toStdString().c_str()<<std::endl;
return false;
}
incomingConnection
QFile keyFile("ca.key");
if (!keyFile.open(QIODevice::ReadOnly)) {
delete this->sslSocket;
qDebug()<<"Cant open file: "<<keyFile.fileName();
return false;
}
QByteArray pasp ="qwerty";
QSslKey key(keyFile.readAll(),QSsl::Rsa,QSsl::Pem,QSsl::PrivateKey,pasp);
if (key.isNull()) {
delete this->sslSocket;
qDebug()<<"key in file "<<keyFile.fileName()<<" is empty";
return false;
}
keyFile.close();
this->sslSocket->setPrivateKey(key);
this->sslSocket->setPeerVerifyMode(QSslSocket::VerifyPeer);
this->sslSocket->setLocalCertificate("ca.crt");
this->sslSocket->startServerEncryption();
客户端:
this->sslSocket->setPeerVerifyMode(QSslSocket::VerifyPeer);
QFile keyFile("client01.key");
if (!keyFile.open(QIODevice::ReadOnly)) {
delete this->sslSocket;
qDebug()<<"Cant open file: "<<keyFile.fileName();
return ;
}
QByteArray pasp ="qwerty";
QSslKey key(keyFile.readAll(),QSsl::Rsa,QSsl::Pem,QSsl::PrivateKey,pasp);
if (key.isNull()) {
delete this->sslSocket;
qDebug()<<"key in file "<<keyFile.fileName()<<" is empty";
return ;
}
keyFile.close();
this->sslSocket->setPrivateKey(key);
this->sslSocket->setLocalCertificate("client01.crt");
this->sslSocket->connectToHostEncrypted("192.168.0.10",1258);
if (!this->sslSocket->waitForEncrypted()) {
qDebug()<<"error: "<<sslSocket->errorString();
}
,当我从客户端连接时,我得到服务器错误
soket ssl error
"The certificate is self-signed, and untrusted"
"The certificate is self-signed, and untrusted"
socketError: QAbstractSocket::SocketError( 13 )
你知道我做错了什么吗?
更新:
Qt Creator 3.0.1基于Qt 5.2.1 (GCC 4.8.2, 64位)
我建议你在服务器上试试:
QList<QSslCertificate> cert = QSslCertificate::fromPath(QLatin1String("your-certificate.pem"));
QSslError error(QSslError::SelfSignedCertificate, cert.at(0));
QList<QSslError> expectedSslErrors;
expectedSslErrors.append(error);
this->sslSocket.ignoreSslErrors(expectedSslErrors);
问题已解决
我做了什么:更新版本Qt为5.5并生成新的SSL证书:
openssl req -x509 -newkey rsa:1024 -keyout key.key -out key.pem -days 365 -nodes
在服务器:sslServer.setSslLocalCertificate("key.pem");
sslServer.setSslPrivateKey("key.key");
sslServer.setSslProtocol(QSsl::TlsV1_2);
在客户:sslSocket.addCaCertificates("key.pem");
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 我的神经网络不起作用 [XOR 问题]
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- QSqlquery prepare()和bindvalue()不工作
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 检查输入是否不是整数或数字
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 使用不带参数的函数访问结构元素
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何输出a'&quot在不使用线端的情况下进行文件
- 如何检测我何时向可变参数函数传递"std::string"而不是"c_str()"
- 基本的c程序不是在Ubuntu中编译的可信的
- Qt QSslSocket "证书是自签名的,不可信的"