3 # extract-mod-sig <part> <module-file>
5 # Reads the module file and writes out some or all of the signature
6 # section to stdout. Part is the bit to be written and is one of:
8 # -0: The unsigned module, no signature data at all
9 # -a: All of the signature data, including magic number
10 # -d: Just the descriptor values as a sequence of numbers
11 # -n: Just the signer's name
13 # -s: Just the crypto signature or PKCS#7 message
18 die "Format: $0 -[0adnks] module-file >out\n"
22 my $modfile = $ARGV[1];
24 my $magic_number = "~Module signature appended~\n";
27 # Read the module contents
29 open FD
, "<$modfile" || die $modfile;
32 die "$modfile" unless (@st);
34 my $len = sysread(FD
, $buf, $st[7]);
35 die "$modfile" unless (defined($len));
36 die "Short read on $modfile\n" unless ($len == $st[7]);
37 close(FD
) || die $modfile;
39 print STDERR
"Read ", $len, " bytes from module file\n";
41 die "The file is too short to have a sig magic number and descriptor\n"
42 if ($len < 12 + length($magic_number));
45 # Check for the magic number and extract the information block
47 my $p = $len - length($magic_number);
48 my $raw_magic = substr($buf, $p);
50 die "Magic number not found at $len\n"
51 if ($raw_magic ne $magic_number);
52 print STDERR
"Found magic number at $len\n";
55 my $raw_info = substr($buf, $p, 12);
57 my @info = unpack("CCCCCxxxN", $raw_info);
58 my ($algo, $hash, $id_type, $name_len, $kid_len, $sig_len) = @info;
61 print STDERR
"Found PGP key identifier\n";
62 } elsif ($id_type == 1) {
63 print STDERR
"Found X.509 cert identifier\n";
64 } elsif ($id_type == 2) {
65 print STDERR
"Found PKCS#7/CMS encapsulation\n";
67 print STDERR
"Found unsupported identifier type $id_type\n";
71 # Extract the three pieces of info data
73 die "Insufficient name+kid+sig data in file\n"
74 unless ($p >= $name_len + $kid_len + $sig_len);
77 my $raw_sig = substr($buf, $p, $sig_len);
79 my $raw_kid = substr($buf, $p, $kid_len);
81 my $raw_name = substr($buf, $p, $name_len);
86 print STDERR
"Found $sig_len bytes of signature [";
87 my $n = $sig_len > 16 ?
16 : $sig_len;
88 foreach my $i (unpack("C" x
$n, substr($raw_sig, 0, $n))) {
89 printf STDERR
"%02x", $i;
95 print STDERR
"Found $kid_len bytes of key identifier [";
96 my $n = $kid_len > 16 ?
16 : $kid_len;
97 foreach my $i (unpack("C" x
$n, substr($raw_kid, 0, $n))) {
98 printf STDERR
"%02x", $i;
104 print STDERR
"Found $name_len bytes of signer's name [$raw_name]\n";
108 # Produce the requested output
111 # The unsigned module, no signature data at all
113 print substr($buf, 0, $module_len);
114 } elsif ($part eq "-a") {
115 # All of the signature data, including magic number
117 print substr($buf, $module_len);
118 } elsif ($part eq "-d") {
119 # Just the descriptor values as a sequence of numbers
120 print join(" ", @info), "\n";
121 } elsif ($part eq "-n") {
122 # Just the signer's name
123 print STDERR
"No signer's name for PKCS#7 message type sig\n"
127 } elsif ($part eq "-k") {
128 # Just the key identifier
129 print STDERR
"No key ID for PKCS#7 message type sig\n"
133 } elsif ($part eq "-s") {
134 # Just the crypto signature or PKCS#7 message