|
10 | 10 | #include "ccf/crypto/rsa_key_pair.h" |
11 | 11 | #include "ccf/pal/locking.h" |
12 | 12 | #include "ccf/receipt.h" |
| 13 | +#include "crypto/cbor.h" |
13 | 14 | #include "crypto/openssl/hash.h" |
14 | 15 | #include "ds/messaging.h" |
15 | 16 | #include "ds/test/stub_writer.h" |
@@ -254,90 +255,49 @@ size_t get_cache_limit_for_entries( |
254 | 255 |
|
255 | 256 | struct MerkleProofData |
256 | 257 | { |
| 258 | + using PathItem = |
| 259 | + std::pair</* left/right */ bool, /* digest */ std::vector<uint8_t>>; |
| 260 | + |
257 | 261 | std::vector<uint8_t> write_set_digest; |
258 | 262 | std::string commit_evidence; |
259 | 263 | std::vector<uint8_t> claims_digest; |
260 | | - std::vector<std::pair<int64_t, std::vector<uint8_t>>> path; |
| 264 | + std::vector<PathItem> path; |
261 | 265 | }; |
262 | 266 |
|
263 | | -std::vector<uint8_t> bstring_to_bytes(QCBORItem& item) |
264 | | -{ |
265 | | - return { |
266 | | - static_cast<const uint8_t*>(item.val.string.ptr), |
267 | | - static_cast<const uint8_t*>(item.val.string.ptr) + item.val.string.len}; |
268 | | -} |
269 | | - |
270 | | -std::string tstring_to_string(QCBORItem& item) |
271 | | -{ |
272 | | - return { |
273 | | - static_cast<const char*>(item.val.string.ptr), |
274 | | - static_cast<const char*>(item.val.string.ptr) + item.val.string.len}; |
275 | | -} |
276 | | - |
277 | 267 | MerkleProofData decode_merkle_proof(const std::vector<uint8_t>& encoded) |
278 | 268 | { |
279 | | - q_useful_buf_c buf{encoded.data(), encoded.size()}; |
280 | | - QCBORDecodeContext ctx; |
281 | | - QCBORDecode_Init(&ctx, buf, QCBOR_DECODE_MODE_NORMAL); |
282 | | - struct q_useful_buf_c params; |
283 | | - QCBORDecode_EnterMap(&ctx, NULL); |
284 | | - QCBORDecode_EnterArrayFromMapN( |
285 | | - &ctx, ccf::MerkleProofLabel::MERKLE_PROOF_LEAF_LABEL); |
286 | | - QCBORItem item; |
287 | 269 | MerkleProofData data; |
288 | 270 |
|
289 | | - QCBORDecode_GetNext(&ctx, &item); |
290 | | - REQUIRE(item.uDataType == QCBOR_TYPE_BYTE_STRING); |
291 | | - data.write_set_digest = bstring_to_bytes(item); |
| 271 | + auto decoded = ccf::cbor::parse(encoded); |
292 | 272 |
|
293 | | - QCBORDecode_GetNext(&ctx, &item); |
294 | | - REQUIRE(item.uDataType == QCBOR_TYPE_TEXT_STRING); |
295 | | - data.commit_evidence = tstring_to_string(item); |
| 273 | + const auto& leaf = decoded->map_at( |
| 274 | + ccf::cbor::make_signed(ccf::MerkleProofLabel::MERKLE_PROOF_LEAF_LABEL)); |
296 | 275 |
|
297 | | - QCBORDecode_GetNext(&ctx, &item); |
298 | | - REQUIRE(item.uDataType == QCBOR_TYPE_BYTE_STRING); |
299 | | - data.claims_digest = bstring_to_bytes(item); |
| 276 | + REQUIRE_EQ(leaf->size(), 3); |
300 | 277 |
|
301 | | - QCBORDecode_ExitArray(&ctx); |
302 | | - QCBORDecode_EnterArrayFromMapN( |
303 | | - &ctx, ccf::MerkleProofLabel::MERKLE_PROOF_PATH_LABEL); |
| 278 | + const auto& wsd = leaf->array_at(0)->as_bytes(); |
| 279 | + data.write_set_digest.assign(wsd.begin(), wsd.end()); |
304 | 280 |
|
305 | | - for (;;) |
306 | | - { |
307 | | - QCBORDecode_EnterArray(&ctx, &item); |
308 | | - if (QCBORDecode_GetError(&ctx) != QCBOR_SUCCESS) |
309 | | - break; |
| 281 | + data.commit_evidence = leaf->array_at(1)->as_string(); |
310 | 282 |
|
311 | | - std::pair<int64_t, std::vector<uint8_t>> path_item; |
| 283 | + const auto& cd = leaf->array_at(2)->as_bytes(); |
| 284 | + data.claims_digest.assign(cd.begin(), cd.end()); |
312 | 285 |
|
313 | | - REQUIRE(QCBORDecode_GetNext(&ctx, &item) == QCBOR_SUCCESS); |
314 | | - if (item.uDataType == CBOR_SIMPLEV_TRUE) |
315 | | - { |
316 | | - path_item.first = true; |
317 | | - } |
318 | | - else if (item.uDataType == CBOR_SIMPLEV_FALSE) |
319 | | - { |
320 | | - path_item.first = false; |
321 | | - } |
322 | | - else |
323 | | - { |
324 | | - // Not a valid CBOR boolean |
325 | | - REQUIRE(false); |
326 | | - } |
| 286 | + const auto& path = decoded->map_at( |
| 287 | + ccf::cbor::make_signed(ccf::MerkleProofLabel::MERKLE_PROOF_PATH_LABEL)); |
327 | 288 |
|
328 | | - REQUIRE(QCBORDecode_GetNext(&ctx, &item) == QCBOR_SUCCESS); |
329 | | - REQUIRE(item.uDataType == QCBOR_TYPE_BYTE_STRING); |
330 | | - path_item.second = bstring_to_bytes(item); |
331 | | - |
332 | | - data.path.push_back(path_item); |
333 | | - QCBORDecode_ExitArray(&ctx); |
| 289 | + for (size_t i = 0; i < path->size(); i++) |
| 290 | + { |
| 291 | + const auto& node = path->array_at(i); |
| 292 | + const auto& dir = node->array_at(0)->as_simple(); |
| 293 | + const auto& hash = node->array_at(1)->as_bytes(); |
| 294 | + |
| 295 | + MerkleProofData::PathItem item; |
| 296 | + item.first = ccf::cbor::simple_to_boolean(dir); |
| 297 | + item.second.assign(hash.begin(), hash.end()); |
| 298 | + data.path.push_back(item); |
334 | 299 | } |
335 | 300 |
|
336 | | - QCBORDecode_ExitArray(&ctx); |
337 | | - QCBORDecode_ExitMap(&ctx); |
338 | | - |
339 | | - REQUIRE(QCBORDecode_Finish(&ctx) == QCBOR_ERR_NO_MORE_ITEMS); |
340 | | - |
341 | 301 | return data; |
342 | 302 | } |
343 | 303 |
|
|
0 commit comments