blob: 2c2bbd18ff44004638682dd37fc0b3f14a5cefe5 [file] [log] [blame]
David Howellsb37d1bf2012-10-19 23:56:37 +01001#!/usr/bin/perl -w
David Howells80d65e52012-09-26 10:11:06 +01002#
3# Sign a module file using the given key.
4#
David Howellsb37d1bf2012-10-19 23:56:37 +01005# Format:
David Howells80d65e52012-09-26 10:11:06 +01006#
Michal Marek4bc94102013-01-25 13:41:12 +10307# ./scripts/sign-file [-v] <hash algo> <key> <x509> <module> [<dest>]
David Howellsb37d1bf2012-10-19 23:56:37 +01008#
9#
10use strict;
11use FileHandle;
12use IPC::Open2;
David Howells80d65e52012-09-26 10:11:06 +010013
David Howellsb37d1bf2012-10-19 23:56:37 +010014my $verbose = 0;
15if ($#ARGV >= 0 && $ARGV[0] eq "-v") {
16 $verbose = 1;
17 shift;
18}
David Howells80d65e52012-09-26 10:11:06 +010019
Michal Marek4bc94102013-01-25 13:41:12 +103020die "Format: ./scripts/sign-file [-v] <hash algo> <key> <x509> <module> [<dest>]\n"
21 if ($#ARGV != 3 && $#ARGV != 4);
David Howells80d65e52012-09-26 10:11:06 +010022
Michal Marek4bc94102013-01-25 13:41:12 +103023my $dgst = $ARGV[0];
24my $private_key = $ARGV[1];
25my $x509 = $ARGV[2];
26my $module = $ARGV[3];
27my $dest = ($#ARGV == 4) ? $ARGV[4] : $ARGV[3] . "~";
David Howells80d65e52012-09-26 10:11:06 +010028
David Howellsb37d1bf2012-10-19 23:56:37 +010029die "Can't read private key\n" unless (-r $private_key);
30die "Can't read X.509 certificate\n" unless (-r $x509);
31die "Can't read module\n" unless (-r $module);
David Howells80d65e52012-09-26 10:11:06 +010032
David Howellsb37d1bf2012-10-19 23:56:37 +010033#
David Howellsb37d1bf2012-10-19 23:56:37 +010034# Function to read the contents of a file into a variable.
35#
36sub read_file($)
37{
38 my ($file) = @_;
39 my $contents;
40 my $len;
41
42 open(FD, "<$file") || die $file;
43 binmode FD;
44 my @st = stat(FD);
45 die $file if (!@st);
46 $len = read(FD, $contents, $st[7]) || die $file;
47 close(FD) || die $file;
48 die "$file: Wanted length ", $st[7], ", got ", $len, "\n"
49 if ($len != $st[7]);
50 return $contents;
51}
52
53###############################################################################
54#
55# First of all, we have to parse the X.509 certificate to find certain details
56# about it.
57#
58# We read the DER-encoded X509 certificate and parse it to extract the Subject
59# name and Subject Key Identifier. Theis provides the data we need to build
60# the certificate identifier.
61#
62# The signer's name part of the identifier is fabricated from the commonName,
63# the organizationName or the emailAddress components of the X.509 subject
64# name.
65#
66# The subject key ID is used to select which of that signer's certificates
67# we're intending to use to sign the module.
68#
69###############################################################################
70my $x509_certificate = read_file($x509);
71
72my $UNIV = 0 << 6;
73my $APPL = 1 << 6;
74my $CONT = 2 << 6;
75my $PRIV = 3 << 6;
76
77my $CONS = 0x20;
78
79my $BOOLEAN = 0x01;
80my $INTEGER = 0x02;
81my $BIT_STRING = 0x03;
82my $OCTET_STRING = 0x04;
83my $NULL = 0x05;
84my $OBJ_ID = 0x06;
85my $UTF8String = 0x0c;
86my $SEQUENCE = 0x10;
87my $SET = 0x11;
88my $UTCTime = 0x17;
89my $GeneralizedTime = 0x18;
90
91my %OIDs = (
92 pack("CCC", 85, 4, 3) => "commonName",
93 pack("CCC", 85, 4, 6) => "countryName",
94 pack("CCC", 85, 4, 10) => "organizationName",
95 pack("CCC", 85, 4, 11) => "organizationUnitName",
96 pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 1) => "rsaEncryption",
97 pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 5) => "sha1WithRSAEncryption",
98 pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 9, 1) => "emailAddress",
99 pack("CCC", 85, 29, 35) => "authorityKeyIdentifier",
100 pack("CCC", 85, 29, 14) => "subjectKeyIdentifier",
101 pack("CCC", 85, 29, 19) => "basicConstraints"
102);
103
104###############################################################################
105#
106# Extract an ASN.1 element from a string and return information about it.
107#
108###############################################################################
109sub asn1_extract($$@)
110{
111 my ($cursor, $expected_tag, $optional) = @_;
112
113 return [ -1 ]
114 if ($cursor->[1] == 0 && $optional);
115
116 die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (elem ", $cursor->[1], ")\n"
117 if ($cursor->[1] < 2);
118
119 my ($tag, $len) = unpack("CC", substr(${$cursor->[2]}, $cursor->[0], 2));
120
121 if ($expected_tag != -1 && $tag != $expected_tag) {
122 return [ -1 ]
123 if ($optional);
124 die $x509, ": ", $cursor->[0], ": ASN.1 unexpected tag (", $tag,
125 " not ", $expected_tag, ")\n";
126 }
127
128 $cursor->[0] += 2;
129 $cursor->[1] -= 2;
130
131 die $x509, ": ", $cursor->[0], ": ASN.1 long tag\n"
132 if (($tag & 0x1f) == 0x1f);
133 die $x509, ": ", $cursor->[0], ": ASN.1 indefinite length\n"
134 if ($len == 0x80);
135
136 if ($len > 0x80) {
137 my $l = $len - 0x80;
138 die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (len len $l)\n"
139 if ($cursor->[1] < $l);
140
141 if ($l == 0x1) {
142 $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1));
Chun-Yi Lee916492b2012-11-21 11:26:09 +0000143 } elsif ($l == 0x2) {
David Howellsb37d1bf2012-10-19 23:56:37 +0100144 $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0], 2));
Chun-Yi Lee916492b2012-11-21 11:26:09 +0000145 } elsif ($l == 0x3) {
David Howellsb37d1bf2012-10-19 23:56:37 +0100146 $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)) << 16;
147 $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0] + 1, 2));
Chun-Yi Lee916492b2012-11-21 11:26:09 +0000148 } elsif ($l == 0x4) {
David Howellsb37d1bf2012-10-19 23:56:37 +0100149 $len = unpack("N", substr(${$cursor->[2]}, $cursor->[0], 4));
150 } else {
151 die $x509, ": ", $cursor->[0], ": ASN.1 element too long (", $l, ")\n";
152 }
153
154 $cursor->[0] += $l;
155 $cursor->[1] -= $l;
156 }
157
158 die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (", $len, ")\n"
159 if ($cursor->[1] < $len);
160
161 my $ret = [ $tag, [ $cursor->[0], $len, $cursor->[2] ] ];
162 $cursor->[0] += $len;
163 $cursor->[1] -= $len;
164
165 return $ret;
166}
167
168###############################################################################
169#
170# Retrieve the data referred to by a cursor
171#
172###############################################################################
173sub asn1_retrieve($)
174{
175 my ($cursor) = @_;
176 my ($offset, $len, $data) = @$cursor;
177 return substr($$data, $offset, $len);
178}
179
180###############################################################################
181#
182# Roughly parse the X.509 certificate
183#
184###############################################################################
185my $cursor = [ 0, length($x509_certificate), \$x509_certificate ];
186
187my $cert = asn1_extract($cursor, $UNIV | $CONS | $SEQUENCE);
188my $tbs = asn1_extract($cert->[1], $UNIV | $CONS | $SEQUENCE);
189my $version = asn1_extract($tbs->[1], $CONT | $CONS | 0, 1);
190my $serial_number = asn1_extract($tbs->[1], $UNIV | $INTEGER);
191my $sig_type = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
192my $issuer = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
193my $validity = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
194my $subject = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
195my $key = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
196my $issuer_uid = asn1_extract($tbs->[1], $CONT | $CONS | 1, 1);
197my $subject_uid = asn1_extract($tbs->[1], $CONT | $CONS | 2, 1);
198my $extension_list = asn1_extract($tbs->[1], $CONT | $CONS | 3, 1);
199
200my $subject_key_id = ();
201my $authority_key_id = ();
202
203#
204# Parse the extension list
205#
206if ($extension_list->[0] != -1) {
207 my $extensions = asn1_extract($extension_list->[1], $UNIV | $CONS | $SEQUENCE);
208
209 while ($extensions->[1]->[1] > 0) {
210 my $ext = asn1_extract($extensions->[1], $UNIV | $CONS | $SEQUENCE);
211 my $x_oid = asn1_extract($ext->[1], $UNIV | $OBJ_ID);
212 my $x_crit = asn1_extract($ext->[1], $UNIV | $BOOLEAN, 1);
213 my $x_val = asn1_extract($ext->[1], $UNIV | $OCTET_STRING);
214
215 my $raw_oid = asn1_retrieve($x_oid->[1]);
216 next if (!exists($OIDs{$raw_oid}));
217 my $x_type = $OIDs{$raw_oid};
218
219 my $raw_value = asn1_retrieve($x_val->[1]);
220
221 if ($x_type eq "subjectKeyIdentifier") {
222 my $vcursor = [ 0, length($raw_value), \$raw_value ];
223
224 $subject_key_id = asn1_extract($vcursor, $UNIV | $OCTET_STRING);
225 }
226 }
227}
228
229###############################################################################
230#
231# Determine what we're going to use as the signer's name. In order of
232# preference, take one of: commonName, organizationName or emailAddress.
233#
234###############################################################################
235my $org = "";
236my $cn = "";
237my $email = "";
238
239while ($subject->[1]->[1] > 0) {
240 my $rdn = asn1_extract($subject->[1], $UNIV | $CONS | $SET);
241 my $attr = asn1_extract($rdn->[1], $UNIV | $CONS | $SEQUENCE);
242 my $n_oid = asn1_extract($attr->[1], $UNIV | $OBJ_ID);
243 my $n_val = asn1_extract($attr->[1], -1);
244
245 my $raw_oid = asn1_retrieve($n_oid->[1]);
246 next if (!exists($OIDs{$raw_oid}));
247 my $n_type = $OIDs{$raw_oid};
248
249 my $raw_value = asn1_retrieve($n_val->[1]);
250
251 if ($n_type eq "organizationName") {
252 $org = $raw_value;
253 } elsif ($n_type eq "commonName") {
254 $cn = $raw_value;
255 } elsif ($n_type eq "emailAddress") {
256 $email = $raw_value;
257 }
258}
259
260my $signers_name = $email;
261
262if ($org && $cn) {
263 # Don't use the organizationName if the commonName repeats it
264 if (length($org) <= length($cn) &&
265 substr($cn, 0, length($org)) eq $org) {
266 $signers_name = $cn;
267 goto got_id_name;
268 }
269
270 # Or a signifcant chunk of it
271 if (length($org) >= 7 &&
272 length($cn) >= 7 &&
273 substr($cn, 0, 7) eq substr($org, 0, 7)) {
274 $signers_name = $cn;
275 goto got_id_name;
276 }
277
278 $signers_name = $org . ": " . $cn;
279} elsif ($org) {
280 $signers_name = $org;
281} elsif ($cn) {
282 $signers_name = $cn;
283}
284
285got_id_name:
286
287die $x509, ": ", "X.509: Couldn't find the Subject Key Identifier extension\n"
288 if (!$subject_key_id);
289
290my $key_identifier = asn1_retrieve($subject_key_id->[1]);
291
292###############################################################################
293#
294# Create and attach the module signature
295#
296###############################################################################
David Howells80d65e52012-09-26 10:11:06 +0100297
298#
299# Signature parameters
300#
David Howellsb37d1bf2012-10-19 23:56:37 +0100301my $algo = 1; # Public-key crypto algorithm: RSA
302my $hash = 0; # Digest algorithm
303my $id_type = 1; # Identifier type: X.509
David Howells80d65e52012-09-26 10:11:06 +0100304
305#
306# Digest the data
307#
Michal Marek4bc94102013-01-25 13:41:12 +1030308my $prologue;
309if ($dgst eq "sha1") {
David Howellsb37d1bf2012-10-19 23:56:37 +0100310 $prologue = pack("C*",
311 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
312 0x2B, 0x0E, 0x03, 0x02, 0x1A,
313 0x05, 0x00, 0x04, 0x14);
David Howellsb37d1bf2012-10-19 23:56:37 +0100314 $hash = 2;
Michal Marek4bc94102013-01-25 13:41:12 +1030315} elsif ($dgst eq "sha224") {
David Howellsb37d1bf2012-10-19 23:56:37 +0100316 $prologue = pack("C*",
317 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
318 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
319 0x05, 0x00, 0x04, 0x1C);
David Howellsb37d1bf2012-10-19 23:56:37 +0100320 $hash = 7;
Michal Marek4bc94102013-01-25 13:41:12 +1030321} elsif ($dgst eq "sha256") {
David Howellsb37d1bf2012-10-19 23:56:37 +0100322 $prologue = pack("C*",
323 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
324 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
325 0x05, 0x00, 0x04, 0x20);
David Howellsb37d1bf2012-10-19 23:56:37 +0100326 $hash = 4;
Michal Marek4bc94102013-01-25 13:41:12 +1030327} elsif ($dgst eq "sha384") {
David Howellsb37d1bf2012-10-19 23:56:37 +0100328 $prologue = pack("C*",
329 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
330 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
331 0x05, 0x00, 0x04, 0x30);
David Howellsb37d1bf2012-10-19 23:56:37 +0100332 $hash = 5;
Michal Marek4bc94102013-01-25 13:41:12 +1030333} elsif ($dgst eq "sha512") {
David Howellsb37d1bf2012-10-19 23:56:37 +0100334 $prologue = pack("C*",
335 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
336 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
337 0x05, 0x00, 0x04, 0x40);
David Howellsb37d1bf2012-10-19 23:56:37 +0100338 $hash = 6;
339} else {
Michal Marek4bc94102013-01-25 13:41:12 +1030340 die "Unknown hash algorithm: $dgst\n";
David Howellsb37d1bf2012-10-19 23:56:37 +0100341}
David Howells80d65e52012-09-26 10:11:06 +0100342
David Howellsb37d1bf2012-10-19 23:56:37 +0100343#
344# Generate the digest and read from openssl's stdout
345#
346my $digest;
Michal Marek4bc94102013-01-25 13:41:12 +1030347$digest = readpipe("openssl dgst -$dgst -binary $module") || die "openssl dgst";
David Howells80d65e52012-09-26 10:11:06 +0100348
349#
350# Generate the binary signature, which will be just the integer that comprises
351# the signature with no metadata attached.
352#
David Howellsb37d1bf2012-10-19 23:56:37 +0100353my $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";
Rusty Russelle2a666d2012-10-19 11:53:15 +1030360
David Howellsb37d1bf2012-10-19 23:56:37 +0100361binmode read_from;
362my $signature;
363read(read_from, $signature, 4096) || die "pipe from openssl rsautl";
364close(read_from) || die "pipe from openssl rsautl";
365$signature = pack("n", length($signature)) . $signature,
366
367waitpid($pid, 0) || die;
368die "openssl rsautl died: $?" if ($? >> 8);
David Howells80d65e52012-09-26 10:11:06 +0100369
370#
371# Build the signed binary
372#
David Howellsb37d1bf2012-10-19 23:56:37 +0100373my $unsigned_module = read_file($module);
David Howells80d65e52012-09-26 10:11:06 +0100374
David Howellsb37d1bf2012-10-19 23:56:37 +0100375my $magic_number = "~Module signature appended~\n";
David Howells80d65e52012-09-26 10:11:06 +0100376
David Howellsb37d1bf2012-10-19 23:56:37 +0100377my $info = pack("CCCCCxxxN",
378 $algo, $hash, $id_type,
379 length($signers_name),
380 length($key_identifier),
381 length($signature));
David Howells80d65e52012-09-26 10:11:06 +0100382
David Howellsb37d1bf2012-10-19 23:56:37 +0100383if ($verbose) {
384 print "Size of unsigned module: ", length($unsigned_module), "\n";
David Howellsb37d1bf2012-10-19 23:56:37 +0100385 print "Size of signer's name : ", length($signers_name), "\n";
386 print "Size of key identifier : ", length($key_identifier), "\n";
387 print "Size of signature : ", length($signature), "\n";
388 print "Size of informaton : ", length($info), "\n";
David Howellscaabe242012-10-20 01:19:29 +0100389 print "Size of magic number : ", length($magic_number), "\n";
David Howellsb37d1bf2012-10-19 23:56:37 +0100390 print "Signer's name : '", $signers_name, "'\n";
391 print "Digest : $dgst\n";
392}
393
394open(FD, ">$dest") || die $dest;
395binmode FD;
396print FD
397 $unsigned_module,
David Howellsb37d1bf2012-10-19 23:56:37 +0100398 $signers_name,
399 $key_identifier,
400 $signature,
David Howellscaabe242012-10-20 01:19:29 +0100401 $info,
402 $magic_number
David Howellsb37d1bf2012-10-19 23:56:37 +0100403 ;
404close FD || die $dest;
405
406if ($#ARGV != 3) {
407 rename($dest, $module) || die $module;
408}