forked from Mbed-TLS/mbedtls
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontext-info.sh
executable file
·440 lines (375 loc) · 13.6 KB
/
context-info.sh
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/bin/sh
# context-info.sh
#
# Copyright The Mbed TLS Contributors
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This program is intended for testing the ssl_context_info program
#
set -eu
if ! cd "$(dirname "$0")"; then
exit 125
fi
# Variables
THIS_SCRIPT_NAME=$(basename "$0")
PROG_PATH="../programs/ssl/ssl_context_info"
OUT_FILE="ssl_context_info.log"
IN_DIR="data_files/base64"
USE_VALGRIND=0
T_COUNT=0
T_PASSED=0
T_FAILED=0
# Functions
print_usage() {
echo "Usage: $0 [options]"
printf " -h|--help\tPrint this help.\n"
printf " -m|--memcheck\tUse valgrind to check the memory.\n"
}
# Print test name <name>
print_name() {
printf "%s %.*s " "$1" $(( 71 - ${#1} )) \
"........................................................................"
}
# Print header to the test output file <test name> <file path> <test command>
print_header()
{
date="$(date)"
echo "******************************************************************" > $2
echo "* File created by: $THIS_SCRIPT_NAME" >> $2
echo "* Test name: $1" >> $2
echo "* Date: $date" >> $2
echo "* Command: $3" >> $2
echo "******************************************************************" >> $2
echo "" >> $2
}
# Print footer at the end of file <file path>
print_footer()
{
echo "" >> $1
echo "******************************************************************" >> $1
echo "* End command" >> $1
echo "******************************************************************" >> $1
echo "" >> $1
}
# Use the arguments of this script
get_options() {
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
-m|--memcheck)
USE_VALGRIND=1
;;
*)
echo "Unknown argument: '$1'"
print_usage
exit 1
;;
esac
shift
done
}
# Current test failed
fail()
{
T_FAILED=$(( $T_FAILED + 1))
FAIL_OUT="Fail.$T_FAILED""_$OUT_FILE"
echo "FAIL"
echo " Error: $1"
cp -f "$OUT_FILE" "$FAIL_OUT"
echo "Error: $1" >> "$FAIL_OUT"
}
# Current test passed
pass()
{
T_PASSED=$(( $T_PASSED + 1))
echo "PASS"
}
# Usage: run_test <name> <input file with b64 code> [ -arg <extra arguments for tested program> ] [option [...]]
# Options: -m <pattern that MUST be present in the output of tested program>
# -n <pattern that must NOT be present in the output of tested program>
# -u <pattern that must be UNIQUE in the output of tested program>
run_test()
{
TEST_NAME="$1"
RUN_CMD="$PROG_PATH -f $IN_DIR/$2"
if [ "-arg" = "$3" ]; then
RUN_CMD="$RUN_CMD $4"
shift 4
else
shift 2
fi
# prepend valgrind to our commands if active
if [ "$USE_VALGRIND" -gt 0 ]; then
RUN_CMD="valgrind --leak-check=full $RUN_CMD"
fi
T_COUNT=$(( $T_COUNT + 1))
print_name "$TEST_NAME"
# run tested program
print_header "$TEST_NAME" "$OUT_FILE" "$RUN_CMD"
eval "$RUN_CMD" >> "$OUT_FILE" 2>&1
print_footer "$OUT_FILE"
# check valgrind's results
if [ "$USE_VALGRIND" -gt 0 ]; then
if ! ( grep -F 'All heap blocks were freed -- no leaks are possible' "$OUT_FILE" &&
grep -F 'ERROR SUMMARY: 0 errors from 0 contexts' "$OUT_FILE" ) > /dev/null
then
fail "Memory error detected"
return
fi
fi
# check other assertions
# lines beginning with == are added by valgrind, ignore them, because we already checked them before
# lines with 'Serious error when reading debug info', are valgrind issues as well
# lines beginning with * are added by this script, ignore too
while [ $# -gt 0 ]
do
case $1 in
"-m")
if grep -v '^==' "$OUT_FILE" | grep -v 'Serious error when reading debug info' | grep -v "^*" | grep "$2" >/dev/null; then :; else
fail "pattern '$2' MUST be present in the output"
return
fi
;;
"-n")
if grep -v '^==' "$OUT_FILE" | grep -v 'Serious error when reading debug info' | grep -v "^*" | grep "$2" >/dev/null; then
fail "pattern '$2' MUST NOT be present in the output"
return
fi
;;
"-u")
if [ $(grep -v '^==' "$OUT_FILE"| grep -v 'Serious error when reading debug info' | grep -v "^*" | grep "$2" | wc -l) -ne 1 ]; then
fail "lines following pattern '$2' must be once in the output"
return
fi
;;
*)
echo "Unknown test: $1" >&2
exit 1
esac
shift 2
done
rm -f "$OUT_FILE"
pass
}
get_options "$@"
# Tests
run_test "Default configuration, server" \
"srv_def.txt" \
-n "ERROR" \
-u "major.* 2$" \
-u "minor.* 21$" \
-u "path.* 0$" \
-u "MBEDTLS_HAVE_TIME$" \
-u "MBEDTLS_X509_CRT_PARSE_C$" \
-u "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
-u "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
-u "MBEDTLS_SSL_SESSION_TICKETS$" \
-u "MBEDTLS_SSL_SESSION_TICKETS and client$" \
-u "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
-u "MBEDTLS_SSL_ALPN$" \
-u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
-u "cipher flags.* 0x00$" \
-u "Message-Digest.* SHA256$" \
-u "compression.* disabled$" \
-u "DTLS datagram packing.* enabled$" \
-n "Certificate" \
-n "bytes left to analyze from context"
run_test "Default configuration, client" \
"cli_def.txt" \
-n "ERROR" \
-u "major.* 2$" \
-u "minor.* 21$" \
-u "path.* 0$" \
-u "MBEDTLS_HAVE_TIME$" \
-u "MBEDTLS_X509_CRT_PARSE_C$" \
-u "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
-u "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
-u "MBEDTLS_SSL_SESSION_TICKETS$" \
-u "MBEDTLS_SSL_SESSION_TICKETS and client$" \
-u "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
-u "MBEDTLS_SSL_ALPN$" \
-u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
-u "cipher flags.* 0x00$" \
-u "Message-Digest.* SHA256$" \
-u "compression.* disabled$" \
-u "DTLS datagram packing.* enabled$" \
-u "cert. version .* 3$" \
-u "serial number.* 02$" \
-u "issuer name.* C=NL, O=PolarSSL, CN=PolarSSL Test CA$" \
-u "subject name.* C=NL, O=PolarSSL, CN=localhost$" \
-u "issued on.* 2019-02-10 14:44:06$" \
-u "expires on.* 2029-02-10 14:44:06$" \
-u "signed using.* RSA with SHA-256$" \
-u "RSA key size.* 2048 bits$" \
-u "basic constraints.* CA=false$" \
-n "bytes left to analyze from context"
run_test "Ciphersuite TLS-RSA-WITH-AES-256-CCM-8, server" \
"srv_ciphersuite.txt" \
-n "ERROR" \
-u "ciphersuite.* TLS-RSA-WITH-AES-256-CCM-8$" \
run_test "Ciphersuite TLS-RSA-WITH-AES-256-CCM-8, client" \
"cli_ciphersuite.txt" \
-n "ERROR" \
-u "ciphersuite.* TLS-RSA-WITH-AES-256-CCM-8$" \
run_test "No packing, server" \
"srv_no_packing.txt" \
-n "ERROR" \
-u "DTLS datagram packing.* disabled"
run_test "No packing, client" \
"cli_no_packing.txt" \
-n "ERROR" \
-u "DTLS datagram packing.* disabled"
run_test "DTLS CID, server" \
"srv_cid.txt" \
-n "ERROR" \
-u "in CID.* DE AD" \
-u "out CID.* BE EF"
run_test "DTLS CID, client" \
"cli_cid.txt" \
-n "ERROR" \
-u "in CID.* BE EF" \
-u "out CID.* DE AD"
run_test "No MBEDTLS_SSL_MAX_FRAGMENT_LENGTH, server" \
"srv_no_mfl.txt" \
-n "ERROR" \
-n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH"
run_test "No MBEDTLS_SSL_MAX_FRAGMENT_LENGTH, client" \
"cli_no_mfl.txt" \
-n "ERROR" \
-n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH"
run_test "No MBEDTLS_SSL_ALPN, server" \
"srv_no_alpn.txt" \
-n "ERROR" \
-n "MBEDTLS_SSL_ALPN"
run_test "No MBEDTLS_SSL_ALPN, client" \
"cli_no_alpn.txt" \
-n "ERROR" \
-n "MBEDTLS_SSL_ALPN"
run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, server" \
"srv_no_keep_cert.txt" \
-arg "--keep-peer-cert=0" \
-u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
-u "cipher flags.* 0x00" \
-u "compression.* disabled" \
-u "DTLS datagram packing.* enabled" \
-n "ERROR"
run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, client" \
"cli_no_keep_cert.txt" \
-arg "--keep-peer-cert=0" \
-u "ciphersuite.* TLS-ECDHE-RSA-WITH-CHACHA20-POLY1305-SHA256$" \
-u "cipher flags.* 0x00" \
-u "compression.* disabled" \
-u "DTLS datagram packing.* enabled" \
-n "ERROR"
run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, negative, server" \
"srv_no_keep_cert.txt" \
-m "Deserializing" \
-m "ERROR"
run_test "No MBEDTLS_SSL_KEEP_PEER_CERTIFICATE, negative, client" \
"cli_no_keep_cert.txt" \
-m "Deserializing" \
-m "ERROR"
run_test "Minimal configuration, server" \
"srv_min_cfg.txt" \
-n "ERROR" \
-n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
-n "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
-n "MBEDTLS_SSL_SESSION_TICKETS$" \
-n "MBEDTLS_SSL_SESSION_TICKETS and client$" \
-n "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
-n "MBEDTLS_SSL_ALPN$" \
run_test "Minimal configuration, client" \
"cli_min_cfg.txt" \
-n "ERROR" \
-n "MBEDTLS_SSL_MAX_FRAGMENT_LENGTH$" \
-n "MBEDTLS_SSL_ENCRYPT_THEN_MAC$" \
-n "MBEDTLS_SSL_SESSION_TICKETS$" \
-n "MBEDTLS_SSL_SESSION_TICKETS and client$" \
-n "MBEDTLS_SSL_DTLS_ANTI_REPLAY$" \
-n "MBEDTLS_SSL_ALPN$" \
run_test "MTU=10000" \
"mtu_10000.txt" \
-n "ERROR" \
-u "MTU.* 10000$"
run_test "MFL=1024" \
"mfl_1024.txt" \
-n "ERROR" \
-u "MFL.* 1024$"
run_test "Older version (v2.19.1)" \
"v2.19.1.txt" \
-n "ERROR" \
-u "major.* 2$" \
-u "minor.* 19$" \
-u "path.* 1$" \
-u "ciphersuite.* TLS-ECDHE-ECDSA-WITH-AES-128-CCM-8$" \
-u "Message-Digest.* SHA256$" \
-u "compression.* disabled$" \
-u "serial number.* 01:70:AF:40:B4:E6$" \
-u "issuer name.* CN=ca$" \
-u "subject name.* L=160001, OU=acc1, CN=device01$" \
-u "issued on.* 2020-03-06 09:50:18$" \
-u "expires on.* 2056-02-26 09:50:18$" \
-u "signed using.* ECDSA with SHA256$" \
-u "lifetime.* 0 sec.$" \
-u "MFL.* none$" \
-u "negotiate truncated HMAC.* disabled$" \
-u "Encrypt-then-MAC.* enabled$" \
-u "DTLS datagram packing.* enabled$" \
-u "verify result.* 0x00000000$" \
-n "bytes left to analyze from context"
run_test "Wrong base64 format" \
"def_bad_b64.txt" \
-m "ERROR" \
-u "The length of the base64 code found should be a multiple of 4" \
-n "bytes left to analyze from context"
run_test "Too much data at the beginning of base64 code" \
"def_b64_too_big_1.txt" \
-m "ERROR" \
-n "The length of the base64 code found should be a multiple of 4" \
run_test "Too much data in the middle of base64 code" \
"def_b64_too_big_2.txt" \
-m "ERROR" \
-n "The length of the base64 code found should be a multiple of 4" \
run_test "Too much data at the end of base64 code" \
"def_b64_too_big_3.txt" \
-m "ERROR" \
-n "The length of the base64 code found should be a multiple of 4" \
-u "bytes left to analyze from context"
run_test "Empty file as input" \
"empty.txt" \
-u "Finished. No valid base64 code found"
run_test "Not empty file without base64 code" \
"../../context-info.sh" \
-n "Deserializing"
run_test "Binary file instead of text file" \
"../../../programs/ssl/ssl_context_info" \
-m "ERROR" \
-u "Too many bad symbols detected. File check aborted" \
-n "Deserializing"
run_test "Decoder continues past 0xff character" \
"def_b64_ff.bin" \
-n "No valid base64" \
-u "ciphersuite.* TLS-"
# End of tests
echo
if [ $T_FAILED -eq 0 ]; then
echo "PASSED ( $T_COUNT tests )"
else
echo "FAILED ( $T_FAILED / $T_COUNT tests )"
fi
exit $T_FAILED