2 * Copyright (c) 2019 Stormshield.
3 * Copyright (c) 2019 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include "libsecureboot-priv.h"
34 #include <verify_file.h>
37 * Values to pass to kernel by envs.
39 static char manifest_path[MAXPATHLEN];
40 static char manifest_prefix[MAXPATHLEN];
41 static char manifest_hash[2 * br_sha256_SIZE + 2];
42 static int manifest_present = 0;
45 * Verify and pass manifest path and digest to kernel through envs.
46 * The paths in manifest can be either absolute,
47 * or "prefix", if exists will be added to the ones that are not.
50 pass_manifest(const char *path, const char *prefix)
54 unsigned char digest[br_sha256_SIZE];
55 const br_hash_class *md;
56 br_hash_compat_context ctx;
60 md = &br_sha256_vtable;
62 if (strnlen(path, MAXPATHLEN) == MAXPATHLEN ||
63 strnlen(prefix, MAXPATHLEN) == MAXPATHLEN)
70 if (!S_ISREG(st.st_mode)) {
75 rc = is_verified(&st);
77 if (rc != VE_NOT_CHECKED && rc != VE_VERIFIED) {
82 if (rc == VE_VERIFIED)
83 content = read_file(path, NULL);
85 content = (char *)verify_signed(path, VEF_VERBOSE);
87 if (content == NULL) {
88 add_verify_status(&st, VE_FINGERPRINT_WRONG);
93 add_verify_status(&st, VE_VERIFIED);
95 md->init(&ctx.vtable);
96 md->update(&ctx.vtable, content, st.st_size);
97 md->out(&ctx.vtable, digest);
100 manifest_prefix[0] = '\0';
102 strcpy(manifest_prefix, prefix);
104 strcpy(manifest_path, path);
106 hexdigest(manifest_hash, 2 * br_sha256_SIZE + 2,
107 digest, br_sha256_SIZE);
108 manifest_hash[2*br_sha256_SIZE] = '\0';
110 manifest_present = 1;
121 * Set appropriate envs to inform kernel about manifest location and digest.
122 * This should be called right before boot so that envs can't be replaced.
125 pass_manifest_export_envs()
129 /* If we have nothing to pass make sure that envs are empty. */
130 if (!manifest_present) {
131 unsetenv("veriexec.manifest_path");
132 unsetenv("veriexec.manifest_hash");
133 unsetenv("veriexec.manifest_prefix");
137 rc = setenv("veriexec.manifest_path", manifest_path, 1);
141 rc = setenv("veriexec.manifest_hash", manifest_hash, 1);
143 unsetenv("veriexec.manifest_path");
147 if (manifest_prefix[0] != '\0')
148 rc = setenv("veriexec.manifest_prefix", manifest_prefix, 1);