system_keyring.c 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* System trusted keyring for trusted public keys
 *
 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/cred.h>
#include <linux/err.h>
17
#include <linux/slab.h>
18 19
#include <keys/asymmetric-type.h>
#include <keys/system_keyring.h>
20
#include <crypto/pkcs7.h>
21

22 23 24 25
static struct key *builtin_trusted_keys;
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
static struct key *secondary_trusted_keys;
#endif
26 27

extern __initconst const u8 system_certificate_list[];
28
extern __initconst const unsigned long system_certificate_list_size;
29

30
/**
31
 * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
32 33
 *
 * Restrict the addition of keys into a keyring based on the key-to-be-added
34
 * being vouched for by a key in the built in system keyring.
35
 */
36
int restrict_link_by_builtin_trusted(struct key *dest_keyring,
37
				     const struct key_type *type,
38 39
				     const union key_payload *payload,
				     struct key *restriction_key)
40
{
41 42
	return restrict_link_by_signature(dest_keyring, type, payload,
					  builtin_trusted_keys);
43 44
}

45 46 47 48 49 50 51 52 53 54
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
/**
 * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
 *   addition by both builtin and secondary keyrings
 *
 * Restrict the addition of keys into a keyring based on the key-to-be-added
 * being vouched for by a key in either the built-in or the secondary system
 * keyrings.
 */
int restrict_link_by_builtin_and_secondary_trusted(
55
	struct key *dest_keyring,
56
	const struct key_type *type,
57 58
	const union key_payload *payload,
	struct key *restrict_key)
59 60 61 62 63
{
	/* If we have a secondary trusted keyring, then that contains a link
	 * through to the builtin keyring and the search will follow that link.
	 */
	if (type == &key_type_keyring &&
64
	    dest_keyring == secondary_trusted_keys &&
65 66 67 68
	    payload == &builtin_trusted_keys->payload)
		/* Allow the builtin keyring to be added to the secondary */
		return 0;

69 70
	return restrict_link_by_signature(dest_keyring, type, payload,
					  secondary_trusted_keys);
71
}
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

/**
 * Allocate a struct key_restriction for the "builtin and secondary trust"
 * keyring. Only for use in system_trusted_keyring_init().
 */
static __init struct key_restriction *get_builtin_and_secondary_restriction(void)
{
	struct key_restriction *restriction;

	restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);

	if (!restriction)
		panic("Can't allocate secondary trusted keyring restriction\n");

	restriction->check = restrict_link_by_builtin_and_secondary_trusted;

	return restriction;
}
90 91
#endif

92
/*
93
 * Create the trusted keyrings
94 95 96
 */
static __init int system_trusted_keyring_init(void)
{
97
	pr_notice("Initialise system trusted keyrings\n");
98

99 100
	builtin_trusted_keys =
		keyring_alloc(".builtin_trusted_keys",
101 102
			      KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
			      ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
103
			      KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
104
			      KEY_ALLOC_NOT_IN_QUOTA,
105 106 107 108 109 110 111 112 113 114 115 116
			      NULL, NULL);
	if (IS_ERR(builtin_trusted_keys))
		panic("Can't allocate builtin trusted keyring\n");

#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
	secondary_trusted_keys =
		keyring_alloc(".secondary_trusted_keys",
			      KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
			      ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
			       KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
			       KEY_USR_WRITE),
			      KEY_ALLOC_NOT_IN_QUOTA,
117
			      get_builtin_and_secondary_restriction(),
118 119 120 121 122 123 124 125
			      NULL);
	if (IS_ERR(secondary_trusted_keys))
		panic("Can't allocate secondary trusted keyring\n");

	if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
		panic("Can't link trusted keyrings\n");
#endif

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	return 0;
}

/*
 * Must be initialised before we try and load the keys into the keyring.
 */
device_initcall(system_trusted_keyring_init);

/*
 * Load the compiled-in list of X.509 certificates.
 */
static __init int load_system_certificate_list(void)
{
	key_ref_t key;
	const u8 *p, *end;
	size_t plen;

	pr_notice("Loading compiled-in X.509 certificates\n");

	p = system_certificate_list;
146
	end = p + system_certificate_list_size;
147 148 149 150 151 152 153 154 155 156 157 158 159 160
	while (p < end) {
		/* Each cert begins with an ASN.1 SEQUENCE tag and must be more
		 * than 256 bytes in size.
		 */
		if (end - p < 4)
			goto dodgy_cert;
		if (p[0] != 0x30 &&
		    p[1] != 0x82)
			goto dodgy_cert;
		plen = (p[2] << 8) | p[3];
		plen += 4;
		if (plen > end - p)
			goto dodgy_cert;

161
		key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
162 163 164 165
					   "asymmetric",
					   NULL,
					   p,
					   plen,
166 167
					   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
					   KEY_USR_VIEW | KEY_USR_READ),
168
					   KEY_ALLOC_NOT_IN_QUOTA |
169 170
					   KEY_ALLOC_BUILT_IN |
					   KEY_ALLOC_BYPASS_RESTRICTION);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		if (IS_ERR(key)) {
			pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
			       PTR_ERR(key));
		} else {
			pr_notice("Loaded X.509 cert '%s'\n",
				  key_ref_to_ptr(key)->description);
			key_ref_put(key);
		}
		p += plen;
	}

	return 0;

dodgy_cert:
	pr_err("Problem parsing in-kernel X.509 certificate list\n");
	return 0;
}
late_initcall(load_system_certificate_list);
189 190 191 192

#ifdef CONFIG_SYSTEM_DATA_VERIFICATION

/**
193 194
 * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
 * @data: The data to be verified (NULL if expecting internal data).
195 196 197
 * @len: Size of @data.
 * @raw_pkcs7: The PKCS#7 message that is the signature.
 * @pkcs7_len: The size of @raw_pkcs7.
198 199
 * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
 *					(void *)1UL for all trusted keys).
200
 * @usage: The use to which the key is being put.
201 202
 * @view_content: Callback to gain access to content.
 * @ctx: Context for callback.
203
 */
204 205 206 207 208 209 210 211
int verify_pkcs7_signature(const void *data, size_t len,
			   const void *raw_pkcs7, size_t pkcs7_len,
			   struct key *trusted_keys,
			   enum key_being_used_for usage,
			   int (*view_content)(void *ctx,
					       const void *data, size_t len,
					       size_t asn1hdrlen),
			   void *ctx)
212 213 214 215 216 217 218 219 220
{
	struct pkcs7_message *pkcs7;
	int ret;

	pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
	if (IS_ERR(pkcs7))
		return PTR_ERR(pkcs7);

	/* The data should be detached - so we need to supply it. */
221
	if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
222 223 224 225 226
		pr_err("PKCS#7 signature with non-detached data\n");
		ret = -EBADMSG;
		goto error;
	}

227
	ret = pkcs7_verify(pkcs7, usage);
228 229 230
	if (ret < 0)
		goto error;

231 232 233 234 235 236 237 238 239
	if (!trusted_keys) {
		trusted_keys = builtin_trusted_keys;
	} else if (trusted_keys == (void *)1UL) {
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
		trusted_keys = secondary_trusted_keys;
#else
		trusted_keys = builtin_trusted_keys;
#endif
	}
240 241 242 243
	ret = pkcs7_validate_trust(pkcs7, trusted_keys);
	if (ret < 0) {
		if (ret == -ENOKEY)
			pr_err("PKCS#7 signature not signed with a trusted key\n");
244 245 246 247 248 249 250 251 252 253 254 255 256 257
		goto error;
	}

	if (view_content) {
		size_t asn1hdrlen;

		ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
		if (ret < 0) {
			if (ret == -ENODATA)
				pr_devel("PKCS#7 message does not contain data\n");
			goto error;
		}

		ret = view_content(ctx, data, len, asn1hdrlen);
258 259 260 261 262 263 264
	}

error:
	pkcs7_free_message(pkcs7);
	pr_devel("<==%s() = %d\n", __func__, ret);
	return ret;
}
265
EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
266 267

#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */