]> git.ipfire.org Git - pakfire.git/commitdiff
keys: Make a simple function to export a key as a string
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 5 Feb 2025 10:52:09 +0000 (10:52 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 5 Feb 2025 10:52:09 +0000 (10:52 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/key.c
src/pakfire/key.h
src/python/key.c

index 384321c33d55fffad3b9022abb64eac771ee4a60..4f4fd0c39078e4ae7009049c65db06cff82efb0d 100644 (file)
@@ -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;
 }
 
 /*
index 959f155b84cf232a10dccdbbb18043c1cfaf5a96..55a6cc7941bb1ef562ae0e01b707c831d8db01d9 100644 (file)
@@ -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,
index 24bafd1f9b7723dd00cf4ca9dad3600bda76cbdc..fe772c4a15638c67f743639c6d4a8bbe340ad2b0 100644 (file)
@@ -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;
 }