1 // SPDX-License-Identifier: GPL-2.0-or-later 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) 6 */ 7 8 #define pr_fmt(fmt) "X.509: "fmt 9 #include <crypto/hash.h> 10 #include <crypto/sm2.h> 11 #include <keys/asymmetric-parser.h> 12 #include <keys/asymmetric-subtype.h> 13 #include <keys/system_keyring.h> 14 #include <linux/module.h> 15 #include <linux/kernel.h> 16 #include <linux/slab.h> 17 #include <linux/string.h> 18 #include "asymmetric_keys.h" 19 #include "x509_parser.h" 20 21 /* 22 * Set up the signature parameters in an X.509 certificate. This involves 23 * digesting the signed data and extracting the signature. 24 */ 25 int x509_get_sig_params(struct x509_certificate *cert) 26 { 27 struct public_key_signature *sig = cert->sig; 28 struct crypto_shash *tfm; 29 struct shash_desc *desc; 30 size_t desc_size; 31 int ret; 32 33 pr_devel("==>%s()\n", __func__); 34 35 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL); 36 if (!sig->s) 37 return -ENOMEM; 38 39 sig->s_size = cert->raw_sig_size; 40 41 /* Allocate the hashing algorithm we're going to need and find out how 42 * big the hash operational data will be. 43 */ 44 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0); 45 if (IS_ERR(tfm)) { 46 if (PTR_ERR(tfm) == -ENOENT) { 47 cert->unsupported_sig = true; 48 return 0; 49 } 50 return PTR_ERR(tfm); 51 } 52 53 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); 54 sig->digest_size = crypto_shash_digestsize(tfm); 55 56 ret = -ENOMEM; 57 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); 58 if (!sig->digest) 59 goto error; 60 61 desc = kzalloc(desc_size, GFP_KERNEL); 62 if (!desc) 63 goto error; 64 65 desc->tfm = tfm; 66 67 if (strcmp(cert->pub->pkey_algo, "sm2") == 0) { 68 ret = strcmp(sig->hash_algo, "sm3") != 0 ? -EINVAL : 69 crypto_shash_init(desc) ?: 70 sm2_compute_z_digest(desc, cert->pub->key, 71 cert->pub->keylen, sig->digest) ?: 72 crypto_shash_init(desc) ?: 73 crypto_shash_update(desc, sig->digest, 74 sig->digest_size) ?: 75 crypto_shash_finup(desc, cert->tbs, cert->tbs_size, 76 sig->digest); 77 } else { 78 ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, 79 sig->digest); 80 } 81 82 if (ret < 0) 83 goto error_2; 84 85 ret = is_hash_blacklisted(sig->digest, sig->digest_size, 86 BLACKLIST_HASH_X509_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 } 93 94 error_2: 95 kfree(desc); 96 error: 97 crypto_free_shash(tfm); 98 pr_devel("<==%s() = %d\n", __func__, ret); 99 return ret; 100 } 101 102 /* 103 * Check for self-signedness in an X.509 cert and if found, check the signature 104 * immediately if we can. 105 */ 106 int x509_check_for_self_signed(struct x509_certificate *cert) 107 { 108 int ret = 0; 109 110 pr_devel("==>%s()\n", __func__); 111 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 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]); 123 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 133 if (cert->unsupported_sig) { 134 ret = 0; 135 goto out; 136 } 137 138 ret = public_key_verify_signature(cert->pub, cert->sig); 139 if (ret < 0) { 140 if (ret == -ENOPKG) { 141 cert->unsupported_sig = true; 142 ret = 0; 143 } 144 goto out; 145 } 146 147 pr_devel("Cert Self-signature verified"); 148 cert->self_signed = true; 149 150 out: 151 pr_devel("<==%s() = %d\n", __func__, ret); 152 return ret; 153 154 not_self_signed: 155 pr_devel("<==%s() = 0 [not]\n", __func__); 156 return 0; 157 } 158 159 /* 160 * Attempt to parse a data blob for a key as an X509 certificate. 161 */ 162 static int x509_key_preparse(struct key_preparsed_payload *prep) 163 { 164 struct x509_certificate *cert __free(x509_free_certificate); 165 struct asymmetric_key_ids *kids __free(kfree) = NULL; 166 char *p, *desc __free(kfree) = NULL; 167 const char *q; 168 size_t srlen, sulen; 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); 176 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo); 177 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to); 178 179 cert->pub->id_type = "X509"; 180 181 if (cert->unsupported_sig) { 182 public_key_signature_free(cert->sig); 183 cert->sig = NULL; 184 } else { 185 pr_devel("Cert Signature: %s + %s\n", 186 cert->sig->pkey_algo, cert->sig->hash_algo); 187 } 188 189 /* Don't permit addition of blacklisted keys */ 190 if (cert->blacklisted) 191 return -EKEYREJECTED; 192 193 /* Propose a description */ 194 sulen = strlen(cert->subject); 195 if (cert->raw_skid) { 196 srlen = cert->raw_skid_size; 197 q = cert->raw_skid; 198 } else { 199 srlen = cert->raw_serial_size; 200 q = cert->raw_serial; 201 } 202 203 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL); 204 if (!desc) 205 return -ENOMEM; 206 p = memcpy(desc, cert->subject, sulen); 207 p += sulen; 208 *p++ = ':'; 209 *p++ = ' '; 210 p = bin2hex(p, q, srlen); 211 *p = 0; 212 213 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL); 214 if (!kids) 215 return -ENOMEM; 216 kids->id[0] = cert->id; 217 kids->id[1] = cert->skid; 218 kids->id[2] = asymmetric_key_generate_id(cert->raw_subject, 219 cert->raw_subject_size, 220 "", 0); 221 if (IS_ERR(kids->id[2])) 222 return PTR_ERR(kids->id[2]); 223 224 /* We're pinning the module by being linked against it */ 225 __module_get(public_key_subtype.owner); 226 prep->payload.data[asym_subtype] = &public_key_subtype; 227 prep->payload.data[asym_key_ids] = kids; 228 prep->payload.data[asym_crypto] = cert->pub; 229 prep->payload.data[asym_auth] = cert->sig; 230 prep->description = desc; 231 prep->quotalen = 100; 232 233 /* We've finished with the certificate */ 234 cert->pub = NULL; 235 cert->id = NULL; 236 cert->skid = NULL; 237 cert->sig = NULL; 238 desc = NULL; 239 kids = NULL; 240 return 0; 241 } 242 243 static struct asymmetric_key_parser x509_key_parser = { 244 .owner = THIS_MODULE, 245 .name = "x509", 246 .parse = x509_key_preparse, 247 }; 248 249 /* 250 * Module stuff 251 */ 252 static int __init x509_key_init(void) 253 { 254 return register_asymmetric_key_parser(&x509_key_parser); 255 } 256 257 static void __exit x509_key_exit(void) 258 { 259 unregister_asymmetric_key_parser(&x509_key_parser); 260 } 261 262 module_init(x509_key_init); 263 module_exit(x509_key_exit); 264 265 MODULE_DESCRIPTION("X.509 certificate parser"); 266 MODULE_AUTHOR("Red Hat, Inc."); 267 MODULE_LICENSE("GPL"); 268