]> git.karo-electronics.de Git - linux-beck.git/blob - crypto/asymmetric_keys/x509_public_key.c
Merge remote-tracking branch 'integrity/next-with-keys' into keys-next
[linux-beck.git] / crypto / asymmetric_keys / x509_public_key.c
1 /* Instantiate a public key crypto key from an X.509 Certificate
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 #define pr_fmt(fmt) "X.509: "fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/mpi.h>
18 #include <linux/asn1_decoder.h>
19 #include <keys/asymmetric-subtype.h>
20 #include <keys/asymmetric-parser.h>
21 #include <keys/system_keyring.h>
22 #include <crypto/hash.h>
23 #include "asymmetric_keys.h"
24 #include "public_key.h"
25 #include "x509_parser.h"
26
27 static bool use_builtin_keys;
28 static char *ca_keyid;
29
30 #ifndef MODULE
31 static int __init ca_keys_setup(char *str)
32 {
33         if (!str)               /* default system keyring */
34                 return 1;
35
36         if (strncmp(str, "id:", 3) == 0)
37                 ca_keyid = str; /* owner key 'id:xxxxxx' */
38         else if (strcmp(str, "builtin") == 0)
39                 use_builtin_keys = true;
40
41         return 1;
42 }
43 __setup("ca_keys=", ca_keys_setup);
44 #endif
45
46 /*
47  * Find a key in the given keyring by issuer and authority.
48  */
49 static struct key *x509_request_asymmetric_key(struct key *keyring,
50                                                const char *signer,
51                                                size_t signer_len,
52                                                const char *authority,
53                                                size_t auth_len)
54 {
55         key_ref_t key;
56         char *id;
57
58         /* Construct an identifier. */
59         id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL);
60         if (!id)
61                 return ERR_PTR(-ENOMEM);
62
63         memcpy(id, signer, signer_len);
64         id[signer_len + 0] = ':';
65         id[signer_len + 1] = ' ';
66         memcpy(id + signer_len + 2, authority, auth_len);
67         id[signer_len + 2 + auth_len] = 0;
68
69         pr_debug("Look up: \"%s\"\n", id);
70
71         key = keyring_search(make_key_ref(keyring, 1),
72                              &key_type_asymmetric, id);
73         if (IS_ERR(key))
74                 pr_debug("Request for module key '%s' err %ld\n",
75                          id, PTR_ERR(key));
76         kfree(id);
77
78         if (IS_ERR(key)) {
79                 switch (PTR_ERR(key)) {
80                         /* Hide some search errors */
81                 case -EACCES:
82                 case -ENOTDIR:
83                 case -EAGAIN:
84                         return ERR_PTR(-ENOKEY);
85                 default:
86                         return ERR_CAST(key);
87                 }
88         }
89
90         pr_devel("<==%s() = 0 [%x]\n", __func__,
91                  key_serial(key_ref_to_ptr(key)));
92         return key_ref_to_ptr(key);
93 }
94
95 /*
96  * Set up the signature parameters in an X.509 certificate.  This involves
97  * digesting the signed data and extracting the signature.
98  */
99 int x509_get_sig_params(struct x509_certificate *cert)
100 {
101         struct crypto_shash *tfm;
102         struct shash_desc *desc;
103         size_t digest_size, desc_size;
104         void *digest;
105         int ret;
106
107         pr_devel("==>%s()\n", __func__);
108
109         if (cert->sig.rsa.s)
110                 return 0;
111
112         cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
113         if (!cert->sig.rsa.s)
114                 return -ENOMEM;
115         cert->sig.nr_mpi = 1;
116
117         /* Allocate the hashing algorithm we're going to need and find out how
118          * big the hash operational data will be.
119          */
120         tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
121         if (IS_ERR(tfm))
122                 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
123
124         desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
125         digest_size = crypto_shash_digestsize(tfm);
126
127         /* We allocate the hash operational data storage on the end of the
128          * digest storage space.
129          */
130         ret = -ENOMEM;
131         digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
132         if (!digest)
133                 goto error;
134
135         cert->sig.digest = digest;
136         cert->sig.digest_size = digest_size;
137
138         desc = digest + digest_size;
139         desc->tfm = tfm;
140         desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
141
142         ret = crypto_shash_init(desc);
143         if (ret < 0)
144                 goto error;
145         might_sleep();
146         ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
147 error:
148         crypto_free_shash(tfm);
149         pr_devel("<==%s() = %d\n", __func__, ret);
150         return ret;
151 }
152 EXPORT_SYMBOL_GPL(x509_get_sig_params);
153
154 /*
155  * Check the signature on a certificate using the provided public key
156  */
157 int x509_check_signature(const struct public_key *pub,
158                          struct x509_certificate *cert)
159 {
160         int ret;
161
162         pr_devel("==>%s()\n", __func__);
163
164         ret = x509_get_sig_params(cert);
165         if (ret < 0)
166                 return ret;
167
168         ret = public_key_verify_signature(pub, &cert->sig);
169         pr_debug("Cert Verification: %d\n", ret);
170         return ret;
171 }
172 EXPORT_SYMBOL_GPL(x509_check_signature);
173
174 /*
175  * Check the new certificate against the ones in the trust keyring.  If one of
176  * those is the signing key and validates the new certificate, then mark the
177  * new certificate as being trusted.
178  *
179  * Return 0 if the new certificate was successfully validated, 1 if we couldn't
180  * find a matching parent certificate in the trusted list and an error if there
181  * is a matching certificate but the signature check fails.
182  */
183 static int x509_validate_trust(struct x509_certificate *cert,
184                                struct key *trust_keyring)
185 {
186         struct key *key;
187         int ret = 1;
188
189         if (!trust_keyring)
190                 return -EOPNOTSUPP;
191
192         if (ca_keyid && !asymmetric_keyid_match(cert->authority, ca_keyid))
193                 return -EPERM;
194
195         key = x509_request_asymmetric_key(trust_keyring,
196                                           cert->issuer, strlen(cert->issuer),
197                                           cert->authority,
198                                           strlen(cert->authority));
199         if (!IS_ERR(key))  {
200                 if (!use_builtin_keys
201                     || test_bit(KEY_FLAG_BUILTIN, &key->flags))
202                         ret = x509_check_signature(key->payload.data, cert);
203                 key_put(key);
204         }
205         return ret;
206 }
207
208 /*
209  * Attempt to parse a data blob for a key as an X509 certificate.
210  */
211 static int x509_key_preparse(struct key_preparsed_payload *prep)
212 {
213         struct x509_certificate *cert;
214         size_t srlen, sulen;
215         char *desc = NULL;
216         int ret;
217
218         cert = x509_cert_parse(prep->data, prep->datalen);
219         if (IS_ERR(cert))
220                 return PTR_ERR(cert);
221
222         pr_devel("Cert Issuer: %s\n", cert->issuer);
223         pr_devel("Cert Subject: %s\n", cert->subject);
224
225         if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
226             cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
227             cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
228             !pkey_algo[cert->pub->pkey_algo] ||
229             !pkey_algo[cert->sig.pkey_algo] ||
230             !hash_algo_name[cert->sig.pkey_hash_algo]) {
231                 ret = -ENOPKG;
232                 goto error_free_cert;
233         }
234
235         pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
236         pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
237                  cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
238                  cert->valid_from.tm_mday, cert->valid_from.tm_hour,
239                  cert->valid_from.tm_min,  cert->valid_from.tm_sec);
240         pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
241                  cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
242                  cert->valid_to.tm_mday, cert->valid_to.tm_hour,
243                  cert->valid_to.tm_min,  cert->valid_to.tm_sec);
244         pr_devel("Cert Signature: %s + %s\n",
245                  pkey_algo_name[cert->sig.pkey_algo],
246                  hash_algo_name[cert->sig.pkey_hash_algo]);
247
248         if (!cert->fingerprint) {
249                 pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
250                         cert->subject);
251                 ret = -EKEYREJECTED;
252                 goto error_free_cert;
253         }
254
255         cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
256         cert->pub->id_type = PKEY_ID_X509;
257
258         /* Check the signature on the key if it appears to be self-signed */
259         if (!cert->authority ||
260             strcmp(cert->fingerprint, cert->authority) == 0) {
261                 ret = x509_check_signature(cert->pub, cert); /* self-signed */
262                 if (ret < 0)
263                         goto error_free_cert;
264         } else if (!prep->trusted) {
265                 ret = x509_validate_trust(cert, get_system_trusted_keyring());
266                 if (!ret)
267                         prep->trusted = 1;
268         }
269
270         /* Propose a description */
271         sulen = strlen(cert->subject);
272         srlen = strlen(cert->fingerprint);
273         ret = -ENOMEM;
274         desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
275         if (!desc)
276                 goto error_free_cert;
277         memcpy(desc, cert->subject, sulen);
278         desc[sulen] = ':';
279         desc[sulen + 1] = ' ';
280         memcpy(desc + sulen + 2, cert->fingerprint, srlen);
281         desc[sulen + 2 + srlen] = 0;
282
283         /* We're pinning the module by being linked against it */
284         __module_get(public_key_subtype.owner);
285         prep->type_data[0] = &public_key_subtype;
286         prep->type_data[1] = cert->fingerprint;
287         prep->payload[0] = cert->pub;
288         prep->description = desc;
289         prep->quotalen = 100;
290
291         /* We've finished with the certificate */
292         cert->pub = NULL;
293         cert->fingerprint = NULL;
294         desc = NULL;
295         ret = 0;
296
297 error_free_cert:
298         x509_free_certificate(cert);
299         return ret;
300 }
301
302 static struct asymmetric_key_parser x509_key_parser = {
303         .owner  = THIS_MODULE,
304         .name   = "x509",
305         .parse  = x509_key_preparse,
306 };
307
308 /*
309  * Module stuff
310  */
311 static int __init x509_key_init(void)
312 {
313         return register_asymmetric_key_parser(&x509_key_parser);
314 }
315
316 static void __exit x509_key_exit(void)
317 {
318         unregister_asymmetric_key_parser(&x509_key_parser);
319 }
320
321 module_init(x509_key_init);
322 module_exit(x509_key_exit);
323
324 MODULE_DESCRIPTION("X.509 certificate parser");
325 MODULE_LICENSE("GPL");