-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure.sh
More file actions
executable file
·199 lines (162 loc) · 5.09 KB
/
Copy pathsecure.sh
File metadata and controls
executable file
·199 lines (162 loc) · 5.09 KB
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
191
192
193
194
195
196
197
198
199
#!/bin/bash
set -e
set -u
set -o pipefail
SRC=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
if ! type -t config_value &>/dev/null; then
source "${SRC}/utils.sh"
fi
KEYS="${BUILD}/.keys"
# Android Verified Boot (AVB)
AVB_KEY="avb.key"
AVB_PUB_KEY="avb_pub.key"
# Trusted Applications
TA_KEY="ta.pem"
TA_PUB_KEY="ta_pub.pem"
function get_ta_keys {
local ta_key_config=$(config_value "$1" secure.ta_key)
local -n ta_key_ref="$2"
local ta_pub_key_config=$(config_value "$1" secure.ta_pub_key)
local -n ta_pub_key_ref="$3"
# check private/public keys in config
if [ -n "${ta_key_config}" ]; then
if [ -a "${ta_key_config}" ]; then
ta_key_ref="${ta_key_config}"
else
error_exit "TA key not found: ${ta_key_config}"
fi
fi
if [ -n "${ta_pub_key_config}" ]; then
if [ -a "${ta_pub_key_config}" ]; then
ta_pub_key_ref="${ta_pub_key_config}"
else
error_exit "TA public key not found: ${ta_pub_key_config}"
fi
fi
[ -n "${ta_key_ref}" ] && [ -n "${ta_pub_key_ref}" ] && return
# check private/public keys under ${KEYS}
if [ -a "${KEYS}/${TA_KEY}" ] && [ -a "${KEYS}/${TA_PUB_KEY}" ]; then
ta_key_ref="${KEYS}/${TA_KEY}"
ta_pub_key_ref="${KEYS}/${TA_PUB_KEY}"
fi
}
function generate_ta_keys {
local ta_key="${KEYS}/${TA_KEY}"
local ta_pub_key="${KEYS}/${TA_PUB_KEY}"
! [ -d "${KEYS}" ] && mkdir -p "${KEYS}"
openssl genrsa -out "${ta_key}" 4096
openssl rsa -in "${ta_key}" -out "${ta_pub_key}" --pubout
printf "TA keys generated here:\n%s\n%s\n" "${ta_key}" "${ta_pub_key}"
}
function get_avb_key {
local avb_key_config=$(config_value "$1" secure.avb_key)
local -n avb_key_ref="$2"
if [ -n "${avb_key_config}" ]; then
if [ -a "${avb_key_config}" ]; then
avb_key_ref="${avb_key_config}"
else
error_exit "AVB key not found: ${avb_key_config}"
fi
else
if [ -a "${KEYS}/${AVB_KEY}" ]; then
avb_key_ref="${KEYS}/${AVB_KEY}"
fi
fi
}
function get_avb_pub_key {
local avb_pub_key_config=$(config_value "$1" secure.avb_pub_key)
local -n avb_pub_key_ref="$2"
if [ -n "${avb_pub_key_config}" ]; then
if [ -a "${avb_pub_key_config}" ]; then
avb_pub_key_ref="${avb_pub_key_config}"
else
error_exit "AVB public key not found: ${avb_pub_key_config}"
fi
else
if [ -a "${KEYS}/${AVB_PUB_KEY}" ]; then
avb_pub_key_ref="${KEYS}/${AVB_PUB_KEY}"
fi
fi
}
# Helper function to get AVB key path directly (for command substitution)
# Usage: get_avb_key_path <config> [pub|priv]
function get_avb_key_path {
local config="$1"
local key_type="${2:-priv}"
local key_path=""
if [[ "${key_type}" == "pub" ]]; then
local avb_pub_key_config=$(config_value "${config}" secure.avb_pub_key)
if [ -n "${avb_pub_key_config}" ] && [ -f "${avb_pub_key_config}" ]; then
key_path="${avb_pub_key_config}"
elif [ -f "${KEYS}/${AVB_PUB_KEY}" ]; then
key_path="${KEYS}/${AVB_PUB_KEY}"
fi
else
local avb_key_config=$(config_value "${config}" secure.avb_key)
if [ -n "${avb_key_config}" ] && [ -f "${avb_key_config}" ]; then
key_path="${avb_key_config}"
elif [ -f "${KEYS}/${AVB_KEY}" ]; then
key_path="${KEYS}/${AVB_KEY}"
fi
fi
echo "${key_path}"
}
function generate_avb_keys {
local avb_key="${KEYS}/${AVB_KEY}"
local avb_pub_key="${KEYS}/${AVB_PUB_KEY}"
! [ -d "${KEYS}" ] && mkdir -p "${KEYS}"
avbtool_env
openssl genrsa -out "${avb_key}" 4096
avbtool extract_public_key --key "${avb_key}" --output "${avb_pub_key}"
printf "AVB keys generated here:\n%s\n%s\n" "${avb_key}" "${avb_pub_key}"
}
function generate_secure_package {
local board=$(board_name "$1")
local plat=$(config_value "$1" plat)
local out_dir="$2"
local package="secure_${board}.zip"
! [ -d "${KEYS}" ] && mkdir -p "${KEYS}"
pushd "${KEYS}"
[ -a "${package}" ] && rm "${package}"
# add Trusted Applications keys
local ta_key=""
local ta_pub_key=""
get_ta_keys "$1" ta_key ta_pub_key
if [ -n "${ta_key}" ]; then
zip -ju "${package}" "${ta_key}"
fi
if [ -n "${ta_pub_key}" ]; then
zip -ju "${package}" "${ta_pub_key}"
fi
# add Android Verified Boot keys
local avb_key=""
local avb_pub_key=""
get_avb_key "$1" avb_key
if [ -n "${avb_key}" ]; then
zip -ju "${package}" "${avb_key}"
fi
get_avb_pub_key "$1" avb_pub_key
if [ -n "${avb_pub_key}" ]; then
zip -ju "${package}" "${avb_pub_key}"
fi
mv "${package}" "${out_dir}/"
popd
}
# standalone main for secure.sh
function secure_usage {
cat <<DELIM__
usage: $(basename "$0") function
Functions supported can be found in "$0"
DELIM__
}
function secure_main {
if ! [ $# -eq 1 ]; then
secure_usage
else
local command="$1"
"${command}"
fi
}
if [ "$0" = "$BASH_SOURCE" ]; then
secure_main "$@"
fi