diff --git a/cli/kdeconnect-cli.cpp b/cli/kdeconnect-cli.cpp index 69856cb6a..9c9447f34 100644 --- a/cli/kdeconnect-cli.cpp +++ b/cli/kdeconnect-cli.cpp @@ -184,7 +184,7 @@ int main(int argc, char** argv) } } else if(parser.isSet("encryption-info")) { DeviceDbusInterface dev(device); - QDBusPendingReply devReply = dev.encryptionInfo(); // QSsl::Der = 1 + QDBusPendingReply devReply = dev.encryptionInfo(); // QSsl::Der = 1 devReply.waitForFinished(); QTextStream(stderr) << devReply.value() << endl; } else { diff --git a/core/backends/devicelink.h b/core/backends/devicelink.h index 269787388..9c9b419b9 100644 --- a/core/backends/devicelink.h +++ b/core/backends/devicelink.h @@ -49,7 +49,7 @@ public: virtual PairingHandler* createPairingHandler(Device* device) = 0; virtual bool sendPackage(NetworkPackage& np) = 0; - virtual bool sendPackageEncrypted(QCA::PublicKey& publicKey, NetworkPackage& np) = 0; + virtual bool sendPackageEncrypted(NetworkPackage& np) = 0; ConnectionStarted connectionSource() const { return mConnectionSource; diff --git a/core/backends/lan/landevicelink.cpp b/core/backends/lan/landevicelink.cpp index f2f6dc5d9..169ff8db3 100644 --- a/core/backends/lan/landevicelink.cpp +++ b/core/backends/lan/landevicelink.cpp @@ -30,7 +30,6 @@ LanDeviceLink::LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource) : DeviceLink(deviceId, parent, connectionSource) , mSocketLineReader(new SocketLineReader(socket)) - , onSsl(false) { connect(mSocketLineReader, SIGNAL(readyRead()), this, SLOT(dataReceived())); @@ -50,26 +49,17 @@ QString LanDeviceLink::name() return "LanLink"; // Should be same in both android and kde version } -void LanDeviceLink::setOnSsl(bool value) -{ - onSsl = value; -} - PairingHandler* LanDeviceLink::createPairingHandler(Device* device) { return new LanPairingHandler(device->id()); } -bool LanDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np) +bool LanDeviceLink::sendPackageEncrypted(NetworkPackage& np) { if (np.hasPayload()) { np.setPayloadTransferInfo(sendPayload(np)->transferInfo()); } - if (!onSsl) { - np.encrypt(key); - } - int written = mSocketLineReader->write(np.serialize()); //TODO: Actually detect if a package is received or not, now we keep TCP @@ -91,10 +81,9 @@ bool LanDeviceLink::sendPackage(NetworkPackage& np) UploadJob* LanDeviceLink::sendPayload(NetworkPackage& np) { QVariantMap transferInfo; - if (onSsl) { - transferInfo.insert("useSsl", true); - transferInfo.insert("deviceId", deviceId()); - } + //FIXME: The next two lines shouldn't be needed! Why are they here? + transferInfo.insert("useSsl", true); + transferInfo.insert("deviceId", deviceId()); UploadJob* job = new UploadJob(np.payload(), deviceId()); job->start(); return job; @@ -120,10 +109,9 @@ void LanDeviceLink::dataReceived() if (unserialized.hasPayloadTransferInfo()) { //qCDebug(KDECONNECT_CORE) << "HasPayloadTransferInfo"; QVariantMap transferInfo = unserialized.payloadTransferInfo(); - if (onSsl) { - transferInfo.insert("useSsl", true); - transferInfo.insert("deviceId", deviceId()); - } + //FIXME: The next two lines shouldn't be needed! Why are they here? + transferInfo.insert("useSsl", true); + transferInfo.insert("deviceId", deviceId()); DownloadJob* job = new DownloadJob(mSocketLineReader->peerAddress(), transferInfo); job->start(); unserialized.setPayload(job->getPayload(), unserialized.payloadSize()); diff --git a/core/backends/lan/landevicelink.h b/core/backends/lan/landevicelink.h index 271e12ac2..6c72fe6ce 100644 --- a/core/backends/lan/landevicelink.h +++ b/core/backends/lan/landevicelink.h @@ -39,11 +39,10 @@ public: LanDeviceLink(const QString& deviceId, LinkProvider* parent, QSslSocket* socket, ConnectionStarted connectionSource); virtual QString name() Q_DECL_OVERRIDE; - void setOnSsl(bool value); virtual PairingHandler* createPairingHandler(Device* device) Q_DECL_OVERRIDE; bool sendPackage(NetworkPackage& np) override; - bool sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& np) override; - UploadJob* sendPayload(NetworkPackage&); + bool sendPackageEncrypted(NetworkPackage& np) override; + UploadJob* sendPayload(NetworkPackage& np); private Q_SLOTS: void dataReceived(); diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index 417250ca9..5ff16d7f3 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -427,10 +427,6 @@ void LanLinkProvider::addLink(const QString& deviceId, QSslSocket* socket, Netwo // Socket disconnection will now be handled by LanDeviceLink disconnect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); - if (socket->isEncrypted()) { - deviceLink->setOnSsl(true); - } - //We kill any possible link from this same device QMap< QString, DeviceLink* >::iterator oldLinkIterator = mLinks.find(deviceLink->deviceId()); if (oldLinkIterator != mLinks.end()) { diff --git a/core/backends/loopback/loopbackdevicelink.cpp b/core/backends/loopback/loopbackdevicelink.cpp index 3cb53b516..fefaa5d65 100644 --- a/core/backends/loopback/loopbackdevicelink.cpp +++ b/core/backends/loopback/loopbackdevicelink.cpp @@ -38,33 +38,9 @@ PairingHandler* LoopbackDeviceLink::createPairingHandler(Device *device) { return new LoopbackPairingHandler(device->id()); } -bool LoopbackDeviceLink::sendPackageEncrypted(QCA::PublicKey& key, NetworkPackage& input) +bool LoopbackDeviceLink::sendPackageEncrypted(NetworkPackage& input) { - if (mPrivateKey.isNull() || key.isNull()) { - return false; - } - - input.encrypt(key); - - QByteArray serialized = input.serialize(); - - NetworkPackage unserialized(QString::null); - NetworkPackage::unserialize(serialized, &unserialized); - - NetworkPackage output(QString::null); - unserialized.decrypt(mPrivateKey, &output); - - bool b = true; - //LoopbackDeviceLink does not need deviceTransferInfo - if (input.hasPayload()) { - b = input.payload()->open(QIODevice::ReadOnly); - Q_ASSERT(b); - output.setPayload(input.payload(), input.payloadSize()); - } - - Q_EMIT receivedPackage(output); - - return b; + return sendPackage(input); } bool LoopbackDeviceLink::sendPackage(NetworkPackage& input) diff --git a/core/backends/loopback/loopbackdevicelink.h b/core/backends/loopback/loopbackdevicelink.h index 3550b8f2a..97864e7e5 100644 --- a/core/backends/loopback/loopbackdevicelink.h +++ b/core/backends/loopback/loopbackdevicelink.h @@ -35,7 +35,7 @@ public: virtual QString name() override; virtual PairingHandler* createPairingHandler(Device* device) override; virtual bool sendPackage(NetworkPackage& np) override; - virtual bool sendPackageEncrypted(QCA::PublicKey& publicKey, NetworkPackage& np) override; + virtual bool sendPackageEncrypted(NetworkPackage& np) override; }; diff --git a/core/backends/pairinghandler.cpp b/core/backends/pairinghandler.cpp index 0d7726306..2633f5324 100644 --- a/core/backends/pairinghandler.cpp +++ b/core/backends/pairinghandler.cpp @@ -21,17 +21,22 @@ #include "pairinghandler.h" PairingHandler::PairingHandler() - : m_pairStatus(NotPaired) - , m_deviceLink(nullptr) + : m_deviceLink(nullptr) + , m_pairStatus(NotPaired) { } -void PairingHandler::setLink(DeviceLink *dl) +void PairingHandler::setDeviceLink(DeviceLink *dl) { m_deviceLink = dl; } +DeviceLink* PairingHandler::deviceLink() const +{ + return m_deviceLink; +} + void PairingHandler::linkDestroyed(QObject* o) { DeviceLink* dl = static_cast(o); @@ -49,3 +54,9 @@ void PairingHandler::setPairStatus(PairingHandler::PairStatus status) Q_EMIT pairStatusChanged(status, oldStatus); } } + +PairingHandler::PairStatus PairingHandler::pairStatus() const +{ + return m_pairStatus; +} + diff --git a/core/backends/pairinghandler.h b/core/backends/pairinghandler.h index 4edc6c594..0caf5c8aa 100644 --- a/core/backends/pairinghandler.h +++ b/core/backends/pairinghandler.h @@ -54,7 +54,7 @@ public: PairingHandler(); virtual ~PairingHandler() { } - void setLink(DeviceLink* dl); + void setDeviceLink(DeviceLink* dl); bool isPaired() const { return m_pairStatus == PairStatus::Paired; } bool isPairRequested() const { return m_pairStatus == PairStatus::Requested; } diff --git a/core/device.cpp b/core/device.cpp index bf42d4a26..a37e8eb61 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include "core_debug.h" #include "kdeconnectplugin.h" @@ -288,7 +289,7 @@ void Device::addLink(const NetworkPackage& identityPackage, DeviceLink* link) 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); + m_pairingHandlers[link->name()]->setDeviceLink(link); connect(link, SIGNAL(destroyed(QObject*)), m_pairingHandlers[link->name()], SLOT(linkDestroyed(QObject*))); } @@ -320,7 +321,7 @@ bool Device::sendPackage(NetworkPackage& np) { if (np.type() != PACKAGE_TYPE_PAIR && isPaired()) { Q_FOREACH(DeviceLink* dl, m_deviceLinks) { - if (dl->sendPackageEncrypted(m_publicKey, np)) return true; + if (dl->sendPackageEncrypted(np)) return true; } } else { //Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data @@ -366,8 +367,6 @@ void Device::rejectPairing() { qCDebug(KDECONNECT_CORE) << "Rejected pairing"; - m_pairStatus = PairingHandler::NotPaired; - Q_FOREACH(PairingHandler* ph, m_pairingHandlers.values()) { ph->rejectPairing(); } @@ -387,7 +386,8 @@ void Device::acceptPairing() } -void Device::isPaired() { +bool Device::isPaired() const +{ Q_FOREACH(PairingHandler* ph, m_pairingHandlers) { if (ph->isPaired()) return true; } @@ -466,9 +466,9 @@ void Device::setName(const QString &name) } } -Device::PairStatus Device::pairStatus() const +PairingHandler::PairStatus Device::pairStatus() const { - return m_pairStatus; + return isPaired()? PairingHandler::Paired : PairingHandler::NotPaired; } KdeConnectPlugin* Device::plugin(const QString& pluginName) const @@ -499,17 +499,17 @@ QString Device::encryptionInfo() const { QString result; - QByteArray myCertificate = KdeConnectConfig::instance()->certificate().toDer(); + QString myCertificate = QString::fromLatin1(KdeConnectConfig::instance()->certificate().toDer()); for (int i=2 ; igetDeviceProperty(id(), "certificate"); for (int i=2 ; ipair_button->setVisible(!paired); kcmUi->unpair_button->setVisible(paired); - kcmUi->progressBar->setVisible(senderDevice->pairRequested()); + kcmUi->progressBar->setVisible(senderDevice->isPairRequested()); kcmUi->ping_button->setVisible(paired); kcmUi->status_label->setText(paired ? i18n("(paired)") : i18n("(unpaired)")); } diff --git a/tests/devicetest.cpp b/tests/devicetest.cpp index 82bccd600..f7b9d27a5 100644 --- a/tests/devicetest.cpp +++ b/tests/devicetest.cpp @@ -18,6 +18,7 @@ * along with this program. If not, see . */ +#include "../core/device.h" #include "../core/backends/lan/lanlinkprovider.h" #include "../core/kdeconnectconfig.h" @@ -68,15 +69,8 @@ void DeviceTest::testPairedDevice() QCOMPARE(device.name(), deviceName); QCOMPARE(device.type(), deviceType); - QVERIFY2(!device.publicKey().isNull(), "Public key for device is null"); - QCOMPARE(device.publicKey(), kcc->publicKey()); - - QVERIFY2(device.certificate().isNull(), "Certificate should be null before initialisation"); - - // TODO : Set certificate via identity package - QCOMPARE(device.isPaired(), true); - QCOMPARE(device.pairRequested(), false); + QCOMPARE(device.isPairRequested(), false); QCOMPARE(device.isReachable(), false); @@ -112,11 +106,8 @@ void DeviceTest::testUnpairedDevice() QCOMPARE(device.name(), deviceName); QCOMPARE(device.type(), deviceType); - QVERIFY2(device.publicKey().isNull(), "Public key for unpaired device should be null"); - QVERIFY2(device.certificate().isNull(), "Certificate for unpaired device should be null"); - QCOMPARE(device.isPaired(), false); - QCOMPARE(device.pairRequested(), false); + QCOMPARE(device.isPairRequested(), false); QCOMPARE(device.isReachable(), true); QCOMPARE(device.availableLinks().contains(linkProvider.name()), true); diff --git a/tests/kdeconnectconfigtest.cpp b/tests/kdeconnectconfigtest.cpp index b928f1a28..6ed8a1d0b 100644 --- a/tests/kdeconnectconfigtest.cpp +++ b/tests/kdeconnectconfigtest.cpp @@ -34,9 +34,9 @@ Q_OBJECT private Q_SLOTS: void initTestCase(); void addTrustedDevice(); - +/* void remoteCertificateTest(); - +*/ void removeTrustedDevice(); private: @@ -46,13 +46,6 @@ private: void KdeConnectConfigTest::initTestCase() { kcc = KdeConnectConfig::instance(); - - QVERIFY2(!kcc->publicKey().isNull(), "Public key not generated, is null"); - QVERIFY2(!kcc->privateKey().isNull(), "Private key not generated, is null"); - QVERIFY2(!kcc->certificate().isNull(), "Certificate not generated, is null"); - QVERIFY2(QFile::exists(kcc->privateKeyPath()), "Private key file does not exists, private key not saved properly"); - QVERIFY2(QFile::exists(kcc->certificatePath()), "Certificate file does not exists, certificate not saved properly"); - } void KdeConnectConfigTest::addTrustedDevice() @@ -63,6 +56,7 @@ void KdeConnectConfigTest::addTrustedDevice() QCOMPARE(devInfo.deviceType, QString("phone")); } +/* // This checks whether certificate is generated correctly and stored correctly or not void KdeConnectConfigTest::remoteCertificateTest() { @@ -84,6 +78,7 @@ void KdeConnectConfigTest::remoteCertificateTest() QCOMPARE(devCertificate.subjectInfo(QSslCertificate::OrganizationalUnitName).first(), QString("Kde connect")); } +*/ void KdeConnectConfigTest::removeTrustedDevice() diff --git a/tests/lanlinkprovidertest.cpp b/tests/lanlinkprovidertest.cpp index 10fba977b..965adc1fd 100644 --- a/tests/lanlinkprovidertest.cpp +++ b/tests/lanlinkprovidertest.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include /* @@ -322,11 +323,10 @@ QSslCertificate LanLinkProviderTest::generateCertificate(QString& commonName, QC return certificate; } -void LanLinkProviderTest::setSocketAttributes(QSslSocket *socket) { - +void LanLinkProviderTest::setSocketAttributes(QSslSocket *socket) +{ socket->setPrivateKey(QSslKey(privateKey.toPEM().toLatin1(), QSsl::Rsa)); socket->setLocalCertificate(certificate); - } void LanLinkProviderTest::addTrustedDevice()