GNUnet 0.25.2-11-g84e94e98c
 
Loading...
Searching...
No Matches
gnunet-identity.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2018, 2019 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 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
31
32
36#define TIMEOUT_STATUS_CODE 40
37
42
46static int list;
47
51static int monitor;
52
56static int private_keys;
57
61static unsigned int verbose;
62
66static int quiet;
67
71static int type_eddsa;
72
76static char *write_msg;
77
81static char *read_msg;
82
86static char *create_ego;
87
91static char *delete_ego;
92
96static char *privkey_ego;
97
101static char *pubkey_msg;
102
106static char *set_ego;
107
112
117
122
127
131static int global_ret;
132
133
139static void
140shutdown_task (void *cls)
141{
142 if (NULL != set_op)
143 {
145 set_op = NULL;
146 }
147 if (NULL != create_op)
148 {
150 create_op = NULL;
151 }
152 if (NULL != delete_op)
153 {
155 delete_op = NULL;
156 }
157 if (NULL != set_ego)
158 {
160 set_ego = NULL;
161 }
163 sh = NULL;
164}
165
166
170static void
172{
173 if ( (NULL == create_op) &&
174 (NULL == delete_op) &&
175 (NULL == set_op) &&
176 (NULL == write_msg) &&
177 (NULL == read_msg) &&
178 (! list) &&
179 (! monitor))
180 {
182 global_ret = 0;
184 }
185}
186
187
194static void
196 enum GNUNET_ErrorCode ec)
197{
199
200 *op = NULL;
201 if (GNUNET_EC_NONE != ec)
202 fprintf (stderr, "%s\n", GNUNET_ErrorCode_get_hint (ec));
203 test_finished ();
204}
205
206
214static void
216 const struct GNUNET_CRYPTO_BlindablePrivateKey *pk_created,
217 enum GNUNET_ErrorCode ec)
218{
220
221 *op = NULL;
222 if (NULL == pk_created)
223 {
224 fprintf (stderr,
225 _ ("Failed to create ego: %s\n"),
227 global_ret = ec;
228 }
229 else if (verbose)
230 {
232 char *pubs;
233
236 if (private_keys)
237 {
238 char *privs;
239
241 fprintf (stdout, "%s - %s\n", pubs, privs);
242 GNUNET_free (privs);
243 }
244 else
245 {
246 fprintf (stdout, "%s\n", pubs);
247 }
248 GNUNET_free (pubs);
249 }
250 test_finished ();
251}
252
253
258static void
260{
261 struct GNUNET_CRYPTO_BlindablePublicKey recipient;
262 struct GNUNET_CRYPTO_HpkePublicKey hpke_key = {0};
263 size_t msg_len = strlen (write_msg) + 1;
264 size_t ct_len = strlen (write_msg) + 1
266 unsigned char ct[ct_len];
269 {
272 &hpke_key));
274 NULL, 0, // FIXME provide?
275 NULL, 0,
276 (uint8_t*) write_msg,
277 msg_len,
278 ct, NULL))
279 {
280 char *serialized_msg;
281 serialized_msg = GNUNET_STRINGS_data_to_string_alloc (ct, ct_len);
282 fprintf (stdout,
283 "%s\n",
284 serialized_msg);
285 GNUNET_free (serialized_msg);
286 }
287 else
288 {
289 fprintf (stderr, "Error during encryption.\n");
290 global_ret = 1;
291 }
292 }
293 else
294 {
295 fprintf (stderr, "Invalid recipient public key.\n");
296 global_ret = 1;
297 }
298}
299
300
307static void
309{
310 struct GNUNET_CRYPTO_HpkePrivateKey hpke_key;
311 char *deserialized_msg;
312 size_t msg_len;
314 strlen (read_msg),
315 (void **) &
316 deserialized_msg,
317 &msg_len))
318 {
322 &hpke_key));
324 NULL, 0,
325 NULL, 0,
326 (uint8_t*)
327 deserialized_msg,
328 msg_len,
329 (uint8_t*)
330 deserialized_msg,
331 NULL))
332 {
333 deserialized_msg[msg_len - 1] = '\0';
334 fprintf (stdout,
335 "%s\n",
336 deserialized_msg);
337 }
338 else
339 {
340 fprintf (stderr, "Failed to decrypt message.\n");
341 global_ret = 1;
342 }
343 GNUNET_free (deserialized_msg);
344 }
345 else
346 {
347 fprintf (stderr, "Invalid message format.\n");
348 global_ret = 1;
349 }
350}
351
352
385static void
386print_ego (void *cls,
387 struct GNUNET_IDENTITY_Ego *ego,
388 void **ctx,
389 const char *identifier)
390{
392 char *s;
393 char *privs;
394
395 if ( (NULL == ego) &&
396 (NULL != set_ego) &&
397 (NULL != read_msg) )
398 {
399 fprintf (stderr,
400 "Ego `%s' is not known, cannot decrypt message.\n",
401 set_ego);
403 read_msg = NULL;
405 set_ego = NULL;
406 }
407 if ((NULL == ego) && (! monitor))
408 {
409 list = 0;
410 test_finished ();
411 return;
412 }
413 if (! (list | monitor) && (NULL == read_msg))
414 return;
415 if ( (NULL == ego) ||
416 (NULL == identifier) )
417 return;
418 if ( (NULL != set_ego) &&
419 (0 != strcmp (identifier,
420 set_ego)) )
421 return;
426 if ((NULL != read_msg) && (NULL != set_ego))
427 {
428 // due to the check above, set_ego and the identifier are equal
431 read_msg = NULL;
432 }
433 else if ((monitor) || (NULL != identifier))
434 {
435 if (quiet)
436 {
437 if (private_keys)
438 fprintf (stdout, "%s - %s\n", s, privs);
439 else
440 fprintf (stdout, "%s\n", s);
441 }
442 else
443 {
444 if (private_keys)
445 fprintf (stdout, "%s - %s - %s - %s\n",
446 identifier, s, privs,
447 (ntohl (pk_tmp.type) == GNUNET_PUBLIC_KEY_TYPE_ECDSA) ?
448 "ECDSA" : "EdDSA");
449 else
450 fprintf (stdout, "%s - %s - %s\n",
451 identifier, s,
452 (ntohl (pk_tmp.type) == GNUNET_PUBLIC_KEY_TYPE_ECDSA) ?
453 "ECDSA" : "EdDSA");
454
455 }
456 }
457 GNUNET_free (privs);
458 GNUNET_free (s);
459}
460
461
470static void
471run (void *cls,
472 char *const *args,
473 const char *cfgfile,
474 const struct GNUNET_CONFIGURATION_Handle *cfg)
475{
476 if ((NULL != read_msg) && (NULL == set_ego))
477 {
478 fprintf (stderr,
479 "Option -R requires options -e to be specified as well.\n");
480 return;
481 }
482
483 if ((NULL != write_msg) && (NULL == pubkey_msg))
484 {
485 fprintf (stderr, "Option -W requires option -k to be specified as well.\n");
486 return;
487 }
489 (monitor | list) ||
490 (NULL != set_ego)
491 ? &print_ego
492 : NULL,
493 NULL);
494 if (NULL != write_msg)
495 {
498 write_msg = NULL;
499 }
500 // read message is handled in ego callback (print_ego)
501 if (NULL != delete_ego)
502 delete_op =
506 &delete_op);
507 if (NULL != create_ego)
508 {
509 if (NULL != privkey_ego)
510 {
512 strlen (privkey_ego),
513 &pk,
514 sizeof(struct
516 create_op =
519 &pk,
520 0, // Ignored
522 &create_op);
523 }
524 else
525 create_op =
528 NULL,
529 (type_eddsa) ?
533 &create_op);
534 }
536 NULL);
537 test_finished ();
538}
539
540
548int
549main (int argc, char *const *argv)
550{
553 "create",
554 "NAME",
555 gettext_noop ("create ego NAME"),
556 &create_ego),
558 "delete",
559 "NAME",
560 gettext_noop ("delete ego NAME "),
561 &delete_ego),
563 "privkey",
564 "PRIVATE_KEY",
566 "set the private key for the identity to PRIVATE_KEY (use together with -C)"),
567 &privkey_ego),
569 "read",
570 "MESSAGE",
572 "Read and decrypt message encrypted for the given ego (use together with -e EGO)"),
573 &read_msg),
575 "write",
576 "MESSAGE",
578 "Encrypt and write message for recipient identity PULBIC_KEY, (use together with -k RECIPIENT_PUBLIC_KEY)"),
579 &write_msg),
581 "eddsa",
583 "generate an EdDSA identity. (use together with -C) EXPERIMENTAL"),
584 &type_eddsa),
586 "display",
587 gettext_noop ("display all egos"),
588 &list),
590 "quiet",
591 gettext_noop ("reduce output"),
592 &quiet),
594 'e',
595 "ego",
596 "NAME",
598 "restrict results to NAME (use together with -d) or read and decrypt a message for NAME (use together with -R)"),
599 &set_ego),
601 "key",
602 "PUBLIC_KEY",
604 "The public key of the recipient (with -W)"),
605 &pubkey_msg),
607 "monitor",
608 gettext_noop ("run in monitor mode egos"),
609 &monitor),
611 "private-keys",
612 gettext_noop ("display private keys as well"),
613 &private_keys),
616 };
617 int res;
618
619 global_ret = TIMEOUT_STATUS_CODE; /* timeout */
621 argc,
622 argv,
623 "gnunet-identity",
624 gettext_noop ("Maintain egos"),
625 options,
626 &run,
627 NULL);
628
629 if (GNUNET_OK != res)
630 return 3;
631 return global_ret;
632}
633
634
635/* end of gnunet-identity.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
static struct GNUNET_ARM_Operation * op
Current operation.
Definition gnunet-arm.c:143
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static struct GNUNET_FS_Handle * ctx
#define TIMEOUT_STATUS_CODE
Return value from main on timeout.
static unsigned int verbose
Was "verbose" specified?
static void write_encrypted_message(void)
Encrypt a message given with -W, encrypted using public key of an identity given with -k.
static struct GNUNET_IDENTITY_Operation * set_op
Operation handle for set operation.
static char * privkey_ego
-P option
static struct GNUNET_IDENTITY_Operation * create_op
Handle for create operation.
static int global_ret
Value to return from main().
static int monitor
Was "monitor" specified?
static int list
Was "list" specified?
static int quiet
Was "quiet" specified?
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
static char * read_msg
-R option
static void print_ego(void *cls, struct GNUNET_IDENTITY_Ego *ego, void **ctx, const char *identifier)
If listing is enabled, prints information about the egos.
static void read_encrypted_message(struct GNUNET_IDENTITY_Ego *ego)
Decrypt a message given with -R, encrypted using public key of ego and ephemeral key given with -k.
struct GNUNET_CRYPTO_BlindablePrivateKey pk
Private key from command line option, or NULL.
static int private_keys
Was "private" specified?
static char * delete_ego
-D option
static void test_finished(void)
Test if we are finished yet.
static struct GNUNET_IDENTITY_Operation * delete_op
Handle for delete operation.
static char * create_ego
-C option
static void create_finished(void *cls, const struct GNUNET_CRYPTO_BlindablePrivateKey *pk_created, enum GNUNET_ErrorCode ec)
Creation operation finished.
static char * pubkey_msg
-k option
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
static int type_eddsa
Was "eddsa" specified?
static void delete_finished(void *cls, enum GNUNET_ErrorCode ec)
Deletion operation finished.
static char * set_ego
-s option.
static char * write_msg
-W option
struct GNUNET_SCHEDULER_Task * shutdown_task
static char * res
Currently read line or NULL on EOF.
static struct GNUNET_CRYPTO_EddsaPublicKey pub
const char * GNUNET_ErrorCode_get_hint(enum GNUNET_ErrorCode ec)
Returns a hint for a given error code.
GNUNET_ErrorCode
Taler error codes.
@ GNUNET_EC_NONE
No error (success).
Identity service; implements identity management for GNUnet.
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_verbose(unsigned int *level)
Define the '-V' verbosity option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_create(struct GNUNET_IDENTITY_Handle *id, const char *name, const struct GNUNET_CRYPTO_BlindablePrivateKey *privkey, enum GNUNET_CRYPTO_KeyType ktype, GNUNET_IDENTITY_CreateContinuation cont, void *cont_cls)
Create a new ego with the given name.
void GNUNET_IDENTITY_ego_get_public_key(struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_CRYPTO_BlindablePublicKey *pk)
Get the identifier (public key) of an ego.
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_delete(struct GNUNET_IDENTITY_Handle *id, const char *name, GNUNET_IDENTITY_Continuation cb, void *cb_cls)
Delete an existing ego.
struct GNUNET_IDENTITY_Handle * GNUNET_IDENTITY_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_IDENTITY_Callback cb, void *cb_cls)
Connect to the identity service.
void GNUNET_IDENTITY_cancel(struct GNUNET_IDENTITY_Operation *op)
Cancel an identity operation.
void GNUNET_IDENTITY_disconnect(struct GNUNET_IDENTITY_Handle *h)
Disconnect from identity service.
const struct GNUNET_CRYPTO_BlindablePrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blindable_key_get_public(const struct GNUNET_CRYPTO_BlindablePrivateKey *privkey, struct GNUNET_CRYPTO_BlindablePublicKey *key)
Retrieves the public key representation of a private key.
char * GNUNET_CRYPTO_blindable_public_key_to_string(const struct GNUNET_CRYPTO_BlindablePublicKey *key)
Creates a (Base32) string representation of the public key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_blindable_public_key_from_string(const char *str, struct GNUNET_CRYPTO_BlindablePublicKey *key)
Parses a (Base32) string representation of the public key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_seal_oneshot(const struct GNUNET_CRYPTO_HpkePublicKey *pkR, const uint8_t *info, size_t info_len, const uint8_t *aad, size_t aad_len, const uint8_t *pt, size_t pt_len, uint8_t *ct, unsigned long long *ct_len)
RFC9180 HPKE encryption.
char * GNUNET_CRYPTO_blindable_private_key_to_string(const struct GNUNET_CRYPTO_BlindablePrivateKey *key)
Creates a (Base32) string representation of the private key.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_sk_to_x25519(const struct GNUNET_CRYPTO_BlindablePrivateKey *sk, struct GNUNET_CRYPTO_HpkePrivateKey *sk_enc)
Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_open_oneshot(const struct GNUNET_CRYPTO_HpkePrivateKey *skR, const uint8_t *info, size_t info_len, const uint8_t *aad, size_t aad_len, const uint8_t *ct, size_t ct_len, uint8_t *pt, unsigned long long *pt_len)
RFC9180 HPKE encryption.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_hpke_pk_to_x25519(const struct GNUNET_CRYPTO_BlindablePublicKey *pk, struct GNUNET_CRYPTO_HpkePublicKey *pk_enc)
Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
#define GNUNET_CRYPTO_HPKE_SEAL_ONESHOT_OVERHEAD_BYTES
@ GNUNET_PUBLIC_KEY_TYPE_EDDSA
EDDSA identity.
@ GNUNET_PUBLIC_KEY_TYPE_ECDSA
The identity type.
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition program.c:407
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:567
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition scheduler.c:1339
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc(const char *enc, size_t enclen, void **out, size_t *out_size)
Convert CrockfordBase32 encoding back to data.
Definition strings.c:898
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition strings.c:807
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:832
#define _(String)
GNU gettext support macro.
Definition platform.h:179
A private key for an identity as per LSD0001.
An identity key as per LSD0001.
uint32_t type
Type of public key.
A public key used for decryption.
A public key used for encryption.
Definition of a command line option.
Handle for an ego.
Definition identity.h:37
Handle for the service.
Handle for an operation with the identity service.
void * cls
Closure for cont or cb.