Please give an example of the server side, which shows use TLS.
Now I have the following code:
#include <QCoreApplication>
#include "server.h"
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
Server h;
return a.exec();
}
///////////////////////////////////////
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
class Server : public QTcpServer {
public:
Server();
void incomingConnection(int);
};
#endif // SERVER_H
///////////////////////////////////////////
#include "server.h"
#include <QSslSocket>
#include <QSslCertificate>
Server::Server() {
if (!listen(QHostAddress::Any, 80)) {
//error
}
}
void Server::incomingConnection(int d) {
QSslSocket * socket = new QSslSocket();
if(socket->setSocketDescriptor(d)) {
QString c, k;
c = "site.crt";
k = "site.key";
socket->setLocalCertificate(c);
socket->setPrivateKey(k);
socket->startServerEncryption();
if(socket->waitForEncrypted()) {
if(socket->waitForReadyRead()) {
socket->write(socket->readAll());
socket->waitForBytesWritten();
socket->disconnectFromHost();
if(socket->state() == QTcpSocket::UnconnectedState) {
socket->waitForDisconnected();
}
socket->close();
socket->deleteLater();
}
else {
delete socket;
}
}
else {
delete socket;
}
}
}
How can I change it to first use protocol TLS, and then SSL?
Common task is to write a server with support for SNI.
Here is a sample SSL or TLS server which uses a server.key and a server.crt file for encryption:
you can set the protocol to
QSsl::SslV3
orQSsl::TlsV1
to only accept SSL or TLS connections.