aboutsummaryrefslogtreecommitdiff
path: root/src/fs/fs_publish_ublock.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/fs_publish_ublock.c')
-rw-r--r--src/fs/fs_publish_ublock.c247
1 files changed, 122 insertions, 125 deletions
diff --git a/src/fs/fs_publish_ublock.c b/src/fs/fs_publish_ublock.c
index 368a4aff4..4dfb4a585 100644
--- a/src/fs/fs_publish_ublock.c
+++ b/src/fs/fs_publish_ublock.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file fs/fs_publish_ublock.c 22 * @file fs/fs_publish_ublock.c
@@ -43,21 +43,21 @@
43 * @param pub public key to use for key derivation 43 * @param pub public key to use for key derivation
44 */ 44 */
45static void 45static void
46derive_ublock_encryption_key (struct GNUNET_CRYPTO_SymmetricSessionKey *skey, 46derive_ublock_encryption_key(struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
47 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv, 47 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
48 const char *label, 48 const char *label,
49 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub) 49 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
50{ 50{
51 struct GNUNET_HashCode key; 51 struct GNUNET_HashCode key;
52 52
53 /* derive key from 'label' and public key of the namespace */ 53 /* derive key from 'label' and public key of the namespace */
54 GNUNET_assert (GNUNET_YES == 54 GNUNET_assert(GNUNET_YES ==
55 GNUNET_CRYPTO_kdf (&key, sizeof (key), 55 GNUNET_CRYPTO_kdf(&key, sizeof(key),
56 "UBLOCK-ENC", strlen ("UBLOCK-ENC"), 56 "UBLOCK-ENC", strlen("UBLOCK-ENC"),
57 label, strlen (label), 57 label, strlen(label),
58 pub, sizeof (*pub), 58 pub, sizeof(*pub),
59 NULL, 0)); 59 NULL, 0));
60 GNUNET_CRYPTO_hash_to_aes_key (&key, skey, iv); 60 GNUNET_CRYPTO_hash_to_aes_key(&key, skey, iv);
61} 61}
62 62
63 63
@@ -71,29 +71,27 @@ derive_ublock_encryption_key (struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
71 * @param output where to write the result, has input_len bytes 71 * @param output where to write the result, has input_len bytes
72 */ 72 */
73void 73void
74GNUNET_FS_ublock_decrypt_ (const void *input, 74GNUNET_FS_ublock_decrypt_(const void *input,
75 size_t input_len, 75 size_t input_len,
76 const struct GNUNET_CRYPTO_EcdsaPublicKey *ns, 76 const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
77 const char *label, 77 const char *label,
78 void *output) 78 void *output)
79{ 79{
80 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 80 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
81 struct GNUNET_CRYPTO_SymmetricSessionKey skey; 81 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
82 82
83 derive_ublock_encryption_key (&skey, &iv, 83 derive_ublock_encryption_key(&skey, &iv,
84 label, ns); 84 label, ns);
85 GNUNET_CRYPTO_symmetric_decrypt (input, input_len, 85 GNUNET_CRYPTO_symmetric_decrypt(input, input_len,
86 &skey, &iv, 86 &skey, &iv,
87 output); 87 output);
88} 88}
89 89
90 90
91/** 91/**
92 * Context for 'ublock_put_cont'. 92 * Context for 'ublock_put_cont'.
93 */ 93 */
94struct GNUNET_FS_PublishUblockContext 94struct GNUNET_FS_PublishUblockContext {
95{
96
97 /** 95 /**
98 * Function to call when done. 96 * Function to call when done.
99 */ 97 */
@@ -113,7 +111,6 @@ struct GNUNET_FS_PublishUblockContext
113 * Task to run continuation asynchronously. 111 * Task to run continuation asynchronously.
114 */ 112 */
115 struct GNUNET_SCHEDULER_Task * task; 113 struct GNUNET_SCHEDULER_Task * task;
116
117}; 114};
118 115
119 116
@@ -130,16 +127,16 @@ struct GNUNET_FS_PublishUblockContext
130 * @param msg NULL on success, otherwise an error message 127 * @param msg NULL on success, otherwise an error message
131 */ 128 */
132static void 129static void
133ublock_put_cont (void *cls, 130ublock_put_cont(void *cls,
134 int32_t success, 131 int32_t success,
135 struct GNUNET_TIME_Absolute min_expiration, 132 struct GNUNET_TIME_Absolute min_expiration,
136 const char *msg) 133 const char *msg)
137{ 134{
138 struct GNUNET_FS_PublishUblockContext *uc = cls; 135 struct GNUNET_FS_PublishUblockContext *uc = cls;
139 136
140 uc->qre = NULL; 137 uc->qre = NULL;
141 uc->cont (uc->cont_cls, msg); 138 uc->cont(uc->cont_cls, msg);
142 GNUNET_free (uc); 139 GNUNET_free(uc);
143} 140}
144 141
145 142
@@ -149,13 +146,13 @@ ublock_put_cont (void *cls,
149 * @param cls the `struct GNUNET_FS_PublishUblockContext *` 146 * @param cls the `struct GNUNET_FS_PublishUblockContext *`
150 */ 147 */
151static void 148static void
152run_cont (void *cls) 149run_cont(void *cls)
153{ 150{
154 struct GNUNET_FS_PublishUblockContext *uc = cls; 151 struct GNUNET_FS_PublishUblockContext *uc = cls;
155 152
156 uc->task = NULL; 153 uc->task = NULL;
157 uc->cont (uc->cont_cls, NULL); 154 uc->cont(uc->cont_cls, NULL);
158 GNUNET_free (uc); 155 GNUNET_free(uc);
159} 156}
160 157
161 158
@@ -176,16 +173,16 @@ run_cont (void *cls)
176 * @return NULL on error (@a cont will still be called) 173 * @return NULL on error (@a cont will still be called)
177 */ 174 */
178struct GNUNET_FS_PublishUblockContext * 175struct GNUNET_FS_PublishUblockContext *
179GNUNET_FS_publish_ublock_ (struct GNUNET_FS_Handle *h, 176GNUNET_FS_publish_ublock_(struct GNUNET_FS_Handle *h,
180 struct GNUNET_DATASTORE_Handle *dsh, 177 struct GNUNET_DATASTORE_Handle *dsh,
181 const char *label, 178 const char *label,
182 const char *ulabel, 179 const char *ulabel,
183 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns, 180 const struct GNUNET_CRYPTO_EcdsaPrivateKey *ns,
184 const struct GNUNET_CONTAINER_MetaData *meta, 181 const struct GNUNET_CONTAINER_MetaData *meta,
185 const struct GNUNET_FS_Uri *uri, 182 const struct GNUNET_FS_Uri *uri,
186 const struct GNUNET_FS_BlockOptions *bo, 183 const struct GNUNET_FS_BlockOptions *bo,
187 enum GNUNET_FS_PublishOptions options, 184 enum GNUNET_FS_PublishOptions options,
188 GNUNET_FS_UBlockContinuation cont, void *cont_cls) 185 GNUNET_FS_UBlockContinuation cont, void *cont_cls)
189{ 186{
190 struct GNUNET_FS_PublishUblockContext *uc; 187 struct GNUNET_FS_PublishUblockContext *uc;
191 struct GNUNET_HashCode query; 188 struct GNUNET_HashCode query;
@@ -207,101 +204,101 @@ GNUNET_FS_publish_ublock_ (struct GNUNET_FS_Handle *h,
207 if (NULL == meta) 204 if (NULL == meta)
208 mdsize = 0; 205 mdsize = 0;
209 else 206 else
210 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size (meta); 207 mdsize = GNUNET_CONTAINER_meta_data_get_serialized_size(meta);
211 GNUNET_assert (mdsize >= 0); 208 GNUNET_assert(mdsize >= 0);
212 uris = GNUNET_FS_uri_to_string (uri); 209 uris = GNUNET_FS_uri_to_string(uri);
213 slen = strlen (uris) + 1; 210 slen = strlen(uris) + 1;
214 if (NULL == ulabel) 211 if (NULL == ulabel)
215 ulen = 1; 212 ulen = 1;
216 else 213 else
217 ulen = strlen (ulabel) + 1; 214 ulen = strlen(ulabel) + 1;
218 size = mdsize + sizeof (struct UBlock) + slen + ulen; 215 size = mdsize + sizeof(struct UBlock) + slen + ulen;
219 if (size > MAX_UBLOCK_SIZE) 216 if (size > MAX_UBLOCK_SIZE)
220 { 217 {
221 size = MAX_UBLOCK_SIZE; 218 size = MAX_UBLOCK_SIZE;
222 mdsize = size - sizeof (struct UBlock) - (slen + ulen); 219 mdsize = size - sizeof(struct UBlock) - (slen + ulen);
223 } 220 }
224 ub_plain = GNUNET_malloc (size); 221 ub_plain = GNUNET_malloc(size);
225 kbe = (char *) &ub_plain[1]; 222 kbe = (char *)&ub_plain[1];
226 if (NULL != ulabel) 223 if (NULL != ulabel)
227 GNUNET_memcpy (kbe, ulabel, ulen); 224 GNUNET_memcpy(kbe, ulabel, ulen);
228 kbe += ulen; 225 kbe += ulen;
229 GNUNET_memcpy (kbe, uris, slen); 226 GNUNET_memcpy(kbe, uris, slen);
230 kbe += slen; 227 kbe += slen;
231 GNUNET_free (uris); 228 GNUNET_free(uris);
232 sptr = kbe; 229 sptr = kbe;
233 if (NULL != meta) 230 if (NULL != meta)
234 mdsize = 231 mdsize =
235 GNUNET_CONTAINER_meta_data_serialize (meta, &sptr, mdsize, 232 GNUNET_CONTAINER_meta_data_serialize(meta, &sptr, mdsize,
236 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART); 233 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
237 if (-1 == mdsize) 234 if (-1 == mdsize)
238 { 235 {
239 GNUNET_break (0); 236 GNUNET_break(0);
240 GNUNET_free (ub_plain); 237 GNUNET_free(ub_plain);
241 cont (cont_cls, _("Internal error.")); 238 cont(cont_cls, _("Internal error."));
242 return NULL; 239 return NULL;
243 } 240 }
244 size = sizeof (struct UBlock) + slen + mdsize + ulen; 241 size = sizeof(struct UBlock) + slen + mdsize + ulen;
245 242
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 243 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
247 "Publishing under identifier `%s'\n", 244 "Publishing under identifier `%s'\n",
248 label); 245 label);
249 /* get public key of the namespace */ 246 /* get public key of the namespace */
250 GNUNET_CRYPTO_ecdsa_key_get_public (ns, 247 GNUNET_CRYPTO_ecdsa_key_get_public(ns,
251 &pub); 248 &pub);
252 derive_ublock_encryption_key (&skey, &iv, 249 derive_ublock_encryption_key(&skey, &iv,
253 label, &pub); 250 label, &pub);
254 251
255 /* encrypt ublock */ 252 /* encrypt ublock */
256 ub_enc = GNUNET_malloc (size); 253 ub_enc = GNUNET_malloc(size);
257 GNUNET_CRYPTO_symmetric_encrypt (&ub_plain[1], 254 GNUNET_CRYPTO_symmetric_encrypt(&ub_plain[1],
258 ulen + slen + mdsize, 255 ulen + slen + mdsize,
259 &skey, &iv, 256 &skey, &iv,
260 &ub_enc[1]); 257 &ub_enc[1]);
261 GNUNET_free (ub_plain); 258 GNUNET_free(ub_plain);
262 ub_enc->purpose.size = htonl (ulen + slen + mdsize + 259 ub_enc->purpose.size = htonl(ulen + slen + mdsize +
263 sizeof (struct UBlock) 260 sizeof(struct UBlock)
264 - sizeof (struct GNUNET_CRYPTO_EcdsaSignature)); 261 - sizeof(struct GNUNET_CRYPTO_EcdsaSignature));
265 ub_enc->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK); 262 ub_enc->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_FS_UBLOCK);
266 263
267 /* derive signing-key from 'label' and public key of the namespace */ 264 /* derive signing-key from 'label' and public key of the namespace */
268 nsd = GNUNET_CRYPTO_ecdsa_private_key_derive (ns, label, "fs-ublock"); 265 nsd = GNUNET_CRYPTO_ecdsa_private_key_derive(ns, label, "fs-ublock");
269 GNUNET_CRYPTO_ecdsa_key_get_public (nsd, 266 GNUNET_CRYPTO_ecdsa_key_get_public(nsd,
270 &ub_enc->verification_key); 267 &ub_enc->verification_key);
271 GNUNET_assert (GNUNET_OK == 268 GNUNET_assert(GNUNET_OK ==
272 GNUNET_CRYPTO_ecdsa_sign (nsd, 269 GNUNET_CRYPTO_ecdsa_sign(nsd,
273 &ub_enc->purpose, 270 &ub_enc->purpose,
274 &ub_enc->signature)); 271 &ub_enc->signature));
275 GNUNET_CRYPTO_hash (&ub_enc->verification_key, 272 GNUNET_CRYPTO_hash(&ub_enc->verification_key,
276 sizeof (ub_enc->verification_key), 273 sizeof(ub_enc->verification_key),
277 &query); 274 &query);
278 GNUNET_free (nsd); 275 GNUNET_free(nsd);
279 276
280 uc = GNUNET_new (struct GNUNET_FS_PublishUblockContext); 277 uc = GNUNET_new(struct GNUNET_FS_PublishUblockContext);
281 uc->cont = cont; 278 uc->cont = cont;
282 uc->cont_cls = cont_cls; 279 uc->cont_cls = cont_cls;
283 if (NULL != dsh) 280 if (NULL != dsh)
284 { 281 {
285 uc->qre = 282 uc->qre =
286 GNUNET_DATASTORE_put (dsh, 283 GNUNET_DATASTORE_put(dsh,
287 0, 284 0,
288 &query, 285 &query,
289 ulen + slen + mdsize + sizeof (struct UBlock), 286 ulen + slen + mdsize + sizeof(struct UBlock),
290 ub_enc, 287 ub_enc,
291 GNUNET_BLOCK_TYPE_FS_UBLOCK, 288 GNUNET_BLOCK_TYPE_FS_UBLOCK,
292 bo->content_priority, 289 bo->content_priority,
293 bo->anonymity_level, 290 bo->anonymity_level,
294 bo->replication_level, 291 bo->replication_level,
295 bo->expiration_time, 292 bo->expiration_time,
296 -2, 1, 293 -2, 1,
297 &ublock_put_cont, uc); 294 &ublock_put_cont, uc);
298 } 295 }
299 else 296 else
300 { 297 {
301 uc->task = GNUNET_SCHEDULER_add_now (&run_cont, 298 uc->task = GNUNET_SCHEDULER_add_now(&run_cont,
302 uc); 299 uc);
303 } 300 }
304 GNUNET_free (ub_enc); 301 GNUNET_free(ub_enc);
305 return uc; 302 return uc;
306} 303}
307 304
@@ -312,13 +309,13 @@ GNUNET_FS_publish_ublock_ (struct GNUNET_FS_Handle *h,
312 * @param uc operation to abort. 309 * @param uc operation to abort.
313 */ 310 */
314void 311void
315GNUNET_FS_publish_ublock_cancel_ (struct GNUNET_FS_PublishUblockContext *uc) 312GNUNET_FS_publish_ublock_cancel_(struct GNUNET_FS_PublishUblockContext *uc)
316{ 313{
317 if (NULL != uc->qre) 314 if (NULL != uc->qre)
318 GNUNET_DATASTORE_cancel (uc->qre); 315 GNUNET_DATASTORE_cancel(uc->qre);
319 if (NULL != uc->task) 316 if (NULL != uc->task)
320 GNUNET_SCHEDULER_cancel (uc->task); 317 GNUNET_SCHEDULER_cancel(uc->task);
321 GNUNET_free (uc); 318 GNUNET_free(uc);
322} 319}
323 320
324/* end of fs_publish_ublock.c */ 321/* end of fs_publish_ublock.c */