1#!/usr/bin/env perl 2 3# Generate error.c 4# 5# Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments, 6# or generate_errors.pl include_dir data_dir error_file 7# 8# Copyright The Mbed TLS Contributors 9# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 10 11use strict; 12use warnings; 13 14my ($include_dir, $data_dir, $error_file); 15 16if( @ARGV ) { 17 die "Invalid number of arguments" if scalar @ARGV != 3; 18 ($include_dir, $data_dir, $error_file) = @ARGV; 19 20 -d $include_dir or die "No such directory: $include_dir\n"; 21 -d $data_dir or die "No such directory: $data_dir\n"; 22} else { 23 $include_dir = 'include/mbedtls'; 24 $data_dir = 'scripts/data_files'; 25 $error_file = 'library/error.c'; 26 27 unless( -d $include_dir && -d $data_dir ) { 28 chdir '..' or die; 29 -d $include_dir && -d $data_dir 30 or die "Without arguments, must be run from root or scripts\n" 31 } 32} 33 34my $error_format_file = $data_dir.'/error.fmt'; 35 36my @low_level_modules = qw( AES ARC4 ARIA ASN1 BASE64 BIGNUM BLOWFISH 37 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES 38 ENTROPY ERROR GCM HKDF HMAC_DRBG MD2 MD4 MD5 39 NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160 40 SHA1 SHA256 SHA512 THREADING XTEA ); 41my @high_level_modules = qw( CIPHER DHM ECP MD 42 PEM PK PKCS12 PKCS5 43 RSA SSL X509 ); 44 45undef $/; 46 47open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!"; 48my $error_format = <FORMAT_FILE>; 49close(FORMAT_FILE); 50 51my @files = glob qq("$include_dir/*.h"); 52my @necessary_include_files; 53my @matches; 54foreach my $file (@files) { 55 open(FILE, '<:crlf', $file) or die("$0: $file: $!"); 56 my $content = <FILE>; 57 close FILE; 58 my $found = 0; 59 while ($content =~ m[ 60 # Both the before-comment and the after-comment are optional. 61 # Only the comment content is a regex capture group. The comment 62 # start and end parts are outside the capture group. 63 (?:/\*[*!](?!<) # Doxygen before-comment start 64 ((?:[^*]|\*+[^*/])*) # $1: Comment content (no */ inside) 65 \*/)? # Comment end 66 \s*\#\s*define\s+(MBEDTLS_ERR_\w+) # $2: name 67 \s+\-(0[Xx][0-9A-Fa-f]+)\s* # $3: value (without the sign) 68 (?:/\*[*!]< # Doxygen after-comment start 69 ((?:[^*]|\*+[^*/])*) # $4: Comment content (no */ inside) 70 \*/)? # Comment end 71 ]gsx) { 72 my ($before, $name, $value, $after) = ($1, $2, $3, $4); 73 # Discard Doxygen comments that are coincidentally present before 74 # an error definition but not attached to it. This is ad hoc, based 75 # on what actually matters (or mattered at some point). 76 undef $before if defined($before) && $before =~ /\s*\\name\s/s; 77 die "Description neither before nor after $name in $file\n" 78 if !defined($before) && !defined($after); 79 die "Description both before and after $name in $file\n" 80 if defined($before) && defined($after); 81 my $description = (defined($before) ? $before : $after); 82 $description =~ s/^\s+//; 83 $description =~ s/\n( *\*)? */ /g; 84 $description =~ s/\.?\s+$//; 85 push @matches, [$name, $value, $description]; 86 ++$found; 87 } 88 if ($found) { 89 my $include_name = $file; 90 $include_name =~ s!.*/!!; 91 push @necessary_include_files, $include_name; 92 } 93} 94 95my $ll_old_define = ""; 96my $hl_old_define = ""; 97 98my $ll_code_check = ""; 99my $hl_code_check = ""; 100 101my $headers = ""; 102my %included_headers; 103 104my %error_codes_seen; 105 106foreach my $match (@matches) 107{ 108 my ($error_name, $error_code, $description) = @$match; 109 110 die "Duplicated error code: $error_code ($error_name)\n" 111 if( $error_codes_seen{$error_code}++ ); 112 113 $description =~ s/\\/\\\\/g; 114 115 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/; 116 117 # Fix faulty ones 118 $module_name = "BIGNUM" if ($module_name eq "MPI"); 119 $module_name = "CTR_DRBG" if ($module_name eq "CTR"); 120 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC"); 121 122 my $define_name = $module_name; 123 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509"); 124 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1"); 125 $define_name = "SSL_TLS" if ($define_name eq "SSL"); 126 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM"); 127 128 my $include_name = $module_name; 129 $include_name =~ tr/A-Z/a-z/; 130 131 # Fix faulty ones 132 $include_name = "net_sockets" if ($module_name eq "NET"); 133 134 $included_headers{"${include_name}.h"} = $module_name; 135 136 my $found_ll = grep $_ eq $module_name, @low_level_modules; 137 my $found_hl = grep $_ eq $module_name, @high_level_modules; 138 if (!$found_ll && !$found_hl) 139 { 140 printf("Error: Do not know how to handle: $module_name\n"); 141 exit 1; 142 } 143 144 my $code_check; 145 my $old_define; 146 my $white_space; 147 my $first; 148 149 if ($found_ll) 150 { 151 $code_check = \$ll_code_check; 152 $old_define = \$ll_old_define; 153 $white_space = ' '; 154 } 155 else 156 { 157 $code_check = \$hl_code_check; 158 $old_define = \$hl_old_define; 159 $white_space = ' '; 160 } 161 162 if ($define_name ne ${$old_define}) 163 { 164 if (${$old_define} ne "") 165 { 166 ${$code_check} .= "#endif /* "; 167 $first = 0; 168 foreach my $dep (split(/,/, ${$old_define})) 169 { 170 ${$code_check} .= " || " if ($first++); 171 ${$code_check} .= "MBEDTLS_${dep}_C"; 172 } 173 ${$code_check} .= " */\n\n"; 174 } 175 176 ${$code_check} .= "#if "; 177 $headers .= "#if " if ($include_name ne ""); 178 $first = 0; 179 foreach my $dep (split(/,/, ${define_name})) 180 { 181 ${$code_check} .= " || " if ($first); 182 $headers .= " || " if ($first++); 183 184 ${$code_check} .= "defined(MBEDTLS_${dep}_C)"; 185 $headers .= "defined(MBEDTLS_${dep}_C)" if 186 ($include_name ne ""); 187 } 188 ${$code_check} .= "\n"; 189 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n". 190 "#endif\n\n" if ($include_name ne ""); 191 ${$old_define} = $define_name; 192 } 193 194 ${$code_check} .= "${white_space}case -($error_name):\n". 195 "${white_space} return( \"$module_name - $description\" );\n" 196}; 197 198if ($ll_old_define ne "") 199{ 200 $ll_code_check .= "#endif /* "; 201 my $first = 0; 202 foreach my $dep (split(/,/, $ll_old_define)) 203 { 204 $ll_code_check .= " || " if ($first++); 205 $ll_code_check .= "MBEDTLS_${dep}_C"; 206 } 207 $ll_code_check .= " */\n"; 208} 209if ($hl_old_define ne "") 210{ 211 $hl_code_check .= "#endif /* "; 212 my $first = 0; 213 foreach my $dep (split(/,/, $hl_old_define)) 214 { 215 $hl_code_check .= " || " if ($first++); 216 $hl_code_check .= "MBEDTLS_${dep}_C"; 217 } 218 $hl_code_check .= " */\n"; 219} 220 221$error_format =~ s/HEADER_INCLUDED\n/$headers/g; 222$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g; 223$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g; 224 225open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!"; 226print ERROR_FILE $error_format; 227close(ERROR_FILE); 228 229my $errors = 0; 230for my $include_name (@necessary_include_files) 231{ 232 if (not $included_headers{$include_name}) 233 { 234 print STDERR "The header file \"$include_name\" defines error codes but has not been included!\n"; 235 ++$errors; 236 } 237} 238 239exit !!$errors; 240