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