• 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
sender.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/sender.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2009 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 "sender.h"
36 
37 #include <models/predicates.h>
38 #include <models/keycache.h>
39 
40 #include <utils/kleo_assert.h>
41 #include <utils/cached.h>
42 
43 #include <kmime/kmime_header_parsing.h>
44 
45 #include <gpgme++/key.h>
46 
47 using namespace Kleo;
48 using namespace Kleo::Crypto;
49 using namespace KMime::Types;
50 using namespace GpgME;
51 using namespace boost;
52 
53 namespace KMime {
54 namespace Types {
55 static bool operator==( const AddrSpec & lhs, const AddrSpec & rhs ) {
56  return lhs.localPart == rhs.localPart
57  && lhs.domain == rhs.domain ;
58 }
59 
60 static bool operator==( const Mailbox & lhs, const Mailbox & rhs ) {
61  return lhs.name() == rhs.name()
62  && lhs.addrSpec() == rhs.addrSpec() ;
63 }
64 
65 static bool determine_ambiguous( const Mailbox & mb, const std::vector<Key> & keys ) {
66  Q_UNUSED( mb );
67  // ### really do check when we don't only show matching keys
68  return keys.size() != 1 ;
69 }
70 } // namespace Types
71 } // namespace KMime
72 
73 class Sender::Private {
74  friend class ::Kleo::Crypto::Sender;
75 public:
76  explicit Private( const Mailbox & mb )
77  : mailbox( mb )
78  {
79  // ### also fill up to a certain number of keys with those
80  // ### that don't match, for the case where there's a low
81  // ### total number of keys
82  const std::vector<Key> signers = KeyCache::instance()->findSigningKeysByMailbox( mb );
83  const std::vector<Key> encrypt = KeyCache::instance()->findEncryptionKeysByMailbox( mb );
84  kdtools::separate_if( signers,
85  std::back_inserter( pgpSigners ), std::back_inserter( cmsSigners ),
86  boost::bind( &Key::protocol, _1 ) == OpenPGP );
87  kdtools::separate_if( encrypt,
88  std::back_inserter( pgpEncryptToSelfKeys ), std::back_inserter( cmsEncryptToSelfKeys ),
89  boost::bind( &Key::protocol, _1 ) == OpenPGP );
90  }
91 
92 private:
93  const Mailbox mailbox;
94  std::vector<Key> pgpSigners, cmsSigners, pgpEncryptToSelfKeys, cmsEncryptToSelfKeys;
95  cached<bool> signingAmbiguous[2], encryptionAmbiguous[2];
96  Key signingKey[2], cmsEncryptionKey;
97  UserID pgpEncryptionUid;
98 };
99 
100 Sender::Sender( const Mailbox & mb )
101  : d( new Private( mb ) )
102 {
103 
104 }
105 
106 void Sender::detach() {
107  if ( d && !d.unique() )
108  d.reset( new Private( *d ) );
109 }
110 
111 bool Sender::deepEquals( const Sender & other ) const {
112  static const _detail::ByFingerprint<std::equal_to> compare = {};
113  return mailbox() == other.mailbox()
114  && compare( d->signingKey[CMS], other.d->signingKey[CMS] )
115  && compare( d->signingKey[OpenPGP], other.d->signingKey[OpenPGP] )
116  && compare( d->cmsEncryptionKey, other.d->cmsEncryptionKey )
117  && compare( d->pgpEncryptionUid.parent(), other.d->pgpEncryptionUid.parent() )
118  && strcmp( d->pgpEncryptionUid.id(), other.d->pgpEncryptionUid.id() ) == 0
119  && kdtools::equal( d->pgpSigners, other.d->pgpSigners, compare )
120  && kdtools::equal( d->cmsSigners, other.d->cmsSigners, compare )
121  && kdtools::equal( d->pgpEncryptToSelfKeys, other.d->pgpEncryptToSelfKeys, compare )
122  && kdtools::equal( d->cmsEncryptToSelfKeys, other.d->cmsEncryptToSelfKeys, compare )
123  ;
124 }
125 
126 bool Sender::isSigningAmbiguous( GpgME::Protocol proto ) const {
127  if ( d->signingAmbiguous[proto].dirty() )
128  d->signingAmbiguous[proto] = determine_ambiguous( d->mailbox, signingCertificateCandidates( proto ) );
129  return d->signingAmbiguous[proto];
130 }
131 
132 bool Sender::isEncryptionAmbiguous( GpgME::Protocol proto ) const {
133  if ( d->encryptionAmbiguous[proto].dirty() )
134  d->encryptionAmbiguous[proto] = determine_ambiguous( d->mailbox, encryptToSelfCertificateCandidates( proto ) );
135  return d->encryptionAmbiguous[proto];
136 }
137 
138 const Mailbox & Sender::mailbox() const {
139  return d->mailbox;
140 }
141 
142 const std::vector<Key> & Sender::signingCertificateCandidates( GpgME::Protocol proto ) const {
143  if ( proto == OpenPGP )
144  return d->pgpSigners;
145  if ( proto == CMS )
146  return d->cmsSigners;
147  kleo_assert_fail( proto == OpenPGP || proto == CMS );
148 #if 0
149  return
150  proto == OpenPGP ? d->pgpSigners :
151  proto == CMS ? d->cmsSigners :
152  // even though gcc warns about this line, it's completely ok, promise:
153  kleo_assert_fail( proto == OpenPGP || proto == CMS ) ;
154 #endif
155 }
156 
157 const std::vector<Key> & Sender::encryptToSelfCertificateCandidates( GpgME::Protocol proto ) const {
158  if ( proto == OpenPGP )
159  return d->pgpEncryptToSelfKeys;
160  if ( proto == CMS )
161  return d->cmsEncryptToSelfKeys;
162  kleo_assert_fail( proto == OpenPGP || proto == CMS );
163 #if 0
164  return
165  proto == OpenPGP ? d->pgpEncryptToSelfKeys :
166  proto == CMS ? d->cmsEncryptToSelfKeys :
167  // even though gcc warns about this line, it's completely ok, promise:
168  kleo_assert_fail( proto == OpenPGP || proto == CMS ) ;
169 #endif
170 }
171 
172 void Sender::setResolvedSigningKey( const Key & key ) {
173  if ( key.isNull() )
174  return;
175  const Protocol proto = key.protocol();
176  kleo_assert( proto == OpenPGP || proto == CMS );
177  detach();
178  d->signingKey[proto] = key;
179  d->signingAmbiguous[proto] = false;
180 }
181 
182 Key Sender::resolvedSigningKey( GpgME::Protocol proto ) const {
183  kleo_assert( proto == OpenPGP || proto == CMS );
184  return d->signingKey[proto];
185 }
186 
187 void Sender::setResolvedEncryptionKey( const Key & key ) {
188  if ( key.isNull() )
189  return;
190  const Protocol proto = key.protocol();
191  kleo_assert( proto == OpenPGP || proto == CMS );
192  detach();
193  if ( proto == OpenPGP )
194  d->pgpEncryptionUid = key.userID( 0 );
195  else
196  d->cmsEncryptionKey = key;
197  d->encryptionAmbiguous[proto] = false;
198 }
199 
200 Key Sender::resolvedEncryptionKey( GpgME::Protocol proto ) const {
201  kleo_assert( proto == OpenPGP || proto == CMS );
202  if ( proto == OpenPGP )
203  return d->pgpEncryptionUid.parent();
204  else
205  return d->cmsEncryptionKey;
206 }
207 
208 void Sender::setResolvedOpenPGPEncryptionUserID( const UserID & uid ) {
209  if ( uid.isNull() )
210  return;
211  detach();
212  d->pgpEncryptionUid = uid;
213 }
214 
215 UserID Sender::resolvedOpenPGPEncryptionUserID() const {
216  return d->pgpEncryptionUid;
217 }
Kleo::Crypto::Sender::Sender
Sender()
Definition: sender.h:58
Kleo::Crypto::Sender::mailbox
const KMime::Types::Mailbox & mailbox() const
Definition: sender.cpp:138
KMime::Types::operator==
static bool operator==(const Mailbox &lhs, const Mailbox &rhs)
Definition: sender.cpp:60
kleo_assert.h
d
#define d
Definition: adduseridcommand.cpp:90
cached.h
Kleo::Class::OpenPGP
Definition: classify.h:49
Kleo::Class::CMS
Definition: classify.h:48
kleo_assert_fail
#define kleo_assert_fail(cond)
Definition: kleo_assert.h:88
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:84
Kleo::cached< bool >
sender.h
predicates.h
Kleo::KeyCache::instance
static boost::shared_ptr< const KeyCache > instance()
Definition: keycache.cpp:189
keycache.h
Kleo::Crypto::Sender
Definition: sender.h:56
KMime::Types::determine_ambiguous
static bool determine_ambiguous(const Mailbox &mb, const std::vector< Key > &keys)
Definition: sender.cpp:65
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 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