|
18 | 18 | #include <linux/asn1_decoder.h> |
19 | 19 | #include <keys/asymmetric-subtype.h> |
20 | 20 | #include <keys/asymmetric-parser.h> |
| 21 | +#include <keys/system_keyring.h> |
21 | 22 | #include <crypto/hash.h> |
22 | 23 | #include "asymmetric_keys.h" |
23 | 24 | #include "public_key.h" |
24 | 25 | #include "x509_parser.h" |
25 | 26 |
|
| 27 | +static bool use_builtin_keys; |
| 28 | +static char *ca_keyid; |
| 29 | + |
| 30 | +#ifndef MODULE |
| 31 | +static int __init ca_keys_setup(char *str) |
| 32 | +{ |
| 33 | + if (!str) /* default system keyring */ |
| 34 | + return 1; |
| 35 | + |
| 36 | + if (strncmp(str, "id:", 3) == 0) |
| 37 | + ca_keyid = str; /* owner key 'id:xxxxxx' */ |
| 38 | + else if (strcmp(str, "builtin") == 0) |
| 39 | + use_builtin_keys = true; |
| 40 | + |
| 41 | + return 1; |
| 42 | +} |
| 43 | +__setup("ca_keys=", ca_keys_setup); |
| 44 | +#endif |
| 45 | + |
| 46 | +/* |
| 47 | + * Find a key in the given keyring by issuer and authority. |
| 48 | + */ |
| 49 | +static struct key *x509_request_asymmetric_key(struct key *keyring, |
| 50 | + const char *signer, |
| 51 | + size_t signer_len, |
| 52 | + const char *authority, |
| 53 | + size_t auth_len) |
| 54 | +{ |
| 55 | + key_ref_t key; |
| 56 | + char *id; |
| 57 | + |
| 58 | + /* Construct an identifier. */ |
| 59 | + id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL); |
| 60 | + if (!id) |
| 61 | + return ERR_PTR(-ENOMEM); |
| 62 | + |
| 63 | + memcpy(id, signer, signer_len); |
| 64 | + id[signer_len + 0] = ':'; |
| 65 | + id[signer_len + 1] = ' '; |
| 66 | + memcpy(id + signer_len + 2, authority, auth_len); |
| 67 | + id[signer_len + 2 + auth_len] = 0; |
| 68 | + |
| 69 | + pr_debug("Look up: \"%s\"\n", id); |
| 70 | + |
| 71 | + key = keyring_search(make_key_ref(keyring, 1), |
| 72 | + &key_type_asymmetric, id); |
| 73 | + if (IS_ERR(key)) |
| 74 | + pr_debug("Request for module key '%s' err %ld\n", |
| 75 | + id, PTR_ERR(key)); |
| 76 | + kfree(id); |
| 77 | + |
| 78 | + if (IS_ERR(key)) { |
| 79 | + switch (PTR_ERR(key)) { |
| 80 | + /* Hide some search errors */ |
| 81 | + case -EACCES: |
| 82 | + case -ENOTDIR: |
| 83 | + case -EAGAIN: |
| 84 | + return ERR_PTR(-ENOKEY); |
| 85 | + default: |
| 86 | + return ERR_CAST(key); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + pr_devel("<==%s() = 0 [%x]\n", __func__, |
| 91 | + key_serial(key_ref_to_ptr(key))); |
| 92 | + return key_ref_to_ptr(key); |
| 93 | +} |
| 94 | + |
26 | 95 | /* |
27 | 96 | * Set up the signature parameters in an X.509 certificate. This involves |
28 | 97 | * digesting the signed data and extracting the signature. |
@@ -102,6 +171,40 @@ int x509_check_signature(const struct public_key *pub, |
102 | 171 | } |
103 | 172 | EXPORT_SYMBOL_GPL(x509_check_signature); |
104 | 173 |
|
| 174 | +/* |
| 175 | + * Check the new certificate against the ones in the trust keyring. If one of |
| 176 | + * those is the signing key and validates the new certificate, then mark the |
| 177 | + * new certificate as being trusted. |
| 178 | + * |
| 179 | + * Return 0 if the new certificate was successfully validated, 1 if we couldn't |
| 180 | + * find a matching parent certificate in the trusted list and an error if there |
| 181 | + * is a matching certificate but the signature check fails. |
| 182 | + */ |
| 183 | +static int x509_validate_trust(struct x509_certificate *cert, |
| 184 | + struct key *trust_keyring) |
| 185 | +{ |
| 186 | + struct key *key; |
| 187 | + int ret = 1; |
| 188 | + |
| 189 | + if (!trust_keyring) |
| 190 | + return -EOPNOTSUPP; |
| 191 | + |
| 192 | + if (ca_keyid && !asymmetric_keyid_match(cert->authority, ca_keyid)) |
| 193 | + return -EPERM; |
| 194 | + |
| 195 | + key = x509_request_asymmetric_key(trust_keyring, |
| 196 | + cert->issuer, strlen(cert->issuer), |
| 197 | + cert->authority, |
| 198 | + strlen(cert->authority)); |
| 199 | + if (!IS_ERR(key)) { |
| 200 | + if (!use_builtin_keys |
| 201 | + || test_bit(KEY_FLAG_BUILTIN, &key->flags)) |
| 202 | + ret = x509_check_signature(key->payload.data, cert); |
| 203 | + key_put(key); |
| 204 | + } |
| 205 | + return ret; |
| 206 | +} |
| 207 | + |
105 | 208 | /* |
106 | 209 | * Attempt to parse a data blob for a key as an X509 certificate. |
107 | 210 | */ |
@@ -155,9 +258,13 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) |
155 | 258 | /* Check the signature on the key if it appears to be self-signed */ |
156 | 259 | if (!cert->authority || |
157 | 260 | strcmp(cert->fingerprint, cert->authority) == 0) { |
158 | | - ret = x509_check_signature(cert->pub, cert); |
| 261 | + ret = x509_check_signature(cert->pub, cert); /* self-signed */ |
159 | 262 | if (ret < 0) |
160 | 263 | goto error_free_cert; |
| 264 | + } else if (!prep->trusted) { |
| 265 | + ret = x509_validate_trust(cert, get_system_trusted_keyring()); |
| 266 | + if (!ret) |
| 267 | + prep->trusted = 1; |
161 | 268 | } |
162 | 269 |
|
163 | 270 | /* Propose a description */ |
|
0 commit comments