blob: 33b14ee2a9bbbd8618fc9aea19238600dae5e8a8 [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úti700ee442020-05-26 00:33:31 +02007#
Bence Szépkúti1e148272020-08-07 13:07:28 +02008# Copyright The Mbed TLS Contributors
Bence Szépkútic7da1fe2020-05-26 01:54:15 +02009# SPDX-License-Identifier: Apache-2.0
10#
11# Licensed under the Apache License, Version 2.0 (the "License"); you may
12# not use this file except in compliance with the License.
13# You may obtain a copy of the License at
14#
15# http://www.apache.org/licenses/LICENSE-2.0
16#
17# Unless required by applicable law or agreed to in writing, software
18# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20# See the License for the specific language governing permissions and
21# limitations under the License.
Paul Bakker9d781402011-05-09 16:17:09 +000022
23use strict;
24
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020025my ($include_dir, $data_dir, $error_file);
26
27if( @ARGV ) {
28 die "Invalid number of arguments" if scalar @ARGV != 3;
29 ($include_dir, $data_dir, $error_file) = @ARGV;
30
31 -d $include_dir or die "No such directory: $include_dir\n";
32 -d $data_dir or die "No such directory: $data_dir\n";
33} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020035 $data_dir = 'scripts/data_files';
36 $error_file = 'library/error.c';
37
38 unless( -d $include_dir && -d $data_dir ) {
39 chdir '..' or die;
40 -d $include_dir && -d $data_dir
41 or die "Without arguments, must be run from root or scripts\n"
42 }
43}
44
Paul Bakker9d781402011-05-09 16:17:09 +000045my $error_format_file = $data_dir.'/error.fmt';
46
TRodziewicz10e8cf52021-05-31 17:58:57 +020047my @low_level_modules = qw( AES ARIA ASN1 BASE64 BIGNUM
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020048 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
TRodziewicz10e8cf52021-05-31 17:58:57 +020049 ENTROPY ERROR GCM HKDF HMAC_DRBG MD5
Gilles Peskine1fcf7212020-02-26 18:26:46 +010050 NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
TRodziewicz10e8cf52021-05-31 17:58:57 +020051 SHA1 SHA256 SHA512 THREADING );
Gilles Peskine92143272018-01-25 23:26:24 +010052my @high_level_modules = qw( CIPHER DHM ECP MD
53 PEM PK PKCS12 PKCS5
Gilles Peskine314bc892020-02-26 18:28:25 +010054 RSA SSL X509 );
Paul Bakker9d781402011-05-09 16:17:09 +000055
Paul Bakker9d781402011-05-09 16:17:09 +000056undef $/;
57
Gilles Peskinea4d32732021-04-23 09:44:59 +020058open(FORMAT_FILE, '<:crlf', "$error_format_file") or die "Opening error format file '$error_format_file': $!";
Paul Bakker9d781402011-05-09 16:17:09 +000059my $error_format = <FORMAT_FILE>;
60close(FORMAT_FILE);
61
Andres Amaya Garciad2da6222017-10-17 21:23:15 +010062my @files = <$include_dir/*.h>;
Gilles Peskine5241f852020-05-25 12:21:22 +020063my @necessary_include_files;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010064my @matches;
65foreach my $file (@files) {
Gilles Peskinea4d32732021-04-23 09:44:59 +020066 open(FILE, '<:crlf', "$file");
Gilles Peskine47b09562021-07-26 18:39:53 +020067 my $content = <FILE>;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010068 close FILE;
Gilles Peskine47b09562021-07-26 18:39:53 +020069 my $found = 0;
70 while ($content =~ m[
71 (?:/\*[*!]([^<](?:[^*]|\*+[^*/])*)\*/)?
72 \s*\#\s*define\s+(MBEDTLS_ERR_\w+)\s+\-(0[Xx][0-9A-Fa-f]+)\s*
73 (?:/\*[*!]<((?:[^*]|\*+[^*/])*)\*/)?
74 ]gsx) {
75 my ($before, $name, $value, $after) = ($1, $2, $3, $4);
76 # Discard Doxygen comments that are coincidentally present before
77 # an error definition but not attached to it. This is ad hoc, based
78 # on what actually matters (or mattered at some point).
79 undef $before if $before =~ /\s*\\name\s/s;
80 die "Description neither before nor after $name in $file\n"
81 if !defined($before) && !defined($after);
82 die "Description both before and after $name in $file\n"
83 if defined($before) && defined($after);
84 my $description = (defined($before) ? $before : $after);
85 $description =~ s/^\s+//;
Gilles Peskine05aa5432021-07-26 18:45:22 +020086 $description =~ s/\n( *\*)? */ /g;
Gilles Peskine47b09562021-07-26 18:39:53 +020087 $description =~ s/\.?\s+$//;
88 push @matches, [$name, $value, $description];
89 ++$found;
90 }
91 if ($found) {
92 my $include_name = $file;
93 $include_name =~ s!.*/!!;
94 push @necessary_include_files, $include_name;
95 }
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010096}
Paul Bakker9d781402011-05-09 16:17:09 +000097
98my $ll_old_define = "";
99my $hl_old_define = "";
100
101my $ll_code_check = "";
102my $hl_code_check = "";
103
104my $headers = "";
Gilles Peskine5241f852020-05-25 12:21:22 +0200105my %included_headers;
Paul Bakker9d781402011-05-09 16:17:09 +0000106
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000107my %error_codes_seen;
108
Gilles Peskine47b09562021-07-26 18:39:53 +0200109foreach my $match (@matches)
Paul Bakker9d781402011-05-09 16:17:09 +0000110{
Gilles Peskine47b09562021-07-26 18:39:53 +0200111 my ($error_name, $error_code, $description) = @$match;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000112
113 die "Duplicated error code: $error_code ($error_name)\n"
114 if( $error_codes_seen{$error_code}++ );
115
Paul Bakker9d781402011-05-09 16:17:09 +0000116 $description =~ s/\\/\\\\/g;
Paul Bakker9d781402011-05-09 16:17:09 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
Paul Bakker9d781402011-05-09 16:17:09 +0000119
120 # Fix faulty ones
121 $module_name = "BIGNUM" if ($module_name eq "MPI");
Paul Bakker880ac7e2011-11-27 14:50:49 +0000122 $module_name = "CTR_DRBG" if ($module_name eq "CTR");
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +0100123 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
Paul Bakker9d781402011-05-09 16:17:09 +0000124
125 my $define_name = $module_name;
Gilles Peskine1bf45e12020-02-26 18:28:23 +0100126 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
Paul Bakkerdceecd82011-11-15 16:38:34 +0000127 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
Gilles Peskine314bc892020-02-26 18:28:25 +0100128 $define_name = "SSL_TLS" if ($define_name eq "SSL");
Paul Bakkercff68422013-09-15 20:43:33 +0200129 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
Paul Bakker9d781402011-05-09 16:17:09 +0000130
131 my $include_name = $module_name;
132 $include_name =~ tr/A-Z/a-z/;
Paul Bakker9d781402011-05-09 16:17:09 +0000133
Gilles Peskine1fcf7212020-02-26 18:26:46 +0100134 # Fix faulty ones
135 $include_name = "net_sockets" if ($module_name eq "NET");
136
Gilles Peskine5241f852020-05-25 12:21:22 +0200137 $included_headers{"${include_name}.h"} = $module_name;
138
Paul Bakker9d781402011-05-09 16:17:09 +0000139 my $found_ll = grep $_ eq $module_name, @low_level_modules;
140 my $found_hl = grep $_ eq $module_name, @high_level_modules;
141 if (!$found_ll && !$found_hl)
142 {
Manuel Pégourié-Gonnard48573f82015-08-06 17:24:56 +0200143 printf("Error: Do not know how to handle: $module_name\n");
Paul Bakker9d781402011-05-09 16:17:09 +0000144 exit 1;
145 }
146
147 my $code_check;
148 my $old_define;
149 my $white_space;
Paul Bakkercff68422013-09-15 20:43:33 +0200150 my $first;
Paul Bakker9d781402011-05-09 16:17:09 +0000151
152 if ($found_ll)
153 {
154 $code_check = \$ll_code_check;
155 $old_define = \$ll_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700156 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000157 }
158 else
159 {
160 $code_check = \$hl_code_check;
161 $old_define = \$hl_old_define;
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700162 $white_space = ' ';
Paul Bakker9d781402011-05-09 16:17:09 +0000163 }
164
165 if ($define_name ne ${$old_define})
166 {
167 if (${$old_define} ne "")
168 {
Paul Bakkercff68422013-09-15 20:43:33 +0200169 ${$code_check} .= "#endif /* ";
170 $first = 0;
171 foreach my $dep (split(/,/, ${$old_define}))
172 {
173 ${$code_check} .= " || " if ($first++);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 ${$code_check} .= "MBEDTLS_${dep}_C";
Paul Bakkercff68422013-09-15 20:43:33 +0200175 }
176 ${$code_check} .= " */\n\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000177 }
178
Paul Bakkercff68422013-09-15 20:43:33 +0200179 ${$code_check} .= "#if ";
180 $headers .= "#if " if ($include_name ne "");
181 $first = 0;
182 foreach my $dep (split(/,/, ${define_name}))
183 {
184 ${$code_check} .= " || " if ($first);
185 $headers .= " || " if ($first++);
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
188 $headers .= "defined(MBEDTLS_${dep}_C)" if
Paul Bakkercff68422013-09-15 20:43:33 +0200189 ($include_name ne "");
190 }
191 ${$code_check} .= "\n";
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000192 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
Paul Bakker9d781402011-05-09 16:17:09 +0000193 "#endif\n\n" if ($include_name ne "");
194 ${$old_define} = $define_name;
195 }
196
Gaurav Aggarwala4a2aa52020-04-09 11:39:04 -0700197 ${$code_check} .= "${white_space}case -($error_name):\n".
Gaurav Aggarwalcabde252020-04-22 08:13:25 -0700198 "${white_space} return( \"$module_name - $description\" );\n"
Paul Bakker9d781402011-05-09 16:17:09 +0000199};
200
201if ($ll_old_define ne "")
202{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200203 $ll_code_check .= "#endif /* ";
204 my $first = 0;
205 foreach my $dep (split(/,/, $ll_old_define))
206 {
207 $ll_code_check .= " || " if ($first++);
208 $ll_code_check .= "MBEDTLS_${dep}_C";
209 }
210 $ll_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000211}
212if ($hl_old_define ne "")
213{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200214 $hl_code_check .= "#endif /* ";
215 my $first = 0;
216 foreach my $dep (split(/,/, $hl_old_define))
217 {
218 $hl_code_check .= " || " if ($first++);
219 $hl_code_check .= "MBEDTLS_${dep}_C";
220 }
221 $hl_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000222}
223
224$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
225$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
226$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
227
228open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
229print ERROR_FILE $error_format;
230close(ERROR_FILE);
Gilles Peskine5241f852020-05-25 12:21:22 +0200231
232my $errors = 0;
233for my $include_name (@necessary_include_files)
234{
235 if (not $included_headers{$include_name})
236 {
237 print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
238 ++$errors;
239 }
240}
241
242exit !!$errors;