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> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/mpi.h> |
| 18 | #include <linux/asn1_decoder.h> |
| 19 | #include <keys/asymmetric-subtype.h> |
| 20 | #include <keys/asymmetric-parser.h> |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 21 | #include <keys/system_keyring.h> |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 22 | #include <crypto/hash.h> |
| 23 | #include "asymmetric_keys.h" |
| 24 | #include "public_key.h" |
| 25 | #include "x509_parser.h" |
| 26 | |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 27 | static bool use_builtin_keys; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 28 | static struct asymmetric_key_id *ca_keyid; |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 29 | |
| 30 | #ifndef MODULE |
Mimi Zohar | f2b3dee | 2015-02-11 07:33:34 -0500 | [diff] [blame] | 31 | static struct { |
| 32 | struct asymmetric_key_id id; |
| 33 | unsigned char data[10]; |
| 34 | } cakey; |
| 35 | |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 36 | static int __init ca_keys_setup(char *str) |
| 37 | { |
| 38 | if (!str) /* default system keyring */ |
| 39 | return 1; |
| 40 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 41 | if (strncmp(str, "id:", 3) == 0) { |
Mimi Zohar | f2b3dee | 2015-02-11 07:33:34 -0500 | [diff] [blame] | 42 | struct asymmetric_key_id *p = &cakey.id; |
| 43 | size_t hexlen = (strlen(str) - 3) / 2; |
| 44 | int ret; |
| 45 | |
| 46 | if (hexlen == 0 || hexlen > sizeof(cakey.data)) { |
| 47 | pr_err("Missing or invalid ca_keys id\n"); |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen); |
| 52 | if (ret < 0) |
| 53 | pr_err("Unparsable ca_keys id hex string\n"); |
| 54 | else |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 55 | ca_keyid = p; /* owner key 'id:xxxxxx' */ |
| 56 | } else if (strcmp(str, "builtin") == 0) { |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 57 | use_builtin_keys = true; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 58 | } |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 59 | |
| 60 | return 1; |
| 61 | } |
| 62 | __setup("ca_keys=", ca_keys_setup); |
| 63 | #endif |
| 64 | |
David Howells | 5ce43ad | 2014-07-28 14:11:32 +0100 | [diff] [blame] | 65 | /** |
| 66 | * x509_request_asymmetric_key - Request a key by X.509 certificate params. |
| 67 | * @keyring: The keys to search. |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 68 | * @id: The issuer & serialNumber to look for or NULL. |
| 69 | * @skid: The subjectKeyIdentifier to look for or NULL. |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 70 | * @partial: Use partial match if true, exact if false. |
David Howells | 5ce43ad | 2014-07-28 14:11:32 +0100 | [diff] [blame] | 71 | * |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 72 | * Find a key in the given keyring by identifier. The preferred identifier is |
| 73 | * the issuer + serialNumber and the fallback identifier is the |
| 74 | * subjectKeyIdentifier. If both are given, the lookup is by the former, but |
| 75 | * the latter must also match. |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 76 | */ |
David Howells | 5ce43ad | 2014-07-28 14:11:32 +0100 | [diff] [blame] | 77 | struct key *x509_request_asymmetric_key(struct key *keyring, |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 78 | const struct asymmetric_key_id *id, |
| 79 | const struct asymmetric_key_id *skid, |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 80 | bool partial) |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 81 | { |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 82 | struct key *key; |
| 83 | key_ref_t ref; |
| 84 | const char *lookup; |
| 85 | char *req, *p; |
| 86 | int len; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 87 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 88 | if (id) { |
| 89 | lookup = id->data; |
| 90 | len = id->len; |
| 91 | } else { |
| 92 | lookup = skid->data; |
| 93 | len = skid->len; |
| 94 | } |
| 95 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 96 | /* Construct an identifier "id:<keyid>". */ |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 97 | p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL); |
| 98 | if (!req) |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 99 | return ERR_PTR(-ENOMEM); |
| 100 | |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 101 | if (partial) { |
| 102 | *p++ = 'i'; |
| 103 | *p++ = 'd'; |
| 104 | } else { |
| 105 | *p++ = 'e'; |
| 106 | *p++ = 'x'; |
| 107 | } |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 108 | *p++ = ':'; |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 109 | p = bin2hex(p, lookup, len); |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 110 | *p = 0; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 111 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 112 | pr_debug("Look up: \"%s\"\n", req); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 113 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 114 | ref = keyring_search(make_key_ref(keyring, 1), |
| 115 | &key_type_asymmetric, req); |
| 116 | if (IS_ERR(ref)) |
| 117 | pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref)); |
| 118 | kfree(req); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 119 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 120 | if (IS_ERR(ref)) { |
| 121 | switch (PTR_ERR(ref)) { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 122 | /* Hide some search errors */ |
| 123 | case -EACCES: |
| 124 | case -ENOTDIR: |
| 125 | case -EAGAIN: |
| 126 | return ERR_PTR(-ENOKEY); |
| 127 | default: |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 128 | return ERR_CAST(ref); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 132 | key = key_ref_to_ptr(ref); |
| 133 | if (id && skid) { |
| 134 | const struct asymmetric_key_ids *kids = asymmetric_key_ids(key); |
| 135 | if (!kids->id[1]) { |
| 136 | pr_debug("issuer+serial match, but expected SKID missing\n"); |
| 137 | goto reject; |
| 138 | } |
| 139 | if (!asymmetric_key_id_same(skid, kids->id[1])) { |
| 140 | pr_debug("issuer+serial match, but SKID does not\n"); |
| 141 | goto reject; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key)); |
| 146 | return key; |
| 147 | |
| 148 | reject: |
| 149 | key_put(key); |
| 150 | return ERR_PTR(-EKEYREJECTED); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 151 | } |
David Howells | cf5b50f | 2014-08-03 12:54:48 +0100 | [diff] [blame] | 152 | EXPORT_SYMBOL_GPL(x509_request_asymmetric_key); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 153 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 154 | /* |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 155 | * Set up the signature parameters in an X.509 certificate. This involves |
| 156 | * digesting the signed data and extracting the signature. |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 157 | */ |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 158 | int x509_get_sig_params(struct x509_certificate *cert) |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 159 | { |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 160 | struct crypto_shash *tfm; |
| 161 | struct shash_desc *desc; |
| 162 | size_t digest_size, desc_size; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 163 | void *digest; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 164 | int ret; |
| 165 | |
| 166 | pr_devel("==>%s()\n", __func__); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 167 | |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 168 | if (cert->unsupported_crypto) |
| 169 | return -ENOPKG; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 170 | if (cert->sig.rsa.s) |
| 171 | return 0; |
| 172 | |
| 173 | cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size); |
| 174 | if (!cert->sig.rsa.s) |
| 175 | return -ENOMEM; |
| 176 | cert->sig.nr_mpi = 1; |
| 177 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 178 | /* Allocate the hashing algorithm we're going to need and find out how |
| 179 | * big the hash operational data will be. |
| 180 | */ |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 181 | 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] | 182 | if (IS_ERR(tfm)) { |
| 183 | if (PTR_ERR(tfm) == -ENOENT) { |
| 184 | cert->unsupported_crypto = true; |
| 185 | return -ENOPKG; |
| 186 | } |
| 187 | return PTR_ERR(tfm); |
| 188 | } |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 189 | |
| 190 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
| 191 | digest_size = crypto_shash_digestsize(tfm); |
| 192 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 193 | /* We allocate the hash operational data storage on the end of the |
| 194 | * digest storage space. |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 195 | */ |
| 196 | ret = -ENOMEM; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 197 | digest = kzalloc(digest_size + desc_size, GFP_KERNEL); |
| 198 | if (!digest) |
| 199 | goto error; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 200 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 201 | cert->sig.digest = digest; |
| 202 | cert->sig.digest_size = digest_size; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 203 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 204 | desc = digest + digest_size; |
| 205 | desc->tfm = tfm; |
| 206 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 207 | |
| 208 | ret = crypto_shash_init(desc); |
| 209 | if (ret < 0) |
| 210 | goto error; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 211 | might_sleep(); |
| 212 | ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 213 | error: |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 214 | crypto_free_shash(tfm); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 215 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 216 | return ret; |
| 217 | } |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(x509_get_sig_params); |
| 219 | |
| 220 | /* |
| 221 | * Check the signature on a certificate using the provided public key |
| 222 | */ |
| 223 | int x509_check_signature(const struct public_key *pub, |
| 224 | struct x509_certificate *cert) |
| 225 | { |
| 226 | int ret; |
| 227 | |
| 228 | pr_devel("==>%s()\n", __func__); |
| 229 | |
| 230 | ret = x509_get_sig_params(cert); |
| 231 | if (ret < 0) |
| 232 | return ret; |
| 233 | |
| 234 | ret = public_key_verify_signature(pub, &cert->sig); |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 235 | if (ret == -ENOPKG) |
| 236 | cert->unsupported_crypto = true; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 237 | pr_debug("Cert Verification: %d\n", ret); |
| 238 | return ret; |
| 239 | } |
| 240 | EXPORT_SYMBOL_GPL(x509_check_signature); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 241 | |
| 242 | /* |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 243 | * Check the new certificate against the ones in the trust keyring. If one of |
| 244 | * those is the signing key and validates the new certificate, then mark the |
| 245 | * new certificate as being trusted. |
| 246 | * |
| 247 | * Return 0 if the new certificate was successfully validated, 1 if we couldn't |
| 248 | * find a matching parent certificate in the trusted list and an error if there |
| 249 | * is a matching certificate but the signature check fails. |
| 250 | */ |
| 251 | static int x509_validate_trust(struct x509_certificate *cert, |
| 252 | struct key *trust_keyring) |
| 253 | { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 254 | struct key *key; |
| 255 | int ret = 1; |
| 256 | |
| 257 | if (!trust_keyring) |
| 258 | return -EOPNOTSUPP; |
| 259 | |
David Howells | b92e657 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 260 | if (ca_keyid && !asymmetric_key_id_partial(cert->akid_skid, ca_keyid)) |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 261 | return -EPERM; |
| 262 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 263 | key = x509_request_asymmetric_key(trust_keyring, |
| 264 | cert->akid_id, cert->akid_skid, |
Dmitry Kasatkin | f1b731d | 2014-10-06 15:21:05 +0100 | [diff] [blame] | 265 | false); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 266 | if (!IS_ERR(key)) { |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 267 | if (!use_builtin_keys |
| 268 | || test_bit(KEY_FLAG_BUILTIN, &key->flags)) |
| 269 | ret = x509_check_signature(key->payload.data, 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 || |
| 297 | !pkey_algo[cert->pub->pkey_algo] || |
| 298 | !pkey_algo[cert->sig.pkey_algo] || |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 299 | !hash_algo_name[cert->sig.pkey_hash_algo]) { |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 300 | ret = -ENOPKG; |
| 301 | goto error_free_cert; |
| 302 | } |
| 303 | |
David Howells | 67f7d60b | 2013-08-30 16:15:24 +0100 | [diff] [blame] | 304 | 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] | 305 | 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] | 306 | pr_devel("Cert Signature: %s + %s\n", |
| 307 | pkey_algo_name[cert->sig.pkey_algo], |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 308 | hash_algo_name[cert->sig.pkey_hash_algo]); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 309 | |
David Howells | 67f7d60b | 2013-08-30 16:15:24 +0100 | [diff] [blame] | 310 | cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 311 | cert->pub->id_type = PKEY_ID_X509; |
| 312 | |
David Howells | 17334ca | 2013-08-30 16:18:31 +0100 | [diff] [blame] | 313 | /* 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] | 314 | if ((!cert->akid_skid && !cert->akid_id) || |
| 315 | asymmetric_key_id_same(cert->skid, cert->akid_skid) || |
| 316 | asymmetric_key_id_same(cert->id, cert->akid_id)) { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 317 | ret = x509_check_signature(cert->pub, cert); /* self-signed */ |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 318 | if (ret < 0) |
| 319 | goto error_free_cert; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 320 | } else if (!prep->trusted) { |
| 321 | ret = x509_validate_trust(cert, get_system_trusted_keyring()); |
| 322 | if (!ret) |
| 323 | prep->trusted = 1; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* Propose a description */ |
| 327 | sulen = strlen(cert->subject); |
David Howells | dd2f6c4 | 2014-10-03 16:17:02 +0100 | [diff] [blame] | 328 | if (cert->raw_skid) { |
| 329 | srlen = cert->raw_skid_size; |
| 330 | q = cert->raw_skid; |
| 331 | } else { |
| 332 | srlen = cert->raw_serial_size; |
| 333 | q = cert->raw_serial; |
| 334 | } |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 335 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 336 | ret = -ENOMEM; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 337 | desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 338 | if (!desc) |
| 339 | goto error_free_cert; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 340 | p = memcpy(desc, cert->subject, sulen); |
| 341 | p += sulen; |
| 342 | *p++ = ':'; |
| 343 | *p++ = ' '; |
| 344 | p = bin2hex(p, q, srlen); |
| 345 | *p = 0; |
| 346 | |
| 347 | kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL); |
| 348 | if (!kids) |
| 349 | goto error_free_desc; |
| 350 | kids->id[0] = cert->id; |
| 351 | kids->id[1] = cert->skid; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 352 | |
| 353 | /* We're pinning the module by being linked against it */ |
| 354 | __module_get(public_key_subtype.owner); |
| 355 | prep->type_data[0] = &public_key_subtype; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 356 | prep->type_data[1] = kids; |
David Howells | fc7c70e | 2014-07-18 18:56:34 +0100 | [diff] [blame] | 357 | prep->payload[0] = cert->pub; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 358 | prep->description = desc; |
| 359 | prep->quotalen = 100; |
| 360 | |
| 361 | /* We've finished with the certificate */ |
| 362 | cert->pub = NULL; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 363 | cert->id = NULL; |
| 364 | cert->skid = NULL; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 365 | desc = NULL; |
| 366 | ret = 0; |
| 367 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 368 | error_free_desc: |
| 369 | kfree(desc); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 370 | error_free_cert: |
| 371 | x509_free_certificate(cert); |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | static struct asymmetric_key_parser x509_key_parser = { |
| 376 | .owner = THIS_MODULE, |
| 377 | .name = "x509", |
| 378 | .parse = x509_key_preparse, |
| 379 | }; |
| 380 | |
| 381 | /* |
| 382 | * Module stuff |
| 383 | */ |
| 384 | static int __init x509_key_init(void) |
| 385 | { |
| 386 | return register_asymmetric_key_parser(&x509_key_parser); |
| 387 | } |
| 388 | |
| 389 | static void __exit x509_key_exit(void) |
| 390 | { |
| 391 | unregister_asymmetric_key_parser(&x509_key_parser); |
| 392 | } |
| 393 | |
| 394 | module_init(x509_key_init); |
| 395 | module_exit(x509_key_exit); |
Konstantin Khlebnikov | e19aaa7 | 2013-09-17 15:14:55 +0400 | [diff] [blame] | 396 | |
| 397 | MODULE_DESCRIPTION("X.509 certificate parser"); |
| 398 | MODULE_LICENSE("GPL"); |