免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6391 | 回复: 0
打印 上一主题 下一主题

Qt中的SSL通信 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-13 03:03 |只看该作者 |倒序浏览
http://xizhizhu.blogspot.com/200 ... -communication.html

SSL is widely used nowadays to provide secure communication, whichperforms as a new layer between TCP and application. In Qt, theQSslSocket class provides an SSL encrypted socket for both servers andclients.

1.Client
The following steps are most commonly used:
a) call QSslSocket.setProtocol() and other functions to set the parameters of SSL;
b) call QSslSocket.connectToHostEncrypted() to connect to a server;
c) the QSslSocket.encrypted() signal is emitted when the connection and handshake are done;
d) call QSslSocket.peerCertificate() to get the certificate of the server and decide whether to accept it;
e)once secure connection established, the way to read and write afterconnection of QSslSocket performs exactly like that of QTcpSocket.

Notice: whenever an error occurs, signal QSslSocket.sslErrors() will be emitted. If the errors are not ignored (by calling QSslSocket.ignoreSslErrors()), the connection fails.

The following example shows how to do the above steps in real code.

// ssl-client.h
#include <QObject>
#include <QSslSocket>
#include <QString>
class
SSLClient:
public
QObject
{
  Q_OBJECT
public:
SSLClient(QObject* parent =
0);
void start(QString hostName, quint16 port);
public slots:
// handle the signal of QSslSocket.encrypted()
void connectionEstablished();
// handle the signal of QSslSocket.sslErrors()
void errorOccured(const
QList<QSslError>
&error);
private:
QSslSocket client;
};
// ssl-client.cc
#include "ssl-client.h"
#include <QByteArray>
#include <QList>
#include <QSslCertificate>
#include <QString>
SSLClient::SSLClient(QObject* parent):
QObject(parent)
{
  connect(&client, SIGNAL(encrypted()),
this, SLOT(connectionEstablished()));
  connect(&client, SIGNAL(sslErrors(const
QList<QSslError>
&)),
this, SLOT(errorOccured(const
QList<QSslError>
&)));
}
void
SSLClient::errorOccured(const
QList<QSslError>
& error)
{
// simply ignore the errors
// it should be very careful when ignoring errors
  client.ignoreSslErrors();
}
void
SSLClient::connectionEstablished()
{
// get the peer's certificate
QSslCertificate cert = client.peerCertificate();
// write on the SSL connection
  client.write("Hello, world",
13);
}
void
SSLClient::start(QString hostName, quint16 port)
{
  client.connectToHostEncrypted(hostName, port);
}
// main.cc
#include "ssl-client.h"
#include <qapplication>
int main(int argc,
char** argv)
{
QApplication app(argc, argv);
SSLClient client;
  client.start("127.0.0.1",
8888);
return app.exec();
}

2.Server
The following steps are usually used:
a) call QSslSocket.setLocalCertificate() to set the certificate;
b) override QTcpServer.incomingConnection() doing:
call QSslSocket.setSocketDescriptor() to bind SSL to the newly incoming connection;
call QSslSocket.startServerEncryption() to initialize the SSL handshake;
c) the QSslSocket.encrypted() signal is emitted when the connection and handshake are done;
d)once secure connection established, the way to read and write afterconnection of QSslSocket performs exactly like that of QTcpSocket.

Sorry, I'm too lazy to write the sample code for the server :P

P.S. You should add the OpenSSL support when compiling Qt, using ./configure -openssl, and the OpenSSL development package (libcurl3-openssl-dev or libcurl4-openssl-dev in Ubuntu) should be installed yourself.

[ 本帖最后由 zxz1984 于 2009-1-13 03:04 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP