xref: /freebsd/lib/libsecureboot/veta.c (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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 /**
29  * @file veta.c - add to trust anchors
30  *
31  */
32 
33 #define NEED_BRSSL_H
34 #include "libsecureboot-priv.h"
35 #include <brssl.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 
39 #ifdef VE_OPENPGP_SUPPORT
40 #include "openpgp/packet.h"
41 #endif
42 
43 /**
44  * @brief add trust anchors from a file
45  *
46  * The file might contain X.509 certs
47  * or OpenPGP public key
48  */
49 static size_t
50 trust_file_add(const char *trust)
51 {
52 	br_x509_certificate *xcs;
53 	size_t num;
54 
55 	xcs = read_certificates(trust, &num);
56 	if (xcs) {
57 		num = ve_trust_anchors_add(xcs, num);
58 	}
59 #ifdef VE_OPENPGP_SUPPORT
60 	else if (load_key_file(trust)) {
61 		num = 1;
62 	}
63 #endif
64 	return (num);
65 }
66 
67 /**
68  * @brief add trust anchors from a directory
69  *
70  * Pass each file in directory to trust_file_add
71  */
72 static size_t
73 trust_dir_add(const char *trust)
74 {
75 	char fbuf[MAXPATHLEN];
76 	DIR *dh;
77 	struct dirent *de;
78 	struct stat st;
79 	ssize_t sz;
80 	size_t num;
81 
82 	if (!(dh = opendir(trust)))
83 		return (0);
84 	for (num = 0, de = readdir(dh); de; de = readdir(dh)) {
85 		if (de->d_name[0] == '.')
86 			continue;
87 		sz = snprintf(fbuf, sizeof(fbuf), "%s/%s", trust, de->d_name);
88 		if (sz >= (ssize_t)sizeof(fbuf))
89 			continue;
90 		if (stat(fbuf, &st) < 0 || S_ISDIR(st.st_mode))
91 			continue;
92 		num += trust_file_add(fbuf);
93 	}
94 	closedir(dh);
95 	return (num);
96 }
97 
98 /**
99  * @brief add trust anchors
100  */
101 int
102 ve_trust_add(const char *trust)
103 {
104 	struct stat st;
105 
106 	if (stat(trust, &st) < 0)
107 		return (-1);
108 	if (S_ISDIR(st.st_mode))
109 		return (trust_dir_add(trust));
110 	return (trust_file_add(trust));
111 }
112