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