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; |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 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' */ |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 38 | else if (strcmp(str, "builtin") == 0) |
| 39 | use_builtin_keys = true; |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 40 | |
| 41 | return 1; |
| 42 | } |
| 43 | __setup("ca_keys=", ca_keys_setup); |
| 44 | #endif |
| 45 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 46 | /* |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 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, |
David Howells | 185de09 | 2014-07-09 16:48:00 +0100 | [diff] [blame^] | 51 | const char *authority) |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 52 | { |
| 53 | key_ref_t key; |
David Howells | 185de09 | 2014-07-09 16:48:00 +0100 | [diff] [blame^] | 54 | size_t signer_len = strlen(signer), auth_len = strlen(authority); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 55 | char *id; |
| 56 | |
| 57 | /* Construct an identifier. */ |
| 58 | id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL); |
| 59 | if (!id) |
| 60 | return ERR_PTR(-ENOMEM); |
| 61 | |
| 62 | memcpy(id, signer, signer_len); |
| 63 | id[signer_len + 0] = ':'; |
| 64 | id[signer_len + 1] = ' '; |
| 65 | memcpy(id + signer_len + 2, authority, auth_len); |
| 66 | id[signer_len + 2 + auth_len] = 0; |
| 67 | |
| 68 | pr_debug("Look up: \"%s\"\n", id); |
| 69 | |
| 70 | key = keyring_search(make_key_ref(keyring, 1), |
| 71 | &key_type_asymmetric, id); |
| 72 | if (IS_ERR(key)) |
| 73 | pr_debug("Request for module key '%s' err %ld\n", |
| 74 | id, PTR_ERR(key)); |
| 75 | kfree(id); |
| 76 | |
| 77 | if (IS_ERR(key)) { |
| 78 | switch (PTR_ERR(key)) { |
| 79 | /* Hide some search errors */ |
| 80 | case -EACCES: |
| 81 | case -ENOTDIR: |
| 82 | case -EAGAIN: |
| 83 | return ERR_PTR(-ENOKEY); |
| 84 | default: |
| 85 | return ERR_CAST(key); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pr_devel("<==%s() = 0 [%x]\n", __func__, |
| 90 | key_serial(key_ref_to_ptr(key))); |
| 91 | return key_ref_to_ptr(key); |
| 92 | } |
| 93 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 94 | /* |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 95 | * Set up the signature parameters in an X.509 certificate. This involves |
| 96 | * digesting the signed data and extracting the signature. |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 97 | */ |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 98 | int x509_get_sig_params(struct x509_certificate *cert) |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 99 | { |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 100 | struct crypto_shash *tfm; |
| 101 | struct shash_desc *desc; |
| 102 | size_t digest_size, desc_size; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 103 | void *digest; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 104 | int ret; |
| 105 | |
| 106 | pr_devel("==>%s()\n", __func__); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 107 | |
| 108 | if (cert->sig.rsa.s) |
| 109 | return 0; |
| 110 | |
| 111 | cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size); |
| 112 | if (!cert->sig.rsa.s) |
| 113 | return -ENOMEM; |
| 114 | cert->sig.nr_mpi = 1; |
| 115 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 116 | /* Allocate the hashing algorithm we're going to need and find out how |
| 117 | * big the hash operational data will be. |
| 118 | */ |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 119 | tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 120 | if (IS_ERR(tfm)) |
| 121 | return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm); |
| 122 | |
| 123 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
| 124 | digest_size = crypto_shash_digestsize(tfm); |
| 125 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 126 | /* We allocate the hash operational data storage on the end of the |
| 127 | * digest storage space. |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 128 | */ |
| 129 | ret = -ENOMEM; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 130 | digest = kzalloc(digest_size + desc_size, GFP_KERNEL); |
| 131 | if (!digest) |
| 132 | goto error; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 133 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 134 | cert->sig.digest = digest; |
| 135 | cert->sig.digest_size = digest_size; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 136 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 137 | desc = digest + digest_size; |
| 138 | desc->tfm = tfm; |
| 139 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 140 | |
| 141 | ret = crypto_shash_init(desc); |
| 142 | if (ret < 0) |
| 143 | goto error; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 144 | might_sleep(); |
| 145 | ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 146 | error: |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 147 | crypto_free_shash(tfm); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 148 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 149 | return ret; |
| 150 | } |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 151 | EXPORT_SYMBOL_GPL(x509_get_sig_params); |
| 152 | |
| 153 | /* |
| 154 | * Check the signature on a certificate using the provided public key |
| 155 | */ |
| 156 | int x509_check_signature(const struct public_key *pub, |
| 157 | struct x509_certificate *cert) |
| 158 | { |
| 159 | int ret; |
| 160 | |
| 161 | pr_devel("==>%s()\n", __func__); |
| 162 | |
| 163 | ret = x509_get_sig_params(cert); |
| 164 | if (ret < 0) |
| 165 | return ret; |
| 166 | |
| 167 | ret = public_key_verify_signature(pub, &cert->sig); |
| 168 | pr_debug("Cert Verification: %d\n", ret); |
| 169 | return ret; |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(x509_check_signature); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 172 | |
| 173 | /* |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 174 | * Check the new certificate against the ones in the trust keyring. If one of |
| 175 | * those is the signing key and validates the new certificate, then mark the |
| 176 | * new certificate as being trusted. |
| 177 | * |
| 178 | * Return 0 if the new certificate was successfully validated, 1 if we couldn't |
| 179 | * find a matching parent certificate in the trusted list and an error if there |
| 180 | * is a matching certificate but the signature check fails. |
| 181 | */ |
| 182 | static int x509_validate_trust(struct x509_certificate *cert, |
| 183 | struct key *trust_keyring) |
| 184 | { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 185 | struct key *key; |
| 186 | int ret = 1; |
| 187 | |
| 188 | if (!trust_keyring) |
| 189 | return -EOPNOTSUPP; |
| 190 | |
Dmitry Kasatkin | ffb70f6 | 2014-06-17 11:56:58 +0300 | [diff] [blame] | 191 | if (ca_keyid && !asymmetric_keyid_match(cert->authority, ca_keyid)) |
| 192 | return -EPERM; |
| 193 | |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 194 | key = x509_request_asymmetric_key(trust_keyring, |
David Howells | 185de09 | 2014-07-09 16:48:00 +0100 | [diff] [blame^] | 195 | cert->issuer, cert->authority); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 196 | if (!IS_ERR(key)) { |
Dmitry Kasatkin | 32c4741 | 2014-06-17 11:56:59 +0300 | [diff] [blame] | 197 | if (!use_builtin_keys |
| 198 | || test_bit(KEY_FLAG_BUILTIN, &key->flags)) |
| 199 | ret = x509_check_signature(key->payload.data, cert); |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 200 | key_put(key); |
| 201 | } |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | /* |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 206 | * Attempt to parse a data blob for a key as an X509 certificate. |
| 207 | */ |
| 208 | static int x509_key_preparse(struct key_preparsed_payload *prep) |
| 209 | { |
| 210 | struct x509_certificate *cert; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 211 | size_t srlen, sulen; |
| 212 | char *desc = NULL; |
| 213 | int ret; |
| 214 | |
| 215 | cert = x509_cert_parse(prep->data, prep->datalen); |
| 216 | if (IS_ERR(cert)) |
| 217 | return PTR_ERR(cert); |
| 218 | |
| 219 | pr_devel("Cert Issuer: %s\n", cert->issuer); |
| 220 | pr_devel("Cert Subject: %s\n", cert->subject); |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 221 | |
| 222 | if (cert->pub->pkey_algo >= PKEY_ALGO__LAST || |
| 223 | cert->sig.pkey_algo >= PKEY_ALGO__LAST || |
| 224 | cert->sig.pkey_hash_algo >= PKEY_HASH__LAST || |
| 225 | !pkey_algo[cert->pub->pkey_algo] || |
| 226 | !pkey_algo[cert->sig.pkey_algo] || |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 227 | !hash_algo_name[cert->sig.pkey_hash_algo]) { |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 228 | ret = -ENOPKG; |
| 229 | goto error_free_cert; |
| 230 | } |
| 231 | |
David Howells | 67f7d60b | 2013-08-30 16:15:24 +0100 | [diff] [blame] | 232 | pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]); |
David Howells | 2f1c4fe | 2012-10-04 14:21:23 +0100 | [diff] [blame] | 233 | pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n", |
David Howells | a5752d1 | 2012-10-02 14:36:16 +0100 | [diff] [blame] | 234 | cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1, |
| 235 | cert->valid_from.tm_mday, cert->valid_from.tm_hour, |
| 236 | cert->valid_from.tm_min, cert->valid_from.tm_sec); |
David Howells | 2f1c4fe | 2012-10-04 14:21:23 +0100 | [diff] [blame] | 237 | pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n", |
David Howells | a5752d1 | 2012-10-02 14:36:16 +0100 | [diff] [blame] | 238 | cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1, |
| 239 | cert->valid_to.tm_mday, cert->valid_to.tm_hour, |
| 240 | cert->valid_to.tm_min, cert->valid_to.tm_sec); |
Dmitry Kasatkin | c7c8bb2 | 2013-04-25 10:43:56 +0300 | [diff] [blame] | 241 | pr_devel("Cert Signature: %s + %s\n", |
| 242 | pkey_algo_name[cert->sig.pkey_algo], |
Dmitry Kasatkin | 3fe78ca | 2013-05-06 15:58:15 +0300 | [diff] [blame] | 243 | hash_algo_name[cert->sig.pkey_hash_algo]); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 244 | |
David Howells | 17334ca | 2013-08-30 16:18:31 +0100 | [diff] [blame] | 245 | if (!cert->fingerprint) { |
| 246 | pr_warn("Cert for '%s' must have a SubjKeyId extension\n", |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 247 | cert->subject); |
| 248 | ret = -EKEYREJECTED; |
| 249 | goto error_free_cert; |
| 250 | } |
| 251 | |
David Howells | 67f7d60b | 2013-08-30 16:15:24 +0100 | [diff] [blame] | 252 | cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 253 | cert->pub->id_type = PKEY_ID_X509; |
| 254 | |
David Howells | 17334ca | 2013-08-30 16:18:31 +0100 | [diff] [blame] | 255 | /* Check the signature on the key if it appears to be self-signed */ |
| 256 | if (!cert->authority || |
| 257 | strcmp(cert->fingerprint, cert->authority) == 0) { |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 258 | ret = x509_check_signature(cert->pub, cert); /* self-signed */ |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 259 | if (ret < 0) |
| 260 | goto error_free_cert; |
Mimi Zohar | 3be4bea | 2013-08-20 14:36:27 -0400 | [diff] [blame] | 261 | } else if (!prep->trusted) { |
| 262 | ret = x509_validate_trust(cert, get_system_trusted_keyring()); |
| 263 | if (!ret) |
| 264 | prep->trusted = 1; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* Propose a description */ |
| 268 | sulen = strlen(cert->subject); |
| 269 | srlen = strlen(cert->fingerprint); |
| 270 | ret = -ENOMEM; |
| 271 | desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL); |
| 272 | if (!desc) |
| 273 | goto error_free_cert; |
| 274 | memcpy(desc, cert->subject, sulen); |
| 275 | desc[sulen] = ':'; |
| 276 | desc[sulen + 1] = ' '; |
| 277 | memcpy(desc + sulen + 2, cert->fingerprint, srlen); |
| 278 | desc[sulen + 2 + srlen] = 0; |
| 279 | |
| 280 | /* We're pinning the module by being linked against it */ |
| 281 | __module_get(public_key_subtype.owner); |
| 282 | prep->type_data[0] = &public_key_subtype; |
| 283 | prep->type_data[1] = cert->fingerprint; |
David Howells | fc7c70e | 2014-07-18 18:56:34 +0100 | [diff] [blame] | 284 | prep->payload[0] = cert->pub; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 285 | prep->description = desc; |
| 286 | prep->quotalen = 100; |
| 287 | |
| 288 | /* We've finished with the certificate */ |
| 289 | cert->pub = NULL; |
| 290 | cert->fingerprint = NULL; |
| 291 | desc = NULL; |
| 292 | ret = 0; |
| 293 | |
| 294 | error_free_cert: |
| 295 | x509_free_certificate(cert); |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static struct asymmetric_key_parser x509_key_parser = { |
| 300 | .owner = THIS_MODULE, |
| 301 | .name = "x509", |
| 302 | .parse = x509_key_preparse, |
| 303 | }; |
| 304 | |
| 305 | /* |
| 306 | * Module stuff |
| 307 | */ |
| 308 | static int __init x509_key_init(void) |
| 309 | { |
| 310 | return register_asymmetric_key_parser(&x509_key_parser); |
| 311 | } |
| 312 | |
| 313 | static void __exit x509_key_exit(void) |
| 314 | { |
| 315 | unregister_asymmetric_key_parser(&x509_key_parser); |
| 316 | } |
| 317 | |
| 318 | module_init(x509_key_init); |
| 319 | module_exit(x509_key_exit); |
Konstantin Khlebnikov | e19aaa7 | 2013-09-17 15:14:55 +0400 | [diff] [blame] | 320 | |
| 321 | MODULE_DESCRIPTION("X.509 certificate parser"); |
| 322 | MODULE_LICENSE("GPL"); |