GNUnet  0.10.x
fs_publish_ublock.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009, 2010, 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  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
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 
45 static void
48  const char *label,
49  const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
50 {
51  struct GNUNET_HashCode key;
52 
53  /* derive key from 'label' and public key of the namespace */
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 
73 void
74 GNUNET_FS_ublock_decrypt_ (const void *input,
75  size_t input_len,
76  const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
77  const char *label,
78  void *output)
79 {
82 
83  derive_ublock_encryption_key (&skey, &iv,
84  label, ns);
85  GNUNET_CRYPTO_symmetric_decrypt (input, input_len,
86  &skey, &iv,
87  output);
88 }
89 
90 
95 {
96 
101 
105  void *cont_cls;
106 
111 
116 
117 };
118 
119 
132 static void
133 ublock_put_cont (void *cls,
134  int32_t success,
136  const char *msg)
137 {
138  struct GNUNET_FS_PublishUblockContext *uc = cls;
139 
140  uc->qre = NULL;
141  uc->cont (uc->cont_cls, msg);
142  GNUNET_free (uc);
143 }
144 
145 
151 static void
152 run_cont (void *cls)
153 {
154  struct GNUNET_FS_PublishUblockContext *uc = cls;
155 
156  uc->task = NULL;
157  uc->cont (uc->cont_cls, NULL);
158  GNUNET_free (uc);
159 }
160 
161 
181  const char *label,
182  const char *ulabel,
183  const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
184  const struct GNUNET_CONTAINER_MetaData *meta,
185  const struct GNUNET_FS_Uri *uri,
186  const struct GNUNET_FS_BlockOptions *bo,
189 {
191  struct GNUNET_HashCode query;
194  struct GNUNET_CRYPTO_EcdsaPrivateKey *nsd;
195  struct GNUNET_CRYPTO_EcdsaPublicKey pub;
196  char *uris;
197  size_t size;
198  char *kbe;
199  char *sptr;
200  ssize_t mdsize;
201  size_t slen;
202  size_t ulen;
203  struct UBlock *ub_plain;
204  struct UBlock *ub_enc;
205 
206  /* compute ublock to publish */
207  if (NULL == meta)
208  mdsize = 0;
209  else
211  GNUNET_assert (mdsize >= 0);
212  uris = GNUNET_FS_uri_to_string (uri);
213  slen = strlen (uris) + 1;
214  if (NULL == ulabel)
215  ulen = 1;
216  else
217  ulen = strlen (ulabel) + 1;
218  size = mdsize + sizeof (struct UBlock) + slen + ulen;
219  if (size > MAX_UBLOCK_SIZE)
220  {
221  size = MAX_UBLOCK_SIZE;
222  mdsize = size - sizeof (struct UBlock) - (slen + ulen);
223  }
224  ub_plain = GNUNET_malloc (size);
225  kbe = (char *) &ub_plain[1];
226  if (NULL != ulabel)
227  GNUNET_memcpy (kbe, ulabel, ulen);
228  kbe += ulen;
229  GNUNET_memcpy (kbe, uris, slen);
230  kbe += slen;
231  GNUNET_free (uris);
232  sptr = kbe;
233  if (NULL != meta)
234  mdsize =
235  GNUNET_CONTAINER_meta_data_serialize (meta, &sptr, mdsize,
237  if (-1 == mdsize)
238  {
239  GNUNET_break (0);
240  GNUNET_free (ub_plain);
241  cont (cont_cls, _("Internal error."));
242  return NULL;
243  }
244  size = sizeof (struct UBlock) + slen + mdsize + ulen;
245 
247  "Publishing under identifier `%s'\n",
248  label);
249  /* get public key of the namespace */
251  &pub);
252  derive_ublock_encryption_key (&skey, &iv,
253  label, &pub);
254 
255  /* encrypt ublock */
256  ub_enc = GNUNET_malloc (size);
257  GNUNET_CRYPTO_symmetric_encrypt (&ub_plain[1],
258  ulen + slen + mdsize,
259  &skey, &iv,
260  &ub_enc[1]);
261  GNUNET_free (ub_plain);
262  ub_enc->purpose.size = htonl (ulen + slen + mdsize +
263  sizeof (struct UBlock)
264  - sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
266 
267  /* derive signing-key from 'label' and public key of the namespace */
268  nsd = GNUNET_CRYPTO_ecdsa_private_key_derive (ns, label, "fs-ublock");
270  &ub_enc->verification_key);
273  &ub_enc->purpose,
274  &ub_enc->signature));
276  sizeof (ub_enc->verification_key),
277  &query);
278  GNUNET_free (nsd);
279 
281  uc->cont = cont;
282  uc->cont_cls = cont_cls;
283  if (NULL != dsh)
284  {
285  uc->qre =
287  0,
288  &query,
289  ulen + slen + mdsize + sizeof (struct UBlock),
290  ub_enc,
292  bo->content_priority,
293  bo->anonymity_level,
294  bo->replication_level,
295  bo->expiration_time,
296  -2, 1,
297  &ublock_put_cont, uc);
298  }
299  else
300  {
302  uc);
303  }
304  GNUNET_free (ub_enc);
305  return uc;
306 }
307 
308 
314 void
316 {
317  if (NULL != uc->qre)
319  if (NULL != uc->task)
321  GNUNET_free (uc);
322 }
323 
324 /* end of fs_publish_ublock.c */
struct GNUNET_TIME_Absolute expiration_time
At what time should the block expire? Data blocks (DBLOCKS and IBLOCKS) may still be used even if the...
ssize_t GNUNET_CONTAINER_meta_data_get_serialized_size(const struct GNUNET_CONTAINER_MetaData *md)
Get the size of the full meta-data in serialized form.
static struct GNUNET_TIME_Absolute min_expiration
Minimum time that content should have to not be discarded instantly (time stamp of any content that w...
GNUNET_FS_UBlockContinuation cont
Function to call when done.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
ssize_t GNUNET_CONTAINER_meta_data_serialize(const struct GNUNET_CONTAINER_MetaData *md, char **target, size_t max, enum GNUNET_CONTAINER_MetaDataSerializationOptions opt)
Serialize meta-data to target.
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
If not enough space is available, it is acceptable to only serialize some of the metadata.
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
Master context for most FS operations.
Definition: fs_api.h:1087
struct GNUNET_DATASTORE_QueueEntry * GNUNET_DATASTORE_put(struct GNUNET_DATASTORE_Handle *h, uint32_t rid, const struct GNUNET_HashCode *key, size_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, unsigned int queue_priority, unsigned int max_queue_size, GNUNET_DATASTORE_ContinuationWithStatus cont, void *cont_cls)
Store an item in the datastore.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_FS_ublock_decrypt_(const void *input, size_t input_len, const struct GNUNET_CRYPTO_EcdsaPublicKey *ns, const char *label, void *output)
Decrypt the given UBlock, storing the result in output.
static struct GNUNET_FS_UnindexContext * uc
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static void ublock_put_cont(void *cls, int32_t success, struct GNUNET_TIME_Absolute min_expiration, const char *msg)
Continuation of GNUNET_FS_publish_ublock_().
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Private ECC key encoded for transmission.
struct GNUNET_DATASTORE_QueueEntry * qre
Handle for active datastore operation.
#define GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK
UBlock Signature, done using DSS, not ECC.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
char * GNUNET_FS_uri_to_string(const struct GNUNET_FS_Uri *uri)
Convert a URI to a UTF-8 String.
Definition: fs_uri.c:1988
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
uint32_t anonymity_level
At which anonymity level should the block be shared? (0: no anonymity, 1: normal GAP, >1: with cover traffic).
int GNUNET_CRYPTO_ecdsa_sign(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, struct GNUNET_CRYPTO_EcdsaSignature *sig)
ECDSA Sign a given block.
Definition: crypto_ecc.c:931
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature using pseudonym and search keyword / identifier.
Definition: block_fs.h:56
Meta data to associate with a file, directory or namespace.
Entry in our priority queue.
#define GNUNET_memcpy(dst, src, n)
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
ssize_t GNUNET_CRYPTO_symmetric_encrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Encrypt a block using a symmetric sessionkey.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1273
an ECC signature using ECDSA
GNUNET_FS_PublishOptions
Options for publishing.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
Type of a block representing any type of search result (universal).
A 512-bit hashcode.
void GNUNET_CRYPTO_hash_to_aes_key(const struct GNUNET_HashCode *hc, struct GNUNET_CRYPTO_SymmetricSessionKey *skey, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv)
Convert a hashcode into a key.
Definition: crypto_hash.c:212
void GNUNET_FS_publish_ublock_cancel_(struct GNUNET_FS_PublishUblockContext *uc)
Abort UBlock publishing operation.
static unsigned int size
Size of the "table".
Definition: peer.c:67
void GNUNET_CRYPTO_ecdsa_key_get_public(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:241
struct GNUNET_FS_PublishUblockContext * GNUNET_FS_publish_ublock_(struct GNUNET_FS_Handle *h, struct GNUNET_DATASTORE_Handle *dsh, const char *label, const char *ulabel, const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns, const struct GNUNET_CONTAINER_MetaData *meta, const struct GNUNET_FS_Uri *uri, const struct GNUNET_FS_BlockOptions *bo, enum GNUNET_FS_PublishOptions options, GNUNET_FS_UBlockContinuation cont, void *cont_cls)
Publish a UBlock.
Settings for publishing a block (which may of course also apply to an entire directory or file)...
struct GNUNET_SCHEDULER_Task * task
Task to run continuation asynchronously.
static struct GNUNET_NAMESTORE_Handle * ns
Handle to the namestore.
uint32_t replication_level
How often should we try to migrate the block to other peers? Only used if "CONTENT_PUSHING" is set to...
void GNUNET_DATASTORE_cancel(struct GNUNET_DATASTORE_QueueEntry *qe)
Cancel a datastore operation.
shared definitions for the FS library
Context for &#39;ublock_put_cont&#39;.
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:168
static struct GNUNET_CRYPTO_EddsaPublicKey pub
Definition: gnunet-scrypt.c:39
Handle to the datastore service.
static struct GNUNET_CONTAINER_MetaData * meta
Meta-data provided via command-line option.
static struct GNUNET_DATASTORE_Handle * dsh
Datastore handle.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
uint32_t content_priority
How important is it for us to store the block? If we run out of space, the highest-priority, non-expired blocks will be kept.
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
static void run_cont(void *cls)
Run the continuation.
#define MAX_UBLOCK_SIZE
Maximum legal size for a ublock.
Definition: block_fs.h:41
struct GNUNET_CRYPTO_EcdsaPrivateKey * GNUNET_CRYPTO_ecdsa_private_key_derive(const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv, const char *label, const char *context)
Derive a private key from a given private key and a label.
Definition: crypto_ecc.c:1295
void(* GNUNET_FS_UBlockContinuation)(void *cls, const char *emsg)
Signature of a function called as the continuation of a UBlock publication.
ssize_t GNUNET_CRYPTO_symmetric_decrypt(const void *block, size_t size, const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey, const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, void *result)
Decrypt a given block using a symmetric sessionkey.
static void derive_ublock_encryption_key(struct GNUNET_CRYPTO_SymmetricSessionKey *skey, struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, const char *label, const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
Derive the key for symmetric encryption/decryption from the public key and the label.
void * cont_cls
Closure of &#39;cont&#39;.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
What is being signed and why?
Definition: block_fs.h:61
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:91
struct GNUNET_CRYPTO_EcdsaPublicKey verification_key
Public key used to sign this block.
Definition: block_fs.h:66
universal block for keyword and namespace search results
Definition: block_fs.h:50
#define GNUNET_malloc(size)
Wrapper around malloc.
static struct GNUNET_FS_BlockOptions bo
Options we set for published blocks.
#define GNUNET_free(ptr)
Wrapper around free.
Merkle-tree-ish-CHK file encoding for GNUnet.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965
publish a UBLOCK in GNUnet