2 This file is part of GNUnet.
3 (C) 2009-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file namestore/namestore_api_common.c
23 * @brief API to access the NAMESTORE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_namestore_service.h"
34 #include "gnunet_dnsparser_lib.h"
35 #include "gnunet_tun_lib.h"
36 #include "namestore.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
41 GNUNET_NETWORK_STRUCT_BEGIN
45 * Internal format of a record in the serialized form.
51 * Expiration time for the DNS record; relative or absolute depends
52 * on 'flags', network byte order.
54 uint64_t expiration_time GNUNET_PACKED;
57 * Number of bytes in 'data', network byte order.
59 uint32_t data_size GNUNET_PACKED;
62 * Type of the GNS/DNS record, network byte order.
64 uint32_t record_type GNUNET_PACKED;
67 * Flags for the record, network byte order.
69 uint32_t flags GNUNET_PACKED;
73 GNUNET_NETWORK_STRUCT_END
76 * Convert a UTF-8 string to UTF-8 lowercase
77 * @param src source string
78 * @return converted result
81 GNUNET_NAMESTORE_normalize_string (const char *src)
83 GNUNET_assert (NULL != src);
84 char *res = strdup (src);
86 GNUNET_STRINGS_utf8_tolower(src, &res);
92 * Convert a zone key to a string (for printing debug messages).
93 * This is one of the very few calls in the entire API that is
96 * @param z the zone key
97 * @return string form; will be overwritten by next call to #GNUNET_NAMESTORE_z2s
100 GNUNET_NAMESTORE_z2s (const struct GNUNET_CRYPTO_EccPublicSignKey *z)
102 static char buf[sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) * 8];
105 end = GNUNET_STRINGS_data_to_string ((const unsigned char *) z,
106 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
119 * Calculate how many bytes we will need to serialize the given
122 * @param rd_count number of records in the rd array
123 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
124 * @return the required size to serialize
127 GNUNET_NAMESTORE_records_get_size (unsigned int rd_count,
128 const struct GNUNET_NAMESTORE_RecordData *rd)
133 ret = sizeof (struct NetworkRecord) * rd_count;
134 for (i=0;i<rd_count;i++)
136 GNUNET_assert ((ret + rd[i].data_size) >= ret);
137 ret += rd[i].data_size;
144 * Serialize the given records to the given destination buffer.
146 * @param rd_count number of records in the rd array
147 * @param rd array of #GNUNET_NAMESTORE_RecordData with @a rd_count elements
148 * @param dest_size size of the destination array
149 * @param dest where to write the result
150 * @return the size of serialized records, -1 if records do not fit
153 GNUNET_NAMESTORE_records_serialize (unsigned int rd_count,
154 const struct GNUNET_NAMESTORE_RecordData *rd,
158 struct NetworkRecord rec;
163 for (i=0;i<rd_count;i++)
165 rec.expiration_time = GNUNET_htonll (rd[i].expiration_time);
166 rec.data_size = htonl ((uint32_t) rd[i].data_size);
167 rec.record_type = htonl (rd[i].record_type);
168 rec.flags = htonl (rd[i].flags);
169 if (off + sizeof (rec) > dest_size)
171 memcpy (&dest[off], &rec, sizeof (rec));
173 if (off + rd[i].data_size > dest_size)
175 memcpy (&dest[off], rd[i].data, rd[i].data_size);
176 off += rd[i].data_size;
183 * Compares if two records are equal (ignoring flags such
184 * as authority, private and pending, but not relative vs.
185 * absolute expiration time).
189 * @return #GNUNET_YES if the records are equal or #GNUNET_NO if they are not
192 GNUNET_NAMESTORE_records_cmp (const struct GNUNET_NAMESTORE_RecordData *a,
193 const struct GNUNET_NAMESTORE_RecordData *b)
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Comparing records\n");
197 if (a->record_type != b->record_type)
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Record type %lu != %lu\n", a->record_type, b->record_type);
203 if ((a->expiration_time != b->expiration_time) &&
204 ((a->expiration_time != 0) && (b->expiration_time != 0)))
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Expiration time %llu != %llu\n", a->expiration_time, b->expiration_time);
210 if ((a->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS)
211 != (b->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS))
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "Flags %lu (%lu) != %lu (%lu)\n", a->flags,
215 a->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS, b->flags,
216 b->flags & GNUNET_NAMESTORE_RF_RCMP_FLAGS);
219 if (a->data_size != b->data_size)
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Data size %lu != %lu\n", a->data_size, b->data_size);
225 if (0 != memcmp (a->data, b->data, a->data_size))
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Data contents do not match\n");
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Records are equal\n");
238 * Deserialize the given records to the given destination.
240 * @param len size of the serialized record data
241 * @param src the serialized record data
242 * @param rd_count number of records in the rd array
243 * @param dest where to put the data
244 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
247 GNUNET_NAMESTORE_records_deserialize (size_t len,
249 unsigned int rd_count,
250 struct GNUNET_NAMESTORE_RecordData *dest)
252 struct NetworkRecord rec;
257 for (i=0;i<rd_count;i++)
259 if (off + sizeof (rec) > len)
260 return GNUNET_SYSERR;
261 memcpy (&rec, &src[off], sizeof (rec));
262 dest[i].expiration_time = GNUNET_ntohll (rec.expiration_time);
263 dest[i].data_size = ntohl ((uint32_t) rec.data_size);
264 dest[i].record_type = ntohl (rec.record_type);
265 dest[i].flags = ntohl (rec.flags);
268 if (off + dest[i].data_size > len)
269 return GNUNET_SYSERR;
270 dest[i].data = &src[off];
271 off += dest[i].data_size;
278 * Returns the expiration time of the given block of records. The block
279 * expiration time is the expiration time of the block with smallest
282 * @param rd_count number of records given in @a rd
283 * @param rd array of records
284 * @return absolute expiration time
286 struct GNUNET_TIME_Absolute
287 GNUNET_NAMESTORE_record_get_expiration_time (unsigned int rd_count,
288 const struct GNUNET_NAMESTORE_RecordData *rd)
291 struct GNUNET_TIME_Absolute expire;
292 struct GNUNET_TIME_Absolute at;
293 struct GNUNET_TIME_Relative rt;
296 return GNUNET_TIME_UNIT_ZERO_ABS;
297 expire = GNUNET_TIME_UNIT_FOREVER_ABS;
298 for (c = 0; c < rd_count; c++)
300 if (0 != (rd[c].flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
302 rt.rel_value_us = rd[c].expiration_time;
303 at = GNUNET_TIME_relative_to_absolute (rt);
307 at.abs_value_us = rd[c].expiration_time;
309 expire = GNUNET_TIME_absolute_min (at, expire);
316 * Derive session key and iv from label and public key.
318 * @param iv initialization vector to initialize
319 * @param skey session key to initialize
320 * @param label label to use for KDF
321 * @param pub public key to use for KDF
324 derive_block_aes_key (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
325 struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
327 const struct GNUNET_CRYPTO_EccPublicSignKey *pub)
329 static const char ctx_key[] = "gns-aes-ctx-key";
330 static const char ctx_iv[] = "gns-aes-ctx-iv";
332 GNUNET_CRYPTO_kdf (skey, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
333 pub, sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
334 label, strlen (label),
335 ctx_key, strlen (ctx_key),
337 GNUNET_CRYPTO_kdf (iv, sizeof (struct GNUNET_CRYPTO_SymmetricInitializationVector),
338 pub, sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
339 label, strlen (label),
340 ctx_iv, strlen (ctx_iv),
346 * Sign name and records
348 * @param key the private key
349 * @param expire block expiration
350 * @param label the name for the records
351 * @param rd record data
352 * @param rd_count number of records
353 * @return NULL on error (block too large)
355 struct GNUNET_NAMESTORE_Block *
356 GNUNET_NAMESTORE_block_create (const struct GNUNET_CRYPTO_EccPrivateKey *key,
357 struct GNUNET_TIME_Absolute expire,
359 const struct GNUNET_NAMESTORE_RecordData *rd,
360 unsigned int rd_count)
362 size_t payload_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
363 char payload[sizeof (uint32_t) + payload_len];
364 struct GNUNET_NAMESTORE_Block *block;
365 struct GNUNET_CRYPTO_EccPublicSignKey pkey;
366 struct GNUNET_CRYPTO_EccPrivateKey *dkey;
367 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
368 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
369 uint32_t rd_count_nbo;
371 if (payload_len > GNUNET_NAMESTORE_MAX_VALUE_SIZE)
373 rd_count_nbo = htonl (rd_count);
374 memcpy (payload, &rd_count_nbo, sizeof (uint32_t));
375 GNUNET_assert (payload_len ==
376 GNUNET_NAMESTORE_records_serialize (rd_count, rd,
377 payload_len, &payload[sizeof (uint32_t)]));
378 block = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Block) +
379 sizeof (uint32_t) + payload_len);
380 block->purpose.size = htonl (sizeof (uint32_t) + payload_len +
381 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
382 sizeof (struct GNUNET_TIME_AbsoluteNBO));
383 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
384 block->expiration_time = GNUNET_TIME_absolute_hton (expire);
385 dkey = GNUNET_CRYPTO_ecc_key_derive (key,
388 GNUNET_CRYPTO_ecc_key_get_public_for_signature (dkey,
389 &block->derived_key);
390 GNUNET_CRYPTO_ecc_key_get_public_for_signature (key,
392 derive_block_aes_key (&iv, &skey, label, &pkey);
393 GNUNET_break (payload_len + sizeof (uint32_t) ==
394 GNUNET_CRYPTO_symmetric_encrypt (payload, payload_len + sizeof (uint32_t),
398 GNUNET_CRYPTO_ecc_sign (dkey,
413 * Check if a signature is valid. This API is used by the GNS Block
414 * to validate signatures received from the network.
416 * @param block block to verify
417 * @return #GNUNET_OK if the signature is valid
420 GNUNET_NAMESTORE_block_verify (const struct GNUNET_NAMESTORE_Block *block)
422 return GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN,
425 &block->derived_key);
432 * @param block block to decrypt
433 * @param zone_key public key of the zone
434 * @param label the name for the records
435 * @param proc function to call with the result
436 * @param proc_cls closure for proc
437 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
441 GNUNET_NAMESTORE_block_decrypt (const struct GNUNET_NAMESTORE_Block *block,
442 const struct GNUNET_CRYPTO_EccPublicSignKey *zone_key,
444 GNUNET_NAMESTORE_RecordCallback proc,
447 size_t payload_len = ntohl (block->purpose.size) -
448 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) -
449 sizeof (struct GNUNET_TIME_AbsoluteNBO);
450 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
451 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
453 if (ntohl (block->purpose.size) <
454 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
455 sizeof (struct GNUNET_TIME_AbsoluteNBO))
458 return GNUNET_SYSERR;
460 derive_block_aes_key (&iv, &skey, label, zone_key);
462 char payload[payload_len];
465 GNUNET_break (payload_len ==
466 GNUNET_CRYPTO_symmetric_decrypt (&block[1], payload_len,
472 rd_count = ntohl (rd_count);
475 /* limit to sane value */
477 return GNUNET_SYSERR;
480 struct GNUNET_NAMESTORE_RecordData rd[rd_count];
483 GNUNET_NAMESTORE_records_deserialize (payload_len - sizeof (uint32_t),
484 &payload[sizeof (uint32_t)],
489 return GNUNET_SYSERR;
492 proc (proc_cls, rd_count, (0 != rd_count) ? rd : NULL);
500 * Convert the 'value' of a record to a string.
502 * @param type type of the record
503 * @param data value in binary encoding
504 * @param data_size number of bytes in @a data
505 * @return NULL on error, otherwise human-readable representation of the value
508 GNUNET_NAMESTORE_value_to_string (uint32_t type,
514 char tmp[INET6_ADDRSTRLEN];
520 case GNUNET_DNSPARSER_TYPE_A:
521 if (data_size != sizeof (struct in_addr))
523 if (NULL == inet_ntop (AF_INET, data, tmp, sizeof (tmp)))
525 return GNUNET_strdup (tmp);
526 case GNUNET_DNSPARSER_TYPE_NS:
532 ns = GNUNET_DNSPARSER_parse_name (data,
543 case GNUNET_DNSPARSER_TYPE_CNAME:
549 cname = GNUNET_DNSPARSER_parse_name (data,
552 if ( (NULL == cname) ||
560 case GNUNET_DNSPARSER_TYPE_SOA:
562 struct GNUNET_DNSPARSER_SoaRecord *soa;
566 soa = GNUNET_DNSPARSER_parse_soa (data,
569 if ( (NULL == soa) ||
575 GNUNET_asprintf (&result,
576 "rname=%s mname=%s %lu,%lu,%lu,%lu,%lu",
584 GNUNET_DNSPARSER_free_soa (soa);
587 case GNUNET_DNSPARSER_TYPE_PTR:
593 ptr = GNUNET_DNSPARSER_parse_name (data,
596 if ( (NULL == ptr) ||
604 case GNUNET_DNSPARSER_TYPE_MX:
606 struct GNUNET_DNSPARSER_MxRecord *mx;
610 mx = GNUNET_DNSPARSER_parse_mx (data,
619 GNUNET_asprintf (&result,
623 GNUNET_DNSPARSER_free_mx (mx);
626 case GNUNET_DNSPARSER_TYPE_TXT:
627 return GNUNET_strndup (data, data_size);
628 case GNUNET_DNSPARSER_TYPE_AAAA:
629 if (data_size != sizeof (struct in6_addr))
631 if (NULL == inet_ntop (AF_INET6, data, tmp, sizeof (tmp)))
633 return GNUNET_strdup (tmp);
634 case GNUNET_NAMESTORE_TYPE_PKEY:
635 if (data_size != sizeof (struct GNUNET_CRYPTO_EccPublicSignKey))
637 return GNUNET_CRYPTO_ecc_public_sign_key_to_string (data);
638 case GNUNET_NAMESTORE_TYPE_PSEU:
639 return GNUNET_strndup (data, data_size);
640 case GNUNET_NAMESTORE_TYPE_LEHO:
641 return GNUNET_strndup (data, data_size);
642 case GNUNET_NAMESTORE_TYPE_VPN:
644 const struct GNUNET_TUN_GnsVpnRecord *vpn;
648 if ( (data_size <= sizeof (struct GNUNET_TUN_GnsVpnRecord)) ||
649 ('\0' != cdata[data_size - 1]) )
650 return NULL; /* malformed */
652 if (0 == GNUNET_asprintf (&vpn_str, "%u %s %s",
653 (unsigned int) ntohs (vpn->proto),
654 (const char*) GNUNET_i2s_full (&vpn->peer),
655 (const char*) &vpn[1]))
657 GNUNET_free (vpn_str);
662 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
668 ns = GNUNET_DNSPARSER_parse_name (data,
679 case GNUNET_DNSPARSER_TYPE_SRV:
681 struct GNUNET_DNSPARSER_SrvRecord *srv;
685 srv = GNUNET_DNSPARSER_parse_srv ("+", /* FIXME: is this OK? */
689 if ( (NULL == srv) ||
695 GNUNET_asprintf (&result,
696 "%d %d %d _%s._%s.%s",
703 GNUNET_DNSPARSER_free_srv (srv);
706 case GNUNET_DNSPARSER_TYPE_TLSA:
708 const struct GNUNET_TUN_DnsTlsaRecord *tlsa;
712 if ( (data_size <= sizeof (struct GNUNET_TUN_DnsTlsaRecord)) ||
713 ('\0' != cdata[data_size - 1]) )
714 return NULL; /* malformed */
716 if (0 == GNUNET_asprintf (&tlsa_str,
721 (const char *) &tlsa[1]))
723 GNUNET_free (tlsa_str);
731 GNUNET_break (0); // not implemented
737 * Convert human-readable version of a 'value' of a record to the binary
740 * @param type type of the record
741 * @param s human-readable string
742 * @param data set to value in binary encoding (will be allocated)
743 * @param data_size set to number of bytes in @a data
744 * @return #GNUNET_OK on success
747 GNUNET_NAMESTORE_string_to_value (uint32_t type,
752 struct in_addr value_a;
753 struct in6_addr value_aaaa;
754 struct GNUNET_CRYPTO_EccPublicSignKey pkey;
755 struct GNUNET_TUN_GnsVpnRecord *vpn;
756 struct GNUNET_TUN_DnsTlsaRecord *tlsa;
757 char s_peer[103 + 1];
758 char s_serv[253 + 1];
762 return GNUNET_SYSERR;
766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
767 _("Unsupported record type %d\n"),
769 return GNUNET_SYSERR;
770 case GNUNET_DNSPARSER_TYPE_A:
771 if (1 != inet_pton (AF_INET, s, &value_a))
773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
774 _("Unable to parse IPv4 address `%s'\n"),
776 return GNUNET_SYSERR;
778 *data = GNUNET_malloc (sizeof (struct in_addr));
779 memcpy (*data, &value_a, sizeof (value_a));
780 *data_size = sizeof (value_a);
782 case GNUNET_DNSPARSER_TYPE_NS:
789 GNUNET_DNSPARSER_builder_add_name (nsbuf,
794 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
795 _("Failed to serialize NS record with value `%s'\n"),
797 return GNUNET_SYSERR;
800 *data = GNUNET_malloc (off);
801 memcpy (*data, nsbuf, off);
804 case GNUNET_DNSPARSER_TYPE_CNAME:
811 GNUNET_DNSPARSER_builder_add_name (cnamebuf,
816 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
817 _("Failed to serialize CNAME record with value `%s'\n"),
819 return GNUNET_SYSERR;
822 *data = GNUNET_malloc (off);
823 memcpy (*data, cnamebuf, off);
826 case GNUNET_DNSPARSER_TYPE_SOA:
828 struct GNUNET_DNSPARSER_SoaRecord soa;
830 char soa_rname[253 + 1];
831 char soa_mname[253 + 1];
832 unsigned int soa_serial;
833 unsigned int soa_refresh;
834 unsigned int soa_retry;
835 unsigned int soa_expire;
836 unsigned int soa_min;
840 "rname=%253s mname=%253s %u,%u,%u,%u,%u",
841 soa_rname, soa_mname,
842 &soa_serial, &soa_refresh, &soa_retry, &soa_expire, &soa_min))
844 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
845 _("Unable to parse SOA record `%s'\n"),
847 return GNUNET_SYSERR;
849 soa.mname = soa_mname;
850 soa.rname = soa_rname;
851 soa.serial = (uint32_t) soa_serial;
852 soa.refresh =(uint32_t) soa_refresh;
853 soa.retry = (uint32_t) soa_retry;
854 soa.expire = (uint32_t) soa_expire;
855 soa.minimum_ttl = (uint32_t) soa_min;
858 GNUNET_DNSPARSER_builder_add_soa (soabuf,
863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
864 _("Failed to serialize SOA record with mname `%s' and rname `%s'\n"),
867 return GNUNET_SYSERR;
870 *data = GNUNET_malloc (off);
871 memcpy (*data, soabuf, off);
874 case GNUNET_DNSPARSER_TYPE_PTR:
881 GNUNET_DNSPARSER_builder_add_name (ptrbuf,
886 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
887 _("Failed to serialize PTR record with value `%s'\n"),
889 return GNUNET_SYSERR;
892 *data = GNUNET_malloc (off);
893 memcpy (*data, ptrbuf, off);
896 case GNUNET_DNSPARSER_TYPE_MX:
898 struct GNUNET_DNSPARSER_MxRecord mx;
900 char mxhost[253 + 1];
904 if (2 != SSCANF(s, "%hu,%253s", &mx_pref, mxhost))
906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
907 _("Unable to parse MX record `%s'\n"),
909 return GNUNET_SYSERR;
911 mx.preference = mx_pref;
916 GNUNET_DNSPARSER_builder_add_mx (mxbuf,
921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
922 _("Failed to serialize MX record with hostname `%s'\n"),
924 return GNUNET_SYSERR;
927 *data = GNUNET_malloc (off);
928 memcpy (*data, mxbuf, off);
931 case GNUNET_DNSPARSER_TYPE_TXT:
932 *data = GNUNET_strdup (s);
933 *data_size = strlen (s);
935 case GNUNET_DNSPARSER_TYPE_AAAA:
936 if (1 != inet_pton (AF_INET6, s, &value_aaaa))
938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
939 _("Unable to parse IPv6 address `%s'\n"),
941 return GNUNET_SYSERR;
943 *data = GNUNET_malloc (sizeof (struct in6_addr));
944 *data_size = sizeof (struct in6_addr);
945 memcpy (*data, &value_aaaa, sizeof (value_aaaa));
947 case GNUNET_NAMESTORE_TYPE_PKEY:
949 GNUNET_CRYPTO_ecc_public_sign_key_from_string (s, strlen (s), &pkey))
951 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
952 _("Unable to parse PKEY record `%s'\n"),
954 return GNUNET_SYSERR;
956 *data = GNUNET_new (struct GNUNET_CRYPTO_EccPublicSignKey);
957 memcpy (*data, &pkey, sizeof (pkey));
958 *data_size = sizeof (struct GNUNET_CRYPTO_EccPublicSignKey);
960 case GNUNET_NAMESTORE_TYPE_PSEU:
961 *data = GNUNET_strdup (s);
962 *data_size = strlen (s);
964 case GNUNET_NAMESTORE_TYPE_LEHO:
965 *data = GNUNET_strdup (s);
966 *data_size = strlen (s);
968 case GNUNET_NAMESTORE_TYPE_VPN:
969 if (3 != SSCANF (s,"%u %103s %253s",
970 &proto, s_peer, s_serv))
972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
973 _("Unable to parse VPN record string `%s'\n"),
975 return GNUNET_SYSERR;
977 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
978 *data = vpn = GNUNET_malloc (*data_size);
979 if (GNUNET_OK != GNUNET_CRYPTO_ecc_public_sign_key_from_string ((char*) s_peer,
981 &vpn->peer.public_key))
985 return GNUNET_SYSERR;
987 vpn->proto = htons ((uint16_t) proto);
988 strcpy ((char*)&vpn[1], s_serv);
990 case GNUNET_NAMESTORE_TYPE_GNS2DNS:
997 GNUNET_DNSPARSER_builder_add_name (nsbuf,
1002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1003 _("Failed to serialize GNS2DNS record with value `%s'\n"),
1005 return GNUNET_SYSERR;
1008 *data = GNUNET_malloc (off);
1009 memcpy (*data, nsbuf, off);
1012 case GNUNET_DNSPARSER_TYPE_TLSA:
1013 *data_size = sizeof (struct GNUNET_TUN_DnsTlsaRecord) + strlen (s) - 6;
1014 *data = tlsa = GNUNET_malloc (*data_size);
1015 if (4 != SSCANF (s, "%c %c %c %s",
1018 &tlsa->matching_type,
1021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1022 _("Unable to parse TLSA record string `%s'\n"),
1026 return GNUNET_SYSERR;
1030 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1031 _("Unsupported record type %d\n"),
1033 return GNUNET_SYSERR;
1039 * Mapping of record type numbers to human-readable
1040 * record type names.
1046 { "A", GNUNET_DNSPARSER_TYPE_A },
1047 { "NS", GNUNET_DNSPARSER_TYPE_NS },
1048 { "CNAME", GNUNET_DNSPARSER_TYPE_CNAME },
1049 { "SOA", GNUNET_DNSPARSER_TYPE_SOA },
1050 { "PTR", GNUNET_DNSPARSER_TYPE_PTR },
1051 { "MX", GNUNET_DNSPARSER_TYPE_MX },
1052 { "TXT", GNUNET_DNSPARSER_TYPE_TXT },
1053 { "AAAA", GNUNET_DNSPARSER_TYPE_AAAA },
1054 { "PKEY", GNUNET_NAMESTORE_TYPE_PKEY },
1055 { "PSEU", GNUNET_NAMESTORE_TYPE_PSEU },
1056 { "LEHO", GNUNET_NAMESTORE_TYPE_LEHO },
1057 { "VPN", GNUNET_NAMESTORE_TYPE_VPN },
1058 { "GNS2DNS", GNUNET_NAMESTORE_TYPE_GNS2DNS },
1059 { "TLSA", GNUNET_DNSPARSER_TYPE_TLSA },
1060 { NULL, UINT32_MAX }
1065 * Convert a type name (i.e. "AAAA") to the corresponding number.
1067 * @param typename name to convert
1068 * @return corresponding number, UINT32_MAX on error
1071 GNUNET_NAMESTORE_typename_to_number (const char *dns_typename)
1076 while ( (name_map[i].name != NULL) &&
1077 (0 != strcasecmp (dns_typename, name_map[i].name)) )
1079 return name_map[i].number;
1084 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
1086 * @param type number of a type to convert
1087 * @return corresponding typestring, NULL on error
1090 GNUNET_NAMESTORE_number_to_typename (uint32_t type)
1095 while ( (name_map[i].name != NULL) &&
1096 (type != name_map[i].number) )
1098 return name_map[i].name;
1103 * Test if a given record is expired.
1105 * @return #GNUNET_YES if the record is expired,
1109 GNUNET_NAMESTORE_is_expired (const struct GNUNET_NAMESTORE_RecordData *rd)
1111 struct GNUNET_TIME_Absolute at;
1113 if (0 != (rd->flags & GNUNET_NAMESTORE_RF_RELATIVE_EXPIRATION))
1115 at.abs_value_us = rd->expiration_time;
1116 return (0 == GNUNET_TIME_absolute_get_remaining (at).rel_value_us) ? GNUNET_YES : GNUNET_NO;
1121 * Calculate the DHT query for a given @a label in a given @a zone.
1123 * @param zone private key of the zone
1124 * @param label label of the record
1125 * @param query hash to use for the query
1128 GNUNET_NAMESTORE_query_from_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *zone,
1130 struct GNUNET_HashCode *query)
1132 struct GNUNET_CRYPTO_EccPublicSignKey pub;
1134 GNUNET_CRYPTO_ecc_key_get_public_for_signature (zone, &pub);
1135 GNUNET_NAMESTORE_query_from_public_key (&pub, label, query);
1140 * Calculate the DHT query for a given @a label in a given @a zone.
1142 * @param pub public key of the zone
1143 * @param label label of the record
1144 * @param query hash to use for the query
1147 GNUNET_NAMESTORE_query_from_public_key (const struct GNUNET_CRYPTO_EccPublicSignKey *pub,
1149 struct GNUNET_HashCode *query)
1151 struct GNUNET_CRYPTO_EccPublicSignKey pd;
1153 GNUNET_CRYPTO_ecc_public_key_derive (pub, label, "gns", &pd);
1154 GNUNET_CRYPTO_hash (&pd, sizeof (pd), query);
1159 * Convert public key to the respective absolute domain name in the
1161 * This is one of the very few calls in the entire API that is
1164 * @param pkey a public key with a point on the eliptic curve
1165 * @return string "X.zkey" where X is the public
1166 * key in an encoding suitable for DNS labels.
1169 GNUNET_NAMESTORE_pkey_to_zkey (const struct GNUNET_CRYPTO_EccPublicSignKey *pkey)
1171 static char ret[128];
1174 pkeys = GNUNET_CRYPTO_ecc_public_sign_key_to_string (pkey);
1175 GNUNET_snprintf (ret,
1179 GNUNET_free (pkeys);
1185 * Convert an absolute domain name in the ".zkey" pTLD to the
1186 * respective public key.
1188 * @param zkey string "X.zkey" where X is the coordinates of the public
1189 * key in an encoding suitable for DNS labels.
1190 * @param pkey set to a public key on the eliptic curve
1191 * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
1194 GNUNET_NAMESTORE_zkey_to_pkey (const char *zkey,
1195 struct GNUNET_CRYPTO_EccPublicSignKey *pkey)
1201 cpy = GNUNET_strdup (zkey);
1203 if (NULL == (dot = strchr (x, (int) '.')))
1206 if (0 != strcasecmp (dot + 1,
1211 GNUNET_CRYPTO_ecc_public_sign_key_from_string (x,
1219 return GNUNET_SYSERR;
1223 /* end of namestore_common.c */