aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorMichal Marek <mmarek@suse.cz>2013-01-24 22:11:19 -0500
committerRusty Russell <rusty@rustcorp.com.au>2013-01-25 01:25:36 -0500
commit1c37c054a7493e0537ea3d15a59dac3a0aa63a05 (patch)
treef0a07f73ed8707c9843ea5f57a9366608e35af6e /scripts
parent4bc9410c0cf5079219bdfa3295d83dfacefe1bb2 (diff)
MODSIGN: Add -s <signature> option to sign-file
This option allows to append an externally computed singature to the module. This is needed in setups, where the private key is not directly available, but a service exists that returns signatures for given files. Signed-off-by: Michal Marek <mmarek@suse.cz> Acked-by: David Howells <dhowells@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/sign-file99
1 files changed, 56 insertions, 43 deletions
diff --git a/scripts/sign-file b/scripts/sign-file
index 2c2bbd18ff44..2b7c4484d46c 100755
--- a/scripts/sign-file
+++ b/scripts/sign-file
@@ -2,31 +2,41 @@
2# 2#
3# Sign a module file using the given key. 3# Sign a module file using the given key.
4# 4#
5# Format: 5
6# 6my $USAGE =
7# ./scripts/sign-file [-v] <hash algo> <key> <x509> <module> [<dest>] 7"Usage: scripts/sign-file [-v] <hash algo> <key> <x509> <module> [<dest>]\n" .
8# 8" scripts/sign-file [-v] -s <raw sig> <hash algo> <x509> <module> [<dest>]\n";
9# 9
10use strict; 10use strict;
11use FileHandle; 11use FileHandle;
12use IPC::Open2; 12use IPC::Open2;
13use Getopt::Std;
13 14
14my $verbose = 0; 15my %opts;
15if ($#ARGV >= 0 && $ARGV[0] eq "-v") { 16getopts('vs:', \%opts) or die $USAGE;
16 $verbose = 1; 17my $verbose = $opts{'v'};
17 shift; 18my $signature_file = $opts{'s'};
18}
19 19
20die "Format: ./scripts/sign-file [-v] <hash algo> <key> <x509> <module> [<dest>]\n" 20die $USAGE if ($#ARGV > 4);
21 if ($#ARGV != 3 && $#ARGV != 4); 21die $USAGE if (!$signature_file && $#ARGV < 3 || $signature_file && $#ARGV < 2);
22 22
23my $dgst = $ARGV[0]; 23my $dgst = shift @ARGV;
24my $private_key = $ARGV[1]; 24my $private_key;
25my $x509 = $ARGV[2]; 25if (!$signature_file) {
26my $module = $ARGV[3]; 26 $private_key = shift @ARGV;
27my $dest = ($#ARGV == 4) ? $ARGV[4] : $ARGV[3] . "~"; 27}
28my $x509 = shift @ARGV;
29my $module = shift @ARGV;
30my ($dest, $keep_orig);
31if (@ARGV) {
32 $dest = $ARGV[0];
33 $keep_orig = 1;
34} else {
35 $dest = $module . "~";
36}
28 37
29die "Can't read private key\n" unless (-r $private_key); 38die "Can't read private key\n" if (!$signature_file && !-r $private_key);
39die "Can't read signature file\n" if ($signature_file && !-r $signature_file);
30die "Can't read X.509 certificate\n" unless (-r $x509); 40die "Can't read X.509 certificate\n" unless (-r $x509);
31die "Can't read module\n" unless (-r $module); 41die "Can't read module\n" unless (-r $module);
32 42
@@ -340,33 +350,36 @@ if ($dgst eq "sha1") {
340 die "Unknown hash algorithm: $dgst\n"; 350 die "Unknown hash algorithm: $dgst\n";
341} 351}
342 352
343#
344# Generate the digest and read from openssl's stdout
345#
346my $digest;
347$digest = readpipe("openssl dgst -$dgst -binary $module") || die "openssl dgst";
348
349#
350# Generate the binary signature, which will be just the integer that comprises
351# the signature with no metadata attached.
352#
353my $pid;
354$pid = open2(*read_from, *write_to,
355 "openssl rsautl -sign -inkey $private_key -keyform PEM") ||
356 die "openssl rsautl";
357binmode write_to;
358print write_to $prologue . $digest || die "pipe to openssl rsautl";
359close(write_to) || die "pipe to openssl rsautl";
360
361binmode read_from;
362my $signature; 353my $signature;
363read(read_from, $signature, 4096) || die "pipe from openssl rsautl"; 354if ($signature_file) {
364close(read_from) || die "pipe from openssl rsautl"; 355 $signature = read_file($signature_file);
356} else {
357 #
358 # Generate the digest and read from openssl's stdout
359 #
360 my $digest;
361 $digest = readpipe("openssl dgst -$dgst -binary $module") || die "openssl dgst";
362
363 #
364 # Generate the binary signature, which will be just the integer that
365 # comprises the signature with no metadata attached.
366 #
367 my $pid;
368 $pid = open2(*read_from, *write_to,
369 "openssl rsautl -sign -inkey $private_key -keyform PEM") ||
370 die "openssl rsautl";
371 binmode write_to;
372 print write_to $prologue . $digest || die "pipe to openssl rsautl";
373 close(write_to) || die "pipe to openssl rsautl";
374
375 binmode read_from;
376 read(read_from, $signature, 4096) || die "pipe from openssl rsautl";
377 close(read_from) || die "pipe from openssl rsautl";
378 waitpid($pid, 0) || die;
379 die "openssl rsautl died: $?" if ($? >> 8);
380}
365$signature = pack("n", length($signature)) . $signature, 381$signature = pack("n", length($signature)) . $signature,
366 382
367waitpid($pid, 0) || die;
368die "openssl rsautl died: $?" if ($? >> 8);
369
370# 383#
371# Build the signed binary 384# Build the signed binary
372# 385#
@@ -403,6 +416,6 @@ print FD
403 ; 416 ;
404close FD || die $dest; 417close FD || die $dest;
405 418
406if ($#ARGV != 3) { 419if (!$keep_orig) {
407 rename($dest, $module) || die $module; 420 rename($dest, $module) || die $module;
408} 421}