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