HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/ukify/ukify.py

99,044 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: e7f98667a80d75db00722f2cef852de5533d04f1

Lines 1–1000 of 2,747

#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# systemd is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with systemd; If not, see <https://www.gnu.org/licenses/>.
 
# pylint: disable=import-outside-toplevel,consider-using-with,unused-argument
# pylint: disable=unnecessary-lambda-assignment
 
import argparse
import builtins
import collections
import configparser
import contextlib
import dataclasses
import datetime
import fnmatch
import inspect
import itertools
import json
import os
import pprint
import pydoc
import re
import shlex
import shutil
import socket
import struct
import subprocess
import sys
import tempfile
import textwrap
import uuid
from collections.abc import Iterable, Iterator, Sequence
from hashlib import sha256
from pathlib import Path
from types import ModuleType
from typing import (
    IO,
    Any,
    Callable,
    Literal,
    Optional,
    TypeVar,
    Union,
    cast,
)
 
import pefile  # type: ignore
 
__version__ = '{{PROJECT_VERSION}} ({{VERSION_TAG}})'
 
EFI_ARCH_MAP = {
    # host_arch glob : [efi_arch, 32_bit_efi_arch if mixed mode is supported]
    'x86_64':        ['x64', 'ia32'],
    'i[3456]86':     ['ia32'],
    'aarch64':       ['aa64'],
    'armv[45678]*l': ['arm'],
    'loongarch32':   ['loongarch32'],
    'loongarch64':   ['loongarch64'],
    'riscv32':       ['riscv32'],
    'riscv64':       ['riscv64'],
}  # fmt: skip
EFI_ARCHES: list[str] = sum(EFI_ARCH_MAP.values(), [])
 
# Default configuration directories and file name.
# When the user does not specify one, the directories are searched in this order and the first file found is
# used.
DEFAULT_CONFIG_DIRS = ['/etc/systemd', '/run/systemd', '/usr/local/lib/systemd', '/usr/lib/systemd']
DEFAULT_CONFIG_FILE = 'ukify.conf'
 
# https://datatracker.ietf.org/doc/html/rfc5280 ub-common-name; bytes, not chars
COMMON_NAME_MAX_LEN = 64
INITRD_PCR_POLICY: tuple[list[str], str] = (['enter-initrd'], 'initrd')
 
 
def terminal_is_dumb() -> bool:
    return (
        not sys.stdout.isatty()
        or not sys.stderr.isatty()
        or os.getenv('TERM', '') == 'dumb'
        or bool(os.getenv('NO_COLOR'))
    )
 
 
class Style:
    bold = '\033[0;1;39m' if not terminal_is_dumb() else ''
    gray = '\033[0;38;5;245m' if not terminal_is_dumb() else ''
    red = '\033[31;1m' if not terminal_is_dumb() else ''
    yellow = '\033[33;1m' if not terminal_is_dumb() else ''
    reset = '\033[0m' if not terminal_is_dumb() else ''
 
 
def guess_efi_arch() -> str:
    arch = os.uname().machine
 
    for glob, mapping in EFI_ARCH_MAP.items():
        if fnmatch.fnmatch(arch, glob):
            efi_arch, *fallback = mapping
            break
    else:
        raise ValueError(f'Unsupported architecture {arch}')
 
    # This makes sense only on some architectures, but it also probably doesn't
    # hurt on others, so let's just apply the check everywhere.
    if fallback:
        fw_platform_size = Path('/sys/firmware/efi/fw_platform_size')
        try:
            size = fw_platform_size.read_text().strip()
        except FileNotFoundError:
            pass
        else:
            if int(size) == 32:
                efi_arch = fallback[0]
 
    # print(f'Host arch {arch!r}, EFI arch {efi_arch!r}')
    return efi_arch
 
 
def page(text: str, enabled: Optional[bool]) -> None:
    if enabled:
        # Initialize less options from $SYSTEMD_LESS or provide a suitable fallback.
        os.environ['LESS'] = os.getenv('SYSTEMD_LESS', 'FRSXMK')
        pydoc.pager(text)
    else:
        print(text)
 
 
def shell_join(cmd: list[Union[str, Path]]) -> str:
    # TODO: drop in favour of shlex.join once shlex.join supports Path.
    return ' '.join(shlex.quote(str(x)) for x in cmd)
 
 
def round_up(x: int, blocksize: int = 4096) -> int:
    return (x + blocksize - 1) // blocksize * blocksize
 
 
def try_import(modname: str, name: Optional[str] = None) -> ModuleType:
    try:
        return __import__(modname)
    except ImportError as e:
        raise ValueError(f'Kernel is compressed with {name or modname}, but module unavailable') from e
 
 
def read_env_file(text: str) -> dict[str, str]:
    result = {}
 
    for line in text.splitlines():
        line = line.rstrip()
        if not line or line.startswith('#'):
            continue
        if m := re.match(r'([A-Z][A-Z_0-9]+)=(.*)', line):
            name, val = m.groups()
            if val and val[0] in '"\'':
                val = next(shlex.shlex(val, posix=True))
 
            result[name] = val
        else:
            print(f'bad line {line!r}', file=sys.stderr)
 
    return result
 
 
def get_zboot_kernel(f: IO[bytes]) -> bytes:
    """Decompress zboot efistub kernel if compressed. Return contents."""
    # See linux/drivers/firmware/efi/libstub/Makefile.zboot
    # and linux/drivers/firmware/efi/libstub/zboot-header.S
 
    # 4 bytes at offset 0x08 contain the starting offset of compressed data
    f.seek(8)
    _start = f.read(4)
    start = struct.unpack('<i', _start)[0]
 
    # Reading 4 bytes from address 0x0c is the size of compressed data,
    # but it needs to be corrected according to the compressed type.
    f.seek(0xC)
    _sizes = f.read(4)
    size = struct.unpack('<i', _sizes)[0]
 
    # Read 6 bytes from address 0x18, which is a nul-terminated
    # string representing the compressed type.
    f.seek(0x18)
    comp_type = f.read(6)
    f.seek(start)
    if comp_type.startswith(b'gzip'):
        gzip = try_import('gzip')
        data = f.read(size)
        return cast(bytes, gzip.decompress(data))
    elif comp_type.startswith(b'lz4'):
        lz4 = try_import('lz4.frame', 'lz4')
        data = f.read(size)
        return cast(bytes, lz4.frame.decompress(data))
    elif comp_type.startswith(b'lzma'):
        lzma = try_import('lzma')
        data = f.read(size)
        return cast(bytes, lzma.decompress(data))
    elif comp_type.startswith(b'lzo'):
        raise NotImplementedError('lzo decompression not implemented')
    elif comp_type.startswith(b'xzkern'):
        raise NotImplementedError('xzkern decompression not implemented')
    elif comp_type.startswith(b'zstd'):
        try:
            zstd = try_import('compression.zstd')
            data = f.read(size)
            return cast(bytes, zstd.zstd.decompress(data))
        except ValueError:
            zstd = try_import('zstandard')
            data = f.read(size)
            return cast(bytes, zstd.ZstdDecompressor().stream_reader(data).read())
 
    raise NotImplementedError(f'unknown compressed type: {comp_type!r}')
 
 
def maybe_decompress(filename: Union[str, Path]) -> bytes:
    """Decompress file if compressed. Return contents."""
    f = open(filename, 'rb')
    start = f.read(4)
    f.seek(0)
 
    if start.startswith(b'\x7fELF'):
        # not compressed
        return f.read()
 
    if start.startswith(b'MZ'):
        f.seek(4)
        img_type = f.read(4)
        if img_type.startswith(b'zimg'):
            # zboot efistub kernel
            return get_zboot_kernel(f)
        else:
            # not compressed aarch64 and riscv64
            return f.read()
 
    if start.startswith(b'\x1f\x8b'):
        gzip = try_import('gzip')
        return cast(bytes, gzip.open(f).read())
 
    if start.startswith(b'\x28\xb5\x2f\xfd'):
        try:
            zstd = try_import('compression.zstd')
            return cast(bytes, zstd.zstd.decompress(f.read()))
        except ValueError:
            zstd = try_import('zstandard')
            return cast(bytes, zstd.ZstdDecompressor().stream_reader(f.read()).read())
 
    if start.startswith(b'\x02\x21\x4c\x18'):
        lz4 = try_import('lz4.frame', 'lz4')
        return cast(bytes, lz4.frame.decompress(f.read()))
 
    if start.startswith(b'\x04\x22\x4d\x18'):
        print('Newer lz4 stream format detected! This may not boot!', file=sys.stderr)
        lz4 = try_import('lz4.frame', 'lz4')
        return cast(bytes, lz4.frame.decompress(f.read()))
 
    if start.startswith(b'\x89LZO'):
        # python3-lzo is not packaged for Fedora
        raise NotImplementedError('lzo decompression not implemented')
 
    if start.startswith(b'BZh'):
        bz2 = try_import('bz2', 'bzip2')
        return cast(bytes, bz2.open(f).read())
 
    if start.startswith(b'\x5d\x00\x00'):
        lzma = try_import('lzma')
        return cast(bytes, lzma.open(f).read())
 
    raise NotImplementedError(f'unknown file format (starts with {start!r})')
 
 
@dataclasses.dataclass
class UkifyConfig:
    all: bool
    cmdline: Union[str, Path, None]
    devicetree: Path
    devicetree_auto: list[Path]
    efi_arch: str
    hwids: Union[str, Path, None]
    initrd: list[Path]
    efifw: list[Path]
    join_profiles: list[Path]
    sign_profiles: list[str]
    json: Union[Literal['pretty'], Literal['short'], Literal['off']]
    linux: Optional[Path]
    measure: bool
    microcode: Path
    os_release: Union[str, Path, None]
    output: Optional[str]
    pcr_banks: list[str]
    pcr_private_keys: list[str]
    pcr_public_keys: list[str]
    pcr_certificates: list[str]
    pcrpkey: Optional[Path]
    pcrsig: Union[str, Path, None]
    join_pcrsig: Optional[Path]
    phase_path_groups: Optional[list[list[str]]]
    policyrefs: Optional[list[str]]
    sign_initrd_pcrs: bool
    policy_digest: bool
    profile: Optional[str]
    sb_cert: Union[str, Path, None]
    sb_cert_common_name: Optional[str]
    sb_cert_name: Optional[str]
    sb_cert_validity: int
    sb_certdir: Path
    sb_key: Union[str, Path, None]
    sbat: Optional[list[str]]
    sections: list['Section']
    sections_by_name: dict[str, 'Section']
    sign_kernel: Optional[bool]
    signing_engine: Optional[str]
    signing_provider: Optional[str]
    certificate_provider: Optional[str]
    signtool: Optional[str]
    splash: Optional[Path]
    stub: Path
    summary: bool
    tools: list[Path]
    uname: Optional[str]
    verb: str
    files: list[str] = dataclasses.field(default_factory=list)
 
    @classmethod
    def from_namespace(cls, ns: argparse.Namespace) -> 'UkifyConfig':
        return cls(**{k: v for k, v in vars(ns).items() if k in inspect.signature(cls).parameters})
 
 
class Uname:
    # This class is here purely as a namespace for the functions
 
    VERSION_PATTERN = r'(?P<version>[a-z0-9._+-]+) \([^ )]+\) (?:#.*)'
 
    NOTES_PATTERN = r'^\s+Linux\s+0x[0-9a-f]+\s+OPEN\n\s+description data: (?P<version>[0-9a-f ]+)\s*$'
 
    # Linux version 6.0.8-300.fc37.ppc64le (mockbuild@buildvm-ppc64le-03.iad2.fedoraproject.org)
    # (gcc (GCC) 12.2.1 20220819 (Red Hat 12.2.1-2), GNU ld version 2.38-24.fc37)
    # #1 SMP Fri Nov 11 14:39:11 UTC 2022
    TEXT_PATTERN = rb'Linux version (?P<version>\d\.\S+) \('
 
    @classmethod
    def scrape_x86(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> str:
        # Based on https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L136
        # and https://docs.kernel.org/arch/x86/boot.html#the-real-mode-kernel-header
        with open(filename, 'rb') as f:
            f.seek(0x202)
            magic = f.read(4)
            if magic != b'HdrS':
                raise ValueError('Real-Mode Kernel Header magic not found')
            f.seek(0x20E)
            offset = f.read(1)[0] + f.read(1)[0] * 256  # Pointer to kernel version string
            f.seek(0x200 + offset)
            text = f.read(128)
        text = text.split(b'\0', maxsplit=1)[0]
        decoded = text.decode()
 
        if not (m := re.match(cls.VERSION_PATTERN, decoded)):
            raise ValueError(f'Cannot parse version-host-release uname string: {text!r}')
        return m.group('version')
 
    @classmethod
    def scrape_elf(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> str:
        readelf = find_tool('readelf', opts=opts)
 
        cmd = [
            readelf,
            '--notes',
            filename,
        ]
 
        print('+', shell_join(cmd), file=sys.stderr)
        try:
            notes = subprocess.check_output(cmd, stderr=subprocess.PIPE, text=True)
        except subprocess.CalledProcessError as e:
            raise ValueError(e.stderr.strip()) from e
 
        if not (m := re.search(cls.NOTES_PATTERN, notes, re.MULTILINE)):
            raise ValueError('Cannot find Linux version note')
 
        text = ''.join(chr(int(c, 16)) for c in m.group('version').split())
        return text.rstrip('\0')
 
    @classmethod
    def scrape_generic(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> str:
        # import libarchive
        # libarchive-c fails with
        # ArchiveError: Unrecognized archive format (errno=84, retcode=-30, archive_p=94705420454656)
 
        # Based on https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L209
 
        text = maybe_decompress(filename)
        if not (m := re.search(cls.TEXT_PATTERN, text)):
            raise ValueError(f'Cannot find {cls.TEXT_PATTERN!r} in {filename}')
 
        return m.group('version').decode()
 
    @classmethod
    def scrape(cls, filename: Path, opts: Optional[UkifyConfig] = None) -> Optional[str]:
        for func in (cls.scrape_x86, cls.scrape_elf, cls.scrape_generic):
            try:
                version = func(filename, opts=opts)
                print(f'Found uname version: {version}', file=sys.stderr)
                return version
            except ValueError as e:
                print(str(e), file=sys.stderr)
        return None
 
 
DEFAULT_SECTIONS_TO_SHOW = {
    '.linux':   'binary',
    '.initrd':  'binary',
    '.ucode':   'binary',
    '.splash':  'binary',
    '.dtb':     'binary',
    '.dtbauto': 'binary',
    '.hwids':   'binary',
    '.efifw':   'binary',
    '.cmdline': 'text',
    '.osrel':   'text',
    '.uname':   'text',
    '.pcrpkey': 'text',
    '.pcrsig':  'text',
    '.sbat':    'text',
    '.sbom':    'binary',
    '.profile': 'text',
}  # fmt: skip
 
# Sections that may legitimately appear more than once within a single profile: they carry one entry
# per hardware variant and the firmware picks the matching one at boot. 'inspect --json' therefore
# always reports them as a list, and add_section() allows them to repeat.
MULTI_INSTANCE_SECTIONS = ('.dtbauto', '.efifw')
 
 
@dataclasses.dataclass
class Section:
    name: str
    content: Optional[Path]
    tmpfile: Optional[IO[Any]] = None
    measure: bool = False
    output_mode: Optional[str] = None
 
    @classmethod
    def create(cls, name: str, contents: Union[str, bytes, Path, None], **kwargs: Any) -> 'Section':
        if isinstance(contents, (str, bytes)):
            mode = 'wt' if isinstance(contents, str) else 'wb'
            tmp = tempfile.NamedTemporaryFile(mode=mode, prefix=f'tmp{name}')
            tmp.write(contents)
            tmp.flush()
            contents = Path(tmp.name)
        else:
            tmp = None
 
        return cls(name, contents, tmpfile=tmp, **kwargs)
 
    @classmethod
    def parse_input(cls, s: str) -> 'Section':
        try:
            name, contents, *rest = s.split(':')
        except ValueError as e:
            raise ValueError(f'Cannot parse section spec (name or contents missing): {s!r}') from e
        if rest:
            raise ValueError(f'Cannot parse section spec (extraneous parameters): {s!r}')
 
        if contents.startswith('@'):
            sec = cls.create(name, Path(contents[1:]))
        else:
            sec = cls.create(name, contents)
 
        sec.check_name()
        return sec
 
    @classmethod
    def parse_output(cls, s: str) -> 'Section':
        if not (m := re.fullmatch(r'([a-zA-Z0-9_.]+):(text|binary)(?:@(.+))?', s)):
            raise ValueError(f'Cannot parse section spec: {s!r}')
 
        name, ttype, out = m.groups()
        out = Path(out) if out else None
 
        return cls.create(name, out, output_mode=ttype)
 
    def check_name(self) -> None:
        # PE section names with more than 8 characters are legal, but our stub does
        # not support them.
        if not self.name.isascii() or not self.name.isprintable():
            raise ValueError(f'Bad section name: {self.name!r}')
        if len(self.name) > 8:
            raise ValueError(f'Section name too long: {self.name!r}')
 
 
@dataclasses.dataclass
class UKI:
    executable: Path
    sections: list[Section] = dataclasses.field(default_factory=list, init=False)
 
    def add_section(self, section: Section) -> None:
        start = 0
 
        # Start search at last .profile section, if there is one
        for i, s in enumerate(self.sections):
            if s.name == '.profile':
                start = i + 1
 
        if any(
            section.name == s.name for s in self.sections[start:] if s.name not in MULTI_INSTANCE_SECTIONS
        ):
            raise ValueError(f'Duplicate section {section.name}')
 
        self.sections += [section]
 
 
class SignTool:
    @staticmethod
    def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
        raise NotImplementedError
 
    @staticmethod
    def verify(input_f: Path, opts: UkifyConfig) -> bool:
        raise NotImplementedError
 
    @staticmethod
    def from_string(name: str) -> type['SignTool']:
        if name == 'pesign':
            return PeSign
        elif name == 'sbsign':
            return SbSign
        elif name == 'systemd-sbsign':
            return SystemdSbSign
        else:
            raise ValueError(f'Invalid sign tool: {name!r}')
 
 
class PeSign(SignTool):
    @staticmethod
    def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
        assert opts.sb_certdir is not None
        assert opts.sb_cert_name is not None
 
        tool = find_tool('pesign', opts=opts, msg='pesign, required for signing, is not installed')
        cmd = [
            tool,
            '-s',
            '--force',
            '-n', opts.sb_certdir,
            '-c', opts.sb_cert_name,
            '-i', input_f,
            '-o', output_f,
        ]  # fmt: skip
 
        print('+', shell_join(cmd), file=sys.stderr)
        subprocess.check_call(cmd)
 
    @staticmethod
    def verify(input_f: Path, opts: UkifyConfig) -> bool:
        assert input_f is not None
 
        tool = find_tool('pesign', opts=opts)
        cmd = [tool, '-i', input_f, '-S']
 
        print('+', shell_join(cmd), file=sys.stderr)
        info = subprocess.check_output(cmd, text=True)
 
        return 'No signatures found.' in info
 
 
class SbSign(SignTool):
    @staticmethod
    def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
        assert opts.sb_key is not None
        assert opts.sb_cert is not None
 
        tool = find_tool('sbsign', opts=opts, msg='sbsign, required for signing, is not installed')
        cmd = [
            tool,
            '--key', opts.sb_key,
            '--cert', opts.sb_cert,
            *(['--engine', opts.signing_engine] if opts.signing_engine is not None else []),
            input_f,
            '--output', output_f,
        ]  # fmt: skip
 
        print('+', shell_join(cmd), file=sys.stderr)
        subprocess.check_call(cmd)
 
    @staticmethod
    def verify(input_f: Path, opts: UkifyConfig) -> bool:
        assert input_f is not None
 
        tool = find_tool('sbverify', opts=opts)
        cmd = [tool, '--list', input_f]
 
        print('+', shell_join(cmd), file=sys.stderr)
        info = subprocess.check_output(cmd, text=True)
 
        return 'No signature table present' in info
 
 
class SystemdSbSign(SignTool):
    @staticmethod
    def sign(input_f: str, output_f: str, opts: UkifyConfig) -> None:
        assert opts.sb_key is not None
        assert opts.sb_cert is not None
 
        tool = find_tool(
            'systemd-sbsign',
            '/usr/lib/systemd/systemd-sbsign',
            opts=opts,
            msg='systemd-sbsign, required for signing, is not installed',
        )
        cmd = [
            tool,
            'sign',
            '--private-key', opts.sb_key,
            '--certificate', opts.sb_cert,
            *(
                ['--private-key-source', f'engine:{opts.signing_engine}']
                if opts.signing_engine is not None
                else []
            ),
            *(
                ['--private-key-source', f'provider:{opts.signing_provider}']
                if opts.signing_provider is not None
                else []
            ),
            *(
                ['--certificate-source', f'provider:{opts.certificate_provider}']
                if opts.certificate_provider is not None
                else []
            ),
            input_f,
            '--output', output_f,
        ]  # fmt: skip
 
        print('+', shell_join(cmd), file=sys.stderr)
        subprocess.check_call(cmd)
 
    @staticmethod
    def verify(input_f: Path, opts: UkifyConfig) -> bool:
        raise NotImplementedError('systemd-sbsign cannot yet verify if existing PE binaries are signed')
 
 
def parse_banks(s: str) -> list[str]:
    s = s.strip()
    if not s:
        return []
    # Allow a single trailing comma, but reject any other empty entry.
    banks = re.split(r'\s*,\s*|\s+', s.removesuffix(',').rstrip())
    if '' in banks:
        raise argparse.ArgumentTypeError(f'Empty entry in PCR bank list {s!r}')
    # TODO: do some sanity checking here
    return banks
 
 
KNOWN_PHASES = (
    'enter-initrd',
    'leave-initrd',
    'sysinit',
    'ready',
    'shutdown',
    'final',
)
 
 
def parse_phase_paths(s: str) -> list[str]:
    # Split on commas or whitespace here. Commas might be hard to parse visually.
    # Allow a single trailing comma. Other empty entries are rejected below as unknown phases.
    paths = re.split(r'\s*,\s*|\s+', s.strip().removesuffix(',').rstrip())
 
    for path in paths:
        for phase in path.split(':'):
            if phase not in KNOWN_PHASES:
                raise argparse.ArgumentTypeError(f'Unknown boot phase {phase!r} ({path=})')
 
    return paths
 
 
def check_splash(filename: Optional[Path]) -> None:
    if filename is None:
        return
 
    # import is delayed, to avoid import when the splash image is not used
    try:
        from PIL import Image
    except ImportError:
        return
 
    img = Image.open(filename, formats=['BMP'])
    print(f'Splash image {filename} is {img.width}×{img.height} pixels', file=sys.stderr)
 
 
def check_inputs(opts: UkifyConfig) -> None:
    for name, value in vars(opts).items():
        if name in {'output', 'tools'}:
            continue
 
        if isinstance(value, Path):
            # Check that we can open the directory or file, or generate and exception
            if value.is_dir():
                value.iterdir()
            else:
                value.open().close()
        elif isinstance(value, list):
            for item in value:
                if isinstance(item, Path):
                    if item.is_dir():
                        item.iterdir()
                    else:
                        item.open().close()
 
    check_splash(opts.splash)
 
 
def check_cert_and_keys_nonexistent(opts: UkifyConfig) -> None:
    # A repeated PCR key pair is safe, but different outputs must not overwrite one another.
    outputs: dict[Path, tuple[str, tuple[Optional[Path], Optional[Path]], int]] = {}
    groups = [
        ('SecureBoot', opts.sb_key, opts.sb_cert),
        *(
            ('PCR', priv_key, pub_key)
            for priv_key, pub_key, _, _, _ in key_path_groups(opts, include_extra=False)
        ),
    ]
    for kind, private, public in groups:
        pair = (Path(private).resolve() if private else None, Path(public).resolve() if public else None)
        for role, output_path in enumerate(pair):
            if output_path is None:
                continue
            owner = (kind, pair, role)
            if output_path in outputs and outputs[output_path] != owner:
                raise ValueError(f'Conflicting key output path: {output_path}')
            outputs[output_path] = owner
 
    # Raise if any of the keys and certs are found on disk
    paths: Iterator[Union[str, Path, None]] = itertools.chain(
        (opts.sb_key, opts.sb_cert),
        *(
            (priv_key, pub_key, cert)
            for priv_key, pub_key, cert, _, _ in key_path_groups(opts, include_extra=False)
        ),
    )
    for path in paths:
        if path and Path(path).exists():
            raise ValueError(f'{path} is present')
 
 
def find_tool(
    name: str,
    fallback: Optional[str] = None,
    opts: Optional[UkifyConfig] = None,
    msg: str = 'Tool {name} not installed!',
) -> Union[str, Path]:
    if opts and opts.tools:
        for d in opts.tools:
            tool = d / name
            if tool.exists():
                return tool
 
    if shutil.which(name) is not None:
        return name
 
    if fallback is None:
        raise ValueError(msg.format(name=name))
 
    return fallback
 
 
def combine_signatures(pcrsigs: list[dict[str, str]]) -> str:
    combined: collections.defaultdict[str, list[str]] = collections.defaultdict(list)
    for pcrsig in pcrsigs:
        for bank, sigs in pcrsig.items():
            for sig in sigs:
                if sig not in combined[bank]:
                    combined[bank] += [sig]
    return json.dumps(combined)
 
 
def apply_pcr_signatures(pcrsig: dict[str, Any], signatures: dict[str, Any]) -> None:
    for (bank, policies), (input_bank, input_policies) in itertools.product(
        pcrsig.items(), signatures.items()
    ):
        if input_bank != bank:
            continue
        for policy, input_policy in itertools.product(policies, input_policies):
            if 'tbs' in policy and 'tbs' in input_policy:
                match = policy['tbs'] == input_policy['tbs']
            else:
                match = (policy['pol'], policy.get('ref')) == (
                    input_policy['pol'],
                    input_policy.get('ref'),
                )
 
            if match:
                policy['sig'] = input_policy['sig']
 
 
def key_path_groups(
    opts: UkifyConfig,
    include_extra: bool,
) -> Iterator[tuple[str, Optional[str], Optional[str], Optional[list[str]], Optional[str]]]:
    if not opts.pcr_private_keys:
        return
 
    n_priv = len(opts.pcr_private_keys)
    pub_keys = opts.pcr_public_keys or []
    certs = opts.pcr_certificates or []
    pp_groups = opts.phase_path_groups or []
    policyrefs = opts.policyrefs or []
 
    yield from itertools.zip_longest(
        opts.pcr_private_keys,
        pub_keys[:n_priv],
        certs[:n_priv],
        pp_groups[:n_priv],
        policyrefs[:n_priv],
        fillvalue=None,
    )
 
    # When requested, emit an extra signing group that reuses the first PCR signing key to produce
    # a signed PCR policy that can only be used from the initrd.
    if opts.sign_initrd_pcrs and include_extra:
        yield (
            opts.pcr_private_keys[0],
            pub_keys[0] if pub_keys else None,
            certs[0] if certs else None,
            *INITRD_PCR_POLICY,
        )
 
 
def pe_strip_section_name(name: bytes) -> str:
    return name.rstrip(b'\x00').decode()
 
 
def pe_section_size(section: pefile.SectionStructure) -> int:
    return cast(int, min(section.Misc_VirtualSize, section.SizeOfRawData))
 
 
def call_systemd_measure(uki: UKI, opts: UkifyConfig, profile_start: int = 0) -> str:
    measure_tool = find_tool(
        'systemd-measure',
        '/usr/lib/systemd/systemd-measure',
        opts=opts,
    )
    combined = ''
 
    banks = opts.pcr_banks or ()
 
    # PCR measurement
 
    # First, pick up either the base sections or the profile specific sections we shall measure now
    unique_to_measure = {
        s.name: s for s in uki.sections[profile_start:] if s.measure and s.name != '.dtbauto'
    }
 
    dtbauto_to_measure: list[Optional[Section]] = [
        s for s in uki.sections[profile_start:] if s.measure and s.name == '.dtbauto'
    ]
 
    if len(dtbauto_to_measure) == 0:
        dtbauto_to_measure = [None]
 
    # Then, if we're measuring a profile, lookup the missing sections from the base image.
    if profile_start != 0:
        for section in uki.sections:
            # If we reach the first .profile section the base is over
            if section.name == '.profile':
                break
 
            # Only some sections are measured
            if not section.measure:
                continue
 
            # Check if this is a section we already covered above
            if section.name in unique_to_measure:
                continue
 
            unique_to_measure[section.name] = section
 
    if opts.measure or opts.policy_digest:
        pcrsigs = []
        to_measure = unique_to_measure.copy()
 
        for dtbauto in dtbauto_to_measure:
            if dtbauto is not None:
                to_measure[dtbauto.name] = dtbauto
 
            policy_groups: list[tuple[list[str], str]] = [
                (list(itertools.chain.from_iterable(opts.phase_path_groups or [])), ''),
            ]
            if opts.policy_digest and opts.sign_initrd_pcrs:
                policy_groups += [INITRD_PCR_POLICY]
 
            for phase_paths, policyref in policy_groups:
                cmd = [
                    measure_tool,
                    'calculate' if opts.measure else 'policy-digest',
                    '--json',
                    opts.json,
                    *(f'--{s.name.removeprefix(".")}={s.content}' for s in to_measure.values()),
                    *(f'--bank={bank}' for bank in banks),
                    # For measurement, the keys are not relevant, so we can lump all the phase paths
                    # into one call to systemd-measure calculate.
                    *(f'--phase={phase_path}' for phase_path in phase_paths),
                ]
 
                # The JSON object will be used for offline signing, include the public key
                # so that the fingerprint is included too. In case a certificate is passed, use the
                # right parameter so that systemd-measure can extract the public key from it.
                if opts.policy_digest:
                    if opts.pcr_public_keys:
                        cmd += [f'--public-key={opts.pcr_public_keys[0]}']
                    elif opts.pcr_certificates:
                        cmd += [f'--certificate={opts.pcr_certificates[0]}']
                        if opts.certificate_provider:
                            cmd += [f'--certificate-source=provider:{opts.certificate_provider}']
 
                    if policyref:
                        cmd += [f'--policyref={policyref}']
 
                print('+', shell_join(cmd), file=sys.stderr)
                output = subprocess.check_output(cmd, text=True)  # type: ignore
 
                if opts.policy_digest:
                    pcrsig = json.loads(output)
                    pcrsigs += [pcrsig]
                else:
                    print(output)
 
        if opts.policy_digest:
            combined = combine_signatures(pcrsigs)
            # We need to ensure the section has space for signatures, that will be added separately later,
            # so add some whitespace to pad the section. At most we'll need 4kb per digest (rsa4096).
            # We might even check the key type given we have it to know the precise length, but don't
            # bother for now.
            combined += ' ' * 1024 * combined.count('"pol":')
            uki.add_section(Section.create('.pcrsig', combined))
 
    # PCR signing
 
    if opts.pcr_private_keys:
        pcrsigs = []
        to_measure = unique_to_measure.copy()
 
        for dtbauto in dtbauto_to_measure:
            if dtbauto is not None:
                to_measure[dtbauto.name] = dtbauto
 
            cmd = [
                measure_tool,
                'sign',
                *(f'--{s.name.removeprefix(".")}={s.content}' for s in to_measure.values()),
                *(f'--bank={bank}' for bank in banks),
            ]
 
            for priv_key, pub_key, cert, group, ref in key_path_groups(opts, include_extra=True):
                extra = [f'--private-key={priv_key}']
                if opts.signing_engine is not None:
                    assert pub_key or cert
                    # Backward compatibility, we used to pass the public key as the certificate
                    # as there was no --pcr-certificate= parameter
                    extra += [
                        f'--private-key-source=engine:{opts.signing_engine}',
                        f'--certificate={pub_key or cert}',
                    ]
                elif opts.signing_provider is not None:
                    assert pub_key or cert
                    extra += [
                        f'--private-key-source=provider:{opts.signing_provider}',
                        f'--certificate={pub_key or cert}',
                    ]
                elif cert:
                    extra += [f'--certificate={cert}']
                elif pub_key:
                    extra += [f'--public-key={pub_key}']
 
                if opts.certificate_provider is not None:
                    extra += [f'--certificate-source=provider:{opts.certificate_provider}']
 
                extra += [f'--phase={phase_path}' for phase_path in group or ()]
                if ref is not None:
                    extra += [f'--policyref={ref}']
 
                print('+', shell_join(cmd + extra), file=sys.stderr)  # type: ignore
                output = subprocess.check_output(cmd + extra, text=True)  # type: ignore
                pcrsig = json.loads(output)
                pcrsigs += [pcrsig]
 
        combined = combine_signatures(pcrsigs)
        uki.add_section(Section.create('.pcrsig', combined))
 
    return combined