xref: /haiku/headers/os/app/Key.h (revision 25a7b01d15612846f332751841da3579db313082)
1 /*
2  * Copyright 2011, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _KEY_H
6 #define _KEY_H
7 
8 
9 #include <DataIO.h>
10 #include <Message.h>
11 #include <ObjectList.h>
12 #include <String.h>
13 
14 
15 enum BKeyPurpose {
16 	B_KEY_PURPOSE_ANY,
17 	B_KEY_PURPOSE_GENERIC,
18 	B_KEY_PURPOSE_KEYRING,
19 	B_KEY_PURPOSE_WEB,
20 	B_KEY_PURPOSE_NETWORK,
21 	B_KEY_PURPOSE_VOLUME
22 };
23 
24 
25 enum BKeyType {
26 	B_KEY_TYPE_ANY,
27 	B_KEY_TYPE_GENERIC,
28 	B_KEY_TYPE_PASSWORD,
29 	B_KEY_TYPE_CERTIFICATE
30 };
31 
32 
33 class BKey {
34 public:
35 								BKey();
36 								BKey(BKeyPurpose purpose,
37 									const char* identifier,
38 									const char* secondaryIdentifier = NULL,
39 									const uint8* data = NULL,
40 									size_t length = 0);
41 								BKey(BKey& other);
42 	virtual						~BKey();
43 
Type()44 	virtual	BKeyType			Type() const { return B_KEY_TYPE_GENERIC; };
45 
46 			void				Unset();
47 
48 			status_t			SetTo(BKeyPurpose purpose,
49 									const char* identifier,
50 									const char* secondaryIdentifier = NULL,
51 									const uint8* data = NULL,
52 									size_t length = 0);
53 
54 			void				SetPurpose(BKeyPurpose purpose);
55 			BKeyPurpose			Purpose() const;
56 
57 			void				SetIdentifier(const char* identifier);
58 			const char*			Identifier() const;
59 
60 			void				SetSecondaryIdentifier(const char* identifier);
61 			const char*			SecondaryIdentifier() const;
62 
63 			status_t			SetData(const uint8* data, size_t length);
64 			size_t				DataLength() const;
65 			const uint8*		Data() const;
66 			status_t			GetData(uint8* buffer, size_t bufferSize) const;
67 
68 			const char*			Owner() const;
69 			bigtime_t			CreationTime() const;
70 
71 	virtual	status_t			Flatten(BMessage& message) const;
72 	virtual	status_t			Unflatten(const BMessage& message);
73 
74 			BKey&				operator=(const BKey& other);
75 
76 			bool				operator==(const BKey& other) const;
77 			bool				operator!=(const BKey& other) const;
78 
79 	virtual	void				PrintToStream();
80 
81 private:
82 			friend class BKeyStore;
83 
84 			BKeyPurpose			fPurpose;
85 			BString				fIdentifier;
86 			BString				fSecondaryIdentifier;
87 			BString				fOwner;
88 			bigtime_t			fCreationTime;
89 	mutable	BMallocIO			fData;
90 };
91 
92 
93 class BPasswordKey : public BKey {
94 public:
95 								BPasswordKey();
96 								BPasswordKey(const char* password,
97 									BKeyPurpose purpose, const char* identifier,
98 									const char* secondaryIdentifier = NULL);
99 								BPasswordKey(BPasswordKey& other);
100 	virtual						~BPasswordKey();
101 
Type()102 	virtual	BKeyType			Type() const { return B_KEY_TYPE_PASSWORD; };
103 
104 			status_t			SetTo(const char* password,
105 									BKeyPurpose purpose,
106 									const char* identifier,
107 									const char* secondaryIdentifier = NULL);
108 
109 			status_t			SetPassword(const char* password);
110 			const char*			Password() const;
111 
112 	virtual	void				PrintToStream();
113 };
114 
115 #endif	// _KEY_H
116