2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016 GNUnet e.V.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @file credential/credential_serialization.c
24 * @brief API to serialize and deserialize delegation chains
26 * @author Martin Schanzenbach
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_credential_service.h"
33 GNUNET_NETWORK_STRUCT_BEGIN
35 struct DelegationRecordData
40 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
45 uint32_t subject_attribute_len GNUNET_PACKED;
54 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key;
59 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
64 uint32_t issuer_attribute_len GNUNET_PACKED;
69 uint32_t subject_attribute_len GNUNET_PACKED;
72 GNUNET_NETWORK_STRUCT_END
76 * Calculate how many bytes we will need to serialize
77 * the given delegation chain and credential
79 * @param d_count number of delegation chain entries
80 * @param dd array of #GNUNET_CREDENTIAL_Delegation
81 * @param cd a #GNUNET_CREDENTIAL_Credential
82 * @return the required size to serialize
85 GNUNET_CREDENTIAL_delegation_set_get_size (unsigned int ds_count,
86 const struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr)
91 ret = sizeof (struct DelegationRecordData) * (ds_count);
93 for (i=0; i<ds_count;i++)
95 GNUNET_assert ((ret + dsr[i].subject_attribute_len) >= ret);
96 ret += dsr[i].subject_attribute_len;
102 * Serizalize the given delegation chain entries and credential
104 * @param d_count number of delegation chain entries
105 * @param dd array of #GNUNET_CREDENTIAL_Delegation
106 * @param cd a #GNUNET_CREDENTIAL_Credential
107 * @param dest_size size of the destination
108 * @param dest where to store the result
109 * @return the size of the data, -1 on failure
112 GNUNET_CREDENTIAL_delegation_set_serialize (unsigned int d_count,
113 const struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr,
117 struct DelegationRecordData rec;
122 for (i=0;i<d_count;i++)
124 rec.subject_attribute_len = htonl ((uint32_t) dsr[i].subject_attribute_len);
125 rec.subject_key = dsr[i].subject_key;
126 if (off + sizeof (rec) > dest_size)
128 GNUNET_memcpy (&dest[off],
132 if (0 == dsr[i].subject_attribute_len)
134 if (off + dsr[i].subject_attribute_len > dest_size)
136 GNUNET_memcpy (&dest[off],
137 dsr[i].subject_attribute,
138 dsr[i].subject_attribute_len);
139 off += dsr[i].subject_attribute_len;
146 * Deserialize the given destination
148 * @param len size of the serialized delegation chain and cred
149 * @param src the serialized data
150 * @param d_count the number of delegation chain entries
151 * @param dd where to put the delegation chain entries
152 * @param cd where to put the credential data
153 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
156 GNUNET_CREDENTIAL_delegation_set_deserialize (size_t len,
158 unsigned int d_count,
159 struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr)
161 struct DelegationRecordData rec;
166 for (i=0;i<d_count;i++)
168 if (off + sizeof (rec) > len)
169 return GNUNET_SYSERR;
170 GNUNET_memcpy (&rec, &src[off], sizeof (rec));
171 dsr[i].subject_key = rec.subject_key;
173 dsr[i].subject_attribute_len = ntohl ((uint32_t) rec.subject_attribute_len);
174 if (off + dsr[i].subject_attribute_len > len)
175 return GNUNET_SYSERR;
176 dsr[i].subject_attribute = &src[off];
177 off += dsr[i].subject_attribute_len;
182 * Calculate how many bytes we will need to serialize
183 * the given delegation chain and credential
185 * @param d_count number of delegation chain entries
186 * @param dd array of #GNUNET_CREDENTIAL_Delegation
187 * @param c_count number of credential entries
188 * @param cd a #GNUNET_CREDENTIAL_Credential
189 * @return the required size to serialize
192 GNUNET_CREDENTIAL_delegation_chain_get_size (unsigned int d_count,
193 const struct GNUNET_CREDENTIAL_Delegation *dd,
194 unsigned int c_count,
195 const struct GNUNET_CREDENTIAL_Credential *cd)
200 ret = sizeof (struct ChainEntry) * (d_count);
201 ret += sizeof (struct ChainEntry) * (c_count);
203 for (i=0; i<d_count;i++)
205 GNUNET_assert ((ret +
206 dd[i].issuer_attribute_len +
207 dd[i].subject_attribute_len) >= ret);
208 ret += dd[i].issuer_attribute_len + dd[i].subject_attribute_len;
210 for (i=0; i<c_count;i++)
212 GNUNET_assert ((ret + cd[i].issuer_attribute_len) >= ret);
213 ret += cd[i].issuer_attribute_len;
219 * Serizalize the given delegation chain entries and credential
221 * @param d_count number of delegation chain entries
222 * @param dd array of #GNUNET_CREDENTIAL_Delegation
223 * @param c_count number of credential entries
224 * @param cd a #GNUNET_CREDENTIAL_Credential
225 * @param dest_size size of the destination
226 * @param dest where to store the result
227 * @return the size of the data, -1 on failure
230 GNUNET_CREDENTIAL_delegation_chain_serialize (unsigned int d_count,
231 const struct GNUNET_CREDENTIAL_Delegation *dd,
232 unsigned int c_count,
233 const struct GNUNET_CREDENTIAL_Credential *cd,
237 struct ChainEntry rec;
242 for (i=0;i<d_count;i++)
244 rec.issuer_attribute_len = htonl ((uint32_t) dd[i].issuer_attribute_len);
245 rec.subject_attribute_len = htonl ((uint32_t) dd[i].subject_attribute_len);
246 rec.issuer_key = dd[i].issuer_key;
247 rec.subject_key = dd[i].subject_key;
248 if (off + sizeof (rec) > dest_size)
250 GNUNET_memcpy (&dest[off],
254 if (off + dd[i].issuer_attribute_len > dest_size)
256 GNUNET_memcpy (&dest[off],
257 dd[i].issuer_attribute,
258 dd[i].issuer_attribute_len);
259 off += dd[i].issuer_attribute_len;
260 if (0 == dd[i].subject_attribute_len)
262 if (off + dd[i].subject_attribute_len > dest_size)
264 GNUNET_memcpy (&dest[off],
265 dd[i].subject_attribute,
266 dd[i].subject_attribute_len);
267 off += dd[i].subject_attribute_len;
269 for (i=0;i<c_count;i++)
271 rec.issuer_attribute_len = htonl ((uint32_t) cd[i].issuer_attribute_len);
272 rec.subject_attribute_len = htonl (0);
273 rec.issuer_key = cd[i].issuer_key;
274 rec.subject_key = cd[i].subject_key;
275 if (off + sizeof (rec) > dest_size)
277 GNUNET_memcpy (&dest[off],
281 if (off + cd[i].issuer_attribute_len > dest_size)
283 GNUNET_memcpy (&dest[off],
284 cd[i].issuer_attribute,
285 cd[i].issuer_attribute_len);
286 off += cd[i].issuer_attribute_len;
294 * Deserialize the given destination
296 * @param len size of the serialized delegation chain and cred
297 * @param src the serialized data
298 * @param d_count the number of delegation chain entries
299 * @param dd where to put the delegation chain entries
300 * @param c_count the number of credential entries
301 * @param cd where to put the credential data
302 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
305 GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len,
307 unsigned int d_count,
308 struct GNUNET_CREDENTIAL_Delegation *dd,
309 unsigned int c_count,
310 struct GNUNET_CREDENTIAL_Credential *cd)
312 struct ChainEntry rec;
317 for (i=0;i<d_count;i++)
319 if (off + sizeof (rec) > len)
320 return GNUNET_SYSERR;
321 GNUNET_memcpy (&rec, &src[off], sizeof (rec));
322 dd[i].issuer_attribute_len = ntohl ((uint32_t) rec.issuer_attribute_len);
323 dd[i].issuer_key = rec.issuer_key;
324 dd[i].subject_key = rec.subject_key;
326 if (off + dd[i].issuer_attribute_len > len)
327 return GNUNET_SYSERR;
328 dd[i].issuer_attribute = &src[off];
329 off += dd[i].issuer_attribute_len;
330 dd[i].subject_attribute_len = ntohl ((uint32_t) rec.subject_attribute_len);
331 if (off + dd[i].subject_attribute_len > len)
332 return GNUNET_SYSERR;
333 dd[i].subject_attribute = &src[off];
334 off += dd[i].subject_attribute_len;
336 for (i=0;i<c_count;i++)
338 if (off + sizeof (rec) > len)
339 return GNUNET_SYSERR;
340 GNUNET_memcpy (&rec, &src[off], sizeof (rec));
341 cd[i].issuer_attribute_len = ntohl ((uint32_t) rec.issuer_attribute_len);
342 cd[i].issuer_key = rec.issuer_key;
343 cd[i].subject_key = rec.subject_key;
345 if (off + cd[i].issuer_attribute_len > len)
346 return GNUNET_SYSERR;
347 cd[i].issuer_attribute = &src[off];
348 off += cd[i].issuer_attribute_len;
353 /* end of credential_serialization.c */