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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
#include<tqapplication.h>
#include<tqfile.h>
#include<tqsocket.h>
#include<tqserversocket.h>
#include<tqvaluelist.h>
#include<tqtimer.h>
#include"qca.h"
char pemdata_cert[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIDbjCCAtegAwIBAgIBADANBgkqhkiG9w0BAQQFADCBhzELMAkGA1UEBhMCVVMx\n"
"EzARBgNVBAgTCkNhbGlmb3JuaWExDzANBgNVBAcTBklydmluZTEYMBYGA1UEChMP\n"
"RXhhbXBsZSBDb21wYW55MRQwEgYDVQQDEwtleGFtcGxlLmNvbTEiMCAGCSqGSIb3\n"
"DQEJARYTZXhhbXBsZUBleGFtcGxlLmNvbTAeFw0wMzA3MjQwNzMwMDBaFw0wMzA4\n"
"MjMwNzMwMDBaMIGHMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5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+smIy3AbaKAwWWZToesxebu3m9VeA8dqWyOaUMjox\n"
"AcgVYesgVaMpjRe7fcWdJnX1wJoVVPuIcO8ma+AAPByfTORbzpSTmXAQAwIDAQAB\n"
"AoGAP83u+aYghuIcaWhmM03MLf69z/WztKYSi/fu0BcS977w67bL3MC9CVPoPRB/\n"
"0nLSt/jZIuRzHKUCYfXLerSU7v0oXDTy6GPzWMh/oXIrpF0tYNbwWF7LSq2O2gGZ\n"
"XtA9MSmUNNJaKzQQeXjqdVFOY8A0Pho+k2KByBiCi+ChkcECQQDRUuyX0+PKJtA2\n"
"M36BOTFpy61BAv+JRlXUnHuevOfQWl6NR6YGygqCyH1sWtP1sa9S4wWys3DFH+5A\n"
"DkuAqk7zAkEAzf4eUH2hp5CIMsXH+WpIzKj09oY1it2CAKjVq4rUELf8iXvmGoFl\n"
"000spua4MjHNUYm7LR0QaKesKrMyGZUesQJAL8aLdYPJI+SD9Tr/jqLtIkZ4frQe\n"
"eshw4pvsoyheiHF3zyshO791crAr4EVCx3sMlxB1xnmqLXPCPyCEHxO//QJBAIBY\n"
"IYkjDZJ6ofGIe1UyXJNvfdkPu9J+ut4wU5jjEcgs6mK62J6RGuFxhy2iOQfFMdjo\n"
"yL+OCUg7mDCun7uCxrECQAtSvnLOFMjO5qExRjFtwi+b1rcSekd3Osk/izyRFSzg\n"
"Or+AL56/EKfiogNnFipgaXIbb/xj785Cob6v96XoW1I=\n"
"-----END RSA PRIVATE KEY-----\n";
class LayerTracker
{
public:
struct Item
{
int plain;
int encoded;
};
LayerTracker()
{
p = 0;
}
void reset()
{
p = 0;
list.clear();
}
void addPlain(int plain)
{
p += plain;
}
void specifyEncoded(int encoded, int plain)
{
// can't specify more bytes than we have
if(plain > p)
plain = p;
p -= plain;
Item i;
i.plain = plain;
i.encoded = encoded;
list += i;
}
int finished(int encoded)
{
int plain = 0;
for(TQValueList<Item>::Iterator it = list.begin(); it != list.end();) {
Item &i = *it;
// not enough?
if(encoded < i.encoded) {
i.encoded -= encoded;
break;
}
encoded -= i.encoded;
plain += i.plain;
it = list.remove(it);
}
return plain;
}
int p;
TQValueList<Item> list;
};
class SecureServerTest : public QServerSocket
{
Q_OBJECT
public:
enum { Idle, Handshaking, Active, Closing };
SecureServerTest(int _port) : QServerSocket(_port), port(_port)
{
sock = new TQSocket;
connect(sock, SIGNAL(readyRead()), SLOT(sock_readyRead()));
connect(sock, SIGNAL(connectionClosed()), SLOT(sock_connectionClosed()));
connect(sock, SIGNAL(error(int)), SLOT(sock_error(int)));
connect(sock, SIGNAL(bytesWritten(int)), SLOT(sock_bytesWritten(int)));
ssl = new QCA::TLS;
connect(ssl, SIGNAL(handshaken()), SLOT(ssl_handshaken()));
connect(ssl, SIGNAL(readyRead()), SLOT(ssl_readyRead()));
connect(ssl, SIGNAL(readyReadOutgoing(int)), SLOT(ssl_readyReadOutgoing(int)));
connect(ssl, SIGNAL(closed()), SLOT(ssl_closed()));
connect(ssl, SIGNAL(error(int)), SLOT(ssl_error(int)));
cert.fromPEM(pemdata_cert);
privkey.fromPEM(pemdata_privkey);
mode = Idle;
}
~SecureServerTest()
{
delete ssl;
delete sock;
}
void start()
{
if(cert.isNull() || privkey.isNull()) {
printf("Error loading cert and/or private key!\n");
TQTimer::singleShot(0, this, SIGNAL(quit()));
return;
}
if(!ok()) {
printf("Error binding to port %d!\n", port);
TQTimer::singleShot(0, this, SIGNAL(quit()));
return;
}
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()) {
TQSocket 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:
void quit();
private slots:
void sock_readyRead()
{
TQByteArray buf(sock->bytesAvailable());
int num = sock->readBlock(buf.data(), buf.size());
if(num < (int)buf.size())
buf.resize(num);
ssl->writeIncoming(buf);
}
void sock_connectionClosed()
{
printf("Connection closed.\n");
}
void sock_bytesWritten(int x)
{
if(mode == Active && sent) {
int bytes = layer.finished(x);
bytesLeft -= bytes;
if(bytesLeft == 0) {
mode = Closing;
printf("SSL shutdown\n");
ssl->close();
}
}
}
void sock_error(int)
{
printf("Socket error.\n");
}
void ssl_handshaken()
{
printf("Successful SSL handshake. Waiting for newline.\n");
layer.reset();
bytesLeft = 0;
sent = false;
mode = Active;
}
void ssl_readyRead()
{
TQByteArray a = ssl->read();
TQString str =
"<html>\n"
"<head><title>Test</title></head>\n"
"<body>this is only a test</body>\n"
"</html>\n";
TQCString cs = str.latin1();
TQByteArray b(cs.length());
memcpy(b.data(), cs.data(), b.size());
printf("Sending test response...\n");
sent = true;
layer.addPlain(b.size());
ssl->write(b);
}
void ssl_readyReadOutgoing(int plainBytes)
{
TQByteArray a = ssl->readOutgoing();
layer.specifyEncoded(a.size(), plainBytes);
sock->writeBlock(a.data(), a.size());
}
void ssl_closed()
{
printf("Closing.\n");
sock->close();
}
void ssl_error(int x)
{
if(x == QCA::TLS::ErrHandshake) {
printf("SSL Handshake Error! Closing.\n");
sock->close();
}
else {
printf("SSL Error! Closing.\n");
sock->close();
}
}
private:
int port;
TQSocket *sock;
QCA::TLS *ssl;
QCA::Cert cert;
QCA::RSAKey privkey;
bool sent;
int mode;
int bytesLeft;
LayerTracker layer;
};
#include"sslservtest.moc"
int main(int argc, char **argv)
{
TQApplication app(argc, argv, false);
int port = argc > 1 ? TQString(argv[1]).toInt() : 8000;
if(!QCA::isSupported(QCA::CAP_TLS)) {
printf("TLS not supported!\n");
return 1;
}
SecureServerTest *s = new SecureServerTest(port);
TQObject::connect(s, SIGNAL(quit()), &app, SLOT(quit()));
s->start();
app.exec();
delete s;
return 0;
}
|