| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
|  | 2 |  | 
|  | 3 | """Generate library/ssl_debug_helps_generated.c | 
|  | 4 |  | 
|  | 5 | The code generated by this module includes debug helper functions that can not be | 
|  | 6 | implemented by fixed codes. | 
|  | 7 |  | 
|  | 8 | """ | 
|  | 9 |  | 
|  | 10 | # Copyright The Mbed TLS Contributors | 
|  | 11 | # SPDX-License-Identifier: Apache-2.0 | 
|  | 12 | # | 
|  | 13 | # Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 14 | # not use this file except in compliance with the License. | 
|  | 15 | # You may obtain a copy of the License at | 
|  | 16 | # | 
|  | 17 | # http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 18 | # | 
|  | 19 | # Unless required by applicable law or agreed to in writing, software | 
|  | 20 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 21 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 22 | # See the License for the specific language governing permissions and | 
|  | 23 | # limitations under the License. | 
|  | 24 | import sys | 
|  | 25 | import re | 
|  | 26 | import os | 
|  | 27 | import textwrap | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 28 | import argparse | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 29 | from mbedtls_dev import build_tree | 
|  | 30 |  | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 31 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 32 | def remove_c_comments(string): | 
|  | 33 | """ | 
|  | 34 | Remove C style comments from input string | 
|  | 35 | """ | 
|  | 36 | string_pattern = r"(?P<string>\".*?\"|\'.*?\')" | 
|  | 37 | comment_pattern = r"(?P<comment>/\*.*?\*/|//[^\r\n]*$)" | 
|  | 38 | pattern = re.compile(string_pattern + r'|' + comment_pattern, | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 39 | re.MULTILINE | re.DOTALL) | 
|  | 40 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 41 | def replacer(match): | 
|  | 42 | if match.lastgroup == 'comment': | 
|  | 43 | return "" | 
|  | 44 | return match.group() | 
|  | 45 | return pattern.sub(replacer, string) | 
|  | 46 |  | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 47 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 48 | class CondDirectiveNotMatch(Exception): | 
|  | 49 | pass | 
|  | 50 |  | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 51 |  | 
| Jerry Yu | e988f0f | 2021-11-11 13:22:20 +0800 | [diff] [blame] | 52 | def preprocess_c_source_code(source, *classes): | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 53 | """ | 
|  | 54 | Simple preprocessor for C source code. | 
|  | 55 |  | 
| Jerry Yu | 6389b25 | 2021-12-02 10:28:40 +0800 | [diff] [blame] | 56 | Only processses condition directives without expanding them. | 
|  | 57 | Yield object according to the classes input. Most match firstly | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 58 |  | 
| Jerry Yu | 6389b25 | 2021-12-02 10:28:40 +0800 | [diff] [blame] | 59 | If the directive pair does not match , raise CondDirectiveNotMatch. | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 60 |  | 
|  | 61 | Assume source code does not include comments and compile pass. | 
|  | 62 |  | 
|  | 63 | """ | 
|  | 64 |  | 
|  | 65 | pattern = re.compile(r"^[ \t]*#[ \t]*" + | 
|  | 66 | r"(?P<directive>(if[ \t]|ifndef[ \t]|ifdef[ \t]|else|endif))" + | 
|  | 67 | r"[ \t]*(?P<param>(.*\\\n)*.*$)", | 
|  | 68 | re.MULTILINE) | 
|  | 69 | stack = [] | 
|  | 70 |  | 
|  | 71 | def _yield_objects(s, d, p, st, end): | 
|  | 72 | """ | 
|  | 73 | Output matched source piece | 
|  | 74 | """ | 
|  | 75 | nonlocal stack | 
|  | 76 | start_line, end_line = '', '' | 
|  | 77 | if stack: | 
|  | 78 | start_line = '#{} {}'.format(d, p) | 
|  | 79 | if d == 'if': | 
|  | 80 | end_line = '#endif /* {} */'.format(p) | 
|  | 81 | elif d == 'ifdef': | 
|  | 82 | end_line = '#endif /* defined({}) */'.format(p) | 
|  | 83 | else: | 
|  | 84 | end_line = '#endif /* !defined({}) */'.format(p) | 
|  | 85 | has_instance = False | 
|  | 86 | for cls in classes: | 
|  | 87 | for instance in cls.extract(s, st, end): | 
|  | 88 | if has_instance is False: | 
|  | 89 | has_instance = True | 
|  | 90 | yield pair_start, start_line | 
| Jerry Yu | d73d0a3 | 2022-03-29 16:37:51 +0800 | [diff] [blame] | 91 | yield instance.span()[0], instance | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 92 | if has_instance: | 
|  | 93 | yield start, end_line | 
|  | 94 |  | 
|  | 95 | for match in pattern.finditer(source): | 
|  | 96 |  | 
|  | 97 | directive = match.groupdict()['directive'].strip() | 
|  | 98 | param = match.groupdict()['param'] | 
|  | 99 | start, end = match.span() | 
|  | 100 |  | 
|  | 101 | if directive in ('if', 'ifndef', 'ifdef'): | 
|  | 102 | stack.append((directive, param, start, end)) | 
|  | 103 | continue | 
|  | 104 |  | 
|  | 105 | if not stack: | 
|  | 106 | raise CondDirectiveNotMatch() | 
|  | 107 |  | 
|  | 108 | pair_directive, pair_param, pair_start, pair_end = stack.pop() | 
|  | 109 | yield from _yield_objects(source, | 
|  | 110 | pair_directive, | 
|  | 111 | pair_param, | 
|  | 112 | pair_end, | 
|  | 113 | start) | 
|  | 114 |  | 
|  | 115 | if directive == 'endif': | 
|  | 116 | continue | 
|  | 117 |  | 
|  | 118 | if pair_directive == 'if': | 
|  | 119 | directive = 'if' | 
|  | 120 | param = "!( {} )".format(pair_param) | 
|  | 121 | elif pair_directive == 'ifdef': | 
|  | 122 | directive = 'ifndef' | 
|  | 123 | param = pair_param | 
|  | 124 | else: | 
|  | 125 | directive = 'ifdef' | 
|  | 126 | param = pair_param | 
|  | 127 |  | 
|  | 128 | stack.append((directive, param, start, end)) | 
|  | 129 | assert not stack, len(stack) | 
|  | 130 |  | 
|  | 131 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 132 | class EnumDefinition: | 
|  | 133 | """ | 
|  | 134 | Generate helper functions around enumeration. | 
|  | 135 |  | 
| Jerry Yu | 6389b25 | 2021-12-02 10:28:40 +0800 | [diff] [blame] | 136 | Currently, it generate translation function from enum value to string. | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 137 | Enum definition looks like: | 
|  | 138 | [typedef] enum [prefix name] { [body] } [suffix name]; | 
|  | 139 |  | 
|  | 140 | Known limitation: | 
|  | 141 | - the '}' and ';' SHOULD NOT exist in different macro blocks. Like | 
|  | 142 | ``` | 
|  | 143 | enum test { | 
|  | 144 | .... | 
|  | 145 | #if defined(A) | 
|  | 146 | .... | 
|  | 147 | }; | 
|  | 148 | #else | 
|  | 149 | .... | 
|  | 150 | }; | 
|  | 151 | #endif | 
|  | 152 | ``` | 
|  | 153 | """ | 
|  | 154 |  | 
|  | 155 | @classmethod | 
|  | 156 | def extract(cls, source_code, start=0, end=-1): | 
|  | 157 | enum_pattern = re.compile(r'enum\s*(?P<prefix_name>\w*)\s*' + | 
|  | 158 | r'{\s*(?P<body>[^}]*)}' + | 
|  | 159 | r'\s*(?P<suffix_name>\w*)\s*;', | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 160 | re.MULTILINE | re.DOTALL) | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 161 |  | 
|  | 162 | for match in enum_pattern.finditer(source_code, start, end): | 
|  | 163 | yield EnumDefinition(source_code, | 
|  | 164 | span=match.span(), | 
|  | 165 | group=match.groupdict()) | 
|  | 166 |  | 
|  | 167 | def __init__(self, source_code, span=None, group=None): | 
|  | 168 | assert isinstance(group, dict) | 
|  | 169 | prefix_name = group.get('prefix_name', None) | 
|  | 170 | suffix_name = group.get('suffix_name', None) | 
|  | 171 | body = group.get('body', None) | 
|  | 172 | assert prefix_name or suffix_name | 
|  | 173 | assert body | 
|  | 174 | assert span | 
|  | 175 | # If suffix_name exists, it is a typedef | 
|  | 176 | self._prototype = suffix_name if suffix_name else 'enum ' + prefix_name | 
|  | 177 | self._name = suffix_name if suffix_name else prefix_name | 
|  | 178 | self._body = body | 
|  | 179 | self._source = source_code | 
|  | 180 | self._span = span | 
|  | 181 |  | 
|  | 182 | def __repr__(self): | 
|  | 183 | return 'Enum({},{})'.format(self._name, self._span) | 
|  | 184 |  | 
|  | 185 | def __str__(self): | 
|  | 186 | return repr(self) | 
|  | 187 |  | 
|  | 188 | def span(self): | 
|  | 189 | return self._span | 
|  | 190 |  | 
| David Horstmann | 3be1271 | 2021-12-16 10:56:26 +0000 | [diff] [blame] | 191 | def generate_translation_function(self): | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 192 | """ | 
|  | 193 | Generate function for translating value to string | 
|  | 194 | """ | 
|  | 195 | translation_table = [] | 
|  | 196 |  | 
|  | 197 | for line in self._body.splitlines(): | 
|  | 198 |  | 
|  | 199 | if line.strip().startswith('#'): | 
|  | 200 | # Preprocess directive, keep it in table | 
|  | 201 | translation_table.append(line.strip()) | 
|  | 202 | continue | 
|  | 203 |  | 
|  | 204 | if not line.strip(): | 
|  | 205 | continue | 
|  | 206 |  | 
|  | 207 | for field in line.strip().split(','): | 
|  | 208 | if not field.strip(): | 
|  | 209 | continue | 
|  | 210 | member = field.strip().split()[0] | 
|  | 211 | translation_table.append( | 
|  | 212 | '{space}[{member}] = "{member}",'.format(member=member, | 
|  | 213 | space=' '*8) | 
|  | 214 | ) | 
|  | 215 |  | 
|  | 216 | body = textwrap.dedent('''\ | 
|  | 217 | const char *{name}_str( {prototype} in ) | 
|  | 218 | {{ | 
|  | 219 | const char * in_to_str[]= | 
|  | 220 | {{ | 
|  | 221 | {translation_table} | 
|  | 222 | }}; | 
|  | 223 |  | 
|  | 224 | if( in > ( sizeof( in_to_str )/sizeof( in_to_str[0]) - 1 ) || | 
|  | 225 | in_to_str[ in ] == NULL ) | 
|  | 226 | {{ | 
|  | 227 | return "UNKOWN_VAULE"; | 
|  | 228 | }} | 
|  | 229 | return in_to_str[ in ]; | 
|  | 230 | }} | 
|  | 231 | ''') | 
|  | 232 | body = body.format(translation_table='\n'.join(translation_table), | 
|  | 233 | name=self._name, | 
|  | 234 | prototype=self._prototype) | 
| Gilles Peskine | ccbc318 | 2021-12-15 12:55:37 +0100 | [diff] [blame] | 235 | return body | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 236 |  | 
| Jerry Yu | bfcfe74 | 2022-02-22 16:41:39 +0800 | [diff] [blame] | 237 | class SignatureAlgorithmDefinition: | 
|  | 238 | """ | 
|  | 239 | Generate helper functions for signature algorithms. | 
|  | 240 |  | 
|  | 241 | It generates translation function from signature algorithm define to string. | 
|  | 242 | Signature algorithm definition looks like: | 
|  | 243 | #define MBEDTLS_TLS1_3_SIG_[ upper case signature algorithm ] [ value(hex) ] | 
|  | 244 |  | 
|  | 245 | Known limitation: | 
|  | 246 | - the definitions SHOULD  exist in same macro blocks. | 
|  | 247 | """ | 
|  | 248 |  | 
|  | 249 | @classmethod | 
|  | 250 | def extract(cls, source_code, start=0, end=-1): | 
|  | 251 | sig_alg_pattern = re.compile(r'#define\s+(?P<name>MBEDTLS_TLS1_3_SIG_\w+)\s+' + | 
|  | 252 | r'(?P<value>0[xX][0-9a-fA-F]+)$', | 
|  | 253 | re.MULTILINE | re.DOTALL) | 
|  | 254 | matches = list(sig_alg_pattern.finditer(source_code, start, end)) | 
|  | 255 | if matches: | 
|  | 256 | yield SignatureAlgorithmDefinition(source_code, definitions=matches) | 
|  | 257 |  | 
|  | 258 | def __init__(self, source_code, definitions=None): | 
|  | 259 | if definitions is None: | 
|  | 260 | definitions = [] | 
|  | 261 | assert isinstance(definitions, list) and definitions | 
|  | 262 | self._definitions = definitions | 
|  | 263 | self._source = source_code | 
|  | 264 |  | 
|  | 265 | def __repr__(self): | 
|  | 266 | return 'SigAlgs({})'.format(self._definitions[0].span()) | 
|  | 267 |  | 
|  | 268 | def span(self): | 
|  | 269 | return self._definitions[0].span() | 
|  | 270 | def __str__(self): | 
|  | 271 | """ | 
|  | 272 | Generate function for translating value to string | 
|  | 273 | """ | 
|  | 274 | translation_table = [] | 
|  | 275 | for m in self._definitions: | 
|  | 276 | name = m.groupdict()['name'] | 
|  | 277 | translation_table.append( | 
|  | 278 | '\tcase {}:\n\t    return "{}";'.format(name, | 
|  | 279 | name[len('MBEDTLS_TLS1_3_SIG_'):].lower()) | 
|  | 280 | ) | 
|  | 281 |  | 
|  | 282 | body = textwrap.dedent('''\ | 
|  | 283 | const char *mbedtls_ssl_sig_alg_to_str( uint16_t in ) | 
|  | 284 | {{ | 
|  | 285 | switch( in ) | 
|  | 286 | {{ | 
|  | 287 | {translation_table} | 
|  | 288 | }}; | 
|  | 289 |  | 
|  | 290 | return "UNKOWN"; | 
|  | 291 | }}''') | 
|  | 292 | body = body.format(translation_table='\n'.join(translation_table)) | 
|  | 293 | return body | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 294 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 295 | OUTPUT_C_TEMPLATE = '''\ | 
|  | 296 | /* Automatically generated by generate_ssl_debug_helpers.py. DO NOT EDIT. */ | 
|  | 297 |  | 
| Gilles Peskine | 863b96a | 2021-12-16 10:04:58 +0100 | [diff] [blame] | 298 | /** | 
|  | 299 | * \file ssl_debug_helpers_generated.c | 
|  | 300 | * | 
|  | 301 | * \brief Automatically generated helper functions for debugging | 
|  | 302 | */ | 
|  | 303 | /* | 
|  | 304 | *  Copyright The Mbed TLS Contributors | 
|  | 305 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 306 | * | 
|  | 307 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 308 | *  not use this file except in compliance with the License. | 
|  | 309 | *  You may obtain a copy of the License at | 
|  | 310 | * | 
|  | 311 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 312 | * | 
|  | 313 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 314 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 315 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 316 | *  See the License for the specific language governing permissions and | 
|  | 317 | *  limitations under the License. | 
|  | 318 | */ | 
|  | 319 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 320 | #include "common.h" | 
|  | 321 |  | 
|  | 322 | #if defined(MBEDTLS_DEBUG_C) | 
|  | 323 |  | 
| Gilles Peskine | 923d5c9 | 2021-12-15 12:56:54 +0100 | [diff] [blame] | 324 | #include "ssl_debug_helpers.h" | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 325 |  | 
|  | 326 | {functions} | 
|  | 327 |  | 
|  | 328 | #endif /* MBEDTLS_DEBUG_C */ | 
|  | 329 | /* End of automatically generated file. */ | 
|  | 330 |  | 
|  | 331 | ''' | 
|  | 332 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 333 |  | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 334 | def generate_ssl_debug_helpers(output_directory, mbedtls_root): | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 335 | """ | 
|  | 336 | Generate functions of debug helps | 
|  | 337 | """ | 
| Jerry Yu | 0cb2cf6 | 2021-12-10 14:21:27 +0800 | [diff] [blame] | 338 | mbedtls_root = os.path.abspath(mbedtls_root or build_tree.guess_mbedtls_root()) | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 339 | with open(os.path.join(mbedtls_root, 'include/mbedtls/ssl.h')) as f: | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 340 | source_code = remove_c_comments(f.read()) | 
|  | 341 |  | 
|  | 342 | definitions = dict() | 
| Jerry Yu | bfcfe74 | 2022-02-22 16:41:39 +0800 | [diff] [blame] | 343 | for start, instance in preprocess_c_source_code(source_code, | 
|  | 344 | EnumDefinition, | 
|  | 345 | SignatureAlgorithmDefinition): | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 346 | if start in definitions: | 
|  | 347 | continue | 
|  | 348 | if isinstance(instance, EnumDefinition): | 
| David Horstmann | 3be1271 | 2021-12-16 10:56:26 +0000 | [diff] [blame] | 349 | definition = instance.generate_translation_function() | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 350 | else: | 
|  | 351 | definition = instance | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 352 | definitions[start] = definition | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 353 |  | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 354 | function_definitions = [str(v) for _, v in sorted(definitions.items())] | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 355 | if output_directory == sys.stdout: | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 356 | sys.stdout.write(OUTPUT_C_TEMPLATE.format( | 
|  | 357 | functions='\n'.join(function_definitions))) | 
|  | 358 | else: | 
|  | 359 | with open(os.path.join(output_directory, 'ssl_debug_helpers_generated.c'), 'w') as f: | 
|  | 360 | f.write(OUTPUT_C_TEMPLATE.format( | 
|  | 361 | functions='\n'.join(function_definitions))) | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 362 |  | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 363 |  | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 364 | def main(): | 
|  | 365 | """ | 
|  | 366 | Command line entry | 
|  | 367 | """ | 
|  | 368 | parser = argparse.ArgumentParser() | 
| Jerry Yu | 0cb2cf6 | 2021-12-10 14:21:27 +0800 | [diff] [blame] | 369 | parser.add_argument('--mbedtls-root', nargs='?', default=None, | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 370 | help='root directory of mbedtls source code') | 
|  | 371 | parser.add_argument('output_directory', nargs='?', | 
| Jerry Yu | 9817e3e | 2021-12-02 14:32:58 +0800 | [diff] [blame] | 372 | default='library', help='source/header files location') | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 373 |  | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 374 | args = parser.parse_args() | 
|  | 375 |  | 
|  | 376 | generate_ssl_debug_helpers(args.output_directory, args.mbedtls_root) | 
|  | 377 | return 0 | 
| Jerry Yu | e78ee99 | 2021-09-22 15:42:14 +0800 | [diff] [blame] | 378 |  | 
|  | 379 |  | 
|  | 380 | if __name__ == '__main__': | 
| Jerry Yu | e6369b0 | 2021-12-02 13:51:26 +0800 | [diff] [blame] | 381 | sys.exit(main()) |