Store each keyring in a message under a common keyrings field.
* Each keyring is now stored in a proper message which allows it to contain additional meta data along side the flat data. * Adding all keyring messages under a common field also allows to add meta data to the keystore, as the keyrings don't use up random field names anymore. * Treat the master keyring as any other keyring and just add it to the list. This allows to write/read the keystore database without special casing the master keyring.
This commit is contained in:
committed by
Ryan Leavengood
parent
d4d6d12393
commit
bec02d0c2f
@@ -30,6 +30,8 @@ using namespace BPrivate;
|
|||||||
static const char* kMasterKeyringName = "Master";
|
static const char* kMasterKeyringName = "Master";
|
||||||
static const char* kKeyringKeysIdentifier = "Keyrings";
|
static const char* kKeyringKeysIdentifier = "Keyrings";
|
||||||
|
|
||||||
|
static const uint32 kKeyStoreFormatVersion = 1;
|
||||||
|
|
||||||
static const uint32 kFlagGetKey = 0x0001;
|
static const uint32 kFlagGetKey = 0x0001;
|
||||||
static const uint32 kFlagEnumerateKeys = 0x0002;
|
static const uint32 kFlagEnumerateKeys = 0x0002;
|
||||||
static const uint32 kFlagAddKey = 0x0004;
|
static const uint32 kFlagAddKey = 0x0004;
|
||||||
@@ -83,8 +85,10 @@ KeyStoreServer::KeyStoreServer()
|
|||||||
|
|
||||||
_ReadKeyStoreDatabase();
|
_ReadKeyStoreDatabase();
|
||||||
|
|
||||||
if (fMasterKeyring == NULL)
|
if (fMasterKeyring == NULL) {
|
||||||
fMasterKeyring = new(std::nothrow) Keyring(kMasterKeyringName);
|
fMasterKeyring = new(std::nothrow) Keyring(kMasterKeyringName);
|
||||||
|
fKeyrings.BinaryInsert(fMasterKeyring, &Keyring::Compare);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -302,11 +306,7 @@ KeyStoreServer::MessageReceived(BMessage* message)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cookie == 0)
|
keyring = fKeyrings.ItemAt(cookie);
|
||||||
keyring = fMasterKeyring;
|
|
||||||
else
|
|
||||||
keyring = fKeyrings.ItemAt(cookie - 1);
|
|
||||||
|
|
||||||
if (keyring == NULL) {
|
if (keyring == NULL) {
|
||||||
result = B_ENTRY_NOT_FOUND;
|
result = B_ENTRY_NOT_FOUND;
|
||||||
break;
|
break;
|
||||||
@@ -442,34 +442,35 @@ KeyStoreServer::MessageReceived(BMessage* message)
|
|||||||
status_t
|
status_t
|
||||||
KeyStoreServer::_ReadKeyStoreDatabase()
|
KeyStoreServer::_ReadKeyStoreDatabase()
|
||||||
{
|
{
|
||||||
BMessage keyrings;
|
BMessage keystore;
|
||||||
status_t result = keyrings.Unflatten(&fKeyStoreFile);
|
status_t result = keystore.Unflatten(&fKeyStoreFile);
|
||||||
if (result != B_OK) {
|
if (result != B_OK) {
|
||||||
printf("failed to read keystore database\n");
|
printf("failed to read keystore database\n");
|
||||||
_WriteKeyStoreDatabase();
|
_WriteKeyStoreDatabase();
|
||||||
|
// Reinitializes the database.
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32 index = 0;
|
int32 index = 0;
|
||||||
char* keyringName = NULL;
|
BMessage keyringData;
|
||||||
while (keyrings.GetInfo(B_RAW_TYPE, index++, &keyringName, NULL) == B_OK) {
|
while (keystore.FindMessage("keyrings", index++, &keyringData) == B_OK) {
|
||||||
Keyring* keyring = new(std::nothrow) Keyring(keyringName);
|
Keyring* keyring = new(std::nothrow) Keyring();
|
||||||
if (keyring == NULL) {
|
if (keyring == NULL) {
|
||||||
printf("no memory for allocating keyring \"%s\"\n", keyringName);
|
printf("no memory for allocating keyring\n");
|
||||||
continue;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
status_t result = keyring->ReadFromMessage(keyrings);
|
status_t result = keyring->ReadFromMessage(keyringData);
|
||||||
if (result != B_OK) {
|
if (result != B_OK) {
|
||||||
printf("failed to read keyring \"%s\" from data\n", keyringName);
|
printf("failed to read keyring from data\n");
|
||||||
delete keyring;
|
delete keyring;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(keyringName, kMasterKeyringName) == 0)
|
if (strcmp(keyring->Name(), kMasterKeyringName) == 0)
|
||||||
fMasterKeyring = keyring;
|
fMasterKeyring = keyring;
|
||||||
else
|
|
||||||
fKeyrings.BinaryInsert(keyring, &Keyring::Compare);
|
fKeyrings.BinaryInsert(keyring, &Keyring::Compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
return B_OK;
|
return B_OK;
|
||||||
@@ -479,23 +480,25 @@ KeyStoreServer::_ReadKeyStoreDatabase()
|
|||||||
status_t
|
status_t
|
||||||
KeyStoreServer::_WriteKeyStoreDatabase()
|
KeyStoreServer::_WriteKeyStoreDatabase()
|
||||||
{
|
{
|
||||||
BMessage keyrings;
|
BMessage keystore;
|
||||||
if (fMasterKeyring != NULL)
|
keystore.AddUInt32("format", kKeyStoreFormatVersion);
|
||||||
fMasterKeyring->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;
|
||||||
|
|
||||||
status_t result = keyring->WriteToMessage(keyrings);
|
BMessage keyringData;
|
||||||
|
status_t result = keyring->WriteToMessage(keyringData);
|
||||||
if (result != B_OK)
|
if (result != B_OK)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
|
keystore.AddMessage("keyrings", &keyringData);
|
||||||
}
|
}
|
||||||
|
|
||||||
fKeyStoreFile.SetSize(0);
|
fKeyStoreFile.SetSize(0);
|
||||||
fKeyStoreFile.Seek(0, SEEK_SET);
|
fKeyStoreFile.Seek(0, SEEK_SET);
|
||||||
return keyrings.Flatten(&fKeyStoreFile);
|
return keystore.Flatten(&fKeyStoreFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,14 @@
|
|||||||
#include "Keyring.h"
|
#include "Keyring.h"
|
||||||
|
|
||||||
|
|
||||||
|
Keyring::Keyring()
|
||||||
|
:
|
||||||
|
fUnlocked(false),
|
||||||
|
fModified(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Keyring::Keyring(const char* name)
|
Keyring::Keyring(const char* name)
|
||||||
:
|
:
|
||||||
fName(name),
|
fName(name),
|
||||||
@@ -24,9 +32,13 @@ Keyring::~Keyring()
|
|||||||
status_t
|
status_t
|
||||||
Keyring::ReadFromMessage(const BMessage& message)
|
Keyring::ReadFromMessage(const BMessage& message)
|
||||||
{
|
{
|
||||||
|
status_t result = message.FindString("name", &fName);
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
ssize_t size;
|
ssize_t size;
|
||||||
const void* data;
|
const void* data;
|
||||||
status_t result = message.FindData(fName, B_RAW_TYPE, &data, &size);
|
result = message.FindData("data", B_RAW_TYPE, &data, &size);
|
||||||
if (result != B_OK)
|
if (result != B_OK)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
@@ -51,8 +63,12 @@ Keyring::WriteToMessage(BMessage& message)
|
|||||||
if (result != B_OK)
|
if (result != B_OK)
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
return message.AddData(fName, B_RAW_TYPE, fFlatBuffer.Buffer(),
|
result = message.AddData("data", B_RAW_TYPE, fFlatBuffer.Buffer(),
|
||||||
fFlatBuffer.BufferLength());
|
fFlatBuffer.BufferLength());
|
||||||
|
if (result != B_OK)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
return message.AddString("name", fName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
class Keyring {
|
class Keyring {
|
||||||
public:
|
public:
|
||||||
|
Keyring();
|
||||||
Keyring(const char* name);
|
Keyring(const char* name);
|
||||||
~Keyring();
|
~Keyring();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user