-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
122 lines (102 loc) · 3.33 KB
/
main.cpp
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
#include <QCoreApplication>
#include <QSslCertificate>
#include <QSslKey>
#include <QSslConfiguration>
#include <QSslSocket>
#include <QDateTime>
#include <QFile>
#include <QByteArray>
#include <QDebug>
#include "sslclient.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString usage = "<CA cert file> [client cert file] [client key file] [host] [port]";
QString certFileName;
QString keyFileName;
QString host;
QString port;
SslClient client;
if (argc < 2) {
qDebug() << argv[0] << usage;
return 1;
}
// we have enough to load CA file
QFile caFile(argv[1]);
qDebug() << "CA file:" << caFile.fileName();
// we have enough to load client cert
if (argc >= 3) {
certFileName = argv[2];
qDebug() << "Cert file:" << certFileName;
}
// we have enough to load client key
if (argc >= 4) {
keyFileName = argv[3];
qDebug() << "Key file:" << keyFileName;
}
if (argc >= 5) {
// have host
if (argc < 6) {
qDebug() << "Need a port to test the host.";
qDebug() << argv[0] << usage;
return 1;
}
// have both host and port
host = argv[4];
port = argv[5];
}
QFile certFile(certFileName);
QFile keyFile(keyFileName);
QSslKey sslClientKey;
QSslCertificate sslClientCert;
QSslCertificate sslCaCert;
QList<QSslCertificate> sslCaList;
QSslConfiguration sslConfig;
if (caFile.exists()) {
// test CA
qDebug() << "found CA certificate" << caFile.fileName();
caFile.open(QIODevice::ReadOnly);
QSslCertificate ca(&caFile, QSsl::Pem);
sslCaCert = ca;
sslCaList.append(sslCaCert);
sslConfig.setCaCertificates(sslCaList);
qDebug() << "using CA:"
<< sslCaCert.subjectInfo(QSslCertificate::CommonName)
<< sslCaCert.expiryDate();
#if QT_VERSION < 0x050000
qDebug() << "CA is valid:" << sslCaCert.isValid();
#endif
}
if (certFile.exists()) {
// test client cert
qDebug() << "found client certificate" << certFile.fileName();
certFile.open(QIODevice::ReadOnly);
QSslCertificate cert(&certFile, QSsl::Pem);
qDebug() << "cert file not empty" << !cert.isNull();
sslClientCert = cert;
if (keyFile.exists()) {
// test client key
qDebug() << "found client key" << keyFile.fileName();
keyFile.open(QIODevice::ReadOnly);
const QSslKey key(&keyFile, QSsl::Rsa);
sslClientKey = key;
qDebug() << "key file not empty" << !sslClientKey.isNull();
sslConfig.setPrivateKey(sslClientKey);
}
qDebug() << "client cert:"
<< sslClientCert.subjectInfo(QSslCertificate::CommonName)
<< sslClientCert.subjectInfo(QSslCertificate::Organization)
<< sslClientCert.subjectInfo(QSslCertificate::OrganizationalUnitName)
<< sslClientCert.expiryDate();
#if QT_VERSION < 0x050000
qDebug() << "client cert is valid:" << sslClientCert.isValid();
#endif
}
if (!host.isEmpty() && !port.isEmpty()) {
client.secureConnect(sslConfig, host, port.toInt());
QObject::connect(&client, SIGNAL(done()), &a, SLOT(quit()));
} else {
a.exit(0);
}
return a.exec();
}