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>
26*53f151f9SSimon 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;
45*53f151f9SSimon J. Gerraty static int pcr_updating = -1;
46*53f151f9SSimon J. Gerraty
47*53f151f9SSimon J. Gerraty struct hashed_info {
48*53f151f9SSimon J. Gerraty const char *hi_path;
49*53f151f9SSimon J. Gerraty const char *hi_basename;
50*53f151f9SSimon J. Gerraty STAILQ_ENTRY(hashed_info) entries;
51*53f151f9SSimon J. Gerraty };
52*53f151f9SSimon J. Gerraty
53*53f151f9SSimon J. Gerraty static STAILQ_HEAD(, hashed_info) hi_list;
54*53f151f9SSimon 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 {
65*53f151f9SSimon 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);
70*53f151f9SSimon J. Gerraty STAILQ_INIT(&hi_list);
71*53f151f9SSimon 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)96*53f151f9SSimon J. Gerraty ve_pcr_update(const char *path, unsigned char *data, size_t dlen)
975fff9558SSimon J. Gerraty {
98*53f151f9SSimon J. Gerraty struct hashed_info *hip;
99*53f151f9SSimon J. Gerraty
100*53f151f9SSimon J. Gerraty if (pcr_updating > 0 && pcr_md != NULL) {
1015fff9558SSimon J. Gerraty pcr_md->update(&pcr_ctx.vtable, data, dlen);
102*53f151f9SSimon J. Gerraty /* if mallocs fail, measured boot will likely fail too */
103*53f151f9SSimon J. Gerraty if ((hip = malloc(sizeof(struct hashed_info)))) {
104*53f151f9SSimon J. Gerraty hip->hi_path = strdup(path);
105*53f151f9SSimon J. Gerraty if (!hip->hi_path) {
106*53f151f9SSimon J. Gerraty free(hip);
107*53f151f9SSimon J. Gerraty return;
108*53f151f9SSimon J. Gerraty }
109*53f151f9SSimon J. Gerraty hip->hi_basename = strrchr(hip->hi_path, '/');
110*53f151f9SSimon J. Gerraty if (hip->hi_basename) {
111*53f151f9SSimon J. Gerraty hip->hi_basename++;
112*53f151f9SSimon J. Gerraty } else {
113*53f151f9SSimon J. Gerraty hip->hi_basename = hip->hi_path;
114*53f151f9SSimon J. Gerraty }
115*53f151f9SSimon J. Gerraty STAILQ_INSERT_TAIL(&hi_list, hip, entries);
116*53f151f9SSimon J. Gerraty }
117*53f151f9SSimon 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
134*53f151f9SSimon J. Gerraty /**
135*53f151f9SSimon J. Gerraty * @brief get list of paths in prc
136*53f151f9SSimon J. Gerraty */
137*53f151f9SSimon J. Gerraty char *
ve_pcr_hashed_get(int flags)138*53f151f9SSimon J. Gerraty ve_pcr_hashed_get(int flags)
139*53f151f9SSimon J. Gerraty {
140*53f151f9SSimon J. Gerraty const char *cp;
141*53f151f9SSimon J. Gerraty char *hinfo;
142*53f151f9SSimon J. Gerraty struct hashed_info *hip;
143*53f151f9SSimon J. Gerraty size_t nbytes;
144*53f151f9SSimon J. Gerraty size_t x;
145*53f151f9SSimon J. Gerraty int n;
146*53f151f9SSimon J. Gerraty
147*53f151f9SSimon J. Gerraty n = 0;
148*53f151f9SSimon J. Gerraty nbytes = x = 0;
149*53f151f9SSimon J. Gerraty hinfo = NULL;
150*53f151f9SSimon J. Gerraty STAILQ_FOREACH(hip, &hi_list, entries) {
151*53f151f9SSimon J. Gerraty nbytes += 1 + strlen(flags ? hip->hi_basename : hip->hi_path);
152*53f151f9SSimon J. Gerraty }
153*53f151f9SSimon J. Gerraty if (nbytes > 1) {
154*53f151f9SSimon J. Gerraty hinfo = malloc(nbytes + 2);
155*53f151f9SSimon J. Gerraty if (hinfo) {
156*53f151f9SSimon J. Gerraty STAILQ_FOREACH(hip, &hi_list, entries) {
157*53f151f9SSimon J. Gerraty cp = flags ? hip->hi_basename : hip->hi_path;
158*53f151f9SSimon J. Gerraty n = snprintf(&hinfo[x], nbytes - x, "%s,", cp);
159*53f151f9SSimon J. Gerraty x += n;
160*53f151f9SSimon J. Gerraty }
161*53f151f9SSimon J. Gerraty if (x > 0) {
162*53f151f9SSimon J. Gerraty hinfo[x-1] = '\0';
163*53f151f9SSimon J. Gerraty }
164*53f151f9SSimon J. Gerraty }
165*53f151f9SSimon J. Gerraty }
166*53f151f9SSimon J. Gerraty return hinfo;
167*53f151f9SSimon J. Gerraty }
168