diff options
Diffstat (limited to 'src/service/messenger/messenger_api_message.h')
-rw-r--r-- | src/service/messenger/messenger_api_message.h | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/src/service/messenger/messenger_api_message.h b/src/service/messenger/messenger_api_message.h new file mode 100644 index 000000000..20b85dc03 --- /dev/null +++ b/src/service/messenger/messenger_api_message.h | |||
@@ -0,0 +1,351 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020--2024 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @author Tobias Frisch | ||
22 | * @file src/messenger/messenger_api_message.h | ||
23 | * @brief messenger api: client and service implementation of GNUnet MESSENGER service | ||
24 | */ | ||
25 | |||
26 | #ifndef GNUNET_MESSENGER_API_MESSAGE_H | ||
27 | #define GNUNET_MESSENGER_API_MESSAGE_H | ||
28 | |||
29 | #include "gnunet_util_lib.h" | ||
30 | |||
31 | #include "gnunet_messenger_service.h" | ||
32 | |||
33 | #define GNUNET_MESSENGER_MAX_MESSAGE_SIZE (GNUNET_MAX_MESSAGE_SIZE \ | ||
34 | - GNUNET_MIN_MESSAGE_SIZE) | ||
35 | |||
36 | #define GNUNET_MESSENGER_PADDING_MIN (sizeof(uint16_t) + sizeof(char)) | ||
37 | #define GNUNET_MESSENGER_PADDING_LEVEL0 (512) | ||
38 | #define GNUNET_MESSENGER_PADDING_LEVEL1 (4096) | ||
39 | #define GNUNET_MESSENGER_PADDING_LEVEL2 (32768) | ||
40 | |||
41 | /** | ||
42 | * Creates and allocates a new message with a specific <i>kind</i>. | ||
43 | * | ||
44 | * @param[in] kind Kind of message | ||
45 | * @return New message | ||
46 | */ | ||
47 | struct GNUNET_MESSENGER_Message* | ||
48 | create_message (enum GNUNET_MESSENGER_MessageKind kind); | ||
49 | |||
50 | /** | ||
51 | * Creates and allocates a copy of a given <i>message</i>. | ||
52 | * | ||
53 | * @param[in] message Message | ||
54 | * @return New message | ||
55 | */ | ||
56 | struct GNUNET_MESSENGER_Message* | ||
57 | copy_message (const struct GNUNET_MESSENGER_Message *message); | ||
58 | |||
59 | /** | ||
60 | * Copy message <i>header</i> details from another message to | ||
61 | * a given <i>message</i>. | ||
62 | * | ||
63 | * @param[in,out] message Message | ||
64 | * @param[in] header Message header | ||
65 | */ | ||
66 | void | ||
67 | copy_message_header (struct GNUNET_MESSENGER_Message *message, | ||
68 | const struct GNUNET_MESSENGER_MessageHeader *header); | ||
69 | |||
70 | /** | ||
71 | * Frees the messages body memory. | ||
72 | * | ||
73 | * @param[in,out] message Message | ||
74 | */ | ||
75 | void | ||
76 | cleanup_message (struct GNUNET_MESSENGER_Message *message); | ||
77 | |||
78 | /** | ||
79 | * Destroys a message and frees its memory fully. | ||
80 | * | ||
81 | * @param[in,out] message Message | ||
82 | */ | ||
83 | void | ||
84 | destroy_message (struct GNUNET_MESSENGER_Message *message); | ||
85 | |||
86 | /** | ||
87 | * Returns if the message should be bound to a member session. | ||
88 | * | ||
89 | * @param[in] message Message | ||
90 | * @return #GNUNET_YES or #GNUNET_NO | ||
91 | */ | ||
92 | enum GNUNET_GenericReturnValue | ||
93 | is_message_session_bound (const struct GNUNET_MESSENGER_Message *message); | ||
94 | |||
95 | /** | ||
96 | * Returns the minimal size in bytes to encode a message of a specific <i>kind</i>. | ||
97 | * | ||
98 | * @param[in] kind Kind of message | ||
99 | * @param[in] include_header Flag to include header | ||
100 | * @return Minimal size to encode | ||
101 | */ | ||
102 | uint16_t | ||
103 | get_message_kind_size (enum GNUNET_MESSENGER_MessageKind kind, | ||
104 | enum GNUNET_GenericReturnValue include_header); | ||
105 | |||
106 | /** | ||
107 | * Returns the exact size in bytes to encode a given <i>message</i>. | ||
108 | * | ||
109 | * @param[in] message Message | ||
110 | * @param[in] include_header Flag to include header | ||
111 | * @return Size to encode | ||
112 | */ | ||
113 | uint16_t | ||
114 | get_message_size (const struct GNUNET_MESSENGER_Message *message, | ||
115 | enum GNUNET_GenericReturnValue include_header); | ||
116 | |||
117 | /** | ||
118 | * Encodes a given <i>message</i> into a <i>buffer</i> of a maximal <i>length</i> in bytes. | ||
119 | * | ||
120 | * @param[in] message Message | ||
121 | * @param[in] length Maximal length to encode | ||
122 | * @param[out] buffer Buffer | ||
123 | * @param[in] include_header Flag to include header | ||
124 | */ | ||
125 | void | ||
126 | encode_message (const struct GNUNET_MESSENGER_Message *message, | ||
127 | uint16_t length, | ||
128 | char *buffer, | ||
129 | enum GNUNET_GenericReturnValue include_header); | ||
130 | |||
131 | /** | ||
132 | * Decodes a <i>message</i> from a given <i>buffer</i> of a maximal <i>length</i> in bytes. | ||
133 | * | ||
134 | * If the buffer is too small for a message of its decoded kind the function fails with | ||
135 | * resulting #GNUNET_NO after decoding only the messages header. | ||
136 | * | ||
137 | * On success the function returns #GNUNET_YES. | ||
138 | * | ||
139 | * @param[out] message Message | ||
140 | * @param[in] length Maximal length to decode | ||
141 | * @param[in] buffer Buffer | ||
142 | * @param[in] include_header Flag to include header | ||
143 | * @param[out] padding Padding | ||
144 | * @return #GNUNET_YES on success, otherwise #GNUNET_NO | ||
145 | */ | ||
146 | enum GNUNET_GenericReturnValue | ||
147 | decode_message (struct GNUNET_MESSENGER_Message *message, | ||
148 | uint16_t length, | ||
149 | const char *buffer, | ||
150 | enum GNUNET_GenericReturnValue include_header, | ||
151 | uint16_t *padding); | ||
152 | |||
153 | /** | ||
154 | * Calculates a <i>hash</i> of a given <i>buffer</i> with a <i>length</i> in bytes | ||
155 | * from a <i>message</i>. | ||
156 | * | ||
157 | * @param[in] message Message | ||
158 | * @param[in] length Length of buffer | ||
159 | * @param[in] buffer Buffer | ||
160 | * @param[out] hash Hash | ||
161 | */ | ||
162 | void | ||
163 | hash_message (const struct GNUNET_MESSENGER_Message *message, | ||
164 | uint16_t length, | ||
165 | const char *buffer, | ||
166 | struct GNUNET_HashCode *hash); | ||
167 | |||
168 | /** | ||
169 | * Signs the <i>hash</i> of a <i>message</i> with a given private <i>key</i> and writes | ||
170 | * the signature into the <i>buffer</i> as well. | ||
171 | * | ||
172 | * @param[in,out] message Message | ||
173 | * @param[in] length Length of buffer | ||
174 | * @param[out] buffer Buffer | ||
175 | * @param[in] hash Hash of message | ||
176 | * @param[in] key Private key | ||
177 | */ | ||
178 | void | ||
179 | sign_message (struct GNUNET_MESSENGER_Message *message, | ||
180 | uint16_t length, | ||
181 | char *buffer, | ||
182 | const struct GNUNET_HashCode *hash, | ||
183 | const struct GNUNET_CRYPTO_PrivateKey *key); | ||
184 | |||
185 | /** | ||
186 | * Signs the <i>hash</i> of a <i>message</i> with the peer identity of a given <i>config</i> | ||
187 | * and writes the signature into the <i>buffer</i> as well. | ||
188 | * | ||
189 | * @param[in,out] message Message | ||
190 | * @param[in] length Length of buffer | ||
191 | * @param[out] buffer Buffer | ||
192 | * @param[in] hash Hash of message | ||
193 | * @param[in] cfg Peer configuration | ||
194 | */ | ||
195 | void | ||
196 | sign_message_by_peer (struct GNUNET_MESSENGER_Message *message, | ||
197 | uint16_t length, | ||
198 | char *buffer, | ||
199 | const struct GNUNET_HashCode *hash, | ||
200 | const struct GNUNET_CONFIGURATION_Handle *cfg); | ||
201 | |||
202 | /** | ||
203 | * Verifies the signature of a given <i>message</i> and its <i>hash</i> with a specific | ||
204 | * public key. The function returns #GNUNET_OK if the signature was valid, otherwise | ||
205 | * #GNUNET_SYSERR. | ||
206 | * | ||
207 | * @param[in] message Message | ||
208 | * @param[in] hash Hash of message | ||
209 | * @param[in] key Public key | ||
210 | * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR | ||
211 | */ | ||
212 | enum GNUNET_GenericReturnValue | ||
213 | verify_message (const struct GNUNET_MESSENGER_Message *message, | ||
214 | const struct GNUNET_HashCode *hash, | ||
215 | const struct GNUNET_CRYPTO_PublicKey *key); | ||
216 | |||
217 | /** | ||
218 | * Verifies the signature of a given <i>message</i> and its <i>hash</i> with a specific | ||
219 | * peer's <i>identity</i>. The function returns #GNUNET_OK if the signature was valid, | ||
220 | * otherwise #GNUNET_SYSERR. | ||
221 | * | ||
222 | * @param[in] message Message | ||
223 | * @param[in] hash Hash of message | ||
224 | * @param[in] identity Peer identity | ||
225 | * @return #GNUNET_OK on success, otherwise #GNUNET_SYSERR | ||
226 | */ | ||
227 | enum GNUNET_GenericReturnValue | ||
228 | verify_message_by_peer (const struct GNUNET_MESSENGER_Message *message, | ||
229 | const struct GNUNET_HashCode *hash, | ||
230 | const struct GNUNET_PeerIdentity *identity); | ||
231 | |||
232 | /** | ||
233 | * Encrypts a <i>message</i> using a given public <i>key</i> and replaces its body | ||
234 | * and kind with the now private encrypted <i>message</i>. The function returns | ||
235 | * #GNUNET_YES if the operation succeeded, otherwise #GNUNET_NO. | ||
236 | * | ||
237 | * @param[in,out] message Message | ||
238 | * @param[in] key Public key | ||
239 | * @return #GNUNET_YES on success, otherwise #GNUNET_NO | ||
240 | */ | ||
241 | enum GNUNET_GenericReturnValue | ||
242 | encrypt_message (struct GNUNET_MESSENGER_Message *message, | ||
243 | const struct GNUNET_CRYPTO_PublicKey *key); | ||
244 | |||
245 | /** | ||
246 | * Decrypts a private <i>message</i> using a given private <i>key</i> and replaces its body | ||
247 | * and kind with the inner encrypted message. The function returns #GNUNET_YES if the | ||
248 | * operation succeeded, otherwise #GNUNET_NO. | ||
249 | * | ||
250 | * @param[in,out] message Message | ||
251 | * @param[in] key Private key | ||
252 | * @return #GNUNET_YES on success, otherwise #GNUNET_NO | ||
253 | */ | ||
254 | enum GNUNET_GenericReturnValue | ||
255 | decrypt_message (struct GNUNET_MESSENGER_Message *message, | ||
256 | const struct GNUNET_CRYPTO_PrivateKey *key); | ||
257 | |||
258 | /** | ||
259 | * Transcribes a <i>message</i> as a new transcript message using a given public | ||
260 | * <i>key</i> from the recipient of the encrypted message content. | ||
261 | * | ||
262 | * @param[in] message Message | ||
263 | * @param[in] key Public key | ||
264 | * @return Message transcript | ||
265 | */ | ||
266 | struct GNUNET_MESSENGER_Message* | ||
267 | transcribe_message (const struct GNUNET_MESSENGER_Message *message, | ||
268 | const struct GNUNET_CRYPTO_PublicKey *key); | ||
269 | |||
270 | /** | ||
271 | * Read the original message from a transcript <i>message</i> and replaces its body | ||
272 | * and kind with the inner encrypted message. The function returns #GNUNET_YES if the | ||
273 | * operation succeeded, otherwise #GNUNET_NO. | ||
274 | * | ||
275 | * @param[in,out] transcript Message transcript | ||
276 | * @return #GNUNET_YES on success, otherwise #GNUNET_NO | ||
277 | */ | ||
278 | enum GNUNET_GenericReturnValue | ||
279 | read_transcript_message (struct GNUNET_MESSENGER_Message *message); | ||
280 | |||
281 | typedef void (*GNUNET_MESSENGER_SignFunction)( | ||
282 | const void *cls, | ||
283 | struct GNUNET_MESSENGER_Message *message, | ||
284 | uint16_t length, | ||
285 | char *buffer, | ||
286 | const struct GNUNET_HashCode *hash | ||
287 | ); | ||
288 | |||
289 | enum GNUNET_MESSENGER_PackMode | ||
290 | { | ||
291 | GNUNET_MESSENGER_PACK_MODE_ENVELOPE = 0x1, | ||
292 | GNUNET_MESSENGER_PACK_MODE_UNKNOWN = 0x0, | ||
293 | }; | ||
294 | |||
295 | /** | ||
296 | * Encodes the <i>message</i> to pack it into a newly allocated envelope if <i>mode</i> | ||
297 | * is equal to #GNUNET_MESSENGER_PACK_MODE_ENVELOPE. Independent of the mode the message | ||
298 | * will be hashed if <i>hash</i> is not NULL and it will be signed if the <i>sign</i> | ||
299 | * function is not NULL. | ||
300 | * | ||
301 | * @param[out] message Message | ||
302 | * @param[out] hash Hash of message | ||
303 | * @param[in] sign Function to sign | ||
304 | * @param[in] mode Mode of packing | ||
305 | * @param[in,out] cls Closure for signing | ||
306 | * @return Envelope or NULL | ||
307 | */ | ||
308 | struct GNUNET_MQ_Envelope* | ||
309 | pack_message (struct GNUNET_MESSENGER_Message *message, | ||
310 | struct GNUNET_HashCode *hash, | ||
311 | const GNUNET_MESSENGER_SignFunction sign, | ||
312 | enum GNUNET_MESSENGER_PackMode mode, | ||
313 | const void *cls); | ||
314 | |||
315 | /** | ||
316 | * Returns whether a specific kind of message can be sent by the service without usage of a | ||
317 | * clients private key. The function returns #GNUNET_YES if the kind of message can be signed | ||
318 | * via a peer's identity, otherwise #GNUNET_NO. | ||
319 | * | ||
320 | * @param[in] message Message | ||
321 | * @return #GNUNET_YES if sending is allowed, #GNUNET_NO otherwise | ||
322 | */ | ||
323 | enum GNUNET_GenericReturnValue | ||
324 | is_peer_message (const struct GNUNET_MESSENGER_Message *message); | ||
325 | |||
326 | /** | ||
327 | * Returns whether a specific kind of message contains service critical information. That kind | ||
328 | * of information should not be encrypted via private messages for example to guarantee the | ||
329 | * service to work properly. The function returns #GNUNET_YES if the kind of message needs to | ||
330 | * be transferred accessible to all peers and their running service. It returns #GNUNET_NO | ||
331 | * if the message can be encrypted to specific subgroups of members without issues. If the kind | ||
332 | * of message is unknown it returns #GNUNET_SYSERR. | ||
333 | * | ||
334 | * @param[in] message Message | ||
335 | * @return #GNUNET_YES if encrypting is disallowed, #GNUNET_NO or #GNUNET_SYSERR otherwise | ||
336 | */ | ||
337 | enum GNUNET_GenericReturnValue | ||
338 | is_service_message (const struct GNUNET_MESSENGER_Message *message); | ||
339 | |||
340 | /** | ||
341 | * Returns whether a specific kind of message should be sent by a client. The function returns | ||
342 | * #GNUNET_YES or #GNUNET_NO for recommendations and #GNUNET_SYSERR for specific kinds | ||
343 | * of messages which should not be sent manually at all. | ||
344 | * | ||
345 | * @param[in] message Message | ||
346 | * @return #GNUNET_YES if sending is allowed, #GNUNET_NO or #GNUNET_SYSERR otherwise | ||
347 | */ | ||
348 | enum GNUNET_GenericReturnValue | ||
349 | filter_message_sending (const struct GNUNET_MESSENGER_Message *message); | ||
350 | |||
351 | #endif //GNUNET_MESSENGER_API_MESSAGE_H | ||