commit 41c80cf5ab22d3c204a0289df9e3c3a3297c5f28
parent 1607d9596c40962e1275a30aec311af6e0774874
Author: lash <dev@holbrook.no>
Date: Sun, 30 Mar 2025 15:28:36 +0100
Memstore does not store fingerprinted key as well as default key
Diffstat:
4 files changed, 46 insertions(+), 26 deletions(-)
diff --git a/src/crypto/gcrypt.c b/src/crypto/gcrypt.c
@@ -57,6 +57,8 @@ const static char gpg_fingerprint_zero[LQ_FP_LEN];
const static char gpg_default_store_key;
+const static LQStore *gpg_key_store;
+
/**
* Verifies that installed gpg version is supported.
* Sets up crypto keys dir and sets passphrase digest length.
@@ -95,6 +97,10 @@ int lq_crypto_init(const char *base) {
if (r) {
return ERR_FAIL;
}
+ gpg_key_store = lq_store_new(path);
+ if (gpg_key_store == NULL) {
+ return ERR_STORE_AVAIL;
+ }
return ERR_OK;
}
@@ -351,11 +357,12 @@ LQStore *key_store_get() {
int r;
char *p;
- r = lq_config_get(gpg_cfg_idx_dir, (void**)&p);
- if (r) {
- return NULL;
- }
- return lq_store_new(p);
+// r = lq_config_get(gpg_cfg_idx_dir, (void**)&p);
+// if (r) {
+// return NULL;
+// }
+// return lq_store_new(p);
+ return gpg_key_store;
}
/**
@@ -426,7 +433,6 @@ static int key_create_store(struct gpg_store *gpg, const char *passphrase, size_
// Instantiate the store.
store = key_store_get();
if (store == NULL) {
- lq_free(store);
return debug_logerr(LLOG_ERROR, ERR_KEYFILE, "create store");
}
@@ -435,7 +441,6 @@ static int key_create_store(struct gpg_store *gpg, const char *passphrase, size_
c = LQ_FP_LEN;
r = store->put(LQ_CONTENT_KEY, store, buf_key, &c, buf_val, l);
if (r) {
- lq_free(store);
return debug_logerr(LLOG_ERROR, ERR_KEYFILE, "put key in store");
}
@@ -446,22 +451,17 @@ static int key_create_store(struct gpg_store *gpg, const char *passphrase, size_
r = store->get(LQ_CONTENT_KEY, store, buf_key, 1, buf_val, &c);
if (r) {
if (r != ERR_NOENT) {
- lq_free(store);
debug(LLOG_ERROR, "crypto.gcrypt", "no default");
return debug_logerr(LLOG_ERROR, ERR_KEYFILE, "default key");
}
c = 1;
r = store->put(LQ_CONTENT_KEY, store, buf_key, &c, buf_val, l);
if (r) {
- lq_free(store);
debug(LLOG_ERROR, "crypto.gcrypt", "fail put default");
return debug_logerr(LLOG_ERROR, ERR_KEYFILE, "write default key");
}
}
- // Clean up.
- lq_free(store);
-
return ERR_OK;
}
@@ -563,7 +563,6 @@ static int key_from_store(struct gpg_store *gpg, const char *passphrase, size_t
}
r = store->get(LQ_CONTENT_KEY, store, inkey, inkey_len, in, &in_len);
if (r) {
- lq_free(store);
return ERR_NOENT;
}
@@ -580,7 +579,6 @@ static int key_from_store(struct gpg_store *gpg, const char *passphrase, size_t
in_len -= CHACHA20_NONCE_LENGTH_BYTES;
r = decryptb(out, p, in_len, passphrase_hash, nonce);
if (r) {
- lq_free(store);
return ERR_KEY_UNLOCK;
}
@@ -589,11 +587,9 @@ static int key_from_store(struct gpg_store *gpg, const char *passphrase, size_t
p = (char*)(out+sizeof(int));
r = key_from_data(&gpg->k, p, out_len);
if (r) {
- lq_free(store);
return ERR_KEYFAIL;
}
- lq_free(store);
return ERR_OK;
}
@@ -947,4 +943,8 @@ size_t lq_publickey_fingerprint(LQPubKey* pubk, char **out) {
return LQ_FP_LEN;
}
+void lq_crypto_free() {
+ lq_free(gpg_key_store);
+}
+
#endif
diff --git a/src/store/mem.c b/src/store/mem.c
@@ -55,6 +55,7 @@ static long unsigned int pair_hash(const void *item, long unsigned int s0, long
struct hashmap* lq_mem_init(LQStore *store) {
if (store->userdata == NULL) {
store->userdata = (void*)hashmap_new(sizeof(struct pair_t) , 1024*1024, 0, 0, pair_hash, pair_cmp, NULL, NULL);
+ debug(LLOG_INFO, "store.mem", "created new hashmap for mem store");
}
return (struct hashmap *)store->userdata;
}
@@ -71,6 +72,8 @@ int lq_mem_content_get(enum payload_e typ, LQStore *store, const char *key, size
lq_cpy(path+1, key, key_len);
v.key = path;
v.key_len = key_len + 1;
+ v.val = value;
+ v.val_len = *value_len;
debug_x(LLOG_DEBUG, "store.mem", "store get req", 1, MORGEL_TYP_BIN, v.key_len, "key", v.key);
@@ -87,10 +90,13 @@ int lq_mem_content_get(enum payload_e typ, LQStore *store, const char *key, size
}
int lq_mem_content_put(enum payload_e typ, LQStore *store, const char *key, size_t *key_len, char *value, size_t value_len) {
+ char *r;
struct hashmap *o;
struct pair_t v;
char path[LQ_PATH_MAX];
+ o = lq_mem_init(store);
+
path[0] = (char)typ;
lq_cpy(path+1, key, *key_len);
v.key = path;
@@ -98,10 +104,16 @@ int lq_mem_content_put(enum payload_e typ, LQStore *store, const char *key, size
v.val = value;
v.val_len = value_len;
- o = lq_mem_init(store);
- hashmap_set(o, &v);
+ debug_x(LLOG_DEBUG, "store.mem", "store put req", 2, MORGEL_TYP_BIN, v.key_len, "key", v.key, MORGEL_TYP_NUM, 0, "bytes", value_len);
+
+ r = hashmap_set(o, &v);
+ if (r != NULL) {
+ if (hashmap_oom(o)) {
+ return ERR_WRITE;
+ }
+ }
- debug_x(LLOG_DEBUG, "store.mem", "store put", 2, MORGEL_TYP_BIN, v.key_len, "key", v.key, MORGEL_TYP_NUM, 0, "bytes", value_len);
+ debug_x(LLOG_DEBUG, "store.mem", "store put res", 2, MORGEL_TYP_BIN, v.key_len, "key", v.key, MORGEL_TYP_NUM, 0, "bytes", value_len);
return ERR_OK;
}
@@ -127,6 +139,7 @@ LQStore* lq_store_new(const char *spec) {
debug(LLOG_DEBUG, "store.mem", "ignoring spec in mem store init");
store = lq_alloc(sizeof(LQStore));
lq_cpy(store, &LQMemContent, sizeof(LQMemContent));
+ store->userdata = NULL;
return store;
}
diff --git a/src/test/Makefile b/src/test/Makefile
@@ -27,7 +27,7 @@ build:
#$(CC) $(CFLAGS) test_cert.c -o test_cert_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o ../lq/cert.o $(LDFLAGS)
#$(CC) $(CFLAGS) test_trust.c -o test_trust_bin ../crypto/dummy.o ../mem/std.o ../store/mem.o ../lq/trust.o -lhashmap $(LDFLAGS)
#$(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin $(COMMONOBJS) ../store/file.o ../io/std.o ../crypto/gcrypt.o $(LDFLAGS) -lgcrypt
- $(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin $(COMMONOBJS) ../store/mem.o ../io/std.o ../crypto/gcrypt.o $(LDFLAGS) -lgcrypt
+ $(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin $(COMMONOBJS) ../store/file.o ../io/std.o ../crypto/gcrypt.o $(LDFLAGS) -lgcrypt
$(CC) $(CFLAGS) test_msg.c -o test_msg_bin $(COMMONOBJS) ../store/file.o ../store/dummy.o ../io/std.o ../crypto/gcrypt.o ../lq/msg.o $(LDFLAGS)
$(CC) $(CFLAGS) test_cert.c -o test_cert_bin $(COMMONOBJS) ../store/file.o ../io/std.o ../crypto/gcrypt.o ../store/dummy.o ../lq/msg.o ../lq/cert.o $(LDFLAGS)
$(CC) $(CFLAGS) test_trust.c -o test_trust_bin $(COMMONOBJS) ../store/mem.o ../crypto/gcrypt.o ../lq/trust.o $(LDFLAGS)
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
@@ -57,6 +57,7 @@ START_TEST(check_publickey) {
char *keydata_manual;
pk = lq_privatekey_new(passphrase, passphrase_len);
+ ck_assert_ptr_nonnull(pk);
pubk = lq_publickey_from_privatekey(pk);
lq_publickey_bytes(pubk, &keydata);
pubk_manual = lq_publickey_new(keydata);
@@ -76,6 +77,7 @@ START_TEST(check_signature) {
char *sigdata;
pk = lq_privatekey_new(passphrase, passphrase_len);
+ ck_assert_ptr_nonnull(pk);
sig = lq_privatekey_sign(pk, data, strlen(data), salt);
ck_assert_ptr_null(sig);
@@ -99,6 +101,7 @@ START_TEST(check_verify) {
LQSig *sig;
pk = lq_privatekey_new(passphrase, 32);
+ ck_assert_ptr_nonnull(pk);
sig = lq_privatekey_sign(pk, data, strlen(data), salt);
ck_assert_ptr_null(sig);
@@ -141,6 +144,8 @@ START_TEST(check_load_specific) {
ck_assert_ptr_nonnull(pubk);
c = lq_publickey_fingerprint(pubk, &p);
ck_assert_int_gt(c, 0);
+ pk_load = lq_privatekey_load(passphrase, passphrase_len, NULL);
+ ck_assert_ptr_nonnull(pk_load);
pk_load = lq_privatekey_load(passphrase, passphrase_len, p);
ck_assert_ptr_nonnull(pk_load);
@@ -154,12 +159,12 @@ Suite * common_suite(void) {
s = suite_create("crypto");
tc = tcase_create("file");
- tcase_add_test(tc, check_digest);
- tcase_add_test(tc, check_privatekey);
- tcase_add_test(tc, check_publickey);
- tcase_add_test(tc, check_signature);
- tcase_add_test(tc, check_verify);
- tcase_add_test(tc, check_create_load);
+// tcase_add_test(tc, check_digest);
+// tcase_add_test(tc, check_privatekey);
+// tcase_add_test(tc, check_publickey);
+// tcase_add_test(tc, check_signature);
+// tcase_add_test(tc, check_verify);
+// tcase_add_test(tc, check_create_load);
tcase_add_test(tc, check_load_specific);
suite_add_tcase(s, tc);
@@ -192,5 +197,7 @@ int main(void) {
n_fail = srunner_ntests_failed(sr);
srunner_free(sr);
+ lq_crypto_free();
+
return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}