massive refactoring to intruce data structure
[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  * Record type for credential
111  */
112 #define GNUNET_GNSRECORD_TYPE_CREDENTIAL 65547
113
114 /**
115  * Record type for policies
116  */
117 #define GNUNET_GNSRECORD_TYPE_POLICY 65548
118
119 /**
120  * Record type for reverse lookups
121  */
122 #define GNUNET_GNSRECORD_TYPE_ATTRIBUTE 65549
123
124 /**
125  * Record type for reclaim records
126  */
127 #define GNUNET_GNSRECORD_TYPE_RECLAIM_ATTR_REF 65550
128
129 /**
130  * Record type for RECLAIM master
131  */
132 #define GNUNET_GNSRECORD_TYPE_RECLAIM_MASTER 65551
133
134 /**
135  * Record type for reclaim OIDC clients
136  */
137 #define GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_CLIENT 65552
138
139 /**
140  * Record type for reclaim OIDC redirect URIs
141  */
142 #define GNUNET_GNSRECORD_TYPE_RECLAIM_OIDC_REDIRECT 65553
143
144 /**
145  * Flags that can be set for a record.
146  */
147 enum GNUNET_GNSRECORD_Flags {
148
149   /**
150    * No special options.
151    */
152   GNUNET_GNSRECORD_RF_NONE = 0,
153
154   /**
155    * This is a private record of this peer and it should
156    * thus not be handed out to other peers.
157    */
158   GNUNET_GNSRECORD_RF_PRIVATE = 2,
159
160   /**
161    * This flag is currently unused; former RF_PENDING flag
162    *
163    * GNUNET_GNSRECORD_RF_UNUSED = 4,
164    */
165
166   /**
167    * This expiration time of the record is a relative
168    * time (not an absolute time).
169    */
170   GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION = 8,
171
172   /**
173    * This record should not be used unless all (other) records with an absolute
174    * expiration time have expired.
175    */
176   GNUNET_GNSRECORD_RF_SHADOW_RECORD = 16
177
178 /**
179  * When comparing flags for record equality for removal,
180  * which flags should must match (in addition to the type,
181  * name, expiration value and data of the record)?  All flags
182  * that are not listed here will be ignored for this purpose.
183  * (for example, we don't expect that users will remember to
184  * pass the '--private' option when removing a record from
185  * the namestore, hence we don't require this particular option
186  * to match upon removal).  See also
187  * #GNUNET_GNSRECORD_records_cmp.
188  */
189 #define GNUNET_GNSRECORD_RF_RCMP_FLAGS (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)
190 };
191
192
193 /**
194  * A GNS record.
195  */
196 struct GNUNET_GNSRECORD_Data {
197
198   /**
199    * Binary value stored in the DNS record.  Note: "data" must never
200    * be individually 'malloc'ed, but instead always points into some
201    * existing data area.
202    */
203   const void *data;
204
205   /**
206    * Expiration time for the DNS record.  Can be relative
207    * or absolute, depending on @e flags.  Measured in the same
208    * unit as GNUnet time (microseconds).
209    */
210   uint64_t expiration_time;
211
212   /**
213    * Number of bytes in @e data.
214    */
215   size_t data_size;
216
217   /**
218    * Type of the GNS/DNS record.
219    */
220   uint32_t record_type;
221
222   /**
223    * Flags for the record.
224    */
225   enum GNUNET_GNSRECORD_Flags flags;
226 };
227
228
229 GNUNET_NETWORK_STRUCT_BEGIN
230
231 /**
232  * Data stored in a PLACE record.
233  */
234 struct GNUNET_GNSRECORD_PlaceData {
235   /**
236    * Public key of the place.
237    */
238   struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
239
240   /**
241    * Peer identity of the origin.
242    */
243   struct GNUNET_PeerIdentity origin;
244
245   /**
246    * Number of relays that follow.
247    */
248   uint32_t relay_count GNUNET_PACKED;
249
250   /* Followed by struct GNUNET_PeerIdentity relays[relay_count] */
251 };
252
253
254 /**
255  * Information we have in an encrypted block with record data (i.e. in the DHT).
256  */
257 struct GNUNET_GNSRECORD_Block {
258
259   /**
260    * Signature of the block.
261    */
262   struct GNUNET_CRYPTO_EcdsaSignature signature;
263
264   /**
265    * Derived key used for signing; hash of this is the query.
266    */
267   struct GNUNET_CRYPTO_EcdsaPublicKey derived_key;
268
269   /**
270    * Number of bytes signed; also specifies the number of bytes
271    * of encrypted data that follow.
272    */
273   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
274
275   /**
276    * Expiration time of the block.
277    */
278   struct GNUNET_TIME_AbsoluteNBO expiration_time;
279
280   /* followed by encrypted data */
281 };
282
283
284 /**
285  * Record type used to box up SRV and TLSA records.  For example, a
286  * TLSA record for "_https._tcp.foo.gnu" will be stored under
287  * "foo.gnu" as a BOX record with service 443 (https) and protocol 6
288  * (tcp) and record_type "TLSA".  When a BOX record is received, GNS
289  * unboxes it if the name contained "_SERVICE._PROTO", otherwise GNS
290  * leaves it untouched.  This is done to ensure that TLSA (and SRV)
291  * records do not require a separate network request, thus making TLSA
292  * records inseparable from the "main" A/AAAA/VPN/etc. records.
293  */
294 struct GNUNET_GNSRECORD_BoxRecord {
295
296   /**
297    * Protocol of the boxed record (6 = TCP, 17 = UDP, etc.).
298    * Yes, in IP protocols are usually limited to 8 bits. In NBO.
299    */
300   uint16_t protocol GNUNET_PACKED;
301
302   /**
303    * Service of the boxed record (aka port number), in NBO.
304    */
305   uint16_t service GNUNET_PACKED;
306
307   /**
308    * GNS record type of the boxed record. In NBO.
309    */
310   uint32_t record_type GNUNET_PACKED;
311
312   /* followed by the 'original' record */
313 };
314
315 /**
316  * Record type used internally to keep track of reverse mappings into a
317  * namespace.
318  * The record contains data related to PKEY delegations from other namespaces to
319  * the namespace the record belongs to.
320  * It is exclusively found under the label ``+''.
321  */
322 struct GNUNET_GNSRECORD_ReverseRecord {
323   /**
324    * The public key of the namespace the is delegating to our namespace
325    */
326   struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
327
328   /**
329    * The expiration time of the delegation
330    */
331   struct GNUNET_TIME_Absolute expiration;
332
333   /* followed by the name the delegator uses to refer to our namespace */
334 };
335
336 GNUNET_NETWORK_STRUCT_END
337
338
339 /**
340  * Process a records that were decrypted from a block.
341  *
342  * @param cls closure
343  * @param rd_count number of entries in @a rd array
344  * @param rd array of records with data to store
345  */
346 typedef void (*GNUNET_GNSRECORD_RecordCallback) (
347     void *cls, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd);
348
349
350 /* ***************** API related to GNSRECORD plugins ************** */
351
352 /**
353  * Convert the binary value @a data of a record of
354  * type @a type to a human-readable string.
355  *
356  * @param type type of the record
357  * @param data value in binary encoding
358  * @param data_size number of bytes in @a data
359  * @return NULL on error, otherwise human-readable representation of the value
360  */
361 char *
362 GNUNET_GNSRECORD_value_to_string (uint32_t type, const void *data,
363                                   size_t data_size);
364
365
366 /**
367  * Convert human-readable version of the value @a s of a record
368  * of type @a type to the respective binary representation.
369  *
370  * @param type type of the record
371  * @param s human-readable string
372  * @param data set to value in binary encoding (will be allocated)
373  * @param data_size set to number of bytes in @a data
374  * @return #GNUNET_OK on success
375  */
376 int
377 GNUNET_GNSRECORD_string_to_value (uint32_t type, const char *s, void **data,
378                                   size_t *data_size);
379
380
381 /**
382  * Convert a type name (i.e. "AAAA") to the corresponding number.
383  *
384  * @param dns_typename name to convert
385  * @return corresponding number, UINT32_MAX on error
386  */
387 uint32_t
388 GNUNET_GNSRECORD_typename_to_number (const char *dns_typename);
389
390
391 /**
392  * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
393  *
394  * @param type number of a type to convert
395  * @return corresponding typestring, NULL on error
396  */
397 const char *
398 GNUNET_GNSRECORD_number_to_typename (uint32_t type);
399
400
401 /* convenience APIs for serializing / deserializing GNS records */
402
403 /**
404  * Calculate how many bytes we will need to serialize the given
405  * records.
406  *
407  * @param rd_count number of records in the @a rd array
408  * @param rd array of #GNUNET_GNSRECORD_Data with @a rd_count elements
409  * @return the required size to serialize, -1 on error
410  */
411 ssize_t
412 GNUNET_GNSRECORD_records_get_size (unsigned int rd_count,
413                                    const struct GNUNET_GNSRECORD_Data *rd);
414
415
416 /**
417  * Serialize the given records to the given destination buffer.
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  * @param dest_size size of the destination array @a dst
422  * @param dest where to write the result
423  * @return the size of serialized records, -1 if records do not fit
424  */
425 ssize_t
426 GNUNET_GNSRECORD_records_serialize (unsigned int rd_count,
427                                     const struct GNUNET_GNSRECORD_Data *rd,
428                                     size_t dest_size, char *dest);
429
430
431 /**
432  * Deserialize the given records to the given destination.
433  *
434  * @param len size of the serialized record data
435  * @param src the serialized record data
436  * @param rd_count number of records in the @a dest array
437  * @param dest where to put the data
438  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
439  */
440 int
441 GNUNET_GNSRECORD_records_deserialize (size_t len, const char *src,
442                                       unsigned int rd_count,
443                                       struct GNUNET_GNSRECORD_Data *dest);
444
445
446 /* ******* general APIs relating to blocks, records and labels ******** */
447
448
449 /**
450  * Test if a given record is expired.
451  *
452  * @param rd record to test
453  * @return #GNUNET_YES if the record is expired,
454  *         #GNUNET_NO if not
455  */
456 int
457 GNUNET_GNSRECORD_is_expired (const struct GNUNET_GNSRECORD_Data *rd);
458
459
460 /**
461  * Convert a UTF-8 string to UTF-8 lowercase
462  * @param src source string
463  * @return converted result
464  */
465 char *
466 GNUNET_GNSRECORD_string_to_lowercase (const char *src);
467
468
469 /**
470  * Convert a zone to a string (for printing debug messages).
471  * This is one of the very few calls in the entire API that is
472  * NOT reentrant!
473  *
474  * @param z public key of a zone
475  * @return string form; will be overwritten by next call to
476  * #GNUNET_GNSRECORD_z2s.
477  */
478 const char *
479 GNUNET_GNSRECORD_z2s (const struct GNUNET_CRYPTO_EcdsaPublicKey *z);
480
481
482 /**
483  * Convert public key to the respective absolute domain name in the
484  * ".zkey" pTLD.
485  * This is one of the very few calls in the entire API that is
486  * NOT reentrant!
487  *
488  * @param pkey a public key with a point on the eliptic curve
489  * @return string "X.zkey" where X is the coordinates of the public
490  *         key in an encoding suitable for DNS labels.
491  */
492 const char *
493 GNUNET_GNSRECORD_pkey_to_zkey (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
494
495
496 /**
497  * Convert an absolute domain name to the
498  * respective public key.
499  *
500  * @param zkey string "X" where X is the public
501  *         key in an encoding suitable for DNS labels.
502  * @param pkey set to a public key on the eliptic curve
503  * @return #GNUNET_SYSERR if @a zkey has the wrong syntax
504  */
505 int
506 GNUNET_GNSRECORD_zkey_to_pkey (const char *zkey,
507                                struct GNUNET_CRYPTO_EcdsaPublicKey *pkey);
508
509
510 /**
511  * Calculate the DHT query for a given @a label in a given @a zone.
512  *
513  * @param zone private key of the zone
514  * @param label label of the record
515  * @param query hash to use for the query
516  */
517 void
518 GNUNET_GNSRECORD_query_from_private_key (
519     const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label,
520     struct GNUNET_HashCode *query);
521
522
523 /**
524  * Calculate the DHT query for a given @a label in a given @a zone.
525  *
526  * @param pub public key of the zone
527  * @param label label of the record
528  * @param query hash to use for the query
529  */
530 void
531 GNUNET_GNSRECORD_query_from_public_key (
532     const struct GNUNET_CRYPTO_EcdsaPublicKey *pub, const char *label,
533     struct GNUNET_HashCode *query);
534
535
536 /**
537  * Sign name and records
538  *
539  * @param key the private key
540  * @param expire block expiration
541  * @param label the name for the records
542  * @param rd record data
543  * @param rd_count number of records in @a rd
544  */
545 struct GNUNET_GNSRECORD_Block *
546 GNUNET_GNSRECORD_block_create (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
547                                struct GNUNET_TIME_Absolute expire,
548                                const char *label,
549                                const struct GNUNET_GNSRECORD_Data *rd,
550                                unsigned int rd_count);
551
552
553 /**
554  * Sign name and records, cache derived public key (also keeps the
555  * private key in static memory, so do not use this function if
556  * keeping the private key in the process'es RAM is a major issue).
557  *
558  * @param key the private key
559  * @param expire block expiration
560  * @param label the name for the records
561  * @param rd record data
562  * @param rd_count number of records in @a rd
563  */
564 struct GNUNET_GNSRECORD_Block *
565 GNUNET_GNSRECORD_block_create2 (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
566                                 struct GNUNET_TIME_Absolute expire,
567                                 const char *label,
568                                 const struct GNUNET_GNSRECORD_Data *rd,
569                                 unsigned int rd_count);
570
571
572 /**
573  * Check if a signature is valid.  This API is used by the GNS Block
574  * to validate signatures received from the network.
575  *
576  * @param block block to verify
577  * @return #GNUNET_OK if the signature is valid
578  */
579 int
580 GNUNET_GNSRECORD_block_verify (const struct GNUNET_GNSRECORD_Block *block);
581
582
583 /**
584  * Decrypt block.
585  *
586  * @param block block to decrypt
587  * @param zone_key public key of the zone
588  * @param label the name for the records
589  * @param proc function to call with the result
590  * @param proc_cls closure for @a proc
591  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the block was
592  *        not well-formed
593  */
594 int
595 GNUNET_GNSRECORD_block_decrypt (
596     const struct GNUNET_GNSRECORD_Block *block,
597     const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key, const char *label,
598     GNUNET_GNSRECORD_RecordCallback proc, void *proc_cls);
599
600
601 /**
602  * Compares if two records are equal
603  *
604  * @param a a record
605  * @param b another record
606  * @return #GNUNET_YES if the records are equal, or #GNUNET_NO if not.
607  */
608 int
609 GNUNET_GNSRECORD_records_cmp (const struct GNUNET_GNSRECORD_Data *a,
610                               const struct GNUNET_GNSRECORD_Data *b);
611
612
613 /**
614  * Returns the expiration time of the given block of records. The block
615  * expiration time is the expiration time of the record with smallest
616  * expiration time.
617  *
618  * @param rd_count number of records given in @a rd
619  * @param rd array of records
620  * @return absolute expiration time
621  */
622 struct GNUNET_TIME_Absolute
623 GNUNET_GNSRECORD_record_get_expiration_time (
624     unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd);
625
626
627 #if 0 /* keep Emacsens' auto-indent happy */
628 {
629 #endif
630 #ifdef __cplusplus
631 }
632 #endif
633
634 #endif
635
636 /** @} */ /* end of group */