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