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