Messagelib

autocryptkeyresolvercore.cpp
1/* SPDX-FileCopyrightText: 2023 Sandro Knauß <sknauss@kde.org>
2 SPDX-License-Identifier: GPL-2.0-or-later
3*/
4
5#include "composer/autocryptkeyresolvercore.h"
6
7#include <Akonadi/ContactSearchJob>
8#include <MessageComposer/ContactPreference>
9#include <MessageCore/AutocryptStorage>
10
11#include <messagecomposer_debug.h>
12
13using namespace MessageComposer;
14
15class MessageComposer::AutocryptKeyResolverCorePrivate
16{
17public:
18 QStringList autocrypt_keys;
19 QStringList gossip_keys;
20};
21
22AutocryptKeyResolverCore::AutocryptKeyResolverCore(bool encrypt, bool sign, GpgME::Protocol format)
23 : Kleo::KeyResolverCore(encrypt, sign, format)
24 , d(new AutocryptKeyResolverCorePrivate)
25{
26}
27
28AutocryptKeyResolverCore::~AutocryptKeyResolverCore() = default;
29
30Kleo::KeyResolverCore::Result AutocryptKeyResolverCore::resolve()
31{
32 auto result = Kleo::KeyResolverCore::resolve();
33
34 if ((result.flags & Kleo::KeyResolverCore::AllResolved)) {
35 qCDebug(MESSAGECOMPOSER_LOG) << "We found already for all recipient keys and we don't need to add Autocrypt keys.";
36 return result;
37 }
38
39 if (!(result.flags & Kleo::KeyResolverCore::OpenPGPOnly)) {
40 qCWarning(MESSAGECOMPOSER_LOG) << "Autocrypt is only defined for OpenPGP not for SMIME";
41 return result;
42 }
43
44 Kleo::KeyResolver::Solution *solution = &result.solution;
45
46 if (solution->protocol != GpgME::OpenPGP) {
47 solution = &result.alternative;
48 }
49
50 Q_ASSERT(solution->protocol == GpgME::OpenPGP);
51
52 bool allResolved = true;
53 const auto storage = MessageCore::AutocryptStorage::self();
54 for (const auto &recipient : solution->encryptionKeys.keys()) {
55 auto &keys = solution->encryptionKeys[recipient];
56 if (keys.size() > 0) { // already keys found
57 continue;
58 }
59 if (recipient == normalizedSender()) { // Own key needs to be in normal key store (Autocrypt do not offer private keys)
60 allResolved = false;
61 continue;
62 }
63
64 const auto rec = storage->getRecipient(recipient.toUtf8());
65 GpgME::Key autocryptKey;
66 if (rec) {
67 const auto key = rec->gpgKey();
68 if (!key.isBad() && key.canEncrypt()) {
69 d->autocrypt_keys.push_back(recipient);
70 autocryptKey = std::move(key);
71 } else {
72 const auto gossipKey = rec->gossipKey();
73 if (!gossipKey.isBad() && gossipKey.canEncrypt()) {
74 d->gossip_keys.push_back(recipient);
75 autocryptKey = std::move(gossipKey);
76 }
77 }
78 }
79 if (!autocryptKey.isNull()) {
80 keys.push_back(autocryptKey);
81 } else {
82 allResolved = false;
83 }
84 }
85 if (allResolved) {
86 result.flags = Kleo::KeyResolverCore::SolutionFlags(result.flags | Kleo::KeyResolverCore::AllResolved);
87 if (solution == &result.alternative) {
88 const auto _tmp = std::move(result.solution);
89 result.solution = std::move(result.alternative);
90 result.alternative = std::move(_tmp);
91 }
92 }
93
94 return result;
95}
96
97bool AutocryptKeyResolverCore::isAutocryptKey(const QString &recipient) const
98{
99 return d->autocrypt_keys.contains(recipient) || isGossipKey(recipient);
100}
101
102bool AutocryptKeyResolverCore::isGossipKey(const QString &recipient) const
103{
104 return d->gossip_keys.contains(recipient);
105}
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QMap< QString, std::vector< GpgME::Key > > encryptionKeys
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:43:26 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.