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