+sub checktagnoreplay () {
+ # We check that the signed tag mentions the name and tag object id 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.
+ printdebug "checktagnoreplay - no garbage, ok\n";
+ 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.
+ printdebug "checktagnoreplay - not FRESHREPO, new branch, ok\n";
+ return;
+ }
+ m/^(\w+)\n$/ or die "$branch $_ ?";
+ $onlyreferring = $1;
+ printdebug "checktagnoreplay - not FRESHREPO,".
+ " checking for overwriting refs/$branch=$onlyreferring\n";
+ }
+
+ my @problems;
+
+ git_for_each_tag_referring($onlyreferring, sub {
+ my ($tagobjid,$refobjid,$fullrefname,$tagname) = @_;
+ printdebug "checktagnoreplay - overwriting".
+ " $fullrefname=$tagobjid->$refobjid\n";
+ my $supers = $supersedes{$fullrefname};
+ if (!defined $supers) {
+ printdebug "checktagnoreply - fallbacks\n";
+ my $super_fallback = 0;
+ foreach my $didsuper (sort keys %supersedes) {
+ my $didsuper_tagobjid = $supersedes{$didsuper};
+ my $didsuper_refobjid = git_rev_parse $didsuper_tagobjid;
+ printdebug "checktagnoreply - fallback".
+ " $didsuper=$didsuper_refobjid->$didsuper_tagobjid\n";
+ last if
+ $refobjid ne $didsuper_refobjid
+ and is_fast_fwd($refobjid, $didsuper_refobjid);
+ printdebug "checktagnoreply - fallback $didsuper OK\n";
+ $super_fallback = 1;
+ }
+ push @problems, "does not supersede $fullrefname"
+ unless $super_fallback;
+ } elsif ($supers ne $tagobjid) {
+ push @problems,
+ "supersedes $fullrefname=$supers but previously $fullrefname=$tagobjid";
+ } else {
+ # ok;
+ }
+ });
+
+ if (@problems) {
+ reject "replay attack prevention check failed:".
+ " signed tag for $version: ".
+ join("; ", @problems).
+ "\n";
+ }
+ printdebug "checktagnoreply - all ok\n"
+}
+