examples/sign_and_boot.py
| 1 | """Example: manufacturer signs firmware, appliance boots and accepts it. |
| 2 | |
| 3 | Flow: |
| 4 | 1. Manufacturer creates a signing identity and signs the firmware image. |
| 5 | 2. Appliance owner pre-loads the manufacturer public key into the KeyRing. |
| 6 | 3. On boot, the appliance runs FirmwareVerifier against actual image bytes |
| 7 | and the key-ring. |
| 8 | 4. Measured-boot chain extends through 4 stages (bootloader, kernel, initrd, |
| 9 | userspace) producing a final PCR value. |
| 10 | 5. Audit log records the accept decision with the final PCR. |
| 11 | """ |
| 12 | |
| 13 | from __future__ import annotations |
| 14 | |
| 15 | from quantumshield.identity.agent import AgentIdentity |
| 16 | |
| 17 | from pqc_bootloader import ( |
| 18 | BootAttestationLog, |
| 19 | BootStage, |
| 20 | FirmwareImage, |
| 21 | FirmwareMetadata, |
| 22 | FirmwareSigner, |
| 23 | FirmwareVerifier, |
| 24 | KeyRing, |
| 25 | MeasuredBoot, |
| 26 | TargetDevice, |
| 27 | ) |
| 28 | |
| 29 | |
| 30 | def main() -> None: |
| 31 | # --- manufacturer side ----------------------------------------------- |
| 32 | manufacturer = AgentIdentity.create("acme-appliance-vendor") |
| 33 | signer = FirmwareSigner(manufacturer) |
| 34 | |
| 35 | image_bytes = b"\x7fELF" + b"payload bytes for inference OS" * 32 |
| 36 | metadata = FirmwareMetadata( |
| 37 | name="acme-inference-os", |
| 38 | version="1.2.3", |
| 39 | target=TargetDevice.AI_INFERENCE_APPLIANCE, |
| 40 | kernel_version="6.6.12", |
| 41 | architecture="x86_64", |
| 42 | build_id="ci-2026-04-20-a1b2c3", |
| 43 | ) |
| 44 | firmware = FirmwareImage.from_bytes(metadata, image_bytes) |
| 45 | signed = signer.sign(firmware) |
| 46 | print(f"[factory] signed firmware {firmware.metadata.name} v{firmware.metadata.version}") |
| 47 | print(f"[factory] image hash = {firmware.image_hash[:24]}...") |
| 48 | print(f"[factory] key-id = {signed.manufacturer_key_id[:24]}...") |
| 49 | print(f"[factory] algorithm = {signed.algorithm}") |
| 50 | |
| 51 | # --- appliance side -------------------------------------------------- |
| 52 | key_ring = KeyRing() |
| 53 | key_ring.add( |
| 54 | public_key_hex=manufacturer.signing_keypair.public_key.hex(), |
| 55 | algorithm=manufacturer.signing_keypair.algorithm.value, |
| 56 | manufacturer="Acme Appliances Inc.", |
| 57 | ) |
| 58 | print(f"[appliance] key-ring trusts {len(key_ring)} manufacturer key(s)") |
| 59 | |
| 60 | result = FirmwareVerifier.verify( |
| 61 | signed, |
| 62 | actual_bytes=image_bytes, |
| 63 | key_ring=key_ring, |
| 64 | ) |
| 65 | print( |
| 66 | f"[appliance] verify: valid={result.valid} " |
| 67 | f"signature={result.signature_valid} " |
| 68 | f"hash={result.hash_consistent} trusted={result.key_trusted}" |
| 69 | ) |
| 70 | |
| 71 | if not result.valid: |
| 72 | print(f"[appliance] REJECT: {result.error}") |
| 73 | return |
| 74 | |
| 75 | # --- measured boot --------------------------------------------------- |
| 76 | mb = MeasuredBoot() |
| 77 | mb.extend(BootStage.BOOTLOADER, b"bootloader-image-bytes") |
| 78 | mb.extend(BootStage.KERNEL, b"kernel-image-bytes") |
| 79 | mb.extend(BootStage.INITRD, b"initrd-image-bytes") |
| 80 | mb.extend(BootStage.USERSPACE, b"userspace-image-bytes") |
| 81 | print(f"[appliance] measured boot final PCR = {mb.pcr_value[:24]}...") |
| 82 | for m in mb.measurements: |
| 83 | print(f"[appliance] stage={m.stage.value:10s} hash={m.measured_hash[:16]}...") |
| 84 | |
| 85 | # --- audit log ------------------------------------------------------- |
| 86 | log = BootAttestationLog() |
| 87 | log.log_accept( |
| 88 | firmware_name=signed.firmware.metadata.name, |
| 89 | firmware_version=signed.firmware.metadata.version, |
| 90 | firmware_hash=signed.firmware.image_hash, |
| 91 | device_id="device-0001", |
| 92 | pcr_value_after=mb.pcr_value, |
| 93 | ) |
| 94 | print(f"[audit] accepts={len(log.entries(decision='accept'))}") |
| 95 | |
| 96 | |
| 97 | if __name__ == "__main__": |
| 98 | main() |
| 99 | |