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

kleopatra

  • sources
  • kde-4.12
  • 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 #include <boost/bind.hpp>
49 
50 #include <algorithm>
51 #include <iterator>
52 
53 using namespace Kleo;
54 using namespace Kleo::Crypto;
55 using namespace boost;
56 using namespace GpgME;
57 using namespace KMime::Types;
58 using namespace KMime::HeaderParsing;
59 
60 std::vector< std::vector<Key> > CertificateResolver::resolveRecipients( const std::vector<Mailbox> & recipients, Protocol proto ) {
61  std::vector< std::vector<Key> > result;
62  std::transform( recipients.begin(), recipients.end(),
63  std::back_inserter( result ), boost::bind( &resolveRecipient, _1, proto ) );
64  return result;
65 }
66 
67 std::vector<Key> CertificateResolver::resolveRecipient( const Mailbox & recipient, Protocol proto ) {
68  std::vector<Key> result = KeyCache::instance()->findByEMailAddress( recipient.address() );
69  std::vector<Key>::iterator end = std::remove_if( result.begin(), result.end(),
70  !boost::bind( &Key::canEncrypt, _1 ) );
71 
72  if ( proto != UnknownProtocol )
73  end = std::remove_if( result.begin(), end,
74  boost::bind( &Key::protocol, _1 ) != proto );
75 
76  result.erase( end, result.end() );
77  return result;
78 }
79 
80 std::vector< std::vector<Key> > CertificateResolver::resolveSigners( const std::vector<Mailbox> & signers, Protocol proto ) {
81  std::vector< std::vector<Key> > result;
82  std::transform( signers.begin(), signers.end(),
83  std::back_inserter( result ), boost::bind( &resolveSigner, _1, proto ) );
84  return result;
85 }
86 
87 std::vector<Key> CertificateResolver::resolveSigner( const Mailbox & signer, Protocol proto ) {
88  std::vector<Key> result = KeyCache::instance()->findByEMailAddress( signer.address() );
89  std::vector<Key>::iterator end
90  = std::remove_if( result.begin(), result.end(),
91  !boost::bind( &Key::hasSecret, _1 ) );
92  end = std::remove_if( result.begin(), end,
93  !boost::bind( &Key::canReallySign, _1 ) );
94  if ( proto != UnknownProtocol )
95  end = std::remove_if( result.begin(), end,
96  boost::bind( &Key::protocol, _1 ) != proto );
97  result.erase( end, result.end() );
98  return result;
99 }
100 
101 class KConfigBasedRecipientPreferences::Private {
102  friend class ::Kleo::Crypto::KConfigBasedRecipientPreferences;
103  KConfigBasedRecipientPreferences* const q;
104 public:
105  explicit Private( KSharedConfigPtr config, KConfigBasedRecipientPreferences* qq );
106  ~Private();
107 
108 private:
109  void ensurePrefsParsed() const;
110  void writePrefs();
111 
112 private:
113  KSharedConfigPtr m_config;
114 
115  mutable QHash<QByteArray, QByteArray> pgpPrefs;
116  mutable QHash<QByteArray, QByteArray> cmsPrefs;
117  mutable bool m_parsed;
118  mutable bool m_dirty;
119 };
120 
121 KConfigBasedRecipientPreferences::Private::Private( KSharedConfigPtr config , KConfigBasedRecipientPreferences* qq ) : q( qq ), m_config( config ), m_parsed( false ), m_dirty( false )
122 {
123  assert( m_config );
124 }
125 
126 KConfigBasedRecipientPreferences::Private::~Private()
127 {
128  writePrefs();
129 }
130 
131 void KConfigBasedRecipientPreferences::Private::writePrefs()
132 {
133  if ( !m_dirty )
134  return;
135  const QSet<QByteArray> keys = pgpPrefs.keys().toSet() + cmsPrefs.keys().toSet();
136 
137  int n = 0;
138  Q_FOREACH ( const QByteArray& i, keys )
139  {
140  KConfigGroup group( m_config, QString::fromLatin1( "EncryptionPreference_%1" ).arg( n++ ) );
141  group.writeEntry( "email", i );
142  const QByteArray pgp = pgpPrefs.value( i );
143  if ( !pgp.isEmpty() )
144  group.writeEntry( "pgpCertificate", pgp );
145  const QByteArray cms = cmsPrefs.value( i );
146  if ( !cms.isEmpty() )
147  group.writeEntry( "cmsCertificate", cms );
148  }
149  m_config->sync();
150  m_dirty = false;
151 }
152 void KConfigBasedRecipientPreferences::Private::ensurePrefsParsed() const
153 {
154  if ( m_parsed )
155  return;
156  const QStringList groups = m_config->groupList().filter( QRegExp( QLatin1String("^EncryptionPreference_\\d+$") ) );
157 
158  Q_FOREACH ( const QString& i, groups )
159  {
160  const KConfigGroup group( m_config, i );
161  const QByteArray id = group.readEntry( "email", QByteArray() );
162  if ( id.isEmpty() )
163  continue;
164  pgpPrefs.insert( id, group.readEntry( "pgpCertificate", QByteArray() ) );
165  cmsPrefs.insert( id, group.readEntry( "cmsCertificate", QByteArray() ) );
166  }
167  m_parsed = true;
168 }
169 
170 KConfigBasedRecipientPreferences::KConfigBasedRecipientPreferences( KSharedConfigPtr config ) : d( new Private( config, this ) )
171 {
172 }
173 
174 
175 KConfigBasedRecipientPreferences::~KConfigBasedRecipientPreferences()
176 {
177  d->writePrefs();
178 }
179 
180 Key KConfigBasedRecipientPreferences::preferredCertificate( const Mailbox& recipient, Protocol protocol )
181 {
182  d->ensurePrefsParsed();
183 
184  const QByteArray keyId = ( protocol == CMS ? d->cmsPrefs : d->pgpPrefs ).value( recipient.address() );
185  return KeyCache::instance()->findByKeyIDOrFingerprint( keyId );
186 }
187 
188 void KConfigBasedRecipientPreferences::setPreferredCertificate( const Mailbox& recipient, Protocol protocol, const Key& certificate )
189 {
190  d->ensurePrefsParsed();
191  if ( !recipient.hasAddress() )
192  return;
193  ( protocol == CMS ? d->cmsPrefs : d->pgpPrefs ).insert( recipient.address(), certificate.keyID() );
194  d->m_dirty = true;
195 }
196 
197 class KConfigBasedSigningPreferences::Private {
198  friend class ::Kleo::Crypto::KConfigBasedSigningPreferences;
199  KConfigBasedSigningPreferences* const q;
200 public:
201  explicit Private( KSharedConfigPtr config, KConfigBasedSigningPreferences* qq );
202  ~Private();
203 
204 private:
205  void ensurePrefsParsed() const;
206  void writePrefs();
207 
208 private:
209  KSharedConfigPtr m_config;
210 
211  mutable QByteArray pgpSigningCertificate;
212  mutable QByteArray cmsSigningCertificate;
213  mutable bool m_parsed;
214  mutable bool m_dirty;
215 };
216 
217 KConfigBasedSigningPreferences::Private::Private( KSharedConfigPtr config , KConfigBasedSigningPreferences* qq ) : q( qq ), m_config( config ), m_parsed( false ), m_dirty( false )
218 {
219  assert( m_config );
220 }
221 
222 void KConfigBasedSigningPreferences::Private::ensurePrefsParsed() const
223 {
224  if ( m_parsed )
225  return;
226  const KConfigGroup group( m_config, "SigningPreferences" );
227  pgpSigningCertificate = group.readEntry( "pgpSigningCertificate", QByteArray() );
228  cmsSigningCertificate = group.readEntry( "cmsSigningCertificate", QByteArray() );
229  m_parsed = true;
230 }
231 
232 void KConfigBasedSigningPreferences::Private::writePrefs()
233 {
234  if ( !m_dirty )
235  return;
236  KConfigGroup group( m_config, "SigningPreferences" );
237  group.writeEntry( "pgpSigningCertificate", pgpSigningCertificate );
238  group.writeEntry( "cmsSigningCertificate", cmsSigningCertificate );
239  m_config->sync();
240  m_dirty = false;
241 }
242 
243 KConfigBasedSigningPreferences::Private::~Private()
244 {
245  writePrefs();
246 }
247 
248 KConfigBasedSigningPreferences::KConfigBasedSigningPreferences( KSharedConfigPtr config ) : d( new Private( config, this ) )
249 {
250 }
251 
252 
253 KConfigBasedSigningPreferences::~KConfigBasedSigningPreferences()
254 {
255  d->writePrefs();
256 }
257 
258 Key KConfigBasedSigningPreferences::preferredCertificate( Protocol protocol )
259 {
260  d->ensurePrefsParsed();
261 
262  const QByteArray keyId = ( protocol == CMS ? d->cmsSigningCertificate : d->pgpSigningCertificate );
263  const Key key = KeyCache::instance()->findByKeyIDOrFingerprint( keyId );
264  return key.hasSecret() ? key : Key::null;
265 }
266 
267 void KConfigBasedSigningPreferences::setPreferredCertificate( Protocol protocol, const Key& certificate )
268 {
269  d->ensurePrefsParsed();
270  ( protocol == CMS ? d->cmsSigningCertificate : d->pgpSigningCertificate ) = certificate.keyID();
271  d->m_dirty = true;
272 }
273 
Kleo::Crypto::KConfigBasedSigningPreferences::setPreferredCertificate
void setPreferredCertificate(GpgME::Protocol protocol, const GpgME::Key &certificate)
Definition: certificateresolver.cpp:267
Kleo::Crypto::KConfigBasedSigningPreferences
Definition: certificateresolver.h:81
Kleo::Crypto::KConfigBasedSigningPreferences::~KConfigBasedSigningPreferences
~KConfigBasedSigningPreferences()
Definition: certificateresolver.cpp:253
Kleo::Crypto::KConfigBasedRecipientPreferences
Definition: certificateresolver.h:69
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:60
Kleo::Crypto::KConfigBasedRecipientPreferences::~KConfigBasedRecipientPreferences
~KConfigBasedRecipientPreferences()
Definition: certificateresolver.cpp:175
Kleo::Crypto::KConfigBasedRecipientPreferences::setPreferredCertificate
void setPreferredCertificate(const KMime::Types::Mailbox &recipient, GpgME::Protocol protocol, const GpgME::Key &certificate)
Definition: certificateresolver.cpp:188
d
#define d
Definition: adduseridcommand.cpp:90
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:80
Kleo::Crypto::CertificateResolver::resolveRecipient
static std::vector< GpgME::Key > resolveRecipient(const KMime::Types::Mailbox &recipient, GpgME::Protocol proto)
Definition: certificateresolver.cpp:67
Kleo::Crypto::KConfigBasedSigningPreferences::KConfigBasedSigningPreferences
KConfigBasedSigningPreferences(KSharedConfigPtr config)
Definition: certificateresolver.cpp:248
Kleo::Class::CMS
Definition: classify.h:48
Kleo::Crypto::KConfigBasedRecipientPreferences::preferredCertificate
GpgME::Key preferredCertificate(const KMime::Types::Mailbox &recipient, GpgME::Protocol protocol)
Definition: certificateresolver.cpp:180
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::KeyCache::instance
static boost::shared_ptr< const KeyCache > instance()
Definition: keycache.cpp:189
Kleo::Crypto::CertificateResolver::resolveSigner
static std::vector< GpgME::Key > resolveSigner(const KMime::Types::Mailbox &signer, GpgME::Protocol proto)
Definition: certificateresolver.cpp:87
certificateresolver.h
Kleo::Crypto::KConfigBasedRecipientPreferences::KConfigBasedRecipientPreferences
KConfigBasedRecipientPreferences(KSharedConfigPtr config)
Definition: certificateresolver.cpp:170
keycache.h
Kleo::Crypto::KConfigBasedSigningPreferences::preferredCertificate
GpgME::Key preferredCertificate(GpgME::Protocol protocol)
Definition: certificateresolver.cpp:258
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:40 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

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