kee

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

commit 517704ee020b55f77886f170fc5334986613594a
parent 7f1835d18f7cc08ecf5469cbb9becc335611eb3f
Author: lash <dev@holbrook.no>
Date:   Wed, 15 May 2024 09:59:04 +0100

Correct uoa decimal length corrupting digest generation

Diffstat:
Msrc/db.c | 23++++++++++++++++++++---
Msrc/db.h | 12++++++++++++
Msrc/gtk/context.c | 6+++++-
Msrc/gtk/kee-entry-item-store.c | 1+
Msrc/gtk/kee-entry-store.c | 4++--
Msrc/gtk/kee-entry.c | 3+--
Msrc/ledger.c | 51+++++++++++++++++++--------------------------------
Msrc/tests/ledger.c | 10++++------
Mtestdata_asn1.py | 36++++++++++++++++++++++--------------
9 files changed, 86 insertions(+), 60 deletions(-)

diff --git a/src/db.c b/src/db.c @@ -9,17 +9,20 @@ #include "err.h" #include "endian.h" #include "debug.h" +#include "hex.h" + +#define DB_DEFAULT_TX_CAP 10 int db_connect(struct db_ctx *ctx, char *conn) { int r; - db_reset(ctx); ctx->connstr = conn; + db_reset(ctx); r = mdb_env_create(&ctx->env); if (r) { return ERR_FAIL; } - r = mdb_env_open(ctx->env, ctx->connstr, MDB_NOLOCK, S_IRWXU); + r = mdb_env_open(ctx->env, ctx->connstr, MDB_WRITEMAP, S_IRWXU); if (r) { return ERR_FAIL; } @@ -29,6 +32,9 @@ int db_connect(struct db_ctx *ctx, char *conn) { return ERR_OK; } +void db_disconnect(struct db_ctx *ctx) { + mdb_env_close(ctx->env); +} int db_start(struct db_ctx *ctx) { int r; @@ -47,6 +53,8 @@ int db_start(struct db_ctx *ctx) { int db_add(struct db_ctx *ctx, char *key, size_t key_len, char *data, size_t data_len) { int r; + char s[1024]; + size_t c; ctx->k.mv_data = key; ctx->k.mv_size = key_len; @@ -58,6 +66,11 @@ int db_add(struct db_ctx *ctx, char *key, size_t key_len, char *data, size_t dat return ERR_FAIL; } + sprintf(s, "wrote key "); + c = 1000; + bin_to_hex(key, key_len, s+10, &c); + debug_log(DEBUG_DEBUG, s); + return ERR_OK; } @@ -257,8 +270,12 @@ void db_rewind(struct db_ctx *ctx) { void db_reset(struct db_ctx *ctx) { + char *s; + + db_rewind(ctx); mdb_cursor_close(ctx->crsr); mdb_dbi_close(ctx->env, ctx->dbi); - mdb_txn_abort(ctx->tx); + s = ctx->connstr; memset(ctx, 0, sizeof(struct db_ctx)); + ctx->connstr = s; } diff --git a/src/db.h b/src/db.h @@ -58,7 +58,19 @@ struct db_ctx { int browsing; }; +struct db_ctx_w { + char *connstr; + MDB_env *env; + MDB_dbi dbi; + MDB_txn *tx; + MDB_val *add_k; + MDB_val *add_v; + size_t add_cap; + size_t add_count; +}; + int db_connect(struct db_ctx *ctx, char *conn); +void db_disconnect(struct db_ctx *ctx); //int db_put(struct db_ctx *ctx, enum DbKey pfx, char *data, size_t data_len); int db_put(struct db_ctx *ctx, char *key, size_t key_len, char *data, size_t data_len); int db_next(struct db_ctx *ctx, enum DbKey pfx, char **key, size_t *key_len, char **value, size_t *value_len); diff --git a/src/gtk/context.c b/src/gtk/context.c @@ -11,12 +11,16 @@ int kee_context_init(struct kee_context *ctx, struct kee_settings *settings) { + int r; unsigned char *v; memset(ctx, 0, sizeof(struct kee_context)); ctx->state = 1; ctx->settings = settings; - db_connect(&ctx->db, (char*)settings->db); + r = db_connect(&ctx->db, (char*)settings->db); + if (r) { + return ERR_FAIL; + } v = settings_get(ctx->settings, SETTINGS_KEY); gpg_store_init(&ctx->gpg, (char*)v); ctx->entry_store = kee_entry_store_new(&ctx->db); diff --git a/src/gtk/kee-entry-item-store.c b/src/gtk/kee-entry-item-store.c @@ -124,6 +124,7 @@ static int kee_entry_item_store_scan(KeeEntryItemStore *o) { i++; } } + db_rewind(o->db); return i; } diff --git a/src/gtk/kee-entry-store.c b/src/gtk/kee-entry-store.c @@ -96,7 +96,7 @@ static int kee_entry_store_seek(KeeEntryStore *o, int idx) { int r; int i; size_t key_len; - + key_len = 9; o->last_key = o->last; memset(o->last_key, 0, key_len); @@ -117,7 +117,7 @@ static int kee_entry_store_seek(KeeEntryStore *o, int idx) { o->last_state = 1; i++; } - db_rewind(o->db); + return i; } diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c @@ -472,8 +472,7 @@ static int process_entry_ledger(KeeEntry *o) { if (r) { return ERR_FAIL; } - db_rewind(o->db); - + last_value_length = 129; strcpy(last_value, "uid="); if (o->bob_dn.uid == NULL) { diff --git a/src/ledger.c b/src/ledger.c @@ -246,10 +246,10 @@ static int verify_item_asn(struct kee_ledger_t *ledger, asn1_node item, const ch static int kee_ledger_digest(struct kee_ledger_t *ledger, char *out) { int r; - char out_data[1024]; + char out_data[4096]; size_t c; - c = 1024; + c = 4096; r = kee_ledger_serialize(ledger, out_data, &c); if (r) { return r; @@ -555,7 +555,8 @@ int kee_ledger_parse(struct kee_ledger_t *ledger, const char *data, size_t data_ return 1; } - r = calculate_digest_algo(data, data_len, (char*)ledger->digest, GCRY_MD_SHA512); + r = kee_ledger_digest(ledger, (char*)ledger->digest); + //r = calculate_digest_algo(data, data_len, (char*)ledger->digest, GCRY_MD_SHA512); if (r) { return 1; } @@ -588,31 +589,32 @@ int kee_ledger_serialize(struct kee_ledger_t *ledger, char *out, size_t *out_len return ERR_FAIL; } - c = strlen(ledger->uoa) + 1; + //c = strlen(ledger->uoa) + 1; + c = strlen(ledger->uoa); r = asn1_write_value(node, "Kee.KeeEntryHead.uoa", ledger->uoa, c); if (r != ASN1_SUCCESS) { return r; } - c = 4; + c = 1; r = asn1_write_value(node, "Kee.KeeEntryHead.uoaDecimals", &ledger->uoa_decimals, c); if (r != ASN1_SUCCESS) { return r; } - c = 32; + c = PUBKEY_LENGTH; r = asn1_write_value(node, "Kee.KeeEntryHead.alicePubKey", ledger->pubkey_alice, c); if (r != ASN1_SUCCESS) { return r; } - c = 32; + c = PUBKEY_LENGTH; r = asn1_write_value(node, "Kee.KeeEntryHead.bobPubKey", ledger->pubkey_bob, c); if (r != ASN1_SUCCESS) { return r; } - c = 64; + c = DIGEST_LENGTH; r = asn1_write_value(node, "Kee.KeeEntryHead.body", ledger->content.key, c); if (r != ASN1_SUCCESS) { return r; @@ -1046,29 +1048,29 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { size_t c; size_t l; //char *mem; - //char mem[4096]; + char mem[4096]; char *k; char *v; //mem = malloc(4096); - //k = (char*)mem; - //v = k + 2048; + k = (char*)mem; + v = k + 2048; //k = mem; //v = k + 2048; - k = malloc(2048); - v = malloc(2048); - k[0] = DbKeyReverse; + *k = DbKeyReverse; memcpy(k+1, ledger->digest, DIGEST_LENGTH); l = DIGEST_LENGTH + 1; //c = 928; // 1024 - 96 c = 2048; - db_rewind(db); + //db_rewind(db); + + r = db_next(db, DbKeyReverse, &k, &l, &v, &c); if (!r) { - k = v; + memcpy(k, v, c); l = c; - c = 928; + c = 2048; db_rewind(db); r = db_next(db, DbKeyLedgerHead, &k, &l, &v, &c); if (!r) { @@ -1082,8 +1084,6 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { l = db_key(DbKeyLedgerHead, NULL, k, 0); if (l == 0) { - free(k); - free(v); return ERR_FAIL; } @@ -1091,23 +1091,17 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { c = 2048; r = kee_ledger_serialize(ledger, v, &c); if (r) { - free(k); - free(v); return ERR_DB_FAIL; } r = db_start(db); if (r) { - free(k); - free(v); return ERR_DB_FAIL; } //r = db_put(db, k, l, v, c); r = db_add(db, k, l, v, c); if (r) { - free(k); - free(v); return ERR_DB_FAIL; } @@ -1119,8 +1113,6 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { //r = db_put(db, k, l, v+1, c-1); r = db_add(db, k, l, v+1, c-1); if (r) { - free(k); - free(v); return ERR_DB_FAIL; } @@ -1133,14 +1125,9 @@ int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db) { r = db_finish(db); if (r) { - free(k); - free(v); return ERR_DB_FAIL; } - free(k); - free(v); - return ERR_OK; } diff --git a/src/tests/ledger.c b/src/tests/ledger.c @@ -107,6 +107,10 @@ int test_pair() { return 1; } + if (memcmp(t.ledger.digest, ledger.digest, DIGEST_LENGTH)) { + return 1; + } + return 0; } @@ -129,12 +133,6 @@ int test_put() { return 1; } - //r = kee_ledger_item_put(&t.ledger, &t.db, 0); - //if (r) { - // return 1; - //} - - return 0; } diff --git a/testdata_asn1.py b/testdata_asn1.py @@ -84,10 +84,12 @@ def db_init(d): def data_add(data_dir, k, v): - fp = os.path.join(data_dir, k.hex()) - f = open(fp, 'wb') - f.write(v) - f.close() + if data_dir == None: + return + fp = os.path.join(data_dir, k.hex()) + f = open(fp, 'wb') + f.write(v) + f.close() class LedgerContent(email.message.EmailMessage): @@ -253,13 +255,14 @@ class LedgerBundle: class Ledger: + pass - @classmethod - def data_add(self, data_dir, k, v): - fp = os.path.join(data_dir, k.hex()) - f = open(fp, 'wb') - f.write(v) - f.close() +# @classmethod +# def data_add(self, data_dir, k, v): +# fp = os.path.join(data_dir, k.hex()) +# f = open(fp, 'wb') +# f.write(v) +# f.close() class LedgerGenerator: @@ -367,8 +370,10 @@ class LedgerHead(Ledger): if bob_key == None: bob_key = os.urandom(65) self.bob_pubkey_ref = bob_key - logg.info('new ledger header with alice {} bob {}'.format(self.alice_pubkey_ref.hex(), self.bob_pubkey_ref.hex())) self.body = LedgerHeadContent() + (k, v) = self.body.kv() + + logg.info('new ledger header with alice {} bob {} body {}'.format(self.alice_pubkey_ref.hex(), self.bob_pubkey_ref.hex(), k.hex())) def to_asn1(self, data_dir): @@ -378,7 +383,8 @@ class LedgerHead(Ledger): o['alicePubKey'] = self.alice_pubkey_ref o['bobPubKey'] = self.bob_pubkey_ref (k, v) = self.body.kv() - self.data_add(data_dir, k, v) + #self.data_add(data_dir, k, v) + data_add(data_dir, k, v) o['body'] = k return o @@ -386,6 +392,7 @@ class LedgerHead(Ledger): def serialize(self, data_dir, w=sys.stdout.buffer): o = self.to_asn1(data_dir) b = der_encode(o) + logg.debug('ledger header serialize ({}): {}'.format(len(b), b.hex())) w.write(b) @@ -445,7 +452,8 @@ class LedgerItem(Ledger): o['collateralDelta'] = self.collateral_delta (k, v) = self.body.kv() - self.data_add(data_dir, k, v) + #self.data_add(data_dir, k, v) + data_add(data_dir, k, v) o['body'] = k o['response'] = False @@ -455,7 +463,7 @@ class LedgerItem(Ledger): if mode == LedgerMode.REQUEST: return o - logg.debug('encoding new ledger_item for request signature: {}'.format(o)) + logg.debug('encoding new ledger_item for request signature {}: {}'.format(self.head.hex(), o)) b = der_encode(o) self.request_signature = self.signer.sign(self.signer_sequence[0], self.head + b) o['signatureRequest'] = self.request_signature