Skip to content

Commit

Permalink
sign: Factor out logic to read key blobs
Browse files Browse the repository at this point in the history
This defines a new interface OstreeBlobReader, which encapsulates the
key file parsing logic. This would make it easy to support custom file
formats such as PEM.

Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Aug 13, 2024
1 parent 4bc9f37 commit c57874a
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 35 deletions.
1 change: 1 addition & 0 deletions Makefile-libostree-defines.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ libostree_public_headers = \
src/libostree/ostree-kernel-args.h \
src/libostree/ostree-sign.h \
src/libostree/ostree-sign-ed25519.h \
src/libostree/ostree-blob-reader.h \
$(NULL)

# This one is generated via configure.ac, and the gtk-doc
Expand Down
12 changes: 9 additions & 3 deletions Makefile-libostree.am
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ endif # USE_GPGME
symbol_files = $(top_srcdir)/src/libostree/libostree-released.sym

# Uncomment this include when adding new development symbols.
#if BUILDOPT_IS_DEVEL_BUILD
#symbol_files += $(top_srcdir)/src/libostree/libostree-devel.sym
#endif
if BUILDOPT_IS_DEVEL_BUILD
symbol_files += $(top_srcdir)/src/libostree/libostree-devel.sym
endif

# http://blog.jgc.org/2007/06/escaping-comma-and-space-in-gnu-make.html
wl_versionscript_arg = -Wl,--version-script=
Expand Down Expand Up @@ -262,6 +262,12 @@ libostree_1_la_SOURCES += \
src/libostree/ostree-sign-ed25519.c \
src/libostree/ostree-sign-ed25519.h \
src/libostree/ostree-sign-private.h \
src/libostree/ostree-blob-reader.c \
src/libostree/ostree-blob-reader.h \
src/libostree/ostree-blob-reader-base64.c \
src/libostree/ostree-blob-reader-base64.h \
src/libostree/ostree-blob-reader-raw.c \
src/libostree/ostree-blob-reader-raw.h \
$(NULL)

if USE_COMPOSEFS
Expand Down
9 changes: 9 additions & 0 deletions apidoc/ostree-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,15 @@ ostree_sign_metadata_key
ostree_sign_set_pk
ostree_sign_set_sk
ostree_sign_summary
ostree_sign_read_pk
ostree_sign_read_sk
<SUBSECTION Standard>
ostree_sign_get_type
</SECTION>

<SECTION>
<FILE>ostree-blob-reader</FILE>
ostree_blob_reader_read_blob
<SUBSECTION Standard>
ostree_blob_reader_get_type
</SECTION>
8 changes: 8 additions & 0 deletions src/libostree/libostree-devel.sym
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
- uncomment the include in Makefile-libostree.am
*/

LIBOSTREE_2024.8 {
global:
ostree_sign_read_pk;
ostree_sign_read_sk;
ostree_blob_reader_get_type;
ostree_blob_reader_read_blob;
} LIBOSTREE_2024.7;

/* Stub section for the stable release *after* this development one; don't
* edit this other than to update the year. This is just a copy/paste
* source. Replace $LASTSTABLE with the last stable version, and $NEWVERSION
Expand Down
80 changes: 80 additions & 0 deletions src/libostree/ostree-blob-reader-base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2024 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/

#include "config.h"

#include "ostree-blob-reader-base64.h"

struct _OstreeBlobReaderBase64
{
GDataInputStream parent_instance;
};

static void ostree_blob_reader_base64_iface_init (OstreeBlobReaderInterface *iface);

G_DEFINE_TYPE_WITH_CODE (OstreeBlobReaderBase64, _ostree_blob_reader_base64,
G_TYPE_DATA_INPUT_STREAM,
G_IMPLEMENT_INTERFACE (OSTREE_TYPE_BLOB_READER,
ostree_blob_reader_base64_iface_init));

static void
ostree_blob_reader_base64_iface_init (OstreeBlobReaderInterface *iface)
{
iface->read_blob = ostree_blob_reader_base64_read_blob;
}

static void
_ostree_blob_reader_base64_class_init (OstreeBlobReaderBase64Class *klass)
{
}

static void
_ostree_blob_reader_base64_init (OstreeBlobReaderBase64 *self)
{
}

OstreeBlobReaderBase64 *
_ostree_blob_reader_base64_new (GInputStream *stream)
{
return g_object_new (OSTREE_TYPE_BLOB_READER_BASE64, "base-stream", stream, NULL);
}

GBytes *
ostree_blob_reader_base64_read_blob (OstreeBlobReader *self, GCancellable *cancellable,
GError **error)
{
gsize len = 0;
g_autoptr (GError) local_error = NULL;
g_autofree char *line
= g_data_input_stream_read_line (G_DATA_INPUT_STREAM (self), &len, cancellable, &local_error);
if (local_error != NULL)
{
g_propagate_error (error, g_steal_pointer (&local_error));
return NULL;
}

if (line == NULL)
return NULL;

gsize n_elements;
g_base64_decode_inplace (line, &n_elements);
explicit_bzero (line + n_elements, len - n_elements);

return g_bytes_new_take (g_steal_pointer (&line), n_elements);
}
39 changes: 39 additions & 0 deletions src/libostree/ostree-blob-reader-base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2024 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include "ostree-blob-reader.h"

G_BEGIN_DECLS

#define OSTREE_TYPE_BLOB_READER_BASE64 (_ostree_blob_reader_base64_get_type ())

_OSTREE_PUBLIC
G_DECLARE_FINAL_TYPE (OstreeBlobReaderBase64, _ostree_blob_reader_base64, OSTREE,
BLOB_READER_BASE64, GDataInputStream);

_OSTREE_PUBLIC
OstreeBlobReaderBase64 *_ostree_blob_reader_base64_new (GInputStream *stream);

_OSTREE_PUBLIC
GBytes *ostree_blob_reader_base64_read_blob (OstreeBlobReader *self, GCancellable *cancellable,
GError **error);

G_END_DECLS
74 changes: 74 additions & 0 deletions src/libostree/ostree-blob-reader-raw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2024 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/

#include "config.h"

#include "ostree-blob-reader-raw.h"

struct _OstreeBlobReaderRaw
{
GDataInputStream parent_instance;
};

static void ostree_blob_reader_raw_iface_init (OstreeBlobReaderInterface *iface);

G_DEFINE_TYPE_WITH_CODE (OstreeBlobReaderRaw, _ostree_blob_reader_raw, G_TYPE_DATA_INPUT_STREAM,
G_IMPLEMENT_INTERFACE (OSTREE_TYPE_BLOB_READER,
ostree_blob_reader_raw_iface_init));

static void
ostree_blob_reader_raw_iface_init (OstreeBlobReaderInterface *iface)
{
iface->read_blob = ostree_blob_reader_raw_read_blob;
}

static void
_ostree_blob_reader_raw_class_init (OstreeBlobReaderRawClass *klass)
{
}

static void
_ostree_blob_reader_raw_init (OstreeBlobReaderRaw *self)
{
}

OstreeBlobReaderRaw *
_ostree_blob_reader_raw_new (GInputStream *stream)
{
return g_object_new (OSTREE_TYPE_BLOB_READER_RAW, "base-stream", stream, NULL);
}

GBytes *
ostree_blob_reader_raw_read_blob (OstreeBlobReader *self, GCancellable *cancellable, GError **error)
{
gsize len = 0;
g_autoptr (GError) local_error = NULL;
g_autofree char *line
= g_data_input_stream_read_line (G_DATA_INPUT_STREAM (self), &len, cancellable, &local_error);
if (local_error != NULL)
{
g_propagate_error (error, g_steal_pointer (&local_error));
return NULL;
}

if (line == NULL)
return NULL;

return g_bytes_new_take (g_steal_pointer (&line), len);
}
39 changes: 39 additions & 0 deletions src/libostree/ostree-blob-reader-raw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2024 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include "ostree-blob-reader.h"

G_BEGIN_DECLS

#define OSTREE_TYPE_BLOB_READER_RAW (_ostree_blob_reader_raw_get_type ())

_OSTREE_PUBLIC
G_DECLARE_FINAL_TYPE (OstreeBlobReaderRaw, _ostree_blob_reader_raw, OSTREE, BLOB_READER_RAW,
GDataInputStream);

_OSTREE_PUBLIC
OstreeBlobReaderRaw *_ostree_blob_reader_raw_new (GInputStream *stream);

_OSTREE_PUBLIC
GBytes *ostree_blob_reader_raw_read_blob (OstreeBlobReader *self, GCancellable *cancellable,
GError **error);

G_END_DECLS
37 changes: 37 additions & 0 deletions src/libostree/ostree-blob-reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2024 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/

#include "config.h"

#include "ostree-blob-reader.h"

G_DEFINE_INTERFACE (OstreeBlobReader, ostree_blob_reader, G_TYPE_OBJECT);

static void
ostree_blob_reader_default_init (OstreeBlobReaderInterface *iface)
{
g_debug ("OstreeBlobReader initialization");
}

GBytes *
ostree_blob_reader_read_blob (OstreeBlobReader *self, GCancellable *cancellable, GError **error)
{
g_assert (OSTREE_IS_BLOB_READER (self));
return OSTREE_BLOB_READER_GET_IFACE (self)->read_blob (self, cancellable, error);
}
42 changes: 42 additions & 0 deletions src/libostree/ostree-blob-reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2024 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include "ostree-types.h"
#include <gio/gio.h>

G_BEGIN_DECLS

#define OSTREE_TYPE_BLOB_READER (ostree_blob_reader_get_type ())
_OSTREE_PUBLIC
G_DECLARE_INTERFACE (OstreeBlobReader, ostree_blob_reader, OSTREE, BLOB_READER, GObject)

struct _OstreeBlobReaderInterface
{
GTypeInterface g_iface;

GBytes *(*read_blob) (OstreeBlobReader *self, GCancellable *cancellable, GError **error);
};

_OSTREE_PUBLIC
GBytes *ostree_blob_reader_read_blob (OstreeBlobReader *self, GCancellable *cancellable,
GError **error);

G_END_DECLS
Loading

0 comments on commit c57874a

Please sign in to comment.