| 1 | #!/usr/bin/perl
|
|---|
| 2 |
|
|---|
| 3 | # Generate error.c
|
|---|
| 4 | #
|
|---|
| 5 | # Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments,
|
|---|
| 6 | # or generate_errors.pl include_dir data_dir error_file
|
|---|
| 7 |
|
|---|
| 8 | use strict;
|
|---|
| 9 |
|
|---|
| 10 | my ($include_dir, $data_dir, $error_file);
|
|---|
| 11 |
|
|---|
| 12 | if( @ARGV ) {
|
|---|
| 13 | die "Invalid number of arguments" if scalar @ARGV != 3;
|
|---|
| 14 | ($include_dir, $data_dir, $error_file) = @ARGV;
|
|---|
| 15 |
|
|---|
| 16 | -d $include_dir or die "No such directory: $include_dir\n";
|
|---|
| 17 | -d $data_dir or die "No such directory: $data_dir\n";
|
|---|
| 18 | } else {
|
|---|
| 19 | $include_dir = 'include/mbedtls';
|
|---|
| 20 | $data_dir = 'scripts/data_files';
|
|---|
| 21 | $error_file = 'library/error.c';
|
|---|
| 22 |
|
|---|
| 23 | unless( -d $include_dir && -d $data_dir ) {
|
|---|
| 24 | chdir '..' or die;
|
|---|
| 25 | -d $include_dir && -d $data_dir
|
|---|
| 26 | or die "Without arguments, must be run from root or scripts\n"
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | my $error_format_file = $data_dir.'/error.fmt';
|
|---|
| 31 |
|
|---|
| 32 | my @low_level_modules = ( "AES", "ASN1", "BLOWFISH", "CAMELLIA", "BIGNUM",
|
|---|
| 33 | "BASE64", "XTEA", "PBKDF2", "OID",
|
|---|
| 34 | "PADLOCK", "DES", "NET", "CTR_DRBG", "ENTROPY",
|
|---|
| 35 | "HMAC_DRBG", "MD2", "MD4", "MD5", "RIPEMD160",
|
|---|
| 36 | "SHA1", "SHA256", "SHA512", "GCM", "THREADING", "CCM" );
|
|---|
| 37 | my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "ECP", "MD", "CIPHER", "SSL",
|
|---|
| 38 | "PK", "PKCS12", "PKCS5" );
|
|---|
| 39 |
|
|---|
| 40 | my $line_separator = $/;
|
|---|
| 41 | undef $/;
|
|---|
| 42 |
|
|---|
| 43 | open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
|
|---|
| 44 | my $error_format = <FORMAT_FILE>;
|
|---|
| 45 | close(FORMAT_FILE);
|
|---|
| 46 |
|
|---|
| 47 | $/ = $line_separator;
|
|---|
| 48 |
|
|---|
| 49 | open(GREP, "grep \"define MBEDTLS_ERR_\" $include_dir/* |") || die("Failure when calling grep: $!");
|
|---|
| 50 |
|
|---|
| 51 | my $ll_old_define = "";
|
|---|
| 52 | my $hl_old_define = "";
|
|---|
| 53 |
|
|---|
| 54 | my $ll_code_check = "";
|
|---|
| 55 | my $hl_code_check = "";
|
|---|
| 56 |
|
|---|
| 57 | my $headers = "";
|
|---|
| 58 |
|
|---|
| 59 | my %error_codes_seen;
|
|---|
| 60 |
|
|---|
| 61 | while (my $line = <GREP>)
|
|---|
| 62 | {
|
|---|
| 63 | next if ($line =~ /compat-1.2.h/);
|
|---|
| 64 | my ($error_name, $error_code) = $line =~ /(MBEDTLS_ERR_\w+)\s+\-(0x\w+)/;
|
|---|
| 65 | my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//;
|
|---|
| 66 |
|
|---|
| 67 | die "Duplicated error code: $error_code ($error_name)\n"
|
|---|
| 68 | if( $error_codes_seen{$error_code}++ );
|
|---|
| 69 |
|
|---|
| 70 | $description =~ s/\\/\\\\/g;
|
|---|
| 71 | if ($description eq "") {
|
|---|
| 72 | $description = "DESCRIPTION MISSING";
|
|---|
| 73 | warn "Missing description for $error_name\n";
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
|
|---|
| 77 |
|
|---|
| 78 | # Fix faulty ones
|
|---|
| 79 | $module_name = "BIGNUM" if ($module_name eq "MPI");
|
|---|
| 80 | $module_name = "CTR_DRBG" if ($module_name eq "CTR");
|
|---|
| 81 | $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
|
|---|
| 82 |
|
|---|
| 83 | my $define_name = $module_name;
|
|---|
| 84 | $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
|
|---|
| 85 | $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
|
|---|
| 86 | $define_name = "SSL_TLS" if ($define_name eq "SSL");
|
|---|
| 87 | $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
|
|---|
| 88 |
|
|---|
| 89 | my $include_name = $module_name;
|
|---|
| 90 | $include_name =~ tr/A-Z/a-z/;
|
|---|
| 91 | $include_name = "" if ($include_name eq "asn1");
|
|---|
| 92 |
|
|---|
| 93 | my $found_ll = grep $_ eq $module_name, @low_level_modules;
|
|---|
| 94 | my $found_hl = grep $_ eq $module_name, @high_level_modules;
|
|---|
| 95 | if (!$found_ll && !$found_hl)
|
|---|
| 96 | {
|
|---|
| 97 | printf("Error: Do not know how to handle: $module_name\n");
|
|---|
| 98 | exit 1;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | my $code_check;
|
|---|
| 102 | my $old_define;
|
|---|
| 103 | my $white_space;
|
|---|
| 104 | my $first;
|
|---|
| 105 |
|
|---|
| 106 | if ($found_ll)
|
|---|
| 107 | {
|
|---|
| 108 | $code_check = \$ll_code_check;
|
|---|
| 109 | $old_define = \$ll_old_define;
|
|---|
| 110 | $white_space = ' ';
|
|---|
| 111 | }
|
|---|
| 112 | else
|
|---|
| 113 | {
|
|---|
| 114 | $code_check = \$hl_code_check;
|
|---|
| 115 | $old_define = \$hl_old_define;
|
|---|
| 116 | $white_space = ' ';
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | if ($define_name ne ${$old_define})
|
|---|
| 120 | {
|
|---|
| 121 | if (${$old_define} ne "")
|
|---|
| 122 | {
|
|---|
| 123 | ${$code_check} .= "#endif /* ";
|
|---|
| 124 | $first = 0;
|
|---|
| 125 | foreach my $dep (split(/,/, ${$old_define}))
|
|---|
| 126 | {
|
|---|
| 127 | ${$code_check} .= " || " if ($first++);
|
|---|
| 128 | ${$code_check} .= "MBEDTLS_${dep}_C";
|
|---|
| 129 | }
|
|---|
| 130 | ${$code_check} .= " */\n\n";
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | ${$code_check} .= "#if ";
|
|---|
| 134 | $headers .= "#if " if ($include_name ne "");
|
|---|
| 135 | $first = 0;
|
|---|
| 136 | foreach my $dep (split(/,/, ${define_name}))
|
|---|
| 137 | {
|
|---|
| 138 | ${$code_check} .= " || " if ($first);
|
|---|
| 139 | $headers .= " || " if ($first++);
|
|---|
| 140 |
|
|---|
| 141 | ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
|
|---|
| 142 | $headers .= "defined(MBEDTLS_${dep}_C)" if
|
|---|
| 143 | ($include_name ne "");
|
|---|
| 144 | }
|
|---|
| 145 | ${$code_check} .= "\n";
|
|---|
| 146 | $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
|
|---|
| 147 | "#endif\n\n" if ($include_name ne "");
|
|---|
| 148 | ${$old_define} = $define_name;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | if ($error_name eq "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE")
|
|---|
| 152 | {
|
|---|
| 153 | ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
|
|---|
| 154 | "${white_space}\{\n".
|
|---|
| 155 | "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n".
|
|---|
| 156 | "${white_space} return;\n".
|
|---|
| 157 | "${white_space}}\n"
|
|---|
| 158 | }
|
|---|
| 159 | else
|
|---|
| 160 | {
|
|---|
| 161 | ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
|
|---|
| 162 | "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n"
|
|---|
| 163 | }
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 | if ($ll_old_define ne "")
|
|---|
| 167 | {
|
|---|
| 168 | $ll_code_check .= "#endif /* ";
|
|---|
| 169 | my $first = 0;
|
|---|
| 170 | foreach my $dep (split(/,/, $ll_old_define))
|
|---|
| 171 | {
|
|---|
| 172 | $ll_code_check .= " || " if ($first++);
|
|---|
| 173 | $ll_code_check .= "MBEDTLS_${dep}_C";
|
|---|
| 174 | }
|
|---|
| 175 | $ll_code_check .= " */\n";
|
|---|
| 176 | }
|
|---|
| 177 | if ($hl_old_define ne "")
|
|---|
| 178 | {
|
|---|
| 179 | $hl_code_check .= "#endif /* ";
|
|---|
| 180 | my $first = 0;
|
|---|
| 181 | foreach my $dep (split(/,/, $hl_old_define))
|
|---|
| 182 | {
|
|---|
| 183 | $hl_code_check .= " || " if ($first++);
|
|---|
| 184 | $hl_code_check .= "MBEDTLS_${dep}_C";
|
|---|
| 185 | }
|
|---|
| 186 | $hl_code_check .= " */\n";
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | $error_format =~ s/HEADER_INCLUDED\n/$headers/g;
|
|---|
| 190 | $error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
|
|---|
| 191 | $error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
|
|---|
| 192 |
|
|---|
| 193 | open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
|
|---|
| 194 | print ERROR_FILE $error_format;
|
|---|
| 195 | close(ERROR_FILE);
|
|---|