blob: 79ed077ae7ddafb78021b67a062982f9679e533b [file] [log] [blame]
AKASHI Takahiro1faaca42020-04-14 11:51:39 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2018 Patrick Wildt <patrick@blueri.se>
4 * Copyright (c) 2019 Linaro Limited, Author: AKASHI Takahiro
5 */
6
7#include <common.h>
8#include <charset.h>
9#include <efi_loader.h>
Heinrich Schuchardtcaeb73b2021-09-09 08:25:08 +020010#include <efi_variable.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090011#include <image.h>
12#include <hexdump.h>
13#include <malloc.h>
AKASHI Takahiro14afd062020-07-21 19:35:22 +090014#include <crypto/pkcs7.h>
AKASHI Takahiro6ec67672020-04-21 09:38:17 +090015#include <crypto/pkcs7_parser.h>
AKASHI Takahiro14afd062020-07-21 19:35:22 +090016#include <crypto/public_key.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090017#include <linux/compat.h>
18#include <linux/oid_registry.h>
Masahisa Kojima915e4272021-05-14 09:53:36 +090019#include <u-boot/hash-checksum.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090020#include <u-boot/rsa.h>
21#include <u-boot/sha256.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090022
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090023const efi_guid_t efi_guid_sha256 = EFI_CERT_SHA256_GUID;
24const efi_guid_t efi_guid_cert_rsa2048 = EFI_CERT_RSA2048_GUID;
25const efi_guid_t efi_guid_cert_x509 = EFI_CERT_X509_GUID;
26const efi_guid_t efi_guid_cert_x509_sha256 = EFI_CERT_X509_SHA256_GUID;
AKASHI Takahiro5ed9ada2020-05-29 15:41:18 +090027const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090028
Sughosh Ganuea350082020-12-30 19:27:07 +053029static u8 pkcs7_hdr[] = {
30 /* SEQUENCE */
31 0x30, 0x82, 0x05, 0xc7,
32 /* OID: pkcs7-signedData */
33 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02,
34 /* Context Structured? */
35 0xa0, 0x82, 0x05, 0xb8,
36};
37
38/**
39 * efi_parse_pkcs7_header - parse a signature in payload
40 * @buf: Pointer to payload's value
41 * @buflen: Length of @buf
42 * @tmpbuf: Pointer to temporary buffer
43 *
44 * Parse a signature embedded in payload's value and instantiate
45 * a pkcs7_message structure. Since pkcs7_parse_message() accepts only
46 * pkcs7's signedData, some header needed be prepended for correctly
47 * parsing authentication data
48 * A temporary buffer will be allocated if needed, and it should be
49 * kept valid during the authentication because some data in the buffer
50 * will be referenced by efi_signature_verify().
51 *
52 * Return: Pointer to pkcs7_message structure on success, NULL on error
53 */
54struct pkcs7_message *efi_parse_pkcs7_header(const void *buf,
55 size_t buflen,
56 u8 **tmpbuf)
57{
58 u8 *ebuf;
59 size_t ebuflen, len;
60 struct pkcs7_message *msg;
61
62 /*
63 * This is the best assumption to check if the binary is
64 * already in a form of pkcs7's signedData.
65 */
66 if (buflen > sizeof(pkcs7_hdr) &&
67 !memcmp(&((u8 *)buf)[4], &pkcs7_hdr[4], 11)) {
68 msg = pkcs7_parse_message(buf, buflen);
69 if (IS_ERR(msg))
70 return NULL;
71 return msg;
72 }
73
74 /*
75 * Otherwise, we should add a dummy prefix sequence for pkcs7
76 * message parser to be able to process.
77 * NOTE: EDK2 also uses similar hack in WrapPkcs7Data()
78 * in CryptoPkg/Library/BaseCryptLib/Pk/CryptPkcs7VerifyCommon.c
79 * TODO:
80 * The header should be composed in a more refined manner.
81 */
82 EFI_PRINT("Makeshift prefix added to authentication data\n");
83 ebuflen = sizeof(pkcs7_hdr) + buflen;
84 if (ebuflen <= 0x7f) {
85 EFI_PRINT("Data is too short\n");
86 return NULL;
87 }
88
89 ebuf = malloc(ebuflen);
90 if (!ebuf) {
91 EFI_PRINT("Out of memory\n");
92 return NULL;
93 }
94
95 memcpy(ebuf, pkcs7_hdr, sizeof(pkcs7_hdr));
96 memcpy(ebuf + sizeof(pkcs7_hdr), buf, buflen);
97 len = ebuflen - 4;
98 ebuf[2] = (len >> 8) & 0xff;
99 ebuf[3] = len & 0xff;
100 len = ebuflen - 0x13;
101 ebuf[0x11] = (len >> 8) & 0xff;
102 ebuf[0x12] = len & 0xff;
103
104 msg = pkcs7_parse_message(ebuf, ebuflen);
105
106 if (IS_ERR(msg)) {
107 free(ebuf);
108 return NULL;
109 }
110
111 *tmpbuf = ebuf;
112 return msg;
113}
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900114
115/**
116 * efi_hash_regions - calculate a hash value
AKASHI Takahiroddbfa612020-07-08 14:01:55 +0900117 * @regs: Array of regions
118 * @count: Number of regions
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900119 * @hash: Pointer to a pointer to buffer holding a hash value
120 * @size: Size of buffer to be returned
121 *
122 * Calculate a sha256 value of @regs and return a value in @hash.
123 *
124 * Return: true on success, false on error
125 */
AKASHI Takahiroddbfa612020-07-08 14:01:55 +0900126static bool efi_hash_regions(struct image_region *regs, int count,
127 void **hash, size_t *size)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900128{
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900129 if (!*hash) {
AKASHI Takahiroddbfa612020-07-08 14:01:55 +0900130 *hash = calloc(1, SHA256_SUM_LEN);
131 if (!*hash) {
132 EFI_PRINT("Out of memory\n");
133 return false;
134 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900135 }
AKASHI Takahiroddbfa612020-07-08 14:01:55 +0900136 if (size)
137 *size = SHA256_SUM_LEN;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900138
AKASHI Takahiroddbfa612020-07-08 14:01:55 +0900139 hash_calculate("sha256", regs, count, *hash);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900140#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900141 EFI_PRINT("hash calculated:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900142 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
143 *hash, SHA256_SUM_LEN, false);
144#endif
145
146 return true;
147}
148
149/**
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200150 * hash_algo_supported - check if the requested hash algorithm is supported
151 * @guid: guid of the algorithm
152 *
153 * Return: true if supported false otherwise
154 */
155static bool hash_algo_supported(const efi_guid_t guid)
156{
157 int i;
158 const efi_guid_t unsupported_hashes[] = {
159 EFI_CERT_SHA1_GUID,
160 EFI_CERT_SHA224_GUID,
161 EFI_CERT_SHA384_GUID,
162 EFI_CERT_SHA512_GUID,
163 };
164
165 for (i = 0; i < ARRAY_SIZE(unsupported_hashes); i++) {
166 if (!guidcmp(&unsupported_hashes[i], &guid))
167 return false;
168 }
169
170 return true;
171}
172
173/**
AKASHI Takahirode924072020-07-08 14:01:57 +0900174 * efi_signature_lookup_digest - search for an image's digest in sigdb
175 * @regs: List of regions to be authenticated
176 * @db: Signature database for trusted certificates
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200177 * @dbx Caller needs to set this to true if he is searching dbx
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900178 *
AKASHI Takahirode924072020-07-08 14:01:57 +0900179 * A message digest of image pointed to by @regs is calculated and
180 * its hash value is compared to entries in signature database pointed
181 * to by @db.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900182 *
AKASHI Takahirode924072020-07-08 14:01:57 +0900183 * Return: true if found, false if not
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900184 */
AKASHI Takahirode924072020-07-08 14:01:57 +0900185bool efi_signature_lookup_digest(struct efi_image_regions *regs,
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200186 struct efi_signature_store *db,
187 bool dbx)
188
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900189{
AKASHI Takahirode924072020-07-08 14:01:57 +0900190 struct efi_signature_store *siglist;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900191 struct efi_sig_data *sig_data;
AKASHI Takahirode924072020-07-08 14:01:57 +0900192 void *hash = NULL;
193 size_t size = 0;
194 bool found = false;
Ilias Apalodimas81adfff2022-01-29 00:20:32 +0200195 bool hash_done = false;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900196
AKASHI Takahirode924072020-07-08 14:01:57 +0900197 EFI_PRINT("%s: Enter, %p, %p\n", __func__, regs, db);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900198
AKASHI Takahirode924072020-07-08 14:01:57 +0900199 if (!regs || !db || !db->sig_data_list)
200 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900201
AKASHI Takahirode924072020-07-08 14:01:57 +0900202 for (siglist = db; siglist; siglist = siglist->next) {
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200203 /*
204 * if the hash algorithm is unsupported and we get an entry in
205 * dbx reject the image
206 */
207 if (dbx && !hash_algo_supported(siglist->sig_type)) {
208 found = true;
209 continue;
210 };
211 /*
212 * Only support sha256 for now, that's what
213 * hash-to-efi-sig-list produces
214 */
215 if (guidcmp(&siglist->sig_type, &efi_guid_sha256))
216 continue;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900217
Ilias Apalodimas81adfff2022-01-29 00:20:32 +0200218 if (!hash_done &&
219 !efi_hash_regions(regs->reg, regs->num, &hash, &size)) {
AKASHI Takahirode924072020-07-08 14:01:57 +0900220 EFI_PRINT("Digesting an image failed\n");
221 break;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900222 }
Ilias Apalodimas81adfff2022-01-29 00:20:32 +0200223 hash_done = true;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900224
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900225 for (sig_data = siglist->sig_data_list; sig_data;
226 sig_data = sig_data->next) {
227#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900228 EFI_PRINT("Msg digest in database:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900229 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
230 sig_data->data, sig_data->size, false);
231#endif
AKASHI Takahirode924072020-07-08 14:01:57 +0900232 if (sig_data->size == size &&
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900233 !memcmp(sig_data->data, hash, size)) {
AKASHI Takahirode924072020-07-08 14:01:57 +0900234 found = true;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900235 free(hash);
236 goto out;
237 }
238 }
AKASHI Takahirode924072020-07-08 14:01:57 +0900239
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900240 free(hash);
AKASHI Takahirode924072020-07-08 14:01:57 +0900241 hash = NULL;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900242 }
243
AKASHI Takahirode924072020-07-08 14:01:57 +0900244out:
245 EFI_PRINT("%s: Exit, found: %d\n", __func__, found);
246 return found;
247}
248
249/**
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900250 * efi_lookup_certificate - find a certificate within db
251 * @msg: Signature
252 * @db: Signature database
AKASHI Takahirode924072020-07-08 14:01:57 +0900253 *
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900254 * Search signature database pointed to by @db and find a certificate
255 * pointed to by @cert.
AKASHI Takahirode924072020-07-08 14:01:57 +0900256 *
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900257 * Return: true if found, false otherwise.
AKASHI Takahirode924072020-07-08 14:01:57 +0900258 */
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900259static bool efi_lookup_certificate(struct x509_certificate *cert,
260 struct efi_signature_store *db)
AKASHI Takahirode924072020-07-08 14:01:57 +0900261{
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900262 struct efi_signature_store *siglist;
AKASHI Takahirode924072020-07-08 14:01:57 +0900263 struct efi_sig_data *sig_data;
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900264 struct image_region reg[1];
265 void *hash = NULL, *hash_tmp = NULL;
266 size_t size = 0;
267 bool found = false;
AKASHI Takahirode924072020-07-08 14:01:57 +0900268
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900269 EFI_PRINT("%s: Enter, %p, %p\n", __func__, cert, db);
AKASHI Takahirode924072020-07-08 14:01:57 +0900270
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900271 if (!cert || !db || !db->sig_data_list)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900272 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900273
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900274 /*
275 * TODO: identify a certificate using sha256 digest
276 * Is there any better way?
277 */
278 /* calculate hash of TBSCertificate */
279 reg[0].data = cert->tbs;
280 reg[0].size = cert->tbs_size;
281 if (!efi_hash_regions(reg, 1, &hash, &size))
282 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900283
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900284 EFI_PRINT("%s: searching for %s\n", __func__, cert->subject);
285 for (siglist = db; siglist; siglist = siglist->next) {
286 /* only with x509 certificate */
287 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509))
288 continue;
289
290 for (sig_data = siglist->sig_data_list; sig_data;
291 sig_data = sig_data->next) {
292 struct x509_certificate *cert_tmp;
293
294 cert_tmp = x509_cert_parse(sig_data->data,
295 sig_data->size);
296 if (IS_ERR_OR_NULL(cert_tmp))
297 continue;
298
AKASHI Takahiro16411802020-08-14 14:39:23 +0900299 EFI_PRINT("%s: against %s\n", __func__,
300 cert_tmp->subject);
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900301 reg[0].data = cert_tmp->tbs;
302 reg[0].size = cert_tmp->tbs_size;
303 if (!efi_hash_regions(reg, 1, &hash_tmp, NULL))
304 goto out;
305
306 x509_free_certificate(cert_tmp);
307
308 if (!memcmp(hash, hash_tmp, size)) {
309 found = true;
310 goto out;
311 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900312 }
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900313 }
314out:
315 free(hash);
316 free(hash_tmp);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900317
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900318 EFI_PRINT("%s: Exit, found: %d\n", __func__, found);
319 return found;
320}
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900321
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900322/**
323 * efi_verify_certificate - verify certificate's signature with database
324 * @signer: Certificate
325 * @db: Signature database
326 * @root: Certificate to verify @signer
327 *
328 * Determine if certificate pointed to by @signer may be verified
329 * by one of certificates in signature database pointed to by @db.
330 *
331 * Return: true if certificate is verified, false otherwise.
332 */
333static bool efi_verify_certificate(struct x509_certificate *signer,
334 struct efi_signature_store *db,
335 struct x509_certificate **root)
336{
337 struct efi_signature_store *siglist;
338 struct efi_sig_data *sig_data;
339 struct x509_certificate *cert;
340 bool verified = false;
341 int ret;
342
343 EFI_PRINT("%s: Enter, %p, %p\n", __func__, signer, db);
344
345 if (!signer || !db || !db->sig_data_list)
346 goto out;
347
348 for (siglist = db; siglist; siglist = siglist->next) {
349 /* only with x509 certificate */
350 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509))
351 continue;
352
353 for (sig_data = siglist->sig_data_list; sig_data;
354 sig_data = sig_data->next) {
355 cert = x509_cert_parse(sig_data->data, sig_data->size);
356 if (IS_ERR_OR_NULL(cert)) {
357 EFI_PRINT("Cannot parse x509 certificate\n");
358 continue;
359 }
360
361 ret = public_key_verify_signature(cert->pub,
362 signer->sig);
363 if (!ret) {
364 verified = true;
365 if (root)
366 *root = cert;
367 else
368 x509_free_certificate(cert);
369 goto out;
370 }
371 x509_free_certificate(cert);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900372 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900373 }
374
375out:
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900376 EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900377 return verified;
378}
379
380/**
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900381 * efi_signature_check_revocation - check revocation with dbx
382 * @sinfo: Signer's info
383 * @cert: x509 certificate
384 * @dbx: Revocation signature database
385 *
386 * Search revocation signature database pointed to by @dbx and find
387 * an entry matching to certificate pointed to by @cert.
388 *
389 * While this entry contains revocation time, we don't support timestamp
390 * protocol at this time and any image will be unconditionally revoked
391 * when this match occurs.
392 *
AKASHI Takahiro16411802020-08-14 14:39:23 +0900393 * Return: true if check passed (not found), false otherwise.
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900394 */
395static bool efi_signature_check_revocation(struct pkcs7_signed_info *sinfo,
396 struct x509_certificate *cert,
397 struct efi_signature_store *dbx)
398{
399 struct efi_signature_store *siglist;
400 struct efi_sig_data *sig_data;
401 struct image_region reg[1];
402 void *hash = NULL;
403 size_t size = 0;
404 time64_t revoc_time;
405 bool revoked = false;
406
407 EFI_PRINT("%s: Enter, %p, %p, %p\n", __func__, sinfo, cert, dbx);
408
409 if (!sinfo || !cert || !dbx || !dbx->sig_data_list)
410 goto out;
411
412 EFI_PRINT("Checking revocation against %s\n", cert->subject);
413 for (siglist = dbx; siglist; siglist = siglist->next) {
414 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509_sha256))
415 continue;
416
417 /* calculate hash of TBSCertificate */
418 reg[0].data = cert->tbs;
419 reg[0].size = cert->tbs_size;
420 if (!efi_hash_regions(reg, 1, &hash, &size))
421 goto out;
422
423 for (sig_data = siglist->sig_data_list; sig_data;
424 sig_data = sig_data->next) {
425 /*
426 * struct efi_cert_x509_sha256 {
427 * u8 tbs_hash[256/8];
428 * time64_t revocation_time;
429 * };
430 */
431#ifdef DEBUG
432 if (sig_data->size >= size) {
433 EFI_PRINT("hash in db:\n");
434 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
435 16, 1,
436 sig_data->data, size, false);
437 }
438#endif
439 if ((sig_data->size < size + sizeof(time64_t)) ||
440 memcmp(sig_data->data, hash, size))
441 continue;
442
443 memcpy(&revoc_time, sig_data->data + size,
444 sizeof(revoc_time));
445 EFI_PRINT("revocation time: 0x%llx\n", revoc_time);
446 /*
447 * TODO: compare signing timestamp in sinfo
448 * with revocation time
449 */
450
451 revoked = true;
452 free(hash);
453 goto out;
454 }
455 free(hash);
456 hash = NULL;
457 }
458out:
459 EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked);
460 return !revoked;
461}
462
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900463/*
464 * efi_signature_verify - verify signatures with db and dbx
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900465 * @regs: List of regions to be authenticated
466 * @msg: Signature
467 * @db: Signature database for trusted certificates
468 * @dbx: Revocation signature database
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900469 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900470 * All the signature pointed to by @msg against image pointed to by @regs
471 * will be verified by signature database pointed to by @db and @dbx.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900472 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900473 * Return: true if verification for all signatures passed, false otherwise
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900474 */
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900475bool efi_signature_verify(struct efi_image_regions *regs,
476 struct pkcs7_message *msg,
477 struct efi_signature_store *db,
478 struct efi_signature_store *dbx)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900479{
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900480 struct pkcs7_signed_info *sinfo;
481 struct x509_certificate *signer, *root;
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900482 bool verified = false;
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900483 int ret;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900484
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900485 EFI_PRINT("%s: Enter, %p, %p, %p, %p\n", __func__, regs, msg, db, dbx);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900486
AKASHI Takahirode924072020-07-08 14:01:57 +0900487 if (!regs || !msg || !db || !db->sig_data_list)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900488 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900489
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900490 for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) {
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900491 EFI_PRINT("Signed Info: digest algo: %s, pkey algo: %s\n",
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900492 sinfo->sig->hash_algo, sinfo->sig->pkey_algo);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900493
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900494 /*
495 * only for authenticated variable.
496 *
497 * If this function is called for image,
498 * hash calculation will be done in
499 * pkcs7_verify_one().
500 */
501 if (!msg->data &&
502 !efi_hash_regions(regs->reg, regs->num,
503 (void **)&sinfo->sig->digest, NULL)) {
504 EFI_PRINT("Digesting an image failed\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900505 goto out;
506 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900507
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900508 EFI_PRINT("Verifying certificate chain\n");
509 signer = NULL;
510 ret = pkcs7_verify_one(msg, sinfo, &signer);
511 if (ret == -ENOPKG)
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900512 continue;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900513
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900514 if (ret < 0 || !signer)
515 goto out;
516
517 if (sinfo->blacklisted)
518 goto out;
519
520 EFI_PRINT("Verifying last certificate in chain\n");
Ilias Apalodimas1ee9c482022-02-14 11:14:22 +0200521 if (efi_lookup_certificate(signer, db))
522 if (efi_signature_check_revocation(sinfo, signer, dbx))
523 break;
524 if (!signer->self_signed &&
525 efi_verify_certificate(signer, db, &root)) {
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900526 bool check;
527
528 check = efi_signature_check_revocation(sinfo, root,
529 dbx);
530 x509_free_certificate(root);
531 if (check)
AKASHI Takahiro16411802020-08-14 14:39:23 +0900532 break;
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900533 }
534
535 EFI_PRINT("Certificate chain didn't reach trusted CA\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900536 }
AKASHI Takahiro16411802020-08-14 14:39:23 +0900537 if (sinfo)
538 verified = true;
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900539out:
540 EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified);
541 return verified;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900542}
543
544/**
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900545 * efi_signature_check_signers - check revocation against all signers with dbx
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900546 * @msg: Signature
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900547 * @dbx: Revocation signature database
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900548 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900549 * Determine if none of signers' certificates in @msg are revoked
550 * by signature database pointed to by @dbx.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900551 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900552 * Return: true if all signers passed, false otherwise.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900553 */
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900554bool efi_signature_check_signers(struct pkcs7_message *msg,
555 struct efi_signature_store *dbx)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900556{
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900557 struct pkcs7_signed_info *sinfo;
558 bool revoked = false;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900559
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900560 EFI_PRINT("%s: Enter, %p, %p\n", __func__, msg, dbx);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900561
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900562 if (!msg || !dbx)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900563 goto out;
564
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900565 for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) {
566 if (sinfo->signer &&
567 !efi_signature_check_revocation(sinfo, sinfo->signer,
568 dbx)) {
569 revoked = true;
570 break;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900571 }
572 }
573out:
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900574 EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked);
575 return !revoked;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900576}
577
578/**
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900579 * efi_sigstore_free - free signature store
580 * @sigstore: Pointer to signature store structure
581 *
582 * Feee all the memories held in signature store and itself,
583 * which were allocated by efi_sigstore_parse_sigdb().
584 */
585void efi_sigstore_free(struct efi_signature_store *sigstore)
586{
587 struct efi_signature_store *sigstore_next;
588 struct efi_sig_data *sig_data, *sig_data_next;
589
590 while (sigstore) {
591 sigstore_next = sigstore->next;
592
593 sig_data = sigstore->sig_data_list;
594 while (sig_data) {
595 sig_data_next = sig_data->next;
596 free(sig_data->data);
597 free(sig_data);
598 sig_data = sig_data_next;
599 }
600
601 free(sigstore);
602 sigstore = sigstore_next;
603 }
604}
605
606/**
607 * efi_sigstore_parse_siglist - parse a signature list
608 * @name: Pointer to signature list
609 *
610 * Parse signature list and instantiate a signature store structure.
611 * Signature database is a simple concatenation of one or more
612 * signature list(s).
613 *
614 * Return: Pointer to signature store on success, NULL on error
615 */
616static struct efi_signature_store *
617efi_sigstore_parse_siglist(struct efi_signature_list *esl)
618{
619 struct efi_signature_store *siglist = NULL;
620 struct efi_sig_data *sig_data, *sig_data_next;
621 struct efi_signature_data *esd;
622 size_t left;
623
624 /*
625 * UEFI specification defines certificate types:
626 * for non-signed images,
627 * EFI_CERT_SHA256_GUID
628 * EFI_CERT_RSA2048_GUID
629 * EFI_CERT_RSA2048_SHA256_GUID
630 * EFI_CERT_SHA1_GUID
631 * EFI_CERT_RSA2048_SHA_GUID
632 * EFI_CERT_SHA224_GUID
633 * EFI_CERT_SHA384_GUID
634 * EFI_CERT_SHA512_GUID
635 *
636 * for signed images,
637 * EFI_CERT_X509_GUID
638 * NOTE: Each certificate will normally be in a separate
639 * EFI_SIGNATURE_LIST as the size may vary depending on
640 * its algo's.
641 *
642 * for timestamp revocation of certificate,
643 * EFI_CERT_X509_SHA512_GUID
644 * EFI_CERT_X509_SHA256_GUID
645 * EFI_CERT_X509_SHA384_GUID
646 */
647
648 if (esl->signature_list_size
649 <= (sizeof(*esl) + esl->signature_header_size)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900650 EFI_PRINT("Siglist in wrong format\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900651 return NULL;
652 }
653
654 /* Create a head */
655 siglist = calloc(sizeof(*siglist), 1);
656 if (!siglist) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900657 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900658 goto err;
659 }
660 memcpy(&siglist->sig_type, &esl->signature_type, sizeof(efi_guid_t));
661
662 /* Go through the list */
663 sig_data_next = NULL;
664 left = esl->signature_list_size
665 - (sizeof(*esl) + esl->signature_header_size);
666 esd = (struct efi_signature_data *)
667 ((u8 *)esl + sizeof(*esl) + esl->signature_header_size);
668
AKASHI Takahiroa075aa32020-04-21 09:38:57 +0900669 while (left > 0) {
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900670 /* Signature must exist if there is remaining data. */
671 if (left < esl->signature_size) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900672 EFI_PRINT("Certificate is too small\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900673 goto err;
674 }
675
676 sig_data = calloc(esl->signature_size
677 - sizeof(esd->signature_owner), 1);
678 if (!sig_data) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900679 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900680 goto err;
681 }
682
683 /* Append signature data */
684 memcpy(&sig_data->owner, &esd->signature_owner,
685 sizeof(efi_guid_t));
686 sig_data->size = esl->signature_size
687 - sizeof(esd->signature_owner);
688 sig_data->data = malloc(sig_data->size);
689 if (!sig_data->data) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900690 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900691 goto err;
692 }
693 memcpy(sig_data->data, esd->signature_data, sig_data->size);
694
695 sig_data->next = sig_data_next;
696 sig_data_next = sig_data;
697
698 /* Next */
699 esd = (struct efi_signature_data *)
700 ((u8 *)esd + esl->signature_size);
701 left -= esl->signature_size;
702 }
703 siglist->sig_data_list = sig_data_next;
704
705 return siglist;
706
707err:
708 efi_sigstore_free(siglist);
709
710 return NULL;
711}
712
713/**
Sughosh Ganufa0faa62020-12-30 19:27:08 +0530714 * efi_sigstore_parse_sigdb - parse the signature list and populate
715 * the signature store
716 *
717 * @sig_list: Pointer to the signature list
718 * @size: Size of the signature list
719 *
720 * Parse the efi signature list and instantiate a signature store
721 * structure.
722 *
723 * Return: Pointer to signature store on success, NULL on error
724 */
725struct efi_signature_store *efi_build_signature_store(void *sig_list,
726 efi_uintn_t size)
727{
728 struct efi_signature_list *esl;
729 struct efi_signature_store *sigstore = NULL, *siglist;
730
731 esl = sig_list;
732 while (size > 0) {
733 /* List must exist if there is remaining data. */
734 if (size < sizeof(*esl)) {
735 EFI_PRINT("Signature list in wrong format\n");
736 goto err;
737 }
738
739 if (size < esl->signature_list_size) {
740 EFI_PRINT("Signature list in wrong format\n");
741 goto err;
742 }
743
744 /* Parse a single siglist. */
745 siglist = efi_sigstore_parse_siglist(esl);
746 if (!siglist) {
747 EFI_PRINT("Parsing of signature list of failed\n");
748 goto err;
749 }
750
751 /* Append siglist */
752 siglist->next = sigstore;
753 sigstore = siglist;
754
755 /* Next */
756 size -= esl->signature_list_size;
757 esl = (void *)esl + esl->signature_list_size;
758 }
759 free(sig_list);
760
761 return sigstore;
762
763err:
764 efi_sigstore_free(sigstore);
765 free(sig_list);
766
767 return NULL;
768}
769
770/**
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900771 * efi_sigstore_parse_sigdb - parse a signature database variable
772 * @name: Variable's name
773 *
774 * Read in a value of signature database variable pointed to by
775 * @name, parse it and instantiate a signature store structure.
776 *
777 * Return: Pointer to signature store on success, NULL on error
778 */
779struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name)
780{
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900781 const efi_guid_t *vendor;
782 void *db;
783 efi_uintn_t db_size;
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900784
Heinrich Schuchardtcaeb73b2021-09-09 08:25:08 +0200785 vendor = efi_auth_var_get_guid(name);
786 db = efi_get_var(name, vendor, &db_size);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900787 if (!db) {
Heinrich Schuchardtcaeb73b2021-09-09 08:25:08 +0200788 EFI_PRINT("variable, %ls, not found\n", name);
789 return calloc(sizeof(struct efi_signature_store), 1);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900790 }
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900791
Sughosh Ganufa0faa62020-12-30 19:27:08 +0530792 return efi_build_signature_store(db, db_size);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900793}