From: Michael Tremer Date: Fri, 27 Jun 2025 14:11:10 +0000 (+0000) Subject: key: Create its own type X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=bc52861a1d897d4847e2b9b2589ccc92bd039d58;p=pakfire.git key: Create its own type Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/repo_compose.c b/src/cli/lib/repo_compose.c index f481da4e..b39a5890 100644 --- a/src/cli/lib/repo_compose.c +++ b/src/cli/lib/repo_compose.c @@ -76,7 +76,7 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { return 0; } -static int cli_import_key(struct pakfire_key** key, +static int cli_import_key(pakfire_key** key, pakfire_ctx* ctx, const char* path) { FILE* f = NULL; int r; @@ -105,7 +105,7 @@ int cli_repo_compose(void* data, int argc, char* argv[]) { struct cli_global_args* global_args = data; struct cli_local_args local_args = {}; struct pakfire* pakfire = NULL; - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; int r; // Parse the command line diff --git a/src/pakfire/key.c b/src/pakfire/key.c index 58b2402d..bfcb26d6 100644 --- a/src/pakfire/key.c +++ b/src/pakfire/key.c @@ -65,38 +65,38 @@ struct pakfire_key { char comment[COMMENT_MAX]; }; -struct pakfire_key_public_key { +typedef struct pakfire_key_public_key { unsigned char sig_algo[2]; pakfire_key_id id; unsigned char pubkey[32]; -}; +} pakfire_key_public_key; -struct pakfire_key_private_key { +typedef struct pakfire_key_private_key { unsigned char sig_algo[2]; - unsigned char kdf_algo[2]; + unsigned char kdf_algo[2]; uint32_t kdf_rounds; - unsigned char kdf_salt[16]; + unsigned char kdf_salt[16]; unsigned char checksum[8]; pakfire_key_id id; struct { unsigned char secret[32]; unsigned char public[32]; } keys; -}; +} pakfire_key_private_key; -struct pakfire_key_signature { +typedef struct pakfire_key_signature { unsigned char sig_algo[2]; pakfire_key_id key_id; unsigned char signature[64]; -}; +} pakfire_key_signature; static int pakfire_key_id_equals(const pakfire_key_id* id1, const pakfire_key_id* id2) { return !memcmp(*id1, *id2, sizeof(*id1)); } -static int pakfire_key_create(struct pakfire_key** key, pakfire_ctx* ctx, +static int pakfire_key_create(pakfire_key** key, pakfire_ctx* ctx, const pakfire_key_algo_t algo, const pakfire_key_id id, EVP_PKEY* pkey, const char* comment) { - struct pakfire_key* k = NULL; + pakfire_key* k = NULL; int r = 0; if (!pkey) @@ -152,7 +152,7 @@ ERROR: return r; } -static void pakfire_key_free(struct pakfire_key* key) { +static void pakfire_key_free(pakfire_key* key) { // Free the key if (key->pkey) EVP_PKEY_free(key->pkey); @@ -161,13 +161,13 @@ static void pakfire_key_free(struct pakfire_key* key) { free(key); } -struct pakfire_key* pakfire_key_ref(struct pakfire_key* key) { +pakfire_key* pakfire_key_ref(pakfire_key* key) { ++key->nrefs; return key; } -struct pakfire_key* pakfire_key_unref(struct pakfire_key* key) { +pakfire_key* pakfire_key_unref(pakfire_key* key) { if (--key->nrefs > 0) return key; @@ -175,11 +175,11 @@ struct pakfire_key* pakfire_key_unref(struct pakfire_key* key) { return NULL; } -pakfire_key_id* pakfire_key_get_id(struct pakfire_key* key) { +pakfire_key_id* pakfire_key_get_id(pakfire_key* key) { return &key->id; } -const char* pakfire_key_get_algo(struct pakfire_key* key) { +const char* pakfire_key_get_algo(pakfire_key* key) { switch (key->algo) { case PAKFIRE_KEY_ALGO_ED25519: return "Ed255919"; @@ -191,11 +191,11 @@ const char* pakfire_key_get_algo(struct pakfire_key* key) { return NULL; } -const char* pakfire_key_get_comment(struct pakfire_key* key) { +const char* pakfire_key_get_comment(pakfire_key* key) { return key->comment; } -int pakfire_key_generate(struct pakfire_key** key, pakfire_ctx* ctx, +int pakfire_key_generate(pakfire_key** key, pakfire_ctx* ctx, const pakfire_key_algo_t algo, const char* comment) { EVP_PKEY* pkey = NULL; EVP_PKEY_CTX* pctx = NULL; @@ -271,9 +271,9 @@ ERROR: Import */ -static int pakfire_key_import_secret_key(struct pakfire_key** key, +static int pakfire_key_import_secret_key(pakfire_key** key, pakfire_ctx* ctx, const char* comment, - const struct pakfire_key_private_key* buffer) { + const pakfire_key_private_key* buffer) { const pakfire_key_algo_t algo = PAKFIRE_KEY_ALGO_ED25519; EVP_PKEY* pkey = NULL; char error[ERROR_MAX]; @@ -346,9 +346,9 @@ ERROR: return r; } -static int pakfire_key_import_public_key(struct pakfire_key** key, +static int pakfire_key_import_public_key(pakfire_key** key, pakfire_ctx* ctx, const char* comment, - const struct pakfire_key_public_key* buffer) { + const pakfire_key_public_key* buffer) { const pakfire_key_algo_t algo = PAKFIRE_KEY_ALGO_ED25519; EVP_PKEY* pkey = NULL; char error[ERROR_MAX]; @@ -388,7 +388,7 @@ ERROR: return r; } -int pakfire_key_import(struct pakfire_key** key, +int pakfire_key_import(pakfire_key** key, pakfire_ctx* ctx, FILE* f) { unsigned char* buffer = NULL; size_t buffer_length = 0; @@ -439,17 +439,17 @@ int pakfire_key_import(struct pakfire_key** key, // What kind of key do we have? switch (buffer_length) { // Public Key - case sizeof(struct pakfire_key_public_key): + case sizeof(pakfire_key_public_key): r = pakfire_key_import_public_key(key, ctx, comment, - (struct pakfire_key_public_key*)buffer); + (pakfire_key_public_key*)buffer); if (r < 0) goto ERROR; break; // Private Key - case sizeof(struct pakfire_key_private_key): + case sizeof(pakfire_key_private_key): r = pakfire_key_import_secret_key(key, ctx, comment, - (struct pakfire_key_private_key*)buffer); + (pakfire_key_private_key*)buffer); if (r < 0) goto ERROR; break; @@ -482,7 +482,7 @@ ERROR: return r; } -int pakfire_key_import_from_string(struct pakfire_key** key, +int pakfire_key_import_from_string(pakfire_key** key, pakfire_ctx* ctx, const char* data, const size_t length) { FILE* f = NULL; int r; @@ -505,7 +505,7 @@ ERROR: return r; } -static int pakfire_key_get_public_key(struct pakfire_key* key, +static int pakfire_key_get_public_key(pakfire_key* key, unsigned char* buffer, const size_t length) { char error[ERROR_MAX]; int r; @@ -529,7 +529,7 @@ static int pakfire_key_get_public_key(struct pakfire_key* key, return 0; } -static int pakfire_key_get_secret_key(struct pakfire_key* key, +static int pakfire_key_get_secret_key(pakfire_key* key, unsigned char* buffer, const size_t length) { char error[ERROR_MAX]; int r; @@ -557,8 +557,8 @@ static int pakfire_key_get_secret_key(struct pakfire_key* key, Export */ -static int pakfire_key_export_private_key(struct pakfire_key* key, - struct pakfire_key_private_key* buffer) { +static int pakfire_key_export_private_key(pakfire_key* key, + pakfire_key_private_key* buffer) { unsigned char checksum[64]; unsigned int length = sizeof(checksum); int r; @@ -618,8 +618,8 @@ static int pakfire_key_export_private_key(struct pakfire_key* key, return 0; } -static int pakfire_key_export_public_key(struct pakfire_key* key, - struct pakfire_key_public_key* buffer) { +static int pakfire_key_export_public_key(pakfire_key* key, + pakfire_key_public_key* buffer) { int r; // Write the algorithm @@ -648,10 +648,10 @@ static int pakfire_key_export_public_key(struct pakfire_key* key, return 0; } -int pakfire_key_export(struct pakfire_key* key, FILE* f, +int pakfire_key_export(pakfire_key* key, FILE* f, const pakfire_key_export_mode_t mode) { - struct pakfire_key_public_key public_key = { 0 }; - struct pakfire_key_private_key private_key = { 0 }; + pakfire_key_public_key public_key = { 0 }; + pakfire_key_private_key private_key = { 0 }; int r; BIO* bio = NULL; @@ -738,7 +738,7 @@ ERROR: return r; } -int pakfire_key_export_string(struct pakfire_key* self, char** buffer, size_t* length) { +int pakfire_key_export_string(pakfire_key* self, char** buffer, size_t* length) { FILE* f = NULL; int r; @@ -768,8 +768,8 @@ ERROR: Sign */ -static int __pakfire_key_sign(struct pakfire_key* key, - struct pakfire_key_signature* signature, const void* data, const size_t length) { +static int __pakfire_key_sign(pakfire_key* key, + pakfire_key_signature* signature, const void* data, const size_t length) { EVP_MD_CTX* mdctx = NULL; char error[ERROR_MAX]; int r; @@ -822,9 +822,9 @@ ERROR: return r; } -int pakfire_key_sign_string(struct pakfire_key* key, +int pakfire_key_sign_string(pakfire_key* key, FILE* f, const void* data, const size_t length, const char* comment) { - struct pakfire_key_signature signature = { 0 }; + pakfire_key_signature signature = { 0 }; char* buffer = NULL; int r; @@ -869,7 +869,7 @@ ERROR: return r; } -int pakfire_key_sign(struct pakfire_key* key, FILE* s, FILE* f, const char* comment) { +int pakfire_key_sign(pakfire_key* key, FILE* s, FILE* f, const char* comment) { char* buffer = NULL; size_t length = 0; int r; @@ -889,8 +889,8 @@ ERROR: return r; } -static int pakfire_key_read_signature(struct pakfire_key* key, - struct pakfire_key_signature* signature, FILE* f) { +static int pakfire_key_read_signature(pakfire_key* key, + pakfire_key_signature* signature, FILE* f) { unsigned char* buffer = NULL; size_t buffer_length = 0; int r; @@ -957,8 +957,8 @@ ERROR: return r; } -static int pakfire_key_verify_signature(struct pakfire_key* key, - const struct pakfire_key_signature* signature, const void* data, const size_t length) { +static int pakfire_key_verify_signature(pakfire_key* key, + const pakfire_key_signature* signature, const void* data, const size_t length) { EVP_MD_CTX* mdctx = NULL; int r; @@ -1016,9 +1016,9 @@ ERROR: return r; } -int pakfire_key_verify(struct pakfire_key* key, FILE* f, +int pakfire_key_verify(pakfire_key* key, FILE* f, const void* data, const size_t length) { - struct pakfire_key_signature signature = { 0 }; + pakfire_key_signature signature = { 0 }; int r; // Read the signature diff --git a/src/pakfire/key.h b/src/pakfire/key.h index c69ecaf8..a5fc4e2f 100644 --- a/src/pakfire/key.h +++ b/src/pakfire/key.h @@ -23,7 +23,7 @@ #include -struct pakfire_key; +typedef struct pakfire_key pakfire_key; #include @@ -39,28 +39,28 @@ typedef enum pakfire_key_export_mode { typedef unsigned char pakfire_key_id[8]; -struct pakfire_key* pakfire_key_ref(struct pakfire_key* key); -struct pakfire_key* pakfire_key_unref(struct pakfire_key* key); +pakfire_key* pakfire_key_ref(pakfire_key* key); +pakfire_key* pakfire_key_unref(pakfire_key* key); // Access key properties -pakfire_key_id* pakfire_key_get_id(struct pakfire_key* key); -const char* pakfire_key_get_algo(struct pakfire_key* key); -const char* pakfire_key_get_comment(struct pakfire_key* key); +pakfire_key_id* pakfire_key_get_id(pakfire_key* key); +const char* pakfire_key_get_algo(pakfire_key* key); +const char* pakfire_key_get_comment(pakfire_key* key); -int pakfire_key_generate(struct pakfire_key** key, pakfire_ctx* ctx, +int pakfire_key_generate(pakfire_key** key, pakfire_ctx* ctx, const pakfire_key_algo_t algo, const char* comment); -int pakfire_key_export(struct pakfire_key* key, FILE* f, const pakfire_key_export_mode_t mode); -int pakfire_key_export_string(struct pakfire_key* self, char** buffer, size_t* length); -int pakfire_key_import(struct pakfire_key** key, pakfire_ctx* ctx, FILE* f); +int pakfire_key_export(pakfire_key* key, FILE* f, const pakfire_key_export_mode_t mode); +int pakfire_key_export_string(pakfire_key* self, char** buffer, size_t* length); +int pakfire_key_import(pakfire_key** key, pakfire_ctx* ctx, FILE* f); // Sign -int pakfire_key_sign(struct pakfire_key* key, FILE* s, FILE* f, const char* comment); -int pakfire_key_sign_string(struct pakfire_key* key, +int pakfire_key_sign(pakfire_key* key, FILE* s, FILE* f, const char* comment); +int pakfire_key_sign_string(pakfire_key* key, FILE* s, const void* data, const size_t length, const char* comment); -int pakfire_key_verify(struct pakfire_key* key, +int pakfire_key_verify(pakfire_key* key, FILE* f, const void* data, const size_t length); -int pakfire_key_import_from_string(struct pakfire_key** key, +int pakfire_key_import_from_string(pakfire_key** key, pakfire_ctx* ctx, const char* data, const size_t length); #endif /* PAKFIRE_KEY_H */ diff --git a/src/pakfire/repo.c b/src/pakfire/repo.c index e35f79c0..5bc79440 100644 --- a/src/pakfire/repo.c +++ b/src/pakfire/repo.c @@ -104,7 +104,7 @@ struct pakfire_repo_appdata { time_t refresh; // Key - struct pakfire_key* key; + pakfire_key* key; // Mirrorlist struct pakfire_mirrorlist* mirrorlist; @@ -131,7 +131,7 @@ struct pakfire_repo { Repo* repo; struct pakfire_repo_appdata* appdata; - struct pakfire_key* key; + pakfire_key* key; }; static const char* pakfire_repo_get_expanded_baseurl(struct pakfire_repo* repo); @@ -314,7 +314,7 @@ ERROR: } static int pakfire_repo_import_key(struct pakfire_repo* self, const char* data) { - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; int r; // Free any formerly imported keys (this should not happen) @@ -1711,7 +1711,7 @@ const char* pakfire_repo_get_path(struct pakfire_repo* repo) { return baseurl + strlen("file://"); } -struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* self) { +pakfire_key* pakfire_repo_get_key(struct pakfire_repo* self) { if (self->appdata->key) return pakfire_key_ref(self->appdata->key); @@ -1731,7 +1731,7 @@ int pakfire_repo_set_mirrorlist_url(struct pakfire_repo* repo, const char* url) int pakfire_repo_write_config(struct pakfire_repo* repo, FILE* f) { pakfire_config* config = NULL; - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; char* section = NULL; char* buffer = NULL; char refresh[1024]; @@ -2462,7 +2462,7 @@ static int pakfire_repo_make_metadata(struct pakfire_repo* self, struct json_obj return 0; } -int pakfire_repo_write_metadata(struct pakfire_repo* self, struct pakfire_key* key) { +int pakfire_repo_write_metadata(struct pakfire_repo* self, pakfire_key* key) { struct json_object* repomd = NULL; char repomd_path[PATH_MAX]; char sigpath[PATH_MAX]; @@ -2553,7 +2553,7 @@ ERROR: } int pakfire_repo_compose(struct pakfire* pakfire, const char* path, - struct pakfire_key* key, const char** files) { + pakfire_key* key, const char** files) { pakfire_archive* archive = NULL; struct pakfire_repo* repo = NULL; char realpath[PATH_MAX]; diff --git a/src/pakfire/repo.h b/src/pakfire/repo.h index 22d2426c..18a2d60c 100644 --- a/src/pakfire/repo.h +++ b/src/pakfire/repo.h @@ -64,7 +64,7 @@ void pakfire_repo_set_priority(struct pakfire_repo* repo, int priority); const char* pakfire_repo_get_baseurl(struct pakfire_repo* repo); int pakfire_repo_set_baseurl(struct pakfire_repo* repo, const char* baseurl); -struct pakfire_key* pakfire_repo_get_key(struct pakfire_repo* repo); +pakfire_key* pakfire_repo_get_key(struct pakfire_repo* repo); const char* pakfire_repo_get_mirrorlist_url(struct pakfire_repo* repo); int pakfire_repo_set_mirrorlist_url(struct pakfire_repo* repo, const char* url); @@ -103,10 +103,10 @@ int pakfire_repo_refresh(struct pakfire_repo* repo, int force); // Compose -int pakfire_repo_write_metadata(struct pakfire_repo* repo, struct pakfire_key* key); +int pakfire_repo_write_metadata(struct pakfire_repo* repo, pakfire_key* key); int pakfire_repo_compose(struct pakfire* pakfire, const char* path, - struct pakfire_key* key, const char** files); + pakfire_key* key, const char** files); #define PAKFIRE_REPO_COMMANDLINE "@commandline" #define PAKFIRE_REPO_DUMMY "@dummy" diff --git a/src/pakfire/transaction.c b/src/pakfire/transaction.c index cb21e6c5..17262e96 100644 --- a/src/pakfire/transaction.c +++ b/src/pakfire/transaction.c @@ -2082,7 +2082,7 @@ ERROR: } int pakfire_transaction_compose_repo(struct pakfire_transaction* transaction, - struct pakfire_key* key, const char* path) { + pakfire_key* key, const char* path) { pakfire_archive* archive = NULL; int r; diff --git a/src/pakfire/transaction.h b/src/pakfire/transaction.h index faf6b3e6..bd5a2196 100644 --- a/src/pakfire/transaction.h +++ b/src/pakfire/transaction.h @@ -86,6 +86,6 @@ int pakfire_transaction_download(struct pakfire_transaction* transaction); Solver* pakfire_transaction_get_solver(struct pakfire_transaction* transaction); int pakfire_transaction_compose_repo(struct pakfire_transaction* transaction, - struct pakfire_key* key, const char* path); + pakfire_key* key, const char* path); #endif /* PAKFIRE_TRANSACTION_H */ diff --git a/src/python/key.c b/src/python/key.c index fe772c4a..08ab8f6a 100644 --- a/src/python/key.c +++ b/src/python/key.c @@ -28,7 +28,7 @@ #include "pakfire.h" #include "util.h" -PyObject* new_key(PyTypeObject* type, struct pakfire_key* key) { +PyObject* new_key(PyTypeObject* type, pakfire_key* key) { KeyObject* self = (KeyObject *)type->tp_alloc(type, 0); if (self) { self->key = pakfire_key_ref(key); diff --git a/src/python/key.h b/src/python/key.h index fe637f30..a75b6169 100644 --- a/src/python/key.h +++ b/src/python/key.h @@ -27,11 +27,11 @@ typedef struct { PyObject_HEAD - struct pakfire_key* key; + pakfire_key* key; } KeyObject; extern PyTypeObject KeyType; -PyObject* new_key(PyTypeObject* type, struct pakfire_key* key); +PyObject* new_key(PyTypeObject* type, pakfire_key* key); #endif /* PYTHON_PAKFIRE_KEY_H */ diff --git a/src/python/pakfire.c b/src/python/pakfire.c index 58cc029b..ddd13521 100644 --- a/src/python/pakfire.c +++ b/src/python/pakfire.c @@ -173,7 +173,7 @@ static PyObject* Pakfire_get_repo(PakfireObject* self, PyObject* args) { */ static PyObject* Pakfire_generate_key(PakfireObject* self, PyObject* args, PyObject* kwds) { const char* kwlist[] = { "algorithm", "comment", NULL }; - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; pakfire_key_algo_t algo = PAKFIRE_KEY_ALGO_NULL; const char* comment = NULL; @@ -196,7 +196,7 @@ static PyObject* Pakfire_generate_key(PakfireObject* self, PyObject* args, PyObj XXX This could be moved out of here as this no longer depends on Pakfire */ static PyObject* Pakfire_import_key(PakfireObject* self, PyObject* args) { - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; PyObject* object = NULL; char* data = NULL; Py_ssize_t data_length = 0; diff --git a/src/python/repo.c b/src/python/repo.c index 41904ddd..f4825714 100644 --- a/src/python/repo.c +++ b/src/python/repo.c @@ -185,7 +185,7 @@ static int Repo_set_baseurl(RepoObject* self, PyObject* value) { } static PyObject* Repo_get_key(RepoObject* self) { - struct pakfire_key* key = pakfire_repo_get_key(self->repo); + pakfire_key* key = pakfire_repo_get_key(self->repo); if (key) { PyObject* ret = new_key(&KeyType, key); diff --git a/tests/libpakfire/key.c b/tests/libpakfire/key.c index 5ca1cb62..88cac34c 100644 --- a/tests/libpakfire/key.c +++ b/tests/libpakfire/key.c @@ -30,7 +30,7 @@ const char DATA[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const size_t DATA_LENGTH = sizeof(DATA); static int test_generate(const struct test* t) { - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; int r = EXIT_FAILURE; // Try to call pakfire_key_generate() with some invalid inputs @@ -57,7 +57,7 @@ FAIL: } static int test_sign_and_verify(const struct test* t) { - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; int r = EXIT_FAILURE; FILE* f = NULL; @@ -96,7 +96,7 @@ FAIL: } static int test_import_public(const struct test* t) { - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; FILE* f = NULL; int r = EXIT_FAILURE; @@ -122,7 +122,7 @@ FAIL: } static int test_import_secret(const struct test* t) { - struct pakfire_key* key = NULL; + pakfire_key* key = NULL; FILE* f = NULL; int r = EXIT_FAILURE;