From ea1d3fd9f54879992fb997a1e94cfd6830085c4b Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Thu, 22 Oct 2020 19:09:12 +0200 Subject: -more gnsrecord API --- src/gns/gnunet-gns-import.c | 9 ++++-- src/gns/gnunet-service-gns_resolver.c | 28 ++++++++++++------ src/gns/plugin_gnsrecord_gns.c | 29 +++++++++++++------ src/gnsrecord/gnsrecord_misc.c | 47 +++++++++++++++++++++++++++++++ src/gnsrecord/gnunet-gnsrecord-tvg.c | 12 ++++++-- src/include/gnunet_gnsrecord_lib.h | 46 +++++++++++++++++++++++++----- src/namestore/gnunet-namestore-fcfsd.c | 22 ++++++++++++--- src/namestore/gnunet-namestore.c | 10 +++---- src/namestore/plugin_namestore_flat.c | 7 +++-- src/namestore/plugin_namestore_postgres.c | 13 +++++---- src/namestore/plugin_namestore_sqlite.c | 12 ++++---- src/revocation/gnunet-revocation-tvg.c | 14 +++++++-- 12 files changed, 195 insertions(+), 54 deletions(-) diff --git a/src/gns/gnunet-gns-import.c b/src/gns/gnunet-gns-import.c index 78db28cab..972fb49cd 100644 --- a/src/gns/gnunet-gns-import.c +++ b/src/gns/gnunet-gns-import.c @@ -168,12 +168,17 @@ check_pkey (unsigned int rd_len, const struct GNUNET_GNSRECORD_Data *rd, char *pk, int *found_rec) { int i; + struct GNUNET_IDENTITY_PublicKey pubkey; for (i = 0; i < rd_len; i++) { char *s; - if ((GNUNET_GNSRECORD_TYPE_PKEY != rd[i].record_type) || - (rd[i].data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) ) + if (sizeof (uint32_t) > rd[i].data_size) + continue; + if (GNUNET_OK != GNUNET_GNSRECORD_identity_from_data (rd[i].data, + rd[i].data_size, + rd[i].record_type, + &pubkey)) continue; s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, rd[i].data, diff --git a/src/gns/gnunet-service-gns_resolver.c b/src/gns/gnunet-service-gns_resolver.c index 1c13f4df1..d9d0d3099 100644 --- a/src/gns/gnunet-service-gns_resolver.c +++ b/src/gns/gnunet-service-gns_resolver.c @@ -1699,8 +1699,10 @@ recursive_pkey_resolution (struct GNS_ResolverHandle *rh, struct GNUNET_IDENTITY_PublicKey auth; /* delegation to another zone */ - if (GNUNET_OK != GNUNET_GNSRECORD_record_to_identity_key (rd, - &auth)) + if (GNUNET_OK != GNUNET_GNSRECORD_identity_from_data (rd->data, + rd->data_size, + rd->record_type, + &auth)) { GNUNET_break_op (0); fail_resolution (rh); @@ -1711,8 +1713,6 @@ recursive_pkey_resolution (struct GNS_ResolverHandle *rh, ac->rh = rh; ac->gns_authority = GNUNET_YES; ac->authority_info.gns_authority = auth; - GNUNET_GNSRECORD_record_to_identity_key (rd, - &ac->authority_info.gns_authority); ac->label = resolver_lookup_get_next_label (rh); /* add AC to tail */ GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head, @@ -2258,14 +2258,25 @@ handle_gns_resolution_result (void *cls, break; case GNUNET_GNSRECORD_TYPE_PKEY: + case GNUNET_GNSRECORD_TYPE_EDKEY: { - if (rd[i].data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) + struct GNUNET_IDENTITY_PublicKey pubkey; + if (rd[i].data_size < sizeof(uint32_t)) + { + GNUNET_break_op (0); + break; + } + if (GNUNET_OK != + GNUNET_GNSRECORD_identity_from_data (rd[i].data, + rd[i].data_size, + rd[i].record_type, + &pubkey)) { GNUNET_break_op (0); break; } rd_off++; - if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type) + if (rd[i].record_type != rh->record_type) { /* try to resolve "@" */ struct AuthorityChain *ac; @@ -2273,9 +2284,7 @@ handle_gns_resolution_result (void *cls, ac = GNUNET_new (struct AuthorityChain); ac->rh = rh; ac->gns_authority = GNUNET_YES; - GNUNET_GNSRECORD_record_to_identity_key (&rd[i], - &ac->authority_info. - gns_authority); + ac->authority_info.gns_authority = pubkey; ac->label = GNUNET_strdup (GNUNET_GNS_EMPTY_LABEL_AT); GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head, rh->ac_tail, @@ -2365,6 +2374,7 @@ handle_gns_resolution_result (void *cls, return; case GNUNET_GNSRECORD_TYPE_PKEY: + case GNUNET_GNSRECORD_TYPE_EDKEY: GNUNET_break_op (1 == rd_count); /* PKEY should be unique */ recursive_pkey_resolution (rh, &rd[0]); diff --git a/src/gns/plugin_gnsrecord_gns.c b/src/gns/plugin_gnsrecord_gns.c index 0bd1d047f..a2ad0b905 100644 --- a/src/gns/plugin_gnsrecord_gns.c +++ b/src/gns/plugin_gnsrecord_gns.c @@ -55,10 +55,12 @@ gns_value_to_string (void *cls, switch (type) { case GNUNET_GNSRECORD_TYPE_PKEY: - if (data_size != sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)) + case GNUNET_GNSRECORD_TYPE_EDKEY: + if (GNUNET_OK != GNUNET_GNSRECORD_identity_from_data (data, + data_size, + type, + &pk)) return NULL; - pk.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); - memcpy (&pk.ecdsa_key, data, sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); return GNUNET_IDENTITY_public_key_to_string (&pk); case GNUNET_GNSRECORD_TYPE_NICK: @@ -156,25 +158,35 @@ gns_string_to_value (void *cls, void **data, size_t *data_size) { - struct GNUNET_CRYPTO_EcdsaPublicKey pkey; struct GNUNET_IDENTITY_PublicKey pk; + uint32_t record_type; if (NULL == s) return GNUNET_SYSERR; switch (type) { case GNUNET_GNSRECORD_TYPE_PKEY: + case GNUNET_GNSRECORD_TYPE_EDKEY: if (GNUNET_OK != GNUNET_IDENTITY_public_key_from_string (s, &pk)) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - _ ("Unable to parse PKEY record `%s'\n"), + _ ("Unable to parse zone key record `%s'\n"), s); return GNUNET_SYSERR; } - *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey); - GNUNET_memcpy (*data, &pk.ecdsa_key, sizeof(pkey)); - *data_size = sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey); + *data_size = GNUNET_IDENTITY_key_get_length (&pk); + if (GNUNET_OK != GNUNET_GNSRECORD_data_from_identity (&pk, + (char **) data, + data_size, + &record_type)) + return GNUNET_SYSERR; + if (record_type != type) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + _("Record type does not match parsed record type\n")); + return GNUNET_SYSERR; + } return GNUNET_OK; case GNUNET_GNSRECORD_TYPE_NICK: @@ -305,6 +317,7 @@ static struct const char *name; uint32_t number; } gns_name_map[] = { { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY }, + { "EDKEY", GNUNET_GNSRECORD_TYPE_PKEY }, { "NICK", GNUNET_GNSRECORD_TYPE_NICK }, { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO }, { "VPN", GNUNET_GNSRECORD_TYPE_VPN }, diff --git a/src/gnsrecord/gnsrecord_misc.c b/src/gnsrecord/gnsrecord_misc.c index 4b1695d69..ba8803850 100644 --- a/src/gnsrecord/gnsrecord_misc.c +++ b/src/gnsrecord/gnsrecord_misc.c @@ -272,6 +272,51 @@ GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey, } +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; + if (data_size > sizeof (struct GNUNET_IDENTITY_PublicKey)) + return GNUNET_SYSERR; + key->type = type; + memcpy (key, data, data_size); + return GNUNET_OK; +} + +enum GNUNET_GenericReturnValue +GNUNET_GNSRECORD_data_from_identity (const struct + GNUNET_IDENTITY_PublicKey *key, + char **data, + size_t *data_size, + uint32_t *type) +{ + *type = key->type; + *data_size = GNUNET_IDENTITY_key_get_length (key); + if (0 == *data_size) + return GNUNET_SYSERR; + *data = GNUNET_malloc (*data_size); + memcpy (*data, key, *data_size); + 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) { @@ -325,6 +370,7 @@ GNUNET_GNSRECORD_query_from_block (const struct GNUNET_GNSRECORD_Block *block, } + enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_record_to_identity_key (const struct GNUNET_GNSRECORD_Data *rd, struct GNUNET_IDENTITY_PublicKey *key) @@ -346,4 +392,5 @@ GNUNET_GNSRECORD_record_to_identity_key (const struct GNUNET_GNSRECORD_Data *rd, } + /* end of gnsrecord_misc.c */ diff --git a/src/gnsrecord/gnunet-gnsrecord-tvg.c b/src/gnsrecord/gnunet-gnsrecord-tvg.c index c29f377bc..47b13bdab 100644 --- a/src/gnsrecord/gnunet-gnsrecord-tvg.c +++ b/src/gnsrecord/gnunet-gnsrecord-tvg.c @@ -98,16 +98,24 @@ run (void *cls, size_t data_size; char *rdata; size_t rdata_size; + char ztld[128]; 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); - fprintf (stdout, "Zone private key (d, little-endian scalar):\n"); + fprintf (stdout, "Zone private key (d, little-endian, with ztype prepended):\n"); print_bytes (&id_priv, GNUNET_IDENTITY_key_get_length (&id_pub), 8); //FIXME length for privkey? fprintf (stdout, "\n"); - fprintf (stdout, "Zone public key (zk):\n"); + fprintf (stdout, "Zone identifier (zid):\n"); print_bytes (&id_pub, GNUNET_IDENTITY_key_get_length (&id_pub), 8); + GNUNET_STRINGS_data_to_string (&id_pub, + GNUNET_IDENTITY_key_get_length (&id_pub), + ztld, + sizeof (ztld)); + fprintf (stdout, "\n"); + fprintf (stdout, "Encoded zone identifier (zkl = zTLD):\n"); + fprintf (stdout, "%s\n", ztld); fprintf (stdout, "\n"); pkey_data_p.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); diff --git a/src/include/gnunet_gnsrecord_lib.h b/src/include/gnunet_gnsrecord_lib.h index 530879b21..0bf2ceed7 100644 --- a/src/include/gnunet_gnsrecord_lib.h +++ b/src/include/gnunet_gnsrecord_lib.h @@ -686,16 +686,48 @@ GNUNET_GNSRECORD_query_from_block (const struct GNUNET_GNSRECORD_Block *block, /** - * Build a #GNUNET_GNSRECORD_PublicKey from a - * zone delegation resource record set. + * Build a #GNUNET_GNSRECORD_PublicKey from + * zone delegation resource record data. * - * @param the resource record set. MUST be a delegation record. - * @param key where to write the identtiy key. - * @return GNUNET_SYSERR on error or if the zone is not a delegation zone. + * @param data the record data- + * @param data_size the data size. + * @param type the record type + * @param key the identity key to store the data in (must be allocated). + * @return GNUNET_OK if successful. */ enum GNUNET_GenericReturnValue -GNUNET_GNSRECORD_record_to_identity_key (const struct GNUNET_GNSRECORD_Data *rd, - struct GNUNET_IDENTITY_PublicKey *key); +GNUNET_GNSRECORD_identity_from_data (const char *data, + size_t data_size, + uint32_t type, + struct GNUNET_IDENTITY_PublicKey *key); + + +/** + * Create record data and size from an identity key. + * + * @param key the identity key to use. + * @param data the record data (will be allocated) + * @param data_size the allocated data size. + * @param type the resulting record type + * @return GNUNET_OK if successful. + */ +enum GNUNET_GenericReturnValue +GNUNET_GNSRECORD_data_from_identity (const struct GNUNET_IDENTITY_PublicKey *key, + char **data, + size_t *data_size, + uint32_t *type); + + +/** + * Check if this type is one of the supported GNS zone + * types. + * + * @param type the type to check + * @return GNUNET_YES if it is one of the supported types. + */ +enum GNUNET_GenericReturnValue +GNUNET_GNSRECORD_is_zonekey_type (uint32_t type); + #if 0 /* keep Emacsens' auto-indent happy */ { diff --git a/src/namestore/gnunet-namestore-fcfsd.c b/src/namestore/gnunet-namestore-fcfsd.c index 844f4a990..22d108067 100644 --- a/src/namestore/gnunet-namestore-fcfsd.c +++ b/src/namestore/gnunet-namestore-fcfsd.c @@ -349,7 +349,8 @@ iterate_cb (void *cls, return; } - if (GNUNET_GNSRECORD_TYPE_PKEY != rd->record_type) + if ((GNUNET_GNSRECORD_TYPE_PKEY != rd->record_type) && + (GNUNET_GNSRECORD_TYPE_EDKEY != rd->record_type)) { GNUNET_NAMESTORE_zone_iterator_next (zr->list_it, 1); @@ -621,6 +622,7 @@ zone_to_name_cb (void *cls, { struct Request *request = cls; struct GNUNET_GNSRECORD_Data r; + char *rdata; (void) rd; (void) zone_key; @@ -635,10 +637,21 @@ zone_to_name_cb (void *cls, run_httpd_now (); return; } - r.data = &request->pub; - r.data_size = sizeof(request->pub); + if (GNUNET_OK != GNUNET_GNSRECORD_data_from_identity (&request->pub, + &rdata, + &r.data_size, + &r.record_type)) + { + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + _ ("Error creating record data.\n")); + request->phase = RP_FAIL; + MHD_resume_connection (request->con); + run_httpd_now (); + return; + } + + r.data = rdata; r.expiration_time = UINT64_MAX; - r.record_type = GNUNET_GNSRECORD_TYPE_PKEY; r.flags = GNUNET_GNSRECORD_RF_NONE; request->qe = GNUNET_NAMESTORE_records_store (ns, &fcfs_zone_pkey, @@ -646,6 +659,7 @@ zone_to_name_cb (void *cls, 1, &r, &put_continuation, request); + GNUNET_free (rdata); } diff --git a/src/namestore/gnunet-namestore.c b/src/namestore/gnunet-namestore.c index 345d76910..92d2cf627 100644 --- a/src/namestore/gnunet-namestore.c +++ b/src/namestore/gnunet-namestore.c @@ -661,11 +661,11 @@ get_existing_record (void *cls, return; case GNUNET_GNSRECORD_TYPE_PKEY: + case GNUNET_GNSRECORD_TYPE_EDKEY: fprintf ( stderr, _ ( - "A %s record exists already under `%s', no other records can be added.\n"), - "PKEY", + "A zone key record exists already under `%s', no other records can be added.\n"), rec_name); ret = 1; test_finished (); @@ -703,13 +703,13 @@ get_existing_record (void *cls, break; case GNUNET_GNSRECORD_TYPE_PKEY: + case GNUNET_GNSRECORD_TYPE_EDKEY: if (0 != rd_count) { fprintf (stderr, _ ( - "Records already exist under `%s', cannot add `%s' record.\n"), - rec_name, - "PKEY"); + "Records already exist under `%s', cannot add record.\n"), + rec_name); ret = 1; test_finished (); return; diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c index 1a071fd80..9ccc90cfd 100644 --- a/src/namestore/plugin_namestore_flat.c +++ b/src/namestore/plugin_namestore_flat.c @@ -698,11 +698,14 @@ zone_to_name (void *cls, for (unsigned int i = 0; i < entry->record_count; i++) { - if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type) + if (GNUNET_NO == + GNUNET_GNSRECORD_is_zonekey_type (entry->record_data[i].record_type)) + continue; + if (ztn->value_zone->type != entry->record_data[i].record_type) continue; if (0 == memcmp (ztn->value_zone, entry->record_data[i].data, - sizeof(struct GNUNET_IDENTITY_PublicKey))) + entry->record_data[i].data_size)) { ztn->iter (ztn->iter_cls, i + 1, /* zero is illegal! */ diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c index 358fd35d6..bdbaf96b3 100644 --- a/src/namestore/plugin_namestore_postgres.c +++ b/src/namestore/plugin_namestore_postgres.c @@ -195,13 +195,14 @@ namestore_postgres_store_records (void *cls, 0, sizeof(pkey)); for (unsigned int i = 0; i < rd_count; i++) - if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type) + if (GNUNET_YES == + GNUNET_GNSRECORD_is_zonekey_type (rd[i].record_type)) { - GNUNET_break (sizeof(struct GNUNET_IDENTITY_PublicKey) == - rd[i].data_size); - GNUNET_memcpy (&pkey, - rd[i].data, - rd[i].data_size); + GNUNET_break (GNUNET_OK == + GNUNET_GNSRECORD_identity_from_data (rd[i].data, + rd[i].data_size, + rd[i].record_type, + &pkey)); break; } rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c index 4a4ce3d8d..7cb9b7ed0 100644 --- a/src/namestore/plugin_namestore_sqlite.c +++ b/src/namestore/plugin_namestore_sqlite.c @@ -329,13 +329,13 @@ namestore_sqlite_store_records (void *cls, 0, sizeof(pkey)); for (unsigned int i = 0; i < rd_count; i++) - if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type) + if (GNUNET_YES == GNUNET_GNSRECORD_is_zonekey_type (rd[i].record_type)) { - GNUNET_break (sizeof(struct GNUNET_IDENTITY_PublicKey) == - rd[i].data_size); - GNUNET_memcpy (&pkey, - rd[i].data, - rd[i].data_size); + GNUNET_break (GNUNET_YES == + GNUNET_GNSRECORD_identity_from_data (rd[i].data, + rd[i].data_size, + rd[i].record_type, + &pkey)); break; } rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, diff --git a/src/revocation/gnunet-revocation-tvg.c b/src/revocation/gnunet-revocation-tvg.c index 88117dd3f..cb5e31fcd 100644 --- a/src/revocation/gnunet-revocation-tvg.c +++ b/src/revocation/gnunet-revocation-tvg.c @@ -70,16 +70,24 @@ run (void *cls, struct GNUNET_REVOCATION_PowP *pow; struct GNUNET_REVOCATION_PowCalculationHandle *ph; struct GNUNET_TIME_Relative exp; + char ztld[128]; id_priv.type = htonl (GNUNET_IDENTITY_TYPE_ECDSA); GNUNET_CRYPTO_ecdsa_key_create (&id_priv.ecdsa_key); GNUNET_IDENTITY_key_get_public (&id_priv, &id_pub); - fprintf (stdout, "Zone private key (d, little-endian scalar):\n"); + GNUNET_STRINGS_data_to_string (&id_pub, + GNUNET_IDENTITY_key_get_length (&id_pub), + ztld, + sizeof (ztld)); + fprintf (stdout, "Zone private key (d, little-endian scalar, with ztype prepended):\n"); print_bytes (&id_priv, sizeof(id_priv), 8); fprintf (stdout, "\n"); - fprintf (stdout, "Zone public key (zk):\n"); - print_bytes (&id_pub, sizeof(id_pub), 8); + fprintf (stdout, "Zone identifier (zid):\n"); + print_bytes (&id_pub, GNUNET_IDENTITY_key_get_length (&id_pub), 8); + fprintf (stdout, "\n"); + fprintf (stdout, "Encoded zone identifier (zkl = zTLD):\n"); + fprintf (stdout, "%s\n", ztld); fprintf (stdout, "\n"); pow = GNUNET_malloc (GNUNET_REVOCATION_MAX_PROOF_SIZE); GNUNET_REVOCATION_pow_init (&id_priv, -- cgit v1.2.3