Fixed encryption info in cli

This commit is contained in:
Vineet Garg 2015-07-19 20:25:28 +05:30
parent 20e46418ba
commit eda2563d03
7 changed files with 33 additions and 10 deletions

View file

@ -1,6 +1,6 @@
add_executable(kdeconnect-cli kdeconnect-cli.cpp) add_executable(kdeconnect-cli kdeconnect-cli.cpp)
target_link_libraries(kdeconnect-cli kdeconnectinterfaces Qt5::Network KF5::CoreAddons KF5::I18n) target_link_libraries(kdeconnect-cli kdeconnectinterfaces KF5::CoreAddons KF5::I18n)
install(TARGETS kdeconnect-cli ${INSTALL_TARGETS_DEFAULT_ARGS}) install(TARGETS kdeconnect-cli ${INSTALL_TARGETS_DEFAULT_ARGS})

View file

@ -18,10 +18,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QCryptographicHash>
#include <QDBusMessage> #include <QDBusMessage>
#include <QDBusConnection> #include <QDBusConnection>
#include <QCoreApplication> #include <QCoreApplication>
#include <QSslCertificate>
#include <QTextStream> #include <QTextStream>
#include <KAboutData> #include <KAboutData>
@ -148,11 +148,24 @@ int main(int argc, char** argv)
<< ": " << idx.data(NotificationsModel::NameModelRole).toString() << endl; << ": " << idx.data(NotificationsModel::NameModelRole).toString() << endl;
} }
} else if(parser.isSet("encryption-info")) { } else if(parser.isSet("encryption-info")) {
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+device, "org.kde.kdeconnect.device", "certificate"); DeviceDbusInterface dev(device);
msg.setArguments(QVariantList() << QSsl::Pem); QDBusPendingReply<QByteArray> devReply = dev.certificate(1); // QSsl::Der = 1
QDBusMessage reply = QDBusConnection::sessionBus().call(msg); devReply.waitForFinished();
QSslCertificate certificate = QSslCertificate::fromData(reply.arguments().first().toByteArray()).first(); if (devReply.value().isEmpty()) {
QTextStream(stderr) << certificate.toText() << endl; 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;
}
} else { } else {
QTextStream(stderr) << i18n("Nothing to be done") << endl; QTextStream(stderr) << i18n("Nothing to be done") << endl;
} }

View file

@ -129,6 +129,13 @@ QStringList Daemon::devices(bool onlyReachable, bool onlyVisible) const
return ret; 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) void Daemon::onNewDeviceLink(const NetworkPackage& identityPackage, DeviceLink* dl)
{ {
const QString& id = identityPackage.get<QString>("deviceId"); const QString& id = identityPackage.get<QString>("deviceId");

View file

@ -62,6 +62,9 @@ public Q_SLOTS:
//Returns a list of ids. The respective devices can be manipulated using the dbus path: "/modules/kdeconnect/Devices/"+id //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 onlyVisible = false) const; Q_SCRIPTABLE QStringList devices(bool onlyReachable = false, bool onlyVisible = 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;
virtual void requestPairing(Device *d) = 0; virtual void requestPairing(Device *d) = 0;
virtual void reportError(const QString &title, const QString &description) = 0; virtual void reportError(const QString &title, const QString &description) = 0;
virtual QNetworkAccessManager* networkAccessManager(); virtual QNetworkAccessManager* networkAccessManager();

View file

@ -87,7 +87,7 @@ public:
QString type() const { return type2str(m_deviceType); } QString type() const { return type2str(m_deviceType); }
QCA::PublicKey publicKey() const { return m_publicKey; } QCA::PublicKey publicKey() const { return m_publicKey; }
QSslCertificate certificate() const { return m_certificate; } 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 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 iconName() const;
QString statusIconName() const; QString statusIconName() const;

View file

@ -22,7 +22,6 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <QtCrypto>
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
@ -297,3 +296,4 @@ QDir KdeConnectConfig::pluginConfigDir(QString deviceId, QString pluginName)
return QDir(pluginConfigDir); return QDir(pluginConfigDir);
} }

View file

@ -23,7 +23,7 @@
#include <QtCrypto> #include <QtCrypto>
#include <QDir> #include <QDir>
#include <QtNetwork/qsslcertificate.h> #include <QSslCertificate>
#include "kdeconnectcore_export.h" #include "kdeconnectcore_export.h"