blob: 9deda833dae0bfe8841fa6cbd64bad15e1477b93 [file] [log] [blame]
fbrosson533407a2018-04-04 21:44:29 +00001#!/usr/bin/env perl
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02002
3# Generate error.c
Paul Bakker9d781402011-05-09 16:17:09 +00004#
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +02005# 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úti468a76f2020-05-26 00:33:31 +02007#
Bence Szépkútia2947ac2020-08-19 16:37:36 +02008# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02009# 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úti51b41d52020-05-26 01:54:15 +020016#
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úti468a76f2020-05-26 00:33:31 +020028#
Bence Szépkútif744bd72020-06-05 13:02:18 +020029# **********
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 Bakker9d781402011-05-09 16:17:09 +000049
50use strict;
51
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020052my ($include_dir, $data_dir, $error_file);
53
54if( @ARGV ) {
55 die "Invalid number of arguments" if scalar @ARGV != 3;
56 ($include_dir, $data_dir, $error_file) = @ARGV;
57
58 -d $include_dir or die "No such directory: $include_dir\n";
59 -d $data_dir or die "No such directory: $data_dir\n";
60} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020062 $data_dir = 'scripts/data_files';
63 $error_file = 'library/error.c';
64
65 unless( -d $include_dir && -d $data_dir ) {
66 chdir '..' or die;
67 -d $include_dir && -d $data_dir
68 or die "Without arguments, must be run from root or scripts\n"
69 }
70}
71
Paul Bakker9d781402011-05-09 16:17:09 +000072my $error_format_file = $data_dir.'/error.fmt';
73
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000074my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020075 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
Thomas Fossati656864b2016-07-17 08:51:22 +010076 ENTROPY GCM HKDF HMAC_DRBG MD2 MD4 MD5
Ron Eldor0ff4e0b2018-08-29 18:53:20 +030077 NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
Gilles Peskine92143272018-01-25 23:26:24 +010078 SHA1 SHA256 SHA512 THREADING XTEA );
79my @high_level_modules = qw( CIPHER DHM ECP MD
80 PEM PK PKCS12 PKCS5
81 RSA SSL X509 );
Paul Bakker9d781402011-05-09 16:17:09 +000082
Paul Bakker9d781402011-05-09 16:17:09 +000083undef $/;
84
85open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
86my $error_format = <FORMAT_FILE>;
87close(FORMAT_FILE);
88
Andres Amaya Garciad2da6222017-10-17 21:23:15 +010089my @files = <$include_dir/*.h>;
Gilles Peskine4f84cc72020-05-25 12:21:22 +020090my @necessary_include_files;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010091my @matches;
92foreach my $file (@files) {
93 open(FILE, "$file");
Gilles Peskine2638a622021-07-26 18:39:53 +020094 my $content = <FILE>;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010095 close FILE;
Gilles Peskine2638a622021-07-26 18:39:53 +020096 my $found = 0;
97 while ($content =~ m[
Gilles Peskine000a2312021-07-26 19:30:08 +020098 # Both the before-comment and the after-comment are optional.
99 # Only the comment content is a regex capture group. The comment
100 # start and end parts are outside the capture group.
101 (?:/\*[*!](?!<) # Doxygen before-comment start
102 ((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside)
103 \*/)? # Comment end
104 \s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name
105 \s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign)
106 (?:/\*[*!]< # Doxygen after-comment start
107 ((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside)
108 \*/)? # Comment end
Gilles Peskine2638a622021-07-26 18:39:53 +0200109 ]gsx) {
110 my ($before, $name, $value, $after) = ($1, $2, $3, $4);
111 # Discard Doxygen comments that are coincidentally present before
112 # an error definition but not attached to it. This is ad hoc, based
113 # on what actually matters (or mattered at some point).
114 undef $before if $before =~ /\s*\\name\s/s;
115 die "Description neither before nor after $name in $file\n"
116 if !defined($before) && !defined($after);
117 die "Description both before and after $name in $file\n"
118 if defined($before) && defined($after);
119 my $description = (defined($before) ? $before : $after);
120 $description =~ s/^\s+//;
Gilles Peskine63518ea2021-07-26 18:45:22 +0200121 $description =~ s/\n( *\*)? */ /g;
Gilles Peskine2638a622021-07-26 18:39:53 +0200122 $description =~ s/\.?\s+$//;
123 push @matches, [$name, $value, $description];
124 ++$found;
125 }
126 if ($found) {
127 my $include_name = $file;
128 $include_name =~ s!.*/!!;
129 push @necessary_include_files, $include_name;
130 }
Andres Amaya Garcia36855d62017-10-09 17:22:07 +0100131}
Paul Bakker9d781402011-05-09 16:17:09 +0000132
133my $ll_old_define = "";
134my $hl_old_define = "";
135
136my $ll_code_check = "";
137my $hl_code_check = "";
138
139my $headers = "";
Gilles Peskine4f84cc72020-05-25 12:21:22 +0200140my %included_headers;
Paul Bakker9d781402011-05-09 16:17:09 +0000141
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000142my %error_codes_seen;
143
Gilles Peskine2638a622021-07-26 18:39:53 +0200144foreach my $match (@matches)
Paul Bakker9d781402011-05-09 16:17:09 +0000145{
Gilles Peskine2638a622021-07-26 18:39:53 +0200146 my ($error_name, $error_code, $description) = @$match;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000147
148 die "Duplicated error code: $error_code ($error_name)\n"
149 if( $error_codes_seen{$error_code}++ );
150
Paul Bakker9d781402011-05-09 16:17:09 +0000151 $description =~ s/\\/\\\\/g;
Paul Bakker9d781402011-05-09 16:17:09 +0000152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
Paul Bakker9d781402011-05-09 16:17:09 +0000154
155 # Fix faulty ones
156 $module_name = "BIGNUM" if ($module_name eq "MPI");
Paul Bakker880ac7e2011-11-27 14:50:49 +0000157 $module_name = "CTR_DRBG" if ($module_name eq "CTR");
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +0100158 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
Paul Bakker9d781402011-05-09 16:17:09 +0000159
160 my $define_name = $module_name;
Paul Bakker36713e82013-09-17 13:25:29 +0200161 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
Paul Bakkerdceecd82011-11-15 16:38:34 +0000162 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
Paul Bakkere471cd12011-05-18 13:27:35 +0000163 $define_name = "SSL_TLS" if ($define_name eq "SSL");
Paul Bakkercff68422013-09-15 20:43:33 +0200164 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
Paul Bakker9d781402011-05-09 16:17:09 +0000165
166 my $include_name = $module_name;
167 $include_name =~ tr/A-Z/a-z/;
Paul Bakker9d781402011-05-09 16:17:09 +0000168
Andres AG788aa4a2016-09-14 14:32:09 +0100169 # Fix faulty ones
170 $include_name = "net_sockets" if ($module_name eq "NET");
171
Gilles Peskine4f84cc72020-05-25 12:21:22 +0200172 $included_headers{"${include_name}.h"} = $module_name;
173
Paul Bakker9d781402011-05-09 16:17:09 +0000174 my $found_ll = grep $_ eq $module_name, @low_level_modules;
175 my $found_hl = grep $_ eq $module_name, @high_level_modules;
176 if (!$found_ll && !$found_hl)
177 {
Manuel Pégourié-Gonnard48573f82015-08-06 17:24:56 +0200178 printf("Error: Do not know how to handle: $module_name\n");
Paul Bakker9d781402011-05-09 16:17:09 +0000179 exit 1;
180 }
181
182 my $code_check;
183 my $old_define;
184 my $white_space;
Paul Bakkercff68422013-09-15 20:43:33 +0200185 my $first;
Paul Bakker9d781402011-05-09 16:17:09 +0000186
187 if ($found_ll)
188 {
189 $code_check = \$ll_code_check;
190 $old_define = \$ll_old_define;
191 $white_space = ' ';
192 }
193 else
194 {
195 $code_check = \$hl_code_check;
196 $old_define = \$hl_old_define;
197 $white_space = ' ';
198 }
199
200 if ($define_name ne ${$old_define})
201 {
202 if (${$old_define} ne "")
203 {
Paul Bakkercff68422013-09-15 20:43:33 +0200204 ${$code_check} .= "#endif /* ";
205 $first = 0;
206 foreach my $dep (split(/,/, ${$old_define}))
207 {
208 ${$code_check} .= " || " if ($first++);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 ${$code_check} .= "MBEDTLS_${dep}_C";
Paul Bakkercff68422013-09-15 20:43:33 +0200210 }
211 ${$code_check} .= " */\n\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000212 }
213
Paul Bakkercff68422013-09-15 20:43:33 +0200214 ${$code_check} .= "#if ";
215 $headers .= "#if " if ($include_name ne "");
216 $first = 0;
217 foreach my $dep (split(/,/, ${define_name}))
218 {
219 ${$code_check} .= " || " if ($first);
220 $headers .= " || " if ($first++);
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
223 $headers .= "defined(MBEDTLS_${dep}_C)" if
Paul Bakkercff68422013-09-15 20:43:33 +0200224 ($include_name ne "");
225 }
226 ${$code_check} .= "\n";
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000227 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
Paul Bakker9d781402011-05-09 16:17:09 +0000228 "#endif\n\n" if ($include_name ne "");
229 ${$old_define} = $define_name;
230 }
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if ($error_name eq "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE")
Paul Bakker3aac1da2012-05-08 13:12:27 +0000233 {
234 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
235 "${white_space}\{\n".
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n".
Paul Bakker3aac1da2012-05-08 13:12:27 +0000237 "${white_space} return;\n".
238 "${white_space}}\n"
239 }
240 else
241 {
242 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n"
Paul Bakker3aac1da2012-05-08 13:12:27 +0000244 }
Paul Bakker9d781402011-05-09 16:17:09 +0000245};
246
247if ($ll_old_define ne "")
248{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200249 $ll_code_check .= "#endif /* ";
250 my $first = 0;
251 foreach my $dep (split(/,/, $ll_old_define))
252 {
253 $ll_code_check .= " || " if ($first++);
254 $ll_code_check .= "MBEDTLS_${dep}_C";
255 }
256 $ll_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000257}
258if ($hl_old_define ne "")
259{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200260 $hl_code_check .= "#endif /* ";
261 my $first = 0;
262 foreach my $dep (split(/,/, $hl_old_define))
263 {
264 $hl_code_check .= " || " if ($first++);
265 $hl_code_check .= "MBEDTLS_${dep}_C";
266 }
267 $hl_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000268}
269
270$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
271$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
272$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
273
274open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
275print ERROR_FILE $error_format;
276close(ERROR_FILE);
Gilles Peskine4f84cc72020-05-25 12:21:22 +0200277
278my $errors = 0;
279for my $include_name (@necessary_include_files)
280{
281 if (not $included_headers{$include_name})
282 {
283 print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
284 ++$errors;
285 }
286}
287
288exit !!$errors;