1*b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2c26fd69fSDavid Howells /* Instantiate a public key crypto key from an X.509 Certificate 3c26fd69fSDavid Howells * 4c26fd69fSDavid Howells * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. 5c26fd69fSDavid Howells * Written by David Howells (dhowells@redhat.com) 6c26fd69fSDavid Howells */ 7c26fd69fSDavid Howells 8c26fd69fSDavid Howells #define pr_fmt(fmt) "X.509: "fmt 9c26fd69fSDavid Howells #include <linux/module.h> 10c26fd69fSDavid Howells #include <linux/kernel.h> 11c26fd69fSDavid Howells #include <linux/slab.h> 12c26fd69fSDavid Howells #include <keys/asymmetric-subtype.h> 13c26fd69fSDavid Howells #include <keys/asymmetric-parser.h> 143be4beafSMimi Zohar #include <keys/system_keyring.h> 15c26fd69fSDavid Howells #include <crypto/hash.h> 16c26fd69fSDavid Howells #include "asymmetric_keys.h" 17c26fd69fSDavid Howells #include "x509_parser.h" 18c26fd69fSDavid Howells 19c26fd69fSDavid Howells /* 20b426beb6SDavid Howells * Set up the signature parameters in an X.509 certificate. This involves 21b426beb6SDavid Howells * digesting the signed data and extracting the signature. 22c26fd69fSDavid Howells */ 23b426beb6SDavid Howells int x509_get_sig_params(struct x509_certificate *cert) 24c26fd69fSDavid Howells { 2577d0910dSDavid Howells struct public_key_signature *sig = cert->sig; 26c26fd69fSDavid Howells struct crypto_shash *tfm; 27c26fd69fSDavid Howells struct shash_desc *desc; 2877d0910dSDavid Howells size_t desc_size; 29c26fd69fSDavid Howells int ret; 30c26fd69fSDavid Howells 31c26fd69fSDavid Howells pr_devel("==>%s()\n", __func__); 32c26fd69fSDavid Howells 336c2dc5aeSDavid Howells if (!cert->pub->pkey_algo) 346c2dc5aeSDavid Howells cert->unsupported_key = true; 356c2dc5aeSDavid Howells 366c2dc5aeSDavid Howells if (!sig->pkey_algo) 376c2dc5aeSDavid Howells cert->unsupported_sig = true; 386c2dc5aeSDavid Howells 396c2dc5aeSDavid Howells /* We check the hash if we can - even if we can't then verify it */ 406c2dc5aeSDavid Howells if (!sig->hash_algo) { 416c2dc5aeSDavid Howells cert->unsupported_sig = true; 42b426beb6SDavid Howells return 0; 436c2dc5aeSDavid Howells } 44b426beb6SDavid Howells 4577d0910dSDavid Howells sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL); 4677d0910dSDavid Howells if (!sig->s) 47b426beb6SDavid Howells return -ENOMEM; 48db6c43bdSTadeusz Struk 4977d0910dSDavid Howells sig->s_size = cert->raw_sig_size; 50b426beb6SDavid Howells 51c26fd69fSDavid Howells /* Allocate the hashing algorithm we're going to need and find out how 52c26fd69fSDavid Howells * big the hash operational data will be. 53c26fd69fSDavid Howells */ 5477d0910dSDavid Howells tfm = crypto_alloc_shash(sig->hash_algo, 0, 0); 5541559420SDavid Howells if (IS_ERR(tfm)) { 5641559420SDavid Howells if (PTR_ERR(tfm) == -ENOENT) { 576c2dc5aeSDavid Howells cert->unsupported_sig = true; 586c2dc5aeSDavid Howells return 0; 5941559420SDavid Howells } 6041559420SDavid Howells return PTR_ERR(tfm); 6141559420SDavid Howells } 62c26fd69fSDavid Howells 63c26fd69fSDavid Howells desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); 6477d0910dSDavid Howells sig->digest_size = crypto_shash_digestsize(tfm); 65c26fd69fSDavid Howells 66c26fd69fSDavid Howells ret = -ENOMEM; 6777d0910dSDavid Howells sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); 6877d0910dSDavid Howells if (!sig->digest) 69b426beb6SDavid Howells goto error; 70c26fd69fSDavid Howells 7177d0910dSDavid Howells desc = kzalloc(desc_size, GFP_KERNEL); 7277d0910dSDavid Howells if (!desc) 7377d0910dSDavid Howells goto error; 74c26fd69fSDavid Howells 75c26fd69fSDavid Howells desc->tfm = tfm; 76c26fd69fSDavid Howells 77aa330036SEric Biggers ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest); 7843652956SDavid Howells if (ret < 0) 7943652956SDavid Howells goto error_2; 8043652956SDavid Howells 8143652956SDavid Howells ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs"); 8243652956SDavid Howells if (ret == -EKEYREJECTED) { 8343652956SDavid Howells pr_err("Cert %*phN is blacklisted\n", 8443652956SDavid Howells sig->digest_size, sig->digest); 8543652956SDavid Howells cert->blacklisted = true; 8643652956SDavid Howells ret = 0; 8743652956SDavid Howells } 8877d0910dSDavid Howells 8977d0910dSDavid Howells error_2: 9077d0910dSDavid Howells kfree(desc); 91c26fd69fSDavid Howells error: 92c26fd69fSDavid Howells crypto_free_shash(tfm); 93c26fd69fSDavid Howells pr_devel("<==%s() = %d\n", __func__, ret); 94c26fd69fSDavid Howells return ret; 95c26fd69fSDavid Howells } 96b426beb6SDavid Howells 97b426beb6SDavid Howells /* 986c2dc5aeSDavid Howells * Check for self-signedness in an X.509 cert and if found, check the signature 996c2dc5aeSDavid Howells * immediately if we can. 100b426beb6SDavid Howells */ 1016c2dc5aeSDavid Howells int x509_check_for_self_signed(struct x509_certificate *cert) 102b426beb6SDavid Howells { 1036c2dc5aeSDavid Howells int ret = 0; 104b426beb6SDavid Howells 105b426beb6SDavid Howells pr_devel("==>%s()\n", __func__); 106b426beb6SDavid Howells 107ad3043fdSDavid Howells if (cert->raw_subject_size != cert->raw_issuer_size || 108ad3043fdSDavid Howells memcmp(cert->raw_subject, cert->raw_issuer, 109ad3043fdSDavid Howells cert->raw_issuer_size) != 0) 110ad3043fdSDavid Howells goto not_self_signed; 111ad3043fdSDavid Howells 1126c2dc5aeSDavid Howells if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) { 1136c2dc5aeSDavid Howells /* If the AKID is present it may have one or two parts. If 1146c2dc5aeSDavid Howells * both are supplied, both must match. 1156c2dc5aeSDavid Howells */ 1166c2dc5aeSDavid Howells bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]); 1176c2dc5aeSDavid Howells bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]); 1186c2dc5aeSDavid Howells 1196c2dc5aeSDavid Howells if (!a && !b) 1206c2dc5aeSDavid Howells goto not_self_signed; 1216c2dc5aeSDavid Howells 1226c2dc5aeSDavid Howells ret = -EKEYREJECTED; 1236c2dc5aeSDavid Howells if (((a && !b) || (b && !a)) && 1246c2dc5aeSDavid Howells cert->sig->auth_ids[0] && cert->sig->auth_ids[1]) 1256c2dc5aeSDavid Howells goto out; 1266c2dc5aeSDavid Howells } 1276c2dc5aeSDavid Howells 128ad3043fdSDavid Howells ret = -EKEYREJECTED; 12954c1fb39SEric Biggers if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0) 130ad3043fdSDavid Howells goto out; 131ad3043fdSDavid Howells 1326c2dc5aeSDavid Howells ret = public_key_verify_signature(cert->pub, cert->sig); 1336c2dc5aeSDavid Howells if (ret < 0) { 1346c2dc5aeSDavid Howells if (ret == -ENOPKG) { 1356c2dc5aeSDavid Howells cert->unsupported_sig = true; 1366c2dc5aeSDavid Howells ret = 0; 1376c2dc5aeSDavid Howells } 1386c2dc5aeSDavid Howells goto out; 1396c2dc5aeSDavid Howells } 1406c2dc5aeSDavid Howells 1416c2dc5aeSDavid Howells pr_devel("Cert Self-signature verified"); 1426c2dc5aeSDavid Howells cert->self_signed = true; 1436c2dc5aeSDavid Howells 1446c2dc5aeSDavid Howells out: 1456c2dc5aeSDavid Howells pr_devel("<==%s() = %d\n", __func__, ret); 146b426beb6SDavid Howells return ret; 147b426beb6SDavid Howells 1486c2dc5aeSDavid Howells not_self_signed: 1496c2dc5aeSDavid Howells pr_devel("<==%s() = 0 [not]\n", __func__); 1506c2dc5aeSDavid Howells return 0; 151b426beb6SDavid Howells } 152c26fd69fSDavid Howells 153c26fd69fSDavid Howells /* 154c26fd69fSDavid Howells * Attempt to parse a data blob for a key as an X509 certificate. 155c26fd69fSDavid Howells */ 156c26fd69fSDavid Howells static int x509_key_preparse(struct key_preparsed_payload *prep) 157c26fd69fSDavid Howells { 15846963b77SDavid Howells struct asymmetric_key_ids *kids; 159c26fd69fSDavid Howells struct x509_certificate *cert; 16046963b77SDavid Howells const char *q; 161c26fd69fSDavid Howells size_t srlen, sulen; 16246963b77SDavid Howells char *desc = NULL, *p; 163c26fd69fSDavid Howells int ret; 164c26fd69fSDavid Howells 165c26fd69fSDavid Howells cert = x509_cert_parse(prep->data, prep->datalen); 166c26fd69fSDavid Howells if (IS_ERR(cert)) 167c26fd69fSDavid Howells return PTR_ERR(cert); 168c26fd69fSDavid Howells 169c26fd69fSDavid Howells pr_devel("Cert Issuer: %s\n", cert->issuer); 170c26fd69fSDavid Howells pr_devel("Cert Subject: %s\n", cert->subject); 1712ecdb23bSDavid Howells 1726c2dc5aeSDavid Howells if (cert->unsupported_key) { 1732ecdb23bSDavid Howells ret = -ENOPKG; 1742ecdb23bSDavid Howells goto error_free_cert; 1752ecdb23bSDavid Howells } 1762ecdb23bSDavid Howells 1774e8ae72aSDavid Howells pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo); 178fd19a3d1SDavid Howells pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to); 179c26fd69fSDavid Howells 1804e8ae72aSDavid Howells cert->pub->id_type = "X509"; 181c26fd69fSDavid Howells 182a511e1afSDavid Howells if (cert->unsupported_sig) { 1836c2dc5aeSDavid Howells public_key_signature_free(cert->sig); 1846c2dc5aeSDavid Howells cert->sig = NULL; 1856c2dc5aeSDavid Howells } else { 1866c2dc5aeSDavid Howells pr_devel("Cert Signature: %s + %s\n", 1876c2dc5aeSDavid Howells cert->sig->pkey_algo, cert->sig->hash_algo); 188c26fd69fSDavid Howells } 189c26fd69fSDavid Howells 19043652956SDavid Howells /* Don't permit addition of blacklisted keys */ 19143652956SDavid Howells ret = -EKEYREJECTED; 19243652956SDavid Howells if (cert->blacklisted) 19343652956SDavid Howells goto error_free_cert; 19443652956SDavid Howells 195c26fd69fSDavid Howells /* Propose a description */ 196c26fd69fSDavid Howells sulen = strlen(cert->subject); 197dd2f6c44SDavid Howells if (cert->raw_skid) { 198dd2f6c44SDavid Howells srlen = cert->raw_skid_size; 199dd2f6c44SDavid Howells q = cert->raw_skid; 200dd2f6c44SDavid Howells } else { 20146963b77SDavid Howells srlen = cert->raw_serial_size; 20246963b77SDavid Howells q = cert->raw_serial; 203dd2f6c44SDavid Howells } 20446963b77SDavid Howells 205c26fd69fSDavid Howells ret = -ENOMEM; 20646963b77SDavid Howells desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL); 207c26fd69fSDavid Howells if (!desc) 208c26fd69fSDavid Howells goto error_free_cert; 20946963b77SDavid Howells p = memcpy(desc, cert->subject, sulen); 21046963b77SDavid Howells p += sulen; 21146963b77SDavid Howells *p++ = ':'; 21246963b77SDavid Howells *p++ = ' '; 21346963b77SDavid Howells p = bin2hex(p, q, srlen); 21446963b77SDavid Howells *p = 0; 21546963b77SDavid Howells 21646963b77SDavid Howells kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL); 21746963b77SDavid Howells if (!kids) 21846963b77SDavid Howells goto error_free_desc; 21946963b77SDavid Howells kids->id[0] = cert->id; 22046963b77SDavid Howells kids->id[1] = cert->skid; 221c26fd69fSDavid Howells 222c26fd69fSDavid Howells /* We're pinning the module by being linked against it */ 223c26fd69fSDavid Howells __module_get(public_key_subtype.owner); 224146aa8b1SDavid Howells prep->payload.data[asym_subtype] = &public_key_subtype; 225146aa8b1SDavid Howells prep->payload.data[asym_key_ids] = kids; 226146aa8b1SDavid Howells prep->payload.data[asym_crypto] = cert->pub; 22777d0910dSDavid Howells prep->payload.data[asym_auth] = cert->sig; 228c26fd69fSDavid Howells prep->description = desc; 229c26fd69fSDavid Howells prep->quotalen = 100; 230c26fd69fSDavid Howells 231c26fd69fSDavid Howells /* We've finished with the certificate */ 232c26fd69fSDavid Howells cert->pub = NULL; 23346963b77SDavid Howells cert->id = NULL; 23446963b77SDavid Howells cert->skid = NULL; 23577d0910dSDavid Howells cert->sig = NULL; 236c26fd69fSDavid Howells desc = NULL; 237c26fd69fSDavid Howells ret = 0; 238c26fd69fSDavid Howells 23946963b77SDavid Howells error_free_desc: 24046963b77SDavid Howells kfree(desc); 241c26fd69fSDavid Howells error_free_cert: 242c26fd69fSDavid Howells x509_free_certificate(cert); 243c26fd69fSDavid Howells return ret; 244c26fd69fSDavid Howells } 245c26fd69fSDavid Howells 246c26fd69fSDavid Howells static struct asymmetric_key_parser x509_key_parser = { 247c26fd69fSDavid Howells .owner = THIS_MODULE, 248c26fd69fSDavid Howells .name = "x509", 249c26fd69fSDavid Howells .parse = x509_key_preparse, 250c26fd69fSDavid Howells }; 251c26fd69fSDavid Howells 252c26fd69fSDavid Howells /* 253c26fd69fSDavid Howells * Module stuff 254c26fd69fSDavid Howells */ 255c26fd69fSDavid Howells static int __init x509_key_init(void) 256c26fd69fSDavid Howells { 257c26fd69fSDavid Howells return register_asymmetric_key_parser(&x509_key_parser); 258c26fd69fSDavid Howells } 259c26fd69fSDavid Howells 260c26fd69fSDavid Howells static void __exit x509_key_exit(void) 261c26fd69fSDavid Howells { 262c26fd69fSDavid Howells unregister_asymmetric_key_parser(&x509_key_parser); 263c26fd69fSDavid Howells } 264c26fd69fSDavid Howells 265c26fd69fSDavid Howells module_init(x509_key_init); 266c26fd69fSDavid Howells module_exit(x509_key_exit); 267e19aaa7dSKonstantin Khlebnikov 268e19aaa7dSKonstantin Khlebnikov MODULE_DESCRIPTION("X.509 certificate parser"); 2691e684d38SDavid Howells MODULE_AUTHOR("Red Hat, Inc."); 270e19aaa7dSKonstantin Khlebnikov MODULE_LICENSE("GPL"); 271