Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[cascardo/linux.git] / crypto / asymmetric_keys / x509_public_key.c
index 733c046..fb73229 100644 (file)
 #include "asymmetric_keys.h"
 #include "x509_parser.h"
 
-static bool use_builtin_keys;
-static struct asymmetric_key_id *ca_keyid;
-
-#ifndef MODULE
-static struct {
-       struct asymmetric_key_id id;
-       unsigned char data[10];
-} cakey;
-
-static int __init ca_keys_setup(char *str)
-{
-       if (!str)               /* default system keyring */
-               return 1;
-
-       if (strncmp(str, "id:", 3) == 0) {
-               struct asymmetric_key_id *p = &cakey.id;
-               size_t hexlen = (strlen(str) - 3) / 2;
-               int ret;
-
-               if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
-                       pr_err("Missing or invalid ca_keys id\n");
-                       return 1;
-               }
-
-               ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
-               if (ret < 0)
-                       pr_err("Unparsable ca_keys id hex string\n");
-               else
-                       ca_keyid = p;   /* owner key 'id:xxxxxx' */
-       } else if (strcmp(str, "builtin") == 0) {
-               use_builtin_keys = true;
-       }
-
-       return 1;
-}
-__setup("ca_keys=", ca_keys_setup);
-#endif
-
-/**
- * x509_request_asymmetric_key - Request a key by X.509 certificate params.
- * @keyring: The keys to search.
- * @id: The issuer & serialNumber to look for or NULL.
- * @skid: The subjectKeyIdentifier to look for or NULL.
- * @partial: Use partial match if true, exact if false.
- *
- * Find a key in the given keyring by identifier.  The preferred identifier is
- * the issuer + serialNumber and the fallback identifier is the
- * subjectKeyIdentifier.  If both are given, the lookup is by the former, but
- * the latter must also match.
- */
-struct key *x509_request_asymmetric_key(struct key *keyring,
-                                       const struct asymmetric_key_id *id,
-                                       const struct asymmetric_key_id *skid,
-                                       bool partial)
-{
-       struct key *key;
-       key_ref_t ref;
-       const char *lookup;
-       char *req, *p;
-       int len;
-
-       if (id) {
-               lookup = id->data;
-               len = id->len;
-       } else {
-               lookup = skid->data;
-               len = skid->len;
-       }
-       
-       /* Construct an identifier "id:<keyid>". */
-       p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
-       if (!req)
-               return ERR_PTR(-ENOMEM);
-
-       if (partial) {
-               *p++ = 'i';
-               *p++ = 'd';
-       } else {
-               *p++ = 'e';
-               *p++ = 'x';
-       }
-       *p++ = ':';
-       p = bin2hex(p, lookup, len);
-       *p = 0;
-
-       pr_debug("Look up: \"%s\"\n", req);
-
-       ref = keyring_search(make_key_ref(keyring, 1),
-                            &key_type_asymmetric, req);
-       if (IS_ERR(ref))
-               pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
-       kfree(req);
-
-       if (IS_ERR(ref)) {
-               switch (PTR_ERR(ref)) {
-                       /* Hide some search errors */
-               case -EACCES:
-               case -ENOTDIR:
-               case -EAGAIN:
-                       return ERR_PTR(-ENOKEY);
-               default:
-                       return ERR_CAST(ref);
-               }
-       }
-
-       key = key_ref_to_ptr(ref);
-       if (id && skid) {
-               const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
-               if (!kids->id[1]) {
-                       pr_debug("issuer+serial match, but expected SKID missing\n");
-                       goto reject;
-               }
-               if (!asymmetric_key_id_same(skid, kids->id[1])) {
-                       pr_debug("issuer+serial match, but SKID does not\n");
-                       goto reject;
-               }
-       }
-       
-       pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
-       return key;
-
-reject:
-       key_put(key);
-       return ERR_PTR(-EKEYREJECTED);
-}
-EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
-
 /*
  * Set up the signature parameters in an X.509 certificate.  This involves
  * digesting the signed data and extracting the signature.
  */
 int x509_get_sig_params(struct x509_certificate *cert)
 {
+       struct public_key_signature *sig = cert->sig;
        struct crypto_shash *tfm;
        struct shash_desc *desc;
-       size_t digest_size, desc_size;
-       void *digest;
+       size_t desc_size;
        int ret;
 
        pr_devel("==>%s()\n", __func__);
 
-       if (cert->unsupported_crypto)
-               return -ENOPKG;
-       if (cert->sig.s)
+       if (!cert->pub->pkey_algo)
+               cert->unsupported_key = true;
+
+       if (!sig->pkey_algo)
+               cert->unsupported_sig = true;
+
+       /* We check the hash if we can - even if we can't then verify it */
+       if (!sig->hash_algo) {
+               cert->unsupported_sig = true;
                return 0;
+       }
 
-       cert->sig.s = kmemdup(cert->raw_sig, cert->raw_sig_size,
-                             GFP_KERNEL);
-       if (!cert->sig.s)
+       sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
+       if (!sig->s)
                return -ENOMEM;
 
-       cert->sig.s_size = cert->raw_sig_size;
+       sig->s_size = cert->raw_sig_size;
 
        /* Allocate the hashing algorithm we're going to need and find out how
         * big the hash operational data will be.
         */
-       tfm = crypto_alloc_shash(cert->sig.hash_algo, 0, 0);
+       tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
        if (IS_ERR(tfm)) {
                if (PTR_ERR(tfm) == -ENOENT) {
-                       cert->unsupported_crypto = true;
-                       return -ENOPKG;
+                       cert->unsupported_sig = true;
+                       return 0;
                }
                return PTR_ERR(tfm);
        }
 
        desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
-       digest_size = crypto_shash_digestsize(tfm);
+       sig->digest_size = crypto_shash_digestsize(tfm);
 
-       /* We allocate the hash operational data storage on the end of the
-        * digest storage space.
-        */
        ret = -ENOMEM;
-       digest = kzalloc(ALIGN(digest_size, __alignof__(*desc)) + desc_size,
-                        GFP_KERNEL);
-       if (!digest)
+       sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
+       if (!sig->digest)
                goto error;
 
-       cert->sig.digest = digest;
-       cert->sig.digest_size = digest_size;
+       desc = kzalloc(desc_size, GFP_KERNEL);
+       if (!desc)
+               goto error;
 
-       desc = PTR_ALIGN(digest + digest_size, __alignof__(*desc));
        desc->tfm = tfm;
        desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
 
        ret = crypto_shash_init(desc);
        if (ret < 0)
-               goto error;
+               goto error_2;
        might_sleep();
-       ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
+       ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest);
+
+error_2:
+       kfree(desc);
 error:
        crypto_free_shash(tfm);
        pr_devel("<==%s() = %d\n", __func__, ret);
        return ret;
 }
-EXPORT_SYMBOL_GPL(x509_get_sig_params);
 
 /*
- * Check the signature on a certificate using the provided public key
+ * Check for self-signedness in an X.509 cert and if found, check the signature
+ * immediately if we can.
  */
-int x509_check_signature(const struct public_key *pub,
-                        struct x509_certificate *cert)
+int x509_check_for_self_signed(struct x509_certificate *cert)
 {
-       int ret;
+       int ret = 0;
 
        pr_devel("==>%s()\n", __func__);
 
-       ret = x509_get_sig_params(cert);
-       if (ret < 0)
-               return ret;
+       if (cert->raw_subject_size != cert->raw_issuer_size ||
+           memcmp(cert->raw_subject, cert->raw_issuer,
+                  cert->raw_issuer_size) != 0)
+               goto not_self_signed;
+
+       if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
+               /* If the AKID is present it may have one or two parts.  If
+                * both are supplied, both must match.
+                */
+               bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
+               bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
+
+               if (!a && !b)
+                       goto not_self_signed;
+
+               ret = -EKEYREJECTED;
+               if (((a && !b) || (b && !a)) &&
+                   cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
+                       goto out;
+       }
 
-       ret = public_key_verify_signature(pub, &cert->sig);
-       if (ret == -ENOPKG)
-               cert->unsupported_crypto = true;
-       pr_debug("Cert Verification: %d\n", ret);
-       return ret;
-}
-EXPORT_SYMBOL_GPL(x509_check_signature);
+       ret = -EKEYREJECTED;
+       if (cert->pub->pkey_algo != cert->sig->pkey_algo)
+               goto out;
 
-/*
- * Check the new certificate against the ones in the trust keyring.  If one of
- * those is the signing key and validates the new certificate, then mark the
- * new certificate as being trusted.
- *
- * Return 0 if the new certificate was successfully validated, 1 if we couldn't
- * find a matching parent certificate in the trusted list and an error if there
- * is a matching certificate but the signature check fails.
- */
-static int x509_validate_trust(struct x509_certificate *cert,
-                              struct key *trust_keyring)
-{
-       struct key *key;
-       int ret = 1;
-
-       if (!trust_keyring)
-               return -EOPNOTSUPP;
-
-       if (ca_keyid && !asymmetric_key_id_partial(cert->akid_skid, ca_keyid))
-               return -EPERM;
-
-       key = x509_request_asymmetric_key(trust_keyring,
-                                         cert->akid_id, cert->akid_skid,
-                                         false);
-       if (!IS_ERR(key))  {
-               if (!use_builtin_keys
-                   || test_bit(KEY_FLAG_BUILTIN, &key->flags))
-                       ret = x509_check_signature(key->payload.data[asym_crypto],
-                                                  cert);
-               key_put(key);
+       ret = public_key_verify_signature(cert->pub, cert->sig);
+       if (ret < 0) {
+               if (ret == -ENOPKG) {
+                       cert->unsupported_sig = true;
+                       ret = 0;
+               }
+               goto out;
        }
+
+       pr_devel("Cert Self-signature verified");
+       cert->self_signed = true;
+
+out:
+       pr_devel("<==%s() = %d\n", __func__, ret);
        return ret;
+
+not_self_signed:
+       pr_devel("<==%s() = 0 [not]\n", __func__);
+       return 0;
 }
 
 /*
@@ -291,34 +168,22 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
        pr_devel("Cert Issuer: %s\n", cert->issuer);
        pr_devel("Cert Subject: %s\n", cert->subject);
 
-       if (!cert->pub->pkey_algo ||
-           !cert->sig.pkey_algo ||
-           !cert->sig.hash_algo) {
+       if (cert->unsupported_key) {
                ret = -ENOPKG;
                goto error_free_cert;
        }
 
        pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
        pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
-       pr_devel("Cert Signature: %s + %s\n",
-                cert->sig.pkey_algo,
-                cert->sig.hash_algo);
 
        cert->pub->id_type = "X509";
 
-       /* Check the signature on the key if it appears to be self-signed */
-       if ((!cert->akid_skid && !cert->akid_id) ||
-           asymmetric_key_id_same(cert->skid, cert->akid_skid) ||
-           asymmetric_key_id_same(cert->id, cert->akid_id)) {
-               ret = x509_check_signature(cert->pub, cert); /* self-signed */
-               if (ret < 0)
-                       goto error_free_cert;
-       } else if (!prep->trusted) {
-               ret = x509_validate_trust(cert, get_system_trusted_keyring());
-               if (ret)
-                       ret = x509_validate_trust(cert, get_ima_mok_keyring());
-               if (!ret)
-                       prep->trusted = 1;
+       if (cert->unsupported_sig) {
+               public_key_signature_free(cert->sig);
+               cert->sig = NULL;
+       } else {
+               pr_devel("Cert Signature: %s + %s\n",
+                        cert->sig->pkey_algo, cert->sig->hash_algo);
        }
 
        /* Propose a description */
@@ -353,6 +218,7 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
        prep->payload.data[asym_subtype] = &public_key_subtype;
        prep->payload.data[asym_key_ids] = kids;
        prep->payload.data[asym_crypto] = cert->pub;
+       prep->payload.data[asym_auth] = cert->sig;
        prep->description = desc;
        prep->quotalen = 100;
 
@@ -360,6 +226,7 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
        cert->pub = NULL;
        cert->id = NULL;
        cert->skid = NULL;
+       cert->sig = NULL;
        desc = NULL;
        ret = 0;