From dc1acef865f290e8d565f078fc78be69991b5c10 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Thu, 22 Dec 2011 14:58:19 +0100 Subject: [PATCH] Flesh out the API and implement stubs. * Modified the API greatly to be based on BKey* instead of BPassword*. * Added BKeyPurpose and used it instead of BKeyType. It is supposed to indicate the purpose of a key so that an app can look up keys on a more granular level. The BKeyType on the other hand actually identifies the type (i.e. subclass of BKey) so an app knows how to handle a given key or may only enumerate/use keys it is compatible with. * Made everything based on a raw data buffer for now, only BPasswordKey is implemented yet which stores the (0 terminated) string into that data buffer. * Removed the additional data BMessage as I don't yet see where it fits in. While I could imagine adding meta data to a key may be nice it might be an interoperability concern when keys are shared by different apps. * Moved the app functions to the keystore as per the TODO, but not sure how to actually implement them. --- headers/os/app/Key.h | 89 +++++++++++-------- headers/os/app/KeyStore.h | 55 +++++++----- src/kits/app/Key.cpp | 182 +++++++++++++++++++++----------------- src/kits/app/KeyStore.cpp | 111 +++++++++++++---------- 4 files changed, 247 insertions(+), 190 deletions(-) diff --git a/headers/os/app/Key.h b/headers/os/app/Key.h index e3139f4f01..d4c74af78d 100644 --- a/headers/os/app/Key.h +++ b/headers/os/app/Key.h @@ -12,44 +12,46 @@ #include -enum BPasswordType { - B_WEB_PASSWORD, - B_NETWORK_PASSWORD, - B_VOLUME_PASSWORD, - B_GENERIC_PASSWORD +enum BKeyPurpose { + B_KEY_PURPOSE_ANY, + B_KEY_PURPOSE_GENERIC, + B_KEY_PURPOSE_WEB, + B_KEY_PURPOSE_NETWORK, + B_KEY_PURPOSE_VOLUME +}; + + +enum BKeyType { + B_KEY_TYPE_ANY, + B_KEY_TYPE_GENERIC, + B_KEY_TYPE_PASSWORD, + B_KEY_TYPE_CERTIFICATE }; class BKey { public: BKey(); - BKey(BPasswordType type, + BKey(BKeyPurpose purpose, const char* identifier, - const char* password); - BKey(BPasswordType type, - const char* identifier, - const char* secondaryIdentifier, - const char* password); + const char* secondaryIdentifier = NULL, + const uint8* data = NULL, + size_t length = 0); BKey(BKey& other); virtual ~BKey(); + virtual BKeyType Type() const { return B_KEY_TYPE_GENERIC; }; + void Unset(); - status_t SetTo(BPasswordType type, + status_t SetTo(BKeyPurpose purpose, const char* identifier, - const char* password); - status_t SetTo(BPasswordType type, - const char* identifier, - const char* secondaryIdentifier, - const char* password); + const char* secondaryIdentifier = NULL, + const uint8* data = NULL, + size_t length = 0); - status_t SetPassword(const char* password); - const char* Password() const; - - status_t SetKey(const uint8* data, size_t length); - size_t KeyLength() const; - status_t GetKey(uint8* buffer, - size_t bufferLength) const; + void SetPurpose(BKeyPurpose purpose); + BKeyPurpose Purpose() const; void SetIdentifier(const char* identifier); const char* Identifier() const; @@ -57,37 +59,50 @@ public: void SetSecondaryIdentifier(const char* identifier); const char* SecondaryIdentifier() const; - void SetType(BPasswordType type); - BPasswordType Type() const; - - void SetData(const BMessage& data); - const BMessage& Data() const; + status_t SetData(const uint8* data, size_t length); + size_t DataLength() const; + const uint8* Data() const; + status_t GetData(uint8* buffer, size_t bufferSize) const; const char* Owner() const; bigtime_t CreationTime() const; bool IsRegistered() const; -// TODO: move to BKeyStore - status_t GetNextApplication(uint32& cookie, - BString& signature) const; - status_t RemoveApplication(const char* signature); - BKey& operator=(const BKey& other); bool operator==(const BKey& other) const; bool operator!=(const BKey& other) const; private: - mutable BMallocIO fPassword; + BKeyPurpose fPurpose; BString fIdentifier; BString fSecondaryIdentifier; BString fOwner; - BMessage fData; bigtime_t fCreationTime; - BPasswordType fType; + mutable BMallocIO fData; BObjectList fApplications; bool fRegistered; }; +class BPasswordKey : public BKey { +public: + BPasswordKey(); + BPasswordKey(const char* password, + BKeyPurpose purpose, const char* identifier, + const char* secondaryIdentifier = NULL); + BPasswordKey(BPasswordKey& other); + virtual ~BPasswordKey(); + + virtual BKeyType Type() const { return B_KEY_TYPE_PASSWORD; }; + + status_t SetTo(const char* password, + BKeyPurpose purpose, + const char* identifier, + const char* secondaryIdentifier = NULL); + + status_t SetPassword(const char* password); + const char* Password() const; +}; + #endif // _KEY_H diff --git a/headers/os/app/KeyStore.h b/headers/os/app/KeyStore.h index b09e2b17c8..83a86b27ff 100644 --- a/headers/os/app/KeyStore.h +++ b/headers/os/app/KeyStore.h @@ -16,46 +16,46 @@ public: // TODO: -> GetNextPassword() - there can always be more than one key // with the same identifier/secondaryIdentifier (ie. different username) - status_t GetPassword(BPasswordType type, + status_t GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier, BKey& key); - status_t GetPassword(BPasswordType type, + status_t GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier, const char* secondaryIdentifier, BKey& key); - status_t GetPassword(BPasswordType type, + status_t GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier, const char* secondaryIdentifier, bool secondaryIdentifierOptional, BKey& key); - status_t GetPassword(const char* keyring, - BPasswordType type, + status_t GetKey(const char* keyring, + BKeyType type, BKeyPurpose purpose, const char* identifier, BKey& key); - status_t GetPassword(const char* keyring, - BPasswordType type, + status_t GetKey(const char* keyring, + BKeyType type, BKeyPurpose purpose, const char* identifier, const char* secondaryIdentifier, BKey& key); - status_t GetPassword(const char* keyring, - BPasswordType type, + status_t GetKey(const char* keyring, + BKeyType type, BKeyPurpose purpose, const char* identifier, const char* secondaryIdentifier, bool secondaryIdentifierOptional, BKey& key); - status_t RegisterPassword(const BKey& key); - status_t RegisterPassword(const char* keyring, + status_t RegisterKey(const BKey& key); + status_t RegisterKey(const char* keyring, const BKey& key); - status_t UnregisterPassword(const BKey& key); - status_t UnregisterPassword(const char* keyring, + status_t UnregisterKey(const BKey& key); + status_t UnregisterKey(const char* keyring, const BKey& key); - status_t GetNextPassword(uint32& cookie, BKey& key); - status_t GetNextPassword(BPasswordType type, + status_t GetNextKey(uint32& cookie, BKey& key); + status_t GetNextKey(BKeyType type, BKeyPurpose purpose, uint32& cookie, BKey& key); - status_t GetNextPassword(const char* keyring, + status_t GetNextKey(const char* keyring, + uint32& cookie, BKey& key); + status_t GetNextKey(const char* keyring, + BKeyType type, BKeyPurpose purpose, uint32& cookie, BKey& key); - status_t GetNextPassword(const char* keyring, - BPasswordType type, uint32& cookie, - BKey& key); // Keyrings @@ -68,8 +68,8 @@ public: // Master key - status_t SetMasterPassword(const BKey& key); - status_t RemoveMasterPassword(); + status_t SetMasterKey(const BKey& key); + status_t RemoveMasterKey(); status_t AddKeyringToMaster(const char* keyring); status_t RemoveKeyringFromMaster(const char* keyring); @@ -83,11 +83,18 @@ public: status_t RevokeAccess(const char* keyring); status_t RevokeMasterAccess(); + // Applications + + status_t GetNextApplication(const BKey& key, + uint32& cookie, BString& signature) const; + status_t RemoveApplication(const BKey& key, + const char* signature); + // Service functions - status_t GeneratePassword(BKey& key, size_t length, - uint32 flags); - float PasswordStrength(const char* key); + status_t GeneratePassword(BPasswordKey& password, + size_t length, uint32 flags); + float PasswordStrength(const char* password); }; diff --git a/src/kits/app/Key.cpp b/src/kits/app/Key.cpp index 650bb88a1b..bfe187d963 100644 --- a/src/kits/app/Key.cpp +++ b/src/kits/app/Key.cpp @@ -22,7 +22,7 @@ CompareLists(BObjectList a, BObjectList b) } -// #pragma mark - +// #pragma mark - Generic BKey BKey::BKey() @@ -30,17 +30,10 @@ BKey::BKey() } -BKey::BKey(BPasswordType type, const char* identifier, - const char* password) +BKey::BKey(BKeyPurpose purpose, const char* identifier, + const char* secondaryIdentifier, const uint8* data, size_t length) { - SetTo(type, identifier, NULL, password); -} - - -BKey::BKey(BPasswordType type, const char* identifier, - const char* secondaryIdentifier, const char* password) -{ - SetTo(type, identifier, secondaryIdentifier, password); + SetTo(purpose, identifier, secondaryIdentifier, data, length); } @@ -55,65 +48,27 @@ BKey::~BKey() status_t -BKey::SetTo(BPasswordType type, const char* identifier, - const char* password) +BKey::SetTo(BKeyPurpose purpose, const char* identifier, + const char* secondaryIdentifier, const uint8* data, size_t length) { - return SetTo(type, identifier, NULL, password); -} - - -status_t -BKey::SetTo(BPasswordType type, const char* identifier, - const char* secondaryIdentifier, const char* password) -{ - SetType(type); + SetPurpose(purpose); SetIdentifier(identifier); SetSecondaryIdentifier(secondaryIdentifier); - return SetPassword(password); + return SetData(data, length); } -status_t -BKey::SetPassword(const char* password) +void +BKey::SetPurpose(BKeyPurpose purpose) { - return SetKey((const uint8*)password, strlen(password) + 1); + fPurpose = purpose; } -const char* -BKey::Password() const +BKeyPurpose +BKey::Purpose() const { - return (const char*)fPassword.Buffer(); -} - - -status_t -BKey::SetKey(const uint8* data, size_t length) -{ - fPassword.SetSize(0); - ssize_t bytesWritten = fPassword.WriteAt(0, data, length); - if (bytesWritten < 0) - return (status_t)bytesWritten; - - return (size_t)bytesWritten == length ? B_OK : B_NO_MEMORY; -} - - -size_t -BKey::KeyLength() const -{ - return fPassword.BufferLength(); -} - - -status_t -BKey::GetKey(uint8* buffer, size_t bufferLength) const -{ - ssize_t bytesRead = fPassword.ReadAt(0, buffer, bufferLength); - if (bytesRead < 0) - return (status_t)bytesRead; - - return B_OK; + return fPurpose; } @@ -145,34 +100,44 @@ BKey::SecondaryIdentifier() const } -void -BKey::SetType(BPasswordType type) +status_t +BKey::SetData(const uint8* data, size_t length) { - fType = type; + fData.SetSize(0); + ssize_t bytesWritten = fData.WriteAt(0, data, length); + if (bytesWritten < 0) + return (status_t)bytesWritten; + + return (size_t)bytesWritten == length ? B_OK : B_NO_MEMORY; } -BPasswordType -BKey::Type() const +size_t +BKey::DataLength() const { - return fType; + return fData.BufferLength(); } -void -BKey::SetData(const BMessage& data) -{ - fData = data; -} - - -const BMessage& +const uint8* BKey::Data() const { - return fData; + return (const uint8*)fData.Buffer(); } +status_t +BKey::GetData(uint8* buffer, size_t bufferSize) const +{ + ssize_t bytesRead = fData.ReadAt(0, buffer, bufferSize); + if (bytesRead < 0) + return (status_t)bytesRead; + + return B_OK; +} + + + const char* BKey::Owner() const { @@ -194,6 +159,8 @@ BKey::IsRegistered() const } +#if 0 +// To be moved to BKeyStore status_t BKey::GetNextApplication(uint32& cookie, BString& signature) const { @@ -215,16 +182,17 @@ BKey::RemoveApplication(const char* signature) return B_OK; } } + return B_ENTRY_NOT_FOUND; } +#endif BKey& BKey::operator=(const BKey& other) { - SetKey((const uint8*)other.Password(), other.KeyLength()); - SetType(other.Type()); - SetData(other.Data()); + SetPurpose(other.Purpose()); + SetData((const uint8*)other.Data(), other.DataLength()); fIdentifier = other.fIdentifier; fSecondaryIdentifier = other.fSecondaryIdentifier; @@ -240,13 +208,13 @@ BKey::operator=(const BKey& other) bool BKey::operator==(const BKey& other) const { - return KeyLength() == other.KeyLength() + return Type() == other.Type() + && DataLength() == other.DataLength() + && Purpose() == other.Purpose() + && fOwner == other.fOwner && fIdentifier == other.fIdentifier && fSecondaryIdentifier == other.fSecondaryIdentifier - && !memcmp(Password(), other.Password(), KeyLength()) - && fOwner == other.fOwner - && Data().HasSameData(other.Data()) - && Type() == other.Type() + && memcmp(Data(), other.Data(), DataLength()) == 0 && CompareLists(fApplications, other.fApplications); } @@ -256,3 +224,53 @@ BKey::operator!=(const BKey& other) const { return !(*this == other); } + + +// #pragma mark - BPasswordKey + + +BPasswordKey::BPasswordKey() +{ +} + + +BPasswordKey::BPasswordKey(const char* password, BKeyPurpose purpose, + const char* identifier, const char* secondaryIdentifier) + : + BKey(purpose, identifier, secondaryIdentifier, (const uint8*)password, + strlen(password) + 1) +{ +} + + +BPasswordKey::BPasswordKey(BPasswordKey& other) +{ +} + + +BPasswordKey::~BPasswordKey() +{ +} + + +status_t +BPasswordKey::SetTo(const char* password, BKeyPurpose purpose, + const char* identifier, const char* secondaryIdentifier) +{ + return BKey::SetTo(purpose, identifier, secondaryIdentifier, + (const uint8*)password, strlen(password) + 1); +} + + +status_t +BPasswordKey::SetPassword(const char* password) +{ + return SetData((const uint8*)password, strlen(password) + 1); +} + + +const char* +BPasswordKey::Password() const +{ + return (const char*)Data(); +} diff --git a/src/kits/app/KeyStore.cpp b/src/kits/app/KeyStore.cpp index 190b075514..c8417a2106 100644 --- a/src/kits/app/KeyStore.cpp +++ b/src/kits/app/KeyStore.cpp @@ -17,117 +17,115 @@ BKeyStore::~BKeyStore() } -// #pragma mark - Passwords +// #pragma mark - Key handling status_t -BKeyStore::GetPassword(BPasswordType type, const char* identifier, - BKey& password) +BKeyStore::GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier, + BKey& key) { - return GetPassword(NULL, type, identifier, NULL, true, password); + return GetKey(NULL, type, purpose, identifier, NULL, true, key); } status_t -BKeyStore::GetPassword(BPasswordType type, const char* identifier, - const char* secondaryIdentifier, BKey& password) +BKeyStore::GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier, + const char* secondaryIdentifier, BKey& key) { - return GetPassword(NULL, type, identifier, secondaryIdentifier, true, - password); + return GetKey(NULL, type, purpose, identifier, secondaryIdentifier, true, + key); } status_t -BKeyStore::GetPassword(BPasswordType type, const char* identifier, +BKeyStore::GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier, const char* secondaryIdentifier, bool secondaryIdentifierOptional, - BKey& password) + BKey& key) { - return GetPassword(NULL, type, identifier, secondaryIdentifier, - secondaryIdentifierOptional, password); + return GetKey(NULL, type, purpose, identifier, secondaryIdentifier, + secondaryIdentifierOptional, key); } status_t -BKeyStore::GetPassword(const char* keyring, BPasswordType type, - const char* identifier, BKey& password) +BKeyStore::GetKey(const char* keyring, BKeyType type, BKeyPurpose purpose, + const char* identifier, BKey& key) { - return GetPassword(keyring, type, identifier, NULL, true, password); + return GetKey(keyring, type, purpose, identifier, NULL, true, key); } status_t -BKeyStore::GetPassword(const char* keyring, BPasswordType type, +BKeyStore::GetKey(const char* keyring, BKeyType type, BKeyPurpose purpose, + const char* identifier, const char* secondaryIdentifier, BKey& key) +{ + return GetKey(keyring, type, purpose, identifier, secondaryIdentifier, true, + key); +} + + +status_t +BKeyStore::GetKey(const char* keyring, BKeyType type, BKeyPurpose purpose, const char* identifier, const char* secondaryIdentifier, - BKey& password) -{ - return GetPassword(keyring, type, identifier, secondaryIdentifier, true, - password); -} - - -status_t -BKeyStore::GetPassword(const char* keyring, BPasswordType type, - const char* identifier, const char* secondaryIdentifier, - bool secondaryIdentifierOptional, BKey& password) + bool secondaryIdentifierOptional, BKey& key) { return B_ERROR; } status_t -BKeyStore::RegisterPassword(const BKey& password) +BKeyStore::RegisterKey(const BKey& key) { - return RegisterPassword(NULL, password); + return RegisterKey(NULL, key); } status_t -BKeyStore::RegisterPassword(const char* keyring, const BKey& password) +BKeyStore::RegisterKey(const char* keyring, const BKey& key) { return B_ERROR; } status_t -BKeyStore::UnregisterPassword(const BKey& password) +BKeyStore::UnregisterKey(const BKey& key) { - return UnregisterPassword(NULL, password); + return UnregisterKey(NULL, key); } status_t -BKeyStore::UnregisterPassword(const char* keyring, const BKey& password) +BKeyStore::UnregisterKey(const char* keyring, const BKey& key) { return B_ERROR; } status_t -BKeyStore::GetNextPassword(uint32& cookie, BKey& password) +BKeyStore::GetNextKey(uint32& cookie, BKey& key) { - return GetNextPassword(NULL, cookie, password); + return GetNextKey(NULL, cookie, key); } status_t -BKeyStore::GetNextPassword(BPasswordType type, uint32& cookie, - BKey& password) +BKeyStore::GetNextKey(BKeyType type, BKeyPurpose purpose, uint32& cookie, + BKey& key) { - return GetNextPassword(NULL, type, cookie, password); + return GetNextKey(NULL, type, purpose, cookie, key); } status_t -BKeyStore::GetNextPassword(const char* keyring, uint32& cookie, - BKey& password) +BKeyStore::GetNextKey(const char* keyring, uint32& cookie, BKey& key) { return B_ERROR; } status_t -BKeyStore::GetNextPassword(const char* keyring, - BPasswordType type, uint32& cookie, BKey& password) +BKeyStore::GetNextKey(const char* keyring, BKeyType type, BKeyPurpose purpose, + uint32& cookie, BKey& key) { return B_ERROR; } @@ -137,7 +135,7 @@ BKeyStore::GetNextPassword(const char* keyring, status_t -BKeyStore::RegisterKeyring(const char* keyring, const BKey& password) +BKeyStore::RegisterKeyring(const char* keyring, const BKey& key) { return B_ERROR; } @@ -157,18 +155,18 @@ BKeyStore::GetNextKeyring(uint32& cookie, BString& keyring) } -// #pragma mark - Master password +// #pragma mark - Master key status_t -BKeyStore::SetMasterPassword(const BKey& password) +BKeyStore::SetMasterKey(const BKey& key) { return B_ERROR; } status_t -BKeyStore::RemoveMasterPassword() +BKeyStore::RemoveMasterKey() { return B_ERROR; } @@ -219,11 +217,30 @@ BKeyStore::RevokeMasterAccess() } + +// #pragma mark - Applications + + +status_t +BKeyStore::GetNextApplication(const BKey& key, uint32& cookie, + BString& signature) const +{ + return B_ERROR; +} + + +status_t +BKeyStore::RemoveApplication(const BKey& key, const char* signature) +{ + return B_ERROR; +} + + // #pragma mark - Service functions status_t -BKeyStore::GeneratePassword(BKey& password, size_t length, uint32 flags) +BKeyStore::GeneratePassword(BPasswordKey& password, size_t length, uint32 flags) { return B_ERROR; }