fipslink.pl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. print "$fips_cc $fips_cc_args $fips_libdir/fips_premain.c\n";
  27. system "$fips_cc $fips_cc_args $fips_libdir/fips_premain.c";
  28. die "First stage Compile failure" if $? != 0;
  29. print "$fips_link @ARGV\n";
  30. system "$fips_link @ARGV";
  31. die "First stage Link failure" if $? != 0;
  32. print "$fips_premain_dso $fips_target\n";
  33. $fips_hash=`$fips_premain_dso $fips_target`;
  34. chomp $fips_hash;
  35. die "Get hash failure" if $? != 0;
  36. print "$fips_cc -DHMAC_SHA1_SIG=\\\"$fips_hash\\\" $fips_cc_args $fips_libdir/fips_premain.c\n";
  37. system "$fips_cc -DHMAC_SHA1_SIG=\\\"$fips_hash\\\" $fips_cc_args $fips_libdir/fips_premain.c";
  38. die "Second stage Compile failure" if $? != 0;
  39. print "$fips_link @ARGV\n";
  40. system "$fips_link @ARGV";
  41. die "Second stage Link failure" if $? != 0;
  42. sub check_hash
  43. {
  44. my ($sha1_exe, $filename) = @_;
  45. my ($hashfile, $hashval);
  46. open(IN, "${fips_libdir}/${filename}.sha1") || die "Cannot open file hash file ${fips_libdir}/${filename}.sha1";
  47. $hashfile = <IN>;
  48. close IN;
  49. $hashval = `$sha1_exe ${fips_libdir}/$filename`;
  50. chomp $hashfile;
  51. chomp $hashval;
  52. $hashfile =~ s/^.*=\s+//;
  53. $hashval =~ s/^.*=\s+//;
  54. die "Invalid hash syntax in file" if (length($hashfile) != 40);
  55. die "Invalid hash received for file" if (length($hashval) != 40);
  56. die "***HASH VALUE MISMATCH FOR FILE $filename ***" if ($hashval ne $hashfile);
  57. }