diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-19 12:53:29 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-19 12:53:29 +0200 |
commit | 7ef64b65a42cdb65ef2e36649ff84fcf4bb00c66 (patch) | |
tree | 975a7cebf0ed685ab776152db1be67d01171c6e5 /src/messenger/messenger_api_message.c | |
parent | 39e327905f421c470b9cbd5c1ea548261bcae026 (diff) | |
download | gnunet-7ef64b65a42cdb65ef2e36649ff84fcf4bb00c66.tar.gz gnunet-7ef64b65a42cdb65ef2e36649ff84fcf4bb00c66.zip |
BUILD: Move messenger to service
Diffstat (limited to 'src/messenger/messenger_api_message.c')
-rw-r--r-- | src/messenger/messenger_api_message.c | 1026 |
1 files changed, 0 insertions, 1026 deletions
diff --git a/src/messenger/messenger_api_message.c b/src/messenger/messenger_api_message.c deleted file mode 100644 index 1f7e8dbf2..000000000 --- a/src/messenger/messenger_api_message.c +++ /dev/null | |||
@@ -1,1026 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020--2023 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.c | ||
23 | * @brief messenger api: client and service implementation of GNUnet MESSENGER service | ||
24 | */ | ||
25 | |||
26 | #include "platform.h" | ||
27 | #include "messenger_api_message.h" | ||
28 | |||
29 | struct GNUNET_MESSENGER_MessageSignature | ||
30 | { | ||
31 | struct GNUNET_CRYPTO_EccSignaturePurpose purpose; | ||
32 | struct GNUNET_HashCode hash; | ||
33 | }; | ||
34 | |||
35 | struct GNUNET_MESSENGER_ShortMessage | ||
36 | { | ||
37 | enum GNUNET_MESSENGER_MessageKind kind; | ||
38 | struct GNUNET_MESSENGER_MessageBody body; | ||
39 | }; | ||
40 | |||
41 | struct GNUNET_MESSENGER_Message* | ||
42 | create_message (enum GNUNET_MESSENGER_MessageKind kind) | ||
43 | { | ||
44 | struct GNUNET_MESSENGER_Message *message = GNUNET_new (struct | ||
45 | GNUNET_MESSENGER_Message); | ||
46 | |||
47 | message->header.kind = kind; | ||
48 | |||
49 | switch (message->header.kind) | ||
50 | { | ||
51 | case GNUNET_MESSENGER_KIND_NAME: | ||
52 | message->body.name.name = NULL; | ||
53 | break; | ||
54 | case GNUNET_MESSENGER_KIND_TEXT: | ||
55 | message->body.text.text = NULL; | ||
56 | break; | ||
57 | case GNUNET_MESSENGER_KIND_FILE: | ||
58 | message->body.file.uri = NULL; | ||
59 | break; | ||
60 | case GNUNET_MESSENGER_KIND_PRIVATE: | ||
61 | message->body.privacy.length = 0; | ||
62 | message->body.privacy.data = NULL; | ||
63 | break; | ||
64 | default: | ||
65 | break; | ||
66 | } | ||
67 | |||
68 | return message; | ||
69 | } | ||
70 | |||
71 | |||
72 | struct GNUNET_MESSENGER_Message* | ||
73 | copy_message (const struct GNUNET_MESSENGER_Message *message) | ||
74 | { | ||
75 | GNUNET_assert (message); | ||
76 | |||
77 | struct GNUNET_MESSENGER_Message *copy = GNUNET_new (struct | ||
78 | GNUNET_MESSENGER_Message); | ||
79 | |||
80 | GNUNET_memcpy (copy, message, sizeof(struct GNUNET_MESSENGER_Message)); | ||
81 | |||
82 | switch (message->header.kind) | ||
83 | { | ||
84 | case GNUNET_MESSENGER_KIND_NAME: | ||
85 | copy->body.name.name = GNUNET_strdup (message->body.name.name); | ||
86 | break; | ||
87 | case GNUNET_MESSENGER_KIND_TEXT: | ||
88 | copy->body.text.text = GNUNET_strdup (message->body.text.text); | ||
89 | break; | ||
90 | case GNUNET_MESSENGER_KIND_FILE: | ||
91 | copy->body.file.uri = GNUNET_strdup (message->body.file.uri); | ||
92 | break; | ||
93 | case GNUNET_MESSENGER_KIND_PRIVATE: | ||
94 | copy->body.privacy.data = copy->body.privacy.length ? GNUNET_malloc ( | ||
95 | copy->body.privacy.length) : NULL; | ||
96 | |||
97 | if (copy->body.privacy.data) | ||
98 | { | ||
99 | GNUNET_memcpy (copy->body.privacy.data, message->body.privacy.data, | ||
100 | copy->body.privacy.length); | ||
101 | } | ||
102 | |||
103 | break; | ||
104 | default: | ||
105 | break; | ||
106 | } | ||
107 | |||
108 | return copy; | ||
109 | } | ||
110 | |||
111 | |||
112 | static void | ||
113 | destroy_message_body (enum GNUNET_MESSENGER_MessageKind kind, | ||
114 | struct GNUNET_MESSENGER_MessageBody *body) | ||
115 | { | ||
116 | switch (kind) | ||
117 | { | ||
118 | case GNUNET_MESSENGER_KIND_NAME: | ||
119 | GNUNET_free (body->name.name); | ||
120 | break; | ||
121 | case GNUNET_MESSENGER_KIND_TEXT: | ||
122 | GNUNET_free (body->text.text); | ||
123 | break; | ||
124 | case GNUNET_MESSENGER_KIND_FILE: | ||
125 | GNUNET_free (body->file.uri); | ||
126 | break; | ||
127 | case GNUNET_MESSENGER_KIND_PRIVATE: | ||
128 | GNUNET_free (body->privacy.data); | ||
129 | break; | ||
130 | default: | ||
131 | break; | ||
132 | } | ||
133 | } | ||
134 | |||
135 | |||
136 | void | ||
137 | cleanup_message (struct GNUNET_MESSENGER_Message *message) | ||
138 | { | ||
139 | GNUNET_assert (message); | ||
140 | |||
141 | destroy_message_body (message->header.kind, &(message->body)); | ||
142 | } | ||
143 | |||
144 | |||
145 | void | ||
146 | destroy_message (struct GNUNET_MESSENGER_Message *message) | ||
147 | { | ||
148 | GNUNET_assert (message); | ||
149 | |||
150 | destroy_message_body (message->header.kind, &(message->body)); | ||
151 | |||
152 | GNUNET_free (message); | ||
153 | } | ||
154 | |||
155 | |||
156 | int | ||
157 | is_message_session_bound (const struct GNUNET_MESSENGER_Message *message) | ||
158 | { | ||
159 | GNUNET_assert (message); | ||
160 | |||
161 | if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) || | ||
162 | (GNUNET_MESSENGER_KIND_LEAVE == message->header.kind) || | ||
163 | (GNUNET_MESSENGER_KIND_NAME == message->header.kind) || | ||
164 | (GNUNET_MESSENGER_KIND_KEY == message->header.kind) || | ||
165 | (GNUNET_MESSENGER_KIND_ID == message->header.kind)) | ||
166 | return GNUNET_YES; | ||
167 | else | ||
168 | return GNUNET_NO; | ||
169 | } | ||
170 | |||
171 | |||
172 | static void | ||
173 | fold_short_message (const struct GNUNET_MESSENGER_Message *message, | ||
174 | struct GNUNET_MESSENGER_ShortMessage *shortened) | ||
175 | { | ||
176 | shortened->kind = message->header.kind; | ||
177 | |||
178 | GNUNET_memcpy (&(shortened->body), &(message->body), sizeof(struct | ||
179 | GNUNET_MESSENGER_MessageBody)); | ||
180 | } | ||
181 | |||
182 | |||
183 | static void | ||
184 | unfold_short_message (struct GNUNET_MESSENGER_ShortMessage *shortened, | ||
185 | struct GNUNET_MESSENGER_Message *message) | ||
186 | { | ||
187 | destroy_message_body (message->header.kind, &(message->body)); | ||
188 | |||
189 | message->header.kind = shortened->kind; | ||
190 | |||
191 | GNUNET_memcpy (&(message->body), &(shortened->body), sizeof(struct | ||
192 | GNUNET_MESSENGER_MessageBody)); | ||
193 | } | ||
194 | |||
195 | |||
196 | #define member_size(type, member) sizeof(((type*) NULL)->member) | ||
197 | |||
198 | static uint16_t | ||
199 | get_message_body_kind_size (enum GNUNET_MESSENGER_MessageKind kind) | ||
200 | { | ||
201 | uint16_t length = 0; | ||
202 | |||
203 | switch (kind) | ||
204 | { | ||
205 | case GNUNET_MESSENGER_KIND_INFO: | ||
206 | length += member_size (struct GNUNET_MESSENGER_Message, | ||
207 | body.info.messenger_version); | ||
208 | break; | ||
209 | case GNUNET_MESSENGER_KIND_PEER: | ||
210 | length += member_size (struct GNUNET_MESSENGER_Message, body.peer.peer); | ||
211 | break; | ||
212 | case GNUNET_MESSENGER_KIND_ID: | ||
213 | length += member_size (struct GNUNET_MESSENGER_Message, body.id.id); | ||
214 | break; | ||
215 | case GNUNET_MESSENGER_KIND_MISS: | ||
216 | length += member_size (struct GNUNET_MESSENGER_Message, body.miss.peer); | ||
217 | break; | ||
218 | case GNUNET_MESSENGER_KIND_MERGE: | ||
219 | length += member_size (struct GNUNET_MESSENGER_Message, | ||
220 | body.merge.previous); | ||
221 | break; | ||
222 | case GNUNET_MESSENGER_KIND_REQUEST: | ||
223 | length += member_size (struct GNUNET_MESSENGER_Message, body.request.hash); | ||
224 | break; | ||
225 | case GNUNET_MESSENGER_KIND_INVITE: | ||
226 | length += member_size (struct GNUNET_MESSENGER_Message, body.invite.door); | ||
227 | length += member_size (struct GNUNET_MESSENGER_Message, body.invite.key); | ||
228 | break; | ||
229 | case GNUNET_MESSENGER_KIND_TEXT: | ||
230 | break; | ||
231 | case GNUNET_MESSENGER_KIND_FILE: | ||
232 | length += member_size (struct GNUNET_MESSENGER_Message, body.file.key); | ||
233 | length += member_size (struct GNUNET_MESSENGER_Message, body.file.hash); | ||
234 | length += member_size (struct GNUNET_MESSENGER_Message, body.file.name); | ||
235 | break; | ||
236 | case GNUNET_MESSENGER_KIND_DELETE: | ||
237 | length += member_size (struct GNUNET_MESSENGER_Message, body.deletion.hash); | ||
238 | length += member_size (struct GNUNET_MESSENGER_Message, | ||
239 | body.deletion.delay); | ||
240 | break; | ||
241 | default: | ||
242 | break; | ||
243 | } | ||
244 | |||
245 | return length; | ||
246 | } | ||
247 | |||
248 | |||
249 | typedef uint32_t kind_t; | ||
250 | |||
251 | uint16_t | ||
252 | get_message_kind_size (enum GNUNET_MESSENGER_MessageKind kind, | ||
253 | int include_header) | ||
254 | { | ||
255 | uint16_t length = 0; | ||
256 | |||
257 | if (GNUNET_YES == include_header) | ||
258 | { | ||
259 | length += member_size (struct GNUNET_MESSENGER_Message, header.timestamp); | ||
260 | length += member_size (struct GNUNET_MESSENGER_Message, header.sender_id); | ||
261 | length += member_size (struct GNUNET_MESSENGER_Message, header.previous); | ||
262 | } | ||
263 | |||
264 | length += sizeof(kind_t); | ||
265 | |||
266 | return length + get_message_body_kind_size (kind); | ||
267 | } | ||
268 | |||
269 | |||
270 | static uint16_t | ||
271 | get_message_body_size (enum GNUNET_MESSENGER_MessageKind kind, | ||
272 | const struct GNUNET_MESSENGER_MessageBody *body) | ||
273 | { | ||
274 | uint16_t length = 0; | ||
275 | |||
276 | switch (kind) | ||
277 | { | ||
278 | case GNUNET_MESSENGER_KIND_INFO: | ||
279 | length += GNUNET_CRYPTO_public_key_get_length (&(body->info.host_key)); | ||
280 | break; | ||
281 | case GNUNET_MESSENGER_KIND_JOIN: | ||
282 | length += GNUNET_CRYPTO_public_key_get_length (&(body->join.key)); | ||
283 | break; | ||
284 | case GNUNET_MESSENGER_KIND_NAME: | ||
285 | length += (body->name.name ? strlen (body->name.name) : 0); | ||
286 | break; | ||
287 | case GNUNET_MESSENGER_KIND_KEY: | ||
288 | length += GNUNET_CRYPTO_public_key_get_length (&(body->key.key)); | ||
289 | break; | ||
290 | case GNUNET_MESSENGER_KIND_TEXT: | ||
291 | length += strlen (body->text.text); | ||
292 | break; | ||
293 | case GNUNET_MESSENGER_KIND_FILE: | ||
294 | length += strlen (body->file.uri); | ||
295 | break; | ||
296 | case GNUNET_MESSENGER_KIND_PRIVATE: | ||
297 | length += body->privacy.length; | ||
298 | break; | ||
299 | default: | ||
300 | break; | ||
301 | } | ||
302 | |||
303 | return length; | ||
304 | } | ||
305 | |||
306 | |||
307 | uint16_t | ||
308 | get_message_size (const struct GNUNET_MESSENGER_Message *message, | ||
309 | int include_header) | ||
310 | { | ||
311 | GNUNET_assert (message); | ||
312 | |||
313 | uint16_t length = 0; | ||
314 | |||
315 | if (GNUNET_YES == include_header) | ||
316 | length += GNUNET_CRYPTO_signature_get_length ( | ||
317 | &(message->header.signature)); | ||
318 | |||
319 | length += get_message_kind_size (message->header.kind, include_header); | ||
320 | length += get_message_body_size (message->header.kind, &(message->body)); | ||
321 | |||
322 | return length; | ||
323 | } | ||
324 | |||
325 | |||
326 | static uint16_t | ||
327 | get_short_message_size (const struct GNUNET_MESSENGER_ShortMessage *message, | ||
328 | int include_body) | ||
329 | { | ||
330 | const uint16_t minimum_size = sizeof(struct GNUNET_HashCode) + sizeof(kind_t); | ||
331 | |||
332 | if (message) | ||
333 | return minimum_size + get_message_body_kind_size (message->kind) | ||
334 | + (include_body == GNUNET_YES? get_message_body_size (message->kind, | ||
335 | &(message->body)) | ||
336 | : 0); | ||
337 | else | ||
338 | return minimum_size; | ||
339 | } | ||
340 | |||
341 | |||
342 | static uint16_t | ||
343 | calc_usual_padding () | ||
344 | { | ||
345 | uint16_t padding = 0; | ||
346 | uint16_t kind_size; | ||
347 | |||
348 | for (int i = 0; i <= GNUNET_MESSENGER_KIND_MAX; i++) | ||
349 | { | ||
350 | kind_size = get_message_kind_size ((enum GNUNET_MESSENGER_MessageKind) i, | ||
351 | GNUNET_YES); | ||
352 | |||
353 | if (kind_size > padding) | ||
354 | padding = kind_size; | ||
355 | } | ||
356 | |||
357 | return padding + GNUNET_MESSENGER_PADDING_MIN; | ||
358 | } | ||
359 | |||
360 | |||
361 | #define max(x, y) (x > y? x : y) | ||
362 | |||
363 | static uint16_t | ||
364 | calc_padded_length (uint16_t length) | ||
365 | { | ||
366 | static uint16_t usual_padding = 0; | ||
367 | |||
368 | if (! usual_padding) | ||
369 | usual_padding = calc_usual_padding (); | ||
370 | |||
371 | const uint16_t padded_length = max ( | ||
372 | length + GNUNET_MESSENGER_PADDING_MIN, | ||
373 | usual_padding | ||
374 | ); | ||
375 | |||
376 | if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL0) | ||
377 | return GNUNET_MESSENGER_PADDING_LEVEL0; | ||
378 | |||
379 | if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL1) | ||
380 | return GNUNET_MESSENGER_PADDING_LEVEL1; | ||
381 | |||
382 | if (padded_length <= GNUNET_MESSENGER_PADDING_LEVEL2) | ||
383 | return GNUNET_MESSENGER_PADDING_LEVEL2; | ||
384 | |||
385 | return GNUNET_MESSENGER_MAX_MESSAGE_SIZE; | ||
386 | |||
387 | } | ||
388 | |||
389 | |||
390 | #define min(x, y) (x < y? x : y) | ||
391 | |||
392 | #define encode_step_ext(dst, offset, src, size) do { \ | ||
393 | GNUNET_memcpy (dst + offset, src, size); \ | ||
394 | offset += size; \ | ||
395 | } while (0) | ||
396 | |||
397 | #define encode_step(dst, offset, src) do { \ | ||
398 | encode_step_ext (dst, offset, src, sizeof(*src)); \ | ||
399 | } while (0) | ||
400 | |||
401 | #define encode_step_key(dst, offset, src, length) do { \ | ||
402 | ssize_t result = GNUNET_CRYPTO_write_public_key_to_buffer ( \ | ||
403 | src, dst + offset, length - offset \ | ||
404 | ); \ | ||
405 | if (result < 0) \ | ||
406 | GNUNET_break (0); \ | ||
407 | else \ | ||
408 | offset += result; \ | ||
409 | } while (0) | ||
410 | |||
411 | #define encode_step_signature(dst, offset, src, length) do { \ | ||
412 | ssize_t result = GNUNET_CRYPTO_write_signature_to_buffer ( \ | ||
413 | src, dst + offset, length - offset \ | ||
414 | ); \ | ||
415 | if (result < 0) \ | ||
416 | GNUNET_break (0); \ | ||
417 | else \ | ||
418 | offset += result; \ | ||
419 | } while (0) | ||
420 | |||
421 | static void | ||
422 | encode_message_body (enum GNUNET_MESSENGER_MessageKind kind, | ||
423 | const struct GNUNET_MESSENGER_MessageBody *body, | ||
424 | uint16_t length, | ||
425 | char *buffer, | ||
426 | uint16_t offset) | ||
427 | { | ||
428 | uint32_t version; | ||
429 | switch (kind) | ||
430 | { | ||
431 | case GNUNET_MESSENGER_KIND_INFO: | ||
432 | version = GNUNET_htobe32 (body->info.messenger_version); | ||
433 | |||
434 | encode_step_key (buffer, offset, &(body->info.host_key), length); | ||
435 | encode_step (buffer, offset, &version); | ||
436 | break; | ||
437 | case GNUNET_MESSENGER_KIND_JOIN: | ||
438 | encode_step_key (buffer, offset, &(body->join.key), length); | ||
439 | break; | ||
440 | case GNUNET_MESSENGER_KIND_NAME: | ||
441 | if (body->name.name) | ||
442 | encode_step_ext (buffer, offset, body->name.name, min (length - offset, | ||
443 | strlen ( | ||
444 | body->name.name))); | ||
445 | break; | ||
446 | case GNUNET_MESSENGER_KIND_KEY: | ||
447 | encode_step_key (buffer, offset, &(body->key.key), length); | ||
448 | break; | ||
449 | case GNUNET_MESSENGER_KIND_PEER: | ||
450 | encode_step (buffer, offset, &(body->peer.peer)); | ||
451 | break; | ||
452 | case GNUNET_MESSENGER_KIND_ID: | ||
453 | encode_step (buffer, offset, &(body->id.id)); | ||
454 | break; | ||
455 | case GNUNET_MESSENGER_KIND_MISS: | ||
456 | encode_step (buffer, offset, &(body->miss.peer)); | ||
457 | break; | ||
458 | case GNUNET_MESSENGER_KIND_MERGE: | ||
459 | encode_step (buffer, offset, &(body->merge.previous)); | ||
460 | break; | ||
461 | case GNUNET_MESSENGER_KIND_REQUEST: | ||
462 | encode_step (buffer, offset, &(body->request.hash)); | ||
463 | break; | ||
464 | case GNUNET_MESSENGER_KIND_INVITE: | ||
465 | encode_step (buffer, offset, &(body->invite.door)); | ||
466 | encode_step (buffer, offset, &(body->invite.key)); | ||
467 | break; | ||
468 | case GNUNET_MESSENGER_KIND_TEXT: | ||
469 | encode_step_ext (buffer, offset, body->text.text, min (length - offset, | ||
470 | strlen ( | ||
471 | body->text.text))); | ||
472 | break; | ||
473 | case GNUNET_MESSENGER_KIND_FILE: | ||
474 | encode_step (buffer, offset, &(body->file.key)); | ||
475 | encode_step (buffer, offset, &(body->file.hash)); | ||
476 | encode_step_ext (buffer, offset, body->file.name, sizeof(body->file.name)); | ||
477 | encode_step_ext (buffer, offset, body->file.uri, min (length - offset, | ||
478 | strlen ( | ||
479 | body->file.uri))); | ||
480 | break; | ||
481 | case GNUNET_MESSENGER_KIND_PRIVATE: | ||
482 | encode_step_ext (buffer, offset, body->privacy.data, min (length - offset, | ||
483 | body->privacy. | ||
484 | length)); | ||
485 | break; | ||
486 | case GNUNET_MESSENGER_KIND_DELETE: | ||
487 | encode_step (buffer, offset, &(body->deletion.hash)); | ||
488 | encode_step (buffer, offset, &(body->deletion.delay)); | ||
489 | break; | ||
490 | default: | ||
491 | break; | ||
492 | } | ||
493 | |||
494 | if (offset >= length) | ||
495 | return; | ||
496 | |||
497 | const uint16_t padding = length - offset; | ||
498 | const uint16_t used_padding = sizeof(padding) + sizeof(char); | ||
499 | |||
500 | GNUNET_assert (padding >= used_padding); | ||
501 | |||
502 | buffer[offset++] = '\0'; | ||
503 | |||
504 | if (padding > used_padding) | ||
505 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, buffer + offset, | ||
506 | padding - used_padding); | ||
507 | |||
508 | GNUNET_memcpy (buffer + length - sizeof(padding), &padding, sizeof(padding)); | ||
509 | } | ||
510 | |||
511 | |||
512 | void | ||
513 | encode_message (const struct GNUNET_MESSENGER_Message *message, | ||
514 | uint16_t length, | ||
515 | char *buffer, | ||
516 | int include_header) | ||
517 | { | ||
518 | GNUNET_assert ((message) && (buffer)); | ||
519 | |||
520 | uint16_t offset = 0; | ||
521 | |||
522 | if (GNUNET_YES == include_header) | ||
523 | encode_step_signature (buffer, offset, &(message->header.signature), | ||
524 | length); | ||
525 | |||
526 | const kind_t kind = GNUNET_htobe32 ((kind_t) message->header.kind); | ||
527 | |||
528 | if (GNUNET_YES == include_header) | ||
529 | { | ||
530 | encode_step (buffer, offset, &(message->header.timestamp)); | ||
531 | encode_step (buffer, offset, &(message->header.sender_id)); | ||
532 | encode_step (buffer, offset, &(message->header.previous)); | ||
533 | } | ||
534 | |||
535 | encode_step (buffer, offset, &kind); | ||
536 | |||
537 | encode_message_body (message->header.kind, &(message->body), length, buffer, | ||
538 | offset); | ||
539 | } | ||
540 | |||
541 | |||
542 | static void | ||
543 | encode_short_message (const struct GNUNET_MESSENGER_ShortMessage *message, | ||
544 | uint16_t length, | ||
545 | char *buffer) | ||
546 | { | ||
547 | struct GNUNET_HashCode hash; | ||
548 | uint16_t offset = sizeof(hash); | ||
549 | |||
550 | const kind_t kind = GNUNET_htobe32 ((kind_t) message->kind); | ||
551 | |||
552 | encode_step (buffer, offset, &kind); | ||
553 | |||
554 | encode_message_body (message->kind, &(message->body), length, buffer, offset); | ||
555 | |||
556 | GNUNET_CRYPTO_hash ( | ||
557 | buffer + sizeof(hash), | ||
558 | length - sizeof(hash), | ||
559 | &hash | ||
560 | ); | ||
561 | |||
562 | GNUNET_memcpy (buffer, &hash, sizeof(hash)); | ||
563 | } | ||
564 | |||
565 | |||
566 | #define decode_step_ext(src, offset, dst, size) do { \ | ||
567 | GNUNET_memcpy (dst, src + offset, size); \ | ||
568 | offset += size; \ | ||
569 | } while (0) | ||
570 | |||
571 | #define decode_step(src, offset, dst) do { \ | ||
572 | decode_step_ext (src, offset, dst, sizeof(*dst)); \ | ||
573 | } while (0) | ||
574 | |||
575 | #define decode_step_malloc(src, offset, dst, size, zero) do { \ | ||
576 | dst = GNUNET_malloc (size + zero); \ | ||
577 | if (zero) dst[size] = 0; \ | ||
578 | decode_step_ext (src, offset, dst, size); \ | ||
579 | } while (0) | ||
580 | |||
581 | #define decode_step_key(src, offset, dst, length) do { \ | ||
582 | enum GNUNET_GenericReturnValue result; \ | ||
583 | size_t read; \ | ||
584 | result = GNUNET_CRYPTO_read_public_key_from_buffer ( \ | ||
585 | src + offset, length - offset, dst, &read \ | ||
586 | ); \ | ||
587 | if (GNUNET_SYSERR == result) \ | ||
588 | GNUNET_break (0); \ | ||
589 | else \ | ||
590 | offset += read; \ | ||
591 | } while (0) | ||
592 | |||
593 | static uint16_t | ||
594 | decode_message_body (enum GNUNET_MESSENGER_MessageKind *kind, | ||
595 | struct GNUNET_MESSENGER_MessageBody *body, | ||
596 | uint16_t length, | ||
597 | const char *buffer, | ||
598 | uint16_t offset) | ||
599 | { | ||
600 | uint16_t padding = 0; | ||
601 | |||
602 | GNUNET_memcpy (&padding, buffer + length - sizeof(padding), sizeof(padding)); | ||
603 | |||
604 | if (padding > length - offset) | ||
605 | padding = 0; | ||
606 | |||
607 | const uint16_t end_zero = length - padding; | ||
608 | |||
609 | if ((padding) && (buffer[end_zero] != '\0')) | ||
610 | padding = 0; | ||
611 | |||
612 | length -= padding; | ||
613 | |||
614 | uint32_t version; | ||
615 | switch (*kind) | ||
616 | { | ||
617 | case GNUNET_MESSENGER_KIND_INFO: { | ||
618 | decode_step_key (buffer, offset, &(body->info.host_key), length); | ||
619 | decode_step (buffer, offset, &version); | ||
620 | |||
621 | body->info.messenger_version = GNUNET_be32toh (version); | ||
622 | break; | ||
623 | } case GNUNET_MESSENGER_KIND_JOIN: { | ||
624 | decode_step_key (buffer, offset, &(body->join.key), length); | ||
625 | break; | ||
626 | } case GNUNET_MESSENGER_KIND_NAME: | ||
627 | if (length - offset > 0) | ||
628 | decode_step_malloc (buffer, offset, body->name.name, length - offset, 1); | ||
629 | else | ||
630 | body->name.name = NULL; | ||
631 | break; | ||
632 | case GNUNET_MESSENGER_KIND_KEY: | ||
633 | decode_step_key (buffer, offset, &(body->key.key), length); | ||
634 | break; | ||
635 | case GNUNET_MESSENGER_KIND_PEER: | ||
636 | decode_step (buffer, offset, &(body->peer.peer)); | ||
637 | break; | ||
638 | case GNUNET_MESSENGER_KIND_ID: | ||
639 | decode_step (buffer, offset, &(body->id.id)); | ||
640 | break; | ||
641 | case GNUNET_MESSENGER_KIND_MISS: | ||
642 | decode_step (buffer, offset, &(body->miss.peer)); | ||
643 | break; | ||
644 | case GNUNET_MESSENGER_KIND_MERGE: | ||
645 | decode_step (buffer, offset, &(body->merge.previous)); | ||
646 | break; | ||
647 | case GNUNET_MESSENGER_KIND_REQUEST: | ||
648 | decode_step (buffer, offset, &(body->request.hash)); | ||
649 | break; | ||
650 | case GNUNET_MESSENGER_KIND_INVITE: | ||
651 | decode_step (buffer, offset, &(body->invite.door)); | ||
652 | decode_step (buffer, offset, &(body->invite.key)); | ||
653 | break; | ||
654 | case GNUNET_MESSENGER_KIND_TEXT: | ||
655 | decode_step_malloc (buffer, offset, body->text.text, length - offset, 1); | ||
656 | break; | ||
657 | case GNUNET_MESSENGER_KIND_FILE: | ||
658 | decode_step (buffer, offset, &(body->file.key)); | ||
659 | decode_step (buffer, offset, &(body->file.hash)); | ||
660 | decode_step_ext (buffer, offset, body->file.name, sizeof(body->file.name)); | ||
661 | decode_step_malloc (buffer, offset, body->file.uri, length - offset, 1); | ||
662 | break; | ||
663 | case GNUNET_MESSENGER_KIND_PRIVATE: | ||
664 | body->privacy.length = (length - offset); | ||
665 | decode_step_malloc (buffer, offset, body->privacy.data, length - offset, 0); | ||
666 | break; | ||
667 | case GNUNET_MESSENGER_KIND_DELETE: | ||
668 | decode_step (buffer, offset, &(body->deletion.hash)); | ||
669 | decode_step (buffer, offset, &(body->deletion.delay)); | ||
670 | break; | ||
671 | default: | ||
672 | *kind = GNUNET_MESSENGER_KIND_UNKNOWN; | ||
673 | break; | ||
674 | } | ||
675 | |||
676 | return padding; | ||
677 | } | ||
678 | |||
679 | |||
680 | int | ||
681 | decode_message (struct GNUNET_MESSENGER_Message *message, | ||
682 | uint16_t length, | ||
683 | const char *buffer, | ||
684 | int include_header, | ||
685 | uint16_t *padding) | ||
686 | { | ||
687 | GNUNET_assert ( | ||
688 | (message) && | ||
689 | (buffer) && | ||
690 | (length >= get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN, | ||
691 | include_header)) | ||
692 | ); | ||
693 | |||
694 | uint16_t offset = 0; | ||
695 | |||
696 | if (GNUNET_YES == include_header) | ||
697 | { | ||
698 | ssize_t result = GNUNET_CRYPTO_read_signature_from_buffer ( | ||
699 | &(message->header.signature), buffer, length - offset | ||
700 | ); | ||
701 | |||
702 | if (result < 0) | ||
703 | return GNUNET_NO; | ||
704 | else | ||
705 | offset += result; | ||
706 | } | ||
707 | |||
708 | const uint16_t count = length - offset; | ||
709 | |||
710 | if (count < get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN, | ||
711 | include_header)) | ||
712 | return GNUNET_NO; | ||
713 | |||
714 | kind_t kind; | ||
715 | |||
716 | if (GNUNET_YES == include_header) | ||
717 | { | ||
718 | decode_step (buffer, offset, &(message->header.timestamp)); | ||
719 | decode_step (buffer, offset, &(message->header.sender_id)); | ||
720 | decode_step (buffer, offset, &(message->header.previous)); | ||
721 | } | ||
722 | |||
723 | decode_step (buffer, offset, &kind); | ||
724 | |||
725 | message->header.kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh ( | ||
726 | kind); | ||
727 | |||
728 | if (count < get_message_kind_size (message->header.kind, include_header)) | ||
729 | return GNUNET_NO; | ||
730 | |||
731 | const uint16_t result = decode_message_body (&(message->header.kind), | ||
732 | &(message->body), length, buffer, | ||
733 | offset); | ||
734 | |||
735 | if (padding) | ||
736 | *padding = result; | ||
737 | |||
738 | return GNUNET_YES; | ||
739 | } | ||
740 | |||
741 | |||
742 | static int | ||
743 | decode_short_message (struct GNUNET_MESSENGER_ShortMessage *message, | ||
744 | uint16_t length, | ||
745 | const char *buffer) | ||
746 | { | ||
747 | struct GNUNET_HashCode expected, hash; | ||
748 | uint16_t offset = sizeof(hash); | ||
749 | |||
750 | if (length < get_short_message_size (NULL, GNUNET_NO)) | ||
751 | return GNUNET_NO; | ||
752 | |||
753 | GNUNET_memcpy (&hash, buffer, sizeof(hash)); | ||
754 | |||
755 | GNUNET_CRYPTO_hash ( | ||
756 | buffer + sizeof(hash), | ||
757 | length - sizeof(hash), | ||
758 | &expected | ||
759 | ); | ||
760 | |||
761 | if (0 != GNUNET_CRYPTO_hash_cmp (&hash, &expected)) | ||
762 | return GNUNET_NO; | ||
763 | |||
764 | kind_t kind; | ||
765 | |||
766 | decode_step (buffer, offset, &kind); | ||
767 | |||
768 | message->kind = (enum GNUNET_MESSENGER_MessageKind) GNUNET_be32toh (kind); | ||
769 | |||
770 | if (length < get_short_message_size (message, GNUNET_NO)) | ||
771 | return GNUNET_NO; | ||
772 | |||
773 | decode_message_body (&(message->kind), &(message->body), length, buffer, | ||
774 | offset); | ||
775 | |||
776 | if (GNUNET_MESSENGER_KIND_UNKNOWN == message->kind) | ||
777 | return GNUNET_NO; | ||
778 | |||
779 | return GNUNET_YES; | ||
780 | } | ||
781 | |||
782 | |||
783 | void | ||
784 | hash_message (const struct GNUNET_MESSENGER_Message *message, | ||
785 | uint16_t length, | ||
786 | const char *buffer, | ||
787 | struct GNUNET_HashCode *hash) | ||
788 | { | ||
789 | GNUNET_assert ((message) && (buffer) && (hash)); | ||
790 | |||
791 | const ssize_t offset = GNUNET_CRYPTO_signature_get_length ( | ||
792 | &(message->header.signature) | ||
793 | ); | ||
794 | |||
795 | GNUNET_CRYPTO_hash (buffer + offset, length - offset, hash); | ||
796 | } | ||
797 | |||
798 | |||
799 | void | ||
800 | sign_message (struct GNUNET_MESSENGER_Message *message, | ||
801 | uint16_t length, | ||
802 | char *buffer, | ||
803 | const struct GNUNET_HashCode *hash, | ||
804 | const struct GNUNET_MESSENGER_Ego *ego) | ||
805 | { | ||
806 | GNUNET_assert ((message) && (buffer) && (hash) && (ego)); | ||
807 | |||
808 | struct GNUNET_MESSENGER_MessageSignature signature; | ||
809 | |||
810 | signature.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE); | ||
811 | signature.purpose.size = htonl (sizeof(signature)); | ||
812 | |||
813 | GNUNET_memcpy (&(signature.hash), hash, sizeof(struct GNUNET_HashCode)); | ||
814 | GNUNET_CRYPTO_sign (&(ego->priv), &signature, &(message->header.signature)); | ||
815 | |||
816 | uint16_t offset = 0; | ||
817 | encode_step_signature (buffer, offset, &(message->header.signature), length); | ||
818 | } | ||
819 | |||
820 | |||
821 | int | ||
822 | verify_message (const struct GNUNET_MESSENGER_Message *message, | ||
823 | const struct GNUNET_HashCode *hash, | ||
824 | const struct GNUNET_CRYPTO_PublicKey *key) | ||
825 | { | ||
826 | GNUNET_assert ((message) && (hash) && (key)); | ||
827 | |||
828 | if (ntohl (key->type) != ntohl (message->header.signature.type)) | ||
829 | return GNUNET_SYSERR; | ||
830 | |||
831 | struct GNUNET_MESSENGER_MessageSignature signature; | ||
832 | |||
833 | signature.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE); | ||
834 | signature.purpose.size = htonl (sizeof(signature)); | ||
835 | |||
836 | GNUNET_memcpy (&(signature.hash), hash, sizeof(struct GNUNET_HashCode)); | ||
837 | |||
838 | return GNUNET_CRYPTO_signature_verify ( | ||
839 | GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE, &signature, | ||
840 | &(message->header.signature), key); | ||
841 | } | ||
842 | |||
843 | |||
844 | int | ||
845 | encrypt_message (struct GNUNET_MESSENGER_Message *message, | ||
846 | const struct GNUNET_CRYPTO_PublicKey *key) | ||
847 | { | ||
848 | GNUNET_assert ((message) && (key)); | ||
849 | |||
850 | struct GNUNET_MESSENGER_ShortMessage shortened; | ||
851 | |||
852 | fold_short_message (message, &shortened); | ||
853 | |||
854 | const uint16_t length = get_short_message_size (&shortened, GNUNET_YES); | ||
855 | const uint16_t padded_length = calc_padded_length ( | ||
856 | length + GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES | ||
857 | ); | ||
858 | |||
859 | message->header.kind = GNUNET_MESSENGER_KIND_PRIVATE; | ||
860 | message->body.privacy.data = GNUNET_malloc (padded_length); | ||
861 | message->body.privacy.length = padded_length; | ||
862 | |||
863 | const uint16_t encoded_length = ( | ||
864 | padded_length - GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES | ||
865 | ); | ||
866 | |||
867 | encode_short_message (&shortened, encoded_length, message->body.privacy.data); | ||
868 | |||
869 | if (GNUNET_OK != GNUNET_CRYPTO_encrypt (message->body.privacy.data, | ||
870 | encoded_length, | ||
871 | key, | ||
872 | message->body.privacy.data, | ||
873 | padded_length)) | ||
874 | { | ||
875 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Encrypting message failed!\n"); | ||
876 | |||
877 | unfold_short_message (&shortened, message); | ||
878 | return GNUNET_NO; | ||
879 | } | ||
880 | |||
881 | destroy_message_body (shortened.kind, &(shortened.body)); | ||
882 | return GNUNET_YES; | ||
883 | } | ||
884 | |||
885 | |||
886 | int | ||
887 | decrypt_message (struct GNUNET_MESSENGER_Message *message, | ||
888 | const struct GNUNET_CRYPTO_PrivateKey *key) | ||
889 | { | ||
890 | GNUNET_assert ((message) && (key)); | ||
891 | |||
892 | const uint16_t padded_length = message->body.privacy.length; | ||
893 | |||
894 | if (padded_length < GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES) | ||
895 | { | ||
896 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
897 | "Message length too short to decrypt!\n"); | ||
898 | |||
899 | return GNUNET_NO; | ||
900 | } | ||
901 | |||
902 | const uint16_t encoded_length = ( | ||
903 | padded_length - GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES | ||
904 | ); | ||
905 | |||
906 | if (GNUNET_OK != GNUNET_CRYPTO_decrypt (message->body.privacy.data, | ||
907 | padded_length, | ||
908 | key, | ||
909 | message->body.privacy.data, | ||
910 | encoded_length)) | ||
911 | { | ||
912 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Decrypting message failed!\n"); | ||
913 | |||
914 | return GNUNET_NO; | ||
915 | } | ||
916 | |||
917 | struct GNUNET_MESSENGER_ShortMessage shortened; | ||
918 | |||
919 | if (GNUNET_YES != decode_short_message (&shortened, | ||
920 | encoded_length, | ||
921 | message->body.privacy.data)) | ||
922 | { | ||
923 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
924 | "Decoding decrypted message failed!\n"); | ||
925 | |||
926 | return GNUNET_NO; | ||
927 | } | ||
928 | |||
929 | unfold_short_message (&shortened, message); | ||
930 | return GNUNET_YES; | ||
931 | } | ||
932 | |||
933 | |||
934 | struct GNUNET_MQ_Envelope* | ||
935 | pack_message (struct GNUNET_MESSENGER_Message *message, | ||
936 | struct GNUNET_HashCode *hash, | ||
937 | const struct GNUNET_MESSENGER_Ego *ego, | ||
938 | int mode) | ||
939 | { | ||
940 | GNUNET_assert (message); | ||
941 | |||
942 | if (ego) | ||
943 | message->header.signature.type = ego->priv.type; | ||
944 | |||
945 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
946 | "Packing message kind=%u and sender: %s\n", | ||
947 | message->header.kind, GNUNET_sh2s (&(message->header.sender_id))); | ||
948 | |||
949 | struct GNUNET_MessageHeader *header; | ||
950 | |||
951 | const uint16_t length = get_message_size (message, GNUNET_YES); | ||
952 | const uint16_t padded_length = calc_padded_length (length); | ||
953 | |||
954 | struct GNUNET_MQ_Envelope *env; | ||
955 | char *buffer; | ||
956 | |||
957 | if (GNUNET_MESSENGER_PACK_MODE_ENVELOPE == mode) | ||
958 | { | ||
959 | env = GNUNET_MQ_msg_extra (header, padded_length, | ||
960 | GNUNET_MESSAGE_TYPE_CADET_CLI); | ||
961 | |||
962 | buffer = (char*) &(header[1]); | ||
963 | } | ||
964 | else | ||
965 | { | ||
966 | env = NULL; | ||
967 | |||
968 | buffer = GNUNET_malloc (padded_length); | ||
969 | } | ||
970 | |||
971 | encode_message (message, padded_length, buffer, GNUNET_YES); | ||
972 | |||
973 | if (hash) | ||
974 | { | ||
975 | hash_message (message, length, buffer, hash); | ||
976 | |||
977 | if (ego) | ||
978 | sign_message (message, length, buffer, hash, ego); | ||
979 | } | ||
980 | |||
981 | if (GNUNET_MESSENGER_PACK_MODE_ENVELOPE != mode) | ||
982 | GNUNET_free (buffer); | ||
983 | |||
984 | return env; | ||
985 | } | ||
986 | |||
987 | |||
988 | int | ||
989 | filter_message_sending (const struct GNUNET_MESSENGER_Message *message) | ||
990 | { | ||
991 | switch (message->header.kind) | ||
992 | { | ||
993 | case GNUNET_MESSENGER_KIND_INFO: | ||
994 | return GNUNET_SYSERR; // Reserved for connection handling only! | ||
995 | case GNUNET_MESSENGER_KIND_JOIN: | ||
996 | return GNUNET_NO; // Use #GNUNET_MESSENGER_enter_room(...) instead! | ||
997 | case GNUNET_MESSENGER_KIND_LEAVE: | ||
998 | return GNUNET_NO; // Use #GNUNET_MESSENGER_close_room(...) instead! | ||
999 | case GNUNET_MESSENGER_KIND_NAME: | ||
1000 | return GNUNET_YES; | ||
1001 | case GNUNET_MESSENGER_KIND_KEY: | ||
1002 | return GNUNET_NO; // Use #GNUNET_MESSENGER_update(...) instead! | ||
1003 | case GNUNET_MESSENGER_KIND_PEER: | ||
1004 | return GNUNET_NO; // Use #GNUNET_MESSENGER_open_room(...) instead! | ||
1005 | case GNUNET_MESSENGER_KIND_ID: | ||
1006 | return GNUNET_SYSERR; // Reserved for member id handling only! | ||
1007 | case GNUNET_MESSENGER_KIND_MISS: | ||
1008 | return GNUNET_SYSERR; // Reserved for connection handling only! | ||
1009 | case GNUNET_MESSENGER_KIND_MERGE: | ||
1010 | return GNUNET_YES; | ||
1011 | case GNUNET_MESSENGER_KIND_REQUEST: | ||
1012 | return GNUNET_YES; | ||
1013 | case GNUNET_MESSENGER_KIND_INVITE: | ||
1014 | return GNUNET_YES; | ||
1015 | case GNUNET_MESSENGER_KIND_TEXT: | ||
1016 | return GNUNET_YES; | ||
1017 | case GNUNET_MESSENGER_KIND_FILE: | ||
1018 | return GNUNET_YES; | ||
1019 | case GNUNET_MESSENGER_KIND_PRIVATE: | ||
1020 | return GNUNET_NO; // Use #GNUNET_MESSENGER_send_message(...) with a contact instead! | ||
1021 | case GNUNET_MESSENGER_KIND_DELETE: | ||
1022 | return GNUNET_YES; | ||
1023 | default: | ||
1024 | return GNUNET_SYSERR; | ||
1025 | } | ||
1026 | } | ||