aboutsummaryrefslogtreecommitdiff
path: root/src/credential/credential_serialization.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/credential/credential_serialization.c')
-rw-r--r--src/credential/credential_serialization.c175
1 files changed, 124 insertions, 51 deletions
diff --git a/src/credential/credential_serialization.c b/src/credential/credential_serialization.c
index 0586e6baa..76bf491c9 100644
--- a/src/credential/credential_serialization.c
+++ b/src/credential/credential_serialization.c
@@ -138,6 +138,121 @@ GNUNET_CREDENTIAL_delegation_set_deserialize (size_t len,
138 } 138 }
139 return GNUNET_OK; 139 return GNUNET_OK;
140} 140}
141
142
143/**
144 * Calculate how many bytes we will need to serialize
145 * the credentials
146 *
147 * @param c_count number of credential entries
148 * @param cd a #GNUNET_CREDENTIAL_Credential
149 * @return the required size to serialize
150 */
151size_t
152GNUNET_CREDENTIAL_credentials_get_size (unsigned int c_count,
153 const struct GNUNET_CREDENTIAL_Credential *cd)
154{
155 unsigned int i;
156 size_t ret;
157
158 ret = sizeof (struct CredentialEntry) * (c_count);
159
160 for (i=0; i<c_count;i++)
161 {
162 GNUNET_assert ((ret + cd[i].issuer_attribute_len) >= ret);
163 ret += cd[i].issuer_attribute_len;
164 }
165 return ret;
166}
167/**
168 * Serizalize the given credentials
169 *
170 * @param c_count number of credential entries
171 * @param cd a #GNUNET_CREDENTIAL_Credential
172 * @param dest_size size of the destination
173 * @param dest where to store the result
174 * @return the size of the data, -1 on failure
175 */
176ssize_t
177GNUNET_CREDENTIAL_credentials_serialize (unsigned int c_count,
178 const struct GNUNET_CREDENTIAL_Credential *cd,
179 size_t dest_size,
180 char *dest)
181{
182 struct CredentialEntry c_rec;
183 unsigned int i;
184 size_t off;
185
186 off = 0;
187 for (i=0;i<c_count;i++)
188 {
189 c_rec.issuer_attribute_len = htonl ((uint32_t) cd[i].issuer_attribute_len);
190 c_rec.issuer_key = cd[i].issuer_key;
191 c_rec.subject_key = cd[i].subject_key;
192 c_rec.signature = cd[i].signature;
193 c_rec.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
194 c_rec.purpose.size = htonl ((sizeof (struct CredentialEntry) + cd[i].issuer_attribute_len) - sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
195 c_rec.expiration = htonl ((uint32_t) cd[i].expiration.abs_value_us);
196 if (off + sizeof (c_rec) > dest_size)
197 return -1;
198 GNUNET_memcpy (&dest[off],
199 &c_rec,
200 sizeof (c_rec));
201 off += sizeof (c_rec);
202 if (off + cd[i].issuer_attribute_len > dest_size)
203 return -1;
204 GNUNET_memcpy (&dest[off],
205 cd[i].issuer_attribute,
206 cd[i].issuer_attribute_len);
207 off += cd[i].issuer_attribute_len;
208 }
209
210 return off;
211}
212
213
214
215/**
216 * Deserialize the given destination
217 *
218 * @param len size of the serialized creds
219 * @param src the serialized data
220 * @param c_count the number of credential entries
221 * @param cd where to put the credential data
222 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
223 */
224int
225GNUNET_CREDENTIAL_credentials_deserialize (size_t len,
226 const char *src,
227 unsigned int c_count,
228 struct GNUNET_CREDENTIAL_Credential *cd)
229{
230 struct CredentialEntry c_rec;
231 unsigned int i;
232 size_t off;
233
234 off = 0;
235 for (i=0;i<c_count;i++)
236 {
237 if (off + sizeof (c_rec) > len)
238 return GNUNET_SYSERR;
239 GNUNET_memcpy (&c_rec, &src[off], sizeof (c_rec));
240 cd[i].issuer_attribute_len = ntohl ((uint32_t) c_rec.issuer_attribute_len);
241 cd[i].issuer_key = c_rec.issuer_key;
242 cd[i].subject_key = c_rec.subject_key;
243 cd[i].signature = c_rec.signature;
244 cd[i].expiration.abs_value_us = ntohl((uint32_t) c_rec.expiration);
245 off += sizeof (c_rec);
246 if (off + cd[i].issuer_attribute_len > len)
247 return GNUNET_SYSERR;
248 cd[i].issuer_attribute = &src[off];
249 off += cd[i].issuer_attribute_len;
250 }
251 return GNUNET_OK;
252}
253
254
255
141/** 256/**
142 * Calculate how many bytes we will need to serialize 257 * Calculate how many bytes we will need to serialize
143 * the given delegation chain and credential 258 * the given delegation chain and credential
@@ -158,7 +273,6 @@ GNUNET_CREDENTIAL_delegation_chain_get_size (unsigned int d_count,
158 size_t ret; 273 size_t ret;
159 274
160 ret = sizeof (struct ChainEntry) * (d_count); 275 ret = sizeof (struct ChainEntry) * (d_count);
161 ret += sizeof (struct CredentialEntry) * (c_count);
162 276
163 for (i=0; i<d_count;i++) 277 for (i=0; i<d_count;i++)
164 { 278 {
@@ -167,11 +281,7 @@ GNUNET_CREDENTIAL_delegation_chain_get_size (unsigned int d_count,
167 dd[i].subject_attribute_len) >= ret); 281 dd[i].subject_attribute_len) >= ret);
168 ret += dd[i].issuer_attribute_len + dd[i].subject_attribute_len; 282 ret += dd[i].issuer_attribute_len + dd[i].subject_attribute_len;
169 } 283 }
170 for (i=0; i<c_count;i++) 284 return ret+GNUNET_CREDENTIAL_credentials_get_size(c_count, cd);
171 {
172 GNUNET_assert ((ret + cd[i].issuer_attribute_len) >= ret);
173 ret += cd[i].issuer_attribute_len;
174 }
175 return ret; 285 return ret;
176} 286}
177 287
@@ -195,7 +305,6 @@ GNUNET_CREDENTIAL_delegation_chain_serialize (unsigned int d_count,
195 char *dest) 305 char *dest)
196{ 306{
197 struct ChainEntry rec; 307 struct ChainEntry rec;
198 struct CredentialEntry c_rec;
199 unsigned int i; 308 unsigned int i;
200 size_t off; 309 size_t off;
201 310
@@ -227,30 +336,10 @@ GNUNET_CREDENTIAL_delegation_chain_serialize (unsigned int d_count,
227 dd[i].subject_attribute_len); 336 dd[i].subject_attribute_len);
228 off += dd[i].subject_attribute_len; 337 off += dd[i].subject_attribute_len;
229 } 338 }
230 for (i=0;i<c_count;i++) 339 return off+GNUNET_CREDENTIAL_credentials_serialize (c_count,
231 { 340 cd,
232 c_rec.issuer_attribute_len = htonl ((uint32_t) cd[i].issuer_attribute_len); 341 dest_size-off,
233 c_rec.issuer_key = cd[i].issuer_key; 342 &dest[off]);
234 c_rec.subject_key = cd[i].subject_key;
235 c_rec.signature = cd[i].signature;
236 c_rec.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
237 c_rec.purpose.size = htonl ((sizeof (struct CredentialEntry) + cd[i].issuer_attribute_len) - sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
238 c_rec.expiration = htonl ((uint32_t) cd[i].expiration.abs_value_us);
239 if (off + sizeof (c_rec) > dest_size)
240 return -1;
241 GNUNET_memcpy (&dest[off],
242 &c_rec,
243 sizeof (c_rec));
244 off += sizeof (c_rec);
245 if (off + cd[i].issuer_attribute_len > dest_size)
246 return -1;
247 GNUNET_memcpy (&dest[off],
248 cd[i].issuer_attribute,
249 cd[i].issuer_attribute_len);
250 off += cd[i].issuer_attribute_len;
251 }
252
253 return off;
254} 343}
255 344
256 345
@@ -274,7 +363,6 @@ GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len,
274 struct GNUNET_CREDENTIAL_Credential *cd) 363 struct GNUNET_CREDENTIAL_Credential *cd)
275{ 364{
276 struct ChainEntry rec; 365 struct ChainEntry rec;
277 struct CredentialEntry c_rec;
278 unsigned int i; 366 unsigned int i;
279 size_t off; 367 size_t off;
280 368
@@ -298,26 +386,11 @@ GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len,
298 dd[i].subject_attribute = &src[off]; 386 dd[i].subject_attribute = &src[off];
299 off += dd[i].subject_attribute_len; 387 off += dd[i].subject_attribute_len;
300 } 388 }
301 for (i=0;i<c_count;i++) 389 return GNUNET_CREDENTIAL_credentials_deserialize (len-off,
302 { 390 &src[off],
303 if (off + sizeof (c_rec) > len) 391 c_count,
304 return GNUNET_SYSERR; 392 cd);
305 GNUNET_memcpy (&c_rec, &src[off], sizeof (c_rec));
306 cd[i].issuer_attribute_len = ntohl ((uint32_t) c_rec.issuer_attribute_len);
307 cd[i].issuer_key = c_rec.issuer_key;
308 cd[i].subject_key = c_rec.subject_key;
309 cd[i].signature = c_rec.signature;
310 cd[i].expiration.abs_value_us = ntohl((uint32_t) c_rec.expiration);
311 off += sizeof (c_rec);
312 if (off + cd[i].issuer_attribute_len > len)
313 return GNUNET_SYSERR;
314 cd[i].issuer_attribute = &src[off];
315 off += cd[i].issuer_attribute_len;
316 }
317 return GNUNET_OK;
318} 393}
319
320
321int 394int
322GNUNET_CREDENTIAL_credential_serialize (struct GNUNET_CREDENTIAL_Credential *cred, 395GNUNET_CREDENTIAL_credential_serialize (struct GNUNET_CREDENTIAL_Credential *cred,
323 char **data) 396 char **data)