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