1 #ifndef _KVI_CRYPT_ENGINE_H_
2 #define _KVI_CRYPT_ENGINE_H_
3 //=============================================================================
4 //
5 //   File : KviCryptEngine.h
6 //   Creation date : Fri Nov 03 2000 01:45:21 CEST by Szymon Stefanek
7 //
8 //   This file is part of the KVIrc IRC client distribution
9 //   Copyright (C) 2000-2010 Szymon Stefanek (pragma at kvirc dot net)
10 //
11 //   This program is FREE software. You can redistribute it and/or
12 //   modify it under the terms of the GNU General Public License
13 //   as published by the Free Software Foundation; either version 2
14 //   of the License, or (at your option) any later version.
15 //
16 //   This program is distributed in the HOPE that it will be USEFUL,
17 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 //   See the GNU General Public License for more details.
20 //
21 //   You should have received a copy of the GNU General Public License
22 //   along with this program. If not, write to the Free Software Foundation,
23 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 #include "kvi_settings.h"
28 
29 //
30 // Base class for all IRC crypt engines
31 // These intend to encrypt plain text into something
32 // that can be sent through the IRC protocol...
33 // so it should not contain NULL, CR, LF and other
34 // similar stuff...
35 //
36 
37 #include "KviHeapObject.h"
38 
39 #include <QObject>
40 
41 class KviCString;
42 
43 #ifdef COMPILE_CRYPT_SUPPORT
44 class KviCryptEngine;
45 
46 typedef KviCryptEngine * (*crypt_engine_allocator_func)();
47 typedef void (*crypt_engine_deallocator_func)(KviCryptEngine *);
48 #endif //COMPILE_CRYPT_SUPPORT
49 
50 // we must include this declaration to make moc happy even
51 // if we're not compiling the crypt support
52 
53 class KVILIB_API KviCryptEngine : public QObject, public KviHeapObject
54 {
55 	Q_OBJECT
56 	friend class KviCryptEngineManager;
57 
58 public:
59 	enum EngineFlag
60 	{
61 		CanEncrypt = 1,
62 		CanDecrypt = 2,
63 		WantEncryptKey = 4,
64 		WantDecryptKey = 8
65 	};
66 
67 	enum EncryptResult
68 	{
69 		Encrypted,
70 		Encoded,
71 		EncryptError
72 	};
73 
74 	enum DecryptResult
75 	{
76 		DecryptOkWasEncrypted,
77 		DecryptOkWasEncoded,
78 		DecryptOkWasPlainText,
79 		DecryptError
80 	};
81 
82 	KviCryptEngine();
83 	~KviCryptEngine();
84 
85 #ifdef COMPILE_CRYPT_SUPPORT
86 private:
87 	crypt_engine_deallocator_func m_deallocFunc; // this is accessed by KviCryptEngineManager only
88 	QString m_szLastError;
89 	int m_iMaxEncryptLen;
90 
91 public:
setMaxEncryptLen(int m)92 	void setMaxEncryptLen(int m) { m_iMaxEncryptLen = m; }
maxEncryptLen()93 	int maxEncryptLen() const { return m_iMaxEncryptLen; }
94 	virtual bool init(const char * encKey, int encKeyLen, const char * decKey, int decKeyLen);
95 	//
96 	// Encrypts utf8 plainText and returns the encrypted
97 	// data in outBuffer. The encrypted data must be
98 	// suitable for sending through an IRC (eventually DCC
99 	// that is less restrictive) connection and must be utf8 encoded: so
100 	// no NULL, CR and LF in the output.
101 	// 0x01 should be also avoided since
102 	// it is the CTCP delimiter.
103 	// Converting the result in a HEX string
104 	// is a good trick...also Base64 could be used.
105 	// Should return false in case of an error.
106 	// Theoretically we could allow NULLs in plainText
107 	// but this is not the case of KVIrc.
108 	//
109 	virtual EncryptResult encrypt(const char * plainText, KviCString & outBuffer);
110 	//
111 	// Decrypts the utf8 data in inBuffer and puts the decrypted utf8
112 	// stuff in plainText. inBuffer is the thingie
113 	// that we got from outBuffer of encrupt() so it
114 	// follows the same rules.
115 	// Should return false in case of error.
116 	//
117 	virtual DecryptResult decrypt(const char * inBuffer, KviCString & plainText);
118 	//
119 	// Returns the string containing the description
120 	// of the last error or an empty string if there
121 	// was no error after the last init() call.
122 	//
lastError()123 	const QString & lastError() const { return m_szLastError; }
124 protected:
125 	//
126 	// The following two should have clear meaning
127 	//
clearLastError()128 	void clearLastError() { setLastError(""); }
setLastError(const QString & err)129 	void setLastError(const QString & err) { m_szLastError = err; }
130 #endif //COMPILE_CRYPT_SUPPORT
131 };
132 
133 #endif // _KVI_CRYPT_ENGINE_H_
134