Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/crypto/asymmetric_keys/x509_public_key.c
53266 views
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 ([email protected])
6
*/
7
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/hex.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
/* Calculate a SHA256 hash of the TBS and check it against the
36
* blacklist.
37
*/
38
sha256(cert->tbs, cert->tbs_size, cert->sha256);
39
ret = is_hash_blacklisted(cert->sha256, sizeof(cert->sha256),
40
BLACKLIST_HASH_X509_TBS);
41
if (ret == -EKEYREJECTED) {
42
pr_err("Cert %*phN is blacklisted\n",
43
(int)sizeof(cert->sha256), cert->sha256);
44
cert->blacklisted = true;
45
ret = 0;
46
}
47
48
sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
49
if (!sig->s)
50
return -ENOMEM;
51
52
sig->s_size = cert->raw_sig_size;
53
54
if (sig->algo_takes_data) {
55
/* The signature algorithm does whatever passes for hashing. */
56
sig->m = (u8 *)cert->tbs;
57
sig->m_size = cert->tbs_size;
58
sig->m_free = false;
59
goto out;
60
}
61
62
/* Allocate the hashing algorithm we're going to need and find out how
63
* big the hash operational data will be.
64
*/
65
tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
66
if (IS_ERR(tfm)) {
67
if (PTR_ERR(tfm) == -ENOENT) {
68
cert->unsupported_sig = true;
69
return 0;
70
}
71
return PTR_ERR(tfm);
72
}
73
74
desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
75
sig->m_size = crypto_shash_digestsize(tfm);
76
77
ret = -ENOMEM;
78
sig->m = kmalloc(sig->m_size, GFP_KERNEL);
79
if (!sig->m)
80
goto error;
81
sig->m_free = true;
82
83
desc = kzalloc(desc_size, GFP_KERNEL);
84
if (!desc)
85
goto error;
86
87
desc->tfm = tfm;
88
89
ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->m);
90
if (ret < 0)
91
goto error_2;
92
93
error_2:
94
kfree(desc);
95
error:
96
crypto_free_shash(tfm);
97
out:
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) = NULL;
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
269