blob: 3a634d7b578cacf33ebc73fbe137852523eb750e [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>
10#include <image.h>
11#include <hexdump.h>
12#include <malloc.h>
AKASHI Takahiro6ec67672020-04-21 09:38:17 +090013#include <crypto/pkcs7_parser.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090014#include <linux/compat.h>
15#include <linux/oid_registry.h>
16#include <u-boot/rsa.h>
17#include <u-boot/sha256.h>
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090018
19const efi_guid_t efi_guid_image_security_database =
20 EFI_IMAGE_SECURITY_DATABASE_GUID;
21const 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;
AKASHI Takahiro5ed9ada2020-05-29 15:41:18 +090025const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090026
27#ifdef CONFIG_EFI_SECURE_BOOT
28
29/**
30 * efi_hash_regions - calculate a hash value
31 * @regs: List of regions
32 * @hash: Pointer to a pointer to buffer holding a hash value
33 * @size: Size of buffer to be returned
34 *
35 * Calculate a sha256 value of @regs and return a value in @hash.
36 *
37 * Return: true on success, false on error
38 */
39static bool efi_hash_regions(struct efi_image_regions *regs, void **hash,
40 size_t *size)
41{
42 *size = 0;
43 *hash = calloc(1, SHA256_SUM_LEN);
44 if (!*hash) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +090045 EFI_PRINT("Out of memory\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090046 return false;
47 }
48 *size = SHA256_SUM_LEN;
49
50 hash_calculate("sha256", regs->reg, regs->num, *hash);
51#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +090052 EFI_PRINT("hash calculated:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090053 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
54 *hash, SHA256_SUM_LEN, false);
55#endif
56
57 return true;
58}
59
60/**
61 * efi_hash_msg_content - calculate a hash value of contentInfo
62 * @msg: Signature
63 * @hash: Pointer to a pointer to buffer holding a hash value
64 * @size: Size of buffer to be returned
65 *
66 * Calculate a sha256 value of contentInfo in @msg and return a value in @hash.
67 *
68 * Return: true on success, false on error
69 */
70static bool efi_hash_msg_content(struct pkcs7_message *msg, void **hash,
71 size_t *size)
72{
73 struct image_region regtmp;
74
75 *size = 0;
76 *hash = calloc(1, SHA256_SUM_LEN);
77 if (!*hash) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +090078 EFI_PRINT("Out of memory\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090079 free(msg);
80 return false;
81 }
82 *size = SHA256_SUM_LEN;
83
84 regtmp.data = msg->data;
85 regtmp.size = msg->data_len;
86
87 hash_calculate("sha256", &regtmp, 1, *hash);
88#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +090089 EFI_PRINT("hash calculated based on contentInfo:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +090090 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
91 *hash, SHA256_SUM_LEN, false);
92#endif
93
94 return true;
95}
96
97/**
98 * efi_signature_verify - verify a signature with a certificate
99 * @regs: List of regions to be authenticated
100 * @signed_info: Pointer to PKCS7's signed_info
101 * @cert: x509 certificate
102 *
103 * Signature pointed to by @signed_info against image pointed to by @regs
104 * is verified by a certificate pointed to by @cert.
105 * @signed_info holds a signature, including a message digest which is to be
106 * compared with a hash value calculated from @regs.
107 *
108 * Return: true if signature is verified, false if not
109 */
110static bool efi_signature_verify(struct efi_image_regions *regs,
111 struct pkcs7_message *msg,
112 struct pkcs7_signed_info *ps_info,
113 struct x509_certificate *cert)
114{
115 struct image_sign_info info;
116 struct image_region regtmp[2];
117 void *hash;
118 size_t size;
119 char c;
120 bool verified;
121
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900122 EFI_PRINT("%s: Enter, %p, %p, %p(issuer: %s, subject: %s)\n", __func__,
123 regs, ps_info, cert, cert->issuer, cert->subject);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900124
125 verified = false;
126
127 memset(&info, '\0', sizeof(info));
128 info.padding = image_get_padding_algo("pkcs-1.5");
129 /*
130 * Note: image_get_[checksum|crypto]_algo takes an string
131 * argument like "<checksum>,<crypto>"
132 * TODO: support other hash algorithms
133 */
134 if (!strcmp(ps_info->sig->hash_algo, "sha1")) {
135 info.checksum = image_get_checksum_algo("sha1,rsa2048");
136 info.name = "sha1,rsa2048";
137 } else if (!strcmp(ps_info->sig->hash_algo, "sha256")) {
138 info.checksum = image_get_checksum_algo("sha256,rsa2048");
139 info.name = "sha256,rsa2048";
140 } else {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900141 EFI_PRINT("unknown msg digest algo: %s\n",
142 ps_info->sig->hash_algo);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900143 goto out;
144 }
145 info.crypto = image_get_crypto_algo(info.name);
146
147 info.key = cert->pub->key;
148 info.keylen = cert->pub->keylen;
149
150 /* verify signature */
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900151 EFI_PRINT("%s: crypto: %s, signature len:%x\n", __func__,
152 info.name, ps_info->sig->s_size);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900153 if (ps_info->aa_set & (1UL << sinfo_has_message_digest)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900154 EFI_PRINT("%s: RSA verify authentication attribute\n",
155 __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900156 /*
157 * NOTE: This path will be executed only for
158 * PE image authentication
159 */
160
161 /* check if hash matches digest first */
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900162 EFI_PRINT("checking msg digest first, len:0x%x\n",
163 ps_info->msgdigest_len);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900164
165#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900166 EFI_PRINT("hash in database:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900167 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
168 ps_info->msgdigest, ps_info->msgdigest_len,
169 false);
170#endif
171 /* against contentInfo first */
172 if ((msg->data && efi_hash_msg_content(msg, &hash, &size)) ||
173 /* for signed image */
174 efi_hash_regions(regs, &hash, &size)) {
175 /* for authenticated variable */
176 if (ps_info->msgdigest_len != size ||
177 memcmp(hash, ps_info->msgdigest, size)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900178 EFI_PRINT("Digest doesn't match\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900179 free(hash);
180 goto out;
181 }
182
183 free(hash);
184 } else {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900185 EFI_PRINT("Digesting image failed\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900186 goto out;
187 }
188
189 /* against digest */
190 c = 0x31;
191 regtmp[0].data = &c;
192 regtmp[0].size = 1;
193 regtmp[1].data = ps_info->authattrs;
194 regtmp[1].size = ps_info->authattrs_len;
195
196 if (!rsa_verify(&info, regtmp, 2,
197 ps_info->sig->s, ps_info->sig->s_size))
198 verified = true;
199 } else {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900200 EFI_PRINT("%s: RSA verify content data\n", __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900201 /* against all data */
202 if (!rsa_verify(&info, regs->reg, regs->num,
203 ps_info->sig->s, ps_info->sig->s_size))
204 verified = true;
205 }
206
207out:
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900208 EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900209 return verified;
210}
211
212/**
213 * efi_signature_verify_with_list - verify a signature with signature list
214 * @regs: List of regions to be authenticated
215 * @msg: Signature
216 * @signed_info: Pointer to PKCS7's signed_info
217 * @siglist: Signature list for certificates
218 * @valid_cert: x509 certificate that verifies this signature
219 *
220 * Signature pointed to by @signed_info against image pointed to by @regs
221 * is verified by signature list pointed to by @siglist.
222 * Signature database is a simple concatenation of one or more
223 * signature list(s).
224 *
225 * Return: true if signature is verified, false if not
226 */
227static
228bool efi_signature_verify_with_list(struct efi_image_regions *regs,
229 struct pkcs7_message *msg,
230 struct pkcs7_signed_info *signed_info,
231 struct efi_signature_store *siglist,
232 struct x509_certificate **valid_cert)
233{
234 struct x509_certificate *cert;
235 struct efi_sig_data *sig_data;
236 bool verified = false;
237
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900238 EFI_PRINT("%s: Enter, %p, %p, %p, %p\n", __func__,
239 regs, signed_info, siglist, valid_cert);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900240
241 if (!signed_info) {
242 void *hash;
243 size_t size;
244
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900245 EFI_PRINT("%s: unsigned image\n", __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900246 /*
247 * verify based on calculated hash value
248 * TODO: support other hash algorithms
249 */
250 if (guidcmp(&siglist->sig_type, &efi_guid_sha256)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900251 EFI_PRINT("Digest algorithm is not supported: %pUl\n",
252 &siglist->sig_type);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900253 goto out;
254 }
255
256 if (!efi_hash_regions(regs, &hash, &size)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900257 EFI_PRINT("Digesting unsigned image failed\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900258 goto out;
259 }
260
261 /* go through the list */
262 for (sig_data = siglist->sig_data_list; sig_data;
263 sig_data = sig_data->next) {
264#ifdef DEBUG
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900265 EFI_PRINT("Msg digest in database:\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900266 print_hex_dump(" ", DUMP_PREFIX_OFFSET, 16, 1,
267 sig_data->data, sig_data->size, false);
268#endif
269 if ((sig_data->size == size) &&
270 !memcmp(sig_data->data, hash, size)) {
271 verified = true;
272 free(hash);
273 goto out;
274 }
275 }
276 free(hash);
277 goto out;
278 }
279
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900280 EFI_PRINT("%s: signed image\n", __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900281 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900282 EFI_PRINT("Signature type is not supported: %pUl\n",
283 &siglist->sig_type);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900284 goto out;
285 }
286
287 /* go through the list */
288 for (sig_data = siglist->sig_data_list; sig_data;
289 sig_data = sig_data->next) {
290 /* TODO: support owner check based on policy */
291
292 cert = x509_cert_parse(sig_data->data, sig_data->size);
293 if (IS_ERR(cert)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900294 EFI_PRINT("Parsing x509 certificate failed\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900295 goto out;
296 }
297
298 verified = efi_signature_verify(regs, msg, signed_info, cert);
299
300 if (verified) {
301 if (valid_cert)
302 *valid_cert = cert;
303 else
304 x509_free_certificate(cert);
305 break;
306 }
307 x509_free_certificate(cert);
308 }
309
310out:
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900311 EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900312 return verified;
313}
314
315/**
316 * efi_signature_verify_with_sigdb - verify a signature with db
317 * @regs: List of regions to be authenticated
318 * @msg: Signature
319 * @db: Signature database for trusted certificates
320 * @cert: x509 certificate that verifies this signature
321 *
322 * Signature pointed to by @msg against image pointed to by @regs
323 * is verified by signature database pointed to by @db.
324 *
325 * Return: true if signature is verified, false if not
326 */
327bool efi_signature_verify_with_sigdb(struct efi_image_regions *regs,
328 struct pkcs7_message *msg,
329 struct efi_signature_store *db,
330 struct x509_certificate **cert)
331{
332 struct pkcs7_signed_info *info;
333 struct efi_signature_store *siglist;
334 bool verified = false;
335
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900336 EFI_PRINT("%s: Enter, %p, %p, %p, %p\n", __func__, regs, msg, db, cert);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900337
338 if (!db)
339 goto out;
340
341 if (!db->sig_data_list)
342 goto out;
343
344 /* for unsigned image */
345 if (!msg) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900346 EFI_PRINT("%s: Verify unsigned image with db\n", __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900347 for (siglist = db; siglist; siglist = siglist->next)
348 if (efi_signature_verify_with_list(regs, NULL, NULL,
349 siglist, cert)) {
350 verified = true;
351 goto out;
352 }
353
354 goto out;
355 }
356
357 /* for signed image or variable */
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900358 EFI_PRINT("%s: Verify signed image with db\n", __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900359 for (info = msg->signed_infos; info; info = info->next) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900360 EFI_PRINT("Signed Info: digest algo: %s, pkey algo: %s\n",
361 info->sig->hash_algo, info->sig->pkey_algo);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900362
363 for (siglist = db; siglist; siglist = siglist->next) {
364 if (efi_signature_verify_with_list(regs, msg, info,
365 siglist, cert)) {
366 verified = true;
367 goto out;
368 }
369 }
370 }
371
372out:
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900373 EFI_PRINT("%s: Exit, verified: %d\n", __func__, verified);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900374 return verified;
375}
376
377/**
378 * efi_search_siglist - search signature list for a certificate
379 * @cert: x509 certificate
380 * @siglist: Signature list
381 * @revoc_time: Pointer to buffer for revocation time
382 *
383 * Search signature list pointed to by @siglist and find a certificate
384 * pointed to by @cert.
385 * If found, revocation time that is specified in signature database is
386 * returned in @revoc_time.
387 *
388 * Return: true if certificate is found, false if not
389 */
390static bool efi_search_siglist(struct x509_certificate *cert,
391 struct efi_signature_store *siglist,
392 time64_t *revoc_time)
393{
394 struct image_region reg[1];
395 void *hash = NULL, *msg = NULL;
396 struct efi_sig_data *sig_data;
397 bool found = false;
398
399 /* can be null */
400 if (!siglist->sig_data_list)
401 return false;
402
403 if (guidcmp(&siglist->sig_type, &efi_guid_cert_x509_sha256)) {
404 /* TODO: other hash algos */
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900405 EFI_PRINT("Certificate's digest type is not supported: %pUl\n",
406 &siglist->sig_type);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900407 goto out;
408 }
409
410 /* calculate hash of TBSCertificate */
411 msg = calloc(1, SHA256_SUM_LEN);
412 if (!msg) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900413 EFI_PRINT("Out of memory\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900414 goto out;
415 }
416
417 hash = calloc(1, SHA256_SUM_LEN);
418 if (!hash) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900419 EFI_PRINT("Out of memory\n");
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900420 goto out;
421 }
422
423 reg[0].data = cert->tbs;
424 reg[0].size = cert->tbs_size;
425 hash_calculate("sha256", reg, 1, msg);
426
427 /* go through signature list */
428 for (sig_data = siglist->sig_data_list; sig_data;
429 sig_data = sig_data->next) {
430 /*
431 * struct efi_cert_x509_sha256 {
432 * u8 tbs_hash[256/8];
433 * time64_t revocation_time;
434 * };
435 */
436 if ((sig_data->size == SHA256_SUM_LEN) &&
437 !memcmp(sig_data->data, hash, SHA256_SUM_LEN)) {
438 memcpy(revoc_time, sig_data->data + SHA256_SUM_LEN,
439 sizeof(*revoc_time));
440 found = true;
441 goto out;
442 }
443 }
444
445out:
446 free(hash);
447 free(msg);
448
449 return found;
450}
451
452/**
453 * efi_signature_verify_cert - verify a certificate with dbx
454 * @cert: x509 certificate
455 * @dbx: Signature database
456 *
457 * Search signature database pointed to by @dbx and find a certificate
458 * pointed to by @cert.
459 * This function is expected to be used against "dbx".
460 *
461 * Return: true if a certificate is not rejected, false otherwise.
462 */
463bool efi_signature_verify_cert(struct x509_certificate *cert,
464 struct efi_signature_store *dbx)
465{
466 struct efi_signature_store *siglist;
467 time64_t revoc_time;
468 bool found = false;
469
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900470 EFI_PRINT("%s: Enter, %p, %p\n", __func__, dbx, cert);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900471
472 if (!cert)
473 return false;
474
475 for (siglist = dbx; siglist; siglist = siglist->next) {
476 if (efi_search_siglist(cert, siglist, &revoc_time)) {
477 /* TODO */
478 /* compare signing time with revocation time */
479
480 found = true;
481 break;
482 }
483 }
484
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900485 EFI_PRINT("%s: Exit, verified: %d\n", __func__, !found);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900486 return !found;
487}
488
489/**
490 * efi_signature_verify_signers - verify signers' certificates with dbx
491 * @msg: Signature
492 * @dbx: Signature database
493 *
494 * Determine if any of signers' certificates in @msg may be verified
495 * by any of certificates in signature database pointed to by @dbx.
496 * This function is expected to be used against "dbx".
497 *
498 * Return: true if none of certificates is rejected, false otherwise.
499 */
500bool efi_signature_verify_signers(struct pkcs7_message *msg,
501 struct efi_signature_store *dbx)
502{
503 struct pkcs7_signed_info *info;
504 bool found = false;
505
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900506 EFI_PRINT("%s: Enter, %p, %p\n", __func__, msg, dbx);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900507
508 if (!msg)
509 goto out;
510
511 for (info = msg->signed_infos; info; info = info->next) {
512 if (info->signer &&
513 !efi_signature_verify_cert(info->signer, dbx)) {
514 found = true;
515 goto out;
516 }
517 }
518out:
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900519 EFI_PRINT("%s: Exit, verified: %d\n", __func__, !found);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900520 return !found;
521}
522
523/**
524 * efi_image_region_add - add an entry of region
525 * @regs: Pointer to array of regions
526 * @start: Start address of region
527 * @end: End address of region
528 * @nocheck: flag against overlapped regions
529 *
530 * Take one entry of region [@start, @end] and append it to the list
531 * pointed to by @regs. If @nocheck is false, overlapping among entries
532 * will be checked first.
533 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +0200534 * Return: status code
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900535 */
536efi_status_t efi_image_region_add(struct efi_image_regions *regs,
537 const void *start, const void *end,
538 int nocheck)
539{
540 struct image_region *reg;
541 int i, j;
542
543 if (regs->num >= regs->max) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900544 EFI_PRINT("%s: no more room for regions\n", __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900545 return EFI_OUT_OF_RESOURCES;
546 }
547
548 if (end < start)
549 return EFI_INVALID_PARAMETER;
550
551 for (i = 0; i < regs->num; i++) {
552 reg = &regs->reg[i];
553 if (nocheck)
554 continue;
555
556 if (start > reg->data + reg->size)
557 continue;
558
559 if ((start >= reg->data && start < reg->data + reg->size) ||
560 (end > reg->data && end < reg->data + reg->size)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900561 EFI_PRINT("%s: new region already part of another\n",
562 __func__);
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900563 return EFI_INVALID_PARAMETER;
564 }
565
566 if (start < reg->data && end < reg->data + reg->size) {
567 for (j = regs->num - 1; j >= i; j--)
568 memcpy(&regs->reg[j], &regs->reg[j + 1],
569 sizeof(*reg));
570 break;
571 }
572 }
573
574 reg = &regs->reg[i];
575 reg->data = start;
576 reg->size = end - start;
577 regs->num++;
578
579 return EFI_SUCCESS;
580}
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900581
582/**
583 * efi_sigstore_free - free signature store
584 * @sigstore: Pointer to signature store structure
585 *
586 * Feee all the memories held in signature store and itself,
587 * which were allocated by efi_sigstore_parse_sigdb().
588 */
589void efi_sigstore_free(struct efi_signature_store *sigstore)
590{
591 struct efi_signature_store *sigstore_next;
592 struct efi_sig_data *sig_data, *sig_data_next;
593
594 while (sigstore) {
595 sigstore_next = sigstore->next;
596
597 sig_data = sigstore->sig_data_list;
598 while (sig_data) {
599 sig_data_next = sig_data->next;
600 free(sig_data->data);
601 free(sig_data);
602 sig_data = sig_data_next;
603 }
604
605 free(sigstore);
606 sigstore = sigstore_next;
607 }
608}
609
610/**
611 * efi_sigstore_parse_siglist - parse a signature list
612 * @name: Pointer to signature list
613 *
614 * Parse signature list and instantiate a signature store structure.
615 * Signature database is a simple concatenation of one or more
616 * signature list(s).
617 *
618 * Return: Pointer to signature store on success, NULL on error
619 */
620static struct efi_signature_store *
621efi_sigstore_parse_siglist(struct efi_signature_list *esl)
622{
623 struct efi_signature_store *siglist = NULL;
624 struct efi_sig_data *sig_data, *sig_data_next;
625 struct efi_signature_data *esd;
626 size_t left;
627
628 /*
629 * UEFI specification defines certificate types:
630 * for non-signed images,
631 * EFI_CERT_SHA256_GUID
632 * EFI_CERT_RSA2048_GUID
633 * EFI_CERT_RSA2048_SHA256_GUID
634 * EFI_CERT_SHA1_GUID
635 * EFI_CERT_RSA2048_SHA_GUID
636 * EFI_CERT_SHA224_GUID
637 * EFI_CERT_SHA384_GUID
638 * EFI_CERT_SHA512_GUID
639 *
640 * for signed images,
641 * EFI_CERT_X509_GUID
642 * NOTE: Each certificate will normally be in a separate
643 * EFI_SIGNATURE_LIST as the size may vary depending on
644 * its algo's.
645 *
646 * for timestamp revocation of certificate,
647 * EFI_CERT_X509_SHA512_GUID
648 * EFI_CERT_X509_SHA256_GUID
649 * EFI_CERT_X509_SHA384_GUID
650 */
651
652 if (esl->signature_list_size
653 <= (sizeof(*esl) + esl->signature_header_size)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900654 EFI_PRINT("Siglist in wrong format\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900655 return NULL;
656 }
657
658 /* Create a head */
659 siglist = calloc(sizeof(*siglist), 1);
660 if (!siglist) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900661 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900662 goto err;
663 }
664 memcpy(&siglist->sig_type, &esl->signature_type, sizeof(efi_guid_t));
665
666 /* Go through the list */
667 sig_data_next = NULL;
668 left = esl->signature_list_size
669 - (sizeof(*esl) + esl->signature_header_size);
670 esd = (struct efi_signature_data *)
671 ((u8 *)esl + sizeof(*esl) + esl->signature_header_size);
672
AKASHI Takahiroa075aa32020-04-21 09:38:57 +0900673 while (left > 0) {
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900674 /* Signature must exist if there is remaining data. */
675 if (left < esl->signature_size) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900676 EFI_PRINT("Certificate is too small\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900677 goto err;
678 }
679
680 sig_data = calloc(esl->signature_size
681 - sizeof(esd->signature_owner), 1);
682 if (!sig_data) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900683 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900684 goto err;
685 }
686
687 /* Append signature data */
688 memcpy(&sig_data->owner, &esd->signature_owner,
689 sizeof(efi_guid_t));
690 sig_data->size = esl->signature_size
691 - sizeof(esd->signature_owner);
692 sig_data->data = malloc(sig_data->size);
693 if (!sig_data->data) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900694 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900695 goto err;
696 }
697 memcpy(sig_data->data, esd->signature_data, sig_data->size);
698
699 sig_data->next = sig_data_next;
700 sig_data_next = sig_data;
701
702 /* Next */
703 esd = (struct efi_signature_data *)
704 ((u8 *)esd + esl->signature_size);
705 left -= esl->signature_size;
706 }
707 siglist->sig_data_list = sig_data_next;
708
709 return siglist;
710
711err:
712 efi_sigstore_free(siglist);
713
714 return NULL;
715}
716
717/**
718 * efi_sigstore_parse_sigdb - parse a signature database variable
719 * @name: Variable's name
720 *
721 * Read in a value of signature database variable pointed to by
722 * @name, parse it and instantiate a signature store structure.
723 *
724 * Return: Pointer to signature store on success, NULL on error
725 */
726struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name)
727{
728 struct efi_signature_store *sigstore = NULL, *siglist;
729 struct efi_signature_list *esl;
730 const efi_guid_t *vendor;
731 void *db;
732 efi_uintn_t db_size;
733 efi_status_t ret;
734
735 if (!u16_strcmp(name, L"PK") || !u16_strcmp(name, L"KEK")) {
736 vendor = &efi_global_variable_guid;
737 } else if (!u16_strcmp(name, L"db") || !u16_strcmp(name, L"dbx")) {
738 vendor = &efi_guid_image_security_database;
739 } else {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900740 EFI_PRINT("unknown signature database, %ls\n", name);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900741 return NULL;
742 }
743
744 /* retrieve variable data */
745 db_size = 0;
746 ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, NULL));
747 if (ret == EFI_NOT_FOUND) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900748 EFI_PRINT("variable, %ls, not found\n", name);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900749 sigstore = calloc(sizeof(*sigstore), 1);
750 return sigstore;
751 } else if (ret != EFI_BUFFER_TOO_SMALL) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900752 EFI_PRINT("Getting variable, %ls, failed\n", name);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900753 return NULL;
754 }
755
756 db = malloc(db_size);
757 if (!db) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900758 EFI_PRINT("Out of memory\n");
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900759 return NULL;
760 }
761
762 ret = EFI_CALL(efi_get_variable(name, vendor, NULL, &db_size, db));
763 if (ret != EFI_SUCCESS) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900764 EFI_PRINT("Getting variable, %ls, failed\n", name);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900765 goto err;
766 }
767
768 /* Parse siglist list */
769 esl = db;
770 while (db_size > 0) {
771 /* List must exist if there is remaining data. */
772 if (db_size < sizeof(*esl)) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900773 EFI_PRINT("variable, %ls, in wrong format\n", name);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900774 goto err;
775 }
776
777 if (db_size < esl->signature_list_size) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900778 EFI_PRINT("variable, %ls, in wrong format\n", name);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900779 goto err;
780 }
781
782 /* Parse a single siglist. */
783 siglist = efi_sigstore_parse_siglist(esl);
784 if (!siglist) {
AKASHI Takahiro10a9fa82020-06-09 14:09:33 +0900785 EFI_PRINT("Parsing signature list of %ls failed\n",
786 name);
AKASHI Takahirod2cefc82020-04-14 11:51:40 +0900787 goto err;
788 }
789
790 /* Append siglist */
791 siglist->next = sigstore;
792 sigstore = siglist;
793
794 /* Next */
795 db_size -= esl->signature_list_size;
796 esl = (void *)esl + esl->signature_list_size;
797 }
798 free(db);
799
800 return sigstore;
801
802err:
803 efi_sigstore_free(sigstore);
804 free(db);
805
806 return NULL;
807}
AKASHI Takahiro1faaca42020-04-14 11:51:39 +0900808#endif /* CONFIG_EFI_SECURE_BOOT */