mpwmanager.cpp 3.71 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 27 28 29 30 31 32 33 34 35 36 37
/*
  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"

#include <QDebug>

MPWManager::MPWManager(QObject *parent) :
    QObject(parent)
{
}

MPWManager::~MPWManager()
{
}

38 39 40 41 42
MPWManager::AlgorithmVersion MPWManager::getAlgorithmVersion() const
{
    return m_algVersion;
}

Andrea Scarpino's avatar
Andrea Scarpino committed
43 44 45 46 47
QString MPWManager::getName() const
{
    return m_name;
}

48 49 50 51 52 53
void MPWManager::setAlgorithmVersion(AlgorithmVersion version)
{
    m_algVersion = version;
}

void MPWManager::setName(const QString &name)
Andrea Scarpino's avatar
Andrea Scarpino committed
54 55
{
    m_name = name;
56 57 58 59 60 61 62
}

void MPWManager::generateMasterKey(const QString &name, const QString &password, AlgorithmVersion version)
{
    setName(name);
    setAlgorithmVersion(version);
    qDebug() << "Using algorithm version:" << m_algVersion;
Andrea Scarpino's avatar
Andrea Scarpino committed
63

64
    const uint8_t* k = mpw_masterKeyForUser(name.toUtf8().data(), password.toUtf8().data(), toMPAlgorithmVersion(version));
65 66 67 68

    if (k) {
        m_key = QByteArray::fromRawData((const char*) k, MP_dkLen);
    } else {
69 70 71 72 73 74 75 76 77 78 79 80 81
        qCritical() << "Error during master key generation.";
    }
}

MPAlgorithmVersion MPWManager::toMPAlgorithmVersion(AlgorithmVersion version) const
{
    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;
82
    }
83 84

    return v;
Andrea Scarpino's avatar
Andrea Scarpino committed
85 86
}

Andrea Scarpino's avatar
Andrea Scarpino committed
87
QString MPWManager::getPassword(const QString &site, PasswordType type, const uint counter) const
Andrea Scarpino's avatar
Andrea Scarpino committed
88
{
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    const char* p = mpw_passwordForSite((const unsigned char*) m_key.data(), site.toUtf8().data(),
                                        toMPSiteType(type), counter, MPSiteVariantPassword, NULL,
                                        toMPAlgorithmVersion(m_algVersion));

    if (p) {
        return QString::fromUtf8(p);
    } else {
        qCritical() << "Error during password generation.";
        return QString();
    }
}

MPSiteType MPWManager::toMPSiteType(PasswordType type) const
{
    MPSiteType t = MPSiteTypeGeneratedLong;
Andrea Scarpino's avatar
Andrea Scarpino committed
104 105 106 107 108 109 110 111 112
    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;
113
        default: qCritical() << "Unrecognized password type:" << type;
Andrea Scarpino's avatar
Andrea Scarpino committed
114 115
    }

116
    return t;
Andrea Scarpino's avatar
Andrea Scarpino committed
117
}