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

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: MainWindowX509Req.cpp,v 1.50 2003/01/06 19:35:46 chris Exp $ 
  48.  *
  49.  */                           
  50. #include "MainWindow.h"
  51. void MainWindow::newReq(pki_temp *temp)
  52. {
  53. NewX509 *dlg = new NewX509(this, 0, keys, NULL, NULL, temps, csrImg, nsImg);
  54. if (temp) {
  55. dlg->defineTemplate(temp);
  56. }
  57. dlg->setRequest();
  58. //dlg->image->setPixmap(*csrImg);
  59. if (! dlg->exec()){
  60. delete dlg;
  61. return;
  62. }
  63. try {
  64. pki_key *key = (pki_key *)keys->getSelectedPKI(dlg->keyList->currentText().latin1());
  65. string cn = dlg->commonName->text().latin1();
  66. string c = dlg->countryName->text().latin1();
  67. string l = dlg->localityName->text().latin1();
  68. string st = dlg->stateOrProvinceName->text().latin1();
  69. string o = dlg->organisationName->text().latin1();
  70. string ou = dlg->organisationalUnitName->text().latin1();
  71. string email = dlg->emailAddress->text().latin1();
  72. string desc = dlg->description->text().latin1();
  73. pki_x509req *req = new pki_x509req(key, cn,c,l,st,o,ou,email,desc, "");
  74. insertReq(req);
  75. }
  76. catch (errorEx &err) {
  77. Error(err);
  78. }
  79. }
  80. void MainWindow::showDetailsReq()
  81. {
  82. pki_x509req *req = (pki_x509req *)reqs->getSelectedPKI();
  83. showDetailsReq(req);
  84. }
  85. void MainWindow::showDetailsReq(QListViewItem *item)
  86. {
  87. string req = item->text(0).latin1();
  88. showDetailsReq((pki_x509req *)reqs->getSelectedPKI(req));
  89. }
  90. void MainWindow::showDetailsReq(pki_x509req *req)
  91. {
  92. if (!req) return;
  93.     try {
  94. ReqDetail_UI *dlg = new ReqDetail_UI(this,0,true);
  95. MARK
  96. dlg->descr->setText(req->getDescription().c_str());
  97. MARK
  98. dlg->setCaption(tr(XCA_TITLE));
  99. MARK
  100. if (!req->verify() ) {
  101. MARK
  102.        dlg->verify->setDisabled(true);
  103. MARK
  104. dlg->verify->setText("FEHLER");
  105. }
  106. pki_key *key =req->getKey();
  107. if (key)
  108.     if(key->isPrivKey()) {
  109. dlg->privKey->setText(key->getDescription().c_str());
  110. dlg->privKey->setDisabled(false);
  111. }
  112. string land = req->getDN( NID_countryName) + " / " 
  113. + req->getDN(NID_stateOrProvinceName);
  114. dlg->dnCN->setText(req->getDN(NID_commonName).c_str() );
  115. dlg->dnC->setText(land.c_str());
  116. dlg->dnL->setText(req->getDN(NID_localityName).c_str());
  117. dlg->dnO->setText(req->getDN(NID_organizationName).c_str());
  118. dlg->dnOU->setText(req->getDN(NID_organizationalUnitName).c_str());
  119. dlg->dnEmail->setText(req->getDN(NID_pkcs9_emailAddress).c_str());
  120. dlg->image->setPixmap(*csrImg);
  121. if (dlg->exec()) {
  122. string ndesc = dlg->descr->text().latin1();
  123. if (ndesc != req->getDescription()) {
  124. reqs->renamePKI(req, ndesc);
  125. }
  126. }
  127. delete dlg;
  128.     }
  129.     catch (errorEx &err) {
  130.     Error(err);
  131.     }
  132. }
  133. void MainWindow::deleteReq()
  134. {
  135. pki_x509req *req;
  136. try {
  137. req = (pki_x509req *)reqs->getSelectedPKI();
  138. }
  139. catch (errorEx &err) {
  140. Error(err);
  141. return;
  142. }
  143. if (!req) return;
  144. if (QMessageBox::information(this,tr(XCA_TITLE),
  145. tr("Really want to delete the Certificate signing request") +":n'" + 
  146. QString::fromLatin1(req->getDescription().c_str()) +
  147. "'n", "Delete", "Cancel")
  148. ) return;
  149. try {
  150. pki_key *pkey = req->getKey();
  151. reqs->deletePKI(req);
  152. if (pkey) keys->updateViewPKI(pkey);
  153. }
  154. catch (errorEx &err) {
  155. Error(err);
  156. }
  157. }
  158. void MainWindow::loadReq()
  159. {
  160. QStringList filt;
  161. filt.append("PKCS#10 CSR ( *.pem *.der )"); 
  162. filt.append("All Files ( *.* )");
  163. QString s="";
  164. QStringList slist;
  165. QFileDialog *dlg = new QFileDialog(this,0,true);
  166. dlg->setCaption(tr("Import Certificate signing request"));
  167. dlg->setFilters(filt);
  168. dlg->setMode( QFileDialog::ExistingFiles );
  169. setPath(dlg);
  170. if (dlg->exec()) {
  171. slist = dlg->selectedFiles();
  172. newPath(dlg);
  173. }
  174. delete dlg;
  175. for ( QStringList::Iterator it = slist.begin(); it != slist.end(); ++it ) {
  176. s = *it;
  177. s = QDir::convertSeparators(s);
  178. try {
  179. pki_x509req *req = new pki_x509req(s.latin1());
  180. insertReq(req);
  181. MARK
  182. }
  183. catch (errorEx &err) {
  184. Error(err);
  185. }
  186. }
  187. }
  188. void MainWindow::writeReq()
  189. {
  190. pki_x509req *req;
  191. try {
  192. req = (pki_x509req *)reqs->getSelectedPKI();
  193. }
  194. catch (errorEx &err) {
  195. Error(err);
  196. return;
  197. }
  198. if (!req) return;
  199. QStringList filt;
  200. filt.append("PKCS#10 CSR ( *.pem *.der )"); 
  201. filt.append("All Files ( *.* )");
  202. QString s="";
  203. QFileDialog *dlg = new QFileDialog(this,0,true);
  204. dlg->setCaption(tr("Export Certificate signing request"));
  205. dlg->setFilters(filt);
  206. dlg->setMode( QFileDialog::AnyFile );
  207. dlg->setSelection( (req->getDescription() + ".pem").c_str() );
  208. setPath(dlg);
  209. if (dlg->exec()) {
  210. s = dlg->selectedFile();
  211. newPath(dlg);
  212. }
  213. delete dlg;
  214. if (s.isEmpty()) return;
  215. s=QDir::convertSeparators(s);
  216. try {
  217. req->writeReq(s.latin1(),true);
  218. }
  219. catch (errorEx &err) {
  220. Error(err);
  221. }
  222. }
  223. void MainWindow::signReq()
  224. {
  225. pki_x509req *req;
  226. try {
  227. req = (pki_x509req *)reqs->getSelectedPKI();
  228. }
  229. catch (errorEx &err) {
  230. Error(err);
  231. return;
  232. }
  233. newCert(req);
  234. }
  235. void MainWindow::insertReq(pki_x509req *req)
  236. {
  237. pki_x509 *oldreq;
  238. try {
  239. oldreq = (pki_x509 *)reqs->findPKI(req);
  240. MARK
  241. }
  242. catch (errorEx &err) {
  243. Error(err);
  244. }
  245. MARK
  246. if (oldreq) {
  247. MARK
  248.    QMessageBox::information(this,tr(XCA_TITLE),
  249. tr("The certificate signing request already exists in the database as") +":n'" +
  250. QString::fromLatin1(oldreq->getDescription().c_str()) + 
  251. "'n" + tr("and thus was not stored"), "OK");
  252.    delete(req);
  253.    return;
  254. }
  255. MARK
  256. try {
  257. reqs->findKey(req);
  258. MARK
  259. reqs->insertPKI(req);
  260. MARK
  261. if (req->getKey()) keys->updateViewPKI(req->getKey());
  262. }
  263. catch (errorEx &err) {
  264. Error(err);
  265. }
  266. }
  267. void MainWindow::showPopupReq(QListViewItem *item, const QPoint &pt, int x) {
  268. CERR("hallo popup Req");
  269. QPopupMenu *menu = new QPopupMenu(this);
  270. if (!item) {
  271. menu->insertItem(tr("New Request"), this, SLOT(newReq()));
  272. menu->insertItem(tr("Import"), this, SLOT(loadReq()));
  273. }
  274. else {
  275. menu->insertItem(tr("Rename"), this, SLOT(startRenameReq()));
  276. menu->insertItem(tr("Show Details"), this, SLOT(showDetailsReq()));
  277. menu->insertItem(tr("Sign"), this, SLOT(signReq()));
  278. menu->insertItem(tr("Export"), this, SLOT(writeReq()));
  279. menu->insertItem(tr("Delete"), this, SLOT(deleteReq()));
  280. }
  281. menu->exec(pt);
  282. delete menu;
  283. return;
  284. }
  285. void MainWindow::renameReq(QListViewItem *item, int col, const QString &text)
  286. {
  287. try {
  288. pki_base *pki = reqs->getSelectedPKI(item);
  289. string txt =  text.latin1();
  290. reqs->renamePKI(pki, txt);
  291. }
  292. catch (errorEx &err) {
  293. Error(err);
  294. }
  295. }
  296. void MainWindow::startRenameReq()
  297. {
  298. try {
  299. #ifdef qt3
  300. pki_base *pki = reqs->getSelectedPKI();
  301. if (!pki) return;
  302. QListViewItem *item = (QListViewItem *)pki->getPointer();
  303. item->startRename(0);
  304. #else
  305. renamePKI(reqs);
  306. #endif
  307. }
  308. catch (errorEx &err) {
  309. Error(err);
  310. }
  311. }