Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 2824e736 authored by Vineet Garg's avatar Vineet Garg

Pairing handler implemented

parent 27515546
......@@ -18,10 +18,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <kdeconnectconfig.h>
#include <KLocalizedString>
#include "core_debug.h"
#include "daemon.h"
#include "kdeconnectconfig.h"
#include "landevicelink.h"
#include "lanpairinghandler.h"
#include "networkpackagetypes.h"
#include "landevicelink.h"
LanPairingHandler::LanPairingHandler(Device* device)
: PairingHandler(device)
......@@ -30,34 +34,110 @@ LanPairingHandler::LanPairingHandler(Device* device)
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
}
} else {
m_pairStatus = PairStatus::NotPaired;
}
}
void LanPairingHandler::createPairPackage(NetworkPackage& np)
{
np.set("link", m_deviceLink->name());
np.set("pair", true);
np.set("publicKey", KdeConnectConfig::instance()->publicKey().toPEM());
}
bool LanPairingHandler::packageReceived(const NetworkPackage& np)
void LanPairingHandler::packageReceived(const NetworkPackage& np)
{
if (!np.get<QString>("link").compare(m_deviceLink->name())) return; // If this package is not received by my type of link
m_pairingTimeout.stop();
//Retrieve their public key
QString keyString = np.get<QString>("publicKey");
m_device->setPublicKey(QCA::RSAPublicKey::fromPEM(keyString));
if (m_device->publicKey().isNull()) {
return false;
bool wantsPair = np.get<bool>("pair");
if (wantsPair == isPaired()) {
// qCDebug(KDECONNECT_CORE) << "Already" << (wantsPair? "paired":"unpaired");
if (m_pairStatus == PairStatus ::Requested) {
m_pairStatus = PairStatus ::NotPaired;
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
} else if (m_pairStatus == PairStatus ::Paired) {
// Auto accept pairing for the link if device is paired
acceptPairing();
}
return;
}
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;
}
Q_EMIT pairingFailed(i18n("Received incorrect key"));
return;
}
if (m_pairStatus == PairStatus::Requested) { //We started pairing
qCDebug(KDECONNECT_CORE) << "Pair answer";
setAsPaired();
} else {
qCDebug(KDECONNECT_CORE) << "Pair request";
Daemon::instance()->requestPairing(m_device);
m_pairStatus = PairStatus ::RequestedByPeer;
}
} else {
qCDebug(KDECONNECT_CORE) << "Unpair request";
PairStatus prevPairStatus = m_pairStatus;
m_pairStatus = PairStatus::NotPaired;
if (prevPairStatus == PairStatus ::Requested) {
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
} else if (prevPairStatus == PairStatus::Paired) {
Q_EMIT (unpairingDone());
}
}
return true;
}
bool LanPairingHandler::requestPairing()
{
switch (m_pairStatus) {
case PairStatus::Paired:
Q_EMIT pairingFailed(i18n(m_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()));
return false;
case PairStatus ::RequestedByPeer:
qCDebug(KDECONNECT_CORE) << m_deviceLink->name() << " : Pairing already started by the other end, accepting their request.";
acceptPairing();
return false;
case PairStatus::NotPaired:
;
}
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
bool success;
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
success = dl->sendPackage(np);
}
success = m_deviceLink->sendPackage(np);
if (success) m_pairStatus = PairStatus::Requested;
m_pairingTimeout.start();
return success;
}
......@@ -67,45 +147,48 @@ bool LanPairingHandler::acceptPairing()
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
bool success;
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
success = dl->sendPackage(np);
success = m_deviceLink->sendPackage(np);
if (success) {
m_pairStatus = PairStatus::Paired;
setAsPaired();
Q_EMIT(pairingDone());
}
return success;
}
void LanPairingHandler::rejectPairing()
{
// TODO : check status of reject pairing
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
dl->sendPackage(np);
}
}
void LanPairingHandler::pairingDone()
{
// No need to worry, if either of certificate or public key is null an empty qstring will be returned
KdeConnectConfig::instance()->setDeviceProperty(m_device->id(), "key", m_device->publicKey().toPEM());
KdeConnectConfig::instance()->setDeviceProperty(m_device->id(), "certificate", QString(m_device->certificate().toPem()));
m_pairingTimeout.stop(); // Just in case it is started
m_deviceLink->sendPackage(np);
m_pairStatus = PairStatus::NotPaired;
}
void LanPairingHandler::unpair() {
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
dl->sendPackage(np);
}
m_deviceLink->sendPackage(np);
m_pairStatus = PairStatus::NotPaired;
Q_EMIT(unpairingDone()); // There will be multiple signals if there are multiple pairing handlers
}
void LanPairingHandler::pairingTimeout()
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
dl->sendPackage(np);
}
m_device->pairingTimeout();
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());
}
......@@ -21,35 +21,31 @@
#ifndef KDECONNECT_LANPAIRINGHANDLER_H
#define KDECONNECT_LANPAIRINGHANDLER_H
#include <QTimer>
#include <backends/devicelink.h>
#include "../../device.h"
#include "device.h"
#include "../devicelink.h"
#include "../pairinghandler.h"
// This class is used pairing related stuff. It has direct access to links and can directly send packages
class LanPairingHandler
: public PairingHandler
{
private:
QTimer m_pairingTimeout;
private Q_SLOTS:
void pairingTimeout();
public:
LanPairingHandler(Device* device);
virtual ~LanPairingHandler() { }
virtual void createPairPackage(NetworkPackage& np) Q_DECL_OVERRIDE;
virtual bool packageReceived(const NetworkPackage& np) Q_DECL_OVERRIDE;
virtual void packageReceived(const NetworkPackage& np) Q_DECL_OVERRIDE;
virtual bool requestPairing() Q_DECL_OVERRIDE;
virtual bool acceptPairing() Q_DECL_OVERRIDE;
virtual void rejectPairing() Q_DECL_OVERRIDE;
virtual void pairingDone() Q_DECL_OVERRIDE;
virtual void unpair() Q_DECL_OVERRIDE;
public Q_SLOTS:
virtual void pairingTimeout();
private:
virtual void setAsPaired() Q_DECL_OVERRIDE;
};
......
......@@ -25,16 +25,16 @@ PairingHandler::PairingHandler(Device* device)
m_device = device;
}
void PairingHandler::addLink(DeviceLink *dl)
void PairingHandler::setLink(DeviceLink *dl)
{
m_deviceLinks.append(dl);
m_deviceLink = dl;
}
void PairingHandler::linkDestroyed(QObject* o)
{
DeviceLink* dl = static_cast<DeviceLink*>(o);
m_deviceLinks.removeOne(dl);
if (m_deviceLinks.isEmpty()) {
Q_EMIT noLinkAvailable();
if (dl == m_deviceLink) { // Check if same link is destroyed
m_deviceLink = Q_NULLPTR;
Q_EMIT linkNull();
}
}
\ No newline at end of file
......@@ -25,31 +25,52 @@
#include "networkpackage.h"
#include "devicelink.h"
#include <QTimer>
class PairingHandler : public QObject
{
Q_OBJECT
protected:
enum PairStatus {
NotPaired,
Requested,
RequestedByPeer,
Paired,
};
QTimer m_pairingTimeout;
Device* m_device;
QVector<DeviceLink*> m_deviceLinks;
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);
virtual ~PairingHandler() { }
void addLink(DeviceLink* dl);
void setLink(DeviceLink* dl);
bool isPaired() const { return m_pairStatus == PairStatus::Paired; };
bool pairRequested() const { return m_pairStatus == PairStatus::Requested; }
virtual void createPairPackage(NetworkPackage& np) = 0;
virtual bool packageReceived(const NetworkPackage& np) = 0;
virtual void packageReceived(const NetworkPackage& np) = 0;
virtual bool requestPairing() = 0;
virtual bool acceptPairing() = 0;
virtual void rejectPairing() = 0;
virtual void pairingDone() = 0;
virtual void unpair() = 0;
public Q_SLOTS:
void linkDestroyed(QObject*);
virtual void pairingTimeout() = 0;
private:
virtual void setAsPaired() = 0;
Q_SIGNALS:
void noLinkAvailable();
void pairingDone();
void unpairingDone();
void pairingFailed(const QString& error);
void linkNull();
};
......
......@@ -179,19 +179,21 @@ QString Device::pluginsConfigFile() const
return KdeConnectConfig::instance()->deviceConfigDir(id()).absoluteFilePath("config");
}
bool Device::pairRequested() const
{
bool pr = false;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers) {
pr = pr || ph->pairRequested();
}
return pr;
}
void Device::requestPair()
{
switch(m_pairStatus) {
case Device::Paired:
Q_EMIT pairingFailed(i18n("Already paired"));
return;
case Device::Requested:
Q_EMIT pairingFailed(i18n("Pairing already requested for this device"));
return;
case Device::RequestedByPeer:
qCDebug(KDECONNECT_CORE) << "Pairing already started by the other end, accepting their request.";
acceptPairing();
return;
case Device::NotPaired:
;
}
......@@ -201,17 +203,15 @@ void Device::requestPair()
return;
}
m_pairStatus = Device::Requested;
//Send our own public key
bool success = false;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
bool success = ph->requestPairing();
if (!success) {
m_pairStatus = Device::NotPaired;
Q_EMIT pairingFailed(i18n("Error contacting device"));
return;
}
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) {
......@@ -221,12 +221,9 @@ void Device::requestPair()
void Device::unpair()
{
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
ph->unpair();
}
unpairInternal();
}
void Device::unpairInternal()
......@@ -297,9 +294,12 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link)
if (!m_pairingHandlers.contains(link->name())) {
PairingHandler* pairingHandler = link->createPairingHandler(this);
m_pairingHandlers.insert(link->name(), pairingHandler);
connect(m_pairingHandlers[link->name()], SIGNAL(noLinkAvailable()), this, SLOT(destroyPairingHandler()));
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(pairingFailed(const QString&)), this, SIGNAL(pairingFailed(const QString&)));
}
m_pairingHandlers[link->name()]->addLink(link);
m_pairingHandlers[link->name()]->setLink(link);
connect(link, SIGNAL(destroyed(QObject*)), m_pairingHandlers[link->name()], SLOT(linkDestroyed(QObject*)));
}
......@@ -347,63 +347,9 @@ void Device::privateReceivedPackage(const NetworkPackage& np)
{
if (np.type() == PACKAGE_TYPE_PAIR) {
//qCDebug(KDECONNECT_CORE) << "Pair package";
bool wantsPair = np.get<bool>("pair");
if (wantsPair == isPaired()) {
qCDebug(KDECONNECT_CORE) << "Already" << (wantsPair? "paired":"unpaired");
if (m_pairStatus == Device::Requested) {
m_pairStatus = Device::NotPaired;
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
} else if (m_pairStatus == Device::Paired) {
// If other request's pairing, and we have pair status Paired, send accept pairing
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
ph->acceptPairing();
}
}
return;
}
if (wantsPair) {
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
bool success = ph->packageReceived(np);
if (!success) {
if (m_pairStatus == Device::Requested) {
m_pairStatus = Device::NotPaired;
}
Q_EMIT pairingFailed(i18n("Received incorrect key"));
return;
}
}
if (m_pairStatus == Device::Requested) { //We started pairing
qCDebug(KDECONNECT_CORE) << "Pair answer";
setAsPaired();
} else {
qCDebug(KDECONNECT_CORE) << "Pair request";
Daemon::instance()->requestPairing(this);
m_pairStatus = Device::RequestedByPeer;
}
} else {
qCDebug(KDECONNECT_CORE) << "Unpair request";
PairStatus prevPairStatus = m_pairStatus;
m_pairStatus = Device::NotPaired;
if (prevPairStatus == Device::Requested) {
Q_EMIT pairingFailed(i18n("Canceled by other peer"));
} else if (prevPairStatus == Device::Paired) {
unpairInternal();
}
// If PACKAGE_TYPE_PAIR, send it to pairing handlers without thinking
Q_FOREACH(PairingHandler* ph, m_pairingHandlers) {
ph->packageReceived(np);
}
} else if (isPaired()) {
......@@ -413,21 +359,11 @@ void Device::privateReceivedPackage(const NetworkPackage& np)
}
} else {
qCDebug(KDECONNECT_CORE) << "device" << name() << "not paired, ignoring package" << np.type();
if (m_pairStatus != Device::Requested)
unpair();
unpair();
}
}
bool Device::sendOwnPublicKey()
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", true);
np.set("publicKey", KdeConnectConfig::instance()->publicKey().toPEM());
bool success = sendPackage(np);
return success;
}
void Device::rejectPairing()
{
qCDebug(KDECONNECT_CORE) << "Rejected pairing";
......@@ -444,13 +380,11 @@ void Device::rejectPairing()
void Device::acceptPairing()
{
if (m_pairStatus != Device::RequestedByPeer) return;
qCDebug(KDECONNECT_CORE) << "Accepted pairing";
bool success;
bool success = false;
Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) {
success = ph->acceptPairing();
success = success || ph->acceptPairing();
}
if (!success) {
......@@ -458,8 +392,6 @@ void Device::acceptPairing()
return;
}
setAsPaired();
}
void Device::setAsPaired()
......@@ -470,7 +402,7 @@ void Device::setAsPaired()
m_pairStatus = Device::Paired;
//Save device info in the config
KdeConnectConfig::instance()->addTrustedDevice(id(), name(), type2str(m_deviceType), m_publicKey.toPEM(), QString::fromLatin1(m_certificate.toPem()));
KdeConnectConfig::instance()->addTrustedDevice(id(), name(), type2str(m_deviceType));
reloadPlugins(); //Will actually load the plugins
......
......@@ -49,8 +49,6 @@ class KDECONNECTCORE_EXPORT Device
enum PairStatus {
NotPaired,
Requested,
RequestedByPeer,
Paired,
};
......@@ -99,7 +97,7 @@ public:
void setPublicKey(QCA::PublicKey publicKey) { m_publicKey = publicKey; }
Q_SCRIPTABLE bool isPaired() const { return m_pairStatus==Device::Paired; }
Q_SCRIPTABLE bool pairRequested() const { return m_pairStatus==Device::Requested; }
Q_SCRIPTABLE bool pairRequested() const;
Q_SCRIPTABLE QStringList availableLinks() const;
bool isReachable() const { return !m_deviceLinks.isEmpty(); }
......@@ -128,6 +126,9 @@ private Q_SLOTS:
void linkDestroyed(QObject* o);
void destroyPairingHandler();
void setAsPaired();
void unpairInternal();
Q_SIGNALS:
Q_SCRIPTABLE void pluginsChanged();
Q_SCRIPTABLE void reachableStatusChanged();
......@@ -141,9 +142,6 @@ Q_SIGNALS:
private: //Methods
void setName(const QString &name);
QString iconForStatus(bool reachable, bool paired) const;
void unpairInternal();
void setAsPaired();
bool sendOwnPublicKey();
private: //Fields (TODO: dPointer!)
const QString m_deviceId;
......
......@@ -116,7 +116,7 @@ KdeConnectConfig::KdeConnectConfig()
} else {
QCA::CertificateOptions certificateOptions = QCA::CertificateOptions();
// TODO : Set serial number for certificate. Time millis or any constant number?
// FIXME : Set serial number for certificate. Time millis or any constant number?
QCA::BigInteger bigInteger(10);
QDateTime startTime = QDateTime::currentDateTime();
QDateTime endTime = startTime.addYears(10);
......@@ -218,14 +218,12 @@ QStringList KdeConnectConfig::trustedDevices()
return list;
}
void KdeConnectConfig::addTrustedDevice(QString id, QString name, QString type, QString publicKey, QString certificate)
void KdeConnectConfig::addTrustedDevice(QString id, QString name, 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->setValue("certificate", certificate);
d->config->endGroup();
d->config->endGroup();
d->config->sync();
......
......@@ -62,7 +62,7 @@ public:
QStringList trustedDevices(); //list of ids
void removeTrustedDevice(QString id);
void addTrustedDevice(QString id, QString name, QString type, QString publicKey, QString certificate = QString());
void addTrustedDevice(QString id, QString name, QString type);
KdeConnectConfig::DeviceInfo getTrustedDevice(QString id);
void setDeviceProperty(QString deviceId, QString name, QString value);
QString getDeviceProperty(QString deviceId, QString name, QString defaultValue = QString());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment