• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • crypto
certificateresolver.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/certificateresolver.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "certificateresolver.h"
36 
37 #include <models/keycache.h>
38 
39 #include <gpgme++/key.h>
40 
41 #include <KConfig>
42 #include <KConfigGroup>
43 
44 #include <QByteArray>
45 #include <QHash>
46 #include <QSet>
47 
48 #ifndef Q_MOC_RUN
49 #include <boost/bind.hpp>
50 #endif
51 
52 #include <algorithm>
53 #include <iterator>
54 
55 using namespace Kleo;
56 using namespace Kleo::Crypto;
57 using namespace boost;
58 using namespace GpgME;
59 using namespace KMime::Types;
60 using namespace KMime::HeaderParsing;
61 
62 std::vector< std::vector<Key> > CertificateResolver::resolveRecipients( const std::vector<Mailbox> & recipients, Protocol proto ) {
63  std::vector< std::vector<Key> > result;
64  std::transform( recipients.begin(), recipients.end(),
65  std::back_inserter( result ), boost::bind( &resolveRecipient, _1, proto ) );
66  return result;
67 }
68 
69 std::vector<Key> CertificateResolver::resolveRecipient( const Mailbox & recipient, Protocol proto ) {
70  std::vector<Key> result = KeyCache::instance()->findByEMailAddress( recipient.address() );
71  std::vector<Key>::iterator end = std::remove_if( result.begin(), result.end(),
72  !boost::bind( &Key::canEncrypt, _1 ) );
73 
74  if ( proto != UnknownProtocol )
75  end = std::remove_if( result.begin(), end,
76  boost::bind( &Key::protocol, _1 ) != proto );
77 
78  result.erase( end, result.end() );
79  return result;
80 }
81 
82 std::vector< std::vector<Key> > CertificateResolver::resolveSigners( const std::vector<Mailbox> & signers, Protocol proto ) {
83  std::vector< std::vector<Key> > result;
84  std::transform( signers.begin(), signers.end(),
85  std::back_inserter( result ), boost::bind( &resolveSigner, _1, proto ) );
86  return result;
87 }
88 
89 std::vector<Key> CertificateResolver::resolveSigner( const Mailbox & signer, Protocol proto ) {
90  std::vector<Key> result = KeyCache::instance()->findByEMailAddress( signer.address() );
91  std::vector<Key>::iterator end
92  = std::remove_if( result.begin(), result.end(),
93  !boost::bind( &Key::hasSecret, _1 ) );
94  end = std::remove_if( result.begin(), end,
95  !boost::bind( &Key::canReallySign, _1 ) );
96  if ( proto != UnknownProtocol )
97  end = std::remove_if( result.begin(), end,
98  boost::bind( &Key::protocol, _1 ) != proto );
99  result.erase( end, result.end() );
100  return result;
101 }
102 
103 class KConfigBasedRecipientPreferences::Private {
104  friend class ::Kleo::Crypto::KConfigBasedRecipientPreferences;
105  KConfigBasedRecipientPreferences* const q;
106 public:
107  explicit Private( KSharedConfigPtr config, KConfigBasedRecipientPreferences* qq );
108  ~Private();
109 
110 private:
111  void ensurePrefsParsed() const;
112  void writePrefs();
113 
114 private:
115  KSharedConfigPtr m_config;
116 
117  mutable QHash<QByteArray, QByteArray> pgpPrefs;
118  mutable QHash<QByteArray, QByteArray> cmsPrefs;
119  mutable bool m_parsed;
120  mutable bool m_dirty;
121 };
122 
123 KConfigBasedRecipientPreferences::Private::Private( KSharedConfigPtr config , KConfigBasedRecipientPreferences* qq ) : q( qq ), m_config( config ), m_parsed( false ), m_dirty( false )
124 {
125  assert( m_config );
126 }
127 
128 KConfigBasedRecipientPreferences::Private::~Private()
129 {
130  writePrefs();
131 }
132 
133 void KConfigBasedRecipientPreferences::Private::writePrefs()
134 {
135  if ( !m_dirty )
136  return;
137  const QSet<QByteArray> keys = pgpPrefs.keys().toSet() + cmsPrefs.keys().toSet();
138 
139  int n = 0;
140  Q_FOREACH ( const QByteArray& i, keys )
141  {
142  KConfigGroup group( m_config, QString::fromLatin1( "EncryptionPreference_%1" ).arg( n++ ) );
143  group.writeEntry( "email", i );
144  const QByteArray pgp = pgpPrefs.value( i );
145  if ( !pgp.isEmpty() )
146  group.writeEntry( "pgpCertificate", pgp );
147  const QByteArray cms = cmsPrefs.value( i );
148  if ( !cms.isEmpty() )
149  group.writeEntry( "cmsCertificate", cms );
150  }
151  m_config->sync();
152  m_dirty = false;
153 }
154 void KConfigBasedRecipientPreferences::Private::ensurePrefsParsed() const
155 {
156  if ( m_parsed )
157  return;
158  const QStringList groups = m_config->groupList().filter( QRegExp( QLatin1String("^EncryptionPreference_\\d+$") ) );
159 
160  Q_FOREACH ( const QString& i, groups )
161  {
162  const KConfigGroup group( m_config, i );
163  const QByteArray id = group.readEntry( "email", QByteArray() );
164  if ( id.isEmpty() )
165  continue;
166  pgpPrefs.insert( id, group.readEntry( "pgpCertificate", QByteArray() ) );
167  cmsPrefs.insert( id, group.readEntry( "cmsCertificate", QByteArray() ) );
168  }
169  m_parsed = true;
170 }
171 
172 KConfigBasedRecipientPreferences::KConfigBasedRecipientPreferences( KSharedConfigPtr config ) : d( new Private( config, this ) )
173 {
174 }
175 
176 
177 KConfigBasedRecipientPreferences::~KConfigBasedRecipientPreferences()
178 {
179  d->writePrefs();
180 }
181 
182 Key KConfigBasedRecipientPreferences::preferredCertificate( const Mailbox& recipient, Protocol protocol )
183 {
184  d->ensurePrefsParsed();
185 
186  const QByteArray keyId = ( protocol == CMS ? d->cmsPrefs : d->pgpPrefs ).value( recipient.address() );
187  return KeyCache::instance()->findByKeyIDOrFingerprint( keyId );
188 }
189 
190 void KConfigBasedRecipientPreferences::setPreferredCertificate( const Mailbox& recipient, Protocol protocol, const Key& certificate )
191 {
192  d->ensurePrefsParsed();
193  if ( !recipient.hasAddress() )
194  return;
195  ( protocol == CMS ? d->cmsPrefs : d->pgpPrefs ).insert( recipient.address(), certificate.keyID() );
196  d->m_dirty = true;
197 }
198 
199 class KConfigBasedSigningPreferences::Private {
200  friend class ::Kleo::Crypto::KConfigBasedSigningPreferences;
201  KConfigBasedSigningPreferences* const q;
202 public:
203  explicit Private( KSharedConfigPtr config, KConfigBasedSigningPreferences* qq );
204  ~Private();
205 
206 private:
207  void ensurePrefsParsed() const;
208  void writePrefs();
209 
210 private:
211  KSharedConfigPtr m_config;
212 
213  mutable QByteArray pgpSigningCertificate;
214  mutable QByteArray cmsSigningCertificate;
215  mutable bool m_parsed;
216  mutable bool m_dirty;
217 };
218 
219 KConfigBasedSigningPreferences::Private::Private( KSharedConfigPtr config , KConfigBasedSigningPreferences* qq ) : q( qq ), m_config( config ), m_parsed( false ), m_dirty( false )
220 {
221  assert( m_config );
222 }
223 
224 void KConfigBasedSigningPreferences::Private::ensurePrefsParsed() const
225 {
226  if ( m_parsed )
227  return;
228  const KConfigGroup group( m_config, "SigningPreferences" );
229  pgpSigningCertificate = group.readEntry( "pgpSigningCertificate", QByteArray() );
230  cmsSigningCertificate = group.readEntry( "cmsSigningCertificate", QByteArray() );
231  m_parsed = true;
232 }
233 
234 void KConfigBasedSigningPreferences::Private::writePrefs()
235 {
236  if ( !m_dirty )
237  return;
238  KConfigGroup group( m_config, "SigningPreferences" );
239  group.writeEntry( "pgpSigningCertificate", pgpSigningCertificate );
240  group.writeEntry( "cmsSigningCertificate", cmsSigningCertificate );
241  m_config->sync();
242  m_dirty = false;
243 }
244 
245 KConfigBasedSigningPreferences::Private::~Private()
246 {
247  writePrefs();
248 }
249 
250 KConfigBasedSigningPreferences::KConfigBasedSigningPreferences( KSharedConfigPtr config ) : d( new Private( config, this ) )
251 {
252 }
253 
254 
255 KConfigBasedSigningPreferences::~KConfigBasedSigningPreferences()
256 {
257  d->writePrefs();
258 }
259 
260 Key KConfigBasedSigningPreferences::preferredCertificate( Protocol protocol )
261 {
262  d->ensurePrefsParsed();
263 
264  const QByteArray keyId = ( protocol == CMS ? d->cmsSigningCertificate : d->pgpSigningCertificate );
265  const Key key = KeyCache::instance()->findByKeyIDOrFingerprint( keyId );
266  return key.hasSecret() ? key : Key::null;
267 }
268 
269 void KConfigBasedSigningPreferences::setPreferredCertificate( Protocol protocol, const Key& certificate )
270 {
271  d->ensurePrefsParsed();
272  ( protocol == CMS ? d->cmsSigningCertificate : d->pgpSigningCertificate ) = certificate.keyID();
273  d->m_dirty = true;
274 }
275 
Kleo::Crypto::KConfigBasedSigningPreferences::setPreferredCertificate
void setPreferredCertificate(GpgME::Protocol protocol, const GpgME::Key &certificate)
Definition: certificateresolver.cpp:269
Kleo::Crypto::KConfigBasedSigningPreferences
Definition: certificateresolver.h:81
Kleo::Crypto::KConfigBasedSigningPreferences::~KConfigBasedSigningPreferences
~KConfigBasedSigningPreferences()
Definition: certificateresolver.cpp:255
Kleo::Crypto::KConfigBasedRecipientPreferences
Definition: certificateresolver.h:69
QByteArray
Kleo::Crypto::CertificateResolver::resolveRecipients
static std::vector< std::vector< GpgME::Key > > resolveRecipients(const std::vector< KMime::Types::Mailbox > &recipients, GpgME::Protocol proto)
Definition: certificateresolver.cpp:62
QByteArray::isEmpty
bool isEmpty() const
QByteArray::insert
QByteArray & insert(int i, char ch)
Kleo::Crypto::KConfigBasedRecipientPreferences::~KConfigBasedRecipientPreferences
~KConfigBasedRecipientPreferences()
Definition: certificateresolver.cpp:177
Kleo::Crypto::KConfigBasedRecipientPreferences::setPreferredCertificate
void setPreferredCertificate(const KMime::Types::Mailbox &recipient, GpgME::Protocol protocol, const GpgME::Key &certificate)
Definition: certificateresolver.cpp:190
QRegExp
d
#define d
Definition: adduseridcommand.cpp:89
Kleo::Crypto::CertificateResolver::resolveSigners
static std::vector< std::vector< GpgME::Key > > resolveSigners(const std::vector< KMime::Types::Mailbox > &signers, GpgME::Protocol proto)
Definition: certificateresolver.cpp:82
Kleo::Crypto::CertificateResolver::resolveRecipient
static std::vector< GpgME::Key > resolveRecipient(const KMime::Types::Mailbox &recipient, GpgME::Protocol proto)
Definition: certificateresolver.cpp:69
QHash< QByteArray, QByteArray >
Kleo::Crypto::KConfigBasedSigningPreferences::KConfigBasedSigningPreferences
KConfigBasedSigningPreferences(KSharedConfigPtr config)
Definition: certificateresolver.cpp:250
Kleo::Class::CMS
Definition: classify.h:48
QSet
QString
QStringList
Kleo::Crypto::KConfigBasedRecipientPreferences::preferredCertificate
GpgME::Key preferredCertificate(const KMime::Types::Mailbox &recipient, GpgME::Protocol protocol)
Definition: certificateresolver.cpp:182
QLatin1String
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::KeyCache::instance
static boost::shared_ptr< const KeyCache > instance()
Definition: keycache.cpp:190
Kleo::Crypto::CertificateResolver::resolveSigner
static std::vector< GpgME::Key > resolveSigner(const KMime::Types::Mailbox &signer, GpgME::Protocol proto)
Definition: certificateresolver.cpp:89
QString::fromLatin1
QString fromLatin1(const char *str, int size)
certificateresolver.h
QStringList::filter
QStringList filter(const QString &str, Qt::CaseSensitivity cs) const
Kleo::Crypto::KConfigBasedRecipientPreferences::KConfigBasedRecipientPreferences
KConfigBasedRecipientPreferences(KSharedConfigPtr config)
Definition: certificateresolver.cpp:172
keycache.h
Kleo::Crypto::KConfigBasedSigningPreferences::preferredCertificate
GpgME::Key preferredCertificate(GpgME::Protocol protocol)
Definition: certificateresolver.cpp:260
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal