kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit 08c5d584f2b276a06b0f2a9eb2ecccda55490299
parent c784578db051c051abc0c8f1330c3d048f257dc7
Author: lash <dev@holbrook.no>
Date:   Fri, 19 Apr 2024 22:02:27 +0100

Remove error outputs, deserialize remaining fields

Diffstat:
Msrc/ledger.c | 38+++++++++++++++++++++++---------------
Msrc/ledger.h | 4++--
Msrc/tests/ledger.c | 9+++++++++
3 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/src/ledger.c b/src/ledger.c @@ -206,7 +206,6 @@ static int verify_item(asn1_node item, const char *pubkey_first_data, const char err = gcry_pk_verify(sig, msg, pubkey); if (err != GPG_ERR_NO_ERROR) { - fprintf(stderr, "verify fail: %s\n", gcry_strerror(err)); return 1; } @@ -225,6 +224,7 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con int *collateral_delta; const char *pubkey_first; const char *pubkey_last; + char tmp[8]; int v; @@ -237,13 +237,11 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con memset(&item, 0, sizeof(item)); r = asn1_array2tree(schema_entry_asn1_tab, &root, err); if (r != ASN1_SUCCESS) { - debug_log(DEBUG_ERROR, err); return NULL; } r = asn1_create_element(root, "Kee.KeeEntry", &item); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return NULL; } @@ -252,7 +250,7 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con cur->initiator = BOB; credit_delta = &cur->bob_credit_delta; collateral_delta = &cur->bob_collateral_delta; - pubkey_first = (const char*)ledger->pubkey_bob; // alice countersigns bobs + pubkey_first = (const char*)ledger->pubkey_bob; pubkey_last = (const char*)ledger->pubkey_alice; // alice countersigns bobs } else { credit_delta = &cur->alice_credit_delta; @@ -263,7 +261,6 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con r = asn1_der_decoding(&item, data, c, err); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return NULL; } @@ -275,7 +272,6 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con c = sizeof(v); r = asn1_read_value(item, "creditDelta", &v, &c); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return NULL; } @@ -287,7 +283,6 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con c = sizeof(v); r = asn1_read_value(item, "collateralDelta", &v, &c); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return NULL; } @@ -296,6 +291,27 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con flip_endian(sizeof(v), (void*)collateral_delta); } + c = 8; + r = asn1_read_value(item, "response", tmp, &c); + if (r != ASN1_SUCCESS) { + return NULL; + } + if (tmp[0] == "T") { // "TRUE" + cur->response = 1; + } + + c = 8; + r = asn1_read_value(item, "timestamp", tmp, &c); + if (r != ASN1_SUCCESS) { + return NULL; + } + + c = 4096; + r = asn1_read_value(item, "body", cur->body, &c); + if (r != ASN1_SUCCESS) { + return NULL; + } + return cur; } @@ -323,20 +339,17 @@ int kee_ledger_parse(struct kee_ledger_t *ledger, const char *data, size_t data_ memset(&item, 0, sizeof(item)); r = asn1_array2tree(schema_entry_asn1_tab, &root, err); if (r != ASN1_SUCCESS) { - debug_log(DEBUG_ERROR, err); return ERR_FAIL; } r = asn1_create_element(root, "Kee.KeeEntryHead", &item); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return r; } c = (int)data_len; r = asn1_der_decoding(&item, data, c, err); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return r; } @@ -348,35 +361,30 @@ int kee_ledger_parse(struct kee_ledger_t *ledger, const char *data, size_t data_ c = 64; r = asn1_read_value(item, "uoa", ledger->uoa, &c); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return r; } c = 1; r = asn1_read_value(item, "uoaDecimals", &ledger->uoa_decimals, &c); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return r; } c = 32; r = asn1_read_value(item, "alicePubKey", ledger->pubkey_alice, &c); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return r; } c = 32; r = asn1_read_value(item, "bobPubKey", ledger->pubkey_bob, &c); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return r; } c = 4096 - 64; r = asn1_read_value(item, "body", ledger->body, &c); if (r != ASN1_SUCCESS) { - fprintf(stderr, "%s\n", err); return r; } diff --git a/src/ledger.h b/src/ledger.h @@ -16,8 +16,8 @@ struct kee_ledger_item_t { int bob_collateral_delta; time_t time; enum kee_initiator initiator; - int response; - const char *body; + char response; + char body[4096]; }; struct kee_ledger_t { diff --git a/src/tests/ledger.c b/src/tests/ledger.c @@ -7,12 +7,15 @@ const char *test_ledger_data = "30818e0c035553440201020420c042f26197b312fef5def1 const char *test_item_data_a = "3082011d044000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020817c7aecdfb6a9cbd020212d8020219600440c333b18dfa822c3ce875de07d54d932ed631aaa7f996153a120bda81e2f0717f4f5e67547019bcd8af0ba6ecba95313bbdc5e43385670d67c24ef76879514ad6044010b17b8c2bc18d7c040f8a4927d20714b78f48f6c3be23867f0ba4aac96b61081f86157942620852c2a47d12a3fb066604018d4228908cf5b0c29568fd67bf0c0101ff0440d6ea4b6501aa6c87cc67bc3297a999a53d8d756873656a0ad68ce9a3b5f9be3d974acf17f4d54d27f9ecf8e854fed32b6c672fa1378142f8898ebd9d68a8540f00"; +const char *test_item_data_b = "3082011d0440534f94460b11203ec7272d5e5ec67c7d5c2669d0db4b732ce987f7f9a94e6a00d34622d466a1839feedc4e0b690af900baa631ce4cafd67305f5b9c0f8f8faaa020817c7aecdfb94675e02021d4a02020cd0044035c13f852ed6a20385959560c61e780dafebffa44d1e96f9293db1e75bf32b7cee53f3e96beeaeeebdf49e4467727f101b35a3e4a536c8545ec84b2880ce9b0d04403107700183297208a870aa15dcf1ca5da4b59adbc9388eceeb1a859259cd1b0ad7510c67b92a51fc9fc60b1cf7657976b99839ef7bd26134a4d5fed4cf2b820d0101ff044083ba7fd4d08541dee2a3f13189194454d429440fe7799d5e8536d2e18e6a5006487746b68f16fb2534a23372814bce45455e6269d187741217e505ff6f73d70a01"; + int main() { int r; size_t c; struct kee_ledger_t ledger; struct kee_ledger_item_t *ledger_item_a; + struct kee_ledger_item_t *ledger_item_b; char data[1024]; c = hex2bin(test_ledger_data, (unsigned char*)data); @@ -27,6 +30,12 @@ int main() { return 1; } + c = hex2bin(test_item_data_b, (unsigned char*)data); + ledger_item_b = kee_ledger_parse_item(&ledger, data, c); + if (ledger_item_b == NULL) { + return 1; + } + kee_ledger_free(&ledger); return 0;