MainWindowKeys.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: MainWindowKeys.cpp,v 1.42 2003/01/06 19:35:46 chris Exp $ 
  48.  *
  49.  */                           
  50. #include "MainWindow.h"
  51. const int MainWindow::sizeList[] = {256, 512, 1024, 2048, 4096, 0 };
  52. pki_key *MainWindow::getSelectedKey()
  53. {
  54. CERR( "get Selected Key");
  55. pki_key *targetKey = (pki_key *)keys->getSelectedPKI();
  56. CERR( "got selected: "<< (int)targetKey );
  57. if (targetKey) {
  58.    string errtxt = targetKey->getError();
  59.    if (errtxt != "")
  60. QMessageBox::warning(this,tr(XCA_TITLE),
  61. tr("The Key: ") + QString::fromLatin1(targetKey->getDescription().c_str()) +
  62. tr(" is not consistent:") + QString::fromLatin1(errtxt.c_str()) );
  63. }
  64. CERR( "targetKey = " << (int)targetKey );
  65. return targetKey;
  66. }
  67. void MainWindow::newKey()
  68. {
  69. NewKey_UI *dlg = new NewKey_UI(this,0,true,0);
  70. QString x;
  71. for (int i=0; sizeList[i] != 0; i++ ) 
  72.    dlg->keyLength->insertItem( x.number(sizeList[i]) +" bit");
  73. dlg->keyLength->setCurrentItem(2);
  74. dlg->image->setPixmap(*keyImg);
  75. if (dlg->exec()) {
  76.   try {
  77.    int sel = dlg->keyLength->currentItem();
  78.    QProgressDialog *progress = new QProgressDialog(
  79. tr("Please wait, Key generation is in progress"),
  80. tr("Cancel"),90, 0, 0, true);
  81.    progress->setMinimumDuration(0);
  82.    progress->setProgress(0);
  83.    progress->setCaption(tr(XCA_TITLE));
  84.    pki_key *nkey = new pki_key (dlg->keyDesc->text().latin1(), 
  85.        &MainWindow::incProgress,
  86.        progress,
  87.        sizeList[sel]);
  88.            progress->cancel();
  89.    delete progress;
  90.    insertKey(nkey);
  91.    x = nkey->getDescription().c_str();
  92.    emit keyDone(x);
  93.   }
  94.   catch (errorEx &err) {
  95.   Error(err);
  96.   }
  97. }
  98. delete dlg;
  99. }
  100. void MainWindow::deleteKey()
  101. {
  102. pki_key *delKey = getSelectedKey();
  103. if (!delKey) return;
  104. if (QMessageBox::information(this,tr(XCA_TITLE),
  105. tr("The key") + ": '" + 
  106. QString::fromLatin1(delKey->getDescription().c_str()) +
  107. "'n" + tr("is going to be deleted"),
  108. "Delete", "Cancel")
  109. ) return;
  110. try {
  111. keys->deletePKI(delKey);
  112. }
  113. catch (errorEx &err) {
  114. Error(err);
  115. }
  116. }
  117. bool MainWindow::showDetailsKey(pki_key *key, bool import)
  118. {
  119. if (opensslError(key)) return false;
  120. KeyDetail_UI *detDlg = new KeyDetail_UI(this, 0, true, 0 );
  121. try {
  122. detDlg->setCaption(tr(XCA_TITLE));
  123. detDlg->keyDesc->setText(
  124. key->getDescription().c_str() );
  125. detDlg->keyLength->setText(
  126. key->length().c_str() );
  127. detDlg->keyPubEx->setText(
  128. key->pubEx().c_str() );
  129. detDlg->keyModulus->setText(
  130. key->modulus().c_str() );
  131. if (key->isPubKey()) {
  132. detDlg->keyPrivEx->setText(tr("not available") );
  133. detDlg->keyPrivEx->setDisabled(true);
  134. }
  135. detDlg->image->setPixmap(*keyImg);
  136. if (import) {
  137. detDlg->but_ok->setText(tr("Import"));
  138. detDlg->but_cancel->setText(tr("Discard"));
  139. }
  140. }
  141. catch (errorEx &err) {
  142. Error(err);
  143. delete detDlg;
  144. return false;
  145. }
  146. bool ret = detDlg->exec();
  147. string ndesc = detDlg->keyDesc->text().latin1();
  148. delete detDlg;
  149. CERR(ndesc << " " << key->getDescription());
  150. if ( ret && ndesc != key->getDescription()) {
  151. MARK
  152. try {
  153. keys->renamePKI(key, ndesc);
  154. MARK
  155. }
  156. catch (errorEx &err) {
  157. Error(err);
  158. }
  159. return true;
  160. }
  161. MARK
  162. return false;
  163. }
  164. void MainWindow::showDetailsKey()
  165. {
  166. pki_key *targetKey = getSelectedKey();
  167. if (targetKey) showDetailsKey(targetKey);
  168. }
  169. void MainWindow::showDetailsKey(QListViewItem *item)
  170. {
  171. string key = item->text(0).latin1();
  172. showDetailsKey((pki_key *)keys->getSelectedPKI(key));
  173. }
  174. void MainWindow::loadKey()
  175. {
  176. QStringList filt;
  177. filt.append( "PKI Keys ( *.pem *.der )"); 
  178. filt.append( "PKCS#8 Keys ( *.p8 *.pk8 )"); 
  179. filt.append( "All Files ( *.* )");
  180. QString s="";
  181. QStringList slist;
  182. QFileDialog *dlg = new QFileDialog(this,0,true);
  183. dlg->setCaption("Import key");
  184. dlg->setFilters(filt);
  185. dlg->setMode( QFileDialog::ExistingFiles );
  186. setPath(dlg);
  187. if (dlg->exec()) {
  188. slist = dlg->selectedFiles();
  189. newPath(dlg);
  190. }
  191. delete dlg;
  192. for ( QStringList::Iterator it = slist.begin(); it != slist.end(); ++it ) {
  193. s = *it;
  194. s = QDir::convertSeparators(s);
  195. try {
  196. pki_key *lkey = new pki_key(s.latin1(), &MainWindow::passRead);
  197. insertKey(lkey);
  198. }
  199. catch (errorEx &err) {
  200. Error(err);
  201. }
  202. }
  203. }
  204. void MainWindow::insertKey(pki_key *lkey)
  205. {
  206. pki_key *oldkey;
  207. try {
  208.     oldkey = (pki_key *)keys->findPKI(lkey);
  209.     if (oldkey != NULL) {
  210. if ((oldkey->isPrivKey() && lkey->isPrivKey()) ||
  211.     lkey->isPubKey()){
  212.         QMessageBox::information(this,tr(XCA_TITLE),
  213. tr("The key is already in the database as") +":n'" +
  214. QString::fromLatin1(oldkey->getDescription().c_str()) + 
  215. "'n" + tr("and is not going to be imported"), "OK");
  216.     delete(lkey);
  217.     return;
  218. }
  219. else {
  220.         QMessageBox::information(this,tr(XCA_TITLE),
  221. tr("The database already contains the public part of the imported key as") +":n'" +
  222. QString::fromLatin1(oldkey->getDescription().c_str()) + 
  223. "'n" + tr("and will be completed by the new, private part of the key"), "OK");
  224.     CERR( "before deleting pki...");
  225.     keys->deletePKI(oldkey);
  226.     lkey->setDescription(oldkey->getDescription());
  227.     delete(oldkey);
  228. }
  229.     }
  230.     CERR( "after findkey");
  231.     keys->insertPKI(lkey);
  232. }
  233. catch (errorEx &err) {
  234. Error(err);
  235. }
  236. }
  237. void MainWindow::writeKey()
  238. {
  239. bool PEM=false;
  240. const EVP_CIPHER *enc = NULL;
  241. pki_key *targetKey = NULL;
  242. targetKey = getSelectedKey();
  243. if (!targetKey) return;
  244. ExportKey *dlg = new ExportKey((targetKey->getDescription() + ".pem").c_str(),
  245. targetKey->isPubKey(), this);
  246. dlg->image->setPixmap(*keyImg);
  247. if (!dlg->exec()) {
  248. delete dlg;
  249. return;
  250. }
  251. string fname = dlg->filename->text().latin1();
  252. if (fname == "") {
  253. delete dlg;
  254. return;
  255. }
  256. try {
  257. if (dlg->exportFormat->currentText() == "PEM") PEM = true;
  258. if (dlg->exportFormat->currentText() == "PKCS#8")
  259. targetKey->writePKCS8(fname, &MainWindow::passWrite);
  260. else if (dlg->exportPrivate->isChecked()) {
  261. if (dlg->encryptKey->isChecked())
  262. enc = EVP_des_ede3_cbc();
  263. targetKey->writeKey(fname, enc, &MainWindow::passWrite, PEM);
  264. }
  265. else {
  266. targetKey->writePublic(fname, PEM);
  267. }
  268. }
  269. catch (errorEx &err) {
  270. Error(err);
  271. }
  272. delete dlg;
  273. }
  274. void MainWindow::showPopupKey(QListViewItem *item, const QPoint &pt, int x) {
  275. CERR( " popup key" );
  276. QPopupMenu *menu = new QPopupMenu(this);
  277. if (!item) {
  278. menu->insertItem(tr("New Key"), this, SLOT(newKey()));
  279. menu->insertItem(tr("Import"), this, SLOT(loadKey()));
  280. }
  281. else {
  282. menu->insertItem(tr("Rename"), this, SLOT(startRenameKey()));
  283. menu->insertItem(tr("Show Details"), this, SLOT(showDetailsKey()));
  284. menu->insertItem(tr("Export"), this, SLOT(writeKey()));
  285. menu->insertItem(tr("Delete"), this, SLOT(deleteKey()));
  286. }
  287. menu->exec(pt);
  288. delete menu;
  289. return;
  290. }
  291. void MainWindow::renameKey(QListViewItem *item, int col, const QString &text)
  292. {
  293. try {
  294. pki_base *pki = keys->getSelectedPKI(item);
  295. string txt =  text.latin1();
  296. keys->renamePKI(pki, txt);
  297. }
  298. catch (errorEx &err) {
  299. Error(err);
  300. }
  301. }
  302. void MainWindow::startRenameKey()
  303. {
  304. try {
  305. #ifdef qt3
  306. pki_base *pki = keys->getSelectedPKI();
  307. if (!pki) return;
  308. QListViewItem *item = (QListViewItem *)pki->getPointer();
  309. item->startRename(0);
  310. #else
  311. renamePKI(keys);
  312. #endif
  313. }
  314. catch (errorEx &err) {
  315. Error(err);
  316. }
  317. }