From e38dd5370d1de9e6b7767acf30e8b04eafff0518 Mon Sep 17 00:00:00 2001 From: Justin Karneges Date: Fri, 13 Apr 2007 06:32:02 +0000 Subject: [PATCH] keystore api update svn path=/trunk/kdesupport/qca/; revision=653377 --- include/QtCrypto/qca_core.h | 23 +++++++++--------- include/QtCrypto/qca_keystore.h | 37 +++++++++++++++++++++-------- include/QtCrypto/qcaprovider.h | 13 +++++----- plugins/qca-gnupg/qca-gnupg.cpp | 25 ++++++++++---------- src/qca_core.cpp | 42 ++++++++++++++++----------------- src/qca_default.cpp | 19 +++++++++++---- src/qca_keystore.cpp | 42 +++++++++++++++++++++------------ tools/qcatool/main.cpp | 8 +++---- 8 files changed, 125 insertions(+), 84 deletions(-) diff --git a/include/QtCrypto/qca_core.h b/include/QtCrypto/qca_core.h index 3977171c..5fd5d0f2 100644 --- a/include/QtCrypto/qca_core.h +++ b/include/QtCrypto/qca_core.h @@ -68,6 +68,7 @@ namespace QCA class CertificateCollection; class Global; class KeyStore; + class KeyStoreEntry; class KeyStoreManager; class Logger; @@ -1102,7 +1103,7 @@ namespace QCA \sa source() \sa fileName() for the name, if source is Event::Data - \sa keyStoreId() and keyStoreEntryId for the keystore and entry, if + \sa keyStoreId() and keyStoreEntry() for the keystore and entry, if the source is Event::KeyStore */ enum Source @@ -1182,11 +1183,11 @@ namespace QCA QString keyStoreId() const; /** - The id of the KeyStoreEntry associated with this event + The KeyStoreEntry associated with this event This is not meaningful unless the Source is KeyStore. */ - QString keyStoreEntryId() const; + KeyStoreEntry keyStoreEntry() const; /** Name or other identifier for the file or byte array @@ -1208,10 +1209,10 @@ namespace QCA \param pstyle the style of information required (e.g. PIN, password or passphrase) \param keyStoreId the keystore that the information is required for - \param keyStoreEntryId the entry in the keystore that the information is required for + \param keyStoreEntry the entry in the keystore that the information is required for \param ptr opaque data */ - void setPasswordKeyStore(PasswordStyle pstyle, const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr); + void setPasswordKeyStore(PasswordStyle pstyle, const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr); /** Set the values for this Event @@ -1230,10 +1231,10 @@ namespace QCA This creates a Token type event. \param keyStoreId the keystore that the token is required for - \param keyStoreEntryId the entry in the keystore that the token is required for + \param keyStoreEntry the entry in the keystore that the token is required for \param ptr opaque data */ - void setToken(const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr); + void setToken(const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr); private: class Private; @@ -1343,10 +1344,10 @@ namespace QCA \param pstyle the type of information required (e.g. PIN, passphrase or password) \param keyStoreId the key store that the information is required for - \param keyStoreEntryId the item in the key store that the information is required for (if applicable) + \param keyStoreEntry the item in the key store that the information is required for (if applicable) \param ptr opaque data */ - void ask(Event::PasswordStyle pstyle, const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr); + void ask(Event::PasswordStyle pstyle, const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr); /** queue a password / passphrase request associated with a file @@ -1423,10 +1424,10 @@ namespace QCA queue a token request associated with a key store \param keyStoreId the key store that the information is required for - \param keyStoreEntryId the item in the key store that the information is required for (if applicable) + \param keyStoreEntry the item in the key store that the information is required for (if applicable) \param ptr opaque data */ - void ask(const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr); + void ask(const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr); /** Cancel the pending password / passphrase request diff --git a/include/QtCrypto/qca_keystore.h b/include/QtCrypto/qca_keystore.h index 2134e364..27a12925 100644 --- a/include/QtCrypto/qca_keystore.h +++ b/include/QtCrypto/qca_keystore.h @@ -153,9 +153,12 @@ namespace QCA KeyStoreEntry(); /** - Create a passive KeyStoreEntry based on known entry + Create a passive KeyStoreEntry based on a serialized + string + + \sa fromString */ - KeyStoreEntry(const QString &id); + KeyStoreEntry(const QString &serialized); /** Standard copy constructor @@ -218,8 +221,6 @@ namespace QCA /** The ID associated with the key stored in this object. - - The ID is unique across all stores, and may be very long. */ QString id() const; @@ -235,6 +236,19 @@ namespace QCA */ QString storeId() const; + /** + Serialize into a string for use as a passive entry + */ + QString toString() const; + + /** + Load a passive entry by using a serialized string + as input + + \return the newly created KeyStoreEntry + */ + static KeyStoreEntry fromString(const QString &serialized); + /** If a KeyBundle is stored in this object, return that bundle. @@ -405,8 +419,8 @@ namespace QCA list if none are known yet (in this mode, updated() will be emitted once the initial entries are known, even if the store has not actually been altered). writeEntry() will - always return true, and the entryWritten() signal - indicates the result of a write. + always return an empty string, and the entryWritten() + signal indicates the result of a write. */ void startAsynchronousMode(); @@ -433,23 +447,26 @@ namespace QCA /** Add a entry to the KeyStore + Returns the entryId of the written entry or an empty + string on failure. + \param kb the KeyBundle to add to the KeyStore */ - bool writeEntry(const KeyBundle &kb); + QString writeEntry(const KeyBundle &kb); /** \overload \param cert the Certificate to add to the KeyStore */ - bool writeEntry(const Certificate &cert); + QString writeEntry(const Certificate &cert); /** \overload \param crl the CRL to add to the KeyStore */ - bool writeEntry(const CRL &crl); + QString writeEntry(const CRL &crl); /** \overload @@ -458,7 +475,7 @@ namespace QCA \return a ref to the key in the keyring */ - PGPKey writeEntry(const PGPKey &key); + QString writeEntry(const PGPKey &key); /** Delete the a specified KeyStoreEntry from this KeyStore diff --git a/include/QtCrypto/qcaprovider.h b/include/QtCrypto/qcaprovider.h index f564657e..9e360a8d 100644 --- a/include/QtCrypto/qcaprovider.h +++ b/include/QtCrypto/qcaprovider.h @@ -380,6 +380,7 @@ public: virtual QString name() const = 0; virtual QString storeId() const = 0; virtual QString storeName() const = 0; + virtual QString serialize() const = 0; virtual KeyBundle keyBundle() const; virtual Certificate certificate() const; @@ -420,13 +421,13 @@ public: // return 0 if no such entry virtual KeyStoreEntryContext *entry(int id, const QString &entryId); - // return 0 if the provider doesn't handle this type of storeId - virtual KeyStoreEntryContext *entryPassive(const QString &storeId, const QString &entryId); + // return 0 if the provider doesn't handle or understand the string + virtual KeyStoreEntryContext *entryPassive(const QString &serialized); - virtual bool writeEntry(int id, const KeyBundle &kb); - virtual bool writeEntry(int id, const Certificate &cert); - virtual bool writeEntry(int id, const CRL &crl); - virtual PGPKey writeEntry(int id, const PGPKey &key); + virtual QString writeEntry(int id, const KeyBundle &kb); + virtual QString writeEntry(int id, const Certificate &cert); + virtual QString writeEntry(int id, const CRL &crl); + virtual QString writeEntry(int id, const PGPKey &key); virtual bool removeEntry(int id, const QString &entryId); virtual void submitPassphrase(int id, int requestId, const QSecureArray &passphrase); diff --git a/plugins/qca-gnupg/qca-gnupg.cpp b/plugins/qca-gnupg/qca-gnupg.cpp index 18343cb4..6b0f094d 100644 --- a/plugins/qca-gnupg/qca-gnupg.cpp +++ b/plugins/qca-gnupg/qca-gnupg.cpp @@ -168,7 +168,6 @@ class MyKeyStoreEntry : public KeyStoreEntryContext public: KeyStoreEntry::Type item_type; PGPKey pub, sec; - QString _id; QString _storeId, _storeName; MyKeyStoreEntry(const PGPKey &_pub, const PGPKey &_sec, Provider *p) : KeyStoreEntryContext(p) @@ -206,7 +205,7 @@ public: virtual QString id() const { - return _id; + return pub.keyId(); } virtual QString storeId() const @@ -229,12 +228,12 @@ public: return pub; } - void makeId() + virtual QString serialize() const { QStringList out; out += escape_string("qca-gnupg-1"); out += escape_string(pub.keyId()); - _id = out.join(":"); + return out.join(":"); } }; @@ -477,18 +476,15 @@ public: MyKeyStoreEntry *c = new MyKeyStoreEntry(pub, sec, provider()); c->_storeId = storeId(0); c->_storeName = name(0); - c->makeId(); out.append(c); } return out; } - virtual KeyStoreEntryContext *entryPassive(const QString &_storeId, const QString &entryId) + virtual KeyStoreEntryContext *entryPassive(const QString &serialized) { - Q_UNUSED(_storeId); - - QStringList parts = entryId.split(':'); + QStringList parts = serialized.split(':'); if(parts.count() < 2) return 0; if(unescape_string(parts[0]) != "qca-gnupg-1") @@ -559,7 +555,6 @@ public: MyKeyStoreEntry *c = new MyKeyStoreEntry(pub, sec, provider()); c->_storeId = storeId(0); c->_storeName = name(0); - c->_id = entryId; return c; } @@ -821,8 +816,14 @@ public: QStringList out; out += escape_string("qca-gnupg-1"); out += escape_string(keyId); - QString entryId = out.join(":"); - asker.ask(Event::StylePassphrase, keyStoreList->storeId(0), entryId, 0); + QString serialized = out.join(":"); + + KeyStoreEntry kse; + KeyStoreEntryContext *c = keyStoreList->entryPassive(serialized); + if(c) + kse.change(c); + + asker.ask(Event::StylePassphrase, keyStoreList->storeId(0), kse, 0); asker.waitForResponse(); global_gpg = &gpg; keyStoreList->submitPassphrase(0, 0, asker.password()); diff --git a/src/qca_core.cpp b/src/qca_core.cpp index a0afb0f9..df636462 100644 --- a/src/qca_core.cpp +++ b/src/qca_core.cpp @@ -990,31 +990,30 @@ KeyStoreEntryContext *KeyStoreListContext::entry(int id, const QString &entryId) return out; } -KeyStoreEntryContext *KeyStoreListContext::entryPassive(const QString &storeId, const QString &entryId) +KeyStoreEntryContext *KeyStoreListContext::entryPassive(const QString &serialized) { - Q_UNUSED(storeId); - Q_UNUSED(entryId); + Q_UNUSED(serialized); return 0; } -bool KeyStoreListContext::writeEntry(int, const KeyBundle &) +QString KeyStoreListContext::writeEntry(int, const KeyBundle &) { - return false; + return QString(); } -bool KeyStoreListContext::writeEntry(int, const Certificate &) +QString KeyStoreListContext::writeEntry(int, const Certificate &) { - return false; + return QString(); } -bool KeyStoreListContext::writeEntry(int, const CRL &) +QString KeyStoreListContext::writeEntry(int, const CRL &) { - return false; + return QString(); } -PGPKey KeyStoreListContext::writeEntry(int, const PGPKey &) +QString KeyStoreListContext::writeEntry(int, const PGPKey &) { - return PGPKey(); + return QString(); } bool KeyStoreListContext::removeEntry(int, const QString &) @@ -1335,7 +1334,8 @@ public: Type type; Source source; PasswordStyle style; - QString ks, kse; + QString ks; + KeyStoreEntry kse; QString fname; void *ptr; }; @@ -1384,7 +1384,7 @@ QString Event::keyStoreId() const return d->ks; } -QString Event::keyStoreEntryId() const +KeyStoreEntry Event::keyStoreEntry() const { return d->kse; } @@ -1399,7 +1399,7 @@ void *Event::ptr() const return d->ptr; } -void Event::setPasswordKeyStore(PasswordStyle pstyle, const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr) +void Event::setPasswordKeyStore(PasswordStyle pstyle, const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr) { if(!d) d = new Private; @@ -1407,7 +1407,7 @@ void Event::setPasswordKeyStore(PasswordStyle pstyle, const QString &keyStoreId, d->source = KeyStore; d->style = pstyle; d->ks = keyStoreId; - d->kse = keyStoreEntryId; + d->kse = keyStoreEntry; d->fname = QString(); d->ptr = ptr; } @@ -1425,7 +1425,7 @@ void Event::setPasswordData(PasswordStyle pstyle, const QString &fileName, void d->ptr = ptr; } -void Event::setToken(const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr) +void Event::setToken(const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr) { if(!d) d = new Private; @@ -1433,7 +1433,7 @@ void Event::setToken(const QString &keyStoreId, const QString &keyStoreEntryId, d->source = KeyStore; d->style = StylePassword; d->ks = keyStoreId; - d->kse = keyStoreEntryId; + d->kse = keyStoreEntry; d->fname = QString(); d->ptr = ptr; } @@ -1928,10 +1928,10 @@ PasswordAsker::~PasswordAsker() delete d; } -void PasswordAsker::ask(Event::PasswordStyle pstyle, const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr) +void PasswordAsker::ask(Event::PasswordStyle pstyle, const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr) { Event e; - e.setPasswordKeyStore(pstyle, keyStoreId, keyStoreEntryId, ptr); + e.setPasswordKeyStore(pstyle, keyStoreId, keyStoreEntry, ptr); d->ask(e); } @@ -1989,10 +1989,10 @@ TokenAsker::~TokenAsker() delete d; } -void TokenAsker::ask(const QString &keyStoreId, const QString &keyStoreEntryId, void *ptr) +void TokenAsker::ask(const QString &keyStoreId, const KeyStoreEntry &keyStoreEntry, void *ptr) { Event e; - e.setToken(keyStoreId, keyStoreEntryId, ptr); + e.setToken(keyStoreId, keyStoreEntry, ptr); d->ask(e); } diff --git a/src/qca_default.cpp b/src/qca_default.cpp index 52ea3f1f..5fcfc5ef 100644 --- a/src/qca_default.cpp +++ b/src/qca_default.cpp @@ -783,6 +783,7 @@ public: QString item_id, _storeId, _storeName; Certificate _cert; CRL _crl; + QString item_save; QString item_name; @@ -863,6 +864,11 @@ public: { return _crl; } + + virtual QString serialize() const + { + return item_save; + } }; //---------------------------------------------------------------------------- @@ -952,7 +958,8 @@ public: //QString ename = names[n]; QString eid = QString::number(qHash(certs[n].toDER().toByteArray())); c->item_name = ename; - c->item_id = makeId(storeId(0), name(0), eid, ename, "cert", certs[n].toPEM()); + c->item_id = eid; + c->item_save = makeId(storeId(0), name(0), eid, ename, "cert", certs[n].toPEM()); out.append(c); } for(n = 0; n < crls.count(); ++n) @@ -961,7 +968,8 @@ public: QString ename = c->makeName(); QString eid = QString::number(qHash(certs[n].toDER().toByteArray())); c->item_name = ename; - c->item_id = makeId(storeId(0), name(0), eid, ename, "crl", crls[n].toPEM()); + c->item_id = eid; + c->item_save = makeId(storeId(0), name(0), eid, ename, "crl", crls[n].toPEM()); out.append(c); } @@ -969,11 +977,10 @@ public: } // TODO - virtual KeyStoreEntryContext *entryPassive(const QString &_storeId, const QString &entryId) + virtual KeyStoreEntryContext *entryPassive(const QString &serialized) { - Q_UNUSED(_storeId); QString storeId, storeName, eid, ename, etype, pem; - if(parseId(entryId, &storeId, &storeName, &eid, &ename, &etype, &pem)) + if(parseId(serialized, &storeId, &storeName, &eid, &ename, &etype, &pem)) { if(etype == "cert") { @@ -983,6 +990,7 @@ public: DefaultKeyStoreEntry *c = new DefaultKeyStoreEntry(cert, storeId, storeName, provider()); c->item_name = ename; c->item_id = eid; + c->item_save = serialized; return c; } else if(etype == "crl") @@ -993,6 +1001,7 @@ public: DefaultKeyStoreEntry *c = new DefaultKeyStoreEntry(crl, storeId, storeName, provider()); c->item_name = ename; c->item_id = eid; + c->item_save = serialized; return c; } } diff --git a/src/qca_keystore.cpp b/src/qca_keystore.cpp index 090d523a..bce32f14 100644 --- a/src/qca_keystore.cpp +++ b/src/qca_keystore.cpp @@ -279,12 +279,12 @@ public slots: } // hack with void * - void *entryPassive(const QString &entryId) + void *entryPassive(const QString &serialized) { foreach(Item i, items) { // "is this yours?" - KeyStoreEntryContext *e = i.owner->entryPassive(i.storeId, entryId); + KeyStoreEntryContext *e = i.owner->entryPassive(serialized); if(e) return e; } @@ -502,13 +502,10 @@ KeyStoreEntry::KeyStoreEntry() { } -KeyStoreEntry::KeyStoreEntry(const QString &id) +KeyStoreEntry::KeyStoreEntry(const QString &serialized) :d(new Private) { - //KeyStoreEntryContext *c = (KeyStoreEntryContext *)qVariantValue(trackercall("entryPassive", QVariantList() << id)); - KeyStoreEntryContext *c = (KeyStoreEntryContext *)KeyStoreTracker::instance()->entryPassive(id); - if(c) - change(c); + *this = fromString(serialized); } KeyStoreEntry::KeyStoreEntry(const KeyStoreEntry &from) @@ -568,6 +565,21 @@ QString KeyStoreEntry::storeId() const return static_cast(context())->storeId(); } +QString KeyStoreEntry::toString() const +{ + return static_cast(context())->serialize(); +} + +KeyStoreEntry KeyStoreEntry::fromString(const QString &serialized) +{ + KeyStoreEntry e; + //KeyStoreEntryContext *c = (KeyStoreEntryContext *)qVariantValue(trackercall("entryPassive", QVariantList() << id)); + KeyStoreEntryContext *c = (KeyStoreEntryContext *)KeyStoreTracker::instance()->entryPassive(serialized); + if(c) + e.change(c); + return e; +} + KeyBundle KeyStoreEntry::keyBundle() const { return static_cast(context())->keyBundle(); @@ -790,32 +802,32 @@ bool KeyStore::holdsPGPPublicKeys() const return false; } -bool KeyStore::writeEntry(const KeyBundle &kb) +QString KeyStore::writeEntry(const KeyBundle &kb) { // TODO Q_UNUSED(kb); - return false; + return QString(); } -bool KeyStore::writeEntry(const Certificate &cert) +QString KeyStore::writeEntry(const Certificate &cert) { // TODO Q_UNUSED(cert); - return false; + return QString(); } -bool KeyStore::writeEntry(const CRL &crl) +QString KeyStore::writeEntry(const CRL &crl) { // TODO Q_UNUSED(crl); - return false; + return QString(); } -PGPKey KeyStore::writeEntry(const PGPKey &key) +QString KeyStore::writeEntry(const PGPKey &key) { // TODO Q_UNUSED(key); - return PGPKey(); + return QString(); } bool KeyStore::removeEntry(const QString &id) diff --git a/tools/qcatool/main.cpp b/tools/qcatool/main.cpp index 3449837c..1921c24a 100644 --- a/tools/qcatool/main.cpp +++ b/tools/qcatool/main.cpp @@ -225,7 +225,7 @@ private slots: if(e.source() == QCA::Event::KeyStore) { QString name; - QCA::KeyStoreEntry entry(e.keyStoreEntryId()); + QCA::KeyStoreEntry entry = e.keyStoreEntry(); if(!entry.isNull()) { name = entry.name(); @@ -257,7 +257,7 @@ private slots: } else if(e.type() == QCA::Event::Token) { - QCA::KeyStoreEntry entry(e.keyStoreEntryId()); + QCA::KeyStoreEntry entry = e.keyStoreEntry(); QString name; if(!entry.isNull()) { @@ -2401,7 +2401,7 @@ int main(int argc, char **argv) if(cert.isNull()) return 1; - if(store.writeEntry(cert)) + if(!store.writeEntry(cert).isEmpty()) printf("Entry written.\n"); else { @@ -2429,7 +2429,7 @@ int main(int argc, char **argv) if(pub.isNull()) return 1; - if(!store.writeEntry(pub).isNull()) + if(!store.writeEntry(pub).isEmpty()) printf("Entry written.\n"); else {