libkpgp

kpgpbase.h
1 /*
2  kpgpbase.h
3 
4  Copyright (C) 2001,2002 the KPGP authors
5  See file AUTHORS.kpgp for details
6 
7  This file is part of KPGP, the KDE PGP/GnuPG support library.
8 
9  KPGP is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software Foundation,
16  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #ifndef KPGPBASE_H
20 #define KPGPBASE_H
21 
22 #include <tqstring.h>
23 #include <tqcstring.h>
24 #include <tqstringlist.h>
25 
26 #include "kpgpkey.h"
27 #include "kpgpblock.h"
28 
29 namespace Kpgp {
30 
31 class Base
32 {
33 public:
34 
36  Base();
37  virtual ~Base();
38 
39 
41  virtual int encrypt( Block& , const KeyIDList& ) { return OK; }
42 
44  virtual int clearsign( Block& , const TQString& ) { return OK; }
45 
47  virtual int encsign( Block& , const KeyIDList& ,
48  const TQString& = TQString::null) { return OK; }
49 
51  virtual int decrypt( Block& , const TQString& = TQString::null) { return OK; }
52 
54  virtual int verify( Block& block ) { return decrypt( block, 0 ); }
55 
56 
62  virtual Key* readPublicKey( const KeyID&, const bool = false,
63  Key* = 0 )
64  { return 0; }
65 
67  virtual KeyList publicKeys( const TQStringList & = TQStringList() )
68  { return KeyList(); }
69 
71  virtual KeyList secretKeys( const TQStringList & = TQStringList() )
72  { return KeyList(); }
73 
76  virtual TQCString getAsciiPublicKey(const KeyID& ) { return TQCString(); }
77 
80  virtual int signKey(const KeyID& , const TQString& ) { return OK; }
81 
82 
85  virtual TQString lastErrorMessage() const;
86 
87 
88 protected:
89  virtual int run( const char *cmd, const TQString &passphrase = TQString::null,
90  bool onlyReadFromPGP = false );
91  virtual int runGpg( const char *cmd, const TQString &passphrase = TQString::null,
92  bool onlyReadFromGnuPG = false );
93  virtual void clear();
94 
95  TQCString addUserId();
96 
97  TQCString input;
98  TQCString output;
99  TQCString error;
100  TQString errMsg;
101 
102  TQCString mVersion;
103 
104  int status;
105 
106 };
107 
108 // ---------------------------------------------------------------------------
109 
110 class Base2 : public Base
111 {
112 
113 public:
114  Base2();
115  virtual ~Base2();
116 
117  virtual int encrypt( Block& block, const KeyIDList& recipients );
118  virtual int clearsign( Block& block, const TQString &passphrase );
119  virtual int encsign( Block& block, const KeyIDList& recipients,
120  const TQString &passphrase = TQString::null );
121  virtual int decrypt( Block& block, const TQString &passphrase = TQString::null );
122  virtual int verify( Block& block ) { return decrypt( block, 0 ); }
123 
124  virtual Key* readPublicKey( const KeyID& keyID,
125  const bool readTrust = false,
126  Key* key = 0 );
127  virtual KeyList publicKeys( const TQStringList & patterns = TQStringList() );
128  virtual KeyList secretKeys( const TQStringList & patterns = TQStringList() );
129  virtual TQCString getAsciiPublicKey( const KeyID& keyID );
130  virtual int signKey( const KeyID& keyID, const TQString &passphrase );
131 
132 protected:
133  KeyList doGetPublicKeys( const TQCString & cmd,
134  const TQStringList & patterns );
135  virtual KeyList parseKeyList( const TQCString&, bool );
136 
137 private:
138  Key* parsePublicKeyData( const TQCString& output, Key* key = 0 );
139  void parseTrustDataForKey( Key* key, const TQCString& str );
140 };
141 
142 class BaseG : public Base
143 {
144 
145 public:
146  BaseG();
147  virtual ~BaseG();
148 
149  virtual int encrypt( Block& block, const KeyIDList& recipients );
150  virtual int clearsign( Block& block, const TQString &passphrase );
151  virtual int encsign( Block& block, const KeyIDList& recipients,
152  const TQString &passphrase = TQString::null );
153  virtual int decrypt( Block& block, const TQString &passphrase = TQString::null );
154  virtual int verify( Block& block ) { return decrypt( block, 0 ); }
155 
156  virtual Key* readPublicKey( const KeyID& keyID,
157  const bool readTrust = false,
158  Key* key = 0 );
159  virtual KeyList publicKeys( const TQStringList & patterns = TQStringList() );
160  virtual KeyList secretKeys( const TQStringList & patterns = TQStringList() );
161  virtual TQCString getAsciiPublicKey( const KeyID& keyID );
162  virtual int signKey( const KeyID& keyID, const TQString &passphrase );
163 
164 private:
165  Key* parseKeyData( const TQCString& output, int& offset, Key* key = 0 );
166  KeyList parseKeyList( const TQCString&, bool );
167 };
168 
169 
170 class Base5 : public Base
171 {
172 
173 public:
174  Base5();
175  virtual ~Base5();
176 
177  virtual int encrypt( Block& block, const KeyIDList& recipients );
178  virtual int clearsign( Block& block, const TQString &passphrase );
179  virtual int encsign( Block& block, const KeyIDList& recipients,
180  const TQString &passphrase = TQString::null );
181  virtual int decrypt( Block& block, const TQString &passphrase = TQString::null );
182  virtual int verify( Block& block ) { return decrypt( block, 0 ); }
183 
184  virtual Key* readPublicKey( const KeyID& keyID,
185  const bool readTrust = false,
186  Key* key = 0 );
187  virtual KeyList publicKeys( const TQStringList & patterns = TQStringList() );
188  virtual KeyList secretKeys( const TQStringList & patterns = TQStringList() );
189  virtual TQCString getAsciiPublicKey( const KeyID& keyID );
190  virtual int signKey( const KeyID& keyID, const TQString &passphrase );
191 
192 private:
193  Key* parseKeyData( const TQCString& output, int& offset, Key* key = 0 );
194  Key* parseSingleKey( const TQCString& output, Key* key = 0 );
195  KeyList parseKeyList( const TQCString& output, bool );
196  void parseTrustDataForKey( Key* key, const TQCString& str );
197 };
198 
199 
200 class Base6 : public Base2
201 {
202 
203 public:
204  Base6();
205  virtual ~Base6();
206 
207  virtual int decrypt( Block& block, const TQString &passphrase = TQString::null );
208  virtual int verify( Block& block ) { return decrypt( block, 0 ); }
209 
210  virtual Key* readPublicKey( const KeyID& keyID,
211  const bool readTrust = false,
212  Key* key = 0 );
213  virtual KeyList publicKeys( const TQStringList & patterns = TQStringList() );
214  virtual KeyList secretKeys( const TQStringList & patterns = TQStringList() );
215 
216  virtual int isVersion6();
217 
218 protected:
219  virtual KeyList parseKeyList( const TQCString &, bool );
220 
221 private:
222  Key* parseKeyData( const TQCString& output, int& offset, Key* key = 0 );
223  Key* parseSingleKey( const TQCString& output, Key* key = 0 );
224  void parseTrustDataForKey( Key* key, const TQCString& str );
225 };
226 
227 // ---------------------------------------------------------------------------
228 // inlined functions
229 
230 inline TQString
231 Base::lastErrorMessage() const
232 {
233  return errMsg;
234 }
235 
236 
237 } // namespace Kpgp
238 
239 #endif