From 9ef4abad615bea12d13be542b8ae5fbeb2dfee32 Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Wed, 18 Oct 2023 13:37:38 +0200 Subject: NEWS: Refactoring components under src/ into lib/, plugin/, cli/ and service/ This also includes a necessary API refactoring of crypto from IDENTITY to UTIL. --- src/gnsrecord/.gitignore | 6 - src/gnsrecord/Makefile.am | 129 --- src/gnsrecord/gnsrecord.c | 267 ------ src/gnsrecord/gnsrecord_crypto.c | 1091 ----------------------- src/gnsrecord/gnsrecord_crypto.h | 85 -- src/gnsrecord/gnsrecord_misc.c | 567 ------------ src/gnsrecord/gnsrecord_serialization.c | 302 ------- src/gnsrecord/gnunet-gnsrecord-tvg.c | 539 ----------- src/gnsrecord/json_gnsrecord.c | 389 -------- src/gnsrecord/meson.build | 45 - src/gnsrecord/perf_gnsrecord_crypto.c | 139 --- src/gnsrecord/plugin_gnsrecord_dns.c | 818 ----------------- src/gnsrecord/test_gnsrecord_block_expiration.c | 113 --- src/gnsrecord/test_gnsrecord_crypto.c | 207 ----- src/gnsrecord/test_gnsrecord_serialization.c | 156 ---- src/gnsrecord/test_gnsrecord_testvectors.c | 711 --------------- 16 files changed, 5564 deletions(-) delete mode 100644 src/gnsrecord/.gitignore delete mode 100644 src/gnsrecord/Makefile.am delete mode 100644 src/gnsrecord/gnsrecord.c delete mode 100644 src/gnsrecord/gnsrecord_crypto.c delete mode 100644 src/gnsrecord/gnsrecord_crypto.h delete mode 100644 src/gnsrecord/gnsrecord_misc.c delete mode 100644 src/gnsrecord/gnsrecord_serialization.c delete mode 100644 src/gnsrecord/gnunet-gnsrecord-tvg.c delete mode 100644 src/gnsrecord/json_gnsrecord.c delete mode 100644 src/gnsrecord/meson.build delete mode 100644 src/gnsrecord/perf_gnsrecord_crypto.c delete mode 100644 src/gnsrecord/plugin_gnsrecord_dns.c delete mode 100644 src/gnsrecord/test_gnsrecord_block_expiration.c delete mode 100644 src/gnsrecord/test_gnsrecord_crypto.c delete mode 100644 src/gnsrecord/test_gnsrecord_serialization.c delete mode 100644 src/gnsrecord/test_gnsrecord_testvectors.c (limited to 'src/gnsrecord') diff --git a/src/gnsrecord/.gitignore b/src/gnsrecord/.gitignore deleted file mode 100644 index dca3bd309..000000000 --- a/src/gnsrecord/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -test_gnsrecord_block_expiration -test_gnsrecord_crypto -test_gnsrecord_serialization -zonefiles -perf_gnsrecord_crypto -gnunet-gnsrecord-tvg diff --git a/src/gnsrecord/Makefile.am b/src/gnsrecord/Makefile.am deleted file mode 100644 index 7ef7c327e..000000000 --- a/src/gnsrecord/Makefile.am +++ /dev/null @@ -1,129 +0,0 @@ -# This Makefile.am is in the public domain -AM_CPPFLAGS = -I$(top_srcdir)/src/include ${MHD_CFLAGS} - -plugindir = $(libdir)/gnunet - -pkgcfgdir= $(pkgdatadir)/config.d/ - -libexecdir= $(pkglibdir)/libexec/ - -if USE_COVERAGE - AM_CFLAGS = --coverage -O0 - XLIBS = -lgcov -endif - -noinst_PROGRAMS = \ - gnunet-gnsrecord-tvg - - -check_PROGRAMS = \ - test_gnsrecord_crypto \ - test_gnsrecord_serialization \ - test_gnsrecord_lsd0001testvectors \ - test_gnsrecord_block_expiration \ - perf_gnsrecord_crypto - -if ENABLE_TEST_RUN -AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; -TESTS = \ - $(check_PROGRAMS) \ - $(check_SCRIPTS) -endif - -lib_LTLIBRARIES = \ - libgnunetgnsrecord.la \ - libgnunetgnsrecordjson.la - -gnunet_gnsrecord_tvg_SOURCES = \ - gnunet-gnsrecord-tvg.c \ - gnsrecord_crypto.h -gnunet_gnsrecord_tvg_LDADD = \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/identity/libgnunetidentity.la \ - libgnunetgnsrecord.la \ - $(GN_LIBINTL) - - -libgnunetgnsrecord_la_SOURCES = \ - gnsrecord.c \ - gnsrecord_serialization.c \ - gnsrecord_crypto.c \ - gnsrecord_misc.c -libgnunetgnsrecord_la_LIBADD = \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/identity/libgnunetidentity.la \ - $(LIBGCRYPT_LIBS) \ - -lsodium \ - $(GN_LIBINTL) -libgnunetgnsrecord_la_LDFLAGS = \ - $(GN_LIB_LDFLAGS) \ - -version-info 0:0:0 - -libgnunetgnsrecordjson_la_SOURCES = \ - json_gnsrecord.c -libgnunetgnsrecordjson_la_LIBADD = \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(top_builddir)/src/identity/libgnunetidentity.la \ - libgnunetgnsrecord.la \ - -ljansson \ - $(GN_LIBINTL) -libgnunetgnsrecordjson_la_LDFLAGS = \ - $(GN_LIB_LDFLAGS) \ - -version-info 0:0:0 - -plugin_LTLIBRARIES = \ - libgnunet_plugin_gnsrecord_dns.la - - -libgnunet_plugin_gnsrecord_dns_la_SOURCES = \ - plugin_gnsrecord_dns.c -libgnunet_plugin_gnsrecord_dns_la_LIBADD = \ - $(top_builddir)/src/util/libgnunetutil.la \ - $(LTLIBINTL) -libgnunet_plugin_gnsrecord_dns_la_LDFLAGS = \ - $(GN_PLUGIN_LDFLAGS) - - -EXTRA_DIST = \ - $(check_SCRIPTS) - -test_gnsrecord_lsd0001testvectors_SOURCES = \ - test_gnsrecord_testvectors.c -test_gnsrecord_lsd0001testvectors_LDADD = \ - $(top_builddir)/src/testing/libgnunettesting.la \ - $(top_builddir)/src/identity/libgnunetidentity.la \ - libgnunetgnsrecord.la \ - $(top_builddir)/src/util/libgnunetutil.la - - -test_gnsrecord_serialization_SOURCES = \ - test_gnsrecord_serialization.c -test_gnsrecord_serialization_LDADD = \ - $(top_builddir)/src/testing/libgnunettesting.la \ - libgnunetgnsrecord.la \ - $(top_builddir)/src/util/libgnunetutil.la - -test_gnsrecord_block_expiration_SOURCES = \ - test_gnsrecord_block_expiration.c -test_gnsrecord_block_expiration_LDADD = \ - $(top_builddir)/src/testing/libgnunettesting.la \ - libgnunetgnsrecord.la \ - $(top_builddir)/src/util/libgnunetutil.la - - -test_gnsrecord_crypto_SOURCES = \ - test_gnsrecord_crypto.c -test_gnsrecord_crypto_LDADD = \ - $(top_builddir)/src/testing/libgnunettesting.la \ - $(top_builddir)/src/identity/libgnunetidentity.la \ - libgnunetgnsrecord.la \ - $(top_builddir)/src/util/libgnunetutil.la - - -perf_gnsrecord_crypto_SOURCES = \ - perf_gnsrecord_crypto.c -perf_gnsrecord_crypto_LDADD = \ - $(top_builddir)/src/testing/libgnunettesting.la \ - $(top_builddir)/src/identity/libgnunetidentity.la \ - libgnunetgnsrecord.la \ - $(top_builddir)/src/util/libgnunetutil.la diff --git a/src/gnsrecord/gnsrecord.c b/src/gnsrecord/gnsrecord.c deleted file mode 100644 index c71dc1708..000000000 --- a/src/gnsrecord/gnsrecord.c +++ /dev/null @@ -1,267 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2009-2013 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file gnsrecord/gnsrecord.c - * @brief API to access GNS record data - * @author Martin Schanzenbach - * @author Matthias Wachs - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_constants.h" -#include "gnunet_gnsrecord_lib.h" -#include "gnunet_gnsrecord_plugin.h" - -#define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__) - - -/** - * Handle for a plugin. - */ -struct Plugin -{ - /** - * Name of the shared library. - */ - char *library_name; - - /** - * Plugin API. - */ - struct GNUNET_GNSRECORD_PluginFunctions *api; -}; - - -/** - * Array of our plugins. - */ -static struct Plugin **gns_plugins; - -/** - * Size of the 'plugins' array. - */ -static unsigned int num_plugins; - -/** - * Global to mark if we've run the initialization. - */ -static int once; - - -/** - * Add a plugin to the list managed by the block library. - * - * @param cls NULL - * @param library_name name of the plugin - * @param lib_ret the plugin API - */ -static void -add_plugin (void *cls, - const char *library_name, - void *lib_ret) -{ - struct GNUNET_GNSRECORD_PluginFunctions *api = lib_ret; - struct Plugin *plugin; - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Loading block plugin `%s'\n", - library_name); - plugin = GNUNET_new (struct Plugin); - plugin->api = api; - plugin->library_name = GNUNET_strdup (library_name); - GNUNET_array_append (gns_plugins, num_plugins, plugin); -} - - -/** - * Loads all plugins (lazy initialization). - */ -static void -init () -{ - if (1 == once) - return; - once = 1; - - GNUNET_PLUGIN_load_all_in_context (GNUNET_OS_project_data_default (), - "libgnunet_plugin_gnsrecord_", - NULL, - &add_plugin, - NULL); -} - - -/** - * Dual function to #init(). - */ -void __attribute__ ((destructor)) -GNSRECORD_fini () -{ - struct Plugin *plugin; - const struct GNUNET_OS_ProjectData *pd = GNUNET_OS_project_data_get (); - const struct GNUNET_OS_ProjectData *dpd = GNUNET_OS_project_data_default (); - - if (pd != dpd) - GNUNET_OS_init (dpd); - - for (unsigned int i = 0; i < num_plugins; i++) - { - plugin = gns_plugins[i]; - GNUNET_break (NULL == - GNUNET_PLUGIN_unload (plugin->library_name, - plugin->api)); - GNUNET_free (plugin->library_name); - GNUNET_free (plugin); - } - GNUNET_free (gns_plugins); - - if (pd != dpd) - GNUNET_OS_init (pd); - - gns_plugins = NULL; - once = 0; - num_plugins = 0; -} - - -/** - * Convert the 'value' of a record to a string. - * - * @param type type of the record - * @param data value in binary encoding - * @param data_size number of bytes in @a data - * @return NULL on error, otherwise human-readable representation of the value - */ -char * -GNUNET_GNSRECORD_value_to_string (uint32_t type, - const void *data, - size_t data_size) -{ - struct Plugin *plugin; - char *ret; - - init (); - for (unsigned int i = 0; i < num_plugins; i++) - { - plugin = gns_plugins[i]; - if (NULL != (ret = plugin->api->value_to_string (plugin->api->cls, - type, - data, - data_size))) - return ret; - } - return NULL; -} - - -int -GNUNET_GNSRECORD_string_to_value (uint32_t type, - const char *s, - void **data, - size_t *data_size) -{ - struct Plugin *plugin; - - init (); - for (unsigned int i = 0; i < num_plugins; i++) - { - plugin = gns_plugins[i]; - if (GNUNET_OK == plugin->api->string_to_value (plugin->api->cls, - type, - s, - data, - data_size)) - return GNUNET_OK; - } - return GNUNET_SYSERR; -} - - -uint32_t -GNUNET_GNSRECORD_typename_to_number (const char *dns_typename) -{ - struct Plugin *plugin; - uint32_t ret; - - if (0 == strcasecmp (dns_typename, - "ANY")) - return GNUNET_GNSRECORD_TYPE_ANY; - init (); - for (unsigned int i = 0; i < num_plugins; i++) - { - plugin = gns_plugins[i]; - if (UINT32_MAX != (ret = plugin->api->typename_to_number (plugin->api->cls, - dns_typename))) - return ret; - } - return UINT32_MAX; -} - - -/** - * Convert a type number to the corresponding type string (e.g. 1 to "A") - * - * @param type number of a type to convert - * @return corresponding typestring, NULL on error - */ -const char * -GNUNET_GNSRECORD_number_to_typename (uint32_t type) -{ - struct Plugin *plugin; - const char *ret; - - if (GNUNET_GNSRECORD_TYPE_ANY == type) - return "ANY"; - init (); - for (unsigned int i = 0; i < num_plugins; i++) - { - plugin = gns_plugins[i]; - if (NULL != (ret = plugin->api->number_to_typename (plugin->api->cls, - type))) - return ret; - } - return NULL; -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_is_critical (uint32_t type) -{ - struct Plugin *plugin; - - if (GNUNET_GNSRECORD_TYPE_ANY == type) - return GNUNET_NO; - init (); - for (unsigned int i = 0; i < num_plugins; i++) - { - plugin = gns_plugins[i]; - if (NULL == plugin->api->is_critical) - continue; - if (GNUNET_NO == plugin->api->is_critical (plugin->api->cls, type)) - continue; - return GNUNET_YES; - } - return GNUNET_NO; -} - - -/* end of gnsrecord.c */ diff --git a/src/gnsrecord/gnsrecord_crypto.c b/src/gnsrecord/gnsrecord_crypto.c deleted file mode 100644 index 384336c97..000000000 --- a/src/gnsrecord/gnsrecord_crypto.c +++ /dev/null @@ -1,1091 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2009-2013, 2018 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file gnsrecord/gnsrecord_crypto.c - * @brief API for GNS record-related crypto - * @author Martin Schanzenbach - * @author Matthias Wachs - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnsrecord_crypto.h" - -#define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__) - -ssize_t -ecdsa_symmetric_decrypt ( - const void *block, - size_t size, - const unsigned char *key, - const unsigned char *ctr, - void *result) -{ - gcry_cipher_hd_t handle; - int rc; - - GNUNET_assert (0 == gcry_cipher_open (&handle, GCRY_CIPHER_AES256, - GCRY_CIPHER_MODE_CTR, 0)); - rc = gcry_cipher_setkey (handle, - key, - GNUNET_CRYPTO_AES_KEY_LENGTH); - GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); - rc = gcry_cipher_setctr (handle, - ctr, - GNUNET_CRYPTO_AES_KEY_LENGTH / 2); - GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); - GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size)); - gcry_cipher_close (handle); - return size; -} - - -ssize_t -ecdsa_symmetric_encrypt ( - const void *block, - size_t size, - const unsigned char *key, - const unsigned char *ctr, - void *result) -{ - gcry_cipher_hd_t handle; - int rc; - - GNUNET_assert (0 == gcry_cipher_open (&handle, GCRY_CIPHER_AES256, - GCRY_CIPHER_MODE_CTR, 0)); - rc = gcry_cipher_setkey (handle, - key, - GNUNET_CRYPTO_AES_KEY_LENGTH); - GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); - rc = gcry_cipher_setctr (handle, - ctr, - GNUNET_CRYPTO_AES_KEY_LENGTH / 2); - GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); - GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, size, block, size)); - gcry_cipher_close (handle); - return size; -} - - -enum GNUNET_GenericReturnValue -eddsa_symmetric_decrypt ( - const void *block, - size_t size, - const unsigned char *key, - const unsigned char *nonce, - void *result) -{ - ssize_t ctlen = size - crypto_secretbox_MACBYTES; - if (ctlen < 0) - return GNUNET_SYSERR; - if (0 != crypto_secretbox_open_detached (result, - ((unsigned char*) block) - + crypto_secretbox_MACBYTES, // Ciphertext - block, // Tag - ctlen, - nonce, key)) - { - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -eddsa_symmetric_encrypt ( - const void *block, - size_t size, - const unsigned char *key, - const unsigned char *nonce, - void *result) -{ - if (size > crypto_secretbox_MESSAGEBYTES_MAX) - return GNUNET_SYSERR; - crypto_secretbox_detached (result + crypto_secretbox_MACBYTES, // Ciphertext - result, // TAG - block, size, nonce, key); - return GNUNET_OK; -} - - -void -GNR_derive_block_aes_key (unsigned char *ctr, - unsigned char *key, - const char *label, - uint64_t exp, - const struct GNUNET_CRYPTO_EcdsaPublicKey *pub) -{ - static const char ctx_key[] = "gns-aes-ctx-key"; - static const char ctx_iv[] = "gns-aes-ctx-iv"; - - GNUNET_CRYPTO_kdf (key, GNUNET_CRYPTO_AES_KEY_LENGTH, - ctx_key, strlen (ctx_key), - pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey), - label, strlen (label), - NULL, 0); - memset (ctr, 0, GNUNET_CRYPTO_AES_KEY_LENGTH / 2); - /** 4 byte nonce **/ - GNUNET_CRYPTO_kdf (ctr, 4, - ctx_iv, strlen (ctx_iv), - pub, sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey), - label, strlen (label), - NULL, 0); - /** Expiration time 64 bit. **/ - memcpy (ctr + 4, &exp, sizeof (exp)); - /** Set counter part to 1 **/ - ctr[15] |= 0x01; -} - - -void -GNR_derive_block_xsalsa_key (unsigned char *nonce, - unsigned char *key, - const char *label, - uint64_t exp, - const struct GNUNET_CRYPTO_EddsaPublicKey *pub) -{ - static const char ctx_key[] = "gns-xsalsa-ctx-key"; - static const char ctx_iv[] = "gns-xsalsa-ctx-iv"; - - GNUNET_CRYPTO_kdf (key, crypto_secretbox_KEYBYTES, - ctx_key, strlen (ctx_key), - pub, sizeof(struct GNUNET_CRYPTO_EddsaPublicKey), - label, strlen (label), - NULL, 0); - memset (nonce, 0, crypto_secretbox_NONCEBYTES); - /** 16 byte nonce **/ - GNUNET_CRYPTO_kdf (nonce, (crypto_secretbox_NONCEBYTES - sizeof (exp)), - ctx_iv, strlen (ctx_iv), - pub, sizeof(struct GNUNET_CRYPTO_EddsaPublicKey), - label, strlen (label), - NULL, 0); - /** Expiration time 64 bit. **/ - memcpy (nonce + (crypto_secretbox_NONCEBYTES - sizeof (exp)), - &exp, sizeof (exp)); -} - - -static ssize_t -block_get_size_ecdsa (const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count) -{ - ssize_t len; - - len = GNUNET_GNSRECORD_records_get_size (rd_count, rd); - if (len < 0) - return -1; - len += sizeof(struct GNUNET_GNSRECORD_Block); - return len; -} - - -enum GNUNET_GenericReturnValue -block_sign_ecdsa (const struct - GNUNET_CRYPTO_EcdsaPrivateKey *key, - const struct - GNUNET_CRYPTO_EcdsaPublicKey *pkey, - const char *label, - struct GNUNET_GNSRECORD_Block *block) -{ - struct GNRBlockPS *gnr_block; - struct GNUNET_GNSRECORD_EcdsaBlock *ecblock; - size_t size = ntohl (block->size) - sizeof (*block) + sizeof (*gnr_block); - - gnr_block = GNUNET_malloc (size); - ecblock = &(block)->ecdsa_block; - gnr_block->purpose.size = htonl (size); - gnr_block->purpose.purpose = - htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN); - gnr_block->expiration_time = ecblock->expiration_time; - /* encrypt and sign */ - GNUNET_memcpy (&gnr_block[1], &ecblock[1], - size - sizeof (*gnr_block)); - GNUNET_CRYPTO_ecdsa_public_key_derive (pkey, - label, - "gns", - &ecblock->derived_key); - if (GNUNET_OK != - GNUNET_CRYPTO_ecdsa_sign_derived (key, - label, - "gns", - &gnr_block->purpose, - &ecblock->signature)) - { - GNUNET_break (0); - GNUNET_free (gnr_block); - return GNUNET_SYSERR; - } - GNUNET_free (gnr_block); - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -block_sign_eddsa (const struct - GNUNET_CRYPTO_EddsaPrivateKey *key, - const struct - GNUNET_CRYPTO_EddsaPublicKey *pkey, - const char *label, - struct GNUNET_GNSRECORD_Block *block) -{ - struct GNRBlockPS *gnr_block; - struct GNUNET_GNSRECORD_EddsaBlock *edblock; - size_t size = ntohl (block->size) - sizeof (*block) + sizeof (*gnr_block); - gnr_block = GNUNET_malloc (size); - edblock = &(block)->eddsa_block; - gnr_block->purpose.size = htonl (size); - gnr_block->purpose.purpose = - htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN); - gnr_block->expiration_time = edblock->expiration_time; - GNUNET_memcpy (&gnr_block[1], &edblock[1], - size - sizeof (*gnr_block)); - /* encrypt and sign */ - GNUNET_CRYPTO_eddsa_public_key_derive (pkey, - label, - "gns", - &edblock->derived_key); - GNUNET_CRYPTO_eddsa_sign_derived (key, - label, - "gns", - &gnr_block->purpose, - &edblock->signature); - GNUNET_free (gnr_block); - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_block_sign (const struct - GNUNET_IDENTITY_PrivateKey *key, - const char *label, - struct GNUNET_GNSRECORD_Block *block) -{ - struct GNUNET_IDENTITY_PublicKey pkey; - enum GNUNET_GenericReturnValue res = GNUNET_SYSERR; - char *norm_label; - - GNUNET_IDENTITY_key_get_public (key, - &pkey); - norm_label = GNUNET_GNSRECORD_string_normalize (label); - - switch (ntohl (key->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - res = block_sign_ecdsa (&key->ecdsa_key, - &pkey.ecdsa_key, - norm_label, - block); - break; - case GNUNET_GNSRECORD_TYPE_EDKEY: - res = block_sign_eddsa (&key->eddsa_key, - &pkey.eddsa_key, - norm_label, - block); - break; - default: - GNUNET_assert (0); - } - GNUNET_free (norm_label); - return res; -} - - -/** - * Sign name and records - * - * @param key the private key - * @param pkey associated public key - * @param expire block expiration - * @param label the name for the records - * @param rd record data - * @param rd_count number of records - * @param block the block result. Must be allocated sufficiently. - * @param sign sign the block GNUNET_NO if block will be signed later. - * @return GNUNET_SYSERR on error (otherwise GNUNET_OK) - */ -static enum GNUNET_GenericReturnValue -block_create_ecdsa (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, - struct GNUNET_GNSRECORD_Block **block, - int sign) -{ - ssize_t payload_len = GNUNET_GNSRECORD_records_get_size (rd_count, - rd); - struct GNUNET_GNSRECORD_EcdsaBlock *ecblock; - unsigned char ctr[GNUNET_CRYPTO_AES_KEY_LENGTH / 2]; - unsigned char skey[GNUNET_CRYPTO_AES_KEY_LENGTH]; - struct GNUNET_GNSRECORD_Data rdc[GNUNET_NZL (rd_count)]; - struct GNUNET_TIME_Absolute now; - - if (payload_len < 0) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - if (payload_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - /* convert relative to absolute times */ - now = GNUNET_TIME_absolute_get (); - for (unsigned int i = 0; i < rd_count; i++) - { - rdc[i] = rd[i]; - if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - { - struct GNUNET_TIME_Relative t; - - /* encrypted blocks must never have relative expiration times, convert! */ - rdc[i].flags &= ~GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; - t.rel_value_us = rdc[i].expiration_time; - rdc[i].expiration_time = GNUNET_TIME_absolute_add (now, t).abs_value_us; - } - } - /* serialize */ - *block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) + payload_len); - (*block)->size = htonl (sizeof (struct GNUNET_GNSRECORD_Block) + payload_len); - { - char payload[payload_len]; - - GNUNET_assert (payload_len == - GNUNET_GNSRECORD_records_serialize (rd_count, - rdc, - payload_len, - payload)); - ecblock = &(*block)->ecdsa_block; - (*block)->type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); - ecblock->expiration_time = GNUNET_TIME_absolute_hton (expire); - GNR_derive_block_aes_key (ctr, - skey, - label, - ecblock->expiration_time.abs_value_us__, - pkey); - GNUNET_assert (payload_len == - ecdsa_symmetric_encrypt (payload, - payload_len, - skey, - ctr, - &ecblock[1])); - } - if (GNUNET_YES != sign) - return GNUNET_OK; - if (GNUNET_OK != - block_sign_ecdsa (key, pkey, label, *block)) - { - GNUNET_break (0); - GNUNET_free (*block); - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - -static ssize_t -block_get_size_eddsa (const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count) -{ - ssize_t len; - - len = GNUNET_GNSRECORD_records_get_size (rd_count, rd); - if (len < 0) - return -1; - len += sizeof(struct GNUNET_GNSRECORD_Block); - len += crypto_secretbox_MACBYTES; - return len; -} - - -/** - * Sign name and records (EDDSA version) - * - * @param key the private key - * @param pkey associated public key - * @param expire block expiration - * @param label the name for the records - * @param rd record data - * @param rd_count number of records - * @param block where to store the block. Must be allocated sufficiently. - * @param sign GNUNET_YES if block shall be signed as well - * @return GNUNET_SYSERR on error (otherwise GNUNET_OK) - */ -enum GNUNET_GenericReturnValue -block_create_eddsa (const struct GNUNET_CRYPTO_EddsaPrivateKey *key, - const struct GNUNET_CRYPTO_EddsaPublicKey *pkey, - struct GNUNET_TIME_Absolute expire, - const char *label, - const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count, - struct GNUNET_GNSRECORD_Block **block, - int sign) -{ - ssize_t payload_len = GNUNET_GNSRECORD_records_get_size (rd_count, - rd); - struct GNUNET_GNSRECORD_EddsaBlock *edblock; - unsigned char nonce[crypto_secretbox_NONCEBYTES]; - unsigned char skey[crypto_secretbox_KEYBYTES]; - struct GNUNET_GNSRECORD_Data rdc[GNUNET_NZL (rd_count)]; - struct GNUNET_TIME_Absolute now; - - if (payload_len < 0) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - if (payload_len > GNUNET_GNSRECORD_MAX_BLOCK_SIZE) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - /* convert relative to absolute times */ - now = GNUNET_TIME_absolute_get (); - for (unsigned int i = 0; i < rd_count; i++) - { - rdc[i] = rd[i]; - if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - { - struct GNUNET_TIME_Relative t; - - /* encrypted blocks must never have relative expiration times, convert! */ - rdc[i].flags &= ~GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; - t.rel_value_us = rdc[i].expiration_time; - rdc[i].expiration_time = GNUNET_TIME_absolute_add (now, t).abs_value_us; - } - } - /* serialize */ - *block = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Block) - + payload_len + crypto_secretbox_MACBYTES); - (*block)->size = htonl (sizeof (struct GNUNET_GNSRECORD_Block) - + payload_len + crypto_secretbox_MACBYTES); - { - char payload[payload_len]; - - GNUNET_assert (payload_len == - GNUNET_GNSRECORD_records_serialize (rd_count, - rdc, - payload_len, - payload)); - edblock = &(*block)->eddsa_block; - (*block)->type = htonl (GNUNET_GNSRECORD_TYPE_EDKEY); - edblock->expiration_time = GNUNET_TIME_absolute_hton (expire); - GNR_derive_block_xsalsa_key (nonce, - skey, - label, - edblock->expiration_time.abs_value_us__, - pkey); - GNUNET_assert (GNUNET_OK == - eddsa_symmetric_encrypt (payload, - payload_len, - skey, - nonce, - &edblock[1])); - if (GNUNET_YES != sign) - return GNUNET_OK; - block_sign_eddsa (key, pkey, label, *block); - } - return GNUNET_OK; -} - - -ssize_t -GNUNET_GNSRECORD_block_calculate_size (const struct - GNUNET_IDENTITY_PrivateKey *key, - const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count) -{ - struct GNUNET_IDENTITY_PublicKey pkey; - ssize_t res = -1; - - GNUNET_IDENTITY_key_get_public (key, - &pkey); - switch (ntohl (key->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - res = block_get_size_ecdsa (rd, rd_count); - break; - case GNUNET_GNSRECORD_TYPE_EDKEY: - res = block_get_size_eddsa (rd, rd_count); - break; - default: - GNUNET_assert (0); - } - return res; - -} - - -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) -{ - struct GNUNET_IDENTITY_PublicKey pkey; - enum GNUNET_GenericReturnValue res = GNUNET_SYSERR; - char *norm_label; - - GNUNET_IDENTITY_key_get_public (key, - &pkey); - norm_label = GNUNET_GNSRECORD_string_normalize (label); - - switch (ntohl (key->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - res = block_create_ecdsa (&key->ecdsa_key, - &pkey.ecdsa_key, - expire, - norm_label, - rd, - rd_count, - result, - GNUNET_YES); - break; - case GNUNET_GNSRECORD_TYPE_EDKEY: - res = block_create_eddsa (&key->eddsa_key, - &pkey.eddsa_key, - expire, - norm_label, - rd, - rd_count, - result, - GNUNET_YES); - break; - default: - GNUNET_assert (0); - } - GNUNET_free (norm_label); - return res; -} - - -/** - * Line in cache mapping private keys to public keys. - */ -struct KeyCacheLine -{ - /** - * A private key. - */ - struct GNUNET_CRYPTO_EcdsaPrivateKey key; - - /** - * Associated public key. - */ - struct GNUNET_CRYPTO_EcdsaPublicKey pkey; -}; - - -static enum GNUNET_GenericReturnValue -block_create2 (const struct GNUNET_IDENTITY_PrivateKey *pkey, - struct GNUNET_TIME_Absolute expire, - const char *label, - const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count, - struct GNUNET_GNSRECORD_Block **result, - int sign) -{ - const struct GNUNET_CRYPTO_EcdsaPrivateKey *key; - struct GNUNET_CRYPTO_EddsaPublicKey edpubkey; - enum GNUNET_GenericReturnValue res = GNUNET_SYSERR; - char *norm_label; - - norm_label = GNUNET_GNSRECORD_string_normalize (label); - - if (GNUNET_IDENTITY_TYPE_ECDSA == ntohl (pkey->type)) - { - key = &pkey->ecdsa_key; -#define CSIZE 64 - static struct KeyCacheLine cache[CSIZE]; - struct KeyCacheLine *line; - - line = &cache[(*(unsigned int *) key) % CSIZE]; - if (0 != memcmp (&line->key, - key, - sizeof(*key))) - { - /* cache miss, recompute */ - line->key = *key; - GNUNET_CRYPTO_ecdsa_key_get_public (key, - &line->pkey); - } -#undef CSIZE - res = block_create_ecdsa (key, - &line->pkey, - expire, - norm_label, - rd, - rd_count, - result, - sign); - } - else if (GNUNET_IDENTITY_TYPE_EDDSA == ntohl (pkey->type)) - { - GNUNET_CRYPTO_eddsa_key_get_public (&pkey->eddsa_key, - &edpubkey); - res = block_create_eddsa (&pkey->eddsa_key, - &edpubkey, - expire, - norm_label, - rd, - rd_count, - result, - sign); - } - GNUNET_free (norm_label); - return res; -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_block_create_unsigned (const struct - GNUNET_IDENTITY_PrivateKey *pkey, - struct GNUNET_TIME_Absolute expire, - const char *label, - const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count, - struct GNUNET_GNSRECORD_Block **result) -{ - return block_create2 (pkey, expire, label, rd, rd_count, result, GNUNET_NO); -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_block_create2 (const struct GNUNET_IDENTITY_PrivateKey *pkey, - struct GNUNET_TIME_Absolute expire, - const char *label, - const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count, - struct GNUNET_GNSRECORD_Block **result) -{ - return block_create2 (pkey, expire, label, rd, rd_count, result, GNUNET_YES); -} - - -/** - * Check if a signature is valid. This API is used by the GNS Block - * to validate signatures received from the network. - * - * @param block block to verify - * @return #GNUNET_OK if the signature is valid - */ -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block) -{ - struct GNRBlockPS *purp; - size_t payload_len = ntohl (block->size) - - sizeof (struct GNUNET_GNSRECORD_Block); - enum GNUNET_GenericReturnValue res = GNUNET_NO; - purp = GNUNET_malloc (sizeof (struct GNRBlockPS) + payload_len); - purp->purpose.size = htonl (sizeof (struct GNRBlockPS) + payload_len); - purp->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN); - GNUNET_memcpy (&purp[1], - &block[1], - payload_len); - switch (ntohl (block->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - purp->expiration_time = block->ecdsa_block.expiration_time; - res = GNUNET_CRYPTO_ecdsa_verify_ ( - GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, - &purp->purpose, - &block->ecdsa_block.signature, - &block->ecdsa_block.derived_key); - break; - case GNUNET_GNSRECORD_TYPE_EDKEY: - purp->expiration_time = block->eddsa_block.expiration_time; - res = GNUNET_CRYPTO_eddsa_verify_ ( - GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN, - &purp->purpose, - &block->eddsa_block.signature, - &block->eddsa_block.derived_key); - break; - default: - res = GNUNET_NO; - } - GNUNET_free (purp); - return res; -} - - -enum GNUNET_GenericReturnValue -block_decrypt_ecdsa (const struct GNUNET_GNSRECORD_Block *block, - const struct - GNUNET_CRYPTO_EcdsaPublicKey *zone_key, - const char *label, - GNUNET_GNSRECORD_RecordCallback proc, - void *proc_cls) -{ - size_t payload_len = ntohl (block->size) - sizeof (struct - GNUNET_GNSRECORD_Block); - unsigned char ctr[GNUNET_CRYPTO_AES_KEY_LENGTH / 2]; - unsigned char key[GNUNET_CRYPTO_AES_KEY_LENGTH]; - - if (ntohl (block->size) < - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) - + sizeof(struct GNUNET_TIME_AbsoluteNBO)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - GNR_derive_block_aes_key (ctr, - key, - label, - block->ecdsa_block.expiration_time.abs_value_us__, - zone_key); - { - char payload[payload_len]; - unsigned int rd_count; - - GNUNET_assert (payload_len == - ecdsa_symmetric_decrypt (&block[1], payload_len, - key, ctr, - payload)); - rd_count = GNUNET_GNSRECORD_records_deserialize_get_size (payload_len, - payload); - if (rd_count > 2048) - { - /* limit to sane value */ - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - { - struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL (rd_count)]; - unsigned int j; - struct GNUNET_TIME_Absolute now; - - if (GNUNET_OK != - GNUNET_GNSRECORD_records_deserialize (payload_len, - payload, - rd_count, - rd)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - /* hide expired records */ - now = GNUNET_TIME_absolute_get (); - j = 0; - for (unsigned int i = 0; i < rd_count; i++) - { - if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - { - /* encrypted blocks must never have relative expiration times, skip! */ - GNUNET_break_op (0); - continue; - } - - if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW)) - { - int include_record = GNUNET_YES; - /* Shadow record, figure out if we have a not expired active record */ - for (unsigned int k = 0; k < rd_count; k++) - { - if (k == i) - continue; - if (rd[i].expiration_time < now.abs_value_us) - include_record = GNUNET_NO; /* Shadow record is expired */ - if ((rd[k].record_type == rd[i].record_type) && - (rd[k].expiration_time >= now.abs_value_us) && - (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW))) - { - include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */ - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Ignoring shadow record\n"); - break; - } - } - if (GNUNET_YES == include_record) - { - rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW; /* Remove Flag */ - if (j != i) - rd[j] = rd[i]; - j++; - } - } - else if (rd[i].expiration_time >= now.abs_value_us) - { - /* Include this record */ - if (j != i) - rd[j] = rd[i]; - j++; - } - else - { - struct GNUNET_TIME_Absolute at; - - at.abs_value_us = rd[i].expiration_time; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Excluding record that expired %s (%llu ago)\n", - GNUNET_STRINGS_absolute_time_to_string (at), - (unsigned long long) rd[i].expiration_time - - now.abs_value_us); - } - } - rd_count = j; - if (NULL != proc) - proc (proc_cls, - rd_count, - (0 != rd_count) ? rd : NULL); - } - } - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -block_decrypt_eddsa (const struct GNUNET_GNSRECORD_Block *block, - const struct - GNUNET_CRYPTO_EddsaPublicKey *zone_key, - const char *label, - GNUNET_GNSRECORD_RecordCallback proc, - void *proc_cls) -{ - size_t payload_len = ntohl (block->size) - sizeof (struct - GNUNET_GNSRECORD_Block); - unsigned char nonce[crypto_secretbox_NONCEBYTES]; - unsigned char key[crypto_secretbox_KEYBYTES]; - - if (ntohl (block->size) < - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) - + sizeof(struct GNUNET_TIME_AbsoluteNBO)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - GNR_derive_block_xsalsa_key (nonce, - key, - label, - block->eddsa_block.expiration_time.abs_value_us__, - zone_key); - { - char payload[payload_len]; - unsigned int rd_count; - - GNUNET_assert (GNUNET_OK == - eddsa_symmetric_decrypt (&block[1], payload_len, - key, nonce, - payload)); - payload_len -= crypto_secretbox_MACBYTES; - rd_count = GNUNET_GNSRECORD_records_deserialize_get_size (payload_len, - payload); - if (rd_count > 2048) - { - /* limit to sane value */ - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - { - struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL (rd_count)]; - unsigned int j; - struct GNUNET_TIME_Absolute now; - - if (GNUNET_OK != - GNUNET_GNSRECORD_records_deserialize (payload_len, - payload, - rd_count, - rd)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - /* hide expired records */ - now = GNUNET_TIME_absolute_get (); - j = 0; - for (unsigned int i = 0; i < rd_count; i++) - { - if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - { - /* encrypted blocks must never have relative expiration times, skip! */ - GNUNET_break_op (0); - continue; - } - - if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW)) - { - int include_record = GNUNET_YES; - /* Shadow record, figure out if we have a not expired active record */ - for (unsigned int k = 0; k < rd_count; k++) - { - if (k == i) - continue; - if (rd[i].expiration_time < now.abs_value_us) - include_record = GNUNET_NO; /* Shadow record is expired */ - if ((rd[k].record_type == rd[i].record_type) && - (rd[k].expiration_time >= now.abs_value_us) && - (0 == (rd[k].flags & GNUNET_GNSRECORD_RF_SHADOW))) - { - include_record = GNUNET_NO; /* We have a non-expired, non-shadow record of the same type */ - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Ignoring shadow record\n"); - break; - } - } - if (GNUNET_YES == include_record) - { - rd[i].flags ^= GNUNET_GNSRECORD_RF_SHADOW; /* Remove Flag */ - if (j != i) - rd[j] = rd[i]; - j++; - } - } - else if (rd[i].expiration_time >= now.abs_value_us) - { - /* Include this record */ - if (j != i) - rd[j] = rd[i]; - j++; - } - else - { - struct GNUNET_TIME_Absolute at; - - at.abs_value_us = rd[i].expiration_time; - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Excluding record that expired %s (%llu ago)\n", - GNUNET_STRINGS_absolute_time_to_string (at), - (unsigned long long) rd[i].expiration_time - - now.abs_value_us); - } - } - rd_count = j; - if (NULL != proc) - proc (proc_cls, - rd_count, - (0 != rd_count) ? rd : NULL); - } - } - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_block_decrypt (const struct GNUNET_GNSRECORD_Block *block, - const struct - GNUNET_IDENTITY_PublicKey *zone_key, - const char *label, - GNUNET_GNSRECORD_RecordCallback proc, - void *proc_cls) -{ - enum GNUNET_GenericReturnValue res = GNUNET_SYSERR; - char *norm_label; - - norm_label = GNUNET_GNSRECORD_string_normalize (label); - switch (ntohl (zone_key->type)) - { - case GNUNET_IDENTITY_TYPE_ECDSA: - res = block_decrypt_ecdsa (block, - &zone_key->ecdsa_key, norm_label, proc, - proc_cls); - break; - case GNUNET_IDENTITY_TYPE_EDDSA: - res = block_decrypt_eddsa (block, - &zone_key->eddsa_key, norm_label, proc, - proc_cls); - break; - default: - res = GNUNET_SYSERR; - } - GNUNET_free (norm_label); - return res; -} - - -/** - * Calculate the DHT query for a given @a label in a given @a zone. - * - * @param zone private key of the zone - * @param label label of the record - * @param query hash to use for the query - */ -void -GNUNET_GNSRECORD_query_from_private_key (const struct - GNUNET_IDENTITY_PrivateKey *zone, - const char *label, - struct GNUNET_HashCode *query) -{ - char *norm_label; - struct GNUNET_IDENTITY_PublicKey pub; - - norm_label = GNUNET_GNSRECORD_string_normalize (label); - switch (ntohl (zone->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - case GNUNET_GNSRECORD_TYPE_EDKEY: - - GNUNET_IDENTITY_key_get_public (zone, - &pub); - GNUNET_GNSRECORD_query_from_public_key (&pub, - norm_label, - query); - break; - default: - GNUNET_assert (0); - } - GNUNET_free (norm_label); -} - - -void -GNUNET_GNSRECORD_query_from_public_key (const struct - GNUNET_IDENTITY_PublicKey *pub, - const char *label, - struct GNUNET_HashCode *query) -{ - char *norm_label; - struct GNUNET_IDENTITY_PublicKey pd; - - norm_label = GNUNET_GNSRECORD_string_normalize (label); - - switch (ntohl (pub->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - pd.type = pub->type; - GNUNET_CRYPTO_ecdsa_public_key_derive (&pub->ecdsa_key, - norm_label, - "gns", - &pd.ecdsa_key); - GNUNET_CRYPTO_hash (&pd.ecdsa_key, - sizeof (pd.ecdsa_key), - query); - break; - case GNUNET_GNSRECORD_TYPE_EDKEY: - pd.type = pub->type; - GNUNET_CRYPTO_eddsa_public_key_derive (&pub->eddsa_key, - norm_label, - "gns", - &(pd.eddsa_key)); - GNUNET_CRYPTO_hash (&pd.eddsa_key, - sizeof (pd.eddsa_key), - query); - break; - default: - GNUNET_assert (0); - } - GNUNET_free (norm_label); -} - - -/* end of gnsrecord_crypto.c */ diff --git a/src/gnsrecord/gnsrecord_crypto.h b/src/gnsrecord/gnsrecord_crypto.h deleted file mode 100644 index 4f2e4a5ca..000000000 --- a/src/gnsrecord/gnsrecord_crypto.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2009-2013, 2018 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file gnsrecord/gnsrecord_crypto.h - * @brief API for GNS record-related crypto - * @author Martin Schanzenbach - * @author Matthias Wachs - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_constants.h" -#include "gnunet_signatures.h" -#include "gnunet_arm_service.h" -#include "gnunet_gnsrecord_lib.h" - -/** - * Information we have in an encrypted block with record data (i.e. in the DHT). - */ -struct GNRBlockPS -{ - /** - * Number of bytes signed; also specifies the number of bytes - * of encrypted data that follow. - */ - struct GNUNET_CRYPTO_EccSignaturePurpose purpose; - - /** - * Expiration time of the block. - */ - struct GNUNET_TIME_AbsoluteNBO expiration_time; - - /* followed by encrypted data */ -}; - - -/** - * Derive session key and iv from label and public key. - * - * @param iv initialization vector to initialize - * @param skey session key to initialize - * @param label label to use for KDF - * @param pub public key to use for KDF - */ -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. - * - * @param nonce initialization vector to initialize - * @param skey session key to initialize - * @param label label to use for KDF - * @param pub public key to use for KDF - */ -void -GNR_derive_block_xsalsa_key (unsigned char *nonce, - unsigned char *key, - const char *label, - uint64_t exp, - const struct GNUNET_CRYPTO_EddsaPublicKey *pub); - diff --git a/src/gnsrecord/gnsrecord_misc.c b/src/gnsrecord/gnsrecord_misc.c deleted file mode 100644 index 931654c49..000000000 --- a/src/gnsrecord/gnsrecord_misc.c +++ /dev/null @@ -1,567 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2009-2013 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file gnsrecord/gnsrecord_misc.c - * @brief MISC functions related to GNS records - * @author Martin Schanzenbach - * @author Matthias Wachs - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_constants.h" -#include "gnunet_signatures.h" -#include "gnunet_arm_service.h" -#include "gnunet_gnsrecord_lib.h" - - -#define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__) - -char * -GNUNET_GNSRECORD_string_normalize (const char *src) -{ - /*FIXME: We may want to follow RFC5890/RFC5891 */ - return GNUNET_STRINGS_utf8_normalize (src); -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_label_check (const char*label, char **emsg) -{ - if (NULL == label) - { - *emsg = GNUNET_strdup (_ ("Label is NULL which is not allowed\n")); - return GNUNET_NO; - } - if (0 != strchr (label, '.')) - { - *emsg = GNUNET_strdup (_ ("Label contains `.' which is not allowed\n")); - return GNUNET_NO; - } - return GNUNET_OK; -} - - -const char * -GNUNET_GNSRECORD_z2s (const struct GNUNET_IDENTITY_PublicKey *z) -{ - static char buf[sizeof(struct GNUNET_IDENTITY_PublicKey) * 8]; - char *end; - - end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z, - sizeof(struct - GNUNET_IDENTITY_PublicKey), - buf, sizeof(buf)); - if (NULL == end) - { - GNUNET_break (0); - return NULL; - } - *end = '\0'; - return buf; -} - - -/** - * Compares if two records are equal (ignoring flags such - * as authority, private and pending, but not relative vs. - * absolute expiration time). - * - * @param a record - * @param b record - * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not - */ -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a, - const struct GNUNET_GNSRECORD_Data *b) -{ - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Comparing records\n"); - if (a->record_type != b->record_type) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Record type %u != %u\n", a->record_type, b->record_type); - return GNUNET_NO; - } - if ((a->expiration_time != b->expiration_time) && - ((a->expiration_time != 0) && (b->expiration_time != 0))) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Expiration time %llu != %llu\n", - (unsigned long long) a->expiration_time, - (unsigned long long) b->expiration_time); - return GNUNET_NO; - } - if ((a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS) - != (b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS)) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Flags %u (%u) != %u (%u)\n", a->flags, - a->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS, b->flags, - b->flags & GNUNET_GNSRECORD_RF_RCMP_FLAGS); - return GNUNET_NO; - } - if (a->data_size != b->data_size) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Data size %lu != %lu\n", - a->data_size, - b->data_size); - return GNUNET_NO; - } - if (0 != memcmp (a->data, b->data, a->data_size)) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Data contents do not match\n"); - return GNUNET_NO; - } - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Records are equal\n"); - return GNUNET_YES; -} - - -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) -{ - struct GNUNET_TIME_Absolute expire; - struct GNUNET_TIME_Absolute at; - struct GNUNET_TIME_Relative rt; - struct GNUNET_TIME_Absolute at_shadow; - struct GNUNET_TIME_Relative rt_shadow; - - if (0 == rd_count) - return GNUNET_TIME_absolute_max (GNUNET_TIME_UNIT_ZERO_ABS, min); - expire = GNUNET_TIME_UNIT_FOREVER_ABS; - for (unsigned int c = 0; c < rd_count; c++) - { - if (0 != (rd[c].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - { - rt.rel_value_us = rd[c].expiration_time; - at = GNUNET_TIME_relative_to_absolute (rt); - } - else - { - at.abs_value_us = rd[c].expiration_time; - } - - for (unsigned int c2 = 0; c2 < rd_count; c2++) - { - /* Check for shadow record */ - if ((c == c2) || - (rd[c].record_type != rd[c2].record_type) || - (0 == (rd[c2].flags & GNUNET_GNSRECORD_RF_SHADOW))) - continue; - /* We have a shadow record */ - if (0 != (rd[c2].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - { - rt_shadow.rel_value_us = rd[c2].expiration_time; - at_shadow = GNUNET_TIME_relative_to_absolute (rt_shadow); - } - else - { - at_shadow.abs_value_us = rd[c2].expiration_time; - } - at = GNUNET_TIME_absolute_max (at, - at_shadow); - } - expire = GNUNET_TIME_absolute_min (at, - expire); - } - expire = GNUNET_TIME_absolute_max (expire, min); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Determined expiration time for block with %u records to be %s\n", - rd_count, - GNUNET_STRINGS_absolute_time_to_string (expire)); - return expire; -} - - -/** - * Test if a given record is expired. - * - * @return #GNUNET_YES if the record is expired, - * #GNUNET_NO if not - */ -int -GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd) -{ - struct GNUNET_TIME_Absolute at; - - if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - return GNUNET_NO; - at.abs_value_us = rd->expiration_time; - return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ? - GNUNET_YES : GNUNET_NO; -} - - -/** - * Convert public key to the respective absolute domain name in the - * ".zkey" pTLD. - * This is one of the very few calls in the entire API that is - * NOT reentrant! - * - * @param pkey a public key with a point on the eliptic curve - * @return string "X.zkey" where X is the public - * key in an encoding suitable for DNS labels. - */ -const char * -GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_IDENTITY_PublicKey *pkey) -{ - static char ret[128]; - char *pkeys; - - pkeys = GNUNET_IDENTITY_public_key_to_string (pkey); - GNUNET_snprintf (ret, - sizeof(ret), - "%s", - pkeys); - GNUNET_free (pkeys); - return ret; -} - - -/** - * Convert an absolute domain name to the - * respective public key. - * - * @param zkey string encoding the coordinates of the public - * key in an encoding suitable for DNS labels. - * @param pkey set to a public key on the eliptic curve - * @return #GNUNET_SYSERR if @a zkey has the wrong syntax - */ -int -GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey, - struct GNUNET_IDENTITY_PublicKey *pkey) -{ - if (GNUNET_OK != - GNUNET_IDENTITY_public_key_from_string (zkey, - pkey)) - return GNUNET_SYSERR; - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_identity_from_data (const char *data, - size_t data_size, - uint32_t type, - struct GNUNET_IDENTITY_PublicKey *key) -{ - if (GNUNET_NO == GNUNET_GNSRECORD_is_zonekey_type (type)) - return GNUNET_SYSERR; - switch (type) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - if (data_size > sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) - return GNUNET_SYSERR; - memcpy (&key->ecdsa_key, data, data_size); - break; - case GNUNET_GNSRECORD_TYPE_EDKEY: - if (data_size > sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) - return GNUNET_SYSERR; - memcpy (&key->eddsa_key, data, data_size); - break; - default: - return GNUNET_NO; - } - key->type = htonl (type); - - return GNUNET_YES; -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_data_from_identity (const struct - GNUNET_IDENTITY_PublicKey *key, - char **data, - size_t *data_size, - uint32_t *type) -{ - char *tmp; - *type = ntohl (key->type); - *data_size = GNUNET_IDENTITY_public_key_get_length (key) - sizeof (key->type); - if (0 == *data_size) - return GNUNET_SYSERR; - tmp = GNUNET_malloc (*data_size); - memcpy (tmp, ((char*) key) + sizeof (key->type), *data_size); - *data = tmp; - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_is_zonekey_type (uint32_t type) -{ - switch (type) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - case GNUNET_GNSRECORD_TYPE_EDKEY: - return GNUNET_YES; - default: - return GNUNET_NO; - } -} - - -size_t -GNUNET_GNSRECORD_block_get_size (const struct GNUNET_GNSRECORD_Block *block) -{ - return ntohl (block->size); -} - - -struct GNUNET_TIME_Absolute -GNUNET_GNSRECORD_block_get_expiration (const struct - GNUNET_GNSRECORD_Block *block) -{ - - switch (ntohl (block->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - return GNUNET_TIME_absolute_ntoh (block->ecdsa_block.expiration_time); - case GNUNET_GNSRECORD_TYPE_EDKEY: - return GNUNET_TIME_absolute_ntoh (block->eddsa_block.expiration_time); - default: - GNUNET_break (0); /* Hopefully we never get here, but we might */ - } - return GNUNET_TIME_absolute_get_zero_ (); - -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_query_from_block (const struct GNUNET_GNSRECORD_Block *block, - struct GNUNET_HashCode *query) -{ - switch (ntohl (block->type)) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - GNUNET_CRYPTO_hash (&(block->ecdsa_block.derived_key), - sizeof (block->ecdsa_block.derived_key), - query); - return GNUNET_OK; - case GNUNET_GNSRECORD_TYPE_EDKEY: - GNUNET_CRYPTO_hash (&block->eddsa_block.derived_key, - sizeof (block->eddsa_block.derived_key), - query); - return GNUNET_OK; - default: - return GNUNET_SYSERR; - } - return GNUNET_SYSERR; - -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_record_to_identity_key (const struct GNUNET_GNSRECORD_Data *rd, - struct GNUNET_IDENTITY_PublicKey *key) -{ - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Got record of type %u\n", - rd->record_type); - switch (rd->record_type) - { - case GNUNET_GNSRECORD_TYPE_PKEY: - key->type = htonl (rd->record_type); - memcpy (&key->ecdsa_key, rd->data, sizeof (key->ecdsa_key)); - return GNUNET_OK; - case GNUNET_GNSRECORD_TYPE_EDKEY: - key->type = htonl (rd->record_type); - memcpy (&key->eddsa_key, rd->data, sizeof (key->eddsa_key)); - return GNUNET_OK; - default: - return GNUNET_SYSERR; - } - return GNUNET_SYSERR; - - -} - - -enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_normalize_record_set (const char *label, - const struct - GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count, - struct GNUNET_GNSRECORD_Data * - rd_public, - unsigned int *rd_count_public, - struct GNUNET_TIME_Absolute *expiry, - enum GNUNET_GNSRECORD_Filter filter, - char **emsg) -{ - struct GNUNET_TIME_Absolute now; - struct GNUNET_TIME_Absolute minimum_expiration; - int have_zone_delegation = GNUNET_NO; - int have_gns2dns = GNUNET_NO; - int have_other = GNUNET_NO; - int have_redirect = GNUNET_NO; - int have_empty_label = (0 == strcmp (GNUNET_GNS_EMPTY_LABEL_AT, label)); - unsigned int rd_count_tmp; - - minimum_expiration = GNUNET_TIME_UNIT_ZERO_ABS; - now = GNUNET_TIME_absolute_get (); - rd_count_tmp = 0; - for (unsigned int i = 0; i < rd_count; i++) - { - /* Ignore private records for public record set */ - if ((0 != (filter & GNUNET_GNSRECORD_FILTER_OMIT_PRIVATE)) && - (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Filtering private record filter=%u...\n", filter); - continue; - } - /* Skip expired records */ - if ((0 == (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) && - (rd[i].expiration_time < now.abs_value_us)) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Filtering expired record...\n"); - continue; /* record already expired, skip it */ - } - /* Ignore the tombstone unless filter permits explicitly. - * Remember expiration time. */ - if (GNUNET_GNSRECORD_TYPE_TOMBSTONE == rd[i].record_type) - { - minimum_expiration.abs_value_us = rd[i].expiration_time; - if (0 != (filter & GNUNET_GNSRECORD_FILTER_INCLUDE_MAINTENANCE)) - { - rd_public[rd_count_tmp] = rd[i]; - rd_count_tmp++; - } - continue; - } - /* No NICK records unless empty label */ - if (have_empty_label && - (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type)) - continue; - - /** - * Check for delegation and redirect consistency. - * Note that we check for consistency BEFORE we filter for - * private records ON PURPOSE. - * We also want consistent record sets in our local zone(s). - * The only exception is the tombstone (above) which we ignore - * for the consistency check(s). - * FIXME: What about shadow records? Should we ignore them? - */ - if (GNUNET_YES == GNUNET_GNSRECORD_is_zonekey_type (rd[i].record_type)) - { - /* No delegation records under empty label*/ - if (have_empty_label) - { - *emsg = GNUNET_strdup (_ ( - "Zone delegation record not allowed in apex.")); - return GNUNET_SYSERR; - } - if ((GNUNET_YES == have_other) || - (GNUNET_YES == have_redirect) || - (GNUNET_YES == have_gns2dns)) - { - *emsg = GNUNET_strdup (_ ( - "Zone delegation record set contains mutually exclusive records.")); - return GNUNET_SYSERR; - } - have_zone_delegation = GNUNET_YES; - } - else if (GNUNET_GNSRECORD_TYPE_REDIRECT == rd[i].record_type) - { - if (GNUNET_YES == have_redirect) - { - *emsg = GNUNET_strdup (_ ( - "Multiple REDIRECT records.")); - return GNUNET_SYSERR; - - } - if ((GNUNET_YES == have_other) || - (GNUNET_YES == have_zone_delegation) || - (GNUNET_YES == have_gns2dns)) - { - *emsg = GNUNET_strdup (_ ( - "Redirection record set contains mutually exclusive records.")); - return GNUNET_SYSERR; - } - /* No redirection records under empty label*/ - if (have_empty_label) - { - *emsg = GNUNET_strdup (_ ( - "Redirection records not allowed in apex.")); - return GNUNET_SYSERR; - } - have_redirect = GNUNET_YES; - } - else if (GNUNET_GNSRECORD_TYPE_GNS2DNS == rd[i].record_type) - { - /* No gns2dns records under empty label*/ - if (have_empty_label) - { - *emsg = GNUNET_strdup (_ ( - "Redirection records not allowed in apex..")); - return GNUNET_SYSERR; - } - if ((GNUNET_YES == have_other) || - (GNUNET_YES == have_redirect) || - (GNUNET_YES == have_zone_delegation)) - { - *emsg = GNUNET_strdup (_ ( - "Redirection record set contains mutually exclusive records.")); - return GNUNET_SYSERR; - } - have_gns2dns = GNUNET_YES; - } - else - { - /* Some other record. - * Not allowed for zone delegations or redirections */ - if ((GNUNET_YES == have_zone_delegation) || - (GNUNET_YES == have_redirect) || - (GNUNET_YES == have_gns2dns)) - { - *emsg = GNUNET_strdup (_ ( - "Mutually exclusive records.")); - return GNUNET_SYSERR; - } - have_other = GNUNET_YES; - } - - rd_public[rd_count_tmp] = rd[i]; - /* Make sure critical record types are marked as such */ - if (GNUNET_YES == GNUNET_GNSRECORD_is_critical (rd[i].record_type)) - rd_public[rd_count_tmp].flags |= GNUNET_GNSRECORD_RF_CRITICAL; - rd_count_tmp++; - } - - *expiry = GNUNET_GNSRECORD_record_get_expiration_time (rd_count_tmp, - rd_public, - minimum_expiration); - *rd_count_public = rd_count_tmp; - return GNUNET_OK; -} - - -/* end of gnsrecord_misc.c */ diff --git a/src/gnsrecord/gnsrecord_serialization.c b/src/gnsrecord/gnsrecord_serialization.c deleted file mode 100644 index 053edfd33..000000000 --- a/src/gnsrecord/gnsrecord_serialization.c +++ /dev/null @@ -1,302 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2009-2013 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file gnsrecord/gnsrecord_serialization.c - * @brief API to serialize and deserialize GNS records - * @author Martin Schanzenbach - * @author Matthias Wachs - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_constants.h" -#include "gnunet_signatures.h" -#include "gnunet_arm_service.h" -#include "gnunet_gnsrecord_lib.h" - -#define LOG(kind, ...) GNUNET_log_from (kind, "gnsrecord", __VA_ARGS__) - -/** - * Set to 1 to check that all records are well-formed (can be converted - * to string) during serialization/deserialization. - */ -#define DEBUG_GNSRECORDS 0 - -GNUNET_NETWORK_STRUCT_BEGIN - - -/** - * Internal format of a record in the serialized form. - */ -struct NetworkRecord -{ - /** - * Expiration time for the DNS record; relative or absolute depends - * on @e flags, network byte order. - */ - uint64_t expiration_time GNUNET_PACKED; - - /** - * Number of bytes in 'data', network byte order. - */ - uint16_t data_size GNUNET_PACKED; - - /** - * Flags for the record, network byte order. - */ - uint16_t flags GNUNET_PACKED; - - /** - * Type of the GNS/DNS record, network byte order. - */ - uint32_t record_type GNUNET_PACKED; - -}; - -GNUNET_NETWORK_STRUCT_END - - -ssize_t -GNUNET_GNSRECORD_records_get_size (unsigned int rd_count, - const struct GNUNET_GNSRECORD_Data *rd) -{ - size_t ret; - - if (0 == rd_count) - return 0; - - ret = sizeof(struct NetworkRecord) * rd_count; - for (unsigned int i = 0; i < rd_count; i++) - { - if ((ret + rd[i].data_size) < ret) - { - GNUNET_break (0); - return -1; - } - ret += rd[i].data_size; -#if DEBUG_GNSRECORDS - { - char *str; - - str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, - rd[i].data, - rd[i].data_size); - if (NULL == str) - { - GNUNET_break_op (0); - return -1; - } - GNUNET_free (str); - } -#endif - } - if (ret > SSIZE_MAX) - { - GNUNET_break (0); - return -1; - } - // Do not pad PKEY - if ((GNUNET_GNSRECORD_TYPE_PKEY == rd->record_type) || - (GNUNET_GNSRECORD_TYPE_EDKEY == rd->record_type)) - return ret; - /** - * Efficiently round up to the next - * power of 2 for padding - * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 - */ret--; - ret |= ret >> 1; - ret |= ret >> 2; - ret |= ret >> 4; - ret |= ret >> 8; - ret |= ret >> 16; - ret++; - return (ssize_t) ret; -} - - -ssize_t -GNUNET_GNSRECORD_records_serialize (unsigned int rd_count, - const struct GNUNET_GNSRECORD_Data *rd, - size_t dest_size, - char *dest) -{ - struct NetworkRecord rec; - size_t off; - - off = 0; - for (unsigned int i = 0; i < rd_count; i++) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Serializing record %u with flags %d and expiration time %llu\n", - i, - rd[i].flags, - (unsigned long long) rd[i].expiration_time); - rec.expiration_time = GNUNET_htonll (rd[i].expiration_time); - rec.data_size = htons ((uint16_t) rd[i].data_size); - rec.record_type = htonl (rd[i].record_type); - rec.flags = htons (rd[i].flags); - if ((off + sizeof(rec) > dest_size) || - (off + sizeof(rec) < off)) - { - GNUNET_break (0); - return -1; - } - GNUNET_memcpy (&dest[off], - &rec, - sizeof(rec)); - off += sizeof(rec); - if ((off + rd[i].data_size > dest_size) || - (off + rd[i].data_size < off)) - { - GNUNET_break (0); - return -1; - } - GNUNET_memcpy (&dest[off], - rd[i].data, - rd[i].data_size); - off += rd[i].data_size; -#if DEBUG_GNSRECORDS - { - char *str; - - str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, - rd[i].data, - rd[i].data_size); - if (NULL == str) - { - GNUNET_break_op (0); - return -1; - } - GNUNET_free (str); - } -#endif - } - memset (&dest[off], - 0, - dest_size - off); - return dest_size; -} - -unsigned int -GNUNET_GNSRECORD_records_deserialize_get_size (size_t len, - const char *src) -{ - struct NetworkRecord rec; - struct NetworkRecord rec_zero; - size_t off; - unsigned int rd_count = 0; - - memset (&rec_zero, 0, sizeof (rec_zero)); - - off = 0; - for (off = 0; (off + sizeof(rec) <= len) && (off + sizeof(rec) >= off);) - { - GNUNET_memcpy (&rec, - &src[off], - sizeof(rec)); - /* - * If we have found a byte string of zeroes, we have reached - * the padding - */ - if (0 == GNUNET_memcmp (&rec, &rec_zero)) - break; - off += sizeof(rec); - if ((off + ntohs ((uint16_t) rec.data_size) > len) || - (off + ntohs ((uint16_t) rec.data_size) < off)) - { - GNUNET_break_op (0); - return 0; - } - off += ntohs ((uint16_t) rec.data_size); - rd_count++; - } - return rd_count; -} - -/** - * Deserialize the given records to the given destination. - * - * @param len size of the serialized record data - * @param src the serialized record data - * @param rd_count number of records parsed - * @param dest where to put the data - * @return #GNUNET_OK on success, #GNUNET_SYSERR on error - */ -int -GNUNET_GNSRECORD_records_deserialize (size_t len, - const char *src, - unsigned int rd_count, - struct GNUNET_GNSRECORD_Data *dest) -{ - struct NetworkRecord rec; - size_t off; - - off = 0; - for (unsigned int i = 0; i < rd_count; i++) - { - if ((off + sizeof(rec) > len) || - (off + sizeof(rec) < off)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - GNUNET_memcpy (&rec, - &src[off], - sizeof(rec)); - dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time); - dest[i].data_size = ntohs ((uint16_t) rec.data_size); - dest[i].record_type = ntohl (rec.record_type); - dest[i].flags = ntohs (rec.flags); - off += sizeof(rec); - if ((off + dest[i].data_size > len) || - (off + dest[i].data_size < off)) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - dest[i].data = &src[off]; - off += dest[i].data_size; -#if GNUNET_EXTRA_LOGGING - { - char *str; - - str = GNUNET_GNSRECORD_value_to_string (dest[i].record_type, - dest[i].data, - dest[i].data_size); - if (NULL == str) - { - GNUNET_break_op (0); - return GNUNET_SYSERR; - } - GNUNET_free (str); - } -#endif - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Deserialized record %u with flags %d and expiration time %llu\n", - i, - dest[i].flags, - (unsigned long long) dest[i].expiration_time); - } - return GNUNET_OK; -} - - -/* end of gnsrecord_serialization.c */ diff --git a/src/gnsrecord/gnunet-gnsrecord-tvg.c b/src/gnsrecord/gnunet-gnsrecord-tvg.c deleted file mode 100644 index eebbedf21..000000000 --- a/src/gnsrecord/gnunet-gnsrecord-tvg.c +++ /dev/null @@ -1,539 +0,0 @@ -/* - 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 . - - 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_testing_lib.h" -#include "gnsrecord_crypto.h" -#include - - -static char *d_pkey = - "50d7b652a4efeadff37396909785e5952171a02178c8e7d450fa907925fafd98"; - -static char *d_edkey = - "5af7020ee19160328832352bbc6a68a8d71a7cbe1b929969a7c66d415a0d8f65"; - - -static int -parsehex (char *src, char *dst, size_t dstlen, int invert) -{ - char *line = src; - char *data = line; - int off; - int read_byte; - int data_len = 0; - - while (sscanf (data, " %02x%n", &read_byte, &off) == 1) - { - if (invert) - dst[dstlen - 1 - data_len++] = read_byte; - else - dst[data_len++] = read_byte; - data += off; - } - return data_len; -} - - -static void -print_bytes_ (void *buf, - size_t buf_len, - int fold, - int in_be) -{ - int i; - - for (i = 0; i < buf_len; i++) - { - if (0 != i) - { - if ((0 != fold) && (i % fold == 0)) - printf ("\n "); - else - printf (" "); - } - else - { - printf (" "); - } - if (in_be) - printf ("%02x", ((unsigned char*) buf)[buf_len - 1 - i]); - else - printf ("%02x", ((unsigned char*) buf)[i]); - } - printf ("\n"); -} - - -static void -print_bytes (void *buf, - size_t buf_len, - int fold) -{ - print_bytes_ (buf, buf_len, fold, 0); -} - - -static void -print_record (const struct GNUNET_GNSRECORD_Data *rd) -{ - struct GNUNET_TIME_Relative rt; - struct GNUNET_TIME_Absolute at; - uint16_t flags = htons (rd->flags); - uint64_t abs_nbo = GNUNET_htonll (rd->expiration_time); - uint16_t size_nbo = htons (rd->data_size); - uint32_t type_nbo = htonl (rd->record_type); - at.abs_value_us = GNUNET_ntohll (abs_nbo); - if (0 != (rd->flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) - { - rt.rel_value_us = rd->expiration_time; - at = GNUNET_TIME_relative_to_absolute (rt); - abs_nbo = GNUNET_htonll (at.abs_value_us); - } - printf (" EXPIRATION: %" PRIu64 " us\n", rd->expiration_time); - print_bytes (&abs_nbo, sizeof (abs_nbo), 8); - printf ("\n DATA_SIZE:\n"); - print_bytes (&size_nbo, sizeof (size_nbo), 8); - printf ("\n TYPE:\n"); - print_bytes (&type_nbo, sizeof (type_nbo), 8); - printf ("\n FLAGS: "); - print_bytes ((void*) &flags, sizeof (flags), 8); - printf ("\n"); - fprintf (stdout, - " DATA:\n"); - print_bytes ((char*) rd->data, rd->data_size, 8); - printf ("\n"); -} - - -/** - * 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_pkey (struct GNUNET_GNSRECORD_Data *rd, int rd_count, const char *label) -{ - struct GNUNET_TIME_Absolute expire; - struct GNUNET_GNSRECORD_Block *rrblock; - char *bdata; - struct GNUNET_IDENTITY_PrivateKey id_priv; - struct GNUNET_IDENTITY_PublicKey id_pub; - struct GNUNET_IDENTITY_PrivateKey pkey_data_p; - struct GNUNET_IDENTITY_PublicKey pkey_data; - struct GNUNET_HashCode query; - char *rdata; - char *conv_lbl; - size_t rdata_size; - char ztld[128]; - unsigned char ctr[GNUNET_CRYPTO_AES_KEY_LENGTH / 2]; - unsigned char skey[GNUNET_CRYPTO_AES_KEY_LENGTH]; - - id_priv.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); - GNUNET_CRYPTO_ecdsa_key_create (&id_priv.ecdsa_key); - parsehex (d_pkey, - (char*) &id_priv.ecdsa_key, - sizeof (id_priv.ecdsa_key), 1); - - GNUNET_IDENTITY_key_get_public (&id_priv, - &id_pub); - printf ("Zone private key (d, big-endian):\n"); - print_bytes_ (&id_priv.ecdsa_key, - sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 8, 1); - printf ("\n"); - printf ("Zone identifier (ztype|zkey):\n"); - GNUNET_assert (0 < GNUNET_IDENTITY_public_key_get_length (&id_pub)); - print_bytes (&id_pub, GNUNET_IDENTITY_public_key_get_length (&id_pub), 8); - GNUNET_STRINGS_data_to_string (&id_pub, - GNUNET_IDENTITY_public_key_get_length ( - &id_pub), - ztld, - sizeof (ztld)); - printf ("\n"); - printf ("zTLD:\n"); - printf ("%s\n", ztld); - printf ("\n"); - - pkey_data_p.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); - GNUNET_CRYPTO_ecdsa_key_create (&pkey_data_p.ecdsa_key); - GNUNET_IDENTITY_key_get_public (&pkey_data_p, - &pkey_data); - conv_lbl = GNUNET_GNSRECORD_string_normalize (label); - printf ("Label:\n"); - print_bytes (conv_lbl, strlen (conv_lbl), 8); - GNUNET_free (conv_lbl); - printf ("\nNumber of records (integer): %d\n\n", rd_count); - - for (int i = 0; i < rd_count; i++) - { - printf ("Record #%d := (\n", i); - print_record (&rd[i]); - printf (")\n\n"); - } - - rdata_size = GNUNET_GNSRECORD_records_get_size (rd_count, - rd); - rdata = GNUNET_malloc (rdata_size); - GNUNET_GNSRECORD_records_serialize (rd_count, - rd, - (size_t) rdata_size, - rdata); - printf ("RDATA:\n"); - print_bytes (rdata, - (size_t) rdata_size, - 8); - printf ("\n"); - expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_count, rd, - GNUNET_TIME_UNIT_ZERO_ABS); - GNR_derive_block_aes_key (ctr, - skey, - label, - GNUNET_TIME_absolute_hton ( - expire).abs_value_us__, - &id_pub.ecdsa_key); - - printf ("Encryption NONCE|EXPIRATION|BLOCK COUNTER:\n"); - print_bytes (ctr, sizeof (ctr), 8); - printf ("\n"); - printf ("Encryption key (K):\n"); - print_bytes (skey, sizeof (skey), 8); - printf ("\n"); - GNUNET_GNSRECORD_query_from_public_key (&id_pub, - label, - &query); - printf ("Storage key (q):\n"); - print_bytes (&query, sizeof (query), 8); - printf ("\n"); - GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create (&id_priv, - expire, - label, - rd, - rd_count, - &rrblock)); - struct GNUNET_CRYPTO_EcdsaPublicKey derived_key; - struct GNUNET_CRYPTO_EcdsaPrivateKey *derived_privkey; - - GNUNET_CRYPTO_ecdsa_public_key_derive (&id_pub.ecdsa_key, - label, - "gns", - &derived_key); - derived_privkey = GNUNET_CRYPTO_ecdsa_private_key_derive (&id_priv.ecdsa_key, - label, - "gns"); - printf ("ZKDF(zkey):\n"); - print_bytes (&derived_key, sizeof (derived_key), 8); - printf ("\n"); - printf ("Derived private key (d', big-endian):\n"); - print_bytes_ (derived_privkey, sizeof (*derived_privkey), 8, 1); - printf ("\n"); - size_t bdata_size = ntohl (rrblock->size) - sizeof (struct - GNUNET_GNSRECORD_Block); - - GNUNET_free (derived_privkey); - - bdata = (char*) &(&rrblock->ecdsa_block)[1]; - printf ("BDATA:\n"); - print_bytes (bdata, bdata_size, 8); - printf ("\n"); - printf ("RRBLOCK:\n"); - print_bytes (rrblock, ntohl (rrblock->size), 8); - printf ("\n"); - GNUNET_free (rdata); -} - - -/** - * 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_edkey (struct GNUNET_GNSRECORD_Data *rd, int rd_count, const char*label) -{ - struct GNUNET_TIME_Absolute expire; - struct GNUNET_GNSRECORD_Block *rrblock; - char *bdata; - struct GNUNET_IDENTITY_PrivateKey id_priv; - struct GNUNET_IDENTITY_PublicKey id_pub; - struct GNUNET_IDENTITY_PrivateKey pkey_data_p; - struct GNUNET_IDENTITY_PublicKey pkey_data; - struct GNUNET_HashCode query; - char *rdata; - char *conv_lbl; - size_t rdata_size; - - char ztld[128]; - unsigned char nonce[crypto_secretbox_NONCEBYTES]; - unsigned char skey[crypto_secretbox_KEYBYTES]; - - id_priv.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); - GNUNET_CRYPTO_ecdsa_key_create (&id_priv.ecdsa_key); - GNUNET_IDENTITY_key_get_public (&id_priv, - &id_pub); - - id_priv.type = htonl (GNUNET_IDENTITY_TYPE_EDDSA); - GNUNET_CRYPTO_eddsa_key_create (&id_priv.eddsa_key); - parsehex (d_edkey, - (char*) &id_priv.eddsa_key, - sizeof (id_priv.eddsa_key), 0); - GNUNET_IDENTITY_key_get_public (&id_priv, - &id_pub); - fprintf (stdout, - "Zone private key (d):\n"); - print_bytes (&id_priv.eddsa_key, sizeof (struct - GNUNET_CRYPTO_EddsaPrivateKey), 8); - printf ("\n"); - printf ("Zone identifier (ztype|zkey):\n"); - GNUNET_assert (0 < GNUNET_IDENTITY_public_key_get_length (&id_pub)); - print_bytes (&id_pub, GNUNET_IDENTITY_public_key_get_length (&id_pub), 8); - GNUNET_STRINGS_data_to_string (&id_pub, - GNUNET_IDENTITY_public_key_get_length ( - &id_pub), - ztld, - sizeof (ztld)); - printf ("\n"); - printf ("zTLD:\n"); - printf ("%s\n", ztld); - printf ("\n"); - - pkey_data_p.type = htonl (GNUNET_GNSRECORD_TYPE_EDKEY); - GNUNET_CRYPTO_eddsa_key_create (&pkey_data_p.eddsa_key); - GNUNET_IDENTITY_key_get_public (&pkey_data_p, - &pkey_data); - conv_lbl = GNUNET_GNSRECORD_string_normalize (label); - printf ("Label:\n"); - print_bytes (conv_lbl, strlen (conv_lbl), 8); - GNUNET_free (conv_lbl); - fprintf (stdout, - "\nNumber of records (integer): %d\n\n", rd_count); - - for (int i = 0; i < rd_count; i++) - { - printf ("Record #%d := (\n", i); - print_record (&rd[i]); - printf (")\n\n"); - } - - rdata_size = GNUNET_GNSRECORD_records_get_size (rd_count, - rd); - expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_count, - rd, - GNUNET_TIME_UNIT_ZERO_ABS); - GNUNET_assert (0 < rdata_size); - rdata = GNUNET_malloc ((size_t) rdata_size); - GNUNET_GNSRECORD_records_serialize (rd_count, - rd, - (size_t) rdata_size, - rdata); - printf ("RDATA:\n"); - print_bytes (rdata, - (size_t) rdata_size, - 8); - printf ("\n"); - GNR_derive_block_xsalsa_key (nonce, - skey, - label, - GNUNET_TIME_absolute_hton ( - expire).abs_value_us__, - &id_pub.eddsa_key); - printf ("Encryption NONCE|EXPIRATION:\n"); - print_bytes (nonce, sizeof (nonce), 8); - printf ("\n"); - printf ("Encryption key (K):\n"); - print_bytes (skey, sizeof (skey), 8); - printf ("\n"); - GNUNET_GNSRECORD_query_from_public_key (&id_pub, - label, - &query); - printf ("Storage key (q):\n"); - print_bytes (&query, sizeof (query), 8); - printf ("\n"); - - GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create (&id_priv, - expire, - label, - rd, - rd_count, - &rrblock)); - - struct GNUNET_CRYPTO_EddsaPublicKey derived_key; - struct GNUNET_CRYPTO_EddsaPrivateScalar derived_privkey; - GNUNET_CRYPTO_eddsa_public_key_derive (&id_pub.eddsa_key, - label, - "gns", - &derived_key); - GNUNET_CRYPTO_eddsa_private_key_derive (&id_priv.eddsa_key, - label, - "gns", &derived_privkey); - printf ("ZKDF(zkey):\n"); - print_bytes (&derived_key, sizeof (derived_key), 8); - printf ("\n"); - printf ("nonce := SHA-256 (dh[32..63] || h):\n"); - print_bytes (derived_privkey.s + 32, 32, 8); - printf ("\n"); - char derived_privkeyNBO[32]; - /* Convert from little endian */ - for (size_t i = 0; i < 32; i++) - derived_privkeyNBO[i] = derived_privkey.s[31 - i]; - printf ("Derived private key (d', big-endian):\n"); - print_bytes (derived_privkeyNBO, sizeof (derived_privkeyNBO), 8); - printf ("\n"); - size_t bdata_size = ntohl (rrblock->size) - sizeof (struct - GNUNET_GNSRECORD_Block); - - - bdata = (char*) &(&rrblock->eddsa_block)[1]; - printf ("BDATA:\n"); - print_bytes (bdata, bdata_size, 8); - printf ("\n"); - printf ("RRBLOCK:\n"); - print_bytes (rrblock, ntohl (rrblock->size), 8); - printf ("\n"); - GNUNET_free (rdata); -} - - -/** - * 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_pkey; - struct GNUNET_GNSRECORD_Data rd[3]; - struct GNUNET_TIME_Absolute exp1; - struct GNUNET_TIME_Absolute exp2; - struct GNUNET_TIME_Absolute exp3; - struct GNUNET_TIME_AbsoluteNBO exp1nbo; - struct GNUNET_TIME_AbsoluteNBO exp2nbo; - struct GNUNET_TIME_AbsoluteNBO exp3nbo; - size_t pkey_data_size; - size_t ip_data_size; - char *pkey_data; - char *ip_data; - - /* - * Make different expiration times - */ - parsehex ("001cee8c10e25980", (char*) &exp1nbo, sizeof (exp1nbo), 0); - parsehex ("003ff2aa5408db40", (char*) &exp2nbo, sizeof (exp2nbo), 0); - parsehex ("0028bb13ff371940", (char*) &exp3nbo, sizeof (exp3nbo), 0); - exp1 = GNUNET_TIME_absolute_ntoh (exp1nbo); - exp2 = GNUNET_TIME_absolute_ntoh (exp2nbo); - exp3 = GNUNET_TIME_absolute_ntoh (exp3nbo); - - memset (&rd_pkey, 0, sizeof (struct GNUNET_GNSRECORD_Data)); - GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value ( - GNUNET_GNSRECORD_TYPE_PKEY, - "000G0011WESGZY9VRV9NNJ66W3GKNZFZF56BFD2BQF3MHMJST2G2GKDYGG", - (void**) &pkey_data, - &pkey_data_size)); - rd_pkey.data = pkey_data; - rd_pkey.data_size = pkey_data_size; - rd_pkey.expiration_time = exp1.abs_value_us; - rd_pkey.record_type = GNUNET_GNSRECORD_TYPE_PKEY; - rd_pkey.flags = GNUNET_GNSRECORD_RF_CRITICAL; - GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value ( - GNUNET_DNSPARSER_TYPE_AAAA, - "::dead:beef", - (void**) &ip_data, - &ip_data_size)); - - rd[0].data = ip_data; - rd[0].data_size = ip_data_size; - rd[0].expiration_time = exp1.abs_value_us; - rd[0].record_type = GNUNET_DNSPARSER_TYPE_AAAA; - rd[0].flags = GNUNET_GNSRECORD_RF_NONE; - - rd[1].data = "\u611b\u79f0"; - rd[1].data_size = strlen (rd[1].data); - rd[1].expiration_time = exp2.abs_value_us; - rd[1].record_type = GNUNET_GNSRECORD_TYPE_NICK; - rd[1].flags = GNUNET_GNSRECORD_RF_NONE; - - rd[2].data = "Hello World"; - rd[2].data_size = strlen (rd[2].data); - rd[2].expiration_time = exp3.abs_value_us; - rd[2].record_type = GNUNET_DNSPARSER_TYPE_TXT; - rd[2].flags = GNUNET_GNSRECORD_RF_SUPPLEMENTAL; - - run_pkey (&rd_pkey, 1, "testdelegation"); - run_pkey (rd, 3, "\u5929\u4e0b\u7121\u6575"); - run_edkey (&rd_pkey, 1, "testdelegation"); - run_edkey (rd, 3, "\u5929\u4e0b\u7121\u6575"); -} - - -/** - * 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)); - // gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0); - // gcry_control (GCRYCTL_SET_VERBOSITY, 99); - 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 */ diff --git a/src/gnsrecord/json_gnsrecord.c b/src/gnsrecord/json_gnsrecord.c deleted file mode 100644 index cc96e42fb..000000000 --- a/src/gnsrecord/json_gnsrecord.c +++ /dev/null @@ -1,389 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2009-2013 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file json/json_gnsrecord.c - * @brief JSON handling of GNS record data - * @author Philippe Buschmann - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_json_lib.h" -#include "gnunet_gnsrecord_lib.h" - -#define GNUNET_JSON_GNSRECORD_VALUE "value" -#define GNUNET_JSON_GNSRECORD_RECORD_DATA "data" -#define GNUNET_JSON_GNSRECORD_TYPE "record_type" -#define GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME "relative_expiration" -#define GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME "absolute_expiration" -#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE "is_private" -#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL "is_supplemental" -#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE "is_relative_expiration" -#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW "is_shadow" -#define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name" - -struct GnsRecordInfo -{ - char **name; - - unsigned int *rd_count; - - struct GNUNET_GNSRECORD_Data **rd; -}; - - -static void -cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info) -{ - char *tmp; - - if (NULL != *(gnsrecord_info->rd)) - { - for (int i = 0; i < *(gnsrecord_info->rd_count); i++) - { - tmp = (char*) (*(gnsrecord_info->rd))[i].data; - if (NULL != tmp) - GNUNET_free (tmp); - } - GNUNET_free (*(gnsrecord_info->rd)); - *(gnsrecord_info->rd) = NULL; - } - if (NULL != *(gnsrecord_info->name)) - GNUNET_free (*(gnsrecord_info->name)); - *(gnsrecord_info->name) = NULL; -} - - -/** - * Parse given JSON object to gns record - * - * @param cls closure, NULL - * @param root the json object representing data - * @param spec where to write the data - * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error - */ -static int -parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) -{ - struct GNUNET_TIME_Absolute abs_exp; - struct GNUNET_TIME_Relative rel_exp; - const char *value; - const char *record_type; - int private; - int supplemental; - int is_rel_exp; - int shadow; - int unpack_state = 0; - json_error_t err; - - // interpret single gns record - unpack_state = json_unpack_ex (data, - &err, - 0, - "{s:s, s:s, s:I, s:b, s:b, s:b, s:b}", - GNUNET_JSON_GNSRECORD_VALUE, - &value, - GNUNET_JSON_GNSRECORD_TYPE, - &record_type, - GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME, - &rel_exp.rel_value_us, - GNUNET_JSON_GNSRECORD_FLAG_PRIVATE, - &private, - GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL, - &supplemental, - GNUNET_JSON_GNSRECORD_FLAG_RELATIVE, - &is_rel_exp, - GNUNET_JSON_GNSRECORD_FLAG_SHADOW, - &shadow); - if (0 != unpack_state) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Error gnsdata object has a wrong format: `%s'!\n", - err.text); - unpack_state = json_unpack_ex (data, - &err, - 0, - "{s:s, s:s, s:I, s:b, s:b, s:b, s:b}", - GNUNET_JSON_GNSRECORD_VALUE, - &value, - GNUNET_JSON_GNSRECORD_TYPE, - &record_type, - GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME, - &abs_exp.abs_value_us, - GNUNET_JSON_GNSRECORD_FLAG_PRIVATE, - &private, - GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL, - &supplemental, - GNUNET_JSON_GNSRECORD_FLAG_RELATIVE, - &is_rel_exp, - GNUNET_JSON_GNSRECORD_FLAG_SHADOW, - &shadow); - if ((0 != unpack_state) || (is_rel_exp)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Error gnsdata object has a wrong format: `%s'!\n", - (is_rel_exp) ? "No relative expiration given" : err.text); - return GNUNET_SYSERR; - } - rd->expiration_time = abs_exp.abs_value_us; - } - else - { - rd->expiration_time = rel_exp.rel_value_us; - } - rd->record_type = GNUNET_GNSRECORD_typename_to_number (record_type); - if (UINT32_MAX == rd->record_type) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n"); - return GNUNET_SYSERR; - } - if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (rd->record_type, - value, - (void **) &rd->data, - &rd->data_size)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n"); - return GNUNET_SYSERR; - } - - if (is_rel_exp) - rd->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; - if (1 == private) - rd->flags |= GNUNET_GNSRECORD_RF_PRIVATE; - if (1 == supplemental) - rd->flags |= GNUNET_GNSRECORD_RF_SUPPLEMENTAL; - if (1 == shadow) - rd->flags |= GNUNET_GNSRECORD_RF_SHADOW; - return GNUNET_OK; -} - - -/** - * Parse given JSON object to gns record - * - * @param cls closure, NULL - * @param root the json object representing data - * @param spec where to write the data - * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error - */ -static int -parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data) -{ - GNUNET_assert (NULL != data); - if (! json_is_array (data)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Error gns record data JSON is not an array!\n"); - return GNUNET_SYSERR; - } - *(gnsrecord_info->rd_count) = json_array_size (data); - *(gnsrecord_info->rd) = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Data) - * json_array_size (data)); - size_t index; - json_t *value; - json_array_foreach (data, index, value) - { - if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index])) - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - -static int -parse_gnsrecordobject (void *cls, - json_t *root, - struct GNUNET_JSON_Specification *spec) -{ - struct GnsRecordInfo *gnsrecord_info; - int unpack_state = 0; - const char *name; - json_t *data; - - GNUNET_assert (NULL != root); - if (! json_is_object (root)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Error record JSON is not an object!\n"); - return GNUNET_SYSERR; - } - // interpret single gns record - unpack_state = json_unpack (root, - "{s:s, s:o!}", - GNUNET_JSON_GNSRECORD_RECORD_NAME, - &name, - GNUNET_JSON_GNSRECORD_RECORD_DATA, - &data); - if (0 != unpack_state) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Error namestore records object has a wrong format!\n"); - return GNUNET_SYSERR; - } - gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; - *(gnsrecord_info->name) = GNUNET_strdup (name); - if (GNUNET_OK != parse_record_data (gnsrecord_info, data)) - { - cleanup_recordinfo (gnsrecord_info); - return GNUNET_SYSERR; - } - return GNUNET_OK; -} - - -/** - * Cleanup data left from parsing the record. - * - * @param cls closure, NULL - * @param[out] spec where to free the data - */ -static void -clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) -{ - struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; - - GNUNET_free (gnsrecord_info); -} - - -/** - * JSON Specification for GNS Records. - * - * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill - * @return JSON Specification - */ -struct GNUNET_JSON_Specification -GNUNET_GNSRECORD_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd, - unsigned int *rd_count, - char **name) -{ - struct GnsRecordInfo *gnsrecord_info = GNUNET_new (struct GnsRecordInfo); - - gnsrecord_info->rd = rd; - gnsrecord_info->name = name; - gnsrecord_info->rd_count = rd_count; - struct GNUNET_JSON_Specification ret = { .parser = &parse_gnsrecordobject, - .cleaner = &clean_gnsrecordobject, - .cls = NULL, - .field = NULL, - .ptr = (struct GnsRecordInfo *) - gnsrecord_info, - .ptr_size = 0, - .size_ptr = NULL }; - return ret; -} - - -/** - * Convert GNS record to JSON. - * - * @param rname name of record - * @param rd record data - * @return corresponding JSON encoding - */ -json_t * -GNUNET_GNSRECORD_JSON_from_gnsrecord (const char*rname, - const struct GNUNET_GNSRECORD_Data *rd, - unsigned int rd_count) -{ - const char *record_type_str; - char *value_str; - json_t *data; - json_t *record; - json_t *records; - - data = json_object (); - if (NULL == data) - { - GNUNET_break (0); - return NULL; - } - if (0 != - json_object_set_new (data, - "record_name", - json_string (rname))) - { - GNUNET_break (0); - json_decref (data); - return NULL; - } - records = json_array (); - if (NULL == records) - { - GNUNET_break (0); - json_decref (data); - return NULL; - } - for (int i = 0; i < rd_count; i++) - { - value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, - rd[i].data, - rd[i].data_size); - record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Packing %s %s %" PRIu64 " %d\n", - value_str, record_type_str, rd[i].expiration_time, rd[i].flags); - record = json_pack ("{s:s,s:s,s:I,s:b,s:b,s:b,s:b}", - GNUNET_JSON_GNSRECORD_VALUE, - value_str, - GNUNET_JSON_GNSRECORD_TYPE, - record_type_str, - (rd[i].flags - & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION) ? - GNUNET_JSON_GNSRECORD_RELATIVE_EXPIRATION_TIME : - GNUNET_JSON_GNSRECORD_ABSOLUTE_EXPIRATION_TIME, - rd[i].expiration_time, - GNUNET_JSON_GNSRECORD_FLAG_PRIVATE, - rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE, - GNUNET_JSON_GNSRECORD_FLAG_RELATIVE, - rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, - GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL, - rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL, - GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL, - rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW); - GNUNET_free (value_str); - if (NULL == record) - { - GNUNET_break (0); - json_decref (records); - json_decref (data); - return NULL; - } - if (0 != - json_array_append_new (records, - record)) - { - GNUNET_break (0); - json_decref (records); - json_decref (data); - return NULL; - } - } - if (0 != - json_object_set_new (data, - "data", - records)) - { - GNUNET_break (0); - json_decref (data); - return NULL; - } - return data; -} diff --git a/src/gnsrecord/meson.build b/src/gnsrecord/meson.build deleted file mode 100644 index 11b355796..000000000 --- a/src/gnsrecord/meson.build +++ /dev/null @@ -1,45 +0,0 @@ -libgnunetgnsrecord_src = ['gnsrecord.c', - 'gnsrecord_serialization.c', - 'gnsrecord_crypto.c', - 'gnsrecord_misc.c'] -libgnunetgnsrecordjson_src = ['json_gnsrecord.c'] - -if get_option('monolith') - foreach p : libgnunetgnsrecord_src + libgnunetgnsrecordjson_src - gnunet_src += 'gnsrecord/' + p - endforeach - subdir_done() -endif - -libgnunetgnsrecord = library('gnunetgnsrecord', - libgnunetgnsrecord_src, - soversion: '0', - version: '0.0.0', - dependencies: [libgnunetutil_dep, - sodium_dep, - libgnunetidentity_dep, - gcrypt_dep], - include_directories: [incdir, configuration_inc], - install: true, - install_dir: get_option('libdir')) -libgnunetgnsrecord_dep = declare_dependency(link_with : libgnunetgnsrecord) -pkg.generate(libgnunetgnsrecord, url: 'https://www.gnunet.org', - description : 'Provides API for manipulating GNS records') - - -libgnunetgnsrecordjson = library('gnunetgnsrecordjson', - libgnunetgnsrecordjson_src, - soversion: '0', - version: '0.0.0', - dependencies: [libgnunetutil_dep, libgnunetgnsrecord_dep, json_dep], - include_directories: [incdir, configuration_inc], - install: true, - install_dir: get_option('libdir')) -libgnunetgnsrecordjson_dep = declare_dependency(link_with : libgnunetgnsrecordjson) - -shared_module('gnunet_plugin_gnsrecord_dns', - ['plugin_gnsrecord_dns.c'], - dependencies: [libgnunetutil_dep, libgnunetgnsrecord_dep], - include_directories: [incdir, configuration_inc], - install: true, - install_dir: get_option('libdir')/'gnunet') diff --git a/src/gnsrecord/perf_gnsrecord_crypto.c b/src/gnsrecord/perf_gnsrecord_crypto.c deleted file mode 100644 index 6b5f84235..000000000 --- a/src/gnsrecord/perf_gnsrecord_crypto.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2018 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ -/** - * @file gnsrecord/test_gnsrecord_crypto.c - * @brief testcase for block creation, verification and decryption - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_gnsrecord_lib.h" - -#define ROUNDS 1000 - -#define RECORDS 5 - -#define TEST_RECORD_TYPE 1234 - -#define TEST_RECORD_DATALEN 123 - -#define TEST_RECORD_DATA 'a' - -#define TEST_REMOVE_RECORD_TYPE 4321 - -#define TEST_REMOVE_RECORD_DATALEN 255 - -#define TEST_REMOVE_RECORD_DATA 'b' - - -static struct GNUNET_GNSRECORD_Data * -create_record (int count) -{ - struct GNUNET_GNSRECORD_Data *rd; - - rd = GNUNET_new_array (count, - struct GNUNET_GNSRECORD_Data); - for (unsigned int c = 0; c < count; c++) - { - rd[c].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us - + 1000000000; - rd[c].record_type = TEST_RECORD_TYPE; - rd[c].data_size = TEST_RECORD_DATALEN; - rd[c].data = GNUNET_malloc (TEST_RECORD_DATALEN); - memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); - } - return rd; -} - - -static void -run (void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) -{ - struct GNUNET_GNSRECORD_Block *block; - struct GNUNET_HashCode query; - struct GNUNET_GNSRECORD_Data *s_rd; - const char *s_name; - struct GNUNET_TIME_Absolute start_time; - struct GNUNET_IDENTITY_PrivateKey privkey; - struct GNUNET_TIME_Absolute expire; - - (void) cls; - (void) args; - (void) cfgfile; - (void) cfg; - expire = GNUNET_TIME_absolute_get (); - privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); - GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); - - /* test block creation */ - s_name = "DUMMY.dummy.gnunet"; - s_rd = create_record (RECORDS); - start_time = GNUNET_TIME_absolute_get (); - for (unsigned int i = 0; i < ROUNDS; i++) - { - GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create2 (&privkey, - expire, - s_name, - s_rd, - RECORDS, - &block)); - GNUNET_GNSRECORD_query_from_private_key (&privkey, - s_name, - &query); - GNUNET_free (block); - } - fprintf (stderr, - "Took %s to produce %u GNS blocks for the DHT\n", - GNUNET_STRINGS_relative_time_to_string ( - GNUNET_TIME_absolute_get_duration (start_time), - GNUNET_YES), - ROUNDS); - for (unsigned int i = 0; i < RECORDS; i++) - GNUNET_free_nz ((void *) s_rd[i].data); - GNUNET_free (s_rd); -} - - -int -main (int argc, char *argv[]) -{ - static char *const argvx[] = { - "perf-gnsrecord-crypto", - NULL - }; - static struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - if (GNUNET_OK != - GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1, - argvx, - "perf-gnsrecord-crypto", - "nohelp", options, - &run, - NULL)) - return 1; - return 0; -} - - -/* end of test_gnsrecord_crypto.c */ diff --git a/src/gnsrecord/plugin_gnsrecord_dns.c b/src/gnsrecord/plugin_gnsrecord_dns.c deleted file mode 100644 index 5844d9eda..000000000 --- a/src/gnsrecord/plugin_gnsrecord_dns.c +++ /dev/null @@ -1,818 +0,0 @@ -/* - This file is part of GNUnet - Copyright (C) 2013, 2014 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ - -/** - * @file gnsrecord/plugin_gnsrecord_dns.c - * @brief gnsrecord plugin to provide the API for basic DNS records - * @author Christian Grothoff - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_gnsrecord_plugin.h" - - -/** - * Convert the 'value' of a record to a string. - * - * @param cls closure, unused - * @param type type of the record - * @param data value in binary encoding - * @param data_size number of bytes in @a data - * @return NULL on error, otherwise human-readable representation of the value - */ -static char * -dns_value_to_string (void *cls, - uint32_t type, - const void *data, - size_t data_size) -{ - char *result; - char tmp[INET6_ADDRSTRLEN]; - - switch (type) - { - case GNUNET_DNSPARSER_TYPE_A: - if (data_size != sizeof(struct in_addr)) - return NULL; - if (NULL == inet_ntop (AF_INET, data, tmp, sizeof(tmp))) - return NULL; - return GNUNET_strdup (tmp); - - case GNUNET_DNSPARSER_TYPE_NS: { - char *ns; - size_t off; - - off = 0; - ns = GNUNET_DNSPARSER_parse_name (data, data_size, &off); - if ((NULL == ns) || (off != data_size)) - { - GNUNET_break_op (0); - GNUNET_free (ns); - return NULL; - } - return ns; - } - - case GNUNET_DNSPARSER_TYPE_CNAME: { - char *cname; - size_t off; - - off = 0; - cname = GNUNET_DNSPARSER_parse_name (data, data_size, &off); - if ((NULL == cname) || (off != data_size)) - { - GNUNET_break_op (0); - GNUNET_free (cname); - return NULL; - } - return cname; - } - - case GNUNET_DNSPARSER_TYPE_SOA: { - struct GNUNET_DNSPARSER_SoaRecord *soa; - size_t off; - - off = 0; - soa = GNUNET_DNSPARSER_parse_soa (data, data_size, &off); - if ((NULL == soa) || (off != data_size)) - { - GNUNET_break_op (0); - if (NULL != soa) - GNUNET_DNSPARSER_free_soa (soa); - return NULL; - } - GNUNET_asprintf (&result, - "%s %s ( %u %u %u %u %u )", - soa->rname, - soa->mname, - soa->serial, - soa->refresh, - soa->retry, - soa->expire, - soa->minimum_ttl); - GNUNET_DNSPARSER_free_soa (soa); - return result; - } - - case GNUNET_DNSPARSER_TYPE_PTR: { - char *ptr; - size_t off; - - off = 0; - ptr = GNUNET_DNSPARSER_parse_name (data, data_size, &off); - if ((NULL == ptr) || (off != data_size)) - { - GNUNET_break_op (0); - GNUNET_free (ptr); - return NULL; - } - return ptr; - } - - case GNUNET_DNSPARSER_TYPE_CERT: { - struct GNUNET_DNSPARSER_CertRecord *cert; - size_t off; - char *base64; - int len; - - off = 0; - cert = GNUNET_DNSPARSER_parse_cert (data, data_size, &off); - if ((NULL == cert) || (off != data_size)) - { - GNUNET_break_op (0); - GNUNET_DNSPARSER_free_cert (cert); - return NULL; - } - len = GNUNET_STRINGS_base64_encode (cert->certificate_data, - cert->certificate_size, - &base64); - GNUNET_asprintf (&result, - "%u %u %u %.*s", - cert->cert_type, - cert->cert_tag, - cert->algorithm, - len, - base64); - GNUNET_free (base64); - GNUNET_DNSPARSER_free_cert (cert); - return result; - } - - case GNUNET_DNSPARSER_TYPE_MX: { - struct GNUNET_DNSPARSER_MxRecord *mx; - size_t off; - - off = 0; - mx = GNUNET_DNSPARSER_parse_mx (data, data_size, &off); - if ((NULL == mx) || (off != data_size)) - { - GNUNET_break_op (0); - GNUNET_DNSPARSER_free_mx (mx); - return NULL; - } - GNUNET_asprintf (&result, - "%u %s", - (unsigned int) mx->preference, - mx->mxhost); - GNUNET_DNSPARSER_free_mx (mx); - return result; - } - - case GNUNET_DNSPARSER_TYPE_TXT: - return GNUNET_strndup (data, data_size); - - case GNUNET_DNSPARSER_TYPE_AAAA: - if (data_size != sizeof(struct in6_addr)) - return NULL; - if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof(tmp))) - return NULL; - return GNUNET_strdup (tmp); - - case GNUNET_DNSPARSER_TYPE_SRV: { - struct GNUNET_DNSPARSER_SrvRecord *srv; - size_t off; - - off = 0; - srv = GNUNET_DNSPARSER_parse_srv (data, data_size, &off); - if ((NULL == srv) || (off != data_size)) - { - GNUNET_break_op (0); - if (NULL != srv) - GNUNET_DNSPARSER_free_srv (srv); - return NULL; - } - GNUNET_asprintf (&result, - "%d %d %d %s", - srv->priority, - srv->weight, - srv->port, - srv->target); - GNUNET_DNSPARSER_free_srv (srv); - return result; - } - - case GNUNET_DNSPARSER_TYPE_TLSA: { - const struct GNUNET_TUN_DnsTlsaRecord *tlsa; - char *tlsa_str; - char *hex; - - if (data_size < sizeof(struct GNUNET_TUN_DnsTlsaRecord)) - return NULL; /* malformed */ - tlsa = data; - hex = - GNUNET_DNSPARSER_bin_to_hex (&tlsa[1], - data_size - - sizeof(struct GNUNET_TUN_DnsTlsaRecord)); - if (0 == GNUNET_asprintf (&tlsa_str, - "%u %u %u %s", - (unsigned int) tlsa->usage, - (unsigned int) tlsa->selector, - (unsigned int) tlsa->matching_type, - hex)) - { - GNUNET_free (hex); - GNUNET_free (tlsa_str); - return NULL; - } - GNUNET_free (hex); - return tlsa_str; - } - - case GNUNET_DNSPARSER_TYPE_CAA: { // RFC6844 - const struct GNUNET_DNSPARSER_CaaRecord *caa; - char tag[15]; // between 1 and 15 bytes - char value[data_size]; - char *caa_str; - if (data_size < sizeof(struct GNUNET_DNSPARSER_CaaRecord)) - return NULL; /* malformed */ - caa = data; - if ((1 > caa->tag_len) || (15 < caa->tag_len)) - return NULL; /* malformed */ - memset (tag, 0, sizeof(tag)); - memset (value, 0, data_size); - memcpy (tag, &caa[1], caa->tag_len); - memcpy (value, - (char *) &caa[1] + caa->tag_len, - data_size - caa->tag_len - 2); - if (0 == GNUNET_asprintf (&caa_str, - "%u %s %s", - (unsigned int) caa->flags, - tag, - value)) - { - GNUNET_free (caa_str); - return NULL; - } - return caa_str; - } - - default: - return NULL; - } -} - - -/** - * Convert RFC 4394 Mnemonics to the corresponding integer values. - * - * @param mnemonic string to look up - * @return the value, 0 if not found - */ -static unsigned int -rfc4398_mnemonic_to_value (const char *mnemonic) -{ - static struct - { - const char *mnemonic; - unsigned int val; - } table[] = { { "PKIX", 1 }, - { "SPKI", 2 }, - { "PGP", 3 }, - { "IPKIX", 4 }, - { "ISPKI", 5 }, - { "IPGP", 6 }, - { "ACPKIX", 7 }, - { "IACPKIX", 8 }, - { "URI", 253 }, - { "OID", 254 }, - { NULL, 0 } }; - unsigned int i; - - for (i = 0; NULL != table[i].mnemonic; i++) - if (0 == strcasecmp (mnemonic, table[i].mnemonic)) - return table[i].val; - return 0; -} - - -/** - * Convert RFC 4034 algorithm types to the corresponding integer values. - * - * @param mnemonic string to look up - * @return the value, 0 if not found - */ -static unsigned int -rfc4034_mnemonic_to_value (const char *mnemonic) -{ - static struct - { - const char *mnemonic; - unsigned int val; - } table[] = { { "RSAMD5", 1 }, - { "DH", 2 }, - { "DSA", 3 }, - { "ECC", 4 }, - { "RSASHA1", 5 }, - { "INDIRECT", 252 }, - { "PRIVATEDNS", 253 }, - { "PRIVATEOID", 254 }, - { NULL, 0 } }; - unsigned int i; - - for (i = 0; NULL != table[i].mnemonic; i++) - if (0 == strcasecmp (mnemonic, table[i].mnemonic)) - return table[i].val; - return 0; -} - - -/** - * Convert human-readable version of a 'value' of a record to the binary - * representation. - * - * @param cls closure, unused - * @param type type of the record - * @param s human-readable string - * @param data set to value in binary encoding (will be allocated) - * @param data_size set to number of bytes in @a data - * @return #GNUNET_OK on success - */ -static int -dns_string_to_value (void *cls, - uint32_t type, - const char *s, - void **data, - size_t *data_size) -{ - struct in_addr value_a; - struct in6_addr value_aaaa; - struct GNUNET_TUN_DnsTlsaRecord *tlsa; - - if (NULL == s) - return GNUNET_SYSERR; - switch (type) - { - case GNUNET_DNSPARSER_TYPE_A: - if (1 != inet_pton (AF_INET, s, &value_a)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse IPv4 address `%s'\n"), - s); - return GNUNET_SYSERR; - } - *data = GNUNET_new (struct in_addr); - GNUNET_memcpy (*data, &value_a, sizeof(value_a)); - *data_size = sizeof(value_a); - return GNUNET_OK; - - case GNUNET_DNSPARSER_TYPE_NS: { - char nsbuf[256]; - size_t off; - - off = 0; - if (GNUNET_OK != - GNUNET_DNSPARSER_builder_add_name (nsbuf, sizeof(nsbuf), &off, s)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Failed to serialize NS record with value `%s'\n"), - s); - return GNUNET_SYSERR; - } - *data_size = off; - *data = GNUNET_malloc (off); - GNUNET_memcpy (*data, nsbuf, off); - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_CNAME: { - char cnamebuf[256]; - size_t off; - - off = 0; - if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_name (cnamebuf, - sizeof(cnamebuf), - &off, - s)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Failed to serialize CNAME record with value `%s'\n"), - s); - return GNUNET_SYSERR; - } - *data_size = off; - *data = GNUNET_malloc (off); - GNUNET_memcpy (*data, cnamebuf, off); - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_CERT: { - char *sdup; - const char *typep; - const char *keyp; - const char *algp; - const char *certp; - unsigned int type; - unsigned int key; - unsigned int alg; - size_t cert_size; - char *cert_data; - struct GNUNET_DNSPARSER_CertRecord cert; - - sdup = GNUNET_strdup (s); - typep = strtok (sdup, " "); - if ((NULL == typep) || - ((0 == (type = rfc4398_mnemonic_to_value (typep))) && - ((1 != sscanf (typep, "%u", &type)) || (type > UINT16_MAX)))) - { - GNUNET_free (sdup); - return GNUNET_SYSERR; - } - keyp = strtok (NULL, " "); - if ((NULL == keyp) || (1 != sscanf (keyp, "%u", &key)) || - (key > UINT16_MAX)) - { - GNUNET_free (sdup); - return GNUNET_SYSERR; - } - alg = 0; - algp = strtok (NULL, " "); - if ((NULL == algp) || - ((0 == (type = rfc4034_mnemonic_to_value (typep))) && - ((1 != sscanf (algp, "%u", &alg)) || (alg > UINT8_MAX)))) - { - GNUNET_free (sdup); - return GNUNET_SYSERR; - } - certp = strtok (NULL, " "); - if ((NULL == certp) || (0 == strlen (certp))) - { - GNUNET_free (sdup); - return GNUNET_SYSERR; - } - cert_size = GNUNET_STRINGS_base64_decode (certp, - strlen (certp), - (void **) &cert_data); - GNUNET_free (sdup); - cert.cert_type = type; - cert.cert_tag = key; - cert.algorithm = alg; - cert.certificate_size = cert_size; - cert.certificate_data = cert_data; - { - char certbuf[cert_size + sizeof(struct GNUNET_TUN_DnsCertRecord)]; - size_t off; - - off = 0; - if (GNUNET_OK != GNUNET_DNSPARSER_builder_add_cert (certbuf, - sizeof(certbuf), - &off, - &cert)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Failed to serialize CERT record with %u bytes\n"), - (unsigned int) cert_size); - GNUNET_free (cert_data); - return GNUNET_SYSERR; - } - *data_size = off; - *data = GNUNET_malloc (off); - GNUNET_memcpy (*data, certbuf, off); - } - GNUNET_free (cert_data); - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_SOA: { - struct GNUNET_DNSPARSER_SoaRecord soa; - char soabuf[540]; - char soa_rname[253 + 1]; - char soa_mname[253 + 1]; - unsigned int soa_serial; - unsigned int soa_refresh; - unsigned int soa_retry; - unsigned int soa_expire; - unsigned int soa_min; - size_t off; - - if (7 != sscanf (s, - "%253s %253s ( %u %u %u %u %u )", - soa_rname, - soa_mname, - &soa_serial, - &soa_refresh, - &soa_retry, - &soa_expire, - &soa_min)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse SOA record `%s'\n"), - s); - return GNUNET_SYSERR; - } - soa.mname = soa_mname; - soa.rname = soa_rname; - soa.serial = (uint32_t) soa_serial; - soa.refresh = (uint32_t) soa_refresh; - soa.retry = (uint32_t) soa_retry; - soa.expire = (uint32_t) soa_expire; - soa.minimum_ttl = (uint32_t) soa_min; - off = 0; - if (GNUNET_OK != - GNUNET_DNSPARSER_builder_add_soa (soabuf, sizeof(soabuf), &off, &soa)) - { - GNUNET_log ( - GNUNET_ERROR_TYPE_ERROR, - _ ("Failed to serialize SOA record with mname `%s' and rname `%s'\n"), - soa_mname, - soa_rname); - return GNUNET_SYSERR; - } - *data_size = off; - *data = GNUNET_malloc (off); - GNUNET_memcpy (*data, soabuf, off); - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_PTR: { - char ptrbuf[256]; - size_t off; - - off = 0; - if (GNUNET_OK != - GNUNET_DNSPARSER_builder_add_name (ptrbuf, sizeof(ptrbuf), &off, s)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Failed to serialize PTR record with value `%s'\n"), - s); - return GNUNET_SYSERR; - } - *data_size = off; - *data = GNUNET_malloc (off); - GNUNET_memcpy (*data, ptrbuf, off); - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_MX: { - struct GNUNET_DNSPARSER_MxRecord mx; - char mxbuf[258]; - char mxhost[253 + 1]; - unsigned int mx_pref; - size_t off; - - if (2 != sscanf (s, "%u %253s", &mx_pref, mxhost)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse MX record `%s'\n"), - s); - return GNUNET_SYSERR; - } - mx.preference = (uint16_t) mx_pref; - mx.mxhost = mxhost; - off = 0; - - if (GNUNET_OK != - GNUNET_DNSPARSER_builder_add_mx (mxbuf, sizeof(mxbuf), &off, &mx)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Failed to serialize MX record with hostname `%s'\n"), - mxhost); - return GNUNET_SYSERR; - } - *data_size = off; - *data = GNUNET_malloc (off); - GNUNET_memcpy (*data, mxbuf, off); - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_SRV: { - struct GNUNET_DNSPARSER_SrvRecord srv; - char srvbuf[270]; - char srvtarget[253 + 1]; - unsigned int priority; - unsigned int weight; - unsigned int port; - size_t off; - - if (4 != sscanf (s, "%u %u %u %253s", &priority, &weight, &port, - srvtarget)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse SRV record `%s'\n"), - s); - return GNUNET_SYSERR; - } - srv.priority = (uint16_t) priority; - srv.weight = (uint16_t) weight; - srv.port = (uint16_t) port; - srv.target = srvtarget; - off = 0; - if (GNUNET_OK != - GNUNET_DNSPARSER_builder_add_srv (srvbuf, sizeof(srvbuf), &off, &srv)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Failed to serialize SRV record with target `%s'\n"), - srvtarget); - return GNUNET_SYSERR; - } - *data_size = off; - *data = GNUNET_malloc (off); - GNUNET_memcpy (*data, srvbuf, off); - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_TXT: - *data = GNUNET_strdup (s); - *data_size = strlen (s); - return GNUNET_OK; - - case GNUNET_DNSPARSER_TYPE_AAAA: - if (1 != inet_pton (AF_INET6, s, &value_aaaa)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse IPv6 address `%s'\n"), - s); - return GNUNET_SYSERR; - } - *data = GNUNET_new (struct in6_addr); - *data_size = sizeof(struct in6_addr); - GNUNET_memcpy (*data, &value_aaaa, sizeof(value_aaaa)); - return GNUNET_OK; - - case GNUNET_DNSPARSER_TYPE_TLSA: { - unsigned int usage; - unsigned int selector; - unsigned int matching_type; - size_t slen = strlen (s) + 1; - char hex[slen]; - - if (4 != sscanf (s, "%u %u %u %s", &usage, &selector, &matching_type, - hex)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse TLSA record string `%s'\n"), - s); - *data_size = 0; - return GNUNET_SYSERR; - } - - *data_size = sizeof(struct GNUNET_TUN_DnsTlsaRecord) + strlen (hex) / 2; - *data = tlsa = GNUNET_malloc (*data_size); - tlsa->usage = (uint8_t) usage; - tlsa->selector = (uint8_t) selector; - tlsa->matching_type = (uint8_t) matching_type; - if (strlen (hex) / 2 != GNUNET_DNSPARSER_hex_to_bin (hex, &tlsa[1])) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse TLSA record string `%s'\n"), - s); - GNUNET_free (*data); - *data = NULL; - *data_size = 0; - return GNUNET_SYSERR; - } - return GNUNET_OK; - } - - case GNUNET_DNSPARSER_TYPE_CAA: { // RFC6844 - struct GNUNET_DNSPARSER_CaaRecord *caa; - unsigned int flags; - char tag[15]; // Max tag length 15 - char value[strlen (s) + 1]; // Should be more than enough - - if (3 != sscanf (s, "%u %s %[^\n]", &flags, tag, value)) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse CAA record string `%s'\n"), - s); - *data_size = 0; - return GNUNET_SYSERR; - } - *data_size = sizeof(struct GNUNET_DNSPARSER_CaaRecord) + strlen (tag) - + strlen (value); - *data = caa = GNUNET_malloc (*data_size); - caa->flags = flags; - memcpy (&caa[1], tag, strlen (tag)); - caa->tag_len = strlen (tag); - memcpy ((char *) &caa[1] + caa->tag_len, value, strlen (value)); - return GNUNET_OK; - } - - default: - return GNUNET_SYSERR; - } -} - - -/** - * Mapping of record type numbers to human-readable - * record type names. - */ -static struct -{ - const char *name; - uint32_t number; -} name_map[] = { { "A", GNUNET_DNSPARSER_TYPE_A }, - { "NS", GNUNET_DNSPARSER_TYPE_NS }, - { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME }, - { "SOA", GNUNET_DNSPARSER_TYPE_SOA }, - { "PTR", GNUNET_DNSPARSER_TYPE_PTR }, - { "MX", GNUNET_DNSPARSER_TYPE_MX }, - { "TXT", GNUNET_DNSPARSER_TYPE_TXT }, - { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA }, - { "SRV", GNUNET_DNSPARSER_TYPE_SRV }, - { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA }, - { "CERT", GNUNET_DNSPARSER_TYPE_CERT }, - { "CAA", GNUNET_DNSPARSER_TYPE_CAA }, - { NULL, UINT32_MAX } }; - - -/** - * Convert a type name (e.g. "AAAA") to the corresponding number. - * - * @param cls closure, unused - * @param dns_typename name to convert - * @return corresponding number, UINT32_MAX on error - */ -static uint32_t -dns_typename_to_number (void *cls, const char *dns_typename) -{ - unsigned int i; - - i = 0; - while ((NULL != name_map[i].name) && - (0 != strcasecmp (dns_typename, name_map[i].name))) - i++; - return name_map[i].number; -} - - -/** - * Convert a type number to the corresponding type string (e.g. 1 to "A") - * - * @param cls closure, unused - * @param type number of a type to convert - * @return corresponding typestring, NULL on error - */ -static const char * -dns_number_to_typename (void *cls, uint32_t type) -{ - unsigned int i; - - i = 0; - while ((NULL != name_map[i].name) && (type != name_map[i].number)) - i++; - return name_map[i].name; -} - - -static enum GNUNET_GenericReturnValue -dns_is_critical (void *cls, uint32_t type) -{ - return GNUNET_NO; -} - -/** - * Entry point for the plugin. - * - * @param cls NULL - * @return the exported block API - */ -void * -libgnunet_plugin_gnsrecord_dns_init (void *cls) -{ - struct GNUNET_GNSRECORD_PluginFunctions *api; - - api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions); - api->value_to_string = &dns_value_to_string; - api->string_to_value = &dns_string_to_value; - api->typename_to_number = &dns_typename_to_number; - api->number_to_typename = &dns_number_to_typename; - api->is_critical = &dns_is_critical; - return api; -} - - -/** - * Exit point from the plugin. - * - * @param cls the return value from #libgnunet_plugin_block_test_init - * @return NULL - */ -void * -libgnunet_plugin_gnsrecord_dns_done (void *cls) -{ - struct GNUNET_GNSRECORD_PluginFunctions *api = cls; - - GNUNET_free (api); - return NULL; -} - - -/* end of plugin_gnsrecord_dns.c */ diff --git a/src/gnsrecord/test_gnsrecord_block_expiration.c b/src/gnsrecord/test_gnsrecord_block_expiration.c deleted file mode 100644 index 3ef02e631..000000000 --- a/src/gnsrecord/test_gnsrecord_block_expiration.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2013 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ -/** - * @file gnsrecord/test_gnsrecord_crypto.c - * @brief testcase for block creation, verification and decryption - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_gnsrecord_lib.h" - -#define RECORDS 5 - -#define TEST_RECORD_TYPE 1234 - -#define TEST_RECORD_DATALEN 123 - -#define TEST_RECORD_DATA 'a' - -#define TEST_REMOVE_RECORD_TYPE 4321 - -#define TEST_REMOVE_RECORD_DATALEN 255 - -#define TEST_REMOVE_RECORD_DATA 'b' - -static int res; - - -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 expiration_abs; - struct GNUNET_TIME_Absolute expiration_abs_shadow; - - expiration_abs.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - + GNUNET_TIME_UNIT_SECONDS.rel_value_us; - expiration_abs_shadow.abs_value_us = GNUNET_TIME_absolute_get ().abs_value_us - + GNUNET_TIME_UNIT_MINUTES.rel_value_us; - - /* create record */ - rd[0].expiration_time = expiration_abs.abs_value_us; - rd[0].record_type = TEST_RECORD_TYPE; - rd[0].data_size = TEST_RECORD_DATALEN; - rd[0].data = GNUNET_malloc (TEST_RECORD_DATALEN); - rd[0].flags = GNUNET_GNSRECORD_RF_NONE; - memset ((char *) rd[0].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); - - rd[1].expiration_time = expiration_abs.abs_value_us; - rd[1].record_type = TEST_RECORD_TYPE; - rd[1].data_size = TEST_RECORD_DATALEN; - rd[1].data = GNUNET_malloc (TEST_RECORD_DATALEN); - rd[1].flags = GNUNET_GNSRECORD_RF_NONE; - memset ((char *) rd[1].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); - - GNUNET_assert (expiration_abs.abs_value_us == - GNUNET_GNSRECORD_record_get_expiration_time (2, - rd, - GNUNET_TIME_UNIT_ZERO_ABS).abs_value_us); - - rd[1].expiration_time = expiration_abs_shadow.abs_value_us; - rd[1].record_type = TEST_RECORD_TYPE; - rd[1].data_size = TEST_RECORD_DATALEN; - GNUNET_free (rd[1].data); - rd[1].data = GNUNET_malloc (TEST_RECORD_DATALEN); - rd[1].flags = GNUNET_GNSRECORD_RF_SHADOW; - memset ((char *) rd[1].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); - - GNUNET_assert (expiration_abs_shadow.abs_value_us == - GNUNET_GNSRECORD_record_get_expiration_time (2, - rd, - GNUNET_TIME_UNIT_ZERO_ABS).abs_value_us); - GNUNET_free (rd[0].data); - GNUNET_free (rd[1].data); - res = 0; -} - - -int -main (int argc, char *argv[]) -{ - static char *const argvx[] = { "test-gnsrecord-crypto", - NULL }; - static struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - res = 1; - GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1, argvx, - "test-namestore-api", - "nohelp", options, &run, &res); - return res; -} - - -/* end of test_gnsrecord_crypto.c */ diff --git a/src/gnsrecord/test_gnsrecord_crypto.c b/src/gnsrecord/test_gnsrecord_crypto.c deleted file mode 100644 index 92a7a9f1f..000000000 --- a/src/gnsrecord/test_gnsrecord_crypto.c +++ /dev/null @@ -1,207 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2013 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ -/** - * @file gnsrecord/test_gnsrecord_crypto.c - * @brief testcase for block creation, verification and decryption - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_gnsrecord_lib.h" - -#define RECORDS 5 - -#define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT - -#define TEST_RECORD_DATALEN 123 - -#define TEST_RECORD_DATA 'a' - -#define TEST_REMOVE_RECORD_TYPE 4321 - -#define TEST_REMOVE_RECORD_DATALEN 255 - -#define TEST_REMOVE_RECORD_DATA 'b' - - -static struct GNUNET_GNSRECORD_Data *s_rd; - -static char *s_name; - -static int res; - - -static struct GNUNET_GNSRECORD_Data * -create_record (int count) -{ - struct GNUNET_GNSRECORD_Data *rd; - - rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data); - for (unsigned int c = 0; c < count; c++) - { - rd[c].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us - + 1000000000; - rd[c].record_type = TEST_RECORD_TYPE; - rd[c].data_size = TEST_RECORD_DATALEN; - rd[c].data = GNUNET_malloc (TEST_RECORD_DATALEN); - memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); - } - return rd; -} - - -static void -rd_decrypt_cb (void *cls, - unsigned int rd_count, - const struct GNUNET_GNSRECORD_Data *rd) -{ - char rd_cmp_data[TEST_RECORD_DATALEN]; - - GNUNET_assert (RECORDS == rd_count); - GNUNET_assert (NULL != rd); - memset (rd_cmp_data, - 'a', - TEST_RECORD_DATALEN); - for (unsigned int c = 0; c < rd_count; c++) - { - GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type); - GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size); - GNUNET_assert (0 == memcmp (&rd_cmp_data, - rd[c].data, - TEST_RECORD_DATALEN)); - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Block was decrypted successfully \n"); - res = 0; -} - - -static void -test_with_type (struct GNUNET_IDENTITY_PrivateKey *privkey) -{ - struct GNUNET_GNSRECORD_Block *block; - struct GNUNET_IDENTITY_PublicKey pubkey; - struct GNUNET_HashCode query_pub; - struct GNUNET_HashCode query_priv; - struct GNUNET_HashCode query_block; - struct GNUNET_TIME_Absolute expire = GNUNET_TIME_UNIT_FOREVER_ABS; - - - /* get public key */ - GNUNET_IDENTITY_key_get_public (privkey, - &pubkey); - - /* test query derivation */ - GNUNET_GNSRECORD_query_from_private_key (privkey, - "testlabel", - &query_priv); - GNUNET_GNSRECORD_query_from_public_key (&pubkey, - "testlabel", - &query_pub); - GNUNET_assert (0 == memcmp (&query_priv, - &query_pub, - sizeof(struct GNUNET_HashCode))); - /* create record */ - s_name = "testlabel"; - s_rd = create_record (RECORDS); - - /* Create block */ - GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create (privkey, - expire, - s_name, - s_rd, - RECORDS, - &block)); - GNUNET_assert (GNUNET_OK == - GNUNET_GNSRECORD_query_from_block (block, - &query_block)); - GNUNET_assert (0 == memcmp (&query_pub, - &query_block, - sizeof(struct GNUNET_HashCode))); - - GNUNET_assert (GNUNET_OK == - GNUNET_GNSRECORD_block_verify (block)); - GNUNET_assert (GNUNET_OK == - GNUNET_GNSRECORD_block_decrypt (block, - &pubkey, - s_name, - &rd_decrypt_cb, - NULL)); - for (int i = 0; i < RECORDS; i++) GNUNET_free(s_rd[i].data); - GNUNET_free (s_rd); - GNUNET_free (block); -} - - -static void -run (void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) -{ - struct GNUNET_IDENTITY_PrivateKey privkey; - struct GNUNET_IDENTITY_PrivateKey privkey_ed; - struct GNUNET_TIME_Absolute start; - struct GNUNET_TIME_Absolute end; - - - privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); - GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); - start = GNUNET_TIME_absolute_get (); - test_with_type (&privkey); - end = GNUNET_TIME_absolute_get (); - printf ("Time: %llu ms\n", (unsigned long long) - GNUNET_TIME_absolute_get_difference (start, - end).rel_value_us); - - privkey_ed.type = htonl (GNUNET_GNSRECORD_TYPE_EDKEY); - GNUNET_CRYPTO_eddsa_key_create (&privkey_ed.eddsa_key); - start = GNUNET_TIME_absolute_get (); - test_with_type (&privkey_ed); - end = GNUNET_TIME_absolute_get (); - printf ("Time: %llu ms\n", (unsigned long long) - GNUNET_TIME_absolute_get_difference (start, - end).rel_value_us); - - -} - - -int -main (int argc, char *argv[]) -{ - static char *const argvx[] = { - "test-gnsrecord-crypto", - NULL - }; - static struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - res = 1; - GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1, - argvx, - "test-gnsrecord-crypto", - "nohelp", options, - &run, &res); - return res; -} - - -/* end of test_gnsrecord_crypto.c */ diff --git a/src/gnsrecord/test_gnsrecord_serialization.c b/src/gnsrecord/test_gnsrecord_serialization.c deleted file mode 100644 index b06b3a0fe..000000000 --- a/src/gnsrecord/test_gnsrecord_serialization.c +++ /dev/null @@ -1,156 +0,0 @@ -/* - This file is part of GNUnet. - Copyright (C) 2013 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 . - - SPDX-License-Identifier: AGPL3.0-or-later - */ -/** - * @file gnsrecord/test_gnsrecord_serialization.c - * @brief testcase for gnsrecord_serialization.c - */ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_gnsrecord_lib.h" - -#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) - -static int res; - - -static void -run (void *cls, - char *const *args, - const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) -{ - size_t len; - int c; - - int rd_count = 3; - size_t data_len; - struct GNUNET_GNSRECORD_Data src[rd_count]; - - memset (src, '\0', rd_count * sizeof(struct GNUNET_GNSRECORD_Data)); - - data_len = 0; - for (c = 0; c < rd_count; c++) - { - src[c].record_type = GNUNET_DNSPARSER_TYPE_TXT; - src[c].data_size = data_len; - src[c].data = GNUNET_malloc (data_len); - - /* Setting data to data_len * record_type */ - memset ((char *) src[c].data, 'a', data_len); - data_len += 10; - } - res = 0; - - len = GNUNET_GNSRECORD_records_get_size (rd_count, src); - char rd_ser[len]; - GNUNET_assert (len == - GNUNET_GNSRECORD_records_serialize (rd_count, - src, - len, - rd_ser)); - - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Serialized data len: %u\n", - (unsigned int) len); - - GNUNET_assert (rd_ser != NULL); - { - struct GNUNET_GNSRECORD_Data dst[rd_count]; - GNUNET_assert (GNUNET_OK == - GNUNET_GNSRECORD_records_deserialize (len, - rd_ser, - rd_count, - dst)); - - GNUNET_assert (dst != NULL); - - for (c = 0; c < rd_count; c++) - { - if (src[c].data_size != dst[c].data_size) - { - GNUNET_break (0); - res = 1; - } - if (src[c].expiration_time != dst[c].expiration_time) - { - GNUNET_break (0); - res = 1; - } - if (src[c].flags != dst[c].flags) - { - GNUNET_break (0); - res = 1; - } - if (src[c].record_type != dst[c].record_type) - { - GNUNET_break (0); - res = 1; - } - - { - size_t data_size = src[c].data_size; - char data[data_size]; - - memset (data, 'a', data_size); - if (0 != memcmp (data, dst[c].data, data_size)) - { - GNUNET_break (0); - res = 1; - } - if (0 != memcmp (data, src[c].data, data_size)) - { - GNUNET_break (0); - res = 1; - } - if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size)) - { - GNUNET_break (0); - res = 1; - } - } - } - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c); - } - - for (c = 0; c < rd_count; c++) - { - GNUNET_free_nz ((void *) src[c].data); - } -} - - -int -main (int argcx, char *argvx[]) -{ - static char *const argv[] = { "test_gnsrecord_serialization", - NULL }; - static struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - res = 1; - GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv, - "test_namestore_record_serialization", - "nohelp", options, &run, &res); - return res; -} - - -/* end of test_gnsrecord_serialization.c */ diff --git a/src/gnsrecord/test_gnsrecord_testvectors.c b/src/gnsrecord/test_gnsrecord_testvectors.c deleted file mode 100644 index e0d959f0e..000000000 --- a/src/gnsrecord/test_gnsrecord_testvectors.c +++ /dev/null @@ -1,711 +0,0 @@ -#include "platform.h" -#include "gnunet_util_lib.h" -#include "gnunet_gns_service.h" -#include "gnunet_gnsrecord_lib.h" -#include -#include "gnsrecord_crypto.h" - -int res; - -struct GnsTv -{ - uint32_t expected_rd_count; - struct GNUNET_GNSRECORD_Data expected_rd[2048]; - char *d; - char *zid; - char *ztld; - char *label; - char *q; - char *rdata; - char *rrblock; - char *k; - char *nonce; -}; - -struct RevocationTv -{ - char *d; - char *zid; - char *ztld; - char *m; - char *proof; - int diff; - int epochs; -}; - -struct RevocationTv rtvs[] = { - { - .d = - "70 ed 98 b9 07 8c 47 f7" - "d5 78 3b 26 cc f9 8b 7d" - "d5 5f 60 88 d1 53 95 97" - "fa 8b f5 5a c0 32 ea 6f", - .zid = - "00 01 00 00 2c a2 23 e8" - "79 ec c4 bb de b5 da 17" - "31 92 81 d6 3b 2e 3b 69" - "55 f1 c3 77 5c 80 4a 98" - "d5 f8 dd aa", - .ztld = - "000G001CM8HYGYFCRJXXXDET2WRS50EP7CQ3PTANY71QEQ409ACDBY6XN8", - .m = - "00 00 00 34 00 00 00 03" - "00 05 fe b4 6d 86 5c 1c" - "00 01 00 00 2c a2 23 e8" - "79 ec c4 bb de b5 da 17" - "31 92 81 d6 3b 2e 3b 69" - "55 f1 c3 77 5c 80 4a 98" - "d5 f8 dd aa", - .proof = - "00 05 fe b4 6d 86 5c 1c" - "00 00 39 5d 18 27 c0 00" - "e6 6a 57 0b cc d4 b3 93" - "e6 6a 57 0b cc d4 b3 ea" - "e6 6a 57 0b cc d4 b5 36" - "e6 6a 57 0b cc d4 b5 42" - "e6 6a 57 0b cc d4 b6 13" - "e6 6a 57 0b cc d4 b6 5f" - "e6 6a 57 0b cc d4 b6 72" - "e6 6a 57 0b cc d4 b7 0a" - "e6 6a 57 0b cc d4 b7 1a" - "e6 6a 57 0b cc d4 b7 23" - "e6 6a 57 0b cc d4 b7 47" - "e6 6a 57 0b cc d4 b7 77" - "e6 6a 57 0b cc d4 b7 85" - "e6 6a 57 0b cc d4 b7 89" - "e6 6a 57 0b cc d4 b7 cf" - "e6 6a 57 0b cc d4 b7 dc" - "e6 6a 57 0b cc d4 b9 3a" - "e6 6a 57 0b cc d4 b9 56" - "e6 6a 57 0b cc d4 ba 4a" - "e6 6a 57 0b cc d4 ba 9d" - "e6 6a 57 0b cc d4 bb 28" - "e6 6a 57 0b cc d4 bb 5a" - "e6 6a 57 0b cc d4 bb 92" - "e6 6a 57 0b cc d4 bb a2" - "e6 6a 57 0b cc d4 bb d8" - "e6 6a 57 0b cc d4 bb e2" - "e6 6a 57 0b cc d4 bc 93" - "e6 6a 57 0b cc d4 bc 94" - "e6 6a 57 0b cc d4 bd 0f" - "e6 6a 57 0b cc d4 bd ce" - "e6 6a 57 0b cc d4 be 6a" - "e6 6a 57 0b cc d4 be 73" - "00 01 00 00 2c a2 23 e8" - "79 ec c4 bb de b5 da 17" - "31 92 81 d6 3b 2e 3b 69" - "55 f1 c3 77 5c 80 4a 98" - "d5 f8 dd aa 04 4a 87 8a" - "15 8b 40 f0 c8 41 d9 f9" - "78 cb 13 72 ea ee 51 99" - "a3 d8 7e 5e 2b db c7 2a" - "6c 8c 73 d0 00 18 1d fc" - "39 c3 aa a4 81 66 7b 16" - "5b 58 44 e4 50 71 3d 8a" - "b6 a3 b2 ba 8f ef 44 7b" - "65 07 6a 0f", - .diff = 5, - .epochs = 2 - } -}; - -struct GnsTv tvs[] = { - { .d = - "50 d7 b6 52 a4 ef ea df" - "f3 73 96 90 97 85 e5 95" - "21 71 a0 21 78 c8 e7 d4" - "50 fa 90 79 25 fa fd 98", - .zid = - "00 01 00 00 67 7c 47 7d" - "2d 93 09 7c 85 b1 95 c6" - "f9 6d 84 ff 61 f5 98 2c" - "2c 4f e0 2d 5a 11 fe df" - "b0 c2 90 1f", - .ztld = "000G0037FH3QTBCK15Y8BCCNRVWPV17ZC7TSGB1C9ZG2TPGHZVFV1GMG3W", - .label = "74 65 73 74 64 65 6c 65" - "67 61 74 69 6f 6e", - .q = - "4a dc 67 c5 ec ee 9f 76" - "98 6a bd 71 c2 22 4a 3d" - "ce 2e 91 70 26 c9 a0 9d" - "fd 44 ce f3 d2 0f 55 a2" - "73 32 72 5a 6c 8a fb bb" - "b0 f7 ec 9a f1 cc 42 64" - "12 99 40 6b 04 fd 9b 5b" - "57 91 f8 6c 4b 08 d5 f4", - .nonce = - "e9 0a 00 61 00 1c ee 8c" - "10 e2 59 80 00 00 00 01", - .k = - "86 4e 71 38 ea e7 fd 91" - "a3 01 36 89 9c 13 2b 23" - "ac eb db 2c ef 43 cb 19" - "f6 bf 55 b6 7d b9 b3 b3", - .rdata = - "00 1c ee 8c 10 e2 59 80" - "00 20 00 01 00 01 00 00" - "21 e3 b3 0f f9 3b c6 d3" - "5a c8 c6 e0 e1 3a fd ff" - "79 4c b7 b4 4b bb c7 48" - "d2 59 d0 a0 28 4d be 84", - .rrblock = - "00 00 00 a0 00 01 00 00" - "18 2b b6 36 ed a7 9f 79" - "57 11 bc 27 08 ad bb 24" - "2a 60 44 6a d3 c3 08 03" - "12 1d 03 d3 48 b7 ce b6" - "0a d1 0b c1 3b 40 3b 5b" - "25 61 26 b2 14 5a 6f 60" - "c5 14 f9 51 ff a7 66 f7" - "a3 fd 4b ac 4a 4e 19 90" - "05 5c b8 7e 8d 1b fd 19" - "aa 09 a4 29 f7 29 e9 f5" - "c6 ee c2 47 0a ce e2 22" - "07 59 e9 e3 6c 88 6f 35" - "00 1c ee 8c 10 e2 59 80" - "0c 1e da 5c c0 94 a1 c7" - "a8 88 64 9d 25 fa ee bd" - "60 da e6 07 3d 57 d8 ae" - "8d 45 5f 4f 13 92 c0 74" - "e2 6a c6 69 bd ee c2 34" - "62 b9 62 95 2c c6 e9 eb"}, - { .d = - "50 d7 b6 52 a4 ef ea df" - "f3 73 96 90 97 85 e5 95" - "21 71 a0 21 78 c8 e7 d4" - "50 fa 90 79 25 fa fd 98", - .zid = - "00 01 00 00 67 7c 47 7d" - "2d 93 09 7c 85 b1 95 c6" - "f9 6d 84 ff 61 f5 98 2c" - "2c 4f e0 2d 5a 11 fe df" - "b0 c2 90 1f", - .ztld = "000G0037FH3QTBCK15Y8BCCNRVWPV17ZC7TSGB1C9ZG2TPGHZVFV1GMG3W", - .label = - "e5 a4 a9 e4 b8 8b e7 84" - "a1 e6 95 b5", - .nonce = - "ee 96 33 c1 00 1c ee 8c" - "10 e2 59 80 00 00 00 01", - .k = - "fb 3a b5 de 23 bd da e1" - "99 7a af 7b 92 c2 d2 71" - "51 40 8b 77 af 7a 41 ac" - "79 05 7c 4d f5 38 3d 01", - .q = - "af f0 ad 6a 44 09 73 68" - "42 9a c4 76 df a1 f3 4b" - "ee 4c 36 e7 47 6d 07 aa" - "64 63 ff 20 91 5b 10 05" - "c0 99 1d ef 91 fc 3e 10" - "90 9f 87 02 c0 be 40 43" - "67 78 c7 11 f2 ca 47 d5" - "5c f0 b5 4d 23 5d a9 77", - .rdata = - "00 1c ee 8c 10 e2 59 80" - "00 10 00 00 00 00 00 1c" - "00 00 00 00 00 00 00 00" - "00 00 00 00 de ad be ef" - "00 3f f2 aa 54 08 db 40" - "00 06 00 00 00 01 00 01" - "e6 84 9b e7 a7 b0 00 28" - "bb 13 ff 37 19 40 00 0b" - "00 04 00 00 00 10 48 65" - "6c 6c 6f 20 57 6f 72 6c" - "64 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00", - .rrblock = - "00 00 00 f0 00 01 00 00" - "a5 12 96 df 75 7e e2 75" - "ca 11 8d 4f 07 fa 7a ae" - "55 08 bc f5 12 aa 41 12" - "14 29 d4 a0 de 9d 05 7e" - "08 5b d6 5f d4 85 10 51" - "ba ce 2a 45 2a fc 8a 7e" - "4f 6b 2c 1f 74 f0 20 35" - "d9 64 1a cd ba a4 66 e0" - "00 ce d6 f2 d2 3b 63 1c" - "8e 8a 0b 38 e2 ba e7 9a" - "22 ca d8 1d 4c 50 d2 25" - "35 8e bc 17 ac 0f 89 9e" - "00 1c ee 8c 10 e2 59 80" - "d8 c2 8d 2f d6 96 7d 1a" - "b7 22 53 f2 10 98 b8 14" - "a4 10 be 1f 59 98 de 03" - "f5 8f 7e 7c db 7f 08 a6" - "16 51 be 4d 0b 6f 8a 61" - "df 15 30 44 0b d7 47 dc" - "f0 d7 10 4f 6b 8d 24 c2" - "ac 9b c1 3d 9c 6f e8 29" - "05 25 d2 a6 d0 f8 84 42" - "67 a1 57 0e 8e 29 4d c9" - "3a 31 9f cf c0 3e a2 70" - "17 d6 fd a3 47 b4 a7 94" - "97 d7 f6 b1 42 2d 4e dd" - "82 1c 19 93 4e 96 c1 aa" - "87 76 57 25 d4 94 c7 64" - "b1 55 dc 6d 13 26 91 74"}, - { .d = - "5a f7 02 0e e1 91 60 32" - "88 32 35 2b bc 6a 68 a8" - "d7 1a 7c be 1b 92 99 69" - "a7 c6 6d 41 5a 0d 8f 65", - .zid = - "00 01 00 14 3c f4 b9 24" - "03 20 22 f0 dc 50 58 14" - "53 b8 5d 93 b0 47 b6 3d" - "44 6c 58 45 cb 48 44 5d" - "db 96 68 8f", - .ztld = "000G051WYJWJ80S04BRDRM2R2H9VGQCKP13VCFA4DHC4BJT88HEXQ5K8HW", - .label = - "74 65 73 74 64 65 6c 65" - "67 61 74 69 6f 6e", - .nonce = - "98 13 2e a8 68 59 d3 5c" - "88 bf d3 17 fa 99 1b cb" - "00 1c ee 8c 10 e2 59 80", - .k = - "85 c4 29 a9 56 7a a6 33" - "41 1a 96 91 e9 09 4c 45" - "28 16 72 be 58 60 34 aa" - "e4 a2 a2 cc 71 61 59 e2", - .q = - "ab aa ba c0 e1 24 94 59" - "75 98 83 95 aa c0 24 1e" - "55 59 c4 1c 40 74 e2 55" - "7b 9f e6 d1 54 b6 14 fb" - "cd d4 7f c7 f5 1d 78 6d" - "c2 e0 b1 ec e7 60 37 c0" - "a1 57 8c 38 4e c6 1d 44" - "56 36 a9 4e 88 03 29 e9", - .rdata = - "00 1c ee 8c 10 e2 59 80" - "00 20 00 01 00 01 00 00" - "21 e3 b3 0f f9 3b c6 d3" - "5a c8 c6 e0 e1 3a fd ff" - "79 4c b7 b4 4b bb c7 48" - "d2 59 d0 a0 28 4d be 84", - .rrblock = - "00 00 00 b0 00 01 00 14" - "9b f2 33 19 8c 6d 53 bb" - "db ac 49 5c ab d9 10 49" - "a6 84 af 3f 40 51 ba ca" - "b0 dc f2 1c 8c f2 7a 1a" - "9f 56 a8 86 ea 73 9d 59" - "17 50 8f 9b 75 56 39 f3" - "a9 ac fa ed ed ca 7f bf" - "a7 94 b1 92 e0 8b f9 ed" - "4c 7e c8 59 4c 9f 7b 4e" - "19 77 4f f8 38 ec 38 7a" - "8f 34 23 da ac 44 9f 59" - "db 4e 83 94 3f 90 72 00" - "00 1c ee 8c 10 e2 59 80" - "57 7c c6 c9 5a 14 e7 04" - "09 f2 0b 01 67 e6 36 d0" - "10 80 7c 4f 00 37 2d 69" - "8c 82 6b d9 2b c2 2b d6" - "bb 45 e5 27 7c 01 88 1d" - "6a 43 60 68 e4 dd f1 c6" - "b7 d1 41 6f af a6 69 7c" - "25 ed d9 ea e9 91 67 c3"}, - { .d = - "5a f7 02 0e e1 91 60 32" - "88 32 35 2b bc 6a 68 a8" - "d7 1a 7c be 1b 92 99 69" - "a7 c6 6d 41 5a 0d 8f 65", - .zid = - "00 01 00 14 3c f4 b9 24" - "03 20 22 f0 dc 50 58 14" - "53 b8 5d 93 b0 47 b6 3d" - "44 6c 58 45 cb 48 44 5d" - "db 96 68 8f", - .ztld = "000G051WYJWJ80S04BRDRM2R2H9VGQCKP13VCFA4DHC4BJT88HEXQ5K8HW", - .label = - "e5 a4 a9 e4 b8 8b e7 84" - "a1 e6 95 b5", - .nonce = - "bb 0d 3f 0f bd 22 42 77" - "50 da 5d 69 12 16 e6 c9" - "00 1c ee 8c 10 e2 59 80", - .k = - "3d f8 05 bd 66 87 aa 14" - "20 96 28 c2 44 b1 11 91" - "88 c3 92 56 37 a4 1e 5d" - "76 49 6c 29 45 dc 37 7b", - .q = - "ba f8 21 77 ee c0 81 e0" - "74 a7 da 47 ff c6 48 77" - "58 fb 0d f0 1a 6c 7f bb" - "52 fc 8a 31 be f0 29 af" - "74 aa 0d c1 5a b8 e2 fa" - "7a 54 b4 f5 f6 37 f6 15" - "8f a7 f0 3c 3f ce be 78" - "d3 f9 d6 40 aa c0 d1 ed", - .rdata = - "00 1c ee 8c 10 e2 59 80" - "00 10 00 00 00 00 00 1c" - "00 00 00 00 00 00 00 00" - "00 00 00 00 de ad be ef" - "00 3f f2 aa 54 08 db 40" - "00 06 00 00 00 01 00 01" - "e6 84 9b e7 a7 b0 00 28" - "bb 13 ff 37 19 40 00 0b" - "00 04 00 00 00 10 48 65" - "6c 6c 6f 20 57 6f 72 6c" - "64 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00" - "00 00 00 00 00 00 00 00", - .rrblock = - "00 00 01 00 00 01 00 14" - "74 f9 00 68 f1 67 69 53" - "52 a8 a6 c2 eb 98 48 98" - "c5 3a cc a0 98 04 70 c6" - "c8 12 64 cb dd 78 ad 11" - "75 6d 2c 15 7a d2 ea 4f" - "c0 b1 b9 1c 08 03 79 44" - "61 d3 de f2 0d d1 63 6c" - "fe dc 03 89 c5 49 d1 43" - "6c c3 5b 4e 1b f8 89 5a" - "64 6b d9 a6 f4 6b 83 48" - "1d 9c 0e 91 d4 e1 be bb" - "6a 83 52 6f b7 25 2a 06" - "00 1c ee 8c 10 e2 59 80" - "4e b3 5a 50 d4 0f e1 a4" - "29 c7 f4 b2 67 a0 59 de" - "4e 2c 8a 89 a5 ed 53 d3" - "d4 92 58 59 d2 94 9f 7f" - "30 d8 a2 0c aa 96 f8 81" - "45 05 2d 1c da 04 12 49" - "8f f2 5f f2 81 6e f0 ce" - "61 fe 69 9b fa c7 2c 15" - "dc 83 0e a9 b0 36 17 1c" - "cf ca bb dd a8 de 3c 86" - "ed e2 95 70 d0 17 4b 82" - "82 09 48 a9 28 b7 f0 0e" - "fb 40 1c 10 fe 80 bb bb" - "02 76 33 1b f7 f5 1b 8d" - "74 57 9c 14 14 f2 2d 50" - "1a d2 5a e2 49 f5 bb f2" - "a6 c3 72 59 d1 75 e4 40" - "b2 94 39 c6 05 19 cb b1"}, - {.d = NULL} -}; - -static void -print_bytes_ (void *buf, - size_t buf_len, - int fold, - int in_be) -{ - int i; - - for (i = 0; i < buf_len; i++) - { - if (0 != i) - { - if ((0 != fold) && (i % fold == 0)) - printf ("\n "); - else - printf (" "); - } - else - { - printf (" "); - } - if (in_be) - printf ("%02x", ((unsigned char*) buf)[buf_len - 1 - i]); - else - printf ("%02x", ((unsigned char*) buf)[i]); - } - printf ("\n"); -} - - -static void -print_bytes (void *buf, - size_t buf_len, - int fold) -{ - print_bytes_ (buf, buf_len, fold, 0); -} - - -int -parsehex (char *src, char *dst, size_t dstlen, int invert) -{ - int off; - int read_byte; - int data_len = 0; - char data[strlen (src) + 1]; - char *pos = data; - int i = 0; - int j = 0; - memset (data, 0, strlen (src) + 1); - - for (i = 0; i < strlen (src); i++) - { - if ((src[i] == ' ') || (src[i] == '\n')) - continue; - data[j++] = src[i]; - } - - while (sscanf (pos, " %02x%n", &read_byte, &off) == 1) - { - if (invert) - dst[dstlen - 1 - data_len++] = read_byte; - else - dst[data_len++] = read_byte; - pos += off; - } - return data_len; -} - - -void -res_checker (void *cls, - unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd) -{ - struct GnsTv *tv = cls; - if (rd_count != tv->expected_rd_count) - { - printf ("FAIL: Record count expected: %u, was: %u\n", tv->expected_rd_count, - rd_count); - res = 1; - return; - } - for (int i = 0; i < rd_count; i++) - { - if (rd[i].record_type != tv->expected_rd[i].record_type) - { - printf ("FAIL: Record type expected: %u, was: %u\n", - tv->expected_rd[i].record_type, - rd[i].record_type); - res = 1; - return; - } - if (rd[i].expiration_time != tv->expected_rd[i].expiration_time) - { - printf ("FAIL: Expiration expected: %" PRIu64 ", was: %" PRIu64 "\n", - tv->expected_rd[i].expiration_time, - rd[i].expiration_time); - res = 1; - return; - } - if (rd[i].flags != tv->expected_rd[i].flags) - { - printf ("FAIL: Record flags expected: %u, was: %u\n", - tv->expected_rd[i].flags, - rd[i].flags); - res = 1; - return; - } - if (rd[i].data_size != tv->expected_rd[i].data_size) - { - printf ("FAIL: Record data size expected: %lu, was: %lu\n", - tv->expected_rd[i].data_size, - rd[i].data_size); - res = 1; - return; - } - if (0 != memcmp (rd[i].data, tv->expected_rd[i].data, - rd[i].data_size)) - { - printf ("FAIL: Record data does not match\n"); - res = 1; - return; - } - } -} - - -enum GNUNET_GenericReturnValue -check_derivations_edkey (const char*label, - struct GNUNET_TIME_Absolute expire, - struct GNUNET_IDENTITY_PublicKey *pub, - struct GnsTv *tv) -{ - unsigned char nonce[crypto_secretbox_NONCEBYTES]; - unsigned char skey[crypto_secretbox_KEYBYTES]; - unsigned char nonce_expected[crypto_secretbox_NONCEBYTES]; - unsigned char skey_expected[crypto_secretbox_KEYBYTES]; - - - parsehex (tv->nonce,(char*) nonce_expected, crypto_secretbox_NONCEBYTES, 0); - parsehex (tv->k,(char*) skey_expected, crypto_secretbox_KEYBYTES, 0); - GNR_derive_block_xsalsa_key (nonce, - skey, - label, - GNUNET_TIME_absolute_hton ( - expire).abs_value_us__, - &pub->eddsa_key); - /* Ignore random 128-bit nonce, can't check this here. Will be checked on - * decryption. */ - if (0 != memcmp (nonce + 16, nonce_expected + 16, sizeof (nonce) - 16)) - { - printf ("FAIL: Failed to derive nonce:\n"); - print_bytes (nonce, sizeof (nonce), 8); - print_bytes (nonce_expected, sizeof (nonce), 8); - return GNUNET_NO; - } - if (0 != memcmp (skey, skey_expected, sizeof (skey))) - { - printf ("FAIL: Failed to derive secret key\n"); - return GNUNET_NO; - } - return GNUNET_OK; -} - - -enum GNUNET_GenericReturnValue -check_derivations_pkey (const char*label, - struct GNUNET_TIME_Absolute expire, - struct GNUNET_IDENTITY_PublicKey *pub, - struct GnsTv *tv) -{ - unsigned char ctr[GNUNET_CRYPTO_AES_KEY_LENGTH / 2]; - unsigned char ctr_expected[GNUNET_CRYPTO_AES_KEY_LENGTH / 2]; - unsigned char skey[GNUNET_CRYPTO_AES_KEY_LENGTH]; - unsigned char skey_expected[GNUNET_CRYPTO_AES_KEY_LENGTH]; - - parsehex (tv->nonce,(char*) ctr_expected, sizeof (ctr), 0); - parsehex (tv->k,(char*) skey_expected, sizeof (skey), 0); - GNR_derive_block_aes_key (ctr, - skey, - label, - GNUNET_TIME_absolute_hton ( - expire).abs_value_us__, - &pub->ecdsa_key); - - /* Ignore random 32-bit nonce, can't check this here. Will be checked on - * decryption. */ - if (0 != memcmp (ctr + 4, ctr_expected + 4, sizeof (ctr) - 4)) - { - printf ("FAIL: Failed to derive nonce\n"); - return GNUNET_NO; - } - if (0 != memcmp (skey, skey_expected, sizeof (skey))) - { - printf ("FAIL: Failed to derive secret key\n"); - return GNUNET_NO; - } - return GNUNET_OK; -} - - -int -main () -{ - struct GNUNET_IDENTITY_PrivateKey priv; - struct GNUNET_IDENTITY_PublicKey pub; - struct GNUNET_IDENTITY_PublicKey pub_parsed; - struct GNUNET_GNSRECORD_Block *rrblock; - struct GNUNET_HashCode query; - struct GNUNET_HashCode expected_query; - struct GNUNET_TIME_Absolute expire; - struct GNUNET_TIME_Relative exprel; - struct GNUNET_REVOCATION_PowP *pow; - char label[128]; - char rdata[8096]; - char ztld[128]; - res = 0; - - for (int i = 0; NULL != tvs[i].d; i++) - { - printf ("Test vector #%d\n", i); - memset (label, 0, sizeof (label)); - parsehex (tvs[i].zid,(char*) &pub_parsed, 36, 0); - parsehex (tvs[i].d,(char*) &priv.ecdsa_key, sizeof (priv.ecdsa_key), - (GNUNET_GNSRECORD_TYPE_PKEY == ntohl (pub_parsed.type)) ? 1 : 0); - priv.type = pub_parsed.type; - GNUNET_IDENTITY_key_get_public (&priv, &pub); - if (0 != memcmp (&pub, &pub_parsed, GNUNET_IDENTITY_public_key_get_length ( - &pub))) - { - printf ("Wrong pubkey.\n"); - print_bytes (&pub, 36, 8); - print_bytes (&pub_parsed, 36, 8); - res = 1; - break; - } - GNUNET_STRINGS_data_to_string (&pub, - GNUNET_IDENTITY_public_key_get_length ( - &pub), - ztld, - sizeof (ztld)); - if (0 != strcmp (ztld, tvs[i].ztld)) - { - printf ("Wrong zTLD: expected %s, got %s\n", tvs[i].ztld, ztld); - res = 1; - break; - } - rrblock = GNUNET_malloc (strlen (tvs[i].rrblock)); - parsehex (tvs[i].rrblock, (char*) rrblock, 0, 0); - parsehex (tvs[i].label, (char*) label, 0, 0); - parsehex (tvs[i].q, (char*) &query, 0, 0); - GNUNET_GNSRECORD_query_from_public_key (&pub_parsed, - label, - &expected_query); - if (0 != GNUNET_memcmp (&query, &expected_query)) - { - printf ("FAIL: query does not match:"); - printf (" expected: %s", GNUNET_h2s (&expected_query)); - printf (", was: %s\n", GNUNET_h2s (&query)); - res = 1; - break; - } - int len = parsehex (tvs[i].rdata, (char*) rdata, 0, 0); - tvs[i].expected_rd_count = - GNUNET_GNSRECORD_records_deserialize_get_size (len, - rdata); - GNUNET_assert (tvs[i].expected_rd_count < 2048); - if (GNUNET_OK != - GNUNET_GNSRECORD_records_deserialize (len, - rdata, - tvs[i].expected_rd_count, - tvs[i].expected_rd)) - { - printf ("FAIL: Deserialization of RDATA failed\n"); - res = 1; - break; - } - expire = GNUNET_GNSRECORD_record_get_expiration_time ( - tvs[i].expected_rd_count, - tvs[i].expected_rd, - GNUNET_TIME_UNIT_ZERO_ABS); - if ((GNUNET_GNSRECORD_TYPE_PKEY == ntohl (pub.type)) && - (GNUNET_OK != check_derivations_pkey (label, expire, &pub, &tvs[i]))) - { - res = 1; - break; - } - else if ((GNUNET_GNSRECORD_TYPE_EDKEY == ntohl (pub.type)) && - (GNUNET_OK != check_derivations_edkey (label, expire, &pub, - &tvs[i]))) - { - res = 1; - break; - } - if (GNUNET_OK != GNUNET_GNSRECORD_block_decrypt (rrblock, - &pub_parsed, - label, - &res_checker, - &tvs[i])) - { - printf ("FAIL: Decryption of RRBLOCK failed\n"); - res = 1; - break; - } - if (0 != res) - break; - printf ("Good.\n"); - } -finish: - return res; -} -- cgit v1.2.3