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