From bfcfe74b4e7ce9ed83dd9a9627b4b453501c3875 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 22 Feb 2022 16:41:39 +0800 Subject: [PATCH] add signature algorithm debug helper Signed-off-by: Jerry Yu --- library/ssl_debug_helpers.h | 2 +- scripts/generate_ssl_debug_helpers.py | 64 ++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/library/ssl_debug_helpers.h b/library/ssl_debug_helpers.h index 2ffc5f41f..29b64dc4e 100644 --- a/library/ssl_debug_helpers.h +++ b/library/ssl_debug_helpers.h @@ -39,7 +39,7 @@ 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 ); - +const char *mbedtls_ssl_sig_alg_to_str( uint16_t in ); #endif /* MBEDTLS_DEBUG_C */ diff --git a/scripts/generate_ssl_debug_helpers.py b/scripts/generate_ssl_debug_helpers.py index 98e1c48c8..6c9d670f7 100755 --- a/scripts/generate_ssl_debug_helpers.py +++ b/scripts/generate_ssl_debug_helpers.py @@ -88,7 +88,8 @@ def preprocess_c_source_code(source, *classes): if has_instance is False: has_instance = True yield pair_start, start_line - yield instance.span()[0], instance + if instance: + yield instance.span()[0], instance if has_instance: yield start, end_line @@ -234,6 +235,63 @@ class EnumDefinition: prototype=self._prototype) return body +class SignatureAlgorithmDefinition: + """ + Generate helper functions for signature algorithms. + + It generates translation function from signature algorithm define to string. + Signature algorithm definition looks like: + #define MBEDTLS_TLS1_3_SIG_[ upper case signature algorithm ] [ value(hex) ] + + Known limitation: + - the definitions SHOULD exist in same macro blocks. + """ + + @classmethod + def extract(cls, source_code, start=0, end=-1): + sig_alg_pattern = re.compile(r'#define\s+(?PMBEDTLS_TLS1_3_SIG_\w+)\s+' + + r'(?P0[xX][0-9a-fA-F]+)$', + re.MULTILINE | re.DOTALL) + matches = list(sig_alg_pattern.finditer(source_code, start, end)) + if matches: + yield SignatureAlgorithmDefinition(source_code, definitions=matches) + + def __init__(self, source_code, definitions=None): + if definitions is None: + definitions = [] + assert isinstance(definitions, list) and definitions + self._definitions = definitions + self._source = source_code + + def __repr__(self): + return 'SigAlgs({})'.format(self._definitions[0].span()) + + def span(self): + return self._definitions[0].span() + def __str__(self): + """ + Generate function for translating value to string + """ + translation_table = [] + for m in self._definitions: + name = m.groupdict()['name'] + translation_table.append( + '\tcase {}:\n\t return "{}";'.format(name, + name[len('MBEDTLS_TLS1_3_SIG_'):].lower()) + ) + + body = textwrap.dedent('''\ + const char *mbedtls_ssl_sig_alg_to_str( uint16_t in ) + {{ + switch( in ) + {{ + {translation_table} + }}; + + return "UNKOWN"; + }}''') + body = body.format(translation_table='\n'.join(translation_table)) + return body OUTPUT_C_TEMPLATE = '''\ /* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */ @@ -283,7 +341,9 @@ def generate_ssl_debug_helpers(output_directory, mbedtls_root): source_code = remove_c_comments(f.read()) definitions = dict() - for start, instance in preprocess_c_source_code(source_code, EnumDefinition): + for start, instance in preprocess_c_source_code(source_code, + EnumDefinition, + SignatureAlgorithmDefinition): if start in definitions: continue if isinstance(instance, EnumDefinition):