blob: d8b04889350706967f2b0837b2738bcb12125fe6 [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#
8# Copyright (C) 2011-2018, Arm Limited, All Rights Reserved
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# **********
49#
Bence Szépkúti468a76f2020-05-26 00:33:31 +020050# This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker9d781402011-05-09 16:17:09 +000051
52use strict;
53
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020054my ($include_dir, $data_dir, $error_file);
55
56if( @ARGV ) {
57 die "Invalid number of arguments" if scalar @ARGV != 3;
58 ($include_dir, $data_dir, $error_file) = @ARGV;
59
60 -d $include_dir or die "No such directory: $include_dir\n";
61 -d $data_dir or die "No such directory: $data_dir\n";
62} else {
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063 $include_dir = 'include/mbedtls';
Manuel Pégourié-Gonnardd66f9002014-05-09 13:40:14 +020064 $data_dir = 'scripts/data_files';
65 $error_file = 'library/error.c';
66
67 unless( -d $include_dir && -d $data_dir ) {
68 chdir '..' or die;
69 -d $include_dir && -d $data_dir
70 or die "Without arguments, must be run from root or scripts\n"
71 }
72}
73
Paul Bakker9d781402011-05-09 16:17:09 +000074my $error_format_file = $data_dir.'/error.fmt';
75
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000076my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020077 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
Thomas Fossati656864b2016-07-17 08:51:22 +010078 ENTROPY GCM HKDF HMAC_DRBG MD2 MD4 MD5
Ron Eldor0ff4e0b2018-08-29 18:53:20 +030079 NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
Gilles Peskine92143272018-01-25 23:26:24 +010080 SHA1 SHA256 SHA512 THREADING XTEA );
81my @high_level_modules = qw( CIPHER DHM ECP MD
82 PEM PK PKCS12 PKCS5
83 RSA SSL X509 );
Paul Bakker9d781402011-05-09 16:17:09 +000084
85my $line_separator = $/;
86undef $/;
87
88open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
89my $error_format = <FORMAT_FILE>;
90close(FORMAT_FILE);
91
92$/ = $line_separator;
93
Andres Amaya Garciad2da6222017-10-17 21:23:15 +010094my @files = <$include_dir/*.h>;
Gilles Peskine4f84cc72020-05-25 12:21:22 +020095my @necessary_include_files;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +010096my @matches;
97foreach my $file (@files) {
98 open(FILE, "$file");
Andres Amaya Garcia69944b12017-10-17 21:24:56 +010099 my @grep_res = grep(/^\s*#define\s+MBEDTLS_ERR_\w+\s+\-0x[0-9A-Fa-f]+/, <FILE>);
Andres Amaya Garcia36855d62017-10-09 17:22:07 +0100100 push(@matches, @grep_res);
101 close FILE;
Gilles Peskine4f84cc72020-05-25 12:21:22 +0200102 my $include_name = $file;
103 $include_name =~ s!.*/!!;
104 push @necessary_include_files, $include_name if @grep_res;
Andres Amaya Garcia36855d62017-10-09 17:22:07 +0100105}
Paul Bakker9d781402011-05-09 16:17:09 +0000106
107my $ll_old_define = "";
108my $hl_old_define = "";
109
110my $ll_code_check = "";
111my $hl_code_check = "";
112
113my $headers = "";
Gilles Peskine4f84cc72020-05-25 12:21:22 +0200114my %included_headers;
Paul Bakker9d781402011-05-09 16:17:09 +0000115
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000116my %error_codes_seen;
117
Andres Amaya Garcia36855d62017-10-09 17:22:07 +0100118foreach my $line (@matches)
Paul Bakker9d781402011-05-09 16:17:09 +0000119{
Paul Bakker36713e82013-09-17 13:25:29 +0200120 next if ($line =~ /compat-1.2.h/);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 my ($error_name, $error_code) = $line =~ /(MBEDTLS_ERR_\w+)\s+\-(0x\w+)/;
Paul Bakker9d781402011-05-09 16:17:09 +0000122 my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000123
124 die "Duplicated error code: $error_code ($error_name)\n"
125 if( $error_codes_seen{$error_code}++ );
126
Paul Bakker9d781402011-05-09 16:17:09 +0000127 $description =~ s/\\/\\\\/g;
Manuel Pégourié-Gonnarda9a99162015-01-22 13:19:20 +0000128 if ($description eq "") {
129 $description = "DESCRIPTION MISSING";
130 warn "Missing description for $error_name\n";
131 }
Paul Bakker9d781402011-05-09 16:17:09 +0000132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
Paul Bakker9d781402011-05-09 16:17:09 +0000134
135 # Fix faulty ones
136 $module_name = "BIGNUM" if ($module_name eq "MPI");
Paul Bakker880ac7e2011-11-27 14:50:49 +0000137 $module_name = "CTR_DRBG" if ($module_name eq "CTR");
Manuel Pégourié-Gonnardcf383672014-02-01 10:22:21 +0100138 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
Paul Bakker9d781402011-05-09 16:17:09 +0000139
140 my $define_name = $module_name;
Paul Bakker36713e82013-09-17 13:25:29 +0200141 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
Paul Bakkerdceecd82011-11-15 16:38:34 +0000142 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
Paul Bakkere471cd12011-05-18 13:27:35 +0000143 $define_name = "SSL_TLS" if ($define_name eq "SSL");
Paul Bakkercff68422013-09-15 20:43:33 +0200144 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
Paul Bakker9d781402011-05-09 16:17:09 +0000145
146 my $include_name = $module_name;
147 $include_name =~ tr/A-Z/a-z/;
Paul Bakker9d781402011-05-09 16:17:09 +0000148
Andres AG788aa4a2016-09-14 14:32:09 +0100149 # Fix faulty ones
150 $include_name = "net_sockets" if ($module_name eq "NET");
151
Gilles Peskine4f84cc72020-05-25 12:21:22 +0200152 $included_headers{"${include_name}.h"} = $module_name;
153
Paul Bakker9d781402011-05-09 16:17:09 +0000154 my $found_ll = grep $_ eq $module_name, @low_level_modules;
155 my $found_hl = grep $_ eq $module_name, @high_level_modules;
156 if (!$found_ll && !$found_hl)
157 {
Manuel Pégourié-Gonnard48573f82015-08-06 17:24:56 +0200158 printf("Error: Do not know how to handle: $module_name\n");
Paul Bakker9d781402011-05-09 16:17:09 +0000159 exit 1;
160 }
161
162 my $code_check;
163 my $old_define;
164 my $white_space;
Paul Bakkercff68422013-09-15 20:43:33 +0200165 my $first;
Paul Bakker9d781402011-05-09 16:17:09 +0000166
167 if ($found_ll)
168 {
169 $code_check = \$ll_code_check;
170 $old_define = \$ll_old_define;
171 $white_space = ' ';
172 }
173 else
174 {
175 $code_check = \$hl_code_check;
176 $old_define = \$hl_old_define;
177 $white_space = ' ';
178 }
179
180 if ($define_name ne ${$old_define})
181 {
182 if (${$old_define} ne "")
183 {
Paul Bakkercff68422013-09-15 20:43:33 +0200184 ${$code_check} .= "#endif /* ";
185 $first = 0;
186 foreach my $dep (split(/,/, ${$old_define}))
187 {
188 ${$code_check} .= " || " if ($first++);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 ${$code_check} .= "MBEDTLS_${dep}_C";
Paul Bakkercff68422013-09-15 20:43:33 +0200190 }
191 ${$code_check} .= " */\n\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000192 }
193
Paul Bakkercff68422013-09-15 20:43:33 +0200194 ${$code_check} .= "#if ";
195 $headers .= "#if " if ($include_name ne "");
196 $first = 0;
197 foreach my $dep (split(/,/, ${define_name}))
198 {
199 ${$code_check} .= " || " if ($first);
200 $headers .= " || " if ($first++);
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
203 $headers .= "defined(MBEDTLS_${dep}_C)" if
Paul Bakkercff68422013-09-15 20:43:33 +0200204 ($include_name ne "");
205 }
206 ${$code_check} .= "\n";
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000207 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
Paul Bakker9d781402011-05-09 16:17:09 +0000208 "#endif\n\n" if ($include_name ne "");
209 ${$old_define} = $define_name;
210 }
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 if ($error_name eq "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE")
Paul Bakker3aac1da2012-05-08 13:12:27 +0000213 {
214 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
215 "${white_space}\{\n".
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n".
Paul Bakker3aac1da2012-05-08 13:12:27 +0000217 "${white_space} return;\n".
218 "${white_space}}\n"
219 }
220 else
221 {
222 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n"
Paul Bakker3aac1da2012-05-08 13:12:27 +0000224 }
Paul Bakker9d781402011-05-09 16:17:09 +0000225};
226
227if ($ll_old_define ne "")
228{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200229 $ll_code_check .= "#endif /* ";
230 my $first = 0;
231 foreach my $dep (split(/,/, $ll_old_define))
232 {
233 $ll_code_check .= " || " if ($first++);
234 $ll_code_check .= "MBEDTLS_${dep}_C";
235 }
236 $ll_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000237}
238if ($hl_old_define ne "")
239{
Manuel Pégourié-Gonnarde546ad42015-04-08 20:27:02 +0200240 $hl_code_check .= "#endif /* ";
241 my $first = 0;
242 foreach my $dep (split(/,/, $hl_old_define))
243 {
244 $hl_code_check .= " || " if ($first++);
245 $hl_code_check .= "MBEDTLS_${dep}_C";
246 }
247 $hl_code_check .= " */\n";
Paul Bakker9d781402011-05-09 16:17:09 +0000248}
249
250$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
251$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
252$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
253
254open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
255print ERROR_FILE $error_format;
256close(ERROR_FILE);
Gilles Peskine4f84cc72020-05-25 12:21:22 +0200257
258my $errors = 0;
259for my $include_name (@necessary_include_files)
260{
261 if (not $included_headers{$include_name})
262 {
263 print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n";
264 ++$errors;
265 }
266}
267
268exit !!$errors;