blob: 7108f506a7ac407d641d9eeb6197a8c3329af995 [file] [log] [blame]
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +02001#!/usr/bin/perl
2
3# Tune the configuration file
4
5use warnings;
6use strict;
7
Simon Butcher07cc5492017-02-02 14:26:15 +00008my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +02009my $usage = <<EOU;
Brian J Murraye7f8dc32016-11-06 04:45:15 -080010$0 [-f <file>] [set <symbol> <value> | unset <symbol> | full | realfull]
11
12Commands
13 set <symbol> [<value>] - Uncomments or adds a #define for the <symbol> to
14 the configuration file, and optionally making it
15 of <value>.
16 If the symbol isn't present in the file an error
17 is returned.
18 unset <symbol> - Comments out the #define for the given symbol if
19 present in the configuration file.
20 full - Uncomments all #define's in the configuration file
21 excluding some reserved symbols, until the
22 'Module configuration options' section
23 realfull - Uncomments all #define's with no exclusions
24
25Options
26 -f <filename> - The file or file path for the configuration file
27 to edit. When omitted, the following default is
28 used:
29 $config_file
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020030EOU
Manuel Pégourié-Gonnard052ae252014-11-14 13:09:41 +010031# for our eyes only:
Manuel Pégourié-Gonnard2134d252016-01-04 12:57:32 +010032# $0 [-f <file>] full|realfull
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020033
Hanno Becker41f5a0f2017-11-06 15:06:51 +000034# The following options are disabled instead of enabled with "full".
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020035# Notes:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036# - MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 and
37# MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020038# respective tests were adapted
39my @excluded = qw(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040MBEDTLS_DEPRECATED_REMOVED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041MBEDTLS_HAVE_SSE2
42MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
43MBEDTLS_ECP_DP_M221_ENABLED
44MBEDTLS_ECP_DP_M383_ENABLED
45MBEDTLS_ECP_DP_M511_ENABLED
46MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
47MBEDTLS_NO_PLATFORM_ENTROPY
48MBEDTLS_REMOVE_ARC4_CIPHERSUITES
49MBEDTLS_SSL_HW_RECORD_ACCEL
Hanno Becker41f5a0f2017-11-06 15:06:51 +000050MBEDTLS_RSA_NO_CRT
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
52MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
53MBEDTLS_ZLIB_SUPPORT
54MBEDTLS_PKCS11_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020055_ALT\s*$
56);
57
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010058# Things that should be enabled in "full" even if they match @excluded
59my @non_excluded = qw(
60PLATFORM_[A-Z0-9]+_ALT
61);
62
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020063# get -f option
64if (@ARGV >= 2 && $ARGV[0] eq "-f") {
65 shift; # -f
66 $config_file = shift;
67
68 -f $config_file or die "No such file: $config_file\n";
69} else {
70 if (! -f $config_file) {
71 chdir '..' or die;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020072 -f $config_file
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020073 or die "Without -f, must be run from root or scripts\n"
74 }
75}
76
77# get action
78die $usage unless @ARGV;
79my $action = shift;
80
81my ($name, $value);
Manuel Pégourié-Gonnard2134d252016-01-04 12:57:32 +010082if ($action eq "full" || $action eq "realfull") {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020083 # nothing to do
84} elsif ($action eq "unset") {
85 die $usage unless @ARGV;
86 $name = shift;
87} elsif ($action eq "set") {
88 die $usage unless @ARGV;
89 $name = shift;
90 $value = shift if @ARGV;
91} else {
92 die $usage;
93}
94die $usage if @ARGV;
95
96open my $config_read, '<', $config_file or die "read $config_file: $!\n";
97my @config_lines = <$config_read>;
98close $config_read;
99
Manuel Pégourié-Gonnard2134d252016-01-04 12:57:32 +0100100my ($exclude_re, $no_exclude_re);
101if ($action eq "realfull") {
102 $exclude_re = qr/^$/;
103 $no_exclude_re = qr/./;
104} else {
105 $exclude_re = join '|', @excluded;
106 $no_exclude_re = join '|', @non_excluded;
107}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200108
109open my $config_write, '>', $config_file or die "write $config_file: $!\n";
110
111my $done;
112for my $line (@config_lines) {
Manuel Pégourié-Gonnard2134d252016-01-04 12:57:32 +0100113 if ($action eq "full" || $action eq "realfull") {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200114 if ($line =~ /name SECTION: Module configuration options/) {
115 $done = 1;
116 }
117
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100118 if (!$done && $line =~ m!^//\s?#define! &&
119 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100120 $line =~ s!^//\s?!!;
121 }
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100122 if (!$done && $line =~ m!^\s?#define! &&
123 ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100124 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200125 }
126 } elsif ($action eq "unset") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200127 if (!$done && $line =~ /^\s*#define\s*$name\b/) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200128 $line = '//' . $line;
129 $done = 1;
130 }
131 } elsif (!$done && $action eq "set") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200132 if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200133 $line = "#define $name";
134 $line .= " $value" if defined $value && $value ne "";
135 $line .= "\n";
136 $done = 1;
137 }
138 }
139
140 print $config_write $line;
141}
142
143close $config_write;
144
Manuel Pégourié-Gonnarda14cbb62015-06-03 10:49:38 +0100145die "configuration section not found" if ($action eq "full" && !$done);
146die "$name not found" if ($action ne "full" && !$done);
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200147
148__END__