Merge pull request #1878 from d4s/wip/d4s/no_gpg
Alternative signing system
This commit is contained in:
42 files changed
+3717
-187
No files matched your search
@@ -21,6 +21,21 @@
|
||||
LIBOSTREE_2020.2 {
|
||||
global:
|
||||
ostree_repo_commit_modifier_set_sepolicy_from_commit;
|
||||
someostree_symbol_deleteme;
|
||||
ostree_sign_get_type;
|
||||
ostree_sign_list_names;
|
||||
ostree_sign_commit;
|
||||
ostree_sign_commit_verify;
|
||||
ostree_sign_data;
|
||||
ostree_sign_data_verify;
|
||||
ostree_sign_get_by_name;
|
||||
ostree_sign_get_name;
|
||||
ostree_sign_clear_keys;
|
||||
ostree_sign_load_pk;
|
||||
ostree_sign_set_pk;
|
||||
ostree_sign_add_pk;
|
||||
ostree_sign_set_sk;
|
||||
ostree_sign_summary;
|
||||
} LIBOSTREE_2020.1;
|
||||
|
||||
/* Stub section for the stable release *after* this development one; don't
|
||||
|
||||
@@ -73,6 +73,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeRepoFinderOverride, g_object_unref)
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeRepoFinderResult, ostree_repo_finder_result_free)
|
||||
G_DEFINE_AUTO_CLEANUP_FREE_FUNC (OstreeRepoFinderResultv, ostree_repo_finder_result_freev, NULL)
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeSign, g_object_unref)
|
||||
#endif
|
||||
|
||||
G_END_DECLS
|
||||
@@ -54,6 +54,8 @@
|
||||
#include <systemd/sd-journal.h>
|
||||
#endif
|
||||
|
||||
#include "ostree-sign.h"
|
||||
|
||||
#define OSTREE_MESSAGE_FETCH_COMPLETE_ID SD_ID128_MAKE(75,ba,3d,eb,0a,f0,41,a9,a4,62,72,ff,85,d9,e7,3e)
|
||||
|
||||
#define OSTREE_REPO_PULL_CONTENT_PRIORITY (OSTREE_FETCHER_DEFAULT_PRIORITY)
|
||||
@@ -105,6 +107,8 @@ typedef struct {
|
||||
|
||||
gboolean gpg_verify;
|
||||
gboolean gpg_verify_summary;
|
||||
gboolean sign_verify;
|
||||
gboolean sign_verify_summary;
|
||||
gboolean require_static_deltas;
|
||||
gboolean disable_static_deltas;
|
||||
gboolean has_tombstone_commits;
|
||||
@@ -1466,6 +1470,163 @@ process_verify_result (OtPullData *pull_data,
|
||||
}
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
|
||||
/* _load_public_keys:
|
||||
*
|
||||
* Load public keys according remote's configuration:
|
||||
* inlined key passed via config option `verification-key` or
|
||||
* file name with public keys via `verification-file` option.
|
||||
*
|
||||
* If both options are set then load all all public keys
|
||||
* both from file and inlined in config.
|
||||
*
|
||||
* Returns: %FALSE if any source is configured but nothing has been loaded.
|
||||
* Returns: %TRUE if no configuration or any key loaded.
|
||||
* */
|
||||
static gboolean
|
||||
_load_public_keys (OstreeSign *sign,
|
||||
OstreeRepo *repo,
|
||||
const gchar *remote_name,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
g_autofree gchar *pk_ascii = NULL;
|
||||
g_autofree gchar *pk_file = NULL;
|
||||
gboolean loaded_from_file = TRUE;
|
||||
gboolean loaded_inlined = TRUE;
|
||||
g_autoptr (GError) verification_error = NULL;
|
||||
|
||||
glnx_throw (&verification_error, "no public keys loaded");
|
||||
|
||||
ostree_repo_get_remote_option (repo,
|
||||
remote_name,
|
||||
"verification-file", NULL,
|
||||
&pk_file, NULL);
|
||||
|
||||
ostree_repo_get_remote_option (repo,
|
||||
remote_name,
|
||||
"verification-key", NULL,
|
||||
&pk_ascii, NULL);
|
||||
|
||||
/* return TRUE if there is no configuration for remote */
|
||||
if ((pk_file == NULL) &&(pk_ascii == NULL))
|
||||
{
|
||||
/* It is expected what remote may have verification file as
|
||||
* a part of configuration. Hence there is not a lot of sense
|
||||
* for automatic resolve of per-remote keystore file as it
|
||||
* used in find_keyring () for GPG.
|
||||
* If it is needed to add the similar mechanism, it is preferable
|
||||
* to pass the path to ostree_sign_load_pk () via GVariant options
|
||||
* and call it here for loading with method and file structure
|
||||
* specific for signature type.
|
||||
*/
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (pk_file != NULL)
|
||||
{
|
||||
g_autoptr (GError) local_error = NULL;
|
||||
g_autoptr (GVariantBuilder) builder = NULL;
|
||||
g_autoptr (GVariant) options = NULL;
|
||||
|
||||
builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
|
||||
g_variant_builder_add (builder, "{sv}", "filename", g_variant_new_string (pk_file));
|
||||
options = g_variant_builder_end (builder);
|
||||
|
||||
if (ostree_sign_load_pk (sign, options, &local_error))
|
||||
loaded_from_file = TRUE;
|
||||
else
|
||||
{
|
||||
g_debug("Unable to load public keys for '%s' from file '%s': %s",
|
||||
ostree_sign_get_name(sign), pk_file, local_error->message);
|
||||
/* Save error message for better reason detection later if needed */
|
||||
glnx_prefix_error (&verification_error, "%s", local_error->message);
|
||||
}
|
||||
}
|
||||
|
||||
if (pk_ascii != NULL)
|
||||
{
|
||||
g_autoptr (GError) local_error = NULL;
|
||||
g_autoptr (GVariant) pk = g_variant_new_string(pk_ascii);
|
||||
|
||||
/* Add inlined public key */
|
||||
if (loaded_from_file)
|
||||
loaded_inlined = ostree_sign_add_pk (sign, pk, &local_error);
|
||||
else
|
||||
loaded_inlined = ostree_sign_set_pk (sign, pk, &local_error);
|
||||
|
||||
if (!loaded_inlined)
|
||||
{
|
||||
g_debug("Unable to load public key '%s' for '%s': %s",
|
||||
pk_ascii, ostree_sign_get_name (sign), local_error->message);
|
||||
|
||||
/* Save error message for better reason detection later if needed */
|
||||
glnx_prefix_error (&verification_error, "%s", local_error->message);
|
||||
}
|
||||
}
|
||||
|
||||
/* Return true if able to load from any source */
|
||||
if (loaded_from_file || loaded_inlined)
|
||||
return TRUE;
|
||||
|
||||
return glnx_throw (error, "%s", verification_error->message);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_ostree_repo_sign_verify (OstreeRepo *repo,
|
||||
const gchar *remote_name,
|
||||
GBytes *signed_data,
|
||||
GVariant *metadata,
|
||||
GError **error)
|
||||
{
|
||||
/* list all signature types in detached metadata and check if signed by any? */
|
||||
g_auto (GStrv) names = ostree_sign_list_names();
|
||||
g_autoptr (GError) verification_error = NULL;
|
||||
|
||||
glnx_throw (&verification_error, "signed with unknown key");
|
||||
|
||||
for (char **iter=names; iter && *iter; iter++)
|
||||
{
|
||||
g_autoptr (OstreeSign) sign = NULL;
|
||||
g_autoptr (GVariant) signatures = NULL;
|
||||
const gchar *signature_key = NULL;
|
||||
GVariantType *signature_format = NULL;
|
||||
g_autoptr (GError) local_error = NULL;
|
||||
|
||||
if ((sign = ostree_sign_get_by_name (*iter, &local_error)) == NULL)
|
||||
continue;
|
||||
|
||||
signature_key = ostree_sign_metadata_key (sign);
|
||||
signature_format = (GVariantType *) ostree_sign_metadata_format (sign);
|
||||
|
||||
signatures = g_variant_lookup_value (metadata,
|
||||
signature_key,
|
||||
signature_format);
|
||||
|
||||
/* If not found signatures for requested signature subsystem */
|
||||
if (!signatures)
|
||||
continue;
|
||||
|
||||
/* Try to load public key(s) according remote's configuration */
|
||||
if (_load_public_keys (sign, repo, remote_name, &local_error))
|
||||
{
|
||||
/* Return true if any signature fit to pre-loaded public keys.
|
||||
* If no keys configured -- then system configuration will be used */
|
||||
if (ostree_sign_data_verify (sign,
|
||||
signed_data,
|
||||
signatures,
|
||||
&local_error))
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Save error message for better reason detection later if needed */
|
||||
glnx_prefix_error (&verification_error, "%s", local_error->message);
|
||||
}
|
||||
|
||||
/* In case if there were no signatures of known type
|
||||
* or metadata contains invalid data */
|
||||
return glnx_throw (error, "%s", verification_error->message);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
ostree_verify_unwritten_commit (OtPullData *pull_data,
|
||||
const char *checksum,
|
||||
@@ -1475,21 +1636,24 @@ ostree_verify_unwritten_commit (OtPullData *pull_data,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
if (pull_data->gpg_verify || pull_data->sign_verify)
|
||||
/* Shouldn't happen, but see comment in process_verify_result() */
|
||||
if (g_hash_table_contains (pull_data->verified_commits, checksum))
|
||||
return TRUE;
|
||||
|
||||
g_autoptr(GBytes) signed_data = g_variant_get_data_as_bytes (commit);
|
||||
|
||||
#ifndef OSTREE_DISABLE_GPGME
|
||||
if (pull_data->gpg_verify)
|
||||
{
|
||||
const char *keyring_remote = NULL;
|
||||
|
||||
/* Shouldn't happen, but see comment in process_verify_result() */
|
||||
if (g_hash_table_contains (pull_data->verified_commits, checksum))
|
||||
return TRUE;
|
||||
|
||||
if (ref != NULL)
|
||||
keyring_remote = g_hash_table_lookup (pull_data->ref_keyring_map, ref);
|
||||
if (keyring_remote == NULL)
|
||||
keyring_remote = pull_data->remote_name;
|
||||
|
||||
g_autoptr(GBytes) signed_data = g_variant_get_data_as_bytes (commit);
|
||||
g_autoptr(OstreeGpgVerifyResult) result =
|
||||
_ostree_repo_gpg_verify_with_metadata (pull_data->repo, signed_data,
|
||||
detached_metadata,
|
||||
@@ -1500,6 +1664,20 @@ ostree_verify_unwritten_commit (OtPullData *pull_data,
|
||||
}
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
|
||||
if (pull_data->sign_verify)
|
||||
{
|
||||
/* Nothing to check if detached metadata is absent */
|
||||
if (detached_metadata == NULL)
|
||||
return glnx_throw (error, "Can't verify commit without detached metadata");
|
||||
|
||||
if (!_ostree_repo_sign_verify (pull_data->repo, pull_data->remote_name, signed_data, detached_metadata, error))
|
||||
return glnx_prefix_error (error, "Can't verify commit");
|
||||
|
||||
/* Mark the commit as verified to avoid double verification
|
||||
* see process_verify_result () for rationale */
|
||||
g_hash_table_add (pull_data->verified_commits, g_strdup (checksum));
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -1829,6 +2007,44 @@ scan_commit_object (OtPullData *pull_data,
|
||||
}
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
|
||||
if (pull_data->sign_verify &&
|
||||
!g_hash_table_contains (pull_data->verified_commits, checksum))
|
||||
{
|
||||
gboolean ret = FALSE;
|
||||
g_autoptr (GError) verification_error = NULL;
|
||||
|
||||
/* list all signature types in detached metadata and check if signed by any? */
|
||||
g_auto (GStrv) names = ostree_sign_list_names();
|
||||
for (char **iter=names; !ret && iter && *iter; iter++)
|
||||
{
|
||||
g_autoptr (OstreeSign) sign = NULL;
|
||||
g_autoptr (GError) local_error = NULL;
|
||||
|
||||
if ((sign = ostree_sign_get_by_name (*iter, &local_error)) == NULL)
|
||||
continue;
|
||||
|
||||
/* Try to load public key(s) according remote's configuration */
|
||||
if (_load_public_keys (sign, pull_data->repo, pull_data->remote_name, &local_error))
|
||||
{
|
||||
|
||||
/* Set return to true if any sign fit */
|
||||
if (ostree_sign_commit_verify (sign,
|
||||
pull_data->repo,
|
||||
checksum,
|
||||
cancellable,
|
||||
&local_error))
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
/* Save error message for better reason detection later if needed */
|
||||
if (!ret)
|
||||
glnx_prefix_error (&verification_error, "%s", local_error->message);
|
||||
}
|
||||
|
||||
if (!ret)
|
||||
return glnx_throw (error, "Can't verify commit %s: %s", checksum, verification_error->message);
|
||||
}
|
||||
|
||||
/* If we found a legacy transaction flag, assume we have to scan.
|
||||
* We always do a scan of dirtree objects; see
|
||||
* https://github.com/ostreedev/ostree/issues/543
|
||||
@@ -3576,6 +3792,8 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
g_autoptr(GSource) update_timeout = NULL;
|
||||
gboolean opt_gpg_verify_set = FALSE;
|
||||
gboolean opt_gpg_verify_summary_set = FALSE;
|
||||
gboolean opt_sign_verify_set = FALSE;
|
||||
gboolean opt_sign_verify_summary_set = FALSE;
|
||||
gboolean opt_collection_refs_set = FALSE;
|
||||
gboolean opt_n_network_retries_set = FALSE;
|
||||
gboolean opt_ref_keyring_map_set = FALSE;
|
||||
@@ -3610,6 +3828,10 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
g_variant_lookup (options, "gpg-verify", "b", &pull_data->gpg_verify);
|
||||
opt_gpg_verify_summary_set =
|
||||
g_variant_lookup (options, "gpg-verify-summary", "b", &pull_data->gpg_verify_summary);
|
||||
opt_sign_verify_set =
|
||||
g_variant_lookup (options, "sign-verify", "b", &pull_data->sign_verify);
|
||||
opt_sign_verify_summary_set =
|
||||
g_variant_lookup (options, "sign-verify-summary", "b", &pull_data->sign_verify_summary);
|
||||
(void) g_variant_lookup (options, "depth", "i", &pull_data->maxdepth);
|
||||
(void) g_variant_lookup (options, "disable-static-deltas", "b", &pull_data->disable_static_deltas);
|
||||
(void) g_variant_lookup (options, "require-static-deltas", "b", &pull_data->require_static_deltas);
|
||||
@@ -3759,7 +3981,10 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
/* For compatibility with pull-local, don't gpg verify local
|
||||
* pulls by default.
|
||||
*/
|
||||
if ((pull_data->gpg_verify || pull_data->gpg_verify_summary) &&
|
||||
if ((pull_data->gpg_verify ||
|
||||
pull_data->gpg_verify_summary ||
|
||||
pull_data->sign_verify
|
||||
) &&
|
||||
pull_data->remote_name == NULL)
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
||||
@@ -3774,7 +3999,6 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
g_free (pull_data->remote_name);
|
||||
pull_data->remote_name = g_strdup (remote_name_or_baseurl);
|
||||
|
||||
#ifndef OSTREE_DISABLE_GPGME
|
||||
/* Fetch GPG verification settings from remote if it wasn't already
|
||||
* explicitly set in the options. */
|
||||
if (!opt_gpg_verify_set)
|
||||
@@ -3786,7 +4010,18 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
if (!ostree_repo_remote_get_gpg_verify_summary (self, pull_data->remote_name,
|
||||
&pull_data->gpg_verify_summary, error))
|
||||
goto out;
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
/* Fetch verification settings from remote if it wasn't already
|
||||
* explicitly set in the options. */
|
||||
if (!opt_sign_verify_set)
|
||||
if (!ostree_repo_get_remote_boolean_option (self, pull_data->remote_name,
|
||||
"sign-verify", FALSE,
|
||||
&pull_data->sign_verify, error))
|
||||
goto out;
|
||||
if (!opt_sign_verify_summary_set)
|
||||
if (!ostree_repo_get_remote_boolean_option (self, pull_data->remote_name,
|
||||
"sign-verify-summary", FALSE,
|
||||
&pull_data->sign_verify_summary, error))
|
||||
goto out;
|
||||
|
||||
/* NOTE: If changing this, see the matching implementation in
|
||||
* ostree-sysroot-upgrader.c
|
||||
@@ -4168,6 +4403,64 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
}
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
|
||||
if (pull_data->sign_verify_summary)
|
||||
{
|
||||
if (!bytes_sig && pull_data->sign_verify_summary)
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
||||
"Signatures verification enabled, but no summary.sig found (use sign-verify-summary=false in remote config to disable)");
|
||||
goto out;
|
||||
}
|
||||
if (bytes_summary && bytes_sig)
|
||||
{
|
||||
g_autoptr(GVariant) signatures = NULL;
|
||||
g_autoptr(GError) temp_error = NULL;
|
||||
|
||||
signatures = g_variant_new_from_bytes (OSTREE_SUMMARY_SIG_GVARIANT_FORMAT,
|
||||
bytes_sig, FALSE);
|
||||
|
||||
|
||||
if (!_ostree_repo_sign_verify (pull_data->repo, pull_data->remote_name, bytes_summary, signatures, &temp_error))
|
||||
{
|
||||
if (summary_from_cache)
|
||||
{
|
||||
/* The cached summary doesn't match, fetch a new one and verify again */
|
||||
if ((self->test_error_flags & OSTREE_REPO_TEST_ERROR_INVALID_CACHE) > 0)
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
||||
"Remote %s cached summary invalid and "
|
||||
"OSTREE_REPO_TEST_ERROR_INVALID_CACHE specified",
|
||||
pull_data->remote_name);
|
||||
goto out;
|
||||
}
|
||||
else
|
||||
g_debug ("Remote %s cached summary invalid, pulling new version",
|
||||
pull_data->remote_name);
|
||||
|
||||
summary_from_cache = FALSE;
|
||||
g_clear_pointer (&bytes_summary, (GDestroyNotify)g_bytes_unref);
|
||||
if (!_ostree_fetcher_mirrored_request_to_membuf (pull_data->fetcher,
|
||||
pull_data->meta_mirrorlist,
|
||||
"summary",
|
||||
OSTREE_FETCHER_REQUEST_OPTIONAL_CONTENT,
|
||||
pull_data->n_network_retries,
|
||||
&bytes_summary,
|
||||
OSTREE_MAX_METADATA_SIZE,
|
||||
cancellable, error))
|
||||
goto out;
|
||||
|
||||
if (!_ostree_repo_sign_verify (pull_data->repo, pull_data->remote_name, bytes_summary, signatures, error))
|
||||
goto out;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_propagate_error (error, g_steal_pointer (&temp_error));
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bytes_summary)
|
||||
{
|
||||
pull_data->summary_data = g_bytes_ref (bytes_summary);
|
||||
@@ -4648,22 +4941,26 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
g_string_append_printf (msg, "libostree pull from '%s' for %u refs complete",
|
||||
pull_data->remote_name, g_hash_table_size (requested_refs_to_fetch));
|
||||
|
||||
const char *verify_state;
|
||||
const char *gpg_verify_state;
|
||||
#ifndef OSTREE_DISABLE_GPGME
|
||||
if (pull_data->gpg_verify_summary)
|
||||
{
|
||||
if (pull_data->gpg_verify)
|
||||
verify_state = "summary+commit";
|
||||
gpg_verify_state = "summary+commit";
|
||||
else
|
||||
verify_state = "summary-only";
|
||||
gpg_verify_state = "summary-only";
|
||||
}
|
||||
else
|
||||
verify_state = (pull_data->gpg_verify ? "commit" : "disabled");
|
||||
g_string_append_printf (msg, "\nsecurity: GPG: %s ", verify_state);
|
||||
gpg_verify_state = (pull_data->gpg_verify ? "commit" : "disabled");
|
||||
|
||||
#else
|
||||
verify_state = "disabled";
|
||||
g_string_append_printf (msg, "\nsecurity: %s ", verify_state);
|
||||
gpg_verify_state = "disabled";
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
g_string_append_printf (msg, "\nsecurity: GPG: %s ", gpg_verify_state);
|
||||
|
||||
const char *sign_verify_state;
|
||||
sign_verify_state = (pull_data->sign_verify ? "commit" : "disabled");
|
||||
g_string_append_printf (msg, "\nsecurity: SIGN: %s ", sign_verify_state);
|
||||
|
||||
OstreeFetcherURI *first_uri = pull_data->meta_mirrorlist->pdata[0];
|
||||
g_autofree char *first_scheme = _ostree_fetcher_uri_get_scheme (first_uri);
|
||||
@@ -4699,7 +4996,8 @@ ostree_repo_pull_with_options (OstreeRepo *self,
|
||||
ot_journal_send ("MESSAGE=%s", msg->str,
|
||||
"MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(OSTREE_MESSAGE_FETCH_COMPLETE_ID),
|
||||
"OSTREE_REMOTE=%s", pull_data->remote_name,
|
||||
"OSTREE_GPG=%s", verify_state,
|
||||
"OSTREE_SIGN=%s", sign_verify_state,
|
||||
"OSTREE_GPG=%s", gpg_verify_state,
|
||||
"OSTREE_SECONDS=%u", n_seconds,
|
||||
"OSTREE_XFER_SIZE=%s", formatted_xferred,
|
||||
NULL);
|
||||
@@ -6024,6 +6322,8 @@ ostree_repo_pull_from_remotes_async (OstreeRepo *self,
|
||||
g_variant_dict_insert (&local_options_dict, "gpg-verify", "b", FALSE);
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
g_variant_dict_insert (&local_options_dict, "gpg-verify-summary", "b", FALSE);
|
||||
g_variant_dict_insert (&local_options_dict, "sign-verify", "b", FALSE);
|
||||
g_variant_dict_insert (&local_options_dict, "sign-verify-summary", "b", FALSE);
|
||||
g_variant_dict_insert (&local_options_dict, "inherit-transaction", "b", TRUE);
|
||||
if (result->remote->refspec_name != NULL)
|
||||
g_variant_dict_insert (&local_options_dict, "override-remote-name", "s", result->remote->refspec_name);
|
||||
@@ -6170,9 +6470,8 @@ ostree_repo_remote_fetch_summary_with_options (OstreeRepo *self,
|
||||
g_autofree char *metalink_url_string = NULL;
|
||||
g_autoptr(GBytes) summary = NULL;
|
||||
g_autoptr(GBytes) signatures = NULL;
|
||||
#ifndef OSTREE_DISABLE_GPGME
|
||||
gboolean gpg_verify_summary;
|
||||
#endif
|
||||
gboolean sign_verify_summary;
|
||||
gboolean ret = FALSE;
|
||||
gboolean summary_is_from_cache;
|
||||
|
||||
@@ -6194,37 +6493,72 @@ ostree_repo_remote_fetch_summary_with_options (OstreeRepo *self,
|
||||
error))
|
||||
goto out;
|
||||
|
||||
#ifndef OSTREE_DISABLE_GPGME
|
||||
if (!ostree_repo_remote_get_gpg_verify_summary (self, name, &gpg_verify_summary, error))
|
||||
goto out;
|
||||
|
||||
if (gpg_verify_summary && summary == NULL)
|
||||
if (gpg_verify_summary)
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
|
||||
"GPG verification enabled, but no summary found (check that the configured URL in remote config is correct)");
|
||||
goto out;
|
||||
if (summary == NULL)
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
|
||||
"GPG verification enabled, but no summary found (check that the configured URL in remote config is correct)");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (signatures == NULL)
|
||||
{
|
||||
g_set_error (error, OSTREE_GPG_ERROR, OSTREE_GPG_ERROR_NO_SIGNATURE,
|
||||
"GPG verification enabled, but no summary signatures found (use gpg-verify-summary=false in remote config to disable)");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Verify any summary signatures. */
|
||||
if (summary != NULL && signatures != NULL)
|
||||
{
|
||||
g_autoptr(OstreeGpgVerifyResult) result = NULL;
|
||||
|
||||
result = ostree_repo_verify_summary (self,
|
||||
name,
|
||||
summary,
|
||||
signatures,
|
||||
cancellable,
|
||||
error);
|
||||
if (!ostree_gpg_verify_result_require_valid_signature (result, error))
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
if (gpg_verify_summary && signatures == NULL)
|
||||
{
|
||||
g_set_error (error, OSTREE_GPG_ERROR, OSTREE_GPG_ERROR_NO_SIGNATURE,
|
||||
"GPG verification enabled, but no summary signatures found (use gpg-verify-summary=false in remote config to disable)");
|
||||
if (!ostree_repo_get_remote_boolean_option (self, name, "sign-verify-summary",
|
||||
FALSE, &sign_verify_summary, error))
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Verify any summary signatures. */
|
||||
if (gpg_verify_summary && summary != NULL && signatures != NULL)
|
||||
if (sign_verify_summary)
|
||||
{
|
||||
g_autoptr(OstreeGpgVerifyResult) result = NULL;
|
||||
if (summary == NULL)
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
|
||||
"Signature verification enabled, but no summary found (check that the configured URL in remote config is correct)");
|
||||
goto out;
|
||||
}
|
||||
|
||||
result = ostree_repo_verify_summary (self,
|
||||
name,
|
||||
summary,
|
||||
signatures,
|
||||
cancellable,
|
||||
error);
|
||||
if (!ostree_gpg_verify_result_require_valid_signature (result, error))
|
||||
goto out;
|
||||
if (signatures == NULL)
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
|
||||
"Signature verification enabled, but no summary signatures found (use sign-verify-summary=false in remote config to disable)");
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Verify any summary signatures. */
|
||||
if (summary != NULL && signatures != NULL)
|
||||
{
|
||||
g_autoptr(GVariant) sig_variant = NULL;
|
||||
|
||||
sig_variant = g_variant_new_from_bytes (OSTREE_SUMMARY_SIG_GVARIANT_FORMAT,
|
||||
signatures, FALSE);
|
||||
|
||||
if (!_ostree_repo_sign_verify (self, name, summary, sig_variant, error))
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
if (!summary_is_from_cache && summary && signatures)
|
||||
@@ -6248,10 +6582,6 @@ ostree_repo_remote_fetch_summary_with_options (OstreeRepo *self,
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
g_message ("%s: GPG feature is disabled in a build time", __FUNCTION__);
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
|
||||
if (out_summary != NULL)
|
||||
*out_summary = g_steal_pointer (&summary);
|
||||
|
||||
|
||||
@@ -2028,17 +2028,8 @@ ostree_repo_remote_get_gpg_verify (OstreeRepo *self,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#ifndef OSTREE_DISABLE_GPGME
|
||||
return ostree_repo_get_remote_boolean_option (self, name, "gpg-verify",
|
||||
TRUE, out_gpg_verify, error);
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
if (out_gpg_verify != NULL)
|
||||
*out_gpg_verify = FALSE;
|
||||
return FALSE;
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2060,17 +2051,8 @@ ostree_repo_remote_get_gpg_verify_summary (OstreeRepo *self,
|
||||
gboolean *out_gpg_verify_summary,
|
||||
GError **error)
|
||||
{
|
||||
#ifndef OSTREE_DISABLE_GPGME
|
||||
return ostree_repo_get_remote_boolean_option (self, name, "gpg-verify-summary",
|
||||
FALSE, out_gpg_verify_summary, error);
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
if (out_gpg_verify_summary != NULL)
|
||||
*out_gpg_verify_summary = FALSE;
|
||||
return FALSE;
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2344,10 +2326,7 @@ out:
|
||||
|
||||
return ret;
|
||||
#else /* OSTREE_DISABLE_GPGME */
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
return FALSE;
|
||||
return glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
@@ -4991,10 +4970,7 @@ ostree_repo_append_gpg_signature (OstreeRepo *self,
|
||||
|
||||
return TRUE;
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
return FALSE;
|
||||
return glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
@@ -5146,7 +5122,7 @@ ostree_repo_sign_commit (OstreeRepo *self,
|
||||
return TRUE;
|
||||
#else
|
||||
/* FIXME: Return false until refactoring */
|
||||
return FALSE;
|
||||
return glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
@@ -5238,10 +5214,7 @@ ostree_repo_add_gpg_signature_summary (OstreeRepo *self,
|
||||
|
||||
return TRUE;
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
return FALSE;
|
||||
return glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
@@ -5515,10 +5488,7 @@ ostree_repo_verify_commit (OstreeRepo *self,
|
||||
return TRUE;
|
||||
#else
|
||||
/* FIXME: Return false until refactoring */
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
return FALSE;
|
||||
return glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
@@ -5553,9 +5523,7 @@ ostree_repo_verify_commit_ext (OstreeRepo *self,
|
||||
cancellable,
|
||||
error);
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
return NULL;
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
@@ -5592,9 +5560,7 @@ ostree_repo_verify_commit_for_remote (OstreeRepo *self,
|
||||
cancellable,
|
||||
error);
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
return NULL;
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
@@ -5644,9 +5610,7 @@ ostree_repo_gpg_verify_data (OstreeRepo *self,
|
||||
cancellable,
|
||||
error);
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
return NULL;
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
@@ -5692,9 +5656,7 @@ ostree_repo_verify_summary (OstreeRepo *self,
|
||||
cancellable,
|
||||
error);
|
||||
#else
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
||||
"'%s': GPG feature is disabled in a build time",
|
||||
__FUNCTION__);
|
||||
glnx_throw (error, "GPG feature is disabled in a build time");
|
||||
return NULL;
|
||||
#endif /* OSTREE_DISABLE_GPGME */
|
||||
}
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */
|
||||
|
||||
/*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
*
|
||||
* 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, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <libglnx.h>
|
||||
#include "ostree-sign-dummy.h"
|
||||
#include <string.h>
|
||||
|
||||
#undef G_LOG_DOMAIN
|
||||
#define G_LOG_DOMAIN "OSTreeSign"
|
||||
|
||||
#define OSTREE_SIGN_DUMMY_NAME "dummy"
|
||||
|
||||
#define OSTREE_SIGN_METADATA_DUMMY_KEY "ostree.sign.dummy"
|
||||
#define OSTREE_SIGN_METADATA_DUMMY_TYPE "aay"
|
||||
|
||||
struct _OstreeSignDummy
|
||||
{
|
||||
GObject parent;
|
||||
gchar *sk_ascii;
|
||||
gchar *pk_ascii;
|
||||
};
|
||||
|
||||
#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeSignDummy, g_object_unref)
|
||||
#endif
|
||||
|
||||
static void
|
||||
ostree_sign_dummy_iface_init (OstreeSignInterface *self);
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE (OstreeSignDummy, _ostree_sign_dummy, G_TYPE_OBJECT,
|
||||
G_IMPLEMENT_INTERFACE (OSTREE_TYPE_SIGN, ostree_sign_dummy_iface_init));
|
||||
|
||||
static void
|
||||
ostree_sign_dummy_iface_init (OstreeSignInterface *self)
|
||||
{
|
||||
|
||||
self->get_name = ostree_sign_dummy_get_name;
|
||||
self->data = ostree_sign_dummy_data;
|
||||
self->data_verify = ostree_sign_dummy_data_verify;
|
||||
self->metadata_key = ostree_sign_dummy_metadata_key;
|
||||
self->metadata_format = ostree_sign_dummy_metadata_format;
|
||||
self->set_sk = ostree_sign_dummy_set_sk;
|
||||
self->set_pk = ostree_sign_dummy_set_pk;
|
||||
/* Implementation for dummy engine just load the single public key */
|
||||
self->add_pk = ostree_sign_dummy_set_pk;
|
||||
}
|
||||
|
||||
static void
|
||||
_ostree_sign_dummy_class_init (OstreeSignDummyClass *self)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
_ostree_sign_dummy_init (OstreeSignDummy *self)
|
||||
{
|
||||
|
||||
self->sk_ascii = NULL;
|
||||
self->pk_ascii = NULL;
|
||||
}
|
||||
|
||||
gboolean ostree_sign_dummy_set_sk (OstreeSign *self, GVariant *key, GError **error)
|
||||
{
|
||||
|
||||
OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private(OSTREE_SIGN_DUMMY(self));
|
||||
|
||||
g_free(sign->sk_ascii);
|
||||
|
||||
sign->sk_ascii = g_variant_dup_string (key, 0);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean ostree_sign_dummy_set_pk (OstreeSign *self, GVariant *key, GError **error)
|
||||
{
|
||||
|
||||
OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private(OSTREE_SIGN_DUMMY(self));
|
||||
|
||||
g_free(sign->pk_ascii);
|
||||
|
||||
sign->pk_ascii = g_variant_dup_string (key, 0);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean ostree_sign_dummy_data (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GBytes **signature,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private(OSTREE_SIGN_DUMMY(self));
|
||||
|
||||
*signature = g_bytes_new (sign->sk_ascii, strlen(sign->sk_ascii));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
const gchar * ostree_sign_dummy_get_name (OstreeSign *self)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
return OSTREE_SIGN_DUMMY_NAME;
|
||||
}
|
||||
|
||||
const gchar * ostree_sign_dummy_metadata_key (OstreeSign *self)
|
||||
{
|
||||
|
||||
return OSTREE_SIGN_METADATA_DUMMY_KEY;
|
||||
}
|
||||
|
||||
const gchar * ostree_sign_dummy_metadata_format (OstreeSign *self)
|
||||
{
|
||||
|
||||
return OSTREE_SIGN_METADATA_DUMMY_TYPE;
|
||||
}
|
||||
|
||||
gboolean ostree_sign_dummy_data_verify (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GVariant *signatures,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
g_return_val_if_fail (data != NULL, FALSE);
|
||||
|
||||
OstreeSignDummy *sign = _ostree_sign_dummy_get_instance_private(OSTREE_SIGN_DUMMY(self));
|
||||
|
||||
if (signatures == NULL)
|
||||
return glnx_throw (error, "signature: dummy: commit have no signatures of my type");
|
||||
|
||||
if (!g_variant_is_of_type (signatures, (GVariantType *) OSTREE_SIGN_METADATA_DUMMY_TYPE))
|
||||
return glnx_throw (error, "signature: dummy: wrong type passed for verification");
|
||||
|
||||
for (gsize i = 0; i < g_variant_n_children(signatures); i++)
|
||||
{
|
||||
g_autoptr (GVariant) child = g_variant_get_child_value (signatures, i);
|
||||
g_autoptr (GBytes) signature = g_variant_get_data_as_bytes(child);
|
||||
|
||||
gsize sign_size = 0;
|
||||
g_bytes_get_data (signature, &sign_size);
|
||||
g_autofree gchar *sign_ascii = g_strndup(g_bytes_get_data (signature, NULL), sign_size);
|
||||
g_debug("Read signature %d: %s", (gint)i, sign_ascii);
|
||||
g_debug("Stored signature %d: %s", (gint)i, sign->pk_ascii);
|
||||
|
||||
if (!g_strcmp0(sign_ascii, sign->pk_ascii))
|
||||
return TRUE;
|
||||
else
|
||||
return glnx_throw (error, "signature: dummy: incorrect signature %" G_GSIZE_FORMAT, i);
|
||||
}
|
||||
|
||||
return glnx_throw (error, "signature: dummy: no signatures");
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */
|
||||
|
||||
/*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
*
|
||||
* 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, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Authors:
|
||||
* - Denis Pynkin (d4s) <denis.pynkin@collabora.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ostree-sign.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define OSTREE_TYPE_SIGN_DUMMY (_ostree_sign_dummy_get_type ())
|
||||
|
||||
GType _ostree_sign_dummy_get_type (void);
|
||||
|
||||
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
||||
typedef struct _OstreeSignDummy OstreeSignDummy;
|
||||
typedef struct { GObjectClass parent_class; } OstreeSignDummyClass;
|
||||
|
||||
static inline OstreeSignDummy *OSTREE_SIGN_DUMMY (gpointer ptr) { return G_TYPE_CHECK_INSTANCE_CAST (ptr, _ostree_sign_dummy_get_type (), OstreeSignDummy); }
|
||||
static inline gboolean OSTREE_IS_SIGN_DUMMY (gpointer ptr) { return G_TYPE_CHECK_INSTANCE_TYPE (ptr, _ostree_sign_dummy_get_type ()); }
|
||||
|
||||
G_GNUC_END_IGNORE_DEPRECATIONS
|
||||
|
||||
/* Have to use glib-2.44 for this
|
||||
_OSTREE_PUBLIC
|
||||
G_DECLARE_FINAL_TYPE (OstreeSignDummy,
|
||||
ostree_sign_dummy,
|
||||
OSTREE,
|
||||
SIGN_DUMMY,
|
||||
GObject)
|
||||
*/
|
||||
|
||||
const gchar * ostree_sign_dummy_get_name (OstreeSign *self);
|
||||
|
||||
gboolean ostree_sign_dummy_data (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GBytes **signature,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
gboolean ostree_sign_dummy_data_verify (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GVariant *signatures,
|
||||
GError **error);
|
||||
|
||||
const gchar * ostree_sign_dummy_metadata_key (OstreeSign *self);
|
||||
const gchar * ostree_sign_dummy_metadata_format (OstreeSign *self);
|
||||
|
||||
gboolean ostree_sign_dummy_set_sk (OstreeSign *self, GVariant *key, GError **error);
|
||||
gboolean ostree_sign_dummy_set_pk (OstreeSign *self, GVariant *key, GError **error);
|
||||
gboolean ostree_sign_dummy_add_pk (OstreeSign *self, GVariant *key, GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
@@ -0,0 +1,647 @@
|
||||
/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */
|
||||
/*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
*
|
||||
* 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, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Authors:
|
||||
* - Denis Pynkin (d4s) <denis.pynkin@collabora.com>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <libglnx.h>
|
||||
#include "ostree-sign-ed25519.h"
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
#include <sodium.h>
|
||||
#endif
|
||||
|
||||
#undef G_LOG_DOMAIN
|
||||
#define G_LOG_DOMAIN "OSTreeSign"
|
||||
|
||||
#define OSTREE_SIGN_ED25519_NAME "ed25519"
|
||||
|
||||
#define OSTREE_SIGN_METADATA_ED25519_KEY "ostree.sign.ed25519"
|
||||
#define OSTREE_SIGN_METADATA_ED25519_TYPE "aay"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ED25519_OK,
|
||||
ED25519_NOT_SUPPORTED,
|
||||
ED25519_FAILED_INITIALIZATION
|
||||
} ed25519_state;
|
||||
|
||||
struct _OstreeSignEd25519
|
||||
{
|
||||
GObject parent;
|
||||
ed25519_state state;
|
||||
guchar *secret_key;
|
||||
GList *public_keys;
|
||||
GList *revoked_keys;
|
||||
};
|
||||
|
||||
#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC (OstreeSignEd25519, g_object_unref)
|
||||
#endif
|
||||
|
||||
static void
|
||||
ostree_sign_ed25519_iface_init (OstreeSignInterface *self);
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE (OstreeSignEd25519, _ostree_sign_ed25519, G_TYPE_OBJECT,
|
||||
G_IMPLEMENT_INTERFACE (OSTREE_TYPE_SIGN, ostree_sign_ed25519_iface_init));
|
||||
|
||||
static void
|
||||
ostree_sign_ed25519_iface_init (OstreeSignInterface *self)
|
||||
{
|
||||
|
||||
self->data = ostree_sign_ed25519_data;
|
||||
self->data_verify = ostree_sign_ed25519_data_verify;
|
||||
self->get_name = ostree_sign_ed25519_get_name;
|
||||
self->metadata_key = ostree_sign_ed25519_metadata_key;
|
||||
self->metadata_format = ostree_sign_ed25519_metadata_format;
|
||||
self->clear_keys = ostree_sign_ed25519_clear_keys;
|
||||
self->set_sk = ostree_sign_ed25519_set_sk;
|
||||
self->set_pk = ostree_sign_ed25519_set_pk;
|
||||
self->add_pk = ostree_sign_ed25519_add_pk;
|
||||
self->load_pk = ostree_sign_ed25519_load_pk;
|
||||
}
|
||||
|
||||
static void
|
||||
_ostree_sign_ed25519_class_init (OstreeSignEd25519Class *self)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
_ostree_sign_ed25519_init (OstreeSignEd25519 *self)
|
||||
{
|
||||
|
||||
self->state = ED25519_OK;
|
||||
self->secret_key = NULL;
|
||||
self->public_keys = NULL;
|
||||
self->revoked_keys = NULL;
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
if (sodium_init() < 0)
|
||||
self->state = ED25519_FAILED_INITIALIZATION;
|
||||
#else
|
||||
self->state = ED25519_NOT_SUPPORTED;
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_ostree_sign_ed25519_is_initialized (OstreeSignEd25519 *self, GError **error)
|
||||
{
|
||||
switch (self->state)
|
||||
{
|
||||
case ED25519_OK:
|
||||
break;
|
||||
case ED25519_NOT_SUPPORTED:
|
||||
return glnx_throw(error, "ed25519: engine is not supported");
|
||||
case ED25519_FAILED_INITIALIZATION:
|
||||
return glnx_throw(error, "ed25519: libsodium library isn't initialized properly");
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean ostree_sign_ed25519_data (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GBytes **signature,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
guchar *sig = NULL;
|
||||
#endif
|
||||
|
||||
if (!_ostree_sign_ed25519_is_initialized (sign, error))
|
||||
return FALSE;
|
||||
|
||||
if (sign->secret_key == NULL)
|
||||
return glnx_throw (error, "Not able to sign: secret key is not set");
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
unsigned long long sig_size = 0;
|
||||
|
||||
sig = g_malloc0(crypto_sign_BYTES);
|
||||
|
||||
if (crypto_sign_detached (sig,
|
||||
&sig_size,
|
||||
g_bytes_get_data (data, NULL),
|
||||
g_bytes_get_size (data),
|
||||
sign->secret_key))
|
||||
{
|
||||
return glnx_throw (error, "Not able to sign: fail to sign the object");
|
||||
}
|
||||
|
||||
*signature = g_bytes_new_take (sig, sig_size);
|
||||
return TRUE;
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
static gint
|
||||
_compare_ed25519_keys(gconstpointer a, gconstpointer b) {
|
||||
return memcmp (a, b, crypto_sign_PUBLICKEYBYTES);
|
||||
}
|
||||
#endif
|
||||
|
||||
gboolean ostree_sign_ed25519_data_verify (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GVariant *signatures,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
g_return_val_if_fail (data != NULL, FALSE);
|
||||
|
||||
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
|
||||
|
||||
if (!_ostree_sign_ed25519_is_initialized (sign, error))
|
||||
return FALSE;
|
||||
|
||||
if (signatures == NULL)
|
||||
return glnx_throw (error, "ed25519: commit have no signatures of my type");
|
||||
|
||||
if (!g_variant_is_of_type (signatures, (GVariantType *) OSTREE_SIGN_METADATA_ED25519_TYPE))
|
||||
return glnx_throw (error, "ed25519: wrong type passed for verification");
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
/* If no keys pre-loaded then,
|
||||
* try to load public keys from storage(s) */
|
||||
if (sign->public_keys == NULL)
|
||||
{
|
||||
g_autoptr (GVariantBuilder) builder = NULL;
|
||||
g_autoptr (GVariant) options = NULL;
|
||||
|
||||
builder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
|
||||
options = g_variant_builder_end (builder);
|
||||
|
||||
if (!ostree_sign_ed25519_load_pk (self, options, error))
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
g_debug ("verify: data hash = 0x%x", g_bytes_hash(data));
|
||||
|
||||
for (gsize i = 0; i < g_variant_n_children(signatures); i++)
|
||||
{
|
||||
g_autoptr (GVariant) child = g_variant_get_child_value (signatures, i);
|
||||
g_autoptr (GBytes) signature = g_variant_get_data_as_bytes(child);
|
||||
|
||||
g_autofree char * hex = g_malloc0 (crypto_sign_PUBLICKEYBYTES*2 + 1);
|
||||
|
||||
g_debug("Read signature %d: %s", (gint)i, g_variant_print(child, TRUE));
|
||||
|
||||
for (GList *public_key = sign->public_keys;
|
||||
public_key != NULL;
|
||||
public_key = public_key->next)
|
||||
{
|
||||
|
||||
/* TODO: use non-list for tons of revoked keys? */
|
||||
if (g_list_find_custom (sign->revoked_keys, public_key->data, _compare_ed25519_keys) != NULL)
|
||||
{
|
||||
g_debug("Skip revoked key '%s'",
|
||||
sodium_bin2hex (hex, crypto_sign_PUBLICKEYBYTES*2+1, public_key->data, crypto_sign_PUBLICKEYBYTES));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (crypto_sign_verify_detached ((guchar *) g_variant_get_data (child),
|
||||
g_bytes_get_data (data, NULL),
|
||||
g_bytes_get_size (data),
|
||||
public_key->data) != 0)
|
||||
{
|
||||
/* Incorrect signature! */
|
||||
g_debug("Signature couldn't be verified with key '%s'",
|
||||
sodium_bin2hex (hex, crypto_sign_PUBLICKEYBYTES*2+1, public_key->data, crypto_sign_PUBLICKEYBYTES));
|
||||
}
|
||||
else
|
||||
{
|
||||
g_debug ("Signature verified successfully with key '%s'",
|
||||
sodium_bin2hex (hex, crypto_sign_PUBLICKEYBYTES*2+1, public_key->data, crypto_sign_PUBLICKEYBYTES));
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return glnx_throw (error, "Not able to verify: no valid signatures found");
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
const gchar * ostree_sign_ed25519_get_name (OstreeSign *self)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
return OSTREE_SIGN_ED25519_NAME;
|
||||
}
|
||||
|
||||
const gchar * ostree_sign_ed25519_metadata_key (OstreeSign *self)
|
||||
{
|
||||
|
||||
return OSTREE_SIGN_METADATA_ED25519_KEY;
|
||||
}
|
||||
|
||||
const gchar * ostree_sign_ed25519_metadata_format (OstreeSign *self)
|
||||
{
|
||||
|
||||
return OSTREE_SIGN_METADATA_ED25519_TYPE;
|
||||
}
|
||||
|
||||
gboolean ostree_sign_ed25519_clear_keys (OstreeSign *self,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
|
||||
|
||||
if (!_ostree_sign_ed25519_is_initialized (sign, error))
|
||||
return FALSE;
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
/* Clear secret key */
|
||||
if (sign->secret_key != NULL)
|
||||
{
|
||||
memset (sign->secret_key, 0, crypto_sign_SECRETKEYBYTES);
|
||||
g_free (sign->secret_key);
|
||||
sign->secret_key = NULL;
|
||||
}
|
||||
|
||||
/* Clear already loaded trusted keys */
|
||||
if (sign->public_keys != NULL)
|
||||
{
|
||||
g_list_free_full (sign->public_keys, g_free);
|
||||
sign->public_keys = NULL;
|
||||
}
|
||||
|
||||
/* Clear already loaded revoked keys */
|
||||
if (sign->revoked_keys != NULL)
|
||||
{
|
||||
g_list_free_full (sign->revoked_keys, g_free);
|
||||
sign->revoked_keys = NULL;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Support 2 representations:
|
||||
* base64 ascii -- secret key is passed as string
|
||||
* raw key -- key is passed as bytes array
|
||||
* */
|
||||
gboolean ostree_sign_ed25519_set_sk (OstreeSign *self,
|
||||
GVariant *secret_key,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
|
||||
if (!ostree_sign_ed25519_clear_keys (self, error))
|
||||
return FALSE;
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
|
||||
|
||||
gsize n_elements = 0;
|
||||
|
||||
if (g_variant_is_of_type (secret_key, G_VARIANT_TYPE_STRING))
|
||||
{
|
||||
const gchar *sk_ascii = g_variant_get_string (secret_key, NULL);
|
||||
sign->secret_key = g_base64_decode (sk_ascii, &n_elements);
|
||||
}
|
||||
else if (g_variant_is_of_type (secret_key, G_VARIANT_TYPE_BYTESTRING))
|
||||
{
|
||||
sign->secret_key = (guchar *) g_variant_get_fixed_array (secret_key, &n_elements, sizeof(guchar));
|
||||
}
|
||||
else
|
||||
{
|
||||
return glnx_throw (error, "Unknown ed25519 secret key type");
|
||||
}
|
||||
|
||||
if (n_elements != crypto_sign_SECRETKEYBYTES)
|
||||
return glnx_throw (error, "Incorrect ed25519 secret key");
|
||||
|
||||
return TRUE;
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Support 2 representations:
|
||||
* base64 ascii -- public key is passed as string
|
||||
* raw key -- key is passed as bytes array
|
||||
* */
|
||||
gboolean ostree_sign_ed25519_set_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
if (!ostree_sign_ed25519_clear_keys (self, error))
|
||||
return FALSE;
|
||||
|
||||
return ostree_sign_ed25519_add_pk (self, public_key, error);
|
||||
}
|
||||
|
||||
/* Support 2 representations:
|
||||
* base64 ascii -- public key is passed as string
|
||||
* raw key -- key is passed as bytes array
|
||||
* */
|
||||
gboolean ostree_sign_ed25519_add_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
|
||||
|
||||
if (!_ostree_sign_ed25519_is_initialized (sign, error))
|
||||
return FALSE;
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
gpointer key = NULL;
|
||||
gsize n_elements = 0;
|
||||
|
||||
if (g_variant_is_of_type (public_key, G_VARIANT_TYPE_STRING))
|
||||
{
|
||||
const gchar *pk_ascii = g_variant_get_string (public_key, NULL);
|
||||
key = g_base64_decode (pk_ascii, &n_elements);
|
||||
}
|
||||
else if (g_variant_is_of_type (public_key, G_VARIANT_TYPE_BYTESTRING))
|
||||
{
|
||||
key = (gpointer) g_variant_get_fixed_array (public_key, &n_elements, sizeof(guchar));
|
||||
}
|
||||
else
|
||||
{
|
||||
return glnx_throw (error, "Unknown ed25519 public key type");
|
||||
}
|
||||
|
||||
g_autofree char *hex = g_malloc0 (crypto_sign_PUBLICKEYBYTES*2 + 1);
|
||||
g_debug ("Read ed25519 public key = %s", sodium_bin2hex (hex, crypto_sign_PUBLICKEYBYTES*2+1, key, n_elements));
|
||||
|
||||
if (n_elements != crypto_sign_PUBLICKEYBYTES)
|
||||
return glnx_throw (error, "Incorrect ed25519 public key");
|
||||
|
||||
if (g_list_find_custom (sign->public_keys, key, _compare_ed25519_keys) == NULL)
|
||||
{
|
||||
gpointer newkey = g_memdup (key, n_elements);
|
||||
sign->public_keys = g_list_prepend (sign->public_keys, newkey);
|
||||
}
|
||||
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
/* Add revoked public key */
|
||||
static gboolean
|
||||
_ed25519_add_revoked (OstreeSign *self,
|
||||
GVariant *revoked_key,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
if (!g_variant_is_of_type (revoked_key, G_VARIANT_TYPE_STRING))
|
||||
return glnx_throw (error, "Unknown ed25519 revoked key type");
|
||||
|
||||
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
|
||||
|
||||
const gchar *rk_ascii = g_variant_get_string (revoked_key, NULL);
|
||||
gsize n_elements = 0;
|
||||
gpointer key = g_base64_decode (rk_ascii, &n_elements);
|
||||
|
||||
g_autofree char * hex = g_malloc0 (crypto_sign_PUBLICKEYBYTES*2 + 1);
|
||||
g_debug ("Read ed25519 revoked key = %s", sodium_bin2hex (hex, crypto_sign_PUBLICKEYBYTES*2+1, key, n_elements));
|
||||
|
||||
if (n_elements != crypto_sign_PUBLICKEYBYTES)
|
||||
{
|
||||
return glnx_throw (error, "Incorrect ed25519 revoked key");
|
||||
}
|
||||
|
||||
if (g_list_find_custom (sign->revoked_keys, key, _compare_ed25519_keys) == NULL)
|
||||
{
|
||||
gpointer newkey = g_memdup (key, n_elements);
|
||||
sign->revoked_keys = g_list_prepend (sign->revoked_keys, newkey);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
|
||||
|
||||
static gboolean
|
||||
_load_pk_from_stream (OstreeSign *self,
|
||||
GDataInputStream *key_data_in,
|
||||
gboolean trusted,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (key_data_in, FALSE);
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
gboolean ret = FALSE;
|
||||
|
||||
/* Use simple file format with just a list of base64 public keys per line */
|
||||
while (TRUE)
|
||||
{
|
||||
gsize len = 0;
|
||||
g_autofree char *line = g_data_input_stream_read_line (key_data_in, &len, NULL, error);
|
||||
g_autoptr (GVariant) pk = NULL;
|
||||
gboolean added = FALSE;
|
||||
|
||||
if (*error != NULL)
|
||||
return FALSE;
|
||||
|
||||
if (line == NULL)
|
||||
return ret;
|
||||
|
||||
/* Read the key itself */
|
||||
/* base64 encoded key */
|
||||
pk = g_variant_new_string (line);
|
||||
|
||||
if (trusted)
|
||||
added = ostree_sign_ed25519_add_pk (self, pk, error);
|
||||
else
|
||||
added = _ed25519_add_revoked (self, pk, error);
|
||||
|
||||
g_debug ("%s %s key: %s",
|
||||
added ? "Added" : "Invalid",
|
||||
trusted ? "public" : "revoked",
|
||||
line);
|
||||
|
||||
/* Mark what we load at least one key */
|
||||
if (added)
|
||||
ret = TRUE;
|
||||
}
|
||||
#endif /* HAVE_LIBSODIUM */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_load_pk_from_file (OstreeSign *self,
|
||||
const gchar *filename,
|
||||
gboolean trusted,
|
||||
GError **error)
|
||||
{
|
||||
g_debug ("Processing file '%s'", filename);
|
||||
|
||||
g_autoptr (GFile) keyfile = NULL;
|
||||
g_autoptr (GFileInputStream) key_stream_in = NULL;
|
||||
g_autoptr (GDataInputStream) key_data_in = NULL;
|
||||
|
||||
if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR))
|
||||
{
|
||||
g_debug ("Can't open file '%s' with public keys", filename);
|
||||
return glnx_throw (error, "File object '%s' is not a regular file", filename);
|
||||
}
|
||||
|
||||
keyfile = g_file_new_for_path (filename);
|
||||
key_stream_in = g_file_read (keyfile, NULL, error);
|
||||
if (key_stream_in == NULL)
|
||||
return FALSE;
|
||||
|
||||
key_data_in = g_data_input_stream_new (G_INPUT_STREAM(key_stream_in));
|
||||
g_assert (key_data_in != NULL);
|
||||
|
||||
if (!_load_pk_from_stream (self, key_data_in, trusted, error))
|
||||
{
|
||||
if (error == NULL || *error == NULL)
|
||||
return glnx_throw (error,
|
||||
"signature: ed25519: no valid keys in file '%s'",
|
||||
filename);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_ed25519_load_pk (OstreeSign *self,
|
||||
GVariant *options,
|
||||
gboolean trusted,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
gboolean ret = FALSE;
|
||||
const gchar *custom_dir = NULL;
|
||||
|
||||
g_autoptr (GPtrArray) base_dirs = g_ptr_array_new_with_free_func (g_free);
|
||||
g_autoptr (GPtrArray) ed25519_files = g_ptr_array_new_with_free_func (g_free);
|
||||
|
||||
if (g_variant_lookup (options, "basedir", "&s", &custom_dir))
|
||||
{
|
||||
/* Add custom directory */
|
||||
g_ptr_array_add (base_dirs, g_strdup (custom_dir));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Default paths where to find files with public keys */
|
||||
g_ptr_array_add (base_dirs, g_strdup ("/etc/ostree"));
|
||||
g_ptr_array_add (base_dirs, g_strdup (DATADIR "/ostree"));
|
||||
}
|
||||
|
||||
/* Scan all well-known directories and construct the list with file names to scan keys */
|
||||
for (gint i=0; i < base_dirs->len; i++)
|
||||
{
|
||||
gchar *base_name = NULL;
|
||||
g_autofree gchar *base_dir = NULL;
|
||||
g_autoptr (GDir) dir = NULL;
|
||||
|
||||
base_name = g_build_filename ((gchar *)g_ptr_array_index (base_dirs, i),
|
||||
trusted ? "trusted.ed25519" : "revoked.ed25519",
|
||||
NULL);
|
||||
|
||||
g_debug ("Check ed25519 keys from file: %s", base_name);
|
||||
g_ptr_array_add (ed25519_files, base_name);
|
||||
|
||||
base_dir = g_strconcat (base_name, ".d", NULL);
|
||||
dir = g_dir_open (base_dir, 0, error);
|
||||
if (dir == NULL)
|
||||
{
|
||||
g_clear_error (error);
|
||||
continue;
|
||||
}
|
||||
const gchar *entry = NULL;
|
||||
while ((entry = g_dir_read_name (dir)) != NULL)
|
||||
{
|
||||
gchar *filename = g_build_filename (base_dir, entry, NULL);
|
||||
g_debug ("Check ed25519 keys from file: %s", filename);
|
||||
g_ptr_array_add (ed25519_files, filename);
|
||||
}
|
||||
}
|
||||
|
||||
/* Scan all well-known files */
|
||||
for (gint i=0; i < ed25519_files->len; i++)
|
||||
{
|
||||
if (!_load_pk_from_file (self, (gchar *)g_ptr_array_index (ed25519_files, i), trusted, error))
|
||||
{
|
||||
g_debug ("Problem with loading ed25519 %s keys from `%s`",
|
||||
trusted ? "public" : "revoked",
|
||||
(gchar *)g_ptr_array_index (ed25519_files, i));
|
||||
g_clear_error(error);
|
||||
}
|
||||
else
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
if (!ret && (error == NULL || *error == NULL))
|
||||
return glnx_throw (error, "signature: ed25519: no keys loaded");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* options argument should be a{sv}:
|
||||
* - filename -- single file to use to load keys from;
|
||||
* - basedir -- directory containing subdirectories
|
||||
* 'trusted.ed25519.d' and 'revoked.ed25519.d' with appropriate
|
||||
* public keys. Used for testing and re-definition of system-wide
|
||||
* directories if defaults are not suitable for any reason.
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_ed25519_load_pk (OstreeSign *self,
|
||||
GVariant *options,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
const gchar *filename = NULL;
|
||||
|
||||
OstreeSignEd25519 *sign = _ostree_sign_ed25519_get_instance_private(OSTREE_SIGN_ED25519(self));
|
||||
if (!_ostree_sign_ed25519_is_initialized (sign, error))
|
||||
return FALSE;
|
||||
|
||||
/* Read keys only from single file provided */
|
||||
if (g_variant_lookup (options, "filename", "&s", &filename))
|
||||
return _load_pk_from_file (self, filename, TRUE, error);
|
||||
|
||||
/* Load public keys from well-known directories and files */
|
||||
if (!_ed25519_load_pk (self, options, TRUE, error))
|
||||
return FALSE;
|
||||
|
||||
/* Load untrusted keys from well-known directories and files
|
||||
* Ignore the failure from this function -- it is expected to have
|
||||
* empty list of revoked keys.
|
||||
* */
|
||||
if (!_ed25519_load_pk (self, options, FALSE, error))
|
||||
g_clear_error(error);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */
|
||||
|
||||
/*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
*
|
||||
* 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, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Authors:
|
||||
* - Denis Pynkin (d4s) <denis.pynkin@collabora.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ostree-sign.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define OSTREE_TYPE_SIGN_ED25519 (_ostree_sign_ed25519_get_type ())
|
||||
|
||||
GType _ostree_sign_ed25519_get_type (void);
|
||||
|
||||
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
||||
typedef struct _OstreeSignEd25519 OstreeSignEd25519;
|
||||
typedef struct { GObjectClass parent_class; } OstreeSignEd25519Class;
|
||||
|
||||
static inline OstreeSignEd25519 *OSTREE_SIGN_ED25519 (gpointer ptr) { return G_TYPE_CHECK_INSTANCE_CAST (ptr, _ostree_sign_ed25519_get_type (), OstreeSignEd25519); }
|
||||
static inline gboolean OSTREE_IS_SIGN_ED25519 (gpointer ptr) { return G_TYPE_CHECK_INSTANCE_TYPE (ptr, _ostree_sign_ed25519_get_type ()); }
|
||||
|
||||
G_GNUC_END_IGNORE_DEPRECATIONS
|
||||
|
||||
/* Have to use glib-2.44 for this
|
||||
_OSTREE_PUBLIC
|
||||
G_DECLARE_FINAL_TYPE (OstreeSignEd25519,
|
||||
ostree_sign_ed25519,
|
||||
OSTREE,
|
||||
SIGN_ED25519,
|
||||
GObject)
|
||||
*/
|
||||
|
||||
gboolean ostree_sign_ed25519_data (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GBytes **signature,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
gboolean ostree_sign_ed25519_data_verify (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GVariant *signatures,
|
||||
GError **error);
|
||||
|
||||
const gchar * ostree_sign_ed25519_get_name (OstreeSign *self);
|
||||
const gchar * ostree_sign_ed25519_metadata_key (OstreeSign *self);
|
||||
const gchar * ostree_sign_ed25519_metadata_format (OstreeSign *self);
|
||||
|
||||
gboolean ostree_sign_ed25519_clear_keys (OstreeSign *self,
|
||||
GError **error);
|
||||
|
||||
gboolean ostree_sign_ed25519_set_sk (OstreeSign *self,
|
||||
GVariant *secret_key,
|
||||
GError **error);
|
||||
|
||||
gboolean ostree_sign_ed25519_set_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error);
|
||||
|
||||
gboolean ostree_sign_ed25519_add_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error);
|
||||
|
||||
gboolean ostree_sign_ed25519_load_pk (OstreeSign *self,
|
||||
GVariant *options,
|
||||
GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
@@ -0,0 +1,670 @@
|
||||
/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */
|
||||
|
||||
/*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
*
|
||||
* 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, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* SECTION:ostree-sign
|
||||
* @title: Signature management
|
||||
* @short_description: Sign and verify commits
|
||||
*
|
||||
* An #OstreeSign interface allows to select and use any available engine
|
||||
* for signing or verifying the commit object or summary file.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include "libglnx.h"
|
||||
#include "otutil.h"
|
||||
|
||||
#include "ostree-autocleanups.h"
|
||||
#include "ostree-core.h"
|
||||
#include "ostree-sign.h"
|
||||
#include "ostree-sign-dummy.h"
|
||||
#ifdef HAVE_LIBSODIUM
|
||||
#include "ostree-sign-ed25519.h"
|
||||
#endif
|
||||
|
||||
#include "ostree-autocleanups.h"
|
||||
#include "ostree-repo-private.h"
|
||||
|
||||
#undef G_LOG_DOMAIN
|
||||
#define G_LOG_DOMAIN "OSTreeSign"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
gchar *name;
|
||||
GType type;
|
||||
} _sign_type;
|
||||
|
||||
_sign_type sign_types[] =
|
||||
{
|
||||
#if defined(HAVE_LIBSODIUM)
|
||||
{"ed25519", 0},
|
||||
#endif
|
||||
{"dummy", 0}
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
#if defined(HAVE_LIBSODIUM)
|
||||
SIGN_ED25519,
|
||||
#endif
|
||||
SIGN_DUMMY
|
||||
};
|
||||
|
||||
G_DEFINE_INTERFACE (OstreeSign, ostree_sign, G_TYPE_OBJECT)
|
||||
|
||||
static void
|
||||
ostree_sign_default_init (OstreeSignInterface *iface)
|
||||
{
|
||||
g_debug ("OstreeSign initialization");
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_metadata_key:
|
||||
* @self: an #OstreeSign object
|
||||
*
|
||||
* Return the pointer to the name of the key used in (detached) metadata for
|
||||
* current signing engine.
|
||||
*
|
||||
* Returns: (transfer none): pointer to the metadata key name,
|
||||
* @NULL in case of error (unlikely).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
const gchar *
|
||||
ostree_sign_metadata_key (OstreeSign *self)
|
||||
{
|
||||
|
||||
g_return_val_if_fail (OSTREE_SIGN_GET_IFACE (self)->metadata_key != NULL, NULL);
|
||||
return OSTREE_SIGN_GET_IFACE (self)->metadata_key (self);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_metadata_format:
|
||||
* @self: an #OstreeSign object
|
||||
*
|
||||
* Return the pointer to the string with format used in (detached) metadata for
|
||||
* current signing engine.
|
||||
*
|
||||
* Returns: (transfer none): pointer to the metadata format,
|
||||
* @NULL in case of error (unlikely).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
const gchar *
|
||||
ostree_sign_metadata_format (OstreeSign *self)
|
||||
{
|
||||
|
||||
g_return_val_if_fail (OSTREE_SIGN_GET_IFACE (self)->metadata_format != NULL, NULL);
|
||||
return OSTREE_SIGN_GET_IFACE (self)->metadata_format (self);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_clear_keys:
|
||||
* @self: an #OstreeSign object
|
||||
* @error: a #GError
|
||||
*
|
||||
* Clear all previously preloaded secret and public keys.
|
||||
*
|
||||
* Returns: @TRUE in case if no errors, @FALSE in case of error
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_clear_keys (OstreeSign *self,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
if (OSTREE_SIGN_GET_IFACE (self)->clear_keys == NULL)
|
||||
return glnx_throw (error, "not implemented");
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->clear_keys (self, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_set_sk:
|
||||
* @self: an #OstreeSign object
|
||||
* @secret_key: secret key to be added
|
||||
* @error: a #GError
|
||||
*
|
||||
* Set the secret key to be used for signing data, commits and summary.
|
||||
*
|
||||
* The @secret_key argument depends of the particular engine implementation.
|
||||
*
|
||||
* Returns: @TRUE in case if the key could be set successfully,
|
||||
* @FALSE in case of error (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_set_sk (OstreeSign *self,
|
||||
GVariant *secret_key,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
if (OSTREE_SIGN_GET_IFACE (self)->set_sk == NULL)
|
||||
return glnx_throw (error, "not implemented");
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->set_sk (self, secret_key, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_set_pk:
|
||||
* @self: an #OstreeSign object
|
||||
* @public_key: single public key to be added
|
||||
* @error: a #GError
|
||||
*
|
||||
* Set the public key for verification. It is expected what all
|
||||
* previously pre-loaded public keys will be dropped.
|
||||
*
|
||||
* The @public_key argument depends of the particular engine implementation.
|
||||
*
|
||||
* Returns: @TRUE in case if the key could be set successfully,
|
||||
* @FALSE in case of error (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_set_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
if (OSTREE_SIGN_GET_IFACE (self)->set_pk == NULL)
|
||||
return glnx_throw (error, "not implemented");
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->set_pk (self, public_key, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_add_pk:
|
||||
* @self: an #OstreeSign object
|
||||
* @public_key: single public key to be added
|
||||
* @error: a #GError
|
||||
*
|
||||
* Add the public key for verification. Could be called multiple times for
|
||||
* adding all needed keys to be used for verification.
|
||||
*
|
||||
* The @public_key argument depends of the particular engine implementation.
|
||||
*
|
||||
* Returns: @TRUE in case if the key could be added successfully,
|
||||
* @FALSE in case of error (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_add_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
if (OSTREE_SIGN_GET_IFACE (self)->add_pk == NULL)
|
||||
return glnx_throw (error, "not implemented");
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->add_pk (self, public_key, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_load_pk:
|
||||
* @self: an #OstreeSign object
|
||||
* @options: any options
|
||||
* @error: a #GError
|
||||
*
|
||||
* Load public keys for verification from anywhere.
|
||||
* It is expected that all keys would be added to already pre-loaded keys.
|
||||
*
|
||||
* The @options argument depends of the particular engine implementation.
|
||||
*
|
||||
* For example, @ed25515 engine could use following string-formatted options:
|
||||
* - @filename -- single file to use to load keys from
|
||||
* - @basedir -- directory containing subdirectories
|
||||
* 'trusted.ed25519.d' and 'revoked.ed25519.d' with appropriate
|
||||
* public keys. Used for testing and re-definition of system-wide
|
||||
* directories if defaults are not suitable for any reason.
|
||||
*
|
||||
* Returns: @TRUE in case if at least one key could be load successfully,
|
||||
* @FALSE in case of error (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
/*
|
||||
* No need to have similar function for secret keys load -- it is expected
|
||||
* what the signing software will load the secret key in it's own way.
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_load_pk (OstreeSign *self,
|
||||
GVariant *options,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
if (OSTREE_SIGN_GET_IFACE (self)->load_pk == NULL)
|
||||
return glnx_throw (error, "not implemented");
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->load_pk (self, options, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_data:
|
||||
* @self: an #OstreeSign object
|
||||
* @data: the raw data to be signed with pre-loaded secret key
|
||||
* @signature: in case of success will contain signature
|
||||
* @cancellable: A #GCancellable
|
||||
* @error: a #GError
|
||||
*
|
||||
* Sign the given @data with pre-loaded secret key.
|
||||
*
|
||||
* Depending of the signing engine used you will need to load
|
||||
* the secret key with #ostree_sign_set_sk.
|
||||
*
|
||||
* Returns: @TRUE if @data has been signed successfully,
|
||||
* @FALSE in case of error (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_data (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GBytes **signature,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
if (OSTREE_SIGN_GET_IFACE (self)->data == NULL)
|
||||
return glnx_throw (error, "not implemented");
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->data (self, data, signature, cancellable, error);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_data_verify:
|
||||
* @self: an #OstreeSign object
|
||||
* @data: the raw data to check
|
||||
* @signatures: the signatures to be checked
|
||||
* @error: a #GError
|
||||
*
|
||||
* Verify given data against signatures with pre-loaded public keys.
|
||||
*
|
||||
* Depending of the signing engine used you will need to load
|
||||
* the public key(s) with #ostree_sign_set_pk, #ostree_sign_add_pk
|
||||
* or #ostree_sign_load_pk.
|
||||
*
|
||||
* Returns: @TRUE if @data has been signed at least with any single valid key,
|
||||
* @FALSE in case of error or no valid keys are available (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_data_verify (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GVariant *signatures,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
if (OSTREE_SIGN_GET_IFACE (self)->data_verify == NULL)
|
||||
return glnx_throw (error, "not implemented");
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->data_verify(self, data, signatures, error);
|
||||
}
|
||||
|
||||
/*
|
||||
* Adopted version of _ostree_detached_metadata_append_gpg_sig ()
|
||||
*/
|
||||
static GVariant *
|
||||
_sign_detached_metadata_append (OstreeSign *self,
|
||||
GVariant *existing_metadata,
|
||||
GBytes *signature_bytes)
|
||||
{
|
||||
g_return_val_if_fail (signature_bytes != NULL, FALSE);
|
||||
|
||||
GVariantDict metadata_dict;
|
||||
g_autoptr(GVariant) signature_data = NULL;
|
||||
g_autoptr(GVariantBuilder) signature_builder = NULL;
|
||||
|
||||
g_variant_dict_init (&metadata_dict, existing_metadata);
|
||||
|
||||
const gchar *signature_key = ostree_sign_metadata_key(self);
|
||||
GVariantType *signature_format = (GVariantType *) ostree_sign_metadata_format(self);
|
||||
|
||||
signature_data = g_variant_dict_lookup_value (&metadata_dict,
|
||||
signature_key,
|
||||
(GVariantType*)signature_format);
|
||||
|
||||
/* signature_data may be NULL */
|
||||
signature_builder = ot_util_variant_builder_from_variant (signature_data, signature_format);
|
||||
|
||||
g_variant_builder_add (signature_builder, "@ay", ot_gvariant_new_ay_bytes (signature_bytes));
|
||||
|
||||
g_variant_dict_insert_value (&metadata_dict,
|
||||
signature_key,
|
||||
g_variant_builder_end (signature_builder));
|
||||
|
||||
return g_variant_dict_end (&metadata_dict);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_commit_verify:
|
||||
* @self: an #OstreeSign object
|
||||
* @repo: an #OsreeRepo object
|
||||
* @commit_checksum: SHA256 of given commit to verify
|
||||
* @cancellable: A #GCancellable
|
||||
* @error: a #GError
|
||||
*
|
||||
* Verify if commit is signed with known key.
|
||||
*
|
||||
* Depending of the signing engine used you will need to load
|
||||
* the public key(s) for verification with #ostree_sign_set_pk,
|
||||
* #ostree_sign_add_pk and/or #ostree_sign_load_pk.
|
||||
*
|
||||
* Returns: @TRUE if commit has been verified successfully,
|
||||
* @FALSE in case of error or no valid keys are available (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_commit_verify (OstreeSign *self,
|
||||
OstreeRepo *repo,
|
||||
const gchar *commit_checksum,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
|
||||
g_autoptr(GVariant) commit_variant = NULL;
|
||||
/* Load the commit */
|
||||
if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT,
|
||||
commit_checksum, &commit_variant,
|
||||
error))
|
||||
return glnx_prefix_error (error, "Failed to read commit");
|
||||
|
||||
/* Load the metadata */
|
||||
g_autoptr(GVariant) metadata = NULL;
|
||||
if (!ostree_repo_read_commit_detached_metadata (repo,
|
||||
commit_checksum,
|
||||
&metadata,
|
||||
cancellable,
|
||||
error))
|
||||
return glnx_prefix_error (error, "Failed to read detached metadata");
|
||||
|
||||
g_autoptr(GBytes) signed_data = g_variant_get_data_as_bytes (commit_variant);
|
||||
|
||||
g_autoptr(GVariant) signatures = NULL;
|
||||
|
||||
const gchar *signature_key = ostree_sign_metadata_key(self);
|
||||
GVariantType *signature_format = (GVariantType *) ostree_sign_metadata_format(self);
|
||||
|
||||
if (metadata)
|
||||
signatures = g_variant_lookup_value (metadata,
|
||||
signature_key,
|
||||
signature_format);
|
||||
|
||||
|
||||
return ostree_sign_data_verify (self,
|
||||
signed_data,
|
||||
signatures,
|
||||
error);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_get_name:
|
||||
* @self: an #OstreeSign object
|
||||
*
|
||||
* Return the pointer to the name of currently used/selected signing engine.
|
||||
*
|
||||
* The list of available engines could be acquired with #ostree_sign_list_names.
|
||||
*
|
||||
* Returns: (transfer none): pointer to the name
|
||||
* @NULL in case of error (unlikely).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
const gchar *
|
||||
ostree_sign_get_name (OstreeSign *self)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), NULL);
|
||||
g_return_val_if_fail (OSTREE_SIGN_GET_IFACE (self)->get_name != NULL, NULL);
|
||||
|
||||
return OSTREE_SIGN_GET_IFACE (self)->get_name (self);
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_commit:
|
||||
* @self: an #OstreeSign object
|
||||
* @repo: an #OsreeRepo object
|
||||
* @commit_checksum: SHA256 of given commit to sign
|
||||
* @cancellable: A #GCancellable
|
||||
* @error: a #GError
|
||||
*
|
||||
* Add a signature to a commit.
|
||||
*
|
||||
* Depending of the signing engine used you will need to load
|
||||
* the secret key with #ostree_sign_set_sk.
|
||||
*
|
||||
* Returns: @TRUE if commit has been signed successfully,
|
||||
* @FALSE in case of error (@error will contain the reason).
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_commit (OstreeSign *self,
|
||||
OstreeRepo *repo,
|
||||
const gchar *commit_checksum,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
|
||||
g_autoptr(GBytes) commit_data = NULL;
|
||||
g_autoptr(GBytes) signature = NULL;
|
||||
g_autoptr(GVariant) commit_variant = NULL;
|
||||
g_autoptr(GVariant) old_metadata = NULL;
|
||||
g_autoptr(GVariant) new_metadata = NULL;
|
||||
|
||||
if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT,
|
||||
commit_checksum, &commit_variant, error))
|
||||
return glnx_prefix_error (error, "Failed to read commit");
|
||||
|
||||
if (!ostree_repo_read_commit_detached_metadata (repo,
|
||||
commit_checksum,
|
||||
&old_metadata,
|
||||
cancellable,
|
||||
error))
|
||||
return glnx_prefix_error (error, "Failed to read detached metadata");
|
||||
|
||||
commit_data = g_variant_get_data_as_bytes (commit_variant);
|
||||
|
||||
if (!ostree_sign_data (self, commit_data, &signature,
|
||||
cancellable, error))
|
||||
return glnx_prefix_error (error, "Not able to sign the cobject");
|
||||
|
||||
new_metadata =
|
||||
_sign_detached_metadata_append (self, old_metadata, signature);
|
||||
|
||||
if (!ostree_repo_write_commit_detached_metadata (repo,
|
||||
commit_checksum,
|
||||
new_metadata,
|
||||
cancellable,
|
||||
error))
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_list_names:
|
||||
*
|
||||
* Return an array with all available sign engines names.
|
||||
*
|
||||
* Returns: (transfer full): an array of strings, free when you used it
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
GStrv
|
||||
ostree_sign_list_names(void)
|
||||
{
|
||||
|
||||
GStrv names = g_new0 (char *, G_N_ELEMENTS(sign_types) + 1);
|
||||
gint i = 0;
|
||||
|
||||
for (i=0; i < G_N_ELEMENTS(sign_types); i++)
|
||||
{
|
||||
names[i] = g_strdup(sign_types[i].name);
|
||||
g_debug ("Found '%s' signing engine", names[i]);
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_get_by_name:
|
||||
* @name: the name of desired signature engine
|
||||
* @error: return location for a #GError
|
||||
*
|
||||
* Tries to find and return proper signing engine by it's name.
|
||||
*
|
||||
* The list of available engines could be acquired with #ostree_sign_list_names.
|
||||
*
|
||||
* Returns: (transfer full): a constant, free when you used it
|
||||
*
|
||||
* Since: 2020.2
|
||||
*/
|
||||
OstreeSign *
|
||||
ostree_sign_get_by_name (const gchar *name, GError **error)
|
||||
{
|
||||
|
||||
OstreeSign *sign = NULL;
|
||||
|
||||
/* Get types if not initialized yet */
|
||||
#if defined(HAVE_LIBSODIUM)
|
||||
if (sign_types[SIGN_ED25519].type == 0)
|
||||
sign_types[SIGN_ED25519].type = OSTREE_TYPE_SIGN_ED25519;
|
||||
#endif
|
||||
if (sign_types[SIGN_DUMMY].type == 0)
|
||||
sign_types[SIGN_DUMMY].type = OSTREE_TYPE_SIGN_DUMMY;
|
||||
|
||||
for (gint i=0; i < G_N_ELEMENTS(sign_types); i++)
|
||||
{
|
||||
if (g_strcmp0 (name, sign_types[i].name) == 0)
|
||||
{
|
||||
g_debug ("Using '%s' signing engine", sign_types[i].name);
|
||||
sign = g_object_new (sign_types[i].type, NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (sign == NULL)
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
||||
"Requested signature type is not implemented");
|
||||
|
||||
return sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* ostree_sign_summary:
|
||||
* @self: Self
|
||||
* @repo: ostree repository
|
||||
* @keys: keys -- GVariant containing keys as GVarints specific to signature type.
|
||||
* @cancellable: A #GCancellable
|
||||
* @error: a #GError
|
||||
*
|
||||
* Add a signature to a summary file.
|
||||
* Based on ostree_repo_add_gpg_signature_summary implementation.
|
||||
*
|
||||
* Returns: @TRUE if summary file has been signed with all provided keys
|
||||
*/
|
||||
gboolean
|
||||
ostree_sign_summary (OstreeSign *self,
|
||||
OstreeRepo *repo,
|
||||
GVariant *keys,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
g_return_val_if_fail (OSTREE_IS_SIGN (self), FALSE);
|
||||
g_return_val_if_fail (OSTREE_IS_REPO (repo), FALSE);
|
||||
|
||||
g_autoptr(GVariant) normalized = NULL;
|
||||
g_autoptr(GBytes) summary_data = NULL;
|
||||
g_autoptr(GVariant) metadata = NULL;
|
||||
|
||||
glnx_autofd int fd = -1;
|
||||
if (!glnx_openat_rdonly (repo->repo_dir_fd, "summary", TRUE, &fd, error))
|
||||
return FALSE;
|
||||
summary_data = ot_fd_readall_or_mmap (fd, 0, error);
|
||||
if (!summary_data)
|
||||
return FALSE;
|
||||
|
||||
/* Note that fd is reused below */
|
||||
glnx_close_fd (&fd);
|
||||
|
||||
if (!ot_openat_ignore_enoent (repo->repo_dir_fd, "summary.sig", &fd, error))
|
||||
return FALSE;
|
||||
|
||||
if (fd >= 0)
|
||||
{
|
||||
if (!ot_variant_read_fd (fd, 0, OSTREE_SUMMARY_SIG_GVARIANT_FORMAT,
|
||||
FALSE, &metadata, error))
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (g_variant_n_children(keys) == 0)
|
||||
return glnx_throw (error, "No keys passed for signing summary");
|
||||
|
||||
GVariantIter *iter;
|
||||
GVariant *key;
|
||||
|
||||
g_variant_get (keys, "av", &iter);
|
||||
while (g_variant_iter_loop (iter, "v", &key))
|
||||
{
|
||||
g_autoptr (GBytes) signature = NULL;
|
||||
|
||||
if (!ostree_sign_set_sk (self, key, error))
|
||||
return FALSE;
|
||||
|
||||
if (!ostree_sign_data (self,
|
||||
summary_data,
|
||||
&signature,
|
||||
cancellable,
|
||||
error))
|
||||
return FALSE;
|
||||
|
||||
g_autoptr(GVariant) old_metadata = g_steal_pointer (&metadata);
|
||||
metadata =
|
||||
_sign_detached_metadata_append (self, old_metadata, signature);
|
||||
}
|
||||
g_variant_iter_free (iter);
|
||||
|
||||
normalized = g_variant_get_normal_form (metadata);
|
||||
if (!_ostree_repo_file_replace_contents (repo,
|
||||
repo->repo_dir_fd,
|
||||
"summary.sig",
|
||||
g_variant_get_data (normalized),
|
||||
g_variant_get_size (normalized),
|
||||
cancellable, error))
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/* vim:set et sw=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e2s: */
|
||||
|
||||
/*
|
||||
* Copyright © 2019 Collabora Ltd.
|
||||
*
|
||||
* 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, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Authors:
|
||||
* - Denis Pynkin (d4s) <denis.pynkin@collabora.com>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <glib.h>
|
||||
#include <glib-object.h>
|
||||
|
||||
#include "ostree-ref.h"
|
||||
#include "ostree-remote.h"
|
||||
#include "ostree-types.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define OSTREE_TYPE_SIGN (ostree_sign_get_type ())
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
GType ostree_sign_get_type (void);
|
||||
|
||||
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
|
||||
typedef struct _OstreeSign OstreeSign;
|
||||
typedef struct _OstreeSignInterface OstreeSignInterface;
|
||||
|
||||
static inline OstreeSign *OSTREE_SIGN (gpointer ptr) { return G_TYPE_CHECK_INSTANCE_CAST (ptr, ostree_sign_get_type (), OstreeSign); }
|
||||
static inline gboolean OSTREE_IS_SIGN (gpointer ptr) { return G_TYPE_CHECK_INSTANCE_TYPE (ptr, ostree_sign_get_type ()); }
|
||||
static inline OstreeSignInterface *OSTREE_SIGN_GET_IFACE (gpointer ptr) { return G_TYPE_INSTANCE_GET_INTERFACE (ptr, ostree_sign_get_type (), OstreeSignInterface); }
|
||||
G_GNUC_END_IGNORE_DEPRECATIONS
|
||||
|
||||
/* Have to use glib-2.44 for this
|
||||
_OSTREE_PUBLIC
|
||||
G_DECLARE_INTERFACE (OstreeSign, ostree_sign, OSTREE, SIGN, GObject)
|
||||
*/
|
||||
|
||||
struct _OstreeSignInterface
|
||||
{
|
||||
GTypeInterface g_iface;
|
||||
const gchar *(* get_name) (OstreeSign *self);
|
||||
gboolean (* data) (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GBytes **signature,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
gboolean (* data_verify) (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GVariant *signatures,
|
||||
GError **error);
|
||||
const gchar *(* metadata_key) (OstreeSign *self);
|
||||
const gchar *(* metadata_format) (OstreeSign *self);
|
||||
gboolean (* clear_keys) (OstreeSign *self,
|
||||
GError **error);
|
||||
gboolean (* set_sk) (OstreeSign *self,
|
||||
GVariant *secret_key,
|
||||
GError **error);
|
||||
gboolean (* set_pk) (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error);
|
||||
gboolean (* add_pk) (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error);
|
||||
gboolean (* load_pk) (OstreeSign *self,
|
||||
GVariant *options,
|
||||
GError **error);
|
||||
};
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
const gchar * ostree_sign_get_name (OstreeSign *self);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_data (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GBytes **signature,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_data_verify (OstreeSign *self,
|
||||
GBytes *data,
|
||||
GVariant *signatures,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
const gchar * ostree_sign_metadata_key (OstreeSign *self);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
const gchar * ostree_sign_metadata_format (OstreeSign *self);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_commit (OstreeSign *self,
|
||||
OstreeRepo *repo,
|
||||
const gchar *commit_checksum,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_commit_verify (OstreeSign *self,
|
||||
OstreeRepo *repo,
|
||||
const gchar *commit_checksum,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_clear_keys (OstreeSign *self,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_set_sk (OstreeSign *self,
|
||||
GVariant *secret_key,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_set_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_add_pk (OstreeSign *self,
|
||||
GVariant *public_key,
|
||||
GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_load_pk (OstreeSign *self,
|
||||
GVariant *options,
|
||||
GError **error);
|
||||
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
GStrv ostree_sign_list_names(void);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
OstreeSign * ostree_sign_get_by_name (const gchar *name, GError **error);
|
||||
|
||||
_OSTREE_PUBLIC
|
||||
gboolean ostree_sign_summary (OstreeSign *self,
|
||||
OstreeRepo *repo,
|
||||
GVariant *keys,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
G_END_DECLS
|
||||
|
||||
@@ -40,5 +40,6 @@
|
||||
#include <ostree-repo-finder-mount.h>
|
||||
#include <ostree-repo-finder-override.h>
|
||||
#include <ostree-kernel-args.h>
|
||||
#include <ostree-sign.h>
|
||||
#include <ostree-autocleanups.h>
|
||||
#include <ostree-version.h>
|
||||
Reference in new issue
Block a user