1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
//
// File : kvi_cryptcontroller.cpp
// Creation date : Fri Nov 03 2000 14:16:33 CEST by Szymon Stefanek
//
// This file is part of the KVirc irc client distribution
// Copyright (C) 2000-2005 Szymon Stefanek (pragma at kvirc dot net)
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your opinion) any later version.
//
// This program is distributed in the HOPE that it will be USEFUL,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#define __KVIRC__
#include "kvi_settings.h"
#ifdef COMPILE_CRYPT_SUPPORT
#include "kvi_cryptcontroller.h"
#include "kvi_styled_controls.h"
#include "kvi_window.h"
#include "kvi_locale.h"
#include "kvi_out.h"
#include "kvi_iconmanager.h"
#include "kvi_modulemanager.h"
#include "kvi_module.h"
#include "kvi_malloc.h"
#include "kvi_memmove.h"
#include "kvi_toolwindows_container.h"
#include <tqlayout.h>
#include "kvi_pointerhashtable.h"
// kvi_app.cpp
extern KVIRC_API KviCryptEngineManager * g_pCryptEngineManager;
extern KVIRC_API KviModuleManager * g_pModuleManager;
KviEngineListBoxItem::KviEngineListBoxItem(KviTalListBox * lb,KviCryptEngineDescription * d,const char * modName)
: KviTalListBoxText(lb,d->szName)
{
m_szName = d->szName;
m_szAuthor = d->szAuthor;
m_szDescription = d->szDescription;
m_iFlags = d->iFlags;
m_szModuleName = modName;
setText(d->szName);
}
KviEngineListBoxItem::~KviEngineListBoxItem()
{
}
KviCryptController::KviCryptController(TQWidget * par,KviWindowToolPageButton* button,const char * name,KviWindow * wnd,KviCryptSessionInfo * cur)
: KviWindowToolWidget(par,button)
{
// FIXME: #warning "Load the available modules here"
// Load the known encryption modules
(void)g_pModuleManager->loadModulesByCaps("crypt");
// (void)g_pModuleManager->getModule("koi2win");
// (void)g_pModuleManager->getModule("mircstrip");
// (void)g_pModuleManager->getModule("texturizer");
m_pWindow = wnd;
#ifdef COMPILE_USE_QT4
setFocusPolicy(TTQ_ClickFocus);
#else
setFocusPolicy(TQ_ClickFocus);
#endif
TQGridLayout *g = new TQGridLayout(this,10,4,2,2);
TQLabel *l = new TQLabel(this);
l->setPixmap(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_LOCKED)));
g->addWidget(l,0,0);
l = new TQLabel(__tr2qs("Cryptography/text transformation"),this);
g->addMultiCellWidget(l,0,0,1,3);
TQFrame * frm = new TQFrame(this);
frm->setFrameStyle(TQFrame::HLine | TQFrame::Sunken);
g->addMultiCellWidget(frm,1,1,0,3);
m_pEnableCheck = new KviStyledCheckBox(__tr2qs("Use the crypt engine"),this);
g->addMultiCellWidget(m_pEnableCheck,2,2,0,3);
connect(m_pEnableCheck,TQT_SIGNAL(toggled(bool)),this,TQT_SLOT(enableCheckToggled(bool)));
m_pListBox = new KviTalListBox(this);
connect(m_pListBox,TQT_SIGNAL(highlighted(KviTalListBoxItem *)),this,TQT_SLOT(engineHighlighted(KviTalListBoxItem *)));
g->addMultiCellWidget(m_pListBox,3,8,0,0);
m_pDescriptionLabel = new TQLabel(this);
m_pDescriptionLabel->setFrameStyle(TQFrame::Sunken | TQFrame::StyledPanel);
#ifdef COMPILE_USE_QT4
m_pDescriptionLabel->setAlignment(TQt::AlignTop | TQt::AlignLeft);
#else
m_pDescriptionLabel->setAlignment(TQt::AlignTop | TQt::AlignLeft);
#endif
g->addMultiCellWidget(m_pDescriptionLabel,3,3,1,3);
m_pAuthorLabel = new TQLabel(this);
m_pAuthorLabel->setFrameStyle(TQFrame::Sunken | TQFrame::StyledPanel);
g->addMultiCellWidget(m_pAuthorLabel,4,4,1,3);
m_pEnableEncrypt = new KviStyledCheckBox(__tr2qs("Enable encryption"),this);
g->addMultiCellWidget(m_pEnableEncrypt,5,5,1,3);
m_pEncryptKeyLabel = new TQLabel(__tr2qs("Encrypt key:"),this);
g->addWidget(m_pEncryptKeyLabel,6,1);
m_pEncryptKeyEdit = new TQLineEdit(this);
g->addWidget(m_pEncryptKeyEdit,6,2);
m_pEncryptHexKeyCheck = new KviStyledCheckBox(__tr2qs("Hex"),this);
g->addWidget(m_pEncryptHexKeyCheck,6,3);
m_pEnableDecrypt = new KviStyledCheckBox(__tr2qs("Enable decryption"),this);
g->addMultiCellWidget(m_pEnableDecrypt,7,7,1,3);
m_pDecryptKeyLabel = new TQLabel(__tr2qs("Decrypt key:"),this);
g->addWidget(m_pDecryptKeyLabel,8,1);
m_pDecryptKeyEdit = new TQLineEdit(this);
g->addWidget(m_pDecryptKeyEdit,8,2);
m_pDecryptHexKeyCheck = new KviStyledCheckBox(__tr2qs("Hex"),this);
g->addWidget(m_pDecryptHexKeyCheck,8,3);
m_pOkButton = new TQPushButton(__tr2qs("OK"),this);
g->addMultiCellWidget(m_pOkButton,9,9,0,3);
connect(m_pOkButton,TQT_SIGNAL(clicked()),this,TQT_SLOT(okClicked()));
g->setRowStretch(3,1);
g->setColStretch(2,1);
m_pLastItem = 0;
m_pSessionInfo = 0;
fillEngineList();
if(cur)
{
KviTalListBoxItem * it = (KviTalListBoxItem *)m_pListBox->findItem(cur->szEngineName.ptr());
if(it)
{
m_pEnableCheck->setChecked(true);
m_pListBox->setCurrentItem(it);
engineHighlighted(it);
m_pEnableEncrypt->setChecked(cur->bDoEncrypt);
m_pEnableDecrypt->setChecked(cur->bDoDecrypt);
} else enableWidgets(false);
} else {
enableWidgets(false);
}
registerSelf();
}
KviCryptController::~KviCryptController()
{
if(m_pSessionInfo)
{
// huh ?
g_pCryptEngineManager->deallocateEngine(m_pSessionInfo->pEngine);
delete m_pSessionInfo;
}
// Unload the unused ones...
g_pModuleManager->cleanupUnusedModules();
}
void KviCryptController::fillEngineList()
{
const KviPointerHashTable<TQString,KviCryptEngineDescription> * a = g_pCryptEngineManager->engineDict();
if(a)
{
KviPointerHashTableIterator<TQString,KviCryptEngineDescription> it(*a);
while(it.current())
{
KviStr modName = it.current()->providerHandle ? ((KviModule *)(it.current()->providerHandle))->name() : "";
(void)(new KviEngineListBoxItem(m_pListBox,it.current(),modName.ptr()));
++it;
}
if(m_pListBox->count() != 0)return;
}
noEnginesAvailable();
}
void KviCryptController::engineHighlighted(KviTalListBoxItem *it)
{
if(it)
{
KviEngineListBoxItem * eit = (KviEngineListBoxItem *)it;
m_pAuthorLabel->setText(eit->m_szAuthor.ptr());
TQString des = "<p>";
des += eit->m_szDescription.ptr();
des += "<br><br>";
des += __tr2qs("If you dont want to encrypt a particular text line then just start it with the CTRL+P prefix");
m_pDescriptionLabel->setText(des);
m_pEnableEncrypt->setEnabled(eit->m_iFlags & KVI_CRYPTENGINE_CAN_ENCRYPT);
m_pEncryptKeyLabel->setEnabled((eit->m_iFlags & KVI_CRYPTENGINE_CAN_ENCRYPT) &&
(eit->m_iFlags & KVI_CRYPTENGINE_WANT_ENCRYPT_KEY));
m_pEncryptKeyEdit->setEnabled((eit->m_iFlags & KVI_CRYPTENGINE_CAN_ENCRYPT) &&
(eit->m_iFlags & KVI_CRYPTENGINE_WANT_ENCRYPT_KEY));
m_pEnableEncrypt->setChecked(m_pEncryptKeyEdit->isEnabled());
m_pEnableDecrypt->setEnabled(eit->m_iFlags & KVI_CRYPTENGINE_CAN_DECRYPT);
m_pEncryptHexKeyCheck->setEnabled(m_pEncryptKeyEdit->isEnabled());
m_pEncryptHexKeyCheck->setChecked(false);
m_pDecryptKeyLabel->setEnabled((eit->m_iFlags & KVI_CRYPTENGINE_CAN_DECRYPT) &&
(eit->m_iFlags & KVI_CRYPTENGINE_WANT_DECRYPT_KEY));
m_pDecryptKeyEdit->setEnabled((eit->m_iFlags & KVI_CRYPTENGINE_CAN_DECRYPT) &&
(eit->m_iFlags & KVI_CRYPTENGINE_WANT_DECRYPT_KEY));
m_pEnableDecrypt->setChecked(m_pDecryptKeyEdit->isEnabled());
m_pDecryptHexKeyCheck->setEnabled(m_pDecryptKeyEdit->isEnabled());
m_pDecryptHexKeyCheck->setChecked(false);
m_pLastItem = eit;
enableWidgets(true);
} else m_pLastItem = 0;
}
void KviCryptController::enableCheckToggled(bool bChecked)
{
enableWidgets(bChecked);
}
void KviCryptController::enableWidgets(bool bEnabled)
{
m_pListBox->setEnabled(bEnabled);
m_pAuthorLabel->setEnabled(bEnabled && m_pLastItem);
m_pDescriptionLabel->setEnabled(bEnabled && m_pLastItem);
bool bCanDecrypt = m_pLastItem ? m_pLastItem->m_iFlags & KVI_CRYPTENGINE_CAN_DECRYPT : false;
bool bCanEncrypt = m_pLastItem ? m_pLastItem->m_iFlags & KVI_CRYPTENGINE_CAN_ENCRYPT : false;
m_pEnableEncrypt->setEnabled(bEnabled && bCanEncrypt);
m_pEnableDecrypt->setEnabled(bEnabled && bCanDecrypt);
bool bWantDecryptKey = m_pLastItem ? (bCanDecrypt && (m_pLastItem->m_iFlags & KVI_CRYPTENGINE_WANT_DECRYPT_KEY)) : false;
bool bWantEncryptKey = m_pLastItem ? (bCanEncrypt && (m_pLastItem->m_iFlags & KVI_CRYPTENGINE_WANT_ENCRYPT_KEY)) : false;
m_pEncryptKeyLabel->setEnabled(bEnabled && m_pEnableEncrypt->isChecked() && bWantEncryptKey);
m_pDecryptKeyLabel->setEnabled(bEnabled && m_pEnableDecrypt->isChecked() && bWantDecryptKey);
m_pEncryptKeyEdit->setEnabled(m_pEncryptKeyLabel->isEnabled());
m_pDecryptKeyEdit->setEnabled(m_pDecryptKeyLabel->isEnabled());
m_pEncryptHexKeyCheck->setEnabled(m_pEncryptKeyLabel->isEnabled());
m_pDecryptHexKeyCheck->setEnabled(m_pDecryptKeyLabel->isEnabled());
}
void KviCryptController::noEnginesAvailable()
{
m_pEnableCheck->setEnabled(false);
enableWidgets(false);
m_pDescriptionLabel->setText(__tr2qs("Sorry, no crypt engines available"));
m_pDescriptionLabel->setEnabled(true); // we want this text to be visible.
m_pOkButton->setEnabled(false);
}
void KviCryptController::okClicked()
{
if(m_pEnableCheck->isChecked())
{
if(m_pLastItem)
{
if(m_pEnableEncrypt->isChecked() || m_pEnableDecrypt->isChecked())
{
m_pSessionInfo = allocateCryptSessionInfo();
// Reregister the module in case that it has been unloaded
// while this dialog was open
if(m_pLastItem->m_szModuleName.hasData())(void)g_pModuleManager->getModule(m_pLastItem->m_szModuleName.ptr());
m_pSessionInfo->pEngine = g_pCryptEngineManager->allocateEngine(m_pLastItem->m_szName.ptr());
if(!m_pSessionInfo->pEngine)
{
m_pWindow->output(KVI_OUT_SYSTEMERROR,__tr2qs("Crypt: Can't create an engine instance: crypting disabled"));
delete m_pSessionInfo;
m_pSessionInfo = 0;
} else {
// initialize the engine
if(!initializeEngine(m_pSessionInfo->pEngine))
{
KviStr errStr = m_pSessionInfo->pEngine->lastError();
g_pCryptEngineManager->deallocateEngine(m_pSessionInfo->pEngine);
delete m_pSessionInfo;
m_pSessionInfo = 0;
m_pWindow->output(KVI_OUT_SYSTEMERROR,__tr2qs("Crypt: Can't initialize the engine :%s"),errStr.ptr());
} else {
// ok, engine ready and waiting...
m_pSessionInfo->szEngineName = m_pLastItem->m_szName;
m_pSessionInfo->bDoEncrypt = m_pEnableEncrypt->isChecked();
m_pSessionInfo->bDoDecrypt = m_pEnableDecrypt->isChecked();
}
}
} else m_pWindow->output(KVI_OUT_SYSTEMERROR,__tr2qs("Crypt: You have to enable encryption and/or decryption for the engine to work"));
}
}
emit done();
}
bool KviCryptController::initializeEngine(KviCryptEngine * eng)
{
KviStr m_szEncryptKey;
KviStr m_szDecryptKey;
char * encKey = 0;
int encKeyLen = 0;
if(m_pEnableEncrypt->isChecked())
{
m_szEncryptKey = m_pEncryptKeyEdit->text();
if(m_pEncryptHexKeyCheck->isChecked())
{
char * tmpKey;
encKeyLen = m_szEncryptKey.hexToBuffer(&tmpKey,false);
if(encKeyLen > 0)
{
encKey = (char *)kvi_malloc(encKeyLen);
kvi_memmove(encKey,tmpKey,encKeyLen);
KviStr::freeBuffer(tmpKey);
}
} else {
encKey = (char *)kvi_malloc(m_szEncryptKey.len());
kvi_memmove(encKey,m_szEncryptKey.ptr(),m_szEncryptKey.len());
encKeyLen = m_szEncryptKey.len();
}
}
char * decKey = 0;
int decKeyLen = 0;
if(m_pEnableDecrypt->isChecked())
{
m_szDecryptKey = m_pDecryptKeyEdit->text();
if(m_pDecryptHexKeyCheck->isChecked())
{
char * tmpKey;
decKeyLen = m_szDecryptKey.hexToBuffer(&tmpKey,false);
if(decKeyLen > 0)
{
decKey = (char *)kvi_malloc(decKeyLen);
kvi_memmove(decKey,tmpKey,decKeyLen);
KviStr::freeBuffer(tmpKey);
}
} else {
decKey = (char *)kvi_malloc(m_szDecryptKey.len());
kvi_memmove(decKey,m_szDecryptKey.ptr(),m_szDecryptKey.len());
decKeyLen = m_szDecryptKey.len();
}
}
bool bRet = eng->init(encKey,encKeyLen,decKey,decKeyLen);
if(encKey)kvi_free(encKey);
if(decKey)kvi_free(decKey);
return bRet;
}
KviCryptSessionInfo * KviCryptController::getNewSessionInfo()
{
KviCryptSessionInfo * inf = m_pSessionInfo;
m_pSessionInfo = 0;
return inf;
}
KviCryptSessionInfo * KviCryptController::allocateCryptSessionInfo()
{
// this is mainly for modules wanting to alloc this
return new KviCryptSessionInfo();
}
void KviCryptController::destroyCryptSessionInfo(KviCryptSessionInfo ** inf)
{
if(!(*inf))return;
(*inf)->pEngine->disconnect(); // disconnect every signal (this is mainly for destroyed())
g_pCryptEngineManager->deallocateEngine((*inf)->pEngine); // kill the engine
delete *inf;
*inf = 0;
}
#include "kvi_cryptcontroller.moc"
#endif //COMPILE_CRYPT_SUPPORT
|