-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsslclient.cpp
106 lines (91 loc) · 2.48 KB
/
sslclient.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
#include "sslclient.h"
#include <QFile>
#include <QSslKey>
#include <QSslConfiguration>
SslClient::SslClient(QObject *parent)
: QObject{parent}
{
connect(&m_socket, &QSslSocket::encrypted,
this, &SslClient::connectedToServer);
connect(&m_socket, &QSslSocket::sslErrors,
this, &SslClient::error);
connect(&m_socket, &QSslSocket::stateChanged,
this, &SslClient::stateChanged);
//m_socket.setPeerVerifyMode(QSslSocket::QueryPeer);
}
void SslClient::setCertificate(const QString &path)
{
QFile file(path);
file.open(QFile::OpenModeFlag::ReadOnly);
m_socket.setLocalCertificate(QSslCertificate(file.readAll()));
}
void SslClient::setPrivateKey(const QString &path, const QString &passphrase)
{
QFile file(path);
file.open(QFile::OpenModeFlag::ReadOnly);
QSslKey key(file.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, passphrase.toUtf8());
m_socket.setPrivateKey(key);
}
void SslClient::setCA(const QString &path)
{
QFile file(path);
file.open(QFile::OpenModeFlag::ReadOnly);
QSslCertificate ca(file.readAll());
auto sslConfig{m_socket.sslConfiguration()};
sslConfig.addCaCertificate(ca);
m_socket.setSslConfiguration(sslConfig);
QList<QSslCertificate> chain{ca};
m_socket.setLocalCertificateChain(chain);
}
void SslClient::setServerHostName(const QString &host)
{
m_hostname = host;
}
void SslClient::unlock()
{
m_socket.connectToHostEncrypted(m_hostname, 4433, "Aorus-15-XE5");
}
void SslClient::connectedToServer()
{
/* Connected sucsessfully*/
qDebug() << "Writing";
if(m_socket.write("Unlock\n") ==-1)
{
qDebug() << "Error writing to socket";
}
else
m_socket.flush();
m_socket.disconnectFromHost();
}
void SslClient::disconnectedFromServer()
{
}
void SslClient::error(const QList<QSslError> &errors)
{
qDebug()<< "Error occurred";
for(const auto & error : errors)
{
qDebug() << error;
}
}
void SslClient::stateChanged(QAbstractSocket::SocketState newState)
{
if(newState == QAbstractSocket::ConnectedState)
{
qDebug() << "Connected";
}
else if(newState == QAbstractSocket::HostLookupState)
{
qDebug() << "HostLookupState";
}
else if(newState == QAbstractSocket::UnconnectedState)
{
qDebug() << "UnconnectedState";
}
else if(newState == QAbstractSocket::ConnectingState)
{
qDebug() << "ConnectingState";
}
else
qDebug() << newState;
}