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 | } |
| 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 | } |
| 140 | |
| 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 | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 156 | struct crypto_shash *tfm; |
| 157 | struct shash_desc *desc; |
| 158 | size_t digest_size, desc_size; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 159 | void *digest; |
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 | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 164 | if (cert->unsupported_crypto) |
| 165 | return -ENOPKG; |
Tadeusz Struk | 57f96bb | 2016-02-02 10:08:53 -0800 | [diff] [blame^] | 166 | if (cert->sig.s) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 167 | return 0; |
| 168 | |
Tadeusz Struk | 57f96bb | 2016-02-02 10:08:53 -0800 | [diff] [blame^] | 169 | cert->sig.s = kmemdup(cert->raw_sig, cert->raw_sig_size, |
| 170 | GFP_KERNEL); |
| 171 | if (!cert->sig.s) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 172 | return -ENOMEM; |
Tadeusz Struk | 57f96bb | 2016-02-02 10:08:53 -0800 | [diff] [blame^] | 173 | |
| 174 | cert->sig.s_size = cert->raw_sig_size; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 175 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 176 | /* Allocate the hashing algorithm we're going to need and find out how |
| 177 | * big the hash operational data will be. |
| 178 | */ |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 179 | tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0); |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 180 | if (IS_ERR(tfm)) { |
| 181 | if (PTR_ERR(tfm) == -ENOENT) { |
| 182 | cert->unsupported_crypto = true; |
| 183 | return -ENOPKG; |
| 184 | } |
| 185 | return PTR_ERR(tfm); |
| 186 | } |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 187 | |
| 188 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
| 189 | digest_size = crypto_shash_digestsize(tfm); |
| 190 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 191 | /* We allocate the hash operational data storage on the end of the |
| 192 | * digest storage space. |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 193 | */ |
| 194 | ret = -ENOMEM; |
Sowmini Varadhan | 271817a | 2015-10-19 17:23:28 -0400 | [diff] [blame] | 195 | digest = kzalloc(ALIGN(digest_size, __alignof__(*desc)) + desc_size, |
| 196 | GFP_KERNEL); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 197 | if (!digest) |
| 198 | goto error; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 199 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 200 | cert->sig.digest = digest; |
| 201 | cert->sig.digest_size = digest_size; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 202 | |
Sowmini Varadhan | 271817a | 2015-10-19 17:23:28 -0400 | [diff] [blame] | 203 | desc = PTR_ALIGN(digest + digest_size, __alignof__(*desc)); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 204 | desc->tfm = tfm; |
| 205 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 206 | |
| 207 | ret = crypto_shash_init(desc); |
| 208 | if (ret < 0) |
| 209 | goto error; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 210 | might_sleep(); |
| 211 | ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 212 | error: |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 213 | crypto_free_shash(tfm); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 214 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 215 | return ret; |
| 216 | } |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 217 | EXPORT_SYMBOL_GPL(x509_get_sig_params); |
| 218 | |
| 219 | /* |
| 220 | * Check the signature on a certificate using the provided public key |
| 221 | */ |
| 222 | int x509_check_signature(const struct public_key *pub, |
| 223 | struct x509_certificate *cert) |
| 224 | { |
| 225 | int ret; |
| 226 | |
| 227 | pr_devel("==>%s()\n", __func__); |
| 228 | |
| 229 | ret = x509_get_sig_params(cert); |
| 230 | if (ret < 0) |
| 231 | return ret; |
| 232 | |
| 233 | ret = public_key_verify_signature(pub, &cert->sig); |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 234 | if (ret == -ENOPKG) |
| 235 | cert->unsupported_crypto = true; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 236 | pr_debug("Cert Verification: %d\n", ret); |
| 237 | return ret; |
| 238 | } |
| 239 | EXPORT_SYMBOL_GPL(x509_check_signature); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 240 | |
| 241 | /* |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 242 | * Check the new certificate against the ones in the trust keyring. If one of |
| 243 | * those is the signing key and validates the new certificate, then mark the |
| 244 | * new certificate as being trusted. |
| 245 | * |
| 246 | * Return 0 if the new certificate was successfully validated, 1 if we couldn't |
| 247 | * find a matching parent certificate in the trusted list and an error if there |
| 248 | * is a matching certificate but the signature check fails. |
| 249 | */ |
| 250 | static int x509_validate_trust(struct x509_certificate *cert, |
| 251 | struct key *trust_keyring) |
| 252 | { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 253 | struct key *key; |
| 254 | int ret = 1; |
| 255 | |
| 256 | if (!trust_keyring) |
| 257 | return -EOPNOTSUPP; |
| 258 | |
David Howells | b92e657 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 259 | if (ca_keyid && !asymmetric_key_id_partial(cert->akid_skid, ca_keyid)) |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 260 | return -EPERM; |
| 261 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 262 | key = x509_request_asymmetric_key(trust_keyring, |
| 263 | cert->akid_id, cert->akid_skid, |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 264 | false); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 265 | if (!IS_ERR(key)) { |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 266 | if (!use_builtin_keys |
| 267 | || test_bit(KEY_FLAG_BUILTIN, &key->flags)) |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 268 | ret = x509_check_signature(key->payload.data[asym_crypto], |
| 269 | cert); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 270 | key_put(key); |
| 271 | } |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | /* |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 276 | * Attempt to parse a data blob for a key as an X509 certificate. |
| 277 | */ |
| 278 | static int x509_key_preparse(struct key_preparsed_payload *prep) |
| 279 | { |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 280 | struct asymmetric_key_ids *kids; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 281 | struct x509_certificate *cert; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 282 | const char *q; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 283 | size_t srlen, sulen; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 284 | char *desc = NULL, *p; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 285 | int ret; |
| 286 | |
| 287 | cert = x509_cert_parse(prep->data, prep->datalen); |
| 288 | if (IS_ERR(cert)) |
| 289 | return PTR_ERR(cert); |
| 290 | |
| 291 | pr_devel("Cert Issuer: %s\n", cert->issuer); |
| 292 | pr_devel("Cert Subject: %s\n", cert->subject); |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 293 | |
| 294 | if (cert->pub->pkey_algo >= PKEY_ALGO__LAST || |
| 295 | cert->sig.pkey_algo >= PKEY_ALGO__LAST || |
| 296 | cert->sig.pkey_hash_algo >= PKEY_HASH__LAST || |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 297 | !hash_algo_name[cert->sig.pkey_hash_algo]) { |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 298 | ret = -ENOPKG; |
| 299 | goto error_free_cert; |
| 300 | } |
| 301 | |
David Howells | 67f7d60b | 2013-08-30 16:15:24 +0100 | [diff] [blame] | 302 | pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]); |
David Howells | fd19a3d | 2015-07-29 16:58:32 +0100 | [diff] [blame] | 303 | pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 304 | pr_devel("Cert Signature: %s + %s\n", |
| 305 | pkey_algo_name[cert->sig.pkey_algo], |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 306 | hash_algo_name[cert->sig.pkey_hash_algo]); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 307 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 308 | cert->pub->id_type = PKEY_ID_X509; |
| 309 | |
David Howells | 17334ca | 2013-08-30 16:18:31 +0100 | [diff] [blame] | 310 | /* Check the signature on the key if it appears to be self-signed */ |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 311 | if ((!cert->akid_skid && !cert->akid_id) || |
| 312 | asymmetric_key_id_same(cert->skid, cert->akid_skid) || |
| 313 | asymmetric_key_id_same(cert->id, cert->akid_id)) { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 314 | ret = x509_check_signature(cert->pub, cert); /* self-signed */ |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 315 | if (ret < 0) |
| 316 | goto error_free_cert; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 317 | } else if (!prep->trusted) { |
| 318 | ret = x509_validate_trust(cert, get_system_trusted_keyring()); |
Petko Manolov | 41c89b6 | 2015-12-02 17:47:55 +0200 | [diff] [blame] | 319 | if (ret) |
| 320 | ret = x509_validate_trust(cert, get_ima_mok_keyring()); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 321 | if (!ret) |
| 322 | prep->trusted = 1; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* Propose a description */ |
| 326 | sulen = strlen(cert->subject); |
David Howells | dd2f6c4 | 2014-10-03 16:17:02 +0100 | [diff] [blame] | 327 | if (cert->raw_skid) { |
| 328 | srlen = cert->raw_skid_size; |
| 329 | q = cert->raw_skid; |
| 330 | } else { |
| 331 | srlen = cert->raw_serial_size; |
| 332 | q = cert->raw_serial; |
| 333 | } |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 334 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 335 | ret = -ENOMEM; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 336 | desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 337 | if (!desc) |
| 338 | goto error_free_cert; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 339 | p = memcpy(desc, cert->subject, sulen); |
| 340 | p += sulen; |
| 341 | *p++ = ':'; |
| 342 | *p++ = ' '; |
| 343 | p = bin2hex(p, q, srlen); |
| 344 | *p = 0; |
| 345 | |
| 346 | kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL); |
| 347 | if (!kids) |
| 348 | goto error_free_desc; |
| 349 | kids->id[0] = cert->id; |
| 350 | kids->id[1] = cert->skid; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 351 | |
| 352 | /* We're pinning the module by being linked against it */ |
| 353 | __module_get(public_key_subtype.owner); |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 354 | prep->payload.data[asym_subtype] = &public_key_subtype; |
| 355 | prep->payload.data[asym_key_ids] = kids; |
| 356 | prep->payload.data[asym_crypto] = cert->pub; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 357 | prep->description = desc; |
| 358 | prep->quotalen = 100; |
| 359 | |
| 360 | /* We've finished with the certificate */ |
| 361 | cert->pub = NULL; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 362 | cert->id = NULL; |
| 363 | cert->skid = NULL; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 364 | desc = NULL; |
| 365 | ret = 0; |
| 366 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 367 | error_free_desc: |
| 368 | kfree(desc); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 369 | error_free_cert: |
| 370 | x509_free_certificate(cert); |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static struct asymmetric_key_parser x509_key_parser = { |
| 375 | .owner = THIS_MODULE, |
| 376 | .name = "x509", |
| 377 | .parse = x509_key_preparse, |
| 378 | }; |
| 379 | |
| 380 | /* |
| 381 | * Module stuff |
| 382 | */ |
| 383 | static int __init x509_key_init(void) |
| 384 | { |
| 385 | return register_asymmetric_key_parser(&x509_key_parser); |
| 386 | } |
| 387 | |
| 388 | static void __exit x509_key_exit(void) |
| 389 | { |
| 390 | unregister_asymmetric_key_parser(&x509_key_parser); |
| 391 | } |
| 392 | |
| 393 | module_init(x509_key_init); |
| 394 | module_exit(x509_key_exit); |
Konstantin Khlebnikov | e19aaa7 | 2013-09-17 15:14:55 +0400 | [diff] [blame] | 395 | |
| 396 | MODULE_DESCRIPTION("X.509 certificate parser"); |
| 397 | MODULE_LICENSE("GPL"); |