-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbu-lib.sh.in
190 lines (152 loc) · 3.96 KB
/
sbu-lib.sh.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env bash
#
# @PACKAGE_NAME@
# Version: @PACKAGE@-@PACKAGE_VERSION@
# Project: @PACKAGE_URL@
# Send bug reports to: @PACKAGE_BUGREPORT@
#
on_exit() {
local result=${1}
local sec="${SECONDS}"
if [[ -d "${tmp_dir:-}" ]]; then
if [[ ${keep_tmp_dir:-} ]]; then
echo "${script_name}: INFO: tmp dir preserved: '${tmp_dir}'" >&2
else
rm -rf "${tmp_dir:?}"
fi
fi
set +x
echo "${script_name}: Done: ${result}, ${sec} sec." >&2
}
on_err() {
local f_name=${1}
local line_no=${2}
local err_no=${3}
if [[ ${debug:-} ]]; then
echo '------------------------' >&2
set >&2
echo '------------------------' >&2
fi
echo "${script_name}: ERROR: (${err_no}) at ${f_name}:${line_no}." >&2
exit ${err_no}
}
check_in_files() {
local files=("${@}")
#echo "${FUNCNAME[0]}: count: ${#files[@]}" >&2
#echo "${FUNCNAME[0]}: files: @${files[@]}@" >&2
if [[ ${#files[@]} -eq 0 ]]; then
echo "${script_name}: ERROR: No input files given." >&2
usage
exit 1
fi
for ((i = 0; i < ${#files[@]}; i++)); do
if [[ -f "${files[i]}" ]]; then
if [[ ${verbose} ]]; then
echo "${FUNCNAME[0]}: [$((i + 1))] '${files[i]}' OK." >&2
fi
else
echo "${script_name}: ERROR: Bad input file: [$((i + 1))] '${files[i]}'." >&2
usage
exit 1
fi
done
if [[ ${verbose} ]]; then
echo "" >&2
fi
return 0
}
md5sum="${md5sum:-md5sum}"
openssl="${openssl:-openssl}"
sbsign="${sbsign:-sbsign}"
sbverify="${sbverify:-sbverify}"
sha256sum="${sha256sum:-sha256sum}"
check_sb_progs() {
if ! check_progs " ${md5sum} ${openssl} ${sbsign} ${sbverify} ${sha256sum}"; then
exit 1
fi
}
print_cert_der() {
local cert=${1}
"${openssl}" x509 -in "${cert}" -inform der -text -noout
}
print_cert_pem() {
local cert=${1}
"${openssl}" x509 -in "${cert}" -text -noout
}
list_file_cert() {
local file=${1}
echo '=================='
"${sha256sum}" --tag "${file}"
echo '------------------'
"${sbverify}" --list "${file}"
echo '=================='
}
check_file_sig() {
local cert=${1}
local file=${2}
echo '=================='
"${sha256sum}" --tag "${cert}"
"${sha256sum}" --tag "${file}"
echo '------------------'
"${sbverify}" --cert "${cert}" "${file}"
echo '=================='
}
check_if_certificate() {
local cert=${1}
if ! openssl x509 -inform PEM -in "${cert}" -noout > /dev/null ; then
echo "${script_name}: ERROR: Bad certificate: '${cert}'." >&2
exit 1
fi
}
check_if_signing_key() {
local key=${1}
if ! openssl rsa -inform PEM -in "${key}" --check -noout > /dev/null ; then
echo "${script_name}: ERROR: Bad signing key: '${key}'." >&2
exit 1
fi
}
download_file() {
local out_file=${1}
local url=${2}
local sha256=${3}
wget "${url}" -O "${out_file}"
local check
check="$(sha256sum "${out_file}" | cut -f 1 -d ' ')"
if [[ "${check}" != "${sha256}" ]]; then
echo "${script_name}: ERROR: Bad download ${url}." >&2
exit 1
fi
}
download_cert() {
local out_prefix=${1}
local url=${2}
local sha256=${3}
echo "${script_name}: INFO: Downloading ${out_prefix##*/}." >&2
echo '' >&2
download_file "${out_prefix}.der" "${url}" "${sha256}"
openssl x509 -in "${out_prefix}.der" -inform DER -out "${out_prefix}.pem" -outform PEM
print_cert_der "${out_prefix}.der"
}
download_debian_sb() {
local key_store=${1}
download_cert \
"${key_store}/debian-secure-boot-ca" \
"https://dsa.debian.org/secure-boot-ca" \
"079646974bce09b1f04da67bd722d1fb0947ae4c4010bccdbba52d5b23cbf1a2"
}
download_ms_uefi_ca() {
local key_store=${1}
# FIXME: 403 Forbidden. Need 'wget --referer'???
download_cert \
"${key_store}/microsoft-uefica-public" \
"https://go.microsoft.com/fwlink/p/?linkid=321194" \
"48e99b991f57fc52f76149599bff0a58c47154229b9f8d603ac40d3500248507"
}
download_ms_3rd_party() {
local key_store=${1}
# FIXME: 403 Forbidden. Need 'wget --referer'???
download_cert \
"${key_store}/microsoft-3rd-party" \
"http://www.microsoft.com/pki/certs/MicCorThiParMarRoo_2010-10-05.crt" \
"2848361a9c1e32df1d3e2ed6a7b9e67a525cf8a13b164f8006c9479578f746de"
}