Complete conversion of TLS/SSL server example to

Qt4.

svn path=/trunk/kdesupport/qca/; revision=520483
This commit is contained in:
Brad Hards 2006-03-20 02:54:03 +00:00
parent cbbf56cd69
commit 709398ef17
2 changed files with 55 additions and 44 deletions

View File

@ -22,7 +22,6 @@
#include <QtCore> #include <QtCore>
#include <QtNetwork> #include <QtNetwork>
#include<q3serversocket.h>
#include <QtCrypto> #include <QtCrypto>
char pemdata_cert[] = char pemdata_cert[] =
@ -116,7 +115,7 @@ public:
encoded -= i.encoded; encoded -= i.encoded;
plain += i.plain; plain += i.plain;
it = list.remove(it); it = list.erase(it);
} }
return plain; return plain;
} }
@ -125,20 +124,17 @@ public:
QList<Item> list; QList<Item> list;
}; };
class SecureServerTest : public Q3ServerSocket class SecureServer : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
enum { Idle, Handshaking, Active, Closing }; enum { Idle, Handshaking, Active, Closing };
SecureServerTest(int _port) : Q3ServerSocket(_port), port(_port) SecureServer(quint16 _port) : port(_port)
{ {
sock = new QTcpSocket; server = new QTcpServer;
connect(sock, SIGNAL(readyRead()), SLOT(sock_readyRead())); connect( server, SIGNAL(newConnection()), SLOT(server_handleConnection()) );
connect(sock, SIGNAL(connectionClosed()), SLOT(sock_connectionClosed()));
connect(sock, SIGNAL(error(QAbstractSocket::SocketError)),
SLOT(sock_error(QAbstractSocket::SocketError)));
connect(sock, SIGNAL(bytesWritten(qint64)), SLOT(sock_bytesWritten(qint64)));
ssl = new QCA::TLS; ssl = new QCA::TLS;
connect(ssl, SIGNAL(handshaken()), SLOT(ssl_handshaken())); connect(ssl, SIGNAL(handshaken()), SLOT(ssl_handshaken()));
@ -154,10 +150,10 @@ public:
mode = Idle; mode = Idle;
} }
~SecureServerTest() ~SecureServer()
{ {
delete ssl; delete ssl;
delete sock; delete server;
} }
void start() void start()
@ -172,7 +168,7 @@ public:
QTimer::singleShot(0, this, SIGNAL(quit())); QTimer::singleShot(0, this, SIGNAL(quit()));
return; return;
} }
if(!ok()) { if(false == server->listen(QHostAddress::Any, port)) {
printf("Error binding to port %d!\n", port); printf("Error binding to port %d!\n", port);
QTimer::singleShot(0, this, SIGNAL(quit())); QTimer::singleShot(0, this, SIGNAL(quit()));
return; return;
@ -180,35 +176,48 @@ public:
printf("Listening on port %d ...\n", port); printf("Listening on port %d ...\n", port);
} }
void newConnection(int s)
{
// Note: only 1 connection supported at a time in this example!
if(sock->isOpen()) {
QTcpSocket tmp;
tmp.setSocket(s);
printf("throwing away extra connection\n");
return;
}
mode = Handshaking;
sock->setSocket(s);
printf("Connection received! Starting TLS handshake...\n");
ssl->setCertificate(cert, privkey);
ssl->startServer();
}
signals: signals:
void quit(); void quit();
private slots: private slots:
void sock_readyRead() void sock_readyRead()
{ {
QByteArray buf(sock->bytesAvailable()); QByteArray buf(sock->bytesAvailable(), 0x00);
int num = sock->readBlock(buf.data(), buf.size());
if(num < (int)buf.size()) int num = sock->read(buf.data(), buf.size());
if ( -1 == num )
qDebug() << "Error reading data from socket";
if (num < buf.size() )
buf.resize(num); buf.resize(num);
ssl->writeIncoming(buf); ssl->writeIncoming(buf);
} }
void server_handleConnection()
{
// Note: only 1 connection supported at a time in this example!
if(mode != Idle) {
QTcpSocket* tmp = server->nextPendingConnection();
tmp->close();
connect(tmp, SIGNAL(disconnected()), tmp, SLOT(deleteLater()));
printf("throwing away extra connection\n");
return;
}
mode = Handshaking;
sock = server->nextPendingConnection();
connect(sock, SIGNAL(readyRead()), SLOT(sock_readyRead()));
connect(sock, SIGNAL(connectionClosed()), SLOT(sock_connectionClosed()));
connect(sock, SIGNAL(error(QAbstractSocket::SocketError)),
SLOT(sock_error(QAbstractSocket::SocketError)));
connect(sock, SIGNAL(bytesWritten(qint64)), SLOT(sock_bytesWritten(qint64)));
printf("Connection received! Starting TLS handshake...\n");
ssl->setCertificate(cert, privkey);
ssl->startServer();
}
void sock_connectionClosed() void sock_connectionClosed()
{ {
printf("Connection closed.\n"); printf("Connection closed.\n");
@ -259,15 +268,16 @@ private slots:
void ssl_readyReadOutgoing() void ssl_readyReadOutgoing()
{ {
QByteArray a = ssl->readOutgoing(); QByteArray outgoingData = ssl->readOutgoing();
layer.specifyEncoded(a.size(), ssl->bytesOutgoingAvailable()); layer.specifyEncoded( outgoingData.size(), ssl->bytesOutgoingAvailable());
sock->writeBlock(a.data(), a.size()); sock->write( outgoingData );
} }
void ssl_closed() void ssl_closed()
{ {
printf("Closing.\n"); printf("Closing.\n");
sock->close(); sock->close();
mode = Idle;
} }
void ssl_error() void ssl_error()
@ -280,10 +290,12 @@ private slots:
printf("SSL Error! Closing.\n"); printf("SSL Error! Closing.\n");
sock->close(); sock->close();
} }
mode = Idle;
} }
private: private:
int port; quint16 port;
QTcpServer *server;
QTcpSocket *sock; QTcpSocket *sock;
QCA::TLS *ssl; QCA::TLS *ssl;
QCA::Certificate cert; QCA::Certificate cert;
@ -309,11 +321,11 @@ int main(int argc, char **argv)
return 1; return 1;
} }
SecureServerTest *s = new SecureServerTest(port); SecureServer *server = new SecureServer(port);
QObject::connect(s, SIGNAL(quit()), &app, SLOT(quit())); QObject::connect(server, SIGNAL(quit()), &app, SLOT(quit()));
s->start(); server->start();
app.exec(); app.exec();
delete s; delete server;
return 0; return 0;
} }

View File

@ -1,7 +1,6 @@
CONFIG -= app_bundle CONFIG -= app_bundle
CONFIG += thread console CONFIG += console
QT += network QT += network
QT += qt3support
SOURCES += sslservtest.cpp SOURCES += sslservtest.cpp
include(../examples.pri) include(../examples.pri)