Add digest sub command
Subsystems (2)
PATCH 1/1 Add digest sub command
From: Luca Boccassi <[email protected]>
Add a digest sub command that prints a hex-encoded digest of a file, ready to be signed offline (ie: includes the full data that is expected by the kernel - magic string, digest algorithm and size).
Useful in case the integrated signing mechanism with local cert/key cannot be used.
--- Makefile | 3 + README.md | 4 ++ programs/cmd_digest.c | 137 ++++++++++++++++++++++++++++++++++++++++++ programs/cmd_sign.c | 8 --- programs/fsverity.c | 8 +++ programs/fsverity.h | 4 ++ programs/utils.c | 8 +++ programs/utils.h | 1 + 8 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 programs/cmd_digest.c
Makefile +3 -0
@@ -127,6 +127,7 @@ ALL_PROG_HEADERS := $(wildcard programs/*.h) $(COMMON_HEADERS) PROG_COMMON_SRC := programs/utils.c PROG_COMMON_OBJ := $(PROG_COMMON_SRC:.c=.o) FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ) \ + programs/cmd_digest.o \ programs/cmd_enable.o \ programs/cmd_measure.o \ programs/cmd_sign.o \
@@ -181,6 +182,8 @@ check:fsverity test_programs $(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \ --block-size=512 --salt=12345678 \ --key=testdata/key.pem --cert=testdata/cert.pem > /dev/null + $(RUN_FSVERITY) digest fsverity --hash=sha512 \ + --block-size=512 --salt=12345678 > /dev/null rm -f fsverity.sig @echo "All tests passed!"
README.md +4 -0
@@ -112,6 +112,10 @@ the set of X.509 certificates that have been loaded into the fsverity enable file --signature=file.sig rm -f file.sig sha256sum file + + # The digest to be signed can also be printed separately, hex + # encoded, in case the integrated signing cannot be used: + fsverity digest file --compact | xxd -p -r | openssl smime -sign -in /dev/stdin ... ``` By default, it's not required that verity files have a signature.
programs/cmd_digest.c new file +137 -0
137 lines not on this page · raw message
programs/cmd_sign.c +0 -8
@@ -43,14 +43,6 @@ static const struct option longopts[] = { {NULL, 0, NULL, 0} }; -static int read_callback (void *file,void *buf,size_t count) -{ - errno =0 ; -if (!full_read (file, buf, count)) -return errno ? -errno : -EIO ; -return 0 ; -} -/* Sign a file for fs-verity by computing its measurement, then signing it. */ int fsverity_cmd_sign (const struct fsverity_command *cmd,int argc,char *argv[])
programs/fsverity.c +8 -0
@@ -21,6 +21,14 @@ static const struct fsverity_command {const char *usage_str; } fsverity_commands[] = { { + .name = "digest", + .func = fsverity_cmd_digest, + .short_desc = "Compute and print hex-encoded fs-verity digest of a file, for offline signing", + .usage_str = +" fsverity digest FILE\n" +" [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n" +" [--compact]\n" + }, { .name = "enable", .func = fsverity_cmd_enable, .short_desc = "Enable fs-verity on a file",
programs/fsverity.h +4 -0
@@ -25,6 +25,10 @@struct fsverity_command ; +/* cmd_digest.c */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]); +/* cmd_enable.c */ int fsverity_cmd_enable (const struct fsverity_command *cmd,int argc,char *argv[]);
programs/utils.c +8 -0
@@ -175,6 +175,14 @@ bool filedes_close(struct filedes *file)return res ==0 ; } +int read_callback (void *file,void *buf,size_t count) +{ + errno =0 ; +if (!full_read (file, buf, count)) +return errno ? -errno : -EIO ; +return 0 ; +} +/* ========== String utilities ========== */ static int hex2bin_char (char c)
programs/utils.h +1 -0
@@ -43,6 +43,7 @@ bool get_file_size(struct filedes *file, u64 *size_ret);bool full_read (struct filedes *file,void *buf,size_t count);bool full_write (struct filedes *file,const void *buf,size_t count);bool filedes_close (struct filedes *file); +int read_callback (void *file,void *buf,size_t count);bool hex2bin (const char *hex,u8 *bin,size_t bin_len);void bin2hex (const u8 *bin,size_t bin_len,char *hex);
Signature · 1 line
2.20.1
Eric Biggers
maintainerQuoted 72 lines · On Thu, Oct 22, 2020 at 06:21:55PM +0100, [email protected] wrote:
programs/cmd_digest.c new file +72
-0+/* Compute a file's fs-verity measurement, then print it in hex format. */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]) +{ +struct filedes file = { .fd =-1 }; +u8 *salt =NULL ; +struct libfsverity_merkle_tree_params tree_params = { .version =1 }; +struct libfsverity_digest *digest =NULL ; +struct fsverity_signed_digest *d =NULL ; +char digest_hex[FS_VERITY_MAX_DIGEST_SIZE *2 +sizeof (struct fsverity_signed_digest ) *2 +1 ]; +bool compact = false; +int status; +int c; + +while ((c =getopt_long (argc, argv,"" , longopts,NULL )) !=-1 ) { +switch (c) { +case OPT_HASH_ALG : +if (!parse_hash_alg_option (optarg, + &tree_params.hash_algorithm)) + goto out_usage; +break ; +case OPT_BLOCK_SIZE : +if (!parse_block_size_option (optarg, + &tree_params.block_size)) + goto out_usage; +break ; +case OPT_SALT : +if (!parse_salt_option (optarg, &salt, + &tree_params.salt_size)) + goto out_usage; + tree_params.salt = salt; +break ; +case OPT_COMPACT : + compact = true; +break ; +default : + goto out_usage; + } + } + + argv += optind; + argc -= optind; + +if (argc !=1 ) + goto out_usage; + +if (tree_params.hash_algorithm ==0 ) + tree_params.hash_algorithm =FS_VERITY_HASH_ALG_DEFAULT ; + +if (tree_params.block_size ==0 ) + tree_params.block_size =get_default_block_size (); + +if (!open_file (&file, argv[0 ],O_RDONLY ,0 )) + goto out_err; + +if (!get_file_size (&file, &tree_params.file_size)) + goto out_err; + +if (libfsverity_compute_digest (&file, read_callback, + &tree_params, &digest) !=0 ) { +error_msg ("failed to compute digest" ); + goto out_err; + } + +ASSERT (digest->digest_size <=FS_VERITY_MAX_DIGEST_SIZE ); + + d =xzalloc (sizeof (*d) + digest->digest_size); +if (!d) + goto out_err; +memcpy (d->magic,"FSVerity" ,8 ); + d->digest_algorithm =cpu_to_le16 (digest->digest_algorithm); + d->digest_size =cpu_to_le16 (digest->digest_size);
programs/cmd_digest.c line 120
Quoted 10 lines
programs/cmd_digest.c new file +10
-0+memcpy (d->digest, digest->digest, digest->digest_size); + +bin2hex ((const u8 *)d,sizeof (*d) + digest->digest_size, digest_hex); + +if (compact) +printf ("%s" , digest_hex); +else +printf ("File '%s' (%s:%s)\n" , argv[0 ], +libfsverity_get_hash_name (tree_params.hash_algorithm), + digest_hex);
Can you make this command format its output in the same way as 'fsverity measure' by default, and put the 'struct fsverity_signed_digest' formatted output behind an option, like --for-builtin-sig?
The 'struct fsverity_signed_digest' is specific to the builtin (in-kernel) signature support, which isn't the only way to use fs-verity. The signature verification can also be done in userspace, which is more flexible. (And you should consider doing it that way, if you haven't already. I'm not sure exactly what your use case is.)
So when possible, I'd like to have the default be the basic fs-verity feature. If someone then specifically wants to use the builtin signature support on top of that, as opposed to using fs-verity in another way, then they can provide the option they need to do that.
Separately, it would also be nice to share more code with cmd_sign.c, as they both have to parse a lot of the same options. Maybe it doesn't work out, though.
- Eric
Quoted 79 lines · Eric Biggers
Quoted 78 lines · On Thu, Oct 22, 2020 at 06:21:55PM +0100, [email protected] wrote:
programs/cmd_digest.c new file +78
-0+/* Compute a file's fs-verity measurement, then print it in hex format. */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]) +{ +struct filedes file = { .fd =-1 }; +u8 *salt =NULL ; +struct libfsverity_merkle_tree_params tree_params = { .version =1 }; +struct libfsverity_digest *digest =NULL ; +struct fsverity_signed_digest *d =NULL ; +char digest_hex[FS_VERITY_MAX_DIGEST_SIZE *2 +sizeof (struct fsverity_signed_digest ) *2 +1 ]; +bool compact = false; +int status; +int c; + +while ((c =getopt_long (argc, argv,"" , longopts,NULL )) !=-1 ) { +switch (c) { +case OPT_HASH_ALG : +if (!parse_hash_alg_option (optarg, + &tree_params.hash_algorithm)) + goto out_usage; +break ; +case OPT_BLOCK_SIZE : +if (!parse_block_size_option (optarg, + &tree_params.block_size)) + goto out_usage; +break ; +case OPT_SALT : +if (!parse_salt_option (optarg, &salt, + &tree_params.salt_size)) + goto out_usage; + tree_params.salt = salt; +break ; +case OPT_COMPACT : + compact = true; +break ; +default : + goto out_usage; + } + } + + argv += optind; + argc -= optind; + +if (argc !=1 ) + goto out_usage; + +if (tree_params.hash_algorithm ==0 ) + tree_params.hash_algorithm =FS_VERITY_HASH_ALG_DEFAULT ; + +if (tree_params.block_size ==0 ) + tree_params.block_size =get_default_block_size (); + +if (!open_file (&file, argv[0 ],O_RDONLY ,0 )) + goto out_err; + +if (!get_file_size (&file, &tree_params.file_size)) + goto out_err; + +if (libfsverity_compute_digest (&file, read_callback, + &tree_params, &digest) !=0 ) { +error_msg ("failed to compute digest" ); + goto out_err; + } + +ASSERT (digest->digest_size <=FS_VERITY_MAX_DIGEST_SIZE ); + + d =xzalloc (sizeof (*d) + digest->digest_size); +if (!d) + goto out_err; +memcpy (d->magic,"FSVerity" ,8 ); + d->digest_algorithm =cpu_to_le16 (digest->digest_algorithm); + d->digest_size =cpu_to_le16 (digest->digest_size); +memcpy (d->digest, digest->digest, digest->digest_size); + +bin2hex ((const u8 *)d,sizeof (*d) + digest->digest_size, digest_hex); + +if (compact) +printf ("%s" , digest_hex);
Quoted 8 lines
programs/cmd_digest.c line 120
Quoted 4 lines
programs/cmd_digest.c new file +4
-0+else +printf ("File '%s' (%s:%s)\n" , argv[0 ], +libfsverity_get_hash_name (tree_params.hash_algorithm), + digest_hex);Can you make this command format its output in the same way as 'fsverity measure' by default, and put the 'struct
fsverity_signed_digest' formatted output behind an option, like --for-builtin-sig?
Sure, done in v2.
Quoted 5 lines
The 'struct
fsverity_signed_digest' is specific to the builtin (in-kernel) signature support, which isn't the only way to use fs-verity. The signature verification can also be done in userspace, which is more flexible. (And you should consider doing it that way, if you haven't already. I'm not sure exactly what your use case is.)
Our use case is to ultimately add kernel-side policy enforcement of integrity via the IPE LSM: https://microsoft.github.io/ipe/ so we can't do without kernel verification. I need the new command as I've got some teams building and signing on Windows, using whatever native API is available there, so can't use the 'fsverity sign' command.
Quoted 8 lines
So when possible, I'd like to have the default be the basic fs-verity feature. If someone then specifically wants to use the builtin signature support on top of that, as opposed to using fs-verity in another way, then they can provide the option they need to do that.
Separately, it would also be nice to share more code with
cmd_sign.c, as they both have to parse a lot of the same options. Maybe it doesn't work out, though.
I did have a look at this at the beginning, but I was not happy with how it looked like - both are using APIs from the public library and are compact enough, so it felt a bit awkward to add yet another shim layer, and opted to avoid it. If you feel strongly about it let me know and I can revise.
Signature · 2 lines
Kind regards, Luca Boccassi
PATCH v2 1/1 Add digest sub command
In reply to [email protected]From: Luca Boccassi <[email protected]>
Add a digest sub command that prints a hex-encoded digest of a file, ready to be signed offline (ie: includes the full data that is expected by the kernel - magic string, digest algorithm and size).
Useful in case the integrated signing mechanism with local cert/key cannot be used.
---
v2: add --for-builtin-sig and default to printing only the digest,
without the extra values the kernel expects
Makefile | 3 + README.md | 4 ++ programs/cmd_digest.c | 146 ++++++++++++++++++++++++++++++++++++++++++ programs/cmd_sign.c | 8 --- programs/fsverity.c | 8 +++ programs/fsverity.h | 4 ++ programs/utils.c | 8 +++ programs/utils.h | 1 + 8 files changed, 174 insertions(+), 8 deletions(-) create mode 100644 programs/cmd_digest.c
Makefile +3 -0
@@ -127,6 +127,7 @@ ALL_PROG_HEADERS := $(wildcard programs/*.h) $(COMMON_HEADERS) PROG_COMMON_SRC := programs/utils.c PROG_COMMON_OBJ := $(PROG_COMMON_SRC:.c=.o) FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ) \ + programs/cmd_digest.o \ programs/cmd_enable.o \ programs/cmd_measure.o \ programs/cmd_sign.o \
@@ -181,6 +182,8 @@ check:fsverity test_programs $(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \ --block-size=512 --salt=12345678 \ --key=testdata/key.pem --cert=testdata/cert.pem > /dev/null + $(RUN_FSVERITY) digest fsverity --hash=sha512 \ + --block-size=512 --salt=12345678 > /dev/null rm -f fsverity.sig @echo "All tests passed!"
README.md +4 -0
@@ -112,6 +112,10 @@ the set of X.509 certificates that have been loaded into the fsverity enable file --signature=file.sig rm -f file.sig sha256sum file + + # The digest to be signed can also be printed separately, hex + # encoded, in case the integrated signing cannot be used: + fsverity digest file --compact --for-builtin-sig | xxd -p -r | openssl smime -sign -in /dev/stdin ... ``` By default, it's not required that verity files have a signature.
programs/cmd_digest.c new file +146 -0
146 lines not on this page · raw message
programs/cmd_sign.c +0 -8
@@ -43,14 +43,6 @@ static const struct option longopts[] = { {NULL, 0, NULL, 0} }; -static int read_callback (void *file,void *buf,size_t count) -{ - errno =0 ; -if (!full_read (file, buf, count)) -return errno ? -errno : -EIO ; -return 0 ; -} -/* Sign a file for fs-verity by computing its measurement, then signing it. */ int fsverity_cmd_sign (const struct fsverity_command *cmd,int argc,char *argv[])
programs/fsverity.c +8 -0
@@ -21,6 +21,14 @@ static const struct fsverity_command {const char *usage_str; } fsverity_commands[] = { { + .name = "digest", + .func = fsverity_cmd_digest, + .short_desc = "Compute and print hex-encoded fs-verity digest of a file, for offline signing", + .usage_str = +" fsverity digest FILE\n" +" [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n" +" [--compact] [--for-builtin-sig]\n" + }, { .name = "enable", .func = fsverity_cmd_enable, .short_desc = "Enable fs-verity on a file",
programs/fsverity.h +4 -0
@@ -25,6 +25,10 @@struct fsverity_command ; +/* cmd_digest.c */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]); +/* cmd_enable.c */ int fsverity_cmd_enable (const struct fsverity_command *cmd,int argc,char *argv[]);
programs/utils.c +8 -0
@@ -175,6 +175,14 @@ bool filedes_close(struct filedes *file)return res ==0 ; } +int read_callback (void *file,void *buf,size_t count) +{ + errno =0 ; +if (!full_read (file, buf, count)) +return errno ? -errno : -EIO ; +return 0 ; +} +/* ========== String utilities ========== */ static int hex2bin_char (char c)
programs/utils.h +1 -0
@@ -43,6 +43,7 @@ bool get_file_size(struct filedes *file, u64 *size_ret);bool full_read (struct filedes *file,void *buf,size_t count);bool full_write (struct filedes *file,const void *buf,size_t count);bool filedes_close (struct filedes *file); +int read_callback (void *file,void *buf,size_t count);bool hex2bin (const char *hex,u8 *bin,size_t bin_len);void bin2hex (const u8 *bin,size_t bin_len,char *hex);
Signature · 1 line
2.20.1
Eric Biggers
maintainerQuoted 38 lines · On Mon, Oct 26, 2020 at 11:40:07AM +0000, [email protected] wrote:
programs/cmd_digest.c new file +38
-0+/* Compute a file's fs-verity measurement, then print it in hex format. */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]) +{ +struct filedes file = { .fd =-1 }; +u8 *salt =NULL ; +struct libfsverity_merkle_tree_params tree_params = { .version =1 }; +struct libfsverity_digest *digest =NULL ; +struct fsverity_signed_digest *d =NULL ; +char digest_hex[FS_VERITY_MAX_DIGEST_SIZE *2 +sizeof (struct fsverity_signed_digest ) *2 +1 ]; +bool compact = false, for_builtin_sig = false; +int status; +int c; + +while ((c =getopt_long (argc, argv,"" , longopts,NULL )) !=-1 ) { +switch (c) { +case OPT_HASH_ALG : +if (!parse_hash_alg_option (optarg, + &tree_params.hash_algorithm)) + goto out_usage; +break ; +case OPT_BLOCK_SIZE : +if (!parse_block_size_option (optarg, + &tree_params.block_size)) + goto out_usage; +break ; +case OPT_SALT : +if (!parse_salt_option (optarg, &salt, + &tree_params.salt_size)) + goto out_usage; + tree_params.salt = salt; +break ; +case OPT_COMPACT : + compact = true; +break ; +case OPT_FOR_BUILTIN_SIG : + for_builtin_sig = true; +break ;
programs/cmd_digest.c line 88
Quoted 10 lines
programs/cmd_digest.c new file +10
-0+default : + goto out_usage; + } + } + + argv += optind; + argc -= optind; + +if (argc !=1 ) + goto out_usage;
I think this should allow specifying multiple files, like 'fsverity measure' does. 'fsverity measure' is intended to behave like the sha256sum program.
programs/cmd_digest.c line 114
Quoted 5 lines
programs/cmd_digest.c new file +5
-0+/* The kernel expects more than the digest as the signed payload */ +if (for_builtin_sig) { + d =xzalloc (sizeof (*d) + digest->digest_size); +if (!d) + goto out_err;
No need to check the return value of xzalloc(), since it exits on error.
programs/cmd_digest.c line 129
Quoted 6 lines
programs/cmd_digest.c new file +6
-0+if (compact) +printf ("%s" , digest_hex); +else +printf ("File '%s' (%s:%s)\n" , argv[0 ], +libfsverity_get_hash_name (tree_params.hash_algorithm), + digest_hex);
Please make the output in the !compact case match 'fsverity measure':
printf("%s:%s %s\n",
libfsverity_get_hash_name(tree_params.hash_algorithm),
digest_hex, argv[i]);- Eric
Quoted 44 lines · Eric Biggers
Quoted 43 lines · On Mon, Oct 26, 2020 at 11:40:07AM +0000, [email protected] wrote:
programs/cmd_digest.c new file +43
-0+/* Compute a file's fs-verity measurement, then print it in hex format. */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]) +{ +struct filedes file = { .fd =-1 }; +u8 *salt =NULL ; +struct libfsverity_merkle_tree_params tree_params = { .version =1 }; +struct libfsverity_digest *digest =NULL ; +struct fsverity_signed_digest *d =NULL ; +char digest_hex[FS_VERITY_MAX_DIGEST_SIZE *2 +sizeof (struct fsverity_signed_digest ) *2 +1 ]; +bool compact = false, for_builtin_sig = false; +int status; +int c; + +while ((c =getopt_long (argc, argv,"" , longopts,NULL )) !=-1 ) { +switch (c) { +case OPT_HASH_ALG : +if (!parse_hash_alg_option (optarg, + &tree_params.hash_algorithm)) + goto out_usage; +break ; +case OPT_BLOCK_SIZE : +if (!parse_block_size_option (optarg, + &tree_params.block_size)) + goto out_usage; +break ; +case OPT_SALT : +if (!parse_salt_option (optarg, &salt, + &tree_params.salt_size)) + goto out_usage; + tree_params.salt = salt; +break ; +case OPT_COMPACT : + compact = true; +break ; +case OPT_FOR_BUILTIN_SIG : + for_builtin_sig = true; +break ; +default : + goto out_usage; + } + } +
Quoted 8 lines
programs/cmd_digest.c line 88
Quoted 5 lines
programs/cmd_digest.c new file +5
-0+ argv += optind; + argc -= optind; + +if (argc !=1 ) + goto out_usage;I think this should allow specifying multiple files, like 'fsverity measure' does. 'fsverity measure' is intended to behave like the sha256sum program.
Added in v3.
Quoted 7 lines
programs/cmd_digest.c line 114
Quoted 5 lines
programs/cmd_digest.c new file +5
-0+/* The kernel expects more than the digest as the signed payload */ +if (for_builtin_sig) { + d =xzalloc (sizeof (*d) + digest->digest_size); +if (!d) + goto out_err;No need to check the return value of
xzalloc(), since it exits on error.
Removed in v3.
Quoted 9 lines
programs/cmd_digest.c line 129
Quoted 6 lines
programs/cmd_digest.c new file +6
-0+if (compact) +printf ("%s" , digest_hex); +else +printf ("File '%s' (%s:%s)\n" , argv[0 ], +libfsverity_get_hash_name (tree_params.hash_algorithm), + digest_hex);Please make the output in the !compact case match 'fsverity measure':
Quoted 5 lines
printf("%s:%s %s\n", libfsverity_get_hash_name(tree_params.hash_algorithm), digest_hex, argv[i]);- Eric
Done with v3, sorry got confused with the 'sign' output.
Signature · 2 lines
Kind regards, Luca Boccassi
PATCH v3 1/1 Add digest sub command
In reply to [email protected]From: Luca Boccassi <[email protected]>
Add a digest sub command that prints a hex-encoded digest of a file, ready to be signed offline (ie: includes the full data that is expected by the kernel - magic string, digest algorithm and size).
Useful in case the integrated signing mechanism with local cert/key cannot be used.
---
v2: add --for-builtin-sig and default to printing only the digest,
without the extra values the kernel expects
v3: change output to match the measure command
support multiple files as input
Makefile | 3 + README.md | 4 ++ programs/cmd_digest.c | 146 ++++++++++++++++++++++++++++++++++++++++++ programs/cmd_sign.c | 8 --- programs/fsverity.c | 8 +++ programs/fsverity.h | 4 ++ programs/utils.c | 8 +++ programs/utils.h | 1 + 8 files changed, 174 insertions(+), 8 deletions(-) create mode 100644 programs/cmd_digest.c
Makefile +3 -0
@@ -150,6 +150,7 @@ ALL_PROG_HEADERS := $(wildcard programs/*.h) $(COMMON_HEADERS) PROG_COMMON_SRC := programs/utils.c PROG_COMMON_OBJ := $(PROG_COMMON_SRC:.c=.o) FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ) \ + programs/cmd_digest.o \ programs/cmd_enable.o \ programs/cmd_measure.o \ programs/cmd_sign.o \
@@ -204,6 +205,8 @@ check:fsverity test_programs $(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \ --block-size=512 --salt=12345678 \ --key=testdata/key.pem --cert=testdata/cert.pem > /dev/null + $(RUN_FSVERITY) digest fsverity --hash=sha512 \ + --block-size=512 --salt=12345678 > /dev/null rm -f fsverity.sig @echo "All tests passed!"
README.md +4 -0
@@ -112,6 +112,10 @@ the set of X.509 certificates that have been loaded into the fsverity enable file --signature=file.sig rm -f file.sig sha256sum file + + # The digest to be signed can also be printed separately, hex + # encoded, in case the integrated signing cannot be used: + fsverity digest file --compact --for-builtin-sig | tr -d '\n' | xxd -p -r | openssl smime -sign -in /dev/stdin ... ``` By default, it's not required that verity files have a signature.
programs/cmd_digest.c new file +146 -0
146 lines not on this page · raw message
programs/cmd_sign.c +0 -8
@@ -43,14 +43,6 @@ static const struct option longopts[] = { {NULL, 0, NULL, 0} }; -static int read_callback (void *file,void *buf,size_t count) -{ - errno =0 ; -if (!full_read (file, buf, count)) -return errno ? -errno : -EIO ; -return 0 ; -} -/* Sign a file for fs-verity by computing its measurement, then signing it. */ int fsverity_cmd_sign (const struct fsverity_command *cmd,int argc,char *argv[])
programs/fsverity.c +8 -0
@@ -21,6 +21,14 @@ static const struct fsverity_command {const char *usage_str; } fsverity_commands[] = { { + .name = "digest", + .func = fsverity_cmd_digest, + .short_desc = "Compute and print hex-encoded fs-verity digest of a file, for offline signing", + .usage_str = +" fsverity digest FILE...\n" +" [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n" +" [--compact] [--for-builtin-sig]\n" + }, { .name = "enable", .func = fsverity_cmd_enable, .short_desc = "Enable fs-verity on a file",
programs/fsverity.h +4 -0
@@ -25,6 +25,10 @@struct fsverity_command ; +/* cmd_digest.c */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]); +/* cmd_enable.c */ int fsverity_cmd_enable (const struct fsverity_command *cmd,int argc,char *argv[]);
programs/utils.c +8 -0
@@ -175,6 +175,14 @@ bool filedes_close(struct filedes *file)return res ==0 ; } +int read_callback (void *file,void *buf,size_t count) +{ + errno =0 ; +if (!full_read (file, buf, count)) +return errno ? -errno : -EIO ; +return 0 ; +} +/* ========== String utilities ========== */ static int hex2bin_char (char c)
programs/utils.h +1 -0
@@ -43,6 +43,7 @@ bool get_file_size(struct filedes *file, u64 *size_ret);bool full_read (struct filedes *file,void *buf,size_t count);bool full_write (struct filedes *file,const void *buf,size_t count);bool filedes_close (struct filedes *file); +int read_callback (void *file,void *buf,size_t count);bool hex2bin (const char *hex,u8 *bin,size_t bin_len);void bin2hex (const u8 *bin,size_t bin_len,char *hex);
Signature · 1 line
2.20.1
PATCH v4 1/1 Add digest sub command
From: Luca Boccassi <[email protected]>
Add a digest sub command that prints a hex-encoded digest of a file, ready to be signed offline (ie: includes the full data that is expected by the kernel - magic string, digest algorithm and size).
Useful in case the integrated signing mechanism with local cert/key cannot be used.
---
v2: add --for-builtin-sig and default to printing only the digest,
without the extra values the kernel expects
v3: change output to match the measure command
support multiple files as input
v4: fix cleanup issue introduced in v3
Makefile | 3 + README.md | 4 ++ programs/cmd_digest.c | 148 ++++++++++++++++++++++++++++++++++++++++++ programs/cmd_sign.c | 8 --- programs/fsverity.c | 8 +++ programs/fsverity.h | 4 ++ programs/utils.c | 8 +++ programs/utils.h | 1 + 8 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 programs/cmd_digest.c
Makefile +3 -0
@@ -150,6 +150,7 @@ ALL_PROG_HEADERS := $(wildcard programs/*.h) $(COMMON_HEADERS) PROG_COMMON_SRC := programs/utils.c PROG_COMMON_OBJ := $(PROG_COMMON_SRC:.c=.o) FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ) \ + programs/cmd_digest.o \ programs/cmd_enable.o \ programs/cmd_measure.o \ programs/cmd_sign.o \
@@ -204,6 +205,8 @@ check:fsverity test_programs $(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \ --block-size=512 --salt=12345678 \ --key=testdata/key.pem --cert=testdata/cert.pem > /dev/null + $(RUN_FSVERITY) digest fsverity --hash=sha512 \ + --block-size=512 --salt=12345678 > /dev/null rm -f fsverity.sig @echo "All tests passed!"
README.md +4 -0
@@ -112,6 +112,10 @@ the set of X.509 certificates that have been loaded into the fsverity enable file --signature=file.sig rm -f file.sig sha256sum file + + # The digest to be signed can also be printed separately, hex + # encoded, in case the integrated signing cannot be used: + fsverity digest file --compact --for-builtin-sig | tr -d '\n' | xxd -p -r | openssl smime -sign -in /dev/stdin ... ``` By default, it's not required that verity files have a signature.
programs/cmd_digest.c new file +148 -0
148 lines not on this page · raw message
programs/cmd_sign.c +0 -8
@@ -43,14 +43,6 @@ static const struct option longopts[] = { {NULL, 0, NULL, 0} }; -static int read_callback (void *file,void *buf,size_t count) -{ - errno =0 ; -if (!full_read (file, buf, count)) -return errno ? -errno : -EIO ; -return 0 ; -} -/* Sign a file for fs-verity by computing its measurement, then signing it. */ int fsverity_cmd_sign (const struct fsverity_command *cmd,int argc,char *argv[])
programs/fsverity.c +8 -0
@@ -21,6 +21,14 @@ static const struct fsverity_command {const char *usage_str; } fsverity_commands[] = { { + .name = "digest", + .func = fsverity_cmd_digest, + .short_desc = "Compute and print hex-encoded fs-verity digest of a file, for offline signing", + .usage_str = +" fsverity digest FILE...\n" +" [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n" +" [--compact] [--for-builtin-sig]\n" + }, { .name = "enable", .func = fsverity_cmd_enable, .short_desc = "Enable fs-verity on a file",
programs/fsverity.h +4 -0
@@ -25,6 +25,10 @@struct fsverity_command ; +/* cmd_digest.c */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]); +/* cmd_enable.c */ int fsverity_cmd_enable (const struct fsverity_command *cmd,int argc,char *argv[]);
programs/utils.c +8 -0
@@ -175,6 +175,14 @@ bool filedes_close(struct filedes *file)return res ==0 ; } +int read_callback (void *file,void *buf,size_t count) +{ + errno =0 ; +if (!full_read (file, buf, count)) +return errno ? -errno : -EIO ; +return 0 ; +} +/* ========== String utilities ========== */ static int hex2bin_char (char c)
programs/utils.h +1 -0
@@ -43,6 +43,7 @@ bool get_file_size(struct filedes *file, u64 *size_ret);bool full_read (struct filedes *file,void *buf,size_t count);bool full_write (struct filedes *file,const void *buf,size_t count);bool filedes_close (struct filedes *file); +int read_callback (void *file,void *buf,size_t count);bool hex2bin (const char *hex,u8 *bin,size_t bin_len);void bin2hex (const u8 *bin,size_t bin_len,char *hex);
Signature · 1 line
2.20.1
Eric Biggers
maintainerprograms/cmd_digest.c line 43
Quoted 3 lines · On Mon, Oct 26, 2020 at 06:17:29PM +0000, [email protected] wrote:
programs/cmd_digest.c new file +3
-0+/* Compute a file's fs-verity measurement, then print it in hex format. */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[])
Since it can be more than one file now:
/* Compute the fs-verity measurement of the given file(s), for offline signing */
programs/cmd_digest.c line 102
Quoted 11 lines
programs/cmd_digest.c new file +11
-0+for (int i =0 ; i < argc; i++) { +struct filedes file = { .fd =-1 }; +struct fsverity_signed_digest *d =NULL ; +struct libfsverity_digest *digest =NULL ; +char digest_hex[FS_VERITY_MAX_DIGEST_SIZE *2 +sizeof (struct fsverity_signed_digest ) *2 +1 ]; + +if (!open_file (&file, argv[i],O_RDONLY ,0 )) + goto out_err; + +if (!get_file_size (&file, &tree_params.file_size)) + goto out_err;
'file' doesn't get closed on error. Making it back to the outer scope would fix that.
programs/cmd_digest.c line 129
Quoted 6 lines
programs/cmd_digest.c new file +6
-0+if (compact) +printf ("%s\n" , digest_hex); +else +printf ("%s:%s %s\n" , +libfsverity_get_hash_name (tree_params.hash_algorithm), + digest_hex, argv[i]);
I don't think the hash algorithm should be printed in the '!compact && for_builtin_sig' case, since it's already included in the struct that gets hex-encoded. I.e.
else if (for_builtin_sig)
printf("%s %s\n", digest_hex, argv[i]);programs/fsverity.c static const struct fsverity_command { · line 26
Quoted 11 lines
programs/fsverity.c +3
-0@@ -21,6 +21,14 @@ static const struct fsverity_command {const char *usage_str; } fsverity_commands[] = { { + .name = "digest", + .func = fsverity_cmd_digest, + .short_desc = "Compute and print hex-encoded fs-verity digest of a file, for offline signing",
Likewise, since this can now accept multiple files:
"Compute the fs-verity measurement of the given file(s), for offline signing"
(I don't think that "printed as hex" needs to be explicitly mentioned here.)
- Eric
Luca Boccassi
AppliedQuoted 8 lines · Eric Biggers
programs/cmd_digest.c line 43
Quoted 3 lines · On Mon, Oct 26, 2020 at 06:17:29PM +0000, [email protected] wrote:
programs/cmd_digest.c new file +3
-0+/* Compute a file's fs-verity measurement, then print it in hex format. */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[])Since it can be more than one file now:
/* Compute the fs-verity measurement of the given file(s), for offline signing */
Applied in v5.
Quoted 6 lines
Quoted 6 lines
programs/cmd_digest.c new file +6
-0+for (int i =0 ; i < argc; i++) { +struct filedes file = { .fd =-1 }; +struct fsverity_signed_digest *d =NULL ; +struct libfsverity_digest *digest =NULL ; +char digest_hex[FS_VERITY_MAX_DIGEST_SIZE *2 +sizeof (struct fsverity_signed_digest ) *2 +1 ]; +
Quoted 8 lines
programs/cmd_digest.c line 102
Quoted 5 lines
programs/cmd_digest.c new file +5
-0+if (!open_file (&file, argv[i],O_RDONLY ,0 )) + goto out_err; + +if (!get_file_size (&file, &tree_params.file_size)) + goto out_err;'file' doesn't get closed on error. Making it back to the outer scope would fix that.
Added to out_err in v5 (I had ignored it as cmd_sign does currently).
Quoted 11 lines
programs/cmd_digest.c line 129
Quoted 6 lines
programs/cmd_digest.c new file +6
-0+if (compact) +printf ("%s\n" , digest_hex); +else +printf ("%s:%s %s\n" , +libfsverity_get_hash_name (tree_params.hash_algorithm), + digest_hex, argv[i]);I don't think the hash algorithm should be printed in the '!compact &&
for_builtin_sig' case, since it's already included in the struct that gets hex-encoded. I.e.
Quoted 2 lines
else if (for_builtin_sig)
printf("%s %s\n", digest_hex, argv[i]);Fixed in v5.
Quoted 14 lines
programs/fsverity.c static const struct fsverity_command { · line 26
Quoted 11 lines
programs/fsverity.c +3
-0@@ -21,6 +21,14 @@ static const struct fsverity_command {const char *usage_str; } fsverity_commands[] = { { + .name = "digest", + .func = fsverity_cmd_digest, + .short_desc = "Compute and print hex-encoded fs-verity digest of a file, for offline signing",Likewise, since this can now accept multiple files:
Quoted 3 lines
"Compute the fs-verity measurement of the given file(s), for offline signing"
(I don't think that "printed as hex" needs to be explicitly mentioned here.)
Done in v5.
Signature · 2 lines
Kind regards, Luca Boccassi
PATCH v5 1/1 Add digest sub command
In reply to [email protected]From: Luca Boccassi <[email protected]>
Add a digest sub command that prints a hex-encoded digest of a file, ready to be signed offline (ie: includes the full data that is expected by the kernel - magic string, digest algorithm and size).
Useful in case the integrated signing mechanism with local cert/key cannot be used.
---
v2: add --for-builtin-sig and default to printing only the digest,
without the extra values the kernel expects
v3: change output to match the measure command
support multiple files as input
v4: fix cleanup issue introduced in v3
v5: Adjust comments to mention multi-file support in input
Do not print the hash algo with --for-builtin-sig Close file handle on error
Makefile | 3 + README.md | 4 ++ programs/cmd_digest.c | 151 ++++++++++++++++++++++++++++++++++++++++++ programs/cmd_sign.c | 8 --- programs/fsverity.c | 8 +++ programs/fsverity.h | 4 ++ programs/utils.c | 8 +++ programs/utils.h | 1 + 8 files changed, 179 insertions(+), 8 deletions(-) create mode 100644 programs/cmd_digest.c
Makefile +3 -0
@@ -150,6 +150,7 @@ ALL_PROG_HEADERS := $(wildcard programs/*.h) $(COMMON_HEADERS) PROG_COMMON_SRC := programs/utils.c PROG_COMMON_OBJ := $(PROG_COMMON_SRC:.c=.o) FSVERITY_PROG_OBJ := $(PROG_COMMON_OBJ) \ + programs/cmd_digest.o \ programs/cmd_enable.o \ programs/cmd_measure.o \ programs/cmd_sign.o \
@@ -204,6 +205,8 @@ check:fsverity test_programs $(RUN_FSVERITY) sign fsverity fsverity.sig --hash=sha512 \ --block-size=512 --salt=12345678 \ --key=testdata/key.pem --cert=testdata/cert.pem > /dev/null + $(RUN_FSVERITY) digest fsverity --hash=sha512 \ + --block-size=512 --salt=12345678 > /dev/null rm -f fsverity.sig @echo "All tests passed!"
README.md +4 -0
@@ -112,6 +112,10 @@ the set of X.509 certificates that have been loaded into the fsverity enable file --signature=file.sig rm -f file.sig sha256sum file + + # The digest to be signed can also be printed separately, hex + # encoded, in case the integrated signing cannot be used: + fsverity digest file --compact --for-builtin-sig | tr -d '\n' | xxd -p -r | openssl smime -sign -in /dev/stdin ... ``` By default, it's not required that verity files have a signature.
programs/cmd_digest.c new file +151 -0
151 lines not on this page · raw message
programs/cmd_sign.c +0 -8
@@ -43,14 +43,6 @@ static const struct option longopts[] = { {NULL, 0, NULL, 0} }; -static int read_callback (void *file,void *buf,size_t count) -{ - errno =0 ; -if (!full_read (file, buf, count)) -return errno ? -errno : -EIO ; -return 0 ; -} -/* Sign a file for fs-verity by computing its measurement, then signing it. */ int fsverity_cmd_sign (const struct fsverity_command *cmd,int argc,char *argv[])
programs/fsverity.c +8 -0
@@ -21,6 +21,14 @@ static const struct fsverity_command {const char *usage_str; } fsverity_commands[] = { { + .name = "digest", + .func = fsverity_cmd_digest, + .short_desc = "Compute the fs-verity measurement of the given file(s), for offline signing", + .usage_str = +" fsverity digest FILE...\n" +" [--hash-alg=HASH_ALG] [--block-size=BLOCK_SIZE] [--salt=SALT]\n" +" [--compact] [--for-builtin-sig]\n" + }, { .name = "enable", .func = fsverity_cmd_enable, .short_desc = "Enable fs-verity on a file",
programs/fsverity.h +4 -0
@@ -25,6 +25,10 @@struct fsverity_command ; +/* cmd_digest.c */ +int fsverity_cmd_digest (const struct fsverity_command *cmd, +int argc,char *argv[]); +/* cmd_enable.c */ int fsverity_cmd_enable (const struct fsverity_command *cmd,int argc,char *argv[]);
programs/utils.c +8 -0
@@ -175,6 +175,14 @@ bool filedes_close(struct filedes *file)return res ==0 ; } +int read_callback (void *file,void *buf,size_t count) +{ + errno =0 ; +if (!full_read (file, buf, count)) +return errno ? -errno : -EIO ; +return 0 ; +} +/* ========== String utilities ========== */ static int hex2bin_char (char c)
programs/utils.h +1 -0
@@ -43,6 +43,7 @@ bool get_file_size(struct filedes *file, u64 *size_ret);bool full_read (struct filedes *file,void *buf,size_t count);bool full_write (struct filedes *file,const void *buf,size_t count);bool filedes_close (struct filedes *file); +int read_callback (void *file,void *buf,size_t count);bool hex2bin (const char *hex,u8 *bin,size_t bin_len);void bin2hex (const u8 *bin,size_t bin_len,char *hex);
Signature · 1 line
2.20.1
Eric Biggers
maintainerQuoted 11 lines · On Mon, Oct 26, 2020 at 07:18:39PM +0000, [email protected] wrote:
From: Luca Boccassi <[email protected]>
Add a digest sub command that prints a hex-encoded digest of a file, ready to be signed offline (ie: includes the full data that is expected by the kernel - magic string, digest algorithm and size).
Useful in case the integrated signing mechanism with local cert/key cannot be used.
Signed-off-by Luca Boccassi <[email protected]>
Applied with a few tweaks. Thanks!
- Eric