forked from Mbed-TLS/mbedtls
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathssl-opt.sh
executable file
·13859 lines (12739 loc) · 649 KB
/
ssl-opt.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# ssl-opt.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.
#
# Purpose
#
# Executes tests to prove various TLS/SSL options and extensions.
#
# The goal is not to cover every ciphersuite/version, but instead to cover
# specific options (max fragment length, truncated hmac, etc) or procedures
# (session resumption from cache or ticket, renego, etc).
#
# The tests assume a build with default options, with exceptions expressed
# with a dependency. The tests focus on functionality and do not consider
# performance.
#
set -u
# Limit the size of each log to 10 GiB, in case of failures with this script
# where it may output seemingly unlimited length error logs.
ulimit -f 20971520
ORIGINAL_PWD=$PWD
if ! cd "$(dirname "$0")"; then
exit 125
fi
# default values, can be overridden by the environment
: ${P_SRV:=../programs/ssl/ssl_server2}
: ${P_CLI:=../programs/ssl/ssl_client2}
: ${P_PXY:=../programs/test/udp_proxy}
: ${P_QUERY:=../programs/test/query_compile_time_config}
: ${OPENSSL_CMD:=openssl} # OPENSSL would conflict with the build system
: ${GNUTLS_CLI:=gnutls-cli}
: ${GNUTLS_SERV:=gnutls-serv}
: ${PERL:=perl}
guess_config_name() {
if git diff --quiet ../include/mbedtls/mbedtls_config.h 2>/dev/null; then
echo "default"
else
echo "unknown"
fi
}
: ${MBEDTLS_TEST_OUTCOME_FILE=}
: ${MBEDTLS_TEST_CONFIGURATION:="$(guess_config_name)"}
: ${MBEDTLS_TEST_PLATFORM:="$(uname -s | tr -c \\n0-9A-Za-z _)-$(uname -m | tr -c \\n0-9A-Za-z _)"}
O_SRV="$OPENSSL_CMD s_server -www -cert data_files/server5.crt -key data_files/server5.key"
O_CLI="echo 'GET / HTTP/1.0' | $OPENSSL_CMD s_client"
G_SRV="$GNUTLS_SERV --x509certfile data_files/server5.crt --x509keyfile data_files/server5.key"
G_CLI="echo 'GET / HTTP/1.0' | $GNUTLS_CLI --x509cafile data_files/test-ca_cat12.crt"
TCP_CLIENT="$PERL scripts/tcp_client.pl"
# alternative versions of OpenSSL and GnuTLS (no default path)
if [ -n "${OPENSSL_LEGACY:-}" ]; then
O_LEGACY_SRV="$OPENSSL_LEGACY s_server -www -cert data_files/server5.crt -key data_files/server5.key"
O_LEGACY_CLI="echo 'GET / HTTP/1.0' | $OPENSSL_LEGACY s_client"
else
O_LEGACY_SRV=false
O_LEGACY_CLI=false
fi
if [ -n "${OPENSSL_NEXT:-}" ]; then
O_NEXT_SRV="$OPENSSL_NEXT s_server -www -cert data_files/server5.crt -key data_files/server5.key"
O_NEXT_SRV_NO_CERT="$OPENSSL_NEXT s_server -www "
O_NEXT_CLI="echo 'GET / HTTP/1.0' | $OPENSSL_NEXT s_client -CAfile data_files/test-ca_cat12.crt"
O_NEXT_CLI_NO_CERT="echo 'GET / HTTP/1.0' | $OPENSSL_NEXT s_client"
else
O_NEXT_SRV=false
O_NEXT_SRV_NO_CERT=false
O_NEXT_CLI_NO_CERT=false
O_NEXT_CLI=false
fi
if [ -n "${GNUTLS_NEXT_SERV:-}" ]; then
G_NEXT_SRV="$GNUTLS_NEXT_SERV --x509certfile data_files/server5.crt --x509keyfile data_files/server5.key"
G_NEXT_SRV_NO_CERT="$GNUTLS_NEXT_SERV"
else
G_NEXT_SRV=false
G_NEXT_SRV_NO_CERT=false
fi
if [ -n "${GNUTLS_NEXT_CLI:-}" ]; then
G_NEXT_CLI="echo 'GET / HTTP/1.0' | $GNUTLS_NEXT_CLI --x509cafile data_files/test-ca_cat12.crt"
G_NEXT_CLI_NO_CERT="echo 'GET / HTTP/1.0' | $GNUTLS_NEXT_CLI"
else
G_NEXT_CLI=false
G_NEXT_CLI_NO_CERT=false
fi
TESTS=0
FAILS=0
SKIPS=0
CONFIG_H='../include/mbedtls/mbedtls_config.h'
MEMCHECK=0
FILTER='.*'
EXCLUDE='^$'
SHOW_TEST_NUMBER=0
RUN_TEST_NUMBER=''
PRESERVE_LOGS=0
# Pick a "unique" server port in the range 10000-19999, and a proxy
# port which is this plus 10000. Each port number may be independently
# overridden by a command line option.
SRV_PORT=$(($$ % 10000 + 10000))
PXY_PORT=$((SRV_PORT + 10000))
print_usage() {
echo "Usage: $0 [options]"
printf " -h|--help\tPrint this help.\n"
printf " -m|--memcheck\tCheck memory leaks and errors.\n"
printf " -f|--filter\tOnly matching tests are executed (substring or BRE)\n"
printf " -e|--exclude\tMatching tests are excluded (substring or BRE)\n"
printf " -n|--number\tExecute only numbered test (comma-separated, e.g. '245,256')\n"
printf " -s|--show-numbers\tShow test numbers in front of test names\n"
printf " -p|--preserve-logs\tPreserve logs of successful tests as well\n"
printf " --outcome-file\tFile where test outcomes are written\n"
printf " \t(default: \$MBEDTLS_TEST_OUTCOME_FILE, none if empty)\n"
printf " --port \tTCP/UDP port (default: randomish 1xxxx)\n"
printf " --proxy-port\tTCP/UDP proxy port (default: randomish 2xxxx)\n"
printf " --seed \tInteger seed value to use for this test run\n"
}
get_options() {
while [ $# -gt 0 ]; do
case "$1" in
-f|--filter)
shift; FILTER=$1
;;
-e|--exclude)
shift; EXCLUDE=$1
;;
-m|--memcheck)
MEMCHECK=1
;;
-n|--number)
shift; RUN_TEST_NUMBER=$1
;;
-s|--show-numbers)
SHOW_TEST_NUMBER=1
;;
-p|--preserve-logs)
PRESERVE_LOGS=1
;;
--port)
shift; SRV_PORT=$1
;;
--proxy-port)
shift; PXY_PORT=$1
;;
--seed)
shift; SEED="$1"
;;
-h|--help)
print_usage
exit 0
;;
*)
echo "Unknown argument: '$1'"
print_usage
exit 1
;;
esac
shift
done
}
# Make the outcome file path relative to the original directory, not
# to .../tests
case "$MBEDTLS_TEST_OUTCOME_FILE" in
[!/]*)
MBEDTLS_TEST_OUTCOME_FILE="$ORIGINAL_PWD/$MBEDTLS_TEST_OUTCOME_FILE"
;;
esac
# Read boolean configuration options from mbedtls_config.h for easy and quick
# testing. Skip non-boolean options (with something other than spaces
# and a comment after "#define SYMBOL"). The variable contains a
# space-separated list of symbols.
CONFIGS_ENABLED=" $(echo `$P_QUERY -l` )"
# Skip next test; use this macro to skip tests which are legitimate
# in theory and expected to be re-introduced at some point, but
# aren't expected to succeed at the moment due to problems outside
# our control (such as bugs in other TLS implementations).
skip_next_test() {
SKIP_NEXT="YES"
}
# skip next test if the flag is not enabled in mbedtls_config.h
requires_config_enabled() {
case $CONFIGS_ENABLED in
*" $1"[\ =]*) :;;
*) SKIP_NEXT="YES";;
esac
}
# skip next test if the flag is enabled in mbedtls_config.h
requires_config_disabled() {
case $CONFIGS_ENABLED in
*" $1"[\ =]*) SKIP_NEXT="YES";;
esac
}
requires_all_configs_enabled() {
if ! $P_QUERY -all $*
then
SKIP_NEXT="YES"
fi
}
requires_all_configs_disabled() {
if $P_QUERY -any $*
then
SKIP_NEXT="YES"
fi
}
requires_any_configs_enabled() {
if ! $P_QUERY -any $*
then
SKIP_NEXT="YES"
fi
}
requires_any_configs_disabled() {
if $P_QUERY -all $*
then
SKIP_NEXT="YES"
fi
}
TLS1_2_KEY_EXCHANGES_WITH_CERT="MBEDTLS_KEY_EXCHANGE_RSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED"
requires_key_exchange_with_cert_in_tls12_or_tls13_enabled() {
if $P_QUERY -all MBEDTLS_SSL_PROTO_TLS1_2
then
requires_any_configs_enabled $TLS1_2_KEY_EXCHANGES_WITH_CERT
elif ! $P_QUERY -all MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED
then
SKIP_NEXT="YES"
fi
}
get_config_value_or_default() {
# This function uses the query_config command line option to query the
# required Mbed TLS compile time configuration from the ssl_server2
# program. The command will always return a success value if the
# configuration is defined and the value will be printed to stdout.
#
# Note that if the configuration is not defined or is defined to nothing,
# the output of this function will be an empty string.
case $CONFIGS_ENABLED in
*" MBEDTLS_SSL_CLI_C "*) ${P_CLI} "query_config=${1}" ;;
*) ${P_SRV} "query_config=${1}" ;;
esac
}
requires_config_value_at_least() {
VAL="$( get_config_value_or_default "$1" )"
if [ -z "$VAL" ]; then
# Should never happen
echo "Mbed TLS configuration $1 is not defined"
exit 1
elif [ "$VAL" -lt "$2" ]; then
SKIP_NEXT="YES"
fi
}
requires_config_value_at_most() {
VAL=$( get_config_value_or_default "$1" )
if [ -z "$VAL" ]; then
# Should never happen
echo "Mbed TLS configuration $1 is not defined"
exit 1
elif [ "$VAL" -gt "$2" ]; then
SKIP_NEXT="YES"
fi
}
requires_config_value_equals() {
VAL=$( get_config_value_or_default "$1" )
if [ -z "$VAL" ]; then
# Should never happen
echo "Mbed TLS configuration $1 is not defined"
exit 1
elif [ "$VAL" -ne "$2" ]; then
SKIP_NEXT="YES"
fi
}
# Require Mbed TLS to support the given protocol version.
#
# Inputs:
# * $1: protocol version in mbedtls syntax (argument to force_version=)
requires_protocol_version() {
# Support for DTLS is detected separately in detect_dtls().
case "$1" in
tls12|dtls12) requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2;;
tls13|dtls13) requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_3;;
*) echo "Unknown required protocol version: $1"; exit 1;;
esac
}
# Space-separated list of ciphersuites supported by this build of
# Mbed TLS.
P_CIPHERSUITES=" $($P_CLI --help 2>/dev/null |
grep 'TLS-\|TLS1-3' |
tr -s ' \n' ' ')"
requires_ciphersuite_enabled() {
case $P_CIPHERSUITES in
*" $1 "*) :;;
*) SKIP_NEXT="YES";;
esac
}
# detect_required_features CMD [RUN_TEST_OPTION...]
# If CMD (call to a TLS client or server program) requires certain features,
# arrange to only run the following test case if those features are enabled.
detect_required_features() {
case "$1" in
*\ force_version=*)
tmp="${1##*\ force_version=}"
tmp="${tmp%%[!-0-9A-Z_a-z]*}"
requires_protocol_version "$tmp";;
esac
case "$1" in
*\ force_ciphersuite=*)
tmp="${1##*\ force_ciphersuite=}"
tmp="${tmp%%[!-0-9A-Z_a-z]*}"
requires_ciphersuite_enabled "$tmp";;
esac
case " $1 " in
*[-_\ =]tickets=[^0]*)
requires_config_enabled MBEDTLS_SSL_TICKET_C;;
esac
case " $1 " in
*[-_\ =]alpn=*)
requires_config_enabled MBEDTLS_SSL_ALPN;;
esac
unset tmp
}
requires_certificate_authentication () {
if [ "$PSK_ONLY" = "YES" ]; then
SKIP_NEXT="YES"
fi
}
adapt_cmd_for_psk () {
case "$2" in
*openssl*) s='-psk abc123 -nocert';;
*gnutls-*) s='--pskkey=abc123';;
*) s='psk=abc123';;
esac
eval $1='"$2 $s"'
unset s
}
# maybe_adapt_for_psk [RUN_TEST_OPTION...]
# If running in a PSK-only build, maybe adapt the test to use a pre-shared key.
#
# If not running in a PSK-only build, do nothing.
# If the test looks like it doesn't use a pre-shared key but can run with a
# pre-shared key, pass a pre-shared key. If the test looks like it can't run
# with a pre-shared key, skip it. If the test looks like it's already using
# a pre-shared key, do nothing.
#
# This code does not consider builds with ECDHE-PSK or RSA-PSK.
#
# Inputs:
# * $CLI_CMD, $SRV_CMD, $PXY_CMD: client/server/proxy commands.
# * $PSK_ONLY: YES if running in a PSK-only build (no asymmetric key exchanges).
# * "$@": options passed to run_test.
#
# Outputs:
# * $CLI_CMD, $SRV_CMD: may be modified to add PSK-relevant arguments.
# * $SKIP_NEXT: set to YES if the test can't run with PSK.
maybe_adapt_for_psk() {
if [ "$PSK_ONLY" != "YES" ]; then
return
fi
if [ "$SKIP_NEXT" = "YES" ]; then
return
fi
case "$CLI_CMD $SRV_CMD" in
*[-_\ =]psk*|*[-_\ =]PSK*)
return;;
*force_ciphersuite*)
# The test case forces a non-PSK cipher suite. In some cases, a
# PSK cipher suite could be substituted, but we're not ready for
# that yet.
SKIP_NEXT="YES"
return;;
*\ auth_mode=*|*[-_\ =]crt[_=]*)
# The test case involves certificates. PSK won't do.
SKIP_NEXT="YES"
return;;
esac
adapt_cmd_for_psk CLI_CMD "$CLI_CMD"
adapt_cmd_for_psk SRV_CMD "$SRV_CMD"
}
case " $CONFIGS_ENABLED " in
*\ MBEDTLS_KEY_EXCHANGE_[^P]*) PSK_ONLY="NO";;
*\ MBEDTLS_KEY_EXCHANGE_P[^S]*) PSK_ONLY="NO";;
*\ MBEDTLS_KEY_EXCHANGE_PS[^K]*) PSK_ONLY="NO";;
*\ MBEDTLS_KEY_EXCHANGE_PSK[^_]*) PSK_ONLY="NO";;
*\ MBEDTLS_KEY_EXCHANGE_PSK_ENABLED\ *) PSK_ONLY="YES";;
*) PSK_ONLY="NO";;
esac
HAS_ALG_SHA_1="NO"
HAS_ALG_SHA_224="NO"
HAS_ALG_SHA_256="NO"
HAS_ALG_SHA_384="NO"
HAS_ALG_SHA_512="NO"
check_for_hash_alg()
{
CURR_ALG="INVALID";
USE_PSA="NO"
case $CONFIGS_ENABLED in
*" MBEDTLS_USE_PSA_CRYPTO"[\ =]*)
USE_PSA="YES";
;;
*) :;;
esac
if [ $USE_PSA = "YES" ]; then
CURR_ALG=PSA_WANT_ALG_${1}
else
CURR_ALG=MBEDTLS_${1}_C
# Remove the second underscore to match MBEDTLS_* naming convention
CURR_ALG=$(echo "$CURR_ALG" | sed 's/_//2')
fi
case $CONFIGS_ENABLED in
*" $CURR_ALG"[\ =]*)
return 0
;;
*) :;;
esac
return 1
}
populate_enabled_hash_algs()
{
for hash_alg in SHA_1 SHA_224 SHA_256 SHA_384 SHA_512; do
if check_for_hash_alg "$hash_alg"; then
hash_alg_variable=HAS_ALG_${hash_alg}
eval ${hash_alg_variable}=YES
fi
done
}
# skip next test if the given hash alg is not supported
requires_hash_alg() {
HASH_DEFINE="Invalid"
HAS_HASH_ALG="NO"
case $1 in
SHA_1):;;
SHA_224):;;
SHA_256):;;
SHA_384):;;
SHA_512):;;
*)
echo "Unsupported hash alg - $1"
exit 1
;;
esac
HASH_DEFINE=HAS_ALG_${1}
eval "HAS_HASH_ALG=\${${HASH_DEFINE}}"
if [ "$HAS_HASH_ALG" = "NO" ]
then
SKIP_NEXT="YES"
fi
}
# skip next test if OpenSSL doesn't support FALLBACK_SCSV
requires_openssl_with_fallback_scsv() {
if [ -z "${OPENSSL_HAS_FBSCSV:-}" ]; then
if $OPENSSL_CMD s_client -help 2>&1 | grep fallback_scsv >/dev/null
then
OPENSSL_HAS_FBSCSV="YES"
else
OPENSSL_HAS_FBSCSV="NO"
fi
fi
if [ "$OPENSSL_HAS_FBSCSV" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if either IN_CONTENT_LEN or MAX_CONTENT_LEN are below a value
requires_max_content_len() {
requires_config_value_at_least "MBEDTLS_SSL_IN_CONTENT_LEN" $1
requires_config_value_at_least "MBEDTLS_SSL_OUT_CONTENT_LEN" $1
}
# skip next test if GnuTLS isn't available
requires_gnutls() {
if [ -z "${GNUTLS_AVAILABLE:-}" ]; then
if ( which "$GNUTLS_CLI" && which "$GNUTLS_SERV" ) >/dev/null 2>&1; then
GNUTLS_AVAILABLE="YES"
else
GNUTLS_AVAILABLE="NO"
fi
fi
if [ "$GNUTLS_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if GnuTLS-next isn't available
requires_gnutls_next() {
if [ -z "${GNUTLS_NEXT_AVAILABLE:-}" ]; then
if ( which "${GNUTLS_NEXT_CLI:-}" && which "${GNUTLS_NEXT_SERV:-}" ) >/dev/null 2>&1; then
GNUTLS_NEXT_AVAILABLE="YES"
else
GNUTLS_NEXT_AVAILABLE="NO"
fi
fi
if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if OpenSSL-legacy isn't available
requires_openssl_legacy() {
if [ -z "${OPENSSL_LEGACY_AVAILABLE:-}" ]; then
if which "${OPENSSL_LEGACY:-}" >/dev/null 2>&1; then
OPENSSL_LEGACY_AVAILABLE="YES"
else
OPENSSL_LEGACY_AVAILABLE="NO"
fi
fi
if [ "$OPENSSL_LEGACY_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
requires_openssl_next() {
if [ -z "${OPENSSL_NEXT_AVAILABLE:-}" ]; then
if which "${OPENSSL_NEXT:-}" >/dev/null 2>&1; then
OPENSSL_NEXT_AVAILABLE="YES"
else
OPENSSL_NEXT_AVAILABLE="NO"
fi
fi
if [ "$OPENSSL_NEXT_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if tls1_3 is not available
requires_openssl_tls1_3() {
requires_openssl_next
if [ "$OPENSSL_NEXT_AVAILABLE" = "NO" ]; then
OPENSSL_TLS1_3_AVAILABLE="NO"
fi
if [ -z "${OPENSSL_TLS1_3_AVAILABLE:-}" ]; then
if $OPENSSL_NEXT s_client -help 2>&1 | grep tls1_3 >/dev/null
then
OPENSSL_TLS1_3_AVAILABLE="YES"
else
OPENSSL_TLS1_3_AVAILABLE="NO"
fi
fi
if [ "$OPENSSL_TLS1_3_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if tls1_3 is not available
requires_gnutls_tls1_3() {
requires_gnutls_next
if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then
GNUTLS_TLS1_3_AVAILABLE="NO"
fi
if [ -z "${GNUTLS_TLS1_3_AVAILABLE:-}" ]; then
if $GNUTLS_NEXT_CLI -l 2>&1 | grep VERS-TLS1.3 >/dev/null
then
GNUTLS_TLS1_3_AVAILABLE="YES"
else
GNUTLS_TLS1_3_AVAILABLE="NO"
fi
fi
if [ "$GNUTLS_TLS1_3_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# Check %NO_TICKETS option
requires_gnutls_next_no_ticket() {
requires_gnutls_next
if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then
GNUTLS_NO_TICKETS_AVAILABLE="NO"
fi
if [ -z "${GNUTLS_NO_TICKETS_AVAILABLE:-}" ]; then
if $GNUTLS_NEXT_CLI --priority-list 2>&1 | grep NO_TICKETS >/dev/null
then
GNUTLS_NO_TICKETS_AVAILABLE="YES"
else
GNUTLS_NO_TICKETS_AVAILABLE="NO"
fi
fi
if [ "$GNUTLS_NO_TICKETS_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# Check %DISABLE_TLS13_COMPAT_MODE option
requires_gnutls_next_disable_tls13_compat() {
requires_gnutls_next
if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then
GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE="NO"
fi
if [ -z "${GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE:-}" ]; then
if $GNUTLS_NEXT_CLI --priority-list 2>&1 | grep DISABLE_TLS13_COMPAT_MODE >/dev/null
then
GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE="YES"
else
GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE="NO"
fi
fi
if [ "$GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if IPv6 isn't available on this host
requires_ipv6() {
if [ -z "${HAS_IPV6:-}" ]; then
$P_SRV server_addr='::1' > $SRV_OUT 2>&1 &
SRV_PID=$!
sleep 1
kill $SRV_PID >/dev/null 2>&1
if grep "NET - Binding of the socket failed" $SRV_OUT >/dev/null; then
HAS_IPV6="NO"
else
HAS_IPV6="YES"
fi
rm -r $SRV_OUT
fi
if [ "$HAS_IPV6" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if it's i686 or uname is not available
requires_not_i686() {
if [ -z "${IS_I686:-}" ]; then
IS_I686="YES"
if which "uname" >/dev/null 2>&1; then
if [ -z "$(uname -a | grep i686)" ]; then
IS_I686="NO"
fi
fi
fi
if [ "$IS_I686" = "YES" ]; then
SKIP_NEXT="YES"
fi
}
# Calculate the input & output maximum content lengths set in the config
MAX_CONTENT_LEN=16384
MAX_IN_LEN=$( get_config_value_or_default "MBEDTLS_SSL_IN_CONTENT_LEN" )
MAX_OUT_LEN=$( get_config_value_or_default "MBEDTLS_SSL_OUT_CONTENT_LEN" )
# Calculate the maximum content length that fits both
if [ "$MAX_IN_LEN" -lt "$MAX_CONTENT_LEN" ]; then
MAX_CONTENT_LEN="$MAX_IN_LEN"
fi
if [ "$MAX_OUT_LEN" -lt "$MAX_CONTENT_LEN" ]; then
MAX_CONTENT_LEN="$MAX_OUT_LEN"
fi
# skip the next test if the SSL output buffer is less than 16KB
requires_full_size_output_buffer() {
if [ "$MAX_OUT_LEN" -ne 16384 ]; then
SKIP_NEXT="YES"
fi
}
# skip the next test if valgrind is in use
not_with_valgrind() {
if [ "$MEMCHECK" -gt 0 ]; then
SKIP_NEXT="YES"
fi
}
# skip the next test if valgrind is NOT in use
only_with_valgrind() {
if [ "$MEMCHECK" -eq 0 ]; then
SKIP_NEXT="YES"
fi
}
# multiply the client timeout delay by the given factor for the next test
client_needs_more_time() {
CLI_DELAY_FACTOR=$1
}
# wait for the given seconds after the client finished in the next test
server_needs_more_time() {
SRV_DELAY_SECONDS=$1
}
# print_name <name>
print_name() {
TESTS=$(( $TESTS + 1 ))
LINE=""
if [ "$SHOW_TEST_NUMBER" -gt 0 ]; then
LINE="$TESTS "
fi
LINE="$LINE$1"
printf "%s " "$LINE"
LEN=$(( 72 - `echo "$LINE" | wc -c` ))
for i in `seq 1 $LEN`; do printf '.'; done
printf ' '
}
# record_outcome <outcome> [<failure-reason>]
# The test name must be in $NAME.
# Use $TEST_SUITE_NAME as the test suite name if set.
record_outcome() {
echo "$1"
if [ -n "$MBEDTLS_TEST_OUTCOME_FILE" ]; then
printf '%s;%s;%s;%s;%s;%s\n' \
"$MBEDTLS_TEST_PLATFORM" "$MBEDTLS_TEST_CONFIGURATION" \
"${TEST_SUITE_NAME:-ssl-opt}" "$NAME" \
"$1" "${2-}" \
>>"$MBEDTLS_TEST_OUTCOME_FILE"
fi
}
unset TEST_SUITE_NAME
# True if the presence of the given pattern in a log definitely indicates
# that the test has failed. False if the presence is inconclusive.
#
# Inputs:
# * $1: pattern found in the logs
# * $TIMES_LEFT: >0 if retrying is an option
#
# Outputs:
# * $outcome: set to a retry reason if the pattern is inconclusive,
# unchanged otherwise.
# * Return value: 1 if the pattern is inconclusive,
# 0 if the failure is definitive.
log_pattern_presence_is_conclusive() {
# If we've run out of attempts, then don't retry no matter what.
if [ $TIMES_LEFT -eq 0 ]; then
return 0
fi
case $1 in
"resend")
# An undesired resend may have been caused by the OS dropping or
# delaying a packet at an inopportune time.
outcome="RETRY(resend)"
return 1;;
esac
}
# fail <message>
fail() {
record_outcome "FAIL" "$1"
echo " ! $1"
mv $SRV_OUT o-srv-${TESTS}.log
mv $CLI_OUT o-cli-${TESTS}.log
if [ -n "$PXY_CMD" ]; then
mv $PXY_OUT o-pxy-${TESTS}.log
fi
echo " ! outputs saved to o-XXX-${TESTS}.log"
if [ "${LOG_FAILURE_ON_STDOUT:-0}" != 0 ]; then
echo " ! server output:"
cat o-srv-${TESTS}.log
echo " ! ========================================================"
echo " ! client output:"
cat o-cli-${TESTS}.log
if [ -n "$PXY_CMD" ]; then
echo " ! ========================================================"
echo " ! proxy output:"
cat o-pxy-${TESTS}.log
fi
echo ""
fi
FAILS=$(( $FAILS + 1 ))
}
# is_polar <cmd_line>
is_polar() {
case "$1" in
*ssl_client2*) true;;
*ssl_server2*) true;;
*) false;;
esac
}
# openssl s_server doesn't have -www with DTLS
check_osrv_dtls() {
case "$SRV_CMD" in
*s_server*-dtls*)
NEEDS_INPUT=1
SRV_CMD="$( echo $SRV_CMD | sed s/-www// )";;
*) NEEDS_INPUT=0;;
esac
}
# provide input to commands that need it
provide_input() {
if [ $NEEDS_INPUT -eq 0 ]; then
return
fi
while true; do
echo "HTTP/1.0 200 OK"
sleep 1
done
}
# has_mem_err <log_file_name>
has_mem_err() {
if ( grep -F 'All heap blocks were freed -- no leaks are possible' "$1" &&
grep -F 'ERROR SUMMARY: 0 errors from 0 contexts' "$1" ) > /dev/null
then
return 1 # false: does not have errors
else
return 0 # true: has errors
fi
}
# Wait for process $2 named $3 to be listening on port $1. Print error to $4.
if type lsof >/dev/null 2>/dev/null; then
wait_app_start() {
newline='
'
START_TIME=$(date +%s)
if [ "$DTLS" -eq 1 ]; then
proto=UDP
else
proto=TCP
fi
# Make a tight loop, server normally takes less than 1s to start.
while true; do
SERVER_PIDS=$(lsof -a -n -b -i "$proto:$1" -t)
# When we use a proxy, it will be listening on the same port we
# are checking for as well as the server and lsof will list both.
case ${newline}${SERVER_PIDS}${newline} in
*${newline}${2}${newline}*) break;;
esac
if [ $(( $(date +%s) - $START_TIME )) -gt $DOG_DELAY ]; then
echo "$3 START TIMEOUT"
echo "$3 START TIMEOUT" >> $4
break
fi
# Linux and *BSD support decimal arguments to sleep. On other
# OSes this may be a tight loop.
sleep 0.1 2>/dev/null || true
done
}
else
echo "Warning: lsof not available, wait_app_start = sleep"
wait_app_start() {
sleep "$START_DELAY"
}
fi
# Wait for server process $2 to be listening on port $1.
wait_server_start() {
wait_app_start $1 $2 "SERVER" $SRV_OUT
}
# Wait for proxy process $2 to be listening on port $1.
wait_proxy_start() {
wait_app_start $1 $2 "PROXY" $PXY_OUT
}
# Given the client or server debug output, parse the unix timestamp that is
# included in the first 4 bytes of the random bytes and check that it's within
# acceptable bounds
check_server_hello_time() {
# Extract the time from the debug (lvl 3) output of the client
SERVER_HELLO_TIME="$(sed -n 's/.*server hello, current time: //p' < "$1")"
# Get the Unix timestamp for now
CUR_TIME=$(date +'%s')
THRESHOLD_IN_SECS=300
# Check if the ServerHello time was printed
if [ -z "$SERVER_HELLO_TIME" ]; then
return 1
fi
# Check the time in ServerHello is within acceptable bounds
if [ $SERVER_HELLO_TIME -lt $(( $CUR_TIME - $THRESHOLD_IN_SECS )) ]; then
# The time in ServerHello is at least 5 minutes before now
return 1
elif [ $SERVER_HELLO_TIME -gt $(( $CUR_TIME + $THRESHOLD_IN_SECS )) ]; then
# The time in ServerHello is at least 5 minutes later than now
return 1
else
return 0
fi
}
# Get handshake memory usage from server or client output and put it into the variable specified by the first argument
handshake_memory_get() {
OUTPUT_VARIABLE="$1"
OUTPUT_FILE="$2"
# Get memory usage from a pattern like "Heap memory usage after handshake: 23112 bytes. Peak memory usage was 33112"
MEM_USAGE=$(sed -n 's/.*Heap memory usage after handshake: //p' < "$OUTPUT_FILE" | grep -o "[0-9]*" | head -1)
# Check if memory usage was read
if [ -z "$MEM_USAGE" ]; then
echo "Error: Can not read the value of handshake memory usage"
return 1
else
eval "$OUTPUT_VARIABLE=$MEM_USAGE"
return 0
fi
}
# Get handshake memory usage from server or client output and check if this value
# is not higher than the maximum given by the first argument
handshake_memory_check() {
MAX_MEMORY="$1"
OUTPUT_FILE="$2"
# Get memory usage
if ! handshake_memory_get "MEMORY_USAGE" "$OUTPUT_FILE"; then
return 1
fi
# Check if memory usage is below max value
if [ "$MEMORY_USAGE" -gt "$MAX_MEMORY" ]; then
echo "\nFailed: Handshake memory usage was $MEMORY_USAGE bytes," \
"but should be below $MAX_MEMORY bytes"
return 1
else
return 0
fi
}
# wait for client to terminate and set CLI_EXIT
# must be called right after starting the client
wait_client_done() {
CLI_PID=$!
CLI_DELAY=$(( $DOG_DELAY * $CLI_DELAY_FACTOR ))
CLI_DELAY_FACTOR=1
( sleep $CLI_DELAY; echo "===CLIENT_TIMEOUT===" >> $CLI_OUT; kill $CLI_PID ) &
DOG_PID=$!
# For Ubuntu 22.04, `Terminated` message is outputed by wait command.
# To remove it from stdout, redirect stdout/stderr to CLI_OUT
wait $CLI_PID >> $CLI_OUT 2>&1
CLI_EXIT=$?
kill $DOG_PID >/dev/null 2>&1
wait $DOG_PID >> $CLI_OUT 2>&1