2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
23 * @brief library for data block manipulation
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_block_lib.h"
32 * Check if the given KBlock is well-formed.
34 * @param kb the kblock data (or at least "dsize" bytes claiming to be one)
35 * @param dsize size of "kb" in bytes; check for < sizeof(struct KBlock)!
36 * @param query where to store the query that this block answers
37 * @return GNUNET_OK if this is actually a well-formed KBlock
40 check_kblock (const struct KBlock *kb,
42 GNUNET_HashCode *query)
44 if (dsize < sizeof (struct KBlock))
49 if (dsize - sizeof (struct KBlock) !=
50 ntohl (kb->purpose.size)
51 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
52 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
58 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_KBLOCK,
67 GNUNET_CRYPTO_hash (&kb->keyspace,
68 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
75 * Check if the given NBlock is well-formed.
77 * @param nb the nblock data (or at least "dsize" bytes claiming to be one)
78 * @param dsize size of "nb" in bytes; check for < sizeof(struct NBlock)!
79 * @param query where to store the query that this block answers
80 * @return GNUNET_OK if this is actually a well-formed NBlock
83 check_nblock (const struct NBlock *nb,
85 GNUNET_HashCode *query)
87 if (dsize < sizeof (struct NBlock))
92 if (dsize - sizeof (struct NBlock) !=
93 ntohl (nb->ns_purpose.size)
94 - sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose)
95 - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) )
101 ntohl (nb->ksk_purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
104 return GNUNET_SYSERR;
107 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK_KSIG,
113 return GNUNET_SYSERR;
116 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_NBLOCK,
122 return GNUNET_SYSERR;
125 GNUNET_CRYPTO_hash (&nb->keyspace,
126 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
133 * Check if the given SBlock is well-formed.
135 * @param sb the sblock data (or at least "dsize" bytes claiming to be one)
136 * @param dsize size of "kb" in bytes; check for < sizeof(struct SBlock)!
137 * @param query where to store the query that this block answers
138 * @return GNUNET_OK if this is actually a well-formed SBlock
141 check_sblock (const struct SBlock *sb,
143 GNUNET_HashCode *query)
145 if (dsize < sizeof (struct SBlock))
148 return GNUNET_SYSERR;
151 ntohl (sb->purpose.size) + sizeof (struct GNUNET_CRYPTO_RsaSignature))
154 return GNUNET_SYSERR;
157 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_FS_SBLOCK,
163 return GNUNET_SYSERR;
166 *query = sb->identifier;
172 * Check if the given block is well-formed (and of the given type).
174 * @param type type of the block
175 * @param block the block data (or at least "size" bytes claiming to be one)
176 * @param size size of "kb" in bytes; check that it is large enough
177 * @param query where to store the query that this block answers
178 * @return GNUNET_OK if this is actually a well-formed block,
179 * GNUNET_NO if we could not determine the query,
180 * GNUNET_SYSERR if the block is malformed
183 GNUNET_BLOCK_check_block (enum GNUNET_BLOCK_Type type,
186 GNUNET_HashCode *query)
188 /* first, validate! */
191 case GNUNET_BLOCK_TYPE_DBLOCK:
192 case GNUNET_BLOCK_TYPE_IBLOCK:
193 GNUNET_CRYPTO_hash (block, size, query);
195 case GNUNET_BLOCK_TYPE_KBLOCK:
200 return GNUNET_SYSERR;
202 case GNUNET_BLOCK_TYPE_SBLOCK:
207 return GNUNET_SYSERR;
209 case GNUNET_BLOCK_TYPE_NBLOCK:
214 return GNUNET_SYSERR;
216 case GNUNET_BLOCK_TYPE_ONDEMAND:
217 if (size != sizeof (struct OnDemandBlock))
218 return GNUNET_SYSERR;
219 memset (query, 0, sizeof (GNUNET_HashCode));
222 /* unknown block type */
224 return GNUNET_SYSERR;