This commit is contained in:
Albert Vaca 2015-11-30 10:36:01 -08:00
parent 30d2dd991b
commit 16f9af908a
22 changed files with 212 additions and 297 deletions

View file

@ -184,25 +184,9 @@ int main(int argc, char** argv)
}
} else if(parser.isSet("encryption-info")) {
DeviceDbusInterface dev(device);
QDBusPendingReply<QByteArray> devReply = dev.certificate(1); // QSsl::Der = 1
QDBusPendingReply<QByteArray> devReply = dev.encryptionInfo(); // QSsl::Der = 1
devReply.waitForFinished();
if (devReply.value().isEmpty()) {
QTextStream(stderr) << i18n("The other device doesn\'t use a recent version of KDE Connect, using the legacy encryption method.") << endl;
} else {
QByteArray remoteCertificate = QCryptographicHash::hash(devReply.value(), QCryptographicHash::Sha1).toHex();
for (int i=2 ; i<remoteCertificate.size() ; i+=3)
remoteCertificate.insert(i, ':'); // Improve readability
DaemonDbusInterface iface;
QDBusPendingReply<QByteArray> ifaceReply = iface.certificate(1); // QSsl::Der = 1
ifaceReply.waitForFinished();
QByteArray myCertificate = QCryptographicHash::hash(ifaceReply.value(), QCryptographicHash::Sha1).toHex();
for (int i=2 ; i<myCertificate.size() ; i+=3)
myCertificate.insert(i, ':'); // Improve readability
QTextStream(stderr) << i18n("SHA1 fingerprint of your device certificate is : ") << myCertificate << endl;
QTextStream(stderr) << i18n("SHA1 fingerprint of remote device certificate is : ") << remoteCertificate << endl;
}
QTextStream(stderr) << devReply.value() << endl;
} else {
QTextStream(stderr) << i18n("Nothing to be done") << endl;
}

View file

@ -25,10 +25,11 @@
#include <QtCrypto>
#include "core/networkpackage.h"
#include "pairinghandler.h"
class PairingHandler;
class NetworkPackage;
class LinkProvider;
class Device;
class DeviceLink
: public QObject

View file

@ -35,18 +35,15 @@ DownloadJob::DownloadJob(const QHostAddress &address, const QVariantMap &transfe
mAddress = address;
mPort = transferInfo["port"].toInt();
mSocket = QSharedPointer<QSslSocket>(new QSslSocket);
useSsl = transferInfo.value("useSsl", false).toBool();
// Setting ssl related properties for socket when using ssl
if (useSsl) {
mSocket->setLocalCertificate(KdeConnectConfig::instance()->certificate());
mSocket->setPrivateKey(KdeConnectConfig::instance()->privateKeyPath());
mSocket->setProtocol(QSsl::TlsV1_2);
mSocket->setPeerVerifyName(transferInfo.value("deviceId").toString());
mSocket->setPeerVerifyMode(QSslSocket::VerifyPeer);
mSocket->addCaCertificate(QSslCertificate(KdeConnectConfig::instance()->getTrustedDevice(
transferInfo.value("deviceId").toString()).certificate.toLatin1()));
}
mSocket->setLocalCertificate(KdeConnectConfig::instance()->certificate());
mSocket->setPrivateKey(KdeConnectConfig::instance()->privateKeyPath());
mSocket->setProtocol(QSsl::TlsV1_2);
mSocket->setPeerVerifyName(transferInfo.value("deviceId").toString());
mSocket->setPeerVerifyMode(QSslSocket::VerifyPeer);
mSocket->addCaCertificate(QSslCertificate(KdeConnectConfig::instance()->getDeviceProperty(transferInfo.value("deviceId").toString(),"certificate").toLatin1()));
}
DownloadJob::~DownloadJob()
@ -61,14 +58,11 @@ void DownloadJob::start()
connect(mSocket.data(), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(done()));
//connect(mSocket.data(), &QAbstractSocket::connected, [=](){ qDebug() << "Connected"; });
if (useSsl) {
// Cannot use read only, might be due to ssl handshake, getting QIODevice::ReadOnly error and no connection
mSocket->connectToHostEncrypted(mAddress.toString(), mPort, QIODevice::ReadWrite);
mSocket->waitForEncrypted();
} else {
mSocket->connectToHost(mAddress, mPort, QIODevice::ReadOnly);
mSocket->waitForConnected();
}
// Cannot use read only, might be due to ssl handshake, getting QIODevice::ReadOnly error and no connection
mSocket->connectToHostEncrypted(mAddress.toString(), mPort, QIODevice::ReadWrite);
mSocket->waitForEncrypted();
// mSocket->connectToHost(mAddress, mPort, QIODevice::ReadOnly);
// mSocket->waitForConnected();
//mSocket->open(QIODevice::ReadOnly);

View file

@ -57,7 +57,7 @@ void LanDeviceLink::setOnSsl(bool value)
PairingHandler* LanDeviceLink::createPairingHandler(Device* device)
{
return new LanPairingHandler(device);
return new LanPairingHandler(device->id());
}
bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np)
@ -95,7 +95,7 @@ UploadJob* LanDeviceLink::sendPayload(NetworkPackage& np)
transferInfo.insert("useSsl", true);
transferInfo.insert("deviceId", deviceId());
}
UploadJob* job = new UploadJob(np.payload(), transferInfo);
UploadJob* job = new UploadJob(np.payload(), deviceId());
job->start();
return job;
}

View file

@ -37,7 +37,6 @@
#include "../../daemon.h"
#include "landevicelink.h"
#include "lanpairinghandler.h"
#include <kdeconnectconfig.h>
#include <QDBusPendingReply>
#include <QtNetwork/qsslcipher.h>
@ -187,7 +186,6 @@ void LanLinkProvider::connected()
NetworkPackage* receivedPackage = receivedIdentityPackages[socket].np;
const QString& deviceId = receivedPackage->get<QString>("deviceId");
//qCDebug(KDECONNECT_CORE) << "Connected" << socket->isWritable();
LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket, DeviceLink::Remotely);
// If network is on ssl, do not believe when they are connected, believe when handshake is completed
NetworkPackage np2("");
@ -210,7 +208,7 @@ void LanLinkProvider::connected()
if (isDeviceTrusted) {
qDebug() << "Device trusted";
QString certString = KdeConnectConfig::instance()->getTrustedDevice(deviceId).certificate;
QString certString = KdeConnectConfig::instance()->getDeviceProperty(deviceId, "certificate", QString());
socket->addCaCertificate(QSslCertificate(certString.toLatin1()));
socket->setPeerVerifyMode(QSslSocket::VerifyPeer);
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
@ -225,7 +223,7 @@ void LanLinkProvider::connected()
socket->startServerEncryption();
return; // Return statement prevents from deleting received package, needed in slot "encrypted"
} else {
addLink(deviceId, socket, receivedPackage);
addLink(deviceId, socket, receivedPackage, DeviceLink::Remotely);
}
} else {
@ -256,7 +254,7 @@ void LanLinkProvider::encrypted()
receivedPackage->set("certificate", socket->peerCertificate().toPem());
addLink(deviceId, socket, receivedPackage);
addLink(deviceId, socket, receivedPackage, DeviceLink::Remotely);
// Copied from connected slot, now delete received package
delete receivedPackage;
@ -355,7 +353,7 @@ void LanLinkProvider::dataReceived()
if (isDeviceTrusted) {
qDebug() << "Device trusted";
QString certString = KdeConnectConfig::instance()->getTrustedDevice(deviceId).certificate;
QString certString = KdeConnectConfig::instance()->getDeviceProperty(deviceId, "certificate", QString());
socket->addCaCertificate(QSslCertificate(certString.toLatin1()));
socket->setPeerVerifyMode(QSslSocket::VerifyPeer);
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
@ -370,7 +368,7 @@ void LanLinkProvider::dataReceived()
socket->startClientEncryption();
return;
} else {
addLink(deviceId, socket, np);
addLink(deviceId, socket, np, DeviceLink::Locally);
}
delete np;
@ -420,9 +418,10 @@ void LanLinkProvider::configureSocket(QSslSocket* socket)
}
void LanLinkProvider::addLink(QString deviceId, QSslSocket* socket, NetworkPackage* receivedPackage) {
void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, DeviceLink::ConnectionStarted connectionOrigin)
{
LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket, DeviceLink::Locally);
LanDeviceLink* deviceLink = new LanDeviceLink(deviceId, this, socket, connectionOrigin);
connect(deviceLink, SIGNAL(destroyed(QObject*)), this, SLOT(deviceLinkDestroyed(QObject*)));
// Socket disconnection will now be handled by LanDeviceLink

View file

@ -28,7 +28,6 @@
#include <QtNetwork/qsslsocket.h>
#include "../linkprovider.h"
#include "netaddress.h"
#include "server.h"
#include "landevicelink.h"
@ -43,7 +42,6 @@ public:
QString name() override { return "LanLinkProvider"; }
int priority() override { return PRIORITY_HIGH; }
void addLink(QString, QSslSocket*, NetworkPackage*);
public Q_SLOTS:
virtual void onNetworkChange() override;
@ -63,6 +61,7 @@ private Q_SLOTS:
private:
static void configureSocket(QSslSocket* socket);
void onNetworkConfigurationChanged(const QNetworkConfiguration &config);
void addLink(const QString& deviceId, QSslSocket* socket, NetworkPackage* receivedPackage, DeviceLink::ConnectionStarted connectionOrigin);
Server* mServer;
QUdpSocket* mUdpServer;

View file

@ -27,28 +27,28 @@
#include "lanpairinghandler.h"
#include "networkpackagetypes.h"
LanPairingHandler::LanPairingHandler(Device* device)
: PairingHandler(device)
LanPairingHandler::LanPairingHandler(const QString& deviceId)
: PairingHandler()
, m_deviceId(deviceId)
{
m_pairingTimeout.setSingleShot(true);
m_pairingTimeout.setInterval(30 * 1000); //30 seconds of timeout
connect(&m_pairingTimeout, SIGNAL(timeout()),
this, SLOT(pairingTimeout()));
if (device->isPaired()) {
if (!KdeConnectConfig::instance()->getTrustedDevice(device->id()).publicKey.isNull()) {
m_pairStatus = PairStatus::Paired;
} else {
requestPairing(); // Request pairing if device is paired but public key is not there
}
/*
m_publicKey = KdeConnectConfig::instance()->getDeviceProperty(deviceId, "publicKey", QString());
m_certificate = KdeConnectConfig::instance()->getDeviceProperty(deviceId, "certificate", QString());
*/
if (!m_publicKey.isNull()) {
setPairStatus(PairStatus::Paired);
} else {
m_pairStatus = PairStatus::NotPaired;
setPairStatus(PairStatus::NotPaired);
}
}
void LanPairingHandler::createPairPackage(NetworkPackage& np)
{
np.set("link", m_deviceLink->name());
np.set("link", deviceLink()->name());
np.set("pair", true);
np.set("publicKey", KdeConnectConfig::instance()->publicKey().toPEM());
}
@ -56,7 +56,7 @@ void LanPairingHandler::createPairPackage(NetworkPackage& np)
void LanPairingHandler::packageReceived(const NetworkPackage& np)
{
if (np.get<QString>("link", m_deviceLink->name()).compare(m_deviceLink->name()) != 0) return; // If this package is not received by my type of link
if (np.get<QString>("link", deviceLink()->name()).compare(deviceLink()->name()) != 0) return; // If this package is not received by my type of link
m_pairingTimeout.stop();
@ -64,77 +64,75 @@ void LanPairingHandler::packageReceived(const NetworkPackage& np)
if (wantsPair == isPaired()) {
// qCDebug(KDECONNECT_CORE) << "Already" << (wantsPair? "paired":"unpaired");
if (m_pairStatus == PairStatus ::Requested) {
m_pairStatus = PairStatus ::NotPaired;
if (isPairRequested()) {
setPairStatus(PairStatus::NotPaired);
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
return;
} else if (m_pairStatus == PairStatus ::Paired) {
} else if (isPaired()) {
/**
* If wants pair is true and is paired is true, this means other device is trying to pair again, might be because it unpaired this device somehow
* and we don't know it, unpair it internally
*/
Q_EMIT unpairingDone();
KdeConnectConfig::instance()->removeTrustedDevice(m_deviceId);
setPairStatus(PairingHandler::NotPaired);
}
}
if (wantsPair) {
QString keyString = np.get<QString>("publicKey");
m_device->setPublicKey(QCA::RSAPublicKey::fromPEM(keyString));
bool success = !m_device->publicKey().isNull();
if (!success) {
if (m_pairStatus == PairStatus ::Requested) {
m_pairStatus = PairStatus::NotPaired;
QString certificateString = np.get<QByteArray>("certificate");
if (QCA::RSAPublicKey::fromPEM(keyString).isNull()) {
if (isPairRequested()) {
setPairStatus(PairStatus::NotPaired);
}
Q_EMIT pairingFailed(i18n("Received incorrect key"));
return;
}
if (m_pairStatus == PairStatus::Requested) { //We started pairing
if (isPairRequested()) { //We started pairing
qCDebug(KDECONNECT_CORE) << "Pair answer";
setAsPaired();
KdeConnectConfig::instance()->setDeviceProperty(m_deviceId, "publicKey", keyString);
KdeConnectConfig::instance()->setDeviceProperty(m_deviceId, "certificate", certificateString);
} else {
qCDebug(KDECONNECT_CORE) << "Pair request";
if (m_device->isPaired()) {
if (isPaired()) {
acceptPairing();
return;
}
Daemon::instance()->requestPairing(m_device);
m_pairStatus = PairStatus ::RequestedByPeer;
//Daemon::instance()->requestPairing(m_device);
//m_pairStatus = PairStatus::RequestedByPeer;
setPairStatus(PairStatus::RequestedByPeer);
}
} else {
} else { //wantsPair == false
qCDebug(KDECONNECT_CORE) << "Unpair request";
PairStatus prevPairStatus = m_pairStatus;
m_pairStatus = PairStatus::NotPaired;
if (prevPairStatus == PairStatus ::Requested) {
if (isPairRequested()) {
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
} else if (prevPairStatus == PairStatus::Paired) {
Q_EMIT (unpairingDone());
}
setPairStatus(PairStatus::NotPaired);
}
}
bool LanPairingHandler::requestPairing()
{
switch (m_pairStatus) {
switch (pairStatus()) {
case PairStatus::Paired:
Q_EMIT pairingFailed(i18n(m_deviceLink->name().append(" : Already paired").toLatin1().data()));
Q_EMIT pairingFailed(i18n(deviceLink()->name().append(" : Already paired").toLatin1().data()));
return false;
case PairStatus ::Requested:
Q_EMIT pairingFailed(i18n(m_deviceLink->name().append(" : Pairing already requested for this device").toLatin1().data()));
case PairStatus::Requested:
Q_EMIT pairingFailed(i18n(deviceLink()->name().append(" : Pairing already requested for this device").toLatin1().data()));
return false;
case PairStatus ::RequestedByPeer:
qCDebug(KDECONNECT_CORE) << m_deviceLink->name() << " : Pairing already started by the other end, accepting their request.";
case PairStatus::RequestedByPeer:
qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request.";
acceptPairing();
return false;
case PairStatus::NotPaired:
@ -144,9 +142,9 @@ bool LanPairingHandler::requestPairing()
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
bool success;
success = m_deviceLink->sendPackage(np);
success = deviceLink()->sendPackage(np);
if (success) {
m_pairStatus = PairStatus::Requested;
setPairStatus(PairStatus::Requested);
m_pairingTimeout.start();
}
return success;
@ -154,14 +152,13 @@ bool LanPairingHandler::requestPairing()
bool LanPairingHandler::acceptPairing()
{
m_pairingTimeout.stop(); // Just in case it is started
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
bool success;
success = m_deviceLink->sendPackage(np);
success = deviceLink()->sendPackage(np);
if (success) {
m_pairStatus = PairStatus::Paired;
setAsPaired();
Q_EMIT(pairingDone());
setPairStatus(PairStatus::Paired);
}
return success;
}
@ -170,36 +167,25 @@ void LanPairingHandler::rejectPairing()
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
np.set("link", m_deviceLink->name());
m_deviceLink->sendPackage(np);
m_pairStatus = PairStatus::NotPaired;
np.set("link", deviceLink()->name());
deviceLink()->sendPackage(np);
setPairStatus(PairStatus::NotPaired);
}
void LanPairingHandler::unpair() {
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
np.set("link", m_deviceLink->name());
m_deviceLink->sendPackage(np);
m_pairStatus = PairStatus::NotPaired;
np.set("link", deviceLink()->name());
deviceLink()->sendPackage(np);
setPairStatus(PairStatus::NotPaired);
}
void LanPairingHandler::pairingTimeout()
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
np.set("name", m_deviceLink->name());
m_deviceLink->sendPackage(np);
m_pairStatus = PairStatus::NotPaired;
m_device->pairingTimeout(); // Use signal slot, or this is good ?
}
void LanPairingHandler::setAsPaired()
{
KdeConnectConfig::instance()->setDeviceProperty(m_device->id(), "publicKey", m_device->publicKey().toPEM());
KdeConnectConfig::instance()->setDeviceProperty(m_device->id(), "certificate", QString::fromLatin1(m_device->certificate().toPem()));
m_pairStatus = PairStatus::Paired;
m_pairingTimeout.stop(); // Just in case it is started
Q_EMIT(pairingDone());
np.set("name", deviceLink()->name());
deviceLink()->sendPackage(np);
setPairStatus(PairStatus::NotPaired); //Will emit the change as well
Q_EMIT pairingFailed(i18n("Timed out"));
}

View file

@ -30,7 +30,7 @@ class LanPairingHandler
: public PairingHandler
{
public:
LanPairingHandler(Device* device);
LanPairingHandler(const QString& deviceId);
virtual ~LanPairingHandler() { }
virtual void createPairPackage(NetworkPackage& np) Q_DECL_OVERRIDE;
@ -40,12 +40,14 @@ public:
virtual void rejectPairing() Q_DECL_OVERRIDE;
virtual void unpair() Q_DECL_OVERRIDE;
public Q_SLOTS:
private Q_SLOTS:
virtual void pairingTimeout();
private:
virtual void setAsPaired() Q_DECL_OVERRIDE;
protected:
QTimer m_pairingTimeout;
QString m_deviceId;
QCA::PublicKey m_publicKey;
QSslCertificate m_certificate;
};

View file

@ -26,7 +26,7 @@
#include "core_debug.h"
UploadJob::UploadJob(const QSharedPointer<QIODevice>& source, const QVariantMap& transferInfo): KJob()
UploadJob::UploadJob(const QSharedPointer<QIODevice>& source, const QString& deviceId): KJob()
{
// TODO: initialize in constructor
mInput = source;
@ -35,7 +35,7 @@ UploadJob::UploadJob(const QSharedPointer<QIODevice>& source, const QVariantMap&
mPort = 0;
// We will use this info if link is on ssl, to send encrypted payload
this->mTransferInfo = transferInfo;
this->mDeviceId = deviceId;
connect(mInput.data(), SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(mInput.data(), SIGNAL(aboutToClose()), this, SLOT(aboutToClose()));
@ -69,15 +69,13 @@ void UploadJob::newConnection()
mSocket = server->nextPendingConnection();
connect(mSocket, SIGNAL(disconnected()), mSocket, SLOT(deleteLater()));
if (mTransferInfo.value("useSsl", false).toBool()) {
mSocket->setLocalCertificate(KdeConnectConfig::instance()->certificate());
mSocket->setPrivateKey(KdeConnectConfig::instance()->privateKeyPath());
mSocket->setProtocol(QSsl::TlsV1_2);
mSocket->setPeerVerifyName(mTransferInfo.value("deviceId").toString());
mSocket->addCaCertificate(QSslCertificate(KdeConnectConfig::instance()->getTrustedDevice(mTransferInfo.value("deviceId").toString()).certificate.toLatin1()));
mSocket->startServerEncryption();
mSocket->waitForEncrypted();
}
mSocket->setLocalCertificate(KdeConnectConfig::instance()->certificate());
mSocket->setPrivateKey(KdeConnectConfig::instance()->privateKeyPath());
mSocket->setProtocol(QSsl::TlsV1_2);
mSocket->setPeerVerifyName(mDeviceId);
mSocket->addCaCertificate(QSslCertificate(KdeConnectConfig::instance()->getDeviceProperty(mDeviceId, "certificate", QString()).toLatin1()));
mSocket->startServerEncryption();
mSocket->waitForEncrypted();
readyRead();
}

View file

@ -34,7 +34,7 @@ class UploadJob
{
Q_OBJECT
public:
explicit UploadJob(const QSharedPointer<QIODevice>& source, const QVariantMap& sslInfo);
explicit UploadJob(const QSharedPointer<QIODevice>& source, const QString& deviceId);
virtual void start() override;
@ -45,7 +45,7 @@ private:
Server* mServer;
QSslSocket* mSocket;
quint16 mPort;
QVariantMap mTransferInfo;
QString mDeviceId;
private Q_SLOTS:
void readyRead();

View file

@ -36,7 +36,7 @@ QString LoopbackDeviceLink::name()
PairingHandler* LoopbackDeviceLink::createPairingHandler(Device *device)
{
return new LoopbackPairingHandler(device);
return new LoopbackPairingHandler(device->id());
}
bool LoopbackDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& input)
{

View file

@ -27,23 +27,23 @@
#include "loopbackpairinghandler.h"
#include "networkpackagetypes.h"
LoopbackPairingHandler::LoopbackPairingHandler(Device* device)
: LanPairingHandler(device)
LoopbackPairingHandler::LoopbackPairingHandler(const QString& deviceId)
: LanPairingHandler(deviceId)
{
}
}
bool LoopbackPairingHandler::requestPairing()
{
switch (m_pairStatus) {
switch (pairStatus()) {
case PairStatus::Paired:
Q_EMIT pairingFailed(i18n(m_deviceLink->name().append(" : Already paired").toLatin1().data()));
Q_EMIT pairingFailed(deviceLink()->name().append(" : Already paired").toLatin1().data());
return false;
case PairStatus ::Requested:
Q_EMIT pairingFailed(i18n(m_deviceLink->name().append(" : Pairing already requested for this device").toLatin1().data()));
Q_EMIT pairingFailed(deviceLink()->name().append(" : Pairing already requested for this device").toLatin1().data());
return false;
case PairStatus ::RequestedByPeer:
qCDebug(KDECONNECT_CORE) << m_deviceLink->name() << " : Pairing already started by the other end, accepting their request.";
qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request.";
acceptPairing();
return false;
case PairStatus::NotPaired:
@ -52,9 +52,9 @@ bool LoopbackPairingHandler::requestPairing()
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
m_pairStatus = PairStatus::Requested;
setPairStatus(PairStatus::Requested);
m_pairingTimeout.start();
bool success = m_deviceLink->sendPackage(np);
bool success = deviceLink()->sendPackage(np);
return success;
}
@ -62,20 +62,10 @@ bool LoopbackPairingHandler::acceptPairing()
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
m_pairStatus = PairStatus::Paired;
setAsPaired();
bool success = m_deviceLink->sendPackage(np);
Q_EMIT(pairingDone());
setPairStatus(PairStatus::Paired);
bool success = deviceLink()->sendPackage(np);
KdeConnectConfig::instance()->setDeviceProperty(m_deviceId, "publicKey", QString::fromLatin1(KdeConnectConfig::instance()->certificate().toPem()));
KdeConnectConfig::instance()->setDeviceProperty(m_deviceId, "certificate", QString::fromLatin1(KdeConnectConfig::instance()->certificate().toPem()));
return success;
}
void LoopbackPairingHandler::setAsPaired()
{
KdeConnectConfig::instance()->setDeviceProperty(m_device->id(), "publicKey", m_device->publicKey().toPEM());
KdeConnectConfig::instance()->setDeviceProperty(m_device->id(), "certificate", QString::fromLatin1(m_device->certificate().toPem()));
m_pairStatus = PairStatus::Paired;
m_pairingTimeout.stop(); // Just in case it is started
Q_EMIT(pairingDone());
}

View file

@ -30,15 +30,12 @@ class LoopbackPairingHandler
: public LanPairingHandler
{
public:
LoopbackPairingHandler(Device* device);
LoopbackPairingHandler(const QString& deviceId);
virtual ~LoopbackPairingHandler() { }
virtual bool requestPairing() Q_DECL_OVERRIDE;
virtual bool acceptPairing() Q_DECL_OVERRIDE;
private:
virtual void setAsPaired() Q_DECL_OVERRIDE;
};

View file

@ -20,9 +20,11 @@
#include "pairinghandler.h"
PairingHandler::PairingHandler(Device* device)
PairingHandler::PairingHandler()
: m_pairStatus(NotPaired)
, m_deviceLink(nullptr)
{
m_device = device;
}
void PairingHandler::setLink(DeviceLink *dl)
@ -37,4 +39,13 @@ void PairingHandler::linkDestroyed(QObject* o)
m_deviceLink = Q_NULLPTR;
Q_EMIT linkNull();
}
}
}
void PairingHandler::setPairStatus(PairingHandler::PairStatus status)
{
if (m_pairStatus != status) {
PairStatus oldStatus = m_pairStatus;
m_pairStatus = status;
Q_EMIT pairStatusChanged(status, oldStatus);
}
}

View file

@ -21,7 +21,6 @@
#ifndef KDECONNECT_PAIRINGHANDLER_H
#define KDECONNECT_PAIRINGHANDLER_H
#include "device.h"
#include "networkpackage.h"
#include "devicelink.h"
@ -41,8 +40,10 @@
class PairingHandler : public QObject
{
Q_OBJECT
protected:
public:
//TODO: Can we simplify this to just Paired/NotPaired, and leave the detailed status as an backend-specific thing?
enum PairStatus {
NotPaired,
Requested,
@ -50,18 +51,13 @@ protected:
Paired,
};
QTimer m_pairingTimeout;
Device* m_device;
DeviceLink* m_deviceLink; // We keep the latest link here, if this is destroyed without new link, linkDestroyed is emitted and device will destroy pairing handler
PairStatus m_pairStatus;
public:
PairingHandler(Device* device);
PairingHandler();
virtual ~PairingHandler() { }
void setLink(DeviceLink* dl);
bool isPaired() const { return m_pairStatus == PairStatus::Paired; };
bool pairRequested() const { return m_pairStatus == PairStatus::Requested; }
bool isPaired() const { return m_pairStatus == PairStatus::Paired; }
bool isPairRequested() const { return m_pairStatus == PairStatus::Requested; }
virtual void createPairPackage(NetworkPackage& np) = 0;
virtual void packageReceived(const NetworkPackage& np) = 0;
@ -70,12 +66,21 @@ public:
virtual void rejectPairing() = 0;
virtual void unpair() = 0;
protected:
DeviceLink* deviceLink() const;
void setPairStatus(PairStatus status);
PairStatus pairStatus() const;
public Q_SLOTS:
void linkDestroyed(QObject*);
virtual void pairingTimeout() = 0;
Q_SIGNALS:
void pairStatusChanged(PairStatus status, PairStatus previousStatus);
private:
virtual void setAsPaired() = 0;
DeviceLink* m_deviceLink; // We keep the latest link here, if this is destroyed without new link, linkDestroyed is emitted and device will destroy pairing handler
PairStatus m_pairStatus;
Q_SIGNALS:
void pairingDone();

View file

@ -123,7 +123,7 @@ void Daemon::removeDevice(Device* device)
void Daemon::cleanDevices()
{
Q_FOREACH(Device* device, d->mDevices) {
if (device->pairStatus() == Device::NotPaired && device->connectionSource() == DeviceLink::ConnectionStarted::Remotely) {
if (device->pairStatus() == PairingHandler::NotPaired && device->connectionSource() == DeviceLink::ConnectionStarted::Remotely) {
removeDevice(device);
}
}
@ -159,16 +159,6 @@ QStringList Daemon::devices(bool onlyReachable, bool onlyPaired) const
return ret;
}
QByteArray Daemon::certificate(int format) const
{
if (format == QSsl::Pem) {
return KdeConnectConfig::instance()->certificate().toPem();
} else {
return KdeConnectConfig::instance()->certificate().toDer();
}
}
void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* dl)
{
const QString& id = identityPackage.get<QString>("deviceId");

View file

@ -70,9 +70,6 @@ public Q_SLOTS:
//Returns a list of ids. The respective devices can be manipulated using the dbus path: "/modules/kdeconnect/Devices/"+id
Q_SCRIPTABLE QStringList devices(bool onlyReachable = false, bool onlyPaired = false) const;
//Exposing kdeconnectconfig through daemon, needed to show certificate hash in cli, but this can be extended to name, id, public key etc. if needed
Q_SCRIPTABLE QByteArray certificate(int format) const;
Q_SIGNALS:
Q_SCRIPTABLE void deviceAdded(const QString& id);
Q_SCRIPTABLE void deviceRemoved(const QString& id); //Note that paired devices will never be removed

View file

@ -49,14 +49,12 @@ Q_LOGGING_CATEGORY(KDECONNECT_CORE, "kdeconnect.core")
Device::Device(QObject* parent, const QString& id)
: QObject(parent)
, m_deviceId(id)
, m_pairStatus(Device::Paired)
, m_protocolVersion(NetworkPackage::ProtocolVersion) //We don't know it yet
{
KdeConnectConfig::DeviceInfo info = KdeConnectConfig::instance()->getTrustedDevice(id);
m_deviceName = info.deviceName;
m_deviceType = str2type(info.deviceType);
m_publicKey = QCA::RSAPublicKey::fromPEM(info.publicKey);
//Register in bus
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
@ -67,21 +65,12 @@ Device::Device(QObject* parent, const NetworkPackage& identityPackage, DeviceLin
, m_deviceId(identityPackage.get<QString>("deviceId"))
, m_deviceName(identityPackage.get<QString>("deviceName"))
, m_deviceType(str2type(identityPackage.get<QString>("deviceType")))
, m_pairStatus(Device::NotPaired)
, m_protocolVersion(identityPackage.get<int>("protocolVersion", -1))
{
addLink(identityPackage, dl);
//Register in bus
QDBusConnection::sessionBus().registerObject(dbusPath(), this, QDBusConnection::ExportScriptableContents | QDBusConnection::ExportAdaptors);
//Implement deprecated signals
connect(this, &Device::pairingChanged, this, [this](bool newPairing) {
if (newPairing)
Q_EMIT pairingSuccesful();
else
Q_EMIT unpaired();
});
}
Device::~Device()
@ -193,23 +182,20 @@ QString Device::pluginsConfigFile() const
return KdeConnectConfig::instance()->deviceConfigDir(id()).absoluteFilePath("config");
}
bool Device::pairRequested() const
bool Device::isPairRequested() const
{
bool isPairRequested = false;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers) {
isPairRequested = isPairRequested || ph->pairRequested();
isPairRequested = isPairRequested || ph->isPairRequested();
}
return isPairRequested;
}
void Device::requestPair()
{
switch(m_pairStatus) {
case Device::Paired:
Q_EMIT pairingFailed(i18n("Already paired"));
return;
case Device::NotPaired:
;
if (isPaired()) {
Q_EMIT pairingFailed(i18n("Already paired"));
return;
}
if (!isReachable()) {
@ -217,20 +203,14 @@ void Device::requestPair()
return;
}
//Send our own public key
bool success = false;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
success = success || ph->requestPairing(); // If one of many pairing handlers can successfully request pairing, consider it success
}
if (!success) {
m_pairStatus = Device::NotPaired;
Q_EMIT pairingFailed(i18n("Error contacting device"));
return;
}
if (m_pairStatus == Device::Paired) {
return;
}
}
void Device::unpair()
@ -238,23 +218,23 @@ void Device::unpair()
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
ph->unpair();
}
unpairInternal(); // We do not depend on pairing handlers for unpairing, this method is likely to be called due to user events
}
void Device::unpairInternal()
void Device::pairStatusChanged(PairingHandler::PairStatus status, PairingHandler::PairStatus oldStatus)
{
bool alreadyUnpaired = (m_pairStatus != Device::Paired);
m_pairStatus = Device::NotPaired;
KdeConnectConfig::instance()->removeTrustedDevice(id());
reloadPlugins(); //Will unload the plugins
if (!alreadyUnpaired) {
Q_EMIT pairingChanged(false);
if (oldStatus == PairingHandler::Paired && status == PairingHandler::NotPaired) {
KdeConnectConfig::instance()->removeTrustedDevice(m_deviceId);
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
ph->unpair();
}
}
}
void Device::pairingTimeout()
{
Q_EMIT pairingFailed(i18n("Timed out"));
if (oldStatus == PairingHandler::NotPaired && status == PairingHandler::Paired) {
reloadPlugins(); //Will unload the plugins
}
Q_EMIT pairingChanged(status == PairingHandler::Paired);
}
static bool lessThan(DeviceLink* p1, DeviceLink* p2)
@ -280,12 +260,6 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link)
setName(identityPackage.get<QString>("deviceName"));
m_deviceType = str2type(identityPackage.get<QString>("deviceType"));
// Set certificate if the link is on ssl, and it is added to identity package
// This is always sets certificate when link is added to device
if (identityPackage.has("certificate")) {
m_certificate = QSslCertificate(identityPackage.get<QByteArray>("certificate"));
}
//Theoretically we will never add two links from the same provider (the provider should destroy
//the old one before this is called), so we do not have to worry about destroying old links.
//-- Actually, we should not destroy them or the provider will store an invalid ref!
@ -311,8 +285,7 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link)
PairingHandler* pairingHandler = link->createPairingHandler(this);
m_pairingHandlers.insert(link->name(), pairingHandler);
connect(m_pairingHandlers[link->name()], SIGNAL(linkNull()), this, SLOT(destroyPairingHandler()));
connect(m_pairingHandlers[link->name()], SIGNAL(pairingDone()), this, SLOT(setAsPaired()));
connect(m_pairingHandlers[link->name()], SIGNAL(unpairingDone()), this, SLOT(unpairInternal()));
connect(m_pairingHandlers[link->name()], SIGNAL(pairStatusChanged(PairStatus, PairStatus)), this, SLOT(pairStatusChanged(PairStatus, PairStatus)));
connect(m_pairingHandlers[link->name()], SIGNAL(pairingFailed(const QString&)), this, SIGNAL(pairingFailed(const QString&)));
}
m_pairingHandlers[link->name()]->setLink(link);
@ -393,7 +366,7 @@ void Device::rejectPairing()
{
qCDebug(KDECONNECT_CORE) << "Rejected pairing";
m_pairStatus = Device::NotPaired;
m_pairStatus = PairingHandler::NotPaired;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
ph->rejectPairing();
@ -408,35 +381,20 @@ void Device::acceptPairing()
qCDebug(KDECONNECT_CORE) << "Accepted pairing";
bool success = false;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
success = success || ph->acceptPairing();
}
if (!success) {
m_pairStatus = Device::NotPaired;
return;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers) {
success = success || ph->acceptPairing();
}
}
void Device::setAsPaired()
{
bool alreadyPaired = (m_pairStatus == Device::Paired);
m_pairStatus = Device::Paired;
//Save device info in the config
KdeConnectConfig::instance()->addTrustedDevice(id(), name(), type2str(m_deviceType));
reloadPlugins(); //Will actually load the plugins
if (!alreadyPaired) {
Q_EMIT pairingChanged(true);
void Device::isPaired() {
Q_FOREACH(PairingHandler* ph, m_pairingHandlers) {
if (ph->isPaired()) return true;
}
return false;
}
DeviceLink::ConnectionStarted Device::connectionSource() const
{
DeviceLink::ConnectionStarted ret = DeviceLink::Remotely;
@ -535,3 +493,24 @@ bool Device::isPluginEnabled(const QString& pluginName) const
return (pluginStates.hasKey(enabledKey) ? pluginStates.readEntry(enabledKey, false)
: PluginLoader::instance()->getPluginInfo(pluginName).isEnabledByDefault());
}
//HACK
QString Device::encryptionInfo() const
{
QString result;
QByteArray myCertificate = KdeConnectConfig::instance()->certificate().toDer();
for (int i=2 ; i<myCertificate.size() ; i+=3) {
myCertificate.insert(i, ':'); // Improve readability
}
result += i18n("SHA1 fingerprint of your device certificate is : ") + myCertificate + endl;
QString remoteCertificate = KdeConnectConfig::instance()->getDeviceProperty(id(), "certificate");
for (int i=2 ; i<remoteCertificate.size() ; i+=3) {
remoteCertificate.insert(i, ':'); // Improve readability
}
result += i18n("SHA1 fingerprint of remote device certificate is : ") << remoteCertificate << endl;
return result;
}

View file

@ -30,12 +30,12 @@
#include <QSslCertificate>
#include <QTimer>
#include "backends/pairinghandler.h"
#include "networkpackage.h"
#include "backends/devicelink.h"
class DeviceLink;
class KdeConnectPlugin;
class PairingHandler;
class KDECONNECTCORE_EXPORT Device
: public QObject
@ -51,10 +51,6 @@ class KDECONNECTCORE_EXPORT Device
Q_PROPERTY(QStringList unsupportedPlugins READ unsupportedPlugins NOTIFY pluginsChanged)
public:
enum PairStatus {
NotPaired,
Paired,
};
enum DeviceType {
Unknown,
@ -84,22 +80,17 @@ public:
QString name() const { return m_deviceName; }
QString dbusPath() const { return "/modules/kdeconnect/devices/"+id(); }
QString type() const { return type2str(m_deviceType); }
QCA::PublicKey publicKey() const { return m_publicKey; }
QSslCertificate certificate() const { return m_certificate; }
Q_SCRIPTABLE QByteArray certificate(int format) const { return (format == QSsl::Pem) ? m_certificate.toPem() : m_certificate.toDer() ;} // To expose certificate through dbus for cli
QString iconName() const;
QString statusIconName() const;
QStringList unsupportedPlugins() const { return m_unsupportedPlugins; }
QString encryptionInfo() const;
//Add and remove links
void addLink(const NetworkPackage& identityPackage, DeviceLink*);
void removeLink(DeviceLink*);
// Setter for public key after pairing, since it is handled by pairinghandler now
void setPublicKey(QCA::PublicKey publicKey) { m_publicKey = publicKey; }
Q_SCRIPTABLE bool isPaired() const { return m_pairStatus==Device::Paired; }
Q_SCRIPTABLE bool pairRequested() const;
Q_SCRIPTABLE bool isPaired() const;
Q_SCRIPTABLE bool isPairRequested() const;
Q_SCRIPTABLE QStringList availableLinks() const;
bool isReachable() const { return !m_deviceLinks.isEmpty(); }
@ -109,13 +100,11 @@ public:
Q_SCRIPTABLE QString pluginsConfigFile() const;
void pairingTimeout();
KdeConnectPlugin* plugin(const QString& pluginName) const;
void setPluginEnabled(const QString& pluginName, bool enabled);
bool isPluginEnabled(const QString& pluginName) const;
PairStatus pairStatus() const;
PairingHandler::PairStatus pairStatus() const;
DeviceLink::ConnectionStarted connectionSource() const;
@ -136,8 +125,8 @@ private Q_SLOTS:
void linkDestroyed(QObject* o);
void destroyPairingHandler();
void setAsPaired();
void unpairInternal();
void pairStatusChanged(PairingHandler::PairStatus current, PairingHandler::PairStatus old);
Q_SIGNALS:
Q_SCRIPTABLE void pluginsChanged();
@ -146,9 +135,6 @@ Q_SIGNALS:
Q_SCRIPTABLE void pairingFailed(const QString& error);
Q_SCRIPTABLE void nameChanged(const QString& name);
QT_DEPRECATED Q_SCRIPTABLE void pairingSuccesful();
QT_DEPRECATED Q_SCRIPTABLE void unpaired();
private: //Methods
static DeviceType str2type(const QString &deviceType);
static QString type2str(DeviceType deviceType);
@ -160,9 +146,6 @@ private: //Fields (TODO: dPointer!)
const QString m_deviceId;
QString m_deviceName;
DeviceType m_deviceType;
QCA::PublicKey m_publicKey;
QSslCertificate m_certificate;
PairStatus m_pairStatus;
int m_protocolVersion;
QVector<DeviceLink*> m_deviceLinks;
@ -171,7 +154,6 @@ private: //Fields (TODO: dPointer!)
QMultiMap<QString, KdeConnectPlugin*> m_pluginsByIncomingInterface;
QMultiMap<QString, KdeConnectPlugin*> m_pluginsByOutgoingInterface;
QTimer m_pairingTimeut;
QSet<QString> m_incomingCapabilities;
QSet<QString> m_outgoingCapabilities;
QStringList m_supportedIncomingInterfaces;

View file

@ -32,6 +32,8 @@
#include <QHostInfo>
#include <QSettings>
#include <QSslCertificate>
#include <QtCrypto>
#include <QSslCertificate>
#include "core_debug.h"
#include "dbushelper.h"
@ -217,13 +219,12 @@ QStringList KdeConnectConfig::trustedDevices()
}
void KdeConnectConfig::addTrustedDevice(const QString &id, const QString &name, const QString &type, const QString &publicKey)
void KdeConnectConfig::addTrustedDevice(const QString &id, const QString &name, const QString &type)
{
d->config->beginGroup("trustedDevices");
d->config->beginGroup(id);
d->config->setValue("name", name);
d->config->setValue("type", type);
d->config->setValue("publicKey", publicKey);
d->config->endGroup();
d->config->endGroup();
d->config->sync();
@ -239,8 +240,6 @@ KdeConnectConfig::DeviceInfo KdeConnectConfig::getTrustedDevice(const QString &i
KdeConnectConfig::DeviceInfo info;
info.deviceName = d->config->value("name", QLatin1String("unnamed")).toString();
info.deviceType = d->config->value("type", QLatin1String("unknown")).toString();
info.publicKey = d->config->value("publicKey", QString()).toString();
info.certificate = d->config->value("certificate", QString()).toString();
d->config->endGroup();
d->config->endGroup();

View file

@ -21,20 +21,22 @@
#ifndef KDECONNECTCONFIG_H
#define KDECONNECTCONFIG_H
#include <QtCrypto>
#include <QDir>
#include <QSslCertificate>
#include "kdeconnectcore_export.h"
class QSslCertificate;
namespace QCA {
class PrivateKey;
class PublicKey;
}
class KDECONNECTCORE_EXPORT KdeConnectConfig
{
public:
struct DeviceInfo {
QString deviceName;
QString deviceType;
QString publicKey;
QString certificate;
};
static KdeConnectConfig* instance();
@ -62,12 +64,12 @@ public:
QStringList trustedDevices(); //list of ids
void removeTrustedDevice(const QString &id);
void addTrustedDevice(const QString &id, const QString &name, const QString &type, const QString &publicKey);
void addTrustedDevice(const QString &id, const QString &name, const QString &type);
KdeConnectConfig::DeviceInfo getTrustedDevice(const QString &id);
void setDeviceProperty(QString deviceId, QString name, QString value);
QString getDeviceProperty(QString deviceId, QString name, QString defaultValue = QString());
/*
* Paths for config files, there is no guarantee the directories already exist
*/

View file

@ -180,7 +180,7 @@ void KdeConnectKcm::deviceSelected(const QModelIndex& current)
}
kcmUi->messages->setVisible(false);
if (currentDevice->pairRequested()) {
if (currentDevice->isPairRequested()) {
kcmUi->progressBar->setVisible(true);
kcmUi->unpair_button->setVisible(false);
kcmUi->pair_button->setVisible(false);