source: psi/trunk/src/tools/openpgp/openpgp.cpp

Last change on this file was 2, checked in by dmik, 19 years ago

Imported original Psi 0.10 sources from Affinix

File size: 6.2 KB
Line 
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
26using namespace OpenPGP;
27
28
29//----------------------------------------------------------------------------
30// Key
31//----------------------------------------------------------------------------
32Key::Key()
33{
34}
35
36Key::~Key()
37{
38}
39
40const QString & Key::keyID() const
41{
42 return v_keyID;
43}
44
45const QString & Key::userID() const
46{
47 return v_userID;
48}
49
50void Key::setKeyID(const QString &s)
51{
52 v_keyID = s;
53}
54
55void Key::setUserID(const QString &s)
56{
57 v_userID = s;
58}
59
60
61//----------------------------------------------------------------------------
62// Request
63//----------------------------------------------------------------------------
64class Request::Private
65{
66public:
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
79Request::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
88Request::~Request()
89{
90 delete d;
91}
92
93int Request::type() const
94{
95 return d->type;
96}
97
98void Request::encrypt(const QByteArray &in, const QStringList &keys)
99{
100 d->type = Encrypt;
101 d->eng->encrypt(this, in, keys);
102}
103
104void Request::decrypt(const QString &in)
105{
106 d->type = Decrypt;
107 d->eng->decrypt(this, in);
108}
109
110void Request::sign(const QByteArray &in, const QString &keyID)
111{
112 d->type = Sign;
113 d->eng->sign(this, in, keyID);
114}
115
116void Request::verify(const QByteArray &in, const QString &sig)
117{
118 d->type = Verify;
119 d->eng->verify(this, in, sig);
120}
121
122void Request::submitPassphrase(const QString &str)
123{
124 d->eng->submitPassphrase(this, str);
125}
126
127void Request::op_encryptFinished(bool b, const QString &str)
128{
129 if(b)
130 d->enc = str;
131 finished(b);
132}
133
134void Request::op_decryptFinished(bool b, const QByteArray &out)
135{
136 if(b)
137 d->dec = out.copy();
138 finished(b);
139}
140
141void Request::op_signFinished(bool b, const QString &str)
142{
143 if(b)
144 d->sig = str;
145 finished(b);
146}
147
148void 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
163void Request::op_needPassphrase()
164{
165 needPassphrase();
166}
167
168QString Request::encrypted() const
169{
170 return d->enc;
171}
172
173QByteArray Request::decrypted() const
174{
175 return d->dec;
176}
177
178QString Request::signature() const
179{
180 return d->sig;
181}
182
183QString Request::keyID() const
184{
185 return d->keyID;
186}
187
188QDateTime Request::timestamp() const
189{
190 return d->ts;
191}
192
193int Request::verifyResult() const
194{
195 return d->verifyResult;
196}
197
198void Request::op_setBadPassphrase(bool b)
199{
200 d->badpp = b;
201}
202
203bool Request::badPassphrase() const
204{
205 return d->badpp;
206}
207
208
209//----------------------------------------------------------------------------
210// Engine
211//----------------------------------------------------------------------------
212Engine::Engine(QObject *parent)
213:QObject(parent)
214{
215}
216
217Engine::~Engine()
218{
219}
220
221void Engine::encryptFinished(Request *r, bool b, const QString &str)
222{
223 r->op_encryptFinished(b, str);
224}
225
226void Engine::decryptFinished(Request *r, bool b, const QByteArray &out)
227{
228 r->op_decryptFinished(b, out);
229}
230
231void Engine::signFinished(Request *r, bool b, const QString &str)
232{
233 r->op_signFinished(b, str);
234}
235
236void Engine::verifyFinished(Request *r, int x, const QString &str, const QDateTime &ts)
237{
238 r->op_verifyFinished(x, str, ts);
239}
240
241void Engine::needPassphrase(Request *r)
242{
243 r->op_needPassphrase();
244}
245
246void Engine::setBadPassphrase(Request *r, bool b)
247{
248 r->op_setBadPassphrase(b);
249}
250
251
252//----------------------------------------------------------------------------
253// Misc
254//----------------------------------------------------------------------------
255#include"gnupg.h"
256static QPtrList<Engine> createEngineList()
257{
258 QPtrList<Engine> list;
259 list.append(new GnuPG);
260 return list;
261}
262
263QValueList<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
275QValueList<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
289Engine * OpenPGP::createEngine(const QString &id)
290{
291 if(id == "gpg")
292 return (new GnuPG);
293 else
294 return 0;
295}
296
297QString 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
333QString 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}
Note: See TracBrowser for help on using the repository browser.