Skip to content

Commit b4083c6

Browse files
committed
Add OpenCore utilities
1 parent 02f6ca8 commit b4083c6

36 files changed

+1552
-0
lines changed
436 KB
Binary file not shown.
450 KB
Binary file not shown.
456 KB
Binary file not shown.
15.2 KB
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
# create_vault.sh
4+
#
5+
#
6+
# Created by Rodion Shingarev on 13.04.19.
7+
#
8+
OCPath="$1"
9+
10+
if [ "${OCPath}" = "" ]; then
11+
echo "Usage ./create_vault.sh path/to/EFI/OC"
12+
exit 1
13+
fi
14+
15+
if [ ! -d "${OCPath}" ]; then
16+
echo "Path $OCPath is missing!"
17+
exit 1
18+
fi
19+
20+
if [ ! -x /usr/bin/find ] || [ ! -x /bin/rm ] || [ ! -x /usr/bin/sed ] || [ ! -x /usr/bin/xxd ]; then
21+
echo "Unix environment is broken!"
22+
exit 1
23+
fi
24+
25+
if [ ! -x /usr/libexec/PlistBuddy ]; then
26+
echo "PlistBuddy is missing!"
27+
exit 1
28+
fi
29+
30+
if [ ! -x /usr/bin/shasum ]; then
31+
echo "shasum is missing!"
32+
exit 1
33+
fi
34+
35+
abort() {
36+
/bin/rm -rf vault.plist vault.sig /tmp/vault_hash
37+
echo "Fatal error: ${1}!"
38+
exit 1
39+
}
40+
41+
echo "Chose ${OCPath} for hashing..."
42+
43+
cd "${OCPath}" || abort "Failed to reach ${OCPath}"
44+
/bin/rm -rf vault.plist vault.sig || abort "Failed to cleanup"
45+
/usr/libexec/PlistBuddy -c "Add Version integer 1" vault.plist || abort "Failed to set vault.plist version"
46+
47+
echo "Hashing files in ${OCPath}..."
48+
49+
/usr/bin/find . -not -path '*/\.*' -type f \
50+
\( ! -iname ".*" \) \
51+
\( ! -iname "vault.*" \) \
52+
\( ! -iname "OpenCore.efi" \) | while read -r fname; do
53+
fname="${fname#"./"}"
54+
wname="${fname//\//\\\\}"
55+
shasum=$(/usr/bin/shasum -a 256 "${fname}") || abort "Failed to hash ${fname}"
56+
sha=$(echo "$shasum" | /usr/bin/sed 's/^\([a-f0-9]\{64\}\).*/\1/') || abort "Illegit hashsum"
57+
if [ "${#sha}" != 64 ] || [ "$(echo "$sha"| /usr/bin/sed 's/^[a-f0-9]*$//')" ]; then
58+
abort "Got invalid hash: ${sha}!"
59+
fi
60+
61+
echo "${wname}: ${sha}"
62+
63+
echo "${sha}" | /usr/bin/xxd -r -p > /tmp/vault_hash || abort "Hashing failure"
64+
/usr/libexec/PlistBuddy -c "Import Files:'${wname}' /tmp/vault_hash" vault.plist || abort "Failed to append vault.plist!"
65+
done
66+
67+
/bin/rm -rf /tmp/vault_hash
68+
69+
echo "All done!"
70+
exit 0
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/sh
2+
3+
abort() {
4+
echo "Fatal error: ${1}!"
5+
exit 1
6+
}
7+
8+
cleanup() {
9+
echo "Cleaning up keys"
10+
rm -rf "${KeyPath}"
11+
}
12+
13+
if [ ! -x /usr/bin/dirname ] || [ ! -x /bin/chmod ] || [ ! -x /bin/mkdir ] || [ ! -x /usr/bin/openssl ] || [ ! -x /bin/rm ] || [ ! -x /usr/bin/strings ] || [ ! -x /usr/bin/grep ] || [ ! -x /usr/bin/cut ] || [ ! -x /bin/dd ] || [ ! -x /usr/bin/uuidgen ] ; then
14+
abort "Unix environment is broken!"
15+
fi
16+
17+
cd "$(/usr/bin/dirname "$0")" || abort "Failed to enter working directory!"
18+
19+
OCPath="$1"
20+
21+
if [ "$OCPath" = "" ]; then
22+
OCPath=../../EFI/OC
23+
fi
24+
25+
KeyPath="/tmp/Keys-$(/usr/bin/uuidgen)"
26+
OCBin="${OCPath}/OpenCore.efi"
27+
RootCA="${KeyPath}/ca.pem"
28+
PrivKey="${KeyPath}/privatekey.cer"
29+
PubKey="${KeyPath}/vault.pub"
30+
31+
if [ ! -d "${OCPath}" ]; then
32+
abort "Path ${OCPath} is missing!"
33+
fi
34+
35+
if [ ! -f "${OCBin}" ]; then
36+
abort "OpenCore.efi is missing!"
37+
fi
38+
39+
if [ ! -x ./RsaTool ] || [ ! -x ./create_vault.sh ]; then
40+
if [ -f ./RsaTool ]; then
41+
/bin/chmod a+x ./RsaTool || abort "Failed to set permission for RsaTool"
42+
else
43+
abort "Failed to find RsaTool!"
44+
fi
45+
46+
if [ -f ./create_vault.sh ]; then
47+
/bin/chmod a+x ./create_vault.sh || abort "Failed to set permission for create_vault.sh"
48+
else
49+
abort "Failed to find create_vault.sh!"
50+
fi
51+
fi
52+
53+
trap cleanup EXIT INT TERM
54+
55+
if [ ! -d "${KeyPath}" ]; then
56+
/bin/mkdir -p "${KeyPath}" || abort "Failed to create path ${KeyPath}"
57+
fi
58+
59+
./create_vault.sh "${OCPath}" || abort "create_vault.sh returns errors!"
60+
61+
if [ ! -f "${RootCA}" ]; then
62+
/usr/bin/openssl genrsa -out "${RootCA}" 2048 || abort "Failed to generate CA"
63+
if [ -f "${PrivKey}" ]; then
64+
echo "WARNING: Private key exists without CA"
65+
fi
66+
fi
67+
68+
/bin/rm -fP "${PrivKey}" || abort "Failed to remove ${PrivKey}"
69+
echo "Issuing a new private key..."
70+
/usr/bin/openssl req -new -x509 -key "${RootCA}" -out "${PrivKey}" -days 1825 -subj "/C=WO/L=127.0.0.1/O=Acidanthera/OU=Acidanthera OpenCore/CN=Greetings from Acidanthera and WWHC" || abort "Failed to issue private key!"
71+
72+
/bin/rm -fP "${PubKey}" || abort "Failed to remove ${PubKey}"
73+
echo "Getting public key based off private key..."
74+
./RsaTool -cert "${PrivKey}" > "${PubKey}" || abort "Failed to get public key"
75+
76+
echo "Signing ${OCBin}..."
77+
./RsaTool -sign "${OCPath}/vault.plist" "${OCPath}/vault.sig" "${PubKey}" || abort "Failed to patch ${PubKey}"
78+
79+
echo "Bin-patching ${OCBin}..."
80+
off=$(($(/usr/bin/strings -a -t d "${OCBin}" | /usr/bin/grep "=BEGIN OC VAULT=" | /usr/bin/cut -f1 -d' ') + 16))
81+
if [ "${off}" -le 16 ]; then
82+
abort "${OCBin} is borked"
83+
fi
84+
85+
/bin/dd of="${OCBin}" if="${PubKey}" bs=1 seek="${off}" count=528 conv=notrunc || abort "Failed to bin-patch ${OCBin}"
86+
87+
echo "All done!"
88+
exit 0
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
3+
#
4+
# Copyright © 2020 Rodion Shingarev. All rights reserved.
5+
# Slight optimizations by PMheart and vit9696.
6+
#
7+
8+
if [ "$1" = "install" ]; then
9+
SELFNAME=$(basename "$0")
10+
SELFDIR=$(dirname "$0")
11+
cd "$SELFDIR" || exit 1
12+
sudo defaults write com.apple.loginwindow LogoutHook "$(pwd)/${SELFNAME}"
13+
exit 0
14+
fi
15+
16+
if [ ! -x /usr/bin/dirname ] || [ ! -x /usr/sbin/nvram ] || [ ! -x /bin/rm ] || [ ! -x /usr/sbin/diskutil ] || [ ! -x /bin/cp ] ; then
17+
abort "Unix environment is broken!"
18+
fi
19+
20+
thisDir="$(/usr/bin/dirname "${0}")"
21+
cd "${thisDir}" || abort "Failed to enter working directory!"
22+
23+
if [ ! -x ./nvramdump ]; then
24+
abort "nvramdump is not found!"
25+
fi
26+
27+
abort() {
28+
echo "Fatal error: ${1}"
29+
# echo "Fatal error: ${1}" >> error.log
30+
exit 1
31+
}
32+
33+
rm -f /tmp/nvram.plist
34+
./nvramdump || abort "failed to save nvram.plist!"
35+
36+
UUID="$(nvram 4D1FDA02-38C7-4A6A-9CC6-4BCCA8B30102:boot-path | /usr/bin/sed 's/.*GPT,\([^,]*\),.*/\1/')"
37+
if [ "$(printf '%s' "${UUID}" | /usr/bin/wc -c)" -eq 36 ] && [ -z "$(echo "${UUID}" | /usr/bin/sed 's/[-0-9A-F]//g')" ]; then
38+
/usr/sbin/diskutil mount "${UUID}" || abort "Failed to mount ${UUID}!"
39+
p="$(/usr/sbin/diskutil info "${UUID}" | /usr/bin/sed -n 's/.*Mount Point: *//p')"
40+
if ! cmp -s /tmp/nvram.plist "${p}/nvram.plist"
41+
then
42+
/bin/cp /tmp/nvram.plist "${p}/nvram.plist" || abort "Failed to copy nvram.plist!"
43+
fi
44+
/usr/sbin/diskutil unmount "${UUID}" || abort "Failed to unmount ${UUID}!"
45+
exit 0
46+
else
47+
abort "Illegal UUID or unknown loader!"
48+
fi
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
LogoutHook
2+
==========
3+
4+
## Installation
5+
```sudo defaults write com.apple.loginwindow LogoutHook /path/to/LogoutHook.command```
6+
7+
or
8+
9+
```/path/to/LogoutHook.command install```
34.7 KB
Binary file not shown.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/bin/sh
2+
3+
# shim-to-cert.tool - Extract OEM signing certificate public key (and full db, dbx if present) from GRUB shim file.
4+
#
5+
# Copyright (c) 2021, Michael Beaton. All rights reserved.<BR>
6+
# SPDX-License-Identifier: BSD-3-Clause
7+
#
8+
9+
if [ -z "$1" ]; then
10+
echo "Usage: $0 {shimfile}"
11+
exit 1
12+
fi
13+
14+
# require binutils and openssl
15+
command -v objcopy >/dev/null 2>&1 || { echo >&2 "objcopy not found - please install binutils package."; exit 1; }
16+
command -v openssl >/dev/null 2>&1 || { echo >&2 "openssl not found - please install openssl package."; exit 1; }
17+
18+
sectfile=$(mktemp) || exit 1
19+
20+
# make certain we have output file name, as objcopy will trash input file without it
21+
if [ "x$sectfile" = "x" ]; then
22+
echo >&2 "Error creating tempfile!"
23+
exit 1
24+
fi
25+
26+
# extract .vendor_cert section
27+
objcopy -O binary -j .vendor_cert "$1" "$sectfile" || exit 1
28+
29+
if [ ! -s "$sectfile" ] ; then
30+
echo >&2 "No .vendor_cert section in $1."
31+
rm "$sectfile"
32+
exit 1
33+
fi
34+
35+
# xargs trims white space
36+
vendor_authorized_size=$(dd if="$sectfile" ibs=1 skip=0 count=4 2>/dev/null | od -t u4 -An | xargs) || { rm "$sectfile"; exit 1; }
37+
vendor_deauthorized_size=$(dd if="$sectfile" ibs=1 skip=4 count=4 2>/dev/null | od -t u4 -An | xargs) || { rm "$sectfile"; exit 1; }
38+
vendor_authorized_offset=$(dd if="$sectfile" ibs=1 skip=8 count=4 2>/dev/null | od -t u4 -An | xargs) || { rm "$sectfile"; exit 1; }
39+
vendor_deauthorized_offset=$(dd if="$sectfile" ibs=1 skip=12 count=4 2>/dev/null | od -t u4 -An | xargs) || { rm "$sectfile"; exit 1; }
40+
41+
# extract cert or db
42+
certfile=$(mktemp) || { rm "$sectfile"; exit 1; }
43+
44+
dd if="$sectfile" ibs=1 skip="$vendor_authorized_offset" count="$vendor_authorized_size" 2>/dev/null > "$certfile" || { rm "$sectfile"; rm "$certfile"; exit 1; }
45+
46+
# extract dbx
47+
if [ "$vendor_deauthorized_size" -ne "0" ]; then
48+
dd if="$sectfile" ibs=1 skip="$vendor_deauthorized_offset" count="$vendor_deauthorized_size" 2>/dev/null > "vendor.dbx" || { rm "$sectfile"; rm "$certfile"; exit 1; }
49+
echo "Secure Boot block list found and saved as vendor.dbx."
50+
fi
51+
52+
rm "$sectfile"
53+
54+
# valid as single cert?
55+
openssl x509 -noout -inform der -in "$certfile" 2>/dev/null
56+
57+
if [ $? -ne 0 ]; then
58+
# require efitools
59+
command -v sig-list-to-certs >/dev/null 2>&1 || { echo >&2 "sig-list-to-certs not found - please install efitools package."; rm "$certfile"; exit 1; }
60+
61+
certsdir=$(mktemp -d) || { rm "$certfile"; exit 1; }
62+
63+
sig-list-to-certs "$certfile" "${certsdir}/vendor" 1>/dev/null
64+
65+
if [ $? -ne 0 ]; then
66+
echo >&2 "ERROR: vendor_authorized contents cannot be processed as cert file or sig list."
67+
68+
rm -rf "$certsdir"
69+
rm "$certfile"
70+
71+
exit 1
72+
fi
73+
74+
cp "$certfile" vendor.db
75+
echo "Secure Boot allow list found and saved as vendor.db - single cert may not be sufficient to start distro."
76+
77+
# fails when count .der files != 1
78+
cp "$certsdir"/*.der "$certfile" 2>/dev/null
79+
80+
if [ $? -ne 0 ]; then
81+
certcount=$(find "$certsdir" -maxdepth 1 -name "*.der" | wc -l)
82+
83+
if [ "$certcount" -ne "0" ]; then
84+
cp "$certsdir"/*.der .
85+
86+
echo "Extracted multiple signing keys:"
87+
pwd=$(pwd)
88+
cd "$certsdir" || { rm -rf "$certsdir"; rm "$certfile"; exit 1; }
89+
ls -1 ./*.der
90+
cd "$pwd" || { rm -rf "$certsdir"; rm "$certfile"; exit 1; }
91+
fi
92+
93+
rm -rf "$certsdir"
94+
rm "$certfile"
95+
96+
exit 0
97+
fi
98+
99+
rm -rf "$certsdir"
100+
fi
101+
102+
# outfile name from cert CN
103+
certname=$(openssl x509 -noout -subject -inform der -in "$certfile" | sed 's/^subject=.*CN *=[ \"]*//' | sed 's/[,\/].*//' | sed 's/ *//g') || { rm "$certfile"; exit 1; }
104+
outfile="${certname}.pem"
105+
106+
openssl x509 -inform der -in "$certfile" -out "$outfile" || { rm "$certfile"; exit 1; }
107+
108+
rm "$certfile"
109+
110+
echo "Certificate extracted as ${outfile}."

0 commit comments

Comments
 (0)