QCA

tlssocket.cpp
1/*
2 Copyright (C) 2007 Justin Karneges <justin@affinix.com>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*/
21
22#include "tlssocket.h"
23
24#ifdef QT_STATICPLUGIN
25#include "import_plugins.h"
26#endif
27
28class TLSSocket::Private : public QObject
29{
31public:
32 TLSSocket *q;
33 QTcpSocket *sock;
34 QCA::TLS *tls;
35 QString host;
36 bool encrypted;
37 bool error, done;
38 QByteArray readbuf, writebuf;
40 bool waiting;
41
42 Private(TLSSocket *_q)
43 : QObject(_q)
44 , q(_q)
45 , sync(_q)
46 {
47 sock = new QTcpSocket(this);
48 connect(sock, &QTcpSocket::connected, this, &TLSSocket::Private::sock_connected);
49 connect(sock, &QTcpSocket::readyRead, this, &TLSSocket::Private::sock_readyRead);
50 connect(sock, &QTcpSocket::bytesWritten, this, &TLSSocket::Private::sock_bytesWritten);
51#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
52 connect(sock, &QTcpSocket::errorOccurred, this, &TLSSocket::Private::sock_error);
53#else
54 connect(sock,
55 QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error),
56 this,
57 &TLSSocket::Private::sock_error);
58#endif
59
60 tls = new QCA::TLS(this);
61 connect(tls, &QCA::TLS::handshaken, this, &TLSSocket::Private::tls_handshaken);
62 connect(tls, &QCA::TLS::readyRead, this, &TLSSocket::Private::tls_readyRead);
63 connect(tls, &QCA::TLS::readyReadOutgoing, this, &TLSSocket::Private::tls_readyReadOutgoing);
64 connect(tls, &QCA::TLS::closed, this, &TLSSocket::Private::tls_closed);
65 connect(tls, &QCA::TLS::error, this, &TLSSocket::Private::tls_error);
67 encrypted = false;
68 error = false;
69 waiting = false;
70 done = false;
71 }
72
73 bool waitForReadyRead(int msecs)
74 {
75 waiting = true;
76 bool ok = sync.waitForCondition(msecs);
77 // while(1)
78 // QCoreApplication::instance()->processEvents();
79 waiting = false;
80 if (error || done)
81 return false;
82 return ok;
83 }
84
85private Q_SLOTS:
86 void sock_connected()
87 {
88 // printf("sock connected\n");
89 tls->startClient(host);
90 }
91
92 void sock_readyRead()
93 {
94 // printf("sock ready read\n");
95 QByteArray buf = sock->readAll();
96 // printf("%d bytes\n", buf.size());
97 tls->writeIncoming(buf);
98 }
99
100 void sock_bytesWritten(qint64 x)
101 {
102 Q_UNUSED(x);
103 // printf("sock bytes written: %d\n", (int)x);
104 }
105
106 void sock_error(QAbstractSocket::SocketError x)
107 {
108 // printf("sock error: %d\n", x);
109 Q_UNUSED(x);
110 done = true;
111 if (waiting)
112 sync.conditionMet();
113 }
114
115 void tls_handshaken()
116 {
117 // printf("tls handshaken\n");
118 if (tls->peerIdentityResult() != QCA::TLS::Valid) {
119 printf("not valid\n");
120 sock->abort();
121 tls->reset();
122 error = true;
123 } else {
124 // printf("valid\n");
125 encrypted = true;
126 // printf("%d bytes in writebuf\n", writebuf.size());
127 if (!writebuf.isEmpty()) {
128 // printf("[%s]\n", writebuf.data());
129 tls->write(writebuf);
130 writebuf.clear();
131 }
132 }
133 if (waiting)
134 sync.conditionMet();
135 }
136
137 void tls_readyRead()
138 {
139 // printf("tls ready read\n");
140 if (waiting)
141 sync.conditionMet();
142 }
143
144 void tls_readyReadOutgoing()
145 {
146 // printf("tls ready read outgoing\n");
147 QByteArray buf = tls->readOutgoing();
148 // printf("%d bytes\n", buf.size());
149 sock->write(buf);
150 }
151
152 void tls_closed()
153 {
154 // printf("tls closed\n");
155 }
156
157 void tls_error()
158 {
159 // printf("tls error\n");
160 }
161};
162
163TLSSocket::TLSSocket(QObject *parent)
164 : QTcpSocket(parent)
165{
166 d = new Private(this);
167}
168
169TLSSocket::~TLSSocket()
170{
171 delete d;
172}
173
174void TLSSocket::connectToHostEncrypted(const QString &host, quint16 port)
175{
176 d->host = host;
178 d->sock->connectToHost(host, port);
179}
180
181QCA::TLS *TLSSocket::tls()
182{
183 return d->tls;
184}
185
186bool TLSSocket::waitForReadyRead(int msecs)
187{
188 /*if(d->readbuf.isEmpty())
189 return false;
190
191 if(d->tls->bytesAvailable() == 0)
192 return false;*/
193
194 return d->waitForReadyRead(msecs);
195}
196
197qint64 TLSSocket::readData(char *data, qint64 maxlen)
198{
199 if (!d->error)
200 d->readbuf += d->tls->read();
201 unsigned char *p = (unsigned char *)d->readbuf.data();
202 int size = d->readbuf.size();
203 int readsize = qMin(size, (int)maxlen);
204 int newsize = size - readsize;
205 memcpy(data, p, readsize);
206 memmove(p, p + readsize, newsize);
207 d->readbuf.resize(newsize);
208 return readsize;
209}
210
211qint64 TLSSocket::writeData(const char *data, qint64 len)
212{
213 // printf("write %d bytes\n", (int)len);
214 QByteArray buf(data, len);
215 if (d->encrypted)
216 d->tls->write(buf);
217 else
218 d->writebuf += buf;
219 return len;
220}
221
222#include "tlssocket.moc"
void error()
This signal is emitted when an error is detected.
void readyReadOutgoing()
This signal is emitted when SecureLayer has encrypted (network side) data ready to be read.
void closed()
This signal is emitted when the SecureLayer connection is closed.
void readyRead()
This signal is emitted when SecureLayer has decrypted (application side) data ready to be read.
Enable synchronization between two threads.
bool waitForCondition(int msecs=-1)
Call to pause execution in this thread.
void conditionMet()
Call to continue execution in the paused thread.
Transport Layer Security / Secure Socket Layer.
void write(const QByteArray &a) override
This method writes unencrypted (plain) data to the SecureLayer implementation.
void startClient(const QString &host=QString())
Start the TLS/SSL connection as a client.
void writeIncoming(const QByteArray &a) override
This method accepts encoded (typically encrypted) data for processing.
void setTrustedCertificates(const CertificateCollection &trusted)
Set up the set of trusted certificates that will be used to verify that the certificate provided is v...
void reset()
Reset the connection.
IdentityResult peerIdentityResult() const
After the SSL/TLS handshake is complete, this method allows you to determine if the other end of the ...
QByteArray readOutgoing(int *plainBytes=nullptr) override
This method provides encoded (typically encrypted) data.
@ Valid
identity is verified
QByteArray read() override
This method reads decrypted (plain) data from the SecureLayer implementation.
void handshaken()
Emitted when the protocol handshake is complete.
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QCA_EXPORT CertificateCollection systemStore()
Get system-wide root Certificate Authority (CA) certificates.
void connectToHost(const QHostAddress &address, quint16 port, OpenMode openMode)
SocketError error() const const
void errorOccurred(QAbstractSocket::SocketError socketError)
void clear()
char * data()
bool isEmpty() const const
void resize(qsizetype newSize, char c)
qsizetype size() const const
void bytesWritten(qint64 bytes)
QByteArray readAll()
void readyRead()
void setOpenMode(QIODeviceBase::OpenMode openMode)
virtual qint64 size() const const
qint64 write(const QByteArray &data)
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QTcpSocket(QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:53:51 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.