1 /* System trusted keyring for trusted public keys
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/cred.h>
16 #include <linux/err.h>
17 #include <keys/asymmetric-type.h>
18 #include <keys/system_keyring.h>
19 #include <crypto/pkcs7.h>
21 struct key
*system_trusted_keyring
;
22 EXPORT_SYMBOL_GPL(system_trusted_keyring
);
23 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
24 struct key
*system_blacklist_keyring
;
27 extern __initconst
const u8 system_certificate_list
[];
28 extern __initconst
const unsigned long system_certificate_list_size
;
31 * Load the compiled-in keys
33 static __init
int system_trusted_keyring_init(void)
35 pr_notice("Initialise system trusted keyring\n");
37 system_trusted_keyring
=
38 keyring_alloc(".system_keyring",
39 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
40 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
41 KEY_USR_VIEW
| KEY_USR_READ
| KEY_USR_SEARCH
),
42 KEY_ALLOC_NOT_IN_QUOTA
, NULL
);
43 if (IS_ERR(system_trusted_keyring
))
44 panic("Can't allocate system trusted keyring\n");
46 set_bit(KEY_FLAG_TRUSTED_ONLY
, &system_trusted_keyring
->flags
);
48 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
49 system_blacklist_keyring
= keyring_alloc(".system_blacklist_keyring",
50 KUIDT_INIT(0), KGIDT_INIT(0),
52 (KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
53 KEY_USR_VIEW
| KEY_USR_READ
,
54 KEY_ALLOC_NOT_IN_QUOTA
, NULL
);
55 if (IS_ERR(system_blacklist_keyring
))
56 panic("Can't allocate system blacklist keyring\n");
58 set_bit(KEY_FLAG_TRUSTED_ONLY
, &system_blacklist_keyring
->flags
);
65 * Must be initialised before we try and load the keys into the keyring.
67 device_initcall(system_trusted_keyring_init
);
70 * Load the compiled-in list of X.509 certificates.
72 static __init
int load_system_certificate_list(void)
78 pr_notice("Loading compiled-in X.509 certificates\n");
80 p
= system_certificate_list
;
81 end
= p
+ system_certificate_list_size
;
83 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
84 * than 256 bytes in size.
91 plen
= (p
[2] << 8) | p
[3];
96 key
= key_create_or_update(make_key_ref(system_trusted_keyring
, 1),
101 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
102 KEY_USR_VIEW
| KEY_USR_READ
),
103 KEY_ALLOC_NOT_IN_QUOTA
|
107 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
111 pr_notice("Loaded X.509 cert '%s'\n",
112 key_ref_to_ptr(key
)->description
);
121 pr_err("Problem parsing in-kernel X.509 certificate list\n");
124 late_initcall(load_system_certificate_list
);
126 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
129 * Verify a PKCS#7-based signature on system data.
130 * @data: The data to be verified.
131 * @len: Size of @data.
132 * @raw_pkcs7: The PKCS#7 message that is the signature.
133 * @pkcs7_len: The size of @raw_pkcs7.
134 * @usage: The use to which the key is being put.
136 int system_verify_data(const void *data
, unsigned long len
,
137 const void *raw_pkcs7
, size_t pkcs7_len
,
138 enum key_being_used_for usage
)
140 struct pkcs7_message
*pkcs7
;
144 pkcs7
= pkcs7_parse_message(raw_pkcs7
, pkcs7_len
);
146 return PTR_ERR(pkcs7
);
148 /* The data should be detached - so we need to supply it. */
149 if (pkcs7_supply_detached_data(pkcs7
, data
, len
) < 0) {
150 pr_err("PKCS#7 signature with non-detached data\n");
155 ret
= pkcs7_verify(pkcs7
, usage
);
159 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
160 ret
= pkcs7_validate_trust(pkcs7
, system_blacklist_keyring
, &trusted
);
162 /* module is signed with a cert in the blacklist. reject */
163 pr_err("Module key is in the blacklist\n");
169 ret
= pkcs7_validate_trust(pkcs7
, system_trusted_keyring
, &trusted
);
174 pr_err("PKCS#7 signature not signed with a trusted key\n");
179 pkcs7_free_message(pkcs7
);
180 pr_devel("<==%s() = %d\n", __func__
, ret
);
183 EXPORT_SYMBOL_GPL(system_verify_data
);
185 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */