2018-04-04 21:44:29 +00:00
|
|
|
#!/usr/bin/env perl
|
2014-05-09 11:40:14 +00:00
|
|
|
|
|
|
|
# Generate error.c
|
2011-05-09 16:17:09 +00:00
|
|
|
#
|
2014-05-09 11:40:14 +00:00
|
|
|
# Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments,
|
2024-06-14 06:43:28 +00:00
|
|
|
# or generate_errors.pl crypto_include_dir tls_include_dir data_dir error_file
|
2020-05-25 22:33:31 +00:00
|
|
|
#
|
2020-08-07 11:07:28 +00:00
|
|
|
# Copyright The Mbed TLS Contributors
|
2023-11-02 19:47:20 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2011-05-09 16:17:09 +00:00
|
|
|
|
|
|
|
use strict;
|
2021-08-02 20:53:40 +00:00
|
|
|
use warnings;
|
2011-05-09 16:17:09 +00:00
|
|
|
|
2024-06-14 06:43:28 +00:00
|
|
|
my ($crypto_include_dir, $tls_include_dir, $data_dir, $error_file);
|
2014-05-09 11:40:14 +00:00
|
|
|
|
|
|
|
if( @ARGV ) {
|
2024-06-14 06:43:28 +00:00
|
|
|
die "Invalid number of arguments" if scalar @ARGV != 4;
|
|
|
|
($crypto_include_dir, $tls_include_dir, $data_dir, $error_file) = @ARGV;
|
2014-05-09 11:40:14 +00:00
|
|
|
|
2024-06-14 06:43:28 +00:00
|
|
|
-d $crypto_include_dir or die "No such directory: $crypto_include_dir\n";
|
|
|
|
-d $tls_include_dir or die "No such directory: $tls_include_dir\n";
|
2014-05-09 11:40:14 +00:00
|
|
|
-d $data_dir or die "No such directory: $data_dir\n";
|
|
|
|
} else {
|
2024-06-14 06:43:28 +00:00
|
|
|
$crypto_include_dir = 'tf-psa-crypto/drivers/builtin/include/mbedtls';
|
|
|
|
$tls_include_dir = 'include/mbedtls';
|
2014-05-09 11:40:14 +00:00
|
|
|
$data_dir = 'scripts/data_files';
|
2024-10-11 11:04:57 +00:00
|
|
|
$error_file = 'library/error.c';
|
2014-05-09 11:40:14 +00:00
|
|
|
|
2024-06-14 06:43:28 +00:00
|
|
|
unless( -d $crypto_include_dir && -d $tls_include_dir && -d $data_dir ) {
|
2014-05-09 11:40:14 +00:00
|
|
|
chdir '..' or die;
|
2024-06-14 06:43:28 +00:00
|
|
|
-d $crypto_include_dir && -d $tls_include_dir && -d $data_dir
|
2014-05-09 11:40:14 +00:00
|
|
|
or die "Without arguments, must be run from root or scripts\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-09 16:17:09 +00:00
|
|
|
my $error_format_file = $data_dir.'/error.fmt';
|
|
|
|
|
2021-05-31 15:58:57 +00:00
|
|
|
my @low_level_modules = qw( AES ARIA ASN1 BASE64 BIGNUM
|
2018-05-07 08:43:27 +00:00
|
|
|
CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
|
2024-10-16 20:46:44 +00:00
|
|
|
ENTROPY ERROR GCM HKDF HMAC_DRBG LMS MD5
|
2024-06-20 15:45:51 +00:00
|
|
|
NET OID PBKDF2 PLATFORM POLY1305 RIPEMD160
|
2022-05-08 23:04:15 +00:00
|
|
|
SHA1 SHA256 SHA512 SHA3 THREADING );
|
2018-01-25 22:26:24 +00:00
|
|
|
my @high_level_modules = qw( CIPHER DHM ECP MD
|
|
|
|
PEM PK PKCS12 PKCS5
|
2020-06-12 18:44:04 +00:00
|
|
|
RSA SSL X509 PKCS7 );
|
2011-05-09 16:17:09 +00:00
|
|
|
|
|
|
|
undef $/;
|
|
|
|
|
2021-04-23 07:44:59 +00:00
|
|
|
open(FORMAT_FILE, '<:crlf', "$error_format_file") or die "Opening error format file '$error_format_file': $!";
|
2011-05-09 16:17:09 +00:00
|
|
|
my $error_format = <FORMAT_FILE>;
|
|
|
|
close(FORMAT_FILE);
|
|
|
|
|
2024-06-14 06:43:28 +00:00
|
|
|
my @files = glob qq("$crypto_include_dir/*.h");
|
|
|
|
push(@files, glob qq("$tls_include_dir/*.h"));
|
2020-05-25 10:21:22 +00:00
|
|
|
my @necessary_include_files;
|
2017-10-09 16:22:07 +00:00
|
|
|
my @matches;
|
|
|
|
foreach my $file (@files) {
|
2023-01-09 18:17:02 +00:00
|
|
|
open(FILE, '<:crlf', $file) or die("$0: $file: $!");
|
2021-07-26 16:39:53 +00:00
|
|
|
my $content = <FILE>;
|
2017-10-09 16:22:07 +00:00
|
|
|
close FILE;
|
2021-07-26 16:39:53 +00:00
|
|
|
my $found = 0;
|
|
|
|
while ($content =~ m[
|
2021-07-26 17:30:08 +00:00
|
|
|
# Both the before-comment and the after-comment are optional.
|
|
|
|
# Only the comment content is a regex capture group. The comment
|
|
|
|
# start and end parts are outside the capture group.
|
|
|
|
(?:/\*[*!](?!<) # Doxygen before-comment start
|
|
|
|
((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside)
|
|
|
|
\*/)? # Comment end
|
|
|
|
\s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name
|
|
|
|
\s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign)
|
|
|
|
(?:/\*[*!]< # Doxygen after-comment start
|
|
|
|
((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside)
|
|
|
|
\*/)? # Comment end
|
2021-07-26 16:39:53 +00:00
|
|
|
]gsx) {
|
|
|
|
my ($before, $name, $value, $after) = ($1, $2, $3, $4);
|
|
|
|
# Discard Doxygen comments that are coincidentally present before
|
|
|
|
# an error definition but not attached to it. This is ad hoc, based
|
|
|
|
# on what actually matters (or mattered at some point).
|
2021-08-02 20:53:40 +00:00
|
|
|
undef $before if defined($before) && $before =~ /\s*\\name\s/s;
|
2021-07-26 16:39:53 +00:00
|
|
|
die "Description neither before nor after $name in $file\n"
|
|
|
|
if !defined($before) && !defined($after);
|
|
|
|
die "Description both before and after $name in $file\n"
|
|
|
|
if defined($before) && defined($after);
|
|
|
|
my $description = (defined($before) ? $before : $after);
|
|
|
|
$description =~ s/^\s+//;
|
2021-07-26 16:45:22 +00:00
|
|
|
$description =~ s/\n( *\*)? */ /g;
|
2021-07-26 16:39:53 +00:00
|
|
|
$description =~ s/\.?\s+$//;
|
|
|
|
push @matches, [$name, $value, $description];
|
|
|
|
++$found;
|
|
|
|
}
|
|
|
|
if ($found) {
|
|
|
|
my $include_name = $file;
|
|
|
|
$include_name =~ s!.*/!!;
|
2024-10-16 20:46:44 +00:00
|
|
|
$include_name = "error.h" if ($include_name eq "error_common.h");
|
2021-07-26 16:39:53 +00:00
|
|
|
push @necessary_include_files, $include_name;
|
|
|
|
}
|
2017-10-09 16:22:07 +00:00
|
|
|
}
|
2011-05-09 16:17:09 +00:00
|
|
|
|
|
|
|
my $ll_old_define = "";
|
|
|
|
my $hl_old_define = "";
|
|
|
|
|
|
|
|
my $ll_code_check = "";
|
|
|
|
my $hl_code_check = "";
|
|
|
|
|
|
|
|
my $headers = "";
|
2020-05-25 10:21:22 +00:00
|
|
|
my %included_headers;
|
2011-05-09 16:17:09 +00:00
|
|
|
|
2015-01-22 13:19:20 +00:00
|
|
|
my %error_codes_seen;
|
|
|
|
|
2021-07-26 16:39:53 +00:00
|
|
|
foreach my $match (@matches)
|
2011-05-09 16:17:09 +00:00
|
|
|
{
|
2021-07-26 16:39:53 +00:00
|
|
|
my ($error_name, $error_code, $description) = @$match;
|
2015-01-22 13:19:20 +00:00
|
|
|
|
|
|
|
die "Duplicated error code: $error_code ($error_name)\n"
|
|
|
|
if( $error_codes_seen{$error_code}++ );
|
|
|
|
|
2011-05-09 16:17:09 +00:00
|
|
|
$description =~ s/\\/\\\\/g;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
|
2011-05-09 16:17:09 +00:00
|
|
|
|
|
|
|
# Fix faulty ones
|
|
|
|
$module_name = "BIGNUM" if ($module_name eq "MPI");
|
2011-11-27 14:50:49 +00:00
|
|
|
$module_name = "CTR_DRBG" if ($module_name eq "CTR");
|
2014-02-01 09:22:21 +00:00
|
|
|
$module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
|
2011-05-09 16:17:09 +00:00
|
|
|
|
|
|
|
my $define_name = $module_name;
|
2020-02-26 17:28:23 +00:00
|
|
|
$define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
|
2011-11-15 16:38:34 +00:00
|
|
|
$define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
|
2020-02-26 17:28:25 +00:00
|
|
|
$define_name = "SSL_TLS" if ($define_name eq "SSL");
|
2013-09-15 18:43:33 +00:00
|
|
|
$define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
|
2020-06-12 18:44:04 +00:00
|
|
|
$define_name = "PKCS7" if ($define_name eq "PKCS7");
|
2011-05-09 16:17:09 +00:00
|
|
|
|
|
|
|
my $include_name = $module_name;
|
|
|
|
$include_name =~ tr/A-Z/a-z/;
|
|
|
|
|
2020-02-26 17:26:46 +00:00
|
|
|
# Fix faulty ones
|
|
|
|
$include_name = "net_sockets" if ($module_name eq "NET");
|
|
|
|
|
2020-05-25 10:21:22 +00:00
|
|
|
$included_headers{"${include_name}.h"} = $module_name;
|
|
|
|
|
2011-05-09 16:17:09 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-08-06 15:24:56 +00:00
|
|
|
printf("Error: Do not know how to handle: $module_name\n");
|
2011-05-09 16:17:09 +00:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $code_check;
|
|
|
|
my $old_define;
|
|
|
|
my $white_space;
|
2013-09-15 18:43:33 +00:00
|
|
|
my $first;
|
2011-05-09 16:17:09 +00:00
|
|
|
|
|
|
|
if ($found_ll)
|
|
|
|
{
|
|
|
|
$code_check = \$ll_code_check;
|
|
|
|
$old_define = \$ll_old_define;
|
2020-04-09 18:39:04 +00:00
|
|
|
$white_space = ' ';
|
2011-05-09 16:17:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$code_check = \$hl_code_check;
|
|
|
|
$old_define = \$hl_old_define;
|
2020-04-09 18:39:04 +00:00
|
|
|
$white_space = ' ';
|
2011-05-09 16:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($define_name ne ${$old_define})
|
|
|
|
{
|
|
|
|
if (${$old_define} ne "")
|
|
|
|
{
|
2013-09-15 18:43:33 +00:00
|
|
|
${$code_check} .= "#endif /* ";
|
|
|
|
$first = 0;
|
|
|
|
foreach my $dep (split(/,/, ${$old_define}))
|
|
|
|
{
|
|
|
|
${$code_check} .= " || " if ($first++);
|
2015-04-08 10:49:31 +00:00
|
|
|
${$code_check} .= "MBEDTLS_${dep}_C";
|
2013-09-15 18:43:33 +00:00
|
|
|
}
|
|
|
|
${$code_check} .= " */\n\n";
|
2011-05-09 16:17:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 18:43:33 +00: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++);
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
${$code_check} .= "defined(MBEDTLS_${dep}_C)";
|
|
|
|
$headers .= "defined(MBEDTLS_${dep}_C)" if
|
2013-09-15 18:43:33 +00:00
|
|
|
($include_name ne "");
|
|
|
|
}
|
|
|
|
${$code_check} .= "\n";
|
2015-03-09 17:05:11 +00:00
|
|
|
$headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
|
2011-05-09 16:17:09 +00:00
|
|
|
"#endif\n\n" if ($include_name ne "");
|
|
|
|
${$old_define} = $define_name;
|
|
|
|
}
|
|
|
|
|
2020-04-09 18:39:04 +00:00
|
|
|
${$code_check} .= "${white_space}case -($error_name):\n".
|
2020-04-22 15:13:25 +00:00
|
|
|
"${white_space} return( \"$module_name - $description\" );\n"
|
2011-05-09 16:17:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($ll_old_define ne "")
|
|
|
|
{
|
2015-04-08 18:27:02 +00:00
|
|
|
$ll_code_check .= "#endif /* ";
|
|
|
|
my $first = 0;
|
|
|
|
foreach my $dep (split(/,/, $ll_old_define))
|
|
|
|
{
|
|
|
|
$ll_code_check .= " || " if ($first++);
|
|
|
|
$ll_code_check .= "MBEDTLS_${dep}_C";
|
|
|
|
}
|
|
|
|
$ll_code_check .= " */\n";
|
2011-05-09 16:17:09 +00:00
|
|
|
}
|
|
|
|
if ($hl_old_define ne "")
|
|
|
|
{
|
2015-04-08 18:27:02 +00:00
|
|
|
$hl_code_check .= "#endif /* ";
|
|
|
|
my $first = 0;
|
|
|
|
foreach my $dep (split(/,/, $hl_old_define))
|
|
|
|
{
|
|
|
|
$hl_code_check .= " || " if ($first++);
|
|
|
|
$hl_code_check .= "MBEDTLS_${dep}_C";
|
|
|
|
}
|
|
|
|
$hl_code_check .= " */\n";
|
2011-05-09 16:17:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
2020-05-25 10:21:22 +00:00
|
|
|
|
|
|
|
my $errors = 0;
|
|
|
|
for my $include_name (@necessary_include_files)
|
|
|
|
{
|
|
|
|
if (not $included_headers{$include_name})
|
|
|
|
{
|
|
|
|
print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
|
|
|
|
++$errors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit !!$errors;
|