summaryrefslogtreecommitdiff
path: root/src/lib/anastasis_meta.c
blob: ae20db5ce469e374c7cdf7b580199130f92537bc (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
/*
  This file is part of Anastasis
  Copyright (C) 2022 Anastasis SARL

  Anastasis 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.

  Anastasis 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
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @brief anastasis client api to get recovery document meta data
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis.h"
#include <taler/taler_json_lib.h>
#include <gnunet/gnunet_util_lib.h>
#include <taler/taler_merchant_service.h>


/**
 * Handle for a version check operation.
 */
struct ANASTASIS_VersionCheck
{
  /**
   * Function to call with results.
   */
  ANASTASIS_MetaPolicyCallback mpc;

  /**
   * Closure for @e mpc.
   */
  void *mpc_cls;

  /**
   * Handle for the actual REST operation.
   */
  struct ANASTASIS_PolicyMetaLookupOperation *plm;

  /**
   * User identifier (needed to decrypt).
   */
  struct ANASTASIS_CRYPTO_UserIdentifierP id;
};


/**
 * Function called with results from a GET /policy/$POL/meta request
 *
 * @param cls closure with the `struct ANASTASIS_VersionCheck *`
 * @param dd the response details
 */
static void
meta_cb (
  void *cls,
  const struct ANASTASIS_MetaDownloadDetails *dd)
{
  struct ANASTASIS_VersionCheck *vc = cls;

  vc->plm = NULL;
  if (MHD_HTTP_OK != dd->http_status)
  {
    vc->mpc (vc->mpc_cls,
             0,
             GNUNET_TIME_UNIT_ZERO_TS,
             NULL,
             NULL);
    ANASTASIS_recovery_get_versions_cancel (vc);
    return;
  }
  for (size_t i = 0; i<dd->details.ok.metas_length; i++)
  {
    const struct ANASTASIS_MetaDataEntry *meta
      = &dd->details.ok.metas[i];
    const char *secret_name = NULL;
    const struct GNUNET_HashCode *eph;
    void *dec;
    size_t dec_len;

    if (GNUNET_OK !=
        ANASTASIS_CRYPTO_recovery_metadata_decrypt (
          &vc->id,
          meta->meta_data,
          meta->meta_data_size,
          &dec,
          &dec_len))
    {
      GNUNET_break_op (0);
      continue;
    }
    if (sizeof (*eph) > dec_len)
    {
      GNUNET_break_op (0);
      GNUNET_free (dec);
      continue;
    }
    eph = dec;
    if (sizeof (*eph) < dec_len)
    {
      secret_name = (const char *) &eph[1];
      dec_len -= sizeof (*eph);
      if ('\0' != secret_name[dec_len - 1])
      {
        GNUNET_break_op (0);
        GNUNET_free (dec);
        continue;
      }
    }
    vc->mpc (vc->mpc_cls,
             meta->version,
             meta->server_time,
             eph,
             secret_name);
    GNUNET_free (dec);
  }
  vc->mpc (vc->mpc_cls,
           0,
           GNUNET_TIME_UNIT_ZERO_TS,
           NULL,
           NULL);
  ANASTASIS_recovery_get_versions_cancel (vc);
}


struct ANASTASIS_VersionCheck *
ANASTASIS_recovery_get_versions (
  struct GNUNET_CURL_Context *ctx,
  const json_t *id_data,
  unsigned int max_version,
  const char *anastasis_provider_url,
  const struct ANASTASIS_CRYPTO_ProviderSaltP *provider_salt,
  ANASTASIS_MetaPolicyCallback mpc,
  void *mpc_cls)
{
  struct ANASTASIS_VersionCheck *vc;
  struct ANASTASIS_CRYPTO_AccountPublicKeyP account_pub;

  vc = GNUNET_new (struct ANASTASIS_VersionCheck);
  vc->mpc = mpc;
  vc->mpc_cls = mpc_cls;
  ANASTASIS_CRYPTO_user_identifier_derive (id_data,
                                           provider_salt,
                                           &vc->id);
  ANASTASIS_CRYPTO_account_public_key_derive (&vc->id,
                                              &account_pub);
  vc->plm = ANASTASIS_policy_meta_lookup (ctx,
                                          anastasis_provider_url,
                                          &account_pub,
                                          max_version,
                                          &meta_cb,
                                          vc);
  if (NULL == vc->plm)
  {
    GNUNET_break (0);
    GNUNET_free (vc);
    return NULL;
  }
  return vc;
}


void
ANASTASIS_recovery_get_versions_cancel (struct ANASTASIS_VersionCheck *vc)
{
  if (NULL != vc->plm)
  {
    ANASTASIS_policy_meta_lookup_cancel (vc->plm);
    vc->plm = NULL;
  }
  GNUNET_free (vc);
}