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 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 23 | /* |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 24 | * Set up the signature parameters in an X.509 certificate. This involves |
| 25 | * digesting the signed data and extracting the signature. |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 26 | */ |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 27 | int x509_get_sig_params(struct x509_certificate *cert) |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 28 | { |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 29 | struct public_key_signature *sig = cert->sig; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 30 | struct crypto_shash *tfm; |
| 31 | struct shash_desc *desc; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 32 | size_t desc_size; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 33 | int ret; |
| 34 | |
| 35 | pr_devel("==>%s()\n", __func__); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 36 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 37 | if (!cert->pub->pkey_algo) |
| 38 | cert->unsupported_key = true; |
| 39 | |
| 40 | if (!sig->pkey_algo) |
| 41 | cert->unsupported_sig = true; |
| 42 | |
| 43 | /* We check the hash if we can - even if we can't then verify it */ |
| 44 | if (!sig->hash_algo) { |
| 45 | cert->unsupported_sig = true; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 46 | return 0; |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 47 | } |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 48 | |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 49 | sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL); |
| 50 | if (!sig->s) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 51 | return -ENOMEM; |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 52 | |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 53 | sig->s_size = cert->raw_sig_size; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 54 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 55 | /* Allocate the hashing algorithm we're going to need and find out how |
| 56 | * big the hash operational data will be. |
| 57 | */ |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 58 | tfm = crypto_alloc_shash(sig->hash_algo, 0, 0); |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 59 | if (IS_ERR(tfm)) { |
| 60 | if (PTR_ERR(tfm) == -ENOENT) { |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 61 | cert->unsupported_sig = true; |
| 62 | return 0; |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 63 | } |
| 64 | return PTR_ERR(tfm); |
| 65 | } |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 66 | |
| 67 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 68 | sig->digest_size = crypto_shash_digestsize(tfm); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 69 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 70 | ret = -ENOMEM; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 71 | sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); |
| 72 | if (!sig->digest) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 73 | goto error; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 74 | |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 75 | desc = kzalloc(desc_size, GFP_KERNEL); |
| 76 | if (!desc) |
| 77 | goto error; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 78 | |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 79 | desc->tfm = tfm; |
| 80 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 81 | |
Eric Biggers | aa33003 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 82 | ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest); |
David Howells | 4365295 | 2017-04-03 16:07:25 +0100 | [diff] [blame] | 83 | if (ret < 0) |
| 84 | goto error_2; |
| 85 | |
| 86 | ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs"); |
| 87 | if (ret == -EKEYREJECTED) { |
| 88 | pr_err("Cert %*phN is blacklisted\n", |
| 89 | sig->digest_size, sig->digest); |
| 90 | cert->blacklisted = true; |
| 91 | ret = 0; |
| 92 | } |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 93 | |
| 94 | error_2: |
| 95 | kfree(desc); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 96 | error: |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 97 | crypto_free_shash(tfm); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 98 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 99 | return ret; |
| 100 | } |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 101 | |
| 102 | /* |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 103 | * Check for self-signedness in an X.509 cert and if found, check the signature |
| 104 | * immediately if we can. |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 105 | */ |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 106 | int x509_check_for_self_signed(struct x509_certificate *cert) |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 107 | { |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 108 | int ret = 0; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 109 | |
| 110 | pr_devel("==>%s()\n", __func__); |
| 111 | |
David Howells | ad3043f | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 112 | if (cert->raw_subject_size != cert->raw_issuer_size || |
| 113 | memcmp(cert->raw_subject, cert->raw_issuer, |
| 114 | cert->raw_issuer_size) != 0) |
| 115 | goto not_self_signed; |
| 116 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 117 | if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) { |
| 118 | /* If the AKID is present it may have one or two parts. If |
| 119 | * both are supplied, both must match. |
| 120 | */ |
| 121 | bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]); |
| 122 | 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] | 123 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 124 | if (!a && !b) |
| 125 | goto not_self_signed; |
| 126 | |
| 127 | ret = -EKEYREJECTED; |
| 128 | if (((a && !b) || (b && !a)) && |
| 129 | cert->sig->auth_ids[0] && cert->sig->auth_ids[1]) |
| 130 | goto out; |
| 131 | } |
| 132 | |
David Howells | ad3043f | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 133 | ret = -EKEYREJECTED; |
Eric Biggers | 54c1fb3 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 134 | if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0) |
David Howells | ad3043f | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 135 | goto out; |
| 136 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 137 | ret = public_key_verify_signature(cert->pub, cert->sig); |
| 138 | if (ret < 0) { |
| 139 | if (ret == -ENOPKG) { |
| 140 | cert->unsupported_sig = true; |
| 141 | ret = 0; |
| 142 | } |
| 143 | goto out; |
| 144 | } |
| 145 | |
| 146 | pr_devel("Cert Self-signature verified"); |
| 147 | cert->self_signed = true; |
| 148 | |
| 149 | out: |
| 150 | pr_devel("<==%s() = %d\n", __func__, ret); |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 151 | return ret; |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 152 | |
| 153 | not_self_signed: |
| 154 | pr_devel("<==%s() = 0 [not]\n", __func__); |
| 155 | return 0; |
David Howells | b426beb | 2013-08-30 16:18:02 +0100 | [diff] [blame] | 156 | } |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * Attempt to parse a data blob for a key as an X509 certificate. |
| 160 | */ |
| 161 | static int x509_key_preparse(struct key_preparsed_payload *prep) |
| 162 | { |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 163 | struct asymmetric_key_ids *kids; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 164 | struct x509_certificate *cert; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 165 | const char *q; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 166 | size_t srlen, sulen; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 167 | char *desc = NULL, *p; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 168 | int ret; |
| 169 | |
| 170 | cert = x509_cert_parse(prep->data, prep->datalen); |
| 171 | if (IS_ERR(cert)) |
| 172 | return PTR_ERR(cert); |
| 173 | |
| 174 | pr_devel("Cert Issuer: %s\n", cert->issuer); |
| 175 | pr_devel("Cert Subject: %s\n", cert->subject); |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 176 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 177 | if (cert->unsupported_key) { |
David Howells | 2ecdb23 | 2013-08-30 16:18:15 +0100 | [diff] [blame] | 178 | ret = -ENOPKG; |
| 179 | goto error_free_cert; |
| 180 | } |
| 181 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 182 | pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo); |
David Howells | fd19a3d | 2015-07-29 16:58:32 +0100 | [diff] [blame] | 183 | 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] | 184 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 185 | cert->pub->id_type = "X509"; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 186 | |
David Howells | a511e1a | 2016-04-06 16:14:26 +0100 | [diff] [blame] | 187 | if (cert->unsupported_sig) { |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 188 | public_key_signature_free(cert->sig); |
| 189 | cert->sig = NULL; |
| 190 | } else { |
| 191 | pr_devel("Cert Signature: %s + %s\n", |
| 192 | cert->sig->pkey_algo, cert->sig->hash_algo); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 193 | } |
| 194 | |
David Howells | 4365295 | 2017-04-03 16:07:25 +0100 | [diff] [blame] | 195 | /* Don't permit addition of blacklisted keys */ |
| 196 | ret = -EKEYREJECTED; |
| 197 | if (cert->blacklisted) |
| 198 | goto error_free_cert; |
| 199 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 200 | /* Propose a description */ |
| 201 | sulen = strlen(cert->subject); |
David Howells | dd2f6c4 | 2014-10-03 16:17:02 +0100 | [diff] [blame] | 202 | if (cert->raw_skid) { |
| 203 | srlen = cert->raw_skid_size; |
| 204 | q = cert->raw_skid; |
| 205 | } else { |
| 206 | srlen = cert->raw_serial_size; |
| 207 | q = cert->raw_serial; |
| 208 | } |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 209 | |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 210 | ret = -ENOMEM; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 211 | desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 212 | if (!desc) |
| 213 | goto error_free_cert; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 214 | p = memcpy(desc, cert->subject, sulen); |
| 215 | p += sulen; |
| 216 | *p++ = ':'; |
| 217 | *p++ = ' '; |
| 218 | p = bin2hex(p, q, srlen); |
| 219 | *p = 0; |
| 220 | |
| 221 | kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL); |
| 222 | if (!kids) |
| 223 | goto error_free_desc; |
| 224 | kids->id[0] = cert->id; |
| 225 | kids->id[1] = cert->skid; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 226 | |
| 227 | /* We're pinning the module by being linked against it */ |
| 228 | __module_get(public_key_subtype.owner); |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 229 | prep->payload.data[asym_subtype] = &public_key_subtype; |
| 230 | prep->payload.data[asym_key_ids] = kids; |
| 231 | prep->payload.data[asym_crypto] = cert->pub; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 232 | prep->payload.data[asym_auth] = cert->sig; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 233 | prep->description = desc; |
| 234 | prep->quotalen = 100; |
| 235 | |
| 236 | /* We've finished with the certificate */ |
| 237 | cert->pub = NULL; |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 238 | cert->id = NULL; |
| 239 | cert->skid = NULL; |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 240 | cert->sig = NULL; |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 241 | desc = NULL; |
| 242 | ret = 0; |
| 243 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 244 | error_free_desc: |
| 245 | kfree(desc); |
David Howells | c26fd69 | 2012-09-24 17:11:48 +0100 | [diff] [blame] | 246 | error_free_cert: |
| 247 | x509_free_certificate(cert); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static struct asymmetric_key_parser x509_key_parser = { |
| 252 | .owner = THIS_MODULE, |
| 253 | .name = "x509", |
| 254 | .parse = x509_key_preparse, |
| 255 | }; |
| 256 | |
| 257 | /* |
| 258 | * Module stuff |
| 259 | */ |
| 260 | static int __init x509_key_init(void) |
| 261 | { |
| 262 | return register_asymmetric_key_parser(&x509_key_parser); |
| 263 | } |
| 264 | |
| 265 | static void __exit x509_key_exit(void) |
| 266 | { |
| 267 | unregister_asymmetric_key_parser(&x509_key_parser); |
| 268 | } |
| 269 | |
| 270 | module_init(x509_key_init); |
| 271 | module_exit(x509_key_exit); |
Konstantin Khlebnikov | e19aaa7 | 2013-09-17 15:14:55 +0400 | [diff] [blame] | 272 | |
| 273 | MODULE_DESCRIPTION("X.509 certificate parser"); |
David Howells | 1e684d3 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 274 | MODULE_AUTHOR("Red Hat, Inc."); |
Konstantin Khlebnikov | e19aaa7 | 2013-09-17 15:14:55 +0400 | [diff] [blame] | 275 | MODULE_LICENSE("GPL"); |