Add a verification key that's displayed when pairing

The key is a sha256 of both devices' certificates. Both should generate the
same key, so hey user can check they are pairing against the right device.

Thanks Matthias Gerstner <mgerstner@suse.de> for reporting this.
This commit is contained in:
Albert Vaca Cintora 2020-11-26 11:28:32 +01:00
parent b272ab86b3
commit e7518493df
9 changed files with 131 additions and 46 deletions

View file

@ -15,6 +15,7 @@ class PairingHandler;
class NetworkPacket;
class LinkProvider;
class Device;
class QSslCertificate;
class DeviceLink
: public QObject
@ -44,6 +45,8 @@ public:
//The daemon will periodically destroy unpaired links if this returns false
virtual bool linkShouldBeKeptAlive() { return false; }
virtual QSslCertificate certificate() const = 0;
Q_SIGNALS:
void pairingRequest(PairingHandler* handler);
void pairingRequestExpired(PairingHandler* handler);

View file

@ -164,8 +164,7 @@ void LanDeviceLink::setPairStatus(PairStatus status)
DeviceLink::setPairStatus(status);
if (status == Paired) {
Q_ASSERT(KdeConnectConfig::instance().trustedDevices().contains(deviceId()));
Q_ASSERT(!m_socketLineReader->peerCertificate().isNull());
KdeConnectConfig::instance().setDeviceProperty(deviceId(), QStringLiteral("certificate"), QString::fromLatin1(m_socketLineReader->peerCertificate().toPem().data()));
KdeConnectConfig::instance().setDeviceProperty(deviceId(), QStringLiteral("certificate"), QString::fromLatin1(certificate().toPem()));
}
}

View file

@ -42,7 +42,7 @@ public:
bool linkShouldBeKeptAlive() override;
QHostAddress hostAddress() const;
QSslCertificate certificate() const;
QSslCertificate certificate() const override;
private Q_SLOTS:
void dataReceived();

View file

@ -8,6 +8,7 @@
#define LOOPBACKDEVICELINK_H
#include "../devicelink.h"
#include <QSslCertificate>
class LoopbackLinkProvider;
@ -23,6 +24,8 @@ public:
void userRequestsPair() override { setPairStatus(Paired); }
void userRequestsUnpair() override { setPairStatus(NotPaired); }
QSslCertificate certificate() const override { return QSslCertificate(); }
};
#endif

View file

@ -9,6 +9,7 @@
#include <QVector>
#include <QSet>
#include <QSslCertificate>
#include <QSslKey>
#include <KSharedConfig>
#include <KConfigGroup>
@ -537,25 +538,47 @@ bool Device::isPluginEnabled(const QString& pluginName) const
QString Device::encryptionInfo() const
{
QString result;
QCryptographicHash::Algorithm digestAlgorithm = QCryptographicHash::Algorithm::Sha1;
const QCryptographicHash::Algorithm digestAlgorithm = QCryptographicHash::Algorithm::Sha256;
QString localSha1 = QString::fromLatin1(KdeConnectConfig::instance().certificate().digest(digestAlgorithm).toHex());
for (int i = 2; i<localSha1.size(); i += 3) {
localSha1.insert(i, QStringLiteral(":")); // Improve readability
QString localChecksum = QString::fromLatin1(KdeConnectConfig::instance().certificate().digest(digestAlgorithm).toHex());
for (int i = 2; i<localChecksum.size(); i += 3) {
localChecksum.insert(i, QStringLiteral(":")); // Improve readability
}
result += i18n("SHA1 fingerprint of your device certificate is: %1\n", localSha1);
result += i18n("SHA256 fingerprint of your device certificate is: %1\n", localChecksum);
std::string remotePem = KdeConnectConfig::instance().getDeviceProperty(id(), QStringLiteral("certificate")).toStdString();
QSslCertificate remoteCertificate = QSslCertificate(QByteArray(remotePem.c_str(), (int)remotePem.size()));
QString remoteSha1 = QString::fromLatin1(remoteCertificate.digest(digestAlgorithm).toHex());
for (int i = 2; i < remoteSha1.size(); i += 3) {
remoteSha1.insert(i, QStringLiteral(":")); // Improve readability
QString remoteChecksum = QString::fromLatin1(remoteCertificate.digest(digestAlgorithm).toHex());
for (int i = 2; i < remoteChecksum.size(); i += 3) {
remoteChecksum.insert(i, QStringLiteral(":")); // Improve readability
}
result += i18n("SHA1 fingerprint of remote device certificate is: %1\n", remoteSha1);
result += i18n("SHA256 fingerprint of remote device certificate is: %1\n", remoteChecksum);
return result;
}
QSslCertificate Device::certificate() const
{
if (!d->m_deviceLinks.isEmpty()) {
return d->m_deviceLinks[0]->certificate();
}
return QSslCertificate();
}
QByteArray Device::verificationKey() const
{
auto a = KdeConnectConfig::instance().certificate().publicKey().toDer();
auto b = certificate().publicKey().toDer();
if (a < b) {
std::swap(a, b);
}
QCryptographicHash hash(QCryptographicHash::Sha256);
hash.addData(a);
hash.addData(b);
return hash.result().toHex();
}
QString Device::pluginIconName(const QString& pluginName)
{
if (hasPlugin(pluginName)) {

View file

@ -64,6 +64,7 @@ public:
QString type() const;
QString iconName() const;
QString statusIconName() const;
Q_SCRIPTABLE QByteArray verificationKey() const;
Q_SCRIPTABLE QString encryptionInfo() const;
//Add and remove links
@ -132,6 +133,7 @@ private: //Methods
void setName(const QString& name);
void setType(const QString& strtype);
QString iconForStatus(bool reachable, bool paired) const;
QSslCertificate certificate() const;
private:
class DevicePrivate;

View file

@ -49,14 +49,17 @@ public:
notification->setIconName(QStringLiteral("dialog-information"));
notification->setComponentName(QStringLiteral("kdeconnect"));
notification->setTitle(QStringLiteral("KDE Connect"));
notification->setText(i18n("Pairing request from %1", device->name().toHtmlEscaped()));
notification->setText(i18n("Pairing request from %1\n<br/>Key: %2...", device->name().toHtmlEscaped(), QString::fromUtf8(device->verificationKey().left(8))));
notification->setDefaultAction(i18n("Open"));
notification->setActions(QStringList() << i18n("Accept") << i18n("Reject"));
notification->setActions(QStringList() << i18n("Accept") << i18n("Reject") << i18n("View key"));
connect(notification, &KNotification::action1Activated, device, &Device::acceptPairing);
connect(notification, &KNotification::action2Activated, device, &Device::rejectPairing);
connect(notification, QOverload<>::of(&KNotification::activated), this, [this, device] {
openConfiguration(device->id());
});
QString deviceId = device->id();
auto openSettings = [this, deviceId] {
openConfiguration(deviceId);
};
connect(notification, &KNotification::action3Activated, openSettings);
connect(notification, QOverload<>::of(&KNotification::activated), openSettings);
notification->sendEvent();
}

View file

@ -221,6 +221,7 @@ void KdeConnectKcm::resetDeviceView()
delete kcmUi->pluginSelector;
kcmUi->pluginSelector = new KPluginSelector(this);
kcmUi->deviceInfo_layout->addWidget(kcmUi->pluginSelector);
kcmUi->verificationKey->setText(i18n("Key: %1", QString::fromUtf8(currentDevice->verificationKey())));
kcmUi->pluginSelector->setConfigurationArguments(QStringList(currentDevice->id()));

View file

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>965</width>
<width>1198</width>
<height>528</height>
</rect>
</property>
@ -142,6 +142,32 @@
<property name="sizeConstraint">
<enum>QLayout::SetMaximumSize</enum>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QWidget" name="deviceAndStatus" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="deviceAndStatus_layout">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="name_label">
<property name="font">
@ -172,6 +198,31 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="verificationKey">
<property name="text">
<string>🔑 abababab</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">