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