Build with non-standard boost locations.
[kdepim.git] / messagecomposer / signjob.cpp
blob22baf4502e04c277f71dc69b217b13565dd0ba92
1 /*
2 Copyright (C) 2009 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
3 Copyright (c) 2009 Leo Franchi <lfranchi@kde.org>
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
21 #include "signjob.h"
23 #include "contentjobbase_p.h"
24 #include "kleo/cryptobackendfactory.h"
25 #include "kleo/cryptobackend.h"
26 #include "kleo/enum.h"
27 #include "kleo/signjob.h"
28 #include "util.h"
30 #include <kdebug.h>
31 #include <kmime/kmime_message.h>
32 #include <kmime/kmime_content.h>
33 #include <kmime/kmime_util.h>
34 #include <QBuffer>
36 #include <gpgme++/global.h>
37 #include <gpgme++/signingresult.h>
38 #include <gpgme++/encryptionresult.h>
39 #include <sstream>
41 using namespace Message;
43 class Message::SignJobPrivate : public ContentJobBasePrivate
45 public:
46 SignJobPrivate( SignJob *qq )
47 : ContentJobBasePrivate( qq )
48 , content( 0 )
52 KMime::Content* content;
53 std::vector<GpgME::Key> signers;
54 Kleo::CryptoMessageFormat format;
56 // copied from messagecomposer.cpp
57 bool binaryHint( Kleo::CryptoMessageFormat f )
59 switch ( f ) {
60 case Kleo::SMIMEFormat:
61 case Kleo::SMIMEOpaqueFormat:
62 return true;
63 default:
64 case Kleo::OpenPGPMIMEFormat:
65 case Kleo::InlineOpenPGPFormat:
66 return false;
71 GpgME::SignatureMode signingMode( Kleo::CryptoMessageFormat f )
73 switch ( f ) {
74 case Kleo::SMIMEOpaqueFormat:
75 return GpgME::NormalSignatureMode;
76 case Kleo::InlineOpenPGPFormat:
77 return GpgME::Clearsigned;
78 default:
79 case Kleo::SMIMEFormat:
80 case Kleo::OpenPGPMIMEFormat:
81 return GpgME::Detached;
85 Q_DECLARE_PUBLIC( SignJob )
88 SignJob::SignJob( QObject *parent )
89 : ContentJobBase( *new SignJobPrivate( this ), parent )
93 SignJob::~SignJob()
97 void SignJob::setContent( KMime::Content* content )
99 Q_D( SignJob );
101 d->content = content;
104 void SignJob::setCryptoMessageFormat( Kleo::CryptoMessageFormat format)
106 Q_D( SignJob );
108 // There *must* be a concrete format set at this point.
109 Q_ASSERT( format == Kleo::OpenPGPMIMEFormat
110 || format == Kleo::InlineOpenPGPFormat
111 || format == Kleo::SMIMEFormat
112 || format == Kleo::SMIMEOpaqueFormat );
113 d->format = format;
116 void SignJob::setSigningKeys( std::vector<GpgME::Key>& signers )
118 Q_D( SignJob );
120 d->signers = signers;
123 KMime::Content* SignJob::origContent()
125 Q_D( SignJob );
127 return d->content;
131 void SignJob::process()
133 Q_D( SignJob );
134 Q_ASSERT( d->resultContent == 0 ); // Not processed before.
136 // if setContent hasn't been called, we assume that a subjob was added
137 // and we want to use that
138 if( !d->content ) {
139 Q_ASSERT( d->subjobContents.size() == 1 );
140 d->content = d->subjobContents.first();
143 d->resultContent = new KMime::Content;
145 const Kleo::CryptoBackend::Protocol *proto = 0;
146 if( d->format & Kleo::AnyOpenPGP ) {
147 proto = Kleo::CryptoBackendFactory::instance()->openpgp();
148 } else if( d->format & Kleo::AnySMIME ) {
149 proto = Kleo::CryptoBackendFactory::instance()->smime();
152 Q_ASSERT( proto );
155 kDebug() << "creating signJob from:" << proto->name() << proto->displayName();
156 std::auto_ptr<Kleo::SignJob> job( proto->signJob( !d->binaryHint( d->format ), d->format == Kleo::InlineOpenPGPFormat ) );
157 // for now just do the main recipients
158 QByteArray signature;
160 // replace simple LFs by CRLFs for all MIME supporting CryptPlugs
161 // according to RfC 2633, 3.1.1 Canonicalization
162 d->content->assemble();
163 QByteArray content;
166 kDebug() << "signing content before LFtoCRLF:" << d->content->encodedContent();
167 if( d->format & Kleo::InlineOpenPGPFormat &&
168 !( d->format & Kleo::SMIMEOpaqueFormat ) ) {
169 content = KMime::LFtoCRLF( d->content->body() );
170 } else if( !( d->format & Kleo::SMIMEOpaqueFormat ) ) {
171 content = KMime::LFtoCRLF( d->content->encodedContent() );
172 } else { // SMimeOpaque doesn't need LFtoCRLF, else it gets munged
173 content = d->content->encodedContent();
176 kDebug() << "signing content:" << content;
177 GpgME::SigningResult res = job->exec( d->signers,
178 content,
179 d->signingMode( d->format ),
180 signature );
182 if ( res.error() ) {
183 kDebug() << "signing failed:" << res.error().asString();
184 // job->showErrorDialog( globalPart()->parentWidgetForGui() );
185 setError( res.error().code() );
186 setErrorText( QString::fromLocal8Bit( res.error().asString() ) );
189 // exec'ed jobs don't delete themselves
190 job->deleteLater();
192 QByteArray signatureHashAlgo = res.createdSignature( 0 ).hashAlgorithmAsString();
194 d->resultContent = Message::Util::composeHeadersAndBody( d->content, signature, d->format, true, signatureHashAlgo );
195 emitResult();
198 #include "signjob.moc"