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