commit 541cfaa5413191c69153b252c8ee7004a61360c9
parent 4ffcbd833ad5bd206e5c7860e91ae015e414ee97
Author: lash <dev@holbrook.no>
Date: Sat, 19 Apr 2025 14:23:07 +0100
Add envelope type
Diffstat:
5 files changed, 199 insertions(+), 3 deletions(-)
diff --git a/src/asn1/defs.txt b/src/asn1/defs.txt
@@ -20,4 +20,11 @@ Qaeda DEFINITIONS EXPLICIT TAGS ::= BEGIN
--- parent ANY OPTIONAL
parent OCTET STRING OPTIONAL
}
+
+ Envelope ::= SEQUENCE {
+ hint INTEGER,
+ cert Cert,
+ attach SEQUENCE OF OCTET STRING OPTIONAL
+ }
+
END
diff --git a/src/lq/envelope.c b/src/lq/envelope.c
@@ -0,0 +1,63 @@
+#include <stddef.h>
+
+#include <lq/envelope.h>
+#include <lq/cert.h>
+#include <lq/mem.h>
+#include <lq/err.h>
+
+
+static struct lq_attach *lq_attach_new() {
+ struct lq_attach *o;
+
+ o = lq_alloc(sizeof(struct lq_attach));
+ if (o == NULL) {
+ return o;
+ }
+ lq_zero(o, sizeof(struct lq_attach));
+ return o;
+}
+
+static struct lq_attach *lq_attach_add(struct lq_attach *attach, const char *data, size_t data_len) {
+ attach->len = data_len;
+ attach->data = lq_alloc(data_len);
+ lq_cpy(attach->data, data, data_len);
+ attach->next = lq_alloc(sizeof(struct lq_attach));
+ lq_zero(attach->next, sizeof(struct lq_attach));
+ return attach->next;
+}
+
+static void lq_attach_free(struct lq_attach *attach) {
+ if (attach->next != NULL) {
+ lq_attach_free(attach->next);
+ }
+ lq_free(attach->data);
+ lq_free(attach);
+}
+
+LQEnvelope *lq_envelope_new(LQCert *cert, int hint) {
+ LQEnvelope *env;
+
+ env = lq_alloc(sizeof(LQEnvelope));
+ env->hint = hint;
+ env->cert = cert;
+ env->attach_start = lq_attach_new();
+ env->attach_cur = env->attach_start;
+}
+
+int lq_envelope_attach(LQEnvelope *env, const char *data, size_t data_len) {
+ struct lq_attach *attach;
+
+ attach = lq_attach_add(env->attach_cur, data, data_len);
+ if (attach == NULL) {
+ return ERR_FAIL;
+ }
+ env->attach_cur = attach;
+
+ return ERR_OK;
+}
+
+void lq_envelope_free(LQEnvelope *env) {
+ lq_attach_free(env->attach_start);
+ lq_certificate_free(env->cert);
+ lq_free(env);
+}
diff --git a/src/lq/envelope.h b/src/lq/envelope.h
@@ -0,0 +1,38 @@
+#ifndef LIBQAEDA_ENVELOPE_H_
+#define LIBQAEDA_ENVELOPE_H_
+
+#include <stddef.h>
+#include <lq/cert.h>
+
+struct lq_attach {
+ char *data;
+ size_t len;
+ struct lq_attach *next;
+};
+
+struct lq_envelope {
+ int hint;
+ LQCert *cert;
+ struct lq_attach *attach_start;
+ struct lq_attach *attach_cur;
+};
+typedef struct lq_envelope LQEnvelope;
+
+
+/**
+ * \brief Set up a new attachment object
+ *
+ *
+ */
+LQEnvelope *lq_envelope_new(LQCert *cert, int hint);
+
+/**
+ *
+ * \return ERR_OK on success.
+ */
+int lq_envelope_attach(LQEnvelope *env, const char *data, size_t data_len);
+int lq_envelope_serialize(LQEnvelope *env, const char *data, size_t data_len);
+int lq_envelope_deserialize(LQEnvelope **env, const char *data, size_t *data_len);
+void lq_envelope_free(LQEnvelope *env);
+
+#endif // LIBQAEDA_ENVELOPE_H_
diff --git a/src/test/Makefile b/src/test/Makefile
@@ -7,8 +7,8 @@ LIBS := ../asn1/defs_asn1_tab.o `pkg-config --libs libtasn1 libgcrypt` -L.. -L..
LDFLAGS := -lcheck $(LIBS)
COMMONOBJS = ../mem/std.o ../lq/config.o ../lq/err.o ../lq/base.o ../debug.o
-all: build all-tests
-#all: build one-test
+#all: build all-tests
+all: build one-test
all-tests:
cK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_test_bin
@@ -17,11 +17,12 @@ all-tests:
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_crypto_bin
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_msg_bin
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_cert_bin
+ CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_envelope_bin
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_trust_bin
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_store_bin
one-test: build
- CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_msg_bin
+ CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_envelope_bin
test: all
@@ -35,6 +36,7 @@ build:
$(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin $(COMMONOBJS) $(LDFLAGS) -lgcrypt
$(CC) $(CFLAGS) test_msg.c -o test_msg_bin $(COMMONOBJS) ../store/dummy.o $(LDFLAGS) -lgcrypt
$(CC) $(CFLAGS) test_cert.c -o test_cert_bin $(COMMONOBJS) ../store/dummy.o $(LDFLAGS) -lgcrypt
+ $(CC) $(CFLAGS) test_envelope.c -o test_envelope_bin $(COMMONOBJS) ../store/dummy.o $(LDFLAGS) -lgcrypt
$(CC) $(CFLAGS) test_trust.c -o test_trust_bin $(COMMONOBJS) $(LDFLAGS)
$(CC) $(CFLAGS) test_store.c -o test_store_bin $(COMMONOBJS) $(LDFLAGS) -lgcrypt
diff --git a/src/test/test_envelope.c b/src/test/test_envelope.c
@@ -0,0 +1,86 @@
+#include <check.h>
+#include <stdlib.h>
+#include <string.h>
+
+//#include "lq/msg.h"
+#include "lq/cert.h"
+#include "lq/mem.h"
+//#include "lq/crypto.h"
+#include "lq/config.h"
+#include "lq/base.h"
+#include "lq/envelope.h"
+#include "lq/io.h"
+
+const char *data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
+const char *data_two = "Que trata de la condición y ejercicio del famoso hidalgo D. Quijote de la Mancha En un lugar de la Mancha, de cuyo nombre no quiero acordarme, no ha mucho tiempo que vivía un hidalgo de los de lanza en astillero, adarga antigua, rocín flaco y galgo corredor.";
+
+START_TEST(check_envelope) {
+ int r;
+ size_t c;
+ LQCert *cert;
+ LQEnvelope *env;
+ LQMsg *req;
+ char buf[4096];
+
+ req = lq_msg_new(data, strlen(data) + 1);
+ ck_assert_ptr_nonnull(req);
+
+ cert = lq_certificate_new(NULL);
+ ck_assert_ptr_nonnull(cert);
+
+ r = lq_certificate_request(cert, req, NULL);
+ c = LQ_BLOCKSIZE;
+ r = lq_certificate_serialize(cert, NULL, buf, &c);
+ ck_assert_int_eq(r, 0);
+
+ env = lq_envelope_new(cert, 0);
+ ck_assert_ptr_nonnull(env);
+ r = lq_envelope_attach(env, data, strlen(data) + 1);
+ ck_assert_int_eq(r, 0);
+ r = lq_envelope_attach(env, data_two, strlen(data_two) + 1);
+ ck_assert_int_eq(r, 0);
+ lq_envelope_free(env);
+}
+END_TEST
+
+
+Suite * common_suite(void) {
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("envelope");
+ tc = tcase_create("serialize");
+ tcase_add_test(tc, check_envelope);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
+
+int main(void) {
+ int r;
+ int n_fail;
+ char path[LQ_PATH_MAX];
+
+ Suite *s;
+ SRunner *sr;
+
+ r = lq_init();
+ if (r) {
+ return 1;
+ }
+
+ lq_cpy(path, "/tmp/lqcrypto_test_XXXXXX", 26);
+ r = lq_crypto_init(mktempdir(path));
+ if (r) {
+ return 1;
+ }
+
+ s = common_suite();
+ sr = srunner_create(s);
+
+ srunner_run_all(sr, CK_VERBOSE);
+ n_fail = srunner_ntests_failed(sr);
+ srunner_free(sr);
+
+ return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}