Restructure how keyrings are stored/restored.
* Pass them through a flat buffer that can later be encrypted and decrypted in a central place. * Remove the data argument from the constructor as keyrings are now reading their data on their own. * Prepare for additional application info storage in the keyring.
This commit is contained in:
committed by
Ryan Leavengood
parent
97b3abf162
commit
1b3bb46aed
@@ -82,7 +82,7 @@ KeyStoreServer::KeyStoreServer()
|
|||||||
_ReadKeyStoreDatabase();
|
_ReadKeyStoreDatabase();
|
||||||
|
|
||||||
if (fDefaultKeyring == NULL)
|
if (fDefaultKeyring == NULL)
|
||||||
fDefaultKeyring = new(std::nothrow) Keyring("", BMessage());
|
fDefaultKeyring = new(std::nothrow) Keyring("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -388,19 +388,17 @@ KeyStoreServer::_ReadKeyStoreDatabase()
|
|||||||
|
|
||||||
int32 index = 0;
|
int32 index = 0;
|
||||||
char* keyringName = NULL;
|
char* keyringName = NULL;
|
||||||
while (keyrings.GetInfo(B_MESSAGE_TYPE, index++, &keyringName,
|
while (keyrings.GetInfo(B_RAW_TYPE, index++, &keyringName, NULL) == B_OK) {
|
||||||
NULL) == B_OK) {
|
Keyring* keyring = new(std::nothrow) Keyring(keyringName);
|
||||||
|
if (keyring == NULL) {
|
||||||
BMessage keyringData;
|
printf("no memory for allocating keyring \"%s\"\n", keyringName);
|
||||||
if (keyrings.FindMessage(keyringName, &keyringData) != B_OK) {
|
|
||||||
printf("failed to retrieve keyring data for keyring \"%s\"\n",
|
|
||||||
keyringName);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Keyring* keyring = new(std::nothrow) Keyring(keyringName, keyringData);
|
status_t result = keyring->ReadFromMessage(keyrings);
|
||||||
if (keyring == NULL) {
|
if (result != B_OK) {
|
||||||
printf("no memory for allocating keyring \"%s\"\n", keyringName);
|
printf("failed to read keyring \"%s\" from data\n", keyringName);
|
||||||
|
delete keyring;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,14 +420,16 @@ KeyStoreServer::_WriteKeyStoreDatabase()
|
|||||||
|
|
||||||
BMessage keyrings;
|
BMessage keyrings;
|
||||||
if (fDefaultKeyring != NULL)
|
if (fDefaultKeyring != NULL)
|
||||||
keyrings.AddMessage("", &fDefaultKeyring->Data());
|
fDefaultKeyring->WriteToMessage(keyrings);
|
||||||
|
|
||||||
for (int32 i = 0; i < fKeyrings.CountItems(); i++) {
|
for (int32 i = 0; i < fKeyrings.CountItems(); i++) {
|
||||||
Keyring* keyring = fKeyrings.ItemAt(i);
|
Keyring* keyring = fKeyrings.ItemAt(i);
|
||||||
if (keyring == NULL)
|
if (keyring == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
keyrings.AddMessage(keyring->Name(), &keyring->Data());
|
status_t result = keyring->WriteToMessage(keyrings);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return keyrings.Flatten(&fKeyStoreFile);
|
return keyrings.Flatten(&fKeyStoreFile);
|
||||||
@@ -494,7 +494,7 @@ KeyStoreServer::_AddKeyring(const BString& name, const BMessage& keyMessage)
|
|||||||
if (_FindKeyring(name) != NULL)
|
if (_FindKeyring(name) != NULL)
|
||||||
return B_NAME_IN_USE;
|
return B_NAME_IN_USE;
|
||||||
|
|
||||||
Keyring* keyring = new(std::nothrow) Keyring(name, BMessage(), &keyMessage);
|
Keyring* keyring = new(std::nothrow) Keyring(name, &keyMessage);
|
||||||
if (keyring == NULL)
|
if (keyring == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,13 @@
|
|||||||
#include "Keyring.h"
|
#include "Keyring.h"
|
||||||
|
|
||||||
|
|
||||||
Keyring::Keyring(const char* name, const BMessage& data,
|
Keyring::Keyring(const char* name, const BMessage* keyMessage)
|
||||||
const BMessage* keyMessage)
|
|
||||||
:
|
:
|
||||||
fName(name),
|
fName(name),
|
||||||
fData(data),
|
fAccessible(false)
|
||||||
fKeyMessage(*keyMessage),
|
|
||||||
fAccessible(keyMessage != NULL)
|
|
||||||
{
|
{
|
||||||
|
if (keyMessage != NULL)
|
||||||
|
Access(*keyMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -23,10 +22,52 @@ Keyring::~Keyring()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
Keyring::ReadFromMessage(const BMessage& message)
|
||||||
|
{
|
||||||
|
ssize_t size;
|
||||||
|
const void* data;
|
||||||
|
status_t result = message.FindData(fName, B_RAW_TYPE, &data, &size);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
if (size < 0)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
|
fFlatBuffer.SetSize(0);
|
||||||
|
ssize_t written = fFlatBuffer.WriteAt(0, data, size);
|
||||||
|
if (written != size) {
|
||||||
|
fFlatBuffer.SetSize(0);
|
||||||
|
return written < 0 ? written : B_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
Keyring::WriteToMessage(BMessage& message)
|
||||||
|
{
|
||||||
|
status_t result = _EncryptToFlatBuffer();
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return message.AddData(fName, B_RAW_TYPE, fFlatBuffer.Buffer(),
|
||||||
|
fFlatBuffer.BufferLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
Keyring::Access(const BMessage& keyMessage)
|
Keyring::Access(const BMessage& keyMessage)
|
||||||
{
|
{
|
||||||
fKeyMessage = keyMessage;
|
fKeyMessage = keyMessage;
|
||||||
|
|
||||||
|
status_t result = _DecryptFromFlatBuffer();
|
||||||
|
if (result != B_OK) {
|
||||||
|
fKeyMessage.MakeEmpty();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
fAccessible = true;
|
fAccessible = true;
|
||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
@@ -35,7 +76,14 @@ Keyring::Access(const BMessage& keyMessage)
|
|||||||
void
|
void
|
||||||
Keyring::RevokeAccess()
|
Keyring::RevokeAccess()
|
||||||
{
|
{
|
||||||
|
if (!fAccessible)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_EncryptToFlatBuffer();
|
||||||
|
|
||||||
fKeyMessage.MakeEmpty();
|
fKeyMessage.MakeEmpty();
|
||||||
|
fData.MakeEmpty();
|
||||||
|
fApplications.MakeEmpty();
|
||||||
fAccessible = false;
|
fAccessible = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,3 +270,59 @@ Keyring::Compare(const BString* name, const Keyring* keyring)
|
|||||||
{
|
{
|
||||||
return strcmp(name->String(), keyring->Name());
|
return strcmp(name->String(), keyring->Name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
Keyring::_EncryptToFlatBuffer()
|
||||||
|
{
|
||||||
|
if (!fAccessible)
|
||||||
|
return B_NOT_ALLOWED;
|
||||||
|
|
||||||
|
BMessage container;
|
||||||
|
status_t result = container.AddMessage("data", &fData);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
result = container.AddMessage("applications", &fApplications);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
fFlatBuffer.SetSize(0);
|
||||||
|
fFlatBuffer.Seek(0, SEEK_SET);
|
||||||
|
|
||||||
|
result = container.Flatten(&fFlatBuffer);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
// TODO: Actually encrypt the flat buffer...
|
||||||
|
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
status_t
|
||||||
|
Keyring::_DecryptFromFlatBuffer()
|
||||||
|
{
|
||||||
|
if (fFlatBuffer.BufferLength() == 0)
|
||||||
|
return B_OK;
|
||||||
|
|
||||||
|
// TODO: Actually decrypt the flat buffer...
|
||||||
|
|
||||||
|
BMessage container;
|
||||||
|
fFlatBuffer.Seek(0, SEEK_SET);
|
||||||
|
status_t result = container.Unflatten(&fFlatBuffer);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
result = container.FindMessage("data", &fData);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
result = container.FindMessage("applications", &fApplications);
|
||||||
|
if (result != B_OK) {
|
||||||
|
fData.MakeEmpty();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return B_OK;
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,12 +13,12 @@
|
|||||||
class Keyring {
|
class Keyring {
|
||||||
public:
|
public:
|
||||||
Keyring(const char* name,
|
Keyring(const char* name,
|
||||||
const BMessage& data,
|
|
||||||
const BMessage* keyMessage = NULL);
|
const BMessage* keyMessage = NULL);
|
||||||
~Keyring();
|
~Keyring();
|
||||||
|
|
||||||
const char* Name() const { return fName; }
|
const char* Name() const { return fName; }
|
||||||
const BMessage& Data() const { return fData; }
|
status_t ReadFromMessage(const BMessage& message);
|
||||||
|
status_t WriteToMessage(BMessage& message);
|
||||||
|
|
||||||
status_t Access(const BMessage& keyMessage);
|
status_t Access(const BMessage& keyMessage);
|
||||||
void RevokeAccess();
|
void RevokeAccess();
|
||||||
@@ -45,8 +45,13 @@ static int Compare(const BString* name,
|
|||||||
const Keyring* keyring);
|
const Keyring* keyring);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
status_t _EncryptToFlatBuffer();
|
||||||
|
status_t _DecryptFromFlatBuffer();
|
||||||
|
|
||||||
BString fName;
|
BString fName;
|
||||||
|
BMallocIO fFlatBuffer;
|
||||||
BMessage fData;
|
BMessage fData;
|
||||||
|
BMessage fApplications;
|
||||||
BMessage fKeyMessage;
|
BMessage fKeyMessage;
|
||||||
bool fAccessible;
|
bool fAccessible;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user