GNUnet  0.17.6
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 
41 static struct GNUNET_IDENTITY_Handle *sh;
42 
46 static int list;
47 
51 static int monitor;
52 
56 static int private_keys;
57 
61 static unsigned int verbose;
62 
66 static int quiet;
67 
71 static int type_eddsa;
72 
76 static char *write_msg;
77 
81 static char *read_msg;
82 
86 static char *create_ego;
87 
91 static char *delete_ego;
92 
96 static char *privkey_ego;
97 
101 static char *pubkey_msg;
102 
106 static char *set_ego;
107 
111 static char *set_subsystem;
112 
117 
122 
127 
132 
136 static int global_ret;
137 
138 
144 static void
145 shutdown_task (void *cls)
146 {
147  if (NULL != set_op)
148  {
150  set_op = NULL;
151  }
152  if (NULL != create_op)
153  {
155  create_op = NULL;
156  }
157  if (NULL != delete_op)
158  {
160  delete_op = NULL;
161  }
162  if (NULL != set_ego)
163  {
165  set_ego = NULL;
166  }
168  sh = NULL;
169 }
170 
171 
175 static void
177 {
178  if ( (NULL == create_op) &&
179  (NULL == delete_op) &&
180  (NULL == set_op) &&
181  (NULL == set_subsystem) &&
182  (NULL == write_msg) &&
183  (NULL == read_msg) &&
184  (! list) &&
185  (! monitor))
186  {
188  global_ret = 0;
190  }
191 }
192 
193 
200 static void
201 delete_finished (void *cls,
202  const char *emsg)
203 {
204  struct GNUNET_IDENTITY_Operation **op = cls;
205 
206  *op = NULL;
207  if (NULL != emsg)
208  fprintf (stderr, "%s\n", gettext (emsg));
209  test_finished ();
210 }
211 
212 
220 static void
222  const struct GNUNET_IDENTITY_PrivateKey *pk,
223  const char *emsg)
224 {
225  struct GNUNET_IDENTITY_Operation **op = cls;
226 
227  *op = NULL;
228  if (NULL == pk)
229  {
230  fprintf (stderr,
231  _ ("Failed to create ego: %s\n"),
232  emsg);
233  global_ret = 1;
234  }
235  else if (verbose)
236  {
238  char *pubs;
239 
242  if (private_keys)
243  {
244  char *privs;
245 
247  fprintf (stdout, "%s - %s\n", pubs, privs);
248  GNUNET_free (privs);
249  }
250  else
251  {
252  fprintf (stdout, "%s\n", pubs);
253  }
254  GNUNET_free (pubs);
255  }
256  test_finished ();
257 }
258 
259 
266 static void
267 set_done (void *cls, const char *emsg)
268 {
269  set_op = NULL;
270  if (NULL != emsg)
271  {
272  fprintf (stderr, _ ("Failed to set default ego: %s\n"), emsg);
273  global_ret = 1;
274  }
275  test_finished ();
276 }
277 
278 
283 static void
285 {
286  struct GNUNET_IDENTITY_PublicKey recipient;
289  {
290  struct GNUNET_CRYPTO_EcdhePublicKey message_key;
291  size_t msg_len = strlen (write_msg);
293  msg_len,
294  &recipient,
295  &message_key,
296  write_msg);
297  if (-1 != res)
298  {
299  char *keystr;
300  char *serialized_msg;
301  keystr = GNUNET_STRINGS_data_to_string_alloc (&message_key,
302  sizeof(struct
305  msg_len);
306  fprintf (stdout,
307  "%s.%s\n",
308  keystr, serialized_msg);
309  GNUNET_free (keystr);
310  GNUNET_free (serialized_msg);
311  }
312  else
313  {
314  fprintf (stderr, "Error during encryption.\n");
315  global_ret = 1;
316  }
317  }
318  else
319  {
320  fprintf (stderr, "Invalid recipient public key.\n");
321  global_ret = 1;
322  }
323 }
324 
325 
332 static void
334 {
335  // message contains ECDHE key and ciphertext divided by ".", so split up first
336  char delim[2] = ".";
337  char *key_msg = strtok (read_msg, delim);
338  char *cipher;
339  if (NULL == key_msg)
340  {
341  fprintf (stderr, "Invalid message format.\n");
342  global_ret = 1;
343  return;
344  }
345  cipher = strtok (NULL, delim);
346  if (NULL == cipher)
347  {
348  fprintf (stderr, "Invalid message format, text missing.\n");
349  global_ret = 1;
350  return;
351  }
352 
353  if (NULL != strtok (NULL, delim))
354  {
355  fprintf (stderr,
356  "Invalid message format, expecting only key and cipher components.\n");
357  global_ret = 1;
358  return;
359  }
360 
361  struct GNUNET_CRYPTO_EcdhePublicKey message_key;
362  if (GNUNET_OK == GNUNET_STRINGS_string_to_data (key_msg, strlen (
363  key_msg),
364  &message_key,
365  sizeof(message_key)))
366  {
367  char *deserialized_msg;
368  size_t msg_len;
369  if (GNUNET_OK == GNUNET_STRINGS_string_to_data_alloc (cipher, strlen (
370  cipher),
371  (void **) &
372  deserialized_msg,
373  &msg_len))
374  {
375  ssize_t res = GNUNET_IDENTITY_decrypt (deserialized_msg,
376  msg_len,
378  ego),
379  &message_key,
380  deserialized_msg);
381  if (-1 != res)
382  {
383  fprintf (stdout,
384  "%s\n",
385  deserialized_msg);
386  }
387  else
388  {
389  fprintf (stderr, "Failed to decrypt message.\n");
390  global_ret = 1;
391  }
392  GNUNET_free (deserialized_msg);
393  }
394  else
395  {
396  fprintf (stderr, "Invalid message format.\n");
397  global_ret = 1;
398  }
399  }
400  else
401  {
402  fprintf (stderr, "Invalid message ephemeral key.\n");
403  global_ret = 1;
404  }
405 }
406 
407 
440 static void
441 print_ego (void *cls,
442  struct GNUNET_IDENTITY_Ego *ego,
443  void **ctx,
444  const char *identifier)
445 {
447  char *s;
448  char *privs;
449 
450  if ( (NULL != set_ego) &&
451  (NULL != set_subsystem) &&
452  (NULL != ego) &&
453  (NULL != identifier) &&
454  (0 == strcmp (identifier, set_ego)))
455  {
458  ego,
459  &set_done,
460  NULL);
462  set_subsystem = NULL;
464  set_ego = NULL;
465  }
466  if ( (NULL == ego) &&
467  (NULL != set_ego) &&
468  (NULL != set_subsystem) )
469  {
470  fprintf (stderr,
471  "Could not set ego to `%s' for subsystem `%s', ego not known\n",
472  set_ego,
473  set_subsystem);
475  set_subsystem = NULL;
477  set_ego = NULL;
478  }
479  if ( (NULL == ego) &&
480  (NULL != set_ego) &&
481  (NULL != read_msg) )
482  {
483  fprintf (stderr,
484  "Ego `%s' is not known, cannot decrypt message.\n",
485  set_ego);
487  read_msg = NULL;
489  set_ego = NULL;
490  }
491  if ((NULL == ego) && (! monitor))
492  {
493  list = 0;
494  test_finished ();
495  return;
496  }
497  if (! (list | monitor) && (NULL == read_msg))
498  return;
499  if ( (NULL == ego) ||
500  (NULL == identifier) )
501  return;
502  if ( (NULL != set_ego) &&
503  (0 != strcmp (identifier,
504  set_ego)) )
505  return;
510  if ((NULL != read_msg) && (NULL != set_ego))
511  {
512  // due to the check above, set_ego and the identifier are equal
515  read_msg = NULL;
516  }
517  else if ((monitor) || (NULL != identifier))
518  {
519  if (quiet)
520  {
521  if (private_keys)
522  fprintf (stdout, "%s - %s\n", s, privs);
523  else
524  fprintf (stdout, "%s\n", s);
525  }
526  else
527  {
528  if (private_keys)
529  fprintf (stdout, "%s - %s - %s - %s\n",
530  identifier, s, privs,
531  (ntohl (pk.type) == GNUNET_IDENTITY_TYPE_ECDSA) ?
532  "ECDSA" : "EdDSA");
533  else
534  fprintf (stdout, "%s - %s - %s\n",
535  identifier, s,
536  (ntohl (pk.type) == GNUNET_IDENTITY_TYPE_ECDSA) ?
537  "ECDSA" : "EdDSA");
538 
539  }
540  }
541  GNUNET_free (privs);
542  GNUNET_free (s);
543 }
544 
545 
554 static void
555 run (void *cls,
556  char *const *args,
557  const char *cfgfile,
558  const struct GNUNET_CONFIGURATION_Handle *cfg)
559 {
560  if ((NULL != set_subsystem) && (NULL == set_ego))
561  {
562  fprintf (stderr, "Option -s requires option -e to be specified as well.\n");
563  return;
564  }
565 
566  if ((NULL != read_msg) && (NULL == set_ego))
567  {
568  fprintf (stderr,
569  "Option -R requires options -e to be specified as well.\n");
570  return;
571  }
572 
573  if ((NULL != write_msg) && (NULL == pubkey_msg))
574  {
575  fprintf (stderr, "Option -W requires option -k to be specified as well.\n");
576  return;
577  }
579  (monitor | list) ||
580  (NULL != set_ego) ||
581  (NULL != set_subsystem)
582  ? &print_ego
583  : NULL,
584  NULL);
585  if (NULL != write_msg)
586  {
589  write_msg = NULL;
590  }
591  // read message is handled in ego callback (print_ego)
592  if (NULL != delete_ego)
593  delete_op =
595  delete_ego,
597  &delete_op);
598  if (NULL != create_ego)
599  {
600  if (NULL != privkey_ego)
601  {
603  strlen (privkey_ego),
604  &pk,
605  sizeof(struct
607  create_op =
609  create_ego,
610  &pk,
611  0, // Ignored
613  &create_op);
614  }
615  else
616  create_op =
618  create_ego,
619  NULL,
620  (type_eddsa) ?
624  &create_op);
625  }
627  NULL);
628  test_finished ();
629 }
630 
631 
639 int
640 main (int argc, char *const *argv)
641 {
644  "create",
645  "NAME",
646  gettext_noop ("create ego NAME"),
647  &create_ego),
649  "delete",
650  "NAME",
651  gettext_noop ("delete ego NAME "),
652  &delete_ego),
654  "privkey",
655  "PRIVATE_KEY",
656  gettext_noop (
657  "set the private key for the identity to PRIVATE_KEY (use together with -C)"),
658  &privkey_ego),
660  "read",
661  "MESSAGE",
662  gettext_noop (
663  "Read and decrypt message encrypted for the given ego (use together with -e EGO)"),
664  &read_msg),
666  "write",
667  "MESSAGE",
668  gettext_noop (
669  "Encrypt and write message for recipient identity PULBIC_KEY, (use together with -k RECIPIENT_PUBLIC_KEY)"),
670  &write_msg),
672  "eddsa",
673  gettext_noop (
674  "generate an EdDSA identity. (use together with -C) EXPERIMENTAL"),
675  &type_eddsa),
677  "display",
678  gettext_noop ("display all egos"),
679  &list),
681  "quiet",
682  gettext_noop ("reduce output"),
683  &quiet),
685  'e',
686  "ego",
687  "NAME",
688  gettext_noop (
689  "set default identity to NAME for a subsystem SUBSYSTEM (use together with -s), restrict results to NAME (use together with -d) or read and decrypt a message for NAME (use together with -R)"),
690  &set_ego),
692  "key",
693  "PUBLIC_KEY",
694  gettext_noop (
695  "The public key of the recipient (with -W)"),
696  &pubkey_msg),
698  "monitor",
699  gettext_noop ("run in monitor mode egos"),
700  &monitor),
702  "private-keys",
703  gettext_noop ("display private keys as well"),
704  &private_keys),
706  's',
707  "set",
708  "SUBSYSTEM",
709  gettext_noop (
710  "set default identity to EGO for a subsystem SUBSYSTEM (use together with -e)"),
711  &set_subsystem),
714  };
715  int res;
716 
717  if (GNUNET_OK !=
718  GNUNET_STRINGS_get_utf8_args (argc, argv,
719  &argc, &argv))
720  return 4;
721  global_ret = TIMEOUT_STATUS_CODE; /* timeout */
722  res = GNUNET_PROGRAM_run (argc,
723  argv,
724  "gnunet-identity",
725  gettext_noop ("Maintain egos"),
726  options,
727  &run,
728  NULL);
729  GNUNET_free_nz ((void *) argv);
730 
731  if (GNUNET_OK != res)
732  return 3;
733  return global_ret;
734 }
735 
736 
737 /* end of gnunet-identity.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:69
#define gettext(Msgid)
Definition: gettext.h:45
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
static int res
#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.
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
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 shutdown_task(void *cls)
Task run on shutdown.
static void create_finished(void *cls, const struct GNUNET_IDENTITY_PrivateKey *pk, const char *emsg)
Creation operation finished.
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.
static int private_keys
Was "private" specified?
static char * set_subsystem
-S option.
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 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.
int main(int argc, char *const *argv)
The main function.
static void set_done(void *cls, const char *emsg)
Function called by GNUNET_IDENTITY_set up on completion.
static int type_eddsa
Was "eddsa" specified?
static char * set_ego
-s option.
static char * write_msg
-W option
static void delete_finished(void *cls, const char *emsg)
Deletion operation finished.
static struct GNUNET_CRYPTO_EddsaPublicKey pub
Definition: gnunet-scrypt.c:46
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
Identity service; implements identity management for GNUnet.
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.
const struct GNUNET_IDENTITY_PrivateKey * GNUNET_IDENTITY_ego_get_private_key(const struct GNUNET_IDENTITY_Ego *ego)
Obtain the ECC key associated with a ego.
Definition: identity_api.c:639
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_public_key_from_string(const char *str, struct GNUNET_IDENTITY_PublicKey *key)
Parses a (Base32) string representation of the public key.
ssize_t GNUNET_IDENTITY_decrypt(const void *block, size_t size, const struct GNUNET_IDENTITY_PrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *ecc, void *result)
Decrypt a given block with GNUNET_IDENTITY_PrivateKey and a given GNUNET_CRYPTO_EcdhePublicKey using ...
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_key_get_public(const struct GNUNET_IDENTITY_PrivateKey *privkey, struct GNUNET_IDENTITY_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: identity_api.c:175
ssize_t GNUNET_IDENTITY_encrypt(const void *block, size_t size, const struct GNUNET_IDENTITY_PublicKey *pub, struct GNUNET_CRYPTO_EcdhePublicKey *ecc, void *result)
Encrypt a block with GNUNET_IDENTITY_PublicKey and derives a GNUNET_CRYPTO_EcdhePublicKey which is re...
char * GNUNET_IDENTITY_private_key_to_string(const struct GNUNET_IDENTITY_PrivateKey *key)
Creates a (Base32) string representation of the private key.
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_create(struct GNUNET_IDENTITY_Handle *h, const char *name, const struct GNUNET_IDENTITY_PrivateKey *privkey, enum GNUNET_IDENTITY_KeyType ktype, GNUNET_IDENTITY_CreateContinuation cont, void *cont_cls)
Create a new ego with the given name.
Definition: identity_api.c:757
void GNUNET_IDENTITY_ego_get_public_key(struct GNUNET_IDENTITY_Ego *ego, struct GNUNET_IDENTITY_PublicKey *pk)
Get the identifier (public key) of an ego.
Definition: identity_api.c:652
char * GNUNET_IDENTITY_public_key_to_string(const struct GNUNET_IDENTITY_PublicKey *key)
Creates a (Base32) string representation of the public key.
void GNUNET_IDENTITY_cancel(struct GNUNET_IDENTITY_Operation *op)
Cancel an identity operation.
Definition: identity_api.c:904
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.
Definition: identity_api.c:610
void GNUNET_IDENTITY_disconnect(struct GNUNET_IDENTITY_Handle *h)
Disconnect from identity service.
Definition: identity_api.c:921
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_set(struct GNUNET_IDENTITY_Handle *h, const char *service_name, struct GNUNET_IDENTITY_Ego *ego, GNUNET_IDENTITY_Continuation cont, void *cont_cls)
Set the preferred/default identity for a service.
Definition: identity_api.c:720
struct GNUNET_IDENTITY_Operation * GNUNET_IDENTITY_delete(struct GNUNET_IDENTITY_Handle *h, const char *name, GNUNET_IDENTITY_Continuation cb, void *cb_cls)
Delete an existing identity.
Definition: identity_api.c:863
@ GNUNET_IDENTITY_TYPE_ECDSA
The identity type.
@ GNUNET_IDENTITY_TYPE_EDDSA
EDDSA identity.
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(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:399
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:533
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:1316
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:855
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:764
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:789
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1223
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
Definition of a command line option.
Handle for an ego.
Definition: identity.h:37
Handle for the service.
Definition: identity_api.c:96
Handle for an operation with the identity service.
Definition: identity_api.c:40
void * cls
Closure for cont or cb.
Definition: identity_api.c:88
A private key for an identity as per LSD0001.
uint32_t type
Type of public key.
An identity key as per LSD0001.