diff options
author | Markus Teich <markus.teich@stusta.mhn.de> | 2016-08-16 13:25:03 +0200 |
---|---|---|
committer | Markus Teich <markus.teich@stusta.mhn.de> | 2016-08-16 13:25:03 +0200 |
commit | 01c2b8d710ff5a526d1ae2ed2a7fafdec0b1f973 (patch) | |
tree | bff04b124e4e1eae762d618c53640f7255cc3d29 /brandt.c | |
parent | 3a074a3d29ca7b05c79057f9e69d71eae5cb5722 (diff) | |
download | libbrandt-01c2b8d710ff5a526d1ae2ed2a7fafdec0b1f973.tar.gz libbrandt-01c2b8d710ff5a526d1ae2ed2a7fafdec0b1f973.zip |
add descr struct verification
Diffstat (limited to 'brandt.c')
-rw-r--r-- | brandt.c | 90 |
1 files changed, 78 insertions, 12 deletions
@@ -62,8 +62,8 @@ struct BRANDT_Auction * | |||
62 | BRANDT_new (BRANDT_CbBroadcast broadcast, | 62 | BRANDT_new (BRANDT_CbBroadcast broadcast, |
63 | BRANDT_CbResult result, | 63 | BRANDT_CbResult result, |
64 | void *auction_closure, | 64 | void *auction_closure, |
65 | void **auction_data, | 65 | void **auction_desc, |
66 | size_t *auction_data_len, | 66 | size_t *auction_desc_len, |
67 | struct GNUNET_TIME_Absolute time_start, | 67 | struct GNUNET_TIME_Absolute time_start, |
68 | struct GNUNET_TIME_Relative time_round, | 68 | struct GNUNET_TIME_Relative time_round, |
69 | void *description, | 69 | void *description, |
@@ -113,28 +113,94 @@ BRANDT_new (BRANDT_CbBroadcast broadcast, | |||
113 | &start_auction, | 113 | &start_auction, |
114 | ret); | 114 | ret); |
115 | 115 | ||
116 | *auction_data_len = sizeof (struct BRANDT_DescrP); | 116 | *auction_desc_len = sizeof (struct BRANDT_DescrP); |
117 | *auction_data = desc; | 117 | *auction_desc = desc; |
118 | return ret; | 118 | return ret; |
119 | } | 119 | } |
120 | 120 | ||
121 | 121 | ||
122 | int | ||
123 | BRANDT_verify_desc (const void *auction_desc, | ||
124 | size_t auction_desc_len, | ||
125 | const void *description, | ||
126 | uint32_t description_len, | ||
127 | struct GNUNET_TIME_Absolute *time_start, | ||
128 | struct GNUNET_TIME_Relative *time_round, | ||
129 | uint16_t *num_prices, | ||
130 | uint16_t *m, | ||
131 | uint16_t *outcome_public) | ||
132 | { | ||
133 | const struct BRANDT_DescrP *desc = auction_desc; | ||
134 | const uint32_t zero = 0; | ||
135 | struct GNUNET_HashContext *hc = GNUNET_CRYPTO_hash_context_start (); | ||
136 | struct GNUNET_HashCode computed_hash; | ||
137 | |||
138 | if (sizeof (struct BRANDT_DescrP) != auction_desc_len) | ||
139 | { | ||
140 | weprintf ("auction desc struct size mismatch"); | ||
141 | return -1; | ||
142 | } | ||
143 | |||
144 | GNUNET_CRYPTO_hash_context_read (hc, | ||
145 | &desc->time_start, | ||
146 | sizeof (*desc) - sizeof (desc->hash)); | ||
147 | GNUNET_CRYPTO_hash_context_read (hc, | ||
148 | description, | ||
149 | description_len); | ||
150 | GNUNET_CRYPTO_hash_context_finish (hc, &computed_hash); | ||
151 | if (0 != memcmp (&desc->hash, &computed_hash, sizeof (computed_hash))) | ||
152 | { | ||
153 | weprintf ("auction description hash does not match"); | ||
154 | return -1; | ||
155 | } | ||
156 | |||
157 | if (0 != memcmp (&desc->reserved1, &zero, sizeof (desc->reserved1)) || | ||
158 | 0 != memcmp (&desc->reserved2, &zero, sizeof (desc->reserved2))) | ||
159 | { | ||
160 | weprintf ("unknown auction description format"); | ||
161 | return -1; | ||
162 | } | ||
163 | |||
164 | if (time_start) | ||
165 | *time_start = GNUNET_TIME_absolute_ntoh (desc->time_start); | ||
166 | if (time_round) | ||
167 | *time_round = GNUNET_TIME_relative_ntoh (desc->time_round); | ||
168 | if (num_prices) | ||
169 | *num_prices = ntohs (desc->k); | ||
170 | if (m) | ||
171 | *m = ntohs (desc->m); | ||
172 | if (outcome_public) | ||
173 | *outcome_public = ntohs (desc->outcome_public); | ||
174 | |||
175 | return 0; | ||
176 | } | ||
177 | |||
178 | |||
122 | struct BRANDT_Auction * | 179 | struct BRANDT_Auction * |
123 | BRANDT_join (BRANDT_CbBroadcast broadcast, | 180 | BRANDT_join (BRANDT_CbBroadcast broadcast, |
124 | BRANDT_CbUnicast unicast, | 181 | BRANDT_CbUnicast unicast, |
125 | BRANDT_CbResult result, | 182 | BRANDT_CbResult result, |
126 | void *auction_closure, | 183 | void *auction_closure, |
127 | const void *auction_data, | 184 | const void *auction_desc, |
128 | size_t auction_data_len) | 185 | size_t auction_desc_len, |
186 | const void *description, | ||
187 | uint32_t description_len) | ||
129 | { | 188 | { |
130 | struct BRANDT_Auction *ret = GNUNET_new (struct BRANDT_Auction); | 189 | struct BRANDT_Auction *ret = GNUNET_new (struct BRANDT_Auction); |
131 | struct BRANDT_DescrP *desc = (struct BRANDT_DescrP *)auction_data; | ||
132 | 190 | ||
133 | ret->time_start = GNUNET_TIME_absolute_ntoh (desc->time_start); | 191 | if (0 != BRANDT_verify_desc (auction_desc, |
134 | ret->time_round = GNUNET_TIME_relative_ntoh (desc->time_round); | 192 | auction_desc_len, |
135 | ret->k = ntohs (desc->k); | 193 | description, |
136 | ret->m = ntohs (desc->m); | 194 | description_len, |
137 | ret->outcome_public = ntohs (desc->outcome_public); | 195 | &ret->time_start, |
196 | &ret->time_round, | ||
197 | &ret->k, | ||
198 | &ret->m, | ||
199 | &ret->outcome_public)) | ||
200 | { | ||
201 | weprintf ("failed to parse auction description blob"); | ||
202 | return NULL; | ||
203 | } | ||
138 | ret->cur_round = msg_join; | 204 | ret->cur_round = msg_join; |
139 | ret->round_progress = gcry_mpi_new (256); | 205 | ret->round_progress = gcry_mpi_new (256); |
140 | 206 | ||