HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/ukify/ukify.py
Git blob: e7f98667a80d75db00722f2cef852de5533d04f1
Lines 1001–2000 of 2,747
def join_initrds(initrds: list[Path]) -> Union[Path, bytes, None]:
if not initrds:
return None
if len(initrds) == 1:
return initrds[0]
seq = []
for file in initrds:
initrd = file.read_bytes()
n = len(initrd)
padding = b'\0' * (round_up(n, 4) - n) # pad to 32 bit alignment
seq += [initrd, padding]
return b''.join(seq)
T = TypeVar('T')
def pairwise(iterable: Iterable[T]) -> Iterator[tuple[T, Optional[T]]]:
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
class PEError(Exception):
pass
def pe_add_sections(opts: UkifyConfig, uki: UKI, output: str) -> None:
pe = pefile.PE(uki.executable, fast_load=True)
# Old stubs do not have the symbol/string table stripped, even though image files should not have one.
if symbol_table := pe.FILE_HEADER.PointerToSymbolTable:
symbol_table_size = 18 * pe.FILE_HEADER.NumberOfSymbols
if string_table_size := pe.get_dword_from_offset(symbol_table + symbol_table_size):
symbol_table_size += string_table_size
# Let's be safe and only strip it if it's at the end of the file.
if symbol_table + symbol_table_size == len(pe.__data__):
pe.__data__ = pe.__data__[:symbol_table]
pe.FILE_HEADER.PointerToSymbolTable = 0
pe.FILE_HEADER.NumberOfSymbols = 0
pe.FILE_HEADER.IMAGE_FILE_LOCAL_SYMS_STRIPPED = True
# pylint thinks that Structure doesn't have various members that it has…
# pylint: disable=no-member
# Old stubs might have been stripped, leading to unaligned raw data values, so let's fix them up here.
for i, section in enumerate(pe.sections):
oldp = section.PointerToRawData
oldsz = section.SizeOfRawData
section.PointerToRawData = round_up(oldp, pe.OPTIONAL_HEADER.FileAlignment)
section.SizeOfRawData = round_up(oldsz, pe.OPTIONAL_HEADER.FileAlignment)
padp = section.PointerToRawData - oldp
padsz = section.SizeOfRawData - oldsz
for later_section in pe.sections[i + 1 :]:
later_section.PointerToRawData += padp + padsz
pe.__data__ = (
pe.__data__[:oldp]
+ bytes(padp)
+ pe.__data__[oldp : oldp + oldsz]
+ bytes(padsz)
+ pe.__data__[oldp + oldsz :]
)
# We might not have any space to add new sections. Let's try our best to make some space by padding the
# SizeOfHeaders to a multiple of the file alignment. This is safe because the first section's data starts
# at a multiple of the file alignment, so all space before that is unused.
pe.OPTIONAL_HEADER.SizeOfHeaders = round_up(
pe.OPTIONAL_HEADER.SizeOfHeaders, pe.OPTIONAL_HEADER.FileAlignment
)
pe = pefile.PE(data=pe.write(), fast_load=True)
# pefile has an hardcoded limit of 256MB, which is not enough when building an initrd with large firmware
# files and all kernel modules. See: https://github.com/erocarrera/pefile/issues/396
warnings = pe.get_warnings()
for w in warnings:
if 'VirtualSize is extremely large' in w:
continue
if 'VirtualAddress is beyond' in w:
continue
raise PEError(f'pefile warnings treated as errors: {warnings}')
# When attaching signatures we are operating on an existing UKI which might be signed
if not opts.pcrsig:
security = pe.OPTIONAL_HEADER.DATA_DIRECTORY[
pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_SECURITY']
]
if security.VirtualAddress != 0:
# We could strip the signatures, but why would anyone sign the stub?
raise PEError('Stub image is signed, refusing')
# Remember how many sections originate from systemd-stub
n_original_sections = len(pe.sections)
for section in uki.sections:
new_section = pefile.SectionStructure(pe.__IMAGE_SECTION_HEADER_format__, pe=pe)
new_section.__unpack__(b'\0' * new_section.sizeof())
offset = pe.sections[-1].get_file_offset() + new_section.sizeof()
if offset + new_section.sizeof() > pe.OPTIONAL_HEADER.SizeOfHeaders:
raise PEError(f'Not enough header space to add section {section.name}.')
assert section.content
data = section.content.read_bytes()
new_section.set_file_offset(offset)
new_section.Name = section.name.encode()
new_section.Misc_VirtualSize = len(data)
# Non-stripped stubs might still have an unaligned symbol table at the end, making their size
# unaligned, so we make sure to explicitly pad the pointer to new sections to an aligned offset.
new_section.PointerToRawData = round_up(len(pe.__data__), pe.OPTIONAL_HEADER.FileAlignment)
new_section.SizeOfRawData = round_up(len(data), pe.OPTIONAL_HEADER.FileAlignment)
new_section.VirtualAddress = round_up(
pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize,
pe.OPTIONAL_HEADER.SectionAlignment,
)
new_section.IMAGE_SCN_MEM_READ = True
if section.name == '.linux':
# Old kernels that use EFI handover protocol will be executed inline.
new_section.IMAGE_SCN_CNT_CODE = True
# Check if the kernel PE has the NX_COMPAT flag set, if not strip it from the UKI as they need
# to have the same value, otherwise when firmwares start enforcing it, booting will fail.
# https://microsoft.github.io/mu/WhatAndWhy/enhancedmemoryprotection/
# https://www.kraxel.org/blog/2023/12/uefi-nx-linux-boot/
try:
inner_pe = pefile.PE(data=data, fast_load=True)
nxbit = pefile.DLL_CHARACTERISTICS['IMAGE_DLLCHARACTERISTICS_NX_COMPAT']
if not inner_pe.OPTIONAL_HEADER.DllCharacteristics & nxbit:
pe.OPTIONAL_HEADER.DllCharacteristics &= ~nxbit
except pefile.PEFormatError:
# Unit tests build images with bogus data
print(f'{section.name} in {uki.executable} is not a valid PE, ignoring', file=sys.stderr)
else:
new_section.IMAGE_SCN_CNT_INITIALIZED_DATA = True
# Special case, mostly for .sbat: the stub will already have a .sbat section, but we want to append
# the one from the kernel to it. It should be small enough to fit in the existing section, so just
# swap the data.
for i, s in enumerate(pe.sections[:n_original_sections]):
if pe_strip_section_name(s.Name) == section.name and section.name != '.dtbauto':
if new_section.Misc_VirtualSize > s.SizeOfRawData:
raise PEError(
f'Not enough space in existing section {section.name} to append new data'
f' (need {new_section.Misc_VirtualSize}, have {s.SizeOfRawData})'
)
padding = bytes(s.SizeOfRawData - new_section.Misc_VirtualSize)
pe.__data__ = (
pe.__data__[: s.PointerToRawData]
+ data
+ padding
+ pe.__data__[pe.sections[i + 1].PointerToRawData :]
)
s.Misc_VirtualSize = new_section.Misc_VirtualSize
break
else:
pe.__data__ = (
pe.__data__[:]
+ bytes(new_section.PointerToRawData - len(pe.__data__))
+ data
+ bytes(new_section.SizeOfRawData - len(data))
)
pe.FILE_HEADER.NumberOfSections += 1
pe.OPTIONAL_HEADER.SizeOfInitializedData += new_section.Misc_VirtualSize
pe.__structures__.append(new_section)
pe.sections.append(new_section)
# If there is a pre-signed JSON blob, we need to update the existing JSON, by appending the signature to
# each corresponding digest object. We have built the unsigned UKI with enough space to fit the .sig
# objects, so we can just replace the new signed JSON in the existing sections.
if opts.pcrsig:
signatures = json.loads(str(opts.pcrsig))
for i, section in enumerate(pe.sections):
if pe_strip_section_name(section.Name) == '.pcrsig':
j = json.loads(
bytes(
pe.__data__[
section.PointerToRawData : section.PointerToRawData + section.SizeOfRawData
]
)
.rstrip(b'\x00')
.decode()
)
apply_pcr_signatures(j, signatures)
encoded = json.dumps(j).encode()
if len(encoded) > section.SizeOfRawData:
raise PEError(
f'Not enough space in existing section .pcrsig of size {section.SizeOfRawData} to append new data of size {len(encoded)}'
)
section.Misc_VirtualSize = len(encoded)
# bytes(n) results in an array of n zeroes
padding = bytes(section.SizeOfRawData - len(encoded))
pe.__data__ = (
pe.__data__[: section.PointerToRawData]
+ encoded
+ padding
+ pe.__data__[section.PointerToRawData + section.SizeOfRawData :]
)
pe.OPTIONAL_HEADER.CheckSum = 0
pe.OPTIONAL_HEADER.SizeOfImage = round_up(
pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize,
pe.OPTIONAL_HEADER.SectionAlignment,
)
pe.write(output)
def merge_sbat(input_pe: list[Path], input_text: list[str]) -> str:
sbat = []
for f in input_pe:
try:
pe = pefile.PE(f, fast_load=True)
except pefile.PEFormatError:
print(f'{f} is not a valid PE file, not extracting SBAT section.', file=sys.stderr)
continue
for section in pe.sections:
if pe_strip_section_name(section.Name) == '.sbat':
split = section.get_data().rstrip(b'\x00').decode().splitlines()
if not split[0].startswith('sbat,'):
print(f'{f} does not contain a valid SBAT section, skipping.', file=sys.stderr)
continue
# Filter out the sbat line, we'll add it back later, there needs to be only one and it
# needs to be first.
sbat += split[1:]
for t in input_text:
if t.startswith('@'):
t = Path(t[1:]).read_text()
split = t.splitlines()
if not split[0].startswith('sbat,'):
print(f'{t} does not contain a valid SBAT section, skipping.', file=sys.stderr)
continue
sbat += split[1:]
return '\n'.join(
(
'sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md',
*sbat,
'', # an empty line so that we end up with a newline at the end
)
)
# Keep in sync with Device from src/boot/chid.h
# uint32_t descriptor, EFI_GUID chid, uint32_t name_offset, uint32_t compatible_offset
DEVICE_STRUCT_SIZE = 4 + 16 + 4 + 4
NULL_DEVICE = b'\0' * DEVICE_STRUCT_SIZE
DEVICE_TYPE_DEVICETREE = 1
DEVICE_TYPE_UEFI_FW = 2
# Keep in sync with src/boot/efi-firmware.h
FWHEADERMAGIC = 0xFEEDDEAD
EFIFW_HEADER_SIZE = 4 + 4 + 4 + 4
def device_make_descriptor(device_type: int, size: int) -> int:
return (size) | (device_type << 28)
def pack_device(
offsets: dict[str, int], devtype: int, name: str, compatible_or_fwid: str, chids: set[uuid.UUID]
) -> bytes:
data = b''
descriptor = device_make_descriptor(devtype, DEVICE_STRUCT_SIZE)
for chid in sorted(chids):
data += struct.pack('<I', descriptor)
data += chid.bytes_le
data += struct.pack('<II', offsets[name], offsets[compatible_or_fwid])
assert len(data) == DEVICE_STRUCT_SIZE * len(chids)
return data
def pack_strings(strings: set[str], base: int) -> tuple[bytes, dict[str, int]]:
blob = b''
offsets = {}
for string in sorted(strings):
offsets[string] = base + len(blob)
blob += string.encode('utf-8') + b'\00'
return (blob, offsets)
def parse_hwid_dir(path: Path) -> bytes:
hwid_files = sorted(path.rglob('*.json'))
devstr_to_type: dict[str, int] = {
'devicetree': DEVICE_TYPE_DEVICETREE,
'uefi-fw': DEVICE_TYPE_UEFI_FW,
}
# all attributes in the mandatory attributes list must be present
mandatory_attribute = ['type', 'name', 'hwids']
# at least one of the following attributes must be present
one_of = ['compatible', 'fwid']
one_of_key_to_devtype: dict[str, int] = {
'compatible': DEVICE_TYPE_DEVICETREE,
'fwid': DEVICE_TYPE_UEFI_FW,
}
strings: set[str] = set()
devices: collections.defaultdict[tuple[int, str, str], set[uuid.UUID]] = collections.defaultdict(set)
for hwid_file in hwid_files:
data = json.loads(hwid_file.read_text(encoding='UTF-8'))
for k in mandatory_attribute:
if k not in data:
raise ValueError(f'hwid description file "{hwid_file}" does not contain "{k}"')
if not any(key in data for key in one_of):
required_keys = ','.join(one_of)
raise ValueError(f'hwid description file "{hwid_file}" must contain one of {required_keys}')
# (devtype, name, compatible/fwid) pair uniquely identifies the device
devtype = devstr_to_type[data['type']]
for k in one_of:
if k in data:
if one_of_key_to_devtype[k] != devtype:
raise ValueError(
f'wrong attribute "{k}" for hwid description file "{hwid_file}", '
'device type: "%s"' % devtype
)
strings |= {data['name'], data[k]}
devices[(devtype, data['name'], data[k])] |= {uuid.UUID(u) for u in data['hwids']}
total_device_structs = 1
for dev, uuids in devices.items():
total_device_structs += len(uuids)
strings_blob, offsets = pack_strings(strings, total_device_structs * DEVICE_STRUCT_SIZE)
devices_blob = b''
for (devtype, name, compatible_or_fwid), uuids in devices.items():
devices_blob += pack_device(offsets, devtype, name, compatible_or_fwid, uuids)
devices_blob += NULL_DEVICE
return devices_blob + strings_blob
def parse_efifw_dir(path: Path) -> bytes:
if not path.is_dir():
raise ValueError(f'{path} is not a directory or it does not exist')
# only one firmware image must be present in the directory
# to uniquely identify that firmware with its ID.
if len(list(path.glob('*'))) != 1:
raise ValueError(f'{path} must contain exactly one firmware image file')
payload_blob = b''
for fw in path.iterdir():
payload_blob += fw.read_bytes()
payload_len = len(payload_blob)
if payload_len == 0:
raise ValueError(f'{fw} is a zero byte file!')
dirname = path.parts[-1]
# firmware id is the name of the directory the firmware bundle is in,
# terminated by NULL.
fwid = b'' + dirname.encode() + b'\0'
fwid_len = len(fwid)
efifw_header_blob = b''
efifw_header_blob += struct.pack('<I', FWHEADERMAGIC)
efifw_header_blob += struct.pack('<I', EFIFW_HEADER_SIZE)
efifw_header_blob += struct.pack('<I', fwid_len)
efifw_header_blob += struct.pack('<I', payload_len)
efifw_blob = b''
efifw_blob += efifw_header_blob + fwid + payload_blob
return efifw_blob
STUB_SBAT = '''\
sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md
uki,1,UKI,uki,1,https://uapi-group.org/specifications/specs/unified_kernel_image/
'''
ADDON_SBAT = '''\
sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md
uki-addon,1,UKI Addon,addon,1,https://www.freedesktop.org/software/systemd/man/latest/systemd-stub.html
'''
def make_uki(opts: UkifyConfig) -> None:
assert opts.output is not None
# kernel payload signing
sign_args_present = opts.sb_key or opts.sb_cert_name
sign_kernel = opts.sign_kernel
linux = opts.linux
combined_sigs = '{}'
# On some distros, on some architectures, the vmlinuz is a gzip file, so we need to decompress it
# if it's not a valid PE file, as it will fail to be booted by the firmware.
if linux:
try:
pefile.PE(linux, fast_load=True)
except pefile.PEFormatError:
try:
decompressed = maybe_decompress(linux)
except NotImplementedError:
print(f'{linux} is not a valid PE file and cannot be decompressed either', file=sys.stderr)
else:
print(f'{linux} is compressed and cannot be loaded by UEFI, decompressing', file=sys.stderr)
linux_decompressed = tempfile.NamedTemporaryFile(prefix='linux-decompressed')
linux_decompressed.write(decompressed)
linux_decompressed.flush()
linux = Path(linux_decompressed.name)
if linux and sign_args_present:
assert opts.signtool is not None
signtool = SignTool.from_string(opts.signtool)
if sign_kernel is None:
# figure out if we should sign the kernel
sign_kernel = signtool.verify(linux, opts)
if sign_kernel:
linux_signed = tempfile.NamedTemporaryFile(prefix='linux-signed')
signtool.sign(os.fspath(linux), os.fspath(Path(linux_signed.name)), opts=opts)
linux = Path(linux_signed.name)
if opts.uname is None and linux is not None:
print('Kernel version not specified, starting autodetection 😖.', file=sys.stderr)
opts.uname = Uname.scrape(linux, opts=opts)
uki = UKI(opts.join_pcrsig if opts.join_pcrsig else opts.stub)
initrd = join_initrds(opts.initrd)
pcrpkey: Union[bytes, Path, None] = opts.pcrpkey
if pcrpkey is None:
keyutil_tool = find_tool('systemd-keyutil', '/usr/lib/systemd/systemd-keyutil', opts=opts)
cmd = [keyutil_tool, 'extract-public']
if opts.pcr_public_keys and len(opts.pcr_public_keys) == 1:
# If we're using an engine or provider, the public key will be an X.509 certificate.
if opts.signing_engine or opts.signing_provider:
cmd += ['--certificate', opts.pcr_public_keys[0]]
if opts.certificate_provider:
cmd += ['--certificate-source', f'provider:{opts.certificate_provider}']
print('+', shell_join(cmd), file=sys.stderr)
pcrpkey = subprocess.check_output(cmd)
else:
pcrpkey = Path(opts.pcr_public_keys[0])
elif opts.pcr_certificates and len(opts.pcr_certificates) == 1:
cmd += ['--certificate', opts.pcr_certificates[0]]
if opts.certificate_provider:
cmd += ['--certificate-source', f'provider:{opts.certificate_provider}']
print('+', shell_join(cmd), file=sys.stderr)
pcrpkey = subprocess.check_output(cmd)
elif opts.pcr_private_keys and len(opts.pcr_private_keys) == 1:
cmd += ['--private-key', Path(opts.pcr_private_keys[0])]
if opts.signing_engine:
cmd += ['--private-key-source', f'engine:{opts.signing_engine}']
if opts.signing_provider:
cmd += ['--private-key-source', f'provider:{opts.signing_provider}']
print('+', shell_join(cmd), file=sys.stderr)
pcrpkey = subprocess.check_output(cmd)
hwids = None
if opts.hwids != '':
if opts.hwids is not None:
hwids = parse_hwid_dir(Path(opts.hwids))
else:
hwids_dir = Path(f'/usr/lib/systemd/boot/hwids/{opts.efi_arch}')
if hwids_dir.is_dir():
print(f'Automatically building .hwids section from {hwids_dir}', file=sys.stderr)
hwids = parse_hwid_dir(hwids_dir)
sections = [
# name, content, measure?
('.osrel', opts.os_release, True),
('.cmdline', opts.cmdline, True),
('.dtb', opts.devicetree, True),
*(('.dtbauto', dtb, True) for dtb in opts.devicetree_auto),
('.hwids', hwids, True),
('.uname', opts.uname, True),
('.splash', opts.splash, True),
('.pcrpkey', pcrpkey, True),
('.linux', linux, True),
('.initrd', initrd, True),
*(('.efifw', parse_efifw_dir(fw), False) for fw in opts.efifw),
('.ucode', opts.microcode, True),
] # fmt: skip
# If we're building a PE profile binary, the ".profile" section has to be the first one.
if opts.profile and not opts.join_profiles:
uki.add_section(Section.create('.profile', opts.profile, measure=True))
for name, content, measure in sections:
if content:
uki.add_section(Section.create(name, content, measure=measure))
# systemd-measure doesn't know about those extra sections
for section in opts.sections:
uki.add_section(section)
# Don't add an .sbat section to profile PE binaries.
if (opts.join_profiles or not opts.profile) and not opts.pcrsig:
if linux is not None:
# Merge the .sbat sections from stub, kernel, and parameter, so
# that revocation can be done on either.
input_pes = [opts.stub, linux]
if not opts.sbat:
opts.sbat = [STUB_SBAT]
else:
# Addons don't use the stub so we add SBAT manually
input_pes = []
if not opts.sbat:
opts.sbat = [ADDON_SBAT]
uki.add_section(Section.create('.sbat', merge_sbat(input_pes, opts.sbat), measure=linux is not None))
# If we're building a UKI with additional profiles, the .profile section for the base profile has to be
# the last one so that everything before it is shared between profiles. The only thing we don't share
# between profiles is the .pcrsig section which is appended later and doesn't make sense to share.
if opts.profile and opts.join_profiles:
uki.add_section(Section.create('.profile', opts.profile, measure=True))
# PCR measurement and signing
if (
not opts.pcrsig
and (opts.join_profiles or not opts.profile)
and (
not opts.sign_profiles
or (opts.profile and read_env_file(opts.profile).get('ID') in opts.sign_profiles)
)
):
combined_sigs = call_systemd_measure(uki, opts=opts)
# UKI profiles
to_import = {
'.linux',
'.osrel',
'.cmdline',
'.initrd',
'.efifw',
'.ucode',
'.splash',
'.dtb',
'.uname',
'.sbat',
'.profile',
}
if not opts.os_release:
to_import.remove('.osrel')
for profile in opts.join_profiles:
pe = pefile.PE(profile, fast_load=True)
prev_len = len(uki.sections)
names = [pe_strip_section_name(s.Name) for s in pe.sections]
names = [n for n in names if n in to_import]
if len(names) == 0:
raise ValueError(f'Found no valid sections in PE profile binary {profile}')
if names[0] != '.profile':
raise ValueError(
f'Expected .profile section as first valid section in PE profile binary {profile} but got {names[0]}'
)
if names.count('.profile') > 1:
raise ValueError(f'Profile PE binary {profile} contains multiple .profile sections')
for pesection in pe.sections:
n = pe_strip_section_name(pesection.Name)
if n not in to_import:
continue
print(
f"Copying section '{n}' from '{profile}': {pe_section_size(pesection)} bytes",
file=sys.stderr,
)
uki.add_section(
Section.create(n, pesection.get_data(length=pe_section_size(pesection)), measure=True)
)
if opts.sign_profiles:
pesection = next(s for s in pe.sections if pe_strip_section_name(s.Name) == '.profile')
id = read_env_file(pesection.get_data(length=pe_section_size(pesection)).decode()).get('ID')
if not id or id not in opts.sign_profiles:
print(f'Not signing expected PCR measurements for "{id}" profile', file=sys.stderr)
continue
s = call_systemd_measure(uki, opts=opts, profile_start=prev_len)
if s:
combined_sigs = combine_signatures([json.loads(combined_sigs), json.loads(s)])
# UKI creation
if sign_args_present:
unsigned = tempfile.NamedTemporaryFile(prefix='uki')
unsigned_output = unsigned.name
else:
unsigned_output = opts.output
pe_add_sections(opts, uki, unsigned_output)
# UKI signing
if sign_args_present:
assert opts.signtool is not None
signtool = SignTool.from_string(opts.signtool)
signtool.sign(os.fspath(unsigned_output), os.fspath(opts.output), opts)
# We end up with no executable bits, let's reapply them
os.umask(umask := os.umask(0))
os.chmod(opts.output, 0o777 & ~umask)
print(f'Wrote {"signed" if sign_args_present else "unsigned"} {opts.output}', file=sys.stderr)
if opts.policy_digest:
print(combined_sigs)
@contextlib.contextmanager
def temporary_umask(mask: int) -> Iterator[None]:
# Drop <mask> bits from umask
old = os.umask(0)
os.umask(old | mask)
try:
yield
finally:
os.umask(old)
def generate_key_cert_pair(
common_name: str,
valid_days: int,
keylength: int = 2048,
) -> tuple[bytes, bytes]:
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
# We use a keylength of 2048 bits. That is what Microsoft documents as
# supported/expected:
# https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/windows-secure-boot-key-creation-and-management-guidance?view=windows-11#12-public-key-cryptography
now = datetime.datetime.now(datetime.timezone.utc)
key = rsa.generate_private_key(
public_exponent=65537,
key_size=keylength,
)
cert = (
x509.CertificateBuilder()
.subject_name(
x509.Name([x509.NameAttribute(x509.oid.NameOID.COMMON_NAME, common_name)]),
)
.issuer_name(
x509.Name([x509.NameAttribute(x509.oid.NameOID.COMMON_NAME, common_name)]),
)
.not_valid_before(
now,
)
.not_valid_after(
now + datetime.timedelta(days=valid_days),
)
.serial_number(
x509.random_serial_number(),
)
.public_key(
key.public_key(),
)
.add_extension(
x509.BasicConstraints(ca=False, path_length=None),
critical=True,
)
.sign(
private_key=key,
algorithm=hashes.SHA256(),
)
)
cert_pem = cert.public_bytes(
encoding=serialization.Encoding.PEM,
)
key_pem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
return key_pem, cert_pem
def generate_priv_pub_key_pair(keylength: int = 2048) -> tuple[bytes, bytes]:
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
key = rsa.generate_private_key(
public_exponent=65537,
key_size=keylength,
)
priv_key_pem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
pub_key_pem = key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
return priv_key_pem, pub_key_pem
def generate_keys(opts: UkifyConfig) -> None:
work = False
# This will generate keys and certificates and write them to the paths that
# are specified as input paths.
if opts.sb_key and opts.sb_cert:
# The length of CN must not exceed 64 bytes
if opts.sb_cert_common_name is not None:
cn = opts.sb_cert_common_name
if len(cn) == 0:
raise ValueError('--secureboot-certificate-common-name= must not be empty')
# Older cryptography versions (checked 41) don't check that by themselves.
# Newer (at least 49) do, but with a much less useful error message, so keep this extra check.
if len(cn.encode()) > COMMON_NAME_MAX_LEN:
raise ValueError(
f'--secureboot-certificate-common-name= is longer than {COMMON_NAME_MAX_LEN} bytes: {cn!r}'
)
else:
fqdn = socket.getfqdn()
cn = f'SecureBoot signing key on host {fqdn}'
if len(cn) > COMMON_NAME_MAX_LEN:
cn = cn[: COMMON_NAME_MAX_LEN - 3] + '...'
key_pem, cert_pem = generate_key_cert_pair(
common_name=cn,
valid_days=opts.sb_cert_validity,
)
print(f'Writing SecureBoot private key to {opts.sb_key}', file=sys.stderr)
with temporary_umask(0o077):
Path(opts.sb_key).write_bytes(key_pem)
print(f'Writing SecureBoot certificate to {opts.sb_cert}', file=sys.stderr)
Path(opts.sb_cert).write_bytes(cert_pem)
work = True
generated: set[tuple[Path, Optional[Path]]] = set()
for priv_key, pub_key, _, _, _ in key_path_groups(opts, include_extra=False):
pair = (Path(priv_key).resolve(), Path(pub_key).resolve() if pub_key else None)
if pair in generated:
continue
generated.add(pair)
priv_key_pem, pub_key_pem = generate_priv_pub_key_pair()
print(f'Writing private key for PCR signing to {priv_key}', file=sys.stderr)
with temporary_umask(0o077):
Path(priv_key).write_bytes(priv_key_pem)
if pub_key:
print(f'Writing public key for PCR signing to {pub_key}', file=sys.stderr)
Path(pub_key).write_bytes(pub_key_pem)
work = True
if not work:
raise ValueError(
'genkey: --secureboot-private-key=/--secureboot-certificate= or --pcr-private-key/--pcr-public-key must be specified'
)
def inspect_section(
opts: UkifyConfig,
section: pefile.SectionStructure,
name: str,
force: bool = False,
) -> Optional[dict[str, Union[int, str]]]:
# find the config for this section in opts and whether to show it ('force' is used for the
# '.profile' delimiters, which must always appear in the JSON profile structure)
config = opts.sections_by_name.get(name, None)
show = force or config or opts.all or (name in DEFAULT_SECTIONS_TO_SHOW and not opts.sections)
if not show:
return None
ttype = config.output_mode if config else DEFAULT_SECTIONS_TO_SHOW.get(name, 'binary')
size = pe_section_size(section)
data = section.get_data(length=size)
digest = sha256(data).hexdigest()
struct: dict[str, Union[int, str]] = {
'size': size,
'sha256': digest,
}
if ttype == 'text':
try:
struct['text'] = data.rstrip(b'\0').replace(b'\0', b'\\0').decode()
except UnicodeDecodeError as e:
print(f'Section {name!r} is not valid text: {e}', file=sys.stderr)
struct['text'] = '(not valid UTF-8)'
if config and config.content:
assert isinstance(config.content, Path)
config.content.write_bytes(data)
if opts.json == 'off':
print(f'{name}:\n size: {size} bytes\n sha256: {digest}')
if ttype == 'text':
text = textwrap.indent(cast(str, struct['text']).rstrip(), ' ' * 4)
print(f' text:\n{text}')
return struct
def add_section_to_profile(profile: dict[str, Any], name: str, desc: dict[str, Union[int, str]]) -> None:
# Sections in MULTI_INSTANCE_SECTIONS can occur multiple times within the same profile so always
# report them as a list even when there's a single entry. Every other section is unique within a
# profile and is reported as a plain object keyed by name. A repeat of such a section means the
# image is malformed; warn, but still report every instance by turning the entry into a list so
# nothing is dropped.
if name in MULTI_INSTANCE_SECTIONS:
profile.setdefault(name, [])
profile[name] += [desc]
elif name not in profile:
profile[name] = desc
else:
print(f'Unexpected duplicate {name!r} section, reporting all instances as a list', file=sys.stderr)
if isinstance(profile[name], list):
profile[name] += [desc]
else:
profile[name] = [profile[name], desc]
def inspect_sections(opts: UkifyConfig) -> None:
indent = 4 if opts.json == 'pretty' else None
for file in opts.files:
pe = pefile.PE(file, fast_load=True)
# A UKI is a flat list of PE sections with profile structure: the sections before the first
# '.profile' section belong to the base profile, and each subsequent '.profile' section
# introduces a further profile whose sections (up to the next '.profile') override the base
# ones. For JSON output the base profile's sections are reported by name at the root (so e.g.
# '.cmdline' refers to the base profile) and the further profiles as separate by-name objects
# in the '_profiles' list. That key is '_profiles' and not 'profiles' so it can never collide
# with a section of that name: PE section names are at most 8 bytes, so a 9-byte key is never
# a section name.
emit_json = opts.json != 'off'
base: dict[str, Any] = {}
profiles: list[dict[str, Any]] = []
profile = base # sections fill the base profile until the first '.profile' delimiter
for section in pe.sections:
name = pe_strip_section_name(section.Name)
if emit_json and name == '.profile':
profile = {}
profiles += [profile]
# The '.profile' delimiter is forced into the structure so every profile object carries
# its identity, even when --section filters out the other sections.
desc = inspect_section(opts, section, name, force=emit_json and name == '.profile')
if desc and emit_json:
add_section_to_profile(profile, name, desc)
if emit_json:
if profiles:
base['_profiles'] = profiles
json.dump(base, sys.stdout, indent=indent)
@dataclasses.dataclass(frozen=True)
class ConfigItem:
@staticmethod
def config_list_prepend(
namespace: argparse.Namespace,
group: Optional[str],
dest: str,
value: Any,
) -> None:
"Prepend value to namespace.<dest>"
assert not group
old = getattr(namespace, dest, [])
if old is None:
old = []
if not isinstance(value, list):
value = [value]
setattr(namespace, dest, value + old)
@staticmethod
def config_set_if_unset(
namespace: argparse.Namespace,
group: Optional[str],
dest: str,
value: Any,
) -> None:
"Set namespace.<dest> to value only if it was None"
assert not group
if getattr(namespace, dest) is None:
setattr(namespace, dest, value)
@staticmethod
def config_set(
namespace: argparse.Namespace,
group: Optional[str],
dest: str,
value: Any,
) -> None:
"Set namespace.<dest> to value only if it was None"
assert not group
setattr(namespace, dest, value)
@staticmethod
def config_set_group(
namespace: argparse.Namespace,
group: Optional[str],
dest: str,
value: Any,
) -> None:
"Set namespace.<dest>[idx] to value, with idx derived from group"
# pylint: disable=protected-access
if group not in namespace._groups:
namespace._groups += [group]
idx = namespace._groups.index(group)
old = getattr(namespace, dest, None)
if old is None:
old = []
setattr(
namespace,
dest,
old + ([None] * (idx - len(old))) + [value],
)
@staticmethod
def parse_boolean(s: str) -> bool:
"Parse 1/true/yes/y/t/on as true and 0/false/no/n/f/off/None as false"
s_l = s.lower()
if s_l in {'1', 'true', 'yes', 'y', 't', 'on'}:
return True
if s_l in {'0', 'false', 'no', 'n', 'f', 'off'}:
return False
raise ValueError(f'Invalid boolean literal: {s!r}')
# arguments for argparse.ArgumentParser.add_argument()
name: Union[str, tuple[str, str]]
dest: Optional[str] = None
metavar: Optional[str] = None
type: Optional[Callable[[str], Any]] = None
nargs: Optional[str] = None
action: Optional[Union[str, Callable[[str], Any], builtins.type[argparse.Action]]] = None
default: Any = None
version: Optional[str] = None
choices: Optional[tuple[str, ...]] = None
const: Optional[Any] = None
help: Optional[str] = None
# metadata for config file parsing
config_key: Optional[str] = None
config_push: Callable[[argparse.Namespace, Optional[str], str, Any], None] = config_set_if_unset
def _names(self) -> tuple[str, ...]:
return self.name if isinstance(self.name, tuple) else (self.name,)
def argparse_dest(self) -> str: