fbrosson | 533407a | 2018-04-04 21:44:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 2 | |
| 3 | # Generate error.c |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 4 | # |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 5 | # Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments, |
| 6 | # or generate_errors.pl include_dir data_dir error_file |
Bence Szépkúti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 7 | # |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 8 | # Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 9 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 10 | # |
| 11 | # This file is provided under the Apache License 2.0, or the |
| 12 | # GNU General Public License v2.0 or later. |
| 13 | # |
| 14 | # ********** |
| 15 | # Apache License 2.0: |
Bence Szépkúti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 16 | # |
| 17 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 18 | # not use this file except in compliance with the License. |
| 19 | # You may obtain a copy of the License at |
| 20 | # |
| 21 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | # |
| 23 | # Unless required by applicable law or agreed to in writing, software |
| 24 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | # See the License for the specific language governing permissions and |
| 27 | # limitations under the License. |
Bence Szépkúti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 28 | # |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 29 | # ********** |
| 30 | # |
| 31 | # ********** |
| 32 | # GNU General Public License v2.0 or later: |
| 33 | # |
| 34 | # This program is free software; you can redistribute it and/or modify |
| 35 | # it under the terms of the GNU General Public License as published by |
| 36 | # the Free Software Foundation; either version 2 of the License, or |
| 37 | # (at your option) any later version. |
| 38 | # |
| 39 | # This program is distributed in the hope that it will be useful, |
| 40 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 41 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 42 | # GNU General Public License for more details. |
| 43 | # |
| 44 | # You should have received a copy of the GNU General Public License along |
| 45 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 46 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 47 | # |
| 48 | # ********** |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 49 | |
| 50 | use strict; |
Gilles Peskine | 915f258 | 2021-08-02 22:53:40 +0200 | [diff] [blame] | 51 | use warnings; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 53 | my ($include_dir, $data_dir, $error_file); |
| 54 | |
| 55 | if( @ARGV ) { |
| 56 | die "Invalid number of arguments" if scalar @ARGV != 3; |
| 57 | ($include_dir, $data_dir, $error_file) = @ARGV; |
| 58 | |
| 59 | -d $include_dir or die "No such directory: $include_dir\n"; |
| 60 | -d $data_dir or die "No such directory: $data_dir\n"; |
| 61 | } else { |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 62 | $include_dir = 'include/mbedtls'; |
Manuel Pégourié-Gonnard | d66f900 | 2014-05-09 13:40:14 +0200 | [diff] [blame] | 63 | $data_dir = 'scripts/data_files'; |
| 64 | $error_file = 'library/error.c'; |
| 65 | |
| 66 | unless( -d $include_dir && -d $data_dir ) { |
| 67 | chdir '..' or die; |
| 68 | -d $include_dir && -d $data_dir |
| 69 | or die "Without arguments, must be run from root or scripts\n" |
| 70 | } |
| 71 | } |
| 72 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 73 | my $error_format_file = $data_dir.'/error.fmt'; |
| 74 | |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 75 | my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 76 | CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES |
Thomas Fossati | 656864b | 2016-07-17 08:51:22 +0100 | [diff] [blame] | 77 | ENTROPY GCM HKDF HMAC_DRBG MD2 MD4 MD5 |
Ron Eldor | 0ff4e0b | 2018-08-29 18:53:20 +0300 | [diff] [blame] | 78 | NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160 |
Gilles Peskine | 9214327 | 2018-01-25 23:26:24 +0100 | [diff] [blame] | 79 | SHA1 SHA256 SHA512 THREADING XTEA ); |
| 80 | my @high_level_modules = qw( CIPHER DHM ECP MD |
| 81 | PEM PK PKCS12 PKCS5 |
| 82 | RSA SSL X509 ); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 83 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 84 | undef $/; |
| 85 | |
| 86 | open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!"; |
| 87 | my $error_format = <FORMAT_FILE>; |
| 88 | close(FORMAT_FILE); |
| 89 | |
Andres Amaya Garcia | d2da622 | 2017-10-17 21:23:15 +0100 | [diff] [blame] | 90 | my @files = <$include_dir/*.h>; |
Gilles Peskine | 4f84cc7 | 2020-05-25 12:21:22 +0200 | [diff] [blame] | 91 | my @necessary_include_files; |
Andres Amaya Garcia | 36855d6 | 2017-10-09 17:22:07 +0100 | [diff] [blame] | 92 | my @matches; |
| 93 | foreach my $file (@files) { |
| 94 | open(FILE, "$file"); |
Gilles Peskine | 2638a62 | 2021-07-26 18:39:53 +0200 | [diff] [blame] | 95 | my $content = <FILE>; |
Andres Amaya Garcia | 36855d6 | 2017-10-09 17:22:07 +0100 | [diff] [blame] | 96 | close FILE; |
Gilles Peskine | 2638a62 | 2021-07-26 18:39:53 +0200 | [diff] [blame] | 97 | my $found = 0; |
| 98 | while ($content =~ m[ |
Gilles Peskine | 000a231 | 2021-07-26 19:30:08 +0200 | [diff] [blame] | 99 | # Both the before-comment and the after-comment are optional. |
| 100 | # Only the comment content is a regex capture group. The comment |
| 101 | # start and end parts are outside the capture group. |
| 102 | (?:/\*[*!](?!<) # Doxygen before-comment start |
| 103 | ((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside) |
| 104 | \*/)? # Comment end |
| 105 | \s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name |
| 106 | \s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign) |
| 107 | (?:/\*[*!]< # Doxygen after-comment start |
| 108 | ((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside) |
| 109 | \*/)? # Comment end |
Gilles Peskine | 2638a62 | 2021-07-26 18:39:53 +0200 | [diff] [blame] | 110 | ]gsx) { |
| 111 | my ($before, $name, $value, $after) = ($1, $2, $3, $4); |
| 112 | # Discard Doxygen comments that are coincidentally present before |
| 113 | # an error definition but not attached to it. This is ad hoc, based |
| 114 | # on what actually matters (or mattered at some point). |
Gilles Peskine | 915f258 | 2021-08-02 22:53:40 +0200 | [diff] [blame] | 115 | undef $before if defined($before) && $before =~ /\s*\\name\s/s; |
Gilles Peskine | 2638a62 | 2021-07-26 18:39:53 +0200 | [diff] [blame] | 116 | die "Description neither before nor after $name in $file\n" |
| 117 | if !defined($before) && !defined($after); |
| 118 | die "Description both before and after $name in $file\n" |
| 119 | if defined($before) && defined($after); |
| 120 | my $description = (defined($before) ? $before : $after); |
| 121 | $description =~ s/^\s+//; |
Gilles Peskine | 63518ea | 2021-07-26 18:45:22 +0200 | [diff] [blame] | 122 | $description =~ s/\n( *\*)? */ /g; |
Gilles Peskine | 2638a62 | 2021-07-26 18:39:53 +0200 | [diff] [blame] | 123 | $description =~ s/\.?\s+$//; |
| 124 | push @matches, [$name, $value, $description]; |
| 125 | ++$found; |
| 126 | } |
| 127 | if ($found) { |
| 128 | my $include_name = $file; |
| 129 | $include_name =~ s!.*/!!; |
| 130 | push @necessary_include_files, $include_name; |
| 131 | } |
Andres Amaya Garcia | 36855d6 | 2017-10-09 17:22:07 +0100 | [diff] [blame] | 132 | } |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 133 | |
| 134 | my $ll_old_define = ""; |
| 135 | my $hl_old_define = ""; |
| 136 | |
| 137 | my $ll_code_check = ""; |
| 138 | my $hl_code_check = ""; |
| 139 | |
| 140 | my $headers = ""; |
Gilles Peskine | 4f84cc7 | 2020-05-25 12:21:22 +0200 | [diff] [blame] | 141 | my %included_headers; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | a9a9916 | 2015-01-22 13:19:20 +0000 | [diff] [blame] | 143 | my %error_codes_seen; |
| 144 | |
Gilles Peskine | 2638a62 | 2021-07-26 18:39:53 +0200 | [diff] [blame] | 145 | foreach my $match (@matches) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 146 | { |
Gilles Peskine | 2638a62 | 2021-07-26 18:39:53 +0200 | [diff] [blame] | 147 | my ($error_name, $error_code, $description) = @$match; |
Manuel Pégourié-Gonnard | a9a9916 | 2015-01-22 13:19:20 +0000 | [diff] [blame] | 148 | |
| 149 | die "Duplicated error code: $error_code ($error_name)\n" |
| 150 | if( $error_codes_seen{$error_code}++ ); |
| 151 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 152 | $description =~ s/\\/\\\\/g; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 155 | |
| 156 | # Fix faulty ones |
| 157 | $module_name = "BIGNUM" if ($module_name eq "MPI"); |
Paul Bakker | 880ac7e | 2011-11-27 14:50:49 +0000 | [diff] [blame] | 158 | $module_name = "CTR_DRBG" if ($module_name eq "CTR"); |
Manuel Pégourié-Gonnard | cf38367 | 2014-02-01 10:22:21 +0100 | [diff] [blame] | 159 | $module_name = "HMAC_DRBG" if ($module_name eq "HMAC"); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 160 | |
| 161 | my $define_name = $module_name; |
Paul Bakker | 36713e8 | 2013-09-17 13:25:29 +0200 | [diff] [blame] | 162 | $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509"); |
Paul Bakker | dceecd8 | 2011-11-15 16:38:34 +0000 | [diff] [blame] | 163 | $define_name = "ASN1_PARSE" if ($define_name eq "ASN1"); |
Paul Bakker | e471cd1 | 2011-05-18 13:27:35 +0000 | [diff] [blame] | 164 | $define_name = "SSL_TLS" if ($define_name eq "SSL"); |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 165 | $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM"); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 166 | |
| 167 | my $include_name = $module_name; |
| 168 | $include_name =~ tr/A-Z/a-z/; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 169 | |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 170 | # Fix faulty ones |
| 171 | $include_name = "net_sockets" if ($module_name eq "NET"); |
| 172 | |
Gilles Peskine | 4f84cc7 | 2020-05-25 12:21:22 +0200 | [diff] [blame] | 173 | $included_headers{"${include_name}.h"} = $module_name; |
| 174 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 175 | my $found_ll = grep $_ eq $module_name, @low_level_modules; |
| 176 | my $found_hl = grep $_ eq $module_name, @high_level_modules; |
| 177 | if (!$found_ll && !$found_hl) |
| 178 | { |
Manuel Pégourié-Gonnard | 48573f8 | 2015-08-06 17:24:56 +0200 | [diff] [blame] | 179 | printf("Error: Do not know how to handle: $module_name\n"); |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 180 | exit 1; |
| 181 | } |
| 182 | |
| 183 | my $code_check; |
| 184 | my $old_define; |
| 185 | my $white_space; |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 186 | my $first; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 187 | |
| 188 | if ($found_ll) |
| 189 | { |
| 190 | $code_check = \$ll_code_check; |
| 191 | $old_define = \$ll_old_define; |
| 192 | $white_space = ' '; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | $code_check = \$hl_code_check; |
| 197 | $old_define = \$hl_old_define; |
| 198 | $white_space = ' '; |
| 199 | } |
| 200 | |
| 201 | if ($define_name ne ${$old_define}) |
| 202 | { |
| 203 | if (${$old_define} ne "") |
| 204 | { |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 205 | ${$code_check} .= "#endif /* "; |
| 206 | $first = 0; |
| 207 | foreach my $dep (split(/,/, ${$old_define})) |
| 208 | { |
| 209 | ${$code_check} .= " || " if ($first++); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | ${$code_check} .= "MBEDTLS_${dep}_C"; |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 211 | } |
| 212 | ${$code_check} .= " */\n\n"; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 215 | ${$code_check} .= "#if "; |
| 216 | $headers .= "#if " if ($include_name ne ""); |
| 217 | $first = 0; |
| 218 | foreach my $dep (split(/,/, ${define_name})) |
| 219 | { |
| 220 | ${$code_check} .= " || " if ($first); |
| 221 | $headers .= " || " if ($first++); |
| 222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | ${$code_check} .= "defined(MBEDTLS_${dep}_C)"; |
| 224 | $headers .= "defined(MBEDTLS_${dep}_C)" if |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 225 | ($include_name ne ""); |
| 226 | } |
| 227 | ${$code_check} .= "\n"; |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 228 | $headers .= "\n#include \"mbedtls/${include_name}.h\"\n". |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 229 | "#endif\n\n" if ($include_name ne ""); |
| 230 | ${$old_define} = $define_name; |
| 231 | } |
| 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | if ($error_name eq "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE") |
Paul Bakker | 3aac1da | 2012-05-08 13:12:27 +0000 | [diff] [blame] | 234 | { |
| 235 | ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". |
| 236 | "${white_space}\{\n". |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n". |
Paul Bakker | 3aac1da | 2012-05-08 13:12:27 +0000 | [diff] [blame] | 238 | "${white_space} return;\n". |
| 239 | "${white_space}}\n" |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n" |
Paul Bakker | 3aac1da | 2012-05-08 13:12:27 +0000 | [diff] [blame] | 245 | } |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | if ($ll_old_define ne "") |
| 249 | { |
Manuel Pégourié-Gonnard | e546ad4 | 2015-04-08 20:27:02 +0200 | [diff] [blame] | 250 | $ll_code_check .= "#endif /* "; |
| 251 | my $first = 0; |
| 252 | foreach my $dep (split(/,/, $ll_old_define)) |
| 253 | { |
| 254 | $ll_code_check .= " || " if ($first++); |
| 255 | $ll_code_check .= "MBEDTLS_${dep}_C"; |
| 256 | } |
| 257 | $ll_code_check .= " */\n"; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 258 | } |
| 259 | if ($hl_old_define ne "") |
| 260 | { |
Manuel Pégourié-Gonnard | e546ad4 | 2015-04-08 20:27:02 +0200 | [diff] [blame] | 261 | $hl_code_check .= "#endif /* "; |
| 262 | my $first = 0; |
| 263 | foreach my $dep (split(/,/, $hl_old_define)) |
| 264 | { |
| 265 | $hl_code_check .= " || " if ($first++); |
| 266 | $hl_code_check .= "MBEDTLS_${dep}_C"; |
| 267 | } |
| 268 | $hl_code_check .= " */\n"; |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | $error_format =~ s/HEADER_INCLUDED\n/$headers/g; |
| 272 | $error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g; |
| 273 | $error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g; |
| 274 | |
| 275 | open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!"; |
| 276 | print ERROR_FILE $error_format; |
| 277 | close(ERROR_FILE); |
Gilles Peskine | 4f84cc7 | 2020-05-25 12:21:22 +0200 | [diff] [blame] | 278 | |
| 279 | my $errors = 0; |
| 280 | for my $include_name (@necessary_include_files) |
| 281 | { |
| 282 | if (not $included_headers{$include_name}) |
| 283 | { |
| 284 | print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n"; |
| 285 | ++$errors; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | exit !!$errors; |