GNUnet  0.18.1
did_core.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2010-2015 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 
27 // TODO: DID documents do not have an expiration date. Still we add one
28 // TODO: Store DID document with empty label and own type (maybe DID-Document or JSON??)
29 
30 #include "did_core.h"
31 
33 {
35  void *cls;
36 };
37 
39 {
41  void *cls;
42 };
43 
44 // ------------------------------------------------ //
45 // -------------------- Resolve ------------------- //
46 // ------------------------------------------------ //
47 
57 static void
59  void *cls,
60  uint32_t rd_count,
61  const struct GNUNET_GNSRECORD_Data *rd)
62 {
63  char *did_document;
64  DID_resolve_callback *cb = ((struct DID_resolve_return *) cls)->cb;
65  void *cls_did_resolve_cb = ((struct DID_resolve_return *) cls)->cls;
66  free (cls);
67 
68  for (int i = 0; i < rd_count; i++) {
69  if (rd[i].record_type != GNUNET_GNSRECORD_TYPE_DID_DOCUMENT)
70  continue;
71  did_document = (char *) rd[i].data;
72  cb (GNUNET_OK, did_document, cls_did_resolve_cb);
73  return;
74  }
75  cb (GNUNET_NO, "DID Document is not a DID_DOCUMENT record\n",
76  cls_did_resolve_cb);
77 }
78 
87 DID_resolve (const char *did,
90  void *cls)
91 {
93 
94  // did, gns_handle and cont must me set
95  if ((did == NULL) || (gns_handle == NULL) || (cont == NULL))
96  return GNUNET_NO;
97 
98  if (GNUNET_OK != DID_did_to_pkey (did, &pkey))
99  return GNUNET_NO;
100 
101  // Create closure for lookup callback
102  struct DID_resolve_return *cls_gns_lookup_cb
103  = malloc (sizeof(struct DID_resolve_return));
104  cls_gns_lookup_cb->cb = cont;
105  cls_gns_lookup_cb->cls = cls;
106 
109  &pkey,
113  cls_gns_lookup_cb);
114 
115  return GNUNET_OK;
116 }
117 
118 // ------------------------------------------------ //
119 // -------------------- Create -------------------- //
120 // ------------------------------------------------ //
121 
122 static void
124  enum GNUNET_ErrorCode ec)
125 {
126  DID_action_callback *cb = ((struct DID_action_return *) cls)->cb;
127  void *cls_did_create_cb = ((struct DID_action_return *) cls)->cls;
128  free (cls);
129 
130  if (GNUNET_EC_NONE == ec)
131  {
132  cb (GNUNET_OK, (void *) cls_did_create_cb);
133  }
134  else
135  {
136  // TODO: Log emsg. Not writing it to STDOUT
137  printf ("%s\n", GNUNET_ErrorCode_get_hint (ec));
138  cb (GNUNET_NO, (void *) cls_did_create_cb);
139  }
140 }
141 
143 {
144  const char *did_document;
148 };
149 
150 static void
152  const struct
154  const char *label,
155  unsigned int rd_count,
156  const struct GNUNET_GNSRECORD_Data *rd)
157 {
158  struct GNUNET_GNSRECORD_Data record_data;
160 
161  const char *did_document
162  = ((struct DID_create_namestore_lookup_closure *) cls)->did_document;
163 
164  const struct GNUNET_TIME_Relative expire_time
165  = ((struct DID_create_namestore_lookup_closure *) cls)->expire_time;
166 
168  = ((struct DID_create_namestore_lookup_closure *) cls)->namestore_handle;
169 
170  struct DID_action_return *cls_record_store_cb
171  = ((struct DID_create_namestore_lookup_closure *) cls)->ret;
172 
173  free (cls);
174 
175  if (rd_count > 0)
176  {
177  printf ("Ego already has a DID Document. Abort.\n");
178  cls_record_store_cb->cb (GNUNET_NO, cls_record_store_cb->cls);
179  }
180  else {
181  // Get public key
183 
184  // If no DID Document is given a default one is created
185  if (did_document != NULL)
186  printf (
187  "DID Docuement is read from \"DID-document\" argument (EXPERIMENTAL)\n");
188  else
190 
191  // Create record
192  record_data.data = did_document;
194  record_data.data_size = strlen (did_document) + 1;
195  record_data.record_type = GNUNET_GNSRECORD_typename_to_number ("TXT"),
197 
198  // Store record
200  zone,
202  1, // FIXME what if GNUNET_GNS_EMPTY_LABEL_AT has records
203  &record_data,
205  (void *) cls_record_store_cb);
206  }
207 }
208 
220 DID_create (const struct GNUNET_IDENTITY_Ego *ego,
221  const char *did_document,
222  const struct GNUNET_TIME_Relative *expire_time,
224  DID_action_callback *cont,
225  void *cls)
226 {
228 
229  // Ego, namestore_handle and cont must be set
230  if ((ego == NULL) || (namestore_handle == NULL) || (cont == NULL))
231  return GNUNET_NO;
232 
233  // Check if ego has EdDSA key
235  &pkey);
236  if (ntohl (pkey.type) != GNUNET_GNSRECORD_TYPE_EDKEY)
237  {
238  printf ("The EGO has to have an EdDSA key pair\n");
239  return GNUNET_NO;
240  }
241 
242  struct DID_action_return *ret
243  = malloc (sizeof(struct DID_action_return));
244  ret->cb = cont;
245  ret->cls = cls;
246 
247  struct DID_create_namestore_lookup_closure *cls_name_store_lookup_cb
248  = malloc (sizeof(struct DID_create_namestore_lookup_closure));
249  cls_name_store_lookup_cb->did_document = did_document;
250  cls_name_store_lookup_cb->expire_time = (*expire_time);
251  cls_name_store_lookup_cb->namestore_handle = namestore_handle;
252  cls_name_store_lookup_cb->ret = ret;
253 
254  // Check if ego already has a DID Document
258  NULL,
259  NULL,
261  (void *) cls_name_store_lookup_cb);
262 
263  return GNUNET_OK;
264 }
@ GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION
This expiration time of the record is a relative time (not an absolute time).
#define GNUNET_GNSRECORD_TYPE_DID_DOCUMENT
Record type to store DID Documents.
#define GNUNET_GNSRECORD_TYPE_EDKEY
Record type for EDKEY zone delegations.
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).
static void DID_create_namestore_lookup_cb(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Definition: did_core.c:151
enum GNUNET_GenericReturnValue DID_resolve(const char *did, struct GNUNET_GNS_Handle *gns_handle, DID_resolve_callback *cont, void *cls)
Resolve a DID.
Definition: did_core.c:87
static void DID_create_did_store_cb(void *cls, enum GNUNET_ErrorCode ec)
Definition: did_core.c:123
enum GNUNET_GenericReturnValue DID_create(const struct GNUNET_IDENTITY_Ego *ego, const char *did_document, const struct GNUNET_TIME_Relative *expire_time, struct GNUNET_NAMESTORE_Handle *namestore_handle, DID_action_callback *cont, void *cls)
Creates a DID and saves DID Document in Namestore.
Definition: did_core.c:220
static void DID_resolve_gns_lookup_cb(void *cls, uint32_t rd_count, const struct GNUNET_GNSRECORD_Data *rd)
GNS lookup callback.
Definition: did_core.c:58
Core functionality for GNUNET Decentralized Identifier.
void DID_resolve_callback(enum GNUNET_GenericReturnValue status, char *did_document, void *cls)
Signature of a callback function that is called after a did has been resolved.
Definition: did_core.h:50
void DID_action_callback(enum GNUNET_GenericReturnValue status, void *cls)
Signature of a callback function that is called after a did has been removed status = 0 if action was...
Definition: did_core.h:61
#define DID_DOCUMENT_LABEL
Definition: did_core.h:37
enum GNUNET_GenericReturnValue DID_did_to_pkey(const char *did, struct GNUNET_IDENTITY_PublicKey *pkey)
Return the public key of a DID.
Definition: did_helper.c:79
char * DID_pkey_to_did_document(struct GNUNET_IDENTITY_PublicKey *pkey)
Create a did generate did object.
Definition: did_helper.c:138
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_NAMESTORE_Handle * namestore_handle
Definition: gnunet-did.c:111
static struct GNUNET_GNS_Handle * gns_handle
Handle to the GNS service.
uint32_t data
The data value.
uint32_t did
This has a different ID for each parameter, see PRISM_DID_* constants.
static char * pkey
Public key of the zone to look in, in ASCII.
static char * zone
Name of the zone being managed.
static unsigned int rd_count
Number of records for currently parsed set.
static struct GNUNET_GNSRECORD_Data rd[50]
The record data under a single label.
struct GNUNET_GNS_LookupRequest * GNUNET_GNS_lookup(struct GNUNET_GNS_Handle *handle, const char *name, const struct GNUNET_IDENTITY_PublicKey *zone, uint32_t type, enum GNUNET_GNS_LocalOptions options, GNUNET_GNS_LookupResultProcessor proc, void *proc_cls)
Perform an asynchronous lookup operation on the GNS.
Definition: gns_api.c:422
@ GNUNET_GNS_LO_DEFAULT
Defaults, look in cache, then in DHT.
uint32_t GNUNET_GNSRECORD_typename_to_number(const char *dns_typename)
Convert a type name (e.g.
Definition: gnsrecord.c:201
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:560
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:179
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:573
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:97
@ GNUNET_OK
@ GNUNET_NO
Definition: gnunet_common.h:99
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_lookup(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_IDENTITY_PrivateKey *pkey, const char *label, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor rm, void *rm_cls)
Lookup an item in the namestore.
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_store(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_IDENTITY_PrivateKey *pkey, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, GNUNET_NAMESTORE_ContinuationWithStatus cont, void *cont_cls)
Store an item in the namestore.
DID_action_callback * cb
Definition: did_core.c:40
struct GNUNET_NAMESTORE_Handle * namestore_handle
Definition: did_core.c:146
struct GNUNET_TIME_Relative expire_time
Definition: did_core.c:145
struct DID_action_return * ret
Definition: did_core.c:147
DID_resolve_callback * cb
Definition: did_core.c:34
uint32_t record_type
Type of the GNS/DNS record.
const void * data
Binary value stored in the DNS record.
size_t data_size
Number of bytes in data.
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
uint64_t expiration_time
Expiration time for the DNS record.
Connection to the GNS service.
Definition: gns_api.h:36
Handle for an ego.
Definition: identity.h:37
A private key for an identity as per LSD0001.
An identity key as per LSD0001.
Connection to the NAMESTORE service.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.