Back to home page

LXR

 
 

    


0001 /* System trusted keyring for trusted public keys
0002  *
0003  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
0004  * Written by David Howells (dhowells@redhat.com)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public Licence
0008  * as published by the Free Software Foundation; either version
0009  * 2 of the Licence, or (at your option) any later version.
0010  */
0011 
0012 #include <linux/export.h>
0013 #include <linux/kernel.h>
0014 #include <linux/sched.h>
0015 #include <linux/cred.h>
0016 #include <linux/err.h>
0017 #include <keys/asymmetric-type.h>
0018 #include <keys/system_keyring.h>
0019 #include <crypto/pkcs7.h>
0020 
0021 static struct key *builtin_trusted_keys;
0022 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
0023 static struct key *secondary_trusted_keys;
0024 #endif
0025 
0026 extern __initconst const u8 system_certificate_list[];
0027 extern __initconst const unsigned long system_certificate_list_size;
0028 
0029 /**
0030  * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
0031  *
0032  * Restrict the addition of keys into a keyring based on the key-to-be-added
0033  * being vouched for by a key in the built in system keyring.
0034  */
0035 int restrict_link_by_builtin_trusted(struct key *keyring,
0036                      const struct key_type *type,
0037                      const union key_payload *payload)
0038 {
0039     return restrict_link_by_signature(builtin_trusted_keys, type, payload);
0040 }
0041 
0042 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
0043 /**
0044  * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
0045  *   addition by both builtin and secondary keyrings
0046  *
0047  * Restrict the addition of keys into a keyring based on the key-to-be-added
0048  * being vouched for by a key in either the built-in or the secondary system
0049  * keyrings.
0050  */
0051 int restrict_link_by_builtin_and_secondary_trusted(
0052     struct key *keyring,
0053     const struct key_type *type,
0054     const union key_payload *payload)
0055 {
0056     /* If we have a secondary trusted keyring, then that contains a link
0057      * through to the builtin keyring and the search will follow that link.
0058      */
0059     if (type == &key_type_keyring &&
0060         keyring == secondary_trusted_keys &&
0061         payload == &builtin_trusted_keys->payload)
0062         /* Allow the builtin keyring to be added to the secondary */
0063         return 0;
0064 
0065     return restrict_link_by_signature(secondary_trusted_keys, type, payload);
0066 }
0067 #endif
0068 
0069 /*
0070  * Create the trusted keyrings
0071  */
0072 static __init int system_trusted_keyring_init(void)
0073 {
0074     pr_notice("Initialise system trusted keyrings\n");
0075 
0076     builtin_trusted_keys =
0077         keyring_alloc(".builtin_trusted_keys",
0078                   KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
0079                   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
0080                   KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
0081                   KEY_ALLOC_NOT_IN_QUOTA,
0082                   NULL, NULL);
0083     if (IS_ERR(builtin_trusted_keys))
0084         panic("Can't allocate builtin trusted keyring\n");
0085 
0086 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
0087     secondary_trusted_keys =
0088         keyring_alloc(".secondary_trusted_keys",
0089                   KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
0090                   ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
0091                    KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
0092                    KEY_USR_WRITE),
0093                   KEY_ALLOC_NOT_IN_QUOTA,
0094                   restrict_link_by_builtin_and_secondary_trusted,
0095                   NULL);
0096     if (IS_ERR(secondary_trusted_keys))
0097         panic("Can't allocate secondary trusted keyring\n");
0098 
0099     if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
0100         panic("Can't link trusted keyrings\n");
0101 #endif
0102 
0103     return 0;
0104 }
0105 
0106 /*
0107  * Must be initialised before we try and load the keys into the keyring.
0108  */
0109 device_initcall(system_trusted_keyring_init);
0110 
0111 /*
0112  * Load the compiled-in list of X.509 certificates.
0113  */
0114 static __init int load_system_certificate_list(void)
0115 {
0116     key_ref_t key;
0117     const u8 *p, *end;
0118     size_t plen;
0119 
0120     pr_notice("Loading compiled-in X.509 certificates\n");
0121 
0122     p = system_certificate_list;
0123     end = p + system_certificate_list_size;
0124     while (p < end) {
0125         /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
0126          * than 256 bytes in size.
0127          */
0128         if (end - p < 4)
0129             goto dodgy_cert;
0130         if (p[0] != 0x30 &&
0131             p[1] != 0x82)
0132             goto dodgy_cert;
0133         plen = (p[2] << 8) | p[3];
0134         plen += 4;
0135         if (plen > end - p)
0136             goto dodgy_cert;
0137 
0138         key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
0139                        "asymmetric",
0140                        NULL,
0141                        p,
0142                        plen,
0143                        ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
0144                        KEY_USR_VIEW | KEY_USR_READ),
0145                        KEY_ALLOC_NOT_IN_QUOTA |
0146                        KEY_ALLOC_BUILT_IN |
0147                        KEY_ALLOC_BYPASS_RESTRICTION);
0148         if (IS_ERR(key)) {
0149             pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
0150                    PTR_ERR(key));
0151         } else {
0152             pr_notice("Loaded X.509 cert '%s'\n",
0153                   key_ref_to_ptr(key)->description);
0154             key_ref_put(key);
0155         }
0156         p += plen;
0157     }
0158 
0159     return 0;
0160 
0161 dodgy_cert:
0162     pr_err("Problem parsing in-kernel X.509 certificate list\n");
0163     return 0;
0164 }
0165 late_initcall(load_system_certificate_list);
0166 
0167 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
0168 
0169 /**
0170  * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
0171  * @data: The data to be verified (NULL if expecting internal data).
0172  * @len: Size of @data.
0173  * @raw_pkcs7: The PKCS#7 message that is the signature.
0174  * @pkcs7_len: The size of @raw_pkcs7.
0175  * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
0176  *                  (void *)1UL for all trusted keys).
0177  * @usage: The use to which the key is being put.
0178  * @view_content: Callback to gain access to content.
0179  * @ctx: Context for callback.
0180  */
0181 int verify_pkcs7_signature(const void *data, size_t len,
0182                const void *raw_pkcs7, size_t pkcs7_len,
0183                struct key *trusted_keys,
0184                enum key_being_used_for usage,
0185                int (*view_content)(void *ctx,
0186                            const void *data, size_t len,
0187                            size_t asn1hdrlen),
0188                void *ctx)
0189 {
0190     struct pkcs7_message *pkcs7;
0191     int ret;
0192 
0193     pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
0194     if (IS_ERR(pkcs7))
0195         return PTR_ERR(pkcs7);
0196 
0197     /* The data should be detached - so we need to supply it. */
0198     if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
0199         pr_err("PKCS#7 signature with non-detached data\n");
0200         ret = -EBADMSG;
0201         goto error;
0202     }
0203 
0204     ret = pkcs7_verify(pkcs7, usage);
0205     if (ret < 0)
0206         goto error;
0207 
0208     if (!trusted_keys) {
0209         trusted_keys = builtin_trusted_keys;
0210     } else if (trusted_keys == (void *)1UL) {
0211 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
0212         trusted_keys = secondary_trusted_keys;
0213 #else
0214         trusted_keys = builtin_trusted_keys;
0215 #endif
0216     }
0217     ret = pkcs7_validate_trust(pkcs7, trusted_keys);
0218     if (ret < 0) {
0219         if (ret == -ENOKEY)
0220             pr_err("PKCS#7 signature not signed with a trusted key\n");
0221         goto error;
0222     }
0223 
0224     if (view_content) {
0225         size_t asn1hdrlen;
0226 
0227         ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
0228         if (ret < 0) {
0229             if (ret == -ENODATA)
0230                 pr_devel("PKCS#7 message does not contain data\n");
0231             goto error;
0232         }
0233 
0234         ret = view_content(ctx, data, len, asn1hdrlen);
0235     }
0236 
0237 error:
0238     pkcs7_free_message(pkcs7);
0239     pr_devel("<==%s() = %d\n", __func__, ret);
0240     return ret;
0241 }
0242 EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
0243 
0244 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */