Back to home page

LXR

 
 

    


0001 #!/usr/bin/perl -w
0002 #
0003 # extract-mod-sig <part> <module-file>
0004 #
0005 # Reads the module file and writes out some or all of the signature
0006 # section to stdout.  Part is the bit to be written and is one of:
0007 #
0008 #  -0: The unsigned module, no signature data at all
0009 #  -a: All of the signature data, including magic number
0010 #  -d: Just the descriptor values as a sequence of numbers
0011 #  -n: Just the signer's name
0012 #  -k: Just the key ID
0013 #  -s: Just the crypto signature or PKCS#7 message
0014 #
0015 use strict;
0016 
0017 die "Format: $0 -[0adnks] module-file >out\n"
0018     if ($#ARGV != 1);
0019 
0020 my $part = $ARGV[0];
0021 my $modfile = $ARGV[1];
0022 
0023 my $magic_number = "~Module signature appended~\n";
0024 
0025 #
0026 # Read the module contents
0027 #
0028 open FD, "<$modfile" || die $modfile;
0029 binmode(FD);
0030 my @st = stat(FD);
0031 die "$modfile" unless (@st);
0032 my $buf = "";
0033 my $len = sysread(FD, $buf, $st[7]);
0034 die "$modfile" unless (defined($len));
0035 die "Short read on $modfile\n" unless ($len == $st[7]);
0036 close(FD) || die $modfile;
0037 
0038 print STDERR "Read ", $len, " bytes from module file\n";
0039 
0040 die "The file is too short to have a sig magic number and descriptor\n"
0041     if ($len < 12 + length($magic_number));
0042 
0043 #
0044 # Check for the magic number and extract the information block
0045 #
0046 my $p = $len - length($magic_number);
0047 my $raw_magic = substr($buf, $p);
0048 
0049 die "Magic number not found at $len\n"
0050     if ($raw_magic ne $magic_number);
0051 print STDERR "Found magic number at $len\n";
0052 
0053 $p -= 12;
0054 my $raw_info = substr($buf, $p, 12);
0055 
0056 my @info = unpack("CCCCCxxxN", $raw_info);
0057 my ($algo, $hash, $id_type, $name_len, $kid_len, $sig_len) = @info;
0058 
0059 if ($id_type == 0) {
0060     print STDERR "Found PGP key identifier\n";
0061 } elsif ($id_type == 1) {
0062     print STDERR "Found X.509 cert identifier\n";
0063 } elsif ($id_type == 2) {
0064     print STDERR "Found PKCS#7/CMS encapsulation\n";
0065 } else {
0066     print STDERR "Found unsupported identifier type $id_type\n";
0067 }
0068 
0069 #
0070 # Extract the three pieces of info data
0071 #
0072 die "Insufficient name+kid+sig data in file\n"
0073     unless ($p >= $name_len + $kid_len + $sig_len);
0074 
0075 $p -= $sig_len;
0076 my $raw_sig = substr($buf, $p, $sig_len);
0077 $p -= $kid_len;
0078 my $raw_kid = substr($buf, $p, $kid_len);
0079 $p -= $name_len;
0080 my $raw_name = substr($buf, $p, $name_len);
0081 
0082 my $module_len = $p;
0083 
0084 if ($sig_len > 0) {
0085     print STDERR "Found $sig_len bytes of signature [";
0086     my $n = $sig_len > 16 ? 16 : $sig_len;
0087     foreach my $i (unpack("C" x $n, substr($raw_sig, 0, $n))) {
0088     printf STDERR "%02x", $i;
0089     }
0090     print STDERR "]\n";
0091 }
0092 
0093 if ($kid_len > 0) {
0094     print STDERR "Found $kid_len bytes of key identifier [";
0095     my $n = $kid_len > 16 ? 16 : $kid_len;
0096     foreach my $i (unpack("C" x $n, substr($raw_kid, 0, $n))) {
0097     printf STDERR "%02x", $i;
0098     }
0099     print STDERR "]\n";
0100 }
0101 
0102 if ($name_len > 0) {
0103     print STDERR "Found $name_len bytes of signer's name [$raw_name]\n";
0104 }
0105 
0106 #
0107 # Produce the requested output
0108 #
0109 if ($part eq "-0") {
0110     # The unsigned module, no signature data at all
0111     binmode(STDOUT);
0112     print substr($buf, 0, $module_len);
0113 } elsif ($part eq "-a") {
0114     # All of the signature data, including magic number
0115     binmode(STDOUT);
0116     print substr($buf, $module_len);
0117 } elsif ($part eq "-d") {
0118     # Just the descriptor values as a sequence of numbers
0119     print join(" ", @info), "\n";
0120 } elsif ($part eq "-n") {
0121     # Just the signer's name
0122     print STDERR "No signer's name for PKCS#7 message type sig\n"
0123     if ($id_type == 2);
0124     binmode(STDOUT);
0125     print $raw_name;
0126 } elsif ($part eq "-k") {
0127     # Just the key identifier
0128     print STDERR "No key ID for PKCS#7 message type sig\n"
0129     if ($id_type == 2);
0130     binmode(STDOUT);
0131     print $raw_kid;
0132 } elsif ($part eq "-s") {
0133     # Just the crypto signature or PKCS#7 message
0134     binmode(STDOUT);
0135     print $raw_sig;
0136 }