0001
0002
0003
0004
0005
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)
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;
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
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
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
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
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
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
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
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
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
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
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
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 }