blob: 192e39bae9068662314cd745f67ffa2df5fe4ae2 [file] [log] [blame]
AKASHI Takahiro063499e2020-07-21 19:35:19 +09001// SPDX-License-Identifier: GPL-2.0-or-later
2/* Verify the signature on a PKCS#7 message.
3 *
4 * Imported from crypto/asymmetric_keys/pkcs7_verify.c of linux 5.7
5 * with modification marked as __UBOOT__.
6 *
7 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
8 * Written by David Howells (dhowells@redhat.com)
9 */
10
11#define pr_fmt(fmt) "PKCS7: "fmt
12#ifdef __UBOOT__
13#include <string.h>
14#include <linux/bitops.h>
15#include <linux/compat.h>
16#include <linux/asn1.h>
17#include <crypto/public_key.h>
18#include <crypto/pkcs7_parser.h>
19#else
20#include <linux/kernel.h>
21#include <linux/export.h>
22#include <linux/slab.h>
23#include <linux/err.h>
24#include <linux/asn1.h>
25#include <crypto/hash.h>
26#include <crypto/hash_info.h>
27#include <crypto/public_key.h>
28#include "pkcs7_parser.h"
29#endif
30
31/*
32 * Digest the relevant parts of the PKCS#7 data
33 */
34#ifdef __UBOOT__
35static int pkcs7_digest(struct pkcs7_message *pkcs7,
36 struct pkcs7_signed_info *sinfo)
37{
38 return 0;
39}
40#else
41static int pkcs7_digest(struct pkcs7_message *pkcs7,
42 struct pkcs7_signed_info *sinfo)
43{
44 struct public_key_signature *sig = sinfo->sig;
45 struct crypto_shash *tfm;
46 struct shash_desc *desc;
47 size_t desc_size;
48 int ret;
49
50 kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo);
51
52 /* The digest was calculated already. */
53 if (sig->digest)
54 return 0;
55
56 if (!sinfo->sig->hash_algo)
57 return -ENOPKG;
58
59 /* Allocate the hashing algorithm we're going to need and find out how
60 * big the hash operational data will be.
61 */
62 tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0);
63 if (IS_ERR(tfm))
64 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
65
66 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
67 sig->digest_size = crypto_shash_digestsize(tfm);
68
69 ret = -ENOMEM;
70 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
71 if (!sig->digest)
72 goto error_no_desc;
73
74 desc = kzalloc(desc_size, GFP_KERNEL);
75 if (!desc)
76 goto error_no_desc;
77
78 desc->tfm = tfm;
79
80 /* Digest the message [RFC2315 9.3] */
81 ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len,
82 sig->digest);
83 if (ret < 0)
84 goto error;
85 pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest);
86
87 /* However, if there are authenticated attributes, there must be a
88 * message digest attribute amongst them which corresponds to the
89 * digest we just calculated.
90 */
91 if (sinfo->authattrs) {
92 u8 tag;
93
94 if (!sinfo->msgdigest) {
95 pr_warn("Sig %u: No messageDigest\n", sinfo->index);
96 ret = -EKEYREJECTED;
97 goto error;
98 }
99
100 if (sinfo->msgdigest_len != sig->digest_size) {
101 pr_debug("Sig %u: Invalid digest size (%u)\n",
102 sinfo->index, sinfo->msgdigest_len);
103 ret = -EBADMSG;
104 goto error;
105 }
106
107 if (memcmp(sig->digest, sinfo->msgdigest,
108 sinfo->msgdigest_len) != 0) {
109 pr_debug("Sig %u: Message digest doesn't match\n",
110 sinfo->index);
111 ret = -EKEYREJECTED;
112 goto error;
113 }
114
115 /* We then calculate anew, using the authenticated attributes
116 * as the contents of the digest instead. Note that we need to
117 * convert the attributes from a CONT.0 into a SET before we
118 * hash it.
119 */
120 memset(sig->digest, 0, sig->digest_size);
121
122 ret = crypto_shash_init(desc);
123 if (ret < 0)
124 goto error;
125 tag = ASN1_CONS_BIT | ASN1_SET;
126 ret = crypto_shash_update(desc, &tag, 1);
127 if (ret < 0)
128 goto error;
129 ret = crypto_shash_finup(desc, sinfo->authattrs,
130 sinfo->authattrs_len, sig->digest);
131 if (ret < 0)
132 goto error;
133 pr_devel("AADigest = [%*ph]\n", 8, sig->digest);
134 }
135
136error:
137 kfree(desc);
138error_no_desc:
139 crypto_free_shash(tfm);
140 kleave(" = %d", ret);
141 return ret;
142}
143
144int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
145 enum hash_algo *hash_algo)
146{
147 struct pkcs7_signed_info *sinfo = pkcs7->signed_infos;
148 int i, ret;
149
150 /*
151 * This function doesn't support messages with more than one signature.
152 */
153 if (sinfo == NULL || sinfo->next != NULL)
154 return -EBADMSG;
155
156 ret = pkcs7_digest(pkcs7, sinfo);
157 if (ret)
158 return ret;
159
160 *buf = sinfo->sig->digest;
161 *len = sinfo->sig->digest_size;
162
163 for (i = 0; i < HASH_ALGO__LAST; i++)
164 if (!strcmp(hash_algo_name[i], sinfo->sig->hash_algo)) {
165 *hash_algo = i;
166 break;
167 }
168
169 return 0;
170}
171#endif /* !__UBOOT__ */
172
173/*
174 * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
175 * uses the issuer's name and the issuing certificate serial number for
176 * matching purposes. These must match the certificate issuer's name (not
177 * subject's name) and the certificate serial number [RFC 2315 6.7].
178 */
179static int pkcs7_find_key(struct pkcs7_message *pkcs7,
180 struct pkcs7_signed_info *sinfo)
181{
182 struct x509_certificate *x509;
183 unsigned certix = 1;
184
185 kenter("%u", sinfo->index);
186
187 for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
188 /* I'm _assuming_ that the generator of the PKCS#7 message will
189 * encode the fields from the X.509 cert in the same way in the
190 * PKCS#7 message - but I can't be 100% sure of that. It's
191 * possible this will need element-by-element comparison.
192 */
193 if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0]))
194 continue;
195 pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
196 sinfo->index, certix);
197
198 if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) {
199 pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
200 sinfo->index);
201 continue;
202 }
203
204 sinfo->signer = x509;
205 return 0;
206 }
207
208 /* The relevant X.509 cert isn't found here, but it might be found in
209 * the trust keyring.
210 */
211 pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
212 sinfo->index,
213 sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data);
214 return 0;
215}
216
217/*
218 * Verify the internal certificate chain as best we can.
219 */
220static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
221 struct pkcs7_signed_info *sinfo)
222{
223 struct public_key_signature *sig;
224 struct x509_certificate *x509 = sinfo->signer, *p;
225 struct asymmetric_key_id *auth;
226 int ret;
227
228 kenter("");
229
230 for (p = pkcs7->certs; p; p = p->next)
231 p->seen = false;
232
233 for (;;) {
234 pr_debug("verify %s: %*phN\n",
235 x509->subject,
236 x509->raw_serial_size, x509->raw_serial);
237 x509->seen = true;
238
239 if (x509->blacklisted) {
240 /* If this cert is blacklisted, then mark everything
241 * that depends on this as blacklisted too.
242 */
243 sinfo->blacklisted = true;
244 for (p = sinfo->signer; p != x509; p = p->signer)
245 p->blacklisted = true;
246 pr_debug("- blacklisted\n");
247 return 0;
248 }
249
250 if (x509->unsupported_key)
251 goto unsupported_crypto_in_x509;
252
253 pr_debug("- issuer %s\n", x509->issuer);
254 sig = x509->sig;
255 if (sig->auth_ids[0])
256 pr_debug("- authkeyid.id %*phN\n",
257 sig->auth_ids[0]->len, sig->auth_ids[0]->data);
258 if (sig->auth_ids[1])
259 pr_debug("- authkeyid.skid %*phN\n",
260 sig->auth_ids[1]->len, sig->auth_ids[1]->data);
261
262 if (x509->self_signed) {
263 /* If there's no authority certificate specified, then
264 * the certificate must be self-signed and is the root
265 * of the chain. Likewise if the cert is its own
266 * authority.
267 */
268 if (x509->unsupported_sig)
269 goto unsupported_crypto_in_x509;
270 x509->signer = x509;
271 pr_debug("- self-signed\n");
272 return 0;
273 }
274
275 /* Look through the X.509 certificates in the PKCS#7 message's
276 * list to see if the next one is there.
277 */
278 auth = sig->auth_ids[0];
279 if (auth) {
280 pr_debug("- want %*phN\n", auth->len, auth->data);
281 for (p = pkcs7->certs; p; p = p->next) {
282 pr_debug("- cmp [%u] %*phN\n",
283 p->index, p->id->len, p->id->data);
284 if (asymmetric_key_id_same(p->id, auth))
285 goto found_issuer_check_skid;
286 }
287 } else if (sig->auth_ids[1]) {
288 auth = sig->auth_ids[1];
289 pr_debug("- want %*phN\n", auth->len, auth->data);
290 for (p = pkcs7->certs; p; p = p->next) {
291 if (!p->skid)
292 continue;
293 pr_debug("- cmp [%u] %*phN\n",
294 p->index, p->skid->len, p->skid->data);
295 if (asymmetric_key_id_same(p->skid, auth))
296 goto found_issuer;
297 }
298 }
299
300 /* We didn't find the root of this chain */
301 pr_debug("- top\n");
302 return 0;
303
304 found_issuer_check_skid:
305 /* We matched issuer + serialNumber, but if there's an
306 * authKeyId.keyId, that must match the CA subjKeyId also.
307 */
308 if (sig->auth_ids[1] &&
309 !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {
310 pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
311 sinfo->index, x509->index, p->index);
312 return -EKEYREJECTED;
313 }
314 found_issuer:
315 pr_debug("- subject %s\n", p->subject);
316 if (p->seen) {
317 pr_warn("Sig %u: X.509 chain contains loop\n",
318 sinfo->index);
319 return 0;
320 }
321 ret = public_key_verify_signature(p->pub, x509->sig);
322 if (ret < 0)
323 return ret;
324 x509->signer = p;
325 if (x509 == p) {
326 pr_debug("- self-signed\n");
327 return 0;
328 }
329 x509 = p;
330#ifndef __UBOOT__
331 might_sleep();
332#endif
333 }
334
335unsupported_crypto_in_x509:
336 /* Just prune the certificate chain at this point if we lack some
337 * crypto module to go further. Note, however, we don't want to set
338 * sinfo->unsupported_crypto as the signed info block may still be
339 * validatable against an X.509 cert lower in the chain that we have a
340 * trusted copy of.
341 */
342 return 0;
343}
344
345/*
346 * Verify one signed information block from a PKCS#7 message.
347 */
348#ifndef __UBOOT__
349static
350#endif
351int pkcs7_verify_one(struct pkcs7_message *pkcs7,
352 struct pkcs7_signed_info *sinfo)
353{
354 int ret;
355
356 kenter(",%u", sinfo->index);
357
358 /* First of all, digest the data in the PKCS#7 message and the
359 * signed information block
360 */
361 ret = pkcs7_digest(pkcs7, sinfo);
362 if (ret < 0)
363 return ret;
364
365 /* Find the key for the signature if there is one */
366 ret = pkcs7_find_key(pkcs7, sinfo);
367 if (ret < 0)
368 return ret;
369
370 if (!sinfo->signer)
371 return 0;
372
373 pr_devel("Using X.509[%u] for sig %u\n",
374 sinfo->signer->index, sinfo->index);
375
376 /* Check that the PKCS#7 signing time is valid according to the X.509
377 * certificate. We can't, however, check against the system clock
378 * since that may not have been set yet and may be wrong.
379 */
380 if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
381 if (sinfo->signing_time < sinfo->signer->valid_from ||
382 sinfo->signing_time > sinfo->signer->valid_to) {
383 pr_warn("Message signed outside of X.509 validity window\n");
384 return -EKEYREJECTED;
385 }
386 }
387
388 /* Verify the PKCS#7 binary against the key */
389 ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);
390 if (ret < 0)
391 return ret;
392
393 pr_devel("Verified signature %u\n", sinfo->index);
394
395 /* Verify the internal certificate chain */
396 return pkcs7_verify_sig_chain(pkcs7, sinfo);
397}
398
399#ifndef __UBOOT__
400/**
401 * pkcs7_verify - Verify a PKCS#7 message
402 * @pkcs7: The PKCS#7 message to be verified
403 * @usage: The use to which the key is being put
404 *
405 * Verify a PKCS#7 message is internally consistent - that is, the data digest
406 * matches the digest in the AuthAttrs and any signature in the message or one
407 * of the X.509 certificates it carries that matches another X.509 cert in the
408 * message can be verified.
409 *
410 * This does not look to match the contents of the PKCS#7 message against any
411 * external public keys.
412 *
413 * Returns, in order of descending priority:
414 *
415 * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
416 * odds with the specified usage, or:
417 *
418 * (*) -EKEYREJECTED if a signature failed to match for which we found an
419 * appropriate X.509 certificate, or:
420 *
421 * (*) -EBADMSG if some part of the message was invalid, or:
422 *
423 * (*) 0 if a signature chain passed verification, or:
424 *
425 * (*) -EKEYREJECTED if a blacklisted key was encountered, or:
426 *
427 * (*) -ENOPKG if none of the signature chains are verifiable because suitable
428 * crypto modules couldn't be found.
429 */
430int pkcs7_verify(struct pkcs7_message *pkcs7,
431 enum key_being_used_for usage)
432{
433 struct pkcs7_signed_info *sinfo;
434 int actual_ret = -ENOPKG;
435 int ret;
436
437 kenter("");
438
439 switch (usage) {
440 case VERIFYING_MODULE_SIGNATURE:
441 if (pkcs7->data_type != OID_data) {
442 pr_warn("Invalid module sig (not pkcs7-data)\n");
443 return -EKEYREJECTED;
444 }
445 if (pkcs7->have_authattrs) {
446 pr_warn("Invalid module sig (has authattrs)\n");
447 return -EKEYREJECTED;
448 }
449 break;
450 case VERIFYING_FIRMWARE_SIGNATURE:
451 if (pkcs7->data_type != OID_data) {
452 pr_warn("Invalid firmware sig (not pkcs7-data)\n");
453 return -EKEYREJECTED;
454 }
455 if (!pkcs7->have_authattrs) {
456 pr_warn("Invalid firmware sig (missing authattrs)\n");
457 return -EKEYREJECTED;
458 }
459 break;
460 case VERIFYING_KEXEC_PE_SIGNATURE:
461 if (pkcs7->data_type != OID_msIndirectData) {
462 pr_warn("Invalid kexec sig (not Authenticode)\n");
463 return -EKEYREJECTED;
464 }
465 /* Authattr presence checked in parser */
466 break;
467 case VERIFYING_UNSPECIFIED_SIGNATURE:
468 if (pkcs7->data_type != OID_data) {
469 pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
470 return -EKEYREJECTED;
471 }
472 break;
473 default:
474 return -EINVAL;
475 }
476
477 for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
478 ret = pkcs7_verify_one(pkcs7, sinfo);
479 if (sinfo->blacklisted) {
480 if (actual_ret == -ENOPKG)
481 actual_ret = -EKEYREJECTED;
482 continue;
483 }
484 if (ret < 0) {
485 if (ret == -ENOPKG) {
486 sinfo->unsupported_crypto = true;
487 continue;
488 }
489 kleave(" = %d", ret);
490 return ret;
491 }
492 actual_ret = 0;
493 }
494
495 kleave(" = %d", actual_ret);
496 return actual_ret;
497}
498EXPORT_SYMBOL_GPL(pkcs7_verify);
499
500/**
501 * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
502 * @pkcs7: The PKCS#7 message
503 * @data: The data to be verified
504 * @datalen: The amount of data
505 *
506 * Supply the detached data needed to verify a PKCS#7 message. Note that no
507 * attempt to retain/pin the data is made. That is left to the caller. The
508 * data will not be modified by pkcs7_verify() and will not be freed when the
509 * PKCS#7 message is freed.
510 *
511 * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
512 */
513int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
514 const void *data, size_t datalen)
515{
516 if (pkcs7->data) {
517 pr_debug("Data already supplied\n");
518 return -EINVAL;
519 }
520 pkcs7->data = data;
521 pkcs7->data_len = datalen;
522 return 0;
523}
524#endif /* __UBOOT__ */