kee

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

commit 8190c79dd3e8d23e871b99ff9541b12d953b2c83
parent a69f943fb17c11b560b242774273368f05ef211d
Author: lash <dev@holbrook.no>
Date:   Mon, 22 Apr 2024 20:44:23 +0100

Eliminate character leak in dn, clean up list generations

Diffstat:
Msrc/content.c | 1+
Msrc/gtk/kee-entry-item-store.c | 137++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/gtk/kee-entry-item-store.h | 3++-
Msrc/gtk/kee-entry-item.c | 64+++++++++++++++++++++++++---------------------------------------
Msrc/gtk/kee-entry-item.h | 2+-
Msrc/gtk/kee-entry-list.c | 8+++-----
Msrc/gtk/kee-entry-store.c | 7++++---
Msrc/gtk/kee-entry.c | 119++++++++++++++++++++++++++++---------------------------------------------------
Msrc/ledger.c | 2--
9 files changed, 151 insertions(+), 192 deletions(-)

diff --git a/src/content.c b/src/content.c @@ -20,6 +20,7 @@ int kee_content_init(struct kee_content_t *content, const char *key, size_t size } content->body = content->mem; content->flags = 0; + content->subject = 0; memcpy(content->key, key, KEE_CONTENT_KEY_SIZE); return ERR_OK; diff --git a/src/gtk/kee-entry-item-store.c b/src/gtk/kee-entry-item-store.c @@ -6,6 +6,10 @@ #include "kee-entry-item.h" #include "cadiz.h" #include "db.h" +#include "err.h" + +const size_t entry_ref_len = 65; +const size_t entry_key_len = 73; typedef struct { @@ -14,15 +18,17 @@ typedef struct { struct _KeeEntryItemStore { GObject parent; struct db_ctx *db; - int last_idx; - int last_state; +// int last_idx; +// int last_state; int last_count; - char *last; - char *last_key; - char *last_digest; - char *last_value; - size_t last_value_length; - struct Cadiz resolver; +// char *last; +// char *last_key; +// char *last_digest; +// char *last_value; +// size_t last_value_length; + char **ref; + char *ref_mem; + struct Cadiz *resolver; struct kee_ledger_t *ledger; }; @@ -42,13 +48,13 @@ static void kee_entry_item_store_class_init(KeeEntryItemStoreClass *kls) { } static void kee_entry_item_store_init(KeeEntryItemStore *o) { - o->resolver.key_type = CADIZ_KEY_TYPE_ANY; - o->resolver.locator = malloc(1024); +// o->resolver.key_type = CADIZ_KEY_TYPE_ANY; +// o->resolver.locator = malloc(1024); } -void kee_entry_item_store_set_resolve(KeeEntryItemStore *o, const char *locator) { - strcpy(o->resolver.locator, locator); -} +//void kee_entry_item_store_set_resolve(KeeEntryItemStore *o, const char *locator) { +// strcpy(o->resolver.locator, locator); +//} static GType kee_entry_item_store_get_item_type(GListModel *list) { return KEE_TYPE_ENTRY_ITEM; @@ -60,20 +66,19 @@ static guint kee_entry_item_store_get_n_items(GListModel *list) { static gpointer kee_entry_item_store_get_item(GListModel *list, guint index) { - int r; KeeEntryItem *o; KeeEntryItemStore *store; // // //kee_entry_load(o, list->db); store = KEE_ENTRY_ITEM_STORE(list); - o = kee_entry_item_new(store->db, store->ledger); - kee_entry_item_set_resolver(o, &store->resolver); - kee_entry_item_store_seek(store, index); + o = kee_entry_item_new(store->db, store->ledger, (int)index); + kee_entry_item_set_resolver(o, store->resolver); + //kee_entry_item_store_seek(store, index); //kee_entry_deserialize(o, store->last_key, 9, store->last_value, store->last_value_length); - r = kee_entry_item_deserialize(o, store->last_value, store->last_value_length); - if (r) { - return NULL; - } + //r = kee_entry_item_deserialize(o, store->last_value, store->last_value_length); + //if (r) { + // return NULL; + //} // //return o; kee_entry_item_apply_list_item_widget(o); @@ -88,75 +93,81 @@ static void kee_entry_item_store_iface_init(GListModelInterface *ifc) { ifc->get_item = kee_entry_item_store_get_item; } -KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, struct kee_ledger_t *ledger) { +KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, struct kee_ledger_t *ledger, Cadiz *resolver) { KeeEntryItemStore *o; o = g_object_new(KEE_TYPE_ENTRY_ITEM_STORE, NULL); o->db = db; - o->last = calloc(4096, 1); - o->last_digest = o->last + DB_KEY_SIZE_LIMIT; - o->last_key = o->last; - o->last_value = o->last_digest + 64; - o->last_value_length = 1024; +// o->last = calloc(4096, 1); +// o->last_digest = o->last + DB_KEY_SIZE_LIMIT; +// o->last_key = o->last; +// o->last_value = o->last_digest + 64; +// o->last_value_length = 1024; o->ledger = ledger; + o->resolver = resolver; o->last_count = kee_entry_item_store_seek(o, INT_MAX); - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "max key index is: %d", o->last_idx - 1); +// o->ref_mem = malloc(o->last_count * entry_key_len); +// o->ref = &o->ref_mem; + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "max key index is: %d", o->last_count - 1); return o; } + /// \todo always scans from 0, inefficient /// \todo enum lookup states static int kee_entry_item_store_seek(KeeEntryItemStore *o, int idx) { + struct kee_ledger_item_t *item; int r; int i; size_t key_len; //size_t entry_key_len; - size_t cmp_key_len; - //char *mem[4096]; - //char *last_key; - char mem[DB_KEY_SIZE_LIMIT]; - char *cmp_key = (char*)mem; + //size_t cmp_key_len; + char *mem[4096]; + char *last_key; + //char mem[DB_KEY_SIZE_LIMIT]; + //char *cmp_key = (char*)mem; char *entry_key; - //char *last_value; - //size_t last_value_length; + char *last_value; + size_t last_value_length; //char out[1024]; //size_t out_len; - cmp_key_len = 65; - key_len = cmp_key_len + 8; - //o.last_key = (char*)mem; - //entry_key = last_key + 128; - //last_value = entry_key + 128; - //*last_key = DbKeyLedgerEntry; - *cmp_key = DbKeyLedgerEntry; - //memcpy(last_key+1, o->ledger->digest, key_len - 1); - memcpy(cmp_key+1, o->ledger->digest, key_len - 1); - //memcpy(entry_key, last_key, entry_key_len); - memcpy(o->last_key, cmp_key, cmp_key_len); + //cmp_key_len = 65; + //entry_key_len = 65; + key_len = entry_ref_len; //entry_key_len + 8; + last_key = (char*)mem; + entry_key = last_key + 128; + last_value = entry_key + 128; + *last_key = DbKeyLedgerEntry; + //*cmp_key = DbKeyLedgerEntry; + memcpy(last_key+1, o->ledger->digest, key_len - 1); + //memcpy(cmp_key+1, o->ledger->digest, key_len - 1); + memcpy(entry_key, last_key, entry_ref_len); + //memcpy(o->last_key, cmp_key, cmp_key_len); i = 0; while (i <= idx) { - o->last_idx = i; - o->last_value_length = 2048; - //r = db_next(o->db, DbKeyLedgerEntry, &last_key, &key_len, &last_value, &last_value_length); - r = db_next(o->db, DbKeyLedgerEntry, &cmp_key, &key_len, &o->last_value, &o->last_value_length); + last_value_length = 2048; + r = db_next(o->db, DbKeyLedgerEntry, &last_key, &key_len, &last_value, &last_value_length); + //r = db_next(o->db, DbKeyLedgerEntry, &cmp_key, &key_len, &o->last_value, &o->last_value_length); if (r) { break; } - //if (memcmp(last_key, entry_key, entry_key_len)) { - if (memcmp(cmp_key, o->last_key, cmp_key_len)) { + if (memcmp(entry_key, last_key, entry_ref_len)) { + //if (memcmp(cmp_key, o->last_key, cmp_key_len)) { break; } -// out_len = 1024; -// r = kee_entry_item_deserialize(o, last_value, last_value_length); -// if (r) { -// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "corrupt entry!"); -// } else { -// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "adding entry: %s", out); -// i++; -// } - i++; + //out_len = 1024; + //r = kee_entry_item_deserialize(o, last_value, last_value_length); + item = kee_ledger_parse_item(o->ledger, last_value, last_value_length); + if (item == NULL) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "corrupt entry!"); + } else { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "adding entry: %d", i); + i++; + } + //memcpy(entry_key, last_key, entry_key_len); //o->alice_credit_balance += o-> } db_rewind(o->db); @@ -164,8 +175,6 @@ static int kee_entry_item_store_seek(KeeEntryItemStore *o, int idx) { } void kee_entry_item_store_finalize(GObject *go) { - KeeEntryItemStore *o = KEE_ENTRY_ITEM_STORE(go); + //KeeEntryItemStore *o = KEE_ENTRY_ITEM_STORE(go); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "freeing entry item store"); - free(o->resolver.locator); - free(o->last); } diff --git a/src/gtk/kee-entry-item-store.h b/src/gtk/kee-entry-item-store.h @@ -4,13 +4,14 @@ #include <glib-object.h> #include "db.h" #include "ledger.h" +#include "cadiz.h" G_BEGIN_DECLS #define KEE_TYPE_ENTRY_ITEM_STORE kee_entry_item_store_get_type() G_DECLARE_FINAL_TYPE(KeeEntryItemStore, kee_entry_item_store, KEE, ENTRY_ITEM_STORE, GObject); -KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, struct kee_ledger_t *ledger); +KeeEntryItemStore* kee_entry_item_store_new(struct db_ctx *db, struct kee_ledger_t *ledger, Cadiz *resolver); void kee_entry_item_store_set_resolve(KeeEntryItemStore *o, const char *locator); G_END_DECLS diff --git a/src/gtk/kee-entry-item.c b/src/gtk/kee-entry-item.c @@ -30,27 +30,6 @@ struct _KeeEntryItem { G_DEFINE_TYPE(KeeEntryItem, kee_entry_item, GTK_TYPE_BOX); -void kee_entry_item_handle_setup(GtkListItemFactory* o, GtkListItem *item) { - GtkWidget *box; - - box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); - gtk_list_item_set_child(item, box); -} - -void kee_entry_item_handle_bind(GtkListItemFactory *o, GtkListItem *item) { - //GtkWidget *label; - GtkWidget *box; - GtkWidget *box_item; - //GtkStringObject *s; - - box = gtk_list_item_get_child(item); - //s = gtk_list_item_get_item(item); - box_item = gtk_list_item_get_item(item); - //gtk_label_set_label(GTK_LABEL(label), gtk_string_object_get_string(s)); - //gtk_label_set_label(GTK_LABEL(label), GTK_LABEL(s)); - gtk_box_append(GTK_BOX(box), box_item); - -} static void kee_entry_item_dispose(GObject *o) { } @@ -73,30 +52,36 @@ void kee_entry_item_set_resolver(KeeEntryItem *o, struct Cadiz *resolver) { o->resolver = resolver; } -int kee_entry_item_deserialize(KeeEntryItem *o, const char *data, size_t data_len) { - o->item = kee_ledger_parse_item(o->ledger, data, data_len); - if (o->item == NULL) { - return ERR_FAIL; - } - kee_content_resolve(&o->item->content, o->resolver); - - if (o->item->content.flags & KEE_CONTENT_RESOLVED_SUBJECT) { - strcpy(o->header, o->item->content.subject); - } else { - strcpy(o->header, "(no subject)"); - } - - return ERR_OK; -} - -KeeEntryItem* kee_entry_item_new(struct db_ctx *db, struct kee_ledger_t *ledger) { +KeeEntryItem* kee_entry_item_new(struct db_ctx *db, struct kee_ledger_t *ledger, int idx) { + int i; KeeEntryItem *o; + o = KEE_ENTRY_ITEM(g_object_new(KEE_TYPE_ENTRY_ITEM, "orientation", GTK_ORIENTATION_VERTICAL, NULL)); o->db = db; o->ledger = ledger; + o->item = ledger->last_item; + for (i = 0; i < idx; i++) { + o->item = o->item->prev_item; + } return o; } +//int kee_entry_item_deserialize(KeeEntryItem *o, const char *data, size_t data_len) { +// o->item = kee_ledger_parse_item(o->ledger, data, data_len); +// if (o->item == NULL) { +// return ERR_FAIL; +// } +// kee_content_resolve(&o->item->content, o->resolver); +// +// if (o->item->content.flags & KEE_CONTENT_RESOLVED_SUBJECT) { +// strcpy(o->header, o->item->content.subject); +// } else { +// strcpy(o->header, "(no subject)"); +// } +// +// return ERR_OK; +//} + void kee_entry_item_apply_list_item_widget(KeeEntryItem *o) { GtkWidget *widget; @@ -106,7 +91,8 @@ void kee_entry_item_apply_list_item_widget(KeeEntryItem *o) { //} //sprintf(o->header, "%s [%s]\n%s (%s)", o->ledger.content.subject, o->ledger.uoa, o->bob_dn.cn, o->bob_dn.uid); - widget = gtk_label_new(o->header); + kee_content_resolve(&o->item->content, o->resolver); + widget = gtk_label_new(o->item->content.subject); gtk_box_append(GTK_BOX(o), widget); return; } diff --git a/src/gtk/kee-entry-item.h b/src/gtk/kee-entry-item.h @@ -21,7 +21,7 @@ enum KEE_ENTRY_ITEM_PROPS { #define KEE_TYPE_ENTRY_ITEM kee_entry_item_get_type() G_DECLARE_FINAL_TYPE(KeeEntryItem, kee_entry_item, KEE, ENTRY_ITEM, GtkBox); -KeeEntryItem* kee_entry_item_new(struct db_ctx *db, struct kee_ledger_t *ledger); +KeeEntryItem* kee_entry_item_new(struct db_ctx *db, struct kee_ledger_t *ledger, int idx); void kee_entry_item_handle_setup(GtkListItemFactory* o, GtkListItem *item); void kee_entry_item_handle_bind(GtkListItemFactory *o, GtkListItem *item); void kee_entry_item_set_resolver(KeeEntryItem *o, struct Cadiz *resolver); diff --git a/src/gtk/kee-entry-list.c b/src/gtk/kee-entry-list.c @@ -22,21 +22,19 @@ static void kee_entry_list_handle_select(GtkListView *view, guint i, KeeMenu *me KeeEntry *o; GtkWidget *widget; GtkWidget *container; - KeeEntry *showentry; sel = GTK_SINGLE_SELECTION(gtk_list_view_get_model(view)); o = KEE_ENTRY(gtk_single_selection_get_selected_item(sel)); - showentry = kee_entry_new(NULL); - kee_entry_apply_entry(showentry, o); + g_object_take_ref(G_OBJECT(o)); container = kee_menu_next(menu, "entry"); widget = gtk_widget_get_first_child(container); if (widget) { gtk_box_remove(GTK_BOX(container), widget); } - kee_entry_apply_display_widget(showentry); - gtk_box_append(GTK_BOX(container), GTK_WIDGET(showentry)); + kee_entry_apply_display_widget(o); + gtk_box_append(GTK_BOX(container), GTK_WIDGET(o)); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "list item selected %d", i); } diff --git a/src/gtk/kee-entry-store.c b/src/gtk/kee-entry-store.c @@ -145,12 +145,13 @@ KeeEntryStore* kee_entry_store_new(struct db_ctx *db) { void kee_entry_store_finalize(GObject *go) { KeeEntryStore *o = KEE_ENTRY_STORE(go); + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "freeing entry store"); free(o->resolver.locator); free(o->last); } -void kee_entry_store_foo(KeeEntryStore* o) { - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "foo db = %p", o->db); -} +//void kee_entry_store_foo(KeeEntryStore* o) { +// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "foo db = %p", o->db); +//} diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c @@ -50,23 +50,48 @@ struct _KeeEntry { }; G_DEFINE_TYPE(KeeEntry, kee_entry, GTK_TYPE_BOX); - +// +//static void kee_entry_handle_item_setup(GtkListItemFactory* o, GtkListItem *item) { +// GtkWidget *label; +// +// label = gtk_label_new(NULL); +// gtk_list_item_set_child(item, label); +//} +// +//static void kee_entry_handle_item_bind(GtkListItemFactory *o, GtkListItem *item) { +// GtkWidget *label; +// GtkStringObject *s; +// +// label = gtk_list_item_get_child(item); +// s = gtk_list_item_get_item(item); +// //gtk_label_set_label(GTK_LABEL(label), gtk_string_object_get_string(s)); +// +//} +// +//static void kee_entry_item_handle_setup(GtkListItemFactory* o, GtkListItem *item) { static void kee_entry_handle_item_setup(GtkListItemFactory* o, GtkListItem *item) { - GtkWidget *label; + GtkWidget *box; - label = gtk_label_new(NULL); - gtk_list_item_set_child(item, label); + box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + gtk_list_item_set_child(item, box); } +//static void kee_entry_item_handle_bind(GtkListItemFactory *o, GtkListItem *item) { static void kee_entry_handle_item_bind(GtkListItemFactory *o, GtkListItem *item) { - GtkWidget *label; - GtkStringObject *s; - - label = gtk_list_item_get_child(item); - s = gtk_list_item_get_item(item); - gtk_label_set_label(GTK_LABEL(label), gtk_string_object_get_string(s)); + //GtkWidget *label; + GtkWidget *box; + GtkWidget *box_item; + //GtkStringObject *s; + + box = gtk_list_item_get_child(item); + //s = gtk_list_item_get_item(item); + box_item = gtk_list_item_get_item(item); + g_object_take_ref(G_OBJECT(box_item)); + //gtk_label_set_label(GTK_LABEL(label), gtk_string_object_get_string(s)); + //gtk_label_set_label(GTK_LABEL(label), GTK_LABEL(s)); + gtk_box_append(GTK_BOX(box), box_item); + } - /// \todo free reference to self from parent box necessary..? static void kee_entry_dispose(GObject *o) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "disposing entry"); @@ -132,11 +157,11 @@ int kee_entry_deserialize(KeeEntry *o, const char *data, size_t data_len) { if (r) { return ERR_FAIL; } - db_rewind(o->db); r = kee_dn_from_str(&o->bob_dn, last_value, last_value_length); if (r) { return ERR_FAIL; } + db_rewind(o->db); last_value_length = 129; strcpy(last_value, "uid="); @@ -145,7 +170,7 @@ int kee_entry_deserialize(KeeEntry *o, const char *data, size_t data_len) { if (r) { return ERR_FAIL; } - r = kee_dn_from_str(&o->bob_dn, last_value, last_value_length); + r = kee_dn_from_str(&o->bob_dn, last_value, last_value_length+4); if (r) { return ERR_FAIL; } @@ -175,83 +200,23 @@ void kee_entry_apply_list_item_widget(KeeEntry *o) { return; } -static int kee_entry_load_items(KeeEntry *o, GtkStringList *list) { -// int r; -// size_t key_len; -// size_t entry_key_len; -// char *mem = malloc(4096); -// char *last_key; -// char *entry_key; -// char *last_value; -// size_t last_value_length; -// char out[1024]; -// size_t out_len; -// -// entry_key_len = 65; -// key_len = entry_key_len + 8; -// last_key = (char*)mem; -// entry_key = last_key + 128; -// last_value = entry_key + 128; -// *last_key = DbKeyLedgerEntry; -// memcpy(last_key+1, o->current_id, key_len - 1); -// memcpy(entry_key, last_key, entry_key_len); -// while (1) { -// last_value_length = 2048; -// r = db_next(o->db, DbKeyLedgerEntry, &last_key, &key_len, &last_value, &last_value_length); -// if (r) { -// break; -// } -// if (memcmp(last_key, entry_key, entry_key_len)) { -// break; -// } -// out_len = 1024; -// r = kee_entry_deserialize_item(o, last_value, last_value_length, out, &out_len); -// if (r) { -// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "corrupt entry!"); -// } else { -// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "adding entry: %s", out); -// gtk_string_list_append(list, out); -// } -// //o->alice_credit_balance += o-> -// } -// db_rewind(o->db); -// free(mem); - return ERR_OK; -} - void kee_entry_apply_display_widget(KeeEntry *o) { GtkWidget *widget; GtkSingleSelection *sel; GtkListItemFactory *factory; KeeEntryItemStore *model; - //GtkStringList *list; - - //list = gtk_string_list_new(NULL); - //kee_entry_load_items(o, list); widget = gtk_label_new(o->ledger.content.subject); gtk_box_append(GTK_BOX(o), widget); + return; factory = gtk_signal_list_item_factory_new(); - g_signal_connect(factory, "setup", G_CALLBACK(kee_entry_item_handle_setup), NULL); - g_signal_connect(factory, "bind", G_CALLBACK(kee_entry_item_handle_bind), NULL); + g_signal_connect(factory, "setup", G_CALLBACK(kee_entry_handle_item_setup), NULL); + g_signal_connect(factory, "bind", G_CALLBACK(kee_entry_handle_item_bind), NULL); - model = kee_entry_item_store_new(o->db, &o->ledger); - kee_entry_item_store_set_resolve(model, "./testdata_resource"); + model = kee_entry_item_store_new(o->db, &o->ledger, o->resolver); sel = gtk_single_selection_new(G_LIST_MODEL(model)); widget = gtk_list_view_new(GTK_SELECTION_MODEL(sel), GTK_LIST_ITEM_FACTORY(factory)); - //g_signal_connect(view, "activate", G_CALLBACK(kee_entry_item_handle_select), win); gtk_box_append(GTK_BOX(o), widget); return; } - -void kee_entry_apply_entry(KeeEntry *target, KeeEntry *orig) { - target->db = orig->db; - memcpy(target->current_id, orig->current_id, 128); - target->resolver = orig->resolver; - memcpy(&target->ledger, &orig->ledger, sizeof(struct kee_ledger_t)); - target->state = orig->state; - target->bob_dn = orig->bob_dn; - return; -} - diff --git a/src/ledger.c b/src/ledger.c @@ -204,7 +204,6 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con char tmp[64]; int v; - prev = ledger->last_item; ledger->last_item = calloc(sizeof(struct kee_ledger_item_t), 1); cur = ledger->last_item; @@ -319,7 +318,6 @@ int kee_ledger_parse(struct kee_ledger_t *ledger, const char *data, size_t data_ char content_key[64]; memset(ledger, 0, sizeof(struct kee_ledger_t)); - memset(&root, 0, sizeof(root)); memset(&item, 0, sizeof(item)); r = asn1_array2tree(schema_entry_asn1_tab, &root, err);