summaryrefslogtreecommitdiff
path: root/src/extensions/extensions.c
blob: 55d970c57280c3a870612e302e23aa2ec3348822 (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
   This file is part of TALER
   Copyright (C) 2021-2022 Taler Systems SA

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

   TALER 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file extensions.c
 * @brief Utility functions for extensions
 * @author Özgür Kesim
 */
#include "platform.h"
#include "taler_util.h"
#include "taler_signatures.h"
#include "taler_extensions.h"
#include "stdint.h"


/* head of the list of all registered extensions */
static struct TALER_Extension *_extensions = NULL;
static bool _initialized = false;

void
TALER_extensions_init ()
{
  extern struct TALER_Extension _extension_age_restriction;
  if (! _initialized)
    _extensions = &_extension_age_restriction;

  _initialized = true;
}


const struct TALER_Extension *
TALER_extensions_get_head ()
{
  return _extensions;
}


enum GNUNET_GenericReturnValue
TALER_extensions_add (
  const struct TALER_Extension *new)
{
  struct TALER_Extension *ext;

  if (_initialized)
    return GNUNET_SYSERR;

  GNUNET_assert (NULL != _extensions);

  /* Sanity checks */
  if (NULL == new ||
      NULL == new->name ||
      NULL == new->version ||
      NULL == new->disable ||
      NULL == new->test_json_config ||
      NULL == new->load_json_config ||
      NULL == new->config_to_json ||
      NULL == new->load_taler_config ||
      NULL == new->next)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid extension\n");
    return GNUNET_SYSERR;
  }

  /* Check for collisions */
  for (ext = _extensions; NULL != ext; ext = ext->next)
  {
    if (new->type == ext->type ||
        0 == strcmp (new->name, ext->name))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "extension collision\n");
      return GNUNET_NO;
    }
  }

  /* No collisions found, so add this extension to the list */
  ext->next = (struct TALER_Extension *) new;

  return GNUNET_OK;
}


const struct TALER_Extension *
TALER_extensions_get_by_type (
  enum TALER_Extension_Type type)
{

  for (const struct TALER_Extension *it = _extensions;
       NULL != it;
       it = it->next)
  {
    if (it->type == type)
      return it;
  }

  /* No extension found. */
  return NULL;
}


bool
TALER_extensions_is_enabled_type (
  enum TALER_Extension_Type type)
{
  const struct TALER_Extension *ext =
    TALER_extensions_get_by_type (type);

  return (NULL != ext &&
          TALER_extensions_is_enabled (ext));
}


const struct TALER_Extension *
TALER_extensions_get_by_name (
  const char *name)
{
  for (const struct TALER_Extension *it = _extensions;
       NULL != it;
       it = it->next)
  {
    if (0 == strcmp (name, it->name))
      return it;
  }
  /* No extension found. */
  return NULL;
}


enum GNUNET_GenericReturnValue
config_hash_verify (
  const struct TALER_ExtensionConfigHash *h_config,
  const struct TALER_MasterPublicKeyP *master_pub,
  const struct TALER_MasterSignatureP *master_sig
  )
{
  struct TALER_MasterExtensionConfigurationPS ec = {
    .purpose.purpose = htonl (TALER_SIGNATURE_MASTER_EXTENSION),
    .purpose.size = htonl (sizeof(ec)),
    .h_config = *h_config
  };

  return GNUNET_CRYPTO_eddsa_verify (
    TALER_SIGNATURE_MASTER_EXTENSION,
    &ec,
    &master_sig->eddsa_signature,
    &master_pub->eddsa_pub);
}


enum GNUNET_GenericReturnValue
TALER_extensions_verify_json_config_signature (
  json_t *extensions,
  struct TALER_MasterSignatureP *extensions_sig,
  struct TALER_MasterPublicKeyP *master_pub)
{
  struct TALER_ExtensionConfigHash h_config;

  if (GNUNET_OK !=
      TALER_JSON_extensions_config_hash (extensions, &h_config))
    return GNUNET_SYSERR;

  if (GNUNET_OK != config_hash_verify (
        &h_config,
        master_pub,
        extensions_sig))
    return GNUNET_NO;

  return GNUNET_OK;
}


struct load_conf_closure
{
  const struct GNUNET_CONFIGURATION_Handle *cfg;
  enum GNUNET_GenericReturnValue error;
};

static void
collect_extensions (
  void *cls,
  const char *section)
{
  struct load_conf_closure *col = cls;
  const char *name;
  const struct TALER_Extension *extension;

  if (GNUNET_OK != col->error)
    return;

  if (0 != strncasecmp (section,
                        TALER_EXTENSION_SECTION_PREFIX,
                        sizeof(TALER_EXTENSION_SECTION_PREFIX) - 1))
  {
    return;
  }

  name = section + sizeof(TALER_EXTENSION_SECTION_PREFIX) - 1;

  if (NULL == (extension = TALER_extensions_get_by_name (name)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Unsupported extension `%s` (section [%s]).\n", name,
                section);
    col->error = GNUNET_SYSERR;
    return;
  }

  if (GNUNET_OK !=
      extension->load_taler_config (
        (struct TALER_Extension *) extension,
        col->cfg))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Couldn't parse configuration for extension `%s` (section [%s]).\n",
                name,
                section);
    col->error = GNUNET_SYSERR;
    return;
  }
}


enum GNUNET_GenericReturnValue
TALER_extensions_load_taler_config (
  const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct load_conf_closure col = {
    .cfg = cfg,
    .error = GNUNET_OK,
  };

  GNUNET_CONFIGURATION_iterate_sections (cfg,
                                         &collect_extensions,
                                         &col);
  return col.error;
}


static enum GNUNET_GenericReturnValue
is_json_extension_config (
  json_t *obj,
  int *critical,
  const char **version,
  json_t **config)
{
  enum GNUNET_GenericReturnValue ret;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_boolean ("critical",
                              critical),
    GNUNET_JSON_spec_string ("version",
                             version),
    GNUNET_JSON_spec_json ("config",
                           config),
    GNUNET_JSON_spec_end ()
  };

  ret = GNUNET_JSON_parse (obj, spec, NULL, NULL);
  if (GNUNET_OK == ret)
    GNUNET_JSON_parse_free (spec);

  return ret;
}


enum GNUNET_GenericReturnValue
TALER_extensions_load_json_config (
  json_t *extensions)
{
  const char*name;
  json_t *blob;

  GNUNET_assert (NULL != extensions);
  GNUNET_assert (json_is_object (extensions));

  json_object_foreach (extensions, name, blob)
  {
    int critical;
    const char *version;
    json_t *config;
    const struct TALER_Extension *extension =
      TALER_extensions_get_by_name (name);

    if (NULL == extension)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "no such extension: %s\n", name);
      return GNUNET_SYSERR;
    }

    /* load and verify criticality, version, etc. */
    if (GNUNET_OK !=
        is_json_extension_config (
          blob, &critical, &version, &config))
      return GNUNET_SYSERR;

    if (critical != extension->critical
        || 0 != strcmp (version, extension->version) // TODO: libtool compare?
        || NULL == config
        || GNUNET_OK != extension->test_json_config (config))
      return GNUNET_SYSERR;

    /* This _should_ work now */
    if (GNUNET_OK !=
        extension->load_json_config ((struct TALER_Extension *) extension,
                                     config))
      return GNUNET_SYSERR;
  }

  /* make sure to disable all extensions that weren't mentioned in the json */
  for (const struct TALER_Extension *it = TALER_extensions_get_head ();
       NULL != it;
       it = it->next)
  {
    if (NULL == json_object_get (extensions, it->name))
      it->disable ((struct TALER_Extension *) it);
  }

  return GNUNET_OK;
}


/* end of extensions.c */