]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - certs/system_keyring.c
KEYS: Generalise system_verify_data() to provide access to internal content
[mirror_ubuntu-artful-kernel.git] / certs / system_keyring.c
CommitLineData
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
21struct key *system_trusted_keyring;
22EXPORT_SYMBOL_GPL(system_trusted_keyring);
23
24extern __initconst const u8 system_certificate_list[];
62226983 25extern __initconst const unsigned long system_certificate_list_size;
b56e5a17
DH
26
27/*
28 * Load the compiled-in keys
29 */
30static __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),
b56e5a17
DH
39 KEY_ALLOC_NOT_IN_QUOTA, NULL);
40 if (IS_ERR(system_trusted_keyring))
41 panic("Can't allocate system trusted keyring\n");
42
008643b8 43 set_bit(KEY_FLAG_TRUSTED_ONLY, &system_trusted_keyring->flags);
b56e5a17
DH
44 return 0;
45}
46
47/*
48 * Must be initialised before we try and load the keys into the keyring.
49 */
50device_initcall(system_trusted_keyring_init);
51
52/*
53 * Load the compiled-in list of X.509 certificates.
54 */
55static __init int load_system_certificate_list(void)
56{
57 key_ref_t key;
58 const u8 *p, *end;
59 size_t plen;
60
61 pr_notice("Loading compiled-in X.509 certificates\n");
62
b56e5a17 63 p = system_certificate_list;
62226983 64 end = p + system_certificate_list_size;
b56e5a17
DH
65 while (p < end) {
66 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
67 * than 256 bytes in size.
68 */
69 if (end - p < 4)
70 goto dodgy_cert;
71 if (p[0] != 0x30 &&
72 p[1] != 0x82)
73 goto dodgy_cert;
74 plen = (p[2] << 8) | p[3];
75 plen += 4;
76 if (plen > end - p)
77 goto dodgy_cert;
78
79 key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
80 "asymmetric",
81 NULL,
82 p,
83 plen,
af34cb0c
MZ
84 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
85 KEY_USR_VIEW | KEY_USR_READ),
008643b8 86 KEY_ALLOC_NOT_IN_QUOTA |
5d2787cf
DH
87 KEY_ALLOC_TRUSTED |
88 KEY_ALLOC_BUILT_IN);
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
102dodgy_cert:
103 pr_err("Problem parsing in-kernel X.509 certificate list\n");
104 return 0;
105}
106late_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
121int verify_pkcs7_signature(const void *data, size_t len,
122 const void *raw_pkcs7, size_t pkcs7_len,
123 struct key *trusted_keys,
124 int untrusted_error,
125 enum key_being_used_for usage,
126 int (*view_content)(void *ctx,
127 const void *data, size_t len,
128 size_t asn1hdrlen),
129 void *ctx)
091f6e26
DH
130{
131 struct pkcs7_message *pkcs7;
132 bool trusted;
133 int ret;
134
135 pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
136 if (IS_ERR(pkcs7))
137 return PTR_ERR(pkcs7);
138
139 /* The data should be detached - so we need to supply it. */
e68503bd 140 if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
091f6e26
DH
141 pr_err("PKCS#7 signature with non-detached data\n");
142 ret = -EBADMSG;
143 goto error;
144 }
145
99db4435 146 ret = pkcs7_verify(pkcs7, usage);
091f6e26
DH
147 if (ret < 0)
148 goto error;
149
e68503bd
DH
150 if (!trusted_keys)
151 trusted_keys = system_trusted_keyring;
152 ret = pkcs7_validate_trust(pkcs7, trusted_keys, &trusted);
091f6e26
DH
153 if (ret < 0)
154 goto error;
155
e68503bd 156 if (!trusted && untrusted_error) {
091f6e26 157 pr_err("PKCS#7 signature not signed with a trusted key\n");
e68503bd
DH
158 ret = untrusted_error;
159 goto error;
160 }
161
162 if (view_content) {
163 size_t asn1hdrlen;
164
165 ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
166 if (ret < 0) {
167 if (ret == -ENODATA)
168 pr_devel("PKCS#7 message does not contain data\n");
169 goto error;
170 }
171
172 ret = view_content(ctx, data, len, asn1hdrlen);
091f6e26
DH
173 }
174
175error:
176 pkcs7_free_message(pkcs7);
177 pr_devel("<==%s() = %d\n", __func__, ret);
178 return ret;
179}
e68503bd 180EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
091f6e26
DH
181
182#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */