kee

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

commit 509fba34c98c745a33eb2f2982f7eae9d22c34b1
parent a134f5e1671129af6bc1295f9e25b40c798bee0b
Author: lash <dev@holbrook.no>
Date:   Sun, 28 Apr 2024 15:50:47 +0100

Implement deserialize ledger open multi asn item

Diffstat:
Msrc/gtk/kee-entry.c | 9+--------
Msrc/ledger.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/ledger.h | 1+
Msrc/tests/ledger.c | 7+++++++
Msrc/transport.c | 81++++++++++++++++++++++++++++++++++++++++---------------------------------------
5 files changed, 101 insertions(+), 48 deletions(-)

diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c @@ -120,20 +120,13 @@ static void kee_entry_handle_add(GtkButton *butt, KeeEntry *o) { o->state |= ENTRYSTATE_LOAD; g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "adding ledger entry"); - // serialize ledger - - // serialize item out_len = 1024; out = malloc(out_len); if (out == NULL) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "memory for item serialization buffer for qr transport fail"); return; } -// r = kee_ledger_item_serialize(item, out, &out_len, KEE_LEDGER_ITEM_SERIALIZE_REQUEST); -// if (r) { -// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "item serialization failed"); -// return; -// } + r = kee_ledger_serialize_open(&o->ledger, out, &out_len); if (r) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "qr transport renderer failed"); diff --git a/src/ledger.c b/src/ledger.c @@ -776,3 +776,54 @@ int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *ou return ERR_OK; } + +int kee_ledger_parse_open(struct kee_ledger_t *ledger, char *out, size_t *out_len) { + int r; + char err[1024]; + char b[1024]; + size_t c; + asn1_node root; + asn1_node pair; + asn1_node item; + asn1_node entry; + + memset(&root, 0, sizeof(root)); + memset(&pair, 0, sizeof(root)); + 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.KeeTransport", &pair); + if (r) { + debug_log(DEBUG_ERROR, asn1_strerror(r)); + return ERR_FAIL; + } + + r = asn1_der_decoding(&pair, out, *out_len, err); + if (r != ASN1_SUCCESS) { + debug_log(DEBUG_ERROR, err); + return ERR_FAIL; + } + + r = asn1_copy_node(root, "Kee.KeeEntryHead", pair, "?1"); + if (r != ASN1_SUCCESS) { + debug_log(DEBUG_ERROR, asn1_strerror(r)); + return ERR_FAIL; + } + + r = asn1_copy_node(root, "Kee.KeeEntry", pair, "?2"); + if (r != ASN1_SUCCESS) { + debug_log(DEBUG_ERROR, asn1_strerror(r)); + return ERR_FAIL; + } + + r = asn1_der_coding(root, "Kee.KeeEntryHead", out, out_len, err); + if (r != ASN1_SUCCESS) { + debug_log(DEBUG_ERROR, err); + return ERR_FAIL; + } + + return ERR_OK; +} diff --git a/src/ledger.h b/src/ledger.h @@ -65,5 +65,6 @@ int kee_ledger_sign(struct kee_ledger_t *ledger, struct kee_ledger_item_t *item, void kee_ledger_item_init(struct kee_ledger_item_t *item); int kee_ledger_item_serialize(struct kee_ledger_item_t *item, char *out, size_t *out_len, enum kee_item_serialize_mode_e mode); int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *out_len); +int kee_ledger_parse_open(struct kee_ledger_t *ledger, char *out, size_t *out_len); #endif diff --git a/src/tests/ledger.c b/src/tests/ledger.c @@ -373,6 +373,7 @@ int test_pair() { struct kee_test_t t; char out[1024]; size_t out_len; + struct kee_ledger_t ledger; r = kee_test_generate(&t); if (r) { @@ -385,6 +386,12 @@ int test_pair() { return 1; } + r = kee_ledger_parse_open(&ledger, out, &out_len); + if (r) { + return 1; + } + + return 0; } diff --git a/src/transport.c b/src/transport.c @@ -253,46 +253,47 @@ int kee_transport_read(struct kee_transport_t *trans, char *out, size_t *out_len return ERR_OK; } -// make sure reported sizes add up to data boundary -static int validate_multi(char *data, size_t data_len) { - char *p; - size_t c; - unsigned short l; - - c = 0; - p = data; - while (c < data_len - 1) { - memcpy(&l, p, sizeof(unsigned short)); - if (l == 0) { - break; - } - if (is_le()) { - flip_endian(2, &l); - } - c += l + sizeof(unsigned short); - p += c; - } - if (p == data) { - return ERR_FAIL; - } - return c != data_len - 1; -} - -/// \todo check state -int kee_transport_validate(struct kee_transport_t *trans) { - int r; - char cmd; - - cmd = *trans->cmd & 0x1f; - - if (cmd == 0) { - r = validate_multi(trans->chunker.data + 1, trans->chunker.data_len); - if (r) { - return ERR_FAIL; - } - } - return ERR_OK; -} +// +//// make sure reported sizes add up to data boundary +//static int validate_multi(char *data, size_t data_len) { +// char *p; +// size_t c; +// unsigned short l; +// +// c = 0; +// p = data; +// while (c < data_len - 1) { +// memcpy(&l, p, sizeof(unsigned short)); +// if (l == 0) { +// break; +// } +// if (is_le()) { +// flip_endian(2, &l); +// } +// c += l + sizeof(unsigned short); +// p += c; +// } +// if (p == data) { +// return ERR_FAIL; +// } +// return c != data_len - 1; +//} +// +///// \todo check state +//int kee_transport_validate(struct kee_transport_t *trans) { +// int r; +// char cmd; +// +// cmd = *trans->cmd & 0x1f; +// +// if (cmd == 0) { +// r = validate_multi(trans->chunker.data + 1, trans->chunker.data_len); +// if (r) { +// return ERR_FAIL; +// } +// } +// return ERR_OK; +//} //int kee_transport_encode_ledger(struct kee_transport_t *trans_ledger, struct kee_transport_t *trans_item, struct kee_transport_t *trans_out, enum kee_transport_mode_e mode) {