2
0

fipslink.pl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. my $fips_sig = $ENV{"FIPS_SIG"};
  24. if (defined $fips_sig)
  25. {
  26. if ($fips_premain_dso ne "")
  27. {
  28. $fips_premain_dso = "$fips_sig -dso";
  29. }
  30. else
  31. {
  32. $fips_premain_dso = "$fips_sig -exe";
  33. }
  34. }
  35. check_hash($sha1_exe, "fips_premain.c");
  36. check_hash($sha1_exe, "fipscanister.lib");
  37. print "Integrity check OK\n";
  38. print "$fips_cc $fips_cc_args $fips_libdir/fips_premain.c\n";
  39. system "$fips_cc $fips_cc_args $fips_libdir/fips_premain.c";
  40. die "First stage Compile failure" if $? != 0;
  41. print "$fips_link @ARGV\n";
  42. system "$fips_link @ARGV";
  43. die "First stage Link failure" if $? != 0;
  44. print "$fips_premain_dso $fips_target\n";
  45. system("$fips_premain_dso $fips_target >$fips_target.sha1");
  46. die "Get hash failure" if $? != 0;
  47. open my $sha1_res, '<', $fips_target.".sha1" or die "Get hash failure";
  48. $fips_hash=<$sha1_res>;
  49. close $sha1_res;
  50. unlink $fips_target.".sha1";
  51. chomp $fips_hash;
  52. die "Get hash failure" if $? != 0;
  53. print "$fips_cc -DHMAC_SHA1_SIG=\\\"$fips_hash\\\" $fips_cc_args $fips_libdir/fips_premain.c\n";
  54. system "$fips_cc -DHMAC_SHA1_SIG=\\\"$fips_hash\\\" $fips_cc_args $fips_libdir/fips_premain.c";
  55. die "Second stage Compile failure" if $? != 0;
  56. print "$fips_link @ARGV\n";
  57. system "$fips_link @ARGV";
  58. die "Second stage Link failure" if $? != 0;
  59. sub check_hash
  60. {
  61. my ($sha1_exe, $filename) = @_;
  62. my ($hashfile, $hashval);
  63. open(IN, "${fips_libdir}/${filename}.sha1") || die "Cannot open file hash file ${fips_libdir}/${filename}.sha1";
  64. $hashfile = <IN>;
  65. close IN;
  66. $hashval = `$sha1_exe ${fips_libdir}/$filename`;
  67. chomp $hashfile;
  68. chomp $hashval;
  69. $hashfile =~ s/^.*=\s+//;
  70. $hashval =~ s/^.*=\s+//;
  71. die "Invalid hash syntax in file" if (length($hashfile) != 40);
  72. die "Invalid hash received for file" if (length($hashval) != 40);
  73. die "***HASH VALUE MISMATCH FOR FILE $filename ***" if ($hashval ne $hashfile);
  74. }