mpwmanager.cpp 4.82 KB
Newer Older
Andrea Scarpino's avatar
Andrea Scarpino committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
  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 "mpwmanager.h"

27
#include <QCoreApplication>
Andrea Scarpino's avatar
Andrea Scarpino committed
28
#include <QDebug>
29
#include <QSettings>
30 31 32
#include <QThread>

#include "asyncmasterkey.h"
Andrea Scarpino's avatar
Andrea Scarpino committed
33 34

MPWManager::MPWManager(QObject *parent) :
35 36
    QObject(parent)
  , m_key(0)
Andrea Scarpino's avatar
Andrea Scarpino committed
37
{
38 39 40 41
    m_settings = new QSettings(QCoreApplication::applicationName(), QCoreApplication::applicationName(), this);

    m_name = m_settings->value("Name").toString();
    m_algVersion = algVersionFromInt(m_settings->value("Algorithm").toUInt());
Andrea Scarpino's avatar
Andrea Scarpino committed
42 43 44 45
}

MPWManager::~MPWManager()
{
46
    delete m_key;
47
    delete m_settings;
Andrea Scarpino's avatar
Andrea Scarpino committed
48 49
}

50 51 52 53 54
MPWManager::AlgorithmVersion MPWManager::getAlgorithmVersion() const
{
    return m_algVersion;
}

Andrea Scarpino's avatar
Andrea Scarpino committed
55 56 57 58 59
QString MPWManager::getName() const
{
    return m_name;
}

60 61
void MPWManager::setAlgorithmVersion(AlgorithmVersion version)
{
62 63
    qDebug() << "Using algorithm version:" << version;

64
    m_algVersion = version;
65
    m_settings->setValue("Algorithm", version);
66 67 68
}

void MPWManager::setName(const QString &name)
Andrea Scarpino's avatar
Andrea Scarpino committed
69 70
{
    m_name = name;
71
    m_settings->setValue("Name", name);
72 73 74 75 76 77
}

void MPWManager::generateMasterKey(const QString &name, const QString &password, AlgorithmVersion version)
{
    setName(name);
    setAlgorithmVersion(version);
Andrea Scarpino's avatar
Andrea Scarpino committed
78

79 80 81 82 83 84 85 86 87
    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();
88 89
}

90
void MPWManager::gotMasterKey(QByteArray *key)
91
{
92
    qDebug() << "Storing master key";
93
    m_key = key;
94

95
    Q_EMIT generatedMasterKey();
Andrea Scarpino's avatar
Andrea Scarpino committed
96 97
}

Andrea Scarpino's avatar
Andrea Scarpino committed
98
QString MPWManager::getPassword(const QString &site, PasswordType type, const uint counter) const
Andrea Scarpino's avatar
Andrea Scarpino committed
99
{
100
    const char* p = mpw_passwordForSite((const unsigned char*) m_key->data(), site.toUtf8().data(),
101 102 103 104 105 106
                                        toMPSiteType(type), counter, MPSiteVariantPassword, NULL,
                                        toMPAlgorithmVersion(m_algVersion));

    if (p) {
        return QString::fromUtf8(p);
    } else {
107
        qCritical() << "Error during password generation";
108 109 110 111
        return QString();
    }
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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)
127 128
{
    MPSiteType t = MPSiteTypeGeneratedLong;
Andrea Scarpino's avatar
Andrea Scarpino committed
129 130 131 132 133 134 135 136 137
    switch (type) {
        case Maximum: t = MPSiteTypeGeneratedMaximum; break;
        case Long: t = MPSiteTypeGeneratedLong; break;
        case Medium: t = MPSiteTypeGeneratedMedium; break;
        case Basic: t = MPSiteTypeGeneratedBasic; break;
        case Short: t = MPSiteTypeGeneratedShort; break;
        case PIN: t = MPSiteTypeGeneratedPIN; break;
        case Name: t = MPSiteTypeGeneratedName; break;
        case Phrase: t = MPSiteTypeGeneratedPhrase; break;
138
        default: qCritical() << "Unrecognized password type:" << type;
Andrea Scarpino's avatar
Andrea Scarpino committed
139 140
    }

141
    return t;
Andrea Scarpino's avatar
Andrea Scarpino committed
142
}
143 144 145 146 147 148 149 150 151 152 153 154 155

MPWManager::AlgorithmVersion MPWManager::algVersionFromInt(const uint &version)
{
    if (version == 0) {
        return V0;
    } else if (version == 1) {
        return V1;
    } else if (version == 2) {
        return V2;
    } else {
        return V3;
    }
}