blob: d8d6a20ed3876d8fc7a89b8729b06323ea0cb9d0 [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
34# Things that shouldn't be enabled with "full".
35# 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
50MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
51MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
52MBEDTLS_ZLIB_SUPPORT
53MBEDTLS_PKCS11_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020054_ALT\s*$
55);
56
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +010057# Things that should be enabled in "full" even if they match @excluded
58my @non_excluded = qw(
59PLATFORM_[A-Z0-9]+_ALT
60);
61
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020062# get -f option
63if (@ARGV >= 2 && $ARGV[0] eq "-f") {
64 shift; # -f
65 $config_file = shift;
66
67 -f $config_file or die "No such file: $config_file\n";
68} else {
69 if (! -f $config_file) {
70 chdir '..' or die;
Manuel Pégourié-Gonnardb20a70f2015-04-08 14:56:51 +020071 -f $config_file
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020072 or die "Without -f, must be run from root or scripts\n"
73 }
74}
75
76# get action
77die $usage unless @ARGV;
78my $action = shift;
79
80my ($name, $value);
Manuel Pégourié-Gonnard2134d252016-01-04 12:57:32 +010081if ($action eq "full" || $action eq "realfull") {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020082 # nothing to do
83} elsif ($action eq "unset") {
84 die $usage unless @ARGV;
85 $name = shift;
86} elsif ($action eq "set") {
87 die $usage unless @ARGV;
88 $name = shift;
89 $value = shift if @ARGV;
90} else {
91 die $usage;
92}
93die $usage if @ARGV;
94
95open my $config_read, '<', $config_file or die "read $config_file: $!\n";
96my @config_lines = <$config_read>;
97close $config_read;
98
Manuel Pégourié-Gonnard2134d252016-01-04 12:57:32 +010099my ($exclude_re, $no_exclude_re);
100if ($action eq "realfull") {
101 $exclude_re = qr/^$/;
102 $no_exclude_re = qr/./;
103} else {
104 $exclude_re = join '|', @excluded;
105 $no_exclude_re = join '|', @non_excluded;
106}
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200107
108open my $config_write, '>', $config_file or die "write $config_file: $!\n";
109
110my $done;
111for my $line (@config_lines) {
Manuel Pégourié-Gonnard2134d252016-01-04 12:57:32 +0100112 if ($action eq "full" || $action eq "realfull") {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200113 if ($line =~ /name SECTION: Module configuration options/) {
114 $done = 1;
115 }
116
Manuel Pégourié-Gonnardb7527152015-06-03 09:59:06 +0100117 if (!$done && $line =~ m!^//\s?#define! &&
118 ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100119 $line =~ s!^//\s?!!;
120 }
Manuel Pégourié-Gonnard7ee5ddd2015-06-03 10:33:55 +0100121 if (!$done && $line =~ m!^\s?#define! &&
122 ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +0100123 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200124 }
125 } elsif ($action eq "unset") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200126 if (!$done && $line =~ /^\s*#define\s*$name\b/) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200127 $line = '//' . $line;
128 $done = 1;
129 }
130 } elsif (!$done && $action eq "set") {
Manuel Pégourié-Gonnard7f9049b2015-06-23 17:42:51 +0200131 if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200132 $line = "#define $name";
133 $line .= " $value" if defined $value && $value ne "";
134 $line .= "\n";
135 $done = 1;
136 }
137 }
138
139 print $config_write $line;
140}
141
142close $config_write;
143
Manuel Pégourié-Gonnarda14cbb62015-06-03 10:49:38 +0100144die "configuration section not found" if ($action eq "full" && !$done);
145die "$name not found" if ($action ne "full" && !$done);
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +0200146
147__END__