Added SSL encryption in file upload

DeviceId is used for peerVerifyName
This commit is contained in:
Vineet Garg 2015-07-06 02:35:26 +05:30
parent 8212315700
commit 0c110e4392
4 changed files with 45 additions and 26 deletions

View file

@ -57,9 +57,14 @@ void LanDeviceLink::setOnSsl(bool value) {
bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np)
{
if (np.hasPayload()) {
UploadJob* job = new UploadJob(np.payload());
job->start();
np.setPayloadTransferInfo(job->getTransferInfo());
QVariantMap sslInfo;
if (onSsl) {
sslInfo.insert("useSsl", true);
sslInfo.insert("deviceId", deviceId());
}
UploadJob* job = new UploadJob(np.payload(), sslInfo);
job->start();
np.setPayloadTransferInfo(job->getTransferInfo());
}
if (!onSsl) {
@ -77,9 +82,14 @@ bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np
bool LanDeviceLink::sendPackage(NetworkPackage& np)
{
if (np.hasPayload()) {
UploadJob* job = new UploadJob(np.payload());
job->start();
np.setPayloadTransferInfo(job->getTransferInfo());
QVariantMap sslInfo;
if (onSsl) {
sslInfo.insert("useSsl", true);
sslInfo.insert("deviceId", deviceId());
}
UploadJob* job = new UploadJob(np.payload(), sslInfo);
job->start();
np.setPayloadTransferInfo(job->getTransferInfo());
}
int written = mSocketLineReader->write(np.serialize());

View file

@ -204,8 +204,7 @@ void LanLinkProvider::connected()
bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId);
//TODO : Change it too device id from received package, also correct it on Android side
socket->setPeerVerifyName("Vineet Garg");
socket->setPeerVerifyName(receivedPackage->get<QString>("deviceId"));
if (isDeviceTrusted) {
qDebug() << "Device trusted";
@ -340,8 +339,7 @@ void LanLinkProvider::dataReceived()
bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId);
// TODO : Change it to device id of remote device, correct it on Android side too, certificate name is not set there
socket->setPeerVerifyName("Vineet Garg");
socket->setPeerVerifyName(np->get<QString>("deviceId"));
if (isDeviceTrusted) {
qDebug() << "Device trusted";

View file

@ -20,17 +20,21 @@
#include <qalgorithms.h>
#include <QtGlobal>
#include <kdeconnectconfig.h>
#include "uploadjob.h"
#include "core_debug.h"
UploadJob::UploadJob(const QSharedPointer<QIODevice>& source): KJob()
UploadJob::UploadJob(const QSharedPointer<QIODevice>& source, QVariantMap sslInfo): KJob()
{
mInput = source;
mServer = new QTcpServer(this);
mServer = new Server(this);
mSocket = 0;
mPort = 0;
// We will use this info if link is on ssl, to send encrypted payload
this->sslInfo = sslInfo;
connect(mInput.data(), SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(mInput.data(), SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
}
@ -46,27 +50,33 @@ void UploadJob::start()
return;
}
}
connect(mServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
connect(mServer, SIGNAL(newConnection(QSslSocket*)), this, SLOT(newConnection(QSslSocket*)));
}
void UploadJob::newConnection()
void UploadJob::newConnection(QSslSocket* socket)
{
if (mSocket || !mServer->hasPendingConnections()) return;
if (!mInput->open(QIODevice::ReadOnly)) {
qWarning() << "error when opening the input to upload";
return; //TODO: Handle error, clean up...
}
mSocket = mServer->nextPendingConnection();
mSocket = socket;
if (sslInfo.value("useSsl", false).toBool()) {
mSocket->setLocalCertificate(KdeConnectConfig::instance()->certificate());
mSocket->setPrivateKey(KdeConnectConfig::instance()->privateKeyPath());
mSocket->setProtocol(QSsl::TlsV1_2);
mSocket->setPeerVerifyName(sslInfo.value("deviceId").toString());
mSocket->addCaCertificate(QSslCertificate(KdeConnectConfig::instance()->getTrustedDevice(sslInfo.value("deviceId").toString()).certificate.toLatin1()));
mSocket->startServerEncryption();
mSocket->waitForEncrypted();
}
readyRead();
}
void UploadJob::readyRead()
{
//TODO: Implement payload encryption
while ( mInput->bytesAvailable() > 0 )
{
qint64 bytes = qMin(mInput->bytesAvailable(), (qint64)4096);

View file

@ -25,28 +25,29 @@
#include <QIODevice>
#include <QVariantMap>
#include <QTcpServer>
#include <QTcpSocket>
#include <QSharedPointer>
#include <QSslSocket>
#include "server.h"
class UploadJob
: public KJob
{
Q_OBJECT
public:
UploadJob(const QSharedPointer<QIODevice>& source);
UploadJob(const QSharedPointer<QIODevice>& source, QVariantMap sslInfo);
virtual void start();
QVariantMap getTransferInfo();
private:
QSharedPointer<QIODevice> mInput;
QTcpServer* mServer;
QTcpSocket* mSocket;
Server* mServer;
QSslSocket* mSocket;
quint16 mPort;
QVariantMap sslInfo;
private Q_SLOTS:
void readyRead();
void newConnection();
void newConnection(QSslSocket*);
void aboutToClose();
};