BPicture: Fix archive constructor.
[haiku.git] / src / kits / app / KeyStore.cpp
blob58fe5d0da5447f54dc66e92ca9aa9ef961d773c2
1 /*
2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <KeyStore.h>
9 #include <KeyStoreDefs.h>
11 #include <Messenger.h>
12 #include <Roster.h>
15 using namespace BPrivate;
18 BKeyStore::BKeyStore()
23 BKeyStore::~BKeyStore()
28 // #pragma mark - Key handling
31 status_t
32 BKeyStore::GetKey(BKeyType type, const char* identifier, BKey& key)
34 return GetKey(NULL, type, identifier, NULL, true, key);
38 status_t
39 BKeyStore::GetKey(BKeyType type, const char* identifier,
40 const char* secondaryIdentifier, BKey& key)
42 return GetKey(NULL, type, identifier, secondaryIdentifier, true, key);
46 status_t
47 BKeyStore::GetKey(BKeyType type, const char* identifier,
48 const char* secondaryIdentifier, bool secondaryIdentifierOptional,
49 BKey& key)
51 return GetKey(NULL, type, identifier, secondaryIdentifier,
52 secondaryIdentifierOptional, key);
56 status_t
57 BKeyStore::GetKey(const char* keyring, BKeyType type, const char* identifier,
58 BKey& key)
60 return GetKey(keyring, type, identifier, NULL, true, key);
64 status_t
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);
72 status_t
73 BKeyStore::GetKey(const char* keyring, BKeyType type, const char* identifier,
74 const char* secondaryIdentifier, bool secondaryIdentifierOptional,
75 BKey& key)
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);
84 BMessage reply;
85 status_t result = _SendKeyMessage(message, &reply);
86 if (result != B_OK)
87 return result;
89 BMessage keyMessage;
90 if (reply.FindMessage("key", &keyMessage) != B_OK)
91 return B_ERROR;
93 return key.Unflatten(keyMessage);
97 status_t
98 BKeyStore::AddKey(const BKey& key)
100 return AddKey(NULL, key);
104 status_t
105 BKeyStore::AddKey(const char* keyring, const BKey& key)
107 BMessage keyMessage;
108 if (key.Flatten(keyMessage) != B_OK)
109 return B_BAD_VALUE;
111 BMessage message(KEY_STORE_ADD_KEY);
112 message.AddString("keyring", keyring);
113 message.AddMessage("key", &keyMessage);
115 return _SendKeyMessage(message, NULL);
119 status_t
120 BKeyStore::RemoveKey(const BKey& key)
122 return RemoveKey(NULL, key);
126 status_t
127 BKeyStore::RemoveKey(const char* keyring, const BKey& key)
129 BMessage keyMessage;
130 if (key.Flatten(keyMessage) != B_OK)
131 return B_BAD_VALUE;
133 BMessage message(KEY_STORE_REMOVE_KEY);
134 message.AddString("keyring", keyring);
135 message.AddMessage("key", &keyMessage);
137 return _SendKeyMessage(message, NULL);
141 status_t
142 BKeyStore::GetNextKey(uint32& cookie, BKey& key)
144 return GetNextKey(NULL, cookie, key);
148 status_t
149 BKeyStore::GetNextKey(BKeyType type, BKeyPurpose purpose, uint32& cookie,
150 BKey& key)
152 return GetNextKey(NULL, type, purpose, cookie, key);
156 status_t
157 BKeyStore::GetNextKey(const char* keyring, uint32& cookie, BKey& key)
159 return GetNextKey(keyring, B_KEY_TYPE_ANY, B_KEY_PURPOSE_ANY, cookie, key);
163 status_t
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);
173 BMessage reply;
174 status_t result = _SendKeyMessage(message, &reply);
175 if (result != B_OK)
176 return result;
178 BMessage keyMessage;
179 if (reply.FindMessage("key", &keyMessage) != B_OK)
180 return B_ERROR;
182 reply.FindUInt32("cookie", &cookie);
183 return key.Unflatten(keyMessage);
187 // #pragma mark - Keyrings
190 status_t
191 BKeyStore::AddKeyring(const char* keyring)
193 BMessage message(KEY_STORE_ADD_KEYRING);
194 message.AddString("keyring", keyring);
195 return _SendKeyMessage(message, NULL);
199 status_t
200 BKeyStore::RemoveKeyring(const char* keyring)
202 BMessage message(KEY_STORE_REMOVE_KEYRING);
203 message.AddString("keyring", keyring);
204 return _SendKeyMessage(message, NULL);
208 status_t
209 BKeyStore::GetNextKeyring(uint32& cookie, BString& keyring)
211 BMessage message(KEY_STORE_GET_NEXT_KEYRING);
212 message.AddUInt32("cookie", cookie);
214 BMessage reply;
215 status_t result = _SendKeyMessage(message, &reply);
216 if (result != B_OK)
217 return result;
219 if (reply.FindString("keyring", &keyring) != B_OK)
220 return B_ERROR;
222 reply.FindUInt32("cookie", &cookie);
223 return B_OK;
227 status_t
228 BKeyStore::SetUnlockKey(const char* keyring, const BKey& key)
230 BMessage keyMessage;
231 if (key.Flatten(keyMessage) != B_OK)
232 return B_BAD_VALUE;
234 BMessage message(KEY_STORE_SET_UNLOCK_KEY);
235 message.AddString("keyring", keyring);
236 message.AddMessage("key", &keyMessage);
238 return _SendKeyMessage(message, NULL);
242 status_t
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
254 status_t
255 BKeyStore::SetMasterUnlockKey(const BKey& key)
257 return SetUnlockKey(NULL, key);
261 status_t
262 BKeyStore::RemoveMasterUnlockKey()
264 return RemoveUnlockKey(NULL);
268 status_t
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);
277 status_t
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);
286 status_t
287 BKeyStore::GetNextMasterKeyring(uint32& cookie, BString& keyring)
289 BMessage message(KEY_STORE_GET_NEXT_MASTER_KEYRING);
290 message.AddUInt32("cookie", cookie);
292 BMessage reply;
293 status_t result = _SendKeyMessage(message, &reply);
294 if (result != B_OK)
295 return result;
297 if (reply.FindString("keyring", &keyring) != B_OK)
298 return B_ERROR;
300 reply.FindUInt32("cookie", &cookie);
301 return B_OK;
305 // #pragma mark - Locking
308 bool
309 BKeyStore::IsKeyringUnlocked(const char* keyring)
311 BMessage message(KEY_STORE_IS_KEYRING_UNLOCKED);
312 message.AddString("keyring", keyring);
314 BMessage reply;
315 if (_SendKeyMessage(message, &reply) != B_OK)
316 return false;
318 bool unlocked;
319 if (reply.FindBool("unlocked", &unlocked) != B_OK)
320 return false;
322 return unlocked;
326 status_t
327 BKeyStore::LockKeyring(const char* keyring)
329 BMessage message(KEY_STORE_LOCK_KEYRING);
330 message.AddString("keyring", keyring);
331 return _SendKeyMessage(message, NULL);
335 status_t
336 BKeyStore::LockMasterKeyring()
338 return LockKeyring(NULL);
343 // #pragma mark - Applications
346 status_t
347 BKeyStore::GetNextApplication(uint32& cookie, BString& signature) const
349 return GetNextApplication(NULL, cookie, signature);
353 status_t
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);
361 BMessage reply;
362 status_t result = _SendKeyMessage(message, &reply);
363 if (result != B_OK)
364 return result;
366 if (reply.FindString("signature", &signature) != B_OK)
367 return B_ERROR;
369 reply.FindUInt32("cookie", &cookie);
370 return B_OK;
374 status_t
375 BKeyStore::RemoveApplication(const char* signature)
377 return RemoveApplication(NULL, signature);
381 status_t
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
395 status_t
396 BKeyStore::GeneratePassword(BPasswordKey& password, size_t length, uint32 flags)
398 return B_ERROR;
402 float
403 BKeyStore::PasswordStrength(const char* password)
405 return 0;
409 // #pragma mark - Private functions
412 status_t
413 BKeyStore::_SendKeyMessage(BMessage& message, BMessage* reply) const
415 BMessage localReply;
416 if (reply == NULL)
417 reply = &localReply;
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)
424 return B_ERROR;
426 // Then re-target the messenger and check again.
427 messenger.SetTo(kKeyStoreServerSignature);
428 if (!messenger.IsValid())
429 return B_ERROR;
432 if (messenger.SendMessage(&message, reply) != B_OK)
433 return B_ERROR;
435 if (reply->what != KEY_STORE_SUCCESS) {
436 status_t result = B_ERROR;
437 if (reply->FindInt32("result", &result) != B_OK)
438 return B_ERROR;
440 return result;
443 return B_OK;