MainWindow.cpp
上传用户:stc1860
上传日期:2007-01-12
资源大小:234k
文件大小:9k
源码类别:

CA认证

开发平台:

MultiPlatform

  1. /*
  2.  * Copyright (C) 2001 Christian Hohnstaedt.
  3.  *
  4.  *  All rights reserved.
  5.  *
  6.  *
  7.  *  Redistribution and use in source and binary forms, with or without 
  8.  *  modification, are permitted provided that the following conditions are met:
  9.  *
  10.  *  - Redistributions of source code must retain the above copyright notice,
  11.  *    this list of conditions and the following disclaimer.
  12.  *  - Redistributions in binary form must reproduce the above copyright notice,
  13.  *    this list of conditions and the following disclaimer in the documentation
  14.  *    and/or other materials provided with the distribution.
  15.  *  - Neither the name of the author nor the names of its contributors may be 
  16.  *    used to endorse or promote products derived from this software without
  17.  *    specific prior written permission.
  18.  *
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  *
  33.  * This program links to software with different licenses from:
  34.  *
  35.  * http://www.openssl.org which includes cryptographic software
  36.  *  written by Eric Young (eay@cryptsoft.com)"
  37.  *
  38.  * http://www.sleepycat.com
  39.  *
  40.  * http://www.trolltech.com
  41.  * 
  42.  *
  43.  *
  44.  * http://www.hohnstaedt.de/xca
  45.  * email: christian@hohnstaedt.de
  46.  *
  47.  * $Id: MainWindow.cpp,v 1.52 2003/01/06 19:35:46 chris Exp $ 
  48.  *
  49.  */                           
  50. #include "MainWindow.h"
  51. QPixmap *MainWindow::keyImg = NULL, *MainWindow::csrImg = NULL,
  52. *MainWindow::certImg = NULL, *MainWindow::tempImg = NULL,
  53. *MainWindow::nsImg = NULL, *MainWindow::revImg = NULL;
  54. MainWindow::MainWindow(QWidget *parent, const char *name ) 
  55. :MainWindow_UI(parent, name)
  56. {
  57. connect( (QObject *)quitApp, SIGNAL(clicked()), (QObject *)qApp, SLOT(quit()) );
  58. QString cpr = "(c) 2002 by Christian@Hohnstaedt.de - Version: ";
  59. copyright->setText(cpr + VER);
  60. setCaption(tr(XCA_TITLE));
  61. #ifdef WIN32
  62. baseDir = "";
  63. dbfile="xca.db";
  64. #else
  65. baseDir = QDir::homeDirPath();
  66. baseDir += QDir::separator();
  67. baseDir += BASE_DIR;
  68. QDir d(baseDir);
  69.         if ( ! d.exists() ){
  70. if (!d.mkdir(baseDir))
  71. qFatal(  "Couldnt create: " +  baseDir );
  72. }
  73. if (qApp->argc() <2){
  74. dbfile = "xca.db";
  75. }
  76. else {
  77. dbfile = qApp->argv()[1];
  78. }
  79. #endif
  80. try {
  81. dbenv = new DbEnv(0);
  82. dbenv->set_errcall(&MainWindow::dberr);
  83. dbenv->open(baseDir.latin1(), DB_RECOVER | DB_INIT_TXN | DB_INIT_MPOOL | DB_INIT_LOG | DB_INIT_LOCK | DB_CREATE , 0600 );
  84. MARK
  85. }
  86. catch (DbException &err) {
  87. DBEX(err);
  88. QString e = err.what();
  89. e += " (" + baseDir + ")";
  90. qFatal(e);
  91. }
  92. MARK
  93. ERR_load_crypto_strings();
  94. OpenSSL_add_all_algorithms();
  95. keyImg = loadImg("bigkey.png");
  96. csrImg = loadImg("bigcsr.png");
  97. certImg = loadImg("bigcert.png");
  98. tempImg = loadImg("bigtemp.png");
  99. nsImg = loadImg("netscape.png");
  100. revImg = loadImg("revoked.png");
  101. MARK
  102. try {
  103. settings = new db_base(dbenv, dbfile.latin1(), "settings");
  104. MARK
  105. initPass();
  106. keys = new db_key(dbenv, dbfile.latin1(), keyList);
  107. reqs = new db_x509req(dbenv, dbfile.latin1(), reqList, keys);
  108. certs = new db_x509(dbenv, dbfile.latin1(), certList, keys);
  109. temps = new db_temp(dbenv, dbfile.latin1(), tempList);
  110. }
  111. catch (errorEx &err) {
  112. Error(err);
  113. }
  114. catch (DbException &err) {
  115. DBEX(err);
  116. qFatal(err.what());
  117. }
  118. bigKey->setPixmap(*keyImg);
  119. bigCsr->setPixmap(*csrImg);
  120. bigCert->setPixmap(*certImg);
  121. bigTemp->setPixmap(*tempImg);
  122. bigRev->setPixmap(*revImg);
  123. #ifdef qt3
  124. connect( keyList, SIGNAL(itemRenamed(QListViewItem *, int, const QString &)),this, SLOT(renameKey(QListViewItem *, int, const QString &)));
  125. connect( reqList, SIGNAL(itemRenamed(QListViewItem *, int, const QString &)),this, SLOT(renameReq(QListViewItem *, int, const QString &)));
  126. connect( certList, SIGNAL(itemRenamed(QListViewItem *, int, const QString &)),this, SLOT(renameCert(QListViewItem *, int, const QString &)));
  127. connect( tempList, SIGNAL(itemRenamed(QListViewItem *, int, const QString &)),this, SLOT(renameTemp(QListViewItem *, int, const QString &)));
  128. #endif
  129. };
  130. MainWindow::~MainWindow() 
  131. {
  132.  ERR_free_strings();
  133.  EVP_cleanup();
  134.  delete(keys);
  135.  delete(reqs);
  136.  delete(certs);
  137.  delete(temps);
  138.  delete(settings);
  139.  dbenv->close(0);
  140. }
  141. QPixmap *MainWindow::loadImg(const char *name )
  142. {
  143.         return settings->loadImg(name);
  144. }
  145. void MainWindow::initPass()
  146. {
  147. PASS_INFO p;
  148. string passHash = settings->getString("pwhash");
  149. if (passHash == "") {
  150. string title=tr("New Password").latin1();
  151. string description=tr("Please enter a password, that will be used to encrypt your private keys in the database-file").latin1();
  152. p.title = &title;
  153. p.description = &description;
  154. int keylen = passWrite((char *)pki_key::passwd, 25, 0, &p);
  155. if (keylen == 0) {
  156. qFatal("Ohne Passwort laeuft hier gaaarnix :-)");
  157. }
  158. pki_key::passwd[keylen]='';
  159. settings->putString( "pwhash", md5passwd() );
  160. }
  161. else {
  162.      int keylen=0;
  163.      while (md5passwd() != passHash) {
  164. if (keylen !=0)
  165. QMessageBox::warning(this,tr(XCA_TITLE), tr("Password verify error, please try again"));
  166. string title=tr("Password").latin1();
  167. string description=tr("Please enter the password for unlocking the database").latin1();
  168. p.title = &title;
  169. p.description = &description;
  170. keylen = passRead(pki_key::passwd, 25, 0, &p);
  171. if (keylen == 0) {
  172. qFatal("Ohne Passwort laeuft hier gaaarnix :-)");
  173. }
  174. pki_key::passwd[keylen]='';
  175.     }
  176. }
  177. }
  178. void MainWindow::renamePKI(db_base *db)
  179. {
  180.         pki_base * pki = db->getSelectedPKI();
  181. if (!pki) return;
  182.         QString name= pki->getDescription().c_str();
  183. bool ok;
  184. QString nname = QInputDialog::getText (XCA_TITLE, "Please enter the new name",
  185. QLineEdit::Normal, name, &ok, this );
  186. if (ok && name != nname) {
  187. db->renamePKI(pki, nname.latin1());
  188. }
  189. }
  190. // Static Password Callback functions 
  191. int MainWindow::passRead(char *buf, int size, int rwflag, void *userdata)
  192. {
  193. PASS_INFO *p = (PASS_INFO *)userdata;
  194. PassRead_UI *dlg = new PassRead_UI(NULL, 0, true);
  195. if (p != NULL) {
  196. dlg->image->setPixmap( *keyImg );
  197. dlg->title->setText(tr(p->title->c_str()));
  198. dlg->description->setText(tr(p->description->c_str()));
  199. }
  200. dlg->pass->setFocus();
  201. dlg->setCaption(tr(XCA_TITLE));
  202. if (dlg->exec()) {
  203.    QString x = dlg->pass->text();
  204.    strncpy(buf, x.latin1(), size);
  205.    return x.length();
  206. }
  207. else return 0;
  208. }
  209. int MainWindow::passWrite(char *buf, int size, int rwflag, void *userdata)
  210. {
  211. PASS_INFO *p = (PASS_INFO *)userdata;
  212. PassWrite_UI *dlg = new PassWrite_UI(NULL, 0, true);
  213. if (p != NULL) {
  214. dlg->image->setPixmap( *keyImg );
  215. dlg->title->setText(tr(p->title->c_str()));
  216. dlg->description->setText(tr(p->description->c_str()));
  217. }
  218. dlg->passA->setFocus();
  219. dlg->setCaption(tr(XCA_TITLE));
  220. if (dlg->exec()) {
  221.    QString A = dlg->passA->text();
  222.    QString B = dlg->passB->text();
  223.    if (A != B) return 0;
  224.    strncpy(buf, A.latin1(), size);
  225.    return A.length();
  226. }
  227. else return 0;
  228. }
  229. void MainWindow::incProgress(int a, int b, void *progress)
  230. {
  231. int i = ((QProgressDialog *)progress)->progress();
  232. ((QProgressDialog *)progress)->setProgress(++i);
  233. }
  234. string MainWindow::md5passwd()
  235. {
  236. EVP_MD_CTX mdctx;
  237. string str;
  238. unsigned int n;
  239. int j;
  240. char zs[4];
  241. unsigned char m[EVP_MAX_MD_SIZE];
  242. EVP_DigestInit(&mdctx, EVP_md5());
  243. EVP_DigestUpdate(&mdctx, pki_key::passwd, strlen(pki_key::passwd));
  244. EVP_DigestFinal(&mdctx, m, &n);
  245. for (j=0; j<(int)n; j++) {
  246. sprintf(zs, "%02X%c",m[j], (j+1 == (int)n) ?'':':');
  247. str += zs;
  248. }
  249. return str;
  250. }
  251. bool MainWindow::opensslError(pki_base *pki)
  252. {
  253. string err;
  254. if (!pki) {
  255. QMessageBox::warning(this,tr(XCA_TITLE), tr("The system detected a NULL pointer, maybe the system is out of memory" ));
  256. qFatal("NULL pointer detected - Exiting");
  257. }
  258. if (( err = pki->getError()) != "") { 
  259. QMessageBox::warning(this,tr(XCA_TITLE), tr("The following error occured")+
  260. " (" + QString::fromLatin1(pki->getClassName().c_str()) +") :"+
  261. QString::fromLatin1(err.c_str()));
  262. return true;
  263. }
  264. return false;
  265. }
  266. void MainWindow::Error(errorEx &err)
  267. {
  268. if (err.isEmpty()) {
  269. CERR("Empty error Exception silently ignored");
  270. return;
  271. }
  272. QMessageBox::warning(this,tr(XCA_TITLE), tr("The following error occured:") + "n" +
  273. QString::fromLatin1(err.getCString()));
  274. }
  275. void MainWindow::crashApp()
  276. {
  277. pki_base * nullpointer = NULL;
  278. CERR("------> CRASHING the Application <----------");
  279. nullpointer->getDescription();
  280. }
  281. void MainWindow::dberr(const char *errpfx, char *msg)
  282. {
  283. CERR(errpfx << " " << msg);
  284. }
  285. void MainWindow::setPath(QFileDialog *dlg)
  286. {
  287. string wd = settings->getString("workingdir");
  288. if (!wd.empty()) {
  289. dlg->setDir(QString(wd.c_str()));
  290. }
  291. }
  292. void MainWindow::newPath(QFileDialog *dlg)
  293. {
  294. string wd = dlg->dirPath().latin1();
  295. settings->putString("workingdir", wd);
  296. }