2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
9 #include <KeyStoreDefs.h>
11 #include <Messenger.h>
15 using namespace BPrivate
;
18 BKeyStore::BKeyStore()
23 BKeyStore::~BKeyStore()
28 // #pragma mark - Key handling
32 BKeyStore::GetKey(BKeyType type
, const char* identifier
, BKey
& key
)
34 return GetKey(NULL
, type
, identifier
, NULL
, true, key
);
39 BKeyStore::GetKey(BKeyType type
, const char* identifier
,
40 const char* secondaryIdentifier
, BKey
& key
)
42 return GetKey(NULL
, type
, identifier
, secondaryIdentifier
, true, key
);
47 BKeyStore::GetKey(BKeyType type
, const char* identifier
,
48 const char* secondaryIdentifier
, bool secondaryIdentifierOptional
,
51 return GetKey(NULL
, type
, identifier
, secondaryIdentifier
,
52 secondaryIdentifierOptional
, key
);
57 BKeyStore::GetKey(const char* keyring
, BKeyType type
, const char* identifier
,
60 return GetKey(keyring
, type
, identifier
, NULL
, true, key
);
65 BKeyStore::GetKey(const char* keyring
, BKeyType type
, const char* identifier
,
66 const char* secondaryIdentifier
, BKey
& key
)
68 return GetKey(keyring
, type
, identifier
, secondaryIdentifier
, true, key
);
73 BKeyStore::GetKey(const char* keyring
, BKeyType type
, const char* identifier
,
74 const char* secondaryIdentifier
, bool secondaryIdentifierOptional
,
77 BMessage
message(KEY_STORE_GET_KEY
);
78 message
.AddString("keyring", keyring
);
79 message
.AddUInt32("type", type
);
80 message
.AddString("identifier", identifier
);
81 message
.AddString("secondaryIdentifier", secondaryIdentifier
);
82 message
.AddBool("secondaryIdentifierOptional", secondaryIdentifierOptional
);
85 status_t result
= _SendKeyMessage(message
, &reply
);
90 if (reply
.FindMessage("key", &keyMessage
) != B_OK
)
93 return key
.Unflatten(keyMessage
);
98 BKeyStore::AddKey(const BKey
& key
)
100 return AddKey(NULL
, key
);
105 BKeyStore::AddKey(const char* keyring
, const BKey
& key
)
108 if (key
.Flatten(keyMessage
) != B_OK
)
111 BMessage
message(KEY_STORE_ADD_KEY
);
112 message
.AddString("keyring", keyring
);
113 message
.AddMessage("key", &keyMessage
);
115 return _SendKeyMessage(message
, NULL
);
120 BKeyStore::RemoveKey(const BKey
& key
)
122 return RemoveKey(NULL
, key
);
127 BKeyStore::RemoveKey(const char* keyring
, const BKey
& key
)
130 if (key
.Flatten(keyMessage
) != B_OK
)
133 BMessage
message(KEY_STORE_REMOVE_KEY
);
134 message
.AddString("keyring", keyring
);
135 message
.AddMessage("key", &keyMessage
);
137 return _SendKeyMessage(message
, NULL
);
142 BKeyStore::GetNextKey(uint32
& cookie
, BKey
& key
)
144 return GetNextKey(NULL
, cookie
, key
);
149 BKeyStore::GetNextKey(BKeyType type
, BKeyPurpose purpose
, uint32
& cookie
,
152 return GetNextKey(NULL
, type
, purpose
, cookie
, key
);
157 BKeyStore::GetNextKey(const char* keyring
, uint32
& cookie
, BKey
& key
)
159 return GetNextKey(keyring
, B_KEY_TYPE_ANY
, B_KEY_PURPOSE_ANY
, cookie
, key
);
164 BKeyStore::GetNextKey(const char* keyring
, BKeyType type
, BKeyPurpose purpose
,
165 uint32
& cookie
, BKey
& key
)
167 BMessage
message(KEY_STORE_GET_NEXT_KEY
);
168 message
.AddString("keyring", keyring
);
169 message
.AddUInt32("type", type
);
170 message
.AddUInt32("purpose", purpose
);
171 message
.AddUInt32("cookie", cookie
);
174 status_t result
= _SendKeyMessage(message
, &reply
);
179 if (reply
.FindMessage("key", &keyMessage
) != B_OK
)
182 reply
.FindUInt32("cookie", &cookie
);
183 return key
.Unflatten(keyMessage
);
187 // #pragma mark - Keyrings
191 BKeyStore::AddKeyring(const char* keyring
)
193 BMessage
message(KEY_STORE_ADD_KEYRING
);
194 message
.AddString("keyring", keyring
);
195 return _SendKeyMessage(message
, NULL
);
200 BKeyStore::RemoveKeyring(const char* keyring
)
202 BMessage
message(KEY_STORE_REMOVE_KEYRING
);
203 message
.AddString("keyring", keyring
);
204 return _SendKeyMessage(message
, NULL
);
209 BKeyStore::GetNextKeyring(uint32
& cookie
, BString
& keyring
)
211 BMessage
message(KEY_STORE_GET_NEXT_KEYRING
);
212 message
.AddUInt32("cookie", cookie
);
215 status_t result
= _SendKeyMessage(message
, &reply
);
219 if (reply
.FindString("keyring", &keyring
) != B_OK
)
222 reply
.FindUInt32("cookie", &cookie
);
228 BKeyStore::SetUnlockKey(const char* keyring
, const BKey
& key
)
231 if (key
.Flatten(keyMessage
) != B_OK
)
234 BMessage
message(KEY_STORE_SET_UNLOCK_KEY
);
235 message
.AddString("keyring", keyring
);
236 message
.AddMessage("key", &keyMessage
);
238 return _SendKeyMessage(message
, NULL
);
243 BKeyStore::RemoveUnlockKey(const char* keyring
)
245 BMessage
message(KEY_STORE_REMOVE_UNLOCK_KEY
);
246 message
.AddString("keyring", keyring
);
247 return _SendKeyMessage(message
, NULL
);
251 // #pragma mark - Master key
255 BKeyStore::SetMasterUnlockKey(const BKey
& key
)
257 return SetUnlockKey(NULL
, key
);
262 BKeyStore::RemoveMasterUnlockKey()
264 return RemoveUnlockKey(NULL
);
269 BKeyStore::AddKeyringToMaster(const char* keyring
)
271 BMessage
message(KEY_STORE_ADD_KEYRING_TO_MASTER
);
272 message
.AddString("keyring", keyring
);
273 return _SendKeyMessage(message
, NULL
);
278 BKeyStore::RemoveKeyringFromMaster(const char* keyring
)
280 BMessage
message(KEY_STORE_REMOVE_KEYRING_FROM_MASTER
);
281 message
.AddString("keyring", keyring
);
282 return _SendKeyMessage(message
, NULL
);
287 BKeyStore::GetNextMasterKeyring(uint32
& cookie
, BString
& keyring
)
289 BMessage
message(KEY_STORE_GET_NEXT_MASTER_KEYRING
);
290 message
.AddUInt32("cookie", cookie
);
293 status_t result
= _SendKeyMessage(message
, &reply
);
297 if (reply
.FindString("keyring", &keyring
) != B_OK
)
300 reply
.FindUInt32("cookie", &cookie
);
305 // #pragma mark - Locking
309 BKeyStore::IsKeyringUnlocked(const char* keyring
)
311 BMessage
message(KEY_STORE_IS_KEYRING_UNLOCKED
);
312 message
.AddString("keyring", keyring
);
315 if (_SendKeyMessage(message
, &reply
) != B_OK
)
319 if (reply
.FindBool("unlocked", &unlocked
) != B_OK
)
327 BKeyStore::LockKeyring(const char* keyring
)
329 BMessage
message(KEY_STORE_LOCK_KEYRING
);
330 message
.AddString("keyring", keyring
);
331 return _SendKeyMessage(message
, NULL
);
336 BKeyStore::LockMasterKeyring()
338 return LockKeyring(NULL
);
343 // #pragma mark - Applications
347 BKeyStore::GetNextApplication(uint32
& cookie
, BString
& signature
) const
349 return GetNextApplication(NULL
, cookie
, signature
);
354 BKeyStore::GetNextApplication(const char* keyring
, uint32
& cookie
,
355 BString
& signature
) const
357 BMessage
message(KEY_STORE_GET_NEXT_APPLICATION
);
358 message
.AddString("keyring", keyring
);
359 message
.AddUInt32("cookie", cookie
);
362 status_t result
= _SendKeyMessage(message
, &reply
);
366 if (reply
.FindString("signature", &signature
) != B_OK
)
369 reply
.FindUInt32("cookie", &cookie
);
375 BKeyStore::RemoveApplication(const char* signature
)
377 return RemoveApplication(NULL
, signature
);
382 BKeyStore::RemoveApplication(const char* keyring
, const char* signature
)
384 BMessage
message(KEY_STORE_REMOVE_APPLICATION
);
385 message
.AddString("keyring", keyring
);
386 message
.AddString("signature", signature
);
388 return _SendKeyMessage(message
, NULL
);
392 // #pragma mark - Service functions
396 BKeyStore::GeneratePassword(BPasswordKey
& password
, size_t length
, uint32 flags
)
403 BKeyStore::PasswordStrength(const char* password
)
409 // #pragma mark - Private functions
413 BKeyStore::_SendKeyMessage(BMessage
& message
, BMessage
* reply
) const
419 BMessenger
messenger(kKeyStoreServerSignature
);
420 if (!messenger
.IsValid()) {
421 // Try to start the keystore server.
422 status_t result
= be_roster
->Launch(kKeyStoreServerSignature
);
423 if (result
!= B_OK
&& result
!= B_ALREADY_RUNNING
)
426 // Then re-target the messenger and check again.
427 messenger
.SetTo(kKeyStoreServerSignature
);
428 if (!messenger
.IsValid())
432 if (messenger
.SendMessage(&message
, reply
) != B_OK
)
435 if (reply
->what
!= KEY_STORE_SUCCESS
) {
436 status_t result
= B_ERROR
;
437 if (reply
->FindInt32("result", &result
) != B_OK
)