aboutsummaryrefslogtreecommitdiff
path: root/src/gnsrecord/gnunet-gnsrecord-tvg.c
blob: e0ac5881a631b6d72b984ddae790f7c81873e290 (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
/*
     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
 */

/**
 * @file util/gnunet-gns-tvg.c
 * @brief Generate test vectors for GNS.
 * @author Martin Schanzenbach
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_signatures.h"
#include "gnunet_gns_service.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_dnsparser_lib.h"
#include "gnunet_testing_lib.h"
#include <inttypes.h>

#define TEST_RECORD_LABEL "test"
#define TEST_RECORD_A "1.2.3.4"
#define TEST_RRCOUNT 2

static void
print_record(const struct GNUNET_GNSRECORD_Data *rd)
{
  char *data_enc;
  char *string_v;
  string_v = GNUNET_GNSRECORD_value_to_string (rd->record_type,
                                               rd->data,
                                               rd->data_size);
    fprintf (stdout,
           "EXPIRATION: %"PRIu64"\n", rd->expiration_time);
  fprintf (stdout,
           "DATA_SIZE: %"PRIu64"\n", rd->data_size);
  fprintf (stdout,
           "TYPE: %d\n", rd->record_type);
  fprintf (stdout,
           "FLAGS: %d\n", rd->flags);
  GNUNET_STRINGS_base64_encode (rd->data,
                                rd->data_size,
                                &data_enc);
  fprintf (stdout,
           "DATA (base64):\n%s\n",
           data_enc);
  fprintf (stdout,
           "DATA (Human readable):\n%s\n\n", string_v);
  GNUNET_free (string_v);

  GNUNET_free (data_enc);
}

/**
 * Main function that will be run.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_GNSRECORD_Data rd[2];
  struct GNUNET_TIME_Absolute exp_abs = GNUNET_TIME_absolute_get();
  struct GNUNET_GNSRECORD_Block *rrblock;
  char *bdata;
  struct GNUNET_CRYPTO_EcdsaPrivateKey id_priv;
  struct GNUNET_CRYPTO_EcdsaPublicKey id_pub;
  struct GNUNET_CRYPTO_EcdsaPrivateKey pkey_data_p;
  struct GNUNET_CRYPTO_EcdsaPublicKey pkey_data;
  void *data;
  size_t data_size;
  char *rdata;
  size_t rdata_size;

  GNUNET_CRYPTO_ecdsa_key_create (&id_priv);
  GNUNET_CRYPTO_ecdsa_key_get_public (&id_priv,
                                      &id_pub);

  GNUNET_CRYPTO_ecdsa_key_create (&pkey_data_p);
  GNUNET_CRYPTO_ecdsa_key_get_public (&pkey_data_p,
                                      &pkey_data);
  fprintf (stdout,
           "Label: %s\nRRCOUNT: %d\n\n", TEST_RECORD_LABEL, TEST_RRCOUNT);
  memset (rd, 0, sizeof (struct GNUNET_GNSRECORD_Data) * 2);
  GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A, TEST_RECORD_A, &data, &data_size));
  rd[0].data = data;
  rd[0].data_size = data_size;
  rd[0].expiration_time = exp_abs.abs_value_us;
  rd[0].record_type = GNUNET_DNSPARSER_TYPE_A;
  fprintf (stdout, "Record #0\n");
  print_record (&rd[0]);

  rd[1].data = &pkey_data;
  rd[1].data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
  rd[1].expiration_time = exp_abs.abs_value_us;
  rd[1].record_type = GNUNET_GNSRECORD_TYPE_PKEY;
  rd[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
  fprintf (stdout, "Record #1\n");
  print_record (&rd[1]);

  rdata_size = GNUNET_GNSRECORD_records_get_size (2,
                                                  rd);
  rdata = GNUNET_malloc (rdata_size);
  GNUNET_GNSRECORD_records_serialize (2,
                                      rd,
                                      rdata_size,
                                      rdata);
  char* data_enc;
  GNUNET_STRINGS_base64_encode (rdata,
                                rdata_size,
                                &data_enc);
  fprintf(stdout, "RDATA:\n%s\n\n", data_enc);
  GNUNET_free (data_enc);
  rrblock = GNUNET_GNSRECORD_block_create (&id_priv,
                                         exp_abs,
                                         TEST_RECORD_LABEL,
                                         rd,
                                         TEST_RRCOUNT);
  size_t bdata_size = ntohl (rrblock->purpose.size) -
    sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) -
    sizeof(struct GNUNET_TIME_AbsoluteNBO);
  size_t rrblock_size = ntohl (rrblock->purpose.size) +
    sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
    sizeof(struct GNUNET_CRYPTO_EcdsaSignature);

  bdata = (char*)&rrblock[1];
  GNUNET_STRINGS_base64_encode (bdata,
                                bdata_size,
                                &data_enc);
  fprintf(stdout, "BDATA:\n%s\n\n", data_enc);
  GNUNET_free (data_enc);
  GNUNET_STRINGS_base64_encode (rrblock,
                                rrblock_size,
                                &data_enc);
  fprintf(stdout, "RRBLOCK:\n%s\n", data_enc);
  GNUNET_free (data_enc);

}


/**
 * The main function of the test vector generation tool.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc,
      char *const *argv)
{
  const struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };

  GNUNET_assert (GNUNET_OK ==
                 GNUNET_log_setup ("gnunet-gns-tvg",
                                   "INFO",
                                   NULL));
  if (GNUNET_OK !=
      GNUNET_PROGRAM_run (argc, argv,
                          "gnunet-gns-tvg",
                          "Generate test vectors for GNS",
                          options,
                          &run, NULL))
    return 1;
  return 0;
}


/* end of gnunet-gns-tvg.c */