Basic Functionality Implemented
[oweals/gnunet.git] / src / include / gnunet_gnsrecord_lib.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2012, 2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Christian Grothoff
23  *
24  * @file
25  * API that can be used to manipulate GNS record data
26  *
27  * @defgroup gnsrecord  GNS Record library
28  * Manipulate GNS record data
29  *
30  * @see [Documentation](https://gnunet.org/gns-plugins)
31  *
32  * @{
33  */
34 #ifndef GNUNET_GNSRECORD_LIB_H
35 #define GNUNET_GNSRECORD_LIB_H
36
37 #ifdef __cplusplus
38 extern "C" {
39 #if 0 /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44 /**
45  * Maximum size of a value that can be stored in a GNS block.
46  */
47 #define GNUNET_GNSRECORD_MAX_BLOCK_SIZE (63 * 1024)
48
49
50 /**
51  * Record type indicating any record/'*'
52  */
53 #define GNUNET_GNSRECORD_TYPE_ANY 0
54
55 /**
56  * Record type for GNS zone transfer ("PKEY").
57  */
58 #define GNUNET_GNSRECORD_TYPE_PKEY 65536
59
60 /**
61  * Record type for GNS nick names ("NICK").
62  */
63 #define GNUNET_GNSRECORD_TYPE_NICK 65537
64
65 /**
66  * Record type for GNS legacy hostnames ("LEHO").
67  */
68 #define GNUNET_GNSRECORD_TYPE_LEHO 65538
69
70 /**
71  * Record type for VPN resolution
72  */
73 #define GNUNET_GNSRECORD_TYPE_VPN 65539
74
75 /**
76  * Record type for delegation to DNS.
77  */
78 #define GNUNET_GNSRECORD_TYPE_GNS2DNS 65540
79
80 /**
81  * Record type for a boxed record (see TLSA/SRV handling in GNS).
82  */
83 #define GNUNET_GNSRECORD_TYPE_BOX 65541
84
85 /**
86  * Record type for a social place.
87  */
88 #define GNUNET_GNSRECORD_TYPE_PLACE 65542
89
90 /**
91  * Record type for a phone (of CONVERSATION).
92  */
93 #define GNUNET_GNSRECORD_TYPE_PHONE 65543
94
95 /**
96  * Record type for identity attributes (of RECLAIM).
97  */
98 #define GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR 65544
99
100 /**
101  * Record type for local ticket references
102  */
103 #define GNUNET_GNSRECORD_TYPE_RECLAIM_TICKET 65545
104
105 /**
106  * 65546 reserved
107  */
108
109 /**
110  * 65547 deleted, free to use
111  */
112
113 /**
114  * Record type for policies
115  */
116 #define GNUNET_GNSRECORD_TYPE_DELEGATE 65548
117
118 /**
119  * Record type for reverse lookups
120  */
121 #define GNUNET_GNSRECORD_TYPE_ATTRIBUTE 65549
122
123 /**
124  * Record type for reclaim records
125  */
126 #define GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR_REF 65550
127
128 /**
129  * Record type for RECLAIM master
130  */
131 #define GNUNET_GNSRECORD_TYPE_RECLAIM_MASTER 65551
132
133 /**
134  * Record type for reclaim OIDC clients
135  */
136 #define GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT 65552
137
138 /**
139  * Record type for reclaim OIDC redirect URIs
140  */
141 #define GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT 65553
142
143 /**
144  * Record type for reclaim identity attestation
145  */
146 #define GNUNET_GNSRECORD_TYPE_RECLAIM_ATTEST_ATTR 65554
147
148 /**
149  * Record type for reclaim identity references
150  */
151 #define GNUNET_GNSRECORD_TYPE_RECLAIM_REFERENCE 65555
152
153
154 /**
155  * Flags that can be set for a record.
156  */
157 enum GNUNET_GNSRECORD_Flags
158 {
159   /**
160    * No special options.
161    */
162   GNUNET_GNSRECORD_RF_NONE = 0,
163
164   /**
165    * This is a private record of this peer and it should
166    * thus not be handed out to other peers.
167    */
168   GNUNET_GNSRECORD_RF_PRIVATE = 2,
169
170   /**
171    * This flag is currently unused; former RF_PENDING flag
172    *
173    * GNUNET_GNSRECORD_RF_UNUSED = 4,
174    */
175
176   /**
177    * This expiration time of the record is a relative
178    * time (not an absolute time).
179    */
180   GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8,
181
182   /**
183    * This record should not be used unless all (other) records with an absolute
184    * expiration time have expired.
185    */
186   GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16
187
188 /**
189  * When comparing flags for record equality for removal,
190  * which flags should must match (in addition to the type,
191  * name, expiration value and data of the record)?  All flags
192  * that are not listed here will be ignored for this purpose.
193  * (for example, we don't expect that users will remember to
194  * pass the '--private' option when removing a record from
195  * the namestore, hence we don't require this particular option
196  * to match upon removal).  See also
197  * #GNUNET_GNSRECORD_records_cmp.
198  */
199 #define GNUNET_GNSRECORD_RF_RCMP_FLAGS (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)
200 };
201
202
203 /**
204  * A GNS record.
205  */
206 struct GNUNET_GNSRECORD_Data
207 {
208   /**
209    * Binary value stored in the DNS record.  Note: "data" must never
210    * be individually 'malloc'ed, but instead always points into some
211    * existing data area.
212    */
213   const void *data;
214
215   /**
216    * Expiration time for the DNS record.  Can be relative
217    * or absolute, depending on @e flags.  Measured in the same
218    * unit as GNUnet time (microseconds).
219    */
220   uint64_t expiration_time;
221
222   /**
223    * Number of bytes in @e data.
224    */
225   size_t data_size;
226
227   /**
228    * Type of the GNS/DNS record.
229    */
230   uint32_t record_type;
231
232   /**
233    * Flags for the record.
234    */
235   enum GNUNET_GNSRECORD_Flags flags;
236 };
237
238
239 GNUNET_NETWORK_STRUCT_BEGIN
240
241 /**
242  * Data stored in a PLACE record.
243  */
244 struct GNUNET_GNSRECORD_PlaceData
245 {
246   /**
247    * Public key of the place.
248    */
249   struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
250
251   /**
252    * Peer identity of the origin.
253    */
254   struct GNUNET_PeerIdentity origin;
255
256   /**
257    * Number of relays that follow.
258    */
259   uint32_t relay_count GNUNET_PACKED;
260
261   /* Followed by struct GNUNET_PeerIdentity relays[relay_count] */
262 };
263
264
265 /**
266  * Information we have in an encrypted block with record data (i.e. in the DHT).
267  */
268 struct GNUNET_GNSRECORD_Block
269 {
270   /**
271    * Signature of the block.
272    */
273   struct GNUNET_CRYPTO_EcdsaSignature signature;
274
275   /**
276    * Derived key used for signing; hash of this is the query.
277    */
278   struct GNUNET_CRYPTO_EcdsaPublicKey derived_key;
279
280   /**
281    * Number of bytes signed; also specifies the number of bytes
282    * of encrypted data that follow.
283    */
284   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
285
286   /**
287    * Expiration time of the block.
288    */
289   struct GNUNET_TIME_AbsoluteNBO expiration_time;
290
291   /* followed by encrypted data */
292 };
293
294
295 /**
296  * Record type used to box up SRV and TLSA records.  For example, a
297  * TLSA record for "_https._tcp.foo.gnu" will be stored under
298  * "foo.gnu" as a BOX record with service 443 (https) and protocol 6
299  * (tcp) and record_type "TLSA".  When a BOX record is received, GNS
300  * unboxes it if the name contained "_SERVICE._PROTO", otherwise GNS
301  * leaves it untouched.  This is done to ensure that TLSA (and SRV)
302  * records do not require a separate network request, thus making TLSA
303  * records inseparable from the "main" A/AAAA/VPN/etc. records.
304  */
305 struct GNUNET_GNSRECORD_BoxRecord
306 {
307   /**
308    * Protocol of the boxed record (6 = TCP, 17 = UDP, etc.).
309    * Yes, in IP protocols are usually limited to 8 bits. In NBO.
310    */
311   uint16_t protocol GNUNET_PACKED;
312
313   /**
314    * Service of the boxed record (aka port number), in NBO.
315    */
316   uint16_t service GNUNET_PACKED;
317
318   /**
319    * GNS record type of the boxed record. In NBO.
320    */
321   uint32_t record_type GNUNET_PACKED;
322
323   /* followed by the 'original' record */
324 };
325
326 /**
327  * Record type used internally to keep track of reverse mappings into a
328  * namespace.
329  * The record contains data related to PKEY delegations from other namespaces to
330  * the namespace the record belongs to.
331  * It is exclusively found under the label ``+''.
332  */
333 struct GNUNET_GNSRECORD_ReverseRecord
334 {
335   /**
336    * The public key of the namespace the is delegating to our namespace
337    */
338   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
339
340   /**
341    * The expiration time of the delegation
342    */
343   struct GNUNET_TIME_Absolute expiration;
344
345   /* followed by the name the delegator uses to refer to our namespace */
346 };
347
348 GNUNET_NETWORK_STRUCT_END
349
350
351 /**
352  * Process a records that were decrypted from a block.
353  *
354  * @param cls closure
355  * @param rd_count number of entries in @a rd array
356  * @param rd array of records with data to store
357  */
358 typedef void (*GNUNET_GNSRECORD_RecordCallback) (
359   void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd);
360
361
362 /* ***************** API related to GNSRECORD plugins ************** */
363
364 /**
365  * Convert the binary value @a data of a record of
366  * type @a type to a human-readable string.
367  *
368  * @param type type of the record
369  * @param data value in binary encoding
370  * @param data_size number of bytes in @a data
371  * @return NULL on error, otherwise human-readable representation of the value
372  */
373 char *
374 GNUNET_GNSRECORD_value_to_string (uint32_t type, const void *data,
375                                   size_t data_size);
376
377
378 /**
379  * Convert human-readable version of the value @a s of a record
380  * of type @a type to the respective binary representation.
381  *
382  * @param type type of the record
383  * @param s human-readable string
384  * @param data set to value in binary encoding (will be allocated)
385  * @param data_size set to number of bytes in @a data
386  * @return #GNUNET_OK on success
387  */
388 int
389 GNUNET_GNSRECORD_string_to_value (uint32_t type, const char *s, void **data,
390                                   size_t *data_size);
391
392
393 /**
394  * Convert a type name (i.e. "AAAA") to the corresponding number.
395  *
396  * @param dns_typename name to convert
397  * @return corresponding number, UINT32_MAX on error
398  */
399 uint32_t
400 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
401
402
403 /**
404  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
405  *
406  * @param type number of a type to convert
407  * @return corresponding typestring, NULL on error
408  */
409 const char *
410 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
411
412
413 /* convenience APIs for serializing / deserializing GNS records */
414
415 /**
416  * Calculate how many bytes we will need to serialize the given
417  * records.
418  *
419  * @param rd_count number of records in the @a rd array
420  * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
421  * @return the required size to serialize, -1 on error
422  */
423 ssize_t
424 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
425                                    const struct GNUNET_GNSRECORD_Data *rd);
426
427
428 /**
429  * Serialize the given records to the given destination buffer.
430  *
431  * @param rd_count number of records in the @a rd array
432  * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
433  * @param dest_size size of the destination array @a dst
434  * @param dest where to write the result
435  * @return the size of serialized records, -1 if records do not fit
436  */
437 ssize_t
438 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
439                                     const struct GNUNET_GNSRECORD_Data *rd,
440                                     size_t dest_size, char *dest);
441
442
443 /**
444  * Deserialize the given records to the given destination.
445  *
446  * @param len size of the serialized record data
447  * @param src the serialized record data
448  * @param rd_count number of records in the @a dest array
449  * @param dest where to put the data
450  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
451  */
452 int
453 GNUNET_GNSRECORD_records_deserialize (size_t len, const char *src,
454                                       unsigned int rd_count,
455                                       struct GNUNET_GNSRECORD_Data *dest);
456
457
458 /* ******* general APIs relating to blocks, records and labels ******** */
459
460
461 /**
462  * Test if a given record is expired.
463  *
464  * @param rd record to test
465  * @return #GNUNET_YES if the record is expired,
466  *         #GNUNET_NO if not
467  */
468 int
469 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd);
470
471
472 /**
473  * Convert a UTF-8 string to UTF-8 lowercase
474  * @param src source string
475  * @return converted result
476  */
477 char *
478 GNUNET_GNSRECORD_string_to_lowercase (const char *src);
479
480
481 /**
482  * Convert a zone to a string (for printing debug messages).
483  * This is one of the very few calls in the entire API that is
484  * NOT reentrant!
485  *
486  * @param z public key of a zone
487  * @return string form; will be overwritten by next call to
488  * #GNUNET_GNSRECORD_z2s.
489  */
490 const char *
491 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
492
493
494 /**
495  * Convert public key to the respective absolute domain name in the
496  * ".zkey" pTLD.
497  * This is one of the very few calls in the entire API that is
498  * NOT reentrant!
499  *
500  * @param pkey a public key with a point on the eliptic curve
501  * @return string "X.zkey" where X is the coordinates of the public
502  *         key in an encoding suitable for DNS labels.
503  */
504 const char *
505 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
506
507
508 /**
509  * Convert an absolute domain name to the
510  * respective public key.
511  *
512  * @param zkey string "X" where X is the public
513  *         key in an encoding suitable for DNS labels.
514  * @param pkey set to a public key on the eliptic curve
515  * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
516  */
517 int
518 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
519                                struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
520
521
522 /**
523  * Calculate the DHT query for a given @a label in a given @a zone.
524  *
525  * @param zone private key of the zone
526  * @param label label of the record
527  * @param query hash to use for the query
528  */
529 void
530 GNUNET_GNSRECORD_query_from_private_key (
531   const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label,
532   struct GNUNET_HashCode *query);
533
534
535 /**
536  * Calculate the DHT query for a given @a label in a given @a zone.
537  *
538  * @param pub public key of the zone
539  * @param label label of the record
540  * @param query hash to use for the query
541  */
542 void
543 GNUNET_GNSRECORD_query_from_public_key (
544   const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label,
545   struct GNUNET_HashCode *query);
546
547
548 /**
549  * Sign name and records
550  *
551  * @param key the private key
552  * @param expire block expiration
553  * @param label the name for the records
554  * @param rd record data
555  * @param rd_count number of records in @a rd
556  */
557 struct GNUNET_GNSRECORD_Block *
558 GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
559                                struct GNUNET_TIME_Absolute expire,
560                                const char *label,
561                                const struct GNUNET_GNSRECORD_Data *rd,
562                                unsigned int rd_count);
563
564
565 /**
566  * Sign name and records, cache derived public key (also keeps the
567  * private key in static memory, so do not use this function if
568  * keeping the private key in the process'es RAM is a major issue).
569  *
570  * @param key the private key
571  * @param expire block expiration
572  * @param label the name for the records
573  * @param rd record data
574  * @param rd_count number of records in @a rd
575  */
576 struct GNUNET_GNSRECORD_Block *
577 GNUNET_GNSRECORD_block_create2 (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
578                                 struct GNUNET_TIME_Absolute expire,
579                                 const char *label,
580                                 const struct GNUNET_GNSRECORD_Data *rd,
581                                 unsigned int rd_count);
582
583
584 /**
585  * Check if a signature is valid.  This API is used by the GNS Block
586  * to validate signatures received from the network.
587  *
588  * @param block block to verify
589  * @return #GNUNET_OK if the signature is valid
590  */
591 int
592 GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block);
593
594
595 /**
596  * Decrypt block.
597  *
598  * @param block block to decrypt
599  * @param zone_key public key of the zone
600  * @param label the name for the records
601  * @param proc function to call with the result
602  * @param proc_cls closure for @a proc
603  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
604  *        not well-formed
605  */
606 int
607 GNUNET_GNSRECORD_block_decrypt (
608   const struct GNUNET_GNSRECORD_Block *block,
609   const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, const char *label,
610   GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls);
611
612
613 /**
614  * Compares if two records are equal
615  *
616  * @param a a record
617  * @param b another record
618  * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
619  */
620 int
621 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
622                               const struct GNUNET_GNSRECORD_Data *b);
623
624
625 /**
626  * Returns the expiration time of the given block of records. The block
627  * expiration time is the expiration time of the record with smallest
628  * expiration time.
629  *
630  * @param rd_count number of records given in @a rd
631  * @param rd array of records
632  * @return absolute expiration time
633  */
634 struct GNUNET_TIME_Absolute
635 GNUNET_GNSRECORD_record_get_expiration_time (
636   unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd);
637
638
639 #if 0 /* keep Emacsens' auto-indent happy */
640 {
641 #endif
642 #ifdef __cplusplus
643 }
644 #endif
645
646 #endif
647
648 /** @} */ /* end of group */