GNUnet  0.11.x
gnsrecord_crypto.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2013, 2018 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 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_gnsrecord_lib.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_tun_lib.h"
36 
37 
38 #define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__)
39 
40 
49 static void
52  const char *label,
53  const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
54 {
55  static const char ctx_key[] = "gns-aes-ctx-key";
56  static const char ctx_iv[] = "gns-aes-ctx-iv";
57 
59  ctx_key, strlen (ctx_key),
60  pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
61  label, strlen (label),
62  NULL, 0);
63  GNUNET_CRYPTO_kdf (iv, sizeof(struct
65  ctx_iv, strlen (ctx_iv),
66  pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
67  label, strlen (label),
68  NULL, 0);
69 }
70 
71 
83 static struct GNUNET_GNSRECORD_Block *
85  const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
87  const char *label,
88  const struct GNUNET_GNSRECORD_Data *rd,
89  unsigned int rd_count)
90 {
91  ssize_t payload_len = GNUNET_GNSRECORD_records_get_size (rd_count,
92  rd);
93  struct GNUNET_GNSRECORD_Block *block;
94  struct GNUNET_CRYPTO_EcdsaPrivateKey *dkey;
97  struct GNUNET_GNSRECORD_Data rdc[GNUNET_NZL (rd_count)];
98  uint32_t rd_count_nbo;
99  struct GNUNET_TIME_Absolute now;
100 
101  if (payload_len < 0)
102  {
103  GNUNET_break (0);
104  return NULL;
105  }
106  if (payload_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE)
107  {
108  GNUNET_break (0);
109  return NULL;
110  }
111  /* convert relative to absolute times */
112  now = GNUNET_TIME_absolute_get ();
113  for (unsigned int i = 0; i < rd_count; i++)
114  {
115  rdc[i] = rd[i];
116  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
117  {
118  struct GNUNET_TIME_Relative t;
119 
120  /* encrypted blocks must never have relative expiration times, convert! */
121  rdc[i].flags &= ~GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
122  t.rel_value_us = rdc[i].expiration_time;
124  }
125  }
126  /* serialize */
127  rd_count_nbo = htonl (rd_count);
128  {
129  char payload[sizeof(uint32_t) + payload_len];
130 
132  &rd_count_nbo,
133  sizeof(uint32_t));
134  GNUNET_assert (payload_len ==
136  rdc,
137  payload_len,
138  &payload[sizeof(uint32_t)
139  ]));
140  block = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Block)
141  + sizeof(uint32_t)
142  + payload_len);
143  block->purpose.size = htonl (sizeof(uint32_t)
144  + payload_len
145  + sizeof(struct
147  + sizeof(struct GNUNET_TIME_AbsoluteNBO));
149  block->expiration_time = GNUNET_TIME_absolute_hton (expire);
150  /* encrypt and sign */
152  label,
153  "gns");
155  &block->derived_key);
157  &skey,
158  label,
159  pkey);
160  GNUNET_break (payload_len + sizeof(uint32_t) ==
162  payload_len
163  + sizeof(uint32_t),
164  &skey,
165  &iv,
166  &block[1]));
167  }
168  if (GNUNET_OK !=
170  &block->purpose,
171  &block->signature))
172  {
173  GNUNET_break (0);
174  GNUNET_free (dkey);
175  GNUNET_free (block);
176  return NULL;
177  }
178  GNUNET_free (dkey);
179  return block;
180 }
181 
182 
193 struct GNUNET_GNSRECORD_Block *
196  const char *label,
197  const struct GNUNET_GNSRECORD_Data *rd,
198  unsigned int rd_count)
199 {
200  struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
201 
203  &pkey);
204  return block_create (key,
205  &pkey,
206  expire,
207  label,
208  rd,
209  rd_count);
210 }
211 
212 
217 {
222 
227 };
228 
229 
242 struct GNUNET_GNSRECORD_Block *
245  const char *label,
246  const struct GNUNET_GNSRECORD_Data *rd,
247  unsigned int rd_count)
248 {
249 #define CSIZE 64
250  static struct KeyCacheLine cache[CSIZE];
251  struct KeyCacheLine *line;
252 
253  line = &cache[(*(unsigned int *) key) % CSIZE];
254  if (0 != memcmp (&line->key,
255  key,
256  sizeof(*key)))
257  {
258  /* cache miss, recompute */
259  line->key = *key;
261  &line->pkey);
262  }
263 #undef CSIZE
264  return block_create (key,
265  &line->pkey,
266  expire,
267  label,
268  rd,
269  rd_count);
270 }
271 
272 
280 int
282 {
284  &block->purpose,
285  &block->signature,
286  &block->derived_key);
287 }
288 
289 
301 int
303  const struct
305  const char *label,
307  void *proc_cls)
308 {
309  size_t payload_len = ntohl (block->purpose.size)
310  - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
311  - sizeof(struct GNUNET_TIME_AbsoluteNBO);
314 
315  if (ntohl (block->purpose.size) <
316  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
317  + sizeof(struct GNUNET_TIME_AbsoluteNBO))
318  {
319  GNUNET_break_op (0);
320  return GNUNET_SYSERR;
321  }
323  &skey,
324  label,
325  zone_key);
326  {
327  char payload[payload_len];
328  uint32_t rd_count;
329 
330  GNUNET_break (payload_len ==
331  GNUNET_CRYPTO_symmetric_decrypt (&block[1], payload_len,
332  &skey, &iv,
333  payload));
334  GNUNET_memcpy (&rd_count,
335  payload,
336  sizeof(uint32_t));
337  rd_count = ntohl (rd_count);
338  if (rd_count > 2048)
339  {
340  /* limit to sane value */
341  GNUNET_break_op (0);
342  return GNUNET_SYSERR;
343  }
344  {
345  struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL (rd_count)];
346  unsigned int j;
347  struct GNUNET_TIME_Absolute now;
348 
349  if (GNUNET_OK !=
350  GNUNET_GNSRECORD_records_deserialize (payload_len - sizeof(uint32_t),
351  &payload[sizeof(uint32_t)],
352  rd_count,
353  rd))
354  {
355  GNUNET_break_op (0);
356  return GNUNET_SYSERR;
357  }
358  /* hide expired records */
359  now = GNUNET_TIME_absolute_get ();
360  j = 0;
361  for (unsigned int i = 0; i < rd_count; i++)
362  {
363  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
364  {
365  /* encrypted blocks must never have relative expiration times, skip! */
366  GNUNET_break_op (0);
367  continue;
368  }
369 
370  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD))
371  {
372  int include_record = GNUNET_YES;
373  /* Shadow record, figure out if we have a not expired active record */
374  for (unsigned int k = 0; k < rd_count; k++)
375  {
376  if (k == i)
377  continue;
378  if (rd[i].expiration_time < now.abs_value_us)
379  include_record = GNUNET_NO; /* Shadow record is expired */
380  if ((rd[k].record_type == rd[i].record_type) &&
381  (rd[k].expiration_time >= now.abs_value_us) &&
382  (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
383  {
384  include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */
386  "Ignoring shadow record\n");
387  break;
388  }
389  }
390  if (GNUNET_YES == include_record)
391  {
392  rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW_RECORD; /* Remove Flag */
393  if (j != i)
394  rd[j] = rd[i];
395  j++;
396  }
397  }
398  else if (rd[i].expiration_time >= now.abs_value_us)
399  {
400  /* Include this record */
401  if (j != i)
402  rd[j] = rd[i];
403  j++;
404  }
405  else
406  {
407  struct GNUNET_TIME_Absolute at;
408 
409  at.abs_value_us = rd[i].expiration_time;
411  "Excluding record that expired %s (%llu ago)\n",
413  (unsigned long long) rd[i].expiration_time
414  - now.abs_value_us);
415  }
416  }
417  rd_count = j;
418  if (NULL != proc)
419  proc (proc_cls,
420  rd_count,
421  (0 != rd_count) ? rd : NULL);
422  }
423  }
424  return GNUNET_OK;
425 }
426 
427 
435 void
438  const char *label,
439  struct GNUNET_HashCode *query)
440 {
441  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
442 
444  &pub);
446  label,
447  query);
448 }
449 
450 
458 void
461  const char *label,
462  struct GNUNET_HashCode *query)
463 {
466  label,
467  "gns",
468  &pd);
469  GNUNET_CRYPTO_hash (&pd,
470  sizeof(pd),
471  query);
472 }
473 
474 
475 /* end of gnsrecord_crypto.c */
GNUNET_NETWORK_STRUCT_END typedef void(* GNUNET_GNSRECORD_RecordCallback)(void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Process a records that were decrypted from a block.
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
This record should not be used unless all (other) records with an absolute expiration time have expir...
void GNUNET_CRYPTO_ecdsa_public_key_derive(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label, const char *context, struct GNUNET_CRYPTO_EcdsaPublicKey *result)
Derive a public key from a given public key and a label.
Definition: crypto_ecc.c:963
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.
uint64_t rel_value_us
The actual value.
static char * pkey
Public key of the zone to look in, in ASCII.
struct GNUNET_CRYPTO_EcdsaPublicKey pkey
Associated public key.
static struct GNUNET_GNSRECORD_Block * block_create(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Sign name and records.
struct GNUNET_GNSRECORD_Block * GNUNET_GNSRECORD_block_create(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Sign name and records.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
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.
static void expire(void *cls)
Expire a PooledConnection object.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_GNSRECORD_Block * GNUNET_GNSRECORD_block_create2(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key, struct GNUNET_TIME_Absolute expire, const char *label, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count)
Sign name and records, cache derived public key (also keeps the private key in static memory...
#define GNUNET_GNSRECORD_MAX_BLOCK_SIZE
Maximum size of a value that can be stored in a GNS block.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:395
Private ECC key encoded for transmission.
int GNUNET_GNSRECORD_records_deserialize(size_t len, const char *src, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *dest)
Deserialize the given records to the given destination.
Information we have in an encrypted block with record data (i.e.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
uint64_t abs_value_us
The actual value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char * zone
Name of the zone we manage.
void GNUNET_GNSRECORD_query_from_private_key(const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, struct GNUNET_HashCode *query)
Calculate the DHT query for a given label in a given zone.
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
static char * line
Desired phone line (string to be converted to a hash).
struct GNUNET_CRYPTO_EcdsaPrivateKey key
A private key.
#define CSIZE
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
uint64_t expiration_time
Expiration time for the DNS record.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
A 512-bit hashcode.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
int GNUNET_GNSRECORD_block_verify(const struct GNUNET_GNSRECORD_Block *block)
Check if a signature is valid.
struct GNUNET_HashCode key
The key used in the DHT.
void GNUNET_CRYPTO_ecdsa_key_get_public(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:176
int GNUNET_CRYPTO_ecdsa_sign_(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition: crypto_ecc.c:645
static unsigned long long payload
How much data are we currently storing in the database?
static void derive_block_aes_key(struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, const char *label, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Derive session key and iv from label and public key.
int GNUNET_CRYPTO_ecdsa_verify_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const struct GNUNET_CRYPTO_EcdsaSignature *sig, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Verify ECDSA signature.
Definition: crypto_ecc.c:745
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature of the block.
static struct GNUNET_CRYPTO_EddsaPublicKey pub
Definition: gnunet-scrypt.c:39
int GNUNET_GNSRECORD_block_decrypt(const struct GNUNET_GNSRECORD_Block *block, const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, const char *label, GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls)
Decrypt block.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
#define GNUNET_log(kind,...)
#define GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN
Signature of a gnunet naming system record block.
Time for absolute times used by GNUnet, in microseconds.
void GNUNET_GNSRECORD_query_from_public_key(const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label, struct GNUNET_HashCode *query)
Calculate the DHT query for a given label in a given zone.
Line in cache mapping private keys to public keys.
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
This expiration time of the record is a relative time (not an absolute time).
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:756
struct GNUNET_CRYPTO_EcdsaPrivateKey * GNUNET_CRYPTO_ecdsa_private_key_derive(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const char *label, const char *context)
Derive a private key from a given private key and a label.
Definition: crypto_ecc.c:911
ssize_t GNUNET_CRYPTO_symmetric_decrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Decrypt a given block using a symmetric sessionkey.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:657
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:89
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.