aboutsummaryrefslogtreecommitdiff
path: root/src/escrow/escrow_api.c
blob: b293c05a59b86a2272b573e8439506239b8ea833 (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
/*
   This file is part of GNUnet.
   Copyright (C) 2020 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
 */

/**
 * @author Johannes Späth
 * @file escrow/escrow_api.c
 * 
 * @brief api to interact with the escrow component
 */

#include "gnunet_util_lib.h"
#include "gnunet_escrow_lib.h"
#include "gnunet_escrow_plugin.h"


/**
 * Init canary for the plaintext plugin
 */
static int plaintext_initialized;


/**
 * Init canary for the GNS plugin
 */
static int gns_initialized;


/**
 * Init canary for the Anastasis plugin
 */
static int anastasis_initialized;


/**
 * Pointer to the plaintext plugin API
 */
static struct GNUNET_ESCROW_KeyPluginFunctions *plaintext_api;


/**
 * Pointer to the GNS plugin API
 */
static struct GNUNET_ESCROW_KeyPluginFunctions *gns_api;


/**
 * Pointer to the Anastasis plugin API
 */
static struct GNUNET_ESCROW_KeyPluginFunctions *anastasis_api;


/**
 * Initialize an escrow plugin
 * 
 * @param method the escrow method determining the plugin
 * 
 * @return pointer to the escrow plugin API
 */
struct GNUNET_ESCROW_KeyPluginFunctions *
init_plugin (enum GNUNET_ESCROW_Key_Escrow_Method method)
{
  switch (method)
  {
    case GNUNET_ESCROW_KEY_PLAINTEXT:
      if (GNUNET_YES == plaintext_initialized)
        return plaintext_api;
      plaintext_initialized = GNUNET_YES;
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Loading PLAINTEXT escrow plugin\n");
      plaintext_api = GNUNET_PLUGIN_load ("libgnunet_plugin_escrow_plaintext",
                                          NULL);
      return plaintext_api;
    case GNUNET_ESCROW_KEY_GNS:
      if (GNUNET_YES == gns_initialized)
        return gns_api;
      gns_initialized = GNUNET_YES;
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Loading GNS escrow plugin\n");
      gns_api = GNUNET_PLUGIN_load ("libgnunet_plugin_escrow_gns",
                                    NULL);
      return gns_api;
    case GNUNET_ESCROW_KEY_ANASTASIS:
      if (GNUNET_YES == anastasis_initialized)
        return anastasis_api;
      anastasis_initialized = GNUNET_YES;
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Loading ANASTASIS escrow plugin\n");
      anastasis_api = GNUNET_PLUGIN_load ("libgnunet_plugin_escrow_anastasis",
                                          NULL);
      return anastasis_api;
  }
  // should never be reached
  return NULL;
}


/**
 * Unload all loaded plugins on destruction.
 */
void __attribute__ ((destructor))
GNUNET_ESCROW_fini_plugins ()
{
  if (GNUNET_YES == plaintext_initialized)
  {
    plaintext_initialized = GNUNET_NO;
    GNUNET_break (NULL == 
                  GNUNET_PLUGIN_unload ("libgnunet_plugin_escrow_plaintext",
                                        plaintext_api));
    plaintext_api = NULL;
  }

  if (GNUNET_YES == gns_initialized)
  {
    gns_initialized = GNUNET_NO;
    GNUNET_break (NULL == 
                  GNUNET_PLUGIN_unload ("libgnunet_plugin_escrow_gns",
                                        gns_api));
    gns_api = NULL;
  }

  if (GNUNET_YES == anastasis_initialized)
  {
    anastasis_initialized = GNUNET_NO;
    GNUNET_break (NULL == 
                  GNUNET_PLUGIN_unload ("libgnunet_plugin_escrow_anastasis",
                                        anastasis_api));
    anastasis_api = NULL;
  }
}


/**
 * Put some data in escrow using the specified escrow method
 * 
 * @param ego the identity ego to put in escrow
 * @param method the escrow method to use
 * 
 * @return the escrow anchor needed to get the data back
 */
void *
GNUNET_ESCROW_put (const struct GNUNET_IDENTITY_Ego *ego,
                   enum GNUNET_ESCROW_Key_Escrow_Method method)
{
  struct GNUNET_ESCROW_KeyPluginFunctions *api;

  api = init_plugin (method);
  return api->start_key_escrow (ego);
}


/**
 * Renew the escrow of the data related to the given escrow anchor
 * 
 * @param escrowAnchor the escrow anchor returned by the GNUNET_ESCROW_put method
 * @param method the escrow method to use
 * 
 * @return the escrow anchor needed to get the data back
 */
void *
GNUNET_ESCROW_renew (void *escrowAnchor,
                     enum GNUNET_ESCROW_Key_Escrow_Method method)
{
  struct GNUNET_ESCROW_KeyPluginFunctions *api;

  api = init_plugin (method);
  return api->renew_key_escrow (escrowAnchor);
}


/**
 * Get the escrowed data back
 * 
 * @param escrowAnchor the escrow anchor returned by the GNUNET_ESCROW_put method
 * @param egoName the name of the ego to get back
 * @param method the escrow method to use
 * 
 * @return a new identity ego restored from the escrow
 */
const struct GNUNET_IDENTITY_Ego *
GNUNET_ESCROW_get (void *escrowAnchor,
                   char *egoName,
                   enum GNUNET_ESCROW_Key_Escrow_Method method)
{
  struct GNUNET_ESCROW_KeyPluginFunctions *api;

  api = init_plugin (method);
  return api->restore_key (escrowAnchor, egoName);
}


/**
 * Verify the escrowed data
 * 
 * @param ego the identity ego that was put into escrow
 * @param escrowAnchor the escrow anchor returned by the GNUNET_ESCROW_put method
 * @param method the escrow method to use
 * 
 * @return GNUNET_ESCROW_VALID if the escrow could successfully by restored,
 *         GNUNET_ESCROW_RENEW_NEEDED if the escrow needs to be renewed,
 *         GNUNET_ESCROW_INVALID otherwise
 */
int
GNUNET_ESCROW_verify (const struct GNUNET_IDENTITY_Ego *ego,
                      void *escrowAnchor,
                      enum GNUNET_ESCROW_Key_Escrow_Method method)
{
  struct GNUNET_ESCROW_KeyPluginFunctions *api;

  api = init_plugin (method);
  return api->verify_key_escrow (ego, escrowAnchor);
}