2017-02-26 19:29:50 +01:00
|
|
|
|
#ifndef PASSWORDMANAGER_H
|
|
|
|
|
|
#define PASSWORDMANAGER_H
|
|
|
|
|
|
|
2019-09-01 14:07:58 +02:00
|
|
|
|
#include "KeyStrengthener.h"
|
2018-11-08 21:50:49 +01:00
|
|
|
|
#include <QSqlDatabase>
|
2018-11-04 11:24:13 +01:00
|
|
|
|
#include <botan/secmem.h>
|
2017-02-26 19:29:50 +01:00
|
|
|
|
#include <string>
|
2019-09-16 19:24:39 +02:00
|
|
|
|
#include <string_view>
|
2019-09-01 14:07:58 +02:00
|
|
|
|
#include <tuple>
|
2018-11-04 11:24:13 +01:00
|
|
|
|
#include <memory>
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2018-11-04 11:24:13 +01:00
|
|
|
|
#include <botan/pwdhash.h>
|
2017-02-26 19:29:50 +01:00
|
|
|
|
#include <map>
|
|
|
|
|
|
|
2018-11-04 11:24:13 +01:00
|
|
|
|
namespace Botan {
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
class Encrypted_PSK_Database;
|
2018-11-04 11:24:13 +01:00
|
|
|
|
class PasswordHash;
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2018-11-04 11:24:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class PasswordManagerException: public std::exception {
|
2017-02-26 19:29:50 +01:00
|
|
|
|
public:
|
2018-11-04 11:24:13 +01:00
|
|
|
|
using std::exception::exception; //(char const* const _Message);
|
|
|
|
|
|
};
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2018-11-04 11:24:13 +01:00
|
|
|
|
class PasswordManagerLockedException: public PasswordManagerException {
|
|
|
|
|
|
public:
|
|
|
|
|
|
using PasswordManagerException::PasswordManagerException;
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2018-11-04 11:24:13 +01:00
|
|
|
|
};
|
2019-09-01 14:07:58 +02:00
|
|
|
|
class PasswordCryptoEngine;
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2018-11-04 11:24:13 +01:00
|
|
|
|
class PasswordManager {
|
|
|
|
|
|
public:
|
|
|
|
|
|
enum Result {
|
|
|
|
|
|
Ok,
|
|
|
|
|
|
Locked,
|
|
|
|
|
|
Error
|
|
|
|
|
|
};
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
PasswordManager();
|
|
|
|
|
|
~PasswordManager();
|
|
|
|
|
|
|
|
|
|
|
|
/** Check if it has been initialized before.
|
|
|
|
|
|
*
|
|
|
|
|
|
* If returns false then use createDatabase to set it up
|
|
|
|
|
|
* else use openDatabase to get access.
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool initialized(QSqlDatabase &db);
|
|
|
|
|
|
bool createDatabase(QSqlDatabase &db, QString passphrase);
|
2018-11-15 19:24:29 +01:00
|
|
|
|
/// Opens the PSK database
|
2018-11-08 21:50:49 +01:00
|
|
|
|
bool openDatabase(QSqlDatabase &db, QString passphrase);
|
2018-11-04 11:24:13 +01:00
|
|
|
|
void closeDatabase();
|
2018-11-08 21:50:49 +01:00
|
|
|
|
bool locked() const;
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2019-09-01 14:07:58 +02:00
|
|
|
|
std::string encrypt(const std::string &id, const std::string &passwd);
|
2019-09-16 19:24:39 +02:00
|
|
|
|
std::string decrypt(const std::string &id, const std::string_view &encpwd);
|
2019-09-01 14:07:58 +02:00
|
|
|
|
// void remove(const std::string &id);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
private:
|
2018-11-08 21:50:49 +01:00
|
|
|
|
QString m_passwordTableName = "psk_passwd";
|
|
|
|
|
|
QString m_secretAlgoTableName = "psk_masterkey_algo";
|
|
|
|
|
|
QString m_secretHashTableName = "psk_masterkey_hash";
|
2019-09-01 14:07:58 +02:00
|
|
|
|
std::unique_ptr<PasswordCryptoEngine> m_cryptoEngine;
|
2018-11-04 11:24:13 +01:00
|
|
|
|
|
2018-11-08 21:50:49 +01:00
|
|
|
|
bool isPskStoreInitialized(QSqlDatabase& db);
|
|
|
|
|
|
void initializeNewPskStore(QSqlDatabase &db);
|
2018-11-04 11:24:13 +01:00
|
|
|
|
|
|
|
|
|
|
/// Get PasswordHash from parameters in database
|
2018-11-08 21:50:49 +01:00
|
|
|
|
KeyStrengthener getKeyStrengthener(QSqlDatabase &db);
|
2018-11-04 11:24:13 +01:00
|
|
|
|
KeyStrengthener createKeyStrengthener();
|
2017-02-26 19:29:50 +01:00
|
|
|
|
|
2019-09-01 14:07:58 +02:00
|
|
|
|
std::tuple<Botan::secure_vector<uint8_t>, Botan::secure_vector<uint8_t>>
|
|
|
|
|
|
deriveKey(KeyStrengthener &ks, QString passphrase);
|
2017-02-26 19:29:50 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // PASSWORDMANAGER_H
|