sign-file 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #!/usr/bin/perl -w
  2. #
  3. # Sign a module file using the given key.
  4. #
  5. # Format:
  6. #
  7. # ./scripts/sign-file [-v] <key> <x509> <module> [<dest>]
  8. #
  9. #
  10. use strict;
  11. use FileHandle;
  12. use IPC::Open2;
  13. my $verbose = 0;
  14. if ($#ARGV >= 0 && $ARGV[0] eq "-v") {
  15. $verbose = 1;
  16. shift;
  17. }
  18. die "Format: ./scripts/sign-file [-v] <key> <x509> <module> [<dest>]\n"
  19. if ($#ARGV != 2 && $#ARGV != 3);
  20. my $private_key = $ARGV[0];
  21. my $x509 = $ARGV[1];
  22. my $module = $ARGV[2];
  23. my $dest = ($#ARGV == 3) ? $ARGV[3] : $ARGV[2] . "~";
  24. die "Can't read private key\n" unless (-r $private_key);
  25. die "Can't read X.509 certificate\n" unless (-r $x509);
  26. die "Can't read module\n" unless (-r $module);
  27. #
  28. # Read the kernel configuration
  29. #
  30. my %config = (
  31. CONFIG_MODULE_SIG_SHA512 => 1
  32. );
  33. if (-r ".config") {
  34. open(FD, "<.config") || die ".config";
  35. while (<FD>) {
  36. if ($_ =~ /^(CONFIG_.*)=[ym]/) {
  37. $config{$1} = 1;
  38. }
  39. }
  40. close(FD);
  41. }
  42. #
  43. # Function to read the contents of a file into a variable.
  44. #
  45. sub read_file($)
  46. {
  47. my ($file) = @_;
  48. my $contents;
  49. my $len;
  50. open(FD, "<$file") || die $file;
  51. binmode FD;
  52. my @st = stat(FD);
  53. die $file if (!@st);
  54. $len = read(FD, $contents, $st[7]) || die $file;
  55. close(FD) || die $file;
  56. die "$file: Wanted length ", $st[7], ", got ", $len, "\n"
  57. if ($len != $st[7]);
  58. return $contents;
  59. }
  60. ###############################################################################
  61. #
  62. # First of all, we have to parse the X.509 certificate to find certain details
  63. # about it.
  64. #
  65. # We read the DER-encoded X509 certificate and parse it to extract the Subject
  66. # name and Subject Key Identifier. Theis provides the data we need to build
  67. # the certificate identifier.
  68. #
  69. # The signer's name part of the identifier is fabricated from the commonName,
  70. # the organizationName or the emailAddress components of the X.509 subject
  71. # name.
  72. #
  73. # The subject key ID is used to select which of that signer's certificates
  74. # we're intending to use to sign the module.
  75. #
  76. ###############################################################################
  77. my $x509_certificate = read_file($x509);
  78. my $UNIV = 0 << 6;
  79. my $APPL = 1 << 6;
  80. my $CONT = 2 << 6;
  81. my $PRIV = 3 << 6;
  82. my $CONS = 0x20;
  83. my $BOOLEAN = 0x01;
  84. my $INTEGER = 0x02;
  85. my $BIT_STRING = 0x03;
  86. my $OCTET_STRING = 0x04;
  87. my $NULL = 0x05;
  88. my $OBJ_ID = 0x06;
  89. my $UTF8String = 0x0c;
  90. my $SEQUENCE = 0x10;
  91. my $SET = 0x11;
  92. my $UTCTime = 0x17;
  93. my $GeneralizedTime = 0x18;
  94. my %OIDs = (
  95. pack("CCC", 85, 4, 3) => "commonName",
  96. pack("CCC", 85, 4, 6) => "countryName",
  97. pack("CCC", 85, 4, 10) => "organizationName",
  98. pack("CCC", 85, 4, 11) => "organizationUnitName",
  99. pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 1) => "rsaEncryption",
  100. pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 5) => "sha1WithRSAEncryption",
  101. pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 9, 1) => "emailAddress",
  102. pack("CCC", 85, 29, 35) => "authorityKeyIdentifier",
  103. pack("CCC", 85, 29, 14) => "subjectKeyIdentifier",
  104. pack("CCC", 85, 29, 19) => "basicConstraints"
  105. );
  106. ###############################################################################
  107. #
  108. # Extract an ASN.1 element from a string and return information about it.
  109. #
  110. ###############################################################################
  111. sub asn1_extract($$@)
  112. {
  113. my ($cursor, $expected_tag, $optional) = @_;
  114. return [ -1 ]
  115. if ($cursor->[1] == 0 && $optional);
  116. die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (elem ", $cursor->[1], ")\n"
  117. if ($cursor->[1] < 2);
  118. my ($tag, $len) = unpack("CC", substr(${$cursor->[2]}, $cursor->[0], 2));
  119. if ($expected_tag != -1 && $tag != $expected_tag) {
  120. return [ -1 ]
  121. if ($optional);
  122. die $x509, ": ", $cursor->[0], ": ASN.1 unexpected tag (", $tag,
  123. " not ", $expected_tag, ")\n";
  124. }
  125. $cursor->[0] += 2;
  126. $cursor->[1] -= 2;
  127. die $x509, ": ", $cursor->[0], ": ASN.1 long tag\n"
  128. if (($tag & 0x1f) == 0x1f);
  129. die $x509, ": ", $cursor->[0], ": ASN.1 indefinite length\n"
  130. if ($len == 0x80);
  131. if ($len > 0x80) {
  132. my $l = $len - 0x80;
  133. die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (len len $l)\n"
  134. if ($cursor->[1] < $l);
  135. if ($l == 0x1) {
  136. $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1));
  137. } elsif ($l == 0x2) {
  138. $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0], 2));
  139. } elsif ($l == 0x3) {
  140. $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)) << 16;
  141. $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0] + 1, 2));
  142. } elsif ($l == 0x4) {
  143. $len = unpack("N", substr(${$cursor->[2]}, $cursor->[0], 4));
  144. } else {
  145. die $x509, ": ", $cursor->[0], ": ASN.1 element too long (", $l, ")\n";
  146. }
  147. $cursor->[0] += $l;
  148. $cursor->[1] -= $l;
  149. }
  150. die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (", $len, ")\n"
  151. if ($cursor->[1] < $len);
  152. my $ret = [ $tag, [ $cursor->[0], $len, $cursor->[2] ] ];
  153. $cursor->[0] += $len;
  154. $cursor->[1] -= $len;
  155. return $ret;
  156. }
  157. ###############################################################################
  158. #
  159. # Retrieve the data referred to by a cursor
  160. #
  161. ###############################################################################
  162. sub asn1_retrieve($)
  163. {
  164. my ($cursor) = @_;
  165. my ($offset, $len, $data) = @$cursor;
  166. return substr($$data, $offset, $len);
  167. }
  168. ###############################################################################
  169. #
  170. # Roughly parse the X.509 certificate
  171. #
  172. ###############################################################################
  173. my $cursor = [ 0, length($x509_certificate), \$x509_certificate ];
  174. my $cert = asn1_extract($cursor, $UNIV | $CONS | $SEQUENCE);
  175. my $tbs = asn1_extract($cert->[1], $UNIV | $CONS | $SEQUENCE);
  176. my $version = asn1_extract($tbs->[1], $CONT | $CONS | 0, 1);
  177. my $serial_number = asn1_extract($tbs->[1], $UNIV | $INTEGER);
  178. my $sig_type = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
  179. my $issuer = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
  180. my $validity = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
  181. my $subject = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
  182. my $key = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
  183. my $issuer_uid = asn1_extract($tbs->[1], $CONT | $CONS | 1, 1);
  184. my $subject_uid = asn1_extract($tbs->[1], $CONT | $CONS | 2, 1);
  185. my $extension_list = asn1_extract($tbs->[1], $CONT | $CONS | 3, 1);
  186. my $subject_key_id = ();
  187. my $authority_key_id = ();
  188. #
  189. # Parse the extension list
  190. #
  191. if ($extension_list->[0] != -1) {
  192. my $extensions = asn1_extract($extension_list->[1], $UNIV | $CONS | $SEQUENCE);
  193. while ($extensions->[1]->[1] > 0) {
  194. my $ext = asn1_extract($extensions->[1], $UNIV | $CONS | $SEQUENCE);
  195. my $x_oid = asn1_extract($ext->[1], $UNIV | $OBJ_ID);
  196. my $x_crit = asn1_extract($ext->[1], $UNIV | $BOOLEAN, 1);
  197. my $x_val = asn1_extract($ext->[1], $UNIV | $OCTET_STRING);
  198. my $raw_oid = asn1_retrieve($x_oid->[1]);
  199. next if (!exists($OIDs{$raw_oid}));
  200. my $x_type = $OIDs{$raw_oid};
  201. my $raw_value = asn1_retrieve($x_val->[1]);
  202. if ($x_type eq "subjectKeyIdentifier") {
  203. my $vcursor = [ 0, length($raw_value), \$raw_value ];
  204. $subject_key_id = asn1_extract($vcursor, $UNIV | $OCTET_STRING);
  205. }
  206. }
  207. }
  208. ###############################################################################
  209. #
  210. # Determine what we're going to use as the signer's name. In order of
  211. # preference, take one of: commonName, organizationName or emailAddress.
  212. #
  213. ###############################################################################
  214. my $org = "";
  215. my $cn = "";
  216. my $email = "";
  217. while ($subject->[1]->[1] > 0) {
  218. my $rdn = asn1_extract($subject->[1], $UNIV | $CONS | $SET);
  219. my $attr = asn1_extract($rdn->[1], $UNIV | $CONS | $SEQUENCE);
  220. my $n_oid = asn1_extract($attr->[1], $UNIV | $OBJ_ID);
  221. my $n_val = asn1_extract($attr->[1], -1);
  222. my $raw_oid = asn1_retrieve($n_oid->[1]);
  223. next if (!exists($OIDs{$raw_oid}));
  224. my $n_type = $OIDs{$raw_oid};
  225. my $raw_value = asn1_retrieve($n_val->[1]);
  226. if ($n_type eq "organizationName") {
  227. $org = $raw_value;
  228. } elsif ($n_type eq "commonName") {
  229. $cn = $raw_value;
  230. } elsif ($n_type eq "emailAddress") {
  231. $email = $raw_value;
  232. }
  233. }
  234. my $signers_name = $email;
  235. if ($org && $cn) {
  236. # Don't use the organizationName if the commonName repeats it
  237. if (length($org) <= length($cn) &&
  238. substr($cn, 0, length($org)) eq $org) {
  239. $signers_name = $cn;
  240. goto got_id_name;
  241. }
  242. # Or a signifcant chunk of it
  243. if (length($org) >= 7 &&
  244. length($cn) >= 7 &&
  245. substr($cn, 0, 7) eq substr($org, 0, 7)) {
  246. $signers_name = $cn;
  247. goto got_id_name;
  248. }
  249. $signers_name = $org . ": " . $cn;
  250. } elsif ($org) {
  251. $signers_name = $org;
  252. } elsif ($cn) {
  253. $signers_name = $cn;
  254. }
  255. got_id_name:
  256. die $x509, ": ", "X.509: Couldn't find the Subject Key Identifier extension\n"
  257. if (!$subject_key_id);
  258. my $key_identifier = asn1_retrieve($subject_key_id->[1]);
  259. ###############################################################################
  260. #
  261. # Create and attach the module signature
  262. #
  263. ###############################################################################
  264. #
  265. # Signature parameters
  266. #
  267. my $algo = 1; # Public-key crypto algorithm: RSA
  268. my $hash = 0; # Digest algorithm
  269. my $id_type = 1; # Identifier type: X.509
  270. #
  271. # Digest the data
  272. #
  273. my ($dgst, $prologue) = ();
  274. if (exists $config{"CONFIG_MODULE_SIG_SHA1"}) {
  275. $prologue = pack("C*",
  276. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  277. 0x2B, 0x0E, 0x03, 0x02, 0x1A,
  278. 0x05, 0x00, 0x04, 0x14);
  279. $dgst = "-sha1";
  280. $hash = 2;
  281. } elsif (exists $config{"CONFIG_MODULE_SIG_SHA224"}) {
  282. $prologue = pack("C*",
  283. 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
  284. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
  285. 0x05, 0x00, 0x04, 0x1C);
  286. $dgst = "-sha224";
  287. $hash = 7;
  288. } elsif (exists $config{"CONFIG_MODULE_SIG_SHA256"}) {
  289. $prologue = pack("C*",
  290. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
  291. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  292. 0x05, 0x00, 0x04, 0x20);
  293. $dgst = "-sha256";
  294. $hash = 4;
  295. } elsif (exists $config{"CONFIG_MODULE_SIG_SHA384"}) {
  296. $prologue = pack("C*",
  297. 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
  298. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
  299. 0x05, 0x00, 0x04, 0x30);
  300. $dgst = "-sha384";
  301. $hash = 5;
  302. } elsif (exists $config{"CONFIG_MODULE_SIG_SHA512"}) {
  303. $prologue = pack("C*",
  304. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
  305. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
  306. 0x05, 0x00, 0x04, 0x40);
  307. $dgst = "-sha512";
  308. $hash = 6;
  309. } else {
  310. die "Can't determine hash algorithm";
  311. }
  312. #
  313. # Generate the digest and read from openssl's stdout
  314. #
  315. my $digest;
  316. $digest = readpipe("openssl dgst $dgst -binary $module") || die "openssl dgst";
  317. #
  318. # Generate the binary signature, which will be just the integer that comprises
  319. # the signature with no metadata attached.
  320. #
  321. my $pid;
  322. $pid = open2(*read_from, *write_to,
  323. "openssl rsautl -sign -inkey $private_key -keyform PEM") ||
  324. die "openssl rsautl";
  325. binmode write_to;
  326. print write_to $prologue . $digest || die "pipe to openssl rsautl";
  327. close(write_to) || die "pipe to openssl rsautl";
  328. binmode read_from;
  329. my $signature;
  330. read(read_from, $signature, 4096) || die "pipe from openssl rsautl";
  331. close(read_from) || die "pipe from openssl rsautl";
  332. $signature = pack("n", length($signature)) . $signature,
  333. waitpid($pid, 0) || die;
  334. die "openssl rsautl died: $?" if ($? >> 8);
  335. #
  336. # Build the signed binary
  337. #
  338. my $unsigned_module = read_file($module);
  339. my $magic_number = "~Module signature appended~\n";
  340. my $info = pack("CCCCCxxxN",
  341. $algo, $hash, $id_type,
  342. length($signers_name),
  343. length($key_identifier),
  344. length($signature));
  345. if ($verbose) {
  346. print "Size of unsigned module: ", length($unsigned_module), "\n";
  347. print "Size of signer's name : ", length($signers_name), "\n";
  348. print "Size of key identifier : ", length($key_identifier), "\n";
  349. print "Size of signature : ", length($signature), "\n";
  350. print "Size of informaton : ", length($info), "\n";
  351. print "Size of magic number : ", length($magic_number), "\n";
  352. print "Signer's name : '", $signers_name, "'\n";
  353. print "Digest : $dgst\n";
  354. }
  355. open(FD, ">$dest") || die $dest;
  356. binmode FD;
  357. print FD
  358. $unsigned_module,
  359. $signers_name,
  360. $key_identifier,
  361. $signature,
  362. $info,
  363. $magic_number
  364. ;
  365. close FD || die $dest;
  366. if ($#ARGV != 3) {
  367. rename($dest, $module) || die $module;
  368. }