4 This file is part of libkleopatra, the KDE keymanagement library
5 Copyright (c) 2004 Klarälvdalens Datakonsult AB
8 Copyright (C) 2001,2002 the KPGP authors
9 See file libkdenetwork/AUTHORS.kpgp for details
11 Libkleopatra is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
16 Libkleopatra is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 In addition, as a special exception, the copyright holders give
26 permission to link the code of this program with any edition of
27 the Qt library by Trolltech AS, Norway (or with modified versions
28 of Qt that use the same license as Qt), and distribute linked
29 combinations including the two. You must obey the GNU General
30 Public License in all respects for all of the code used other than
31 Qt. If you modify this file, you may extend this exception to
32 your version of the file, but you are not obligated to do so. If
33 you do not wish to do so, delete this exception statement from
37 #ifndef __KLEO_KEYRESOLVER_H__
38 #define __KLEO_KEYRESOLVER_H__
40 #include "messagecomposer_export.h"
41 #include "libkleo/ui/keyapprovaldialog.h"
42 #include "kleo/enum.h"
44 #include <libkpgp/kpgp.h> // for Kpgp::Result
45 #include <gpgme++/key.h>
59 \short A class to resolve signing/encryption keys w.r.t. per-recipient preferences
61 \section Step 1: Set the information needed
63 The constructor takes some basic options as arguments, such as
64 whether or not encryption was actually requested. Recipient and
65 sender information is then set by using \c
66 setEncryptToSelfKeys(), \c setSigningKeys(), \c
67 setPrimaryRecipients() (To/Cc) and \c setSecondaryRecipients()
70 \section Step 2: Lookup and check per-recipient crypto preferences / Opportunistic Encryption
72 First, \c checkSigningPreferences() goes through all recipient's
73 signing perferences, to determine whether or not to sign. It also
74 takes into account the available signing keys and whether or not
75 the user explicitly requested signing.
77 \c checkEncryptionPreferences() does the same for encryption
78 preferences. If opportunistic encryption is enabled, recipients
79 without encryption preferences set are treated as if they had a
80 preference of \c AskWheneverPossible.
82 In both cases an Action code is returned, with the following
85 <dl><dt>Conflict</dt><dd>A conflict was detected. E.g. one
86 recipient's preference was set to "always encrypt", while another
87 one's preference was set to "never encrypt". You should ask the
88 user what to do.</dd></dt>
90 <dt>DoIt, DontDoIt</dt><dd>Do/Don't sign/encrypt</dd>
92 <dt>Ask</dt><dd>(Some) crypto preferences request to prompt the
95 <dt>Impossible</dt><dd>Signing or encryption is impossible,
96 e.g. due to missing keys or unsupported formats.</dd> </dl>
98 \section Step 3: Resolve all keys.
100 In case signing or encryption was implicitly or explicitly
101 requested by the user, \c resolveAllKeys() tries to find signing
102 keys for each required format, as well as encryption keys for all
103 recipients (incl. the sender, if encrypt-to-self is set).
105 \section Step 4: Get signing keys.
107 If, after key resolving, signing is still requested and
108 apparently possible, you can get the result of all this by
109 iterating over the available message formats and retrieving the
110 set of signing keys to use with a call to \c signingKeys().
112 \section Step 5: Get encrytion key sets.
114 If after key resolving, encryption is still requested and
115 apparently possible, you can get the result of all this by
116 calling \c encryptionItems() with the current message format at
117 hand as its argument.
119 This will return a list of recipient-list/key-list pairs that
120 each describe a copy of the (possibly signed) message to be
121 encrypted independantly.
123 Note that it's only necessary to sign the message once for each
124 message format, although it might be necessary to create more
125 than one message when encrypting. This is because encryption
126 allows the recipients to learn about the other recipients the
127 message was encrypted to, so each secondary (BCC) recipient need
128 a copy of it's own to hide the other secondary recipients.
131 class MESSAGECOMPOSER_EXPORT KeyResolver
{
133 KeyResolver( bool encToSelf
, bool showApproval
, bool oppEncryption
,
135 int encrKeyNearExpiryThresholdDays
,
136 int signKeyNearExpiryThresholdDays
,
137 int encrRootCertNearExpiryThresholdDays
,
138 int signRootCertNearExpiryThresholdDays
,
139 int encrChainCertNearExpiryThresholdDays
,
140 int signChainCertNearExpiryThresholdDays
);
144 struct Item
: public KeyApprovalDialog::Item
{
146 : KeyApprovalDialog::Item(),
147 signPref( UnknownSigningPreference
),
148 format( AutoFormat
),
150 Item( const QString
& a
,
151 EncryptionPreference e
, SigningPreference s
,
152 CryptoMessageFormat f
)
153 : KeyApprovalDialog::Item( a
, std::vector
<GpgME::Key
>(), e
),
154 signPref( s
), format( f
), needKeys( true ) {}
155 Item( const QString
& a
, const std::vector
<GpgME::Key
> & k
,
156 EncryptionPreference e
, SigningPreference s
,
157 CryptoMessageFormat f
)
158 : KeyApprovalDialog::Item( a
, k
, e
),
159 signPref( s
), format( f
), needKeys( false ) {}
161 SigningPreference signPref
;
162 CryptoMessageFormat format
;
168 Set the fingerprints of keys to be used for encrypting to
169 self. Also looks them up and complains if they're not usable or
172 Kpgp::Result
setEncryptToSelfKeys( const QStringList
& fingerprints
);
174 Set the fingerprints of keys to be used for signing. Also
175 looks them up and complains if they're not usable or found.
177 Kpgp::Result
setSigningKeys( const QStringList
& fingerprints
);
179 Set the list of primary (To/CC) recipient addresses. Also looks
180 up possible keys, but doesn't interact with the user.
182 void setPrimaryRecipients( const QStringList
& addresses
);
184 Set the list of secondary (BCC) recipient addresses. Also looks
185 up possible keys, but doesn't interact with the user.
187 void setSecondaryRecipients( const QStringList
& addresses
);
191 Determine whether to sign or not, depending on the
192 per-recipient signing preferences, as well as the availability
193 of usable signing keys.
195 Action
checkSigningPreferences( bool signingRequested
) const;
197 Determine whether to encrypt or not, depending on the
198 per-recipient encryption preferences, as well as the availability
199 of usable encryption keys.
201 Action
checkEncryptionPreferences( bool encryptionRequested
) const;
204 Queries the user for missing keys and displays a key approval
207 Kpgp::Result
resolveAllKeys( bool& signingRequested
, bool& encryptionRequested
);
210 @return the signing keys to use (if any) for the given message
213 std::vector
<GpgME::Key
> signingKeys( CryptoMessageFormat f
) const;
217 SplitInfo( const QStringList
& r
) : recipients( r
) {}
218 SplitInfo( const QStringList
& r
, const std::vector
<GpgME::Key
> & k
)
219 : recipients( r
), keys( k
) {}
220 QStringList recipients
;
221 std::vector
<GpgME::Key
> keys
;
223 /** @return the found distinct sets of items for format \a f. The
224 returned vector will contain more than one item only if
225 secondary recipients have been specified.
227 std::vector
<SplitInfo
> encryptionItems( CryptoMessageFormat f
) const;
231 std::vector
<Item
> getEncryptionItems( const QStringList
& recipients
);
232 std::vector
<GpgME::Key
> getEncryptionKeys( const QString
& recipient
, bool quiet
) const;
234 Kpgp::Result
showKeyApprovalDialog();
236 bool encryptionPossible() const;
237 bool signingPossible() const;
238 Kpgp::Result
resolveEncryptionKeys( bool signingRequested
);
239 Kpgp::Result
resolveSigningKeysForEncryption();
240 Kpgp::Result
resolveSigningKeysForSigningOnly();
241 Kpgp::Result
checkKeyNearExpiry( const GpgME::Key
& key
,
242 const char * dontAskAgainName
, bool mine
,
243 bool sign
, bool ca
=false, int recurse_limit
=100,
244 const GpgME::Key
& orig_key
=GpgME::Key::null
) const;
245 void collapseAllSplitInfos();
246 void addToAllSplitInfos( const std::vector
<GpgME::Key
> & keys
, unsigned int formats
);
247 void addKeys( const std::vector
<Item
> & items
, CryptoMessageFormat f
);
248 void addKeys( const std::vector
<Item
> & items
);
249 QStringList
allRecipients() const;
250 std::vector
<GpgME::Key
> signingKeysFor( CryptoMessageFormat f
) const;
251 std::vector
<GpgME::Key
> encryptToSelfKeysFor( CryptoMessageFormat f
) const;
253 std::vector
<GpgME::Key
> lookup( const QStringList
& patterns
, bool secret
=false ) const;
255 bool haveTrustedEncryptionKey( const QString
& person
) const;
257 std::vector
<GpgME::Key
> selectKeys( const QString
& person
, const QString
& msg
,
258 const std::vector
<GpgME::Key
> & selectedKeys
=std::vector
<GpgME::Key
>() ) const;
260 QStringList
keysForAddress( const QString
& address
) const;
261 void setKeysForAddress( const QString
& address
, const QStringList
& pgpKeyFingerprints
, const QStringList
& smimeCertFingerprints
) const;
263 bool encryptToSelf() const { return mEncryptToSelf
; }
264 bool showApprovalDialog() const { return mShowApprovalDialog
; }
266 int encryptKeyNearExpiryWarningThresholdInDays() const {
267 return mEncryptKeyNearExpiryWarningThreshold
;
269 int signingKeyNearExpiryWarningThresholdInDays() const {
270 return mSigningKeyNearExpiryWarningThreshold
;
273 int encryptRootCertNearExpiryWarningThresholdInDays() const {
274 return mEncryptRootCertNearExpiryWarningThreshold
;
276 int signingRootCertNearExpiryWarningThresholdInDays() const {
277 return mSigningRootCertNearExpiryWarningThreshold
;
280 int encryptChainCertNearExpiryWarningThresholdInDays() const {
281 return mEncryptChainCertNearExpiryWarningThreshold
;
283 int signingChainCertNearExpiryWarningThresholdInDays() const {
284 return mSigningChainCertNearExpiryWarningThreshold
;
287 struct ContactPreferences
{
288 ContactPreferences();
289 Kleo::EncryptionPreference encryptionPreference
;
290 Kleo::SigningPreference signingPreference
;
291 Kleo::CryptoMessageFormat cryptoMessageFormat
;
292 QStringList pgpKeyFingerprints
;
293 QStringList smimeCertFingerprints
;
296 ContactPreferences
lookupContactPreferences( const QString
& address
) const;
297 void writeCustomContactProperties( KABC::Addressee
&contact
, const ContactPreferences
& pref
) const;
298 void saveContactPreference( const QString
& email
, const ContactPreferences
& pref
) const;
301 class EncryptionPreferenceCounter
;
302 friend class ::Kleo::KeyResolver::EncryptionPreferenceCounter
;
303 class SigningPreferenceCounter
;
304 friend class ::Kleo::KeyResolver::SigningPreferenceCounter
;
310 const bool mShowApprovalDialog
: 1;
311 const bool mOpportunisticEncyption
: 1;
312 const unsigned int mCryptoMessageFormats
;
314 const int mEncryptKeyNearExpiryWarningThreshold
;
315 const int mSigningKeyNearExpiryWarningThreshold
;
316 const int mEncryptRootCertNearExpiryWarningThreshold
;
317 const int mSigningRootCertNearExpiryWarningThreshold
;
318 const int mEncryptChainCertNearExpiryWarningThreshold
;
319 const int mSigningChainCertNearExpiryWarningThreshold
;
324 #endif // __KLEO_KEYRESOLVER_H__