~ubuntu-branches/ubuntu/oneiric/psi/oneiric

« back to all changes in this revision

Viewing changes to src/tools/openpgp/openpgp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2006-01-20 00:20:36 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060120002036-7nw6yo6totip0ee5
Tags: 0.10-2
* Added upstream changelog (Closes: Bug#327748)
* Mention --no-gpg and --no-gpg-agent in manpage (Closes: Bug#204416)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * openpgp.cpp - OpenPGP base
 
3
 * Copyright (C) 2003  Justin Karneges
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include"openpgp.h"
 
22
 
 
23
#include<qptrlist.h>
 
24
#include<qstringlist.h>
 
25
 
 
26
using namespace OpenPGP;
 
27
 
 
28
 
 
29
//----------------------------------------------------------------------------
 
30
// Key
 
31
//----------------------------------------------------------------------------
 
32
Key::Key()
 
33
{
 
34
}
 
35
 
 
36
Key::~Key()
 
37
{
 
38
}
 
39
 
 
40
const QString & Key::keyID() const
 
41
{
 
42
        return v_keyID;
 
43
}
 
44
 
 
45
const QString & Key::userID() const
 
46
{
 
47
        return v_userID;
 
48
}
 
49
 
 
50
void Key::setKeyID(const QString &s)
 
51
{
 
52
        v_keyID = s;
 
53
}
 
54
 
 
55
void Key::setUserID(const QString &s)
 
56
{
 
57
        v_userID = s;
 
58
}
 
59
 
 
60
 
 
61
//----------------------------------------------------------------------------
 
62
// Request
 
63
//----------------------------------------------------------------------------
 
64
class Request::Private
 
65
{
 
66
public:
 
67
        Private() {}
 
68
 
 
69
        Engine *eng;
 
70
        int type;
 
71
 
 
72
        QString enc, sig, keyID;
 
73
        QDateTime ts;
 
74
        int verifyResult;
 
75
        QByteArray dec;
 
76
        bool badpp;
 
77
};
 
78
 
 
79
Request::Request(Engine *eng)
 
80
:QObject(0)
 
81
{
 
82
        d = new Private;
 
83
        d->eng = eng;
 
84
        d->type = None;
 
85
        d->badpp = false;
 
86
}
 
87
 
 
88
Request::~Request()
 
89
{
 
90
        delete d;
 
91
}
 
92
 
 
93
int Request::type() const
 
94
{
 
95
        return d->type;
 
96
}
 
97
 
 
98
void Request::encrypt(const QByteArray &in, const QStringList &keys)
 
99
{
 
100
        d->type = Encrypt;
 
101
        d->eng->encrypt(this, in, keys);
 
102
}
 
103
 
 
104
void Request::decrypt(const QString &in)
 
105
{
 
106
        d->type = Decrypt;
 
107
        d->eng->decrypt(this, in);
 
108
}
 
109
 
 
110
void Request::sign(const QByteArray &in, const QString &keyID)
 
111
{
 
112
        d->type = Sign;
 
113
        d->eng->sign(this, in, keyID);
 
114
}
 
115
 
 
116
void Request::verify(const QByteArray &in, const QString &sig)
 
117
{
 
118
        d->type = Verify;
 
119
        d->eng->verify(this, in, sig);
 
120
}
 
121
 
 
122
void Request::submitPassphrase(const QString &str)
 
123
{
 
124
        d->eng->submitPassphrase(this, str);
 
125
}
 
126
 
 
127
void Request::op_encryptFinished(bool b, const QString &str)
 
128
{
 
129
        if(b)
 
130
                d->enc = str;
 
131
        finished(b);
 
132
}
 
133
 
 
134
void Request::op_decryptFinished(bool b, const QByteArray &out)
 
135
{
 
136
        if(b)
 
137
                d->dec = out.copy();
 
138
        finished(b);
 
139
}
 
140
 
 
141
void Request::op_signFinished(bool b, const QString &str)
 
142
{
 
143
        if(b)
 
144
                d->sig = str;
 
145
        finished(b);
 
146
}
 
147
 
 
148
void Request::op_verifyFinished(int x, const QString &str, const QDateTime &ts)
 
149
{
 
150
        bool b;
 
151
        if(x != VerifyError) {
 
152
                d->keyID = str;
 
153
                d->ts = ts;
 
154
                d->verifyResult = x;
 
155
                b = true;
 
156
        }
 
157
        else
 
158
                b = false;
 
159
 
 
160
        finished(b);
 
161
}
 
162
 
 
163
void Request::op_needPassphrase()
 
164
{
 
165
        needPassphrase();
 
166
}
 
167
 
 
168
QString Request::encrypted() const
 
169
{
 
170
        return d->enc;
 
171
}
 
172
 
 
173
QByteArray Request::decrypted() const
 
174
{
 
175
        return d->dec;
 
176
}
 
177
 
 
178
QString Request::signature() const
 
179
{
 
180
        return d->sig;
 
181
}
 
182
 
 
183
QString Request::keyID() const
 
184
{
 
185
        return d->keyID;
 
186
}
 
187
 
 
188
QDateTime Request::timestamp() const
 
189
{
 
190
        return d->ts;
 
191
}
 
192
 
 
193
int Request::verifyResult() const
 
194
{
 
195
        return d->verifyResult;
 
196
}
 
197
 
 
198
void Request::op_setBadPassphrase(bool b)
 
199
{
 
200
        d->badpp = b;
 
201
}
 
202
 
 
203
bool Request::badPassphrase() const
 
204
{
 
205
        return d->badpp;
 
206
}
 
207
 
 
208
 
 
209
//----------------------------------------------------------------------------
 
210
// Engine
 
211
//----------------------------------------------------------------------------
 
212
Engine::Engine(QObject *parent)
 
213
:QObject(parent)
 
214
{
 
215
}
 
216
 
 
217
Engine::~Engine()
 
218
{
 
219
}
 
220
 
 
221
void Engine::encryptFinished(Request *r, bool b, const QString &str)
 
222
{
 
223
        r->op_encryptFinished(b, str);
 
224
}
 
225
 
 
226
void Engine::decryptFinished(Request *r, bool b, const QByteArray &out)
 
227
{
 
228
        r->op_decryptFinished(b, out);
 
229
}
 
230
 
 
231
void Engine::signFinished(Request *r, bool b, const QString &str)
 
232
{
 
233
        r->op_signFinished(b, str);
 
234
}
 
235
 
 
236
void Engine::verifyFinished(Request *r, int x, const QString &str, const QDateTime &ts)
 
237
{
 
238
        r->op_verifyFinished(x, str, ts);
 
239
}
 
240
 
 
241
void Engine::needPassphrase(Request *r)
 
242
{
 
243
        r->op_needPassphrase();
 
244
}
 
245
 
 
246
void Engine::setBadPassphrase(Request *r, bool b)
 
247
{
 
248
        r->op_setBadPassphrase(b);
 
249
}
 
250
 
 
251
 
 
252
//----------------------------------------------------------------------------
 
253
// Misc
 
254
//----------------------------------------------------------------------------
 
255
#include"gnupg.h"
 
256
static QPtrList<Engine> createEngineList()
 
257
{
 
258
        QPtrList<Engine> list;
 
259
        list.append(new GnuPG);
 
260
        return list;
 
261
}
 
262
 
 
263
QValueList<EngineItem> OpenPGP::getAllEngines()
 
264
{
 
265
        QValueList<EngineItem> list;
 
266
        QPtrList<Engine> el = createEngineList();
 
267
        QPtrListIterator<Engine> it(el);
 
268
        for(Engine *e; (e = it.current()); ++it)
 
269
                list += qMakePair(e->id(), e->name());
 
270
        el.setAutoDelete(true);
 
271
        el.clear();
 
272
        return list;
 
273
}
 
274
 
 
275
QValueList<EngineItem> OpenPGP::getAvailableEngines()
 
276
{
 
277
        QValueList<EngineItem> list;
 
278
        QPtrList<Engine> el = createEngineList();
 
279
        QPtrListIterator<Engine> it(el);
 
280
        for(Engine *e; (e = it.current()); ++it) {
 
281
                if(e->checkAvailability())
 
282
                        list += qMakePair(e->id(), e->name());
 
283
        }
 
284
        el.setAutoDelete(true);
 
285
        el.clear();
 
286
        return list;
 
287
}
 
288
 
 
289
Engine * OpenPGP::createEngine(const QString &id)
 
290
{
 
291
        if(id == "gpg")
 
292
                return (new GnuPG);
 
293
        else
 
294
                return 0;
 
295
}
 
296
 
 
297
QString OpenPGP::stripHeaderFooter(const QString &str)
 
298
{
 
299
        QString s;
 
300
        if(str.at(0) != '-')
 
301
                return str;
 
302
        QStringList lines = QStringList::split('\n', str, true);
 
303
        QStringList::ConstIterator it = lines.begin();
 
304
        // skip the first line
 
305
        ++it;
 
306
        if(it == lines.end())
 
307
                return str;
 
308
 
 
309
        // skip the header
 
310
        for(; it != lines.end(); ++it) {
 
311
                if((*it).isEmpty())
 
312
                        break;
 
313
        }
 
314
        if(it == lines.end())
 
315
                return str;
 
316
        ++it;
 
317
        if(it == lines.end())
 
318
                return str;
 
319
 
 
320
        bool first = true;
 
321
        for(; it != lines.end(); ++it) {
 
322
                if((*it).at(0) == '-')
 
323
                        break;
 
324
                if(!first)
 
325
                        s += '\n';
 
326
                s += (*it);
 
327
                first = false;
 
328
        }
 
329
 
 
330
        return s;
 
331
}
 
332
 
 
333
QString OpenPGP::addHeaderFooter(const QString &str, int type)
 
334
{
 
335
        QString stype;
 
336
        if(type == 0)
 
337
                stype = "MESSAGE";
 
338
        else
 
339
                stype = "SIGNATURE";
 
340
 
 
341
        QString s;
 
342
        s += QString("-----BEGIN PGP %1-----\n").arg(stype);
 
343
        s += "Version: PGP\n";
 
344
        s += "\n";
 
345
        s += str + '\n';
 
346
        s += QString("-----END PGP %1-----\n").arg(stype);
 
347
        return s;
 
348
}