generate_errors.pl 6.2 KB

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