From dc75fa0171d5aeb70789aaa9678a42cc1cc7f82f Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Mon, 20 Nov 2017 08:48:57 +0100 Subject: [PATCH] Add token as item source, minor fixes Use dynamic cast for sqlSELECTpki() Fix Double-click links in cert details --- lib/db_crl.cpp | 16 ++++++++-------- lib/db_key.cpp | 14 +++++++++----- lib/db_temp.cpp | 4 +++- lib/db_x509.cpp | 16 +++++++++------- lib/db_x509super.cpp | 7 ++++--- lib/pki_base.cpp | 1 + lib/pki_base.h | 3 ++- lib/pki_crl.cpp | 2 ++ lib/pki_scard.cpp | 3 +++ lib/pki_x509.cpp | 11 ++++++----- widgets/CertDetail.cpp | 5 +++-- widgets/ImportMulti.cpp | 14 +++++++------- widgets/KeyDetail.cpp | 2 +- 13 files changed, 58 insertions(+), 40 deletions(-) diff --git a/lib/db_crl.cpp b/lib/db_crl.cpp index fa781bc4..7485c47a 100644 --- a/lib/db_crl.cpp +++ b/lib/db_crl.cpp @@ -81,7 +81,7 @@ void db_crl::removeSigner(pki_base *signer) void db_crl::inToCont(pki_base *pki) { - pki_crl *crl = (pki_crl *)pki; + pki_crl *crl = static_cast(pki); unsigned hash = crl->getSubject().hashNum(); QList items; @@ -91,15 +91,15 @@ void db_crl::inToCont(pki_base *pki) QList() << QVariant(hash)); foreach(pki_base *b, items) { qDebug() << "Possible Crl issuer:" << b->getIntName(); - crl->verify(static_cast(b)); + crl->verify(dynamic_cast(b)); } db_base::inToCont(pki); } pki_base *db_crl::insert(pki_base *item) { - pki_crl *crl = (pki_crl *)item; - pki_crl *oldcrl = (pki_crl *)getByReference(crl); + pki_crl *crl = static_cast(item); + pki_crl *oldcrl = dynamic_cast(getByReference(crl)); if (oldcrl) { XCA_INFO(tr("The revocation list already exists in the database as:\n'%1'\nand so it was not imported").arg(oldcrl->getIntName())); delete(crl); @@ -112,10 +112,10 @@ pki_base *db_crl::insert(pki_base *item) void db_crl::showPki(pki_base *pki) { - pki_crl *crl = (pki_crl *)pki; - CrlDetail *dlg; - - dlg = new CrlDetail(mainwin); + pki_crl *crl = dynamic_cast(pki); + if (!crl) + return; + CrlDetail *dlg = new CrlDetail(mainwin); if (!dlg) return; diff --git a/lib/db_key.cpp b/lib/db_key.cpp index b0e7b153..e1b9098f 100644 --- a/lib/db_key.cpp +++ b/lib/db_key.cpp @@ -81,8 +81,9 @@ void db_key::remFromCont(QModelIndex &idx) QList items = sqlSELECTpki( "SELECT item FROM x509super WHERE pkey is NULL"); foreach(pki_base *b, items) { - pki_x509super *x509s = static_cast(b); - x509s->setRefKey(NULL); + pki_x509super *x509s = dynamic_cast(b); + if (x509s) + x509s->setRefKey(NULL); } /* "UPDATE x509super SET pkey=NULL WHERE pkey=?" done in * pki->deleteSqlData() */ @@ -100,8 +101,8 @@ void db_key::inToCont(pki_base *pki) SQL_PREPARE(q, "UPDATE x509super SET pkey=? WHERE item=?"); q.bindValue(0, key->getSqlItemId()); foreach(pki, items) { - pki_x509super *x509s = static_cast(pki); - if (!x509s->compareRefKey(key)) + pki_x509super *x509s = dynamic_cast(pki); + if (!x509s || !x509s->compareRefKey(key)) continue; /* Found item matching this key */ x509s->setRefKey(key); @@ -207,7 +208,10 @@ void db_key::load(void) void db_key::showPki(pki_base *pki) { - pki_evp *key = (pki_evp *)pki; + qDebug() << "showPki(pki_base *pki): " << pki->getType() << " - " << pki->getIntName(); + pki_key *key = dynamic_cast(pki); + if (!key) + return; KeyDetail *dlg = new KeyDetail(mainwin); if (dlg) { dlg->setKey(key); diff --git a/lib/db_temp.cpp b/lib/db_temp.cpp index 2fa30c71..0aa9ea53 100644 --- a/lib/db_temp.cpp +++ b/lib/db_temp.cpp @@ -114,7 +114,9 @@ void db_temp::newItem() } void db_temp::showPki(pki_base *pki) { - alterTemp(static_cast(pki)); + pki_temp *t = dynamic_cast(pki); + if (t) + alterTemp(t); } void db_temp::load() diff --git a/lib/db_x509.cpp b/lib/db_x509.cpp index 61cb1d51..b91ece35 100644 --- a/lib/db_x509.cpp +++ b/lib/db_x509.cpp @@ -87,10 +87,10 @@ pki_base *db_x509::newPKI(enum pki_type type) QList db_x509::getAllIssuers() { /* Select X509 CA certificates with available private key */ - return sqlSELECTpki("SELECT x509super.item FROM x509super " - "JOIN private_keys ON x509super.pkey = private_keys.item " - "JOIN certs ON certs.item = x509super.item " - "WHERE certs.ca=1"); + return sqlSELECTpki("SELECT DISTINCT x.item " + "FROM (private_keys, tokens) JOIN x509super x " + "ON x.pkey = private_keys.item OR x.pkey = tokens.item " + "JOIN certs ON certs.item = x.item WHERE certs.ca=1"); } void db_x509::remFromCont(QModelIndex &idx) @@ -200,8 +200,8 @@ void db_x509::inToCont(pki_base *pki) "AND x509super.key_hash=?", QList() << namehash << pubhash); foreach(pki_base *b, items) { - pki_x509 *other = static_cast(b); - if (other == cert) + pki_x509 *other = dynamic_cast(b); + if (other == cert || !other) continue; if (!other->compareNameAndKey(cert)) continue; @@ -255,7 +255,9 @@ void db_x509::inToCont(pki_base *pki) QList() << namehash); SQL_PREPARE(q, "UPDATE crls SET issuer=? WHERE item=?"); foreach(pki_base *b, items) { - pki_crl *crl = static_cast(b); + pki_crl *crl = dynamic_cast(b); + if (!crl) + continue; crl->verify(cert); if (cert != crl->getIssuer()) continue; diff --git a/lib/db_x509super.cpp b/lib/db_x509super.cpp index bad9b6aa..da70a255 100644 --- a/lib/db_x509super.cpp +++ b/lib/db_x509super.cpp @@ -187,9 +187,10 @@ void db_x509super::toTemplate(QModelIndex index) void db_x509super::showPki(pki_base *pki) { - pki_x509super *x = (pki_x509req *)pki; - CertDetail *dlg; - dlg = new CertDetail(mainwin); + pki_x509super *x = dynamic_cast(pki); + if (!x) + return; + CertDetail *dlg = new CertDetail(mainwin); if (!dlg) return; diff --git a/lib/pki_base.cpp b/lib/pki_base.cpp index c0159696..e25acf9d 100644 --- a/lib/pki_base.cpp +++ b/lib/pki_base.cpp @@ -256,6 +256,7 @@ QString pki_base::pki_source_name() const case imported: return tr("Imported"); case generated: return tr("Generated"); case transformed: return tr("Transformed"); + case token: return tr("Token"); } return QString("???"); } diff --git a/lib/pki_base.h b/lib/pki_base.h index fa55a0e2..ebdd1e34 100644 --- a/lib/pki_base.h +++ b/lib/pki_base.h @@ -31,7 +31,8 @@ enum pki_source { unknown, imported, generated, - transformed + transformed, + token }; #define VIEW_item_id 0 diff --git a/lib/pki_crl.cpp b/lib/pki_crl.cpp index 5310fdf2..b5f040ea 100644 --- a/lib/pki_crl.cpp +++ b/lib/pki_crl.cpp @@ -319,6 +319,8 @@ x509name pki_crl::getSubject() const bool pki_crl::verify(pki_x509 *issuer) { + if (!issuer) + return false; if (getSubject() != issuer->getSubject()) return false; pki_key *key = issuer->getPubKey(); diff --git a/lib/pki_scard.cpp b/lib/pki_scard.cpp index 36c5e4fc..f551030f 100644 --- a/lib/pki_scard.cpp +++ b/lib/pki_scard.cpp @@ -36,6 +36,7 @@ void pki_scard::init(void) { ownPass = ptPin; pkiType = smartCard; + isPub = false; card_serial = card_manufacturer = card_label = ""; card_model = slot_label = ""; @@ -238,6 +239,8 @@ void pki_scard::load_token(pkcs11 &p11, CK_OBJECT_HANDLE object) card_manufacturer = ti.manufacturerID(); card_serial = ti.serial(); card_model = ti.model(); + pkiSource = token; + isPub = false; pk11_attr_data id(CKA_ID); p11.loadAttribute(id, object); diff --git a/lib/pki_x509.cpp b/lib/pki_x509.cpp index c15c6193..127265a5 100644 --- a/lib/pki_x509.cpp +++ b/lib/pki_x509.cpp @@ -331,6 +331,7 @@ void pki_x509::load_token(pkcs11 &p11, CK_OBJECT_HANDLE object) } } setIntName(desc); + pkiSource = token; pki_openssl_error(); } @@ -362,8 +363,8 @@ void pki_x509::store_token(bool alwaysSelect) if (!p11.selectToken(&slot, NULL)) return; } else { - card = (pki_scard *)privkey; - if (!card->prepare_card(&slot)) + card = dynamic_cast(privkey); + if (!card || !card->prepare_card(&slot)) return; } @@ -398,10 +399,10 @@ void pki_x509::store_token(bool alwaysSelect) void pki_x509::deleteFromToken() { - pki_scard *card = (pki_scard *)privkey; + pki_scard *card = dynamic_cast(privkey); slotidList p11_slots; - if (!pkcs11::loaded()) + if (!card || !pkcs11::loaded()) return; if (privkey && privkey->isToken()) { @@ -896,7 +897,7 @@ int pki_x509::sigAlg() pki_x509 *pki_x509::getSigner() { - return (pki_x509 *)psigner; + return static_cast(psigner); } bool pki_x509::isRevoked() diff --git a/widgets/CertDetail.cpp b/widgets/CertDetail.cpp index 72dc3520..42440d9a 100644 --- a/widgets/CertDetail.cpp +++ b/widgets/CertDetail.cpp @@ -100,8 +100,9 @@ void CertDetail::setCert(pki_x509 *cert) signature->setGreen(); signature->disableToolTip(); } else { - signature->setText(cert->getSigner()->getIntName()); - privKey->setClickText(cert->getSqlItemId().toString()); + pki_x509 *issuer = cert->getSigner(); + signature->setText(issuer->getIntName()); + signature->setClickText(issuer->getSqlItemId().toString()); signature->setGreen(); } diff --git a/widgets/ImportMulti.cpp b/widgets/ImportMulti.cpp index 2f1ad098..3c2c2706 100644 --- a/widgets/ImportMulti.cpp +++ b/widgets/ImportMulti.cpp @@ -79,14 +79,14 @@ void ImportMulti::addItem(pki_base *pki) mcont->inToCont(pki); } else if (cn == "pki_pkcs7") { - pki_pkcs7 *p7 = ( pki_pkcs7 *)pki; + pki_pkcs7 *p7 = static_cast(pki); for (int i=0; inumCert(); i++) { addItem(p7->getCert(i)); } delete p7; } else if (cn == "pki_pkcs12") { - pki_pkcs12 *p12 = ( pki_pkcs12 *)pki; + pki_pkcs12 *p12 = static_cast(pki); addItem(p12->getKey()); addItem(p12->getCert()); for (int i=0; inumCa(); i++) { @@ -95,7 +95,7 @@ void ImportMulti::addItem(pki_base *pki) delete p12; } else if (cn == "pki_multi") { - pki_multi *pm = (pki_multi*)pki; + pki_multi *pm = static_cast(pki); pki_base *inner; while ((inner = pm->pull())) addItem(inner); @@ -272,25 +272,25 @@ void ImportMulti::on_butDetails_clicked() if (cn == "pki_x509"){ CertDetail *dlg; dlg = new CertDetail(mainwin); - dlg->setCert((pki_x509 *)pki); + dlg->setCert(static_cast(pki)); dlg->exec(); delete dlg; } else if (cn == "pki_evp" || cn == "pki_scard") { KeyDetail *dlg; dlg = new KeyDetail(mainwin); - dlg->setKey((pki_key *)pki); + dlg->setKey(static_cast(pki)); dlg->exec(); delete dlg; } else if (cn == "pki_x509req") { CertDetail *dlg; dlg = new CertDetail(mainwin); - dlg->setReq((pki_x509req *)pki); + dlg->setReq(static_cast(pki)); dlg->exec(); delete dlg; } else if (cn == "pki_crl") { CrlDetail *dlg; dlg = new CrlDetail(mainwin); - dlg->setCrl((pki_crl *)pki); + dlg->setCrl(static_cast(pki)); dlg->exec(); delete dlg; } else if (cn == "pki_temp") { diff --git a/widgets/KeyDetail.cpp b/widgets/KeyDetail.cpp index 039ea1ee..c0c2739d 100644 --- a/widgets/KeyDetail.cpp +++ b/widgets/KeyDetail.cpp @@ -53,7 +53,7 @@ void KeyDetail::setKey(pki_key *key) keyPrivEx->setRed(); } else if (key->isToken()) { image->setPixmap(*MainWindow::scardImg); - pki_scard *card = (pki_scard *)key; + pki_scard *card = static_cast(key); cardBox->setTitle(tr("Token") +" [" +card->getCardLabel() +"]"); cardManufacturer->setText(card->getManufacturer() + " " + card->getModel());