sentc_crypto_common/
group.rs

1use alloc::string::String;
2use alloc::vec::Vec;
3
4use serde::{Deserialize, Serialize};
5use serde_json::{from_str, to_string};
6
7use crate::{EncryptionKeyPairId, GroupId, SignKeyPairId, SymKeyId, UserId};
8
9#[derive(Serialize, Deserialize)]
10pub struct CreateData
11{
12	pub encrypted_group_key: String,
13	pub group_key_alg: String,
14	pub encrypted_group_key_alg: String,
15	pub encrypted_private_group_key: String,
16	pub public_group_key: String,
17	pub keypair_encrypt_alg: String,
18	pub creator_public_key_id: EncryptionKeyPairId,
19	pub encrypted_hmac_key: String,
20	pub encrypted_hmac_alg: String,
21	pub encrypted_sortable_key: String,
22	pub encrypted_sortable_alg: String,
23
24	//if user signed the new group key
25	#[serde(skip_serializing_if = "Option::is_none")]
26	pub signed_by_user_id: Option<UserId>,
27	#[serde(skip_serializing_if = "Option::is_none")]
28	pub signed_by_user_sign_key_id: Option<SignKeyPairId>,
29	#[serde(skip_serializing_if = "Option::is_none")]
30	pub group_key_sig: Option<String>,
31
32	//only for user group key rotation not for normal
33	#[serde(skip_serializing_if = "Option::is_none")]
34	pub encrypted_sign_key: Option<String>,
35	#[serde(skip_serializing_if = "Option::is_none")]
36	pub verify_key: Option<String>,
37	#[serde(skip_serializing_if = "Option::is_none")]
38	pub keypair_sign_alg: Option<String>,
39	#[serde(skip_serializing_if = "Option::is_none")]
40	pub public_key_sig: Option<String>,
41}
42
43impl CreateData
44{
45	pub fn from_string(v: &str) -> serde_json::Result<Self>
46	{
47		from_str::<Self>(v)
48	}
49
50	pub fn to_string(&self) -> serde_json::Result<String>
51	{
52		to_string(self)
53	}
54}
55
56#[derive(Serialize, Deserialize)]
57pub struct GroupCreateOutput
58{
59	pub group_id: GroupId,
60}
61
62#[derive(Serialize, Deserialize)]
63pub struct KeyRotationData
64{
65	pub encrypted_group_key_by_user: String, //encrypted by invoker public key
66	pub group_key_alg: String,
67	pub encrypted_group_key_alg: String, //info about how the encrypted group key was encrypted by the pk from the invoker (important for the server)
68	pub encrypted_private_group_key: String,
69	pub public_group_key: String,
70	pub keypair_encrypt_alg: String,
71	pub encrypted_group_key_by_ephemeral: String,
72	pub ephemeral_alg: String,
73	pub encrypted_ephemeral_key: String, //encrypted by the old group key. encrypt this key with every other member public key on the server
74	pub previous_group_key_id: SymKeyId,
75	pub invoker_public_key_id: EncryptionKeyPairId,
76
77	//if user signed the new group key
78	#[serde(skip_serializing_if = "Option::is_none")]
79	pub signed_by_user_id: Option<UserId>,
80	#[serde(skip_serializing_if = "Option::is_none")]
81	pub signed_by_user_sign_key_id: Option<SignKeyPairId>,
82	#[serde(skip_serializing_if = "Option::is_none")]
83	pub group_key_sig: Option<String>,
84
85	//only for user group key rotation not for normal
86	#[serde(skip_serializing_if = "Option::is_none")]
87	pub encrypted_sign_key: Option<String>,
88	#[serde(skip_serializing_if = "Option::is_none")]
89	pub verify_key: Option<String>,
90	#[serde(skip_serializing_if = "Option::is_none")]
91	pub keypair_sign_alg: Option<String>,
92	#[serde(skip_serializing_if = "Option::is_none")]
93	pub public_key_sig: Option<String>,
94}
95
96impl KeyRotationData
97{
98	pub fn from_string(v: &str) -> serde_json::Result<Self>
99	{
100		from_str::<Self>(v)
101	}
102
103	pub fn to_string(&self) -> serde_json::Result<String>
104	{
105		to_string(self)
106	}
107}
108
109#[derive(Serialize, Deserialize)]
110pub struct KeyRotationStartServerOutput
111{
112	pub group_id: GroupId,
113	pub key_id: SymKeyId,
114}
115
116#[derive(Serialize, Deserialize)]
117pub struct KeyRotationInput
118{
119	pub error: Option<String>,
120	pub encrypted_ephemeral_key_by_group_key_and_public_key: String,
121	pub encrypted_group_key_by_ephemeral: String,
122	pub ephemeral_alg: String,
123	pub encrypted_eph_key_key_id: EncryptionKeyPairId, //the public key id which was used to encrypt the eph key on the server.
124	pub previous_group_key_id: SymKeyId,               //use this in the client sdk to load the right group key from the storage
125	pub time: u128,
126	pub new_group_key_id: SymKeyId, //to finish the key rotation on the server
127}
128
129impl KeyRotationInput
130{
131	pub fn from_string(v: &str) -> serde_json::Result<Self>
132	{
133		from_str::<Self>(v)
134	}
135
136	pub fn to_string(&self) -> serde_json::Result<String>
137	{
138		to_string(self)
139	}
140}
141
142#[derive(Serialize, Deserialize)]
143pub struct DoneKeyRotationData
144{
145	pub encrypted_new_group_key: String,
146	pub public_key_id: EncryptionKeyPairId,
147	pub encrypted_alg: String, //the alg of the public key
148}
149
150impl DoneKeyRotationData
151{
152	pub fn from_string(v: &str) -> serde_json::Result<Self>
153	{
154		from_str::<Self>(v)
155	}
156
157	pub fn to_string(&self) -> serde_json::Result<String>
158	{
159		to_string(self)
160	}
161}
162
163/**
164# the current keys of a group
165
166contains:
167- encrypted group key
168- encrypted private group (e.g. for subgroup)
169- public key
170- and which public key was used to encrypt the group key
171
172A group can have multiple of these structs for each key rotation
173*/
174#[derive(Serialize, Deserialize)]
175pub struct GroupKeyServerOutput
176{
177	pub encrypted_group_key: String,
178	pub group_key_alg: String,
179	pub group_key_id: SymKeyId,
180	pub encrypted_private_group_key: String,
181	pub public_group_key: String,
182	pub keypair_encrypt_alg: String,
183	pub key_pair_id: EncryptionKeyPairId,
184	pub user_public_key_id: EncryptionKeyPairId, //to know what private key we should use to decrypt
185	pub time: u128,
186
187	//if user signed the new group key
188	#[serde(skip_serializing_if = "Option::is_none")]
189	pub signed_by_user_id: Option<UserId>,
190	#[serde(skip_serializing_if = "Option::is_none")]
191	pub signed_by_user_sign_key_id: Option<SignKeyPairId>,
192	#[serde(skip_serializing_if = "Option::is_none")]
193	pub group_key_sig: Option<String>,
194
195	#[serde(skip_serializing_if = "Option::is_none")]
196	pub encrypted_sign_key: Option<String>,
197	#[serde(skip_serializing_if = "Option::is_none")]
198	pub verify_key: Option<String>,
199	#[serde(skip_serializing_if = "Option::is_none")]
200	pub keypair_sign_alg: Option<String>,
201	#[serde(skip_serializing_if = "Option::is_none")]
202	pub keypair_sign_id: Option<SignKeyPairId>,
203	#[serde(skip_serializing_if = "Option::is_none")]
204	pub public_key_sig: Option<String>,
205	#[serde(skip_serializing_if = "Option::is_none")]
206	pub public_key_sig_key_id: Option<String>,
207}
208
209impl GroupKeyServerOutput
210{
211	pub fn from_string(v: &str) -> serde_json::Result<Self>
212	{
213		from_str::<Self>(v)
214	}
215
216	pub fn to_string(&self) -> serde_json::Result<String>
217	{
218		to_string(self)
219	}
220}
221
222#[derive(Serialize, Deserialize)]
223pub struct GroupHmacData
224{
225	pub id: SymKeyId,
226	pub encrypted_hmac_key: String,
227	pub encrypted_hmac_alg: String,
228	pub encrypted_hmac_encryption_key_id: SymKeyId,
229	pub time: u128,
230}
231
232#[derive(Serialize, Deserialize)]
233pub struct GroupSortableData
234{
235	pub id: SymKeyId,
236	pub encrypted_sortable_key: String,
237	pub encrypted_sortable_alg: String,
238	pub encrypted_sortable_encryption_key_id: SymKeyId,
239	pub time: u128,
240}
241
242#[derive(Serialize, Deserialize)]
243pub enum GroupUserAccessBy
244{
245	User,
246	Parent(GroupId),
247	GroupAsUser(GroupId),
248	GroupAsUserAsParent
249	{
250		parent: GroupId,
251		group_as_user: GroupId,
252	},
253}
254
255/**
256The same as the real group data but without the keys.
257 */
258#[derive(Serialize, Deserialize)]
259pub struct GroupLightServerData
260{
261	pub group_id: GroupId,
262	pub parent_group_id: Option<GroupId>,
263	pub rank: i32,
264	pub created_time: u128,
265	pub joined_time: u128,
266	pub access_by: GroupUserAccessBy,
267	pub is_connected_group: bool,
268}
269
270/**
271# The data about the group from the server
272
273save this in the sdk impl storage
274*/
275#[derive(Serialize, Deserialize)]
276pub struct GroupServerData
277{
278	pub group_id: GroupId,
279	pub parent_group_id: Option<GroupId>,
280	pub keys: Vec<GroupKeyServerOutput>,
281	pub hmac_keys: Vec<GroupHmacData>,
282	pub sortable_keys: Vec<GroupSortableData>,
283	pub key_update: bool,
284	pub rank: i32,
285	pub created_time: u128,
286	pub joined_time: u128,
287	pub access_by: GroupUserAccessBy,
288	pub is_connected_group: bool,
289}
290
291impl GroupServerData
292{
293	pub fn from_string(v: &str) -> serde_json::Result<Self>
294	{
295		from_str::<Self>(v)
296	}
297
298	pub fn to_string(&self) -> serde_json::Result<String>
299	{
300		to_string(self)
301	}
302}
303
304/**
305This values can be changed while the user still caches the old values in the client
306*/
307#[derive(Serialize, Deserialize)]
308pub struct GroupDataCheckUpdateServerOutput
309{
310	pub key_update: bool,
311	pub rank: i32,
312}
313
314#[derive(Serialize, Deserialize)]
315pub struct GroupDataCheckUpdateServerOutputLight
316{
317	pub rank: i32,
318}
319
320#[derive(Serialize, Deserialize)]
321pub struct GroupUserListItem
322{
323	pub user_id: UserId,
324	pub rank: i32,
325	pub joined_time: u128,
326	pub user_type: i32,
327}
328
329#[derive(Serialize, Deserialize)]
330pub struct GroupKeysForNewMember
331{
332	pub encrypted_group_key: String, //base64 encoded
333	pub alg: String,                 //the group key alg
334	pub encrypted_alg: String,       //the alg of the public encryption
335	pub key_id: SymKeyId,
336	pub user_public_key_id: EncryptionKeyPairId,
337}
338
339#[derive(Serialize, Deserialize)]
340pub struct GroupNewMemberLightInput
341{
342	pub rank: Option<i32>,
343}
344
345#[derive(Serialize, Deserialize)]
346pub struct GroupKeysForNewMemberServerInput
347{
348	pub keys: Vec<GroupKeysForNewMember>,
349	pub rank: Option<i32>,
350	pub key_session: bool, //when there are mor than 100 keys in this group -> the client wants a session
351}
352
353impl GroupKeysForNewMemberServerInput
354{
355	pub fn from_string(v: &str) -> serde_json::Result<Self>
356	{
357		from_str::<Self>(v)
358	}
359
360	pub fn to_string(&self) -> serde_json::Result<String>
361	{
362		to_string(self)
363	}
364}
365
366#[derive(Serialize, Deserialize)]
367pub struct GroupJoinReqList
368{
369	pub user_id: UserId,
370	pub time: u128,
371	pub user_type: i32, // 0 = normal user, 2 = group as member
372}
373
374#[derive(Serialize, Deserialize)]
375pub struct GroupInviteReqList
376{
377	pub group_id: GroupId,
378	pub time: u128,
379}
380
381#[derive(Serialize, Deserialize)]
382pub struct GroupInviteServerOutput
383{
384	#[serde(skip_serializing_if = "Option::is_none")]
385	pub session_id: Option<String>,
386	pub message: String,
387}
388
389#[derive(Serialize, Deserialize)]
390pub struct GroupAcceptJoinReqServerOutput
391{
392	#[serde(skip_serializing_if = "Option::is_none")]
393	pub session_id: Option<String>,
394	pub message: String,
395}
396
397#[derive(Serialize, Deserialize)]
398pub struct GroupChangeRankServerInput
399{
400	pub changed_user_id: UserId,
401	pub new_rank: i32,
402}
403
404impl GroupChangeRankServerInput
405{
406	pub fn from_string(v: &str) -> serde_json::Result<Self>
407	{
408		from_str::<Self>(v)
409	}
410
411	pub fn to_string(&self) -> serde_json::Result<String>
412	{
413		to_string(self)
414	}
415}
416
417#[derive(Serialize, Deserialize)]
418pub struct ListGroups
419{
420	pub group_id: GroupId,
421	pub time: u128,
422	pub joined_time: u128,
423	pub rank: i32,
424	pub parent: Option<GroupId>,
425}
426
427#[derive(Serialize, Deserialize)]
428pub struct GroupChildrenList
429{
430	pub group_id: GroupId,
431	pub time: u128,
432	pub parent: Option<GroupId>,
433}