Commit 4713592f authored by Andrea Scarpino's avatar Andrea Scarpino

Make master key generation async

parent 71fef86f
......@@ -4,10 +4,12 @@ CONFIG += sailfishapp
SOURCES += \
src/main.cpp \
src/mpwmanager.cpp
src/mpwmanager.cpp \
src/asyncmasterkey.cpp
HEADERS += \
src/mpwmanager.h
src/mpwmanager.h \
src/asyncmasterkey.h
OTHER_FILES += \
qml/cover/CoverPage.qml \
......
......@@ -26,9 +26,18 @@ import QtQuick 2.0
import Sailfish.Silica 1.0
Page {
allowedOrientations: Orientation.All
Connections {
target: manager
onGeneratedMasterKey: {
busy.visible = busy.running = false;
name.enabled = password.enabled = version.enabled = true;
pageStack.pop();
}
}
Column {
id: column
x: Theme.horizontalPageMargin
......@@ -65,6 +74,12 @@ Page {
}
}
BusyIndicator {
id: busy
visible: false
anchors.horizontalCenter: parent.horizontalCenter
}
Button {
id: save
text: qsTr("Save");
......@@ -72,8 +87,9 @@ Page {
onClicked: {
if (name.text.length > 0 && password.text.length > 0) {
name.enabled = password.enabled = version.enabled = false;
busy.visible = busy.running = true;
manager.generateMasterKey(name.text, password.text, version.currentIndex);
pageStack.pop();
}
}
}
......
/*
The MIT License (MIT)
Copyright (c) 2016 Andrea Scarpino <me@andreascarpino.it>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "asyncmasterkey.h"
#include <QDebug>
extern "C"
{
#ifndef MPWALGORITHM_H
#define MPWALGORITHM_H
#include <mpw-algorithm.h>
#endif
}
AsyncMasterKey::AsyncMasterKey(const QString &name, const QString &password,
MPWManager::AlgorithmVersion version)
: m_name(name), m_password(password), m_algVersion(version)
{
}
AsyncMasterKey::~AsyncMasterKey()
{
}
void AsyncMasterKey::generate()
{
const uint8_t* k = mpw_masterKeyForUser(m_name.toUtf8().data(), m_password.toUtf8().data(),
MPWManager::toMPAlgorithmVersion(m_algVersion));
QByteArray* key = 0;
if (k) {
key = new QByteArray((const char*) k, MP_dkLen);
} else {
qCritical() << "Error during master key generation.";
}
Q_EMIT finished(key);
}
/*
The MIT License (MIT)
Copyright (c) 2016 Andrea Scarpino <me@andreascarpino.it>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef ASYNCMASTERKEY_H
#define ASYNCMASTERKEY_H
#include <QObject>
#include "mpwmanager.h"
class AsyncMasterKey : public QObject
{
Q_OBJECT
public:
explicit AsyncMasterKey(const QString &name, const QString &password,
MPWManager::AlgorithmVersion version);
virtual ~AsyncMasterKey();
Q_SIGNALS:
void finished(QByteArray *key);
public Q_SLOTS:
void generate();
private:
QString m_name;
QString m_password;
MPWManager::AlgorithmVersion m_algVersion;
};
#endif // ASYNCMASTERKEY_H
......@@ -25,14 +25,18 @@
#include "mpwmanager.h"
#include <QDebug>
#include <QThread>
#include "asyncmasterkey.h"
MPWManager::MPWManager(QObject *parent) :
QObject(parent)
m_key(0), QObject(parent)
{
}
MPWManager::~MPWManager()
{
delete m_key;
}
MPWManager::AlgorithmVersion MPWManager::getAlgorithmVersion() const
......@@ -47,6 +51,8 @@ QString MPWManager::getName() const
void MPWManager::setAlgorithmVersion(AlgorithmVersion version)
{
qDebug() << "Using algorithm version:" << version;
m_algVersion = version;
}
......@@ -59,34 +65,29 @@ void MPWManager::generateMasterKey(const QString &name, const QString &password,
{
setName(name);
setAlgorithmVersion(version);
qDebug() << "Using algorithm version:" << m_algVersion;
const uint8_t* k = mpw_masterKeyForUser(name.toUtf8().data(), password.toUtf8().data(), toMPAlgorithmVersion(version));
if (k) {
m_key = QByteArray::fromRawData((const char*) k, MP_dkLen);
} else {
qCritical() << "Error during master key generation.";
}
QThread* thread = new QThread;
AsyncMasterKey* worker = new AsyncMasterKey(name, password, version);
worker->moveToThread(thread);
connect(thread, &QThread::started, worker, &AsyncMasterKey::generate);
connect(worker, &AsyncMasterKey::finished, this, &MPWManager::gotMasterKey);
connect(worker, &AsyncMasterKey::finished, thread, &QThread::quit);
connect(worker, &AsyncMasterKey::finished, worker, &AsyncMasterKey::deleteLater);
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
thread->start();
}
MPAlgorithmVersion MPWManager::toMPAlgorithmVersion(AlgorithmVersion version) const
void MPWManager::gotMasterKey(QByteArray *key)
{
MPAlgorithmVersion v = MPAlgorithmVersionCurrent;
switch (version) {
case V0: v = MPAlgorithmVersion0; break;
case V1: v = MPAlgorithmVersion1; break;
case V2: v = MPAlgorithmVersion2; break;
case V3: v = MPAlgorithmVersion3; break;
default: qCritical() << "Unrecognized algorithm version:" << version;
}
qDebug() << "Storing master key.";
m_key = key;
return v;
Q_EMIT generatedMasterKey();
}
QString MPWManager::getPassword(const QString &site, PasswordType type, const uint counter) const
{
const char* p = mpw_passwordForSite((const unsigned char*) m_key.data(), site.toUtf8().data(),
const char* p = mpw_passwordForSite((const unsigned char*) m_key->data(), site.toUtf8().data(),
toMPSiteType(type), counter, MPSiteVariantPassword, NULL,
toMPAlgorithmVersion(m_algVersion));
......@@ -98,7 +99,21 @@ QString MPWManager::getPassword(const QString &site, PasswordType type, const ui
}
}
MPSiteType MPWManager::toMPSiteType(PasswordType type) const
MPAlgorithmVersion MPWManager::toMPAlgorithmVersion(AlgorithmVersion version)
{
MPAlgorithmVersion v = MPAlgorithmVersionCurrent;
switch (version) {
case V0: v = MPAlgorithmVersion0; break;
case V1: v = MPAlgorithmVersion1; break;
case V2: v = MPAlgorithmVersion2; break;
case V3: v = MPAlgorithmVersion3; break;
default: qCritical() << "Unrecognized algorithm version:" << version;
}
return v;
}
MPSiteType MPWManager::toMPSiteType(PasswordType type)
{
MPSiteType t = MPSiteTypeGeneratedLong;
switch (type) {
......
......@@ -29,7 +29,10 @@
extern "C"
{
#ifndef MPWALGORITHM_H
#define MPWALGORITHM_H
#include <mpw-algorithm.h>
#endif
}
class MPWManager : public QObject
......@@ -57,13 +60,19 @@ public:
Q_INVOKABLE void generateMasterKey(const QString &name, const QString &password, AlgorithmVersion version);
Q_INVOKABLE QString getPassword(const QString &site, PasswordType type, const uint counter) const;
private:
MPAlgorithmVersion toMPAlgorithmVersion(AlgorithmVersion version) const;
MPSiteType toMPSiteType(PasswordType type) const;
static MPAlgorithmVersion toMPAlgorithmVersion(AlgorithmVersion version);
static MPSiteType toMPSiteType(PasswordType type);
Q_SIGNALS:
void generatedMasterKey();
QByteArray m_key;
protected Q_SLOTS:
void gotMasterKey(QByteArray *key);
private:
QString m_name;
AlgorithmVersion m_algVersion;
QByteArray *m_key;
};
#endif // MPWMANAGER_H
......@@ -82,47 +82,47 @@
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="38"/>
<location filename="../qml/pages/Settings.qml" line="47"/>
<source>Settings</source>
<translation>Impostazioni</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="45"/>
<location filename="../qml/pages/Settings.qml" line="54"/>
<source>Full name</source>
<translation>Nome completo</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="51"/>
<location filename="../qml/pages/Settings.qml" line="60"/>
<source>Master password</source>
<translation>Master password</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="57"/>
<location filename="../qml/pages/Settings.qml" line="66"/>
<source>Algorithm version</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<location filename="../qml/pages/Settings.qml" line="71"/>
<source>V0</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="62"/>
<location filename="../qml/pages/Settings.qml" line="72"/>
<source>V1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="63"/>
<location filename="../qml/pages/Settings.qml" line="73"/>
<source>V2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="64"/>
<location filename="../qml/pages/Settings.qml" line="74"/>
<source>V3</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="70"/>
<location filename="../qml/pages/Settings.qml" line="86"/>
<source>Save</source>
<translation>Salva</translation>
</message>
......
......@@ -82,47 +82,47 @@
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="38"/>
<location filename="../qml/pages/Settings.qml" line="47"/>
<source>Settings</source>
<translation>Inställningar</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="45"/>
<location filename="../qml/pages/Settings.qml" line="54"/>
<source>Full name</source>
<translation>Fullständigt namn</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="51"/>
<location filename="../qml/pages/Settings.qml" line="60"/>
<source>Master password</source>
<translation>Huvudlösenord</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="57"/>
<location filename="../qml/pages/Settings.qml" line="66"/>
<source>Algorithm version</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<location filename="../qml/pages/Settings.qml" line="71"/>
<source>V0</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="62"/>
<location filename="../qml/pages/Settings.qml" line="72"/>
<source>V1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="63"/>
<location filename="../qml/pages/Settings.qml" line="73"/>
<source>V2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="64"/>
<location filename="../qml/pages/Settings.qml" line="74"/>
<source>V3</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="70"/>
<location filename="../qml/pages/Settings.qml" line="86"/>
<source>Save</source>
<translation>Spara</translation>
</message>
......
......@@ -82,47 +82,47 @@
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="38"/>
<location filename="../qml/pages/Settings.qml" line="47"/>
<source>Settings</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="45"/>
<location filename="../qml/pages/Settings.qml" line="54"/>
<source>Full name</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="51"/>
<location filename="../qml/pages/Settings.qml" line="60"/>
<source>Master password</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="57"/>
<location filename="../qml/pages/Settings.qml" line="66"/>
<source>Algorithm version</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<location filename="../qml/pages/Settings.qml" line="71"/>
<source>V0</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="62"/>
<location filename="../qml/pages/Settings.qml" line="72"/>
<source>V1</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="63"/>
<location filename="../qml/pages/Settings.qml" line="73"/>
<source>V2</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="64"/>
<location filename="../qml/pages/Settings.qml" line="74"/>
<source>V3</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="70"/>
<location filename="../qml/pages/Settings.qml" line="86"/>
<source>Save</source>
<translation type="unfinished"></translation>
</message>
......
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