aboutsummaryrefslogtreecommitdiff
path: root/src/reclaim/did_core.c
blob: dea1c82c3342ee3ac4082f2c3c67a0f31e403828 (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
/*
   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
 */


#include "did_core.h"

// #define DID_DOCUMENT_LABEL GNUNET_GNS_EMPTY_LABEL_AT
#define DID_DOCUMENT_LABEL "didd"
#define DID_DOCUMENT_DEFAULT_EXPIRATION_TIME "1d"

static DID_resolve_callback *resolve_cb;
static DID_action_callback *action_cb;
static void *closure;

// ------------------------------------------------ //
// -------------------- 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)
{
  /*
   * FIXME-MSC: The user may decide to put other records here.
   * In general I am fine with the constraint here, but not when
   * we move it to "@"
   */

  char *didd;

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

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

/**
 * @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 resolved
 * @param gns_handle pointer to gns handle.
 * @param cont callback function
 * @param cls closure
 */
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;

  if ((did == NULL) || (gns_handle == NULL) || (cont == NULL))
    return GNUNET_NO;

  resolve_cb = cont;
  closure = cls;

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

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

  return GNUNET_OK;
}

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

static void
DID_create_did_store_cb (void *cls,
                         int32_t success,
                         const char *emsg)
{
  if (GNUNET_OK == success)
  {
    // TEST
    struct GNUNET_IDENTITY_PublicKey *pkey_test;
    GNUNET_IDENTITY_ego_get_public_key ((struct GNUNET_IDENTITY_Ego *) closure,
                                        pkey_test);
    printf ("pkey1: %s\n", GNUNET_IDENTITY_public_key_to_string (pkey_test));

    printf ("cls3: %s\n", (char *) closure);
    action_cb (GNUNET_OK, closure);
  }
  else
  {
    printf ("%s\n", emsg);
    action_cb (GNUNET_NO, closure);
  }
}

/**
 * @brief Store DID Document in Namestore
 *
 * @param didd_str String endoced DID Docuement
 * @param ego Identity whos DID Document is stored
 */
static enum GNUNET_GenericReturnValue
DID_create_did_store (struct GNUNET_NAMESTORE_Handle *namestore_handle,
                      char *didd_str, struct GNUNET_IDENTITY_Ego *ego)
{

  struct GNUNET_TIME_Relative expire_time;
  struct GNUNET_GNSRECORD_Data record_data;
  const struct GNUNET_IDENTITY_PrivateKey *skey;

  if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (
        DID_DOCUMENT_DEFAULT_EXPIRATION_TIME, &expire_time))
  {
    printf ("Failed to read given expiration time\n");
    return GNUNET_NO;
  }

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

  skey = GNUNET_IDENTITY_ego_get_private_key (ego);

  // TEST
  struct GNUNET_IDENTITY_PublicKey *pkey_test;
  GNUNET_IDENTITY_ego_get_public_key ((struct GNUNET_IDENTITY_Ego *) closure,
                                      pkey_test);
  printf ("pkey1: %s\n", GNUNET_IDENTITY_public_key_to_string (pkey_test));

  GNUNET_NAMESTORE_records_store (namestore_handle,
                                  skey,
                                  DID_DOCUMENT_LABEL,
                                  1,   // FIXME what if GNUNET_GNS_EMPTY_LABEL_AT has records
                                  &record_data,
                                  &DID_create_did_store_cb,
                                  NULL);

  return GNUNET_OK;
}

// TODO: Expiration time missing

/**
 * @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_CONFIGURATION_Handle *cfg_handle,
            struct GNUNET_IDENTITY_Handle *identity_handle,
            struct GNUNET_NAMESTORE_Handle *namestore_handle,
            DID_action_callback *cont,
            void *cls)
{
  struct GNUNET_IDENTITY_PublicKey pkey;

  GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);

  if (ntohl (pkey.type) != GNUNET_GNSRECORD_TYPE_EDKEY)
  {
    printf ("The EGO has to have an EDDSA key pair\n");
    return GNUNET_NO;
  }

  if (did_document != NULL)
    printf (
      "DID Docuement is read from \"DID-document\" argument (EXPERIMENTAL)\n");
  else
    did_document = DID_pkey_to_did_document (&pkey);


  // TEST
  struct GNUNET_IDENTITY_PublicKey *pkey_test;
  GNUNET_IDENTITY_ego_get_public_key ((struct GNUNET_IDENTITY_Ego *) cls,
                                      pkey_test);
  printf ("pkey1: %s\n", GNUNET_IDENTITY_public_key_to_string (pkey_test));

  action_cb = cont;
  closure = cls;

  return DID_create_did_store (namestore_handle, did_document, ego);
}