fipslink.pl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/perl
  2. sub check_env
  3. {
  4. my @ret;
  5. foreach (@_)
  6. {
  7. die "Environment variable $_ not defined!\n" unless exists $ENV{$_};
  8. push @ret, $ENV{$_};
  9. }
  10. return @ret;
  11. }
  12. my ($fips_cc,$fips_cc_args, $fips_link,$fips_target, $fips_libdir, $sha1_exe)
  13. = check_env("FIPS_CC", "FIPS_CC_ARGS", "FIPS_LINK", "FIPS_TARGET",
  14. "FIPSLIB_D", "FIPS_SHA1_EXE");
  15. if (exists $ENV{"PREMAIN_DSO_EXE"})
  16. {
  17. $fips_premain_dso = $ENV{"PREMAIN_DSO_EXE"};
  18. }
  19. else
  20. {
  21. $fips_premain_dso = "";
  22. }
  23. check_hash($sha1_exe, "fips_premain.c");
  24. check_hash($sha1_exe, "fipscanister.lib");
  25. print "Integrity check OK\n";
  26. if (is_premain_linked(@ARGV)) {
  27. print "$fips_cc $fips_cc_args $fips_libdir/fips_premain.c\n";
  28. system "$fips_cc $fips_cc_args $fips_libdir/fips_premain.c";
  29. die "First stage Compile failure" if $? != 0;
  30. } elsif (!defined($ENV{FIPS_SIG})) {
  31. die "no fips_premain.obj linked";
  32. }
  33. print "$fips_link @ARGV\n";
  34. system "$fips_link @ARGV";
  35. die "First stage Link failure" if $? != 0;
  36. if (defined($ENV{FIPS_SIG})) {
  37. print "$ENV{FIPS_SIG} $fips_target\n";
  38. system "$ENV{FIPS_SIG} $fips_target";
  39. die "$ENV{FIPS_SIG} $fips_target failed" if $? != 0;
  40. exit;
  41. }
  42. print "$fips_premain_dso $fips_target\n";
  43. system("$fips_premain_dso $fips_target >$fips_target.sha1");
  44. die "Get hash failure" if $? != 0;
  45. open my $sha1_res, '<', $fips_target.".sha1" or die "Get hash failure";
  46. $fips_hash=<$sha1_res>;
  47. close $sha1_res;
  48. unlink $fips_target.".sha1";
  49. chomp $fips_hash;
  50. die "Get hash failure" if $? != 0;
  51. print "$fips_cc -DHMAC_SHA1_SIG=\\\"$fips_hash\\\" $fips_cc_args $fips_libdir/fips_premain.c\n";
  52. system "$fips_cc -DHMAC_SHA1_SIG=\\\"$fips_hash\\\" $fips_cc_args $fips_libdir/fips_premain.c";
  53. die "Second stage Compile failure" if $? != 0;
  54. print "$fips_link @ARGV\n";
  55. system "$fips_link @ARGV";
  56. die "Second stage Link failure" if $? != 0;
  57. sub is_premain_linked
  58. {
  59. return 1 if (grep /fips_premain\.obj/,@_);
  60. foreach (@_)
  61. {
  62. if (/^@(.*)/ && -f $1)
  63. {
  64. open FD,$1 or die "can't open $1";
  65. my $ret = (grep /fips_premain\.obj/,<FD>)?1:0;
  66. close FD;
  67. return $ret;
  68. }
  69. }
  70. return 0;
  71. }
  72. sub check_hash
  73. {
  74. my ($sha1_exe, $filename) = @_;
  75. my ($hashfile, $hashval);
  76. open(IN, "${fips_libdir}/${filename}.sha1") || die "Cannot open file hash file ${fips_libdir}/${filename}.sha1";
  77. $hashfile = <IN>;
  78. close IN;
  79. $hashval = `$sha1_exe ${fips_libdir}/$filename`;
  80. chomp $hashfile;
  81. chomp $hashval;
  82. $hashfile =~ s/^.*=\s+//;
  83. $hashval =~ s/^.*=\s+//;
  84. die "Invalid hash syntax in file" if (length($hashfile) != 40);
  85. die "Invalid hash received for file" if (length($hashval) != 40);
  86. die "***HASH VALUE MISMATCH FOR FILE $filename ***" if ($hashval ne $hashfile);
  87. }