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