mirror of
https://github.com/chris2511/xca.git
synced 2026-09-12 19:51:05 +05:00
Improve transactions, fix CA template and CRLdays import
Make ItemCombo a template class
This commit is contained in:
parent
d36e6eb0cb
commit
e6c92ce890
@ -219,15 +219,15 @@ void db_crl::newItem()
|
||||
ca = cas[0];
|
||||
break;
|
||||
default: {
|
||||
itemCombo *c = new itemCombo(NULL);
|
||||
itemComboCert *c = new itemComboCert(NULL);
|
||||
XcaDialog *d = new XcaDialog(mainwin, revocation, c,
|
||||
tr("Select CA certificate"), QString());
|
||||
c->insertPkiItems<pki_x509>(cas);
|
||||
c->insertPkiItems(cas);
|
||||
if (!d->exec()) {
|
||||
delete d;
|
||||
return;
|
||||
}
|
||||
ca = c->currentPkiItem<pki_x509>();
|
||||
ca = c->currentPkiItem();
|
||||
delete d;
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,12 +94,12 @@ void db_temp::newItem()
|
||||
pki_temp *temp = NULL;
|
||||
QString type;
|
||||
|
||||
itemCombo *ic = new itemCombo(NULL);
|
||||
ic->insertPkiItems<pki_temp>(predefs);
|
||||
itemComboTemp *ic = new itemComboTemp(NULL);
|
||||
ic->insertPkiItems(predefs);
|
||||
XcaDialog *dlg = new XcaDialog(mainwin, tmpl, ic,
|
||||
tr("Preset Template values"), QString());
|
||||
if (dlg->exec()) {
|
||||
temp = new pki_temp(ic->currentPkiItem<pki_temp>());
|
||||
temp = new pki_temp(ic->currentPkiItem());
|
||||
temp->pkiSource = generated;
|
||||
if (temp) {
|
||||
if (runTempDlg(temp)) {
|
||||
|
||||
@ -1029,7 +1029,7 @@ void db_x509::caProperties(QModelIndex idx)
|
||||
QVariant tmplId = cert->getTemplateSqlId();
|
||||
pki_temp *templ = mainwin->temps->lookupPki<pki_temp>(tmplId);
|
||||
|
||||
ui.temp->insertPkiItems<pki_temp>(mainwin->temps->getAll<pki_temp>());
|
||||
ui.temp->insertPkiItems(mainwin->temps->getAll<pki_temp>());
|
||||
ui.temp->setNullItem(tr("No template"));
|
||||
ui.temp->setCurrentIndex(0);
|
||||
if (templ)
|
||||
@ -1067,7 +1067,7 @@ void db_x509::caProperties(QModelIndex idx)
|
||||
int rows = ui.subjectManager->rowCount();
|
||||
XSqlQuery q;
|
||||
QSqlError e;
|
||||
templ = ui.temp->currentPkiItem<pki_temp>();
|
||||
templ = ui.temp->currentPkiItem();
|
||||
tmplId = templ ? templ->getSqlItemId() : QVariant();
|
||||
|
||||
sl.clear();
|
||||
|
||||
@ -105,10 +105,12 @@ QSqlError pki_x509::insertSqlData()
|
||||
if (!isCA())
|
||||
return q.lastError();
|
||||
|
||||
SQL_PREPARE(q, "INSERT INTO authority (item, crlExpire, crlNo) "
|
||||
"VALUES (?, ?, 0)");
|
||||
SQL_PREPARE(q, "INSERT INTO authority (item, template, crlExpire, crlNo, crlDays) "
|
||||
"VALUES (?, ?, ?, 0, ?)");
|
||||
q.bindValue(0, sqlItemId);
|
||||
q.bindValue(1, now.toPlain());
|
||||
q.bindValue(1, caTemplateSqlId);
|
||||
q.bindValue(2, crlExpire.toPlain());
|
||||
q.bindValue(3, crlDays);
|
||||
q.exec();
|
||||
if (fromDataRevList.size() > 0)
|
||||
fromDataRevList.sqlUpdate(sqlItemId);
|
||||
@ -606,7 +608,7 @@ void pki_x509::fromData(const unsigned char *p, db_header_t *head)
|
||||
}
|
||||
pki_openssl_error();
|
||||
/* Superflous CaSerial = */db::stringFromData(ba);
|
||||
QString __caTemplate = db::stringFromData(ba);
|
||||
QString caTemplate = db::stringFromData(ba);
|
||||
crlDays = db::intFromData(ba);
|
||||
crlExpire.d2i(ba);
|
||||
pki_openssl_error();
|
||||
@ -635,8 +637,17 @@ void pki_x509::fromData(const unsigned char *p, db_header_t *head)
|
||||
my_error(tr("Wrong Size %1").arg(ba.count()));
|
||||
}
|
||||
pki_openssl_error();
|
||||
|
||||
XSqlQuery q;
|
||||
SQL_PREPARE(q, "SELECT id FROM items WHERE name=? AND type=?");
|
||||
q.bindValue(0, caTemplate);
|
||||
q.bindValue(1, tmpl);
|
||||
q.exec();
|
||||
if (q.next())
|
||||
caTemplateSqlId = q.value(0);
|
||||
}
|
||||
|
||||
|
||||
void pki_x509::writeDefault(const QString fname)
|
||||
{
|
||||
writeCert(fname + QDir::separator() + getIntName() + ".crt",
|
||||
|
||||
40
lib/sql.cpp
40
lib/sql.cpp
@ -34,44 +34,38 @@ DbTransaction::~DbTransaction()
|
||||
|
||||
bool DbTransaction::begin(const char *file, int line)
|
||||
{
|
||||
QSqlDatabase db = QSqlDatabase::database();
|
||||
if (db.transaction()) {
|
||||
has_begun = true;
|
||||
if (mutex++ == 0)
|
||||
error = 0;
|
||||
debug("Begin", file, line);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
mutex++;
|
||||
has_begun = true;
|
||||
debug("Begin", file, line);
|
||||
return mutex > 1 ? true : QSqlDatabase::database().transaction();
|
||||
}
|
||||
|
||||
bool DbTransaction::commit(const char *file, int line)
|
||||
bool DbTransaction::finish(const char *oper, const char *file, int line)
|
||||
{
|
||||
if (mutex > 0)
|
||||
mutex--;
|
||||
else
|
||||
qCritical() << "Unbalanced DB Transaction (commit)";
|
||||
debug("Commit", file, line);
|
||||
qCritical() << "Unbalanced DB Transaction in " << oper;
|
||||
debug(oper, file, line);
|
||||
has_begun = false;
|
||||
if (mutex > 0)
|
||||
return true;
|
||||
|
||||
QSqlDatabase db = QSqlDatabase::database();
|
||||
return error ? db.rollback() : db.commit();
|
||||
int e = error;
|
||||
error = 0;
|
||||
return e ? db.rollback() : db.commit();
|
||||
}
|
||||
|
||||
bool DbTransaction::commit(const char *file, int line)
|
||||
{
|
||||
return finish("Commit", file, line);
|
||||
}
|
||||
|
||||
bool DbTransaction::rollback(const char *file, int line)
|
||||
{
|
||||
error++;
|
||||
if (mutex > 0)
|
||||
mutex--;
|
||||
else
|
||||
qCritical() << "Unbalanced DB Transaction (rollback)";
|
||||
debug("Rollback", file, line);
|
||||
has_begun = false;
|
||||
if (mutex > 0)
|
||||
return true;
|
||||
QSqlDatabase db = QSqlDatabase::database();
|
||||
return db.rollback();
|
||||
return finish("Rollback", file, line);
|
||||
}
|
||||
|
||||
bool DbTransaction::done(QSqlError e, const char *file, int line)
|
||||
|
||||
@ -22,6 +22,7 @@ class DbTransaction
|
||||
static int error;
|
||||
bool has_begun;
|
||||
void debug(const char *func, const char *file, int line);
|
||||
bool finish(const char *oper, const char *file, int line);
|
||||
|
||||
public:
|
||||
DbTransaction();
|
||||
|
||||
@ -147,7 +147,7 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="itemCombo" name="temp"/>
|
||||
<widget class="itemComboTemp" name="temp"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
@ -187,7 +187,7 @@
|
||||
</slots>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>itemCombo</class>
|
||||
<class>itemComboTemp</class>
|
||||
<extends>QComboBox</extends>
|
||||
<header>widgets/ItemCombo.h</header>
|
||||
</customwidget>
|
||||
|
||||
@ -104,7 +104,7 @@
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="0" column="1">
|
||||
<widget class="itemCombo" name="reqList"/>
|
||||
<widget class="itemComboReq" name="reqList"/>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QPushButton" name="showReqBut">
|
||||
@ -198,7 +198,7 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="itemCombo" name="certList">
|
||||
<widget class="itemComboCert" name="certList">
|
||||
<property name="toolTip">
|
||||
<string>All certificates in your database that can create valid signatures</string>
|
||||
</property>
|
||||
@ -259,7 +259,7 @@
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||
<item>
|
||||
<widget class="itemCombo" name="tempList">
|
||||
<widget class="itemComboTemp" name="tempList">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
@ -425,7 +425,7 @@
|
||||
</property>
|
||||
<layout class="QHBoxLayout">
|
||||
<item>
|
||||
<widget class="itemCombo" name="keyList">
|
||||
<widget class="itemComboKey" name="keyList">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
@ -1224,7 +1224,22 @@
|
||||
<header>widgets/clicklabel.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>itemCombo</class>
|
||||
<class>itemComboReq</class>
|
||||
<extends>QComboBox</extends>
|
||||
<header>widgets/ItemCombo.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>itemComboKey</class>
|
||||
<extends>QComboBox</extends>
|
||||
<header>widgets/ItemCombo.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>itemComboCert</class>
|
||||
<extends>QComboBox</extends>
|
||||
<header>widgets/ItemCombo.h</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>itemComboTemp</class>
|
||||
<extends>QComboBox</extends>
|
||||
<header>widgets/ItemCombo.h</header>
|
||||
</customwidget>
|
||||
|
||||
@ -12,31 +12,43 @@
|
||||
#include <QComboBox>
|
||||
|
||||
#include "lib/pki_base.h"
|
||||
#include "lib/db_base.h"
|
||||
#include "lib/pki_x509.h"
|
||||
#include "lib/pki_x509req.h"
|
||||
#include "lib/pki_temp.h"
|
||||
//#include "lib/db_base.h"
|
||||
|
||||
template <class T>
|
||||
class itemCombo : public QComboBox
|
||||
{
|
||||
public:
|
||||
itemCombo(QWidget *parent) : QComboBox(parent) { }
|
||||
template <class T> void insertPkiItems(QList<T*> items) {
|
||||
void insertPkiItems(QList<T*> items) {
|
||||
clear();
|
||||
foreach(T *p, items) {
|
||||
addItem(p->comboText(), QVariant::fromValue(p));
|
||||
}
|
||||
}
|
||||
template <class T> T *currentPkiItem() {
|
||||
return itemData(currentIndex()).value<T*>();
|
||||
T *currentPkiItem() {
|
||||
return itemData(currentIndex()).template value<T*>();
|
||||
}
|
||||
void setNullItem(QString text) {
|
||||
if (itemData(0).value<pki_base*>() == NULL)
|
||||
if (itemData(0).template value<T*>() == NULL)
|
||||
removeItem(0);
|
||||
insertItem(0, text, QVariant());
|
||||
}
|
||||
int setCurrentPkiItem(pki_base *p) {
|
||||
int setCurrentPkiItem(T *p) {
|
||||
int idx = findData(QVariant::fromValue(p));
|
||||
setCurrentIndex(idx);
|
||||
return idx;
|
||||
}
|
||||
};
|
||||
|
||||
//class pki_temp;
|
||||
//class pki_x509req;
|
||||
//class pki_x509;
|
||||
//class pki_key;
|
||||
typedef class itemCombo<pki_temp> itemComboTemp;
|
||||
typedef class itemCombo<pki_x509req> itemComboReq;
|
||||
typedef class itemCombo<pki_x509> itemComboCert;
|
||||
typedef class itemCombo<pki_key> itemComboKey;
|
||||
#endif
|
||||
|
||||
@ -81,7 +81,7 @@ NewX509::NewX509(QWidget *parent)
|
||||
fromReqCB->setChecked(false);
|
||||
}
|
||||
else {
|
||||
reqList->insertPkiItems<pki_x509req>(requests);
|
||||
reqList->insertPkiItems(requests);
|
||||
}
|
||||
on_fromReqCB_clicked();
|
||||
|
||||
@ -90,7 +90,7 @@ NewX509::NewX509(QWidget *parent)
|
||||
if (issuers.isEmpty()) {
|
||||
foreignSignRB->setDisabled(true);
|
||||
} else {
|
||||
certList->insertPkiItems<pki_x509>(issuers);
|
||||
certList->insertPkiItems(issuers);
|
||||
}
|
||||
|
||||
// set dates to now and now + 1 year
|
||||
@ -99,7 +99,7 @@ NewX509::NewX509(QWidget *parent)
|
||||
on_applyTime_clicked();
|
||||
|
||||
// settings for the templates ....
|
||||
tempList->insertPkiItems<pki_temp>(MainWindow::temps->getAllAndPredefs());
|
||||
tempList->insertPkiItems(MainWindow::temps->getAllAndPredefs());
|
||||
|
||||
// setup Extended keyusage
|
||||
foreach(int nid, eku_nid)
|
||||
@ -655,7 +655,7 @@ void NewX509::switchHashAlgo()
|
||||
|
||||
void NewX509::on_showReqBut_clicked()
|
||||
{
|
||||
emit showReq(reqList->currentPkiItem<pki_x509req>());
|
||||
emit showReq(reqList->currentPkiItem());
|
||||
}
|
||||
|
||||
void NewX509::on_genKeyBut_clicked()
|
||||
@ -712,7 +712,7 @@ pki_temp *NewX509::currentTemplate()
|
||||
{
|
||||
if (!tempList->isEnabled())
|
||||
return NULL;
|
||||
return tempList->currentPkiItem<pki_temp>();
|
||||
return tempList->currentPkiItem();
|
||||
}
|
||||
|
||||
void NewX509::selfComment(QString msg)
|
||||
@ -770,26 +770,26 @@ void NewX509::newKeyDone(pki_key *nkey)
|
||||
|
||||
void NewX509::on_usedKeysToo_toggled(bool)
|
||||
{
|
||||
pki_key *cur = keyList->currentPkiItem<pki_key>();
|
||||
pki_key *cur = keyList->currentPkiItem();
|
||||
keyList->clear();
|
||||
keyList->insertPkiItems<pki_key>(usedKeysToo->isChecked() ?
|
||||
keyList->insertPkiItems(usedKeysToo->isChecked() ?
|
||||
allKeys : unusedKeys);
|
||||
keyList->setCurrentPkiItem(cur);
|
||||
}
|
||||
|
||||
pki_key *NewX509::getSelectedKey()
|
||||
{
|
||||
return keyList->currentPkiItem<pki_key>();
|
||||
return keyList->currentPkiItem();
|
||||
}
|
||||
|
||||
pki_x509 *NewX509::getSelectedSigner()
|
||||
{
|
||||
return certList->currentPkiItem<pki_x509>();
|
||||
return certList->currentPkiItem();
|
||||
}
|
||||
|
||||
pki_x509req *NewX509::getSelectedReq()
|
||||
{
|
||||
return reqList->currentPkiItem<pki_x509req>();
|
||||
return reqList->currentPkiItem();
|
||||
}
|
||||
|
||||
x509name NewX509::getX509name(int _throw)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user