From 638b69d33467cbb0cc806a116f8f0db7b1309bac Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 5 Feb 2025 10:52:09 +0000 Subject: [PATCH] keys: Make a simple function to export a key as a string Signed-off-by: Michael Tremer --- src/pakfire/key.c | 19 ++++++------------- src/pakfire/key.h | 3 +-- src/python/key.c | 28 +++++++++++++++------------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/pakfire/key.c b/src/pakfire/key.c index 384321c3..4f4fd0c3 100644 --- a/src/pakfire/key.c +++ b/src/pakfire/key.c @@ -739,23 +739,22 @@ ERROR: return r; } -char* pakfire_key_dump(struct pakfire_key* key, size_t* length) { - char* buffer = NULL; +int pakfire_key_export_string(struct pakfire_key* self, char** buffer, size_t* length) { FILE* f = NULL; int r; // Create a file handle to write to the buffer - f = open_memstream(&buffer, length); + f = open_memstream(buffer, length); if (!f) { - ERROR(key->ctx, "Could not open a new memstream: %m\n"); + ERROR(self->ctx, "Could not open a new memstream: %m\n"); r = -errno; goto ERROR; } // Export the public part of the key - r = pakfire_key_export(key, f, PAKFIRE_KEY_EXPORT_MODE_PUBLIC); + r = pakfire_key_export(self, f, PAKFIRE_KEY_EXPORT_MODE_PUBLIC); if (r < 0) { - ERROR(key->ctx, "Could not export key: %m\n"); + ERROR(self->ctx, "Could not export key: %m\n"); goto ERROR; } @@ -763,13 +762,7 @@ ERROR: if (f) fclose(f); - // Free the buffer on error - if (r && buffer) { - free(buffer); - buffer = NULL; - } - - return buffer; + return r; } /* diff --git a/src/pakfire/key.h b/src/pakfire/key.h index 959f155b..55a6cc79 100644 --- a/src/pakfire/key.h +++ b/src/pakfire/key.h @@ -50,10 +50,9 @@ const char* pakfire_key_get_comment(struct pakfire_key* key); int pakfire_key_generate(struct pakfire_key** key, struct 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, struct pakfire_ctx* ctx, FILE* f); -char* pakfire_key_dump(struct pakfire_key* key, size_t* length); - // 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, diff --git a/src/python/key.c b/src/python/key.c index 24bafd1f..fe772c4a 100644 --- a/src/python/key.c +++ b/src/python/key.c @@ -71,19 +71,21 @@ static PyObject* Key_repr(KeyObject* self) { static PyObject* Key_str(KeyObject* self) { PyObject* ret = NULL; - char* string = NULL; + char* buffer = NULL; size_t length = 0; + int r; // Dump the key - string = pakfire_key_dump(self->key, &length); - if (!string) { + r = pakfire_key_export_string(self->key, &buffer, &length); + if (r < 0) { + errno = -errno; PyErr_SetFromErrno(PyExc_OSError); return NULL; } // Parse the string - ret = PyUnicode_FromStringAndSize(string, length); - free(string); + ret = PyUnicode_FromStringAndSize(buffer, length); + free(buffer); return ret; } @@ -119,16 +121,15 @@ static PyObject* Key_get_comment(KeyObject* self) { static PyObject* Key_export(KeyObject* self, PyObject* args) { pakfire_key_export_mode_t mode = PAKFIRE_KEY_EXPORT_MODE_PUBLIC; PyObject* object = NULL; + char* buffer = NULL; + size_t length = 0; int secret = 0; if (!PyArg_ParseTuple(args, "|p", &secret)) return NULL; - char* data = NULL; - size_t data_length = 0; - // Create buffer to write the key to - FILE* f = open_memstream(&data, &data_length); + FILE* f = open_memstream(&buffer, &length); if (!f) { PyErr_SetFromErrno(PyExc_OSError); goto ERROR; @@ -139,18 +140,19 @@ static PyObject* Key_export(KeyObject* self, PyObject* args) { // Export the key int r = pakfire_key_export(self->key, f, mode); - if (r) { + if (r < 0) { + errno = -r; PyErr_SetFromErrno(PyExc_OSError); goto ERROR; } - object = PyUnicode_FromStringAndSize(data, data_length); + object = PyUnicode_FromStringAndSize(buffer, length); ERROR: if (f) fclose(f); - if (data) - free(data); + if (buffer) + free(buffer); return object; } -- 2.39.5