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.
This commit is contained in:
Michael Lotz
2013-03-05 10:59:43 -05:00
committed by Ryan Leavengood
parent 3b3884d9ee
commit dc1acef865
4 changed files with 247 additions and 190 deletions
+52 -37
View File
@@ -12,44 +12,46 @@
#include <String.h> #include <String.h>
enum BPasswordType { enum BKeyPurpose {
B_WEB_PASSWORD, B_KEY_PURPOSE_ANY,
B_NETWORK_PASSWORD, B_KEY_PURPOSE_GENERIC,
B_VOLUME_PASSWORD, B_KEY_PURPOSE_WEB,
B_GENERIC_PASSWORD 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 { class BKey {
public: public:
BKey(); BKey();
BKey(BPasswordType type, BKey(BKeyPurpose purpose,
const char* identifier, const char* identifier,
const char* password); const char* secondaryIdentifier = NULL,
BKey(BPasswordType type, const uint8* data = NULL,
const char* identifier, size_t length = 0);
const char* secondaryIdentifier,
const char* password);
BKey(BKey& other); BKey(BKey& other);
virtual ~BKey(); virtual ~BKey();
virtual BKeyType Type() const { return B_KEY_TYPE_GENERIC; };
void Unset(); void Unset();
status_t SetTo(BPasswordType type, status_t SetTo(BKeyPurpose purpose,
const char* identifier, const char* identifier,
const char* password); const char* secondaryIdentifier = NULL,
status_t SetTo(BPasswordType type, const uint8* data = NULL,
const char* identifier, size_t length = 0);
const char* secondaryIdentifier,
const char* password);
status_t SetPassword(const char* password); void SetPurpose(BKeyPurpose purpose);
const char* Password() const; BKeyPurpose Purpose() 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); void SetIdentifier(const char* identifier);
const char* Identifier() const; const char* Identifier() const;
@@ -57,37 +59,50 @@ public:
void SetSecondaryIdentifier(const char* identifier); void SetSecondaryIdentifier(const char* identifier);
const char* SecondaryIdentifier() const; const char* SecondaryIdentifier() const;
void SetType(BPasswordType type); status_t SetData(const uint8* data, size_t length);
BPasswordType Type() const; size_t DataLength() const;
const uint8* Data() const;
void SetData(const BMessage& data); status_t GetData(uint8* buffer, size_t bufferSize) const;
const BMessage& Data() const;
const char* Owner() const; const char* Owner() const;
bigtime_t CreationTime() const; bigtime_t CreationTime() const;
bool IsRegistered() 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); BKey& operator=(const BKey& other);
bool operator==(const BKey& other) const; bool operator==(const BKey& other) const;
bool operator!=(const BKey& other) const; bool operator!=(const BKey& other) const;
private: private:
mutable BMallocIO fPassword; BKeyPurpose fPurpose;
BString fIdentifier; BString fIdentifier;
BString fSecondaryIdentifier; BString fSecondaryIdentifier;
BString fOwner; BString fOwner;
BMessage fData;
bigtime_t fCreationTime; bigtime_t fCreationTime;
BPasswordType fType; mutable BMallocIO fData;
BObjectList<BString> fApplications; BObjectList<BString> fApplications;
bool fRegistered; 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 #endif // _KEY_H
+31 -24
View File
@@ -16,46 +16,46 @@ public:
// TODO: -> GetNextPassword() - there can always be more than one key // TODO: -> GetNextPassword() - there can always be more than one key
// with the same identifier/secondaryIdentifier (ie. different username) // 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); const char* identifier, BKey& key);
status_t GetPassword(BPasswordType type, status_t GetKey(BKeyType type, BKeyPurpose purpose,
const char* identifier, const char* identifier,
const char* secondaryIdentifier, BKey& key); const char* secondaryIdentifier, BKey& key);
status_t GetPassword(BPasswordType type, status_t GetKey(BKeyType type, BKeyPurpose purpose,
const char* identifier, const char* identifier,
const char* secondaryIdentifier, const char* secondaryIdentifier,
bool secondaryIdentifierOptional, bool secondaryIdentifierOptional,
BKey& key); BKey& key);
status_t GetPassword(const char* keyring, status_t GetKey(const char* keyring,
BPasswordType type, BKeyType type, BKeyPurpose purpose,
const char* identifier, BKey& key); const char* identifier, BKey& key);
status_t GetPassword(const char* keyring, status_t GetKey(const char* keyring,
BPasswordType type, BKeyType type, BKeyPurpose purpose,
const char* identifier, const char* identifier,
const char* secondaryIdentifier, BKey& key); const char* secondaryIdentifier, BKey& key);
status_t GetPassword(const char* keyring, status_t GetKey(const char* keyring,
BPasswordType type, BKeyType type, BKeyPurpose purpose,
const char* identifier, const char* identifier,
const char* secondaryIdentifier, const char* secondaryIdentifier,
bool secondaryIdentifierOptional, bool secondaryIdentifierOptional,
BKey& key); BKey& key);
status_t RegisterPassword(const BKey& key); status_t RegisterKey(const BKey& key);
status_t RegisterPassword(const char* keyring, status_t RegisterKey(const char* keyring,
const BKey& key); const BKey& key);
status_t UnregisterPassword(const BKey& key); status_t UnregisterKey(const BKey& key);
status_t UnregisterPassword(const char* keyring, status_t UnregisterKey(const char* keyring,
const BKey& key); const BKey& key);
status_t GetNextPassword(uint32& cookie, BKey& key); status_t GetNextKey(uint32& cookie, BKey& key);
status_t GetNextPassword(BPasswordType type, status_t GetNextKey(BKeyType type, BKeyPurpose purpose,
uint32& cookie, BKey& key); 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); uint32& cookie, BKey& key);
status_t GetNextPassword(const char* keyring,
BPasswordType type, uint32& cookie,
BKey& key);
// Keyrings // Keyrings
@@ -68,8 +68,8 @@ public:
// Master key // Master key
status_t SetMasterPassword(const BKey& key); status_t SetMasterKey(const BKey& key);
status_t RemoveMasterPassword(); status_t RemoveMasterKey();
status_t AddKeyringToMaster(const char* keyring); status_t AddKeyringToMaster(const char* keyring);
status_t RemoveKeyringFromMaster(const char* keyring); status_t RemoveKeyringFromMaster(const char* keyring);
@@ -83,11 +83,18 @@ public:
status_t RevokeAccess(const char* keyring); status_t RevokeAccess(const char* keyring);
status_t RevokeMasterAccess(); 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 // Service functions
status_t GeneratePassword(BKey& key, size_t length, status_t GeneratePassword(BPasswordKey& password,
uint32 flags); size_t length, uint32 flags);
float PasswordStrength(const char* key); float PasswordStrength(const char* password);
}; };
+100 -82
View File
@@ -22,7 +22,7 @@ CompareLists(BObjectList<BString> a, BObjectList<BString> b)
} }
// #pragma mark - // #pragma mark - Generic BKey
BKey::BKey() BKey::BKey()
@@ -30,17 +30,10 @@ BKey::BKey()
} }
BKey::BKey(BPasswordType type, const char* identifier, BKey::BKey(BKeyPurpose purpose, const char* identifier,
const char* password) const char* secondaryIdentifier, const uint8* data, size_t length)
{ {
SetTo(type, identifier, NULL, password); SetTo(purpose, identifier, secondaryIdentifier, data, length);
}
BKey::BKey(BPasswordType type, const char* identifier,
const char* secondaryIdentifier, const char* password)
{
SetTo(type, identifier, secondaryIdentifier, password);
} }
@@ -55,65 +48,27 @@ BKey::~BKey()
status_t status_t
BKey::SetTo(BPasswordType type, const char* identifier, BKey::SetTo(BKeyPurpose purpose, const char* identifier,
const char* password) const char* secondaryIdentifier, const uint8* data, size_t length)
{ {
return SetTo(type, identifier, NULL, password); SetPurpose(purpose);
}
status_t
BKey::SetTo(BPasswordType type, const char* identifier,
const char* secondaryIdentifier, const char* password)
{
SetType(type);
SetIdentifier(identifier); SetIdentifier(identifier);
SetSecondaryIdentifier(secondaryIdentifier); SetSecondaryIdentifier(secondaryIdentifier);
return SetPassword(password); return SetData(data, length);
} }
status_t void
BKey::SetPassword(const char* password) BKey::SetPurpose(BKeyPurpose purpose)
{ {
return SetKey((const uint8*)password, strlen(password) + 1); fPurpose = purpose;
} }
const char* BKeyPurpose
BKey::Password() const BKey::Purpose() const
{ {
return (const char*)fPassword.Buffer(); return fPurpose;
}
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;
} }
@@ -145,34 +100,44 @@ BKey::SecondaryIdentifier() const
} }
void status_t
BKey::SetType(BPasswordType type) 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 size_t
BKey::Type() const BKey::DataLength() const
{ {
return fType; return fData.BufferLength();
} }
void const uint8*
BKey::SetData(const BMessage& data)
{
fData = data;
}
const BMessage&
BKey::Data() const 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* const char*
BKey::Owner() const BKey::Owner() const
{ {
@@ -194,6 +159,8 @@ BKey::IsRegistered() const
} }
#if 0
// To be moved to BKeyStore
status_t status_t
BKey::GetNextApplication(uint32& cookie, BString& signature) const BKey::GetNextApplication(uint32& cookie, BString& signature) const
{ {
@@ -215,16 +182,17 @@ BKey::RemoveApplication(const char* signature)
return B_OK; return B_OK;
} }
} }
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
} }
#endif
BKey& BKey&
BKey::operator=(const BKey& other) BKey::operator=(const BKey& other)
{ {
SetKey((const uint8*)other.Password(), other.KeyLength()); SetPurpose(other.Purpose());
SetType(other.Type()); SetData((const uint8*)other.Data(), other.DataLength());
SetData(other.Data());
fIdentifier = other.fIdentifier; fIdentifier = other.fIdentifier;
fSecondaryIdentifier = other.fSecondaryIdentifier; fSecondaryIdentifier = other.fSecondaryIdentifier;
@@ -240,13 +208,13 @@ BKey::operator=(const BKey& other)
bool bool
BKey::operator==(const BKey& other) const 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 && fIdentifier == other.fIdentifier
&& fSecondaryIdentifier == other.fSecondaryIdentifier && fSecondaryIdentifier == other.fSecondaryIdentifier
&& !memcmp(Password(), other.Password(), KeyLength()) && memcmp(Data(), other.Data(), DataLength()) == 0
&& fOwner == other.fOwner
&& Data().HasSameData(other.Data())
&& Type() == other.Type()
&& CompareLists(fApplications, other.fApplications); && CompareLists(fApplications, other.fApplications);
} }
@@ -256,3 +224,53 @@ BKey::operator!=(const BKey& other) const
{ {
return !(*this == other); 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();
}
+64 -47
View File
@@ -17,117 +17,115 @@ BKeyStore::~BKeyStore()
} }
// #pragma mark - Passwords // #pragma mark - Key handling
status_t status_t
BKeyStore::GetPassword(BPasswordType type, const char* identifier, BKeyStore::GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier,
BKey& password) BKey& key)
{ {
return GetPassword(NULL, type, identifier, NULL, true, password); return GetKey(NULL, type, purpose, identifier, NULL, true, key);
} }
status_t status_t
BKeyStore::GetPassword(BPasswordType type, const char* identifier, BKeyStore::GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier,
const char* secondaryIdentifier, BKey& password) const char* secondaryIdentifier, BKey& key)
{ {
return GetPassword(NULL, type, identifier, secondaryIdentifier, true, return GetKey(NULL, type, purpose, identifier, secondaryIdentifier, true,
password); key);
} }
status_t status_t
BKeyStore::GetPassword(BPasswordType type, const char* identifier, BKeyStore::GetKey(BKeyType type, BKeyPurpose purpose, const char* identifier,
const char* secondaryIdentifier, bool secondaryIdentifierOptional, const char* secondaryIdentifier, bool secondaryIdentifierOptional,
BKey& password) BKey& key)
{ {
return GetPassword(NULL, type, identifier, secondaryIdentifier, return GetKey(NULL, type, purpose, identifier, secondaryIdentifier,
secondaryIdentifierOptional, password); secondaryIdentifierOptional, key);
} }
status_t status_t
BKeyStore::GetPassword(const char* keyring, BPasswordType type, BKeyStore::GetKey(const char* keyring, BKeyType type, BKeyPurpose purpose,
const char* identifier, BKey& password) const char* identifier, BKey& key)
{ {
return GetPassword(keyring, type, identifier, NULL, true, password); return GetKey(keyring, type, purpose, identifier, NULL, true, key);
} }
status_t 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, const char* identifier, const char* secondaryIdentifier,
BKey& password) bool secondaryIdentifierOptional, BKey& key)
{
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; return B_ERROR;
} }
status_t status_t
BKeyStore::RegisterPassword(const BKey& password) BKeyStore::RegisterKey(const BKey& key)
{ {
return RegisterPassword(NULL, password); return RegisterKey(NULL, key);
} }
status_t status_t
BKeyStore::RegisterPassword(const char* keyring, const BKey& password) BKeyStore::RegisterKey(const char* keyring, const BKey& key)
{ {
return B_ERROR; return B_ERROR;
} }
status_t status_t
BKeyStore::UnregisterPassword(const BKey& password) BKeyStore::UnregisterKey(const BKey& key)
{ {
return UnregisterPassword(NULL, password); return UnregisterKey(NULL, key);
} }
status_t status_t
BKeyStore::UnregisterPassword(const char* keyring, const BKey& password) BKeyStore::UnregisterKey(const char* keyring, const BKey& key)
{ {
return B_ERROR; return B_ERROR;
} }
status_t 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 status_t
BKeyStore::GetNextPassword(BPasswordType type, uint32& cookie, BKeyStore::GetNextKey(BKeyType type, BKeyPurpose purpose, uint32& cookie,
BKey& password) BKey& key)
{ {
return GetNextPassword(NULL, type, cookie, password); return GetNextKey(NULL, type, purpose, cookie, key);
} }
status_t status_t
BKeyStore::GetNextPassword(const char* keyring, uint32& cookie, BKeyStore::GetNextKey(const char* keyring, uint32& cookie, BKey& key)
BKey& password)
{ {
return B_ERROR; return B_ERROR;
} }
status_t status_t
BKeyStore::GetNextPassword(const char* keyring, BKeyStore::GetNextKey(const char* keyring, BKeyType type, BKeyPurpose purpose,
BPasswordType type, uint32& cookie, BKey& password) uint32& cookie, BKey& key)
{ {
return B_ERROR; return B_ERROR;
} }
@@ -137,7 +135,7 @@ BKeyStore::GetNextPassword(const char* keyring,
status_t status_t
BKeyStore::RegisterKeyring(const char* keyring, const BKey& password) BKeyStore::RegisterKeyring(const char* keyring, const BKey& key)
{ {
return B_ERROR; return B_ERROR;
} }
@@ -157,18 +155,18 @@ BKeyStore::GetNextKeyring(uint32& cookie, BString& keyring)
} }
// #pragma mark - Master password // #pragma mark - Master key
status_t status_t
BKeyStore::SetMasterPassword(const BKey& password) BKeyStore::SetMasterKey(const BKey& key)
{ {
return B_ERROR; return B_ERROR;
} }
status_t status_t
BKeyStore::RemoveMasterPassword() BKeyStore::RemoveMasterKey()
{ {
return B_ERROR; 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 // #pragma mark - Service functions
status_t status_t
BKeyStore::GeneratePassword(BKey& password, size_t length, uint32 flags) BKeyStore::GeneratePassword(BPasswordKey& password, size_t length, uint32 flags)
{ {
return B_ERROR; return B_ERROR;
} }