162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* Instantiate a public key crypto key from an X.509 Certificate 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved. 562306a36Sopenharmony_ci * Written by David Howells (dhowells@redhat.com) 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#define pr_fmt(fmt) "ASYM: "fmt 962306a36Sopenharmony_ci#include <linux/module.h> 1062306a36Sopenharmony_ci#include <linux/kernel.h> 1162306a36Sopenharmony_ci#include <linux/err.h> 1262306a36Sopenharmony_ci#include <crypto/public_key.h> 1362306a36Sopenharmony_ci#include "asymmetric_keys.h" 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_cistatic bool use_builtin_keys; 1662306a36Sopenharmony_cistatic struct asymmetric_key_id *ca_keyid; 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#ifndef MODULE 1962306a36Sopenharmony_cistatic struct { 2062306a36Sopenharmony_ci struct asymmetric_key_id id; 2162306a36Sopenharmony_ci unsigned char data[10]; 2262306a36Sopenharmony_ci} cakey; 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cistatic int __init ca_keys_setup(char *str) 2562306a36Sopenharmony_ci{ 2662306a36Sopenharmony_ci if (!str) /* default system keyring */ 2762306a36Sopenharmony_ci return 1; 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci if (strncmp(str, "id:", 3) == 0) { 3062306a36Sopenharmony_ci struct asymmetric_key_id *p = &cakey.id; 3162306a36Sopenharmony_ci size_t hexlen = (strlen(str) - 3) / 2; 3262306a36Sopenharmony_ci int ret; 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci if (hexlen == 0 || hexlen > sizeof(cakey.data)) { 3562306a36Sopenharmony_ci pr_err("Missing or invalid ca_keys id\n"); 3662306a36Sopenharmony_ci return 1; 3762306a36Sopenharmony_ci } 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen); 4062306a36Sopenharmony_ci if (ret < 0) 4162306a36Sopenharmony_ci pr_err("Unparsable ca_keys id hex string\n"); 4262306a36Sopenharmony_ci else 4362306a36Sopenharmony_ci ca_keyid = p; /* owner key 'id:xxxxxx' */ 4462306a36Sopenharmony_ci } else if (strcmp(str, "builtin") == 0) { 4562306a36Sopenharmony_ci use_builtin_keys = true; 4662306a36Sopenharmony_ci } 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci return 1; 4962306a36Sopenharmony_ci} 5062306a36Sopenharmony_ci__setup("ca_keys=", ca_keys_setup); 5162306a36Sopenharmony_ci#endif 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci/** 5462306a36Sopenharmony_ci * restrict_link_by_signature - Restrict additions to a ring of public keys 5562306a36Sopenharmony_ci * @dest_keyring: Keyring being linked to. 5662306a36Sopenharmony_ci * @type: The type of key being added. 5762306a36Sopenharmony_ci * @payload: The payload of the new key. 5862306a36Sopenharmony_ci * @trust_keyring: A ring of keys that can be used to vouch for the new cert. 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * Check the new certificate against the ones in the trust keyring. If one of 6162306a36Sopenharmony_ci * those is the signing key and validates the new certificate, then mark the 6262306a36Sopenharmony_ci * new certificate as being trusted. 6362306a36Sopenharmony_ci * 6462306a36Sopenharmony_ci * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a 6562306a36Sopenharmony_ci * matching parent certificate in the trusted list, -EKEYREJECTED if the 6662306a36Sopenharmony_ci * signature check fails or the key is blacklisted, -ENOPKG if the signature 6762306a36Sopenharmony_ci * uses unsupported crypto, or some other error if there is a matching 6862306a36Sopenharmony_ci * certificate but the signature check cannot be performed. 6962306a36Sopenharmony_ci */ 7062306a36Sopenharmony_ciint restrict_link_by_signature(struct key *dest_keyring, 7162306a36Sopenharmony_ci const struct key_type *type, 7262306a36Sopenharmony_ci const union key_payload *payload, 7362306a36Sopenharmony_ci struct key *trust_keyring) 7462306a36Sopenharmony_ci{ 7562306a36Sopenharmony_ci const struct public_key_signature *sig; 7662306a36Sopenharmony_ci struct key *key; 7762306a36Sopenharmony_ci int ret; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci pr_devel("==>%s()\n", __func__); 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci if (!trust_keyring) 8262306a36Sopenharmony_ci return -ENOKEY; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci if (type != &key_type_asymmetric) 8562306a36Sopenharmony_ci return -EOPNOTSUPP; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci sig = payload->data[asym_auth]; 8862306a36Sopenharmony_ci if (!sig) 8962306a36Sopenharmony_ci return -ENOPKG; 9062306a36Sopenharmony_ci if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2]) 9162306a36Sopenharmony_ci return -ENOKEY; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid)) 9462306a36Sopenharmony_ci return -EPERM; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci /* See if we have a key that signed this one. */ 9762306a36Sopenharmony_ci key = find_asymmetric_key(trust_keyring, 9862306a36Sopenharmony_ci sig->auth_ids[0], sig->auth_ids[1], 9962306a36Sopenharmony_ci sig->auth_ids[2], false); 10062306a36Sopenharmony_ci if (IS_ERR(key)) 10162306a36Sopenharmony_ci return -ENOKEY; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags)) 10462306a36Sopenharmony_ci ret = -ENOKEY; 10562306a36Sopenharmony_ci else 10662306a36Sopenharmony_ci ret = verify_signature(key, sig); 10762306a36Sopenharmony_ci key_put(key); 10862306a36Sopenharmony_ci return ret; 10962306a36Sopenharmony_ci} 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci/** 11262306a36Sopenharmony_ci * restrict_link_by_ca - Restrict additions to a ring of CA keys 11362306a36Sopenharmony_ci * @dest_keyring: Keyring being linked to. 11462306a36Sopenharmony_ci * @type: The type of key being added. 11562306a36Sopenharmony_ci * @payload: The payload of the new key. 11662306a36Sopenharmony_ci * @trust_keyring: Unused. 11762306a36Sopenharmony_ci * 11862306a36Sopenharmony_ci * Check if the new certificate is a CA. If it is a CA, then mark the new 11962306a36Sopenharmony_ci * certificate as being ok to link. 12062306a36Sopenharmony_ci * 12162306a36Sopenharmony_ci * Returns 0 if the new certificate was accepted, -ENOKEY if the 12262306a36Sopenharmony_ci * certificate is not a CA. -ENOPKG if the signature uses unsupported 12362306a36Sopenharmony_ci * crypto, or some other error if there is a matching certificate but 12462306a36Sopenharmony_ci * the signature check cannot be performed. 12562306a36Sopenharmony_ci */ 12662306a36Sopenharmony_ciint restrict_link_by_ca(struct key *dest_keyring, 12762306a36Sopenharmony_ci const struct key_type *type, 12862306a36Sopenharmony_ci const union key_payload *payload, 12962306a36Sopenharmony_ci struct key *trust_keyring) 13062306a36Sopenharmony_ci{ 13162306a36Sopenharmony_ci const struct public_key *pkey; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci if (type != &key_type_asymmetric) 13462306a36Sopenharmony_ci return -EOPNOTSUPP; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci pkey = payload->data[asym_crypto]; 13762306a36Sopenharmony_ci if (!pkey) 13862306a36Sopenharmony_ci return -ENOPKG; 13962306a36Sopenharmony_ci if (!test_bit(KEY_EFLAG_CA, &pkey->key_eflags)) 14062306a36Sopenharmony_ci return -ENOKEY; 14162306a36Sopenharmony_ci if (!test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags)) 14262306a36Sopenharmony_ci return -ENOKEY; 14362306a36Sopenharmony_ci if (!IS_ENABLED(CONFIG_INTEGRITY_CA_MACHINE_KEYRING_MAX)) 14462306a36Sopenharmony_ci return 0; 14562306a36Sopenharmony_ci if (test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags)) 14662306a36Sopenharmony_ci return -ENOKEY; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci return 0; 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci/** 15262306a36Sopenharmony_ci * restrict_link_by_digsig - Restrict additions to a ring of digsig keys 15362306a36Sopenharmony_ci * @dest_keyring: Keyring being linked to. 15462306a36Sopenharmony_ci * @type: The type of key being added. 15562306a36Sopenharmony_ci * @payload: The payload of the new key. 15662306a36Sopenharmony_ci * @trust_keyring: A ring of keys that can be used to vouch for the new cert. 15762306a36Sopenharmony_ci * 15862306a36Sopenharmony_ci * Check if the new certificate has digitalSignature usage set. If it is, 15962306a36Sopenharmony_ci * then mark the new certificate as being ok to link. Afterwards verify 16062306a36Sopenharmony_ci * the new certificate against the ones in the trust_keyring. 16162306a36Sopenharmony_ci * 16262306a36Sopenharmony_ci * Returns 0 if the new certificate was accepted, -ENOKEY if the 16362306a36Sopenharmony_ci * certificate is not a digsig. -ENOPKG if the signature uses unsupported 16462306a36Sopenharmony_ci * crypto, or some other error if there is a matching certificate but 16562306a36Sopenharmony_ci * the signature check cannot be performed. 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ciint restrict_link_by_digsig(struct key *dest_keyring, 16862306a36Sopenharmony_ci const struct key_type *type, 16962306a36Sopenharmony_ci const union key_payload *payload, 17062306a36Sopenharmony_ci struct key *trust_keyring) 17162306a36Sopenharmony_ci{ 17262306a36Sopenharmony_ci const struct public_key *pkey; 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci if (type != &key_type_asymmetric) 17562306a36Sopenharmony_ci return -EOPNOTSUPP; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci pkey = payload->data[asym_crypto]; 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci if (!pkey) 18062306a36Sopenharmony_ci return -ENOPKG; 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci if (!test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags)) 18362306a36Sopenharmony_ci return -ENOKEY; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci if (test_bit(KEY_EFLAG_CA, &pkey->key_eflags)) 18662306a36Sopenharmony_ci return -ENOKEY; 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci if (test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags)) 18962306a36Sopenharmony_ci return -ENOKEY; 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci return restrict_link_by_signature(dest_keyring, type, payload, 19262306a36Sopenharmony_ci trust_keyring); 19362306a36Sopenharmony_ci} 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_cistatic bool match_either_id(const struct asymmetric_key_id **pair, 19662306a36Sopenharmony_ci const struct asymmetric_key_id *single) 19762306a36Sopenharmony_ci{ 19862306a36Sopenharmony_ci return (asymmetric_key_id_same(pair[0], single) || 19962306a36Sopenharmony_ci asymmetric_key_id_same(pair[1], single)); 20062306a36Sopenharmony_ci} 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_cistatic int key_or_keyring_common(struct key *dest_keyring, 20362306a36Sopenharmony_ci const struct key_type *type, 20462306a36Sopenharmony_ci const union key_payload *payload, 20562306a36Sopenharmony_ci struct key *trusted, bool check_dest) 20662306a36Sopenharmony_ci{ 20762306a36Sopenharmony_ci const struct public_key_signature *sig; 20862306a36Sopenharmony_ci struct key *key = NULL; 20962306a36Sopenharmony_ci int ret; 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci pr_devel("==>%s()\n", __func__); 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci if (!dest_keyring) 21462306a36Sopenharmony_ci return -ENOKEY; 21562306a36Sopenharmony_ci else if (dest_keyring->type != &key_type_keyring) 21662306a36Sopenharmony_ci return -EOPNOTSUPP; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci if (!trusted && !check_dest) 21962306a36Sopenharmony_ci return -ENOKEY; 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci if (type != &key_type_asymmetric) 22262306a36Sopenharmony_ci return -EOPNOTSUPP; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci sig = payload->data[asym_auth]; 22562306a36Sopenharmony_ci if (!sig) 22662306a36Sopenharmony_ci return -ENOPKG; 22762306a36Sopenharmony_ci if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2]) 22862306a36Sopenharmony_ci return -ENOKEY; 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci if (trusted) { 23162306a36Sopenharmony_ci if (trusted->type == &key_type_keyring) { 23262306a36Sopenharmony_ci /* See if we have a key that signed this one. */ 23362306a36Sopenharmony_ci key = find_asymmetric_key(trusted, sig->auth_ids[0], 23462306a36Sopenharmony_ci sig->auth_ids[1], 23562306a36Sopenharmony_ci sig->auth_ids[2], false); 23662306a36Sopenharmony_ci if (IS_ERR(key)) 23762306a36Sopenharmony_ci key = NULL; 23862306a36Sopenharmony_ci } else if (trusted->type == &key_type_asymmetric) { 23962306a36Sopenharmony_ci const struct asymmetric_key_id **signer_ids; 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci signer_ids = (const struct asymmetric_key_id **) 24262306a36Sopenharmony_ci asymmetric_key_ids(trusted)->id; 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci /* 24562306a36Sopenharmony_ci * The auth_ids come from the candidate key (the 24662306a36Sopenharmony_ci * one that is being considered for addition to 24762306a36Sopenharmony_ci * dest_keyring) and identify the key that was 24862306a36Sopenharmony_ci * used to sign. 24962306a36Sopenharmony_ci * 25062306a36Sopenharmony_ci * The signer_ids are identifiers for the 25162306a36Sopenharmony_ci * signing key specified for dest_keyring. 25262306a36Sopenharmony_ci * 25362306a36Sopenharmony_ci * The first auth_id is the preferred id, 2nd and 25462306a36Sopenharmony_ci * 3rd are the fallbacks. If exactly one of 25562306a36Sopenharmony_ci * auth_ids[0] and auth_ids[1] is present, it may 25662306a36Sopenharmony_ci * match either signer_ids[0] or signed_ids[1]. 25762306a36Sopenharmony_ci * If both are present the first one may match 25862306a36Sopenharmony_ci * either signed_id but the second one must match 25962306a36Sopenharmony_ci * the second signer_id. If neither of them is 26062306a36Sopenharmony_ci * available, auth_ids[2] is matched against 26162306a36Sopenharmony_ci * signer_ids[2] as a fallback. 26262306a36Sopenharmony_ci */ 26362306a36Sopenharmony_ci if (!sig->auth_ids[0] && !sig->auth_ids[1]) { 26462306a36Sopenharmony_ci if (asymmetric_key_id_same(signer_ids[2], 26562306a36Sopenharmony_ci sig->auth_ids[2])) 26662306a36Sopenharmony_ci key = __key_get(trusted); 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_ci } else if (!sig->auth_ids[0] || !sig->auth_ids[1]) { 26962306a36Sopenharmony_ci const struct asymmetric_key_id *auth_id; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci auth_id = sig->auth_ids[0] ?: sig->auth_ids[1]; 27262306a36Sopenharmony_ci if (match_either_id(signer_ids, auth_id)) 27362306a36Sopenharmony_ci key = __key_get(trusted); 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci } else if (asymmetric_key_id_same(signer_ids[1], 27662306a36Sopenharmony_ci sig->auth_ids[1]) && 27762306a36Sopenharmony_ci match_either_id(signer_ids, 27862306a36Sopenharmony_ci sig->auth_ids[0])) { 27962306a36Sopenharmony_ci key = __key_get(trusted); 28062306a36Sopenharmony_ci } 28162306a36Sopenharmony_ci } else { 28262306a36Sopenharmony_ci return -EOPNOTSUPP; 28362306a36Sopenharmony_ci } 28462306a36Sopenharmony_ci } 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci if (check_dest && !key) { 28762306a36Sopenharmony_ci /* See if the destination has a key that signed this one. */ 28862306a36Sopenharmony_ci key = find_asymmetric_key(dest_keyring, sig->auth_ids[0], 28962306a36Sopenharmony_ci sig->auth_ids[1], sig->auth_ids[2], 29062306a36Sopenharmony_ci false); 29162306a36Sopenharmony_ci if (IS_ERR(key)) 29262306a36Sopenharmony_ci key = NULL; 29362306a36Sopenharmony_ci } 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci if (!key) 29662306a36Sopenharmony_ci return -ENOKEY; 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci ret = key_validate(key); 29962306a36Sopenharmony_ci if (ret == 0) 30062306a36Sopenharmony_ci ret = verify_signature(key, sig); 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci key_put(key); 30362306a36Sopenharmony_ci return ret; 30462306a36Sopenharmony_ci} 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci/** 30762306a36Sopenharmony_ci * restrict_link_by_key_or_keyring - Restrict additions to a ring of public 30862306a36Sopenharmony_ci * keys using the restrict_key information stored in the ring. 30962306a36Sopenharmony_ci * @dest_keyring: Keyring being linked to. 31062306a36Sopenharmony_ci * @type: The type of key being added. 31162306a36Sopenharmony_ci * @payload: The payload of the new key. 31262306a36Sopenharmony_ci * @trusted: A key or ring of keys that can be used to vouch for the new cert. 31362306a36Sopenharmony_ci * 31462306a36Sopenharmony_ci * Check the new certificate only against the key or keys passed in the data 31562306a36Sopenharmony_ci * parameter. If one of those is the signing key and validates the new 31662306a36Sopenharmony_ci * certificate, then mark the new certificate as being ok to link. 31762306a36Sopenharmony_ci * 31862306a36Sopenharmony_ci * Returns 0 if the new certificate was accepted, -ENOKEY if we 31962306a36Sopenharmony_ci * couldn't find a matching parent certificate in the trusted list, 32062306a36Sopenharmony_ci * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses 32162306a36Sopenharmony_ci * unsupported crypto, or some other error if there is a matching certificate 32262306a36Sopenharmony_ci * but the signature check cannot be performed. 32362306a36Sopenharmony_ci */ 32462306a36Sopenharmony_ciint restrict_link_by_key_or_keyring(struct key *dest_keyring, 32562306a36Sopenharmony_ci const struct key_type *type, 32662306a36Sopenharmony_ci const union key_payload *payload, 32762306a36Sopenharmony_ci struct key *trusted) 32862306a36Sopenharmony_ci{ 32962306a36Sopenharmony_ci return key_or_keyring_common(dest_keyring, type, payload, trusted, 33062306a36Sopenharmony_ci false); 33162306a36Sopenharmony_ci} 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci/** 33462306a36Sopenharmony_ci * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of 33562306a36Sopenharmony_ci * public keys using the restrict_key information stored in the ring. 33662306a36Sopenharmony_ci * @dest_keyring: Keyring being linked to. 33762306a36Sopenharmony_ci * @type: The type of key being added. 33862306a36Sopenharmony_ci * @payload: The payload of the new key. 33962306a36Sopenharmony_ci * @trusted: A key or ring of keys that can be used to vouch for the new cert. 34062306a36Sopenharmony_ci * 34162306a36Sopenharmony_ci * Check the new certificate against the key or keys passed in the data 34262306a36Sopenharmony_ci * parameter and against the keys already linked to the destination keyring. If 34362306a36Sopenharmony_ci * one of those is the signing key and validates the new certificate, then mark 34462306a36Sopenharmony_ci * the new certificate as being ok to link. 34562306a36Sopenharmony_ci * 34662306a36Sopenharmony_ci * Returns 0 if the new certificate was accepted, -ENOKEY if we 34762306a36Sopenharmony_ci * couldn't find a matching parent certificate in the trusted list, 34862306a36Sopenharmony_ci * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses 34962306a36Sopenharmony_ci * unsupported crypto, or some other error if there is a matching certificate 35062306a36Sopenharmony_ci * but the signature check cannot be performed. 35162306a36Sopenharmony_ci */ 35262306a36Sopenharmony_ciint restrict_link_by_key_or_keyring_chain(struct key *dest_keyring, 35362306a36Sopenharmony_ci const struct key_type *type, 35462306a36Sopenharmony_ci const union key_payload *payload, 35562306a36Sopenharmony_ci struct key *trusted) 35662306a36Sopenharmony_ci{ 35762306a36Sopenharmony_ci return key_or_keyring_common(dest_keyring, type, payload, trusted, 35862306a36Sopenharmony_ci true); 35962306a36Sopenharmony_ci} 360