aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/did_core.c
blob: 645ba0cd48db541de73a055ecf8197b6091630c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
   This file is part of GNUnet
   Copyright (C) 2010-2015 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file reclaim/did_core.c
 * @brief Core functionality for DID
 * @author Tristan Schwieren
 */

// TODO: DID documents do not have an expiration date. Still we add one
// TODO: Store DID document with empty label and own type (maybe DID-Document or JSON??)

#include "did_core.h"

struct DID_resolve_return
{
  DID_resolve_callback *cb;
  void *cls;
};

struct DID_action_return
{
  DID_action_callback *cb;
  void *cls;
};

// ------------------------------------------------ //
// -------------------- Resolve ------------------- //
// ------------------------------------------------ //

/**
 * @brief GNS lookup callback. Calls the given callback function
 * and gives it the DID Document.
 * Fails if there is more than one DID record.
 *
 * @param cls closure
 * @param rd_count number of records in rd
 * @param rd the records in the reply
 */
static void
DID_resolve_gns_lookup_cb (
  void *cls,
  uint32_t rd_count,
  const struct GNUNET_GNSRECORD_Data *rd)
{
  char *did_document;
  DID_resolve_callback *cb = ((struct DID_resolve_return *) cls)->cb;
  void *cls2 = ((struct DID_resolve_return *) cls)->cls;
  free (cls);

  if (rd_count != 1)
    cb (GNUNET_NO, "An ego should only have one DID Document", cls2);

  if (rd[0].record_type == GNUNET_DNSPARSER_TYPE_TXT)
  {
    did_document = (char *) rd[0].data;
    cb (GNUNET_OK, did_document, cls2);
  }
  else
    cb (GNUNET_NO, "DID Document is not a TXT record\n", cls2);
}

/**
 * @brief Resolve a DID.
 * Calls the given callback function with the resolved DID Document and the given closure.
 * If the did can not be resolved did_document is NULL.
 *
 * @param did DID that is resolve
 */
enum GNUNET_GenericReturnValue
DID_resolve (const char *did,
             struct GNUNET_GNS_Handle *gns_handle,
             DID_resolve_callback *cont,
             void *cls)
{
  struct GNUNET_IDENTITY_PublicKey pkey;

  // did, gns_handle and cont must me set
  if ((did == NULL) || (gns_handle == NULL) || (cont == NULL))
    return GNUNET_NO;

  if (GNUNET_OK != DID_did_to_pkey (did, &pkey))
    return GNUNET_NO;

  // Create closure for lookup callback
  struct DID_resolve_return *cls2 = malloc (sizeof(struct DID_resolve_return));
  cls2->cb = cont;
  cls2->cls = cls;

  GNUNET_GNS_lookup (gns_handle, DID_DOCUMENT_LABEL, &pkey,
                     GNUNET_DNSPARSER_TYPE_TXT,
                     GNUNET_GNS_LO_DEFAULT, &DID_resolve_gns_lookup_cb, cls2);

  return GNUNET_OK;
}

// ------------------------------------------------ //
// -------------------- Create -------------------- //
// ------------------------------------------------ //

static void
DID_create_did_store_cb (void *cls,
                         int32_t success,
                         const char *emsg)
{
  DID_action_callback *cb = ((struct DID_action_return *) cls)->cb;
  void *cls2 = ((struct DID_action_return *) cls)->cls;
  free (cls);

  if (GNUNET_OK == success)
  {
    cb (GNUNET_OK, (void *) cls2);
  }
  else
  {
    // TODO: Log emsg. Not writing it to STDOUT
    printf ("%s\n", emsg);
    cb (GNUNET_NO, (void *) cls2);
  }
}

struct DID_create_namestore_lookup_closure
{
  const char *did_document;
  struct GNUNET_TIME_Relative expire_time;
  struct GNUNET_NAMESTORE_Handle *namestore_handle;
  DID_action_callback *cont;
  void *cls;
};

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)
{
  struct GNUNET_GNSRECORD_Data record_data;
  struct GNUNET_IDENTITY_PublicKey pkey;

  const char *did_document
    = ((struct DID_create_namestore_lookup_closure *) cls)->did_document;

  const struct GNUNET_TIME_Relative expire_time
    = ((struct DID_create_namestore_lookup_closure *) cls)->expire_time;

  struct GNUNET_NAMESTORE_Handle *namestore_handle
    = ((struct DID_create_namestore_lookup_closure *) cls)->namestore_handle;

  DID_action_callback *cont
    = ((struct DID_create_namestore_lookup_closure *) cls)->cont;

  void *cls1
    = ((struct DID_create_namestore_lookup_closure *) cls)->cls;

  if (rd_count > 0)
  {
    printf ("Ego already has a DID Document. Abort.\n");
    cont (GNUNET_NO, cls1);
    return;
  }
  else {
    // Get public key
    GNUNET_IDENTITY_key_get_public (zone, &pkey);

    // No DID Document is given a default one is created
    if (did_document != NULL)
      printf (
        "DID Docuement is read from \"DID-document\" argument (EXPERIMENTAL)\n");
    else
      did_document = DID_pkey_to_did_document (&pkey);

    // Create record
    record_data.data = did_document;
    record_data.expiration_time = expire_time.rel_value_us;
    record_data.data_size = strlen (did_document) + 1;
    record_data.record_type = GNUNET_GNSRECORD_typename_to_number ("TXT"),
    record_data.flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;

    // Create closure for record store callback
    struct DID_action_return *cls2 = malloc (sizeof(struct DID_action_return));
    cls2->cb = cont;
    cls2->cls = cls1;

    // Store record
    GNUNET_NAMESTORE_records_store (namestore_handle,
                                    zone,
                                    DID_DOCUMENT_LABEL,
                                    1, // FIXME what if GNUNET_GNS_EMPTY_LABEL_AT has records
                                    &record_data,
                                    &DID_create_did_store_cb,
                                    (void *) cls2);

    free(cls);
  }

}

/**
 * @brief Creates a DID and saves DID Document in Namestore.
 *
 * @param ego ego for which the DID should be created.
 * @param did_document did_document that should be saved in namestore.
 * If did_document==NULL -> Default DID document is created.
 * @param cfg_handle pointer to configuration handle
 * @param identity_hanlde pointer to identity handle. Can be NULL if ego!=NULL
 * @param namestore_handle
 * @param cont callback function
 * @param cls closure
 */
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)
{
  struct GNUNET_IDENTITY_PublicKey pkey;

  // Ego, namestore_handle and cont must be set
  if ((ego == NULL) || (namestore_handle == NULL) || (cont == NULL))
    return GNUNET_NO;

  // Check if ego has EdDSA key
  GNUNET_IDENTITY_ego_get_public_key ((struct GNUNET_IDENTITY_Ego *) ego,
                                      &pkey);
  if (ntohl (pkey.type) != GNUNET_GNSRECORD_TYPE_EDKEY)
  {
    printf ("The EGO has to have an EdDSA key pair\n");
    return GNUNET_NO;
  }

  struct DID_create_namestore_lookup_closure *cls2
    = malloc (sizeof(struct DID_create_namestore_lookup_closure));
  cls2->did_document = did_document;
  cls2->expire_time = (*expire_time);
  cls2->namestore_handle = namestore_handle;
  cls2->cont = cont;
  cls2->cls = cls;

  // Check if ego already has a DID Document
  GNUNET_NAMESTORE_records_lookup (namestore_handle,
                                   GNUNET_IDENTITY_ego_get_private_key (ego),
                                   DID_DOCUMENT_LABEL,
                                   NULL,
                                   NULL,
                                   DID_create_namestore_lookup_cb,
                                   (void *) cls2);

  return GNUNET_OK;
}