xref: /freebsd/lib/libsecureboot/vepcr.c (revision 1d386b48)
15fff9558SSimon J. Gerraty /*-
25fff9558SSimon J. Gerraty  * Copyright (c) 2018, Juniper Networks, Inc.
35fff9558SSimon J. Gerraty  *
45fff9558SSimon J. Gerraty  * Redistribution and use in source and binary forms, with or without
55fff9558SSimon J. Gerraty  * modification, are permitted provided that the following conditions
65fff9558SSimon J. Gerraty  * are met:
75fff9558SSimon J. Gerraty  * 1. Redistributions of source code must retain the above copyright
85fff9558SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer.
95fff9558SSimon J. Gerraty  * 2. Redistributions in binary form must reproduce the above copyright
105fff9558SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer in the
115fff9558SSimon J. Gerraty  *    documentation and/or other materials provided with the distribution.
125fff9558SSimon J. Gerraty  *
135fff9558SSimon J. Gerraty  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
145fff9558SSimon J. Gerraty  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
155fff9558SSimon J. Gerraty  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
165fff9558SSimon J. Gerraty  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
175fff9558SSimon J. Gerraty  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
185fff9558SSimon J. Gerraty  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
195fff9558SSimon J. Gerraty  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
205fff9558SSimon J. Gerraty  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
215fff9558SSimon J. Gerraty  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225fff9558SSimon J. Gerraty  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
235fff9558SSimon J. Gerraty  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245fff9558SSimon J. Gerraty  */
255fff9558SSimon J. Gerraty #include <sys/cdefs.h>
2653f151f9SSimon J. Gerraty #include <sys/queue.h>
275fff9558SSimon J. Gerraty #include "libsecureboot-priv.h"
285fff9558SSimon J. Gerraty 
295fff9558SSimon J. Gerraty /*
305fff9558SSimon J. Gerraty  * To support measured boot without putting a ton
315fff9558SSimon J. Gerraty  * of extra code in the loader, we just maintain
325fff9558SSimon J. Gerraty  * a hash of all the hashes we (attempt to) verify.
335fff9558SSimon J. Gerraty  * The loader can export this for kernel or rc script
345fff9558SSimon J. Gerraty  * to feed to a TPM pcr register - hence the name ve_pcr.
355fff9558SSimon J. Gerraty  *
365fff9558SSimon J. Gerraty  * NOTE: in the current standard the TPM pcr register size is for SHA1,
375fff9558SSimon J. Gerraty  * the fact that we provide a SHA256 hash should not matter
385fff9558SSimon J. Gerraty  * as long as we are consistent - it can be truncated or hashed
395fff9558SSimon J. Gerraty  * before feeding to TPM.
405fff9558SSimon J. Gerraty  */
415fff9558SSimon J. Gerraty 
425fff9558SSimon J. Gerraty static const br_hash_class *pcr_md = NULL;
435fff9558SSimon J. Gerraty static br_hash_compat_context pcr_ctx;
445fff9558SSimon J. Gerraty static size_t pcr_hlen = 0;
4553f151f9SSimon J. Gerraty static int pcr_updating = -1;
4653f151f9SSimon J. Gerraty 
4753f151f9SSimon J. Gerraty struct hashed_info {
4853f151f9SSimon J. Gerraty 	const char *hi_path;
4953f151f9SSimon J. Gerraty 	const char *hi_basename;
5053f151f9SSimon J. Gerraty 	STAILQ_ENTRY(hashed_info) entries;
5153f151f9SSimon J. Gerraty };
5253f151f9SSimon J. Gerraty 
5353f151f9SSimon J. Gerraty static STAILQ_HEAD(, hashed_info) hi_list;
5453f151f9SSimon J. Gerraty 
555fff9558SSimon J. Gerraty 
565fff9558SSimon J. Gerraty /**
575fff9558SSimon J. Gerraty  * @brief initialize pcr context
585fff9558SSimon J. Gerraty  *
595fff9558SSimon J. Gerraty  * Real TPM registers only hold a SHA1 hash
605fff9558SSimon J. Gerraty  * but we use SHA256
615fff9558SSimon J. Gerraty  */
625fff9558SSimon J. Gerraty void
ve_pcr_init(void)635fff9558SSimon J. Gerraty ve_pcr_init(void)
645fff9558SSimon J. Gerraty {
6553f151f9SSimon J. Gerraty 	if (pcr_updating < 0) {
66980bde58SSimon J. Gerraty 		pcr_updating = 0;
675fff9558SSimon J. Gerraty 		pcr_hlen = br_sha256_SIZE;
685fff9558SSimon J. Gerraty 		pcr_md = &br_sha256_vtable;
695fff9558SSimon J. Gerraty 		pcr_md->init(&pcr_ctx.vtable);
7053f151f9SSimon J. Gerraty 		STAILQ_INIT(&hi_list);
7153f151f9SSimon J. Gerraty 	}
725fff9558SSimon J. Gerraty }
735fff9558SSimon J. Gerraty 
745fff9558SSimon J. Gerraty /**
75980bde58SSimon J. Gerraty  * @brief get pcr_updating state
76980bde58SSimon J. Gerraty  */
77980bde58SSimon J. Gerraty int
ve_pcr_updating_get(void)78980bde58SSimon J. Gerraty ve_pcr_updating_get(void)
79980bde58SSimon J. Gerraty {
80980bde58SSimon J. Gerraty 	return (pcr_updating);
81980bde58SSimon J. Gerraty }
82980bde58SSimon J. Gerraty 
83980bde58SSimon J. Gerraty /**
84980bde58SSimon J. Gerraty  * @brief set pcr_updating state
85980bde58SSimon J. Gerraty  */
86980bde58SSimon J. Gerraty void
ve_pcr_updating_set(int updating)87980bde58SSimon J. Gerraty ve_pcr_updating_set(int updating)
88980bde58SSimon J. Gerraty {
89980bde58SSimon J. Gerraty 	pcr_updating = updating;
90980bde58SSimon J. Gerraty }
91980bde58SSimon J. Gerraty 
92980bde58SSimon J. Gerraty /**
935fff9558SSimon J. Gerraty  * @brief update pcr context
945fff9558SSimon J. Gerraty  */
955fff9558SSimon J. Gerraty void
ve_pcr_update(const char * path,unsigned char * data,size_t dlen)9653f151f9SSimon J. Gerraty ve_pcr_update(const char *path, unsigned char *data, size_t dlen)
975fff9558SSimon J. Gerraty {
9853f151f9SSimon J. Gerraty 	struct hashed_info *hip;
9953f151f9SSimon J. Gerraty 
10053f151f9SSimon J. Gerraty 	if (pcr_updating > 0 && pcr_md != NULL) {
1015fff9558SSimon J. Gerraty 		pcr_md->update(&pcr_ctx.vtable, data, dlen);
10253f151f9SSimon J. Gerraty 		/* if mallocs fail, measured boot will likely fail too */
10353f151f9SSimon J. Gerraty 		if ((hip = malloc(sizeof(struct hashed_info)))) {
10453f151f9SSimon J. Gerraty 			hip->hi_path = strdup(path);
10553f151f9SSimon J. Gerraty 			if (!hip->hi_path) {
10653f151f9SSimon J. Gerraty 			    free(hip);
10753f151f9SSimon J. Gerraty 			    return;
10853f151f9SSimon J. Gerraty 			}
10953f151f9SSimon J. Gerraty 			hip->hi_basename = strrchr(hip->hi_path, '/');
11053f151f9SSimon J. Gerraty 			if (hip->hi_basename) {
11153f151f9SSimon J. Gerraty 				hip->hi_basename++;
11253f151f9SSimon J. Gerraty 			} else {
11353f151f9SSimon J. Gerraty 				hip->hi_basename = hip->hi_path;
11453f151f9SSimon J. Gerraty 			}
11553f151f9SSimon J. Gerraty 			STAILQ_INSERT_TAIL(&hi_list, hip, entries);
11653f151f9SSimon J. Gerraty 		}
11753f151f9SSimon J. Gerraty 	}
1185fff9558SSimon J. Gerraty }
1195fff9558SSimon J. Gerraty 
1205fff9558SSimon J. Gerraty /**
1215fff9558SSimon J. Gerraty  * @brief get pcr result
1225fff9558SSimon J. Gerraty  */
1235fff9558SSimon J. Gerraty ssize_t
ve_pcr_get(unsigned char * buf,size_t sz)1245fff9558SSimon J. Gerraty ve_pcr_get(unsigned char *buf, size_t sz)
1255fff9558SSimon J. Gerraty {
1265fff9558SSimon J. Gerraty 	if (!pcr_md)
1275fff9558SSimon J. Gerraty 		return (-1);
1285fff9558SSimon J. Gerraty 	if (sz < pcr_hlen)
1295fff9558SSimon J. Gerraty 		return (-1);
1305fff9558SSimon J. Gerraty 	pcr_md->out(&pcr_ctx.vtable, buf);
1315fff9558SSimon J. Gerraty 	return (pcr_hlen);
1325fff9558SSimon J. Gerraty }
1335fff9558SSimon J. Gerraty 
13453f151f9SSimon J. Gerraty /**
13553f151f9SSimon J. Gerraty  * @brief get list of paths in prc
13653f151f9SSimon J. Gerraty  */
13753f151f9SSimon J. Gerraty char *
ve_pcr_hashed_get(int flags)13853f151f9SSimon J. Gerraty ve_pcr_hashed_get(int flags)
13953f151f9SSimon J. Gerraty {
14053f151f9SSimon J. Gerraty 	const char *cp;
14153f151f9SSimon J. Gerraty 	char *hinfo;
14253f151f9SSimon J. Gerraty 	struct hashed_info *hip;
14353f151f9SSimon J. Gerraty 	size_t nbytes;
14453f151f9SSimon J. Gerraty 	size_t x;
14553f151f9SSimon J. Gerraty 	int n;
14653f151f9SSimon J. Gerraty 
14753f151f9SSimon J. Gerraty 	n = 0;
14853f151f9SSimon J. Gerraty 	nbytes = x = 0;
14953f151f9SSimon J. Gerraty 	hinfo = NULL;
15053f151f9SSimon J. Gerraty 	STAILQ_FOREACH(hip, &hi_list, entries) {
15153f151f9SSimon J. Gerraty 		nbytes += 1 + strlen(flags ? hip->hi_basename : hip->hi_path);
15253f151f9SSimon J. Gerraty 	}
15353f151f9SSimon J. Gerraty 	if (nbytes > 1) {
15453f151f9SSimon J. Gerraty 		hinfo = malloc(nbytes + 2);
15553f151f9SSimon J. Gerraty 		if (hinfo) {
15653f151f9SSimon J. Gerraty 			STAILQ_FOREACH(hip, &hi_list, entries) {
15753f151f9SSimon J. Gerraty 				cp = flags ? hip->hi_basename : hip->hi_path;
15853f151f9SSimon J. Gerraty 				n = snprintf(&hinfo[x], nbytes - x, "%s,", cp);
15953f151f9SSimon J. Gerraty 				x += n;
16053f151f9SSimon J. Gerraty 			}
16153f151f9SSimon J. Gerraty 			if (x > 0) {
16253f151f9SSimon J. Gerraty 				hinfo[x-1] = '\0';
16353f151f9SSimon J. Gerraty 			}
16453f151f9SSimon J. Gerraty 		}
16553f151f9SSimon J. Gerraty 	}
16653f151f9SSimon J. Gerraty 	return hinfo;
16753f151f9SSimon J. Gerraty }
168