mainwindow.cpp

来自「一个精简的CA程序」· C++ 代码 · 共 614 行 · 第 1/2 页

CPP
614
字号
		dbenv->txn_begin(NULL, &global_tid, 0);#ifndef DB_AUTO_COMMIT#define DB_AUTO_COMMIT 0#endif		dbenv->set_flags(DB_AUTO_COMMIT,1);	}	catch (DbException &err) {		QString e = err.what();		e += QString::fromLatin1(" (") + baseDir + QString::fromLatin1(")");		qFatal(e);	}	try {		settings = new db_base(dbenv, dbfile, "settings",global_tid, NULL);		initPass();		keys = new db_key(dbenv, dbfile, global_tid, keyList);		reqs = new db_x509req(dbenv, dbfile, keys, global_tid, reqList);		certs = new db_x509(dbenv, dbfile, keys, global_tid, certList);		temps = new db_temp(dbenv, dbfile, global_tid, tempList);		crls = new db_crl(dbenv, dbfile, global_tid, crlList);		reqs->setKeyDb(keys);		certs->setKeyDb(keys);			keyList->setDB(keys);		reqList->setDB(reqs);		certList->setDB(certs);		tempList->setDB(temps);		crlList->setDB(crls);	}	catch (errorEx &err) {		Error(err);	}	catch (DbException &err) {		qFatal(err.what());	}	connect( keys, SIGNAL(newKey(pki_key *)),		certs, SLOT(newKey(pki_key *)) );	connect( keys, SIGNAL(delKey(pki_key *)),		certs, SLOT(delKey(pki_key *)) );	connect( keys, SIGNAL(newKey(pki_key *)),		reqs, SLOT(newKey(pki_key *)) );	connect( keys, SIGNAL(delKey(pki_key *)),		reqs, SLOT(delKey(pki_key *)) );	}		MainWindow::~MainWindow() {	ERR_free_strings();	EVP_cleanup();	if (dbenv) {		delete(crls);		delete(reqs);		delete(certs);		delete(temps);		delete(keys);		delete(settings);		global_tid->commit(0);		dbenv->close(0);	}}void MainWindow::initPass(){	pass_info p(tr("New Password"), 	  tr("Please enter a password, that will be used to encrypt your private keys in the database-file"));	QString passHash = settings->getString("pwhash");	if (passHash.isEmpty()) {		int keylen = passWrite((char *)pki_key::passwd, 25, 0, &p);		if (keylen == 0) {			qFatal("Ohne Passwort laeuft hier gaaarnix :-)");		}		pki_key::passwd[keylen]='\0';		settings->putString( "pwhash", md5passwd(pki_key::passwd) );	}	else {	     int keylen=0;			     while (md5passwd(pki_key::passwd) != passHash) {		if (keylen !=0)			QMessageBox::warning(this,tr(XCA_TITLE), tr("Password verify error, please try again"));			p.setTitle(tr("Password"));		p.setDescription(tr("Please enter the password for unlocking the database"));		keylen = passRead(pki_key::passwd, 25, 0, &p);		if (keylen == 0) {			qFatal("Ohne Passwort laeuft hier gaaarnix :-)");		}		pki_key::passwd[keylen]='\0';	    }	}}// Static Password Callback functions int MainWindow::passRead(char *buf, int size, int rwflag, void *userdata){	pass_info *p = (pass_info *)userdata;	PassRead_UI *dlg = new PassRead_UI(NULL, 0, true);	if (p != NULL) {		dlg->image->setPixmap( *keyImg );		dlg->title->setText(tr(p->getTitle()));		dlg->description->setText(tr(p->getDescription()));	}	dlg->pass->setFocus();	dlg->setCaption(tr(XCA_TITLE));	if (dlg->exec()) {	   QString x = dlg->pass->text();	   strncpy(buf, x.latin1(), size);	   return x.length();	}	else return 0;}int MainWindow::passWrite(char *buf, int size, int rwflag, void *userdata){	pass_info *p = (pass_info *)userdata;	PassWrite_UI *dlg = new PassWrite_UI(NULL, 0, true);	if (p != NULL) {		dlg->image->setPixmap( *keyImg );		dlg->title->setText(tr(p->getTitle()));		dlg->description->setText(tr(p->getDescription()));	}	dlg->passA->setFocus();	dlg->setCaption(tr(XCA_TITLE));	if (dlg->exec()) {	   QString A = dlg->passA->text();	   QString B = dlg->passB->text();	   if (A != B) return 0;	   strncpy(buf, A.latin1(), size);	   return A.length();	}	else return 0;}QString MainWindow::md5passwd(const char *pass){	EVP_MD_CTX mdctx;	QString str;	unsigned int n;	int j;	char zs[4];	unsigned char m[EVP_MAX_MD_SIZE];	EVP_DigestInit(&mdctx, EVP_md5());	EVP_DigestUpdate(&mdctx, pass, strlen(pass));	EVP_DigestFinal(&mdctx, m, &n);	for (j=0; j<(int)n; j++) {		sprintf(zs, "%02X%c",m[j], (j+1 == (int)n) ?'\0':':');		str += zs;	}	return str;}	void MainWindow::Error(errorEx &err){	if (err.isEmpty()) return;	QString msg =  tr("The following error occured:") + "\n" + err.getString();	int ret = QMessageBox::warning(NULL, XCA_TITLE, msg, tr("&OK"), tr("Copy to Clipboard"));	if (ret == 1) {		QClipboard *cb = QApplication::clipboard();		cb->setText(msg);	}}void MainWindow::dberr(const char *errpfx, char *msg){	errorEx e(QString(errpfx) + "\n" + msg);	Error(e);}QString MainWindow::getPath(){	QString x = settings->getString("workingdir");	return x;}void MainWindow::setPath(QString str){	settings->putString("workingdir", str);}NewX509 *MainWindow::newX509(){	return new NewX509(NULL, 0, true);}void MainWindow::connNewX509(NewX509 *nx){	connect( (const QObject *)nx->genKeyBUT, SIGNAL(clicked()), keyList, SLOT(newItem()) );	connect( nx, SIGNAL(genKey()), keyList, SLOT(newItem()) );	connect( keyList, SIGNAL(keyDone(QString)), nx, SLOT(newKeyDone(QString)) );}void MainWindow::changeView(){	certList->changeView(BNviewState);}QString MainWindow::getBaseDir(){#ifdef WIN32	unsigned char reg_path_buf[255] = "";	TCHAR data_path_buf[255];	// verification registry keys	LONG lRc;    HKEY hKey;	DWORD dwDisposition;	DWORD dwLength = 255;    lRc=RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\xca",0,KEY_READ, &hKey);    if(lRc!= ERROR_SUCCESS){		QMessageBox::warning(NULL, XCA_TITLE,			"Registry Key: 'HKEY_LOCAL_MACHINE->Software->xca' not found. ReInstall Xca.");		qFatal("");	}    else {		lRc=RegQueryValueEx(hKey,"Install_Dir",NULL,NULL, reg_path_buf, &dwLength);        if(lRc!= ERROR_SUCCESS){			QMessageBox::warning(NULL, XCA_TITLE,				"Registry Key: 'HKEY_LOCAL_MACHINE->Software->xca->Install_Dir' not found. ReInstall Xca.");					qFatal("");		}		lRc=RegCloseKey(hKey);	}	lRc=RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\xca",0,KEY_ALL_ACCESS, &hKey);    if(lRc!= ERROR_SUCCESS)    {		//First run for current user		lRc=RegCloseKey(hKey);		lRc=RegCreateKeyEx(HKEY_CURRENT_USER,"Software\\xca",0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,		NULL,&hKey, &dwDisposition);				//setup data dir for current user		OSVERSIONINFOEX osvi;		BOOL bOsVersionInfoEx;		LPITEMIDLIST pidl=NULL; 		ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));		osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);		if(!(bOsVersionInfoEx=GetVersionEx((OSVERSIONINFO*)&osvi))){			osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);			if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) return FALSE;		}		if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT){			if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDL_APPDATA,&pidl))){				SHGetPathFromIDList(pidl,data_path_buf);				lstrcat(data_path_buf, "\\xca");	 			}		}else{			strncpy(data_path_buf,(char *)reg_path_buf,255);			strcat(data_path_buf,"\\data");		}		baseDir = QString::fromLocal8Bit(data_path_buf);		// save in registry		lRc=RegSetValueEx(hKey,"data_path",0,REG_SZ,(BYTE*)data_path_buf, 255);		lRc=RegCloseKey(hKey);		QMessageBox::warning(this,tr(XCA_TITLE), QString::fromLatin1("New data dir create:")+ baseDir);		QMessageBox::warning(this,tr(XCA_TITLE), QString::fromLatin1("WARNING: If you have updated your 'xca' application \n you have to copy your 'xca.db' from 'C:\\PROGAM FILES\\XCA\\' to ") + baseDir + QString::fromLatin1(" \n or change HKEY_CURRENT_USER->Software->xca->data_path key"));        }	else{		dwLength = sizeof(data_path_buf);		lRc=RegQueryValueEx(hKey,"data_path",NULL,NULL, (BYTE*)data_path_buf, &dwLength);		if ((lRc != ERROR_SUCCESS)) {			QMessageBox::warning(NULL,tr(XCA_TITLE), "Registry Key: 'HKEY_CURRENT_USER->Software->xca->data_path' not found.");			//recreate data dir for current user			OSVERSIONINFOEX osvi;			BOOL bOsVersionInfoEx;			LPITEMIDLIST pidl=NULL; 			ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));			osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);			if(!(bOsVersionInfoEx=GetVersionEx((OSVERSIONINFO*)&osvi))){				osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);				if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) return FALSE;			}			if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT){				if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDL_APPDATA,&pidl))){					SHGetPathFromIDList(pidl,data_path_buf);					lstrcat(data_path_buf, "\\xca");	  				}			}else{				strncpy(data_path_buf,(char *)reg_path_buf,255);				strcat(data_path_buf,"\\data");			}			baseDir = QString::fromLocal8Bit(data_path_buf);			// save in registry			lRc=RegSetValueEx(hKey,"data_path",0,REG_SZ,(BYTE*)data_path_buf, 255);			lRc=RegCloseKey(hKey);			QMessageBox::warning(this,tr(XCA_TITLE), QString::fromLatin1("data dir:")+ baseDir);		}		lRc=RegCloseKey(hKey);		baseDir = QString::fromLocal8Bit(data_path_buf);	}// #else		baseDir = QDir::homeDirPath();	baseDir += QDir::separator();	baseDir += "xca";#endif	return baseDir;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?