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
|
#include<tqapplication.h>
#include<tqdom.h>
#include<tqfile.h>
#include<tqsocket.h>
#include<tqptrlist.h>
#include"base64.h"
#include"qca.h"
TQCA::Cert readCertXml(const TQDomElement &e)
{
TQCA::Cert cert;
// there should be one child data tag
TQDomElement data = e.elementsByTagName("data").item(0).toElement();
if(!data.isNull())
cert.fromDER(Base64::stringToArray(data.text()));
return cert;
}
void showCertInfo(const TQCA::Cert &cert)
{
printf("-- Cert --\n");
printf(" CN: %s\n", cert.subject()["CN"].latin1());
printf(" Valid from: %s, until %s\n",
cert.notBefore().toString().latin1(),
cert.notAfter().toString().latin1());
printf(" PEM:\n%s\n", cert.toPEM().latin1());
}
TQPtrList<TQCA::Cert> getRootCerts(const TQString &store)
{
TQPtrList<TQCA::Cert> list;
// open the Psi rootcerts file
TQFile f(store);
if(!f.open(IO_ReadOnly)) {
printf("unable to open %s\n", f.name().latin1());
return list;
}
TQDomDocument doc;
doc.setContent(&f);
f.close();
TQDomElement base = doc.documentElement();
if(base.tagName() != "store") {
printf("wrong format of %s\n", f.name().latin1());
return list;
}
TQDomNodeList cl = base.elementsByTagName("certificate");
if(cl.count() == 0) {
printf("no certs found in %s\n", f.name().latin1());
return list;
}
int num = 0;
for(int n = 0; n < (int)cl.count(); ++n) {
TQCA::Cert *cert = new TQCA::Cert(readCertXml(cl.item(n).toElement()));
if(cert->isNull()) {
printf("error reading cert\n");
delete cert;
continue;
}
++num;
list.append(cert);
}
printf("imported %d root certs\n", num);
return list;
}
TQString resultToString(int result)
{
TQString s;
switch(result) {
case TQCA::TLS::NoCert:
s = TQObject::tr("No certificate presented.");
break;
case TQCA::TLS::Valid:
break;
case TQCA::TLS::HostMismatch:
s = TQObject::tr("Hostname mismatch.");
break;
case TQCA::TLS::Rejected:
s = TQObject::tr("Root CA rejects the specified purpose.");
break;
case TQCA::TLS::Untrusted:
s = TQObject::tr("Not trusted for the specified purpose.");
break;
case TQCA::TLS::SignatureFailed:
s = TQObject::tr("Invalid signature.");
break;
case TQCA::TLS::InvalidCA:
s = TQObject::tr("Invalid CA certificate.");
break;
case TQCA::TLS::InvalidPurpose:
s = TQObject::tr("Invalid certificate purpose.");
break;
case TQCA::TLS::SelfSigned:
s = TQObject::tr("Certificate is self-signed.");
break;
case TQCA::TLS::Revoked:
s = TQObject::tr("Certificate has been revoked.");
break;
case TQCA::TLS::PathLengthExceeded:
s = TQObject::tr("Maximum cert chain length exceeded.");
break;
case TQCA::TLS::Expired:
s = TQObject::tr("Certificate has expired.");
break;
case TQCA::TLS::Unknown:
default:
s = TQObject::tr("General validation error.");
break;
}
return s;
}
class SecureTest : public TQObject
{
TQ_OBJECT
public:
SecureTest()
{
sock = new TQSocket;
connect(sock, TQ_SIGNAL(connected()), TQ_SLOT(sock_connected()));
connect(sock, TQ_SIGNAL(readyRead()), TQ_SLOT(sock_readyRead()));
connect(sock, TQ_SIGNAL(connectionClosed()), TQ_SLOT(sock_connectionClosed()));
connect(sock, TQ_SIGNAL(error(int)), TQ_SLOT(sock_error(int)));
ssl = new TQCA::TLS;
connect(ssl, TQ_SIGNAL(handshaken()), TQ_SLOT(ssl_handshaken()));
connect(ssl, TQ_SIGNAL(readyRead()), TQ_SLOT(ssl_readyRead()));
connect(ssl, TQ_SIGNAL(readyReadOutgoing(int)), TQ_SLOT(ssl_readyReadOutgoing(int)));
connect(ssl, TQ_SIGNAL(closed()), TQ_SLOT(ssl_closed()));
connect(ssl, TQ_SIGNAL(error(int)), TQ_SLOT(ssl_error(int)));
rootCerts.setAutoDelete(true);
rootCerts = getRootCerts("/usr/local/share/psi/certs/rootcert.xml");
}
~SecureTest()
{
delete ssl;
delete sock;
}
void start(const TQString &_host)
{
int n = _host.find(':');
int port;
if(n != -1) {
host = _host.mid(0, n);
port = _host.mid(n+1).toInt();
}
else {
host = _host;
port = 443;
}
printf("Trying %s:%d...\n", host.latin1(), port);
sock->connectToHost(host, port);
}
signals:
void quit();
private slots:
void sock_connected()
{
printf("Connected, starting TLS handshake...\n");
ssl->setCertificateStore(rootCerts);
ssl->startClient(host);
}
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("\nConnection closed.\n");
quit();
}
void sock_error(int)
{
printf("\nSocket error.\n");
quit();
}
void ssl_handshaken()
{
cert = ssl->peerCertificate();
int vr = ssl->certificateValidityResult();
printf("Successful SSL handshake.\n");
if(!cert.isNull())
showCertInfo(cert);
if(vr == TQCA::TLS::Valid)
printf("Valid certificate.\n");
else
printf("Invalid certificate: %s\n", resultToString(vr).latin1());
printf("Let's try a GET request now.\n");
TQString req = "GET / HTTP/1.0\nHost: " + host + "\n\n";
TQCString cs = req.latin1();
TQByteArray buf(cs.length());
memcpy(buf.data(), cs.data(), buf.size());
ssl->write(buf);
}
void ssl_readyRead()
{
TQByteArray a = ssl->read();
TQCString cs;
cs.resize(a.size()+1);
memcpy(cs.data(), a.data(), a.size());
printf("%s", cs.data());
}
void ssl_readyReadOutgoing(int)
{
TQByteArray a = ssl->readOutgoing();
sock->writeBlock(a.data(), a.size());
}
void ssl_closed()
{
printf("SSL session closed\n");
}
void ssl_error(int x)
{
if(x == TQCA::TLS::ErrHandshake) {
printf("SSL Handshake Error!\n");
quit();
}
else {
printf("SSL Error!\n");
quit();
}
}
private:
TQString host;
TQSocket *sock;
TQCA::TLS *ssl;
TQCA::Cert cert;
TQPtrList<TQCA::Cert> rootCerts;
};
#include"ssltest.moc"
int main(int argc, char **argv)
{
TQApplication app(argc, argv, false);
TQString host = argc > 1 ? argv[1] : "andbit.net";
if(!TQCA::isSupported(TQCA::CAP_TLS)) {
printf("TLS not supported!\n");
return 1;
}
SecureTest *s = new SecureTest;
TQObject::connect(s, TQ_SIGNAL(quit()), &app, TQ_SLOT(quit()));
s->start(host);
app.exec();
delete s;
return 0;
}
|