-
Notifications
You must be signed in to change notification settings - Fork 31
/
run-nitro-enclaves-acm-tests
executable file
·402 lines (318 loc) · 11.4 KB
/
run-nitro-enclaves-acm-tests
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/bin/bash
# Error codes related to package installation files
ERROR_MISSING_BIN=1
ERROR_MISSING_FILE=2
ERROR_CODE=0
# Error codes which get stacked when a specifc action fails
ERROR_SETUP=3
ERROR_INIT_TOKEN=4
ERROR_VERIFY=5
ERROR_DECRYPT=6
ERROR_REFRESH=7
ERROR_RELEASE=8
ERROR_CRYPTOGRAPHIC_TESTS=9
ERROR_LIST_MODULES=10
ERROR_P11_LIST_TOKEN=11
ERRORS=""
# Resources which are to be downloaded locally
ACM_FOR_NE_REPO_S3="acm-for-ne-rpm-testing-deps"
ACM_FOR_NE_REPO_ARCH="aws-nitro-enclaves-acm.tar.gz"
ACM_FOR_NE_REPO_DIR="aws-nitro-enclaves-acm"
# Binaries installed through the previous resources
P11NE_CLI="p11ne-cli"
P11NE_DB="p11ne-db"
BINDIR="/usr/bin"
# Identifiers used throughout testing
KEY_PATH="key.pem"
KEY_ID="1"
KEY_DB="key.db"
KEY_LABEL="mykey"
KEY_OUT_FILE="key"
KMS_KEY_ID="feb1bc22-0fda-424a-b451-231334b0ca03"
KMS_KEY_REGION="us-east-1"
TOKEN_LABEL_RSA="test-token-rsa"
TOKEN_LABEL_SECP384R1_EC="test-token-secp384r1-ec"
TOKEN_PIN="1234"
TMP_TEST_DIR=".tmp-acm-for-ne"
TMP_CRYPTOGRAPHIC_TESTS_OUT=".cryptographic_tests_results.out"
P11_KIT="p11-kit"
P11_TOOL="p11tool"
# Stores the NE ACM service file and a flag which tells
# whether the service was active prior to starting the tests
# (0 = is active / anything else = is not active)
NE_ACM_SERVICE="nitro-enclaves-acm.service"
NE_ACM_SERVICE_RUNNING=0
# Used for taking into account the delay with which the describe-token
# result might be reported (during testing, the "ttl_secs" field was
# sometimes reported with 1 second less than the expected value)
EPS=1
# Flag used for printing errors encountered during tests
VERBOSE=0
# Wait a bit after the NE ACM service has been stopped in order to
# allow the p11 enclave to terminate
SLEEP_AFTER_SERVICE_STOP=10
INIT_DIR="$(pwd)"
# Binaries which should exist under $PATH after package installation
acm_ne_bins_array=("nitro-cli" \
"vsock-proxy" \
"p11ne-agent" \
"p11ne-client" \
)
# Files brought in by the RPM(s), specified by full path
acm_ne_files_array=("/lib/systemd/system/nitro-enclaves-allocator.service" \
"/lib/systemd/system/nitro-enclaves-vsock-proxy.service" \
"/lib/systemd/system/nitro-enclaves-acm.service" \
"/usr/share/nitro_enclaves/p11ne/p11ne.eif" \
"/usr/share/nitro_enclaves/p11ne/image-measurements.json" \
"/usr/share/nitro_enclaves/p11ne/acm.example.yaml" \
)
function check_usage {
if [[ $1 -gt 2 ]]; then
echo "Usage: $0 [CUSTOM_DEPENDENCIES_S3_BUCKET] [KMS_KEY_ID]"
exit $ERROR_SETUP
fi
if [[ $1 -ge 2 ]]; then
KMS_KEY_ID="$3"
fi
if [[ $1 -ge 1 ]]; then
ACM_FOR_NE_REPO_S3="$2"
fi
}
function configure_aux_tools {
local prev_dir
# Fetch GitHub repo, as some tools do not come with the RPM
prev_dir=$(pwd) && mkdir -p $TMP_TEST_DIR && cd $TMP_TEST_DIR || return
aws s3 cp s3://$ACM_FOR_NE_REPO_S3/$ACM_FOR_NE_REPO_ARCH . > /dev/null || exit $ERROR_SETUP
tar -zxf $ACM_FOR_NE_REPO_ARCH || exit $ERROR_SETUP
# Install p11ne-cli and p11ne-db
sudo install -D -m 0755 $ACM_FOR_NE_REPO_DIR/tools/$P11NE_CLI $BINDIR || exit $ERROR_SETUP
sudo install -D -m 0755 $ACM_FOR_NE_REPO_DIR/tools/$P11NE_DB $BINDIR || exit $ERROR_SETUP
}
function ensure_testhelpers {
local prev_dir
mkdir -p "$HOME"/.tmp-helper
cp -r ./$ACM_FOR_NE_REPO_DIR/tests/helpers "$HOME"/.tmp-helper
prev_dir=$(pwd) \
&& cd "$HOME"/.tmp-helper/helpers \
&& cargo build --release 2> /dev/null
cd "$prev_dir" || return
cp "$HOME"/.tmp-helper/helpers/target/release/testhelpers ./$ACM_FOR_NE_REPO_DIR/tests/
rm -rf "$HOME"/.tmp-helper
rm -rf ./$ACM_FOR_NE_REPO_DIR/tests/results
}
function test_acm_ne_bins() {
local ret
echo -n "Checking that ACM NE binaries are installed .........."
for binary in "${acm_ne_bins_array[@]}"; do
ret=$(which "$binary" 2>&1)
echo "$ret" | grep "no $binary in" > /dev/null 2>&1 && echo " [FAIL]" && ERROR_CODE=$ERROR_MISSING_BIN && return
done
echo "[PASS]"
}
function test_acm_ne_files() {
echo -n "Checking that ACM NE files are installed .........."
for file in "${acm_ne_files_array[@]}"; do
if [ ! -f "$file" ]; then
echo " [FAIL]" && ERROR_CODE=$ERROR_MISSING_FILE && return
fi
done
echo "[PASS]"
}
function test_token_rsa_key {
local out
local key_uri
local key_uri_base
local ttl_init
local ttl_end
echo -n "Testing token initialized from RSA key .......... "
# Generate and use and RSA key for both sign / verify
# and encrypt / decrypt
openssl genrsa -out key.pem 2048 > /dev/null 2>&1
$P11NE_DB pack-key --id $KEY_ID --label $KEY_LABEL --key-file $KEY_PATH --out-file $KEY_OUT_FILE --kms-key-id $KMS_KEY_ID --kms-region $KMS_KEY_REGION
out=$($P11NE_CLI init-token \
--key-db $KEY_DB \
--label $TOKEN_LABEL_RSA \
--pin $TOKEN_PIN)
echo "$out" | grep "\"Ok\": \"None\"" > /dev/null || ERRORS="$ERRORS $ERROR_INIT_TOKEN"
out=$($P11NE_CLI describe-device)
ttl_init=$($P11NE_CLI describe-device \
| jq '.Ok.DeviceDescription.tokens[] | select(.label == "test-token-rsa")' \
| grep "ttl_secs" \
| sed -e "s/[[:space:]]\+/ /g" \
| cut -d' ' -f3)
out=$($P11NE_CLI describe-token \
--label $TOKEN_LABEL_RSA \
--pin $TOKEN_PIN)
key_uri=$(echo "$out" | jq '.' | grep "\"uri\"" | cut -d'"' -f4)
key_uri_base=$(echo "$key_uri" | cut -d' ' -f 2 | cut -d'"' -f2 | rev | cut -d'=' -f2- | rev)
# Test sign / verify
echo "Sign This" > input
openssl dgst \
-keyform engine \
-engine pkcs11 \
-sign "${key_uri_base}=private?pin-value=${TOKEN_PIN}" \
-out test.sig input 2> /dev/null
out=$(openssl dgst \
-keyform engine \
-engine pkcs11 \
-verify "${key_uri_base}=public" \
-signature test.sig input 2> /dev/null)
echo "$out" | grep "Verified OK" > /dev/null || ERRORS="$ERRORS $ERROR_VERIFY"
# Test encrypt / decrypt
echo "Encrypt This" > input
openssl pkeyutl \
-keyform engine \
-engine pkcs11 \
-encrypt \
-pubin \
-inkey "${key_uri_base}=public" \
-out test.crypt -in input 2> /dev/null
openssl pkeyutl \
-keyform engine \
-engine pkcs11 \
-decrypt \
-inkey "${key_uri_base}=private?pin-value=${TOKEN_PIN}" \
-in test.crypt > test.decrypt 2> /dev/null
out=$(diff input test.decrypt)
[[ -z $out ]] || ERRORS="$ERRORS $ERROR_DECRYPT"
# Test refresh token
out=$($P11NE_CLI refresh-token --label $TOKEN_LABEL_RSA --pin $TOKEN_PIN)
echo "$out" | grep "\"Ok\": \"None\"" > /dev/null || ERRORS="$ERRORS $ERROR_REFRESH"
out=$($P11NE_CLI describe-token --label $TOKEN_LABEL_RSA --pin $TOKEN_PIN)
ttl_end=$(echo "$out" | jq '.' | grep "ttl_secs" | sed -e "s/[[:space:]]\+/ /g" | cut -d' ' -f3 | sed "s/.$//")
[[ $(( ttl_end + EPS )) -ge $ttl_init ]] || ERRORS="$ERRORS $ERROR_REFRESH"
# Release token
out=$($P11NE_CLI release-token --label $TOKEN_LABEL_RSA --pin $TOKEN_PIN)
echo "$out" | grep "\"Ok\": \"None\"" > /dev/null || ERRORS="$ERRORS $ERROR_RELEASE"
out=$($P11NE_CLI describe-token --label $TOKEN_LABEL_RSA --pin $TOKEN_PIN)
echo "$out" | grep "\"Err\": \"TokenNotFound\"" > /dev/null || ERRORS="$ERRORS $ERROR_RELEASE"
if [[ -z $ERRORS ]]; then
echo "[PASS]"
else
echo "[FAIL]"
fi
}
function test_token_ec_secp384r1_key {
local out
local key_uri
local key_uri_base
local ttl_init
local ttl_end
echo -n "Testing token initialized from secp384r1 EC key .......... "
# Generate and use an secp384r1 EC key
openssl genrsa -out key.pem 2048 > /dev/null 2>&1
openssl ecparam -name secp384r1 -genkey -noout -out key.pem
$P11NE_DB pack-key --id $KEY_ID --label $KEY_LABEL --key-file $KEY_PATH --out-file $KEY_OUT_FILE --kms-key-id $KMS_KEY_ID --kms-region $KMS_KEY_REGION
out=$($P11NE_CLI init-token \
--key-db $KEY_DB \
--label $TOKEN_LABEL_SECP384R1_EC \
--pin $TOKEN_PIN)
echo "$out" | grep "\"Ok\": \"None\"" > /dev/null || ERRORS="$ERRORS $ERROR_INIT_TOKEN"
out=$($P11NE_CLI describe-device)
ttl_init=$($P11NE_CLI describe-device \
| jq '.Ok.DeviceDescription.tokens[] | select(.label == "test-token-secp384r1-ec")' \
| grep "ttl_secs" \
| sed -e "s/[[:space:]]\+/ /g" \
| cut -d' ' -f3)
out=$($P11NE_CLI describe-token \
--label $TOKEN_LABEL_SECP384R1_EC \
--pin $TOKEN_PIN)
key_uri=$(echo "$out" | jq '.' | grep "\"uri\"" | cut -d'"' -f4)
key_uri_base=$(echo "$key_uri" | cut -d' ' -f 2 | cut -d'"' -f2 | rev | cut -d'=' -f2- | rev)
# Test sign / verify
echo "Sign This" > input
openssl dgst \
-keyform engine \
-engine pkcs11 \
-sign "${key_uri_base}=private?pin-value=${TOKEN_PIN}" \
-out test.sig input 2> /dev/null
out=$(openssl dgst \
-keyform engine \
-engine pkcs11 \
-verify "${key_uri_base}=public" \
-signature test.sig input 2> /dev/null)
echo "$out" | grep "Verified OK" > /dev/null || ERRORS="$ERRORS $ERROR_VERIFY"
# Test refresh token
out=$($P11NE_CLI refresh-token --label $TOKEN_LABEL_SECP384R1_EC --pin $TOKEN_PIN)
echo "$out" | grep "\"Ok\": \"None\"" > /dev/null || ERRORS="$ERRORS $ERROR_REFRESH"
out=$($P11NE_CLI describe-token --label $TOKEN_LABEL_SECP384R1_EC --pin $TOKEN_PIN)
ttl_end=$(echo "$out" | jq '.' | grep "ttl_secs" | sed -e "s/[[:space:]]\+/ /g" | cut -d' ' -f3 | sed "s/.$//")
[[ $(( ttl_end + EPS )) -ge $ttl_init ]] || ERRORS="$ERRORS $ERROR_REFRESH"
# Release token
out=$($P11NE_CLI release-token --label $TOKEN_LABEL_SECP384R1_EC --pin $TOKEN_PIN)
echo "$out" | grep "\"Ok\": \"None\"" > /dev/null || ERRORS="$ERRORS $ERROR_RELEASE"
out=$($P11NE_CLI describe-token --label $TOKEN_LABEL_SECP384R1_EC --pin $TOKEN_PIN)
echo "$out" | grep "\"Err\": \"TokenNotFound\"" > /dev/null || ERRORS="$ERRORS $ERROR_RELEASE"
if [[ -z $ERRORS ]]; then
echo "[PASS]"
else
echo "[FAIL]"
fi
}
function test_cryptographic_capabilities {
echo -n "Cryptographic tests .......... "
# Stop an enclave if it is already running
if [[ $NE_ACM_SERVICE_RUNNING -eq 0 ]]; then
sudo systemctl stop $NE_ACM_SERVICE
$P11NE_CLI stop > /dev/null 2>&1
sleep $SLEEP_AFTER_SERVICE_STOP
fi
ensure_testhelpers
./$ACM_FOR_NE_REPO_DIR/tests/testtool openssl \
--kms-key-id $KMS_KEY_ID \
--kms-region $KMS_KEY_REGION > $TMP_CRYPTOGRAPHIC_TESTS_OUT
grep "FAILED" $TMP_CRYPTOGRAPHIC_TESTS_OUT > /dev/null && ERRORS="$ERRORS $ERROR_CRYPTOGRAPHIC_TESTS"
if [[ "$(grep -c FAILED $TMP_CRYPTOGRAPHIC_TESTS_OUT)" == "0" ]]; then
echo "[PASS]"
else
echo "[FAIL]"
fi
rm -f $TMP_CRYPTOGRAPHIC_TESTS_OUT
sudo systemctl restart $NE_ACM_SERVICE
sleep $SLEEP_AFTER_SERVICE_STOP
}
function test_al2_libp11 {
local out
echo -n "Testing AL2 libp11 .......... "
$P11NE_CLI init-token \
--key-db $KEY_DB \
--label $TOKEN_LABEL_RSA \
--pin $TOKEN_PIN > /dev/null
out=$($P11_KIT list-modules 2> /dev/null)
echo "$out" | grep "manufacturer: Amazon" > /dev/null || ERRORS="$ERRORS $ERROR_LIST_MODULES"
out="$($P11_TOOL --list-all 2> /dev/null | grep $TOKEN_LABEL_RSA | cut -d' ' -f3)"
if [[ -z $out ]]; then
ERRORS="$ERRORS $ERROR_P11_LIST_TOKEN"
echo "[FAIL]"
else
echo "[PASS]"
fi
# Release token
$P11NE_CLI release-token --label $TOKEN_LABEL_RSA --pin $TOKEN_PIN > /dev/null
}
function run_tests() {
NE_ACM_SERVICE_RUNNING=$(systemctl is-active --quiet $NE_ACM_SERVICE)
test_acm_ne_bins
test_acm_ne_files
configure_aux_tools
if [[ $NE_ACM_SERVICE_RUNNING -eq 1 ]]; then
sudo systemctl restart $NE_ACM_SERVICE
fi
test_token_rsa_key
test_token_ec_secp384r1_key
test_cryptographic_capabilities
test_al2_libp11
if [[ $NE_ACM_SERVICE_RUNNING -eq 1 ]]; then
sudo systemctl stop $NE_ACM_SERVICE
fi
cd "$INIT_DIR" || return
rm -rf $TMP_TEST_DIR
if [[ "$VERBOSE" == 1 ]]; then
echo "Error string: $ERRORS"
fi
}
check_usage $# "$@"
run_tests
if [ $ERROR_CODE -ne 0 ]; then
echo "Tests exit code: $ERROR_CODE" && exit $ERROR_CODE
fi