kdeconnect-kde/core/backends/lan/lanpairinghandler.cpp

194 lines
6.2 KiB
C++
Raw Normal View History

2015-07-05 14:23:53 +01:00
/**
* Copyright 2015 Vineet Garg <grg.vineet@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2015-07-27 16:28:58 +01:00
#include <KLocalizedString>
#include "core_debug.h"
#include "daemon.h"
#include "kdeconnectconfig.h"
#include "landevicelink.h"
2015-07-05 14:23:53 +01:00
#include "lanpairinghandler.h"
2015-07-09 22:51:08 +01:00
#include "networkpackagetypes.h"
2015-07-05 14:23:53 +01:00
2015-11-30 18:36:01 +00:00
LanPairingHandler::LanPairingHandler(const QString& deviceId)
: PairingHandler()
, m_deviceId(deviceId)
2015-12-01 18:45:14 +00:00
, m_status(NotPaired)
2015-07-14 13:04:04 +01:00
{
m_pairingTimeout.setSingleShot(true);
m_pairingTimeout.setInterval(30 * 1000); //30 seconds of timeout
2015-12-06 01:11:50 +00:00
connect(&m_pairingTimeout, &QTimer::timeout, this, &LanPairingHandler::pairingTimeout);
2015-07-05 14:23:53 +01:00
}
2015-07-14 13:04:04 +01:00
void LanPairingHandler::createPairPackage(NetworkPackage& np)
{
2015-11-30 18:36:01 +00:00
np.set("link", deviceLink()->name());
2015-07-09 22:51:08 +01:00
np.set("pair", true);
np.set("publicKey", KdeConnectConfig::instance()->publicKey().toPEM());
2015-07-05 14:23:53 +01:00
}
2015-07-27 16:28:58 +01:00
void LanPairingHandler::packageReceived(const NetworkPackage& np)
2015-07-14 13:04:04 +01:00
{
2015-07-27 16:28:58 +01:00
2015-11-30 18:36:01 +00:00
if (np.get<QString>("link", deviceLink()->name()).compare(deviceLink()->name()) != 0) return; // If this package is not received by my type of link
2015-07-27 16:28:58 +01:00
m_pairingTimeout.stop();
2015-07-27 16:28:58 +01:00
bool wantsPair = np.get<bool>("pair");
if (wantsPair == isPaired()) {
// qCDebug(KDECONNECT_CORE) << "Already" << (wantsPair? "paired":"unpaired");
2015-11-30 18:36:01 +00:00
if (isPairRequested()) {
2015-12-01 18:45:14 +00:00
setInternalPairStatus(NotPaired);
Q_EMIT pairingError(i18n("Canceled by other peer"));
2015-08-13 10:26:28 +01:00
return;
2015-11-30 18:36:01 +00:00
} else if (isPaired()) {
2015-08-13 10:26:28 +01:00
/**
* 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
*/
2015-11-30 18:36:01 +00:00
KdeConnectConfig::instance()->removeTrustedDevice(m_deviceId);
2015-12-01 18:45:14 +00:00
setInternalPairStatus(NotPaired);
2015-07-27 16:28:58 +01:00
}
}
if (wantsPair) {
QString keyString = np.get<QString>("publicKey");
2015-11-30 18:36:01 +00:00
QString certificateString = np.get<QByteArray>("certificate");
if (QCA::RSAPublicKey::fromPEM(keyString).isNull()) {
if (isPairRequested()) {
2015-12-01 18:45:14 +00:00
setInternalPairStatus(NotPaired);
2015-07-27 16:28:58 +01:00
}
2015-12-01 18:45:14 +00:00
Q_EMIT pairingError(i18n("Received incorrect key"));
2015-07-27 16:28:58 +01:00
return;
}
2015-11-30 18:36:01 +00:00
if (isPairRequested()) { //We started pairing
2015-07-27 16:28:58 +01:00
qCDebug(KDECONNECT_CORE) << "Pair answer";
2015-11-30 18:36:01 +00:00
KdeConnectConfig::instance()->setDeviceProperty(m_deviceId, "publicKey", keyString);
KdeConnectConfig::instance()->setDeviceProperty(m_deviceId, "certificate", certificateString);
2015-07-27 16:28:58 +01:00
} else {
qCDebug(KDECONNECT_CORE) << "Pair request";
2015-11-30 18:36:01 +00:00
if (isPaired()) {
2015-08-13 10:26:28 +01:00
acceptPairing();
return;
}
2015-12-02 19:04:35 +00:00
Daemon::instance()->askPairingConfirmation(this);
2015-12-01 18:45:14 +00:00
setInternalPairStatus(RequestedByPeer);
2015-07-27 16:28:58 +01:00
}
2015-11-30 18:36:01 +00:00
} else { //wantsPair == false
2015-07-27 16:28:58 +01:00
qCDebug(KDECONNECT_CORE) << "Unpair request";
2015-11-30 18:36:01 +00:00
if (isPairRequested()) {
2015-12-01 18:45:14 +00:00
Q_EMIT pairingError(i18n("Canceled by other peer"));
2015-07-27 16:28:58 +01:00
}
2015-12-01 18:45:14 +00:00
setInternalPairStatus(NotPaired);
2015-07-09 22:51:08 +01:00
}
2015-07-05 14:23:53 +01:00
}
bool LanPairingHandler::requestPairing()
2015-07-14 13:04:04 +01:00
{
2015-12-01 18:45:14 +00:00
switch (m_status) {
case Paired:
2015-12-02 19:04:35 +00:00
Q_EMIT pairingError(i18n("%1: Already paired", deviceLink()->name()));
2015-07-27 16:28:58 +01:00
return false;
2015-12-01 18:45:14 +00:00
case Requested:
2015-12-02 19:04:35 +00:00
Q_EMIT pairingError(i18n("%1: Pairing already requested for this device", deviceLink()->name()));
2015-07-27 16:28:58 +01:00
return false;
2015-12-01 18:45:14 +00:00
case RequestedByPeer:
2015-11-30 18:36:01 +00:00
qCDebug(KDECONNECT_CORE) << deviceLink()->name() << " : Pairing already started by the other end, accepting their request.";
2015-07-27 16:28:58 +01:00
acceptPairing();
return false;
2015-12-01 18:45:14 +00:00
case NotPaired:
2015-07-27 16:28:58 +01:00
;
}
2015-07-14 13:04:04 +01:00
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
bool success;
2015-11-30 18:36:01 +00:00
success = deviceLink()->sendPackage(np);
if (success) {
2015-12-01 18:45:14 +00:00
setInternalPairStatus(Requested);
m_pairingTimeout.start();
}
2015-07-09 22:51:08 +01:00
return success;
2015-07-05 14:23:53 +01:00
}
bool LanPairingHandler::acceptPairing()
2015-07-14 13:04:04 +01:00
{
2015-11-30 18:36:01 +00:00
m_pairingTimeout.stop(); // Just in case it is started
2015-07-14 13:04:04 +01:00
NetworkPackage np(PACKAGE_TYPE_PAIR);
createPairPackage(np);
bool success;
2015-11-30 18:36:01 +00:00
success = deviceLink()->sendPackage(np);
2015-07-27 16:28:58 +01:00
if (success) {
2015-12-01 18:45:14 +00:00
setInternalPairStatus(Paired);
}
2015-07-09 22:51:08 +01:00
return success;
2015-07-05 14:23:53 +01:00
}
void LanPairingHandler::rejectPairing()
2015-07-14 13:04:04 +01:00
{
2015-07-09 22:51:08 +01:00
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
2015-11-30 18:36:01 +00:00
np.set("link", deviceLink()->name());
deviceLink()->sendPackage(np);
2015-12-01 18:45:14 +00:00
setInternalPairStatus(NotPaired);
2015-07-09 22:51:08 +01:00
}
2015-07-05 14:23:53 +01:00
void LanPairingHandler::unpair() {
2015-07-09 22:51:08 +01:00
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
2015-11-30 18:36:01 +00:00
np.set("link", deviceLink()->name());
deviceLink()->sendPackage(np);
2015-12-01 18:45:14 +00:00
setInternalPairStatus(NotPaired);
}
void LanPairingHandler::pairingTimeout()
{
NetworkPackage np(PACKAGE_TYPE_PAIR);
np.set("pair", false);
2015-11-30 18:36:01 +00:00
np.set("name", deviceLink()->name());
deviceLink()->sendPackage(np);
2015-12-01 18:45:14 +00:00
setInternalPairStatus(NotPaired); //Will emit the change as well
Q_EMIT pairingError(i18n("Timed out"));
}
void LanPairingHandler::setInternalPairStatus(LanPairingHandler::InternalPairStatus status)
{
m_status = status;
if (status == Paired) {
deviceLink()->setPairStatus(DeviceLink::Paired);
} else {
deviceLink()->setPairStatus(DeviceLink::NotPaired);
}
2015-12-02 19:04:35 +00:00
qobject_cast<LanDeviceLink*>(deviceLink())->storeTrustedDeviceInformation();
//TODO: Tell link to store certificate and key
2015-07-05 14:23:53 +01:00
}