2
0

fipslink.pl 2.1 KB

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