1/*-
2 * Copyright (c) 2018, Juniper Networks, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include <sys/cdefs.h>
26__FBSDID("$FreeBSD$");
27
28#include <sys/queue.h>
29#include "libsecureboot-priv.h"
30
31/*
32 * To support measured boot without putting a ton
33 * of extra code in the loader, we just maintain
34 * a hash of all the hashes we (attempt to) verify.
35 * The loader can export this for kernel or rc script
36 * to feed to a TPM pcr register - hence the name ve_pcr.
37 *
38 * NOTE: in the current standard the TPM pcr register size is for SHA1,
39 * the fact that we provide a SHA256 hash should not matter
40 * as long as we are consistent - it can be truncated or hashed
41 * before feeding to TPM.
42 */
43
44static const br_hash_class *pcr_md = NULL;
45static br_hash_compat_context pcr_ctx;
46static size_t pcr_hlen = 0;
47static int pcr_updating = -1;
48
49struct hashed_info {
50	const char *hi_path;
51	const char *hi_basename;
52	STAILQ_ENTRY(hashed_info) entries;
53};
54
55static STAILQ_HEAD(, hashed_info) hi_list;
56
57
58/**
59 * @brief initialize pcr context
60 *
61 * Real TPM registers only hold a SHA1 hash
62 * but we use SHA256
63 */
64void
65ve_pcr_init(void)
66{
67	if (pcr_updating < 0) {
68		pcr_updating = 0;
69		pcr_hlen = br_sha256_SIZE;
70		pcr_md = &br_sha256_vtable;
71		pcr_md->init(&pcr_ctx.vtable);
72		STAILQ_INIT(&hi_list);
73	}
74}
75
76/**
77 * @brief get pcr_updating state
78 */
79int
80ve_pcr_updating_get(void)
81{
82	return (pcr_updating);
83}
84
85/**
86 * @brief set pcr_updating state
87 */
88void
89ve_pcr_updating_set(int updating)
90{
91	pcr_updating = updating;
92}
93
94/**
95 * @brief update pcr context
96 */
97void
98ve_pcr_update(const char *path, unsigned char *data, size_t dlen)
99{
100	struct hashed_info *hip;
101
102	if (pcr_updating > 0 && pcr_md != NULL) {
103		pcr_md->update(&pcr_ctx.vtable, data, dlen);
104		/* if mallocs fail, measured boot will likely fail too */
105		if ((hip = malloc(sizeof(struct hashed_info)))) {
106			hip->hi_path = strdup(path);
107			if (!hip->hi_path) {
108			    free(hip);
109			    return;
110			}
111			hip->hi_basename = strrchr(hip->hi_path, '/');
112			if (hip->hi_basename) {
113				hip->hi_basename++;
114			} else {
115				hip->hi_basename = hip->hi_path;
116			}
117			STAILQ_INSERT_TAIL(&hi_list, hip, entries);
118		}
119	}
120}
121
122/**
123 * @brief get pcr result
124 */
125ssize_t
126ve_pcr_get(unsigned char *buf, size_t sz)
127{
128	if (!pcr_md)
129		return (-1);
130	if (sz < pcr_hlen)
131		return (-1);
132	pcr_md->out(&pcr_ctx.vtable, buf);
133	return (pcr_hlen);
134}
135
136/**
137 * @brief get list of paths in prc
138 */
139char *
140ve_pcr_hashed_get(int flags)
141{
142	const char *cp;
143	char *hinfo;
144	struct hashed_info *hip;
145	size_t nbytes;
146	size_t x;
147	int n;
148
149	n = 0;
150	nbytes = x = 0;
151	hinfo = NULL;
152	STAILQ_FOREACH(hip, &hi_list, entries) {
153		nbytes += 1 + strlen(flags ? hip->hi_basename : hip->hi_path);
154	}
155	if (nbytes > 1) {
156		hinfo = malloc(nbytes + 2);
157		if (hinfo) {
158			STAILQ_FOREACH(hip, &hi_list, entries) {
159				cp = flags ? hip->hi_basename : hip->hi_path;
160				n = snprintf(&hinfo[x], nbytes - x, "%s,", cp);
161				x += n;
162			}
163			if (x > 0) {
164				hinfo[x-1] = '\0';
165			}
166		}
167	}
168	return hinfo;
169}
170