File indexing completed on 2024-05-12 05:26:16

0001 /*
0002     Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #pragma once
0021 
0022 #include "errors.h"
0023 
0024 #include <QByteArray>
0025 #include <QVariant>
0026 
0027 #include <functional>
0028 #include <memory>
0029 #include <QDateTime>
0030 
0031 namespace Crypto {
0032 
0033 enum CryptoProtocol {
0034     UnknownProtocol,
0035     OpenPGP,
0036     CMS
0037 };
0038 
0039 #ifndef SINK_EXPORT
0040 #define SINK_EXPORT
0041 #endif
0042 
0043 struct SINK_EXPORT UserId {
0044     QByteArray name;
0045     QByteArray email;
0046     QByteArray id;
0047 };
0048 
0049 struct SINK_EXPORT Key {
0050     QByteArray keyId;
0051     QByteArray shortKeyId;
0052     QByteArray fingerprint;
0053     bool isUsable = false;
0054     std::vector<UserId> userIds;
0055 };
0056 
0057 struct SINK_EXPORT Error {
0058     unsigned int error;
0059     operator bool() const
0060     {
0061         return error != 0;
0062     }
0063 };
0064 
0065 struct SINK_EXPORT Signature {
0066     QByteArray fingerprint;
0067     Error status;
0068     QDateTime creationTime;
0069     enum Result {
0070         Ok,
0071         NotVerified,
0072         Expired,
0073         KeyNotFound,
0074         Invalid
0075     };
0076     Result result{NotVerified};
0077     bool isTrusted{false};
0078 };
0079 
0080 struct SINK_EXPORT VerificationResult {
0081     std::vector<Signature> signatures;
0082     Error error;
0083 };
0084 
0085 struct SINK_EXPORT Recipient {
0086     QByteArray keyId;
0087     bool secretKeyAvailable{false};
0088 };
0089 
0090 struct SINK_EXPORT DecryptionResult {
0091     std::vector<Recipient> recipients;
0092     Error error;
0093     enum Result {
0094         NoError,
0095         NotEncrypted,
0096         PassphraseError,
0097         NoSecretKeyError,
0098         DecryptionError
0099     };
0100     Result result{NoError};
0101 };
0102 
0103 struct SINK_EXPORT KeyListResult {
0104     std::vector<Key> keys;
0105     Error error;
0106 };
0107 
0108 std::vector<Key> SINK_EXPORT findKeys(const QStringList &filter, bool findPrivate = false, bool remote = false);
0109 
0110 Expected<Error, QByteArray> SINK_EXPORT exportPublicKey(const Key &key);
0111 
0112 struct SINK_EXPORT ImportResult {
0113     int considered;
0114     int imported;
0115     int unchanged;
0116 };
0117 ImportResult SINK_EXPORT importKey(CryptoProtocol protocol, const QByteArray &certData);
0118 ImportResult SINK_EXPORT importKey(CryptoProtocol protocol, const Key &key);
0119 
0120 /**
0121  * Sign the given content and returns the signing data and the algorithm used
0122  * for integrity check in the "pgp-<algorithm>" format.
0123  */
0124 Expected<Error, std::pair<QByteArray, QString>> SINK_EXPORT sign(const QByteArray &content, const std::vector<Key> &signingKeys);
0125 Expected<Error, QByteArray> SINK_EXPORT signAndEncrypt(const QByteArray &content, const std::vector<Key> &encryptionKeys, const std::vector<Key> &signingKeys);
0126 
0127 std::pair<DecryptionResult,VerificationResult> SINK_EXPORT decryptAndVerify(CryptoProtocol protocol, const QByteArray &ciphertext, QByteArray &outdata);
0128 DecryptionResult SINK_EXPORT decrypt(CryptoProtocol protocol, const QByteArray &ciphertext, QByteArray &outdata);
0129 VerificationResult SINK_EXPORT verifyDetachedSignature(CryptoProtocol protocol, const QByteArray &signature, const QByteArray &outdata);
0130 VerificationResult SINK_EXPORT verifyOpaqueSignature(CryptoProtocol protocol, const QByteArray &signature, QByteArray &outdata);
0131 };
0132 
0133 Q_DECLARE_METATYPE(Crypto::Key);
0134 
0135 QDebug SINK_EXPORT operator<< (QDebug d, const Crypto::Key &);
0136 QDebug SINK_EXPORT operator<< (QDebug d, const Crypto::Error &);