1 // SPDX-License-Identifier: MIT
2 /*
3 * The 'fsverity measure' command
4 *
5 * Copyright 2018 Google LLC
6 *
7 * Use of this source code is governed by an MIT-style
8 * license that can be found in the LICENSE file or at
9 * https://opensource.org/licenses/MIT.
10 */
11
12 #include "fsverity.h"
13
14 #include <fcntl.h>
15 #include <getopt.h>
16 #include <sys/ioctl.h>
17
18 static const struct option longopts[] = {
19 {NULL, 0, NULL, 0}
20 };
21
22 /* Display the fs-verity digest of the given verity file(s). */
fsverity_cmd_measure(const struct fsverity_command * cmd,int argc,char * argv[])23 int fsverity_cmd_measure(const struct fsverity_command *cmd,
24 int argc, char *argv[])
25 {
26 struct fsverity_digest *d = NULL;
27 struct filedes file;
28 char digest_hex[FS_VERITY_MAX_DIGEST_SIZE * 2 + 1];
29 char _hash_alg_name[32];
30 const char *hash_alg_name;
31 int status;
32 int i;
33
34 /*
35 * No supported options, but run getopt_long() with an empty longopts
36 * array so that any options are rejected and "--" works as expected.
37 */
38 if (getopt_long(argc, argv, "", longopts, NULL) != -1)
39 goto out_usage;
40
41 argv += optind;
42 argc -= optind;
43
44 if (argc < 1)
45 goto out_usage;
46
47 d = xzalloc(sizeof(*d) + FS_VERITY_MAX_DIGEST_SIZE);
48
49 for (i = 0; i < argc; i++) {
50 d->digest_size = FS_VERITY_MAX_DIGEST_SIZE;
51
52 if (!open_file(&file, argv[i], O_RDONLY, 0))
53 goto out_err;
54 if (ioctl(file.fd, FS_IOC_MEASURE_VERITY, d) != 0) {
55 error_msg_errno("FS_IOC_MEASURE_VERITY failed on '%s'",
56 file.name);
57 filedes_close(&file);
58 goto out_err;
59 }
60 filedes_close(&file);
61
62 ASSERT(d->digest_size <= FS_VERITY_MAX_DIGEST_SIZE);
63 bin2hex(d->digest, d->digest_size, digest_hex);
64 hash_alg_name = libfsverity_get_hash_name(d->digest_algorithm);
65 if (!hash_alg_name) {
66 sprintf(_hash_alg_name, "ALG_%u", d->digest_algorithm);
67 hash_alg_name = _hash_alg_name;
68 }
69 printf("%s:%s %s\n", hash_alg_name, digest_hex, argv[i]);
70 }
71 status = 0;
72 out:
73 free(d);
74 return status;
75
76 out_err:
77 status = 1;
78 goto out;
79
80 out_usage:
81 usage(cmd, stderr);
82 status = 2;
83 goto out;
84 }
85