fix bit counting mess
[oweals/gnunet.git] / src / gnsrecord / gnunet-gnsrecord-tvg.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2020 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file util/gnunet-gns-tvg.c
23  * @brief Generate test vectors for GNS.
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_gns_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include <inttypes.h>
34
35 #define TEST_RECORD_LABEL "test"
36 #define TEST_RECORD_A "1.2.3.4"
37 #define TEST_RRCOUNT 2
38
39 static void
40 print_record(const struct GNUNET_GNSRECORD_Data *rd)
41 {
42   char *data_enc;
43   char *string_v;
44   string_v = GNUNET_GNSRECORD_value_to_string (rd->record_type,
45                                                rd->data,
46                                                rd->data_size);
47     fprintf (stdout,
48            "EXPIRATION: %"PRIu64"\n", rd->expiration_time);
49   fprintf (stdout,
50            "DATA_SIZE: %zu\n", rd->data_size);
51   fprintf (stdout,
52            "TYPE: %d\n", rd->record_type);
53   fprintf (stdout,
54            "FLAGS: %d\n", rd->flags);
55   GNUNET_STRINGS_base64_encode (rd->data,
56                                 rd->data_size,
57                                 &data_enc);
58   fprintf (stdout,
59            "DATA (base64):\n%s\n",
60            data_enc);
61   fprintf (stdout,
62            "DATA (Human readable):\n%s\n\n", string_v);
63   GNUNET_free (string_v);
64
65   GNUNET_free (data_enc);
66 }
67
68 /**
69  * Main function that will be run.
70  *
71  * @param cls closure
72  * @param args remaining command-line arguments
73  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
74  * @param cfg configuration
75  */
76 static void
77 run (void *cls,
78      char *const *args,
79      const char *cfgfile,
80      const struct GNUNET_CONFIGURATION_Handle *cfg)
81 {
82   struct GNUNET_GNSRECORD_Data rd[2];
83   struct GNUNET_TIME_Absolute exp_abs = GNUNET_TIME_absolute_get();
84   struct GNUNET_GNSRECORD_Block *rrblock;
85   char *bdata;
86   struct GNUNET_CRYPTO_EcdsaPrivateKey id_priv;
87   struct GNUNET_CRYPTO_EcdsaPublicKey id_pub;
88   struct GNUNET_CRYPTO_EcdsaPrivateKey pkey_data_p;
89   struct GNUNET_CRYPTO_EcdsaPublicKey pkey_data;
90   void *data;
91   size_t data_size;
92   char *rdata;
93   size_t rdata_size;
94   char* data_enc;
95
96   GNUNET_CRYPTO_ecdsa_key_create (&id_priv);
97   GNUNET_CRYPTO_ecdsa_key_get_public (&id_priv,
98                                       &id_pub);
99   GNUNET_STRINGS_base64_encode (&id_priv,
100                                 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
101                                 &data_enc);
102   fprintf(stdout, "Zone private key (d):\n%s\n", data_enc);
103   GNUNET_free (data_enc);
104   GNUNET_STRINGS_base64_encode (&id_pub,
105                                 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
106                                 &data_enc);
107   fprintf(stdout, "Zone public key (zk):\n%s\n", data_enc);
108   GNUNET_free (data_enc);
109
110
111   GNUNET_CRYPTO_ecdsa_key_create (&pkey_data_p);
112   GNUNET_CRYPTO_ecdsa_key_get_public (&pkey_data_p,
113                                       &pkey_data);
114   fprintf (stdout,
115            "Label: %s\nRRCOUNT: %d\n\n", TEST_RECORD_LABEL, TEST_RRCOUNT);
116   memset (rd, 0, sizeof (struct GNUNET_GNSRECORD_Data) * 2);
117   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (GNUNET_DNSPARSER_TYPE_A, TEST_RECORD_A, &data, &data_size));
118   rd[0].data = data;
119   rd[0].data_size = data_size;
120   rd[0].expiration_time = exp_abs.abs_value_us;
121   rd[0].record_type = GNUNET_DNSPARSER_TYPE_A;
122   fprintf (stdout, "Record #0\n");
123   print_record (&rd[0]);
124
125   rd[1].data = &pkey_data;
126   rd[1].data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
127   rd[1].expiration_time = exp_abs.abs_value_us;
128   rd[1].record_type = GNUNET_GNSRECORD_TYPE_PKEY;
129   rd[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
130   fprintf (stdout, "Record #1\n");
131   print_record (&rd[1]);
132
133   rdata_size = GNUNET_GNSRECORD_records_get_size (2,
134                                                   rd);
135   rdata = GNUNET_malloc (rdata_size);
136   GNUNET_GNSRECORD_records_serialize (2,
137                                       rd,
138                                       rdata_size,
139                                       rdata);
140   GNUNET_STRINGS_base64_encode (rdata,
141                                 rdata_size,
142                                 &data_enc);
143   fprintf(stdout, "RDATA:\n%s\n\n", data_enc);
144   GNUNET_free (data_enc);
145   rrblock = GNUNET_GNSRECORD_block_create (&id_priv,
146                                          exp_abs,
147                                          TEST_RECORD_LABEL,
148                                          rd,
149                                          TEST_RRCOUNT);
150   size_t bdata_size = ntohl (rrblock->purpose.size) -
151     sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) -
152     sizeof(struct GNUNET_TIME_AbsoluteNBO);
153   size_t rrblock_size = ntohl (rrblock->purpose.size) +
154     sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
155     sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
156
157   bdata = (char*)&rrblock[1];
158   GNUNET_STRINGS_base64_encode (bdata,
159                                 bdata_size,
160                                 &data_enc);
161   fprintf(stdout, "BDATA:\n%s\n\n", data_enc);
162   GNUNET_free (data_enc);
163   GNUNET_STRINGS_base64_encode (rrblock,
164                                 rrblock_size,
165                                 &data_enc);
166   fprintf(stdout, "RRBLOCK:\n%s\n", data_enc);
167   GNUNET_free (data_enc);
168
169 }
170
171
172 /**
173  * The main function of the test vector generation tool.
174  *
175  * @param argc number of arguments from the command line
176  * @param argv command line arguments
177  * @return 0 ok, 1 on error
178  */
179 int
180 main (int argc,
181       char *const *argv)
182 {
183   const struct GNUNET_GETOPT_CommandLineOption options[] = {
184     GNUNET_GETOPT_OPTION_END
185   };
186
187   GNUNET_assert (GNUNET_OK ==
188                  GNUNET_log_setup ("gnunet-gns-tvg",
189                                    "INFO",
190                                    NULL));
191   if (GNUNET_OK !=
192       GNUNET_PROGRAM_run (argc, argv,
193                           "gnunet-gns-tvg",
194                           "Generate test vectors for GNS",
195                           options,
196                           &run, NULL))
197     return 1;
198   return 0;
199 }
200
201
202 /* end of gnunet-gns-tvg.c */