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.c159
1 files changed, 98 insertions, 61 deletions
diff --git a/src/credential/credential_serialization.c b/src/credential/credential_serialization.c
index b08920d96..0586e6baa 100644
--- a/src/credential/credential_serialization.c
+++ b/src/credential/credential_serialization.c
@@ -29,48 +29,8 @@
29#include "gnunet_util_lib.h" 29#include "gnunet_util_lib.h"
30#include "gnunet_constants.h" 30#include "gnunet_constants.h"
31#include "gnunet_credential_service.h" 31#include "gnunet_credential_service.h"
32 32#include "gnunet_signatures.h"
33GNUNET_NETWORK_STRUCT_BEGIN 33#include "credential.h"
34
35struct DelegationRecordData
36{
37 /**
38 * Subject key
39 */
40 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
41
42 /**
43 * Subject attributes
44 */
45 uint32_t subject_attribute_len GNUNET_PACKED;
46};
47
48
49struct ChainEntry
50{
51 /**
52 * Issuer key
53 */
54 struct GNUNET_CRYPTO_EcdsaPublicKey issuer_key;
55
56 /**
57 * Subject key
58 */
59 struct GNUNET_CRYPTO_EcdsaPublicKey subject_key;
60
61 /**
62 * Issuer attributes
63 */
64 uint32_t issuer_attribute_len GNUNET_PACKED;
65
66 /**
67 * Subject attributes
68 */
69 uint32_t subject_attribute_len GNUNET_PACKED;
70};
71
72GNUNET_NETWORK_STRUCT_END
73
74 34
75/** 35/**
76 * Calculate how many bytes we will need to serialize 36 * Calculate how many bytes we will need to serialize
@@ -83,7 +43,7 @@ GNUNET_NETWORK_STRUCT_END
83 */ 43 */
84size_t 44size_t
85GNUNET_CREDENTIAL_delegation_set_get_size (unsigned int ds_count, 45GNUNET_CREDENTIAL_delegation_set_get_size (unsigned int ds_count,
86 const struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr) 46 const struct GNUNET_CREDENTIAL_DelegationSet *dsr)
87{ 47{
88 unsigned int i; 48 unsigned int i;
89 size_t ret; 49 size_t ret;
@@ -110,7 +70,7 @@ GNUNET_CREDENTIAL_delegation_set_get_size (unsigned int ds_count,
110 */ 70 */
111ssize_t 71ssize_t
112GNUNET_CREDENTIAL_delegation_set_serialize (unsigned int d_count, 72GNUNET_CREDENTIAL_delegation_set_serialize (unsigned int d_count,
113 const struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr, 73 const struct GNUNET_CREDENTIAL_DelegationSet *dsr,
114 size_t dest_size, 74 size_t dest_size,
115 char *dest) 75 char *dest)
116{ 76{
@@ -156,7 +116,7 @@ int
156GNUNET_CREDENTIAL_delegation_set_deserialize (size_t len, 116GNUNET_CREDENTIAL_delegation_set_deserialize (size_t len,
157 const char *src, 117 const char *src,
158 unsigned int d_count, 118 unsigned int d_count,
159 struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr) 119 struct GNUNET_CREDENTIAL_DelegationSet *dsr)
160{ 120{
161 struct DelegationRecordData rec; 121 struct DelegationRecordData rec;
162 unsigned int i; 122 unsigned int i;
@@ -173,7 +133,7 @@ GNUNET_CREDENTIAL_delegation_set_deserialize (size_t len,
173 dsr[i].subject_attribute_len = ntohl ((uint32_t) rec.subject_attribute_len); 133 dsr[i].subject_attribute_len = ntohl ((uint32_t) rec.subject_attribute_len);
174 if (off + dsr[i].subject_attribute_len > len) 134 if (off + dsr[i].subject_attribute_len > len)
175 return GNUNET_SYSERR; 135 return GNUNET_SYSERR;
176 dsr[i].subject_attribute = &src[off]; 136 dsr[i].subject_attribute = (char*)&src[off];
177 off += dsr[i].subject_attribute_len; 137 off += dsr[i].subject_attribute_len;
178 } 138 }
179 return GNUNET_OK; 139 return GNUNET_OK;
@@ -198,7 +158,7 @@ GNUNET_CREDENTIAL_delegation_chain_get_size (unsigned int d_count,
198 size_t ret; 158 size_t ret;
199 159
200 ret = sizeof (struct ChainEntry) * (d_count); 160 ret = sizeof (struct ChainEntry) * (d_count);
201 ret += sizeof (struct ChainEntry) * (c_count); 161 ret += sizeof (struct CredentialEntry) * (c_count);
202 162
203 for (i=0; i<d_count;i++) 163 for (i=0; i<d_count;i++)
204 { 164 {
@@ -235,6 +195,7 @@ GNUNET_CREDENTIAL_delegation_chain_serialize (unsigned int d_count,
235 char *dest) 195 char *dest)
236{ 196{
237 struct ChainEntry rec; 197 struct ChainEntry rec;
198 struct CredentialEntry c_rec;
238 unsigned int i; 199 unsigned int i;
239 size_t off; 200 size_t off;
240 201
@@ -268,16 +229,19 @@ GNUNET_CREDENTIAL_delegation_chain_serialize (unsigned int d_count,
268 } 229 }
269 for (i=0;i<c_count;i++) 230 for (i=0;i<c_count;i++)
270 { 231 {
271 rec.issuer_attribute_len = htonl ((uint32_t) cd[i].issuer_attribute_len); 232 c_rec.issuer_attribute_len = htonl ((uint32_t) cd[i].issuer_attribute_len);
272 rec.subject_attribute_len = htonl (0); 233 c_rec.issuer_key = cd[i].issuer_key;
273 rec.issuer_key = cd[i].issuer_key; 234 c_rec.subject_key = cd[i].subject_key;
274 rec.subject_key = cd[i].subject_key; 235 c_rec.signature = cd[i].signature;
275 if (off + sizeof (rec) > dest_size) 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)
276 return -1; 240 return -1;
277 GNUNET_memcpy (&dest[off], 241 GNUNET_memcpy (&dest[off],
278 &rec, 242 &c_rec,
279 sizeof (rec)); 243 sizeof (c_rec));
280 off += sizeof (rec); 244 off += sizeof (c_rec);
281 if (off + cd[i].issuer_attribute_len > dest_size) 245 if (off + cd[i].issuer_attribute_len > dest_size)
282 return -1; 246 return -1;
283 GNUNET_memcpy (&dest[off], 247 GNUNET_memcpy (&dest[off],
@@ -310,6 +274,7 @@ GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len,
310 struct GNUNET_CREDENTIAL_Credential *cd) 274 struct GNUNET_CREDENTIAL_Credential *cd)
311{ 275{
312 struct ChainEntry rec; 276 struct ChainEntry rec;
277 struct CredentialEntry c_rec;
313 unsigned int i; 278 unsigned int i;
314 size_t off; 279 size_t off;
315 280
@@ -335,13 +300,15 @@ GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len,
335 } 300 }
336 for (i=0;i<c_count;i++) 301 for (i=0;i<c_count;i++)
337 { 302 {
338 if (off + sizeof (rec) > len) 303 if (off + sizeof (c_rec) > len)
339 return GNUNET_SYSERR; 304 return GNUNET_SYSERR;
340 GNUNET_memcpy (&rec, &src[off], sizeof (rec)); 305 GNUNET_memcpy (&c_rec, &src[off], sizeof (c_rec));
341 cd[i].issuer_attribute_len = ntohl ((uint32_t) rec.issuer_attribute_len); 306 cd[i].issuer_attribute_len = ntohl ((uint32_t) c_rec.issuer_attribute_len);
342 cd[i].issuer_key = rec.issuer_key; 307 cd[i].issuer_key = c_rec.issuer_key;
343 cd[i].subject_key = rec.subject_key; 308 cd[i].subject_key = c_rec.subject_key;
344 off += sizeof (rec); 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);
345 if (off + cd[i].issuer_attribute_len > len) 312 if (off + cd[i].issuer_attribute_len > len)
346 return GNUNET_SYSERR; 313 return GNUNET_SYSERR;
347 cd[i].issuer_attribute = &src[off]; 314 cd[i].issuer_attribute = &src[off];
@@ -350,4 +317,74 @@ GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len,
350 return GNUNET_OK; 317 return GNUNET_OK;
351} 318}
352 319
320
321int
322GNUNET_CREDENTIAL_credential_serialize (struct GNUNET_CREDENTIAL_Credential *cred,
323 char **data)
324{
325 size_t size;
326 struct CredentialEntry *cdata;
327
328 size = sizeof (struct CredentialEntry) + strlen (cred->issuer_attribute) + 1;
329 *data = GNUNET_malloc (size);
330 cdata = (struct CredentialEntry*)*data;
331 cdata->subject_key = cred->subject_key;
332 cdata->issuer_key = cred->issuer_key;
333 cdata->expiration = GNUNET_htonll (cred->expiration.abs_value_us);
334 cdata->signature = cred->signature;
335 cdata->issuer_attribute_len = htonl (strlen (cred->issuer_attribute) + 1);
336 cdata->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CREDENTIAL);
337 cdata->purpose.size = htonl (size - sizeof (struct GNUNET_CRYPTO_EcdsaSignature));
338 GNUNET_memcpy (&cdata[1],
339 cred->issuer_attribute,
340 strlen (cred->issuer_attribute));
341
342 if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL,
343 &cdata->purpose,
344 &cdata->signature,
345 &cdata->issuer_key))
346 {
347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
348 "Invalid credential\n");
349 //return NULL;
350 }
351 return size;
352}
353
354struct GNUNET_CREDENTIAL_Credential*
355GNUNET_CREDENTIAL_credential_deserialize (const char* data,
356 size_t data_size)
357{
358 struct GNUNET_CREDENTIAL_Credential *cred;
359 struct CredentialEntry *cdata;
360 char *issuer_attribute;
361
362 if (data_size < sizeof (struct CredentialEntry))
363 return NULL;
364 cdata = (struct CredentialEntry*)data;
365 if(GNUNET_OK != GNUNET_CRYPTO_ecdsa_verify(GNUNET_SIGNATURE_PURPOSE_CREDENTIAL,
366 &cdata->purpose,
367 &cdata->signature,
368 &cdata->issuer_key))
369 {
370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
371 "Invalid credential\n");
372 //return NULL;
373 }
374 issuer_attribute = (char*)&cdata[1];
375
376 cred = GNUNET_malloc (sizeof (struct GNUNET_CREDENTIAL_Credential) + ntohl(cdata->issuer_attribute_len));
377
378 cred->issuer_key = cdata->issuer_key;
379 cred->subject_key = cdata->subject_key;
380 GNUNET_memcpy (&cred[1],
381 issuer_attribute,
382 ntohl (cdata->issuer_attribute_len));
383 cred->signature = cdata->signature;
384 cred->issuer_attribute = (char*)&cred[1];
385 cred->expiration.abs_value_us = GNUNET_ntohll (cdata->expiration);
386 return cred;
387}
388
389
353/* end of credential_serialization.c */ 390/* end of credential_serialization.c */