blob: 184eac8cddb700914304920fd43bb595b5d1faac [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
AKASHI Takahiro1faaca42020-04-14 11:51:39 +09007#include <charset.h>
8#include <efi_loader.h>
Heinrich Schuchardtcaeb73b2021-09-09 08:25:08 +02009#include <efi_variable.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090010#include <image.h>
11#include <hexdump.h>
12#include <malloc.h>
AKASHI Takahiro14afd062020-07-21 19:35:22 +090013#include <crypto/pkcs7.h>
AKASHI Takahiro6ec67672020-04-21 09:38:17 +090014#include <crypto/pkcs7_parser.h>
AKASHI Takahiro14afd062020-07-21 19:35:22 +090015#include <crypto/public_key.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090016#include <linux/compat.h>
17#include <linux/oid_registry.h>
Masahisa Kojima915e4272021-05-14 09:53:36 +090018#include <u-boot/hash-checksum.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090019#include <u-boot/rsa.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090020
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090021const efi_guid_t efi_guid_sha256 = EFI_CERT_SHA256_GUID;
22const efi_guid_t efi_guid_cert_rsa2048 = EFI_CERT_RSA2048_GUID;
23const efi_guid_t efi_guid_cert_x509 = EFI_CERT_X509_GUID;
24const efi_guid_t efi_guid_cert_x509_sha256 = EFI_CERT_X509_SHA256_GUID;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +030025const efi_guid_t efi_guid_cert_x509_sha384 = EFI_CERT_X509_SHA384_GUID;
26const efi_guid_t efi_guid_cert_x509_sha512 = EFI_CERT_X509_SHA512_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 Takahirof47b9b32022-07-05 14:48:12 +0900126bool efi_hash_regions(struct image_region *regs, int count,
127 void **hash, const char *hash_algo, int *len)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900128{
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300129 int ret, hash_len;
130
131 if (!hash_algo)
132 return false;
133
134 hash_len = algo_to_len(hash_algo);
135 if (!hash_len)
136 return false;
137
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900138 if (!*hash) {
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300139 *hash = calloc(1, hash_len);
AKASHI Takahiroddbfa612020-07-08 14:01:55 +0900140 if (!*hash) {
141 EFI_PRINT("Out of memory\n");
142 return false;
143 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900144 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900145
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300146 ret = hash_calculate(hash_algo, regs, count, *hash);
147 if (ret)
148 return false;
149
150 if (len)
151 *len = hash_len;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900152#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900153 EFI_PRINT("hash calculated:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900154 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300155 *hash, hash_len, false);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900156#endif
157
158 return true;
159}
160
161/**
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200162 * hash_algo_supported - check if the requested hash algorithm is supported
163 * @guid: guid of the algorithm
164 *
165 * Return: true if supported false otherwise
166 */
167static bool hash_algo_supported(const efi_guid_t guid)
168{
169 int i;
170 const efi_guid_t unsupported_hashes[] = {
171 EFI_CERT_SHA1_GUID,
172 EFI_CERT_SHA224_GUID,
173 EFI_CERT_SHA384_GUID,
174 EFI_CERT_SHA512_GUID,
175 };
176
177 for (i = 0; i < ARRAY_SIZE(unsupported_hashes); i++) {
178 if (!guidcmp(&unsupported_hashes[i], &guid))
179 return false;
180 }
181
182 return true;
183}
184
185/**
AKASHI Takahirode924072020-07-08 14:01:57 +0900186 * efi_signature_lookup_digest - search for an image's digest in sigdb
187 * @regs: List of regions to be authenticated
188 * @db: Signature database for trusted certificates
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200189 * @dbx Caller needs to set this to true if he is searching dbx
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900190 *
AKASHI Takahirode924072020-07-08 14:01:57 +0900191 * A message digest of image pointed to by @regs is calculated and
192 * its hash value is compared to entries in signature database pointed
193 * to by @db.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900194 *
AKASHI Takahirode924072020-07-08 14:01:57 +0900195 * Return: true if found, false if not
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900196 */
AKASHI Takahirode924072020-07-08 14:01:57 +0900197bool efi_signature_lookup_digest(struct efi_image_regions *regs,
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200198 struct efi_signature_store *db,
199 bool dbx)
200
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900201{
AKASHI Takahirode924072020-07-08 14:01:57 +0900202 struct efi_signature_store *siglist;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900203 struct efi_sig_data *sig_data;
AKASHI Takahirode924072020-07-08 14:01:57 +0900204 void *hash = NULL;
AKASHI Takahirode924072020-07-08 14:01:57 +0900205 bool found = false;
Ilias Apalodimas81adfff2022-01-29 00:20:32 +0200206 bool hash_done = false;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900207
AKASHI Takahirode924072020-07-08 14:01:57 +0900208 EFI_PRINT("%s: Enter, %p, %p\n", __func__, regs, db);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900209
AKASHI Takahirode924072020-07-08 14:01:57 +0900210 if (!regs || !db || !db->sig_data_list)
211 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900212
AKASHI Takahirode924072020-07-08 14:01:57 +0900213 for (siglist = db; siglist; siglist = siglist->next) {
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300214 int len = 0;
215 const char *hash_algo = NULL;
Ilias Apalodimas71eae982022-01-29 00:20:31 +0200216 /*
217 * if the hash algorithm is unsupported and we get an entry in
218 * dbx reject the image
219 */
220 if (dbx && !hash_algo_supported(siglist->sig_type)) {
221 found = true;
222 continue;
223 };
224 /*
225 * Only support sha256 for now, that's what
226 * hash-to-efi-sig-list produces
227 */
228 if (guidcmp(&siglist->sig_type, &efi_guid_sha256))
229 continue;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900230
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300231 hash_algo = guid_to_sha_str(&efi_guid_sha256);
232 /*
233 * We could check size and hash_algo but efi_hash_regions()
234 * will do that for us
235 */
Ilias Apalodimas81adfff2022-01-29 00:20:32 +0200236 if (!hash_done &&
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300237 !efi_hash_regions(regs->reg, regs->num, &hash, hash_algo,
238 &len)) {
AKASHI Takahirode924072020-07-08 14:01:57 +0900239 EFI_PRINT("Digesting an image failed\n");
240 break;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900241 }
Ilias Apalodimas81adfff2022-01-29 00:20:32 +0200242 hash_done = true;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900243
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900244 for (sig_data = siglist->sig_data_list; sig_data;
245 sig_data = sig_data->next) {
246#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900247 EFI_PRINT("Msg digest in database:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900248 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
249 sig_data->data, sig_data->size, false);
250#endif
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300251 if (sig_data->size == len &&
252 !memcmp(sig_data->data, hash, len)) {
AKASHI Takahirode924072020-07-08 14:01:57 +0900253 found = true;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900254 free(hash);
255 goto out;
256 }
257 }
AKASHI Takahirode924072020-07-08 14:01:57 +0900258
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900259 free(hash);
AKASHI Takahirode924072020-07-08 14:01:57 +0900260 hash = NULL;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900261 }
262
AKASHI Takahirode924072020-07-08 14:01:57 +0900263out:
264 EFI_PRINT("%s: Exit, found: %d\n", __func__, found);
265 return found;
266}
267
268/**
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900269 * efi_lookup_certificate - find a certificate within db
270 * @msg: Signature
271 * @db: Signature database
AKASHI Takahirode924072020-07-08 14:01:57 +0900272 *
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900273 * Search signature database pointed to by @db and find a certificate
274 * pointed to by @cert.
AKASHI Takahirode924072020-07-08 14:01:57 +0900275 *
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900276 * Return: true if found, false otherwise.
AKASHI Takahirode924072020-07-08 14:01:57 +0900277 */
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900278static bool efi_lookup_certificate(struct x509_certificate *cert,
279 struct efi_signature_store *db)
AKASHI Takahirode924072020-07-08 14:01:57 +0900280{
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900281 struct efi_signature_store *siglist;
AKASHI Takahirode924072020-07-08 14:01:57 +0900282 struct efi_sig_data *sig_data;
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900283 struct image_region reg[1];
284 void *hash = NULL, *hash_tmp = NULL;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300285 int len = 0;
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900286 bool found = false;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300287 const char *hash_algo = NULL;
AKASHI Takahirode924072020-07-08 14:01:57 +0900288
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900289 EFI_PRINT("%s: Enter, %p, %p\n", __func__, cert, db);
AKASHI Takahirode924072020-07-08 14:01:57 +0900290
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900291 if (!cert || !db || !db->sig_data_list)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900292 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900293
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900294 /*
295 * TODO: identify a certificate using sha256 digest
296 * Is there any better way?
297 */
298 /* calculate hash of TBSCertificate */
299 reg[0].data = cert->tbs;
300 reg[0].size = cert->tbs_size;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300301
302 /* We just need any sha256 algo to start the matching */
303 hash_algo = guid_to_sha_str(&efi_guid_sha256);
304 if (!efi_hash_regions(reg, 1, &hash, hash_algo, &len))
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900305 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900306
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900307 EFI_PRINT("%s: searching for %s\n", __func__, cert->subject);
308 for (siglist = db; siglist; siglist = siglist->next) {
309 /* only with x509 certificate */
310 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509))
311 continue;
312
313 for (sig_data = siglist->sig_data_list; sig_data;
314 sig_data = sig_data->next) {
315 struct x509_certificate *cert_tmp;
316
317 cert_tmp = x509_cert_parse(sig_data->data,
318 sig_data->size);
319 if (IS_ERR_OR_NULL(cert_tmp))
320 continue;
321
AKASHI Takahiro16411802020-08-14 14:39:23 +0900322 EFI_PRINT("%s: against %s\n", __func__,
323 cert_tmp->subject);
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900324 reg[0].data = cert_tmp->tbs;
325 reg[0].size = cert_tmp->tbs_size;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300326 if (!efi_hash_regions(reg, 1, &hash_tmp, hash_algo,
327 NULL))
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900328 goto out;
329
330 x509_free_certificate(cert_tmp);
331
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300332 if (!memcmp(hash, hash_tmp, len)) {
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900333 found = true;
334 goto out;
335 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900336 }
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900337 }
338out:
339 free(hash);
340 free(hash_tmp);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900341
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900342 EFI_PRINT("%s: Exit, found: %d\n", __func__, found);
343 return found;
344}
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900345
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900346/**
347 * efi_verify_certificate - verify certificate's signature with database
348 * @signer: Certificate
349 * @db: Signature database
350 * @root: Certificate to verify @signer
351 *
352 * Determine if certificate pointed to by @signer may be verified
353 * by one of certificates in signature database pointed to by @db.
354 *
355 * Return: true if certificate is verified, false otherwise.
356 */
357static bool efi_verify_certificate(struct x509_certificate *signer,
358 struct efi_signature_store *db,
359 struct x509_certificate **root)
360{
361 struct efi_signature_store *siglist;
362 struct efi_sig_data *sig_data;
363 struct x509_certificate *cert;
364 bool verified = false;
365 int ret;
366
367 EFI_PRINT("%s: Enter, %p, %p\n", __func__, signer, db);
368
369 if (!signer || !db || !db->sig_data_list)
370 goto out;
371
372 for (siglist = db; siglist; siglist = siglist->next) {
373 /* only with x509 certificate */
374 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509))
375 continue;
376
377 for (sig_data = siglist->sig_data_list; sig_data;
378 sig_data = sig_data->next) {
379 cert = x509_cert_parse(sig_data->data, sig_data->size);
380 if (IS_ERR_OR_NULL(cert)) {
381 EFI_PRINT("Cannot parse x509 certificate\n");
382 continue;
383 }
384
385 ret = public_key_verify_signature(cert->pub,
386 signer->sig);
387 if (!ret) {
388 verified = true;
389 if (root)
390 *root = cert;
391 else
392 x509_free_certificate(cert);
393 goto out;
394 }
395 x509_free_certificate(cert);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900396 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900397 }
398
399out:
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900400 EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900401 return verified;
402}
403
404/**
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900405 * efi_signature_check_revocation - check revocation with dbx
406 * @sinfo: Signer's info
407 * @cert: x509 certificate
408 * @dbx: Revocation signature database
409 *
410 * Search revocation signature database pointed to by @dbx and find
411 * an entry matching to certificate pointed to by @cert.
412 *
413 * While this entry contains revocation time, we don't support timestamp
414 * protocol at this time and any image will be unconditionally revoked
415 * when this match occurs.
416 *
AKASHI Takahiro16411802020-08-14 14:39:23 +0900417 * Return: true if check passed (not found), false otherwise.
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900418 */
419static bool efi_signature_check_revocation(struct pkcs7_signed_info *sinfo,
420 struct x509_certificate *cert,
421 struct efi_signature_store *dbx)
422{
423 struct efi_signature_store *siglist;
424 struct efi_sig_data *sig_data;
425 struct image_region reg[1];
426 void *hash = NULL;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300427 int len = 0;
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900428 time64_t revoc_time;
429 bool revoked = false;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300430 const char *hash_algo = NULL;
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900431
432 EFI_PRINT("%s: Enter, %p, %p, %p\n", __func__, sinfo, cert, dbx);
433
434 if (!sinfo || !cert || !dbx || !dbx->sig_data_list)
435 goto out;
436
437 EFI_PRINT("Checking revocation against %s\n", cert->subject);
438 for (siglist = dbx; siglist; siglist = siglist->next) {
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300439 hash_algo = guid_to_sha_str(&siglist->sig_type);
440 if (!hash_algo)
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900441 continue;
442
443 /* calculate hash of TBSCertificate */
444 reg[0].data = cert->tbs;
445 reg[0].size = cert->tbs_size;
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300446 if (!efi_hash_regions(reg, 1, &hash, hash_algo, &len))
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900447 goto out;
448
449 for (sig_data = siglist->sig_data_list; sig_data;
450 sig_data = sig_data->next) {
451 /*
452 * struct efi_cert_x509_sha256 {
453 * u8 tbs_hash[256/8];
454 * time64_t revocation_time;
455 * };
456 */
457#ifdef DEBUG
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300458 if (sig_data->size >= len) {
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900459 EFI_PRINT("hash in db:\n");
460 print_hex_dump(" ", DUMP_PREFIX_OFFSET,
461 16, 1,
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300462 sig_data->data, len, false);
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900463 }
464#endif
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300465 if ((sig_data->size < len + sizeof(time64_t)) ||
466 memcmp(sig_data->data, hash, len))
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900467 continue;
468
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300469 memcpy(&revoc_time, sig_data->data + len,
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900470 sizeof(revoc_time));
471 EFI_PRINT("revocation time: 0x%llx\n", revoc_time);
472 /*
473 * TODO: compare signing timestamp in sinfo
474 * with revocation time
475 */
476
477 revoked = true;
478 free(hash);
479 goto out;
480 }
481 free(hash);
482 hash = NULL;
483 }
484out:
485 EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked);
486 return !revoked;
487}
488
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900489/*
490 * efi_signature_verify - verify signatures with db and dbx
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900491 * @regs: List of regions to be authenticated
492 * @msg: Signature
493 * @db: Signature database for trusted certificates
494 * @dbx: Revocation signature database
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900495 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900496 * All the signature pointed to by @msg against image pointed to by @regs
497 * will be verified by signature database pointed to by @db and @dbx.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900498 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900499 * Return: true if verification for all signatures passed, false otherwise
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900500 */
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900501bool efi_signature_verify(struct efi_image_regions *regs,
502 struct pkcs7_message *msg,
503 struct efi_signature_store *db,
504 struct efi_signature_store *dbx)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900505{
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900506 struct pkcs7_signed_info *sinfo;
507 struct x509_certificate *signer, *root;
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900508 bool verified = false;
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900509 int ret;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900510
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900511 EFI_PRINT("%s: Enter, %p, %p, %p, %p\n", __func__, regs, msg, db, dbx);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900512
AKASHI Takahirode924072020-07-08 14:01:57 +0900513 if (!regs || !msg || !db || !db->sig_data_list)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900514 goto out;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900515
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900516 for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) {
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900517 EFI_PRINT("Signed Info: digest algo: %s, pkey algo: %s\n",
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900518 sinfo->sig->hash_algo, sinfo->sig->pkey_algo);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900519
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900520 /*
521 * only for authenticated variable.
522 *
523 * If this function is called for image,
524 * hash calculation will be done in
525 * pkcs7_verify_one().
526 */
527 if (!msg->data &&
528 !efi_hash_regions(regs->reg, regs->num,
Ilias Apalodimas34db9b12022-05-06 15:36:00 +0300529 (void **)&sinfo->sig->digest,
530 guid_to_sha_str(&efi_guid_sha256),
531 NULL)) {
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900532 EFI_PRINT("Digesting an image failed\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900533 goto out;
534 }
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900535
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900536 EFI_PRINT("Verifying certificate chain\n");
537 signer = NULL;
538 ret = pkcs7_verify_one(msg, sinfo, &signer);
539 if (ret == -ENOPKG)
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900540 continue;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900541
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900542 if (ret < 0 || !signer)
543 goto out;
544
545 if (sinfo->blacklisted)
546 goto out;
547
548 EFI_PRINT("Verifying last certificate in chain\n");
Ilias Apalodimas1ee9c482022-02-14 11:14:22 +0200549 if (efi_lookup_certificate(signer, db))
550 if (efi_signature_check_revocation(sinfo, signer, dbx))
551 break;
552 if (!signer->self_signed &&
553 efi_verify_certificate(signer, db, &root)) {
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900554 bool check;
555
556 check = efi_signature_check_revocation(sinfo, root,
557 dbx);
558 x509_free_certificate(root);
559 if (check)
AKASHI Takahiro16411802020-08-14 14:39:23 +0900560 break;
AKASHI Takahiro14afd062020-07-21 19:35:22 +0900561 }
562
563 EFI_PRINT("Certificate chain didn't reach trusted CA\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900564 }
AKASHI Takahiro16411802020-08-14 14:39:23 +0900565 if (sinfo)
566 verified = true;
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900567out:
568 EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified);
569 return verified;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900570}
571
572/**
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900573 * efi_signature_check_signers - check revocation against all signers with dbx
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900574 * @msg: Signature
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900575 * @dbx: Revocation signature database
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900576 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900577 * Determine if none of signers' certificates in @msg are revoked
578 * by signature database pointed to by @dbx.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900579 *
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900580 * Return: true if all signers passed, false otherwise.
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900581 */
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900582bool efi_signature_check_signers(struct pkcs7_message *msg,
583 struct efi_signature_store *dbx)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900584{
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900585 struct pkcs7_signed_info *sinfo;
586 bool revoked = false;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900587
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900588 EFI_PRINT("%s: Enter, %p, %p\n", __func__, msg, dbx);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900589
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900590 if (!msg || !dbx)
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900591 goto out;
592
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900593 for (sinfo = msg->signed_infos; sinfo; sinfo = sinfo->next) {
594 if (sinfo->signer &&
595 !efi_signature_check_revocation(sinfo, sinfo->signer,
596 dbx)) {
597 revoked = true;
598 break;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900599 }
600 }
601out:
AKASHI Takahiro4154b642020-07-08 14:01:56 +0900602 EFI_PRINT("%s: Exit, revoked: %d\n", __func__, revoked);
603 return !revoked;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900604}
605
606/**
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900607 * efi_sigstore_free - free signature store
608 * @sigstore: Pointer to signature store structure
609 *
610 * Feee all the memories held in signature store and itself,
611 * which were allocated by efi_sigstore_parse_sigdb().
612 */
613void efi_sigstore_free(struct efi_signature_store *sigstore)
614{
615 struct efi_signature_store *sigstore_next;
616 struct efi_sig_data *sig_data, *sig_data_next;
617
618 while (sigstore) {
619 sigstore_next = sigstore->next;
620
621 sig_data = sigstore->sig_data_list;
622 while (sig_data) {
623 sig_data_next = sig_data->next;
624 free(sig_data->data);
625 free(sig_data);
626 sig_data = sig_data_next;
627 }
628
629 free(sigstore);
630 sigstore = sigstore_next;
631 }
632}
633
634/**
635 * efi_sigstore_parse_siglist - parse a signature list
636 * @name: Pointer to signature list
637 *
638 * Parse signature list and instantiate a signature store structure.
639 * Signature database is a simple concatenation of one or more
640 * signature list(s).
641 *
642 * Return: Pointer to signature store on success, NULL on error
643 */
644static struct efi_signature_store *
645efi_sigstore_parse_siglist(struct efi_signature_list *esl)
646{
647 struct efi_signature_store *siglist = NULL;
648 struct efi_sig_data *sig_data, *sig_data_next;
649 struct efi_signature_data *esd;
650 size_t left;
651
652 /*
653 * UEFI specification defines certificate types:
654 * for non-signed images,
655 * EFI_CERT_SHA256_GUID
656 * EFI_CERT_RSA2048_GUID
657 * EFI_CERT_RSA2048_SHA256_GUID
658 * EFI_CERT_SHA1_GUID
659 * EFI_CERT_RSA2048_SHA_GUID
660 * EFI_CERT_SHA224_GUID
661 * EFI_CERT_SHA384_GUID
662 * EFI_CERT_SHA512_GUID
663 *
664 * for signed images,
665 * EFI_CERT_X509_GUID
666 * NOTE: Each certificate will normally be in a separate
667 * EFI_SIGNATURE_LIST as the size may vary depending on
668 * its algo's.
669 *
670 * for timestamp revocation of certificate,
671 * EFI_CERT_X509_SHA512_GUID
672 * EFI_CERT_X509_SHA256_GUID
673 * EFI_CERT_X509_SHA384_GUID
674 */
675
676 if (esl->signature_list_size
677 <= (sizeof(*esl) + esl->signature_header_size)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900678 EFI_PRINT("Siglist in wrong format\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900679 return NULL;
680 }
681
682 /* Create a head */
683 siglist = calloc(sizeof(*siglist), 1);
684 if (!siglist) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900685 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900686 goto err;
687 }
688 memcpy(&siglist->sig_type, &esl->signature_type, sizeof(efi_guid_t));
689
690 /* Go through the list */
691 sig_data_next = NULL;
692 left = esl->signature_list_size
693 - (sizeof(*esl) + esl->signature_header_size);
694 esd = (struct efi_signature_data *)
695 ((u8 *)esl + sizeof(*esl) + esl->signature_header_size);
696
AKASHI Takahiroa075aa32020-04-21 09:38:57 +0900697 while (left > 0) {
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900698 /* Signature must exist if there is remaining data. */
699 if (left < esl->signature_size) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900700 EFI_PRINT("Certificate is too small\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900701 goto err;
702 }
703
704 sig_data = calloc(esl->signature_size
705 - sizeof(esd->signature_owner), 1);
706 if (!sig_data) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900707 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900708 goto err;
709 }
710
711 /* Append signature data */
712 memcpy(&sig_data->owner, &esd->signature_owner,
713 sizeof(efi_guid_t));
714 sig_data->size = esl->signature_size
715 - sizeof(esd->signature_owner);
716 sig_data->data = malloc(sig_data->size);
717 if (!sig_data->data) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900718 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900719 goto err;
720 }
721 memcpy(sig_data->data, esd->signature_data, sig_data->size);
722
723 sig_data->next = sig_data_next;
724 sig_data_next = sig_data;
725
726 /* Next */
727 esd = (struct efi_signature_data *)
728 ((u8 *)esd + esl->signature_size);
729 left -= esl->signature_size;
730 }
731 siglist->sig_data_list = sig_data_next;
732
733 return siglist;
734
735err:
736 efi_sigstore_free(siglist);
737
738 return NULL;
739}
740
741/**
Sughosh Ganufa0faa62020-12-30 19:27:08 +0530742 * efi_sigstore_parse_sigdb - parse the signature list and populate
743 * the signature store
744 *
745 * @sig_list: Pointer to the signature list
746 * @size: Size of the signature list
747 *
748 * Parse the efi signature list and instantiate a signature store
749 * structure.
750 *
751 * Return: Pointer to signature store on success, NULL on error
752 */
753struct efi_signature_store *efi_build_signature_store(void *sig_list,
754 efi_uintn_t size)
755{
756 struct efi_signature_list *esl;
757 struct efi_signature_store *sigstore = NULL, *siglist;
758
759 esl = sig_list;
760 while (size > 0) {
761 /* List must exist if there is remaining data. */
762 if (size < sizeof(*esl)) {
763 EFI_PRINT("Signature list in wrong format\n");
764 goto err;
765 }
766
767 if (size < esl->signature_list_size) {
768 EFI_PRINT("Signature list in wrong format\n");
769 goto err;
770 }
771
772 /* Parse a single siglist. */
773 siglist = efi_sigstore_parse_siglist(esl);
774 if (!siglist) {
775 EFI_PRINT("Parsing of signature list of failed\n");
776 goto err;
777 }
778
779 /* Append siglist */
780 siglist->next = sigstore;
781 sigstore = siglist;
782
783 /* Next */
784 size -= esl->signature_list_size;
785 esl = (void *)esl + esl->signature_list_size;
786 }
787 free(sig_list);
788
789 return sigstore;
790
791err:
792 efi_sigstore_free(sigstore);
793 free(sig_list);
794
795 return NULL;
796}
797
798/**
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900799 * efi_sigstore_parse_sigdb - parse a signature database variable
800 * @name: Variable's name
801 *
802 * Read in a value of signature database variable pointed to by
803 * @name, parse it and instantiate a signature store structure.
804 *
805 * Return: Pointer to signature store on success, NULL on error
806 */
807struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name)
808{
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900809 const efi_guid_t *vendor;
810 void *db;
811 efi_uintn_t db_size;
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900812
Heinrich Schuchardtcaeb73b2021-09-09 08:25:08 +0200813 vendor = efi_auth_var_get_guid(name);
814 db = efi_get_var(name, vendor, &db_size);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900815 if (!db) {
Heinrich Schuchardtcaeb73b2021-09-09 08:25:08 +0200816 EFI_PRINT("variable, %ls, not found\n", name);
817 return calloc(sizeof(struct efi_signature_store), 1);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900818 }
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900819
Sughosh Ganufa0faa62020-12-30 19:27:08 +0530820 return efi_build_signature_store(db, db_size);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900821}