1#!/usr/bin/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 8use strict; 9 10my ($include_dir, $data_dir, $error_file); 11 12if( @ARGV ) { 13 die "Invalid number of arguments" if scalar @ARGV != 3; 14 ($include_dir, $data_dir, $error_file) = @ARGV; 15 16 -d $include_dir or die "No such directory: $include_dir\n"; 17 -d $data_dir or die "No such directory: $data_dir\n"; 18} else { 19 $include_dir = 'include/mbedtls'; 20 $data_dir = 'scripts/data_files'; 21 $error_file = 'library/error.c'; 22 23 unless( -d $include_dir && -d $data_dir ) { 24 chdir '..' or die; 25 -d $include_dir && -d $data_dir 26 or die "Without arguments, must be run from root or scripts\n" 27 } 28} 29 30my $error_format_file = $data_dir.'/error.fmt'; 31 32my @low_level_modules = ( "AES", "ASN1", "BLOWFISH", "CAMELLIA", "BIGNUM", 33 "BASE64", "XTEA", "PBKDF2", "OID", 34 "PADLOCK", "DES", "NET", "CTR_DRBG", "ENTROPY", 35 "HMAC_DRBG", "MD2", "MD4", "MD5", "RIPEMD160", 36 "SHA1", "SHA256", "SHA512", "GCM", "THREADING", "CCM" ); 37my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "ECP", "MD", "CIPHER", "SSL", 38 "PK", "PKCS12", "PKCS5" ); 39 40my $line_separator = $/; 41undef $/; 42 43open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!"; 44my $error_format = <FORMAT_FILE>; 45close(FORMAT_FILE); 46 47$/ = $line_separator; 48 49open(GREP, "grep \"define MBEDTLS_ERR_\" $include_dir/* |") || die("Failure when calling grep: $!"); 50 51my $ll_old_define = ""; 52my $hl_old_define = ""; 53 54my $ll_code_check = ""; 55my $hl_code_check = ""; 56 57my $headers = ""; 58 59my %error_codes_seen; 60 61while (my $line = <GREP>) 62{ 63 next if ($line =~ /compat-1.2.h/); 64 my ($error_name, $error_code) = $line =~ /(MBEDTLS_ERR_\w+)\s+\-(0x\w+)/; 65 my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//; 66 67 die "Duplicated error code: $error_code ($error_name)\n" 68 if( $error_codes_seen{$error_code}++ ); 69 70 $description =~ s/\\/\\\\/g; 71 if ($description eq "") { 72 $description = "DESCRIPTION MISSING"; 73 warn "Missing description for $error_name\n"; 74 } 75 76 my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/; 77 78 # Fix faulty ones 79 $module_name = "BIGNUM" if ($module_name eq "MPI"); 80 $module_name = "CTR_DRBG" if ($module_name eq "CTR"); 81 $module_name = "HMAC_DRBG" if ($module_name eq "HMAC"); 82 83 my $define_name = $module_name; 84 $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509"); 85 $define_name = "ASN1_PARSE" if ($define_name eq "ASN1"); 86 $define_name = "SSL_TLS" if ($define_name eq "SSL"); 87 $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM"); 88 89 my $include_name = $module_name; 90 $include_name =~ tr/A-Z/a-z/; 91 $include_name = "" if ($include_name eq "asn1"); 92 93 # Fix faulty ones 94 $include_name = "net_sockets" if ($module_name eq "NET"); 95 96 my $found_ll = grep $_ eq $module_name, @low_level_modules; 97 my $found_hl = grep $_ eq $module_name, @high_level_modules; 98 if (!$found_ll && !$found_hl) 99 { 100 printf("Error: Do not know how to handle: $module_name\n"); 101 exit 1; 102 } 103 104 my $code_check; 105 my $old_define; 106 my $white_space; 107 my $first; 108 109 if ($found_ll) 110 { 111 $code_check = \$ll_code_check; 112 $old_define = \$ll_old_define; 113 $white_space = ' '; 114 } 115 else 116 { 117 $code_check = \$hl_code_check; 118 $old_define = \$hl_old_define; 119 $white_space = ' '; 120 } 121 122 if ($define_name ne ${$old_define}) 123 { 124 if (${$old_define} ne "") 125 { 126 ${$code_check} .= "#endif /* "; 127 $first = 0; 128 foreach my $dep (split(/,/, ${$old_define})) 129 { 130 ${$code_check} .= " || " if ($first++); 131 ${$code_check} .= "MBEDTLS_${dep}_C"; 132 } 133 ${$code_check} .= " */\n\n"; 134 } 135 136 ${$code_check} .= "#if "; 137 $headers .= "#if " if ($include_name ne ""); 138 $first = 0; 139 foreach my $dep (split(/,/, ${define_name})) 140 { 141 ${$code_check} .= " || " if ($first); 142 $headers .= " || " if ($first++); 143 144 ${$code_check} .= "defined(MBEDTLS_${dep}_C)"; 145 $headers .= "defined(MBEDTLS_${dep}_C)" if 146 ($include_name ne ""); 147 } 148 ${$code_check} .= "\n"; 149 $headers .= "\n#include \"mbedtls/${include_name}.h\"\n". 150 "#endif\n\n" if ($include_name ne ""); 151 ${$old_define} = $define_name; 152 } 153 154 if ($error_name eq "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE") 155 { 156 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". 157 "${white_space}\{\n". 158 "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n". 159 "${white_space} return;\n". 160 "${white_space}}\n" 161 } 162 else 163 { 164 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n". 165 "${white_space} mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n" 166 } 167}; 168 169if ($ll_old_define ne "") 170{ 171 $ll_code_check .= "#endif /* "; 172 my $first = 0; 173 foreach my $dep (split(/,/, $ll_old_define)) 174 { 175 $ll_code_check .= " || " if ($first++); 176 $ll_code_check .= "MBEDTLS_${dep}_C"; 177 } 178 $ll_code_check .= " */\n"; 179} 180if ($hl_old_define ne "") 181{ 182 $hl_code_check .= "#endif /* "; 183 my $first = 0; 184 foreach my $dep (split(/,/, $hl_old_define)) 185 { 186 $hl_code_check .= " || " if ($first++); 187 $hl_code_check .= "MBEDTLS_${dep}_C"; 188 } 189 $hl_code_check .= " */\n"; 190} 191 192$error_format =~ s/HEADER_INCLUDED\n/$headers/g; 193$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g; 194$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g; 195 196open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!"; 197print ERROR_FILE $error_format; 198close(ERROR_FILE); 199