2011-05-09 18:17:09 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
my $include_dir = shift or die "Missing include directory";
|
|
|
|
my $data_dir = shift or die "Missing data directory";
|
|
|
|
my $error_file = shift or die "Missing destination file";
|
|
|
|
my $error_format_file = $data_dir.'/error.fmt';
|
|
|
|
|
2012-07-04 13:08:50 +02:00
|
|
|
my @low_level_modules = ( "AES", "ASN1", "BLOWFISH", "CAMELLIA", "BIGNUM",
|
2013-06-24 19:28:55 +02:00
|
|
|
"BASE64", "XTEA", "PBKDF2", "OID",
|
2011-12-10 22:55:01 +01:00
|
|
|
"PADLOCK", "DES", "NET", "CTR_DRBG", "ENTROPY",
|
2014-02-01 10:22:21 +01:00
|
|
|
"HMAC_DRBG", "MD2", "MD4", "MD5", "RIPEMD160",
|
|
|
|
"SHA1", "SHA256", "SHA512", "GCM", "THREADING" );
|
2013-06-24 19:17:19 +02:00
|
|
|
my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "ECP", "MD", "CIPHER", "SSL",
|
2013-09-17 14:36:05 +02:00
|
|
|
"PK", "PKCS12", "PKCS5" );
|
2011-05-09 18:17:09 +02:00
|
|
|
|
|
|
|
my $line_separator = $/;
|
|
|
|
undef $/;
|
|
|
|
|
|
|
|
open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
|
|
|
|
my $error_format = <FORMAT_FILE>;
|
|
|
|
close(FORMAT_FILE);
|
|
|
|
|
|
|
|
$/ = $line_separator;
|
|
|
|
|
|
|
|
open(GREP, "/bin/grep \"define POLARSSL_ERR_\" $include_dir/* |") || die("Failure when calling grep: $!");
|
|
|
|
|
|
|
|
my $ll_old_define = "";
|
|
|
|
my $hl_old_define = "";
|
|
|
|
|
|
|
|
my $ll_code_check = "";
|
|
|
|
my $hl_code_check = "";
|
|
|
|
|
|
|
|
my $headers = "";
|
|
|
|
|
|
|
|
while (my $line = <GREP>)
|
|
|
|
{
|
2013-09-17 13:25:29 +02:00
|
|
|
next if ($line =~ /compat-1.2.h/);
|
2011-05-09 18:17:09 +02:00
|
|
|
my ($error_name, $error_code) = $line =~ /(POLARSSL_ERR_\w+)\s+\-(0x\w+)/;
|
|
|
|
my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//;
|
|
|
|
$description =~ s/\\/\\\\/g;
|
|
|
|
$description = "DESCRIPTION MISSING" if ($description eq "");
|
|
|
|
|
|
|
|
my ($module_name) = $error_name =~ /^POLARSSL_ERR_([^_]+)/;
|
|
|
|
|
|
|
|
# Fix faulty ones
|
|
|
|
$module_name = "BIGNUM" if ($module_name eq "MPI");
|
2011-11-27 15:50:49 +01:00
|
|
|
$module_name = "CTR_DRBG" if ($module_name eq "CTR");
|
2014-02-01 10:22:21 +01:00
|
|
|
$module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
|
2011-05-09 18:17:09 +02:00
|
|
|
|
|
|
|
my $define_name = $module_name;
|
2013-09-17 13:25:29 +02:00
|
|
|
$define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
|
2011-11-15 17:38:34 +01:00
|
|
|
$define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
|
2011-05-18 15:27:35 +02:00
|
|
|
$define_name = "SSL_TLS" if ($define_name eq "SSL");
|
2013-09-15 20:43:33 +02:00
|
|
|
$define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
|
2011-05-09 18:17:09 +02:00
|
|
|
|
|
|
|
my $include_name = $module_name;
|
|
|
|
$include_name =~ tr/A-Z/a-z/;
|
|
|
|
$include_name = "" if ($include_name eq "asn1");
|
|
|
|
|
|
|
|
my $found_ll = grep $_ eq $module_name, @low_level_modules;
|
|
|
|
my $found_hl = grep $_ eq $module_name, @high_level_modules;
|
|
|
|
if (!$found_ll && !$found_hl)
|
|
|
|
{
|
|
|
|
printf("Error: Do not know how to handle: $module_name\n");
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $code_check;
|
|
|
|
my $old_define;
|
|
|
|
my $white_space;
|
2013-09-15 20:43:33 +02:00
|
|
|
my $first;
|
2011-05-09 18:17:09 +02:00
|
|
|
|
|
|
|
if ($found_ll)
|
|
|
|
{
|
|
|
|
$code_check = \$ll_code_check;
|
|
|
|
$old_define = \$ll_old_define;
|
|
|
|
$white_space = ' ';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$code_check = \$hl_code_check;
|
|
|
|
$old_define = \$hl_old_define;
|
|
|
|
$white_space = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($define_name ne ${$old_define})
|
|
|
|
{
|
|
|
|
if (${$old_define} ne "")
|
|
|
|
{
|
2013-09-15 20:43:33 +02:00
|
|
|
${$code_check} .= "#endif /* ";
|
|
|
|
$first = 0;
|
|
|
|
foreach my $dep (split(/,/, ${$old_define}))
|
|
|
|
{
|
|
|
|
${$code_check} .= " || " if ($first++);
|
|
|
|
${$code_check} .= "POLARSSL_${dep}_C";
|
|
|
|
}
|
|
|
|
${$code_check} .= " */\n\n";
|
2011-05-09 18:17:09 +02:00
|
|
|
}
|
|
|
|
|
2013-09-15 20:43:33 +02:00
|
|
|
${$code_check} .= "#if ";
|
|
|
|
$headers .= "#if " if ($include_name ne "");
|
|
|
|
$first = 0;
|
|
|
|
foreach my $dep (split(/,/, ${define_name}))
|
|
|
|
{
|
|
|
|
${$code_check} .= " || " if ($first);
|
|
|
|
$headers .= " || " if ($first++);
|
|
|
|
|
|
|
|
${$code_check} .= "defined(POLARSSL_${dep}_C)";
|
|
|
|
$headers .= "defined(POLARSSL_${dep}_C)" if
|
|
|
|
($include_name ne "");
|
|
|
|
}
|
|
|
|
${$code_check} .= "\n";
|
|
|
|
$headers .= "\n#include \"polarssl/${include_name}.h\"\n".
|
2011-05-09 18:17:09 +02:00
|
|
|
"#endif\n\n" if ($include_name ne "");
|
|
|
|
${$old_define} = $define_name;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:12:27 +02:00
|
|
|
if ($error_name eq "POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE")
|
|
|
|
{
|
|
|
|
${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
|
|
|
|
"${white_space}\{\n".
|
|
|
|
"${white_space} snprintf( buf, buflen, \"$module_name - $description\" );\n".
|
|
|
|
"${white_space} return;\n".
|
|
|
|
"${white_space}}\n"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
|
|
|
|
"${white_space} snprintf( buf, buflen, \"$module_name - $description\" );\n"
|
|
|
|
}
|
2011-05-09 18:17:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($ll_old_define ne "")
|
|
|
|
{
|
|
|
|
$ll_code_check .= "#endif /* POLARSSL_${ll_old_define}_C */\n\n";
|
|
|
|
}
|
|
|
|
if ($hl_old_define ne "")
|
|
|
|
{
|
|
|
|
$hl_code_check .= "#endif /* POLARSSL_${hl_old_define}_C */\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
|
|
|
|
$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
|
|
|
|
$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
|
|
|
|
|
|
|
|
open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
|
|
|
|
print ERROR_FILE $error_format;
|
|
|
|
close(ERROR_FILE);
|