From 09f1ee68b62c922a640a72455ec08fd10e0cc2e4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 15 Dec 2021 12:53:34 +0100 Subject: [PATCH 1/5] Commit header file Having an automatically generated header file makes it harder to have working build scripts. The content of ssl_debug_helpers_generated.h isn't likely to change often, so we can update it manually. Signed-off-by: Gilles Peskine --- library/.gitignore | 1 - library/ssl_debug_helpers_generated.h | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 library/ssl_debug_helpers_generated.h diff --git a/library/.gitignore b/library/.gitignore index f6619d273..cf2ddac3a 100644 --- a/library/.gitignore +++ b/library/.gitignore @@ -7,4 +7,3 @@ libmbed* /error.c /version_features.c /ssl_debug_helpers_generated.c -/ssl_debug_helpers_generated.h diff --git a/library/ssl_debug_helpers_generated.h b/library/ssl_debug_helpers_generated.h new file mode 100644 index 000000000..0315273d2 --- /dev/null +++ b/library/ssl_debug_helpers_generated.h @@ -0,0 +1,26 @@ +/* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */ +#ifndef MBEDTLS_SSL_DEBUG_HELPERS_H +#define MBEDTLS_SSL_DEBUG_HELPERS_H + +#include "common.h" + +#if defined(MBEDTLS_DEBUG_C) + +#include "mbedtls/ssl.h" +#include "ssl_misc.h" + + +const char *mbedtls_ssl_states_str( mbedtls_ssl_states in ); + +const char *mbedtls_tls_prf_types_str( mbedtls_tls_prf_types in ); + +const char *mbedtls_ssl_key_export_type_str( mbedtls_ssl_key_export_type in ); + + + +#endif /* MBEDTLS_DEBUG_C */ + +#endif /* SSL_DEBUG_HELPERS_H */ + +/* End of automatically generated file. */ + From ccbc318fc59ba8140c342797e77c5f76cbb21cf7 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 15 Dec 2021 12:55:37 +0100 Subject: [PATCH 2/5] Remove generation of ssl_debug_helpers_generated.h It's now under version control and meant to be updated manually. Signed-off-by: Gilles Peskine --- library/CMakeLists.txt | 2 -- library/Makefile | 9 +++--- scripts/generate_ssl_debug_helpers.py | 39 ++------------------------ tests/scripts/check-generated-files.sh | 2 +- 4 files changed, 7 insertions(+), 45 deletions(-) diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 1059e8c05..add078413 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -147,7 +147,6 @@ if(GEN_FILES) add_custom_command( OUTPUT - ${CMAKE_CURRENT_BINARY_DIR}/ssl_debug_helpers_generated.h ${CMAKE_CURRENT_BINARY_DIR}/ssl_debug_helpers_generated.c COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} @@ -162,7 +161,6 @@ else() link_to_source(error.c) link_to_source(version_features.c) link_to_source(ssl_debug_helpers_generated.c) - link_to_source(ssl_debug_helpers_generated.h) endif() if(CMAKE_COMPILER_IS_GNUCC) diff --git a/library/Makefile b/library/Makefile index a245d971d..b936c236b 100644 --- a/library/Makefile +++ b/library/Makefile @@ -290,7 +290,7 @@ libmbedcrypto.dll: $(OBJS_CRYPTO) .PHONY: generated_files GENERATED_FILES = \ error.c version_features.c \ - ssl_debug_helpers_generated.c ssl_debug_helpers_generated.h + ssl_debug_helpers_generated.c generated_files: $(GENERATED_FILES) error.c: ../scripts/generate_errors.pl @@ -300,10 +300,9 @@ error.c: echo " Gen $@" $(PERL) ../scripts/generate_errors.pl -ssl_debug_helpers_generated.c: | ssl_debug_helpers_generated.h -ssl_debug_helpers_generated.h: ../scripts/generate_ssl_debug_helpers.py -ssl_debug_helpers_generated.h: $(filter-out %config%,$(wildcard ../include/mbedtls/*.h)) -ssl_debug_helpers_generated.h: +ssl_debug_helpers_generated.c: ../scripts/generate_ssl_debug_helpers.py +ssl_debug_helpers_generated.c: $(filter-out %config%,$(wildcard ../include/mbedtls/*.h)) +ssl_debug_helpers_generated.c: echo " Gen $@" $(PYTHON) ../scripts/generate_ssl_debug_helpers.py --mbedtls-root .. . diff --git a/scripts/generate_ssl_debug_helpers.py b/scripts/generate_ssl_debug_helpers.py index 19fac656c..37e978646 100755 --- a/scripts/generate_ssl_debug_helpers.py +++ b/scripts/generate_ssl_debug_helpers.py @@ -232,10 +232,7 @@ class EnumDefinition: body = body.format(translation_table='\n'.join(translation_table), name=self._name, prototype=self._prototype) - prototype = 'const char *{name}_str( {prototype} in );\n' - prototype = prototype.format(name=self._name, - prototype=self._prototype) - return body, prototype + return body OUTPUT_C_TEMPLATE = '''\ @@ -254,28 +251,6 @@ OUTPUT_C_TEMPLATE = '''\ ''' -OUTPUT_H_TEMPLATE = '''\ -/* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */ -#ifndef MBEDTLS_SSL_DEBUG_HELPERS_H -#define MBEDTLS_SSL_DEBUG_HELPERS_H - -#include "common.h" - -#if defined(MBEDTLS_DEBUG_C) - -#include "mbedtls/ssl.h" -#include "ssl_misc.h" - -{functions} - -#endif /* MBEDTLS_DEBUG_C */ - -#endif /* SSL_DEBUG_HELPERS_H */ - -/* End of automatically generated file. */ - -''' - def generate_ssl_debug_helpers(output_directory, mbedtls_root): """ @@ -286,23 +261,17 @@ def generate_ssl_debug_helpers(output_directory, mbedtls_root): source_code = remove_c_comments(f.read()) definitions = dict() - prototypes = dict() for start, instance in preprocess_c_source_code(source_code, EnumDefinition): if start in definitions: continue if isinstance(instance, EnumDefinition): - definition, prototype = instance.generate_tranlation_function() + definition = instance.generate_tranlation_function() else: definition = instance - prototype = instance definitions[start] = definition - prototypes[start] = prototype function_definitions = [str(v) for _, v in sorted(definitions.items())] - function_prototypes = [str(v) for _, v in sorted(prototypes.items())] if output_directory == sys.stdout: - sys.stdout.write(OUTPUT_H_TEMPLATE.format( - functions='\n'.join(function_prototypes))) sys.stdout.write(OUTPUT_C_TEMPLATE.format( functions='\n'.join(function_definitions))) else: @@ -310,10 +279,6 @@ def generate_ssl_debug_helpers(output_directory, mbedtls_root): f.write(OUTPUT_C_TEMPLATE.format( functions='\n'.join(function_definitions))) - with open(os.path.join(output_directory, 'ssl_debug_helpers_generated.h'), 'w') as f: - f.write(OUTPUT_H_TEMPLATE.format( - functions='\n'.join(function_prototypes))) - def main(): """ diff --git a/tests/scripts/check-generated-files.sh b/tests/scripts/check-generated-files.sh index b35da3d68..994fd243b 100755 --- a/tests/scripts/check-generated-files.sh +++ b/tests/scripts/check-generated-files.sh @@ -118,7 +118,7 @@ check() check scripts/generate_errors.pl library/error.c check scripts/generate_query_config.pl programs/test/query_config.c check scripts/generate_features.pl library/version_features.c -check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.h library/ssl_debug_helpers_generated.c +check scripts/generate_ssl_debug_helpers.py library/ssl_debug_helpers_generated.c # generate_visualc_files enumerates source files (library/*.c). It doesn't # care about their content, but the files must exist. So it must run after # the step that creates or updates these files. From 923d5c9e3ced47c34a4a08bd6969df6cd60b8f18 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 15 Dec 2021 12:56:54 +0100 Subject: [PATCH 3/5] Rename ssl_debug_helpers.h It's no longer generated, so rename it accordingly. Signed-off-by: Gilles Peskine --- library/{ssl_debug_helpers_generated.h => ssl_debug_helpers.h} | 0 library/ssl_tls13_client.c | 2 +- library/ssl_tls13_server.c | 2 +- scripts/generate_ssl_debug_helpers.py | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename library/{ssl_debug_helpers_generated.h => ssl_debug_helpers.h} (100%) diff --git a/library/ssl_debug_helpers_generated.h b/library/ssl_debug_helpers.h similarity index 100% rename from library/ssl_debug_helpers_generated.h rename to library/ssl_debug_helpers.h diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 31d7dafdb..dcf1ff896 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -34,7 +34,7 @@ #include "ssl_misc.h" #include "ecdh_misc.h" #include "ssl_tls13_keys.h" -#include "ssl_debug_helpers_generated.h" +#include "ssl_debug_helpers.h" /* Write extensions */ diff --git a/library/ssl_tls13_server.c b/library/ssl_tls13_server.c index faace4a0e..5e74083dd 100644 --- a/library/ssl_tls13_server.c +++ b/library/ssl_tls13_server.c @@ -26,7 +26,7 @@ #include "mbedtls/debug.h" #include "ssl_misc.h" -#include "ssl_debug_helpers_generated.h" +#include "ssl_debug_helpers.h" int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl ) { diff --git a/scripts/generate_ssl_debug_helpers.py b/scripts/generate_ssl_debug_helpers.py index 37e978646..21122b560 100755 --- a/scripts/generate_ssl_debug_helpers.py +++ b/scripts/generate_ssl_debug_helpers.py @@ -242,7 +242,7 @@ OUTPUT_C_TEMPLATE = '''\ #if defined(MBEDTLS_DEBUG_C) -#include "ssl_debug_helpers_generated.h" +#include "ssl_debug_helpers.h" {functions} From 1a1e78fa550b041d6a8ce22b7af84839e3fd7eb0 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 15 Dec 2021 12:58:22 +0100 Subject: [PATCH 4/5] Remove comments indicating that the file was automatically generated Signed-off-by: Gilles Peskine --- library/ssl_debug_helpers.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/library/ssl_debug_helpers.h b/library/ssl_debug_helpers.h index 0315273d2..7288be1c2 100644 --- a/library/ssl_debug_helpers.h +++ b/library/ssl_debug_helpers.h @@ -1,4 +1,3 @@ -/* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */ #ifndef MBEDTLS_SSL_DEBUG_HELPERS_H #define MBEDTLS_SSL_DEBUG_HELPERS_H @@ -21,6 +20,3 @@ const char *mbedtls_ssl_key_export_type_str( mbedtls_ssl_key_export_type in ); #endif /* MBEDTLS_DEBUG_C */ #endif /* SSL_DEBUG_HELPERS_H */ - -/* End of automatically generated file. */ - From 863b96a21b476ad9519c74492bb5bc7f3c665c56 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 16 Dec 2021 10:04:58 +0100 Subject: [PATCH 5/5] Add copyright notice to ssl_debug_helpers* Signed-off-by: Gilles Peskine --- library/ssl_debug_helpers.h | 22 ++++++++++++++++++++++ scripts/generate_ssl_debug_helpers.py | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/library/ssl_debug_helpers.h b/library/ssl_debug_helpers.h index 7288be1c2..2fc416351 100644 --- a/library/ssl_debug_helpers.h +++ b/library/ssl_debug_helpers.h @@ -1,3 +1,25 @@ +/** + * \file ssl_debug_helpers.h + * + * \brief Automatically generated helper functions for debugging + */ +/* + * 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. + */ + #ifndef MBEDTLS_SSL_DEBUG_HELPERS_H #define MBEDTLS_SSL_DEBUG_HELPERS_H diff --git a/scripts/generate_ssl_debug_helpers.py b/scripts/generate_ssl_debug_helpers.py index 21122b560..c1487260d 100755 --- a/scripts/generate_ssl_debug_helpers.py +++ b/scripts/generate_ssl_debug_helpers.py @@ -238,6 +238,28 @@ class EnumDefinition: OUTPUT_C_TEMPLATE = '''\ /* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */ +/** + * \file ssl_debug_helpers_generated.c + * + * \brief Automatically generated helper functions for debugging + */ +/* + * 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. + */ + #include "common.h" #if defined(MBEDTLS_DEBUG_C)