Skip to content

Commit e4fc02f

Browse files
author
James Morris
committed
Merge tag 'modsign-pkcs7-20150812-3' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs into next
2 parents aa62eff + e9a5e8c commit e4fc02f

35 files changed

+1597
-928
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ GTAGS
9797
# Leavings from module signing
9898
#
9999
extra_certificates
100+
signing_key.pem
100101
signing_key.priv
101102
signing_key.x509
102103
x509.genkey

Documentation/kbuild/kbuild.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ The output directory is often set using "O=..." on the commandline.
174174

175175
The value can be overridden in which case the default value is ignored.
176176

177+
KBUILD_SIGN_PIN
178+
--------------------------------------------------
179+
This variable allows a passphrase or PIN to be passed to the sign-file
180+
utility when signing kernel modules, if the private key requires such.
181+
177182
KBUILD_MODPOST_WARN
178183
--------------------------------------------------
179184
KBUILD_MODPOST_WARN can be set to avoid errors in case of undefined

Documentation/module-signing.txt

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,32 @@ This has a number of options available:
8989
their signatures checked without causing a dependency loop.
9090

9191

92+
(4) "File name or PKCS#11 URI of module signing key" (CONFIG_MODULE_SIG_KEY)
93+
94+
Setting this option to something other than its default of
95+
"signing_key.pem" will disable the autogeneration of signing keys and
96+
allow the kernel modules to be signed with a key of your choosing.
97+
The string provided should identify a file containing both a private
98+
key and its corresponding X.509 certificate in PEM form, or — on
99+
systems where the OpenSSL ENGINE_pkcs11 is functional — a PKCS#11 URI
100+
as defined by RFC7512. In the latter case, the PKCS#11 URI should
101+
reference both a certificate and a private key.
102+
103+
If the PEM file containing the private key is encrypted, or if the
104+
PKCS#11 token requries a PIN, this can be provided at build time by
105+
means of the KBUILD_SIGN_PIN variable.
106+
107+
108+
(5) "Additional X.509 keys for default system keyring" (CONFIG_SYSTEM_TRUSTED_KEYS)
109+
110+
This option can be set to the filename of a PEM-encoded file containing
111+
additional certificates which will be included in the system keyring by
112+
default.
113+
114+
Note that enabling module signing adds a dependency on the OpenSSL devel
115+
packages to the kernel build processes for the tool that does the signing.
116+
117+
92118
=======================
93119
GENERATING SIGNING KEYS
94120
=======================
@@ -100,11 +126,11 @@ it can be deleted or stored securely. The public key gets built into the
100126
kernel so that it can be used to check the signatures as the modules are
101127
loaded.
102128

103-
Under normal conditions, the kernel build will automatically generate a new
104-
keypair using openssl if one does not exist in the files:
129+
Under normal conditions, when CONFIG_MODULE_SIG_KEY is unchanged from its
130+
default, the kernel build will automatically generate a new keypair using
131+
openssl if one does not exist in the file:
105132

106-
signing_key.priv
107-
signing_key.x509
133+
signing_key.pem
108134

109135
during the building of vmlinux (the public part of the key needs to be built
110136
into vmlinux) using parameters in the:
@@ -135,8 +161,12 @@ kernel sources tree and the openssl command. The following is an example to
135161
generate the public/private key files:
136162

137163
openssl req -new -nodes -utf8 -sha256 -days 36500 -batch -x509 \
138-
-config x509.genkey -outform DER -out signing_key.x509 \
139-
-keyout signing_key.priv
164+
-config x509.genkey -outform PEM -out kernel_key.pem \
165+
-keyout kernel_key.pem
166+
167+
The full pathname for the resulting kernel_key.pem file can then be specified
168+
in the CONFIG_MODULE_SIG_KEY option, and the certificate and key therein will
169+
be used instead of an autogenerated keypair.
140170

141171

142172
=========================
@@ -152,10 +182,9 @@ in a keyring called ".system_keyring" that can be seen by:
152182
302d2d52 I------ 1 perm 1f010000 0 0 asymmetri Fedora kernel signing key: d69a84e6bce3d216b979e9505b3e3ef9a7118079: X509.RSA a7118079 []
153183
...
154184

155-
Beyond the public key generated specifically for module signing, any file
156-
placed in the kernel source root directory or the kernel build root directory
157-
whose name is suffixed with ".x509" will be assumed to be an X.509 public key
158-
and will be added to the keyring.
185+
Beyond the public key generated specifically for module signing, additional
186+
trusted certificates can be provided in a PEM-encoded file referenced by the
187+
CONFIG_SYSTEM_TRUSTED_KEYS configuration option.
159188

160189
Further, the architecture code may take public keys from a hardware store and
161190
add those in also (e.g. from the UEFI key database).
@@ -181,7 +210,7 @@ To manually sign a module, use the scripts/sign-file tool available in
181210
the Linux kernel source tree. The script requires 4 arguments:
182211

183212
1. The hash algorithm (e.g., sha256)
184-
2. The private key filename
213+
2. The private key filename or PKCS#11 URI
185214
3. The public key filename
186215
4. The kernel module to be signed
187216

@@ -194,6 +223,9 @@ The hash algorithm used does not have to match the one configured, but if it
194223
doesn't, you should make sure that hash algorithm is either built into the
195224
kernel or can be loaded without requiring itself.
196225

226+
If the private key requires a passphrase or PIN, it can be provided in the
227+
$KBUILD_SIGN_PIN environment variable.
228+
197229

198230
============================
199231
SIGNED MODULES AND STRIPPING

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,10 +870,10 @@ INITRD_COMPRESS-$(CONFIG_RD_LZ4) := lz4
870870
# export INITRD_COMPRESS := $(INITRD_COMPRESS-y)
871871

872872
ifdef CONFIG_MODULE_SIG_ALL
873-
MODSECKEY = ./signing_key.priv
873+
MODSECKEY = $(CONFIG_MODULE_SIG_KEY)
874874
MODPUBKEY = ./signing_key.x509
875875
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)
877877
else
878878
mod_sign_cmd = true
879879
endif
@@ -1173,8 +1173,8 @@ MRPROPER_DIRS += include/config usr/include include/generated \
11731173
arch/*/include/generated .tmp_objdiff
11741174
MRPROPER_FILES += .config .config.old .version .old_version \
11751175
Module.symvers tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS \
1176-
signing_key.priv signing_key.x509 x509.genkey \
1177-
extra_certificates signing_key.x509.keyid \
1176+
signing_key.pem signing_key.priv signing_key.x509 \
1177+
x509.genkey extra_certificates signing_key.x509.keyid \
11781178
signing_key.x509.signer vmlinux-gdb.py
11791179

11801180
# clean - Delete most, but leave enough to build external modules

arch/x86/kernel/kexec-bzimage64.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
536536
int ret;
537537

538538
ret = verify_pefile_signature(kernel, kernel_len,
539-
system_trusted_keyring, &trusted);
539+
system_trusted_keyring,
540+
VERIFYING_KEXEC_PE_SIGNATURE,
541+
&trusted);
540542
if (ret < 0)
541543
return ret;
542544
if (!trusted)

crypto/asymmetric_keys/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ obj-$(CONFIG_PUBLIC_KEY_ALGO_RSA) += rsa.o
1515
obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o
1616
x509_key_parser-y := \
1717
x509-asn1.o \
18+
x509_akid-asn1.o \
1819
x509_rsakey-asn1.o \
1920
x509_cert_parser.o \
2021
x509_public_key.o
2122

22-
$(obj)/x509_cert_parser.o: $(obj)/x509-asn1.h $(obj)/x509_rsakey-asn1.h
23+
$(obj)/x509_cert_parser.o: \
24+
$(obj)/x509-asn1.h \
25+
$(obj)/x509_akid-asn1.h \
26+
$(obj)/x509_rsakey-asn1.h
2327
$(obj)/x509-asn1.o: $(obj)/x509-asn1.c $(obj)/x509-asn1.h
28+
$(obj)/x509_akid-asn1.o: $(obj)/x509_akid-asn1.c $(obj)/x509_akid-asn1.h
2429
$(obj)/x509_rsakey-asn1.o: $(obj)/x509_rsakey-asn1.c $(obj)/x509_rsakey-asn1.h
2530

2631
clean-files += x509-asn1.c x509-asn1.h
32+
clean-files += x509_akid-asn1.c x509_akid-asn1.h
2733
clean-files += x509_rsakey-asn1.c x509_rsakey-asn1.h
2834

2935
#

crypto/asymmetric_keys/asymmetric_type.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
#include <keys/asymmetric-subtype.h>
1414
#include <keys/asymmetric-parser.h>
15+
#include <crypto/public_key.h>
1516
#include <linux/seq_file.h>
1617
#include <linux/module.h>
1718
#include <linux/slab.h>
@@ -20,6 +21,16 @@
2021

2122
MODULE_LICENSE("GPL");
2223

24+
const char *const key_being_used_for[NR__KEY_BEING_USED_FOR] = {
25+
[VERIFYING_MODULE_SIGNATURE] = "mod sig",
26+
[VERIFYING_FIRMWARE_SIGNATURE] = "firmware sig",
27+
[VERIFYING_KEXEC_PE_SIGNATURE] = "kexec PE sig",
28+
[VERIFYING_KEY_SIGNATURE] = "key sig",
29+
[VERIFYING_KEY_SELF_SIGNATURE] = "key self sig",
30+
[VERIFYING_UNSPECIFIED_SIGNATURE] = "unspec sig",
31+
};
32+
EXPORT_SYMBOL_GPL(key_being_used_for);
33+
2334
static LIST_HEAD(asymmetric_key_parsers);
2435
static DECLARE_RWSEM(asymmetric_key_parsers_sem);
2536

crypto/asymmetric_keys/pkcs7.asn1

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
PKCS7ContentInfo ::= SEQUENCE {
2-
contentType ContentType,
2+
contentType ContentType ({ pkcs7_check_content_type }),
33
content [0] EXPLICIT SignedData OPTIONAL
44
}
55

66
ContentType ::= OBJECT IDENTIFIER ({ pkcs7_note_OID })
77

88
SignedData ::= SEQUENCE {
9-
version INTEGER,
9+
version INTEGER ({ pkcs7_note_signeddata_version }),
1010
digestAlgorithms DigestAlgorithmIdentifiers,
11-
contentInfo ContentInfo,
11+
contentInfo ContentInfo ({ pkcs7_note_content }),
1212
certificates CHOICE {
1313
certSet [0] IMPLICIT ExtendedCertificatesAndCertificates,
1414
certSequence [2] IMPLICIT Certificates
@@ -21,7 +21,7 @@ SignedData ::= SEQUENCE {
2121
}
2222

2323
ContentInfo ::= SEQUENCE {
24-
contentType ContentType,
24+
contentType ContentType ({ pkcs7_note_OID }),
2525
content [0] EXPLICIT Data OPTIONAL
2626
}
2727

@@ -68,8 +68,8 @@ SignerInfos ::= CHOICE {
6868
}
6969

7070
SignerInfo ::= SEQUENCE {
71-
version INTEGER,
72-
issuerAndSerialNumber IssuerAndSerialNumber,
71+
version INTEGER ({ pkcs7_note_signerinfo_version }),
72+
sid SignerIdentifier, -- CMS variant, not PKCS#7
7373
digestAlgorithm DigestAlgorithmIdentifier ({ pkcs7_sig_note_digest_algo }),
7474
authenticatedAttributes CHOICE {
7575
aaSet [0] IMPLICIT SetOfAuthenticatedAttribute
@@ -88,13 +88,21 @@ SignerInfo ::= SEQUENCE {
8888
} OPTIONAL
8989
} ({ pkcs7_note_signed_info })
9090

91+
SignerIdentifier ::= CHOICE {
92+
-- RFC5652 sec 5.3
93+
issuerAndSerialNumber IssuerAndSerialNumber,
94+
subjectKeyIdentifier [0] IMPLICIT SubjectKeyIdentifier
95+
}
96+
9197
IssuerAndSerialNumber ::= SEQUENCE {
9298
issuer Name ({ pkcs7_sig_note_issuer }),
9399
serialNumber CertificateSerialNumber ({ pkcs7_sig_note_serial })
94100
}
95101

96102
CertificateSerialNumber ::= INTEGER
97103

104+
SubjectKeyIdentifier ::= OCTET STRING ({ pkcs7_sig_note_skid })
105+
98106
SetOfAuthenticatedAttribute ::= SET OF AuthenticatedAttribute
99107

100108
AuthenticatedAttribute ::= SEQUENCE {
@@ -103,7 +111,7 @@ AuthenticatedAttribute ::= SEQUENCE {
103111
}
104112

105113
UnauthenticatedAttribute ::= SEQUENCE {
106-
type OBJECT IDENTIFIER ({ pkcs7_note_OID }),
114+
type OBJECT IDENTIFIER,
107115
values SET OF ANY
108116
}
109117

crypto/asymmetric_keys/pkcs7_key_type.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@
1414
#include <linux/err.h>
1515
#include <linux/module.h>
1616
#include <linux/key-type.h>
17+
#include <keys/asymmetric-type.h>
1718
#include <crypto/pkcs7.h>
1819
#include <keys/user-type.h>
1920
#include <keys/system_keyring.h>
2021
#include "pkcs7_parser.h"
2122

23+
MODULE_LICENSE("GPL");
24+
MODULE_DESCRIPTION("PKCS#7 testing key type");
25+
26+
static unsigned pkcs7_usage;
27+
module_param_named(usage, pkcs7_usage, uint, S_IWUSR | S_IRUGO);
28+
MODULE_PARM_DESC(pkcs7_usage,
29+
"Usage to specify when verifying the PKCS#7 message");
30+
2231
/*
2332
* Preparse a PKCS#7 wrapped and validated data blob.
2433
*/
2534
static int pkcs7_preparse(struct key_preparsed_payload *prep)
2635
{
36+
enum key_being_used_for usage = pkcs7_usage;
2737
struct pkcs7_message *pkcs7;
2838
const void *data, *saved_prep_data;
2939
size_t datalen, saved_prep_datalen;
@@ -32,6 +42,11 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep)
3242

3343
kenter("");
3444

45+
if (usage >= NR__KEY_BEING_USED_FOR) {
46+
pr_err("Invalid usage type %d\n", usage);
47+
return -EINVAL;
48+
}
49+
3550
saved_prep_data = prep->data;
3651
saved_prep_datalen = prep->datalen;
3752
pkcs7 = pkcs7_parse_message(saved_prep_data, saved_prep_datalen);
@@ -40,7 +55,7 @@ static int pkcs7_preparse(struct key_preparsed_payload *prep)
4055
goto error;
4156
}
4257

43-
ret = pkcs7_verify(pkcs7);
58+
ret = pkcs7_verify(pkcs7, usage);
4459
if (ret < 0)
4560
goto error_free;
4661

0 commit comments

Comments
 (0)