GNUnet  0.10.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  pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
60  label, strlen(label),
61  ctx_key, strlen(ctx_key),
62  NULL, 0);
64  pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
65  label, strlen(label),
66  ctx_iv, strlen(ctx_iv),
67  NULL, 0);
68 }
69 
70 
84  const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
86  const char *label,
87  const struct GNUNET_GNSRECORD_Data *rd,
88  unsigned int rd_count)
89 {
90  ssize_t payload_len = GNUNET_GNSRECORD_records_get_size(rd_count,
91  rd);
92  struct GNUNET_GNSRECORD_Block *block;
93  struct GNUNET_CRYPTO_EcdsaPrivateKey *dkey;
96  struct GNUNET_GNSRECORD_Data rdc[GNUNET_NZL(rd_count)];
97  uint32_t rd_count_nbo;
98  struct GNUNET_TIME_Absolute now;
99 
100  if (payload_len < 0)
101  {
102  GNUNET_break(0);
103  return NULL;
104  }
105  if (payload_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE)
106  {
107  GNUNET_break(0);
108  return NULL;
109  }
110  /* convert relative to absolute times */
111  now = GNUNET_TIME_absolute_get();
112  for (unsigned int i = 0; i < rd_count; i++)
113  {
114  rdc[i] = rd[i];
115  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
116  {
117  struct GNUNET_TIME_Relative t;
118 
119  /* encrypted blocks must never have relative expiration times, convert! */
120  rdc[i].flags &= ~GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
121  t.rel_value_us = rdc[i].expiration_time;
123  }
124  }
125  /* serialize */
126  rd_count_nbo = htonl(rd_count);
127  {
128  char payload[sizeof(uint32_t) + payload_len];
129 
131  &rd_count_nbo,
132  sizeof(uint32_t));
133  GNUNET_assert(payload_len ==
135  rdc,
136  payload_len,
137  &payload[sizeof(uint32_t)]));
138  block = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Block) +
139  sizeof(uint32_t) +
140  payload_len);
141  block->purpose.size = htonl(sizeof(uint32_t) +
142  payload_len +
143  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
144  sizeof(struct GNUNET_TIME_AbsoluteNBO));
147  /* encrypt and sign */
149  label,
150  "gns");
152  &block->derived_key);
154  &skey,
155  label,
156  pkey);
157  GNUNET_break(payload_len + sizeof(uint32_t) ==
159  payload_len + sizeof(uint32_t),
160  &skey,
161  &iv,
162  &block[1]));
163  }
164  if (GNUNET_OK !=
166  &block->purpose,
167  &block->signature))
168  {
169  GNUNET_break(0);
170  GNUNET_free(dkey);
171  GNUNET_free(block);
172  return NULL;
173  }
174  GNUNET_free(dkey);
175  return block;
176 }
177 
178 
189 struct GNUNET_GNSRECORD_Block *
192  const char *label,
193  const struct GNUNET_GNSRECORD_Data *rd,
194  unsigned int rd_count)
195 {
196  struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
197 
199  &pkey);
200  return block_create(key,
201  &pkey,
202  expire,
203  label,
204  rd,
205  rd_count);
206 }
207 
208 
212 struct KeyCacheLine {
217 
222 };
223 
224 
237 struct GNUNET_GNSRECORD_Block *
240  const char *label,
241  const struct GNUNET_GNSRECORD_Data *rd,
242  unsigned int rd_count)
243 {
244 #define CSIZE 64
245  static struct KeyCacheLine cache[CSIZE];
246  struct KeyCacheLine *line;
247 
248  line = &cache[(*(unsigned int *)key) % CSIZE];
249  if (0 != memcmp(&line->key,
250  key,
251  sizeof(*key)))
252  {
253  /* cache miss, recompute */
254  line->key = *key;
256  &line->pkey);
257  }
258 #undef CSIZE
259  return block_create(key,
260  &line->pkey,
261  expire,
262  label,
263  rd,
264  rd_count);
265 }
266 
267 
268 
276 int
278 {
280  &block->purpose,
281  &block->signature,
282  &block->derived_key);
283 }
284 
285 
297 int
299  const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
300  const char *label,
302  void *proc_cls)
303 {
304  size_t payload_len = ntohl(block->purpose.size) -
305  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) -
306  sizeof(struct GNUNET_TIME_AbsoluteNBO);
309 
310  if (ntohl(block->purpose.size) <
311  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) +
312  sizeof(struct GNUNET_TIME_AbsoluteNBO))
313  {
314  GNUNET_break_op(0);
315  return GNUNET_SYSERR;
316  }
318  &skey,
319  label,
320  zone_key);
321  {
322  char payload[payload_len];
323  uint32_t rd_count;
324 
325  GNUNET_break(payload_len ==
326  GNUNET_CRYPTO_symmetric_decrypt(&block[1], payload_len,
327  &skey, &iv,
328  payload));
329  GNUNET_memcpy(&rd_count,
330  payload,
331  sizeof(uint32_t));
332  rd_count = ntohl(rd_count);
333  if (rd_count > 2048)
334  {
335  /* limit to sane value */
336  GNUNET_break_op(0);
337  return GNUNET_SYSERR;
338  }
339  {
340  struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL(rd_count)];
341  unsigned int j;
342  struct GNUNET_TIME_Absolute now;
343 
344  if (GNUNET_OK !=
345  GNUNET_GNSRECORD_records_deserialize(payload_len - sizeof(uint32_t),
346  &payload[sizeof(uint32_t)],
347  rd_count,
348  rd))
349  {
350  GNUNET_break_op(0);
351  return GNUNET_SYSERR;
352  }
353  /* hide expired records */
354  now = GNUNET_TIME_absolute_get();
355  j = 0;
356  for (unsigned int i = 0; i < rd_count; i++)
357  {
358  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION))
359  {
360  /* encrypted blocks must never have relative expiration times, skip! */
361  GNUNET_break_op(0);
362  continue;
363  }
364 
365  if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD))
366  {
367  int include_record = GNUNET_YES;
368  /* Shadow record, figure out if we have a not expired active record */
369  for (unsigned int k = 0; k < rd_count; k++)
370  {
371  if (k == i)
372  continue;
373  if (rd[i].expiration_time < now.abs_value_us)
374  include_record = GNUNET_NO; /* Shadow record is expired */
375  if ((rd[k].record_type == rd[i].record_type) &&
376  (rd[k].expiration_time >= now.abs_value_us) &&
377  (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD)))
378  {
379  include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */
381  "Ignoring shadow record\n");
382  break;
383  }
384  }
385  if (GNUNET_YES == include_record)
386  {
387  rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW_RECORD; /* Remove Flag */
388  if (j != i)
389  rd[j] = rd[i];
390  j++;
391  }
392  }
393  else if (rd[i].expiration_time >= now.abs_value_us)
394  {
395  /* Include this record */
396  if (j != i)
397  rd[j] = rd[i];
398  j++;
399  }
400  else
401  {
402  struct GNUNET_TIME_Absolute at;
403 
404  at.abs_value_us = rd[i].expiration_time;
406  "Excluding record that expired %s (%llu ago)\n",
408  (unsigned long long)rd[i].expiration_time - now.abs_value_us);
409  }
410  }
411  rd_count = j;
412  if (NULL != proc)
413  proc(proc_cls,
414  rd_count,
415  (0 != rd_count) ? rd : NULL);
416  }
417  }
418  return GNUNET_OK;
419 }
420 
421 
429 void
431  const char *label,
432  struct GNUNET_HashCode *query)
433 {
434  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
435 
437  &pub);
439  label,
440  query);
441 }
442 
443 
451 void
453  const char *label,
454  struct GNUNET_HashCode *query)
455 {
457 
459  label,
460  "gns",
461  &pd);
462  GNUNET_CRYPTO_hash(&pd,
463  sizeof(pd),
464  query);
465 }
466 
467 
468 /* 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:1340
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.
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:1048
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.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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_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:393
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 ...
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:931
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:44
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.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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:241
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.
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.
#define GNUNET_YES
Definition: gnunet_common.h:77
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 flag is currently unused; former RF_PENDING flag.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition: strings.c:741
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:1296
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:655
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:91
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.