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)
88 server =
new QTcpServer;
104 ~SecureServer()
override
113 qDebug() <<
"Error loading cert!";
117 if (privkey.isNull()) {
118 qDebug() <<
"Error loading private key!";
123 qDebug() <<
"Error binding to port " << port;
127 qDebug() <<
"Listening on port" << port;
134 void sock_readyRead()
136 QByteArray buf(sock->bytesAvailable(), 0x00);
138 int num = sock->read(buf.data(), buf.size());
141 qDebug() <<
"Error reading data from socket";
143 if (num < buf.size())
146 ssl->writeIncoming(buf);
149 void server_handleConnection()
153 QTcpSocket *tmp = server->nextPendingConnection();
156 qDebug() <<
"throwing away extra connection";
160 sock = server->nextPendingConnection();
163#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
170 qDebug() <<
"Connection received! Starting TLS handshake.";
171 ssl->setCertificate(cert, privkey);
175 void sock_disconnected()
177 qDebug() <<
"Connection closed.";
180 void sock_bytesWritten(qint64 x)
182 if (mode == Active && sent) {
183 qint64 bytes = ssl->convertBytesWritten(x);
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.";
205 ssl->continueAfterStep();
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()
225 QByteArray outgoingData = ssl->readOutgoing(&plainBytes);
226 sock->write(outgoingData);
231 qDebug() <<
"Closing socket.";
239 qDebug() <<
"SSL Handshake Error! Closing.";
242 qDebug() <<
"SSL Error! Closing.";
253 QCA::Certificate cert;
254 QCA::PrivateKey privkey;
261#include "sslservtest.moc"
263int main(
int argc,
char **argv)
271 qDebug() <<
"TLS not supported!";
275 SecureServer *server =
new SecureServer(port);
static Certificate fromPEM(const QString &s, ConvertResult *result=nullptr, const QString &provider=QString())
Import the certificate from PEM format.
Convenience method for initialising and cleaning up QCA.
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.
@ ErrorHandshake
problem during the negotiation
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 void close() override
SocketError error() const const
void errorOccurred(QAbstractSocket::SocketError socketError)
void bytesWritten(qint64 bytes)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString fromLatin1(QByteArrayView str)
int toInt(bool *ok, int base) const const