blob: bec69e66ba758591cbea46be3c9f15a14c0530a2 [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
8my $usage = <<EOU;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +02009$0 [-f <file>] unset <name>
10$0 [-f <file>] set <name> [<value>]
11EOU
Manuel Pégourié-Gonnard052ae252014-11-14 13:09:41 +010012# for our eyes only:
13# $0 [-f <file>] full
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020014
15# Things that shouldn't be enabled with "full".
16# Notes:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017# - MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3 and
18# MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION could be enabled if the
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020019# respective tests were adapted
20my @excluded = qw(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021MBEDTLS_DEPRECATED_REMOVED
22MBEDTLS_HAVE_INT8
23MBEDTLS_HAVE_INT16
24MBEDTLS_HAVE_SSE2
25MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
26MBEDTLS_ECP_DP_M221_ENABLED
27MBEDTLS_ECP_DP_M383_ENABLED
28MBEDTLS_ECP_DP_M511_ENABLED
29MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
30MBEDTLS_NO_PLATFORM_ENTROPY
31MBEDTLS_REMOVE_ARC4_CIPHERSUITES
32MBEDTLS_SSL_HW_RECORD_ACCEL
33MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3
34MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
35MBEDTLS_ZLIB_SUPPORT
36MBEDTLS_PKCS11_C
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020037_ALT\s*$
38);
39
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040my $config_file = "include/mbedtls/config.h";
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020041
42# get -f option
43if (@ARGV >= 2 && $ARGV[0] eq "-f") {
44 shift; # -f
45 $config_file = shift;
46
47 -f $config_file or die "No such file: $config_file\n";
48} else {
49 if (! -f $config_file) {
50 chdir '..' or die;
51 -d $config_file
52 or die "Without -f, must be run from root or scripts\n"
53 }
54}
55
56# get action
57die $usage unless @ARGV;
58my $action = shift;
59
60my ($name, $value);
61if ($action eq "full") {
62 # nothing to do
63} elsif ($action eq "unset") {
64 die $usage unless @ARGV;
65 $name = shift;
66} elsif ($action eq "set") {
67 die $usage unless @ARGV;
68 $name = shift;
69 $value = shift if @ARGV;
70} else {
71 die $usage;
72}
73die $usage if @ARGV;
74
75open my $config_read, '<', $config_file or die "read $config_file: $!\n";
76my @config_lines = <$config_read>;
77close $config_read;
78
79my $exclude_re = join '|', @excluded;
80
81open my $config_write, '>', $config_file or die "write $config_file: $!\n";
82
83my $done;
84for my $line (@config_lines) {
85 if ($action eq "full") {
86 if ($line =~ /name SECTION: Module configuration options/) {
87 $done = 1;
88 }
89
90 if (!$done && $line =~ m!^//\s?#define! && $line !~ /$exclude_re/) {
Manuel Pégourié-Gonnardea0920f2015-03-24 09:50:15 +010091 $line =~ s!^//\s?!!;
92 }
93 if (!$done && $line =~ m!^\s?#define! && $line =~ /$exclude_re/) {
94 $line =~ s!^!//!;
Manuel Pégourié-Gonnardab3d8622014-07-12 03:19:18 +020095 }
96 } elsif ($action eq "unset") {
97 if (!$done && $line =~ /^\s*#define\s*$name/) {
98 $line = '//' . $line;
99 $done = 1;
100 }
101 } elsif (!$done && $action eq "set") {
102 if ($line =~ m!^(?://)?\s*#define\s*$name!) {
103 $line = "#define $name";
104 $line .= " $value" if defined $value && $value ne "";
105 $line .= "\n";
106 $done = 1;
107 }
108 }
109
110 print $config_write $line;
111}
112
113close $config_write;
114
115warn "configuration section not found" if ($action eq "full" && !$done);
116warn "$name not found" if ($action ne "full" && !$done);
117
118__END__