| 1 | #!/usr/bin/perl
|
|---|
| 2 |
|
|---|
| 3 | # Tune the configuration file
|
|---|
| 4 |
|
|---|
| 5 | use warnings;
|
|---|
| 6 | use strict;
|
|---|
| 7 |
|
|---|
| 8 | my $usage = <<EOU;
|
|---|
| 9 | $0 [-f <file>] unset <name>
|
|---|
| 10 | $0 [-f <file>] set <name> [<value>]
|
|---|
| 11 | EOU
|
|---|
| 12 | # for our eyes only:
|
|---|
| 13 | # $0 [-f <file>] full|realfull
|
|---|
| 14 |
|
|---|
| 15 | # Things that shouldn't be enabled with "full".
|
|---|
| 16 | # Notes:
|
|---|
| 17 | # - MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 and
|
|---|
| 18 | # MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the
|
|---|
| 19 | # respective tests were adapted
|
|---|
| 20 | my @excluded = qw(
|
|---|
| 21 | MBEDTLS_DEPRECATED_REMOVED
|
|---|
| 22 | MBEDTLS_HAVE_SSE2
|
|---|
| 23 | MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
|
|---|
| 24 | MBEDTLS_ECP_DP_M221_ENABLED
|
|---|
| 25 | MBEDTLS_ECP_DP_M383_ENABLED
|
|---|
| 26 | MBEDTLS_ECP_DP_M511_ENABLED
|
|---|
| 27 | MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
|
|---|
| 28 | MBEDTLS_NO_PLATFORM_ENTROPY
|
|---|
| 29 | MBEDTLS_REMOVE_ARC4_CIPHERSUITES
|
|---|
| 30 | MBEDTLS_SSL_HW_RECORD_ACCEL
|
|---|
| 31 | MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
|
|---|
| 32 | MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
|
|---|
| 33 | MBEDTLS_ZLIB_SUPPORT
|
|---|
| 34 | MBEDTLS_PKCS11_C
|
|---|
| 35 | _ALT\s*$
|
|---|
| 36 | );
|
|---|
| 37 |
|
|---|
| 38 | # Things that should be enabled in "full" even if they match @excluded
|
|---|
| 39 | my @non_excluded = qw(
|
|---|
| 40 | PLATFORM_[A-Z0-9]+_ALT
|
|---|
| 41 | );
|
|---|
| 42 |
|
|---|
| 43 | my $config_file = "include/mbedtls/config.h";
|
|---|
| 44 |
|
|---|
| 45 | # get -f option
|
|---|
| 46 | if (@ARGV >= 2 && $ARGV[0] eq "-f") {
|
|---|
| 47 | shift; # -f
|
|---|
| 48 | $config_file = shift;
|
|---|
| 49 |
|
|---|
| 50 | -f $config_file or die "No such file: $config_file\n";
|
|---|
| 51 | } else {
|
|---|
| 52 | if (! -f $config_file) {
|
|---|
| 53 | chdir '..' or die;
|
|---|
| 54 | -f $config_file
|
|---|
| 55 | or die "Without -f, must be run from root or scripts\n"
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | # get action
|
|---|
| 60 | die $usage unless @ARGV;
|
|---|
| 61 | my $action = shift;
|
|---|
| 62 |
|
|---|
| 63 | my ($name, $value);
|
|---|
| 64 | if ($action eq "full" || $action eq "realfull") {
|
|---|
| 65 | # nothing to do
|
|---|
| 66 | } elsif ($action eq "unset") {
|
|---|
| 67 | die $usage unless @ARGV;
|
|---|
| 68 | $name = shift;
|
|---|
| 69 | } elsif ($action eq "set") {
|
|---|
| 70 | die $usage unless @ARGV;
|
|---|
| 71 | $name = shift;
|
|---|
| 72 | $value = shift if @ARGV;
|
|---|
| 73 | } else {
|
|---|
| 74 | die $usage;
|
|---|
| 75 | }
|
|---|
| 76 | die $usage if @ARGV;
|
|---|
| 77 |
|
|---|
| 78 | open my $config_read, '<', $config_file or die "read $config_file: $!\n";
|
|---|
| 79 | my @config_lines = <$config_read>;
|
|---|
| 80 | close $config_read;
|
|---|
| 81 |
|
|---|
| 82 | my ($exclude_re, $no_exclude_re);
|
|---|
| 83 | if ($action eq "realfull") {
|
|---|
| 84 | $exclude_re = qr/^$/;
|
|---|
| 85 | $no_exclude_re = qr/./;
|
|---|
| 86 | } else {
|
|---|
| 87 | $exclude_re = join '|', @excluded;
|
|---|
| 88 | $no_exclude_re = join '|', @non_excluded;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | open my $config_write, '>', $config_file or die "write $config_file: $!\n";
|
|---|
| 92 |
|
|---|
| 93 | my $done;
|
|---|
| 94 | for my $line (@config_lines) {
|
|---|
| 95 | if ($action eq "full" || $action eq "realfull") {
|
|---|
| 96 | if ($line =~ /name SECTION: Module configuration options/) {
|
|---|
| 97 | $done = 1;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | if (!$done && $line =~ m!^//\s?#define! &&
|
|---|
| 101 | ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
|
|---|
| 102 | $line =~ s!^//\s?!!;
|
|---|
| 103 | }
|
|---|
| 104 | if (!$done && $line =~ m!^\s?#define! &&
|
|---|
| 105 | ! ( $line !~ /$exclude_re/ || $line =~ /$no_exclude_re/ ) ) {
|
|---|
| 106 | $line =~ s!^!//!;
|
|---|
| 107 | }
|
|---|
| 108 | } elsif ($action eq "unset") {
|
|---|
| 109 | if (!$done && $line =~ /^\s*#define\s*$name\b/) {
|
|---|
| 110 | $line = '//' . $line;
|
|---|
| 111 | $done = 1;
|
|---|
| 112 | }
|
|---|
| 113 | } elsif (!$done && $action eq "set") {
|
|---|
| 114 | if ($line =~ m!^(?://)?\s*#define\s*$name\b!) {
|
|---|
| 115 | $line = "#define $name";
|
|---|
| 116 | $line .= " $value" if defined $value && $value ne "";
|
|---|
| 117 | $line .= "\n";
|
|---|
| 118 | $done = 1;
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | print $config_write $line;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | close $config_write;
|
|---|
| 126 |
|
|---|
| 127 | die "configuration section not found" if ($action eq "full" && !$done);
|
|---|
| 128 | die "$name not found" if ($action ne "full" && !$done);
|
|---|
| 129 |
|
|---|
| 130 | __END__
|
|---|