Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Instantiate a public key crypto key from an X.509 Certificate
0003  *
0004  * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #define pr_fmt(fmt) "ASYM: "fmt
0009 #include <linux/module.h>
0010 #include <linux/kernel.h>
0011 #include <linux/err.h>
0012 #include <crypto/public_key.h>
0013 #include "asymmetric_keys.h"
0014 
0015 static bool use_builtin_keys;
0016 static struct asymmetric_key_id *ca_keyid;
0017 
0018 #ifndef MODULE
0019 static struct {
0020     struct asymmetric_key_id id;
0021     unsigned char data[10];
0022 } cakey;
0023 
0024 static int __init ca_keys_setup(char *str)
0025 {
0026     if (!str)       /* default system keyring */
0027         return 1;
0028 
0029     if (strncmp(str, "id:", 3) == 0) {
0030         struct asymmetric_key_id *p = &cakey.id;
0031         size_t hexlen = (strlen(str) - 3) / 2;
0032         int ret;
0033 
0034         if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
0035             pr_err("Missing or invalid ca_keys id\n");
0036             return 1;
0037         }
0038 
0039         ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
0040         if (ret < 0)
0041             pr_err("Unparsable ca_keys id hex string\n");
0042         else
0043             ca_keyid = p;   /* owner key 'id:xxxxxx' */
0044     } else if (strcmp(str, "builtin") == 0) {
0045         use_builtin_keys = true;
0046     }
0047 
0048     return 1;
0049 }
0050 __setup("ca_keys=", ca_keys_setup);
0051 #endif
0052 
0053 /**
0054  * restrict_link_by_signature - Restrict additions to a ring of public keys
0055  * @dest_keyring: Keyring being linked to.
0056  * @type: The type of key being added.
0057  * @payload: The payload of the new key.
0058  * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
0059  *
0060  * Check the new certificate against the ones in the trust keyring.  If one of
0061  * those is the signing key and validates the new certificate, then mark the
0062  * new certificate as being trusted.
0063  *
0064  * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
0065  * matching parent certificate in the trusted list, -EKEYREJECTED if the
0066  * signature check fails or the key is blacklisted, -ENOPKG if the signature
0067  * uses unsupported crypto, or some other error if there is a matching
0068  * certificate but the signature check cannot be performed.
0069  */
0070 int restrict_link_by_signature(struct key *dest_keyring,
0071                    const struct key_type *type,
0072                    const union key_payload *payload,
0073                    struct key *trust_keyring)
0074 {
0075     const struct public_key_signature *sig;
0076     struct key *key;
0077     int ret;
0078 
0079     pr_devel("==>%s()\n", __func__);
0080 
0081     if (!trust_keyring)
0082         return -ENOKEY;
0083 
0084     if (type != &key_type_asymmetric)
0085         return -EOPNOTSUPP;
0086 
0087     sig = payload->data[asym_auth];
0088     if (!sig)
0089         return -ENOPKG;
0090     if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
0091         return -ENOKEY;
0092 
0093     if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
0094         return -EPERM;
0095 
0096     /* See if we have a key that signed this one. */
0097     key = find_asymmetric_key(trust_keyring,
0098                   sig->auth_ids[0], sig->auth_ids[1],
0099                   sig->auth_ids[2], false);
0100     if (IS_ERR(key))
0101         return -ENOKEY;
0102 
0103     if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
0104         ret = -ENOKEY;
0105     else
0106         ret = verify_signature(key, sig);
0107     key_put(key);
0108     return ret;
0109 }
0110 
0111 static bool match_either_id(const struct asymmetric_key_id **pair,
0112                 const struct asymmetric_key_id *single)
0113 {
0114     return (asymmetric_key_id_same(pair[0], single) ||
0115         asymmetric_key_id_same(pair[1], single));
0116 }
0117 
0118 static int key_or_keyring_common(struct key *dest_keyring,
0119                  const struct key_type *type,
0120                  const union key_payload *payload,
0121                  struct key *trusted, bool check_dest)
0122 {
0123     const struct public_key_signature *sig;
0124     struct key *key = NULL;
0125     int ret;
0126 
0127     pr_devel("==>%s()\n", __func__);
0128 
0129     if (!dest_keyring)
0130         return -ENOKEY;
0131     else if (dest_keyring->type != &key_type_keyring)
0132         return -EOPNOTSUPP;
0133 
0134     if (!trusted && !check_dest)
0135         return -ENOKEY;
0136 
0137     if (type != &key_type_asymmetric)
0138         return -EOPNOTSUPP;
0139 
0140     sig = payload->data[asym_auth];
0141     if (!sig)
0142         return -ENOPKG;
0143     if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
0144         return -ENOKEY;
0145 
0146     if (trusted) {
0147         if (trusted->type == &key_type_keyring) {
0148             /* See if we have a key that signed this one. */
0149             key = find_asymmetric_key(trusted, sig->auth_ids[0],
0150                           sig->auth_ids[1],
0151                           sig->auth_ids[2], false);
0152             if (IS_ERR(key))
0153                 key = NULL;
0154         } else if (trusted->type == &key_type_asymmetric) {
0155             const struct asymmetric_key_id **signer_ids;
0156 
0157             signer_ids = (const struct asymmetric_key_id **)
0158                 asymmetric_key_ids(trusted)->id;
0159 
0160             /*
0161              * The auth_ids come from the candidate key (the
0162              * one that is being considered for addition to
0163              * dest_keyring) and identify the key that was
0164              * used to sign.
0165              *
0166              * The signer_ids are identifiers for the
0167              * signing key specified for dest_keyring.
0168              *
0169              * The first auth_id is the preferred id, 2nd and
0170              * 3rd are the fallbacks. If exactly one of
0171              * auth_ids[0] and auth_ids[1] is present, it may
0172              * match either signer_ids[0] or signed_ids[1].
0173              * If both are present the first one may match
0174              * either signed_id but the second one must match
0175              * the second signer_id. If neither of them is
0176              * available, auth_ids[2] is matched against
0177              * signer_ids[2] as a fallback.
0178              */
0179             if (!sig->auth_ids[0] && !sig->auth_ids[1]) {
0180                 if (asymmetric_key_id_same(signer_ids[2],
0181                                sig->auth_ids[2]))
0182                     key = __key_get(trusted);
0183 
0184             } else if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
0185                 const struct asymmetric_key_id *auth_id;
0186 
0187                 auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
0188                 if (match_either_id(signer_ids, auth_id))
0189                     key = __key_get(trusted);
0190 
0191             } else if (asymmetric_key_id_same(signer_ids[1],
0192                               sig->auth_ids[1]) &&
0193                    match_either_id(signer_ids,
0194                            sig->auth_ids[0])) {
0195                 key = __key_get(trusted);
0196             }
0197         } else {
0198             return -EOPNOTSUPP;
0199         }
0200     }
0201 
0202     if (check_dest && !key) {
0203         /* See if the destination has a key that signed this one. */
0204         key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
0205                       sig->auth_ids[1], sig->auth_ids[2],
0206                       false);
0207         if (IS_ERR(key))
0208             key = NULL;
0209     }
0210 
0211     if (!key)
0212         return -ENOKEY;
0213 
0214     ret = key_validate(key);
0215     if (ret == 0)
0216         ret = verify_signature(key, sig);
0217 
0218     key_put(key);
0219     return ret;
0220 }
0221 
0222 /**
0223  * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
0224  * keys using the restrict_key information stored in the ring.
0225  * @dest_keyring: Keyring being linked to.
0226  * @type: The type of key being added.
0227  * @payload: The payload of the new key.
0228  * @trusted: A key or ring of keys that can be used to vouch for the new cert.
0229  *
0230  * Check the new certificate only against the key or keys passed in the data
0231  * parameter. If one of those is the signing key and validates the new
0232  * certificate, then mark the new certificate as being ok to link.
0233  *
0234  * Returns 0 if the new certificate was accepted, -ENOKEY if we
0235  * couldn't find a matching parent certificate in the trusted list,
0236  * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
0237  * unsupported crypto, or some other error if there is a matching certificate
0238  * but the signature check cannot be performed.
0239  */
0240 int restrict_link_by_key_or_keyring(struct key *dest_keyring,
0241                     const struct key_type *type,
0242                     const union key_payload *payload,
0243                     struct key *trusted)
0244 {
0245     return key_or_keyring_common(dest_keyring, type, payload, trusted,
0246                      false);
0247 }
0248 
0249 /**
0250  * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
0251  * public keys using the restrict_key information stored in the ring.
0252  * @dest_keyring: Keyring being linked to.
0253  * @type: The type of key being added.
0254  * @payload: The payload of the new key.
0255  * @trusted: A key or ring of keys that can be used to vouch for the new cert.
0256  *
0257  * Check the new certificate against the key or keys passed in the data
0258  * parameter and against the keys already linked to the destination keyring. If
0259  * one of those is the signing key and validates the new certificate, then mark
0260  * the new certificate as being ok to link.
0261  *
0262  * Returns 0 if the new certificate was accepted, -ENOKEY if we
0263  * couldn't find a matching parent certificate in the trusted list,
0264  * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
0265  * unsupported crypto, or some other error if there is a matching certificate
0266  * but the signature check cannot be performed.
0267  */
0268 int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
0269                       const struct key_type *type,
0270                       const union key_payload *payload,
0271                       struct key *trusted)
0272 {
0273     return key_or_keyring_common(dest_keyring, type, payload, trusted,
0274                      true);
0275 }