diff options
Diffstat (limited to 'src/credential/credential_serialization.c')
-rw-r--r-- | src/credential/credential_serialization.c | 129 |
1 files changed, 125 insertions, 4 deletions
diff --git a/src/credential/credential_serialization.c b/src/credential/credential_serialization.c index 2fbcebd9f..99138441e 100644 --- a/src/credential/credential_serialization.c +++ b/src/credential/credential_serialization.c | |||
@@ -32,7 +32,21 @@ | |||
32 | 32 | ||
33 | GNUNET_NETWORK_STRUCT_BEGIN | 33 | GNUNET_NETWORK_STRUCT_BEGIN |
34 | 34 | ||
35 | struct NetworkRecord | 35 | struct 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 | |||
49 | struct ChainEntry | ||
36 | { | 50 | { |
37 | /** | 51 | /** |
38 | * Issuer key | 52 | * Issuer key |
@@ -57,6 +71,113 @@ struct NetworkRecord | |||
57 | 71 | ||
58 | GNUNET_NETWORK_STRUCT_END | 72 | GNUNET_NETWORK_STRUCT_END |
59 | 73 | ||
74 | |||
75 | /** | ||
76 | * Calculate how many bytes we will need to serialize | ||
77 | * the given delegation chain and credential | ||
78 | * | ||
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 | ||
83 | */ | ||
84 | size_t | ||
85 | GNUNET_CREDENTIAL_delegation_set_get_size (unsigned int ds_count, | ||
86 | const struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr) | ||
87 | { | ||
88 | unsigned int i; | ||
89 | size_t ret; | ||
90 | |||
91 | ret = sizeof (struct DelegationRecordData) * (ds_count); | ||
92 | |||
93 | for (i=0; i<ds_count;i++) | ||
94 | { | ||
95 | GNUNET_assert ((ret + dsr[i].subject_attribute_len) >= ret); | ||
96 | ret += dsr[i].subject_attribute_len; | ||
97 | } | ||
98 | return ret; | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Serizalize the given delegation chain entries and credential | ||
103 | * | ||
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 | ||
110 | */ | ||
111 | ssize_t | ||
112 | GNUNET_CREDENTIAL_delegation_set_serialize (unsigned int d_count, | ||
113 | const struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr, | ||
114 | size_t dest_size, | ||
115 | char *dest) | ||
116 | { | ||
117 | struct DelegationRecordData rec; | ||
118 | unsigned int i; | ||
119 | size_t off; | ||
120 | |||
121 | off = 0; | ||
122 | for (i=0;i<d_count;i++) | ||
123 | { | ||
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) | ||
127 | return -1; | ||
128 | GNUNET_memcpy (&dest[off], | ||
129 | &rec, | ||
130 | sizeof (rec)); | ||
131 | off += sizeof (rec); | ||
132 | if (0 == dsr[i].subject_attribute_len) | ||
133 | continue; | ||
134 | if (off + dsr[i].subject_attribute_len > dest_size) | ||
135 | return -1; | ||
136 | GNUNET_memcpy (&dest[off], | ||
137 | dsr[i].subject_attribute, | ||
138 | dsr[i].subject_attribute_len); | ||
139 | off += dsr[i].subject_attribute_len; | ||
140 | } | ||
141 | return off; | ||
142 | } | ||
143 | |||
144 | |||
145 | /** | ||
146 | * Deserialize the given destination | ||
147 | * | ||
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 | ||
154 | */ | ||
155 | int | ||
156 | GNUNET_CREDENTIAL_delegation_set_deserialize (size_t len, | ||
157 | const char *src, | ||
158 | unsigned int d_count, | ||
159 | struct GNUNET_CREDENTIAL_DelegationSetRecord *dsr) | ||
160 | { | ||
161 | struct DelegationRecordData rec; | ||
162 | unsigned int i; | ||
163 | size_t off; | ||
164 | |||
165 | off = 0; | ||
166 | for (i=0;i<d_count;i++) | ||
167 | { | ||
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; | ||
172 | off += sizeof (rec); | ||
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; | ||
178 | } | ||
179 | return GNUNET_OK; | ||
180 | } | ||
60 | /** | 181 | /** |
61 | * Calculate how many bytes we will need to serialize | 182 | * Calculate how many bytes we will need to serialize |
62 | * the given delegation chain and credential | 183 | * the given delegation chain and credential |
@@ -74,7 +195,7 @@ GNUNET_CREDENTIAL_delegation_chain_get_size (unsigned int d_count, | |||
74 | unsigned int i; | 195 | unsigned int i; |
75 | size_t ret; | 196 | size_t ret; |
76 | 197 | ||
77 | ret = sizeof (struct NetworkRecord) * (d_count + 1); | 198 | ret = sizeof (struct ChainEntry) * (d_count + 1); |
78 | 199 | ||
79 | for (i=0; i<d_count;i++) | 200 | for (i=0; i<d_count;i++) |
80 | { | 201 | { |
@@ -105,7 +226,7 @@ GNUNET_CREDENTIAL_delegation_chain_serialize (unsigned int d_count, | |||
105 | size_t dest_size, | 226 | size_t dest_size, |
106 | char *dest) | 227 | char *dest) |
107 | { | 228 | { |
108 | struct NetworkRecord rec; | 229 | struct ChainEntry rec; |
109 | unsigned int i; | 230 | unsigned int i; |
110 | size_t off; | 231 | size_t off; |
111 | 232 | ||
@@ -174,7 +295,7 @@ GNUNET_CREDENTIAL_delegation_chain_deserialize (size_t len, | |||
174 | struct GNUNET_CREDENTIAL_Delegation *dd, | 295 | struct GNUNET_CREDENTIAL_Delegation *dd, |
175 | struct GNUNET_CREDENTIAL_Credential *cd) | 296 | struct GNUNET_CREDENTIAL_Credential *cd) |
176 | { | 297 | { |
177 | struct NetworkRecord rec; | 298 | struct ChainEntry rec; |
178 | unsigned int i; | 299 | unsigned int i; |
179 | size_t off; | 300 | size_t off; |
180 | 301 | ||