gwenhywfar  4.3.3
fox16/cppgui.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Mar 01 2004
3  copyright : (C) 2004-2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14 
15 #include "cppgui_p.hpp"
16 #include <assert.h>
17 
18 #include <gwenhywfar/inherit.h>
19 #include <gwenhywfar/debug.h>
20 #include <gwenhywfar/gui_be.h>
21 #include <gwenhywfar/i18n.h>
22 
23 #include <gwenhywfar/text.h>
24 #include <gwenhywfar/mdigest.h>
25 #include <gwenhywfar/debug.h>
26 
27 
28 
30 
31 
32 
33 
34 int CppGuiLinker::Print(GWEN_GUI *gui,
35  const char *docTitle,
36  const char *docType,
37  const char *descr,
38  const char *text,
39  uint32_t guiid){
40  CppGui *xgui;
41 
42  assert(gui);
44  assert(xgui);
45 
46  return xgui->print(docTitle, docType, descr, text, guiid);
47 }
48 
49 
50 
51 int CppGuiLinker::GetPassword(GWEN_GUI *gui,
52  uint32_t flags,
53  const char *token,
54  const char *title,
55  const char *text,
56  char *buffer,
57  int minLen,
58  int maxLen,
59  uint32_t guiid) {
60  CppGui *xgui;
61 
62  assert(gui);
64  assert(xgui);
65 
66  return xgui->getPassword(flags, token, title, text, buffer, minLen, maxLen, guiid);
67 }
68 
69 
70 
71 int CppGuiLinker::SetPasswordStatus(GWEN_GUI *gui,
72  const char *token,
73  const char *pin,
75  uint32_t guiid) {
76  CppGui *xgui;
77 
78  assert(gui);
80  assert(xgui);
81 
82  return xgui->setPasswordStatus(token, pin, status, guiid);
83 }
84 
85 
86 
87 int CppGuiLinker::CheckCert(GWEN_GUI *gui,
88  const GWEN_SSLCERTDESCR *cert,
89  GWEN_SYNCIO *sio,
90  uint32_t guiid) {
91  CppGui *xgui;
92 
93  assert(gui);
95  assert(xgui);
96 
97  return xgui->checkCert(cert, sio, guiid);
98 }
99 
100 
101 
102 int CppGuiLinker::LogHook(GWEN_GUI *gui,
103  const char *logDomain,
104  GWEN_LOGGER_LEVEL priority, const char *s) {
105  CppGui *xgui;
106 
107  assert(gui);
109  assert(xgui);
110 
111  return xgui->logHook(logDomain, priority, s);
112 }
113 
114 
115 
116 int CppGuiLinker::ExecDialog(GWEN_GUI *gui,
117  GWEN_DIALOG *dlg,
118  uint32_t guiid) {
119  CppGui *xgui;
120 
121  assert(gui);
123  assert(xgui);
124 
125  return xgui->execDialog(dlg, guiid);
126 }
127 
128 
129 
130 int CppGuiLinker::OpenDialog(GWEN_GUI *gui,
131  GWEN_DIALOG *dlg,
132  uint32_t guiid) {
133  CppGui *xgui;
134 
135  assert(gui);
137  assert(xgui);
138 
139  return xgui->openDialog(dlg, guiid);
140 }
141 
142 
143 
144 int CppGuiLinker::CloseDialog(GWEN_GUI *gui,
145  GWEN_DIALOG *dlg) {
146  CppGui *xgui;
147 
148  assert(gui);
150  assert(xgui);
151 
152  return xgui->closeDialog(dlg);
153 }
154 
155 
156 
157 int CppGuiLinker::RunDialog(GWEN_GUI *gui,
158  GWEN_DIALOG *dlg,
159  int untilEnd) {
160  CppGui *xgui;
161 
162  assert(gui);
164  assert(xgui);
165 
166  return xgui->runDialog(dlg, untilEnd);
167 }
168 
169 
170 
171 int CppGuiLinker::GetFileName(GWEN_GUI *gui,
172  const char *caption,
174  uint32_t flags,
175  const char *patterns,
176  GWEN_BUFFER *pathBuffer,
177  uint32_t guiid) {
178  CppGui *xgui;
179 
180  assert(gui);
182  assert(xgui);
183 
184  return xgui->getFileName(caption, fnt, flags, patterns, pathBuffer, guiid);
185 }
186 
187 
188 
190 void CppGuiLinker::freeData(void *bp, void *p) {
191  CppGui *xgui;
192 
193  DBG_NOTICE(0, "CppGuiLinker: Freeing CppGui");
194  xgui=(CppGui*)p;
195  if (xgui->_gui) {
196  xgui->_gui=0;
197  }
198  delete xgui;
199 }
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
213 :_checkCertFn(NULL)
214 ,_dbPasswords(NULL)
215 ,_gui(NULL) {
216  _gui=GWEN_Gui_new();
217  _dbPasswords=GWEN_DB_Group_new("passwords");
218 
220  _gui, this,
221  CppGuiLinker::freeData);
223  _printFn=GWEN_Gui_SetPrintFn(_gui, CppGuiLinker::Print);
224  _getPasswordFn=GWEN_Gui_SetGetPasswordFn(_gui, CppGuiLinker::GetPassword);
225  _setPasswordStatusFn=GWEN_Gui_SetSetPasswordStatusFn(_gui, CppGuiLinker::SetPasswordStatus);
226  _checkCertFn=GWEN_Gui_SetCheckCertFn(_gui, CppGuiLinker::CheckCert);
227  GWEN_Gui_SetLogHookFn(_gui, CppGuiLinker::LogHook);
228  _execDialogFn=GWEN_Gui_SetExecDialogFn(_gui, CppGuiLinker::ExecDialog);
229  _openDialogFn=GWEN_Gui_SetOpenDialogFn(_gui, CppGuiLinker::OpenDialog);
230  _closeDialogFn=GWEN_Gui_SetCloseDialogFn(_gui, CppGuiLinker::CloseDialog);
231  _runDialogFn=GWEN_Gui_SetRunDialogFn(_gui, CppGuiLinker::RunDialog);
232  _getFileNameFn=GWEN_Gui_SetGetFileNameFn(_gui, CppGuiLinker::GetFileName);
233 }
234 
235 
236 
238  if (_gui) {
241  }
242  GWEN_DB_Group_free(_dbPasswords);
243 }
244 
245 
246 
247 int CppGui::print(const char *docTitle,
248  const char *docType,
249  const char *descr,
250  const char *text,
251  uint32_t guiid){
252  if (_printFn)
253  return _printFn(_gui, docTitle, docType, descr, text, guiid);
254  else
256 }
257 
258 
259 
260 std::string CppGui::_getPasswordHash(const char *token, const char *pin) {
261  GWEN_MDIGEST *md;
262  std::string s;
263  GWEN_BUFFER *buf;
264  int rv;
265 
266  /* hash token and pin */
268  rv=GWEN_MDigest_Begin(md);
269  if (rv==0)
270  rv=GWEN_MDigest_Update(md, (const uint8_t*)token, strlen(token));
271  if (rv==0)
272  rv=GWEN_MDigest_Update(md, (const uint8_t*)pin, strlen(pin));
273  if (rv==0)
274  rv=GWEN_MDigest_End(md);
275  if (rv<0) {
276  DBG_ERROR(GWEN_LOGDOMAIN, "Hash error (%d)", rv);
277  GWEN_MDigest_free(md);
278  return "";
279  }
280 
281  buf=GWEN_Buffer_new(0, 256, 0, 1);
284  buf,
285  0, 0, 0);
286  s=std::string(GWEN_Buffer_GetStart(buf),
288  GWEN_Buffer_free(buf);
289 
290  GWEN_MDigest_free(md);
291  return s;
292 }
293 
294 
295 
296 int CppGui::getPassword(uint32_t flags,
297  const char *token,
298  const char *title,
299  const char *text,
300  char *buffer,
301  int minLen,
302  int maxLen,
303  uint32_t guiid) {
304  if (flags & GWEN_GUI_INPUT_FLAGS_TAN) {
305  return GWEN_Gui_InputBox(flags,
306  title,
307  text,
308  buffer,
309  minLen,
310  maxLen,
311  guiid);
312  }
313  else {
314  GWEN_BUFFER *buf;
315  int rv;
316  const char *s;
317 
318  buf=GWEN_Buffer_new(0, 256, 0, 1);
320 
321  if (!(flags & GWEN_GUI_INPUT_FLAGS_CONFIRM)) {
322  s=GWEN_DB_GetCharValue(_dbPasswords,
324  0, NULL);
325  if (s) {
326  int i;
327 
328  i=strlen(s);
329  if (i>=minLen && i<=maxLen) {
330  memmove(buffer, s, i+1);
331  GWEN_Buffer_free(buf);
332  return 0;
333  }
334  }
335  }
336 
337  for (;;) {
338  rv=GWEN_Gui_InputBox(flags,
339  title,
340  text,
341  buffer,
342  minLen,
343  maxLen,
344  guiid);
345  if (rv) {
346  GWEN_Buffer_free(buf);
347  return rv;
348  }
349  else {
350  std::string s;
351  std::list<std::string>::iterator it;
352  bool isBad=false;
353 
354  s=_getPasswordHash(token, buffer);
355  for (it=_badPasswords.begin();
356  it!=_badPasswords.end();
357  it++) {
358  if (*it==s) {
359  /* password is bad */
360  isBad=true;
361  break;
362  }
363  }
364 
365  if (!isBad)
366  break;
370  I18N("Enforce PIN"),
371  I18N(
372  "You entered the same PIN twice.\n"
373  "The PIN is marked as bad, do you want\n"
374  "to use it anyway?"
375  "<html>"
376  "<p>"
377  "You entered the same PIN twice."
378  "</p>"
379  "<p>"
380  "The PIN is marked as <b>bad</b>, "
381  "do you want to use it anyway?"
382  "</p>"
383  "</html>"),
384  I18N("Use my input"),
385  I18N("Re-enter"),
386  0,
387  guiid);
388  if (rv==1) {
389  /* accept this input */
390  _badPasswords.remove(s);
391  break;
392  }
393  }
394  }
395 
396  GWEN_Buffer_free(buf);
397  return 0;
398  }
399 }
400 
401 
402 
404  GWEN_SYNCIO *sio,
405  uint32_t guiid) {
406  return checkCertBuiltIn(cd, sio, guiid);
407 }
408 
409 
410 
411 int CppGui::logHook(const char *logDomain,
412  GWEN_LOGGER_LEVEL priority, const char *s) {
413  /* not hooked */
414  return 0;
415 }
416 
417 
418 
419 int CppGui::execDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
421 }
422 
423 
424 
425 int CppGui::openDialog(GWEN_DIALOG *dlg, uint32_t guiid) {
427 }
428 
429 
430 
433 }
434 
435 
436 
437 int CppGui::runDialog(GWEN_DIALOG *dlg, int untilEnd) {
439 }
440 
441 
442 
443 int CppGui::getFileName(const char *caption,
445  uint32_t flags,
446  const char *patterns,
447  GWEN_BUFFER *pathBuffer,
448  uint32_t guiid) {
449  DBG_ERROR(GWEN_LOGDOMAIN, "Not supported");
451 }
452 
453 
454 
455 
457  GWEN_SYNCIO *sio,
458  uint32_t guiid) {
459  if (_checkCertFn)
460  return _checkCertFn(_gui, cert, sio, guiid);
461  else {
462  DBG_ERROR(GWEN_LOGDOMAIN, "No built-in checkcert function?");
464  }
465 }
466 
467 
468 
469 int CppGui::setPasswordStatus(const char *token,
470  const char *pin,
472  uint32_t guiid) {
473  if (token==NULL && pin==NULL && status==GWEN_Gui_PasswordStatus_Remove) {
474  GWEN_DB_ClearGroup(_dbPasswords, NULL);
475  }
476  else {
477  GWEN_BUFFER *buf;
478  std::string s;
479 
480  buf=GWEN_Buffer_new(0, 256, 0, 1);
482 
483  s=_getPasswordHash(token, pin);
484  if (status==GWEN_Gui_PasswordStatus_Bad) {
485  std::list<std::string>::iterator it;
486 
487  s=_getPasswordHash(token, pin);
488  for (it=_badPasswords.begin();
489  it!=_badPasswords.end();
490  it++) {
491  if (*it==s) {
492  /* bad password already in list */
493  GWEN_Buffer_free(buf);
494  return 0;
495  }
496  }
497  _badPasswords.push_back(s);
498  }
499  else if (status==GWEN_Gui_PasswordStatus_Ok) {
500  /* only store passwords of which we know that they are ok */
502  GWEN_Buffer_GetStart(buf), pin);
503  }
504  GWEN_Buffer_free(buf);
505  }
506 
507  return 0;
508 }
509 
510 
511 
513  return _gui;
514 }
515 
516 
517 
519  GWEN_GUI *gui;
520  CppGui *xgui;
521 
522  gui=GWEN_Gui_GetGui();
523  if (gui==NULL)
524  return NULL;
526  return xgui;
527 }
528 
529 
530 
531 
532