xref: /freebsd/lib/libsecureboot/veta.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1*5fff9558SSimon J. Gerraty /*-
2*5fff9558SSimon J. Gerraty  * Copyright (c) 2018, Juniper Networks, Inc.
3*5fff9558SSimon J. Gerraty  *
4*5fff9558SSimon J. Gerraty  * Redistribution and use in source and binary forms, with or without
5*5fff9558SSimon J. Gerraty  * modification, are permitted provided that the following conditions
6*5fff9558SSimon J. Gerraty  * are met:
7*5fff9558SSimon J. Gerraty  * 1. Redistributions of source code must retain the above copyright
8*5fff9558SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer.
9*5fff9558SSimon J. Gerraty  * 2. Redistributions in binary form must reproduce the above copyright
10*5fff9558SSimon J. Gerraty  *    notice, this list of conditions and the following disclaimer in the
11*5fff9558SSimon J. Gerraty  *    documentation and/or other materials provided with the distribution.
12*5fff9558SSimon J. Gerraty  *
13*5fff9558SSimon J. Gerraty  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14*5fff9558SSimon J. Gerraty  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15*5fff9558SSimon J. Gerraty  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16*5fff9558SSimon J. Gerraty  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
17*5fff9558SSimon J. Gerraty  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18*5fff9558SSimon J. Gerraty  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19*5fff9558SSimon J. Gerraty  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*5fff9558SSimon J. Gerraty  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*5fff9558SSimon J. Gerraty  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*5fff9558SSimon J. Gerraty  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23*5fff9558SSimon J. Gerraty  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*5fff9558SSimon J. Gerraty  */
25*5fff9558SSimon J. Gerraty #include <sys/cdefs.h>
26*5fff9558SSimon J. Gerraty /**
27*5fff9558SSimon J. Gerraty  * @file veta.c - add to trust anchors
28*5fff9558SSimon J. Gerraty  *
29*5fff9558SSimon J. Gerraty  */
30*5fff9558SSimon J. Gerraty 
31*5fff9558SSimon J. Gerraty #define NEED_BRSSL_H
32*5fff9558SSimon J. Gerraty #include "libsecureboot-priv.h"
33*5fff9558SSimon J. Gerraty #include <brssl.h>
34*5fff9558SSimon J. Gerraty #include <sys/stat.h>
35*5fff9558SSimon J. Gerraty #include <dirent.h>
36*5fff9558SSimon J. Gerraty 
37*5fff9558SSimon J. Gerraty #ifdef VE_OPENPGP_SUPPORT
38*5fff9558SSimon J. Gerraty #include "openpgp/packet.h"
39*5fff9558SSimon J. Gerraty #endif
40*5fff9558SSimon J. Gerraty 
41*5fff9558SSimon J. Gerraty /**
42*5fff9558SSimon J. Gerraty  * @brief add trust anchors from a file
43*5fff9558SSimon J. Gerraty  *
44*5fff9558SSimon J. Gerraty  * The file might contain X.509 certs
45*5fff9558SSimon J. Gerraty  * or OpenPGP public key
46*5fff9558SSimon J. Gerraty  */
47*5fff9558SSimon J. Gerraty static size_t
trust_file_add(const char * trust)48*5fff9558SSimon J. Gerraty trust_file_add(const char *trust)
49*5fff9558SSimon J. Gerraty {
50*5fff9558SSimon J. Gerraty 	br_x509_certificate *xcs;
51*5fff9558SSimon J. Gerraty 	size_t num;
52*5fff9558SSimon J. Gerraty 
53*5fff9558SSimon J. Gerraty 	xcs = read_certificates(trust, &num);
54*5fff9558SSimon J. Gerraty 	if (xcs) {
55*5fff9558SSimon J. Gerraty 		num = ve_trust_anchors_add(xcs, num);
56*5fff9558SSimon J. Gerraty 	}
57*5fff9558SSimon J. Gerraty #ifdef VE_OPENPGP_SUPPORT
58*5fff9558SSimon J. Gerraty 	else if (load_key_file(trust)) {
59*5fff9558SSimon J. Gerraty 		num = 1;
60*5fff9558SSimon J. Gerraty 	}
61*5fff9558SSimon J. Gerraty #endif
62*5fff9558SSimon J. Gerraty 	return (num);
63*5fff9558SSimon J. Gerraty }
64*5fff9558SSimon J. Gerraty 
65*5fff9558SSimon J. Gerraty /**
66*5fff9558SSimon J. Gerraty  * @brief add trust anchors from a directory
67*5fff9558SSimon J. Gerraty  *
68*5fff9558SSimon J. Gerraty  * Pass each file in directory to trust_file_add
69*5fff9558SSimon J. Gerraty  */
70*5fff9558SSimon J. Gerraty static size_t
trust_dir_add(const char * trust)71*5fff9558SSimon J. Gerraty trust_dir_add(const char *trust)
72*5fff9558SSimon J. Gerraty {
73*5fff9558SSimon J. Gerraty 	char fbuf[MAXPATHLEN];
74*5fff9558SSimon J. Gerraty 	DIR *dh;
75*5fff9558SSimon J. Gerraty 	struct dirent *de;
76*5fff9558SSimon J. Gerraty 	struct stat st;
77*5fff9558SSimon J. Gerraty 	ssize_t sz;
78*5fff9558SSimon J. Gerraty 	size_t num;
79*5fff9558SSimon J. Gerraty 
80*5fff9558SSimon J. Gerraty 	if (!(dh = opendir(trust)))
81*5fff9558SSimon J. Gerraty 		return (0);
82*5fff9558SSimon J. Gerraty 	for (num = 0, de = readdir(dh); de; de = readdir(dh)) {
83*5fff9558SSimon J. Gerraty 		if (de->d_name[0] == '.')
84*5fff9558SSimon J. Gerraty 			continue;
85*5fff9558SSimon J. Gerraty 		sz = snprintf(fbuf, sizeof(fbuf), "%s/%s", trust, de->d_name);
86*5fff9558SSimon J. Gerraty 		if (sz >= (ssize_t)sizeof(fbuf))
87*5fff9558SSimon J. Gerraty 			continue;
88*5fff9558SSimon J. Gerraty 		if (stat(fbuf, &st) < 0 || S_ISDIR(st.st_mode))
89*5fff9558SSimon J. Gerraty 			continue;
90*5fff9558SSimon J. Gerraty 		num += trust_file_add(fbuf);
91*5fff9558SSimon J. Gerraty 	}
92*5fff9558SSimon J. Gerraty 	closedir(dh);
93*5fff9558SSimon J. Gerraty 	return (num);
94*5fff9558SSimon J. Gerraty }
95*5fff9558SSimon J. Gerraty 
96*5fff9558SSimon J. Gerraty /**
97*5fff9558SSimon J. Gerraty  * @brief add trust anchors
98*5fff9558SSimon J. Gerraty  */
99*5fff9558SSimon J. Gerraty int
ve_trust_add(const char * trust)100*5fff9558SSimon J. Gerraty ve_trust_add(const char *trust)
101*5fff9558SSimon J. Gerraty {
102*5fff9558SSimon J. Gerraty 	struct stat st;
103*5fff9558SSimon J. Gerraty 
104*5fff9558SSimon J. Gerraty 	if (stat(trust, &st) < 0)
105*5fff9558SSimon J. Gerraty 		return (-1);
106*5fff9558SSimon J. Gerraty 	if (S_ISDIR(st.st_mode))
107*5fff9558SSimon J. Gerraty 		return (trust_dir_add(trust));
108*5fff9558SSimon J. Gerraty 	return (trust_file_add(trust));
109*5fff9558SSimon J. Gerraty }
110