multicast: added replay_end(), returning replay handle from join_decision(); removed...
[oweals/gnunet.git] / src / fs / fs_publish_ublock.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2012, 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file fs/fs_publish_ublock.c
23  * @brief publish a UBLOCK in GNUnet
24  * @see https://gnunet.org/encoding and #2564 
25  * @author Krista Bennett
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_signatures.h"
31 #include "fs_publish_ublock.h"
32 #include "fs_api.h"
33 #include "fs_tree.h"
34
35
36 /**
37  * Derive the key for symmetric encryption/decryption from
38  * the public key and the label.
39  *
40  * @param skey where to store symmetric key
41  * @param iv where to store the IV
42  * @param label label to use for key derivation
43  * @param pub public key to use for key derivation
44  */ 
45 static void
46 derive_ublock_encryption_key (struct GNUNET_CRYPTO_AesSessionKey *skey,
47                               struct GNUNET_CRYPTO_AesInitializationVector *iv,
48                               const char *label,
49                               const struct GNUNET_CRYPTO_EccPublicKey *pub)
50 {
51   struct GNUNET_HashCode key;
52
53   /* derive key from 'label' and public key of the namespace */
54   GNUNET_assert (GNUNET_YES ==
55                  GNUNET_CRYPTO_kdf (&key, sizeof (key),
56                                     "UBLOCK-ENC", strlen ("UBLOCK-ENC"),
57                                     label, strlen (label),
58                                     pub, sizeof (*pub),
59                                     NULL, 0));
60   GNUNET_CRYPTO_hash_to_aes_key (&key, skey, iv);
61 }
62
63
64 /**
65  * Decrypt the given UBlock, storing the result in output.
66  *
67  * @param input input data
68  * @param input_len number of bytes in input
69  * @param ns public key under which the UBlock was stored
70  * @param label label under which the UBlock was stored
71  * @param output where to write the result, has input_len bytes
72  */ 
73 void
74 GNUNET_FS_ublock_decrypt_ (const void *input,
75                            size_t input_len,
76                            const struct GNUNET_CRYPTO_EccPublicKey *ns,
77                            const char *label,
78                            void *output)
79 {
80   struct GNUNET_CRYPTO_AesInitializationVector iv;
81   struct GNUNET_CRYPTO_AesSessionKey skey;
82
83   derive_ublock_encryption_key (&skey, &iv,
84                                 label, ns);
85   GNUNET_CRYPTO_aes_decrypt (input, input_len,
86                              &skey, &iv,
87                              output);
88 }
89
90
91 /**
92  * Context for 'ublock_put_cont'.
93  */
94 struct GNUNET_FS_PublishUblockContext
95 {
96
97   /**
98    * Function to call when done.
99    */
100   GNUNET_FS_UBlockContinuation cont;
101
102   /**
103    * Closure of 'cont'.
104    */
105   void *cont_cls;
106
107   /**
108    * Handle for active datastore operation.
109    */
110   struct GNUNET_DATASTORE_QueueEntry *qre;
111 };
112
113
114 /**
115  * Continuation of "GNUNET_FS_publish_ublock_".
116  *
117  * @param cls closure of type "struct GNUNET_FS_PublishUblockContext*"
118  * @param success GNUNET_SYSERR on failure (including timeout/queue drop)
119  *                GNUNET_NO if content was already there
120  *                GNUNET_YES (or other positive value) on success
121  * @param min_expiration minimum expiration time required for 0-priority content to be stored
122  *                by the datacache at this time, zero for unknown, forever if we have no
123  *                space for 0-priority content
124  * @param msg NULL on success, otherwise an error message
125  */
126 static void
127 ublock_put_cont (void *cls, 
128                  int32_t success,
129                  struct GNUNET_TIME_Absolute min_expiration,
130                  const char *msg)
131 {
132   struct GNUNET_FS_PublishUblockContext *uc = cls;
133
134   uc->qre = NULL;
135   uc->cont (uc->cont_cls, msg);
136   GNUNET_free (uc);
137 }
138
139
140 /**
141  * Publish a UBlock.
142  *
143  * @param h handle to the file sharing subsystem
144  * @param dsh datastore handle to use for storage operation
145  * @param label identifier to use
146  * @param ulabel update label to use, may be an empty string for none
147  * @param ns namespace to publish in
148  * @param meta metadata to use
149  * @param uri URI to refer to in the UBlock
150  * @param bo per-block options
151  * @param options publication options
152  * @param cont continuation
153  * @param cont_cls closure for cont
154  * @return NULL on error ('cont' will still be called)
155  */
156 struct GNUNET_FS_PublishUblockContext *
157 GNUNET_FS_publish_ublock_ (struct GNUNET_FS_Handle *h,
158                            struct GNUNET_DATASTORE_Handle *dsh,
159                            const char *label,
160                            const char *ulabel,
161                            const struct GNUNET_CRYPTO_EccPrivateKey *ns,
162                            const struct GNUNET_CONTAINER_MetaData *meta,
163                            const struct GNUNET_FS_Uri *uri,
164                            const struct GNUNET_FS_BlockOptions *bo,
165                            enum GNUNET_FS_PublishOptions options,
166                            GNUNET_FS_UBlockContinuation cont, void *cont_cls)
167 {
168   struct GNUNET_FS_PublishUblockContext *uc;
169   struct GNUNET_HashCode query;
170   struct GNUNET_CRYPTO_AesInitializationVector iv;
171   struct GNUNET_CRYPTO_AesSessionKey skey;
172   struct GNUNET_CRYPTO_EccPrivateKey *nsd;
173   struct GNUNET_CRYPTO_EccPublicKey pub;
174   char *uris;
175   size_t size;
176   char *kbe;
177   char *sptr;
178   ssize_t mdsize;
179   size_t slen;
180   size_t ulen;
181   struct UBlock *ub_plain;
182   struct UBlock *ub_enc;
183
184   /* compute ublock to publish */
185   if (NULL == meta)
186     mdsize = 0;
187   else
188     mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
189   GNUNET_assert (mdsize >= 0);
190   uris = GNUNET_FS_uri_to_string (uri);
191   slen = strlen (uris) + 1;
192   if (NULL == ulabel)
193     ulen = 1;
194   else
195     ulen = strlen (ulabel) + 1;
196   size = mdsize + sizeof (struct UBlock) + slen + ulen;
197   if (size > MAX_UBLOCK_SIZE)
198   {
199     size = MAX_UBLOCK_SIZE;
200     mdsize = size - sizeof (struct UBlock) - (slen + ulen);
201   }
202   ub_plain = GNUNET_malloc (size);
203   kbe = (char *) &ub_plain[1];
204   if (NULL != ulabel)
205     memcpy (kbe, ulabel, ulen);
206   kbe += ulen;
207   memcpy (kbe, uris, slen);
208   kbe += slen;
209   GNUNET_free (uris);
210   sptr = kbe;
211   if (NULL != meta)
212     mdsize =
213       GNUNET_CONTAINER_meta_data_serialize (meta, &sptr, mdsize,
214                                             GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
215   if (-1 == mdsize)
216   {
217     GNUNET_break (0);
218     GNUNET_free (ub_plain);
219     cont (cont_cls, _("Internal error."));
220     return NULL;
221   }
222   size = sizeof (struct UBlock) + slen + mdsize + ulen;
223
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
225               "Publishing under identifier `%s'\n",
226               label);
227   /* get public key of the namespace */
228   GNUNET_CRYPTO_ecc_key_get_public (ns,
229                                     &pub);
230   derive_ublock_encryption_key (&skey, &iv,
231                                 label, &pub);
232
233   /* encrypt ublock */
234   ub_enc = GNUNET_malloc (size);
235   GNUNET_CRYPTO_aes_encrypt (&ub_plain[1], 
236                              ulen + slen + mdsize,
237                              &skey, &iv,
238                              &ub_enc[1]);
239   ub_enc->purpose.size = htonl (ulen + slen + mdsize + 
240                                 sizeof (struct UBlock)
241                                 - sizeof (struct GNUNET_CRYPTO_EccSignature));
242   ub_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK);
243
244   /* derive signing-key from 'label' and public key of the namespace */
245   nsd = GNUNET_CRYPTO_ecc_key_derive (ns, label, "fs-ublock");
246   GNUNET_CRYPTO_ecc_key_get_public (nsd,
247                                     &ub_enc->verification_key);
248   GNUNET_assert (GNUNET_OK ==
249                  GNUNET_CRYPTO_ecc_sign (nsd,
250                                          &ub_enc->purpose,
251                                          &ub_enc->signature));
252   GNUNET_CRYPTO_hash (&ub_enc->verification_key,
253                       sizeof (ub_enc->verification_key),
254                       &query);
255   GNUNET_free (nsd);
256
257   uc = GNUNET_new (struct GNUNET_FS_PublishUblockContext);
258   uc->cont = cont;
259   uc->cont_cls = cont_cls;
260   uc->qre =
261     GNUNET_DATASTORE_put (dsh, 0, &query,
262                           ulen + slen + mdsize + sizeof (struct UBlock),
263                           ub_enc, GNUNET_BLOCK_TYPE_FS_UBLOCK,
264                           bo->content_priority, bo->anonymity_level,
265                           bo->replication_level, bo->expiration_time,
266                           -2, 1, GNUNET_CONSTANTS_SERVICE_TIMEOUT,
267                           &ublock_put_cont, uc);
268   return uc;
269 }
270
271
272 /**
273  * Abort UBlock publishing operation.
274  *
275  * @param uc operation to abort.
276  */
277 void
278 GNUNET_FS_publish_ublock_cancel_ (struct GNUNET_FS_PublishUblockContext *uc)
279 {
280   GNUNET_DATASTORE_cancel (uc->qre);
281   GNUNET_free (uc);
282 }