QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
rnpcrypto.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#ifndef SRC_RNPCRYPTO_H_
9#define SRC_RNPCRYPTO_H_
10
11#include <QByteArray>
12#include <QDateTime>
13#include <QList>
14#include <QString>
15#include <QStringList>
16#include <functional>
17
18struct rnp_ffi_st;
19
41class RnpCrypto {
42public:
46 struct KeyInfo {
48 QString fingerprint;
50 QString keyId;
55 QStringList userIds;
57 QDateTime created;
59 QDateTime expires;
61 bool revoked = false;
63 bool expired = false;
68 bool hasSecret = false;
73 bool secretProtected = true;
75 bool canEncrypt = false;
77 bool canSign = false;
78 };
79
86 using PassphraseProvider = std::function<QString(const QString &keyId)>;
87
95 static constexpr qint64 kMaxPlaintextBytes = 64LL * 1024 * 1024;
96
104 static constexpr qint64 kMaxKeyringBytes = 64LL * 1024 * 1024;
105
109 RnpCrypto();
113 ~RnpCrypto();
114 // RNP holds a pointer to m_passphrase (the passphrase callback's context),
115 // so the object must stay where it is: neither copied nor moved.
116 RnpCrypto(const RnpCrypto &) = delete;
117 auto operator=(const RnpCrypto &) -> RnpCrypto & = delete;
118 RnpCrypto(RnpCrypto &&) = delete;
119 auto operator=(RnpCrypto &&) -> RnpCrypto & = delete;
120
125 [[nodiscard]] auto isValid() const -> bool;
126
133 auto importKeys(const QByteArray &keyData, QString *error = nullptr)
134 -> QStringList;
135
141
149 void setMaxPlaintextBytes(qint64 bytes);
150
168 auto encrypt(const QByteArray &plaintext, const QStringList &recipients,
169 QString *error = nullptr) -> QByteArray;
170
184 auto decrypt(const QByteArray &ciphertext, QString *error = nullptr,
185 bool *ok = nullptr) -> QByteArray;
186
192 [[nodiscard]] auto listKeys(bool secretOnly = false) const -> QList<KeyInfo>;
193
205 auto sign(const QByteArray &data, const QString &signer,
206 QString *error = nullptr) -> QByteArray;
207
223 auto verify(const QByteArray &data, const QByteArray &signature,
224 QString *error = nullptr) -> QStringList;
225
241 auto protectKey(const QString &fingerprint, const QString &passphrase,
242 QString *error = nullptr) -> bool;
243
268 auto saveKeyring(const QString &dir, bool allowUnprotected = false,
269 QString *error = nullptr) const -> bool;
270
283 auto loadKeyring(const QString &dir, QString *error = nullptr) -> bool;
284
285private:
290 rnp_ffi_st *m_ffi = nullptr;
298};
299
300#endif // SRC_RNPCRYPTO_H_
static constexpr qint64 kMaxKeyringBytes
Most bytes of a keyring file: saveKeyring() writes no larger one, loadKeyring() reads no larger one.
Definition rnpcrypto.h:104
auto sign(const QByteArray &data, const QString &signer, QString *error=nullptr) -> QByteArray
Makes a detached signature over data, as gpg –detach-sign –local-user signer does (for ....
void setPassphraseProvider(PassphraseProvider provider)
Sets how secret-key passphrases are obtained.
std::function< QString(const QString &keyId)> PassphraseProvider
Asks for the passphrase of a secret key.
Definition rnpcrypto.h:86
RnpCrypto()
Creates an empty keyring.
auto importKeys(const QByteArray &keyData, QString *error=nullptr) -> QStringList
Imports public and secret keys from armored or binary key data.
qint64 m_maxPlaintext
Largest plaintext decrypt() returns, in bytes.
Definition rnpcrypto.h:297
auto verify(const QByteArray &data, const QByteArray &signature, QString *error=nullptr) -> QStringList
Checks a detached signature over data, as QtPass reads gpg's VALIDSIG.
void setMaxPlaintextBytes(qint64 bytes)
Sets the most bytes of plaintext decrypt() may produce.
rnp_ffi_st * m_ffi
RNP's handle holding this object's keyring; null when RNP could not be initialised,...
Definition rnpcrypto.h:290
auto protectKey(const QString &fingerprint, const QString &passphrase, QString *error=nullptr) -> bool
Protects the secret parts of a key with passphrase.
auto decrypt(const QByteArray &ciphertext, QString *error=nullptr, bool *ok=nullptr) -> QByteArray
Decrypts an OpenPGP message with any secret key in the keyring.
auto listKeys(bool secretOnly=false) const -> QList< KeyInfo >
The primary keys in the keyring.
auto isValid() const -> bool
Whether the RNP context could be created at all.
auto loadKeyring(const QString &dir, QString *error=nullptr) -> bool
Adds the keyring in dir (as saveKeyring() wrote it) to this one. Secret keys stay protected until an ...
auto encrypt(const QByteArray &plaintext, const QStringList &recipients, QString *error=nullptr) -> QByteArray
Encrypts plaintext to every recipient, as one message.
PassphraseProvider m_passphrase
Asked for the passphrase of a locked secret key during decryption; set with setPassphraseProvider(),...
Definition rnpcrypto.h:295
static constexpr qint64 kMaxPlaintextBytes
Most bytes of plaintext decrypt() produces by default.
Definition rnpcrypto.h:95
auto saveKeyring(const QString &dir, bool allowUnprotected=false, QString *error=nullptr) const -> bool
Writes the keyring to dir: keyring.gpg, public and secret keys in one file (OpenPGP keys,...
One primary key in the keyring, as the Users dialog lists keys.
Definition rnpcrypto.h:46
QStringList userIds
Its user IDs that are still its own (valid, not revoked), in the keyring's order.
Definition rnpcrypto.h:55
QDateTime expires
When it expires; invalid when it does not.
Definition rnpcrypto.h:59
bool canSign
Whether it can sign now, with a secret key that is here.
Definition rnpcrypto.h:77
bool canEncrypt
Whether a message can be encrypted to it now.
Definition rnpcrypto.h:75
bool secretProtected
Whether every secret part of it that is here is protected by a passphrase; true when none is here.
Definition rnpcrypto.h:73
bool expired
Whether it has expired.
Definition rnpcrypto.h:63
QString fingerprint
The primary key's fingerprint, upper-case hex.
Definition rnpcrypto.h:48
QString keyId
The primary key's 16-hex-digit key ID.
Definition rnpcrypto.h:50
QDateTime created
When it was created.
Definition rnpcrypto.h:57
bool hasSecret
Whether a secret part of it (primary or subkey) is here to use: a GnuPG stub (a key left at home,...
Definition rnpcrypto.h:68
bool revoked
Whether it has been revoked.
Definition rnpcrypto.h:61