24#include <QCoreApplication>
28#include "import_plugins.h"
31char exampleCA_cert[] =
32 "-----BEGIN CERTIFICATE-----\n"
33 "MIICSzCCAbSgAwIBAgIBADANBgkqhkiG9w0BAQUFADA4MRMwEQYDVQQDEwpFeGFt\n"
34 "cGxlIENBMQswCQYDVQQGEwJVUzEUMBIGA1UEChMLRXhhbXBsZSBPcmcwHhcNMDYw\n"
35 "MzE1MDY1ODMyWhcNMDYwNDE1MDY1ODMyWjA4MRMwEQYDVQQDEwpFeGFtcGxlIENB\n"
36 "MQswCQYDVQQGEwJVUzEUMBIGA1UEChMLRXhhbXBsZSBPcmcwgZ8wDQYJKoZIhvcN\n"
37 "AQEBBQADgY0AMIGJAoGBAL6ULdOxmpeZ+G/ypV12eNO4qnHSVIPTrYPkQuweXqPy\n"
38 "atwGFheG+hLVsNIh9GGOS0tCe7a3hBBKN0BJg1ppfk2x39cDx7hefYqjBuZvp/0O\n"
39 "8Ja3qlQiJLezITZKLxMBrsibcvcuH8zpfUdys2yaN+YGeqNfjQuoNN3Byl1TwuGJ\n"
40 "AgMBAAGjZTBjMB0GA1UdDgQWBBSQKCUCLNM7uKrAt5o7qv/yQm6qEzASBgNVHRMB\n"
41 "Af8ECDAGAQEBAgEIMB4GA1UdEQQXMBWBE2V4YW1wbGVAZXhhbXBsZS5jb20wDgYD\n"
42 "VR0PAQH/BAQDAgEGMA0GCSqGSIb3DQEBBQUAA4GBAAh+SIeT1Ao5qInw8oMSoTdO\n"
43 "lQ6h67ec/Jk5KmK4OoskuimmHI0Sp0C5kOCLehXbsVWW8pXsNC2fv0d2HkdaSUcX\n"
44 "hwLzqgyZXd4mupIYlaOTZhuHDwWPCAOZS4LVsi2tndTRHKCP12441JjNKhmZRhkR\n"
45 "u5zzD60nWgM9dKTaxuZM\n"
46 "-----END CERTIFICATE-----\n";
50 printf(
"-- Cert --\n");
51 printf(
" CN: %s\n", qPrintable(cert.
commonName()));
52 printf(
" Valid from: %s, until %s\n",
55 printf(
" PEM:\n%s\n", qPrintable(cert.
toPEM()));
63 s = QStringLiteral(
"Validated");
66 s = QStringLiteral(
"Root CA is marked to reject the specified purpose");
69 s = QStringLiteral(
"Certificate not trusted for the required purpose");
72 s = QStringLiteral(
"Invalid signature");
75 s = QStringLiteral(
"Invalid CA certificate");
78 s = QStringLiteral(
"Invalid certificate purpose");
81 s = QStringLiteral(
"Certificate is self-signed");
84 s = QStringLiteral(
"Certificate has been revoked");
87 s = QStringLiteral(
"Maximum certificate chain length exceeded");
90 s = QStringLiteral(
"Certificate has expired");
93 s = QStringLiteral(
"CA has expired");
97 s = QStringLiteral(
"General certificate validation error");
103class SecureTest :
public QObject
112 sock =
new QTcpSocket;
115#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
130 ~SecureTest()
override
136 void start(
const QString &_host)
138 int n = _host.
indexOf(QLatin1Char(
':'));
141 host = _host.
mid(0, n);
142#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
143 port = QStringView(_host).mid(n + 1).toInt();
145 port = _host.midRef(n + 1).
toInt();
152 printf(
"Trying %s:%d...\n", qPrintable(host), port);
153 sock->connectToHost(host, port);
160 void sock_connected()
163 QCA::TLS *ssl = SecureTest::ssl;
165 printf(
"Connected, starting TLS handshake...\n");
174 printf(
"Warning: no root certs\n");
176 ssl->setTrustedCertificates(rootCerts);
178 ssl->startClient(host);
181 void sock_readyRead()
184 QCA::TLS *ssl = SecureTest::ssl;
189 void sock_connectionClosed()
191 printf(
"\nConnection closed.\n");
194 if (ssl_done && sock_done)
201 sock_connectionClosed();
205 printf(
"\nSocket error.\n");
209 void ssl_handshaken()
212 QCA::TLS *ssl = SecureTest::ssl;
216 printf(
"Successful SSL handshake using %s (%i of %i bits)\n",
217 qPrintable(ssl->cipherSuite()),
219 ssl->cipherMaxBits());
221 cert = ssl->peerCertificateChain().primary();
226 QString str = QStringLiteral(
"Peer Identity: ");
228 str += QStringLiteral(
"Valid");
230 str += QStringLiteral(
"Error: Wrong certificate");
232 str += QStringLiteral(
"Error: Invalid certificate.\n -> Reason: ") +
233 validityToString(ssl->peerCertificateValidity());
235 str += QStringLiteral(
"Error: No certificate");
236 printf(
"%s\n", qPrintable(str));
238 ssl->continueAfterStep();
240 printf(
"Let's try a GET request now.\n");
241 QString req = QStringLiteral(
"GET / HTTP/1.0\nHost: ") + host + QStringLiteral(
"\n\n");
245 void ssl_certificateRequested()
248 QCA::TLS *ssl = SecureTest::ssl;
250 printf(
"Server requested client certificate.\n");
251 QList<QCA::CertificateInfoOrdered> issuerList = ssl->issuerList();
253 printf(
"Allowed issuers:\n");
254 foreach (QCA::CertificateInfoOrdered i, issuerList)
255 printf(
" %s\n", qPrintable(i.
toString()));
258 ssl->continueAfterStep();
264 QCA::TLS *ssl = SecureTest::ssl;
266 QByteArray a = ssl->
read();
267 printf(
"%s", a.
data());
270 void ssl_readyReadOutgoing()
273 QCA::TLS *ssl = SecureTest::ssl;
275 sock->
write(ssl->readOutgoing());
280 printf(
"SSL session closed.\n");
283 if (ssl_done && sock_done)
290 QCA::TLS *ssl = SecureTest::ssl;
294 printf(
"SSL Handshake Error!\n");
297 printf(
"SSL Error!\n");
306 QCA::Certificate cert;
307 bool sock_done, ssl_done;
310#include "ssltest.moc"
312int main(
int argc,
char **argv)
320 printf(
"TLS not supported!\n");
324 SecureTest *s =
new SecureTest;
void addCertificate(const Certificate &cert)
Append a Certificate to this collection.
QString toString() const
Convert to RFC 1779 string format.
Public Key (X.509) certificate.
static Certificate fromPEM(const QString &s, ConvertResult *result=nullptr, const QString &provider=QString())
Import the certificate from PEM format.
QString commonName() const
The common name of the subject of the certificate.
QDateTime notValidBefore() const
The earliest date that the certificate is valid.
QString toPEM() const
Export the Certificate into a PEM format.
QDateTime notValidAfter() const
The latest date that the certificate is valid.
Convenience method for initialising and cleaning up QCA.
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.
void write(const QByteArray &a) override
This method writes unencrypted (plain) data to the SecureLayer implementation.
void writeIncoming(const QByteArray &a) override
This method accepts encoded (typically encrypted) data for processing.
@ ErrorHandshake
problem during the negotiation
IdentityResult peerIdentityResult() const
After the SSL/TLS handshake is complete, this method allows you to determine if the other end of the ...
IdentityResult
Type of identity.
@ HostMismatch
valid cert provided, but wrong owner
@ InvalidCertificate
invalid cert
@ Valid
identity is verified
@ NoCertificate
identity unknown
QByteArray read() override
This method reads decrypted (plain) data from the SecureLayer implementation.
Error errorCode() const
This method returns the type of error that has occurred.
void handshaken()
Emitted when the protocol handshake is complete.
void certificateRequested()
Emitted when the server requests a certificate.
Q_SCRIPTABLE Q_NOREPLY void start()
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
Validity
The validity (or otherwise) of a certificate.
@ ErrorValidityUnknown
Validity is unknown.
@ ErrorRevoked
The certificate has been revoked.
@ ErrorUntrusted
The certificate is not trusted.
@ ErrorExpired
The certificate has expired, or is not yet valid (e.g.
@ ErrorPathLengthExceeded
The path length from the root CA to this certificate is too long.
@ ErrorSignatureFailed
The signature does not match.
@ ErrorInvalidPurpose
The purpose does not match the intended usage.
@ ErrorExpiredCA
The Certificate Authority has expired.
@ ErrorSelfSigned
The certificate is self-signed, and is not found in the list of trusted certificates.
@ ErrorInvalidCA
The Certificate Authority is invalid.
@ ValidityGood
The certificate is valid.
@ ErrorRejected
The root CA rejected the certificate purpose.
QCA_EXPORT bool haveSystemStore()
Test if QCA can access the root CA certificates.
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
QCA_EXPORT CertificateCollection systemStore()
Get system-wide root Certificate Authority (CA) certificates.
SocketError error() const const
void errorOccurred(QAbstractSocket::SocketError socketError)
QString toString(QStringView format, QCalendar cal) const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLatin1(QByteArrayView str)
QString fromLocal8Bit(QByteArrayView str)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString mid(qsizetype position, qsizetype n) const const
int toInt(bool *ok, int base) const const
QByteArray toLatin1() const const