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