GNUnet 0.28.0-dev.2-28-ge1ff59a3e
 
Loading...
Searching...
No Matches
plugin_gnsrecord_messenger.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021--2026 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 */
27#include "gnunet_common.h"
28#include "platform.h"
29#include "gnunet_util_lib.h"
33
34
44static char *
46 uint32_t type,
47 const void *data,
48 size_t data_size)
49{
50 (void) cls;
51 switch (type)
52 {
54 {
55 if (data_size != sizeof(struct GNUNET_MESSENGER_RoomEntryRecord))
56 {
58 return NULL;
59 }
60 {
62
64 &(record->door.public_key));
66 &(record->key), sizeof(struct GNUNET_HashCode));
67
68 char *ret;
69 GNUNET_asprintf (&ret, "%s-%s", key, door);
72 return ret;
73 }
74 }
76 {
78 {
80 return NULL;
81 }
82 {
84
85 char *name = GNUNET_strndup (record->name, 256);
87 &(record->flags), sizeof(uint32_t));
88
89 char *ret;
90 GNUNET_asprintf (&ret, "%s-%s", flags, name);
93 return ret;
94 }
95 }
97 {
99 {
100 GNUNET_break_op (0);
101 return NULL;
102 }
103 {
105
107 &(record->key),
108 sizeof(struct GNUNET_HashCode));
110 &(record->hash),
111 sizeof(struct GNUNET_HashCode));
113 &(record->identifier),
114 sizeof(struct GNUNET_ShortHashCode));
116 &(record->nonce),
117 sizeof(record->nonce));
119 &(record->mac),
120 sizeof(record->mac));
122 &(record->shared_key),
123 sizeof(record->shared_key));
125 &(record->flags),
126 sizeof(uint32_t));
127
128 char *ret;
130 &ret,
131 "%s-%s-%s-%s-%s-%s-%s",
132 flags,
134 mac,
135 nonce,
137 hash,
138 key);
139
147 return ret;
148 }
149 }
151 {
153 {
154 GNUNET_break_op (0);
155 return NULL;
156 }
157 {
159
161 &(record->key),
162 sizeof(struct GNUNET_HashCode));
164 &(record->nonce),
165 sizeof(record->nonce));
167 &(record->mac),
168 sizeof(record->mac));
169 char *encryption_key_length = GNUNET_STRINGS_data_to_string_alloc (
170 &(record->encrypted_key_length),
171 sizeof(record->encrypted_key_length));
172 char *encryption_key = GNUNET_STRINGS_data_to_string_alloc (
173 &(record->encrypted_key_data),
174 sizeof(record->encrypted_key_data));
175
176 char *ret;
178 &ret,
179 "%s-%s-%s-%s-%s",
180 encryption_key,
181 encryption_key_length,
182 mac,
183 nonce,
184 key);
185
186 GNUNET_free (encryption_key);
187 GNUNET_free (encryption_key_length);
191 return ret;
192 }
193 }
194 default:
195 return NULL;
196 }
197}
198
199
211static int
213 uint32_t type,
214 const char *s,
215 void **data,
216 size_t *data_size)
217{
218 (void) cls;
219 if (NULL == s)
220 {
221 GNUNET_break (0);
222 return GNUNET_SYSERR;
223 }
224
225 switch (type)
226 {
228 {
229 char key[104];
230 const char *dash;
231 struct GNUNET_PeerIdentity door;
232
233 if ((NULL == (dash = strchr (s, '-'))) ||
234 (1 != sscanf (s, "%103s-", key)) ||
236 dash + 1, strlen (dash + 1), &(door.public_key))))
237 {
238 GNUNET_log (
240 _ ("Unable to parse MESSENGER_ROOM_ENTRY record `%s'\n"),
241 s);
242
243 return GNUNET_SYSERR;
244 }
245 {
248
250 key, strlen (key),
251 &(record->key), sizeof(struct GNUNET_HashCode)))
252 {
253 GNUNET_log (
255 _ ("Unable to parse MESSENGER_ROOM_ENTRY record `%s'\n"),
256 s);
257
259 return GNUNET_SYSERR;
260 }
261
262 record->door = door;
263 *data = record;
265 return GNUNET_OK;
266 }
267 }
269 {
270 char flags[8];
271 const char *dash;
272
273 if ((NULL == (dash = strchr (s, '-'))) ||
274 (1 != sscanf (s, "%7s-", flags)) ||
275 (strlen (dash + 1) > 256))
276 {
277 GNUNET_log (
279 _ ("Unable to parse MESSENGER_ROOM_DETAILS record `%s'\n"),
280 s);
281
282 return GNUNET_SYSERR;
283 }
284 {
287
289 flags, strlen (flags),
290 &(record->flags), sizeof(uint32_t)))
291 {
292 GNUNET_log (
294 _ ("Unable to parse MESSENGER_ROOM_DETAILS record `%s'\n"),
295 s);
296
298 return GNUNET_SYSERR;
299 }
300
301 GNUNET_memcpy (record->name, dash + 1, strlen (dash + 1));
302
303 *data = record;
305 return GNUNET_OK;
306 }
307 }
309 {
310 char key[104];
311 char hash[104];
312 char identifier[53];
313 char nonce[40];
314 char mac[27];
315 char shared_key[53];
316 char flags[8];
317 const char *dash;
318 const char *s0;
319
320 s0 = s;
321 if ((NULL == (dash = strchr (s0, '-'))) ||
322 (1 != sscanf (s0, "%7s-", flags)) ||
323 (strlen (dash + 1) < sizeof (shared_key)))
324 {
325 GNUNET_log (
327 _ ("Unable to parse MESSENGER_ROOM_EPOCH_KEY record `%s'\n"),
328 s);
329
330 return GNUNET_SYSERR;
331 }
332
333 s0 = dash + 1;
334 if ((NULL == (dash = strchr (s0, '-'))) ||
335 (1 != sscanf (s0, "%52s-", shared_key)) ||
336 (strlen (dash + 1) < sizeof (mac)))
337 {
338 GNUNET_log (
340 _ ("Unable to parse MESSENGER_ROOM_EPOCH_KEY record `%s'\n"),
341 s);
342
343 return GNUNET_SYSERR;
344 }
345
346 s0 = dash + 1;
347 if ((NULL == (dash = strchr (s0, '-'))) ||
348 (1 != sscanf (s0, "%26s-", mac)) ||
349 (strlen (dash + 1) < sizeof (nonce)))
350 {
351 GNUNET_log (
353 _ ("Unable to parse MESSENGER_ROOM_EPOCH_KEY record `%s'\n"),
354 s);
355
356 return GNUNET_SYSERR;
357 }
358
359 s0 = dash + 1;
360 if ((NULL == (dash = strchr (s0, '-'))) ||
361 (1 != sscanf (s0, "%39s-", nonce)) ||
362 (strlen (dash + 1) < sizeof (identifier)))
363 {
364 GNUNET_log (
366 _ ("Unable to parse MESSENGER_ROOM_EPOCH_KEY record `%s'\n"),
367 s);
368
369 return GNUNET_SYSERR;
370 }
371
372 s0 = dash + 1;
373 if ((NULL == (dash = strchr (s0, '-'))) ||
374 (1 != sscanf (s0, "%52s-", identifier)) ||
375 (strlen (dash + 1) < sizeof (hash)))
376 {
377 GNUNET_log (
379 _ ("Unable to parse MESSENGER_ROOM_EPOCH_KEY record `%s'\n"),
380 s);
381
382 return GNUNET_SYSERR;
383 }
384
385 s0 = dash + 1;
386 if ((NULL == (dash = strchr (s0, '-'))) ||
387 (1 != sscanf (s0, "%103s-", hash)) ||
388 (strlen (dash + 1) != sizeof (key)))
389 {
390 GNUNET_log (
392 _ ("Unable to parse MESSENGER_ROOM_EPOCH_KEY record `%s'\n"),
393 s);
394
395 return GNUNET_SYSERR;
396 }
397
398 GNUNET_memcpy (key, dash + 1, strlen (dash + 1));
399 key[103] = '\0';
400
401 {
404
406 flags, strlen (flags),
407 &(record->flags),
408 sizeof(record->flags)))
409 ||
411 shared_key, strlen (shared_key),
412 &(record->shared_key),
413 sizeof(record->shared_key)))
414 ||
416 mac, strlen (mac),
417 &(record->mac),
418 sizeof(record->mac)))
419 ||
421 nonce, strlen (nonce),
422 &(record->nonce),
423 sizeof(record->nonce)))
424 ||
426 identifier, strlen (identifier),
427 &(record->identifier),
428 sizeof(record->identifier)))
429 ||
431 hash, strlen (hash),
432 &(record->hash),
433 sizeof(record->hash)))
434 ||
436 key, strlen (key),
437 &(record->key),
438 sizeof(record->key))))
439 {
440 GNUNET_log (
442 _ ("Unable to parse MESSENGER_ROOM_EPOCH_KEY record `%s'\n"),
443 s);
444
446 return GNUNET_SYSERR;
447 }
448
449 *data = record;
451 return GNUNET_OK;
452 }
453 }
455 {
456 char key[104];
457 char nonce[40];
458 char mac[27];
459 char encryption_key_length[8];
460 char encryption_key[827];
461 const char *dash;
462 const char *s0;
463
464 s0 = s;
465 if ((NULL == (dash = strchr (s0, '-'))) ||
466 (1 != sscanf (s0, "%826s-", encryption_key)) ||
467 (strlen (dash + 1) < sizeof (encryption_key_length)))
468 {
469 GNUNET_log (
471 _ ("Unable to parse MESSENGER_ENCRYPTION_KEY record `%s'\n"),
472 s);
473
474 return GNUNET_SYSERR;
475 }
476
477 s0 = dash + 1;
478 if ((NULL == (dash = strchr (s0, '-'))) ||
479 (1 != sscanf (s0, "%7s-", encryption_key_length)) ||
480 (strlen (dash + 1) < sizeof (mac)))
481 {
482 GNUNET_log (
484 _ ("Unable to parse MESSENGER_ENCRYPTION_KEY record `%s'\n"),
485 s);
486
487 return GNUNET_SYSERR;
488 }
489
490 s0 = dash + 1;
491 if ((NULL == (dash = strchr (s0, '-'))) ||
492 (1 != sscanf (s0, "%26s-", mac)) ||
493 (strlen (dash + 1) < sizeof (nonce)))
494 {
495 GNUNET_log (
497 _ ("Unable to parse MESSENGER_ENCRYPTION_KEY record `%s'\n"),
498 s);
499
500 return GNUNET_SYSERR;
501 }
502
503 s0 = dash + 1;
504 if ((NULL == (dash = strchr (s0, '-'))) ||
505 (1 != sscanf (s0, "%39s-", nonce)) ||
506 (strlen (dash + 1) < sizeof (key)))
507 {
508 GNUNET_log (
510 _ ("Unable to parse MESSENGER_ENCRYPTION_KEY record `%s'\n"),
511 s);
512
513 return GNUNET_SYSERR;
514 }
515
516 GNUNET_memcpy (key, dash + 1, strlen (dash + 1));
517 key[103] = '\0';
518
519 {
522
524 encryption_key, strlen (encryption_key),
525 record->encrypted_key_data,
526 sizeof(record->encrypted_key_data)))
527 ||
529 encryption_key_length, strlen (encryption_key_length),
530 &(record->encrypted_key_length),
531 sizeof(record->encrypted_key_length)))
532 ||
534 mac, strlen (mac),
535 &(record->mac),
536 sizeof(record->mac)))
537 ||
539 nonce, strlen (nonce),
540 &(record->nonce),
541 sizeof(record->nonce)))
542 ||
544 key, strlen (key),
545 &(record->key),
546 sizeof(record->key))))
547 {
548 GNUNET_log (
550 _ ("Unable to parse MESSENGER_ENCRYPTION_KEY record `%s'\n"),
551 s);
552
554 return GNUNET_SYSERR;
555 }
556
557 *data = record;
559 return GNUNET_OK;
560 }
561 }
562 default:
563 return GNUNET_SYSERR;
564 }
565}
566
567
572static struct
573{
574 const char *name;
575 uint32_t number;
576} name_map[] = {
577 { "MESSENGER_ROOM_ENTRY", GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY },
578 { "MESSENGER_ROOM_DETAILS", GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_DETAILS },
579 { "MESSENGER_ROOM_EPOCH_KEY", GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_EPOCH_KEY }
580 ,
581 { "MESSENGER_ENCRYPTION_KEY", GNUNET_GNSRECORD_TYPE_MESSENGER_ENCRYPTION_KEY }
582 ,
583 { NULL, UINT32_MAX }
585
586
594static uint32_t
596 const char *gns_typename)
597{
598 unsigned int i;
599
600 (void) cls;
601 i = 0;
602 while ((name_map[i].name != NULL) &&
603 (0 != strcasecmp (gns_typename, name_map[i].name)))
604 i++;
605 return name_map[i].number;
606}
607
608
616static const char *
618 uint32_t type)
619{
620 unsigned int i;
621
622 (void) cls;
623 i = 0;
624 while ((name_map[i].name != NULL) &&
625 (type != name_map[i].number))
626 i++;
627 return name_map[i].name;
628}
629
630
631void *
633
640void *
653
654
655void *
657
664void *
666{
668
669 GNUNET_free (api);
670 return NULL;
671}
#define GNUNET_GNSRECORD_TYPE_MESSENGER_ENCRYPTION_KEY
Record type to store encryption keys used in messenger rooms.
#define GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_EPOCH_KEY
Record type to store epoch keys from a messenger room.
#define GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY
Record type to share an entry of a messenger room.
#define GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_DETAILS
Record type to store details about a messenger room.
static int ret
Final status code.
Definition gnunet-arm.c:93
static void record(void *cls, size_t data_size, const void *data)
Process recorded audio data.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
Plugin API for GNS record types.
#define GNUNET_log(kind,...)
char * GNUNET_CRYPTO_eddsa_public_key_to_string(const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a public key to a string.
Definition crypto_ecc.c:255
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_eddsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition crypto_ecc.c:361
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition strings.c:818
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition strings.c:843
#define _(String)
GNU gettext support macro.
Definition platform.h:179
void * libgnunet_plugin_gnsrecord_messenger_init(void *cls)
Entry point for the plugin.
void * libgnunet_plugin_gnsrecord_messenger_done(void *cls)
Exit point from the plugin.
static char * messenger_value_to_string(void *cls, uint32_t type, const void *data, size_t data_size)
Convert the 'value' of a record to a string.
static struct @47 name_map[]
Mapping of record type numbers to human-readable record type names.
const char * name
static const char * messenger_number_to_typename(void *cls, uint32_t type)
Convert a type number to the corresponding type string (e.g.
static uint32_t messenger_typename_to_number(void *cls, const char *gns_typename)
Convert a type name (e.g.
static int messenger_string_to_value(void *cls, uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of a 'value' of a record to the binary representation.
uint32_t number
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
GNUNET_GNSRECORD_TypenameToNumberFunction typename_to_number
Typename to number.
GNUNET_GNSRECORD_NumberToTypenameFunction number_to_typename
Number to typename.
GNUNET_GNSRECORD_ValueToStringFunction value_to_string
Conversion to string.
void * cls
Closure for all of the callbacks.
GNUNET_GNSRECORD_StringToValueFunction string_to_value
Conversion to binary.
A 512-bit hashcode.
An encryption key record specifies an encryption key for a given room that can be identified via a gi...
struct GNUNET_CRYPTO_AeadNonce nonce
The nonce for the AEAD encryption of the key.
struct GNUNET_CRYPTO_AeadMac mac
The mac from the AEAD encryption of the key.
A room details record specifies a custom name for a given room and some additional space for flags.
A room entry record specifies which peer is hosting a given room and may also specify the key to ente...
struct GNUNET_PeerIdentity door
The peer identity of an open door to a room.
A room epoch key record specifies an epoch key for a given room that can be identified via a given ke...
struct GNUNET_CRYPTO_AeadMac mac
The mac from the AEAD encryption of the key.
uint32_t flags
The flags of the epoch or group key.
struct GNUNET_ShortHashCode identifier
The short hash identifying the epoch or group key.
struct GNUNET_CRYPTO_AeadNonce nonce
The nonce for the AEAD encryption of the key.
struct GNUNET_CRYPTO_AeadSecretKey shared_key
The encrypted shared epoch or group key in the room.
struct GNUNET_HashCode hash
The hash identifying the epoch.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CRYPTO_EddsaPublicKey public_key
A 256-bit hashcode.