From 9da73073c8d24cf630523db43ffb4f47b5104a7d Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Sat, 22 Feb 2020 07:31:54 +0100 Subject: [PATCH] Create CRL by providing a crljob, describing the parameters The GUI will prepare such a job and the cmdline can do so, too. --- lib/db_crl.cpp | 45 ++++++++++++++++++++++----------------------- lib/db_crl.h | 1 + lib/pki_base.cpp | 3 ++- lib/pki_crl.h | 29 +++++++++++++++++++++++++++++ widgets/NewCrl.cpp | 41 +++++++++++++++++++++++++++++++---------- widgets/NewCrl.h | 7 +++++-- widgets/hashBox.cpp | 5 +++++ widgets/hashBox.h | 1 + 8 files changed, 96 insertions(+), 36 deletions(-) diff --git a/lib/db_crl.cpp b/lib/db_crl.cpp index 18978460..79559a77 100644 --- a/lib/db_crl.cpp +++ b/lib/db_crl.cpp @@ -220,17 +220,20 @@ void db_crl::newItem() void db_crl::newItem(pki_x509 *cert) { - if (!cert) - return; - - pki_crl *crl = NULL; - NewCrl *widget = new NewCrl(NULL, cert); + crljob task(cert); + NewCrl *widget = new NewCrl(NULL, task); XcaDialog *dlg = new XcaDialog(mainwin, revocation, widget, tr("Create CRL"), QString()); - if (!dlg->exec()) { - delete dlg; - return; + if (dlg->exec()) { + newItem(widget->getCrlJob()); } + delete dlg; +} + +void db_crl::newItem(const crljob &task) +{ + pki_crl *crl = NULL; + pki_x509 *cert = task.issuer; QSqlDatabase db = QSqlDatabase::database(); try { x509v3ext e; @@ -243,38 +246,35 @@ void db_crl::newItem(pki_x509 *cert) crl->createCrl(cert->getIntName(), cert); crl->pkiSource = generated; - bool withReason = widget->revocationReasons->isChecked(); foreach(x509rev rev, cert->getRevList()) - crl->addRev(rev, withReason); + crl->addRev(rev, task.withReason); - if (widget->authKeyId->isChecked()) { + if (task.authKeyId) { crl->addV3ext(e.create(NID_authority_key_identifier, "keyid,issuer", &ext_ctx)); } - if (widget->subAltName->isChecked()) { + if (task.subAltName) { if (cert->hasExtension(NID_subject_alt_name)) { crl->addV3ext(e.create(NID_issuer_alt_name, "issuer:copy", &ext_ctx)); } } - if (widget->setCrlNumber->isChecked()) { - a1int num; - num.setDec(widget->crlNumber->text()); - crl->setCrlNumber(num); - cert->setCrlNumber(num); + if (task.setCrlNumber) { + crl->setCrlNumber(task.crlNumber); + cert->setCrlNumber(task.crlNumber); } crl->setIssuer(cert); - crl->setLastUpdate(widget->lastUpdate->getDate()); - crl->setNextUpdate(widget->nextUpdate->getDate()); - crl->sign(cert->getRefKey(), widget->hashAlgo->currentHash()); + crl->setLastUpdate(task.lastUpdate); + crl->setNextUpdate(task.nextUpdate); + crl->sign(cert->getRefKey(), task.hashAlgo); Transaction; if (!TransBegin()) throw errorEx(tr("Failed to initiate DB transaction")); - cert->setCrlExpire(widget->nextUpdate->getDate()); + cert->setCrlExpire(task.nextUpdate); SQL_PREPARE(q, "UPDATE authority set crlNo=?, crlExpire=? WHERE item=?"); q.bindValue(0, (uint)cert->getCrlNumber().getLong()); - q.bindValue(1, widget->nextUpdate->getDate().toPlain()); + q.bindValue(1, task.nextUpdate.toPlain()); q.bindValue(2, cert->getSqlItemId()); AffectedItems(cert->getSqlItemId()); q.exec(); @@ -302,6 +302,5 @@ void db_crl::newItem(pki_x509 *cert) delete crl; crl = NULL; } - delete dlg; return; } diff --git a/lib/db_crl.h b/lib/db_crl.h index c3bef2b5..f1669206 100644 --- a/lib/db_crl.h +++ b/lib/db_crl.h @@ -30,6 +30,7 @@ class db_crl: public db_x509name void load(); void showPki(pki_base *pki); void updateCertView(); + void newItem(const crljob &crljob); public slots: void newItem(); diff --git a/lib/pki_base.cpp b/lib/pki_base.cpp index 14609df9..fb1cfd0d 100644 --- a/lib/pki_base.cpp +++ b/lib/pki_base.cpp @@ -386,7 +386,8 @@ void pki_base::selfComment(QString msg) void pki_base::print(FILE *fp) const { - fprintf(fp, "File: %s\n", CCHAR(getFilename())); + fprintf(fp, "File: %s [%s]\n", CCHAR(getFilename()), + CCHAR(getIntName())); } static QString icsValue(QString s) diff --git a/lib/pki_crl.h b/lib/pki_crl.h index 77f14bd0..a0bfd3c2 100644 --- a/lib/pki_crl.h +++ b/lib/pki_crl.h @@ -20,6 +20,35 @@ #define VIEW_crls_issuer 7 #define VIEW_crls_crl 8 +#include "widgets/hashBox.h" +class crljob +{ + public: + pki_x509 *issuer; + bool withReason; + bool authKeyId; + bool subAltName; + bool setCrlNumber; + a1int crlNumber; + int crlDays; + const EVP_MD *hashAlgo; + a1time lastUpdate; + a1time nextUpdate; + + crljob(pki_x509 *x) : issuer(x) + { + withReason = true; + authKeyId = true; + subAltName = true; + setCrlNumber = issuer->getCrlNumber().getLong() > 0; + crlNumber = issuer->getCrlNumber(); + crlNumber++; + crlDays = issuer->getCrlDays(); + hashAlgo = hashBox::getDefaultMD(); + nextUpdate = lastUpdate.addDays(crlDays); + } +}; + class pki_crl: public pki_x509name { Q_OBJECT diff --git a/widgets/NewCrl.cpp b/widgets/NewCrl.cpp index 6f3c7401..86d18c5a 100644 --- a/widgets/NewCrl.cpp +++ b/widgets/NewCrl.cpp @@ -17,27 +17,44 @@ #include #include -NewCrl::NewCrl(QWidget *parent, pki_x509 *signer) - :QWidget(parent) +NewCrl::NewCrl(QWidget *parent, const crljob &j) + :QWidget(parent), task(j) { + pki_x509 *issuer = task.issuer; + pki_key *key = issuer->getRefKey(); + setupUi(this); - dateBox->setTitle(signer->getIntName()); - validNumber->setText(QString::number(signer->getCrlDays())); + dateBox->setTitle(issuer->getIntName()); + validNumber->setText(QString::number(task.crlDays)); validRange->setCurrentIndex(0); on_applyTime_clicked(); nextUpdate->setEndDate(true); - pki_key *key = signer->getRefKey(); hashAlgo->setKeyType(key->getKeyType()); hashAlgo->setupHashes(key->possibleHashNids()); + hashAlgo->setCurrentMD(task.hashAlgo); - a1int num = signer->getCrlNumber(); - num++; - crlNumber->setText(num.toDec()); - if (signer->hasExtension(NID_subject_alt_name)) + crlNumber->setText(task.crlNumber.toDec()); + if (issuer->hasExtension(NID_subject_alt_name)) { subAltName->setEnabled(true); - else + subAltName->setChecked(task.subAltName); + } else { subAltName->setEnabled(false); + } + revocationReasons->setChecked(task.withReason); + authKeyId->setChecked(task.authKeyId); +} + +crljob NewCrl::getCrlJob() const +{ + crljob t = task; + t.withReason = revocationReasons->isChecked(); + t.authKeyId = authKeyId->isChecked(); + t.subAltName = subAltName->isChecked(); + t.setCrlNumber = setCrlNumber->isChecked(); + t.lastUpdate = lastUpdate->getDate(); + t.nextUpdate = nextUpdate->getDate(); + return t; } void NewCrl::on_applyTime_clicked() @@ -46,3 +63,7 @@ void NewCrl::on_applyTime_clicked() validRange->currentIndex()); } +NewCrl::~NewCrl() +{ + qDebug() << "NewCrl::~NewCrl() -- DELETED"; +} diff --git a/widgets/NewCrl.h b/widgets/NewCrl.h index e9d349af..1fc17d76 100644 --- a/widgets/NewCrl.h +++ b/widgets/NewCrl.h @@ -9,7 +9,7 @@ #define __NEWCRL_H #include "ui_NewCrl.h" -#include "lib/pki_x509.h" +#include "lib/pki_crl.h" class pki_key; @@ -17,8 +17,11 @@ class NewCrl: public QWidget, public Ui::NewCrl { Q_OBJECT + crljob task; public: - NewCrl(QWidget *parent, pki_x509 *signer); + NewCrl(QWidget *parent, const crljob &task); + ~NewCrl(); + crljob getCrlJob() const; public slots: void on_applyTime_clicked(); diff --git a/widgets/hashBox.cpp b/widgets/hashBox.cpp index 8bc4ecc7..660269b2 100644 --- a/widgets/hashBox.cpp +++ b/widgets/hashBox.cpp @@ -214,3 +214,8 @@ QString hashBox::getDefault() { return QString(hashalgos[default_md].name); } + +const EVP_MD *hashBox::getDefaultMD() +{ + return EVP_get_digestbynid(hashalgos[default_md].nid); +} diff --git a/widgets/hashBox.h b/widgets/hashBox.h index af9f2803..4a8d031c 100644 --- a/widgets/hashBox.h +++ b/widgets/hashBox.h @@ -33,6 +33,7 @@ class hashBox: public QComboBox void setCurrentString(QString md); static void setDefault(QString def); static QString getDefault(); + static const EVP_MD *getDefaultMD(); }; #endif