generate_errors.pl 6.1 KB

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