2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
13 // TODO: move this to the KeyStore or the registrar backend if needed
15 CompareLists(BObjectList
<BString
> a
, BObjectList
<BString
> b
)
17 if (a
.CountItems() != b
.CountItems())
20 for (int32 i
= 0; i
< a
.CountItems(); i
++) {
21 if (*a
.ItemAt(i
) != *b
.ItemAt(i
))
30 // #pragma mark - Generic BKey
39 BKey::BKey(BKeyPurpose purpose
, const char* identifier
,
40 const char* secondaryIdentifier
, const uint8
* data
, size_t length
)
42 SetTo(purpose
, identifier
, secondaryIdentifier
, data
, length
);
46 BKey::BKey(BKey
& other
)
60 SetTo(B_KEY_PURPOSE_GENERIC
, "", "", NULL
, 0);
65 BKey::SetTo(BKeyPurpose purpose
, const char* identifier
,
66 const char* secondaryIdentifier
, const uint8
* data
, size_t length
)
70 SetIdentifier(identifier
);
71 SetSecondaryIdentifier(secondaryIdentifier
);
72 return SetData(data
, length
);
77 BKey::SetPurpose(BKeyPurpose purpose
)
91 BKey::SetIdentifier(const char* identifier
)
93 fIdentifier
= identifier
;
98 BKey::Identifier() const
100 return fIdentifier
.String();
105 BKey::SetSecondaryIdentifier(const char* identifier
)
107 fSecondaryIdentifier
= identifier
;
112 BKey::SecondaryIdentifier() const
114 return fSecondaryIdentifier
.String();
119 BKey::SetData(const uint8
* data
, size_t length
)
122 ssize_t bytesWritten
= fData
.WriteAt(0, data
, length
);
123 if (bytesWritten
< 0)
124 return (status_t
)bytesWritten
;
126 return (size_t)bytesWritten
== length
? B_OK
: B_NO_MEMORY
;
131 BKey::DataLength() const
133 return fData
.BufferLength();
140 return (const uint8
*)fData
.Buffer();
145 BKey::GetData(uint8
* buffer
, size_t bufferSize
) const
147 ssize_t bytesRead
= fData
.ReadAt(0, buffer
, bufferSize
);
149 return (status_t
)bytesRead
;
159 return fOwner
.String();
164 BKey::CreationTime() const
166 return fCreationTime
;
171 BKey::Flatten(BMessage
& message
) const
173 if (message
.MakeEmpty() != B_OK
174 || message
.AddUInt32("type", Type()) != B_OK
175 || message
.AddUInt32("purpose", fPurpose
) != B_OK
176 || message
.AddString("identifier", fIdentifier
) != B_OK
177 || message
.AddString("secondaryIdentifier", fSecondaryIdentifier
)
179 || message
.AddString("owner", fOwner
) != B_OK
180 || message
.AddInt64("creationTime", fCreationTime
) != B_OK
181 || message
.AddData("data", B_RAW_TYPE
, fData
.Buffer(),
182 fData
.BufferLength()) != B_OK
) {
191 BKey::Unflatten(const BMessage
& message
)
194 if (message
.FindUInt32("type", (uint32
*)&type
) != B_OK
|| type
!= Type())
197 const void* data
= NULL
;
198 ssize_t dataLength
= 0;
199 if (message
.FindUInt32("purpose", (uint32
*)&fPurpose
) != B_OK
200 || message
.FindString("identifier", &fIdentifier
) != B_OK
201 || message
.FindString("secondaryIdentifier", &fSecondaryIdentifier
)
203 || message
.FindString("owner", &fOwner
) != B_OK
204 || message
.FindInt64("creationTime", &fCreationTime
) != B_OK
205 || message
.FindData("data", B_RAW_TYPE
, &data
, &dataLength
) != B_OK
210 return SetData((const uint8
*)data
, (size_t)dataLength
);
215 BKey::operator=(const BKey
& other
)
217 SetPurpose(other
.Purpose());
218 SetData((const uint8
*)other
.Data(), other
.DataLength());
220 fIdentifier
= other
.fIdentifier
;
221 fSecondaryIdentifier
= other
.fSecondaryIdentifier
;
222 fOwner
= other
.fOwner
;
223 fCreationTime
= other
.fCreationTime
;
230 BKey::operator==(const BKey
& other
) const
232 return Type() == other
.Type()
233 && DataLength() == other
.DataLength()
234 && Purpose() == other
.Purpose()
235 && fOwner
== other
.fOwner
236 && fIdentifier
== other
.fIdentifier
237 && fSecondaryIdentifier
== other
.fSecondaryIdentifier
238 && memcmp(Data(), other
.Data(), DataLength()) == 0;
243 BKey::operator!=(const BKey
& other
) const
245 return !(*this == other
);
250 BKey::PrintToStream()
252 if (Type() == B_KEY_TYPE_GENERIC
)
253 printf("generic key:\n");
255 const char* purposeString
= "unknown";
257 case B_KEY_PURPOSE_ANY
:
258 purposeString
= "any";
260 case B_KEY_PURPOSE_GENERIC
:
261 purposeString
= "generic";
263 case B_KEY_PURPOSE_KEYRING
:
264 purposeString
= "keyring";
266 case B_KEY_PURPOSE_WEB
:
267 purposeString
= "web";
269 case B_KEY_PURPOSE_NETWORK
:
270 purposeString
= "network";
272 case B_KEY_PURPOSE_VOLUME
:
273 purposeString
= "volume";
277 printf("\tpurpose: %s\n", purposeString
);
278 printf("\tidentifier: \"%s\"\n", fIdentifier
.String());
279 printf("\tsecondary identifier: \"%s\"\n", fSecondaryIdentifier
.String());
280 printf("\towner: \"%s\"\n", fOwner
.String());
281 printf("\tcreation time: %" B_PRIu64
"\n", fCreationTime
);
282 printf("\traw data length: %" B_PRIuSIZE
"\n", fData
.BufferLength());
286 // #pragma mark - BPasswordKey
289 BPasswordKey::BPasswordKey()
294 BPasswordKey::BPasswordKey(const char* password
, BKeyPurpose purpose
,
295 const char* identifier
, const char* secondaryIdentifier
)
297 BKey(purpose
, identifier
, secondaryIdentifier
, (const uint8
*)password
,
298 strlen(password
) + 1)
303 BPasswordKey::BPasswordKey(BPasswordKey
& other
)
308 BPasswordKey::~BPasswordKey()
314 BPasswordKey::SetTo(const char* password
, BKeyPurpose purpose
,
315 const char* identifier
, const char* secondaryIdentifier
)
317 return BKey::SetTo(purpose
, identifier
, secondaryIdentifier
,
318 (const uint8
*)password
, strlen(password
) + 1);
323 BPasswordKey::SetPassword(const char* password
)
325 return SetData((const uint8
*)password
, strlen(password
) + 1);
330 BPasswordKey::Password() const
332 return (const char*)Data();
337 BPasswordKey::PrintToStream()
339 printf("password key:\n");
340 BKey::PrintToStream();
341 printf("\tpassword: \"%s\"\n", Password());