^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/usr/bin/env perl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) # extract-mod-sig <part> <module-file>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) # Reads the module file and writes out some or all of the signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) # section to stdout. Part is the bit to be written and is one of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) # -0: The unsigned module, no signature data at all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) # -a: All of the signature data, including magic number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # -d: Just the descriptor values as a sequence of numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # -n: Just the signer's name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) # -k: Just the key ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # -s: Just the crypto signature or PKCS#7 message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) use warnings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) use strict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) die "Format: $0 -[0adnks] module-file >out\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if ($#ARGV != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) my $part = $ARGV[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) my $modfile = $ARGV[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) my $magic_number = "~Module signature appended~\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) # Read the module contents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) open FD, "<$modfile" || die $modfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) binmode(FD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) my @st = stat(FD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) die "$modfile" unless (@st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) my $buf = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) my $len = sysread(FD, $buf, $st[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) die "$modfile" unless (defined($len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) die "Short read on $modfile\n" unless ($len == $st[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) close(FD) || die $modfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) print STDERR "Read ", $len, " bytes from module file\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) die "The file is too short to have a sig magic number and descriptor\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if ($len < 12 + length($magic_number));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) # Check for the magic number and extract the information block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) my $p = $len - length($magic_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) my $raw_magic = substr($buf, $p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) die "Magic number not found at $len\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if ($raw_magic ne $magic_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) print STDERR "Found magic number at $len\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) $p -= 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) my $raw_info = substr($buf, $p, 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) my @info = unpack("CCCCCxxxN", $raw_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) my ($algo, $hash, $id_type, $name_len, $kid_len, $sig_len) = @info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if ($id_type == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) print STDERR "Found PGP key identifier\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) } elsif ($id_type == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) print STDERR "Found X.509 cert identifier\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) } elsif ($id_type == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) print STDERR "Found PKCS#7/CMS encapsulation\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) print STDERR "Found unsupported identifier type $id_type\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) # Extract the three pieces of info data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) die "Insufficient name+kid+sig data in file\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unless ($p >= $name_len + $kid_len + $sig_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) $p -= $sig_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) my $raw_sig = substr($buf, $p, $sig_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) $p -= $kid_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) my $raw_kid = substr($buf, $p, $kid_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) $p -= $name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) my $raw_name = substr($buf, $p, $name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) my $module_len = $p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if ($sig_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) print STDERR "Found $sig_len bytes of signature [";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) my $n = $sig_len > 16 ? 16 : $sig_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) foreach my $i (unpack("C" x $n, substr($raw_sig, 0, $n))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) printf STDERR "%02x", $i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) print STDERR "]\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if ($kid_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) print STDERR "Found $kid_len bytes of key identifier [";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) my $n = $kid_len > 16 ? 16 : $kid_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) foreach my $i (unpack("C" x $n, substr($raw_kid, 0, $n))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) printf STDERR "%02x", $i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) print STDERR "]\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if ($name_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) print STDERR "Found $name_len bytes of signer's name [$raw_name]\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) # Produce the requested output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if ($part eq "-0") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) # The unsigned module, no signature data at all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) binmode(STDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) print substr($buf, 0, $module_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) } elsif ($part eq "-a") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) # All of the signature data, including magic number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) binmode(STDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) print substr($buf, $module_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) } elsif ($part eq "-d") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) # Just the descriptor values as a sequence of numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) print join(" ", @info), "\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) } elsif ($part eq "-n") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) # Just the signer's name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) print STDERR "No signer's name for PKCS#7 message type sig\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if ($id_type == 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) binmode(STDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) print $raw_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) } elsif ($part eq "-k") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) # Just the key identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) print STDERR "No key ID for PKCS#7 message type sig\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if ($id_type == 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) binmode(STDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) print $raw_kid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) } elsif ($part eq "-s") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) # Just the crypto signature or PKCS#7 message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) binmode(STDOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) print $raw_sig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }