<commit_before>From 312937eb03c2b48e92bffd8f6ab88108008c6b6f Mon Sep 17 00:00:00 2001
From: <NAME> <<EMAIL>>
Date: Mon, 29 Apr 2019 14:15:18 -0500
Subject: [PATCH] rauc: Add command line option to skip bundle verification

Conditionally remove bundle verification checks when operating
on bundles.

Upstream-Status: Pending

Signed-off-by: <NAME> <<EMAIL>>
---
 include/context.h |  2 ++
 src/bundle.c      |  3 +++
 src/install.c     |  2 +-
 src/main.c        | 27 ++++++++++++++++-----------
 4 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/include/context.h b/include/context.h
index b0bcfba..ba54e81 100644
--- a/include/context.h
+++ b/include/context.h
@@ -43,6 +43,8 @@ typedef struct {
 	gchar *handlerextra;
 	/* ignore compatible check */
 	gboolean ignore_compatible;
+	/* skip bundle verification */
+	gboolean bundle_nosig;
 
 	/* for storing installation runtime informations */
 	RContextInstallationInfo *install_info;
diff --git a/src/bundle.c b/src/bundle.c
index 9ca512c..e0fb5a1 100644
--- a/src/bundle.c
+++ b/src/bundle.c
@@ -326,6 +326,9 @@ static gboolean sign_bundle(const gchar *bundlename, GError **error)
 	g_autoptr(GFileOutputStream) bundlestream = NULL;
 	guint64 offset;
 
+	if (r_context()->bundle_nosig) 
+		return TRUE;
+
 	g_assert_nonnull(r_context()->certpath);
 	g_assert_nonnull(r_context()->keypath);
 
diff --git a/src/install.c b/src/install.c
index 6f535f7..3665b49 100644
--- a/src/install.c
+++ b/src/install.c
@@ -1283,7 +1283,7 @@ gboolean do_install_bundle(RaucInstallArgs *args, GError **error)
 	// TODO: mount info in context ?
 	install_args_update(args, "Checking and mounting bundle...");
 
-	res = check_bundle(bundlefile, &bundle, TRUE, &ierror);
+	res = check_bundle(bundlefile, &bundle, !r_context()->bundle_nosig, &ierror);
 	if (!res) {
 		g_propagate_error(error, ierror);
 		goto out;
diff --git a/src/main.c b/src/main.c
index 5f0ce56..bc1c7fc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,7 +23,8 @@ GMainLoop *r_loop = NULL;
 int r_exit_status = 0;
 
 gboolean install_ignore_compatible = FALSE;
-gboolean info_noverify, info_dumpcert = FALSE;
+gboolean bundle_nosig = FALSE;
+gboolean info_dumpcert = FALSE;
 gboolean status_detailed = FALSE;
 gchar *output_format = NULL;
 
@@ -176,6 +177,7 @@ static gboolean install_start(int argc, char **argv)
 	args->status_result = 2;
 
 	r_context_conf()->ignore_compatible = install_ignore_compatible;
+	r_context_conf()->bundle_nosig = bundle_nosig;
 
 	r_loop = g_main_loop_new(NULL, FALSE);
 	if (ENABLE_SERVICE) {
@@ -261,11 +263,13 @@ static gboolean bundle_start(int argc, char **argv)
 		goto out;
 	}
 
-	if (r_context()->certpath == NULL ||
-	    r_context()->keypath == NULL) {
-		g_printerr("Cert and key files must be provided\n");
-		r_exit_status = 1;
-		goto out;
+	if (!r_context()->bundle_nosig) {
+		if (r_context()->certpath == NULL ||
+		    r_context()->keypath == NULL) {
+			g_printerr("Cert and key files must be provided\n");
+			r_exit_status = 1;
+			goto out;
+		}
 	}
 
 	g_debug("input directory: %s", argv[2]);
@@ -409,7 +413,7 @@ static gboolean resign_start(int argc, char **argv)
 		goto out;
 	}
 
-	if (!check_bundle(argv[2], &bundle, TRUE, &ierror)) {
+	if (!check_bundle(argv[2], &bundle, !bundle_nosig, &ierror)) {
 		g_printerr("%s\n", ierror->message);
 		g_clear_error(&ierror);
 		r_exit_status = 1;
@@ -454,7 +458,7 @@ static gboolean extract_start(int argc, char **argv)
 	g_debug("input bundle: %s", argv[2]);
 	g_debug("output dir: %s", argv[3]);
 
-	if (!check_bundle(argv[2], &bundle, TRUE, &ierror)) {
+	if (!check_bundle(argv[2], &bundle, !bundle_nosig, &ierror)) {
 		g_printerr("%s\n", ierror->message);
 		g_clear_error(&ierror);
 		r_exit_status = 1;
@@ -506,7 +510,7 @@ static gboolean convert_start(int argc, char **argv)
 	g_debug("input bundle: %s", argv[2]);
 	g_debug("output bundle: %s", argv[3]);
 
-	if (!check_bundle(argv[2], &bundle, TRUE, &ierror)) {
+	if (!check_bundle(argv[2], &bundle, !bundle_nosig, &ierror)) {
 		g_printerr("%s\n", ierror->message);
 		g_clear_error(&ierror);
 		r_exit_status = 1;
@@ -865,7 +869,7 @@ static gboolean info_start(int argc, char **argv)
 		goto out;
 	g_debug("input bundle: %s", bundlelocation);
 
-	res = check_bundle(bundlelocation, &bundle, !info_noverify, &error);
+	res = check_bundle(bundlelocation, &bundle, !bundle_nosig, &error);
 	if (!res) {
 		g_printerr("%s\n", error->message);
 		g_clear_error(&error);
@@ -1611,7 +1615,6 @@ GOptionEntry entries_install[] = {
 };
 
 GOptionEntry entries_info[] = {
-	{"no-verify", '\0', 0, G_OPTION_ARG_NONE, &info_noverify, "disable bundle verification", NULL},
 	{"output-format", '\0', 0, G_OPTION_ARG_STRING, &output_format, "output format", "FORMAT"},
 	{"dump-cert", '\0', 0, G_OPTION_ARG_NONE, &info_dumpcert, "dump certificate", NULL},
 	{0}
@@ -1639,6 +1642,7 @@ static void cmdline_handler(int argc, char **argv)
 		{"mount", '\0', 0, G_OPTION_ARG_FILENAME, &mount, "mount prefix", "PATH"},
 		{"override-boot-slot", '\0', 0, G_OPTION_ARG_STRING, &bootslot, "override auto-detection of booted slot", "BOOTNAME"},
 		{"handler-args", '\0', 0, G_OPTION_ARG_STRING, &handlerextra, "extra handler arguments", "ARGS"},
+	        {"no-signatures", '\0', 0, G_OPTION_ARG_NONE, &bundle_nosig, "disable bundle verification", NULL},
 		{"debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "enable debug output", NULL},
 		{"version", '\0', 0, G_OPTION_ARG_NONE, &version, "display version", NULL},
 		{"help", 'h', 0, G_OPTION_ARG_NONE, &help, NULL, NULL},
@@ -1796,6 +1800,7 @@ static void cmdline_handler(int argc, char **argv)
 			r_context_conf()->bootslot = bootslot;
 		if (handlerextra)
 			r_context_conf()->handlerextra = handlerextra;
+		r_context_conf()->bundle_nosig = bundle_nosig;
 	} else {
 		if (confpath != NULL ||
 		    certpath != NULL ||
<commit_msg>rauc: Additional changes to make --no-signatures work in rauc<commit_after>From 29f2eeb0d7744840f668dc616f349a9bfbad073b Mon Sep 17 00:00:00 2001
From: <NAME> <<EMAIL>>
Date: Fri, 28 Jun 2019 12:37:23 -0500
Subject: [PATCH] rauc: Add command line option to skip bundle verification

Conditionally remove bundle verification checks when operating
on bundles.

Upstream-Status: Pending

Co-authored-by: <NAME> <<EMAIL>>
Signed-off-by: <NAME> <<EMAIL>>
---
 include/context.h    |  2 ++
 src/bundle.c         | 78 +++++++++++++++++++++++++-------------------
 src/install.c        |  2 +-
 src/main.c           | 27 ++++++++-------
 src/update_handler.c |  2 +-
 5 files changed, 64 insertions(+), 47 deletions(-)

diff --git a/include/context.h b/include/context.h
index b0bcfba64855806b14bc7a7e1108d7342c124463..ba54e81eaefe34a7f5b26c9571d770bd2dc2f15a 100644
--- a/include/context.h
+++ b/include/context.h
@@ -43,6 +43,8 @@ typedef struct {
 	gchar *handlerextra;
 	/* ignore compatible check */
 	gboolean ignore_compatible;
+	/* skip bundle verification */
+	gboolean bundle_nosig;
 
 	/* for storing installation runtime informations */
 	RContextInstallationInfo *install_info;
diff --git a/src/bundle.c b/src/bundle.c
index 9ca512c0ef2512a9e011f638edba762c4f2ff87b..864e8bad283699d039f9b60762283aadc11875d4 100644
--- a/src/bundle.c
+++ b/src/bundle.c
@@ -318,6 +318,8 @@ static gboolean input_stream_read_bytes_all(GInputStream *stream,
 	return TRUE;
 }
 
+/* Appends signature to specified bundle file.
+ * Signature format: N bytes followed by uint64(N) */
 static gboolean sign_bundle(const gchar *bundlename, GError **error)
 {
 	GError *ierror = NULL;
@@ -326,22 +328,7 @@ static gboolean sign_bundle(const gchar *bundlename, GError **error)
 	g_autoptr(GFileOutputStream) bundlestream = NULL;
 	guint64 offset;
 
-	g_assert_nonnull(r_context()->certpath);
-	g_assert_nonnull(r_context()->keypath);
-
-	sig = cms_sign_file(bundlename,
-			r_context()->certpath,
-			r_context()->keypath,
-			r_context()->intermediatepaths,
-			&ierror);
-	if (sig == NULL) {
-		g_propagate_prefixed_error(
-				error,
-				ierror,
-				"failed signing bundle: ");
-		return FALSE;
-	}
-
+	/* open file and seek to end */
 	bundlefile = g_file_new_for_path(bundlename);
 	bundlestream = g_file_append_to(bundlefile, G_FILE_CREATE_NONE, NULL, &ierror);
 	if (bundlestream == NULL) {
@@ -361,17 +348,40 @@ static gboolean sign_bundle(const gchar *bundlename, GError **error)
 		return FALSE;
 	}
 
+	/* get current file size */
 	offset = g_seekable_tell((GSeekable *)bundlestream);
-	if (!output_stream_write_bytes_all((GOutputStream *)bundlestream, sig, NULL, &ierror)) {
-		g_propagate_prefixed_error(
-				error,
-				ierror,
-				"failed to append signature to bundle: ");
-		return FALSE;
-	}
 
+	/* append signature bytes, if not disabled */
+	if (!r_context()->bundle_nosig) {
+		g_assert_nonnull(r_context()->certpath);
+		g_assert_nonnull(r_context()->keypath);
+
+		sig = cms_sign_file(bundlename,
+				r_context()->certpath,
+				r_context()->keypath,
+				r_context()->intermediatepaths,
+				&ierror);
+		if (sig == NULL) {
+			g_propagate_prefixed_error(
+					error,
+					ierror,
+					"failed signing bundle: ");
+			return FALSE;
+		}
 
+		if (!output_stream_write_bytes_all((GOutputStream *)bundlestream, sig, NULL, &ierror)) {
+			g_propagate_prefixed_error(
+					error,
+					ierror,
+					"failed to append signature to bundle: ");
+			return FALSE;
+		}
+	}
+
+	/* get file size size difference */
 	offset = g_seekable_tell((GSeekable *)bundlestream) - offset;
+
+	/* append signature size */
 	if (!output_stream_write_uint64_all((GOutputStream *)bundlestream, offset, NULL, &ierror)) {
 		g_propagate_prefixed_error(
 				error,
@@ -750,7 +760,7 @@ gboolean check_bundle(const gchar *bundlename, RaucBundle **bundle, gboolean ver
 		goto out;
 	}
 
-	if (sigsize == 0) {
+	if (verify && sigsize == 0) {
 		g_set_error(error, R_BUNDLE_ERROR, R_BUNDLE_ERROR_SIGNATURE,
 				"Signature size is 0");
 		res = FALSE;
@@ -785,17 +795,17 @@ gboolean check_bundle(const gchar *bundlename, RaucBundle **bundle, gboolean ver
 		goto out;
 	}
 
-	res = input_stream_read_bytes_all(G_INPUT_STREAM(bundlestream),
-			&sig, sigsize, NULL, &ierror);
-	if (!res) {
-		g_propagate_prefixed_error(
-				error,
-				ierror,
-				"Failed to read signature from bundle: ");
-		goto out;
-	}
-
 	if (verify) {
+		res = input_stream_read_bytes_all(G_INPUT_STREAM(bundlestream),
+				&sig, sigsize, NULL, &ierror);
+		if (!res) {
+			g_propagate_prefixed_error(
+					error,
+					ierror,
+					"Failed to read signature from bundle: ");
+			goto out;
+		}
+
 		CMS_ContentInfo *cms = NULL;
 		X509_STORE *store = NULL;
 
diff --git a/src/install.c b/src/install.c
index 6f535f7c83c3f01564e9b5fcd4a7f790fb40c185..3665b49c6bedaca6ee53e901cc6a4281a561785c 100644
--- a/src/install.c
+++ b/src/install.c
@@ -1283,7 +1283,7 @@ gboolean do_install_bundle(RaucInstallArgs *args, GError **error)
 	// TODO: mount info in context ?
 	install_args_update(args, "Checking and mounting bundle...");
 
-	res = check_bundle(bundlefile, &bundle, TRUE, &ierror);
+	res = check_bundle(bundlefile, &bundle, !r_context()->bundle_nosig, &ierror);
 	if (!res) {
 		g_propagate_error(error, ierror);
 		goto out;
diff --git a/src/main.c b/src/main.c
index 5f0ce56ec2be5f1e2ac4137d34d9a53a3df12865..bc1c7fc16672e145d42b00450b9fb04105df9741 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,7 +23,8 @@ GMainLoop *r_loop = NULL;
 int r_exit_status = 0;
 
 gboolean install_ignore_compatible = FALSE;
-gboolean info_noverify, info_dumpcert = FALSE;
+gboolean bundle_nosig = FALSE;
+gboolean info_dumpcert = FALSE;
 gboolean status_detailed = FALSE;
 gchar *output_format = NULL;
 
@@ -176,6 +177,7 @@ static gboolean install_start(int argc, char **argv)
 	args->status_result = 2;
 
 	r_context_conf()->ignore_compatible = install_ignore_compatible;
+	r_context_conf()->bundle_nosig = bundle_nosig;
 
 	r_loop = g_main_loop_new(NULL, FALSE);
 	if (ENABLE_SERVICE) {
@@ -261,11 +263,13 @@ static gboolean bundle_start(int argc, char **argv)
 		goto out;
 	}
 
-	if (r_context()->certpath == NULL ||
-	    r_context()->keypath == NULL) {
-		g_printerr("Cert and key files must be provided\n");
-		r_exit_status = 1;
-		goto out;
+	if (!r_context()->bundle_nosig) {
+		if (r_context()->certpath == NULL ||
+		    r_context()->keypath == NULL) {
+			g_printerr("Cert and key files must be provided\n");
+			r_exit_status = 1;
+			goto out;
+		}
 	}
 
 	g_debug("input directory: %s", argv[2]);
@@ -409,7 +413,7 @@ static gboolean resign_start(int argc, char **argv)
 		goto out;
 	}
 
-	if (!check_bundle(argv[2], &bundle, TRUE, &ierror)) {
+	if (!check_bundle(argv[2], &bundle, !bundle_nosig, &ierror)) {
 		g_printerr("%s\n", ierror->message);
 		g_clear_error(&ierror);
 		r_exit_status = 1;
@@ -454,7 +458,7 @@ static gboolean extract_start(int argc, char **argv)
 	g_debug("input bundle: %s", argv[2]);
 	g_debug("output dir: %s", argv[3]);
 
-	if (!check_bundle(argv[2], &bundle, TRUE, &ierror)) {
+	if (!check_bundle(argv[2], &bundle, !bundle_nosig, &ierror)) {
 		g_printerr("%s\n", ierror->message);
 		g_clear_error(&ierror);
 		r_exit_status = 1;
@@ -506,7 +510,7 @@ static gboolean convert_start(int argc, char **argv)
 	g_debug("input bundle: %s", argv[2]);
 	g_debug("output bundle: %s", argv[3]);
 
-	if (!check_bundle(argv[2], &bundle, TRUE, &ierror)) {
+	if (!check_bundle(argv[2], &bundle, !bundle_nosig, &ierror)) {
 		g_printerr("%s\n", ierror->message);
 		g_clear_error(&ierror);
 		r_exit_status = 1;
@@ -865,7 +869,7 @@ static gboolean info_start(int argc, char **argv)
 		goto out;
 	g_debug("input bundle: %s", bundlelocation);
 
-	res = check_bundle(bundlelocation, &bundle, !info_noverify, &error);
+	res = check_bundle(bundlelocation, &bundle, !bundle_nosig, &error);
 	if (!res) {
 		g_printerr("%s\n", error->message);
 		g_clear_error(&error);
@@ -1611,7 +1615,6 @@ GOptionEntry entries_install[] = {
 };
 
 GOptionEntry entries_info[] = {
-	{"no-verify", '\0', 0, G_OPTION_ARG_NONE, &info_noverify, "disable bundle verification", NULL},
 	{"output-format", '\0', 0, G_OPTION_ARG_STRING, &output_format, "output format", "FORMAT"},
 	{"dump-cert", '\0', 0, G_OPTION_ARG_NONE, &info_dumpcert, "dump certificate", NULL},
 	{0}
@@ -1639,6 +1642,7 @@ static void cmdline_handler(int argc, char **argv)
 		{"mount", '\0', 0, G_OPTION_ARG_FILENAME, &mount, "mount prefix", "PATH"},
 		{"override-boot-slot", '\0', 0, G_OPTION_ARG_STRING, &bootslot, "override auto-detection of booted slot", "BOOTNAME"},
 		{"handler-args", '\0', 0, G_OPTION_ARG_STRING, &handlerextra, "extra handler arguments", "ARGS"},
+	        {"no-signatures", '\0', 0, G_OPTION_ARG_NONE, &bundle_nosig, "disable bundle verification", NULL},
 		{"debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "enable debug output", NULL},
 		{"version", '\0', 0, G_OPTION_ARG_NONE, &version, "display version", NULL},
 		{"help", 'h', 0, G_OPTION_ARG_NONE, &help, NULL, NULL},
@@ -1796,6 +1800,7 @@ static void cmdline_handler(int argc, char **argv)
 			r_context_conf()->bootslot = bootslot;
 		if (handlerextra)
 			r_context_conf()->handlerextra = handlerextra;
+		r_context_conf()->bundle_nosig = bundle_nosig;
 	} else {
 		if (confpath != NULL ||
 		    certpath != NULL ||
diff --git a/src/update_handler.c b/src/update_handler.c
index 95ca24b9d7fd57a3dbd4f7f5568c09deae8c1bed..024f5e58b197270f213cee995b2efa03a39a84bf 100644
--- a/src/update_handler.c
+++ b/src/update_handler.c
@@ -632,7 +632,7 @@ static gboolean run_slot_hook(const gchar *hook_name, const gchar *hook_cmd, Rau
 	}
 	g_subprocess_launcher_setenv(launcher, "RAUC_MOUNT_PREFIX", r_context()->config->mount_prefix, TRUE);
 
-	if (r_context()->install_info->mounted_bundle) {
+	if (r_context()->install_info->mounted_bundle && r_context()->install_info->mounted_bundle->verified_chain) {
 		gchar **hashes = NULL;
 		gchar *string = NULL;
 
-- 
2.21.0


{'fraction_non_alphanumeric': 0.1652338811630847, 'fraction_numerical': 0.0511378002528445, 'mean_word_length': 4.423723003085362, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1223937', 'n_tokens_mistral': 6849, 'n_tokens_neox': 6077, 'n_words': 1298}