add option to create identity from private key spaeth/import_identity
authorjospaeth <spaethj@in.tum.de>
Mon, 25 May 2020 19:38:58 +0000 (21:38 +0200)
committerjospaeth <spaethj@in.tum.de>
Mon, 25 May 2020 19:38:58 +0000 (21:38 +0200)
src/conversation/test_conversation_api.c
src/conversation/test_conversation_api_reject.c
src/conversation/test_conversation_api_twocalls.c
src/identity/gnunet-identity.c
src/identity/identity_api.c
src/identity/plugin_rest_identity.c
src/identity/test_identity.c
src/identity/test_identity_defaults.c
src/include/gnunet_identity_service.h
src/revocation/test_revocation.c

index 2b717367a48c6f89d1c0a9b2b39f83ad560c5fba..dbb742d913332f4df3de084c0462dabf43c71e38 100644 (file)
@@ -402,7 +402,7 @@ namestore_put_cont (void *cls, int32_t success, const char *emsg)
   GNUNET_assert (GNUNET_YES == success);
   GNUNET_assert (NULL == emsg);
   GNUNET_assert (NULL == op);
-  op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
+  op = GNUNET_IDENTITY_create (id, "caller-ego", NULL, &caller_ego_create_cont, NULL);
 }
 
 
@@ -483,7 +483,7 @@ run (void *cls,
   cfg = c;
   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
   id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
-  op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
+  op = GNUNET_IDENTITY_create (id, "phone-ego", NULL, &phone_ego_create_cont, NULL);
   ns = GNUNET_NAMESTORE_connect (cfg);
 }
 
index 62e4109b0266ecd162204e6e5569efa68c538e57..855b21fd72f14665ef5d239a56fd7c6c3e11ca62 100644 (file)
@@ -255,7 +255,7 @@ namestore_put_cont (void *cls, int32_t success, const char *emsg)
   GNUNET_assert (GNUNET_YES == success);
   GNUNET_assert (NULL == emsg);
   GNUNET_assert (NULL == op);
-  op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
+  op = GNUNET_IDENTITY_create (id, "caller-ego", NULL, &caller_ego_create_cont, NULL);
 }
 
 
@@ -336,7 +336,7 @@ run (void *cls,
   cfg = c;
   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
   id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
-  op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
+  op = GNUNET_IDENTITY_create (id, "phone-ego", NULL, &phone_ego_create_cont, NULL);
   ns = GNUNET_NAMESTORE_connect (cfg);
 }
 
index fab49f7d76ccbc4fbf5297f75b5a0e64cfcbe395..6d434a3e1d6ab9ffbadaa4a652b420f72deb288c 100644 (file)
@@ -524,7 +524,7 @@ namestore_put_cont (void *cls, int32_t success, const char *emsg)
   GNUNET_assert (GNUNET_YES == success);
   GNUNET_assert (NULL == emsg);
   GNUNET_assert (NULL == op);
-  op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
+  op = GNUNET_IDENTITY_create (id, "caller-ego", NULL, &caller_ego_create_cont, NULL);
 }
 
 
@@ -613,7 +613,7 @@ run (void *cls,
   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test, NULL);
   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
   id = GNUNET_IDENTITY_connect (cfg, &identity_cb, NULL);
-  op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
+  op = GNUNET_IDENTITY_create (id, "phone-ego", NULL, &phone_ego_create_cont, NULL);
   ns = GNUNET_NAMESTORE_connect (cfg);
 }
 
index fd73048c48da59c573ff2ca43a97309f70d0199d..cf44afd1fdbcca2f06cef17003fd66a5a4747f5f 100644 (file)
@@ -75,6 +75,11 @@ static char *create_ego;
  */
 static char *delete_ego;
 
+/**
+ * -P option
+ */
+static char *privkey_ego;
+
 /**
  * -s option.
  */
@@ -100,6 +105,11 @@ static struct GNUNET_IDENTITY_Operation *create_op;
  */
 static struct GNUNET_IDENTITY_Operation *delete_op;
 
+/**
+ * Private key from command line option, or NULL.
+ */
+struct GNUNET_CRYPTO_EcdsaPrivateKey pk;
+
 /**
  * Value to return from #main().
  */
@@ -390,11 +400,28 @@ run (void *cls,
                               &delete_finished,
                               &delete_op);
   if (NULL != create_ego)
-    create_op =
-      GNUNET_IDENTITY_create (sh,
-                              create_ego,
-                              &create_finished,
-                              &create_op);
+  {
+    if (NULL != privkey_ego)
+    {
+      GNUNET_STRINGS_string_to_data (privkey_ego,
+                                     strlen (privkey_ego),
+                                     &pk,
+                                     sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
+      create_op =
+        GNUNET_IDENTITY_create (sh,
+                                create_ego,
+                                &pk,
+                                &create_finished,
+                                &create_op);
+    }
+    else
+      create_op =
+        GNUNET_IDENTITY_create (sh,
+                                create_ego,
+                                NULL,
+                                &create_finished,
+                                &create_op);
+  }
   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                  NULL);
   test_finished ();
@@ -422,6 +449,11 @@ main (int argc, char *const *argv)
                                  "NAME",
                                  gettext_noop ("delete ego NAME "),
                                  &delete_ego),
+    GNUNET_GETOPT_option_string ('P',
+                                 "privkey",
+                                 "PRIVATE_KEY",
+                                 gettext_noop ("set the private key for the identity to PRIVATE_KEY (use together with -C)"),
+                                 &privkey_ego),
     GNUNET_GETOPT_option_flag ('d',
                                "display",
                                gettext_noop ("display all egos"),
index c2fcc5075a20ed50988a7492c22672bcde34e22e..5d17ac5d5216991ed8384cf57c056645aee7f04d 100644 (file)
@@ -715,6 +715,7 @@ GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *h,
  *
  * @param h identity service to use
  * @param name desired name
+ * @param privkey desired private key or NULL to create one
  * @param cont function to call with the result (will only be called once)
  * @param cont_cls closure for @a cont
  * @return handle to abort the operation
@@ -722,6 +723,7 @@ GNUNET_IDENTITY_set (struct GNUNET_IDENTITY_Handle *h,
 struct GNUNET_IDENTITY_Operation *
 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *h,
                         const char *name,
+                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey,
                         GNUNET_IDENTITY_CreateContinuation cont,
                         void *cont_cls)
 {
@@ -746,7 +748,10 @@ GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *h,
   env = GNUNET_MQ_msg_extra (crm, slen, GNUNET_MESSAGE_TYPE_IDENTITY_CREATE);
   crm->name_len = htons (slen);
   crm->reserved = htons (0);
-  GNUNET_CRYPTO_ecdsa_key_create (&crm->private_key);
+  if (NULL == privkey)
+    GNUNET_CRYPTO_ecdsa_key_create (&crm->private_key);
+  else
+    crm->private_key = *privkey;
   op->pk = crm->private_key;
   GNUNET_memcpy (&crm[1], name, slen);
   GNUNET_MQ_send (h->mq, env);
index ef01cc5782f3c96674932c84391ca991b91a388e..009a01f16c0011e303cb2d3084940c271d76076d 100644 (file)
@@ -57,7 +57,7 @@
 #define GNUNET_REST_IDENTITY_PARAM_PUBKEY "pubkey"
 
 /**
- * Parameter public key
+ * Parameter private key
  */
 #define GNUNET_REST_IDENTITY_PARAM_PRIVKEY "privkey"
 
@@ -990,6 +990,9 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle,
   json_t *data_js;
   json_error_t err;
   char *egoname;
+  char *privkey;
+  struct GNUNET_CRYPTO_EcdsaPrivateKey pk;
+  struct GNUNET_CRYPTO_EcdsaPrivateKey *pk_ptr;
   int json_unpack_state;
   char term_data[handle->data_size + 1];
 
@@ -1016,8 +1019,11 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle,
     return;
   }
   json_unpack_state = 0;
+  privkey = NULL;
   json_unpack_state =
-    json_unpack (data_js, "{s:s!}", GNUNET_REST_IDENTITY_PARAM_NAME, &egoname);
+    json_unpack (data_js, "{s:s, s?:s!}",
+                 GNUNET_REST_IDENTITY_PARAM_NAME, &egoname,
+                 GNUNET_REST_IDENTITY_PARAM_PRIVKEY, &privkey);
   if (0 != json_unpack_state)
   {
     handle->emsg = GNUNET_strdup (GNUNET_REST_ERROR_DATA_INVALID);
@@ -1054,10 +1060,21 @@ ego_create (struct GNUNET_REST_RequestHandle *con_handle,
     }
   }
   handle->name = GNUNET_strdup (egoname);
+  if (NULL != privkey)
+  {
+    GNUNET_STRINGS_string_to_data (privkey,
+                                   strlen (privkey),
+                                   &pk,
+                                   sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
+    pk_ptr = &pk;
+  }
+  else
+    pk_ptr = NULL;
   json_decref (data_js);
   handle->response_code = MHD_HTTP_CREATED;
   handle->op = GNUNET_IDENTITY_create (handle->identity_handle,
                                        handle->name,
+                                       pk_ptr,
                                        &do_finished_create,
                                        handle);
 }
index aaa435d4f835e974906b7b378a11c1004e231e7b..37eeab2382ced070e5055dbafa9d5448fb45fda6 100644 (file)
@@ -279,7 +279,7 @@ run (void *cls,
   GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
   h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
   CHECK (NULL != h);
-  op = GNUNET_IDENTITY_create (h, "test-id", &create_cb, NULL);
+  op = GNUNET_IDENTITY_create (h, "test-id", NULL, &create_cb, NULL);
 }
 
 
index 1dd05f1b98571478be6b3cab181aab66df684b92..53eec1252cddeb7a48020af20b413f943b2735ca 100644 (file)
@@ -266,7 +266,7 @@ run_set (void *cls,
   GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
   h = GNUNET_IDENTITY_connect (cfg, &notification_cb, NULL);
   CHECK (NULL != h);
-  op = GNUNET_IDENTITY_create (h, "test-id", &create_cb, NULL);
+  op = GNUNET_IDENTITY_create (h, "test-id", NULL, &create_cb, NULL);
 }
 
 
index 81af671e2fecaec3018d72f2f05cb59f62cf9d7d..f4e6535982e5e6638955c7be00156346ffa28fac 100644 (file)
@@ -233,6 +233,7 @@ typedef void
  *
  * @param id identity service to use
  * @param name desired name
+ * @param privkey desired private key or NULL to create one
  * @param cont function to call with the result (will only be called once)
  * @param cont_cls closure for @a cont
  * @return handle to abort the operation
@@ -240,6 +241,7 @@ typedef void
 struct GNUNET_IDENTITY_Operation *
 GNUNET_IDENTITY_create (struct GNUNET_IDENTITY_Handle *id,
                         const char *name,
+                        const struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey,
                         GNUNET_IDENTITY_CreateContinuation cont,
                         void *cont_cls);
 
index f193d5f6c605b3a0ccd201ac185770c504ebcff7..1c2efa60fa1b63a970a371e7eb20368235e22846 100644 (file)
@@ -237,10 +237,12 @@ identity_completion_cb (void *cls,
   fprintf (stderr, "All peers connected @ IDENTITY ...\n");
   testpeers[0].create_id_op = GNUNET_IDENTITY_create (testpeers[0].idh,
                                                       "client",
+                                                      NULL,
                                                       &identity_create_cb,
                                                       &testpeers[0]);
   testpeers[1].create_id_op = GNUNET_IDENTITY_create (testpeers[1].idh,
                                                       "toberevoked",
+                                                      NULL,
                                                       &identity_create_cb,
                                                       &testpeers[1]);
 }