1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* Instantiate a public key crypto key from an 1 /* Instantiate a public key crypto key from an X.509 Certificate 3 * 2 * 4 * Copyright (C) 2012, 2016 Red Hat, Inc. All 3 * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.c 4 * Written by David Howells (dhowells@redhat.com) >> 5 * >> 6 * This program is free software; you can redistribute it and/or >> 7 * modify it under the terms of the GNU General Public Licence >> 8 * as published by the Free Software Foundation; either version >> 9 * 2 of the Licence, or (at your option) any later version. 6 */ 10 */ 7 11 8 #define pr_fmt(fmt) "ASYM: "fmt 12 #define pr_fmt(fmt) "ASYM: "fmt 9 #include <linux/module.h> 13 #include <linux/module.h> 10 #include <linux/kernel.h> 14 #include <linux/kernel.h> 11 #include <linux/err.h> 15 #include <linux/err.h> 12 #include <crypto/public_key.h> 16 #include <crypto/public_key.h> 13 #include "asymmetric_keys.h" 17 #include "asymmetric_keys.h" 14 18 15 static bool use_builtin_keys; 19 static bool use_builtin_keys; 16 static struct asymmetric_key_id *ca_keyid; 20 static struct asymmetric_key_id *ca_keyid; 17 21 18 #ifndef MODULE 22 #ifndef MODULE 19 static struct { 23 static struct { 20 struct asymmetric_key_id id; 24 struct asymmetric_key_id id; 21 unsigned char data[10]; 25 unsigned char data[10]; 22 } cakey; 26 } cakey; 23 27 24 static int __init ca_keys_setup(char *str) 28 static int __init ca_keys_setup(char *str) 25 { 29 { 26 if (!str) /* default sys 30 if (!str) /* default system keyring */ 27 return 1; 31 return 1; 28 32 29 if (strncmp(str, "id:", 3) == 0) { 33 if (strncmp(str, "id:", 3) == 0) { 30 struct asymmetric_key_id *p = 34 struct asymmetric_key_id *p = &cakey.id; 31 size_t hexlen = (strlen(str) - 35 size_t hexlen = (strlen(str) - 3) / 2; 32 int ret; 36 int ret; 33 37 34 if (hexlen == 0 || hexlen > si 38 if (hexlen == 0 || hexlen > sizeof(cakey.data)) { 35 pr_err("Missing or inv 39 pr_err("Missing or invalid ca_keys id\n"); 36 return 1; 40 return 1; 37 } 41 } 38 42 39 ret = __asymmetric_key_hex_to_ 43 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen); 40 if (ret < 0) 44 if (ret < 0) 41 pr_err("Unparsable ca_ 45 pr_err("Unparsable ca_keys id hex string\n"); 42 else 46 else 43 ca_keyid = p; /* own 47 ca_keyid = p; /* owner key 'id:xxxxxx' */ 44 } else if (strcmp(str, "builtin") == 0 48 } else if (strcmp(str, "builtin") == 0) { 45 use_builtin_keys = true; 49 use_builtin_keys = true; 46 } 50 } 47 51 48 return 1; 52 return 1; 49 } 53 } 50 __setup("ca_keys=", ca_keys_setup); 54 __setup("ca_keys=", ca_keys_setup); 51 #endif 55 #endif 52 56 53 /** 57 /** 54 * restrict_link_by_signature - Restrict addit 58 * restrict_link_by_signature - Restrict additions to a ring of public keys 55 * @dest_keyring: Keyring being linked to. 59 * @dest_keyring: Keyring being linked to. 56 * @type: The type of key being added. 60 * @type: The type of key being added. 57 * @payload: The payload of the new key. 61 * @payload: The payload of the new key. 58 * @trust_keyring: A ring of keys that can be 62 * @trust_keyring: A ring of keys that can be used to vouch for the new cert. 59 * 63 * 60 * Check the new certificate against the ones 64 * Check the new certificate against the ones in the trust keyring. If one of 61 * those is the signing key and validates the 65 * those is the signing key and validates the new certificate, then mark the 62 * new certificate as being trusted. 66 * new certificate as being trusted. 63 * 67 * 64 * Returns 0 if the new certificate was accept 68 * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a 65 * matching parent certificate in the trusted 69 * matching parent certificate in the trusted list, -EKEYREJECTED if the 66 * signature check fails or the key is blackli !! 70 * signature check fails or the key is blacklisted and some other error if 67 * uses unsupported crypto, or some other erro !! 71 * there is a matching certificate but the signature check cannot be performed. 68 * certificate but the signature check cannot << 69 */ 72 */ 70 int restrict_link_by_signature(struct key *des 73 int restrict_link_by_signature(struct key *dest_keyring, 71 const struct ke 74 const struct key_type *type, 72 const union key 75 const union key_payload *payload, 73 struct key *tru 76 struct key *trust_keyring) 74 { 77 { 75 const struct public_key_signature *sig 78 const struct public_key_signature *sig; 76 struct key *key; 79 struct key *key; 77 int ret; 80 int ret; 78 81 79 pr_devel("==>%s()\n", __func__); 82 pr_devel("==>%s()\n", __func__); 80 83 81 if (!trust_keyring) 84 if (!trust_keyring) 82 return -ENOKEY; 85 return -ENOKEY; 83 86 84 if (type != &key_type_asymmetric) 87 if (type != &key_type_asymmetric) 85 return -EOPNOTSUPP; 88 return -EOPNOTSUPP; 86 89 87 sig = payload->data[asym_auth]; 90 sig = payload->data[asym_auth]; 88 if (!sig) !! 91 if (!sig->auth_ids[0] && !sig->auth_ids[1]) 89 return -ENOPKG; << 90 if (!sig->auth_ids[0] && !sig->auth_id << 91 return -ENOKEY; 92 return -ENOKEY; 92 93 93 if (ca_keyid && !asymmetric_key_id_par 94 if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid)) 94 return -EPERM; 95 return -EPERM; 95 96 96 /* See if we have a key that signed th 97 /* See if we have a key that signed this one. */ 97 key = find_asymmetric_key(trust_keyrin 98 key = find_asymmetric_key(trust_keyring, 98 sig->auth_id 99 sig->auth_ids[0], sig->auth_ids[1], 99 sig->auth_id !! 100 false); 100 if (IS_ERR(key)) 101 if (IS_ERR(key)) 101 return -ENOKEY; 102 return -ENOKEY; 102 103 103 if (use_builtin_keys && !test_bit(KEY_ 104 if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags)) 104 ret = -ENOKEY; 105 ret = -ENOKEY; 105 else if (IS_BUILTIN(CONFIG_SECONDARY_T << 106 !strcmp(dest_keyring->descrip << 107 !test_bit(KEY_FLAG_BUILTIN, & << 108 ret = -ENOKEY; << 109 else 106 else 110 ret = verify_signature(key, si 107 ret = verify_signature(key, sig); 111 key_put(key); 108 key_put(key); 112 return ret; 109 return ret; 113 } 110 } 114 111 115 /** !! 112 static bool match_either_id(const struct asymmetric_key_ids *pair, 116 * restrict_link_by_ca - Restrict additions to << 117 * @dest_keyring: Keyring being linked to. << 118 * @type: The type of key being added. << 119 * @payload: The payload of the new key. << 120 * @trust_keyring: Unused. << 121 * << 122 * Check if the new certificate is a CA. If it << 123 * certificate as being ok to link. << 124 * << 125 * Returns 0 if the new certificate was accept << 126 * certificate is not a CA. -ENOPKG if the sig << 127 * crypto, or some other error if there is a m << 128 * the signature check cannot be performed. << 129 */ << 130 int restrict_link_by_ca(struct key *dest_keyri << 131 const struct key_type << 132 const union key_payloa << 133 struct key *trust_keyr << 134 { << 135 const struct public_key *pkey; << 136 << 137 if (type != &key_type_asymmetric) << 138 return -EOPNOTSUPP; << 139 << 140 pkey = payload->data[asym_crypto]; << 141 if (!pkey) << 142 return -ENOPKG; << 143 if (!test_bit(KEY_EFLAG_CA, &pkey->key << 144 return -ENOKEY; << 145 if (!test_bit(KEY_EFLAG_KEYCERTSIGN, & << 146 return -ENOKEY; << 147 if (!IS_ENABLED(CONFIG_INTEGRITY_CA_MA << 148 return 0; << 149 if (test_bit(KEY_EFLAG_DIGITALSIG, &pk << 150 return -ENOKEY; << 151 << 152 return 0; << 153 } << 154 << 155 /** << 156 * restrict_link_by_digsig - Restrict addition << 157 * @dest_keyring: Keyring being linked to. << 158 * @type: The type of key being added. << 159 * @payload: The payload of the new key. << 160 * @trust_keyring: A ring of keys that can be << 161 * << 162 * Check if the new certificate has digitalSig << 163 * then mark the new certificate as being ok t << 164 * the new certificate against the ones in the << 165 * << 166 * Returns 0 if the new certificate was accept << 167 * certificate is not a digsig. -ENOPKG if the << 168 * crypto, or some other error if there is a m << 169 * the signature check cannot be performed. << 170 */ << 171 int restrict_link_by_digsig(struct key *dest_k << 172 const struct key_t << 173 const union key_pa << 174 struct key *trust_ << 175 { << 176 const struct public_key *pkey; << 177 << 178 if (type != &key_type_asymmetric) << 179 return -EOPNOTSUPP; << 180 << 181 pkey = payload->data[asym_crypto]; << 182 << 183 if (!pkey) << 184 return -ENOPKG; << 185 << 186 if (!test_bit(KEY_EFLAG_DIGITALSIG, &p << 187 return -ENOKEY; << 188 << 189 if (test_bit(KEY_EFLAG_CA, &pkey->key_ << 190 return -ENOKEY; << 191 << 192 if (test_bit(KEY_EFLAG_KEYCERTSIGN, &p << 193 return -ENOKEY; << 194 << 195 return restrict_link_by_signature(dest << 196 trus << 197 } << 198 << 199 static bool match_either_id(const struct asymm << 200 const struct asymm 113 const struct asymmetric_key_id *single) 201 { 114 { 202 return (asymmetric_key_id_same(pair[0] !! 115 return (asymmetric_key_id_same(pair->id[0], single) || 203 asymmetric_key_id_same(pair[1] !! 116 asymmetric_key_id_same(pair->id[1], single)); 204 } 117 } 205 118 206 static int key_or_keyring_common(struct key *d 119 static int key_or_keyring_common(struct key *dest_keyring, 207 const struct 120 const struct key_type *type, 208 const union k 121 const union key_payload *payload, 209 struct key *t 122 struct key *trusted, bool check_dest) 210 { 123 { 211 const struct public_key_signature *sig 124 const struct public_key_signature *sig; 212 struct key *key = NULL; 125 struct key *key = NULL; 213 int ret; 126 int ret; 214 127 215 pr_devel("==>%s()\n", __func__); 128 pr_devel("==>%s()\n", __func__); 216 129 217 if (!dest_keyring) 130 if (!dest_keyring) 218 return -ENOKEY; 131 return -ENOKEY; 219 else if (dest_keyring->type != &key_ty 132 else if (dest_keyring->type != &key_type_keyring) 220 return -EOPNOTSUPP; 133 return -EOPNOTSUPP; 221 134 222 if (!trusted && !check_dest) 135 if (!trusted && !check_dest) 223 return -ENOKEY; 136 return -ENOKEY; 224 137 225 if (type != &key_type_asymmetric) 138 if (type != &key_type_asymmetric) 226 return -EOPNOTSUPP; 139 return -EOPNOTSUPP; 227 140 228 sig = payload->data[asym_auth]; 141 sig = payload->data[asym_auth]; 229 if (!sig) !! 142 if (!sig->auth_ids[0] && !sig->auth_ids[1]) 230 return -ENOPKG; << 231 if (!sig->auth_ids[0] && !sig->auth_id << 232 return -ENOKEY; 143 return -ENOKEY; 233 144 234 if (trusted) { 145 if (trusted) { 235 if (trusted->type == &key_type 146 if (trusted->type == &key_type_keyring) { 236 /* See if we have a ke 147 /* See if we have a key that signed this one. */ 237 key = find_asymmetric_ 148 key = find_asymmetric_key(trusted, sig->auth_ids[0], 238 !! 149 sig->auth_ids[1], false); 239 << 240 if (IS_ERR(key)) 150 if (IS_ERR(key)) 241 key = NULL; 151 key = NULL; 242 } else if (trusted->type == &k 152 } else if (trusted->type == &key_type_asymmetric) { 243 const struct asymmetri !! 153 const struct asymmetric_key_ids *signer_ids; 244 154 245 signer_ids = (const st !! 155 signer_ids = asymmetric_key_ids(trusted); 246 asymmetric_key << 247 156 248 /* 157 /* 249 * The auth_ids come f 158 * The auth_ids come from the candidate key (the 250 * one that is being c 159 * one that is being considered for addition to 251 * dest_keyring) and i 160 * dest_keyring) and identify the key that was 252 * used to sign. 161 * used to sign. 253 * 162 * 254 * The signer_ids are 163 * The signer_ids are identifiers for the 255 * signing key specifi 164 * signing key specified for dest_keyring. 256 * 165 * 257 * The first auth_id i !! 166 * The first auth_id is the preferred id, and 258 * 3rd are the fallbac !! 167 * the second is the fallback. If only one 259 * auth_ids[0] and aut !! 168 * auth_id is present, it may match against 260 * match either signer !! 169 * either signer_id. If two auth_ids are 261 * If both are present !! 170 * present, the first auth_id must match one 262 * either signed_id bu !! 171 * signer_id and the second auth_id must match 263 * the second signer_i !! 172 * the second signer_id. 264 * available, auth_ids << 265 * signer_ids[2] as a << 266 */ 173 */ 267 if (!sig->auth_ids[0] !! 174 if (!sig->auth_ids[0] || !sig->auth_ids[1]) { 268 if (asymmetric << 269 << 270 key = << 271 << 272 } else if (!sig->auth_ << 273 const struct a 175 const struct asymmetric_key_id *auth_id; 274 176 275 auth_id = sig- 177 auth_id = sig->auth_ids[0] ?: sig->auth_ids[1]; 276 if (match_eith 178 if (match_either_id(signer_ids, auth_id)) 277 key = 179 key = __key_get(trusted); 278 180 279 } else if (asymmetric_ !! 181 } else if (asymmetric_key_id_same(signer_ids->id[1], 280 182 sig->auth_ids[1]) && 281 match_eithe 183 match_either_id(signer_ids, 282 184 sig->auth_ids[0])) { 283 key = __key_ge 185 key = __key_get(trusted); 284 } 186 } 285 } else { 187 } else { 286 return -EOPNOTSUPP; 188 return -EOPNOTSUPP; 287 } 189 } 288 } 190 } 289 191 290 if (check_dest && !key) { 192 if (check_dest && !key) { 291 /* See if the destination has 193 /* See if the destination has a key that signed this one. */ 292 key = find_asymmetric_key(dest 194 key = find_asymmetric_key(dest_keyring, sig->auth_ids[0], 293 sig- !! 195 sig->auth_ids[1], false); 294 fals << 295 if (IS_ERR(key)) 196 if (IS_ERR(key)) 296 key = NULL; 197 key = NULL; 297 } 198 } 298 199 299 if (!key) 200 if (!key) 300 return -ENOKEY; 201 return -ENOKEY; 301 202 302 ret = key_validate(key); 203 ret = key_validate(key); 303 if (ret == 0) 204 if (ret == 0) 304 ret = verify_signature(key, si 205 ret = verify_signature(key, sig); 305 206 306 key_put(key); 207 key_put(key); 307 return ret; 208 return ret; 308 } 209 } 309 210 310 /** 211 /** 311 * restrict_link_by_key_or_keyring - Restrict 212 * restrict_link_by_key_or_keyring - Restrict additions to a ring of public 312 * keys using the restrict_key information sto 213 * keys using the restrict_key information stored in the ring. 313 * @dest_keyring: Keyring being linked to. 214 * @dest_keyring: Keyring being linked to. 314 * @type: The type of key being added. 215 * @type: The type of key being added. 315 * @payload: The payload of the new key. 216 * @payload: The payload of the new key. 316 * @trusted: A key or ring of keys that can be 217 * @trusted: A key or ring of keys that can be used to vouch for the new cert. 317 * 218 * 318 * Check the new certificate only against the 219 * Check the new certificate only against the key or keys passed in the data 319 * parameter. If one of those is the signing k 220 * parameter. If one of those is the signing key and validates the new 320 * certificate, then mark the new certificate 221 * certificate, then mark the new certificate as being ok to link. 321 * 222 * 322 * Returns 0 if the new certificate was accept 223 * Returns 0 if the new certificate was accepted, -ENOKEY if we 323 * couldn't find a matching parent certificate 224 * couldn't find a matching parent certificate in the trusted list, 324 * -EKEYREJECTED if the signature check fails, !! 225 * -EKEYREJECTED if the signature check fails, and some other error if 325 * unsupported crypto, or some other error if !! 226 * there is a matching certificate but the signature check cannot be 326 * but the signature check cannot be performed !! 227 * performed. 327 */ 228 */ 328 int restrict_link_by_key_or_keyring(struct key 229 int restrict_link_by_key_or_keyring(struct key *dest_keyring, 329 const stru 230 const struct key_type *type, 330 const unio 231 const union key_payload *payload, 331 struct key 232 struct key *trusted) 332 { 233 { 333 return key_or_keyring_common(dest_keyr 234 return key_or_keyring_common(dest_keyring, type, payload, trusted, 334 false); 235 false); 335 } 236 } 336 237 337 /** 238 /** 338 * restrict_link_by_key_or_keyring_chain - Res 239 * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of 339 * public keys using the restrict_key informat 240 * public keys using the restrict_key information stored in the ring. 340 * @dest_keyring: Keyring being linked to. 241 * @dest_keyring: Keyring being linked to. 341 * @type: The type of key being added. 242 * @type: The type of key being added. 342 * @payload: The payload of the new key. 243 * @payload: The payload of the new key. 343 * @trusted: A key or ring of keys that can be 244 * @trusted: A key or ring of keys that can be used to vouch for the new cert. 344 * 245 * 345 * Check the new certificate against the key o !! 246 * Check the new certificate only against the key or keys passed in the data 346 * parameter and against the keys already link !! 247 * parameter. If one of those is the signing key and validates the new 347 * one of those is the signing key and validat !! 248 * certificate, then mark the new certificate as being ok to link. 348 * the new certificate as being ok to link. << 349 * 249 * 350 * Returns 0 if the new certificate was accept 250 * Returns 0 if the new certificate was accepted, -ENOKEY if we 351 * couldn't find a matching parent certificate 251 * couldn't find a matching parent certificate in the trusted list, 352 * -EKEYREJECTED if the signature check fails, !! 252 * -EKEYREJECTED if the signature check fails, and some other error if 353 * unsupported crypto, or some other error if !! 253 * there is a matching certificate but the signature check cannot be 354 * but the signature check cannot be performed !! 254 * performed. 355 */ 255 */ 356 int restrict_link_by_key_or_keyring_chain(stru 256 int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring, 357 cons 257 const struct key_type *type, 358 cons 258 const union key_payload *payload, 359 stru 259 struct key *trusted) 360 { 260 { 361 return key_or_keyring_common(dest_keyr 261 return key_or_keyring_common(dest_keyring, type, payload, trusted, 362 true); 262 true); 363 } 263 } 364 264
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.