liserk_shared/
message.rs

1use crate::{message_type::MessageType, query::Query};
2use serde::{Deserialize, Serialize};
3///
4/// QueryOutput is a serialized output of the query
5pub type QueryOutput = (Vec<Vec<u8>>, Option<Vec<Vec<u8>>>);
6
7/// Enum representing different types of messages exchanged between the client and server.
8#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
9pub enum Message {
10    /// Message sent by the client when setting up a secure connection.
11    /// The associated `ClientSetupSecureConnection` contains the necessary information for establishing the secure connection.
12    ClientSetup(ClientSetupSecureConnection),
13
14    /// Message used for client authentication.
15    /// The associated `ClientAuthentication` typically contains the credentials needed for authentication.
16    ClientAuthentification(ClientAuthentication),
17
18    /// Used by the client to insert data into the database.
19    /// The `Insertion` structure typically contains the data to be inserted along with metadata such as the collection in which the data should be stored.
20    Insert(Insertion),
21
22    /// Similar to `Insert`, but used specifically for inserting data that is encrypted using Order-Preserving Encryption (OPE).
23    InsertOpe(InsertionOpe),
24
25    /// Sent by the server in response to an `Insert` message to acknowledge that the data has been inserted.
26    /// Contains the ID of the inserted data.
27    InsertResponse { inserted_id: String },
28
29    /// Used by the client to query data from the database.
30    /// The `Query` structure contains the necessary information to perform the data query.
31    Query(Query),
32
33    /// Sent by the server in response to a `Query` message.
34    /// Contains the data retrieved as a result of the query.
35    QueryResponse(QueryOutput),
36
37    /// Sent by the server in response to a query that requests a single value.
38    /// Contains the requested data, or None if it doesn't exist.
39    SingleValueResponse { data: Option<Vec<u8>>, nonce: Option<Vec<u8>> },
40
41    /// Message sent by the client to request a count of documents that meet certain criteria.
42    /// The `CountSubject` structure defines the criteria for counting.
43    Count(CountSubject),
44
45    /// Sent by the server in response to a `Count` message.
46    /// Contains the number of documents that meet the specified criteria.
47    CountResponse(u32),
48
49    /// Message sent by the client to request an update to existing data.
50    /// The `Update` structure contains the details of what data should be updated and how.
51    Update(Update),
52
53    /// Sent by the server in response to an `Update` message to indicate the status of the update operation.
54    UpdateResponse { status: UpdateStatus },
55
56    /// Message sent by the client to request the deletion of data.
57    /// The `Delete` structure contains the details of what data should be deleted.
58    Delete(Delete),
59
60    /// Sent by the server to indicate the result of a deletion request.
61    DeleteResult(bool),
62
63    /// Message sent by the client to delete data for a specific use case.
64    /// Contains the collection name and the ID of the document to be deleted.
65    DeleteForUsecase { collection: String, id: String },
66
67    /// Message sent by the client to request the deletion of an entire collection or use case.
68    /// The `DropSubject` structure defines what should be dropped.
69    Drop(DropSubject),
70
71    /// Sent by the server to indicate the result of a drop request.
72    DropResult(bool),
73
74    /// Message indicating the end of a communication sequence.
75    EndOfCommunication,
76
77    /// Message requesting the termination of the communication channel.
78    CloseCommunication,
79}
80
81impl Message {
82    pub fn message_type(&self) -> MessageType {
83        match self {
84            Message::ClientSetup(_) => MessageType::Setup,
85            Message::ClientAuthentification(_) => MessageType::Authentification,
86            Message::Insert(_) => MessageType::Insert,
87            Message::InsertOpe(_) => MessageType::InsertOpe,
88            Message::InsertResponse { .. } => MessageType::InsertResponse,
89            Message::Query(_) => MessageType::Query,
90            Message::QueryResponse { .. } => MessageType::QueryResponse,
91            Message::SingleValueResponse { .. } => MessageType::SingleValueResponse,
92            Message::Count(_) => MessageType::Count,
93            Message::CountResponse(_) => todo!(),
94            Message::Update { .. } => MessageType::Update,
95            Message::UpdateResponse { .. } => MessageType::UpdateResponse,
96            Message::Delete(_) => MessageType::Delete,
97            Message::DeleteResult(_) => MessageType::DeleteResult,
98            Message::DeleteForUsecase { .. } => MessageType::DeleteForUsecase,
99            Message::Drop(_) => MessageType::Drop,
100            Message::DropResult(_) => MessageType::DropResult,
101            Message::EndOfCommunication => MessageType::EndOfCommunication,
102            Message::CloseCommunication => MessageType::CloseCommunication,
103        }
104    }
105
106    pub fn setup_for_network(&self) -> Result<Vec<u8>, serde_cbor::Error> {
107        let message_type: MessageType = self.message_type();
108        let message_type: u8 = message_type as u8;
109        let message = serde_cbor::to_vec(&self)?;
110        let message_length = message.len() as u32;
111        let message_length = message_length.to_be_bytes();
112
113        let message_type_as_bytes = [message_type];
114        Ok([&message_type_as_bytes[..], &message_length, &message].concat())
115    }
116}
117
118#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
119pub enum CountSubject {
120    Collection(String),
121    Usecase { collection: String, usecase: String },
122}
123
124#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
125pub enum DropSubject {
126    Collection(String),
127    Usecase { collection: String, usecase: String },
128}
129
130#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
131pub struct ClientSetupSecureConnection {
132    protocol_version: String,
133    client_public_key: Vec<u8>,
134    cipher_suits: Vec<String>,
135    compression: String,
136}
137
138impl ClientSetupSecureConnection {
139    pub fn new(public_key: Vec<u8>) -> Self {
140        Self {
141            protocol_version: String::from("0.1.0"),
142            client_public_key: public_key,
143            cipher_suits: vec![String::from("kyber768"), String::from("falcon")],
144            compression: String::from("0"),
145        }
146    }
147}
148
149#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
150pub struct ClientAuthentication {
151    pub username: String,
152    pub password: String,
153}
154
155#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
156pub struct Update {
157    pub collection: String,
158    pub id: String,
159    pub new_value: Vec<u8>,
160}
161
162#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
163pub enum UpdateStatus {
164    Success,
165    Failure,
166    KeyNotFound,
167}
168
169#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
170pub struct Insertion {
171    pub collection: String,
172    pub acl: Vec<String>,
173    pub data: Vec<u8>,
174    pub usecases: Vec<String>,
175    pub nonce: Vec<u8>,
176}
177
178#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
179pub struct InsertionOpe {
180    pub collection: String,
181    pub acl: Vec<String>,
182    pub data: Vec<u8>,
183    pub usecases: Vec<String>,
184}
185
186#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
187pub struct Delete {
188    pub collection: String,
189    pub id: String,
190}