File indexing completed on 2024-04-28 05:26:02

0001 // SPDX-FileCopyrightText: 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include "errors.h"
0007 #include <QByteArray>
0008 #include <QVariant>
0009 
0010 #include <QDateTime>
0011 #include <functional>
0012 #include <memory>
0013 
0014 namespace Crypto
0015 {
0016 
0017 enum CryptoProtocol { UnknownProtocol, OpenPGP, CMS };
0018 
0019 struct UserId {
0020     QByteArray name;
0021     QByteArray email;
0022     QByteArray id;
0023 };
0024 
0025 struct Key {
0026     QByteArray keyId;
0027     QByteArray shortKeyId;
0028     QByteArray fingerprint;
0029     bool isUsable = false;
0030     std::vector<UserId> userIds;
0031 };
0032 
0033 struct Error {
0034     unsigned int error;
0035     operator bool() const
0036     {
0037         return error != 0;
0038     }
0039 };
0040 
0041 struct Signature {
0042     QByteArray fingerprint;
0043     Error status;
0044     QDateTime creationTime;
0045     enum Result { Ok, NotVerified, Expired, KeyNotFound, Invalid };
0046     Result result{NotVerified};
0047     bool isTrusted{false};
0048 };
0049 
0050 struct VerificationResult {
0051     std::vector<Signature> signatures;
0052     Error error;
0053 };
0054 
0055 struct Recipient {
0056     QByteArray keyId;
0057     bool secretKeyAvailable{false};
0058 };
0059 
0060 struct DecryptionResult {
0061     std::vector<Recipient> recipients;
0062     Error error;
0063     enum Result { NoError, NotEncrypted, PassphraseError, NoSecretKeyError, DecryptionError };
0064     Result result{NoError};
0065 };
0066 
0067 struct KeyListResult {
0068     std::vector<Key> keys;
0069     Error error;
0070 };
0071 
0072 struct ImportResult {
0073     int considered;
0074     int imported;
0075     int unchanged;
0076 };
0077 
0078 #ifndef _WIN32
0079 std::vector<Key> findKeys(const QStringList &filter, bool findPrivate = false, bool remote = false);
0080 
0081 Expected<Error, QByteArray> exportPublicKey(const Key &key);
0082 
0083 ImportResult importKey(CryptoProtocol protocol, const QByteArray &certData);
0084 ImportResult importKey(CryptoProtocol protocol, const Key &key);
0085 
0086 /**
0087  * Sign the given content and returns the signing data and the algorithm used
0088  * for integrity check in the "pgp-<algorithm>" format.
0089  */
0090 Expected<Error, std::pair<QByteArray, QString>> sign(const QByteArray &content, const std::vector<Key> &signingKeys);
0091 Expected<Error, QByteArray> signAndEncrypt(const QByteArray &content, const std::vector<Key> &encryptionKeys, const std::vector<Key> &signingKeys);
0092 
0093 std::pair<DecryptionResult, VerificationResult> decryptAndVerify(CryptoProtocol protocol, const QByteArray &ciphertext, QByteArray &outdata);
0094 DecryptionResult decrypt(CryptoProtocol protocol, const QByteArray &ciphertext, QByteArray &outdata);
0095 VerificationResult verifyDetachedSignature(CryptoProtocol protocol, const QByteArray &signature, const QByteArray &outdata);
0096 VerificationResult verifyOpaqueSignature(CryptoProtocol protocol, const QByteArray &signature, QByteArray &outdata);
0097 };
0098 #endif
0099 
0100 Q_DECLARE_METATYPE(Crypto::Key);
0101 
0102 QDebug operator<<(QDebug d, const Crypto::Key &);
0103 QDebug operator<<(QDebug d, const Crypto::Error &);