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

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: NewX509.cpp,v 1.35 2002/12/16 21:32:17 chris Exp $ 
  48.  *
  49.  */                           
  50. #include "NewX509.h"
  51. NewX509::NewX509(QWidget *parent , const char *name, db_key *key, db_x509req *req, db_x509 *cert, db_temp *temp, QPixmap *image, QPixmap *ns)
  52. :NewX509_UI(parent, name, true, 0)
  53. {
  54. connect( this, SIGNAL(genKey()), parent, SLOT(newKey()) );
  55. connect( parent, SIGNAL(keyDone(QString)), this, SLOT(newKeyDone(QString)) );
  56. setCaption(tr(XCA_TITLE));
  57. keys = key;
  58. reqs = req;
  59. temps = temp;
  60. certs = cert;
  61. fixtemp = NULL;
  62. pki_x509 *possibleSigner; 
  63. if (image) {
  64. bigImg1->setPixmap(*image);
  65. bigImg2->setPixmap(*image);
  66. bigImg3->setPixmap(*image);
  67. bigImg4->setPixmap(*image);
  68. bigImg5->setPixmap(*image);
  69. bigImg6->setPixmap(*image);
  70. nsImg->setPixmap(*ns);
  71. }
  72. #ifdef qt3
  73. // pretty fat Title :-)
  74. QFont tFont;// = getFont();
  75. tFont.setPointSize(14);
  76. tFont.setBold(true);
  77. tFont.setUnderline(true);
  78. setTitleFont( tFont );
  79. #else
  80. //setFont( tFont );
  81. #endif
  82. serialNr->setValidator( new QIntValidator(0, 32767, this));
  83. QStringList strings;
  84. // are there any useable private keys  ?
  85. if (keys) {
  86. strings = keys->get0PrivateDesc();
  87. if (strings.isEmpty()) {
  88. newKey();
  89. }
  90. else {
  91. keyList->insertStringList(strings);
  92. }
  93. }
  94. else {
  95. keyList->setEnabled(false);
  96. genKeyBUT->setEnabled(false);
  97. }
  98. // any PKCS#10 requests to be used ?
  99. if (reqs) {
  100. strings = reqs->getDesc();
  101. if (strings.isEmpty()) {
  102. fromReqCB->setDisabled(true);
  103. }
  104. else {
  105. reqList->insertStringList(strings);
  106. }
  107. }
  108. else {
  109. reqList->setEnabled(false);
  110. fromReqCB->setEnabled(false);
  111. }
  112. // How about signing certificates ?
  113. if (certs) {
  114. strings = certs->getSignerDesc();
  115. if (strings.isEmpty()) {
  116. foreignSignRB->setDisabled(true);
  117. certList->setDisabled(true);
  118. }
  119. else {
  120. certList->insertStringList(strings);
  121. // suggested from:  Andrey Brindeew <abr@abr.pp.ru>
  122. possibleSigner=(pki_x509 *)certs->getSelectedPKI();
  123. if (possibleSigner && possibleSigner->canSign()) {
  124. QString name = possibleSigner->getDescription().c_str();
  125. #ifdef qt3
  126. certList->setCurrentText(name);
  127. #else
  128. for (int i=0; i<certList->count();i++) {
  129. if (certList->text(i) == name) {
  130. certList->setCurrentItem(i);
  131. break;
  132. }
  133. }
  134. #endif
  135. foreignSignRB->setChecked(true);
  136. // certList->setEnabled(true);
  137. }
  138. }
  139. }
  140. else {
  141. foreignSignRB->setDisabled(true);
  142. certList->setDisabled(true);
  143. }
  144. // settings for the templates ....
  145. if (temps) {
  146. strings = temps->getDesc();
  147. strings.prepend(tr("Server Template"));
  148. strings.prepend(tr("Client Template"));
  149. strings.prepend(tr("CA Template"));
  150. strings.prepend(tr("Empty Template"));
  151. tempList->insertStringList(strings);
  152. }
  153. else {
  154. templateBox->setEnabled(false);
  155. }
  156. setFinishEnabled(page7,true);
  157. setNextEnabled(page2,false);
  158. }
  159. void NewX509::setRequest()
  160. {
  161. setAppropriate(page4, false);
  162. setAppropriate(page5, false);
  163. setAppropriate(page6, false);
  164. finishButton()->setEnabled(true);
  165. changeDefault->setEnabled(false);
  166. changeDefault->setChecked(false);
  167. signerBox->setEnabled(false);
  168. startText=tr("Welcome to the settings for Certificate signing requests.... (needs more prosa, volunteers ?)");
  169. endText=tr("You are done with entering all parameters for generating a Certificate signing request..... (needs more prosa, volunteers ?)");
  170. tText=tr("Certificate request");
  171. setup();
  172. }
  173. NewX509::~NewX509()
  174. {
  175. }
  176. void NewX509::setTemp(pki_temp *temp)
  177. {
  178. setAppropriate(page1, false);
  179. finishButton()->setEnabled(true);
  180. startText=tr("Welcome to the settings for Templates.... (needs more prosa)");
  181. endText=tr("You are done with entering all parameters for generating a Template..... (needs more prosa, volunteers ?)");
  182. tText=tr("Template");
  183. if (temp->getDescription() != "--") {
  184. description->setText(temp->getDescription().c_str());
  185. tText += tr(" change");
  186. }
  187. setup();
  188. }
  189. void NewX509::setCert()
  190. {
  191. finishButton()->setEnabled(true);
  192. startText=tr("Welcome to the settings for Certificates.... (needs more prosa)");
  193. endText=tr("You are done with entering all parameters for generating a Certificate..... (needs more prosa, volunteers ?)");
  194. tText=tr("Certificate");
  195. setup();
  196. }
  197. void NewX509::setup()
  198. {
  199. startLabel->setText(startText);
  200. endLabel->setText(endText);
  201. setTitle(page0, tText + " Wizard");
  202. setTitle(page1, tText + " template selection");
  203. setTitle(page2, tText + " personal settings");
  204. setTitle(page4, tText + " X.509 v3 Extensions");
  205. setTitle(page5, tText + " key usage setup");
  206. setTitle(page6, tText + " Netscape extensions");
  207. setTitle(page7, tText + " Wizard finished");
  208. }
  209. void NewX509::defineTemplate(pki_temp *temp)
  210. {
  211. fromTemplate(temp);
  212. templateChanged(temp);
  213. tempList->setEnabled(false);
  214. }
  215. void NewX509::defineRequest(pki_x509req *req)
  216. {
  217. if (!req) return;
  218. fromReqCB->setEnabled(true);
  219. fromReqCB->setChecked(true);
  220. QString reqname = req->getDescription().c_str(); 
  221. #ifdef qt3
  222. reqList->setCurrentText(reqname);
  223. #else
  224. for (int i=0; i<reqList->count(); i++) {
  225. if (reqList->text(i) == reqname) {
  226. reqList->setCurrentItem(i);
  227. break;
  228. }
  229. }
  230. #endif
  231. }
  232. int NewX509::lb2int(QListBox *lb)
  233. {
  234. int x=0;
  235. for (int i=0; lb->item(i); i++) {
  236. if (lb->isSelected(i)){
  237. x |= 1<<i;
  238. }
  239. }
  240. return x;
  241. }
  242. void NewX509::int2lb(QListBox *lb, int x)
  243. {
  244. for (int i=0; lb->item(i); i++) {
  245. lb->setSelected(i, (1<<i) & x);
  246. }
  247. }
  248. void NewX509::fromTemplate(pki_temp *temp)
  249. {
  250. countryName->setText(temp->C.c_str());
  251. stateOrProvinceName->setText(temp->P.c_str());
  252. localityName->setText(temp->L.c_str());
  253. organisationName->setText(temp->O.c_str());
  254. organisationalUnitName->setText(temp->OU.c_str());
  255. commonName->setText(temp->CN.c_str());
  256. emailAddress->setText(temp->EMAIL.c_str());
  257. subAltName->setText(temp->subAltName.c_str());
  258. issAltName->setText(temp->issAltName.c_str());
  259. crlDist->setText(temp->crlDist.c_str());
  260. nsComment->setText(temp->nsComment.c_str());
  261. nsBaseUrl->setText(temp->nsBaseUrl.c_str());
  262. nsRevocationUrl->setText(temp->nsRevocationUrl.c_str());
  263. nsCARevocationUrl->setText(temp->nsCARevocationUrl.c_str());
  264. nsRenewalUrl->setText(temp->nsRenewalUrl.c_str());
  265. nsCaPolicyUrl->setText(temp->nsCaPolicyUrl.c_str());
  266. nsSslServerName->setText(temp->nsSslServerName.c_str());
  267. int2lb(nsCertType, temp->nsCertType);
  268. basicCA->setCurrentItem(temp->ca?1:0);
  269. bcCritical->setChecked(temp->bcCrit);
  270. kuCritical->setChecked(temp->keyUseCrit);
  271. ekuCritical->setChecked(temp->eKeyUseCrit);
  272. subKey->setChecked(temp->subKey);
  273. authKey->setChecked(temp->authKey);
  274. subAltCp->setChecked(temp->subAltCp);
  275. issAltCp->setChecked(temp->issAltCp);
  276. int2lb(keyUsage, temp->keyUse);
  277. int2lb(ekeyUsage, temp->eKeyUse);
  278. validNumber->setText(QString::number(temp->validN));
  279. validRange->setCurrentItem(temp->validM);
  280. if (temp->pathLen) {
  281. basicPath->setText(QString::number(temp->pathLen));
  282. }
  283. }
  284. void NewX509::toTemplate(pki_temp *temp)
  285. {
  286. temp->setDescription(description->text().latin1());
  287. temp->C = countryName->text().latin1();
  288. temp->P = stateOrProvinceName->text().latin1();
  289. temp->L = localityName->text().latin1();
  290. temp->O = organisationName->text().latin1();
  291. temp->OU = organisationalUnitName->text().latin1();
  292. temp->CN = commonName->text().latin1();
  293. temp->EMAIL = emailAddress->text().latin1();
  294. temp->subAltName = subAltName->text().latin1();
  295. temp->issAltName = issAltName->text().latin1();
  296. temp->crlDist = crlDist->text().latin1();
  297. temp->nsComment = nsComment->text().latin1();
  298. temp->nsBaseUrl = nsBaseUrl->text().latin1();
  299. temp->nsRevocationUrl = nsRevocationUrl->text().latin1();
  300. temp->nsCARevocationUrl = nsCARevocationUrl->text().latin1();
  301. temp->nsRenewalUrl = nsRenewalUrl->text().latin1();
  302. temp->nsCaPolicyUrl = nsCaPolicyUrl->text().latin1();
  303. temp->nsSslServerName = nsSslServerName->text().latin1();
  304. temp->nsCertType =  lb2int(nsCertType);
  305. temp->ca = basicCA->currentItem();
  306. temp->bcCrit = bcCritical->isChecked();
  307. temp->keyUseCrit = kuCritical->isChecked();
  308. temp->eKeyUseCrit = ekuCritical->isChecked();
  309. temp->subKey = subKey->isChecked();
  310. temp->authKey = authKey->isChecked();
  311. temp->subAltCp = subAltCp->isChecked();
  312. temp->issAltCp = issAltCp->isChecked();
  313. temp->keyUse = lb2int(keyUsage);
  314. temp->eKeyUse = lb2int(ekeyUsage);
  315. temp->validN = validNumber->text().toInt();
  316. temp->validM = validRange->currentItem();
  317. temp->pathLen = basicPath->text().toInt();
  318. }
  319. void NewX509::toggleFromRequest()
  320. {
  321. if (fromReqCB->isChecked()) {
  322. setAppropriate(page2, false);
  323. reqList->setEnabled(true);
  324. }
  325. else {
  326. setAppropriate(page2, true);
  327. reqList->setEnabled(false);
  328. }
  329. }
  330. void NewX509::dataChangeP2()
  331. {
  332. CERR( "Data changed" );
  333. if (description->text() != ""  && countryName->text().length() !=1 &&
  334.     (keyList->count() > 0  || !keyList->isEnabled())){
  335. setNextEnabled(page2,true);
  336. }
  337. else {
  338. setNextEnabled(page2,false);
  339. }
  340. }
  341. void NewX509::showPage(QWidget *page)
  342. {
  343. if (page == page0) {
  344. signerChanged();
  345. switchExtended();
  346. toggleFromRequest();
  347. }
  348. else if ( page == page2 ) {
  349. dataChangeP2();
  350. }
  351. QWizard::showPage(page);
  352. if ( page == page2 ) {
  353. description->setFocus();
  354. }
  355. else if (page == page4) {
  356. basicCA->setFocus();
  357. }
  358. }
  359. void NewX509::signerChanged()
  360. {
  361. CERR("signer Changed");
  362. if (!certs) return;
  363. QString name = certList->currentText();
  364. CERR( "Certificate: " << name.latin1());
  365. if (name.isEmpty()) return;
  366. pki_x509 *cert = (pki_x509 *)certs->getSelectedPKI(name.latin1());
  367. if (!cert) return;
  368. QString templ = cert->getTemplate().c_str();
  369. if (templ.isEmpty()) return;
  370. CERR( "set Template: " << templ );
  371. templateChanged(templ);
  372. }
  373. void NewX509::templateChanged(QString tempname)
  374. {
  375. if (!tempList->isEnabled()) return;
  376. #ifdef qt3
  377. tempList->setCurrentText(tempname);
  378. #else
  379. for (int i=0; i<tempList->count();i++) {
  380. if (tempList->text(i) == tempname) {
  381. tempList->setCurrentItem(i);
  382. break;
  383. }
  384. }
  385. #endif
  386. }
  387. void NewX509::templateChanged(pki_temp *templ)
  388. {
  389. QString tempname = templ->getDescription().c_str();
  390. templateChanged(tempname);
  391. }
  392. void NewX509::templateChanged()
  393. {
  394. pki_temp *temp = NULL;
  395. int item;
  396. if (!appropriate(page1)) return;
  397. if (!tempList->isEnabled()) return;
  398. if ((item = tempList->currentItem())<4) {
  399. temp = new pki_temp("temp",item);
  400. if (temp) { 
  401. fromTemplate(temp);
  402. CERR("using default template: "<< item);
  403. delete (temp);
  404. }
  405. return;
  406. }
  407. QString name = tempList->currentText();
  408. if (name == "" || !temps) return;
  409. temp = (pki_temp *)temps->getSelectedPKI(name.latin1());
  410. if (!temp) return;
  411. CERR("CHANGING TEMPLATE");
  412. fromTemplate(temp);
  413. }
  414. void NewX509::switchExtended()
  415. {
  416. if ( !appropriate(page1) ) return;
  417. CERR( "SWITCH Extended");
  418. if (changeDefault->isChecked() || !templateBox->isEnabled()) {
  419. setAppropriate(page4, true);
  420. setAppropriate(page5, true);
  421. setAppropriate(page6, true);
  422. }
  423. else {
  424. setAppropriate(page4, false);
  425. setAppropriate(page5, false);
  426. setAppropriate(page6, false);
  427. }
  428. }
  429.     
  430. void NewX509::newKey()
  431. {
  432. emit genKey();
  433. }
  434. void NewX509::newKeyDone(QString name)
  435. {
  436. keyList->insertItem(name,0);
  437. keyList->setCurrentItem(0);
  438. dataChangeP2();
  439. }