From 8de196a590bf7f145bd2e91ffb55167d4a1e724e Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 11 Feb 2020 17:18:08 +0100 Subject: [PATCH 01/10] programs/Makefile: List all programs one by one This makes it easier to add or remove programs as well as see which programs were added or removed in diffs. Side port of 30fae8ee7dc247116da968aa07d8164eaa6f07af in mbed-crypto. --- programs/Makefile | 78 ++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/programs/Makefile b/programs/Makefile index 188c2beea8..99c83ce1c8 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -56,34 +56,56 @@ ifdef ZLIB LOCAL_LDFLAGS += -lz endif -APPS = aes/aescrypt2$(EXEXT) aes/crypt_and_hash$(EXEXT) \ - hash/hello$(EXEXT) hash/generic_sum$(EXEXT) \ - pkey/dh_client$(EXEXT) \ - pkey/dh_genprime$(EXEXT) pkey/dh_server$(EXEXT) \ - pkey/ecdh_curve25519$(EXEXT) \ - pkey/ecdsa$(EXEXT) pkey/gen_key$(EXEXT) \ - pkey/key_app$(EXEXT) pkey/key_app_writer$(EXEXT) \ - pkey/mpi_demo$(EXEXT) pkey/pk_decrypt$(EXEXT) \ - pkey/pk_encrypt$(EXEXT) pkey/pk_sign$(EXEXT) \ - pkey/pk_verify$(EXEXT) pkey/rsa_genkey$(EXEXT) \ - pkey/rsa_decrypt$(EXEXT) pkey/rsa_encrypt$(EXEXT) \ - pkey/rsa_sign$(EXEXT) pkey/rsa_verify$(EXEXT) \ - pkey/rsa_sign_pss$(EXEXT) pkey/rsa_verify_pss$(EXEXT) \ - ssl/dtls_client$(EXEXT) ssl/dtls_server$(EXEXT) \ - ssl/ssl_client1$(EXEXT) ssl/ssl_client2$(EXEXT) \ - ssl/ssl_server$(EXEXT) ssl/ssl_server2$(EXEXT) \ - ssl/ssl_fork_server$(EXEXT) ssl/mini_client$(EXEXT) \ - ssl/ssl_mail_client$(EXEXT) random/gen_entropy$(EXEXT) \ - random/gen_random_havege$(EXEXT) \ - random/gen_random_ctr_drbg$(EXEXT) \ - test/benchmark$(EXEXT) \ - test/selftest$(EXEXT) test/udp_proxy$(EXEXT) \ - test/zeroize$(EXEXT) \ - test/query_compile_time_config$(EXEXT) \ - util/pem2der$(EXEXT) util/strerror$(EXEXT) \ - x509/cert_app$(EXEXT) x509/crl_app$(EXEXT) \ - x509/cert_req$(EXEXT) x509/cert_write$(EXEXT) \ - x509/req_app$(EXEXT) +APPS = \ + aes/aescrypt2$(EXEXT) \ + aes/crypt_and_hash$(EXEXT) \ + hash/hello$(EXEXT) \ + hash/generic_sum$(EXEXT) \ + pkey/dh_client$(EXEXT) \ + pkey/dh_genprime$(EXEXT) \ + pkey/dh_server$(EXEXT) \ + pkey/ecdh_curve25519$(EXEXT) \ + pkey/ecdsa$(EXEXT) \ + pkey/gen_key$(EXEXT) \ + pkey/key_app$(EXEXT) \ + pkey/key_app_writer$(EXEXT) \ + pkey/mpi_demo$(EXEXT) \ + pkey/pk_decrypt$(EXEXT) \ + pkey/pk_encrypt$(EXEXT) \ + pkey/pk_sign$(EXEXT) \ + pkey/pk_verify$(EXEXT) \ + pkey/rsa_genkey$(EXEXT) \ + pkey/rsa_decrypt$(EXEXT) \ + pkey/rsa_encrypt$(EXEXT) \ + pkey/rsa_sign$(EXEXT) \ + pkey/rsa_verify$(EXEXT) \ + pkey/rsa_sign_pss$(EXEXT) \ + pkey/rsa_verify_pss$(EXEXT) \ + ssl/dtls_client$(EXEXT) \ + ssl/dtls_server$(EXEXT) \ + ssl/ssl_client1$(EXEXT) \ + ssl/ssl_client2$(EXEXT) \ + ssl/ssl_server$(EXEXT) \ + ssl/ssl_server2$(EXEXT) \ + ssl/ssl_fork_server$(EXEXT) \ + ssl/mini_client$(EXEXT) \ + ssl/ssl_mail_client$(EXEXT) \ + random/gen_entropy$(EXEXT) \ + random/gen_random_havege$(EXEXT) \ + random/gen_random_ctr_drbg$(EXEXT) \ + test/benchmark$(EXEXT) \ + test/selftest$(EXEXT) \ + test/udp_proxy$(EXEXT) \ + test/zeroize$(EXEXT) \ + test/query_compile_time_config$(EXEXT) \ + util/pem2der$(EXEXT) \ + util/strerror$(EXEXT) \ + x509/cert_app$(EXEXT) \ + x509/crl_app$(EXEXT) \ + x509/cert_req$(EXEXT) \ + x509/cert_write$(EXEXT) \ + x509/req_app$(EXEXT) \ +# End of APPS ifdef PTHREAD APPS += ssl/ssl_pthread_server$(EXEXT) From ed736992d5ec7487e47a4b78101c72f408f6f78d Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Fri, 26 Oct 2018 16:55:14 +0100 Subject: [PATCH 02/10] pkey: Remove dependency on X.509 --- programs/pkey/key_app.c | 2 +- programs/pkey/rsa_genkey.c | 1 - programs/pkey/rsa_sign_pss.c | 2 +- programs/pkey/rsa_verify_pss.c | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/programs/pkey/key_app.c b/programs/pkey/key_app.c index 7939309919..19dcdfe494 100644 --- a/programs/pkey/key_app.c +++ b/programs/pkey/key_app.c @@ -40,7 +40,7 @@ defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) #include "mbedtls/error.h" #include "mbedtls/rsa.h" -#include "mbedtls/x509.h" +#include "mbedtls/pk.h" #include #endif diff --git a/programs/pkey/rsa_genkey.c b/programs/pkey/rsa_genkey.c index d556c19021..45d19e9ffa 100644 --- a/programs/pkey/rsa_genkey.c +++ b/programs/pkey/rsa_genkey.c @@ -42,7 +42,6 @@ #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/bignum.h" -#include "mbedtls/x509.h" #include "mbedtls/rsa.h" #include diff --git a/programs/pkey/rsa_sign_pss.c b/programs/pkey/rsa_sign_pss.c index 42209e27c1..5019f28f5b 100644 --- a/programs/pkey/rsa_sign_pss.c +++ b/programs/pkey/rsa_sign_pss.c @@ -55,7 +55,7 @@ int main( void ) #include "mbedtls/ctr_drbg.h" #include "mbedtls/md.h" #include "mbedtls/rsa.h" -#include "mbedtls/x509.h" +#include "mbedtls/pk.h" #include #include diff --git a/programs/pkey/rsa_verify_pss.c b/programs/pkey/rsa_verify_pss.c index 148cd5110b..de28337c8d 100644 --- a/programs/pkey/rsa_verify_pss.c +++ b/programs/pkey/rsa_verify_pss.c @@ -55,7 +55,6 @@ int main( void ) #include "mbedtls/pem.h" #include "mbedtls/pk.h" #include "mbedtls/md.h" -#include "mbedtls/x509.h" #include #include From 93a0f90dcaffb0eb26b62ea36c99e5a70f7c16f7 Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Fri, 22 Feb 2019 16:57:56 +0000 Subject: [PATCH 03/10] pkey/rsa_genkey: Remove commented out code There is some commented out X.509 certificate writing code present in rsa_genkey. It looks like it has been commented out since the beginning of time. Let's remove it, since commented out code is not in good style. --- programs/pkey/rsa_genkey.c | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/programs/pkey/rsa_genkey.c b/programs/pkey/rsa_genkey.c index 45d19e9ffa..f2b7b5078b 100644 --- a/programs/pkey/rsa_genkey.c +++ b/programs/pkey/rsa_genkey.c @@ -148,19 +148,6 @@ int main( void ) mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret ); goto exit; } -/* - mbedtls_printf( " ok\n . Generating the certificate..." ); - - x509write_init_raw( &cert ); - x509write_add_pubkey( &cert, &rsa ); - x509write_add_subject( &cert, "CN='localhost'" ); - x509write_add_validity( &cert, "2007-09-06 17:00:32", - "2010-09-06 17:00:32" ); - x509write_create_selfsign( &cert, &rsa ); - x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER ); - x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM ); - x509write_free_raw( &cert ); -*/ mbedtls_printf( " ok\n\n" ); exit_code = MBEDTLS_EXIT_SUCCESS; From 7cb47de12ac18e348c5a5c57363757fa7c63da1c Mon Sep 17 00:00:00 2001 From: Jaeden Amero Date: Thu, 28 Feb 2019 11:37:23 +0000 Subject: [PATCH 04/10] query_config: Move to programs/test As the SSL programs, like ssl_client2 and ssl_server2, are dependent on SSL and therefore about to be removed, the only consumer of query_config is the query_compile_time_config test. As such, it makes sense to move query_config to be next to what uses it. --- programs/Makefile | 12 ++++++------ programs/ssl/CMakeLists.txt | 4 ++-- programs/test/CMakeLists.txt | 2 +- programs/{ssl => test}/query_config.c | 0 scripts/bump_version.sh | 2 +- scripts/generate_query_config.pl | 2 +- scripts/generate_visualc_files.pl | 2 +- tests/scripts/check-generated-files.sh | 2 +- visualc/VS2010/query_compile_time_config.vcxproj | 2 +- visualc/VS2010/ssl_client2.vcxproj | 2 +- visualc/VS2010/ssl_server2.vcxproj | 2 +- 11 files changed, 16 insertions(+), 16 deletions(-) rename programs/{ssl => test}/query_config.c (100%) diff --git a/programs/Makefile b/programs/Makefile index 99c83ce1c8..d004e2e06d 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -253,17 +253,17 @@ ssl/ssl_client1$(EXEXT): ssl/ssl_client1.c $(DEP) echo " CC ssl/ssl_client1.c" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) ssl/ssl_client1.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ -ssl/ssl_client2$(EXEXT): ssl/ssl_client2.c ssl/query_config.c $(DEP) +ssl/ssl_client2$(EXEXT): ssl/ssl_client2.c test/query_config.c $(DEP) echo " CC ssl/ssl_client2.c" - $(CC) $(LOCAL_CFLAGS) $(CFLAGS) ssl/ssl_client2.c ssl/query_config.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ + $(CC) $(LOCAL_CFLAGS) $(CFLAGS) ssl/ssl_client2.c test/query_config.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ ssl/ssl_server$(EXEXT): ssl/ssl_server.c $(DEP) echo " CC ssl/ssl_server.c" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) ssl/ssl_server.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ -ssl/ssl_server2$(EXEXT): ssl/ssl_server2.c ssl/query_config.c $(DEP) +ssl/ssl_server2$(EXEXT): ssl/ssl_server2.c test/query_config.c $(DEP) echo " CC ssl/ssl_server2.c" - $(CC) $(LOCAL_CFLAGS) $(CFLAGS) ssl/ssl_server2.c ssl/query_config.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ + $(CC) $(LOCAL_CFLAGS) $(CFLAGS) ssl/ssl_server2.c test/query_config.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ ssl/ssl_fork_server$(EXEXT): ssl/ssl_fork_server.c $(DEP) echo " CC ssl/ssl_fork_server.c" @@ -301,9 +301,9 @@ test/zeroize$(EXEXT): test/zeroize.c $(DEP) echo " CC test/zeroize.c" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/zeroize.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ -test/query_compile_time_config$(EXEXT): test/query_compile_time_config.c ssl/query_config.c $(DEP) +test/query_compile_time_config$(EXEXT): test/query_compile_time_config.c test/query_config.c $(DEP) echo " CC test/query_compile_time_config.c" - $(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/query_compile_time_config.c ssl/query_config.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ + $(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/query_compile_time_config.c test/query_config.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ util/pem2der$(EXEXT): util/pem2der.c $(DEP) echo " CC util/pem2der.c" diff --git a/programs/ssl/CMakeLists.txt b/programs/ssl/CMakeLists.txt index 803920cde6..f28a47d87a 100644 --- a/programs/ssl/CMakeLists.txt +++ b/programs/ssl/CMakeLists.txt @@ -34,14 +34,14 @@ add_executable(ssl_client1 ssl_client1.c) target_link_libraries(ssl_client1 ${libs}) add_executable(ssl_client2 ssl_client2.c) -target_sources(ssl_client2 PUBLIC query_config.c) +target_sources(ssl_client2 PUBLIC ../test/query_config.c) target_link_libraries(ssl_client2 ${libs}) add_executable(ssl_server ssl_server.c) target_link_libraries(ssl_server ${libs}) add_executable(ssl_server2 ssl_server2.c) -target_sources(ssl_server2 PUBLIC query_config.c) +target_sources(ssl_server2 PUBLIC ../test/query_config.c) target_link_libraries(ssl_server2 ${libs}) add_executable(ssl_fork_server ssl_fork_server.c) diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index 282ef58aaf..64ed379e73 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -28,7 +28,7 @@ add_executable(zeroize zeroize.c) target_link_libraries(zeroize ${libs}) add_executable(query_compile_time_config query_compile_time_config.c) -target_sources(query_compile_time_config PUBLIC ../ssl/query_config.c) +target_sources(query_compile_time_config PUBLIC query_config.c) target_link_libraries(query_compile_time_config ${libs}) install(TARGETS selftest benchmark udp_proxy query_compile_time_config diff --git a/programs/ssl/query_config.c b/programs/test/query_config.c similarity index 100% rename from programs/ssl/query_config.c rename to programs/test/query_config.c diff --git a/scripts/bump_version.sh b/scripts/bump_version.sh index c39a86a5e7..cf875c88d8 100755 --- a/scripts/bump_version.sh +++ b/scripts/bump_version.sh @@ -132,7 +132,7 @@ done [ $VERBOSE ] && echo "Re-generating library/error.c" scripts/generate_errors.pl -[ $VERBOSE ] && echo "Re-generating programs/ssl/query_config.c" +[ $VERBOSE ] && echo "Re-generating programs/test/query_config.c" scripts/generate_query_config.pl [ $VERBOSE ] && echo "Re-generating library/version_features.c" diff --git a/scripts/generate_query_config.pl b/scripts/generate_query_config.pl index f15e03a358..d94fdad627 100755 --- a/scripts/generate_query_config.pl +++ b/scripts/generate_query_config.pl @@ -21,7 +21,7 @@ use strict; my $config_file = "./include/mbedtls/config.h"; my $query_config_format_file = "./scripts/data_files/query_config.fmt"; -my $query_config_file = "./programs/ssl/query_config.c"; +my $query_config_file = "./programs/test/query_config.c"; # Excluded macros from the generated query_config.c. For example, macros that # have commas or function-like macros cannot be transformed into strings easily diff --git a/scripts/generate_visualc_files.pl b/scripts/generate_visualc_files.pl index 90ab609d76..0342f8f8d8 100755 --- a/scripts/generate_visualc_files.pl +++ b/scripts/generate_visualc_files.pl @@ -117,7 +117,7 @@ sub gen_app { my $srcs = "\n \r"; if( $appname eq "ssl_client2" or $appname eq "ssl_server2" or $appname eq "query_compile_time_config" ) { - $srcs .= "\n \r"; + $srcs .= "\n \r"; } my $content = $template; diff --git a/tests/scripts/check-generated-files.sh b/tests/scripts/check-generated-files.sh index 065ea33a2a..f41e465c38 100755 --- a/tests/scripts/check-generated-files.sh +++ b/tests/scripts/check-generated-files.sh @@ -65,6 +65,6 @@ check() } check scripts/generate_errors.pl library/error.c -check scripts/generate_query_config.pl programs/ssl/query_config.c +check scripts/generate_query_config.pl programs/test/query_config.c check scripts/generate_features.pl library/version_features.c check scripts/generate_visualc_files.pl visualc/VS2010 diff --git a/visualc/VS2010/query_compile_time_config.vcxproj b/visualc/VS2010/query_compile_time_config.vcxproj index bb2f7ad19b..112fea8d01 100644 --- a/visualc/VS2010/query_compile_time_config.vcxproj +++ b/visualc/VS2010/query_compile_time_config.vcxproj @@ -20,7 +20,7 @@ - + diff --git a/visualc/VS2010/ssl_client2.vcxproj b/visualc/VS2010/ssl_client2.vcxproj index dd922c047b..f23f9a16b0 100644 --- a/visualc/VS2010/ssl_client2.vcxproj +++ b/visualc/VS2010/ssl_client2.vcxproj @@ -20,7 +20,7 @@ - + diff --git a/visualc/VS2010/ssl_server2.vcxproj b/visualc/VS2010/ssl_server2.vcxproj index d4629bd414..df1ee28254 100644 --- a/visualc/VS2010/ssl_server2.vcxproj +++ b/visualc/VS2010/ssl_server2.vcxproj @@ -20,7 +20,7 @@ - + From e123395317421f8b75a09e6c556aa1ed6c4cfba2 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 11 Feb 2020 16:51:34 +0100 Subject: [PATCH 05/10] cmake: link programs that only use crypto with libmbedcrypto When building with CMake, for sample programs that only use functionality in libmbedcrypto (i.e. crypto and platform), link with libmbedcrypto, not with libmbedtls. This doesn't change the result, because the linker skips libraries in which no symbol is used, but it changes the build dependencies, and it has the advantage of bringing programs/*/CMakeLists.txt closer to the corresponding files under crypto/. The programs concerned are crypto sample and test programs, and programs that only use (potential) platform functions such as mbedtls_printf. dh_client and dh_server keep linking with mbedtls because they use functions from the net_sockets module. --- programs/aes/CMakeLists.txt | 4 ++-- programs/hash/CMakeLists.txt | 4 ++-- programs/pkey/CMakeLists.txt | 36 +++++++++++++++++----------------- programs/random/CMakeLists.txt | 6 +++--- programs/test/CMakeLists.txt | 8 ++++---- programs/util/CMakeLists.txt | 2 +- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/programs/aes/CMakeLists.txt b/programs/aes/CMakeLists.txt index f5a0caabbb..6c4c7e10f3 100644 --- a/programs/aes/CMakeLists.txt +++ b/programs/aes/CMakeLists.txt @@ -1,8 +1,8 @@ add_executable(aescrypt2 aescrypt2.c) -target_link_libraries(aescrypt2 mbedtls) +target_link_libraries(aescrypt2 mbedcrypto) add_executable(crypt_and_hash crypt_and_hash.c) -target_link_libraries(crypt_and_hash mbedtls) +target_link_libraries(crypt_and_hash mbedcrypto) install(TARGETS aescrypt2 crypt_and_hash DESTINATION "bin" diff --git a/programs/hash/CMakeLists.txt b/programs/hash/CMakeLists.txt index eda975bb06..3c6cca9d4f 100644 --- a/programs/hash/CMakeLists.txt +++ b/programs/hash/CMakeLists.txt @@ -1,8 +1,8 @@ add_executable(hello hello.c) -target_link_libraries(hello mbedtls) +target_link_libraries(hello mbedcrypto) add_executable(generic_sum generic_sum.c) -target_link_libraries(generic_sum mbedtls) +target_link_libraries(generic_sum mbedcrypto) install(TARGETS hello generic_sum DESTINATION "bin" diff --git a/programs/pkey/CMakeLists.txt b/programs/pkey/CMakeLists.txt index 5a37a42129..8456228db0 100644 --- a/programs/pkey/CMakeLists.txt +++ b/programs/pkey/CMakeLists.txt @@ -2,61 +2,61 @@ add_executable(dh_client dh_client.c) target_link_libraries(dh_client mbedtls) add_executable(dh_genprime dh_genprime.c) -target_link_libraries(dh_genprime mbedtls) +target_link_libraries(dh_genprime mbedcrypto) add_executable(dh_server dh_server.c) target_link_libraries(dh_server mbedtls) add_executable(ecdh_curve25519 ecdh_curve25519.c) -target_link_libraries(ecdh_curve25519 mbedtls) +target_link_libraries(ecdh_curve25519 mbedcrypto) add_executable(ecdsa ecdsa.c) -target_link_libraries(ecdsa mbedtls) +target_link_libraries(ecdsa mbedcrypto) add_executable(gen_key gen_key.c) -target_link_libraries(gen_key mbedtls) +target_link_libraries(gen_key mbedcrypto) add_executable(key_app key_app.c) -target_link_libraries(key_app mbedtls) +target_link_libraries(key_app mbedcrypto) add_executable(key_app_writer key_app_writer.c) -target_link_libraries(key_app_writer mbedtls) +target_link_libraries(key_app_writer mbedcrypto) add_executable(mpi_demo mpi_demo.c) -target_link_libraries(mpi_demo mbedtls) +target_link_libraries(mpi_demo mbedcrypto) add_executable(rsa_genkey rsa_genkey.c) -target_link_libraries(rsa_genkey mbedtls) +target_link_libraries(rsa_genkey mbedcrypto) add_executable(rsa_sign rsa_sign.c) -target_link_libraries(rsa_sign mbedtls) +target_link_libraries(rsa_sign mbedcrypto) add_executable(rsa_verify rsa_verify.c) -target_link_libraries(rsa_verify mbedtls) +target_link_libraries(rsa_verify mbedcrypto) add_executable(rsa_sign_pss rsa_sign_pss.c) -target_link_libraries(rsa_sign_pss mbedtls) +target_link_libraries(rsa_sign_pss mbedcrypto) add_executable(rsa_verify_pss rsa_verify_pss.c) -target_link_libraries(rsa_verify_pss mbedtls) +target_link_libraries(rsa_verify_pss mbedcrypto) add_executable(rsa_encrypt rsa_encrypt.c) -target_link_libraries(rsa_encrypt mbedtls) +target_link_libraries(rsa_encrypt mbedcrypto) add_executable(rsa_decrypt rsa_decrypt.c) -target_link_libraries(rsa_decrypt mbedtls) +target_link_libraries(rsa_decrypt mbedcrypto) add_executable(pk_sign pk_sign.c) -target_link_libraries(pk_sign mbedtls) +target_link_libraries(pk_sign mbedcrypto) add_executable(pk_verify pk_verify.c) -target_link_libraries(pk_verify mbedtls) +target_link_libraries(pk_verify mbedcrypto) add_executable(pk_encrypt pk_encrypt.c) -target_link_libraries(pk_encrypt mbedtls) +target_link_libraries(pk_encrypt mbedcrypto) add_executable(pk_decrypt pk_decrypt.c) -target_link_libraries(pk_decrypt mbedtls) +target_link_libraries(pk_decrypt mbedcrypto) install(TARGETS dh_client dh_genprime dh_server key_app mpi_demo rsa_genkey rsa_sign rsa_verify rsa_encrypt rsa_decrypt pk_encrypt pk_decrypt pk_sign pk_verify gen_key DESTINATION "bin" diff --git a/programs/random/CMakeLists.txt b/programs/random/CMakeLists.txt index 30933d88dd..630c66e9d2 100644 --- a/programs/random/CMakeLists.txt +++ b/programs/random/CMakeLists.txt @@ -1,11 +1,11 @@ add_executable(gen_random_havege gen_random_havege.c) -target_link_libraries(gen_random_havege mbedtls) +target_link_libraries(gen_random_havege mbedcrypto) add_executable(gen_random_ctr_drbg gen_random_ctr_drbg.c) -target_link_libraries(gen_random_ctr_drbg mbedtls) +target_link_libraries(gen_random_ctr_drbg mbedcrypto) add_executable(gen_entropy gen_entropy.c) -target_link_libraries(gen_entropy mbedtls) +target_link_libraries(gen_entropy mbedcrypto) install(TARGETS gen_random_havege gen_random_ctr_drbg gen_entropy DESTINATION "bin" diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index 64ed379e73..a26c096de9 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -14,22 +14,22 @@ add_executable(selftest selftest.c) target_link_libraries(selftest ${libs}) add_executable(benchmark benchmark.c) -target_link_libraries(benchmark ${libs}) +target_link_libraries(benchmark mbedcrypto) if(TEST_CPP) add_executable(cpp_dummy_build cpp_dummy_build.cpp) - target_link_libraries(cpp_dummy_build ${libs}) + target_link_libraries(cpp_dummy_build mbedcrypto) endif() add_executable(udp_proxy udp_proxy.c) target_link_libraries(udp_proxy ${libs}) add_executable(zeroize zeroize.c) -target_link_libraries(zeroize ${libs}) +target_link_libraries(zeroize mbedcrypto) add_executable(query_compile_time_config query_compile_time_config.c) target_sources(query_compile_time_config PUBLIC query_config.c) -target_link_libraries(query_compile_time_config ${libs}) +target_link_libraries(query_compile_time_config mbedcrypto) install(TARGETS selftest benchmark udp_proxy query_compile_time_config DESTINATION "bin" diff --git a/programs/util/CMakeLists.txt b/programs/util/CMakeLists.txt index f9b6604538..4c3fb0dfaa 100644 --- a/programs/util/CMakeLists.txt +++ b/programs/util/CMakeLists.txt @@ -1,5 +1,5 @@ set(libs - mbedtls + mbedcrypto ) add_executable(strerror strerror.c) From e732f044432d6b69d2be45df96f3937a48621cb6 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 11 Feb 2020 16:55:12 +0100 Subject: [PATCH 06/10] cmake: link programs that only use x509 with libmbedx509 When building with CMake, for sample programs that only use functionality in libmbedcrypto and libmbedx509, link with libmbedx509, not with libmbedtls. cert_app makes a TLS connection, so do link it with libmbedtls. --- programs/x509/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/x509/CMakeLists.txt b/programs/x509/CMakeLists.txt index 39b8b5bab3..68dec99a00 100644 --- a/programs/x509/CMakeLists.txt +++ b/programs/x509/CMakeLists.txt @@ -1,5 +1,5 @@ set(libs - mbedtls + mbedx509 ) if(USE_PKCS11_HELPER_LIBRARY) @@ -11,7 +11,7 @@ if(ENABLE_ZLIB_SUPPORT) endif(ENABLE_ZLIB_SUPPORT) add_executable(cert_app cert_app.c) -target_link_libraries(cert_app ${libs}) +target_link_libraries(cert_app ${libs} mbedtls) add_executable(crl_app crl_app.c) target_link_libraries(crl_app ${libs}) From db0cb2578c160ca7d083b5e2ca356b6ed5d77b8c Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 28 Oct 2019 17:28:46 +0100 Subject: [PATCH 07/10] Fix CTR_DRBG benchmark You can't reuse a CTR_DRBG context without free()ing it and re-init()ing. This generally happened to work, but was never guaranteed. It could have failed with alternative implementations of the AES module because mbedtls_ctr_drbg_seed() calls mbedtls_aes_init() on a context which is already initialized if mbedtls_ctr_drbg_seed() hasn't been called before, plausibly causing a memory leak. Calling free() and seed() with no intervening init fails when MBEDTLS_THREADING_C is enabled and all-bits-zero is not a valid mutex representation. So add the missing free() and init(). --- programs/test/benchmark.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/programs/test/benchmark.c b/programs/test/benchmark.c index b005c203af..8f89c70c61 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -686,12 +686,13 @@ int main( int argc, char *argv[] ) mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_init( &ctr_drbg ); - if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) mbedtls_exit(1); TIME_AND_TSC( "CTR_DRBG (NOPR)", mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) ); + mbedtls_ctr_drbg_free( &ctr_drbg ); + mbedtls_ctr_drbg_init( &ctr_drbg ); if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 ) mbedtls_exit(1); mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON ); From 6fc21f630cb6cdd56d91e05e39aa1ed30f655b37 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 17 Sep 2019 18:18:58 +0200 Subject: [PATCH 08/10] Add a calloc self-test Add a very basic test of calloc to the selftest program. The selftest program acts in its capacity as a platform compatibility checker rather than in its capacity as a test of the library. The main objective is to report whether calloc returns NULL for a size of 0. Also observe whether a free/alloc sequence returns the address that was just freed and whether a size overflow is properly detected. --- programs/test/selftest.c | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/programs/test/selftest.c b/programs/test/selftest.c index 727054ee60..bd28e9a368 100644 --- a/programs/test/selftest.c +++ b/programs/test/selftest.c @@ -66,6 +66,8 @@ #else #include #include +#define mbedtls_calloc calloc +#define mbedtls_free free #define mbedtls_printf printf #define mbedtls_snprintf snprintf #define mbedtls_exit exit @@ -78,6 +80,86 @@ #endif +#if defined MBEDTLS_SELF_TEST +/* Sanity check for malloc. This is not expected to fail, and is rather + * intended to display potentially useful information about the platform, + * in particular the behavior of malloc(0). */ +static int calloc_self_test( int verbose ) +{ + int failures = 0; + void *empty1 = mbedtls_calloc( 0, 1 ); + void *empty2 = mbedtls_calloc( 0, 1 ); + void *buffer1 = mbedtls_calloc( 1, 1 ); + void *buffer2 = mbedtls_calloc( 1, 1 ); + uintptr_t old_buffer1; + + if( empty1 == NULL && empty2 == NULL ) + { + if( verbose ) + mbedtls_printf( " CALLOC(0): passed (NULL)\n" ); + } + else if( empty1 == NULL || empty2 == NULL ) + { + if( verbose ) + mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" ); + ++failures; + } + else if( empty1 == empty2 ) + { + if( verbose ) + mbedtls_printf( " CALLOC(0): passed (same non-null)\n" ); + } + else + { + if( verbose ) + mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" ); + } + + if( buffer1 == NULL || buffer2 == NULL ) + { + if( verbose ) + mbedtls_printf( " CALLOC(1): failed (NULL)\n" ); + ++failures; + } + else if( buffer1 == buffer2 ) + { + if( verbose ) + mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" ); + ++failures; + } + else + { + if( verbose ) + mbedtls_printf( " CALLOC(1): passed\n" ); + } + + old_buffer1 = (uintptr_t) buffer1; + mbedtls_free( buffer1 ); + buffer1 = mbedtls_calloc( 1, 1 ); + if( buffer1 == NULL ) + { + if( verbose ) + mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" ); + ++failures; + } + else + { + if( verbose ) + mbedtls_printf( " CALLOC(1 again): passed (%s address)\n", + (uintptr_t) old_buffer1 == (uintptr_t) buffer1 ? + "same" : "different" ); + } + + if( verbose ) + mbedtls_printf( "\n" ); + mbedtls_free( empty1 ); + mbedtls_free( empty2 ); + mbedtls_free( buffer1 ); + mbedtls_free( buffer2 ); + return( failures ); +} +#endif /* MBEDTLS_SELF_TEST */ + static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret ) { int ret; @@ -174,6 +256,7 @@ typedef struct const selftest_t selftests[] = { + {"calloc", calloc_self_test}, #if defined(MBEDTLS_MD2_C) {"md2", mbedtls_md2_self_test}, #endif From c4ef7a9de31276365e7350fb38427af0b16dc48d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 17 Sep 2019 19:04:38 +0200 Subject: [PATCH 09/10] Add a test component with malloc(0) returning NULL Exercise the library functions with calloc returning NULL for a size of 0. Make this a separate job with UBSan (and ASan) to detect places where we try to dereference the result of calloc(0) or to do things like buf = calloc(size, 1); if (buf == NULL && size != 0) return INSUFFICIENT_MEMORY; memcpy(buf, source, size); which has undefined behavior when buf is NULL at the memcpy call even if size is 0. This is needed because other test components jobs either use the system malloc which returns non-NULL on Linux and FreeBSD, or the memory_buffer_alloc malloc which returns NULL but does not give as useful feedback with ASan (because the whole heap is a single C object). --- tests/configs/config-wrapper-malloc-0-null.h | 39 ++++++++++++++++++++ tests/scripts/all.sh | 15 ++++++++ 2 files changed, 54 insertions(+) create mode 100644 tests/configs/config-wrapper-malloc-0-null.h diff --git a/tests/configs/config-wrapper-malloc-0-null.h b/tests/configs/config-wrapper-malloc-0-null.h new file mode 100644 index 0000000000..ed74eda63a --- /dev/null +++ b/tests/configs/config-wrapper-malloc-0-null.h @@ -0,0 +1,39 @@ +/* config.h wrapper that forces calloc(0) to return NULL. + * Used for testing. + */ +/* + * Copyright (C) 2019, ARM Limited, All Rights Reserved + * 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 file is part of mbed TLS (https://tls.mbed.org) + */ + +#ifndef MBEDTLS_CONFIG_H +/* Don't #define MBEDTLS_CONFIG_H, let config.h do it. */ + +#include "mbedtls/config.h" + +#include +static inline void *custom_calloc( size_t nmemb, size_t size ) +{ + if( nmemb == 0 || size == 0 ) + return( NULL ); + return( calloc( nmemb, size ) ); +} + +#define MBEDTLS_PLATFORM_MEMORY +#define MBEDTLS_PLATFORM_STD_CALLOC custom_calloc + +#endif /* MBEDTLS_CONFIG_H */ diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index cc19ab2bbf..173c096878 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -1183,6 +1183,21 @@ component_test_platform_calloc_macro () { make test } +component_test_malloc_0_null () { + msg "build: malloc(0) returns NULL (ASan+UBSan build)" + scripts/config.pl full + scripts/config.pl unset MBEDTLS_MEMORY_BUFFER_ALLOC_C + make CC=gcc CFLAGS="'-DMBEDTLS_CONFIG_FILE=\"$PWD/tests/configs/config-wrapper-malloc-0-null.h\"' -O -Werror -Wall -Wextra -fsanitize=address,undefined" LDFLAGS='-fsanitize=address,undefined' + + msg "test: malloc(0) returns NULL (ASan+UBSan build)" + make test + + msg "selftest: malloc(0) returns NULL (ASan+UBSan build)" + # Just the calloc selftest. "make test" ran the others as part of the + # test suites. + if_build_succeeded programs/test/selftest calloc +} + component_test_make_shared () { msg "build/test: make shared" # ~ 40s make SHARED=1 all check From 765d240ca6890e7e801513d9705d08984ac27318 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 11 Feb 2020 18:26:34 +0100 Subject: [PATCH 10/10] Test component with malloc(0) returning NULL: run some ssl tests --- tests/scripts/all.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 173c096878..2ade64dac2 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -1196,6 +1196,14 @@ component_test_malloc_0_null () { # Just the calloc selftest. "make test" ran the others as part of the # test suites. if_build_succeeded programs/test/selftest calloc + + msg "test ssl-opt.sh: malloc(0) returns NULL (ASan+UBSan build)" + # Run a subset of the tests. The choice is a balance between coverage + # and time (including time indirectly wasted due to flaky tests). + # The current choice is to skip tests whose description includes + # "proxy", which is an approximation of skipping tests that use the + # UDP proxy, which tend to be slower and flakier. + if_build_succeeded tests/ssl-opt.sh -e 'proxy' } component_test_make_shared () {