diff --git a/headers/os/app/Key.h b/headers/os/app/Key.h new file mode 100644 index 0000000000..e3139f4f01 --- /dev/null +++ b/headers/os/app/Key.h @@ -0,0 +1,93 @@ +/* + * Copyright 2011, Haiku, Inc. + * Distributed under the terms of the MIT License. + */ +#ifndef _KEY_H +#define _KEY_H + + +#include +#include +#include +#include + + +enum BPasswordType { + B_WEB_PASSWORD, + B_NETWORK_PASSWORD, + B_VOLUME_PASSWORD, + B_GENERIC_PASSWORD +}; + + +class BKey { +public: + BKey(); + BKey(BPasswordType type, + const char* identifier, + const char* password); + BKey(BPasswordType type, + const char* identifier, + const char* secondaryIdentifier, + const char* password); + BKey(BKey& other); + virtual ~BKey(); + + void Unset(); + + status_t SetTo(BPasswordType type, + const char* identifier, + const char* password); + status_t SetTo(BPasswordType type, + const char* identifier, + const char* secondaryIdentifier, + const char* password); + + 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 SetIdentifier(const char* identifier); + const char* Identifier() const; + + 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; + + 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; + BString fIdentifier; + BString fSecondaryIdentifier; + BString fOwner; + BMessage fData; + bigtime_t fCreationTime; + BPasswordType fType; + BObjectList fApplications; + bool fRegistered; +}; + + +#endif // _KEY_H diff --git a/headers/os/app/KeyStore.h b/headers/os/app/KeyStore.h new file mode 100644 index 0000000000..b09e2b17c8 --- /dev/null +++ b/headers/os/app/KeyStore.h @@ -0,0 +1,94 @@ +/* + * Copyright 2011, Haiku, Inc. + * Distributed under the terms of the MIT License. + */ +#ifndef _KEY_STORE_H +#define _KEY_STORE_H + + +#include + + +class BKeyStore { +public: + BKeyStore(); + virtual ~BKeyStore(); + +// TODO: -> GetNextPassword() - there can always be more than one key +// with the same identifier/secondaryIdentifier (ie. different username) + status_t GetPassword(BPasswordType type, + const char* identifier, BKey& key); + status_t GetPassword(BPasswordType type, + const char* identifier, + const char* secondaryIdentifier, BKey& key); + status_t GetPassword(BPasswordType type, + const char* identifier, + const char* secondaryIdentifier, + bool secondaryIdentifierOptional, + BKey& key); + + status_t GetPassword(const char* keyring, + BPasswordType type, + const char* identifier, BKey& key); + status_t GetPassword(const char* keyring, + BPasswordType type, + const char* identifier, + const char* secondaryIdentifier, BKey& key); + status_t GetPassword(const char* keyring, + BPasswordType type, + const char* identifier, + const char* secondaryIdentifier, + bool secondaryIdentifierOptional, + BKey& key); + + status_t RegisterPassword(const BKey& key); + status_t RegisterPassword(const char* keyring, + const BKey& key); + status_t UnregisterPassword(const BKey& key); + status_t UnregisterPassword(const char* keyring, + const BKey& key); + + status_t GetNextPassword(uint32& cookie, BKey& key); + status_t GetNextPassword(BPasswordType type, + uint32& cookie, BKey& key); + status_t GetNextPassword(const char* keyring, + uint32& cookie, BKey& key); + status_t GetNextPassword(const char* keyring, + BPasswordType type, uint32& cookie, + BKey& key); + + // Keyrings + + status_t RegisterKeyring(const char* keyring, + const BKey& key); + status_t UnregisterKeyring(const char* keyring); + + status_t GetNextKeyring(uint32& cookie, + BString& keyring); + + // Master key + + status_t SetMasterPassword(const BKey& key); + status_t RemoveMasterPassword(); + + status_t AddKeyringToMaster(const char* keyring); + status_t RemoveKeyringFromMaster(const char* keyring); + + status_t GetNextMasterKeyring(uint32& cookie, + BString& keyring); + + // Access + + bool IsKeyringAccessible(const char* keyring); + status_t RevokeAccess(const char* keyring); + status_t RevokeMasterAccess(); + + // Service functions + + status_t GeneratePassword(BKey& key, size_t length, + uint32 flags); + float PasswordStrength(const char* key); +}; + + +#endif // _KEY_STORE_H diff --git a/src/kits/app/Jamfile b/src/kits/app/Jamfile index 1aec0536d7..85e267bd9e 100644 --- a/src/kits/app/Jamfile +++ b/src/kits/app/Jamfile @@ -57,5 +57,9 @@ MergeObject app_kit.o : ServerMemoryAllocator.cpp TokenSpace.cpp TypeConstants.cpp + + # KeyStore implementation + Key.cpp + KeyStore.cpp ; diff --git a/src/kits/app/Key.cpp b/src/kits/app/Key.cpp new file mode 100644 index 0000000000..650bb88a1b --- /dev/null +++ b/src/kits/app/Key.cpp @@ -0,0 +1,258 @@ +/* + * Copyright 2011, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include + + +static bool +CompareLists(BObjectList a, BObjectList b) +{ + if (a.CountItems() != b.CountItems()) + return false; + + for (int32 i = 0; i < a.CountItems(); i++) { + if (*a.ItemAt(i) != *b.ItemAt(i)) + return false; + } + + return true; +} + + +// #pragma mark - + + +BKey::BKey() +{ +} + + +BKey::BKey(BPasswordType type, const char* identifier, + const char* password) +{ + SetTo(type, identifier, NULL, password); +} + + +BKey::BKey(BPasswordType type, const char* identifier, + const char* secondaryIdentifier, const char* password) +{ + SetTo(type, identifier, secondaryIdentifier, password); +} + + +BKey::BKey(BKey& other) +{ +} + + +BKey::~BKey() +{ +} + + +status_t +BKey::SetTo(BPasswordType type, const char* identifier, + const char* password) +{ + return SetTo(type, identifier, NULL, password); +} + + +status_t +BKey::SetTo(BPasswordType type, const char* identifier, + const char* secondaryIdentifier, const char* password) +{ + SetType(type); + SetIdentifier(identifier); + SetSecondaryIdentifier(secondaryIdentifier); + return SetPassword(password); +} + + +status_t +BKey::SetPassword(const char* password) +{ + return SetKey((const uint8*)password, strlen(password) + 1); +} + + +const char* +BKey::Password() 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; +} + + +void +BKey::SetIdentifier(const char* identifier) +{ + fIdentifier = identifier; +} + + +const char* +BKey::Identifier() const +{ + return fIdentifier.String(); +} + + +void +BKey::SetSecondaryIdentifier(const char* identifier) +{ + fSecondaryIdentifier = identifier; +} + + +const char* +BKey::SecondaryIdentifier() const +{ + return fSecondaryIdentifier.String(); +} + + +void +BKey::SetType(BPasswordType type) +{ + fType = type; +} + + +BPasswordType +BKey::Type() const +{ + return fType; +} + + +void +BKey::SetData(const BMessage& data) +{ + fData = data; +} + + +const BMessage& +BKey::Data() const +{ + return fData; +} + + +const char* +BKey::Owner() const +{ + return fOwner.String(); +} + + +bigtime_t +BKey::CreationTime() const +{ + return fCreationTime; +} + + +bool +BKey::IsRegistered() const +{ + return fRegistered; +} + + +status_t +BKey::GetNextApplication(uint32& cookie, BString& signature) const +{ + BString* item = fApplications.ItemAt(cookie++); + if (item == NULL) + return B_ENTRY_NOT_FOUND; + + signature = *item; + return B_OK; +} + + +status_t +BKey::RemoveApplication(const char* signature) +{ + for (int32 i = 0; i < fApplications.CountItems(); i++) { + if (*fApplications.ItemAt(i) == signature) { + fApplications.RemoveItemAt(i); + return B_OK; + } + } + return B_ENTRY_NOT_FOUND; +} + + +BKey& +BKey::operator=(const BKey& other) +{ + SetKey((const uint8*)other.Password(), other.KeyLength()); + SetType(other.Type()); + SetData(other.Data()); + + fIdentifier = other.fIdentifier; + fSecondaryIdentifier = other.fSecondaryIdentifier; + fOwner = other.fOwner; + fCreationTime = other.CreationTime(); + fRegistered = other.IsRegistered(); + fApplications = other.fApplications; + + return *this; +} + + +bool +BKey::operator==(const BKey& other) const +{ + return KeyLength() == other.KeyLength() + && fIdentifier == other.fIdentifier + && fSecondaryIdentifier == other.fSecondaryIdentifier + && !memcmp(Password(), other.Password(), KeyLength()) + && fOwner == other.fOwner + && Data().HasSameData(other.Data()) + && Type() == other.Type() + && CompareLists(fApplications, other.fApplications); +} + + +bool +BKey::operator!=(const BKey& other) const +{ + return !(*this == other); +} diff --git a/src/kits/app/KeyStore.cpp b/src/kits/app/KeyStore.cpp new file mode 100644 index 0000000000..190b075514 --- /dev/null +++ b/src/kits/app/KeyStore.cpp @@ -0,0 +1,236 @@ +/* + * Copyright 2011, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include + + +BKeyStore::BKeyStore() +{ +} + + +BKeyStore::~BKeyStore() +{ +} + + +// #pragma mark - Passwords + + +status_t +BKeyStore::GetPassword(BPasswordType type, const char* identifier, + BKey& password) +{ + return GetPassword(NULL, type, identifier, NULL, true, password); +} + + +status_t +BKeyStore::GetPassword(BPasswordType type, const char* identifier, + const char* secondaryIdentifier, BKey& password) +{ + return GetPassword(NULL, type, identifier, secondaryIdentifier, true, + password); +} + + +status_t +BKeyStore::GetPassword(BPasswordType type, const char* identifier, + const char* secondaryIdentifier, bool secondaryIdentifierOptional, + BKey& password) +{ + return GetPassword(NULL, type, identifier, secondaryIdentifier, + secondaryIdentifierOptional, password); +} + + +status_t +BKeyStore::GetPassword(const char* keyring, BPasswordType type, + const char* identifier, BKey& password) +{ + return GetPassword(keyring, type, identifier, NULL, true, password); +} + + +status_t +BKeyStore::GetPassword(const char* keyring, BPasswordType type, + 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) +{ + return B_ERROR; +} + + +status_t +BKeyStore::RegisterPassword(const BKey& password) +{ + return RegisterPassword(NULL, password); +} + + +status_t +BKeyStore::RegisterPassword(const char* keyring, const BKey& password) +{ + return B_ERROR; +} + + +status_t +BKeyStore::UnregisterPassword(const BKey& password) +{ + return UnregisterPassword(NULL, password); +} + + +status_t +BKeyStore::UnregisterPassword(const char* keyring, const BKey& password) +{ + return B_ERROR; +} + + +status_t +BKeyStore::GetNextPassword(uint32& cookie, BKey& password) +{ + return GetNextPassword(NULL, cookie, password); +} + + +status_t +BKeyStore::GetNextPassword(BPasswordType type, uint32& cookie, + BKey& password) +{ + return GetNextPassword(NULL, type, cookie, password); +} + + +status_t +BKeyStore::GetNextPassword(const char* keyring, uint32& cookie, + BKey& password) +{ + return B_ERROR; +} + + +status_t +BKeyStore::GetNextPassword(const char* keyring, + BPasswordType type, uint32& cookie, BKey& password) +{ + return B_ERROR; +} + + +// #pragma mark - Keyrings + + +status_t +BKeyStore::RegisterKeyring(const char* keyring, const BKey& password) +{ + return B_ERROR; +} + + +status_t +BKeyStore::UnregisterKeyring(const char* keyring) +{ + return B_ERROR; +} + + +status_t +BKeyStore::GetNextKeyring(uint32& cookie, BString& keyring) +{ + return B_ERROR; +} + + +// #pragma mark - Master password + + +status_t +BKeyStore::SetMasterPassword(const BKey& password) +{ + return B_ERROR; +} + + +status_t +BKeyStore::RemoveMasterPassword() +{ + return B_ERROR; +} + + +status_t +BKeyStore::AddKeyringToMaster(const char* keyring) +{ + return B_ERROR; +} + + +status_t +BKeyStore::RemoveKeyringFromMaster(const char* keyring) +{ + return B_ERROR; +} + + +status_t +BKeyStore::GetNextMasterKeyring(uint32& cookie, BString& keyring) +{ + return B_ERROR; +} + + +// #pragma mark - Access + + +bool +BKeyStore::IsKeyringAccessible(const char* keyring) +{ + return false; +} + + +status_t +BKeyStore::RevokeAccess(const char* keyring) +{ + return B_ERROR; +} + + +status_t +BKeyStore::RevokeMasterAccess() +{ + return B_ERROR; +} + + +// #pragma mark - Service functions + + +status_t +BKeyStore::GeneratePassword(BKey& password, size_t length, uint32 flags) +{ + return B_ERROR; +} + + +float +BKeyStore::PasswordStrength(const char* password) +{ + return 0; +}