+sub checktagnoreplay () {
+ # We check that the signed tag mentions the name and value of
+ # (a) in the case of FRESHREPO all tags in the repo;
+ # (b) in the case of just NOFFCHECK all tags referring to
+ # the current head for the suite (there must be at least one).
+ # This prevents a replay attack using an earlier signed tag.
+ return unless $policy & (FRESHREPO|NOFFCHECK);
+
+ my $garbagerepo = "$dgitrepos/${package}_garbage";
+ lockrealtree();
+
+ local $ENV{GIT_DIR};
+ foreach my $garb ("$garbagerepo", "$garbagerepo-old") {
+ if (stat_exists $garb) {
+ $ENV{GIT_DIR} = $garb;
+ last;
+ }
+ }
+ if (!defined $ENV{GIT_DIR}) {
+ # Nothing to overwrite so the FRESHREPO and NOFFCHECK were
+ # pointless. Oh well.
+ debug "checktagnoreplay - no garbage, ok";
+ return;
+ }
+
+ my $onlyreferring;
+ if (!($policy & FRESHREPO)) {
+ my $branch = server_branch($suite);
+ $!=0; $?=0; $_ =
+ `git for-each-ref --format='%(objectname)' '[r]efs/$branch'`;
+ defined or die "$branch $? $!";
+ $? and die "$branch $?";
+ if (!length) {
+ # No such branch - NOFFCHECK was unnecessary. Oh well.
+ debug "checktagnoreplay - not FRESHREPO, new branch, ok";
+ return;
+ }
+ m/^(\w+)\n$/ or die "$branch $_ ?";
+ $onlyreferring = $1;
+ debug "checktagnoreplay - not FRESHREPO,".
+ " checking for overwriting refs/$branch=$onlyreferring";
+ }
+
+ my @problems;
+
+ git_for_each_tag_referring($objreferring, sub {
+ my ($objid,$fullrefname,$tagname) = @_;
+ debug "checktagnoreplay - overwriting $fullrefname=$objid";
+ my $supers = $supersedes{$fullrefname};
+ if (!defined $supers) {
+ push @problems, "does not supersede $fullrefname";
+ } elsif ($supers ne $objid) {
+ push @problems,
+ "supersedes $fullrefname=$supers but previously $fullrefname=$objid";
+ } else {
+ # ok;
+ }
+ });
+
+ if (@problems) {
+ reject "replay attack prevention check failed:".
+ " signed tag for $version: ".
+ join("; ", @problems).
+ "\n";
+ }
+ debug "checktagnoreply - all ok"
+}
+