File indexing completed on 2024-04-28 05:50:07

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2020-2021 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 #ifndef ACCOUNTS_KEYS_H
0006 #define ACCOUNTS_KEYS_H
0007 
0008 #include <QByteArray>
0009 #include <QObject>
0010 
0011 #include "../secrets/secrets.h"
0012 
0013 namespace accounts
0014 {
0015     class AccountSecret : public QObject
0016     {
0017         Q_OBJECT
0018     Q_SIGNALS:
0019         void newPasswordNeeded(void);
0020         void existingPasswordNeeded(void);
0021         void keyFailed(void);
0022         void passwordAvailable(void);
0023         void keyAvailable(void);
0024         void requestsCancelled(void);
0025     public:
0026         AccountSecret(const secrets::SecureRandom &random = secrets::defaultSecureRandom, QObject *parent = nullptr);
0027         void cancelRequests(void);
0028         bool requestNewPassword(void);
0029         bool requestExistingPassword(const secrets::EncryptedSecret &challenge,
0030                                      const QByteArray& salt, const secrets::KeyDerivationParameters &keyParams);
0031 
0032         // HACK: disables challenge verification, remove at some point!
0033         bool requestExistingPassword(const QByteArray& salt, const secrets::KeyDerivationParameters &keyParams);
0034 
0035         bool answerExistingPassword(QString &password);
0036         bool answerNewPassword(QString &password, const secrets::KeyDerivationParameters &keyParams);
0037 
0038         secrets::SecureMasterKey * deriveKey(void);
0039 
0040         secrets::SecureMasterKey * key(void) const;
0041         std::optional<secrets::EncryptedSecret> challenge(void) const;
0042         std::optional<secrets::EncryptedSecret> encrypt(const secrets::SecureMemory *secret) const;
0043         secrets::SecureMemory * decrypt(const secrets::EncryptedSecret &secret) const;
0044         bool isStillAlive(void) const;
0045         bool isNewPasswordRequested(void) const;
0046         bool isExistingPasswordRequested(void) const;
0047         bool isKeyAvailable(void) const;
0048         bool isPasswordAvailable(void) const;
0049         bool isChallengeAvailable(void) const;
0050     private:
0051         bool acceptPassword(QString &password, bool answerMatchesRequest);
0052     private:
0053         bool m_stillAlive;
0054         bool m_newPassword;
0055         bool m_passwordRequested;
0056         bool m_hackWithoutChallenge; // HACK: disables challenge verification, remove at some point!
0057         const secrets::SecureRandom m_random;
0058         std::optional<QByteArray> m_salt;
0059         std::optional<secrets::EncryptedSecret> m_challenge;
0060         QScopedPointer<secrets::SecureMasterKey> m_key;
0061         QScopedPointer<secrets::SecureMemory> m_password;
0062         std::optional<secrets::KeyDerivationParameters> m_keyParams;
0063     };
0064 }
0065 
0066 #endif