From 4eb7b170b156dcc19039a6de4f028ed260572beb Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Fri, 22 Mar 2019 15:00:19 +0100 Subject: [PATCH] Do not reference the key by pointer, but by Key Id This way the pointer to the key may change during reload. --- lib/pki_x509.cpp | 3 +++ lib/pki_x509req.cpp | 3 --- lib/pki_x509super.cpp | 11 ++++++----- lib/pki_x509super.h | 1 - lib/sql.cpp | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/pki_x509.cpp b/lib/pki_x509.cpp index c332e870..b0cb7caa 100644 --- a/lib/pki_x509.cpp +++ b/lib/pki_x509.cpp @@ -360,6 +360,7 @@ void pki_x509::store_token(bool alwaysSelect) pkcs11 p11; + pki_key *privkey = getRefKey(); if (!privkey || !privkey->isToken() || alwaysSelect) { if (!p11.selectToken(&slot, NULL)) return; @@ -400,6 +401,7 @@ void pki_x509::store_token(bool alwaysSelect) void pki_x509::deleteFromToken() { + pki_key *privkey = getRefKey(); pki_scard *card = dynamic_cast(privkey); slotidList p11_slots; @@ -557,6 +559,7 @@ bool pki_x509::isCA() const bool pki_x509::canSign() const { + pki_key *privkey = getRefKey(); if (!privkey || privkey->isPubKey()) return false; if (privkey->isToken() && !pkcs11::loaded()) diff --git a/lib/pki_x509req.cpp b/lib/pki_x509req.cpp index 9bd84efb..efbc005d 100644 --- a/lib/pki_x509req.cpp +++ b/lib/pki_x509req.cpp @@ -25,7 +25,6 @@ QPixmap *pki_x509req::icon[3] = { NULL, NULL, NULL }; pki_x509req::pki_x509req(const QString name) : pki_x509super(name) { - privkey = NULL; request = X509_REQ_new(); pki_openssl_error(); pkiType=x509_req; @@ -209,9 +208,7 @@ void pki_x509req::fromData(const unsigned char *p, db_header_t *head ) int size; size = head->len - sizeof(db_header_t); - QByteArray ba((const char *)p, size); - privkey = NULL; d2i(ba); pki_openssl_error(); diff --git a/lib/pki_x509super.cpp b/lib/pki_x509super.cpp index d89f8600..91c4a3ab 100644 --- a/lib/pki_x509super.cpp +++ b/lib/pki_x509super.cpp @@ -15,7 +15,6 @@ QPixmap *pki_x509super::icon[1]; pki_x509super::pki_x509super(const QString name) : pki_x509name(name) { - privkey = NULL; } pki_x509super::~pki_x509super() @@ -59,7 +58,7 @@ QSqlError pki_x509super::insertSqlData() "VALUES (?, ?, ?, ?)"); q.bindValue(0, sqlItemId); q.bindValue(1, (uint)getSubject().hashNum()); - q.bindValue(2, privkey ? privkey->getSqlItemId() : QVariant()); + q.bindValue(2, keySqlId); q.bindValue(3, pubHash()); q.exec(); return q.lastError(); @@ -69,12 +68,12 @@ void pki_x509super::restoreSql(const QSqlRecord &rec) { pki_base::restoreSql(rec); keySqlId = rec.value(VIEW_x509super_keyid); - privkey = NULL; } QSqlError pki_x509super::deleteSqlData() { XSqlQuery q; + pki_key *privkey = getRefKey(); if (privkey) privkey->resetUcount(); SQL_PREPARE(q, "DELETE FROM x509super WHERE item=?"); @@ -85,12 +84,13 @@ QSqlError pki_x509super::deleteSqlData() pki_key *pki_x509super::getRefKey() const { - return privkey; + return db_base::lookupPki(keySqlId); } unsigned pki_x509super::pubHash() const { unsigned hash = 0; + pki_key *privkey = getRefKey(); if (privkey) { hash = privkey->hash(); } else { @@ -119,7 +119,7 @@ bool pki_x509super::compareRefKey(pki_key *ref) const void pki_x509super::setRefKey(pki_key *ref) { - privkey = ref; + keySqlId = ref ? ref->getSqlItemId() : QVariant(); } QString pki_x509super::getSigAlg() const @@ -149,6 +149,7 @@ QVariant pki_x509super::getIcon(const dbheader *hd) const QVariant pki_x509super::column_data(const dbheader *hd) const { if (hd->id == HD_x509key_name) { + pki_key *privkey = getRefKey(); if (!privkey) return QVariant(""); return QVariant(privkey->getIntName()); diff --git a/lib/pki_x509super.h b/lib/pki_x509super.h index d916cb6a..ea03c41b 100644 --- a/lib/pki_x509super.h +++ b/lib/pki_x509super.h @@ -34,7 +34,6 @@ class pki_x509super : public pki_x509name Q_OBJECT protected: QVariant keySqlId; - pki_key *privkey; virtual int sigAlg() const = 0; public: pki_x509super(const QString name = ""); diff --git a/lib/sql.cpp b/lib/sql.cpp index 182ed89b..ce5d151e 100644 --- a/lib/sql.cpp +++ b/lib/sql.cpp @@ -129,7 +129,7 @@ QString XSqlQuery::rewriteQuery(QString _q) "items" << "crls" << "private_keys" << "public_keys" << "tokens" << "token_mechanism" << "templates" << "certs" << "authority" << "revocations" << "requests" << "x509super" << - "settings" << "revocations" << + "settings" << "view_public_keys" << "view_certs" << "view_requests" << "view_crls" << "view_templates" << "view_private" ;