File strip_authinfo.pl of Package ovmf

#!/usr/bin/perl

use strict;
use FileHandle;

if ($#ARGV != 1) {
	print "Usage: stripe_authinfo <variable with AuthInfo> <stripped binary>\n";
	exit;
}

my $file_in = $ARGV[0];
my $file_out = $ARGV[1];

sub read_file($)
{
	my ($file) = @_;
	my $contents;
	my $len;

	open(FD, "<$file") || die $file;
	binmode FD;
	my @st = stat(FD);
	die $file if (!@st);
	$len = read(FD, $contents, $st[7]) || die $file;
	close(FD) || die $file;
	die "$file: Wanted length ", $st[7], ", got ", $len, "\n"
		if ($len != $st[7]);
	return $contents;
}

my $authvar = read_file($file_in);
my $authvar_len = length($authvar);

# Skip the first 16 bytes (EFI_TIME) and check the following 8 bytes
#
# WIN_CERTIFICATE (8 bytes)
#	UINT32	dwLength
#	UINT16	wRevision 		0x0200
#	UINT16	wCertificateType	0x0EF0 to 0x0EFF
my($dwLength, $wRevision, $wCertificateType) = unpack("VSS", substr($authvar, 16, 8));

# check the contents
die "invalid certificate length" if ($dwLength > $authvar_len);
die "invalid Revision" if ($wRevision != 0x200);
die "invalid certificate type"
	if ($wCertificateType != 0x0EF0 && $wCertificateType != 0x0EF1 && $wCertificateType != 0x0002);

my $skip = $dwLength + 16;

open(FD, ">$file_out") || die $file_out;
binmode FD;
print FD substr($authvar, $skip, $authvar_len - $skip);
close FD || die $file_out;
openSUSE Build Service is sponsored by