kee

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

commit 19895b833c77b2f3b1b597a3bd46175b4674b8a4
parent a329ade72aff32f4af6595eb1f9fbe55ed4524c8
Author: lash <dev@holbrook.no>
Date:   Sat,  4 May 2024 13:29:11 +0100

Remove db format initiator hint from transport serialization

Diffstat:
Msrc/gtk/kee-entry-item-store.c | 2+-
Msrc/gtk/kee-import.c | 12------------
Msrc/gtk/kee-menu.c | 2+-
Msrc/ledger.c | 62++++++++++++++++++++++++++++++++++++++------------------------
Msrc/ledger.h | 7++++---
Msrc/tests/ledger.c | 10+++++-----
Msrc/tests/testutil.h | 2+-
7 files changed, 50 insertions(+), 47 deletions(-)

diff --git a/src/gtk/kee-entry-item-store.c b/src/gtk/kee-entry-item-store.c @@ -113,7 +113,7 @@ static int kee_entry_item_store_scan(KeeEntryItemStore *o) { if (memcmp(entry_key, last_key, entry_ref_len)) { break; } - item = kee_ledger_parse_item(o->ledger, last_value, last_value_length); + item = kee_ledger_parse_item_db(o->ledger, last_value, last_value_length); if (item == NULL) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "corrupt entry!"); } else { diff --git a/src/gtk/kee-import.c b/src/gtk/kee-import.c @@ -145,18 +145,6 @@ static int check_cmd(KeeImport *o, char cmd) { } -//static int import_ledger(KeeImport *o, const char *in, size_t in_len) { -// int r; -// struct kee_ledger_t ledger; -// -// r = kee_ledger_parse_open(&ledger, in, in_len); -// if (r) { -// return r; -// } -// -// return ERR_OK; -//} - /// \todo too long, split up static void kee_import_handle_import_data_accept(GtkActionable *actn, void *null, KeeImport *o) { int r; diff --git a/src/gtk/kee-menu.c b/src/gtk/kee-menu.c @@ -58,7 +58,7 @@ static void kee_menu_act_import_entry(GAction *act, GVariant *param, KeeMenu *me c = (size_t)g_variant_n_children(param); b = (const char*)g_variant_get_data(param); - r = kee_ledger_parse_open(&ledger, b, c); + r = kee_ledger_parse_open(&ledger, &menu->ctx->gpg, b, c); if (r) { return; } diff --git a/src/ledger.c b/src/ledger.c @@ -325,7 +325,21 @@ struct kee_ledger_item_t *kee_ledger_add_item(struct kee_ledger_t *ledger) { return ledger->last_item; } -struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, const char *data, size_t data_len) { +struct kee_ledger_item_t *kee_ledger_parse_item_db(struct kee_ledger_t *ledger, const char *data, size_t data_len) { + size_t c; + + enum kee_initiator_e initiator; + + if (*(data+(data_len-1))) { + initiator = BOB; + } else { + initiator = ALICE; + } + c = data_len - 1; + return kee_ledger_parse_item(ledger, data, c, initiator); +} + +struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, const char *data, size_t data_len, enum kee_initiator_e initiator) { int r; int c; char err[1024]; @@ -339,8 +353,6 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con char tmp[64]; int v; - cur = kee_ledger_add_item(ledger); - memset(&root, 0, sizeof(root)); memset(&item, 0, sizeof(item)); r = asn1_array2tree(schema_entry_asn1_tab, &root, err); @@ -353,21 +365,22 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con return NULL; } - c = (int)data_len - 1; - if (*(data+c)) { + cur = kee_ledger_add_item(ledger); + if (initiator == BOB) { cur->initiator = BOB; credit_delta = &cur->bob_credit_delta; collateral_delta = &cur->bob_collateral_delta; pubkey_first = (const char*)ledger->pubkey_bob; pubkey_last = (const char*)ledger->pubkey_alice; // alice countersigns bobs } else { + cur->initiator = ALICE; credit_delta = &cur->alice_credit_delta; collateral_delta = &cur->alice_collateral_delta; pubkey_first = (const char*)ledger->pubkey_alice; pubkey_last = (const char*)ledger->pubkey_bob; } - r = asn1_der_decoding(&item, data, c, err); + r = asn1_der_decoding(&item, data, data_len, err); if (r != ASN1_SUCCESS) { return NULL; } @@ -815,26 +828,25 @@ int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *ou debug_log(DEBUG_ERROR, err); return ERR_FAIL; } - *(out+*out_len) = 0; - if (ledger->last_item->initiator == BOB) { - *(out+*out_len) = 1; - } - *out_len += 1; +// *(out+*out_len) = 0; +// if (ledger->last_item->initiator == BOB) { +// *(out+*out_len) = 1; +// } +// *out_len += 1; return ERR_OK; } -int kee_ledger_parse_open(struct kee_ledger_t *ledger, const char *in, size_t in_len) { +int kee_ledger_parse_open(struct kee_ledger_t *ledger, struct gpg_store *gpg, const char *in, size_t in_len) { int r; char err[1024]; char b[1024]; size_t c; asn1_node root; asn1_node pair; - //asn1_node item; - //asn1_node entry; struct kee_ledger_item_t *item; char is_bob; + enum kee_initiator_e initiator; kee_ledger_init(ledger); @@ -852,8 +864,6 @@ int kee_ledger_parse_open(struct kee_ledger_t *ledger, const char *in, size_t in return ERR_FAIL; } - is_bob = (char)*(in+in_len-1); - in_len--; r = asn1_der_decoding(&pair, in, in_len, err); if (r != ASN1_SUCCESS) { debug_log(DEBUG_ERROR, err); @@ -893,9 +903,8 @@ int kee_ledger_parse_open(struct kee_ledger_t *ledger, const char *in, size_t in return ERR_FAIL; } - *(b+c) = is_bob; - c++; - item = kee_ledger_parse_item(ledger, b, c); + initiator = kee_ledger_item_initiator(ledger, gpg, NULL); + item = kee_ledger_parse_item(ledger, b, c, initiator); if (item == NULL) { return ERR_FAIL; } @@ -1193,12 +1202,17 @@ enum kee_ledger_state_e kee_ledger_item_state(struct kee_ledger_item_t *item) { /// \todo consider optional verify with item signature /// \todo don't get confused; ledger alice is ALWAYS the requester, but when SIGNING alice is always the current keystore private key holder - consider renaming the latter to carol/dave... -enum kee_initiator_e kee_ledger_item_initiator(struct kee_ledger_t *ledger, struct kee_ledger_item_t *item, struct gpg_store *gpg) { - item->initiator = NOONE; +enum kee_initiator_e kee_ledger_item_initiator(struct kee_ledger_t *ledger, struct gpg_store *gpg, struct kee_ledger_item_t *item) { + enum kee_initiator_e initiator; + + initiator = NOONE; if (!memcmp(ledger->pubkey_alice, gpg->public_key, PUBKEY_LENGTH)) { - item->initiator = ALICE; + initiator = ALICE; } else if (memcmp(ledger->pubkey_bob, zero_content, PUBKEY_LENGTH)) { - item->initiator = BOB; + initiator = BOB; + } + if (item != NULL) { + item->initiator = initiator; } - return item->initiator; + return initiator; } diff --git a/src/ledger.h b/src/ledger.h @@ -62,17 +62,18 @@ void kee_ledger_resolve(struct kee_ledger_t *ledger, Cadiz *cadiz); void kee_ledger_reset_cache(struct kee_ledger_t *ledger); int kee_ledger_sign(struct kee_ledger_t *ledger, struct kee_ledger_item_t *item, struct gpg_store *gpg, const char *passphrase); 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, const char *in, size_t in_len); +int kee_ledger_parse_open(struct kee_ledger_t *ledger, struct gpg_store *gpg, const char *in, size_t in_len); int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db); int kee_ledger_item_put(struct kee_ledger_t *ledger, struct db_ctx *db, int idx); int kee_ledger_verify(struct kee_ledger_t *ledger, int *err_idx); -struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, const char *data, size_t data_len); +struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, const char *data, size_t data_len, enum kee_initiator_e initiator); +struct kee_ledger_item_t *kee_ledger_parse_item_db(struct kee_ledger_t *ledger, const char *data, size_t data_len); struct kee_ledger_item_t *kee_ledger_add_item(struct kee_ledger_t *ledger); 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_ledger_state_e mode); void kee_ledger_item_free(struct kee_ledger_item_t *item); enum kee_ledger_state_e kee_ledger_item_state(struct kee_ledger_item_t *item); -enum kee_initiator_e kee_ledger_item_initiator(struct kee_ledger_t *ledger, struct kee_ledger_item_t *item, struct gpg_store *gpg); +enum kee_initiator_e kee_ledger_item_initiator(struct kee_ledger_t *ledger, struct gpg_store *gpg, struct kee_ledger_item_t *item); #endif diff --git a/src/tests/ledger.c b/src/tests/ledger.c @@ -46,13 +46,13 @@ int test_parse() { } c = hex2bin(test_item_data_a, (unsigned char*)data); - ledger_item_a = kee_ledger_parse_item(&ledger, data, c); + ledger_item_a = kee_ledger_parse_item_db(&ledger, data, c); if (ledger_item_a == NULL) { return 1; } c = hex2bin(test_item_data_b, (unsigned char*)data); - ledger_item_b = kee_ledger_parse_item(&ledger, data, c); + ledger_item_b = kee_ledger_parse_item_db(&ledger, data, c); if (ledger_item_b == NULL) { return 1; } @@ -94,7 +94,7 @@ int test_pair() { return 1; } - r = kee_ledger_parse_open(&ledger, out, out_len); + r = kee_ledger_parse_open(&ledger, &t.gpg, out, out_len); if (r) { return 1; } @@ -211,7 +211,7 @@ int test_initiator() { } item->initiator = NOONE; - initiator = kee_ledger_item_initiator(&t.ledger, item, &t.gpg); + initiator = kee_ledger_item_initiator(&t.ledger, &t.gpg, item); if (initiator != ALICE) { return 1; } @@ -234,7 +234,7 @@ int test_initiator() { item = t.ledger.last_item; item->initiator = NOONE; - initiator = kee_ledger_item_initiator(&t.ledger, item, &t.gpg); + initiator = kee_ledger_item_initiator(&t.ledger, &t.gpg, item); if (initiator != BOB) { return 1; } diff --git a/src/tests/testutil.h b/src/tests/testutil.h @@ -35,6 +35,6 @@ size_t kee_test_get_ledger_item_data(struct kee_test_t *t, int idx, char **out); int kee_test_db(struct kee_test_t *t); int kee_test_sign_request(struct kee_test_t *t); int kee_test_sign_response(struct kee_test_t *t); -//void kee_test_swap_identities(struct kee_test_t *t); +void kee_test_swap_identities(struct kee_test_t *t); #endif