summaryrefslogtreecommitdiffstats
path: root/kmail/keyresolver.h
blob: c7ec0430dba02bb81fec5b6495ef39f57b267697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*  -*- c++ -*-
    keyresolver.h

    This file is part of libkleopatra, the KDE keymanagement library
    Copyright (c) 2004 Klar�lvdalens Datakonsult AB

    Based on kpgp.h
    Copyright (C) 2001,2002 the KPGP authors
    See file libkdenetwork/AUTHORS.kpgp for details

    Libkleopatra is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    Libkleopatra is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#ifndef __KLEO_KEYRESOLVER_H__
#define __KLEO_KEYRESOLVER_H__

#include <ui/keyapprovaldialog.h>

#include <kleo/enum.h>

#include <kpgp.h> // for Kpgp::Result
#include <gpgmepp/key.h>

#include <vector>

class TQStringList;

namespace Kleo {


  /**
     \short A class to resolve signing/encryption keys w.r.t. per-recipient preferences

     \section Step 1: Set the information needed

     The constructor takes some basic options as arguments, such as
     whether or not encryption was actually requested. Recipient and
     sender information is then set by using \c
     setEncryptToSelfKeys(), \c setSigningKeys(), \c
     setPrimaryRecipients() (To/Cc) and \c setSecondaryRecipients()
     (Bcc).

     \section Step 2: Lookup and check per-recipient crypto preferences / Opportunistic Encryption

     First, \c checkSigningPreferences() goes through all recipient's
     signing perferences, to determine whether or not to sign. It also
     takes into account the available signing keys and whether or not
     the user explicitly requested signing.

     \c checkEncryptionPreferences() does the same for encryption
     preferences. If opportunistic encryption is enabled, recipients
     without encryption preferences set are treated as if they had a
     preference of \c AskWheneverPossible.

     In both cases an Action code is returned, with the following
     meanings:

     <dl><dt>Conflict</dt><dd>A conflict was detected. E.g. one
     recipient's preference was set to "always encrypt", while another
     one's preference was set to "never encrypt". You should ask the
     user what to do.</dd></dt>

     <dt>DoIt, DontDoIt</dt><dd>Do/Don't sign/encrypt</dd>

     <dt>Ask</dt><dd>(Some) crypto preferences request to prompt the
     user, so do it.</dd>

     <dt>Impossible</dt><dd>Signing or encryption is impossible,
     e.g. due to missing keys or unsupported formats.</dd> </dl>

     \section Step 3: Resolve all keys.

     In case signing or encryption was implicitly or explicitly
     requested by the user, \c resolveAllKeys() tries to find signing
     keys for each required format, as well as encryption keys for all
     recipients (incl. the sender, if encrypt-to-self is set).

     \section Step 4: Get signing keys.

     If, after key resolving, signing is still requested and
     apparently possible, you can get the result of all this by
     iterating over the available message formats and retrieving the
     set of signing keys to use with a call to \c signingKeys().

     \section Step 5: Get encrytion key sets.

     If after key resolving, encryption is still requested and
     apparently possible, you can get the result of all this by
     calling \c encryptionItems() with the current message format at
     hand as its argument.

     This will return a list of recipient-list/key-list pairs that
     each describe a copy of the (possibly signed) message to be
     encrypted independantly.

     Note that it's only necessary to sign the message once for each
     message format, although it might be necessary to create more
     than one message when encrypting. This is because encryption
     allows the recipients to learn about the other recipients the
     message was encrypted to, so each secondary (BCC) recipient need
     a copy of it's own to hide the other secondary recipients.
    */

  class KeyResolver {
  public:
    KeyResolver( bool encToSelf, bool showApproval, bool oppEncryption,
		 unsigned int format,
		 int encrKeyNearExpiryThresholdDays,
		 int signKeyNearExpiryThresholdDays,
		 int encrRootCertNearExpiryThresholdDays,
		 int signRootCertNearExpiryThresholdDays,
		 int encrChainCertNearExpiryThresholdDays,
		 int signChainCertNearExpiryThresholdDays );

    ~KeyResolver();

    struct Item : public KeyApprovalDialog::Item {
      Item()
	: KeyApprovalDialog::Item(),
	  signPref( UnknownSigningPreference ),
	  format( AutoFormat ),
	  needKeys( true ) {}
      Item( const TQString & a,
	    EncryptionPreference e, SigningPreference s,
	    CryptoMessageFormat f )
	: KeyApprovalDialog::Item( a, std::vector<GpgME::Key>(), e ),
	  signPref( s ), format( f ), needKeys( true ) {}
      Item( const TQString & a, const std::vector<GpgME::Key> & k,
	    EncryptionPreference e, SigningPreference s,
	    CryptoMessageFormat f )
	: KeyApprovalDialog::Item( a, k, e ),
	  signPref( s ), format( f ), needKeys( false ) {}

      SigningPreference signPref;
      CryptoMessageFormat format;
      bool needKeys;
    };


    /**
       Set the fingerprints of keys to be used for encrypting to
       self. Also looks them up and complains if they're not usable or
       found.
    */
    Kpgp::Result setEncryptToSelfKeys( const TQStringList & fingerprints );
    /**
	Set the fingerprints of keys to be used for signing. Also
	looks them up and complains if they're not usable or found.
    */
    Kpgp::Result setSigningKeys( const TQStringList & fingerprints );
    /**
       Set the list of primary (To/CC) recipient addresses. Also looks
       up possible keys, but doesn't interact with the user.
    */
    void setPrimaryRecipients( const TQStringList & addresses );
    /**
       Set the list of secondary (BCC) recipient addresses. Also looks
       up possible keys, but doesn't interact with the user.
    */
    void setSecondaryRecipients( const TQStringList & addresses );


    /**
       Determine whether to sign or not, depending on the
       per-recipient signing preferences, as well as the availability
       of usable signing keys.
    */
    Action checkSigningPreferences( bool signingRequested ) const;
    /**
       Determine whether to encrypt or not, depending on the
       per-recipient encryption preferences, as well as the availability
       of usable encryption keys.
    */
    Action checkEncryptionPreferences( bool encryptionRequested ) const;

    /**
       Queries the user for missing keys and displays a key approval
       dialog if needed.
    */
    Kpgp::Result resolveAllKeys( bool& signingRequested, bool& encryptionRequested );

    /**
       @return the signing keys to use (if any) for the given message
       format.
    */
    std::vector<GpgME::Key> signingKeys( CryptoMessageFormat f ) const;

    struct SplitInfo {
      SplitInfo() {}
      SplitInfo( const TQStringList & r ) : recipients( r ) {}
      SplitInfo( const TQStringList & r, const std::vector<GpgME::Key> & k )
	: recipients( r ), keys( k ) {}
      TQStringList recipients;
      std::vector<GpgME::Key> keys;
    };
    /** @return the found distinct sets of items for format \a f.  The
	returned vector will contain more than one item only if
	secondary recipients have been specified.
    */
    std::vector<SplitInfo> encryptionItems( CryptoMessageFormat f ) const;

  private:
    void dump() const;
    std::vector<Item> getEncryptionItems( const TQStringList & recipients );
    std::vector<GpgME::Key> getEncryptionKeys( const TQString & recipient, bool quiet ) const;

    Kpgp::Result showKeyApprovalDialog();

    bool encryptionPossible() const;
    bool signingPossible() const;
    Kpgp::Result resolveEncryptionKeys( bool signingRequested );
    Kpgp::Result resolveSigningKeysForEncryption();
    Kpgp::Result resolveSigningKeysForSigningOnly();
    Kpgp::Result checkKeyNearExpiry( const GpgME::Key & key,
				     const char * dontAskAgainName, bool mine,
				     bool sign, bool ca=false, int recurse_limit=100,
				     const GpgME::Key & orig_key=GpgME::Key::null ) const;
    void collapseAllSplitInfos();
    void addToAllSplitInfos( const std::vector<GpgME::Key> & keys, unsigned int formats );
    void addKeys( const std::vector<Item> & items, CryptoMessageFormat f );
    void addKeys( const std::vector<Item> & items );
    TQStringList allRecipients() const;
    std::vector<GpgME::Key> signingKeysFor( CryptoMessageFormat f ) const;
    std::vector<GpgME::Key> encryptToSelfKeysFor( CryptoMessageFormat f ) const;

    std::vector<GpgME::Key> lookup( const TQStringList & patterns, bool secret=false ) const;

    bool haveTrustedEncryptionKey( const TQString & person ) const;

    std::vector<GpgME::Key> selectKeys( const TQString & person, const TQString & msg,
					const std::vector<GpgME::Key> & selectedKeys=std::vector<GpgME::Key>() ) const;

    TQStringList keysForAddress( const TQString & address ) const;
    void setKeysForAddress( const TQString & address, const TQStringList& pgpKeyFingerprints, const TQStringList& smimeCertFingerprints ) const;

    bool encryptToSelf() const { return mEncryptToSelf; }
    bool showApprovalDialog() const { return mShowApprovalDialog; }

    int encryptKeyNearExpiryWarningThresholdInDays() const {
      return mEncryptKeyNearExpiryWarningThreshold;
    }
    int signingKeyNearExpiryWarningThresholdInDays() const {
      return mSigningKeyNearExpiryWarningThreshold;
    }

    int encryptRootCertNearExpiryWarningThresholdInDays() const {
      return mEncryptRootCertNearExpiryWarningThreshold;
    }
    int signingRootCertNearExpiryWarningThresholdInDays() const {
      return mSigningRootCertNearExpiryWarningThreshold;
    }

    int encryptChainCertNearExpiryWarningThresholdInDays() const {
      return mEncryptChainCertNearExpiryWarningThreshold;
    }
    int signingChainCertNearExpiryWarningThresholdInDays() const {
      return mSigningChainCertNearExpiryWarningThreshold;
    }

    struct ContactPreferences {
      ContactPreferences();
      Kleo::EncryptionPreference encryptionPreference;
      Kleo::SigningPreference signingPreference;
      Kleo::CryptoMessageFormat cryptoMessageFormat;
      TQStringList pgpKeyFingerprints;
      TQStringList smimeCertFingerprints;
    };

    ContactPreferences lookupContactPreferences( const TQString& address ) const;
    void saveContactPreference( const TQString& email, const ContactPreferences& pref ) const;

  private:
    class EncryptionPreferenceCounter;
    friend class ::Kleo::KeyResolver::EncryptionPreferenceCounter;
    class SigningPreferenceCounter;
    friend class ::Kleo::KeyResolver::SigningPreferenceCounter;

    class Private;
    Private * d;

    bool mEncryptToSelf;
    const bool mShowApprovalDialog : 1;
    const bool mOpportunisticEncyption : 1;
    const unsigned int mCryptoMessageFormats;

    const int mEncryptKeyNearExpiryWarningThreshold;
    const int mSigningKeyNearExpiryWarningThreshold;
    const int mEncryptRootCertNearExpiryWarningThreshold;
    const int mSigningRootCertNearExpiryWarningThreshold;
    const int mEncryptChainCertNearExpiryWarningThreshold;
    const int mSigningChainCertNearExpiryWarningThreshold;
  };

} // namespace Kleo

#endif // __KLEO_KEYRESOLVER_H__