GNUnet  0.19.3
gnunet-gnsrecord-tvg.c
Go to the documentation of this file.
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 
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_testing_lib.h"
32 #include "gnsrecord_crypto.h"
33 #include <inttypes.h>
34 
35 
36 static char *d_pkey =
37  "50d7b652a4efeadff37396909785e5952171a02178c8e7d450fa907925fafd98";
38 
39 static char *d_edkey =
40  "5af7020ee19160328832352bbc6a68a8d71a7cbe1b929969a7c66d415a0d8f65";
41 
42 
43 static int
44 parsehex (char *src, char *dst, size_t dstlen, int invert)
45 {
46  char *line = src;
47  char *data = line;
48  int off;
49  int read_byte;
50  int data_len = 0;
51 
52  while (sscanf (data, " %02x%n", &read_byte, &off) == 1)
53  {
54  if (invert)
55  dst[dstlen - 1 - data_len++] = read_byte;
56  else
57  dst[data_len++] = read_byte;
58  data += off;
59  }
60  return data_len;
61 }
62 
63 
64 static void
66  size_t buf_len,
67  int fold,
68  int in_be)
69 {
70  int i;
71 
72  for (i = 0; i < buf_len; i++)
73  {
74  if ((0 != i) && (0 != fold) && (i % fold == 0))
75  printf ("\n");
76  if (in_be)
77  printf ("%02x", ((unsigned char*) buf)[buf_len - 1 - i]);
78  else
79  printf ("%02x", ((unsigned char*) buf)[i]);
80  }
81  printf ("\n");
82 }
83 
84 
85 static void
87  size_t buf_len,
88  int fold)
89 {
90  print_bytes_ (buf, buf_len, fold, 0);
91 }
92 
93 
94 static void
96 {
97  uint16_t flags = htons (rd->flags);
98  uint64_t abs_nbo = GNUNET_htonll (rd->expiration_time);
99  uint16_t size_nbo = htons (rd->data_size);
100  uint32_t type_nbo = htonl (rd->record_type);
101  printf ("EXPIRATION:\n");
102  print_bytes (&abs_nbo, sizeof (abs_nbo), 8);
103  printf ("\nDATA_SIZE:\n");
104  print_bytes (&size_nbo, sizeof (size_nbo), 8);
105  printf ("\nTYPE:\n");
106  print_bytes (&type_nbo, sizeof (type_nbo), 8);
107  printf ("\nFLAGS: ");
108  print_bytes ((void*) &flags, sizeof (flags), 8);
109  printf ("\n");
110  fprintf (stdout,
111  "DATA:\n");
112  print_bytes ((char*) rd->data, rd->data_size, 8);
113  printf ("\n");
114 }
115 
116 
125 static void
126 run_pkey (struct GNUNET_GNSRECORD_Data *rd, int rd_count, const char *label)
127 {
129  struct GNUNET_GNSRECORD_Block *rrblock;
130  char *bdata;
131  struct GNUNET_IDENTITY_PrivateKey id_priv;
132  struct GNUNET_IDENTITY_PublicKey id_pub;
133  struct GNUNET_IDENTITY_PrivateKey pkey_data_p;
134  struct GNUNET_IDENTITY_PublicKey pkey_data;
135  struct GNUNET_HashCode query;
136  char *rdata;
137  char *conv_lbl;
138  size_t rdata_size;
139  char ztld[128];
140  unsigned char ctr[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
141  unsigned char skey[GNUNET_CRYPTO_AES_KEY_LENGTH];
142 
143  id_priv.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
145  parsehex (d_pkey,
146  (char*) &id_priv.ecdsa_key,
147  sizeof (id_priv.ecdsa_key), 1);
148 
150  &id_pub);
151  printf ("Zone private key (d, big-endian):\n");
152  print_bytes_ (&id_priv.ecdsa_key,
153  sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 8, 1);
154  printf ("\n");
155  printf ("Zone identifier (ztype|zkey):\n");
157  print_bytes (&id_pub, GNUNET_IDENTITY_public_key_get_length (&id_pub), 8);
160  ztld,
161  sizeof (ztld));
162  printf ("\n");
163  printf ("zTLD:\n");
164  printf ("%s\n", ztld);
165  printf ("\n");
166 
167  pkey_data_p.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
169  GNUNET_IDENTITY_key_get_public (&pkey_data_p,
170  &pkey_data);
171  conv_lbl = GNUNET_GNSRECORD_string_normalize (label);
172  printf ("Label:\n");
173  print_bytes (conv_lbl, strlen (conv_lbl), 8);
174  GNUNET_free (conv_lbl);
175  printf ("\nNumber of records (integer): %d\n\n", rd_count);
176 
177  for (int i = 0; i < rd_count; i++)
178  {
179  printf ("Record #%d := (\n", i);
180  print_record (&rd[i]);
181  printf (")\n\n");
182  }
183 
185  rd);
186  rdata = GNUNET_malloc (rdata_size);
188  rd,
189  (size_t) rdata_size,
190  rdata);
191  printf ("RDATA:\n");
192  print_bytes (rdata,
193  (size_t) rdata_size,
194  8);
195  printf ("\n");
199  skey,
200  label,
202  expire).abs_value_us__,
203  &id_pub.ecdsa_key);
204 
205  printf ("Encryption NONCE|EXPIRATION|BLOCK COUNTER:\n");
206  print_bytes (ctr, sizeof (ctr), 8);
207  printf ("\n");
208  printf ("Encryption key (K):\n");
209  print_bytes (skey, sizeof (skey), 8);
210  printf ("\n");
212  label,
213  &query);
214  printf ("Storage key (q):\n");
215  print_bytes (&query, sizeof (query), 8);
216  printf ("\n");
218  expire,
219  label,
220  rd,
221  rd_count,
222  &rrblock));
223  size_t bdata_size = ntohl (rrblock->size) - sizeof (struct
225 
226  bdata = (char*) &(&rrblock->ecdsa_block)[1];
227  printf ("BDATA:\n");
228  print_bytes (bdata, bdata_size, 8);
229  printf ("\n");
230  printf ("RRBLOCK:\n");
231  print_bytes (rrblock, ntohl (rrblock->size), 8);
232  printf ("\n");
233  GNUNET_free (rdata);
234 }
235 
236 
245 static void
246 run_edkey (struct GNUNET_GNSRECORD_Data *rd, int rd_count, const char*label)
247 {
249  struct GNUNET_GNSRECORD_Block *rrblock;
250  char *bdata;
251  struct GNUNET_IDENTITY_PrivateKey id_priv;
252  struct GNUNET_IDENTITY_PublicKey id_pub;
253  struct GNUNET_IDENTITY_PrivateKey pkey_data_p;
254  struct GNUNET_IDENTITY_PublicKey pkey_data;
255  struct GNUNET_HashCode query;
256  char *rdata;
257  char *conv_lbl;
258  size_t rdata_size;
259 
260  char ztld[128];
261  unsigned char nonce[crypto_secretbox_NONCEBYTES];
262  unsigned char skey[crypto_secretbox_KEYBYTES];
263 
264  id_priv.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
267  &id_pub);
268 
269  id_priv.type = htonl (GNUNET_IDENTITY_TYPE_EDDSA);
271  parsehex (d_edkey,
272  (char*) &id_priv.eddsa_key,
273  sizeof (id_priv.eddsa_key), 0);
275  &id_pub);
276  fprintf (stdout,
277  "Zone private key (d):\n");
278  print_bytes (&id_priv.eddsa_key, sizeof (struct
280  printf ("\n");
281  printf ("Zone identifier (ztype|zkey):\n");
283  print_bytes (&id_pub, GNUNET_IDENTITY_public_key_get_length (&id_pub), 8);
286  ztld,
287  sizeof (ztld));
288  printf ("\n");
289  printf ("zTLD:\n");
290  printf ("%s\n", ztld);
291  printf ("\n");
292 
293  pkey_data_p.type = htonl (GNUNET_GNSRECORD_TYPE_EDKEY);
295  GNUNET_IDENTITY_key_get_public (&pkey_data_p,
296  &pkey_data);
297  conv_lbl = GNUNET_GNSRECORD_string_normalize (label);
298  printf ("Label:\n");
299  print_bytes (conv_lbl, strlen (conv_lbl), 8);
300  GNUNET_free (conv_lbl);
301  fprintf (stdout,
302  "\nNumber of records (integer): %d\n\n", rd_count);
303 
304  for (int i = 0; i < rd_count; i++)
305  {
306  printf ("Record #%d := (\n", i);
307  print_record (&rd[i]);
308  printf (")\n\n");
309  }
310 
312  rd);
314  rd,
316  GNUNET_assert (0 < rdata_size);
317  rdata = GNUNET_malloc ((size_t) rdata_size);
319  rd,
320  (size_t) rdata_size,
321  rdata);
322  printf ("RDATA:\n");
323  print_bytes (rdata,
324  (size_t) rdata_size,
325  8);
326  printf ("\n");
328  skey,
329  label,
331  expire).abs_value_us__,
332  &id_pub.eddsa_key);
333  printf ("Encryption NONCE|EXPIRATION:\n");
334  print_bytes (nonce, sizeof (nonce), 8);
335  printf ("\n");
336  printf ("Encryption key (K):\n");
337  print_bytes (skey, sizeof (skey), 8);
338  printf ("\n");
340  label,
341  &query);
342  printf ("Storage key (q):\n");
343  print_bytes (&query, sizeof (query), 8);
344  printf ("\n");
345 
347  expire,
348  label,
349  rd,
350  rd_count,
351  &rrblock));
352  size_t bdata_size = ntohl (rrblock->size) - sizeof (struct
354 
355  bdata = (char*) &(&rrblock->eddsa_block)[1];
356  printf ("BDATA:\n");
357  print_bytes (bdata, bdata_size, 8);
358  printf ("\n");
359  printf ("RRBLOCK:\n");
360  print_bytes (rrblock, ntohl (rrblock->size), 8);
361  printf ("\n");
362  GNUNET_free (rdata);
363 }
364 
365 
374 static void
375 run (void *cls,
376  char *const *args,
377  const char *cfgfile,
378  const struct GNUNET_CONFIGURATION_Handle *cfg)
379 {
380  struct GNUNET_GNSRECORD_Data rd_pkey;
381  struct GNUNET_GNSRECORD_Data rd[3];
382  struct GNUNET_TIME_Absolute exp1;
383  struct GNUNET_TIME_Absolute exp2;
384  struct GNUNET_TIME_Relative exp3;
385  size_t pkey_data_size;
386  size_t ip_data_size;
387  char *pkey_data;
388  char *ip_data;
389 
390  /*
391  * Make different expiration times
392  */
393  GNUNET_STRINGS_fancy_time_to_absolute ("2048-01-23 10:51:34",
394  &exp1);
395  GNUNET_STRINGS_fancy_time_to_absolute ("3540-05-22 07:55:01",
396  &exp2);
398  &exp3);
399 
400 
401  memset (&rd_pkey, 0, sizeof (struct GNUNET_GNSRECORD_Data));
404  "000G0011WESGZY9VRV9NNJ66W3GKNZFZF56BFD2BQF3MHMJST2G2GKDYGG",
405  (void**) &pkey_data,
406  &pkey_data_size));
407  rd_pkey.data = pkey_data;
408  rd_pkey.data_size = pkey_data_size;
409  rd_pkey.expiration_time = exp1.abs_value_us;
414  "::dead:beef",
415  (void**) &ip_data,
416  &ip_data_size));
417 
418  rd[0].data = ip_data;
419  rd[0].data_size = ip_data_size;
420  rd[0].expiration_time = exp1.abs_value_us;
423 
424  rd[1].data = "\u611b\u79f0";
425  rd[1].data_size = strlen (rd[1].data);
426  rd[1].expiration_time = exp2.abs_value_us;
429 
430  rd[2].data = "Hello World";
431  rd[2].data_size = strlen (rd[2].data);
432  rd[2].expiration_time = exp3.rel_value_us;
436 
437  run_pkey (&rd_pkey, 1, "testdelegation");
438  run_pkey (rd, 3, "\u5929\u4e0b\u7121\u6575");
439  run_edkey (&rd_pkey, 1, "testdelegation");
440  run_edkey (rd, 3, "\u5929\u4e0b\u7121\u6575");
441 }
442 
443 
451 int
452 main (int argc,
453  char *const *argv)
454 {
455  const struct GNUNET_GETOPT_CommandLineOption options[] = {
457  };
458 
460  GNUNET_log_setup ("gnunet-gns-tvg",
461  "INFO",
462  NULL));
463  if (GNUNET_OK !=
464  GNUNET_PROGRAM_run (argc, argv,
465  "gnunet-gns-tvg",
466  "Generate test vectors for GNS",
467  options,
468  &run, NULL))
469  return 1;
470  return 0;
471 }
472 
473 
474 /* end of gnunet-gns-tvg.c */
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define GNUNET_GNSRECORD_TYPE_NICK
GNS nick names.
#define GNUNET_GNSRECORD_TYPE_PKEY
WARNING: This header is generated! In order to add GNS record types, you must register them in GANA,...
#define GNUNET_GNSRECORD_TYPE_EDKEY
Record type for EDKEY zone delegations.
void GNR_derive_block_xsalsa_key(unsigned char *nonce, unsigned char *key, const char *label, uint64_t exp, const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Derive session key and iv from label and public key.
void GNR_derive_block_aes_key(unsigned char *ctr, unsigned char *key, const char *label, uint64_t exp, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Derive session key and iv from label and public key.
API for GNS record-related crypto.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static char * line
Desired phone line (string to be converted to a hash).
static char * expire
DID Document expiration Date Attribut String.
Definition: gnunet-did.c:101
static void print_bytes_(void *buf, size_t buf_len, int fold, int in_be)
static int parsehex(char *src, char *dst, size_t dstlen, int invert)
static void print_bytes(void *buf, size_t buf_len, int fold)
static char * d_edkey
static void print_record(const struct GNUNET_GNSRECORD_Data *rd)
static char * d_pkey
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run.
int main(int argc, char *const *argv)
The main function of the test vector generation tool.
static void run_pkey(struct GNUNET_GNSRECORD_Data *rd, int rd_count, const char *label)
Main function that will be run.
static void run_edkey(struct GNUNET_GNSRECORD_Data *rd, int rd_count, const char *label)
Main function that will be run.
uint32_t data
The data value.
static unsigned int rd_count
Number of records for currently parsed set.
static struct GNUNET_GNSRECORD_Data rd[50]
The record data under a single label.
static char buf[2048]
API to the GNS service.
API that can be used to manipulate GNS record data.
Convenience API for writing testcases for GNUnet.
void GNUNET_CRYPTO_eddsa_key_create(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:461
void GNUNET_CRYPTO_ecdsa_key_create(struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:446
#define GNUNET_DNSPARSER_TYPE_AAAA
#define GNUNET_DNSPARSER_TYPE_TXT
char * GNUNET_GNSRECORD_string_normalize(const char *src)
Normalize a UTF-8 string to a GNS name.
@ GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION
This expiration time of the record is a relative time (not an absolute time).
@ GNUNET_GNSRECORD_RF_CRITICAL
This record is critical.
@ GNUNET_GNSRECORD_RF_SUPPLEMENTAL
This is a supplemental record.
@ GNUNET_GNSRECORD_RF_PRIVATE
This is a private record of this peer and it should thus not be published.
@ GNUNET_GNSRECORD_RF_NONE
Entry for no flags / cleared flags.
void GNUNET_GNSRECORD_query_from_public_key(const struct GNUNET_IDENTITY_PublicKey *pub, const char *label, struct GNUNET_HashCode *query)
Calculate the DHT query for a given label in a given zone.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_create(const struct GNUNET_IDENTITY_PrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count, struct GNUNET_GNSRECORD_Block **result)
Sign name and records.
ssize_t GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, size_t dest_size, char *dest)
Serialize the given records to the given destination buffer.
int GNUNET_GNSRECORD_string_to_value(uint32_t type, const char *s, void **data, size_t *data_size)
Convert human-readable version of the value s of a record of type type to the respective binary repre...
Definition: gnsrecord.c:177
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_record_get_expiration_time(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, struct GNUNET_TIME_Absolute min)
Returns the expiration time of the given block of records.
GNUNET_NETWORK_STRUCT_END ssize_t GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Calculate how many bytes we will need to serialize the given records.
enum GNUNET_GenericReturnValue GNUNET_IDENTITY_key_get_public(const struct GNUNET_IDENTITY_PrivateKey *privkey, struct GNUNET_IDENTITY_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: identity_api.c:179
ssize_t GNUNET_IDENTITY_public_key_get_length(const struct GNUNET_IDENTITY_PublicKey *key)
Get the compacted length of a GNUNET_IDENTITY_PublicKey.
Definition: identity_api.c:830
@ GNUNET_IDENTITY_TYPE_EDDSA
EDDSA identity.
#define GNUNET_CRYPTO_AES_KEY_LENGTH
length of the sessionkey in bytes (256 BIT sessionkey)
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37
@ GNUNET_OK
int GNUNET_log_setup(const char *comp, const char *loglevel, const char *logfile)
Setup logging.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:400
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:708
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
Convert a given fancy human-readable time to our internal representation.
Definition: strings.c:260
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:638
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, struct GNUNET_TIME_Absolute *atime)
Convert a given fancy human-readable time to our internal representation.
Definition: strings.c:302
Private ECC key encoded for transmission.
Private ECC key encoded for transmission.
Definition of a command line option.
struct GNUNET_GNSRECORD_EcdsaBlock ecdsa_block
struct GNUNET_GNSRECORD_EddsaBlock eddsa_block
uint32_t size
Size of the block.
uint32_t record_type
Type of the GNS/DNS record.
const void * data
Binary value stored in the DNS record.
size_t data_size
Number of bytes in data.
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
uint64_t expiration_time
Expiration time for the DNS record.
A 512-bit hashcode.
A private key for an identity as per LSD0001.
uint32_t type
Type of public key.
struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_key
An ECDSA identity key.
struct GNUNET_CRYPTO_EddsaPrivateKey eddsa_key
AN EdDSA identtiy key.
An identity key as per LSD0001.
struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_key
An ECDSA identity key.
struct GNUNET_CRYPTO_EddsaPublicKey eddsa_key
AN EdDSA identtiy key.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.