1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 IBM Corporation
6 * - loads keys and hashes stored and controlled by the firmware.
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/cred.h>
11 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <asm/secure_boot.h>
14 #include <asm/secvar.h>
15 #include "keyring_handler.h"
16 #include "../integrity.h"
18 #define extract_esl(db, data, size, offset) \
19 do { db = data + offset; size = size - offset; } while (0)
22 * Get a certificate list blob from the named secure variable.
25 * - a pointer to a kmalloc'd buffer containing the cert list on success
26 * - NULL if the key does not exist
27 * - an ERR_PTR on error
29 static __init void *get_cert_list(u8 *key, unsigned long keylen, u64 *size)
34 rc = secvar_ops->get(key, keylen, NULL, size);
41 db = kmalloc(*size, GFP_KERNEL);
43 return ERR_PTR(-ENOMEM);
45 rc = secvar_ops->get(key, keylen, db, size);
55 * Load the certs contained in the keys databases into the platform trusted
56 * keyring and the blacklisted X.509 cert SHA256 hashes into the blacklist
59 static int __init load_powerpc_certs(void)
61 void *db = NULL, *dbx = NULL, *data = NULL;
73 len = secvar_ops->format(buf, sizeof(buf));
77 // Check for known secure boot implementations from OPAL or PLPKS
78 if (strcmp("ibm,edk2-compat-v1", buf) && strcmp("ibm,plpks-sb-v1", buf)) {
79 pr_err("Unsupported secvar implementation \"%s\", not loading certs\n", buf);
83 if (strcmp("ibm,plpks-sb-v1", buf) == 0)
84 /* PLPKS authenticated variables ESL data is prefixed with 8 bytes of timestamp */
88 * Get db, and dbx. They might not exist, so it isn't an error if we
91 data = get_cert_list("db", 3, &dsize);
93 pr_info("Couldn't get db list from firmware\n");
94 } else if (IS_ERR(data)) {
96 pr_err("Error reading db from firmware: %d\n", rc);
99 extract_esl(db, data, dsize, offset);
101 rc = parse_efi_signature_list("powerpc:db", db, dsize,
104 pr_err("Couldn't parse db signatures: %d\n", rc);
108 data = get_cert_list("dbx", 4, &dsize);
110 pr_info("Couldn't get dbx list from firmware\n");
111 } else if (IS_ERR(data)) {
113 pr_err("Error reading dbx from firmware: %d\n", rc);
116 extract_esl(dbx, data, dsize, offset);
118 rc = parse_efi_signature_list("powerpc:dbx", dbx, dsize,
119 get_handler_for_dbx);
121 pr_err("Couldn't parse dbx signatures: %d\n", rc);
125 data = get_cert_list("trustedcadb", 12, &dsize);
127 pr_info("Couldn't get trustedcadb list from firmware\n");
128 } else if (IS_ERR(data)) {
130 pr_err("Error reading trustedcadb from firmware: %d\n", rc);
132 extract_esl(trustedca, data, dsize, offset);
134 rc = parse_efi_signature_list("powerpc:trustedca", trustedca, dsize,
135 get_handler_for_ca_keys);
137 pr_err("Couldn't parse trustedcadb signatures: %d\n", rc);
141 data = get_cert_list("moduledb", 9, &dsize);
143 pr_info("Couldn't get moduledb list from firmware\n");
144 } else if (IS_ERR(data)) {
146 pr_err("Error reading moduledb from firmware: %d\n", rc);
148 extract_esl(moduledb, data, dsize, offset);
150 rc = parse_efi_signature_list("powerpc:moduledb", moduledb, dsize,
151 get_handler_for_code_signing_keys);
153 pr_err("Couldn't parse moduledb signatures: %d\n", rc);
159 late_initcall(load_powerpc_certs);