File: tlssocket.cpp

package info (click to toggle)
qca2 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,884 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (222 lines) | stat: -rw-r--r-- 6,246 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 Copyright (C) 2007 Justin Karneges <justin@affinix.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "tlssocket.h"

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

class TLSSocket::Private : public QObject
{
    Q_OBJECT
public:
    TLSSocket        *q;
    QTcpSocket       *sock;
    QCA::TLS         *tls;
    QString           host;
    bool              encrypted;
    bool              error, done;
    QByteArray        readbuf, writebuf;
    QCA::Synchronizer sync;
    bool              waiting;

    Private(TLSSocket *_q)
        : QObject(_q)
        , q(_q)
        , sync(_q)
    {
        sock = new QTcpSocket(this);
        connect(sock, &QTcpSocket::connected, this, &TLSSocket::Private::sock_connected);
        connect(sock, &QTcpSocket::readyRead, this, &TLSSocket::Private::sock_readyRead);
        connect(sock, &QTcpSocket::bytesWritten, this, &TLSSocket::Private::sock_bytesWritten);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
        connect(sock, &QTcpSocket::errorOccurred, this, &TLSSocket::Private::sock_error);
#else
        connect(sock,
                QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error),
                this,
                &TLSSocket::Private::sock_error);
#endif

        tls = new QCA::TLS(this);
        connect(tls, &QCA::TLS::handshaken, this, &TLSSocket::Private::tls_handshaken);
        connect(tls, &QCA::TLS::readyRead, this, &TLSSocket::Private::tls_readyRead);
        connect(tls, &QCA::TLS::readyReadOutgoing, this, &TLSSocket::Private::tls_readyReadOutgoing);
        connect(tls, &QCA::TLS::closed, this, &TLSSocket::Private::tls_closed);
        connect(tls, &QCA::TLS::error, this, &TLSSocket::Private::tls_error);
        tls->setTrustedCertificates(QCA::systemStore());
        encrypted = false;
        error     = false;
        waiting   = false;
        done      = false;
    }

    bool waitForReadyRead(int msecs)
    {
        waiting = true;
        bool ok = sync.waitForCondition(msecs);
        // while(1)
        //	QCoreApplication::instance()->processEvents();
        waiting = false;
        if (error || done)
            return false;
        return ok;
    }

private Q_SLOTS:
    void sock_connected()
    {
        // printf("sock connected\n");
        tls->startClient(host);
    }

    void sock_readyRead()
    {
        // printf("sock ready read\n");
        QByteArray buf = sock->readAll();
        // printf("%d bytes\n", buf.size());
        tls->writeIncoming(buf);
    }

    void sock_bytesWritten(qint64 x)
    {
        Q_UNUSED(x);
        // printf("sock bytes written: %d\n", (int)x);
    }

    void sock_error(QAbstractSocket::SocketError x)
    {
        // printf("sock error: %d\n", x);
        Q_UNUSED(x);
        done = true;
        if (waiting)
            sync.conditionMet();
    }

    void tls_handshaken()
    {
        // printf("tls handshaken\n");
        if (tls->peerIdentityResult() != QCA::TLS::Valid) {
            printf("not valid\n");
            sock->abort();
            tls->reset();
            error = true;
        } else {
            // printf("valid\n");
            encrypted = true;
            // printf("%d bytes in writebuf\n", writebuf.size());
            if (!writebuf.isEmpty()) {
                // printf("[%s]\n", writebuf.data());
                tls->write(writebuf);
                writebuf.clear();
            }
        }
        if (waiting)
            sync.conditionMet();
    }

    void tls_readyRead()
    {
        // printf("tls ready read\n");
        if (waiting)
            sync.conditionMet();
    }

    void tls_readyReadOutgoing()
    {
        // printf("tls ready read outgoing\n");
        QByteArray buf = tls->readOutgoing();
        // printf("%d bytes\n", buf.size());
        sock->write(buf);
    }

    void tls_closed()
    {
        // printf("tls closed\n");
    }

    void tls_error()
    {
        // printf("tls error\n");
    }
};

TLSSocket::TLSSocket(QObject *parent)
    : QTcpSocket(parent)
{
    d = new Private(this);
}

TLSSocket::~TLSSocket()
{
    delete d;
}

void TLSSocket::connectToHostEncrypted(const QString &host, quint16 port)
{
    d->host = host;
    setOpenMode(QIODevice::ReadWrite);
    d->sock->connectToHost(host, port);
}

QCA::TLS *TLSSocket::tls()
{
    return d->tls;
}

bool TLSSocket::waitForReadyRead(int msecs)
{
    /*if(d->readbuf.isEmpty())
        return false;

    if(d->tls->bytesAvailable() == 0)
        return false;*/

    return d->waitForReadyRead(msecs);
}

qint64 TLSSocket::readData(char *data, qint64 maxlen)
{
    if (!d->error)
        d->readbuf += d->tls->read();
    unsigned char *p        = (unsigned char *)d->readbuf.data();
    int            size     = d->readbuf.size();
    int            readsize = qMin(size, (int)maxlen);
    int            newsize  = size - readsize;
    memcpy(data, p, readsize);
    memmove(p, p + readsize, newsize);
    d->readbuf.resize(newsize);
    return readsize;
}

qint64 TLSSocket::writeData(const char *data, qint64 len)
{
    // printf("write %d bytes\n", (int)len);
    QByteArray buf(data, len);
    if (d->encrypted)
        d->tls->write(buf);
    else
        d->writebuf += buf;
    return len;
}

#include "tlssocket.moc"