Commit | Line | Data |
---|---|---|
b56e5a17 DH |
1 | /* System trusted keyring for trusted public keys |
2 | * | |
3 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
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. | |
10 | */ | |
11 | ||
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> | |
091f6e26 | 19 | #include <crypto/pkcs7.h> |
b56e5a17 DH |
20 | |
21 | struct key *system_trusted_keyring; | |
22 | EXPORT_SYMBOL_GPL(system_trusted_keyring); | |
23 | ||
24 | extern __initconst const u8 system_certificate_list[]; | |
62226983 | 25 | extern __initconst const unsigned long system_certificate_list_size; |
b56e5a17 DH |
26 | |
27 | /* | |
28 | * Load the compiled-in keys | |
29 | */ | |
30 | static __init int system_trusted_keyring_init(void) | |
31 | { | |
32 | pr_notice("Initialise system trusted keyring\n"); | |
33 | ||
34 | system_trusted_keyring = | |
35 | keyring_alloc(".system_keyring", | |
36 | KUIDT_INIT(0), KGIDT_INIT(0), current_cred(), | |
37 | ((KEY_POS_ALL & ~KEY_POS_SETATTR) | | |
af34cb0c | 38 | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH), |
5ac7eace DH |
39 | KEY_ALLOC_NOT_IN_QUOTA, |
40 | keyring_restrict_trusted_only, NULL); | |
b56e5a17 DH |
41 | if (IS_ERR(system_trusted_keyring)) |
42 | panic("Can't allocate system trusted keyring\n"); | |
b56e5a17 DH |
43 | return 0; |
44 | } | |
45 | ||
46 | /* | |
47 | * Must be initialised before we try and load the keys into the keyring. | |
48 | */ | |
49 | device_initcall(system_trusted_keyring_init); | |
50 | ||
51 | /* | |
52 | * Load the compiled-in list of X.509 certificates. | |
53 | */ | |
54 | static __init int load_system_certificate_list(void) | |
55 | { | |
56 | key_ref_t key; | |
57 | const u8 *p, *end; | |
58 | size_t plen; | |
59 | ||
60 | pr_notice("Loading compiled-in X.509 certificates\n"); | |
61 | ||
b56e5a17 | 62 | p = system_certificate_list; |
62226983 | 63 | end = p + system_certificate_list_size; |
b56e5a17 DH |
64 | while (p < end) { |
65 | /* Each cert begins with an ASN.1 SEQUENCE tag and must be more | |
66 | * than 256 bytes in size. | |
67 | */ | |
68 | if (end - p < 4) | |
69 | goto dodgy_cert; | |
70 | if (p[0] != 0x30 && | |
71 | p[1] != 0x82) | |
72 | goto dodgy_cert; | |
73 | plen = (p[2] << 8) | p[3]; | |
74 | plen += 4; | |
75 | if (plen > end - p) | |
76 | goto dodgy_cert; | |
77 | ||
78 | key = key_create_or_update(make_key_ref(system_trusted_keyring, 1), | |
79 | "asymmetric", | |
80 | NULL, | |
81 | p, | |
82 | plen, | |
af34cb0c MZ |
83 | ((KEY_POS_ALL & ~KEY_POS_SETATTR) | |
84 | KEY_USR_VIEW | KEY_USR_READ), | |
008643b8 | 85 | KEY_ALLOC_NOT_IN_QUOTA | |
5d2787cf | 86 | KEY_ALLOC_TRUSTED | |
5ac7eace DH |
87 | KEY_ALLOC_BUILT_IN | |
88 | KEY_ALLOC_BYPASS_RESTRICTION); | |
b56e5a17 DH |
89 | if (IS_ERR(key)) { |
90 | pr_err("Problem loading in-kernel X.509 certificate (%ld)\n", | |
91 | PTR_ERR(key)); | |
92 | } else { | |
93 | pr_notice("Loaded X.509 cert '%s'\n", | |
94 | key_ref_to_ptr(key)->description); | |
95 | key_ref_put(key); | |
96 | } | |
97 | p += plen; | |
98 | } | |
99 | ||
100 | return 0; | |
101 | ||
102 | dodgy_cert: | |
103 | pr_err("Problem parsing in-kernel X.509 certificate list\n"); | |
104 | return 0; | |
105 | } | |
106 | late_initcall(load_system_certificate_list); | |
091f6e26 DH |
107 | |
108 | #ifdef CONFIG_SYSTEM_DATA_VERIFICATION | |
109 | ||
110 | /** | |
e68503bd DH |
111 | * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data. |
112 | * @data: The data to be verified (NULL if expecting internal data). | |
091f6e26 DH |
113 | * @len: Size of @data. |
114 | * @raw_pkcs7: The PKCS#7 message that is the signature. | |
115 | * @pkcs7_len: The size of @raw_pkcs7. | |
e68503bd | 116 | * @trusted_keys: Trusted keys to use (NULL for system_trusted_keyring). |
99db4435 | 117 | * @usage: The use to which the key is being put. |
e68503bd DH |
118 | * @view_content: Callback to gain access to content. |
119 | * @ctx: Context for callback. | |
091f6e26 | 120 | */ |
e68503bd DH |
121 | int verify_pkcs7_signature(const void *data, size_t len, |
122 | const void *raw_pkcs7, size_t pkcs7_len, | |
123 | struct key *trusted_keys, | |
e68503bd DH |
124 | enum key_being_used_for usage, |
125 | int (*view_content)(void *ctx, | |
126 | const void *data, size_t len, | |
127 | size_t asn1hdrlen), | |
128 | void *ctx) | |
091f6e26 DH |
129 | { |
130 | struct pkcs7_message *pkcs7; | |
091f6e26 DH |
131 | int ret; |
132 | ||
133 | pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len); | |
134 | if (IS_ERR(pkcs7)) | |
135 | return PTR_ERR(pkcs7); | |
136 | ||
137 | /* The data should be detached - so we need to supply it. */ | |
e68503bd | 138 | if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) { |
091f6e26 DH |
139 | pr_err("PKCS#7 signature with non-detached data\n"); |
140 | ret = -EBADMSG; | |
141 | goto error; | |
142 | } | |
143 | ||
99db4435 | 144 | ret = pkcs7_verify(pkcs7, usage); |
091f6e26 DH |
145 | if (ret < 0) |
146 | goto error; | |
147 | ||
e68503bd DH |
148 | if (!trusted_keys) |
149 | trusted_keys = system_trusted_keyring; | |
bda850cd DH |
150 | ret = pkcs7_validate_trust(pkcs7, trusted_keys); |
151 | if (ret < 0) { | |
152 | if (ret == -ENOKEY) | |
153 | pr_err("PKCS#7 signature not signed with a trusted key\n"); | |
e68503bd DH |
154 | goto error; |
155 | } | |
156 | ||
157 | if (view_content) { | |
158 | size_t asn1hdrlen; | |
159 | ||
160 | ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen); | |
161 | if (ret < 0) { | |
162 | if (ret == -ENODATA) | |
163 | pr_devel("PKCS#7 message does not contain data\n"); | |
164 | goto error; | |
165 | } | |
166 | ||
167 | ret = view_content(ctx, data, len, asn1hdrlen); | |
091f6e26 DH |
168 | } |
169 | ||
170 | error: | |
171 | pkcs7_free_message(pkcs7); | |
172 | pr_devel("<==%s() = %d\n", __func__, ret); | |
173 | return ret; | |
174 | } | |
e68503bd | 175 | EXPORT_SYMBOL_GPL(verify_pkcs7_signature); |
091f6e26 DH |
176 | |
177 | #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */ |