David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 1 | /* Instantiate a public key crypto key from an X.509 Certificate |
| 2 | * |
| 3 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #define pr_fmt(fmt) "X.509: "fmt |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 16 | #include <keys/asymmetric-subtype.h> |
| 17 | #include <keys/asymmetric-parser.h> |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 18 | #include <keys/system_keyring.h> |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 19 | #include <crypto/hash.h> |
| 20 | #include "asymmetric_keys.h" |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 21 | #include "x509_parser.h" |
| 22 | |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 23 | static bool use_builtin_keys; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 24 | static struct asymmetric_key_id *ca_keyid; |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 25 | |
| 26 | #ifndef MODULE |
Mimi Zohar | f2b3dee | 2015-02-11 07:33:34 -0500 | [diff] [blame] | 27 | static struct { |
| 28 | struct asymmetric_key_id id; |
| 29 | unsigned char data[10]; |
| 30 | } cakey; |
| 31 | |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 32 | static int __init ca_keys_setup(char *str) |
| 33 | { |
| 34 | if (!str) /* default system keyring */ |
| 35 | return 1; |
| 36 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 37 | if (strncmp(str, "id:", 3) == 0) { |
Mimi Zohar | f2b3dee | 2015-02-11 07:33:34 -0500 | [diff] [blame] | 38 | struct asymmetric_key_id *p = &cakey.id; |
| 39 | size_t hexlen = (strlen(str) - 3) / 2; |
| 40 | int ret; |
| 41 | |
| 42 | if (hexlen == 0 || hexlen > sizeof(cakey.data)) { |
| 43 | pr_err("Missing or invalid ca_keys id\n"); |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen); |
| 48 | if (ret < 0) |
| 49 | pr_err("Unparsable ca_keys id hex string\n"); |
| 50 | else |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 51 | ca_keyid = p; /* owner key 'id:xxxxxx' */ |
| 52 | } else if (strcmp(str, "builtin") == 0) { |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 53 | use_builtin_keys = true; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 54 | } |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 55 | |
| 56 | return 1; |
| 57 | } |
| 58 | __setup("ca_keys=", ca_keys_setup); |
| 59 | #endif |
| 60 | |
David Howells | 5ce43ad | 2014-07-28 14:11:32 +0100 | [diff] [blame] | 61 | /** |
| 62 | * x509_request_asymmetric_key - Request a key by X.509 certificate params. |
| 63 | * @keyring: The keys to search. |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 64 | * @id: The issuer & serialNumber to look for or NULL. |
| 65 | * @skid: The subjectKeyIdentifier to look for or NULL. |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 66 | * @partial: Use partial match if true, exact if false. |
David Howells | 5ce43ad | 2014-07-28 14:11:32 +0100 | [diff] [blame] | 67 | * |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 68 | * Find a key in the given keyring by identifier. The preferred identifier is |
| 69 | * the issuer + serialNumber and the fallback identifier is the |
| 70 | * subjectKeyIdentifier. If both are given, the lookup is by the former, but |
| 71 | * the latter must also match. |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 72 | */ |
David Howells | 5ce43ad | 2014-07-28 14:11:32 +0100 | [diff] [blame] | 73 | struct key *x509_request_asymmetric_key(struct key *keyring, |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 74 | const struct asymmetric_key_id *id, |
| 75 | const struct asymmetric_key_id *skid, |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 76 | bool partial) |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 77 | { |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 78 | struct key *key; |
| 79 | key_ref_t ref; |
| 80 | const char *lookup; |
| 81 | char *req, *p; |
| 82 | int len; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 83 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 84 | if (id) { |
| 85 | lookup = id->data; |
| 86 | len = id->len; |
| 87 | } else { |
| 88 | lookup = skid->data; |
| 89 | len = skid->len; |
| 90 | } |
David Howells | 864e7a8 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 91 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 92 | /* Construct an identifier "id:<keyid>". */ |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 93 | p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL); |
| 94 | if (!req) |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 95 | return ERR_PTR(-ENOMEM); |
| 96 | |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 97 | if (partial) { |
| 98 | *p++ = 'i'; |
| 99 | *p++ = 'd'; |
| 100 | } else { |
| 101 | *p++ = 'e'; |
| 102 | *p++ = 'x'; |
| 103 | } |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 104 | *p++ = ':'; |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 105 | p = bin2hex(p, lookup, len); |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 106 | *p = 0; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 107 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 108 | pr_debug("Look up: \"%s\"\n", req); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 109 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 110 | ref = keyring_search(make_key_ref(keyring, 1), |
| 111 | &key_type_asymmetric, req); |
| 112 | if (IS_ERR(ref)) |
| 113 | pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref)); |
| 114 | kfree(req); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 115 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 116 | if (IS_ERR(ref)) { |
| 117 | switch (PTR_ERR(ref)) { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 118 | /* Hide some search errors */ |
| 119 | case -EACCES: |
| 120 | case -ENOTDIR: |
| 121 | case -EAGAIN: |
| 122 | return ERR_PTR(-ENOKEY); |
| 123 | default: |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 124 | return ERR_CAST(ref); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 128 | key = key_ref_to_ptr(ref); |
| 129 | if (id && skid) { |
| 130 | const struct asymmetric_key_ids *kids = asymmetric_key_ids(key); |
| 131 | if (!kids->id[1]) { |
| 132 | pr_debug("issuer+serial match, but expected SKID missing\n"); |
| 133 | goto reject; |
| 134 | } |
| 135 | if (!asymmetric_key_id_same(skid, kids->id[1])) { |
| 136 | pr_debug("issuer+serial match, but SKID does not\n"); |
| 137 | goto reject; |
| 138 | } |
| 139 | } |
David Howells | 864e7a8 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 140 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 141 | pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key)); |
| 142 | return key; |
| 143 | |
| 144 | reject: |
| 145 | key_put(key); |
| 146 | return ERR_PTR(-EKEYREJECTED); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 147 | } |
David Howells | cf5b50f | 2014-08-03 12:54:48 +0100 | [diff] [blame] | 148 | EXPORT_SYMBOL_GPL(x509_request_asymmetric_key); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 149 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 150 | /* |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 151 | * Set up the signature parameters in an X.509 certificate. This involves |
| 152 | * digesting the signed data and extracting the signature. |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 153 | */ |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 154 | int x509_get_sig_params(struct x509_certificate *cert) |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 155 | { |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 156 | struct public_key_signature *sig = cert->sig; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 157 | struct crypto_shash *tfm; |
| 158 | struct shash_desc *desc; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 159 | size_t desc_size; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 160 | int ret; |
| 161 | |
| 162 | pr_devel("==>%s()\n", __func__); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 163 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 164 | if (!cert->pub->pkey_algo) |
| 165 | cert->unsupported_key = true; |
| 166 | |
| 167 | if (!sig->pkey_algo) |
| 168 | cert->unsupported_sig = true; |
| 169 | |
| 170 | /* We check the hash if we can - even if we can't then verify it */ |
| 171 | if (!sig->hash_algo) { |
| 172 | cert->unsupported_sig = true; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 173 | return 0; |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 174 | } |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 175 | |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 176 | sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL); |
| 177 | if (!sig->s) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 178 | return -ENOMEM; |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 179 | |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 180 | sig->s_size = cert->raw_sig_size; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 181 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 182 | /* Allocate the hashing algorithm we're going to need and find out how |
| 183 | * big the hash operational data will be. |
| 184 | */ |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 185 | tfm = crypto_alloc_shash(sig->hash_algo, 0, 0); |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 186 | if (IS_ERR(tfm)) { |
| 187 | if (PTR_ERR(tfm) == -ENOENT) { |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 188 | cert->unsupported_sig = true; |
| 189 | return 0; |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 190 | } |
| 191 | return PTR_ERR(tfm); |
| 192 | } |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 193 | |
| 194 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 195 | sig->digest_size = crypto_shash_digestsize(tfm); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 196 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 197 | ret = -ENOMEM; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 198 | sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); |
| 199 | if (!sig->digest) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 200 | goto error; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 201 | |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 202 | desc = kzalloc(desc_size, GFP_KERNEL); |
| 203 | if (!desc) |
| 204 | goto error; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 205 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 206 | desc->tfm = tfm; |
| 207 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 208 | |
| 209 | ret = crypto_shash_init(desc); |
| 210 | if (ret < 0) |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 211 | goto error_2; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 212 | might_sleep(); |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 213 | ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest); |
| 214 | |
| 215 | error_2: |
| 216 | kfree(desc); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 217 | error: |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 218 | crypto_free_shash(tfm); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 219 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 220 | return ret; |
| 221 | } |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 222 | |
| 223 | /* |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 224 | * Check for self-signedness in an X.509 cert and if found, check the signature |
| 225 | * immediately if we can. |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 226 | */ |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 227 | int x509_check_for_self_signed(struct x509_certificate *cert) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 228 | { |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 229 | int ret = 0; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 230 | |
| 231 | pr_devel("==>%s()\n", __func__); |
| 232 | |
David Howells | ad3043f | 2016-04-06 16:13:34 +0100 | [diff] [blame^] | 233 | if (cert->raw_subject_size != cert->raw_issuer_size || |
| 234 | memcmp(cert->raw_subject, cert->raw_issuer, |
| 235 | cert->raw_issuer_size) != 0) |
| 236 | goto not_self_signed; |
| 237 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 238 | if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) { |
| 239 | /* If the AKID is present it may have one or two parts. If |
| 240 | * both are supplied, both must match. |
| 241 | */ |
| 242 | bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]); |
| 243 | bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 244 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 245 | if (!a && !b) |
| 246 | goto not_self_signed; |
| 247 | |
| 248 | ret = -EKEYREJECTED; |
| 249 | if (((a && !b) || (b && !a)) && |
| 250 | cert->sig->auth_ids[0] && cert->sig->auth_ids[1]) |
| 251 | goto out; |
| 252 | } |
| 253 | |
David Howells | ad3043f | 2016-04-06 16:13:34 +0100 | [diff] [blame^] | 254 | ret = -EKEYREJECTED; |
| 255 | if (cert->pub->pkey_algo != cert->sig->pkey_algo) |
| 256 | goto out; |
| 257 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 258 | ret = public_key_verify_signature(cert->pub, cert->sig); |
| 259 | if (ret < 0) { |
| 260 | if (ret == -ENOPKG) { |
| 261 | cert->unsupported_sig = true; |
| 262 | ret = 0; |
| 263 | } |
| 264 | goto out; |
| 265 | } |
| 266 | |
| 267 | pr_devel("Cert Self-signature verified"); |
| 268 | cert->self_signed = true; |
| 269 | |
| 270 | out: |
| 271 | pr_devel("<==%s() = %d\n", __func__, ret); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 272 | return ret; |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 273 | |
| 274 | not_self_signed: |
| 275 | pr_devel("<==%s() = 0 [not]\n", __func__); |
| 276 | return 0; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 277 | } |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 278 | |
| 279 | /* |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 280 | * Check the new certificate against the ones in the trust keyring. If one of |
| 281 | * those is the signing key and validates the new certificate, then mark the |
| 282 | * new certificate as being trusted. |
| 283 | * |
| 284 | * Return 0 if the new certificate was successfully validated, 1 if we couldn't |
| 285 | * find a matching parent certificate in the trusted list and an error if there |
| 286 | * is a matching certificate but the signature check fails. |
| 287 | */ |
| 288 | static int x509_validate_trust(struct x509_certificate *cert, |
| 289 | struct key *trust_keyring) |
| 290 | { |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 291 | struct public_key_signature *sig = cert->sig; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 292 | struct key *key; |
| 293 | int ret = 1; |
| 294 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 295 | if (!sig->auth_ids[0] && !sig->auth_ids[1]) |
| 296 | return 1; |
| 297 | |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 298 | if (!trust_keyring) |
| 299 | return -EOPNOTSUPP; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 300 | if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid)) |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 301 | return -EPERM; |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 302 | if (cert->unsupported_sig) |
| 303 | return -ENOPKG; |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 304 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 305 | key = x509_request_asymmetric_key(trust_keyring, |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 306 | sig->auth_ids[0], sig->auth_ids[1], |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 307 | false); |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 308 | if (IS_ERR(key)) |
| 309 | return PTR_ERR(key); |
| 310 | |
| 311 | if (!use_builtin_keys || |
| 312 | test_bit(KEY_FLAG_BUILTIN, &key->flags)) { |
| 313 | ret = public_key_verify_signature( |
| 314 | key->payload.data[asym_crypto], cert->sig); |
| 315 | if (ret == -ENOPKG) |
| 316 | cert->unsupported_sig = true; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 317 | } |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 318 | key_put(key); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | /* |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 323 | * Attempt to parse a data blob for a key as an X509 certificate. |
| 324 | */ |
| 325 | static int x509_key_preparse(struct key_preparsed_payload *prep) |
| 326 | { |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 327 | struct asymmetric_key_ids *kids; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 328 | struct x509_certificate *cert; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 329 | const char *q; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 330 | size_t srlen, sulen; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 331 | char *desc = NULL, *p; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 332 | int ret; |
| 333 | |
| 334 | cert = x509_cert_parse(prep->data, prep->datalen); |
| 335 | if (IS_ERR(cert)) |
| 336 | return PTR_ERR(cert); |
| 337 | |
| 338 | pr_devel("Cert Issuer: %s\n", cert->issuer); |
| 339 | pr_devel("Cert Subject: %s\n", cert->subject); |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 340 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 341 | if (cert->unsupported_key) { |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 342 | ret = -ENOPKG; |
| 343 | goto error_free_cert; |
| 344 | } |
| 345 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 346 | pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo); |
David Howells | fd19a3d | 2015-07-29 16:58:32 +0100 | [diff] [blame] | 347 | pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 348 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 349 | cert->pub->id_type = "X509"; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 350 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 351 | /* See if we can derive the trustability of this certificate. |
| 352 | * |
| 353 | * When it comes to self-signed certificates, we cannot evaluate |
| 354 | * trustedness except by the fact that we obtained it from a trusted |
| 355 | * location. So we just rely on x509_validate_trust() failing in this |
| 356 | * case. |
| 357 | * |
| 358 | * Note that there's a possibility of a self-signed cert matching a |
| 359 | * cert that we have (most likely a duplicate that we already trust) - |
| 360 | * in which case it will be marked trusted. |
| 361 | */ |
| 362 | if (cert->unsupported_sig || cert->self_signed) { |
| 363 | public_key_signature_free(cert->sig); |
| 364 | cert->sig = NULL; |
| 365 | } else { |
| 366 | pr_devel("Cert Signature: %s + %s\n", |
| 367 | cert->sig->pkey_algo, cert->sig->hash_algo); |
| 368 | |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 369 | ret = x509_validate_trust(cert, get_system_trusted_keyring()); |
Petko Manolov | 41c89b6 | 2015-12-02 17:47:55 +0200 | [diff] [blame] | 370 | if (ret) |
| 371 | ret = x509_validate_trust(cert, get_ima_mok_keyring()); |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 372 | if (ret == -EKEYREJECTED) |
| 373 | goto error_free_cert; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 374 | if (!ret) |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 375 | prep->trusted = true; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* Propose a description */ |
| 379 | sulen = strlen(cert->subject); |
David Howells | dd2f6c4 | 2014-10-03 16:17:02 +0100 | [diff] [blame] | 380 | if (cert->raw_skid) { |
| 381 | srlen = cert->raw_skid_size; |
| 382 | q = cert->raw_skid; |
| 383 | } else { |
| 384 | srlen = cert->raw_serial_size; |
| 385 | q = cert->raw_serial; |
| 386 | } |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 387 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 388 | ret = -ENOMEM; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 389 | desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 390 | if (!desc) |
| 391 | goto error_free_cert; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 392 | p = memcpy(desc, cert->subject, sulen); |
| 393 | p += sulen; |
| 394 | *p++ = ':'; |
| 395 | *p++ = ' '; |
| 396 | p = bin2hex(p, q, srlen); |
| 397 | *p = 0; |
| 398 | |
| 399 | kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL); |
| 400 | if (!kids) |
| 401 | goto error_free_desc; |
| 402 | kids->id[0] = cert->id; |
| 403 | kids->id[1] = cert->skid; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 404 | |
| 405 | /* We're pinning the module by being linked against it */ |
| 406 | __module_get(public_key_subtype.owner); |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 407 | prep->payload.data[asym_subtype] = &public_key_subtype; |
| 408 | prep->payload.data[asym_key_ids] = kids; |
| 409 | prep->payload.data[asym_crypto] = cert->pub; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 410 | prep->payload.data[asym_auth] = cert->sig; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 411 | prep->description = desc; |
| 412 | prep->quotalen = 100; |
| 413 | |
| 414 | /* We've finished with the certificate */ |
| 415 | cert->pub = NULL; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 416 | cert->id = NULL; |
| 417 | cert->skid = NULL; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 418 | cert->sig = NULL; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 419 | desc = NULL; |
| 420 | ret = 0; |
| 421 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 422 | error_free_desc: |
| 423 | kfree(desc); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 424 | error_free_cert: |
| 425 | x509_free_certificate(cert); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | static struct asymmetric_key_parser x509_key_parser = { |
| 430 | .owner = THIS_MODULE, |
| 431 | .name = "x509", |
| 432 | .parse = x509_key_preparse, |
| 433 | }; |
| 434 | |
| 435 | /* |
| 436 | * Module stuff |
| 437 | */ |
| 438 | static int __init x509_key_init(void) |
| 439 | { |
| 440 | return register_asymmetric_key_parser(&x509_key_parser); |
| 441 | } |
| 442 | |
| 443 | static void __exit x509_key_exit(void) |
| 444 | { |
| 445 | unregister_asymmetric_key_parser(&x509_key_parser); |
| 446 | } |
| 447 | |
| 448 | module_init(x509_key_init); |
| 449 | module_exit(x509_key_exit); |
Konstantin Khlebnikov | e19aaa7 | 2013-09-17 15:14:55 +0400 | [diff] [blame] | 450 | |
| 451 | MODULE_DESCRIPTION("X.509 certificate parser"); |
| 452 | MODULE_LICENSE("GPL"); |