-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathssl-opt.sh
executable file
·14154 lines (12956 loc) · 655 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 OR GPL-2.0-or-later
#
# 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
DATA_FILES_PATH=../framework/data_files
# 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:=openssl}
: ${GNUTLS_CLI:=gnutls-cli}
: ${GNUTLS_SERV:=gnutls-serv}
: ${PERL:=perl}
# The OPENSSL variable used to be OPENSSL_CMD for historical reasons.
# To help the migration, error out if the old variable is set,
# but only if it has a different value than the new one.
if [ "${OPENSSL_CMD+set}" = set ]; then
# the variable is set, we can now check its value
if [ "$OPENSSL_CMD" != "$OPENSSL" ]; then
echo "Please use OPENSSL instead of OPENSSL_CMD." >&2
exit 125
fi
fi
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 _)"}
: ${EARLY_DATA_INPUT:="$DATA_FILES_PATH/tls13_early_data.txt"}
O_SRV="$OPENSSL s_server -www -cert $DATA_FILES_PATH/server5.crt -key $DATA_FILES_PATH/server5.key"
O_CLI="echo 'GET / HTTP/1.0' | $OPENSSL s_client"
G_SRV="$GNUTLS_SERV --x509certfile $DATA_FILES_PATH/server5.crt --x509keyfile $DATA_FILES_PATH/server5.key"
G_CLI="echo 'GET / HTTP/1.0' | $GNUTLS_CLI --x509cafile $DATA_FILES_PATH/test-ca_cat12.crt"
# alternative versions of OpenSSL and GnuTLS (no default path)
# If $OPENSSL is at least 1.1.1, use it as OPENSSL_NEXT as well.
if [ -z "${OPENSSL_NEXT:-}" ]; then
case $($OPENSSL version) in
OpenSSL\ 1.1.[1-9]*) OPENSSL_NEXT=$OPENSSL;;
OpenSSL\ [3-9]*) OPENSSL_NEXT=$OPENSSL;;
esac
fi
# If $GNUTLS_CLI is at least 3.7, use it as GNUTLS_NEXT_CLI as well.
if [ -z "${GNUTLS_NEXT_CLI:-}" ]; then
case $($GNUTLS_CLI --version) in
gnutls-cli\ 3.[1-9][0-9]*) GNUTLS_NEXT_CLI=$GNUTLS_CLI;;
gnutls-cli\ 3.[7-9].*) GNUTLS_NEXT_CLI=$GNUTLS_CLI;;
gnutls-cli\ [4-9]*) GNUTLS_NEXT_CLI=$GNUTLS_CLI;;
esac
fi
# If $GNUTLS_SERV is at least 3.7, use it as GNUTLS_NEXT_SERV as well.
if [ -z "${GNUTLS_NEXT_SERV:-}" ]; then
case $($GNUTLS_SERV --version) in
gnutls-cli\ 3.[1-9][0-9]*) GNUTLS_NEXT_SERV=$GNUTLS_SERV;;
gnutls-cli\ 3.[7-9].*) GNUTLS_NEXT_SERV=$GNUTLS_SERV;;
gnutls-cli\ [4-9]*) GNUTLS_NEXT_SERV=$GNUTLS_SERV;;
esac
fi
if [ -n "${OPENSSL_NEXT:-}" ]; then
O_NEXT_SRV="$OPENSSL_NEXT s_server -www -cert $DATA_FILES_PATH/server5.crt -key $DATA_FILES_PATH/server5.key"
O_NEXT_SRV_EARLY_DATA="$OPENSSL_NEXT s_server -early_data -cert $DATA_FILES_PATH/server5.crt -key $DATA_FILES_PATH/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_PATH/test-ca_cat12.crt"
O_NEXT_CLI_NO_CERT="echo 'GET / HTTP/1.0' | $OPENSSL_NEXT s_client"
O_NEXT_CLI_RENEGOTIATE="echo 'R' | $OPENSSL_NEXT s_client -cert $DATA_FILES_PATH/server5.crt -key $DATA_FILES_PATH/server5.key"
else
O_NEXT_SRV=false
O_NEXT_SRV_NO_CERT=false
O_NEXT_SRV_EARLY_DATA=false
O_NEXT_CLI_NO_CERT=false
O_NEXT_CLI=false
O_NEXT_CLI_RENEGOTIATE=false
fi
if [ -n "${GNUTLS_NEXT_SERV:-}" ]; then
G_NEXT_SRV="$GNUTLS_NEXT_SERV --x509certfile $DATA_FILES_PATH/server5.crt --x509keyfile $DATA_FILES_PATH/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_PATH/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
LIST_TESTS=0
RUN_TEST_NUMBER=''
RUN_TEST_SUITE=''
MIN_TESTS=1
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 " --list-test-cases\tList all potential test cases (No Execution)\n"
printf " --min \tMinimum number of non-skipped tests (default 1)\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"
printf " --test-suite\tOnly matching test suites are executed\n"
printf " \t(comma-separated, e.g. 'ssl-opt,tls13-compat')\n\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
;;
-l|--list-test-cases)
LIST_TESTS=1
;;
-p|--preserve-logs)
PRESERVE_LOGS=1
;;
--min)
shift; MIN_TESTS=$1
;;
--outcome-file)
shift; MBEDTLS_TEST_OUTCOME_FILE=$1
;;
--port)
shift; SRV_PORT=$1
;;
--proxy-port)
shift; PXY_PORT=$1
;;
--seed)
shift; SEED="$1"
;;
--test-suite)
shift; RUN_TEST_SUITE="$1"
;;
-h|--help)
print_usage
exit 0
;;
*)
echo "Unknown argument: '$1'"
print_usage
exit 1
;;
esac
shift
done
}
get_options "$@"
# 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. The list should always be
# terminated by a single whitespace character, otherwise the last entry
# will not get matched by the parsing regex.
if [ "$LIST_TESTS" -eq 0 ];then
CONFIGS_ENABLED=" $(echo `$P_QUERY -l` ) "
else
P_QUERY=":"
CONFIGS_ENABLED=""
fi
# 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"
}
# Check if the required configuration ($1) is enabled
is_config_enabled()
{
case $CONFIGS_ENABLED in
*" $1"[\ =]*) return 0;;
*) return 1;;
esac
}
# 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() {
for x in "$@"; do
if ! is_config_enabled "$x"; then
SKIP_NEXT="YES"
return
fi
done
}
requires_all_configs_disabled() {
for x in "$@"; do
if is_config_enabled "$x"; then
SKIP_NEXT="YES"
return
fi
done
}
requires_any_configs_enabled() {
for x in "$@"; do
if is_config_enabled "$x"; then
return
fi
done
SKIP_NEXT="YES"
}
requires_any_configs_disabled() {
for x in "$@"; do
if ! is_config_enabled "$x"; then
return
fi
done
SKIP_NEXT="YES"
}
TLS1_2_KEY_EXCHANGES_WITH_CERT="MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED"
TLS1_2_KEY_EXCHANGES_WITH_ECDSA_CERT="MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED"
TLS1_2_KEY_EXCHANGES_WITH_CERT_WO_ECDH="MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED \
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED"
requires_certificate_authentication () {
if is_config_enabled MBEDTLS_SSL_PROTO_TLS1_3
then
# TLS 1.3 is negotiated by default, so check whether it supports
# certificate-based authentication.
requires_config_enabled MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED
else # Only TLS 1.2 is enabled.
requires_any_configs_enabled $TLS1_2_KEY_EXCHANGES_WITH_CERT
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.
if [ "$LIST_TESTS" -eq 0 ];then
${P_SRV} "query_config=${1}"
else
echo "1"
fi
}
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=""
if [ "$LIST_TESTS" -eq 0 ]; then
P_CIPHERSUITES=" $($P_CLI help_ciphersuites 2>/dev/null |
grep 'TLS-\|TLS1-3' |
tr -s ' \n' ' ')"
if [ -z "${P_CIPHERSUITES# }" ]; then
echo >&2 "$0: fatal error: no cipher suites found!"
exit 125
fi
fi
requires_ciphersuite_enabled() {
case $P_CIPHERSUITES in
*" $1 "*) :;;
*) SKIP_NEXT="YES";;
esac
}
requires_cipher_enabled() {
KEY_TYPE=$1
MODE=${2:-}
case "$KEY_TYPE" in
CHACHA20)
requires_config_enabled PSA_WANT_ALG_CHACHA20_POLY1305
requires_config_enabled PSA_WANT_KEY_TYPE_CHACHA20
;;
*)
requires_config_enabled PSA_WANT_ALG_${MODE}
requires_config_enabled PSA_WANT_KEY_TYPE_${KEY_TYPE}
;;
esac
}
# Automatically detect required features based on command line parameters.
# Parameters are:
# - $1 = command line (call to a TLS client or server program)
# - $2 = client/server
# - $3 = TLS version (TLS12 or TLS13)
# - $4 = Use an external tool without ECDH support
# - $5 = run test options
detect_required_features() {
CMD_LINE=$1
ROLE=$2
TLS_VERSION=$3
EXT_WO_ECDH=$4
TEST_OPTIONS=${5:-}
case "$CMD_LINE" in
*\ force_version=*)
tmp="${CMD_LINE##*\ force_version=}"
tmp="${tmp%%[!-0-9A-Z_a-z]*}"
requires_protocol_version "$tmp";;
esac
case "$CMD_LINE" in
*\ force_ciphersuite=*)
tmp="${CMD_LINE##*\ force_ciphersuite=}"
tmp="${tmp%%[!-0-9A-Z_a-z]*}"
requires_ciphersuite_enabled "$tmp";;
esac
case " $CMD_LINE " in
*[-_\ =]tickets=[^0]*)
requires_config_enabled MBEDTLS_SSL_TICKET_C;;
esac
case " $CMD_LINE " in
*[-_\ =]alpn=*)
requires_config_enabled MBEDTLS_SSL_ALPN;;
esac
case " $CMD_LINE " in
*\ auth_mode=*|*[-_\ =]crt[_=]*)
# The test case involves certificates (crt), or a relevant
# aspect of it is the (certificate-based) authentication mode.
requires_certificate_authentication;;
esac
case " $CMD_LINE " in
*\ ca_callback=1\ *)
requires_config_enabled MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK;;
esac
case " $CMD_LINE " in
*"programs/ssl/dtls_client "*|\
*"programs/ssl/ssl_client1 "*)
requires_config_enabled MBEDTLS_CTR_DRBG_C
requires_config_enabled MBEDTLS_ENTROPY_C
requires_config_enabled MBEDTLS_PEM_PARSE_C
requires_config_enabled MBEDTLS_SSL_CLI_C
requires_certificate_authentication
;;
*"programs/ssl/dtls_server "*|\
*"programs/ssl/ssl_fork_server "*|\
*"programs/ssl/ssl_pthread_server "*|\
*"programs/ssl/ssl_server "*)
requires_config_enabled MBEDTLS_CTR_DRBG_C
requires_config_enabled MBEDTLS_ENTROPY_C
requires_config_enabled MBEDTLS_PEM_PARSE_C
requires_config_enabled MBEDTLS_SSL_SRV_C
requires_certificate_authentication
# The actual minimum depends on the configuration since it's
# mostly about the certificate size.
# In config-suite-b.h, for the test certificates (server5.crt),
# 1024 is not enough.
requires_config_value_at_least MBEDTLS_SSL_OUT_CONTENT_LEN 2000
;;
esac
case " $CMD_LINE " in
*"programs/ssl/ssl_pthread_server "*)
requires_config_enabled MBEDTLS_THREADING_PTHREAD;;
esac
case "$CMD_LINE" in
*[-_\ =]psk*|*[-_\ =]PSK*) :;; # No certificate requirement with PSK
*/server5*|\
*/server7*|\
*/dir-maxpath*)
requires_certificate_authentication
if [ "$TLS_VERSION" = "TLS13" ]; then
# In case of TLS13 the support for ECDSA is enough
requires_pk_alg "ECDSA"
else
# For TLS12 requirements are different between server and client
if [ "$ROLE" = "server" ]; then
# If the server uses "server5*" certificates, then an ECDSA based
# key exchange is required. However gnutls also does not
# support ECDH, so this limit the choice to ECDHE-ECDSA
if [ "$EXT_WO_ECDH" = "yes" ]; then
requires_any_configs_enabled MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
else
requires_any_configs_enabled $TLS1_2_KEY_EXCHANGES_WITH_ECDSA_CERT
fi
elif [ "$ROLE" = "client" ]; then
# On the client side it is enough to have any certificate
# based authentication together with support for ECDSA.
# Of course the GnuTLS limitation mentioned above applies
# also here.
if [ "$EXT_WO_ECDH" = "yes" ]; then
requires_any_configs_enabled $TLS1_2_KEY_EXCHANGES_WITH_CERT_WO_ECDH
else
requires_any_configs_enabled $TLS1_2_KEY_EXCHANGES_WITH_CERT
fi
requires_pk_alg "ECDSA"
fi
fi
;;
esac
case "$CMD_LINE" in
*[-_\ =]psk*|*[-_\ =]PSK*) :;; # No certificate requirement with PSK
*/server1*|\
*/server2*|\
*/server7*)
requires_certificate_authentication
# Certificates with an RSA key. The algorithm requirement is
# some subset of {PKCS#1v1.5 encryption, PKCS#1v1.5 signature,
# PSS signature}. We can't easily tell which subset works, and
# we aren't currently running ssl-opt.sh in configurations
# where partial RSA support is a problem, so generically, we
# just require RSA and it works out for our tests so far.
requires_config_enabled "MBEDTLS_RSA_C"
esac
unset tmp
}
adapt_cmd_for_psk () {
case "$2" in
*openssl*s_server*) s='-psk 73776f726466697368 -nocert';;
*openssl*) s='-psk 73776f726466697368';;
*gnutls-cli*) s='--pskusername=Client_identity --pskkey=73776f726466697368';;
*gnutls-serv*) s='--pskpasswd=../framework/data_files/simplepass.psk';;
*) s='psk=73776f726466697368';;
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.
#
# 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"
}
# PSK_PRESENT="YES" if at least one protocol versions supports at least
# one PSK key exchange mode.
PSK_PRESENT="NO"
# PSK_ONLY="YES" if all the available key exchange modes are PSK-based
# (pure-PSK or PSK-ephemeral, possibly both).
PSK_ONLY=""
for c in $CONFIGS_ENABLED; do
case $c in
MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) PSK_PRESENT="YES";;
MBEDTLS_KEY_EXCHANGE_*_PSK_ENABLED) PSK_PRESENT="YES";;
MBEDTLS_KEY_EXCHANGE_*_ENABLED) PSK_ONLY="NO";;
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED) PSK_PRESENT="YES";;
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_*_ENABLED) PSK_PRESENT="YES";;
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_*_ENABLED) PSK_ONLY="NO";;
esac
done
# At this stage, $PSK_ONLY is empty if we haven't detected a non-PSK
# key exchange, i.e. if we're in a PSK-only build or a build with no
# key exchanges at all. We avoid triggering PSK-only adaptation code in
# the edge case of no key exchanges.
: ${PSK_ONLY:=$PSK_PRESENT}
unset c
HAS_ALG_MD5="NO"
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";
CURR_ALG=PSA_WANT_ALG_${1}
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 MD5; 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
MD5):;;
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 the given pk alg is not enabled
requires_pk_alg() {
case $1 in
ECDSA)
requires_config_enabled PSA_WANT_ALG_ECDSA
;;
*)
echo "Unknown/unimplemented case $1 in requires_pk_alg"
exit 1
;;
esac
}
# skip next test if OpenSSL doesn't support FALLBACK_SCSV
requires_openssl_with_fallback_scsv() {
if [ -z "${OPENSSL_HAS_FBSCSV:-}" ]; then
if $OPENSSL 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
}
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 openssl version is lower than 3.0
requires_openssl_3_x() {
requires_openssl_next
if [ "$OPENSSL_NEXT_AVAILABLE" = "NO" ]; then
OPENSSL_3_X_AVAILABLE="NO"
fi
if [ -z "${OPENSSL_3_X_AVAILABLE:-}" ]; then
if $OPENSSL_NEXT version 2>&1 | grep "OpenSSL 3." >/dev/null
then
OPENSSL_3_X_AVAILABLE="YES"
else
OPENSSL_3_X_AVAILABLE="NO"
fi
fi
if [ "$OPENSSL_3_X_AVAILABLE" = "NO" ]; then
SKIP_NEXT="YES"
fi
}
# skip next test if openssl does not support ffdh keys
requires_openssl_tls1_3_with_ffdh() {
requires_openssl_3_x
}
# skip next test if openssl cannot handle ephemeral key exchange
requires_openssl_tls1_3_with_compatible_ephemeral() {
requires_openssl_next
if !(is_config_enabled "PSA_WANT_ALG_ECDH"); then
requires_openssl_tls1_3_with_ffdh
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
}
# OpenSSL servers forbid client renegotiation by default since OpenSSL 3.0.
# Older versions always allow it and have no command-line option.
OPENSSL_S_SERVER_CLIENT_RENEGOTIATION=
case $($OPENSSL s_server -help 2>&1) in
*-client_renegotiation*)
OPENSSL_S_SERVER_CLIENT_RENEGOTIATION=-client_renegotiation;;
esac
# 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 GnuTLS does not support the record size limit extension
requires_gnutls_record_size_limit() {
requires_gnutls_next
if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then
GNUTLS_RECORD_SIZE_LIMIT_AVAILABLE="NO"
else
GNUTLS_RECORD_SIZE_LIMIT_AVAILABLE="YES"
fi
if [ "$GNUTLS_RECORD_SIZE_LIMIT_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
}
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" )
if [ "$LIST_TESTS" -eq 0 ];then
# Calculate the input & output maximum content lengths set in the config
# 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
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 called by all.sh in a component with MSan
# (which we also call MemSan) or Valgrind.
not_with_msan_or_valgrind() {
case "_${MBEDTLS_TEST_CONFIGURATION:-}_" in
*_msan_*|*_memsan_*|*_valgrind_*) SKIP_NEXT="YES";;
esac
}
# 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=""