Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Gabor Mezei | 9f2b817 | 2024-08-06 12:02:18 +0200 | [diff] [blame] | 3 | """Mbed TLS and PSA configuration file manipulation library and tool |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 4 | |
Fredrik Hesse | cc207bc | 2021-09-28 21:06:08 +0200 | [diff] [blame] | 5 | Basic usage, to read the Mbed TLS configuration: |
Gabor Mezei | 9f2b817 | 2024-08-06 12:02:18 +0200 | [diff] [blame] | 6 | config = CombinedConfigFile() |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 7 | if 'MBEDTLS_RSA_C' in config: print('RSA is enabled') |
| 8 | """ |
| 9 | |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 10 | ## Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 11 | ## SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 12 | ## |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 13 | |
Gilles Peskine | 208e4ec | 2019-07-29 23:43:20 +0200 | [diff] [blame] | 14 | import os |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 15 | import re |
| 16 | |
Gabor Mezei | e7742b3 | 2024-06-26 18:04:09 +0200 | [diff] [blame] | 17 | from abc import ABCMeta |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 18 | |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 19 | class Setting: |
Gabor Mezei | 9f2b817 | 2024-08-06 12:02:18 +0200 | [diff] [blame] | 20 | """Representation of one Mbed TLS mbedtls_config.h pr PSA crypto_config.h setting. |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 21 | |
| 22 | Fields: |
| 23 | * name: the symbol name ('MBEDTLS_xxx'). |
| 24 | * value: the value of the macro. The empty string for a plain #define |
| 25 | with no value. |
| 26 | * active: True if name is defined, False if a #define for name is |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 27 | present in mbedtls_config.h but commented out. |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 28 | * section: the name of the section that contains this symbol. |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 29 | * configfile: the file the settings is defined |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 30 | """ |
Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 31 | # pylint: disable=too-few-public-methods, too-many-arguments |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 32 | def __init__(self, configfile, active, name, value='', section=None): |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 33 | self.active = active |
| 34 | self.name = name |
| 35 | self.value = value |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 36 | self.section = section |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 37 | self.configfile = configfile |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 38 | |
| 39 | class Config: |
Gabor Mezei | 9f2b817 | 2024-08-06 12:02:18 +0200 | [diff] [blame] | 40 | """Representation of the Mbed TLS and PSA configuration. |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 41 | |
| 42 | In the documentation of this class, a symbol is said to be *active* |
| 43 | if there is a #define for it that is not commented out, and *known* |
| 44 | if there is a #define for it whether commented out or not. |
| 45 | |
| 46 | This class supports the following protocols: |
Gilles Peskine | c190c90 | 2019-08-01 23:31:05 +0200 | [diff] [blame] | 47 | * `name in config` is `True` if the symbol `name` is active, `False` |
| 48 | otherwise (whether `name` is inactive or not known). |
| 49 | * `config[name]` is the value of the macro `name`. If `name` is inactive, |
| 50 | raise `KeyError` (even if `name` is known). |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 51 | * `config[name] = value` sets the value associated to `name`. `name` |
| 52 | must be known, but does not need to be set. This does not cause |
| 53 | name to become set. |
| 54 | """ |
| 55 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 56 | def __init__(self): |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 57 | self.settings = {} |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 58 | self.configfiles = [] |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 59 | |
| 60 | def __contains__(self, name): |
| 61 | """True if the given symbol is active (i.e. set). |
| 62 | |
| 63 | False if the given symbol is not set, even if a definition |
| 64 | is present but commented out. |
| 65 | """ |
| 66 | return name in self.settings and self.settings[name].active |
| 67 | |
| 68 | def all(self, *names): |
| 69 | """True if all the elements of names are active (i.e. set).""" |
Gabor Mezei | daf807f | 2024-08-14 11:33:46 +0200 | [diff] [blame] | 70 | return all(name in self for name in names) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 71 | |
| 72 | def any(self, *names): |
| 73 | """True if at least one symbol in names are active (i.e. set).""" |
Gabor Mezei | daf807f | 2024-08-14 11:33:46 +0200 | [diff] [blame] | 74 | return any(name in self for name in names) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 75 | |
| 76 | def known(self, name): |
| 77 | """True if a #define for name is present, whether it's commented out or not.""" |
| 78 | return name in self.settings |
| 79 | |
| 80 | def __getitem__(self, name): |
| 81 | """Get the value of name, i.e. what the preprocessor symbol expands to. |
| 82 | |
| 83 | If name is not known, raise KeyError. name does not need to be active. |
| 84 | """ |
| 85 | return self.settings[name].value |
| 86 | |
| 87 | def get(self, name, default=None): |
| 88 | """Get the value of name. If name is inactive (not set), return default. |
| 89 | |
| 90 | If a #define for name is present and not commented out, return |
| 91 | its expansion, even if this is the empty string. |
| 92 | |
| 93 | If a #define for name is present but commented out, return default. |
| 94 | """ |
| 95 | if name in self.settings: |
| 96 | return self.settings[name].value |
| 97 | else: |
| 98 | return default |
| 99 | |
| 100 | def __setitem__(self, name, value): |
| 101 | """If name is known, set its value. |
| 102 | |
| 103 | If name is not known, raise KeyError. |
| 104 | """ |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 105 | setting = self.settings[name] |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 106 | if setting != value: |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 107 | setting.configfile.modified = True |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 108 | |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 109 | setting.value = value |
| 110 | |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 111 | def set(self, name, value=None): |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 112 | """Set name to the given value and make it active. |
| 113 | |
| 114 | If value is None and name is already known, don't change its value. |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 115 | If value is None and name is not known, set its value. |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 116 | """ |
| 117 | if name in self.settings: |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 118 | setting = self.settings[name] |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 119 | if setting.value != value or not setting.active: |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 120 | setting.configfile.modified = True |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 121 | if value is not None: |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 122 | setting.value = value |
| 123 | setting.active = True |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 124 | else: |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 125 | configfile = self._get_configfile(name) |
| 126 | self.settings[name] = Setting(configfile, True, name, value=value) |
| 127 | configfile.modified = True |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 128 | |
| 129 | def unset(self, name): |
| 130 | """Make name unset (inactive). |
| 131 | |
Gilles Peskine | 55cc4db | 2019-08-01 23:13:23 +0200 | [diff] [blame] | 132 | name remains known if it was known before. |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 133 | """ |
Gilles Peskine | 55cc4db | 2019-08-01 23:13:23 +0200 | [diff] [blame] | 134 | if name not in self.settings: |
| 135 | return |
Gabor Mezei | c5ff33c | 2024-06-28 17:46:44 +0200 | [diff] [blame] | 136 | |
| 137 | setting = self.settings[name] |
| 138 | # Check if modifying the config file |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 139 | if setting.active: |
Gabor Mezei | c5ff33c | 2024-06-28 17:46:44 +0200 | [diff] [blame] | 140 | setting.configfile.modified = True |
| 141 | |
| 142 | setting.active = False |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 143 | |
| 144 | def adapt(self, adapter): |
| 145 | """Run adapter on each known symbol and (de)activate it accordingly. |
| 146 | |
| 147 | `adapter` must be a function that returns a boolean. It is called as |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 148 | `adapter(name, active, section)` for each setting, where `active` is |
| 149 | `True` if `name` is set and `False` if `name` is known but unset, |
| 150 | and `section` is the name of the section containing `name`. If |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 151 | `adapter` returns `True`, then set `name` (i.e. make it active), |
| 152 | otherwise unset `name` (i.e. make it known but inactive). |
| 153 | """ |
| 154 | for setting in self.settings.values(): |
Gabor Mezei | c5ff33c | 2024-06-28 17:46:44 +0200 | [diff] [blame] | 155 | is_active = setting.active |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 156 | setting.active = adapter(setting.name, setting.active, |
| 157 | setting.section) |
Gabor Mezei | c5ff33c | 2024-06-28 17:46:44 +0200 | [diff] [blame] | 158 | # Check if modifying the config file |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 159 | if setting.active != is_active: |
Gabor Mezei | c5ff33c | 2024-06-28 17:46:44 +0200 | [diff] [blame] | 160 | setting.configfile.modified = True |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 161 | |
Gilles Peskine | 8e90cf4 | 2021-05-27 22:12:57 +0200 | [diff] [blame] | 162 | def change_matching(self, regexs, enable): |
| 163 | """Change all symbols matching one of the regexs to the desired state.""" |
| 164 | if not regexs: |
| 165 | return |
| 166 | regex = re.compile('|'.join(regexs)) |
| 167 | for setting in self.settings.values(): |
| 168 | if regex.search(setting.name): |
Gabor Mezei | c5ff33c | 2024-06-28 17:46:44 +0200 | [diff] [blame] | 169 | # Check if modifying the config file |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 170 | if setting.active != enable: |
Gabor Mezei | c5ff33c | 2024-06-28 17:46:44 +0200 | [diff] [blame] | 171 | setting.configfile.modified = True |
Gilles Peskine | 8e90cf4 | 2021-05-27 22:12:57 +0200 | [diff] [blame] | 172 | setting.active = enable |
| 173 | |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 174 | def _get_configfile(self, name=None): |
| 175 | """Find a config for a setting name. |
| 176 | |
| 177 | If more then one configfile is used this function must be overridden. |
| 178 | """ |
| 179 | |
| 180 | if name and name in self.settings: |
| 181 | return self.get(name).configfile |
| 182 | return self.configfiles[0] |
| 183 | |
| 184 | def write(self, filename=None): |
| 185 | """Write the whole configuration to the file it was read from. |
| 186 | |
| 187 | If filename is specified, write to this file instead. |
| 188 | """ |
| 189 | |
| 190 | for configfile in self.configfiles: |
| 191 | configfile.write(self.settings, filename) |
| 192 | |
| 193 | def filename(self, name=None): |
| 194 | """Get the name of the config file.""" |
| 195 | |
| 196 | return self._get_configfile(name).filename |
| 197 | |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 198 | def is_full_section(section): |
Gabor Mezei | de6e192 | 2024-06-28 17:10:50 +0200 | [diff] [blame] | 199 | """Is this section affected by "config.py full" and friends? |
| 200 | |
| 201 | In a config file where the sections are not used the whole config file |
| 202 | is an empty section (with value None) and the whole file is affected. |
| 203 | """ |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 204 | return section is None or section.endswith('support') or section.endswith('modules') |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 205 | |
| 206 | def realfull_adapter(_name, active, section): |
Gilles Peskine | ba4162a | 2022-04-11 17:04:38 +0200 | [diff] [blame] | 207 | """Activate all symbols found in the global and boolean feature sections. |
| 208 | |
| 209 | This is intended for building the documentation, including the |
| 210 | documentation of settings that are activated by defining an optional |
| 211 | preprocessor macro. |
| 212 | |
| 213 | Do not activate definitions in the section containing symbols that are |
| 214 | supposed to be defined and documented in their own module. |
| 215 | """ |
| 216 | if section == 'Module configuration options': |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 217 | return active |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 218 | return True |
| 219 | |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 220 | PSA_UNSUPPORTED_FEATURE = frozenset([ |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 221 | 'PSA_WANT_ALG_CBC_MAC', |
| 222 | 'PSA_WANT_ALG_XTS', |
| 223 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_DERIVE', |
| 224 | 'PSA_WANT_KEY_TYPE_DH_KEY_PAIR_DERIVE' |
| 225 | ]) |
| 226 | |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 227 | PSA_DEPRECATED_FEATURE = frozenset([ |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 228 | 'PSA_WANT_KEY_TYPE_ECC_KEY_PAIR', |
| 229 | 'PSA_WANT_KEY_TYPE_RSA_KEY_PAIR' |
| 230 | ]) |
| 231 | |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 232 | PSA_UNSTABLE_FEATURE = frozenset([ |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 233 | 'PSA_WANT_ECC_SECP_K1_224' |
| 234 | ]) |
| 235 | |
Gabor Mezei | 9b0f9e7 | 2024-06-26 18:08:17 +0200 | [diff] [blame] | 236 | EXCLUDE_FROM_CRYPTO = PSA_UNSUPPORTED_FEATURE | \ |
| 237 | PSA_DEPRECATED_FEATURE | \ |
| 238 | PSA_UNSTABLE_FEATURE |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 239 | |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 240 | # The goal of the full configuration is to have everything that can be tested |
| 241 | # together. This includes deprecated or insecure options. It excludes: |
| 242 | # * Options that require additional build dependencies or unusual hardware. |
| 243 | # * Options that make testing less effective. |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 244 | # * Options that are incompatible with other options, or more generally that |
| 245 | # interact with other parts of the code in such a way that a bulk enabling |
| 246 | # is not a good way to test them. |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 247 | # * Options that remove features. |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 248 | EXCLUDE_FROM_FULL = frozenset([ |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 249 | #pylint: disable=line-too-long |
Yanray Wang | a870467 | 2023-04-20 17:16:48 +0800 | [diff] [blame] | 250 | 'MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH', # interacts with CTR_DRBG_128_BIT_KEY |
Gilles Peskine | a8861e0 | 2023-09-05 20:20:51 +0200 | [diff] [blame] | 251 | 'MBEDTLS_AES_USE_HARDWARE_ONLY', # hardware dependency |
Yanray Wang | 42be1ba | 2023-11-23 14:28:47 +0800 | [diff] [blame] | 252 | 'MBEDTLS_BLOCK_CIPHER_NO_DECRYPT', # incompatible with ECB in PSA, CBC/XTS/NIST_KW/DES |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 253 | 'MBEDTLS_CTR_DRBG_USE_128_BIT_KEY', # interacts with ENTROPY_FORCE_SHA256 |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 254 | 'MBEDTLS_DEPRECATED_REMOVED', # conflicts with deprecated options |
Gilles Peskine | 90581ee | 2020-04-12 14:02:47 +0200 | [diff] [blame] | 255 | 'MBEDTLS_DEPRECATED_WARNING', # conflicts with deprecated options |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 256 | 'MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED', # influences the use of ECDH in TLS |
Janos Follath | 5b7c38f | 2023-08-01 08:51:12 +0100 | [diff] [blame] | 257 | 'MBEDTLS_ECP_WITH_MPI_UINT', # disables the default ECP and is experimental |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 258 | 'MBEDTLS_ENTROPY_FORCE_SHA256', # interacts with CTR_DRBG_128_BIT_KEY |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 259 | 'MBEDTLS_HAVE_SSE2', # hardware dependency |
| 260 | 'MBEDTLS_MEMORY_BACKTRACE', # depends on MEMORY_BUFFER_ALLOC_C |
| 261 | 'MBEDTLS_MEMORY_BUFFER_ALLOC_C', # makes sanitizers (e.g. ASan) less effective |
| 262 | 'MBEDTLS_MEMORY_DEBUG', # depends on MEMORY_BUFFER_ALLOC_C |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 263 | 'MBEDTLS_NO_64BIT_MULTIPLICATION', # influences anything that uses bignum |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 264 | 'MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES', # removes a feature |
| 265 | 'MBEDTLS_NO_PLATFORM_ENTROPY', # removes a feature |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 266 | 'MBEDTLS_NO_UDBL_DIVISION', # influences anything that uses bignum |
Gilles Peskine | efaee9a | 2023-09-20 20:49:47 +0200 | [diff] [blame] | 267 | 'MBEDTLS_PSA_P256M_DRIVER_ENABLED', # influences SECP256R1 KeyGen/ECDH/ECDSA |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 268 | 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS', # removes a feature |
David Horstmann | 6f8c95b | 2024-03-14 14:52:45 +0000 | [diff] [blame] | 269 | 'MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS', # removes a feature |
Gilles Peskine | f08b3f8 | 2020-11-13 17:36:48 +0100 | [diff] [blame] | 270 | 'MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG', # behavior change + build dependency |
Ronald Cron | c3623db | 2020-10-29 10:51:32 +0100 | [diff] [blame] | 271 | 'MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER', # incompatible with USE_PSA_CRYPTO |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 272 | 'MBEDTLS_PSA_CRYPTO_SPM', # platform dependency (PSA SPM) |
Gilles Peskine | a08def9 | 2023-04-28 21:01:49 +0200 | [diff] [blame] | 273 | 'MBEDTLS_PSA_INJECT_ENTROPY', # conflicts with platform entropy sources |
Gilles Peskine | c9d0433 | 2020-04-16 20:50:17 +0200 | [diff] [blame] | 274 | 'MBEDTLS_RSA_NO_CRT', # influences the use of RSA in X.509 and TLS |
Tom Cosgrove | 87fbfb5 | 2022-03-15 10:51:52 +0000 | [diff] [blame] | 275 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT |
Dave Rodgman | 9be3cf0 | 2023-10-11 14:47:55 +0100 | [diff] [blame] | 276 | 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_ONLY', # interacts with *_USE_ARMV8_A_CRYPTO_IF_PRESENT |
Tom Cosgrove | 87fbfb5 | 2022-03-15 10:51:52 +0000 | [diff] [blame] | 277 | 'MBEDTLS_SHA512_USE_A64_CRYPTO_ONLY', # interacts with *_USE_A64_CRYPTO_IF_PRESENT |
Dave Rodgman | 7cb635a | 2023-10-12 16:14:51 +0100 | [diff] [blame] | 278 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # setting *_USE_ARMV8_A_CRYPTO is sufficient |
Manuel Pégourié-Gonnard | 6240def | 2020-07-10 09:35:54 +0200 | [diff] [blame] | 279 | 'MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN', # build dependency (clang+memsan) |
Manuel Pégourié-Gonnard | 73afa37 | 2020-08-19 10:27:38 +0200 | [diff] [blame] | 280 | 'MBEDTLS_TEST_CONSTANT_FLOW_VALGRIND', # build dependency (valgrind headers) |
Hanno Becker | e111356 | 2019-06-12 13:59:14 +0100 | [diff] [blame] | 281 | 'MBEDTLS_X509_REMOVE_INFO', # removes a feature |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 282 | *PSA_UNSUPPORTED_FEATURE, |
| 283 | *PSA_DEPRECATED_FEATURE, |
| 284 | *PSA_UNSTABLE_FEATURE |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 285 | ]) |
| 286 | |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 287 | def is_seamless_alt(name): |
Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 288 | """Whether the xxx_ALT symbol should be included in the full configuration. |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 289 | |
Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 290 | Include alternative implementations of platform functions, which are |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 291 | configurable function pointers that default to the built-in function. |
| 292 | This way we test that the function pointers exist and build correctly |
| 293 | without changing the behavior, and tests can verify that the function |
| 294 | pointers are used by modifying those pointers. |
| 295 | |
| 296 | Exclude alternative implementations of library functions since they require |
| 297 | an implementation of the relevant functions and an xxx_alt.h header. |
| 298 | """ |
Gilles Peskine | a8861e0 | 2023-09-05 20:20:51 +0200 | [diff] [blame] | 299 | if name in ( |
| 300 | 'MBEDTLS_PLATFORM_GMTIME_R_ALT', |
| 301 | 'MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT', |
| 302 | 'MBEDTLS_PLATFORM_MS_TIME_ALT', |
| 303 | 'MBEDTLS_PLATFORM_ZEROIZE_ALT', |
| 304 | ): |
Gilles Peskine | c34faba | 2020-04-20 15:44:14 +0200 | [diff] [blame] | 305 | # Similar to non-platform xxx_ALT, requires platform_alt.h |
| 306 | return False |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 307 | return name.startswith('MBEDTLS_PLATFORM_') |
| 308 | |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 309 | def include_in_full(name): |
| 310 | """Rules for symbols in the "full" configuration.""" |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 311 | if name in EXCLUDE_FROM_FULL: |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 312 | return False |
| 313 | if name.endswith('_ALT'): |
Gilles Peskine | 32e889d | 2020-04-12 23:43:28 +0200 | [diff] [blame] | 314 | return is_seamless_alt(name) |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 315 | return True |
| 316 | |
| 317 | def full_adapter(name, active, section): |
| 318 | """Config adapter for "full".""" |
| 319 | if not is_full_section(section): |
| 320 | return active |
| 321 | return include_in_full(name) |
| 322 | |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 323 | # The baremetal configuration excludes options that require a library or |
| 324 | # operating system feature that is typically not present on bare metal |
| 325 | # systems. Features that are excluded from "full" won't be in "baremetal" |
| 326 | # either (unless explicitly turned on in baremetal_adapter) so they don't |
| 327 | # need to be repeated here. |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 328 | EXCLUDE_FROM_BAREMETAL = frozenset([ |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 329 | #pylint: disable=line-too-long |
Gilles Peskine | 98f8f95 | 2020-04-20 15:38:39 +0200 | [diff] [blame] | 330 | 'MBEDTLS_ENTROPY_NV_SEED', # requires a filesystem and FS_IO or alternate NV seed hooks |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 331 | 'MBEDTLS_FS_IO', # requires a filesystem |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 332 | 'MBEDTLS_HAVE_TIME', # requires a clock |
| 333 | 'MBEDTLS_HAVE_TIME_DATE', # requires a clock |
| 334 | 'MBEDTLS_NET_C', # requires POSIX-like networking |
| 335 | 'MBEDTLS_PLATFORM_FPRINTF_ALT', # requires FILE* from stdio.h |
Gilles Peskine | 98f8f95 | 2020-04-20 15:38:39 +0200 | [diff] [blame] | 336 | 'MBEDTLS_PLATFORM_NV_SEED_ALT', # requires a filesystem and ENTROPY_NV_SEED |
| 337 | 'MBEDTLS_PLATFORM_TIME_ALT', # requires a clock and HAVE_TIME |
| 338 | 'MBEDTLS_PSA_CRYPTO_SE_C', # requires a filesystem and PSA_CRYPTO_STORAGE_C |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 339 | 'MBEDTLS_PSA_CRYPTO_STORAGE_C', # requires a filesystem |
| 340 | 'MBEDTLS_PSA_ITS_FILE_C', # requires a filesystem |
| 341 | 'MBEDTLS_THREADING_C', # requires a threading interface |
| 342 | 'MBEDTLS_THREADING_PTHREAD', # requires pthread |
| 343 | 'MBEDTLS_TIMING_C', # requires a clock |
Dave Rodgman | 9be3cf0 | 2023-10-11 14:47:55 +0100 | [diff] [blame] | 344 | 'MBEDTLS_SHA256_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection |
Dave Rodgman | 5b89c55 | 2023-10-10 14:59:02 +0100 | [diff] [blame] | 345 | 'MBEDTLS_SHA256_USE_ARMV8_A_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection |
Dave Rodgman | be7915a | 2023-10-11 10:46:38 +0100 | [diff] [blame] | 346 | 'MBEDTLS_SHA512_USE_A64_CRYPTO_IF_PRESENT', # requires an OS for runtime-detection |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 347 | ]) |
| 348 | |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 349 | def keep_in_baremetal(name): |
| 350 | """Rules for symbols in the "baremetal" configuration.""" |
Gilles Peskine | bbaa2b7 | 2020-04-12 13:33:57 +0200 | [diff] [blame] | 351 | if name in EXCLUDE_FROM_BAREMETAL: |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 352 | return False |
| 353 | return True |
| 354 | |
| 355 | def baremetal_adapter(name, active, section): |
| 356 | """Config adapter for "baremetal".""" |
| 357 | if not is_full_section(section): |
| 358 | return active |
| 359 | if name == 'MBEDTLS_NO_PLATFORM_ENTROPY': |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 360 | # No OS-provided entropy source |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 361 | return True |
| 362 | return include_in_full(name) and keep_in_baremetal(name) |
| 363 | |
Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 364 | # This set contains options that are mostly for debugging or test purposes, |
| 365 | # and therefore should be excluded when doing code size measurements. |
| 366 | # Options that are their own module (such as MBEDTLS_ERROR_C) are not listed |
| 367 | # and therefore will be included when doing code size measurements. |
| 368 | EXCLUDE_FOR_SIZE = frozenset([ |
| 369 | 'MBEDTLS_DEBUG_C', # large code size increase in TLS |
| 370 | 'MBEDTLS_SELF_TEST', # increases the size of many modules |
| 371 | 'MBEDTLS_TEST_HOOKS', # only useful with the hosted test framework, increases code size |
| 372 | ]) |
| 373 | |
| 374 | def baremetal_size_adapter(name, active, section): |
| 375 | if name in EXCLUDE_FOR_SIZE: |
| 376 | return False |
| 377 | return baremetal_adapter(name, active, section) |
| 378 | |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 379 | def include_in_crypto(name): |
| 380 | """Rules for symbols in a crypto configuration.""" |
| 381 | if name.startswith('MBEDTLS_X509_') or \ |
| 382 | name.startswith('MBEDTLS_SSL_') or \ |
| 383 | name.startswith('MBEDTLS_KEY_EXCHANGE_'): |
| 384 | return False |
| 385 | if name in [ |
Gilles Peskine | cfffc28 | 2020-04-12 13:55:45 +0200 | [diff] [blame] | 386 | 'MBEDTLS_DEBUG_C', # part of libmbedtls |
| 387 | 'MBEDTLS_NET_C', # part of libmbedtls |
Nayna Jain | c9deb18 | 2020-11-16 19:03:12 +0000 | [diff] [blame] | 388 | 'MBEDTLS_PKCS7_C', # part of libmbedx509 |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 389 | ]: |
| 390 | return False |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 391 | if name in EXCLUDE_FROM_CRYPTO: |
| 392 | return False |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 393 | return True |
| 394 | |
| 395 | def crypto_adapter(adapter): |
| 396 | """Modify an adapter to disable non-crypto symbols. |
| 397 | |
| 398 | ``crypto_adapter(adapter)(name, active, section)`` is like |
| 399 | ``adapter(name, active, section)``, but unsets all X.509 and TLS symbols. |
| 400 | """ |
| 401 | def continuation(name, active, section): |
| 402 | if not include_in_crypto(name): |
| 403 | return False |
| 404 | if adapter is None: |
| 405 | return active |
| 406 | return adapter(name, active, section) |
| 407 | return continuation |
| 408 | |
Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 409 | DEPRECATED = frozenset([ |
| 410 | 'MBEDTLS_PSA_CRYPTO_SE_C', |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 411 | *PSA_DEPRECATED_FEATURE |
Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 412 | ]) |
Gilles Peskine | 30de2e8 | 2020-04-20 21:39:22 +0200 | [diff] [blame] | 413 | def no_deprecated_adapter(adapter): |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 414 | """Modify an adapter to disable deprecated symbols. |
| 415 | |
Gilles Peskine | 30de2e8 | 2020-04-20 21:39:22 +0200 | [diff] [blame] | 416 | ``no_deprecated_adapter(adapter)(name, active, section)`` is like |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 417 | ``adapter(name, active, section)``, but unsets all deprecated symbols |
| 418 | and sets ``MBEDTLS_DEPRECATED_REMOVED``. |
| 419 | """ |
| 420 | def continuation(name, active, section): |
| 421 | if name == 'MBEDTLS_DEPRECATED_REMOVED': |
| 422 | return True |
Gilles Peskine | ed5c21d | 2022-06-27 23:02:09 +0200 | [diff] [blame] | 423 | if name in DEPRECATED: |
| 424 | return False |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 425 | if adapter is None: |
| 426 | return active |
| 427 | return adapter(name, active, section) |
| 428 | return continuation |
| 429 | |
Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 430 | def no_platform_adapter(adapter): |
| 431 | """Modify an adapter to disable platform symbols. |
| 432 | |
| 433 | ``no_platform_adapter(adapter)(name, active, section)`` is like |
| 434 | ``adapter(name, active, section)``, but unsets all platform symbols other |
| 435 | ``than MBEDTLS_PLATFORM_C. |
| 436 | """ |
| 437 | def continuation(name, active, section): |
| 438 | # Allow MBEDTLS_PLATFORM_C but remove all other platform symbols. |
| 439 | if name.startswith('MBEDTLS_PLATFORM_') and name != 'MBEDTLS_PLATFORM_C': |
| 440 | return False |
| 441 | if adapter is None: |
| 442 | return active |
| 443 | return adapter(name, active, section) |
| 444 | return continuation |
| 445 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 446 | class ConfigFile(metaclass=ABCMeta): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 447 | """Representation of a configuration file.""" |
| 448 | |
Gabor Mezei | 93a6d1f | 2024-06-26 18:01:09 +0200 | [diff] [blame] | 449 | def __init__(self, default_path, name, filename=None): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 450 | """Check if the config file exists.""" |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 451 | if filename is None: |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 452 | for candidate in default_path: |
Gilles Peskine | ce674a9 | 2020-03-24 15:37:00 +0100 | [diff] [blame] | 453 | if os.path.lexists(candidate): |
| 454 | filename = candidate |
Gilles Peskine | 208e4ec | 2019-07-29 23:43:20 +0200 | [diff] [blame] | 455 | break |
Gilles Peskine | ce674a9 | 2020-03-24 15:37:00 +0100 | [diff] [blame] | 456 | else: |
Gabor Mezei | 8d72ac6 | 2024-06-28 17:18:37 +0200 | [diff] [blame] | 457 | raise FileNotFoundError(f'{name} configuration file not found: ' |
| 458 | f'{filename if filename else default_path}') |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 459 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 460 | self.filename = filename |
| 461 | self.templates = [] |
| 462 | self.current_section = None |
| 463 | self.inclusion_guard = None |
Gabor Mezei | 8a64d8e | 2024-06-10 15:23:43 +0200 | [diff] [blame] | 464 | self.modified = False |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 465 | |
| 466 | _define_line_regexp = (r'(?P<indentation>\s*)' + |
| 467 | r'(?P<commented_out>(//\s*)?)' + |
| 468 | r'(?P<define>#\s*define\s+)' + |
| 469 | r'(?P<name>\w+)' + |
| 470 | r'(?P<arguments>(?:\((?:\w|\s|,)*\))?)' + |
| 471 | r'(?P<separator>\s*)' + |
| 472 | r'(?P<value>.*)') |
Gilles Peskine | 9ba9c21 | 2024-05-23 15:03:43 +0200 | [diff] [blame] | 473 | _ifndef_line_regexp = r'#ifndef (?P<inclusion_guard>\w+)' |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 474 | _section_line_regexp = (r'\s*/?\*+\s*[\\@]name\s+SECTION:\s*' + |
| 475 | r'(?P<section>.*)[ */]*') |
| 476 | _config_line_regexp = re.compile(r'|'.join([_define_line_regexp, |
Gilles Peskine | 9ba9c21 | 2024-05-23 15:03:43 +0200 | [diff] [blame] | 477 | _ifndef_line_regexp, |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 478 | _section_line_regexp])) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 479 | def _parse_line(self, line): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 480 | """Parse a line in the config file, save the templates representing the lines |
| 481 | and return the corresponding setting element. |
| 482 | """ |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 483 | |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 484 | line = line.rstrip('\r\n') |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 485 | m = re.match(self._config_line_regexp, line) |
| 486 | if m is None: |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 487 | self.templates.append(line) |
| 488 | return None |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 489 | elif m.group('section'): |
| 490 | self.current_section = m.group('section') |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 491 | self.templates.append(line) |
| 492 | return None |
Gilles Peskine | 9ba9c21 | 2024-05-23 15:03:43 +0200 | [diff] [blame] | 493 | elif m.group('inclusion_guard') and self.inclusion_guard is None: |
| 494 | self.inclusion_guard = m.group('inclusion_guard') |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 495 | self.templates.append(line) |
| 496 | return None |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 497 | else: |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 498 | active = not m.group('commented_out') |
| 499 | name = m.group('name') |
| 500 | value = m.group('value') |
Gilles Peskine | 9ba9c21 | 2024-05-23 15:03:43 +0200 | [diff] [blame] | 501 | if name == self.inclusion_guard and value == '': |
| 502 | # The file double-inclusion guard is not an option. |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 503 | self.templates.append(line) |
| 504 | return None |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 505 | template = (name, |
| 506 | m.group('indentation'), |
| 507 | m.group('define') + name + |
| 508 | m.group('arguments') + m.group('separator')) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 509 | self.templates.append(template) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 510 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 511 | return (active, name, value, self.current_section) |
| 512 | |
| 513 | def parse_file(self): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 514 | """Parse the whole file and return the settings.""" |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 515 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 516 | with open(self.filename, 'r', encoding='utf-8') as file: |
| 517 | for line in file: |
| 518 | setting = self._parse_line(line) |
| 519 | if setting is not None: |
| 520 | yield setting |
| 521 | self.current_section = None |
| 522 | |
Gabor Mezei | e7742b3 | 2024-06-26 18:04:09 +0200 | [diff] [blame] | 523 | #pylint: disable=no-self-use |
| 524 | def _format_template(self, setting, indent, middle): |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 525 | """Build a line for the config file for the given setting. |
Gabor Mezei | e7742b3 | 2024-06-26 18:04:09 +0200 | [diff] [blame] | 526 | |
| 527 | The line has the form "<indent>#define <name> <value>" |
| 528 | where <middle> is "#define <name> ". |
| 529 | """ |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 530 | |
Gabor Mezei | e7742b3 | 2024-06-26 18:04:09 +0200 | [diff] [blame] | 531 | value = setting.value |
| 532 | if value is None: |
| 533 | value = '' |
| 534 | # Normally the whitespace to separate the symbol name from the |
| 535 | # value is part of middle, and there's no whitespace for a symbol |
| 536 | # with no value. But if a symbol has been changed from having a |
| 537 | # value to not having one, the whitespace is wrong, so fix it. |
| 538 | if value: |
| 539 | if middle[-1] not in '\t ': |
| 540 | middle += ' ' |
| 541 | else: |
| 542 | middle = middle.rstrip() |
| 543 | return ''.join([indent, |
| 544 | '' if setting.active else '//', |
| 545 | middle, |
| 546 | value]).rstrip() |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 547 | |
| 548 | def write_to_stream(self, settings, output): |
| 549 | """Write the whole configuration to output.""" |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 550 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 551 | for template in self.templates: |
| 552 | if isinstance(template, str): |
| 553 | line = template |
| 554 | else: |
Gabor Mezei | e7742b3 | 2024-06-26 18:04:09 +0200 | [diff] [blame] | 555 | name, indent, middle = template |
| 556 | line = self._format_template(settings[name], indent, middle) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 557 | output.write(line + '\n') |
| 558 | |
| 559 | def write(self, settings, filename=None): |
| 560 | """Write the whole configuration to the file it was read from. |
| 561 | |
| 562 | If filename is specified, write to this file instead. |
| 563 | """ |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 564 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 565 | if filename is None: |
| 566 | filename = self.filename |
Gabor Mezei | 8a64d8e | 2024-06-10 15:23:43 +0200 | [diff] [blame] | 567 | |
| 568 | # Not modified so no need to write to the file |
| 569 | if not self.modified and filename == self.filename: |
| 570 | return |
| 571 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 572 | with open(filename, 'w', encoding='utf-8') as output: |
| 573 | self.write_to_stream(settings, output) |
| 574 | |
Gabor Mezei | f77722d | 2024-06-28 16:49:33 +0200 | [diff] [blame] | 575 | class MbedTLSConfigFile(ConfigFile): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 576 | """Representation of an MbedTLS configuration file.""" |
| 577 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 578 | _path_in_tree = 'include/mbedtls/mbedtls_config.h' |
| 579 | default_path = [_path_in_tree, |
| 580 | os.path.join(os.path.dirname(__file__), |
| 581 | os.pardir, |
| 582 | _path_in_tree), |
| 583 | os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), |
| 584 | _path_in_tree)] |
| 585 | |
| 586 | def __init__(self, filename=None): |
Gabor Mezei | 93a6d1f | 2024-06-26 18:01:09 +0200 | [diff] [blame] | 587 | super().__init__(self.default_path, 'Mbed TLS', filename) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 588 | self.current_section = 'header' |
| 589 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 590 | class CryptoConfigFile(ConfigFile): |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 591 | """Representation of a Crypto configuration file.""" |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 592 | |
Gabor Mezei | 3de6586 | 2024-07-08 16:14:10 +0200 | [diff] [blame] | 593 | # Temporary, while Mbed TLS does not just rely on the TF-PSA-Crypto |
| 594 | # build system to build its crypto library. When it does, the |
| 595 | # condition can just be removed. |
Gabor Mezei | 776ee90 | 2024-09-09 17:00:50 +0200 | [diff] [blame^] | 596 | _path_in_tree = ('include/psa/crypto_config.h' |
| 597 | if not os.path.isdir(os.path.join(os.path.dirname(__file__), |
| 598 | os.pardir, |
| 599 | 'tf-psa-crypto')) else |
| 600 | 'tf-psa-crypto/include/psa/crypto_config.h') |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 601 | default_path = [_path_in_tree, |
| 602 | os.path.join(os.path.dirname(__file__), |
| 603 | os.pardir, |
| 604 | _path_in_tree), |
| 605 | os.path.join(os.path.dirname(os.path.abspath(os.path.dirname(__file__))), |
| 606 | _path_in_tree)] |
| 607 | |
| 608 | def __init__(self, filename=None): |
Gabor Mezei | 93a6d1f | 2024-06-26 18:01:09 +0200 | [diff] [blame] | 609 | super().__init__(self.default_path, 'Crypto', filename) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 610 | |
Gabor Mezei | f77722d | 2024-06-28 16:49:33 +0200 | [diff] [blame] | 611 | class MbedTLSConfig(Config): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 612 | """Representation of the Mbed TLS configuration. |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 613 | |
| 614 | See the documentation of the `Config` class for methods to query |
| 615 | and modify the configuration. |
| 616 | """ |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 617 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 618 | def __init__(self, filename=None): |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 619 | """Read the Mbed TLS configuration file.""" |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 620 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 621 | super().__init__() |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 622 | configfile = MbedTLSConfigFile(filename) |
| 623 | self.configfiles.append(configfile) |
| 624 | self.settings.update({name: Setting(configfile, active, name, value, section) |
Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 625 | for (active, name, value, section) |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 626 | in configfile.parse_file()}) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 627 | |
| 628 | def set(self, name, value=None): |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 629 | """Set name to the given value and make it active.""" |
| 630 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 631 | if name not in self.settings: |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 632 | self._get_configfile().templates.append((name, '', '#define ' + name + ' ')) |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 633 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 634 | super().set(name, value) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 635 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 636 | class CryptoConfig(Config): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 637 | """Representation of the PSA crypto configuration. |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 638 | |
| 639 | See the documentation of the `Config` class for methods to query |
| 640 | and modify the configuration. |
| 641 | """ |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 642 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 643 | def __init__(self, filename=None): |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 644 | """Read the PSA crypto configuration file.""" |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 645 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 646 | super().__init__() |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 647 | configfile = CryptoConfigFile(filename) |
| 648 | self.configfiles.append(configfile) |
| 649 | self.settings.update({name: Setting(configfile, active, name, value, section) |
Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 650 | for (active, name, value, section) |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 651 | in configfile.parse_file()}) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 652 | |
Gabor Mezei | d723b51 | 2024-06-07 15:31:52 +0200 | [diff] [blame] | 653 | def set(self, name, value='1'): |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 654 | """Set name to the given value and make it active.""" |
| 655 | |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 656 | if name in PSA_UNSUPPORTED_FEATURE: |
Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 657 | raise ValueError(f'Feature is unsupported: \'{name}\'') |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 658 | if name in PSA_UNSTABLE_FEATURE: |
Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 659 | raise ValueError(f'Feature is unstable: \'{name}\'') |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 660 | |
| 661 | if name not in self.settings: |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 662 | self._get_configfile().templates.append((name, '', '#define ' + name + ' ')) |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 663 | |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 664 | super().set(name, value) |
| 665 | |
Gabor Mezei | 33dd293 | 2024-06-28 17:51:58 +0200 | [diff] [blame] | 666 | class CombinedConfig(Config): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 667 | """Representation of MbedTLS and PSA crypto configuration |
| 668 | |
| 669 | See the documentation of the `Config` class for methods to query |
| 670 | and modify the configuration. |
| 671 | """ |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 672 | |
Gabor Mezei | 3e2a550 | 2024-06-28 17:27:19 +0200 | [diff] [blame] | 673 | def __init__(self, *configs): |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 674 | super().__init__() |
Gabor Mezei | 3e2a550 | 2024-06-28 17:27:19 +0200 | [diff] [blame] | 675 | for config in configs: |
| 676 | if isinstance(config, MbedTLSConfigFile): |
| 677 | self.mbedtls_configfile = config |
| 678 | elif isinstance(config, CryptoConfigFile): |
| 679 | self.crypto_configfile = config |
| 680 | else: |
| 681 | raise ValueError(f'Invalid configfile: {config}') |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 682 | self.configfiles.append(config) |
Gabor Mezei | 3e2a550 | 2024-06-28 17:27:19 +0200 | [diff] [blame] | 683 | |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 684 | self.settings.update({name: Setting(configfile, active, name, value, section) |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 685 | for configfile in [self.mbedtls_configfile, self.crypto_configfile] |
| 686 | for (active, name, value, section) in configfile.parse_file()}) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 687 | |
| 688 | _crypto_regexp = re.compile(r'$PSA_.*') |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 689 | def _get_configfile(self, name=None): |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 690 | """Find a config type for a setting name""" |
| 691 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 692 | if name in self.settings: |
| 693 | return self.settings[name].configfile |
| 694 | elif re.match(self._crypto_regexp, name): |
| 695 | return self.crypto_configfile |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 696 | else: |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 697 | return self.mbedtls_configfile |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 698 | |
| 699 | def set(self, name, value=None): |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 700 | """Set name to the given value and make it active.""" |
| 701 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 702 | configfile = self._get_configfile(name) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 703 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 704 | if configfile == self.crypto_configfile: |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 705 | if name in PSA_UNSUPPORTED_FEATURE: |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 706 | raise ValueError(f'Feature is unsupported: \'{name}\'') |
Gabor Mezei | 542fd38 | 2024-06-10 14:07:42 +0200 | [diff] [blame] | 707 | if name in PSA_UNSTABLE_FEATURE: |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 708 | raise ValueError(f'Feature is unstable: \'{name}\'') |
| 709 | |
Gabor Mezei | d723b51 | 2024-06-07 15:31:52 +0200 | [diff] [blame] | 710 | # The default value in the crypto config is '1' |
| 711 | if not value: |
| 712 | value = '1' |
| 713 | |
Gabor Mezei | c659c1b | 2024-08-06 17:37:55 +0200 | [diff] [blame] | 714 | if name not in self.settings: |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 715 | configfile.templates.append((name, '', '#define ' + name + ' ')) |
| 716 | |
Gabor Mezei | d53080d | 2024-08-27 14:06:54 +0200 | [diff] [blame] | 717 | super().set(name, value) |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 718 | |
Gabor Mezei | daf807f | 2024-08-14 11:33:46 +0200 | [diff] [blame] | 719 | #pylint: disable=arguments-differ |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 720 | def write(self, mbedtls_file=None, crypto_file=None): |
Gabor Mezei | 62a9bd0 | 2024-06-07 13:44:40 +0200 | [diff] [blame] | 721 | """Write the whole configuration to the file it was read from. |
| 722 | |
| 723 | If mbedtls_file or crypto_file is specified, write the specific configuration |
| 724 | to the corresponding file instead. |
| 725 | """ |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 726 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 727 | self.mbedtls_configfile.write(self.settings, mbedtls_file) |
| 728 | self.crypto_configfile.write(self.settings, crypto_file) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 729 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 730 | def filename(self, name=None): |
Gabor Mezei | 4706fe7 | 2024-07-08 17:00:55 +0200 | [diff] [blame] | 731 | """Get the names of the config files. |
| 732 | |
| 733 | If 'name' is specified return the name of the config file where it is defined. |
| 734 | """ |
| 735 | |
Gabor Mezei | ee521b6 | 2024-06-07 13:50:41 +0200 | [diff] [blame] | 736 | if not name: |
| 737 | return [config.filename for config in [self.mbedtls_configfile, self.crypto_configfile]] |
| 738 | |
| 739 | return self._get_configfile(name).filename |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 740 | |
| 741 | if __name__ == '__main__': |
Gabor Mezei | 92065ed | 2024-06-07 13:47:59 +0200 | [diff] [blame] | 742 | #pylint: disable=too-many-statements |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 743 | def main(): |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 744 | """Command line mbedtls_config.h manipulation tool.""" |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 745 | parser = argparse.ArgumentParser(description=""" |
Fredrik Hesse | 0ec8a90 | 2021-10-04 22:13:51 +0200 | [diff] [blame] | 746 | Mbed TLS configuration file manipulation tool. |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 747 | """) |
| 748 | parser.add_argument('--file', '-f', |
| 749 | help="""File to read (and modify if requested). |
| 750 | Default: {}. |
Gabor Mezei | f77722d | 2024-06-28 16:49:33 +0200 | [diff] [blame] | 751 | """.format(MbedTLSConfigFile.default_path)) |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 752 | parser.add_argument('--cryptofile', '-c', |
| 753 | help="""Crypto file to read (and modify if requested). |
| 754 | Default: {}. |
| 755 | """.format(CryptoConfigFile.default_path)) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 756 | parser.add_argument('--force', '-o', |
Gilles Peskine | 435ce22 | 2019-08-01 23:13:47 +0200 | [diff] [blame] | 757 | action='store_true', |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 758 | help="""For the set command, if SYMBOL is not |
| 759 | present, add a definition for it.""") |
Gilles Peskine | c190c90 | 2019-08-01 23:31:05 +0200 | [diff] [blame] | 760 | parser.add_argument('--write', '-w', metavar='FILE', |
Gilles Peskine | 40f103c | 2019-07-27 23:44:01 +0200 | [diff] [blame] | 761 | help="""File to write to instead of the input file.""") |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 762 | subparsers = parser.add_subparsers(dest='command', |
| 763 | title='Commands') |
| 764 | parser_get = subparsers.add_parser('get', |
| 765 | help="""Find the value of SYMBOL |
| 766 | and print it. Exit with |
| 767 | status 0 if a #define for SYMBOL is |
| 768 | found, 1 otherwise. |
| 769 | """) |
| 770 | parser_get.add_argument('symbol', metavar='SYMBOL') |
| 771 | parser_set = subparsers.add_parser('set', |
| 772 | help="""Set SYMBOL to VALUE. |
| 773 | If VALUE is omitted, just uncomment |
| 774 | the #define for SYMBOL. |
| 775 | Error out of a line defining |
| 776 | SYMBOL (commented or not) is not |
| 777 | found, unless --force is passed. |
| 778 | """) |
| 779 | parser_set.add_argument('symbol', metavar='SYMBOL') |
Gilles Peskine | 0c7fcd2 | 2019-08-01 23:14:00 +0200 | [diff] [blame] | 780 | parser_set.add_argument('value', metavar='VALUE', nargs='?', |
| 781 | default='') |
Gilles Peskine | 8e90cf4 | 2021-05-27 22:12:57 +0200 | [diff] [blame] | 782 | parser_set_all = subparsers.add_parser('set-all', |
| 783 | help="""Uncomment all #define |
| 784 | whose name contains a match for |
| 785 | REGEX.""") |
| 786 | parser_set_all.add_argument('regexs', metavar='REGEX', nargs='*') |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 787 | parser_unset = subparsers.add_parser('unset', |
| 788 | help="""Comment out the #define |
| 789 | for SYMBOL. Do nothing if none |
| 790 | is present.""") |
| 791 | parser_unset.add_argument('symbol', metavar='SYMBOL') |
Gilles Peskine | 8e90cf4 | 2021-05-27 22:12:57 +0200 | [diff] [blame] | 792 | parser_unset_all = subparsers.add_parser('unset-all', |
| 793 | help="""Comment out all #define |
| 794 | whose name contains a match for |
| 795 | REGEX.""") |
| 796 | parser_unset_all.add_argument('regexs', metavar='REGEX', nargs='*') |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 797 | |
| 798 | def add_adapter(name, function, description): |
| 799 | subparser = subparsers.add_parser(name, help=description) |
| 800 | subparser.set_defaults(adapter=function) |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 801 | add_adapter('baremetal', baremetal_adapter, |
| 802 | """Like full, but exclude features that require platform |
| 803 | features such as file input-output.""") |
Gilles Peskine | 120f29d | 2021-09-01 19:51:19 +0200 | [diff] [blame] | 804 | add_adapter('baremetal_size', baremetal_size_adapter, |
| 805 | """Like baremetal, but exclude debugging features. |
| 806 | Useful for code size measurements.""") |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 807 | add_adapter('full', full_adapter, |
| 808 | """Uncomment most features. |
| 809 | Exclude alternative implementations and platform support |
| 810 | options, as well as some options that are awkward to test. |
| 811 | """) |
Gilles Peskine | 30de2e8 | 2020-04-20 21:39:22 +0200 | [diff] [blame] | 812 | add_adapter('full_no_deprecated', no_deprecated_adapter(full_adapter), |
Gilles Peskine | be1d609 | 2020-04-12 14:17:16 +0200 | [diff] [blame] | 813 | """Uncomment most non-deprecated features. |
| 814 | Like "full", but without deprecated features. |
| 815 | """) |
Paul Elliott | fb81f77 | 2023-10-18 17:44:59 +0100 | [diff] [blame] | 816 | add_adapter('full_no_platform', no_platform_adapter(full_adapter), |
| 817 | """Uncomment most non-platform features. |
| 818 | Like "full", but without platform features. |
| 819 | """) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 820 | add_adapter('realfull', realfull_adapter, |
Gilles Peskine | 53d41ae | 2019-07-27 23:31:53 +0200 | [diff] [blame] | 821 | """Uncomment all boolean #defines. |
| 822 | Suitable for generating documentation, but not for building.""") |
Gilles Peskine | 31987c6 | 2020-01-31 14:23:30 +0100 | [diff] [blame] | 823 | add_adapter('crypto', crypto_adapter(None), |
| 824 | """Only include crypto features. Exclude X.509 and TLS.""") |
| 825 | add_adapter('crypto_baremetal', crypto_adapter(baremetal_adapter), |
| 826 | """Like baremetal, but with only crypto features, |
| 827 | excluding X.509 and TLS.""") |
| 828 | add_adapter('crypto_full', crypto_adapter(full_adapter), |
| 829 | """Like full, but with only crypto features, |
| 830 | excluding X.509 and TLS.""") |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 831 | |
| 832 | args = parser.parse_args() |
Gabor Mezei | 33dd293 | 2024-06-28 17:51:58 +0200 | [diff] [blame] | 833 | config = CombinedConfig(MbedTLSConfigFile(args.file), CryptoConfigFile(args.cryptofile)) |
Gilles Peskine | 90b30b6 | 2019-07-28 00:36:53 +0200 | [diff] [blame] | 834 | if args.command is None: |
| 835 | parser.print_help() |
| 836 | return 1 |
| 837 | elif args.command == 'get': |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 838 | if args.symbol in config: |
| 839 | value = config[args.symbol] |
| 840 | if value: |
| 841 | sys.stdout.write(value + '\n') |
Gilles Peskine | e22a4da | 2020-03-24 15:43:49 +0100 | [diff] [blame] | 842 | return 0 if args.symbol in config else 1 |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 843 | elif args.command == 'set': |
Gilles Peskine | 98eb365 | 2019-07-28 16:39:19 +0200 | [diff] [blame] | 844 | if not args.force and args.symbol not in config.settings: |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 845 | sys.stderr.write("A #define for the symbol {} " |
Gilles Peskine | 221df1e | 2019-08-01 23:14:29 +0200 | [diff] [blame] | 846 | "was not found in {}\n" |
Gabor Mezei | 3678dee | 2024-06-04 19:58:43 +0200 | [diff] [blame] | 847 | .format(args.symbol, config.filename(args.symbol))) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 848 | return 1 |
| 849 | config.set(args.symbol, value=args.value) |
Gilles Peskine | 8e90cf4 | 2021-05-27 22:12:57 +0200 | [diff] [blame] | 850 | elif args.command == 'set-all': |
| 851 | config.change_matching(args.regexs, True) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 852 | elif args.command == 'unset': |
| 853 | config.unset(args.symbol) |
Gilles Peskine | 8e90cf4 | 2021-05-27 22:12:57 +0200 | [diff] [blame] | 854 | elif args.command == 'unset-all': |
| 855 | config.change_matching(args.regexs, False) |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 856 | else: |
| 857 | config.adapt(args.adapter) |
Gilles Peskine | 40f103c | 2019-07-27 23:44:01 +0200 | [diff] [blame] | 858 | config.write(args.write) |
Gilles Peskine | e22a4da | 2020-03-24 15:43:49 +0100 | [diff] [blame] | 859 | return 0 |
Gilles Peskine | b406389 | 2019-07-27 21:36:44 +0200 | [diff] [blame] | 860 | |
| 861 | # Import modules only used by main only if main is defined and called. |
| 862 | # pylint: disable=wrong-import-position |
| 863 | import argparse |
| 864 | import sys |
| 865 | sys.exit(main()) |