25#include <QCoreApplication>
27#include <QHostAddress>
33#include "import_plugins.h"
37 "-----BEGIN CERTIFICATE-----\n"
38 "MIICeTCCAeKgAwIBAgIRAKKKnOj6Aarmwf0phApitVAwDQYJKoZIhvcNAQEFBQAw\n"
39 "ODELMAkGA1UEBhMCVVMxFDASBgNVBAoTC0V4YW1wbGUgT3JnMRMwEQYDVQQDEwpF\n"
40 "eGFtcGxlIENBMB4XDTA2MDMxNTA3MDU1MloXDTA3MDMxNTA3MDU1MlowOjEVMBMG\n"
41 "A1UEAxMMRXhhbXBsZSBVc2VyMQswCQYDVQQGEwJVUzEUMBIGA1UEChMLRXhhbXBs\n"
42 "ZSBPcmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAPkKn0FfHMvRZv+3uFcw\n"
43 "VrOadJmANzLVeVW/DHZp4CXokXSksM66ZMqFuQRBk5rnIZZpZmVp1tTRDVt9sEAY\n"
44 "YNa8CRM4HXkVlU0lCKdey18CSq2VuSvNtw8dDpoBmQt3nr9tePvKHnpS3nm6YjR2\n"
45 "NEvIKt1P4mHzYXLmwoF24C1bAgMBAAGjgYAwfjAdBgNVHQ4EFgQUmQIdzyDaPYWF\n"
46 "fPJ8PPOOm1eSsucwHwYDVR0jBBgwFoAUkCglAizTO7iqwLeaO6r/8kJuqhMwDAYD\n"
47 "VR0TAQH/BAIwADAeBgNVHREEFzAVgRNleGFtcGxlQGV4YW1wbGUuY29tMA4GA1Ud\n"
48 "DwEB/wQEAwIF4DANBgkqhkiG9w0BAQUFAAOBgQAuhbiUgy2a++EUccaonID7eTJZ\n"
49 "F3D5qXMqUpQxlYxU8du+9AxDD7nFxTMkQC2pzfmEc1znRNmJ1ZeLRL72VYsVndcT\n"
50 "psyM8ABkvPp1d2jWIyccVjGpt+/RN5IPKm/YIbtIZcywvWuXrOp1lanVmppLfPnO\n"
51 "6yneBkC9iqjOv/+Q+A==\n"
52 "-----END CERTIFICATE-----\n";
54char pemdata_privkey[] =
55 "-----BEGIN PRIVATE KEY-----\n"
56 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAPkKn0FfHMvRZv+3\n"
57 "uFcwVrOadJmANzLVeVW/DHZp4CXokXSksM66ZMqFuQRBk5rnIZZpZmVp1tTRDVt9\n"
58 "sEAYYNa8CRM4HXkVlU0lCKdey18CSq2VuSvNtw8dDpoBmQt3nr9tePvKHnpS3nm6\n"
59 "YjR2NEvIKt1P4mHzYXLmwoF24C1bAgMBAAECgYEAyIjJHDaeVXDU42zovyxpZE4n\n"
60 "PcOEryY+gdFJE8DFgUD4f1huFsj4iCuNg+PaG42p+hf9IARNvSho/RcEaVg4AJrV\n"
61 "jRP8r7fSqcIGr6lGuvDFFv3SU5ddy84g5oqLYGKvuPSHMGfVsZSxAwOrzD4bH19L\n"
62 "SNqtNcpdBsBd7ZiEE4ECQQD/oJGui9D5Dx3QVcS+QV4F8wuyN9jYIANmX/17o0fl\n"
63 "BL0bwRU4RICwadrcybi5N0JQLIYSUm2HGqNvAJbtnuQxAkEA+WeYLLYPeawcy+WU\n"
64 "kGcOR7BUjHiG71+6cvU4XIDW2bezA04fqWXkZRFAwHTMpQb785/XalFftgS21kql\n"
65 "8yLDSwJAHkeT2hwftdDPlEUEmBDAJW5DvWmWGwu3u2G1cfbGZl9oUyhM7ixXHg57\n"
66 "6VlPs0jTZxHPE86FwNIr99MXDbCbkQJBAMDFOJK+ecGirXNP1P+0GA6DFSap9inJ\n"
67 "BRTbwx+EmgwX966DUOefEOSpbDIVVSPs/Qr2LgtIMEFA7Y0+j3wZD3cCQBsTwccd\n"
68 "ASQx59xakpq11eOlTYz14rjwodr4QMyj26WxEPJtz7hKokx/+EH6fWuPIUSrROM5\n"
70 "-----END PRIVATE KEY-----\n";
72class SecureServer :
public QObject
85 SecureServer(quint16 _port)
104 ~SecureServer()
override
113 qDebug() <<
"Error loading cert!";
118 qDebug() <<
"Error loading private key!";
123 qDebug() <<
"Error binding to port " << port;
127 qDebug() <<
"Listening on port" << port;
134 void sock_readyRead()
138 int num = sock->
read(buf.data(), buf.size());
141 qDebug() <<
"Error reading data from socket";
143 if (num < buf.size())
149 void server_handleConnection()
156 qDebug() <<
"throwing away extra connection";
163#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
170 qDebug() <<
"Connection received! Starting TLS handshake.";
175 void sock_disconnected()
177 qDebug() <<
"Connection closed.";
180 void sock_bytesWritten(qint64 x)
182 if (mode == Active && sent) {
186 if (bytesLeft == 0) {
188 qDebug() <<
"Data transfer complete - SSL shutting down";
196 qDebug() <<
"Socket error: " << (unsigned)error;
199 void ssl_handshaken()
201 qDebug() <<
"Successful SSL handshake. Waiting for newline.";
213 "<head><title>Test</title></head>\n"
214 "<body>this is only a test</body>\n"
217 qDebug() <<
"Sending test response.";
222 void ssl_readyReadOutgoing()
226 sock->
write(outgoingData);
231 qDebug() <<
"Closing socket.";
239 qDebug() <<
"SSL Handshake Error! Closing.";
242 qDebug() <<
"SSL Error! Closing.";
261#include "sslservtest.moc"
263int main(
int argc,
char **argv)
271 qDebug() <<
"TLS not supported!";
275 SecureServer *server =
new SecureServer(port);
Public Key (X.509) certificate.
static Certificate fromPEM(const QString &s, ConvertResult *result=nullptr, const QString &provider=QString())
Import the certificate from PEM format.
bool isNull() const
Test if the certificate is empty (null)
Convenience method for initialising and cleaning up QCA.
bool isNull() const
Test if the key is null (empty)
static PrivateKey fromPEM(const QString &s, const SecureArray &passphrase=SecureArray(), ConvertResult *result=nullptr, const QString &provider=QString())
Import the key from Privacy Enhanced Mail (PEM) format.
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.
Transport Layer Security / Secure Socket Layer.
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.
void continueAfterStep()
Resumes TLS processing.
@ ErrorHandshake
problem during the negotiation
void startServer()
Start the TLS/SSL connection as a server.
QByteArray readOutgoing(int *plainBytes=nullptr) override
This method provides encoded (typically encrypted) data.
void close() override
Close the link.
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.
int convertBytesWritten(qint64 encryptedBytes) override
Convert encrypted bytes written to plain text bytes written.
void setCertificate(const CertificateChain &cert, const PrivateKey &key)
The local certificate to use.
void handshaken()
Emitted when the protocol handshake is complete.
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
virtual qint64 bytesAvailable() const const override
virtual void close() override
SocketError error() const const
void errorOccurred(QAbstractSocket::SocketError socketError)
void resize(qsizetype newSize, char c)
void bytesWritten(qint64 bytes)
QByteArray read(qint64 maxSize)
qint64 write(const QByteArray &data)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLatin1(QByteArrayView str)
int toInt(bool *ok, int base) const const
bool listen(const QHostAddress &address, quint16 port)
virtual QTcpSocket * nextPendingConnection()