1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Instantiate a public key crypto key from an X.509 Certificate
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) "X.509: "fmt
9 #include <crypto/hash.h>
10 #include <keys/asymmetric-parser.h>
11 #include <keys/asymmetric-subtype.h>
12 #include <keys/system_keyring.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include "asymmetric_keys.h"
18 #include "x509_parser.h"
21 * Set up the signature parameters in an X.509 certificate. This involves
22 * digesting the signed data and extracting the signature.
24 int x509_get_sig_params(struct x509_certificate
*cert
)
26 struct public_key_signature
*sig
= cert
->sig
;
27 struct crypto_shash
*tfm
;
28 struct shash_desc
*desc
;
32 pr_devel("==>%s()\n", __func__
);
34 sig
->s
= kmemdup(cert
->raw_sig
, cert
->raw_sig_size
, GFP_KERNEL
);
38 sig
->s_size
= cert
->raw_sig_size
;
40 /* Allocate the hashing algorithm we're going to need and find out how
41 * big the hash operational data will be.
43 tfm
= crypto_alloc_shash(sig
->hash_algo
, 0, 0);
45 if (PTR_ERR(tfm
) == -ENOENT
) {
46 cert
->unsupported_sig
= true;
52 desc_size
= crypto_shash_descsize(tfm
) + sizeof(*desc
);
53 sig
->digest_size
= crypto_shash_digestsize(tfm
);
56 sig
->digest
= kmalloc(sig
->digest_size
, GFP_KERNEL
);
60 desc
= kzalloc(desc_size
, GFP_KERNEL
);
66 ret
= crypto_shash_digest(desc
, cert
->tbs
, cert
->tbs_size
,
72 ret
= is_hash_blacklisted(sig
->digest
, sig
->digest_size
,
73 BLACKLIST_HASH_X509_TBS
);
74 if (ret
== -EKEYREJECTED
) {
75 pr_err("Cert %*phN is blacklisted\n",
76 sig
->digest_size
, sig
->digest
);
77 cert
->blacklisted
= true;
84 crypto_free_shash(tfm
);
85 pr_devel("<==%s() = %d\n", __func__
, ret
);
90 * Check for self-signedness in an X.509 cert and if found, check the signature
91 * immediately if we can.
93 int x509_check_for_self_signed(struct x509_certificate
*cert
)
97 pr_devel("==>%s()\n", __func__
);
99 if (cert
->raw_subject_size
!= cert
->raw_issuer_size
||
100 memcmp(cert
->raw_subject
, cert
->raw_issuer
,
101 cert
->raw_issuer_size
) != 0)
102 goto not_self_signed
;
104 if (cert
->sig
->auth_ids
[0] || cert
->sig
->auth_ids
[1]) {
105 /* If the AKID is present it may have one or two parts. If
106 * both are supplied, both must match.
108 bool a
= asymmetric_key_id_same(cert
->skid
, cert
->sig
->auth_ids
[1]);
109 bool b
= asymmetric_key_id_same(cert
->id
, cert
->sig
->auth_ids
[0]);
112 goto not_self_signed
;
115 if (((a
&& !b
) || (b
&& !a
)) &&
116 cert
->sig
->auth_ids
[0] && cert
->sig
->auth_ids
[1])
120 if (cert
->unsupported_sig
) {
125 ret
= public_key_verify_signature(cert
->pub
, cert
->sig
);
127 if (ret
== -ENOPKG
) {
128 cert
->unsupported_sig
= true;
134 pr_devel("Cert Self-signature verified");
135 cert
->self_signed
= true;
138 pr_devel("<==%s() = %d\n", __func__
, ret
);
142 pr_devel("<==%s() = 0 [not]\n", __func__
);
147 * Attempt to parse a data blob for a key as an X509 certificate.
149 static int x509_key_preparse(struct key_preparsed_payload
*prep
)
151 struct x509_certificate
*cert
__free(x509_free_certificate
);
152 struct asymmetric_key_ids
*kids
__free(kfree
) = NULL
;
153 char *p
, *desc
__free(kfree
) = NULL
;
157 cert
= x509_cert_parse(prep
->data
, prep
->datalen
);
159 return PTR_ERR(cert
);
161 pr_devel("Cert Issuer: %s\n", cert
->issuer
);
162 pr_devel("Cert Subject: %s\n", cert
->subject
);
163 pr_devel("Cert Key Algo: %s\n", cert
->pub
->pkey_algo
);
164 pr_devel("Cert Valid period: %lld-%lld\n", cert
->valid_from
, cert
->valid_to
);
166 cert
->pub
->id_type
= "X509";
168 if (cert
->unsupported_sig
) {
169 public_key_signature_free(cert
->sig
);
172 pr_devel("Cert Signature: %s + %s\n",
173 cert
->sig
->pkey_algo
, cert
->sig
->hash_algo
);
176 /* Don't permit addition of blacklisted keys */
177 if (cert
->blacklisted
)
178 return -EKEYREJECTED
;
180 /* Propose a description */
181 sulen
= strlen(cert
->subject
);
182 if (cert
->raw_skid
) {
183 srlen
= cert
->raw_skid_size
;
186 srlen
= cert
->raw_serial_size
;
187 q
= cert
->raw_serial
;
190 desc
= kmalloc(sulen
+ 2 + srlen
* 2 + 1, GFP_KERNEL
);
193 p
= memcpy(desc
, cert
->subject
, sulen
);
197 p
= bin2hex(p
, q
, srlen
);
200 kids
= kmalloc(sizeof(struct asymmetric_key_ids
), GFP_KERNEL
);
203 kids
->id
[0] = cert
->id
;
204 kids
->id
[1] = cert
->skid
;
205 kids
->id
[2] = asymmetric_key_generate_id(cert
->raw_subject
,
206 cert
->raw_subject_size
,
208 if (IS_ERR(kids
->id
[2]))
209 return PTR_ERR(kids
->id
[2]);
211 /* We're pinning the module by being linked against it */
212 __module_get(public_key_subtype
.owner
);
213 prep
->payload
.data
[asym_subtype
] = &public_key_subtype
;
214 prep
->payload
.data
[asym_key_ids
] = kids
;
215 prep
->payload
.data
[asym_crypto
] = cert
->pub
;
216 prep
->payload
.data
[asym_auth
] = cert
->sig
;
217 prep
->description
= desc
;
218 prep
->quotalen
= 100;
220 /* We've finished with the certificate */
230 static struct asymmetric_key_parser x509_key_parser
= {
231 .owner
= THIS_MODULE
,
233 .parse
= x509_key_preparse
,
239 static int __init
x509_key_init(void)
241 return register_asymmetric_key_parser(&x509_key_parser
);
244 static void __exit
x509_key_exit(void)
246 unregister_asymmetric_key_parser(&x509_key_parser
);
249 module_init(x509_key_init
);
250 module_exit(x509_key_exit
);
252 MODULE_DESCRIPTION("X.509 certificate parser");
253 MODULE_AUTHOR("Red Hat, Inc.");
254 MODULE_LICENSE("GPL");