diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | init/Kconfig | 1 | ||||
-rw-r--r-- | kernel/module_signing.c | 220 | ||||
-rw-r--r-- | scripts/Makefile | 2 | ||||
-rwxr-xr-x | scripts/sign-file | 421 |
5 files changed, 48 insertions, 598 deletions
@@ -873,7 +873,7 @@ ifdef CONFIG_MODULE_SIG_ALL | |||
873 | MODSECKEY = ./signing_key.priv | 873 | MODSECKEY = ./signing_key.priv |
874 | MODPUBKEY = ./signing_key.x509 | 874 | MODPUBKEY = ./signing_key.x509 |
875 | export MODPUBKEY | 875 | export MODPUBKEY |
876 | mod_sign_cmd = perl $(srctree)/scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY) | 876 | mod_sign_cmd = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) $(MODSECKEY) $(MODPUBKEY) |
877 | else | 877 | else |
878 | mod_sign_cmd = true | 878 | mod_sign_cmd = true |
879 | endif | 879 | endif |
diff --git a/init/Kconfig b/init/Kconfig index af09b4fb43d2..e16d9e587cee 100644 --- a/init/Kconfig +++ b/init/Kconfig | |||
@@ -1869,6 +1869,7 @@ config MODULE_SIG | |||
1869 | select ASN1 | 1869 | select ASN1 |
1870 | select OID_REGISTRY | 1870 | select OID_REGISTRY |
1871 | select X509_CERTIFICATE_PARSER | 1871 | select X509_CERTIFICATE_PARSER |
1872 | select PKCS7_MESSAGE_PARSER | ||
1872 | help | 1873 | help |
1873 | Check modules for valid signatures upon load: the signature | 1874 | Check modules for valid signatures upon load: the signature |
1874 | is simply appended to the module. For more information see | 1875 | is simply appended to the module. For more information see |
diff --git a/kernel/module_signing.c b/kernel/module_signing.c index be5b8fac4bd0..8eb20cc66b39 100644 --- a/kernel/module_signing.c +++ b/kernel/module_signing.c | |||
@@ -11,10 +11,9 @@ | |||
11 | 11 | ||
12 | #include <linux/kernel.h> | 12 | #include <linux/kernel.h> |
13 | #include <linux/err.h> | 13 | #include <linux/err.h> |
14 | #include <crypto/public_key.h> | ||
15 | #include <crypto/hash.h> | ||
16 | #include <keys/asymmetric-type.h> | ||
17 | #include <keys/system_keyring.h> | 14 | #include <keys/system_keyring.h> |
15 | #include <crypto/public_key.h> | ||
16 | #include <crypto/pkcs7.h> | ||
18 | #include "module-internal.h" | 17 | #include "module-internal.h" |
19 | 18 | ||
20 | /* | 19 | /* |
@@ -28,157 +27,53 @@ | |||
28 | * - Information block | 27 | * - Information block |
29 | */ | 28 | */ |
30 | struct module_signature { | 29 | struct module_signature { |
31 | u8 algo; /* Public-key crypto algorithm [enum pkey_algo] */ | 30 | u8 algo; /* Public-key crypto algorithm [0] */ |
32 | u8 hash; /* Digest algorithm [enum hash_algo] */ | 31 | u8 hash; /* Digest algorithm [0] */ |
33 | u8 id_type; /* Key identifier type [enum pkey_id_type] */ | 32 | u8 id_type; /* Key identifier type [PKEY_ID_PKCS7] */ |
34 | u8 signer_len; /* Length of signer's name */ | 33 | u8 signer_len; /* Length of signer's name [0] */ |
35 | u8 key_id_len; /* Length of key identifier */ | 34 | u8 key_id_len; /* Length of key identifier [0] */ |
36 | u8 __pad[3]; | 35 | u8 __pad[3]; |
37 | __be32 sig_len; /* Length of signature data */ | 36 | __be32 sig_len; /* Length of signature data */ |
38 | }; | 37 | }; |
39 | 38 | ||
40 | /* | 39 | /* |
41 | * Digest the module contents. | 40 | * Verify a PKCS#7-based signature on a module. |
42 | */ | 41 | */ |
43 | static struct public_key_signature *mod_make_digest(enum hash_algo hash, | 42 | static int mod_verify_pkcs7(const void *mod, unsigned long modlen, |
44 | const void *mod, | 43 | const void *raw_pkcs7, size_t pkcs7_len) |
45 | unsigned long modlen) | ||
46 | { | 44 | { |
47 | struct public_key_signature *pks; | 45 | struct pkcs7_message *pkcs7; |
48 | struct crypto_shash *tfm; | 46 | bool trusted; |
49 | struct shash_desc *desc; | ||
50 | size_t digest_size, desc_size; | ||
51 | int ret; | 47 | int ret; |
52 | 48 | ||
53 | pr_devel("==>%s()\n", __func__); | 49 | pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len); |
54 | 50 | if (IS_ERR(pkcs7)) | |
55 | /* Allocate the hashing algorithm we're going to need and find out how | 51 | return PTR_ERR(pkcs7); |
56 | * big the hash operational data will be. | 52 | |
57 | */ | 53 | /* The data should be detached - so we need to supply it. */ |
58 | tfm = crypto_alloc_shash(hash_algo_name[hash], 0, 0); | 54 | if (pkcs7_supply_detached_data(pkcs7, mod, modlen) < 0) { |
59 | if (IS_ERR(tfm)) | 55 | pr_err("PKCS#7 signature with non-detached data\n"); |
60 | return (PTR_ERR(tfm) == -ENOENT) ? ERR_PTR(-ENOPKG) : ERR_CAST(tfm); | 56 | ret = -EBADMSG; |
61 | 57 | goto error; | |
62 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); | 58 | } |
63 | digest_size = crypto_shash_digestsize(tfm); | 59 | |
64 | 60 | ret = pkcs7_verify(pkcs7); | |
65 | /* We allocate the hash operational data storage on the end of our | ||
66 | * context data and the digest output buffer on the end of that. | ||
67 | */ | ||
68 | ret = -ENOMEM; | ||
69 | pks = kzalloc(digest_size + sizeof(*pks) + desc_size, GFP_KERNEL); | ||
70 | if (!pks) | ||
71 | goto error_no_pks; | ||
72 | |||
73 | pks->pkey_hash_algo = hash; | ||
74 | pks->digest = (u8 *)pks + sizeof(*pks) + desc_size; | ||
75 | pks->digest_size = digest_size; | ||
76 | |||
77 | desc = (void *)pks + sizeof(*pks); | ||
78 | desc->tfm = tfm; | ||
79 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; | ||
80 | |||
81 | ret = crypto_shash_init(desc); | ||
82 | if (ret < 0) | 61 | if (ret < 0) |
83 | goto error; | 62 | goto error; |
84 | 63 | ||
85 | ret = crypto_shash_finup(desc, mod, modlen, pks->digest); | 64 | ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted); |
86 | if (ret < 0) | 65 | if (ret < 0) |
87 | goto error; | 66 | goto error; |
88 | 67 | ||
89 | crypto_free_shash(tfm); | 68 | if (!trusted) { |
90 | pr_devel("<==%s() = ok\n", __func__); | 69 | pr_err("PKCS#7 signature not signed with a trusted key\n"); |
91 | return pks; | 70 | ret = -ENOKEY; |
71 | } | ||
92 | 72 | ||
93 | error: | 73 | error: |
94 | kfree(pks); | 74 | pkcs7_free_message(pkcs7); |
95 | error_no_pks: | ||
96 | crypto_free_shash(tfm); | ||
97 | pr_devel("<==%s() = %d\n", __func__, ret); | 75 | pr_devel("<==%s() = %d\n", __func__, ret); |
98 | return ERR_PTR(ret); | 76 | return ret; |
99 | } | ||
100 | |||
101 | /* | ||
102 | * Extract an MPI array from the signature data. This represents the actual | ||
103 | * signature. Each raw MPI is prefaced by a BE 2-byte value indicating the | ||
104 | * size of the MPI in bytes. | ||
105 | * | ||
106 | * RSA signatures only have one MPI, so currently we only read one. | ||
107 | */ | ||
108 | static int mod_extract_mpi_array(struct public_key_signature *pks, | ||
109 | const void *data, size_t len) | ||
110 | { | ||
111 | size_t nbytes; | ||
112 | MPI mpi; | ||
113 | |||
114 | if (len < 3) | ||
115 | return -EBADMSG; | ||
116 | nbytes = ((const u8 *)data)[0] << 8 | ((const u8 *)data)[1]; | ||
117 | data += 2; | ||
118 | len -= 2; | ||
119 | if (len != nbytes) | ||
120 | return -EBADMSG; | ||
121 | |||
122 | mpi = mpi_read_raw_data(data, nbytes); | ||
123 | if (!mpi) | ||
124 | return -ENOMEM; | ||
125 | pks->mpi[0] = mpi; | ||
126 | pks->nr_mpi = 1; | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | /* | ||
131 | * Request an asymmetric key. | ||
132 | */ | ||
133 | static struct key *request_asymmetric_key(const char *signer, size_t signer_len, | ||
134 | const u8 *key_id, size_t key_id_len) | ||
135 | { | ||
136 | key_ref_t key; | ||
137 | size_t i; | ||
138 | char *id, *q; | ||
139 | |||
140 | pr_devel("==>%s(,%zu,,%zu)\n", __func__, signer_len, key_id_len); | ||
141 | |||
142 | /* Construct an identifier. */ | ||
143 | id = kmalloc(signer_len + 2 + key_id_len * 2 + 1, GFP_KERNEL); | ||
144 | if (!id) | ||
145 | return ERR_PTR(-ENOKEY); | ||
146 | |||
147 | memcpy(id, signer, signer_len); | ||
148 | |||
149 | q = id + signer_len; | ||
150 | *q++ = ':'; | ||
151 | *q++ = ' '; | ||
152 | for (i = 0; i < key_id_len; i++) { | ||
153 | *q++ = hex_asc[*key_id >> 4]; | ||
154 | *q++ = hex_asc[*key_id++ & 0x0f]; | ||
155 | } | ||
156 | |||
157 | *q = 0; | ||
158 | |||
159 | pr_debug("Look up: \"%s\"\n", id); | ||
160 | |||
161 | key = keyring_search(make_key_ref(system_trusted_keyring, 1), | ||
162 | &key_type_asymmetric, id); | ||
163 | if (IS_ERR(key)) | ||
164 | pr_warn("Request for unknown module key '%s' err %ld\n", | ||
165 | id, PTR_ERR(key)); | ||
166 | kfree(id); | ||
167 | |||
168 | if (IS_ERR(key)) { | ||
169 | switch (PTR_ERR(key)) { | ||
170 | /* Hide some search errors */ | ||
171 | case -EACCES: | ||
172 | case -ENOTDIR: | ||
173 | case -EAGAIN: | ||
174 | return ERR_PTR(-ENOKEY); | ||
175 | default: | ||
176 | return ERR_CAST(key); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key))); | ||
181 | return key_ref_to_ptr(key); | ||
182 | } | 77 | } |
183 | 78 | ||
184 | /* | 79 | /* |
@@ -186,12 +81,8 @@ static struct key *request_asymmetric_key(const char *signer, size_t signer_len, | |||
186 | */ | 81 | */ |
187 | int mod_verify_sig(const void *mod, unsigned long *_modlen) | 82 | int mod_verify_sig(const void *mod, unsigned long *_modlen) |
188 | { | 83 | { |
189 | struct public_key_signature *pks; | ||
190 | struct module_signature ms; | 84 | struct module_signature ms; |
191 | struct key *key; | ||
192 | const void *sig; | ||
193 | size_t modlen = *_modlen, sig_len; | 85 | size_t modlen = *_modlen, sig_len; |
194 | int ret; | ||
195 | 86 | ||
196 | pr_devel("==>%s(,%zu)\n", __func__, modlen); | 87 | pr_devel("==>%s(,%zu)\n", __func__, modlen); |
197 | 88 | ||
@@ -205,46 +96,23 @@ int mod_verify_sig(const void *mod, unsigned long *_modlen) | |||
205 | if (sig_len >= modlen) | 96 | if (sig_len >= modlen) |
206 | return -EBADMSG; | 97 | return -EBADMSG; |
207 | modlen -= sig_len; | 98 | modlen -= sig_len; |
208 | if ((size_t)ms.signer_len + ms.key_id_len >= modlen) | ||
209 | return -EBADMSG; | ||
210 | modlen -= (size_t)ms.signer_len + ms.key_id_len; | ||
211 | |||
212 | *_modlen = modlen; | 99 | *_modlen = modlen; |
213 | sig = mod + modlen; | ||
214 | |||
215 | /* For the moment, only support RSA and X.509 identifiers */ | ||
216 | if (ms.algo != PKEY_ALGO_RSA || | ||
217 | ms.id_type != PKEY_ID_X509) | ||
218 | return -ENOPKG; | ||
219 | 100 | ||
220 | if (ms.hash >= PKEY_HASH__LAST || | 101 | if (ms.id_type != PKEY_ID_PKCS7) { |
221 | !hash_algo_name[ms.hash]) | 102 | pr_err("Module is not signed with expected PKCS#7 message\n"); |
222 | return -ENOPKG; | 103 | return -ENOPKG; |
223 | |||
224 | key = request_asymmetric_key(sig, ms.signer_len, | ||
225 | sig + ms.signer_len, ms.key_id_len); | ||
226 | if (IS_ERR(key)) | ||
227 | return PTR_ERR(key); | ||
228 | |||
229 | pks = mod_make_digest(ms.hash, mod, modlen); | ||
230 | if (IS_ERR(pks)) { | ||
231 | ret = PTR_ERR(pks); | ||
232 | goto error_put_key; | ||
233 | } | 104 | } |
234 | 105 | ||
235 | ret = mod_extract_mpi_array(pks, sig + ms.signer_len + ms.key_id_len, | 106 | if (ms.algo != 0 || |
236 | sig_len); | 107 | ms.hash != 0 || |
237 | if (ret < 0) | 108 | ms.signer_len != 0 || |
238 | goto error_free_pks; | 109 | ms.key_id_len != 0 || |
239 | 110 | ms.__pad[0] != 0 || | |
240 | ret = verify_signature(key, pks); | 111 | ms.__pad[1] != 0 || |
241 | pr_devel("verify_signature() = %d\n", ret); | 112 | ms.__pad[2] != 0) { |
113 | pr_err("PKCS#7 signature info has unexpected non-zero params\n"); | ||
114 | return -EBADMSG; | ||
115 | } | ||
242 | 116 | ||
243 | error_free_pks: | 117 | return mod_verify_pkcs7(mod, modlen, mod + modlen, sig_len); |
244 | mpi_free(pks->rsa.s); | ||
245 | kfree(pks); | ||
246 | error_put_key: | ||
247 | key_put(key); | ||
248 | pr_devel("<==%s() = %d\n", __func__, ret); | ||
249 | return ret; | ||
250 | } | 118 | } |
diff --git a/scripts/Makefile b/scripts/Makefile index 2016a64497ab..b12fe020664d 100644 --- a/scripts/Makefile +++ b/scripts/Makefile | |||
@@ -16,9 +16,11 @@ hostprogs-$(CONFIG_VT) += conmakehash | |||
16 | hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount | 16 | hostprogs-$(BUILD_C_RECORDMCOUNT) += recordmcount |
17 | hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable | 17 | hostprogs-$(CONFIG_BUILDTIME_EXTABLE_SORT) += sortextable |
18 | hostprogs-$(CONFIG_ASN1) += asn1_compiler | 18 | hostprogs-$(CONFIG_ASN1) += asn1_compiler |
19 | hostprogs-$(CONFIG_MODULE_SIG) += sign-file | ||
19 | 20 | ||
20 | HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include | 21 | HOSTCFLAGS_sortextable.o = -I$(srctree)/tools/include |
21 | HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include | 22 | HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include |
23 | HOSTLOADLIBES_sign-file = -lcrypto | ||
22 | 24 | ||
23 | always := $(hostprogs-y) $(hostprogs-m) | 25 | always := $(hostprogs-y) $(hostprogs-m) |
24 | 26 | ||
diff --git a/scripts/sign-file b/scripts/sign-file deleted file mode 100755 index 3906ee1e2f76..000000000000 --- a/scripts/sign-file +++ /dev/null | |||
@@ -1,421 +0,0 @@ | |||
1 | #!/usr/bin/perl -w | ||
2 | # | ||
3 | # Sign a module file using the given key. | ||
4 | # | ||
5 | |||
6 | my $USAGE = | ||
7 | "Usage: scripts/sign-file [-v] <hash algo> <key> <x509> <module> [<dest>]\n" . | ||
8 | " scripts/sign-file [-v] -s <raw sig> <hash algo> <x509> <module> [<dest>]\n"; | ||
9 | |||
10 | use strict; | ||
11 | use FileHandle; | ||
12 | use IPC::Open2; | ||
13 | use Getopt::Std; | ||
14 | |||
15 | my %opts; | ||
16 | getopts('vs:', \%opts) or die $USAGE; | ||
17 | my $verbose = $opts{'v'}; | ||
18 | my $signature_file = $opts{'s'}; | ||
19 | |||
20 | die $USAGE if ($#ARGV > 4); | ||
21 | die $USAGE if (!$signature_file && $#ARGV < 3 || $signature_file && $#ARGV < 2); | ||
22 | |||
23 | my $dgst = shift @ARGV; | ||
24 | my $private_key; | ||
25 | if (!$signature_file) { | ||
26 | $private_key = shift @ARGV; | ||
27 | } | ||
28 | my $x509 = shift @ARGV; | ||
29 | my $module = shift @ARGV; | ||
30 | my ($dest, $keep_orig); | ||
31 | if (@ARGV) { | ||
32 | $dest = $ARGV[0]; | ||
33 | $keep_orig = 1; | ||
34 | } else { | ||
35 | $dest = $module . "~"; | ||
36 | } | ||
37 | |||
38 | die "Can't read private key\n" if (!$signature_file && !-r $private_key); | ||
39 | die "Can't read signature file\n" if ($signature_file && !-r $signature_file); | ||
40 | die "Can't read X.509 certificate\n" unless (-r $x509); | ||
41 | die "Can't read module\n" unless (-r $module); | ||
42 | |||
43 | # | ||
44 | # Function to read the contents of a file into a variable. | ||
45 | # | ||
46 | sub read_file($) | ||
47 | { | ||
48 | my ($file) = @_; | ||
49 | my $contents; | ||
50 | my $len; | ||
51 | |||
52 | open(FD, "<$file") || die $file; | ||
53 | binmode FD; | ||
54 | my @st = stat(FD); | ||
55 | die $file if (!@st); | ||
56 | $len = read(FD, $contents, $st[7]) || die $file; | ||
57 | close(FD) || die $file; | ||
58 | die "$file: Wanted length ", $st[7], ", got ", $len, "\n" | ||
59 | if ($len != $st[7]); | ||
60 | return $contents; | ||
61 | } | ||
62 | |||
63 | ############################################################################### | ||
64 | # | ||
65 | # First of all, we have to parse the X.509 certificate to find certain details | ||
66 | # about it. | ||
67 | # | ||
68 | # We read the DER-encoded X509 certificate and parse it to extract the Subject | ||
69 | # name and Subject Key Identifier. Theis provides the data we need to build | ||
70 | # the certificate identifier. | ||
71 | # | ||
72 | # The signer's name part of the identifier is fabricated from the commonName, | ||
73 | # the organizationName or the emailAddress components of the X.509 subject | ||
74 | # name. | ||
75 | # | ||
76 | # The subject key ID is used to select which of that signer's certificates | ||
77 | # we're intending to use to sign the module. | ||
78 | # | ||
79 | ############################################################################### | ||
80 | my $x509_certificate = read_file($x509); | ||
81 | |||
82 | my $UNIV = 0 << 6; | ||
83 | my $APPL = 1 << 6; | ||
84 | my $CONT = 2 << 6; | ||
85 | my $PRIV = 3 << 6; | ||
86 | |||
87 | my $CONS = 0x20; | ||
88 | |||
89 | my $BOOLEAN = 0x01; | ||
90 | my $INTEGER = 0x02; | ||
91 | my $BIT_STRING = 0x03; | ||
92 | my $OCTET_STRING = 0x04; | ||
93 | my $NULL = 0x05; | ||
94 | my $OBJ_ID = 0x06; | ||
95 | my $UTF8String = 0x0c; | ||
96 | my $SEQUENCE = 0x10; | ||
97 | my $SET = 0x11; | ||
98 | my $UTCTime = 0x17; | ||
99 | my $GeneralizedTime = 0x18; | ||
100 | |||
101 | my %OIDs = ( | ||
102 | pack("CCC", 85, 4, 3) => "commonName", | ||
103 | pack("CCC", 85, 4, 6) => "countryName", | ||
104 | pack("CCC", 85, 4, 10) => "organizationName", | ||
105 | pack("CCC", 85, 4, 11) => "organizationUnitName", | ||
106 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 1) => "rsaEncryption", | ||
107 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 5) => "sha1WithRSAEncryption", | ||
108 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 9, 1) => "emailAddress", | ||
109 | pack("CCC", 85, 29, 35) => "authorityKeyIdentifier", | ||
110 | pack("CCC", 85, 29, 14) => "subjectKeyIdentifier", | ||
111 | pack("CCC", 85, 29, 19) => "basicConstraints" | ||
112 | ); | ||
113 | |||
114 | ############################################################################### | ||
115 | # | ||
116 | # Extract an ASN.1 element from a string and return information about it. | ||
117 | # | ||
118 | ############################################################################### | ||
119 | sub asn1_extract($$@) | ||
120 | { | ||
121 | my ($cursor, $expected_tag, $optional) = @_; | ||
122 | |||
123 | return [ -1 ] | ||
124 | if ($cursor->[1] == 0 && $optional); | ||
125 | |||
126 | die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (elem ", $cursor->[1], ")\n" | ||
127 | if ($cursor->[1] < 2); | ||
128 | |||
129 | my ($tag, $len) = unpack("CC", substr(${$cursor->[2]}, $cursor->[0], 2)); | ||
130 | |||
131 | if ($expected_tag != -1 && $tag != $expected_tag) { | ||
132 | return [ -1 ] | ||
133 | if ($optional); | ||
134 | die $x509, ": ", $cursor->[0], ": ASN.1 unexpected tag (", $tag, | ||
135 | " not ", $expected_tag, ")\n"; | ||
136 | } | ||
137 | |||
138 | $cursor->[0] += 2; | ||
139 | $cursor->[1] -= 2; | ||
140 | |||
141 | die $x509, ": ", $cursor->[0], ": ASN.1 long tag\n" | ||
142 | if (($tag & 0x1f) == 0x1f); | ||
143 | die $x509, ": ", $cursor->[0], ": ASN.1 indefinite length\n" | ||
144 | if ($len == 0x80); | ||
145 | |||
146 | if ($len > 0x80) { | ||
147 | my $l = $len - 0x80; | ||
148 | die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (len len $l)\n" | ||
149 | if ($cursor->[1] < $l); | ||
150 | |||
151 | if ($l == 0x1) { | ||
152 | $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)); | ||
153 | } elsif ($l == 0x2) { | ||
154 | $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0], 2)); | ||
155 | } elsif ($l == 0x3) { | ||
156 | $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)) << 16; | ||
157 | $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0] + 1, 2)); | ||
158 | } elsif ($l == 0x4) { | ||
159 | $len = unpack("N", substr(${$cursor->[2]}, $cursor->[0], 4)); | ||
160 | } else { | ||
161 | die $x509, ": ", $cursor->[0], ": ASN.1 element too long (", $l, ")\n"; | ||
162 | } | ||
163 | |||
164 | $cursor->[0] += $l; | ||
165 | $cursor->[1] -= $l; | ||
166 | } | ||
167 | |||
168 | die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (", $len, ")\n" | ||
169 | if ($cursor->[1] < $len); | ||
170 | |||
171 | my $ret = [ $tag, [ $cursor->[0], $len, $cursor->[2] ] ]; | ||
172 | $cursor->[0] += $len; | ||
173 | $cursor->[1] -= $len; | ||
174 | |||
175 | return $ret; | ||
176 | } | ||
177 | |||
178 | ############################################################################### | ||
179 | # | ||
180 | # Retrieve the data referred to by a cursor | ||
181 | # | ||
182 | ############################################################################### | ||
183 | sub asn1_retrieve($) | ||
184 | { | ||
185 | my ($cursor) = @_; | ||
186 | my ($offset, $len, $data) = @$cursor; | ||
187 | return substr($$data, $offset, $len); | ||
188 | } | ||
189 | |||
190 | ############################################################################### | ||
191 | # | ||
192 | # Roughly parse the X.509 certificate | ||
193 | # | ||
194 | ############################################################################### | ||
195 | my $cursor = [ 0, length($x509_certificate), \$x509_certificate ]; | ||
196 | |||
197 | my $cert = asn1_extract($cursor, $UNIV | $CONS | $SEQUENCE); | ||
198 | my $tbs = asn1_extract($cert->[1], $UNIV | $CONS | $SEQUENCE); | ||
199 | my $version = asn1_extract($tbs->[1], $CONT | $CONS | 0, 1); | ||
200 | my $serial_number = asn1_extract($tbs->[1], $UNIV | $INTEGER); | ||
201 | my $sig_type = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
202 | my $issuer = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
203 | my $validity = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
204 | my $subject = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
205 | my $key = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
206 | my $issuer_uid = asn1_extract($tbs->[1], $CONT | $CONS | 1, 1); | ||
207 | my $subject_uid = asn1_extract($tbs->[1], $CONT | $CONS | 2, 1); | ||
208 | my $extension_list = asn1_extract($tbs->[1], $CONT | $CONS | 3, 1); | ||
209 | |||
210 | my $subject_key_id = (); | ||
211 | my $authority_key_id = (); | ||
212 | |||
213 | # | ||
214 | # Parse the extension list | ||
215 | # | ||
216 | if ($extension_list->[0] != -1) { | ||
217 | my $extensions = asn1_extract($extension_list->[1], $UNIV | $CONS | $SEQUENCE); | ||
218 | |||
219 | while ($extensions->[1]->[1] > 0) { | ||
220 | my $ext = asn1_extract($extensions->[1], $UNIV | $CONS | $SEQUENCE); | ||
221 | my $x_oid = asn1_extract($ext->[1], $UNIV | $OBJ_ID); | ||
222 | my $x_crit = asn1_extract($ext->[1], $UNIV | $BOOLEAN, 1); | ||
223 | my $x_val = asn1_extract($ext->[1], $UNIV | $OCTET_STRING); | ||
224 | |||
225 | my $raw_oid = asn1_retrieve($x_oid->[1]); | ||
226 | next if (!exists($OIDs{$raw_oid})); | ||
227 | my $x_type = $OIDs{$raw_oid}; | ||
228 | |||
229 | my $raw_value = asn1_retrieve($x_val->[1]); | ||
230 | |||
231 | if ($x_type eq "subjectKeyIdentifier") { | ||
232 | my $vcursor = [ 0, length($raw_value), \$raw_value ]; | ||
233 | |||
234 | $subject_key_id = asn1_extract($vcursor, $UNIV | $OCTET_STRING); | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | |||
239 | ############################################################################### | ||
240 | # | ||
241 | # Determine what we're going to use as the signer's name. In order of | ||
242 | # preference, take one of: commonName, organizationName or emailAddress. | ||
243 | # | ||
244 | ############################################################################### | ||
245 | my $org = ""; | ||
246 | my $cn = ""; | ||
247 | my $email = ""; | ||
248 | |||
249 | while ($subject->[1]->[1] > 0) { | ||
250 | my $rdn = asn1_extract($subject->[1], $UNIV | $CONS | $SET); | ||
251 | my $attr = asn1_extract($rdn->[1], $UNIV | $CONS | $SEQUENCE); | ||
252 | my $n_oid = asn1_extract($attr->[1], $UNIV | $OBJ_ID); | ||
253 | my $n_val = asn1_extract($attr->[1], -1); | ||
254 | |||
255 | my $raw_oid = asn1_retrieve($n_oid->[1]); | ||
256 | next if (!exists($OIDs{$raw_oid})); | ||
257 | my $n_type = $OIDs{$raw_oid}; | ||
258 | |||
259 | my $raw_value = asn1_retrieve($n_val->[1]); | ||
260 | |||
261 | if ($n_type eq "organizationName") { | ||
262 | $org = $raw_value; | ||
263 | } elsif ($n_type eq "commonName") { | ||
264 | $cn = $raw_value; | ||
265 | } elsif ($n_type eq "emailAddress") { | ||
266 | $email = $raw_value; | ||
267 | } | ||
268 | } | ||
269 | |||
270 | my $signers_name = $email; | ||
271 | |||
272 | if ($org && $cn) { | ||
273 | # Don't use the organizationName if the commonName repeats it | ||
274 | if (length($org) <= length($cn) && | ||
275 | substr($cn, 0, length($org)) eq $org) { | ||
276 | $signers_name = $cn; | ||
277 | goto got_id_name; | ||
278 | } | ||
279 | |||
280 | # Or a signifcant chunk of it | ||
281 | if (length($org) >= 7 && | ||
282 | length($cn) >= 7 && | ||
283 | substr($cn, 0, 7) eq substr($org, 0, 7)) { | ||
284 | $signers_name = $cn; | ||
285 | goto got_id_name; | ||
286 | } | ||
287 | |||
288 | $signers_name = $org . ": " . $cn; | ||
289 | } elsif ($org) { | ||
290 | $signers_name = $org; | ||
291 | } elsif ($cn) { | ||
292 | $signers_name = $cn; | ||
293 | } | ||
294 | |||
295 | got_id_name: | ||
296 | |||
297 | die $x509, ": ", "X.509: Couldn't find the Subject Key Identifier extension\n" | ||
298 | if (!$subject_key_id); | ||
299 | |||
300 | my $key_identifier = asn1_retrieve($subject_key_id->[1]); | ||
301 | |||
302 | ############################################################################### | ||
303 | # | ||
304 | # Create and attach the module signature | ||
305 | # | ||
306 | ############################################################################### | ||
307 | |||
308 | # | ||
309 | # Signature parameters | ||
310 | # | ||
311 | my $algo = 1; # Public-key crypto algorithm: RSA | ||
312 | my $hash = 0; # Digest algorithm | ||
313 | my $id_type = 1; # Identifier type: X.509 | ||
314 | |||
315 | # | ||
316 | # Digest the data | ||
317 | # | ||
318 | my $prologue; | ||
319 | if ($dgst eq "sha1") { | ||
320 | $prologue = pack("C*", | ||
321 | 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, | ||
322 | 0x2B, 0x0E, 0x03, 0x02, 0x1A, | ||
323 | 0x05, 0x00, 0x04, 0x14); | ||
324 | $hash = 2; | ||
325 | } elsif ($dgst eq "sha224") { | ||
326 | $prologue = pack("C*", | ||
327 | 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, | ||
328 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, | ||
329 | 0x05, 0x00, 0x04, 0x1C); | ||
330 | $hash = 7; | ||
331 | } elsif ($dgst eq "sha256") { | ||
332 | $prologue = pack("C*", | ||
333 | 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, | ||
334 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, | ||
335 | 0x05, 0x00, 0x04, 0x20); | ||
336 | $hash = 4; | ||
337 | } elsif ($dgst eq "sha384") { | ||
338 | $prologue = pack("C*", | ||
339 | 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, | ||
340 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, | ||
341 | 0x05, 0x00, 0x04, 0x30); | ||
342 | $hash = 5; | ||
343 | } elsif ($dgst eq "sha512") { | ||
344 | $prologue = pack("C*", | ||
345 | 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, | ||
346 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, | ||
347 | 0x05, 0x00, 0x04, 0x40); | ||
348 | $hash = 6; | ||
349 | } else { | ||
350 | die "Unknown hash algorithm: $dgst\n"; | ||
351 | } | ||
352 | |||
353 | my $signature; | ||
354 | if ($signature_file) { | ||
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 | } | ||
381 | $signature = pack("n", length($signature)) . $signature, | ||
382 | |||
383 | # | ||
384 | # Build the signed binary | ||
385 | # | ||
386 | my $unsigned_module = read_file($module); | ||
387 | |||
388 | my $magic_number = "~Module signature appended~\n"; | ||
389 | |||
390 | my $info = pack("CCCCCxxxN", | ||
391 | $algo, $hash, $id_type, | ||
392 | length($signers_name), | ||
393 | length($key_identifier), | ||
394 | length($signature)); | ||
395 | |||
396 | if ($verbose) { | ||
397 | print "Size of unsigned module: ", length($unsigned_module), "\n"; | ||
398 | print "Size of signer's name : ", length($signers_name), "\n"; | ||
399 | print "Size of key identifier : ", length($key_identifier), "\n"; | ||
400 | print "Size of signature : ", length($signature), "\n"; | ||
401 | print "Size of information : ", length($info), "\n"; | ||
402 | print "Size of magic number : ", length($magic_number), "\n"; | ||
403 | print "Signer's name : '", $signers_name, "'\n"; | ||
404 | print "Digest : $dgst\n"; | ||
405 | } | ||
406 | |||
407 | open(FD, ">$dest") || die $dest; | ||
408 | binmode FD; | ||
409 | print FD | ||
410 | $unsigned_module, | ||
411 | $signers_name, | ||
412 | $key_identifier, | ||
413 | $signature, | ||
414 | $info, | ||
415 | $magic_number | ||
416 | ; | ||
417 | close FD || die $dest; | ||
418 | |||
419 | if (!$keep_orig) { | ||
420 | rename($dest, $module) || die $module; | ||
421 | } | ||