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 static struct key
*system_trusted_keyring
;
23 extern __initconst
const u8 system_certificate_list
[];
24 extern __initconst
const unsigned long system_certificate_list_size
;
27 * restrict_link_by_builtin_trusted - Restrict keyring addition by system CA
29 * Restrict the addition of keys into a keyring based on the key-to-be-added
30 * being vouched for by a key in the system keyring.
32 int restrict_link_by_builtin_trusted(struct key
*keyring
,
33 const struct key_type
*type
,
35 const union key_payload
*payload
)
37 return restrict_link_by_signature(system_trusted_keyring
,
42 * Load the compiled-in keys
44 static __init
int system_trusted_keyring_init(void)
46 pr_notice("Initialise system trusted keyring\n");
48 system_trusted_keyring
=
49 keyring_alloc(".system_keyring",
50 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
51 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
52 KEY_USR_VIEW
| KEY_USR_READ
| KEY_USR_SEARCH
),
53 KEY_ALLOC_NOT_IN_QUOTA
,
54 restrict_link_by_builtin_trusted
, NULL
);
55 if (IS_ERR(system_trusted_keyring
))
56 panic("Can't allocate system trusted keyring\n");
61 * Must be initialised before we try and load the keys into the keyring.
63 device_initcall(system_trusted_keyring_init
);
66 * Load the compiled-in list of X.509 certificates.
68 static __init
int load_system_certificate_list(void)
74 pr_notice("Loading compiled-in X.509 certificates\n");
76 p
= system_certificate_list
;
77 end
= p
+ system_certificate_list_size
;
79 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
80 * than 256 bytes in size.
87 plen
= (p
[2] << 8) | p
[3];
92 key
= key_create_or_update(make_key_ref(system_trusted_keyring
, 1),
97 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
98 KEY_USR_VIEW
| KEY_USR_READ
),
99 KEY_ALLOC_NOT_IN_QUOTA
|
102 KEY_ALLOC_BYPASS_RESTRICTION
);
104 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
107 pr_notice("Loaded X.509 cert '%s'\n",
108 key_ref_to_ptr(key
)->description
);
117 pr_err("Problem parsing in-kernel X.509 certificate list\n");
120 late_initcall(load_system_certificate_list
);
122 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
125 * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
126 * @data: The data to be verified (NULL if expecting internal data).
127 * @len: Size of @data.
128 * @raw_pkcs7: The PKCS#7 message that is the signature.
129 * @pkcs7_len: The size of @raw_pkcs7.
130 * @trusted_keys: Trusted keys to use (NULL for system_trusted_keyring).
131 * @usage: The use to which the key is being put.
132 * @view_content: Callback to gain access to content.
133 * @ctx: Context for callback.
135 int verify_pkcs7_signature(const void *data
, size_t len
,
136 const void *raw_pkcs7
, size_t pkcs7_len
,
137 struct key
*trusted_keys
,
138 enum key_being_used_for usage
,
139 int (*view_content
)(void *ctx
,
140 const void *data
, size_t len
,
144 struct pkcs7_message
*pkcs7
;
147 pkcs7
= pkcs7_parse_message(raw_pkcs7
, pkcs7_len
);
149 return PTR_ERR(pkcs7
);
151 /* The data should be detached - so we need to supply it. */
152 if (data
&& pkcs7_supply_detached_data(pkcs7
, data
, len
) < 0) {
153 pr_err("PKCS#7 signature with non-detached data\n");
158 ret
= pkcs7_verify(pkcs7
, usage
);
163 trusted_keys
= system_trusted_keyring
;
164 ret
= pkcs7_validate_trust(pkcs7
, trusted_keys
);
167 pr_err("PKCS#7 signature not signed with a trusted key\n");
174 ret
= pkcs7_get_content_data(pkcs7
, &data
, &len
, &asn1hdrlen
);
177 pr_devel("PKCS#7 message does not contain data\n");
181 ret
= view_content(ctx
, data
, len
, asn1hdrlen
);
185 pkcs7_free_message(pkcs7
);
186 pr_devel("<==%s() = %d\n", __func__
, ret
);
189 EXPORT_SYMBOL_GPL(verify_pkcs7_signature
);
191 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */