File: certifycertificatecommand.cpp

package info (click to toggle)
kdepim4 4%3A4.14.10-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 67,112 kB
  • ctags: 67,853
  • sloc: cpp: 553,448; xml: 5,447; perl: 1,434; sh: 796; ansic: 435; makefile: 45; php: 44
file content (283 lines) | stat: -rw-r--r-- 8,631 bytes parent folder | download
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
/* -*- mode: c++; c-basic-offset:4 -*-
    commands/signcertificatecommand.cpp

    This file is part of Kleopatra, the KDE keymanager
    Copyright (c) 2008 Klarälvdalens Datakonsult AB

    Kleopatra 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.

    Kleopatra 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 Softwarls   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 Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), 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
    Qt.  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.
*/

#include <config-kleopatra.h>

#include "certifycertificatecommand.h"

#include "command_p.h"

#include "exportopenpgpcertstoservercommand.h"

#include <dialogs/certifycertificatedialog.h>

#include <models/keycache.h>

#include <utils/formatting.h>

#include <kleo/cryptobackendfactory.h>
#include <kleo/cryptobackend.h>
#include <kleo/signkeyjob.h>

#include <gpgme++/key.h>

#include <KLocalizedString>
#include <kdebug.h>

#ifndef Q_MOC_RUN
#include <boost/bind.hpp>
#endif

#include <cassert>

using namespace Kleo;
using namespace Kleo::Commands;
using namespace Kleo::Dialogs;
using namespace GpgME;

class CertifyCertificateCommand::Private : public Command::Private {
    friend class ::Kleo::Commands::CertifyCertificateCommand;
    CertifyCertificateCommand * q_func() const { return static_cast<CertifyCertificateCommand*>( q ); }
public:
    explicit Private( CertifyCertificateCommand * qq, KeyListController * c );
    ~Private();

    void init();

private:
    void slotDialogRejected();
    void slotResult( const Error & err );
    void slotCertificationPrepared();

private:
    void ensureDialogCreated();
    void createJob();

private:
    std::vector<UserID> uids;
    QPointer<CertifyCertificateDialog> dialog;
    QPointer<SignKeyJob> job;
};


CertifyCertificateCommand::Private * CertifyCertificateCommand::d_func() { return static_cast<Private*>( d.get() ); }
const CertifyCertificateCommand::Private * CertifyCertificateCommand::d_func() const { return static_cast<const Private*>( d.get() ); }

#define d d_func()
#define q q_func()

CertifyCertificateCommand::Private::Private( CertifyCertificateCommand * qq, KeyListController * c )
    : Command::Private( qq, c ),
      uids(),
      dialog(),
      job()
{

}

CertifyCertificateCommand::Private::~Private() { kDebug(); }

CertifyCertificateCommand::CertifyCertificateCommand( KeyListController * c )
    : Command( new Private( this, c ) )
{
    d->init();
}

CertifyCertificateCommand::CertifyCertificateCommand( QAbstractItemView * v, KeyListController * c )
    : Command( v, new Private( this, c ) )
{
    d->init();
}

CertifyCertificateCommand::CertifyCertificateCommand( const Key & key )
    : Command( key, new Private( this, 0 ) )
{
    d->init();
}

CertifyCertificateCommand::CertifyCertificateCommand( const UserID & uid )
    : Command( uid.parent(), new Private( this, 0 ) )
{
    std::vector<UserID>( 1, uid ).swap( d->uids );
    d->init();
}

CertifyCertificateCommand::CertifyCertificateCommand( const std::vector<UserID> & uids )
    : Command( uids.empty() ? Key() : uids.front().parent(), new Private( this, 0 ) )
{
    d->uids = uids;
    d->init();
}

void CertifyCertificateCommand::Private::init() {

}

CertifyCertificateCommand::~CertifyCertificateCommand() { kDebug(); }

void CertifyCertificateCommand::setCertificationExportable( bool on ) {
    Q_UNUSED( on );
}

void CertifyCertificateCommand::setCertificationRevocable( bool on ) {
    Q_UNUSED( on );
}

void CertifyCertificateCommand::setCertifyingKey( const Key & signer ) {
    Q_UNUSED( signer );
}

void CertifyCertificateCommand::setUserIDs( const std::vector<UserID> & uids ) {
    d->uids = uids;
    if ( !uids.empty() && d->key().isNull() )
        setKey( uids.front().parent() );
}

void CertifyCertificateCommand::setUserID( const UserID & uid ) {
    setUserIDs( std::vector<UserID>( 1, uid ) );
}

void CertifyCertificateCommand::doStart() {

    const std::vector<Key> keys = d->keys();
    if ( keys.size() != 1 ||
         keys.front().protocol() != GpgME::OpenPGP ) {
        d->finished();
        return;
    }

    std::vector<Key> secKeys = KeyCache::instance()->secretKeys();
    std::vector<Key>::iterator it = std::remove_if( secKeys.begin(), secKeys.end(), !boost::bind( &Key::canCertify, _1 ) );
    it = std::remove_if( it, secKeys.end(), boost::bind( &Key::protocol, _1 ) != OpenPGP );
    secKeys.erase( it, secKeys.end() );

    if ( secKeys.empty() ) {
        d->error( i18nc( "@info", "To certify other certificates, you first need to create an OpenPGP certificate for yourself. Choose <interface>File->New Certificate...</interface> to create one." ),
                  i18n( "Certification Not Possible" ) );
        d->finished();
        return;
    }
    const Key & key = keys.front();

    Q_FOREACH( const UserID & uid, d->uids )
        if ( qstricmp( uid.parent().primaryFingerprint(), key.primaryFingerprint() ) != 0 ) {
            kWarning() << "User-ID <-> Key mismatch!";
            d->finished();
            return;
        }

    d->ensureDialogCreated();
    assert( d->dialog );
    d->dialog->setCertificateToCertify( d->key() );
    d->dialog->setSelectedUserIDs( d->uids );
    d->dialog->setCertificatesWithSecretKeys( secKeys );
    d->dialog->show();
}

void CertifyCertificateCommand::Private::slotDialogRejected() {
    emit q->canceled();
    finished();
}

void CertifyCertificateCommand::Private::slotResult( const Error & err ) {
    if ( !err && !err.isCanceled() && dialog && dialog->exportableCertificationSelected() && dialog->sendToServer() ) {
        ExportOpenPGPCertsToServerCommand * const cmd = new ExportOpenPGPCertsToServerCommand( key() );
        cmd->start();
    }

    finished();
}

void CertifyCertificateCommand::Private::slotCertificationPrepared() {
    assert( dialog );

    createJob();
    assert( job );
    job->setExportable( dialog->exportableCertificationSelected() );
    job->setNonRevocable( dialog->nonRevocableCertificationSelected() );
    job->setUserIDsToSign( dialog->selectedUserIDs() );
    job->setSigningKey( dialog->selectedSecretKey() );
    job->setCheckLevel( dialog->selectedCheckLevel() );

    dialog->connectJob( job );

    if ( const Error err = job->start( key() ) ) {
        dialog->setError( err );
        finished();
    }
}

void CertifyCertificateCommand::doCancel() {
    kDebug();
    if ( d->job )
        d->job->slotCancel();
}

void CertifyCertificateCommand::Private::ensureDialogCreated() {
    if ( dialog )
        return;

    dialog = new CertifyCertificateDialog;
    applyWindowID( dialog );
    dialog->setAttribute( Qt::WA_DeleteOnClose );

    connect( dialog, SIGNAL(rejected()), q, SLOT(slotDialogRejected()) );
    connect( dialog, SIGNAL(certificationPrepared()), q, SLOT(slotCertificationPrepared()) );
}

void CertifyCertificateCommand::Private::createJob() {
    if ( dialog )
        disconnect( dialog, SIGNAL(certificationPrepared()), q, SLOT(slotCertificationPrepared()) );

    assert( !job );

    assert( key().protocol() == OpenPGP );
    const CryptoBackend::Protocol * const backend = CryptoBackendFactory::instance()->protocol( key().protocol() );
    if ( !backend )
        return;

    SignKeyJob * const j = backend->signKeyJob();
    if ( !j )
        return;

    connect( j, SIGNAL(progress(QString,int,int)),
             q, SIGNAL(progress(QString,int,int)) );
    connect( j, SIGNAL(result(GpgME::Error)),
             q, SLOT(slotResult(GpgME::Error)) );

    job = j;
}


#undef d
#undef q

#include "moc_certifycertificatecommand.cpp"