puu-installer 0.2.19

Standalone installer for bootc-based OSs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) Opinsys Oy 2026

//! Native Secure Boot signing and key enrollment, replacing the `sbctl`
//! `sign`/`verify`/`enroll-keys` subcommands.
//!
//! All cryptography goes through OpenSSL (libcrypto) so the binary depends on a
//! single, FIPS-certifiable crypto backend. The Authenticode/PKCS#7 and PE
//! container structures are assembled and parsed with the small hand-rolled DER
//! helpers below rather than a third-party ASN.1 stack.

#![allow(clippy::doc_markdown)]

use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

use anyhow::{Result, anyhow, bail};
use openssl::hash::MessageDigest;
use openssl::pkcs7::{Pkcs7, Pkcs7Flags};
use openssl::pkey::{PKey, PKeyRef, Private};
use openssl::sha::{Sha256, sha256};
use openssl::sign::Signer;
use openssl::stack::Stack;
use openssl::x509::X509;

use crate::pipeline::{TARGET_EFI_MOUNT, TARGET_MOUNT};
use crate::subproc::LogFn;

// Object identifiers, expressed as arc sequences and DER-encoded on demand.
const OID_SPC_INDIRECT_DATA: &[u32] = &[1, 3, 6, 1, 4, 1, 311, 2, 1, 4];
const OID_SPC_PE_IMAGE_DATA: &[u32] = &[1, 3, 6, 1, 4, 1, 311, 2, 1, 15];
const OID_SHA256: &[u32] = &[2, 16, 840, 1, 101, 3, 4, 2, 1];
const OID_RSA_ENCRYPTION: &[u32] = &[1, 2, 840, 113_549, 1, 1, 1];
const OID_SIGNED_DATA: &[u32] = &[1, 2, 840, 113_549, 1, 7, 2];
const OID_CONTENT_TYPE: &[u32] = &[1, 2, 840, 113_549, 1, 9, 3];
const OID_MESSAGE_DIGEST: &[u32] = &[1, 2, 840, 113_549, 1, 9, 4];

// WIN_CERTIFICATE wrapper constants.
const WIN_CERT_REVISION_2_0: u16 = 0x0200;
const WIN_CERT_TYPE_PKCS_SIGNED_DATA: u16 = 0x0002;

// Well-known "obsolete" SpcPeImageData value (as emitted by signtool/sbsign):
// SEQUENCE { flags BIT STRING {}, file [0] SpcLink:[2] { [0] BMPString "<<<Obsolete>>>" } }
const SPC_PE_IMAGE_DATA_VALUE: &[u8] = &[
    0x30, 0x25, // SEQUENCE (37 bytes)
    0x03, 0x01, 0x00, // flags BIT STRING {}
    0xa0, 0x20, // file [0] (32 bytes)
    0xa2, 0x1e, // SpcLink file [2] (30 bytes)
    0x80, 0x1c, // SpcString unicode [0] BMPString (28 bytes)
    0x00, 0x3c, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x4f, 0x00, 0x62, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x6c,
    0x00, 0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3e, 0x00, 0x3e, 0x00, 0x3e,
];

// --- Minimal DER encoding ---

/// Encode a DER definite-length prefix.
fn der_len(len: usize) -> Vec<u8> {
    if len < 0x80 {
        return vec![len as u8];
    }
    let bytes = len.to_be_bytes();
    let start = bytes
        .iter()
        .position(|&b| b != 0)
        .unwrap_or(bytes.len() - 1);
    let body = &bytes[start..];
    let mut out = Vec::with_capacity(1 + body.len());
    out.push(0x80 | body.len() as u8);
    out.extend_from_slice(body);
    out
}

/// Encode one DER TLV: `tag || length || contents`.
fn der_tlv(tag: u8, contents: &[u8]) -> Vec<u8> {
    let mut out = Vec::with_capacity(2 + contents.len());
    out.push(tag);
    out.extend_from_slice(&der_len(contents.len()));
    out.extend_from_slice(contents);
    out
}

/// SEQUENCE built from already-encoded elements.
fn der_seq(elems: &[Vec<u8>]) -> Vec<u8> {
    der_tlv(0x30, &elems.concat())
}

/// SET built from already-encoded elements. The caller orders the elements per
/// the DER SET OF rules (or passes a single element).
fn der_set(elems: &[Vec<u8>]) -> Vec<u8> {
    der_tlv(0x31, &elems.concat())
}

fn der_octet(bytes: &[u8]) -> Vec<u8> {
    der_tlv(0x04, bytes)
}

fn der_int_u8(value: u8) -> Vec<u8> {
    der_tlv(0x02, &[value])
}

fn der_null() -> Vec<u8> {
    vec![0x05, 0x00]
}

/// Encode an OBJECT IDENTIFIER from its arc values.
fn der_oid(arcs: &[u32]) -> Vec<u8> {
    let mut body = vec![(arcs[0] * 40 + arcs[1]) as u8];
    for &arc in &arcs[2..] {
        let mut group = vec![(arc & 0x7f) as u8];
        let mut rest = arc >> 7;
        while rest > 0 {
            group.push(0x80 | (rest & 0x7f) as u8);
            rest >>= 7;
        }
        group.reverse();
        body.extend_from_slice(&group);
    }
    der_tlv(0x06, &body)
}

/// `AlgorithmIdentifier { algorithm, NULL }`.
fn algorithm_identifier(oid: &[u32]) -> Vec<u8> {
    der_seq(&[der_oid(oid), der_null()])
}

// --- Minimal DER decoding ---

/// A single DER element: its tag, its contents, and the full `tag||len||value`
/// slice it occupies.
struct Tlv<'a> {
    tag: u8,
    contents: &'a [u8],
    total: &'a [u8],
}

/// Read the TLV at `data[pos..]`, returning it and the offset just past it.
fn der_read(data: &[u8], pos: usize) -> Result<(Tlv<'_>, usize)> {
    let tag = *data.get(pos).ok_or_else(|| anyhow!("truncated DER tag"))?;
    let first = *data
        .get(pos + 1)
        .ok_or_else(|| anyhow!("truncated DER length"))?;
    let (header, len) = if first < 0x80 {
        (2, first as usize)
    } else {
        let n = (first & 0x7f) as usize;
        let mut len = 0usize;
        for i in 0..n {
            let byte = *data
                .get(pos + 2 + i)
                .ok_or_else(|| anyhow!("truncated DER length"))?;
            len = (len << 8) | byte as usize;
        }
        (2 + n, len)
    };
    let start = pos + header;
    let end = start
        .checked_add(len)
        .filter(|&e| e <= data.len())
        .ok_or_else(|| anyhow!("DER element exceeds buffer"))?;
    Ok((
        Tlv {
            tag,
            contents: &data[start..end],
            total: &data[pos..end],
        },
        end,
    ))
}

/// Extract the raw `issuer` Name and `serialNumber` TLVs from a certificate's
/// DER, for use in a CMS `IssuerAndSerialNumber`.
fn issuer_and_serial(cert_der: &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
    let (cert, _) = der_read(cert_der, 0)?;
    if cert.tag != 0x30 {
        bail!("certificate is not a SEQUENCE");
    }
    let (tbs, _) = der_read(cert.contents, 0)?;
    if tbs.tag != 0x30 {
        bail!("tbsCertificate is not a SEQUENCE");
    }
    let body = tbs.contents;
    let (first, mut pos) = der_read(body, 0)?;
    // Skip the optional [0] EXPLICIT version.
    if first.tag != 0xa0 {
        pos = 0;
    }
    let (serial, after_serial) = der_read(body, pos)?;
    if serial.tag != 0x02 {
        bail!("serialNumber is not an INTEGER");
    }
    let (_signature, after_sig) = der_read(body, after_serial)?;
    let (issuer, _) = der_read(body, after_sig)?;
    if issuer.tag != 0x30 {
        bail!("issuer is not a Name SEQUENCE");
    }
    Ok((issuer.total.to_vec(), serial.total.to_vec()))
}

/// Parse the embedded Authenticode digest and signer certificates out of a
/// PKCS#7 `SignedData` ContentInfo. Returns `None` if the structure does not
/// match the expected Authenticode shape.
fn parse_authenticode(pkcs7: &[u8]) -> Option<(Vec<u8>, Vec<Vec<u8>>)> {
    let (content_info, _) = der_read(pkcs7, 0).ok()?;
    if content_info.tag != 0x30 {
        return None;
    }
    let (_content_type, after_type) = der_read(content_info.contents, 0).ok()?;
    let (content, _) = der_read(content_info.contents, after_type).ok()?;
    if content.tag != 0xa0 {
        return None;
    }
    let (signed_data, _) = der_read(content.contents, 0).ok()?;
    if signed_data.tag != 0x30 {
        return None;
    }
    let body = signed_data.contents;

    // version, digestAlgorithms, encapContentInfo, then optional [0] certs /
    // [1] crls and the signerInfos SET.
    let (_version, mut pos) = der_read(body, 0).ok()?;
    let (_digest_algs, next) = der_read(body, pos).ok()?;
    pos = next;
    let (encap, next) = der_read(body, pos).ok()?;
    pos = next;

    // encapContentInfo: eContentType OID, eContent [0] EXPLICIT.
    let (_econtent_type, e1) = der_read(encap.contents, 0).ok()?;
    let (econtent, _) = der_read(encap.contents, e1).ok()?;
    if econtent.tag != 0xa0 {
        return None;
    }
    // SpcIndirectDataContent: data SpcAttr, message_digest DigestInfo.
    let (spc, _) = der_read(econtent.contents, 0).ok()?;
    let (_spc_data, s1) = der_read(spc.contents, 0).ok()?;
    let (digest_info, _) = der_read(spc.contents, s1).ok()?;
    // DigestInfo: AlgorithmIdentifier, OCTET STRING.
    let (_alg, d1) = der_read(digest_info.contents, 0).ok()?;
    let (digest, _) = der_read(digest_info.contents, d1).ok()?;
    if digest.tag != 0x04 {
        return None;
    }

    // Remaining top-level elements: the [0] IMPLICIT certificate set holds the
    // concatenated signer certificates.
    let mut certs = Vec::new();
    while pos < body.len() {
        let (elem, next) = der_read(body, pos).ok()?;
        pos = next;
        if elem.tag == 0xa0 {
            let mut cpos = 0;
            while cpos < elem.contents.len() {
                let (cert, cnext) = der_read(elem.contents, cpos).ok()?;
                certs.push(cert.total.to_vec());
                cpos = cnext;
            }
        }
    }

    Some((digest.contents.to_vec(), certs))
}

/// Parse a PEM private key and certificate pair via OpenSSL.
fn load_key_and_cert(key_pem: &str, cert_pem: &str) -> Result<(PKey<Private>, X509)> {
    let key = PKey::private_key_from_pem(key_pem.as_bytes())?;
    let cert = X509::from_pem(cert_pem.as_bytes())?;
    Ok((key, cert))
}

/// Build the Authenticode PKCS#7 SignedData blob for a precomputed PE digest.
/// The ASN.1 is hand-encoded; the RSA-SHA256 signature over the signed
/// attributes is produced by OpenSSL.
fn build_pkcs7(digest: &[u8], key: &PKeyRef<Private>, cert: &X509) -> Result<Vec<u8>> {
    let cert_der = cert.to_der()?;
    let (issuer, serial) = issuer_and_serial(&cert_der)?;

    // SpcIndirectDataContent { SpcAttributeTypeAndOptionalValue, DigestInfo }.
    let spc_attr = der_seq(&[
        der_oid(OID_SPC_PE_IMAGE_DATA),
        SPC_PE_IMAGE_DATA_VALUE.to_vec(),
    ]);
    let spc_digest = der_seq(&[algorithm_identifier(OID_SHA256), der_octet(digest)]);
    let spc = der_seq(&[spc_attr, spc_digest]);

    // Signed attributes: content-type = SPC_INDIRECT_DATA, message-digest = SHA-256(spc).
    let content_digest = sha256(&spc);
    let ct_attr = der_seq(&[
        der_oid(OID_CONTENT_TYPE),
        der_set(&[der_oid(OID_SPC_INDIRECT_DATA)]),
    ]);
    let md_attr = der_seq(&[
        der_oid(OID_MESSAGE_DIGEST),
        der_set(&[der_octet(&content_digest)]),
    ]);
    // DER SET OF requires the elements to be sorted by their encoding.
    let mut attr_elems = [ct_attr, md_attr];
    attr_elems.sort_unstable();
    let attrs_contents = attr_elems.concat();

    // RSA-SHA256 signature over the DER SET OF signed attributes (tag 0x31).
    let signed_attrs_to_sign = der_tlv(0x31, &attrs_contents);
    let mut signer = Signer::new(MessageDigest::sha256(), key)?;
    signer.update(&signed_attrs_to_sign)?;
    let signature = signer.sign_to_vec()?;

    // SignerInfo. signedAttrs appears as [0] IMPLICIT inside the structure.
    let issuer_and_serial = der_seq(&[issuer, serial]);
    let signed_attrs_implicit = der_tlv(0xa0, &attrs_contents);
    let signer_info = der_seq(&[
        der_int_u8(1), // version
        issuer_and_serial,
        algorithm_identifier(OID_SHA256),
        signed_attrs_implicit,
        algorithm_identifier(OID_RSA_ENCRYPTION),
        der_octet(&signature),
    ]);

    // SignedData.
    let econtent = der_tlv(0xa0, &spc); // [0] EXPLICIT SpcIndirectDataContent
    let encap = der_seq(&[der_oid(OID_SPC_INDIRECT_DATA), econtent]);
    let certificates = der_tlv(0xa0, &cert_der); // [0] IMPLICIT SET OF Certificate
    let signed_data = der_seq(&[
        der_int_u8(1), // version
        der_set(&[algorithm_identifier(OID_SHA256)]),
        encap,
        certificates,
        der_set(&[signer_info]),
    ]);

    // ContentInfo { signedData, [0] EXPLICIT SignedData }.
    let content = der_tlv(0xa0, &signed_data);
    Ok(der_seq(&[der_oid(OID_SIGNED_DATA), content]))
}

fn read_u32_le(data: &[u8], off: usize) -> Result<u32> {
    let bytes: [u8; 4] = data
        .get(off..off + 4)
        .ok_or_else(|| anyhow!("truncated PE at offset {off:#x}"))?
        .try_into()
        .map_err(|_| anyhow!("slice length mismatch reading PE"))?;
    Ok(u32::from_le_bytes(bytes))
}

fn read_u16_le(data: &[u8], off: usize) -> Result<u16> {
    let bytes: [u8; 2] = data
        .get(off..off + 2)
        .ok_or_else(|| anyhow!("truncated PE at offset {off:#x}"))?
        .try_into()
        .map_err(|_| anyhow!("slice length mismatch reading PE"))?;
    Ok(u16::from_le_bytes(bytes))
}

/// Offset of the PE optional header: PE signature (4) + COFF file header (20)
/// after `e_lfanew`.
fn optional_header_offset(data: &[u8]) -> Result<usize> {
    Ok(read_u32_le(data, 0x3c)? as usize + 24)
}

/// Offset of the IMAGE_DATA_DIRECTORY for the certificate table (index 4)
/// within the PE optional header.
fn security_dir_offset(data: &[u8]) -> Result<usize> {
    let opt = optional_header_offset(data)?;
    let magic = read_u16_le(data, opt)?;
    // NumberOfRvaAndSizes lives at a magic-dependent offset; data directories
    // start right after it. PE32: dirs at opt+96; PE32+: opt+112.
    let dir_start = match magic {
        0x10b => opt + 96,  // PE32
        0x20b => opt + 112, // PE32+
        other => bail!("unsupported PE optional header magic {other:#x}"),
    };
    Ok(dir_start + 4 * 8) // certificate table = directory entry 4
}

/// Compute the Authenticode SHA-256 digest of a PE image, hashing everything
/// except the optional-header checksum, the certificate-table data directory
/// entry, and the attribute certificate table itself.
fn authenticode_sha256(image: &[u8]) -> Result<[u8; 32]> {
    let e_lfanew = read_u32_le(image, 0x3c)? as usize;
    let opt = optional_header_offset(image)?;
    let check_sum = opt + 64;
    let after_check_sum = check_sum + 4;
    let sec_dir = security_dir_offset(image)?;
    let after_sec_dir = sec_dir + 8;
    let size_of_headers = read_u32_le(image, opt + 60)? as usize;

    let slice = |start: usize, end: usize| -> Result<&[u8]> {
        image
            .get(start..end)
            .ok_or_else(|| anyhow!("PE region {start:#x}..{end:#x} out of bounds"))
    };

    let mut hasher = Sha256::new();
    hasher.update(slice(0, check_sum)?);
    hasher.update(slice(after_check_sum, sec_dir)?);
    hasher.update(slice(after_sec_dir, size_of_headers)?);

    // Section raw data, in ascending PointerToRawData order.
    let num_sections = read_u16_le(image, e_lfanew + 6)? as usize;
    let size_of_optional_header = read_u16_le(image, e_lfanew + 20)? as usize;
    let section_table = opt + size_of_optional_header;
    let mut ranges = Vec::with_capacity(num_sections);
    for i in 0..num_sections {
        let header = section_table + i * 40;
        let ptr = read_u32_le(image, header + 20)? as usize;
        let size = read_u32_le(image, header + 16)? as usize;
        ranges.push((ptr, ptr + size));
    }
    ranges.sort_unstable_by_key(|r| r.0);

    let mut bytes_hashed = size_of_headers;
    for (start, end) in ranges {
        hasher.update(slice(start, end)?);
        bytes_hashed += end - start;
    }

    // Trailing data after the sections, minus the attribute certificate table.
    let cert_table_size = read_u32_le(image, sec_dir + 4)? as usize;
    let extra = image
        .len()
        .checked_sub(bytes_hashed)
        .and_then(|n| n.checked_sub(cert_table_size))
        .ok_or_else(|| anyhow!("invalid PE layout while hashing trailing data"))?;
    hasher.update(slice(bytes_hashed, bytes_hashed + extra)?);

    Ok(hasher.finish())
}

/// Iterate the PKCS#7 entries of the PE attribute certificate table.
fn attribute_certificates(image: &[u8]) -> Result<Vec<&[u8]>> {
    let sec_dir = security_dir_offset(image)?;
    let table_off = read_u32_le(image, sec_dir)? as usize;
    let table_size = read_u32_le(image, sec_dir + 4)? as usize;
    if table_size == 0 {
        return Ok(Vec::new());
    }
    let table_end = table_off + table_size;
    let mut out = Vec::new();
    let mut pos = table_off;
    while pos + 8 <= table_end {
        let cert_size = read_u32_le(image, pos)? as usize;
        let revision = read_u16_le(image, pos + 4)?;
        let cert_type = read_u16_le(image, pos + 6)?;
        if cert_size < 8 {
            break;
        }
        let data = image
            .get(pos + 8..pos + cert_size)
            .ok_or_else(|| anyhow!("certificate entry exceeds table"))?;
        if revision == WIN_CERT_REVISION_2_0 && cert_type == WIN_CERT_TYPE_PKCS_SIGNED_DATA {
            out.push(data);
        }
        pos += (cert_size + 7) & !7; // 8-byte aligned entries
    }
    Ok(out)
}

/// Authenticode-sign the PE image bytes, returning the signed image.
pub fn sign_pe(image: &[u8], key_pem: &str, cert_pem: &str) -> Result<Vec<u8>> {
    let (key, cert) = load_key_and_cert(key_pem, cert_pem)?;

    let digest = authenticode_sha256(image)?;
    let pkcs7 = build_pkcs7(&digest, &key, &cert)?;

    // WIN_CERTIFICATE: dwLength (incl. 8-byte header) | wRevision | wType | data,
    // padded so the whole entry is 8-byte aligned.
    let cert_len = 8 + pkcs7.len();
    let mut entry = Vec::with_capacity((cert_len + 7) & !7);
    entry.extend_from_slice(&(cert_len as u32).to_le_bytes());
    entry.extend_from_slice(&WIN_CERT_REVISION_2_0.to_le_bytes());
    entry.extend_from_slice(&WIN_CERT_TYPE_PKCS_SIGNED_DATA.to_le_bytes());
    entry.extend_from_slice(&pkcs7);
    while entry.len() % 8 != 0 {
        entry.push(0);
    }

    // The certificate table is appended at the (8-aligned) end of the image.
    let mut out = image.to_vec();
    while out.len() % 8 != 0 {
        out.push(0);
    }
    let cert_offset = out.len();
    out.extend_from_slice(&entry);

    // Point the security data directory at the appended certificate table.
    let dir = security_dir_offset(image)?;
    out[dir..dir + 4].copy_from_slice(&(cert_offset as u32).to_le_bytes());
    out[dir + 4..dir + 8].copy_from_slice(&(entry.len() as u32).to_le_bytes());

    Ok(out)
}

/// Verify that the PE image carries an Authenticode signature whose digest
/// matches the image and whose signer matches `cert_pem`.
pub fn verify_pe(image: &[u8], cert_pem: &str) -> Result<bool> {
    let Ok(cert) = X509::from_pem(cert_pem.as_bytes()) else {
        return Ok(false);
    };
    let cert_der = cert.to_der()?;
    let expected = authenticode_sha256(image)?;

    for pkcs7 in attribute_certificates(image)? {
        let Some((digest, certs)) = parse_authenticode(pkcs7) else {
            continue;
        };
        if digest != expected {
            continue;
        }
        if certs.iter().any(|c| c == &cert_der) {
            return Ok(true);
        }
    }
    Ok(false)
}

// --- Key enrollment (replaces `sbctl enroll-keys`) ---

// GUIDs in on-disk (mixed-endian) byte order.
const EFI_CERT_X509_GUID: [u8; 16] = [
    0xa1, 0x59, 0xc0, 0xa5, 0xe4, 0x94, 0xa7, 0x4a, 0x87, 0xb5, 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72,
];
const EFI_CERT_TYPE_PKCS7_GUID: [u8; 16] = [
    0x9d, 0xd2, 0xaf, 0x4a, 0xdf, 0x68, 0xee, 0x49, 0x8a, 0xa9, 0x34, 0x7d, 0x37, 0x56, 0x65, 0xa7,
];
const EFI_GLOBAL_VARIABLE_GUID: [u8; 16] = [
    0x61, 0xdf, 0xe4, 0x8b, 0xca, 0x93, 0xd2, 0x11, 0xaa, 0x0d, 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c,
];
const EFI_IMAGE_SECURITY_DATABASE_GUID: [u8; 16] = [
    0xcb, 0xb2, 0x19, 0xd7, 0x3a, 0x3d, 0x96, 0x45, 0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f,
];

// NV | BS | RT | TIME_BASED_AUTHENTICATED_WRITE_ACCESS
const AUTH_VAR_ATTRS: u32 = 0x1 | 0x2 | 0x4 | 0x20;
const WIN_CERT_TYPE_EFI_GUID: u16 = 0x0ef1;
const EFIVARS_DIR: &str = "/sys/firmware/efi/efivars";

/// EFI_SIGNATURE_LIST holding a single X.509 certificate.
fn signature_list(cert_der: &[u8], owner_guid: &[u8; 16]) -> Vec<u8> {
    let signature_size = 16 + cert_der.len();
    let list_size = 28 + signature_size;
    let mut out = Vec::with_capacity(list_size);
    out.extend_from_slice(&EFI_CERT_X509_GUID);
    out.extend_from_slice(&(list_size as u32).to_le_bytes());
    out.extend_from_slice(&0u32.to_le_bytes()); // SignatureHeaderSize
    out.extend_from_slice(&(signature_size as u32).to_le_bytes());
    out.extend_from_slice(owner_guid);
    out.extend_from_slice(cert_der);
    out
}

/// A zero EFI_TIME; valid as the first timestamp when enrolling in Setup Mode.
fn efi_time_zero() -> [u8; 16] {
    [0u8; 16]
}

/// VariableName (CHAR16, no NUL) || VendorGuid || Attributes || TimeStamp || Data.
fn auth_message(name: &str, vendor_guid: &[u8; 16], data: &[u8]) -> Vec<u8> {
    let mut msg = Vec::new();
    for unit in name.encode_utf16() {
        msg.extend_from_slice(&unit.to_le_bytes());
    }
    msg.extend_from_slice(vendor_guid);
    msg.extend_from_slice(&AUTH_VAR_ATTRS.to_le_bytes());
    msg.extend_from_slice(&efi_time_zero());
    msg.extend_from_slice(data);
    msg
}

/// Detached PKCS#7 SignedData over `message` (pkcs7-data content type), produced
/// by OpenSSL — the canonical approach used by efitools for authenticated vars.
fn sign_detached_pkcs7(
    message: &[u8],
    signer_key: &PKeyRef<Private>,
    signer_cert: &X509,
) -> Result<Vec<u8>> {
    let certs = Stack::new()?;
    let pkcs7 = Pkcs7::sign(
        signer_cert,
        signer_key,
        &certs,
        message,
        Pkcs7Flags::DETACHED | Pkcs7Flags::BINARY | Pkcs7Flags::NOATTR,
    )?;
    Ok(pkcs7.to_der()?)
}

/// Build the full authenticated-variable payload (EFI_VARIABLE_AUTHENTICATION_2
/// descriptor followed by the EFI_SIGNATURE_LIST).
fn authenticated_payload(
    name: &str,
    vendor_guid: &[u8; 16],
    esl: &[u8],
    signer_key: &PKeyRef<Private>,
    signer_cert: &X509,
) -> Result<Vec<u8>> {
    let message = auth_message(name, vendor_guid, esl);
    let pkcs7 = sign_detached_pkcs7(&message, signer_key, signer_cert)?;

    let mut out = Vec::new();
    out.extend_from_slice(&efi_time_zero()); // EFI_TIME timestamp
    // WIN_CERTIFICATE_UEFI_GUID header.
    let win_cert_len = 8 + 16 + pkcs7.len();
    out.extend_from_slice(&(win_cert_len as u32).to_le_bytes()); // dwLength
    out.extend_from_slice(&WIN_CERT_REVISION_2_0.to_le_bytes());
    out.extend_from_slice(&WIN_CERT_TYPE_EFI_GUID.to_le_bytes());
    out.extend_from_slice(&EFI_CERT_TYPE_PKCS7_GUID);
    out.extend_from_slice(&pkcs7);
    out.extend_from_slice(esl);
    Ok(out)
}

/// Write an authenticated EFI variable via efivarfs.
fn write_auth_variable(name: &str, vendor_guid: &[u8; 16], payload: &[u8]) -> Result<()> {
    use std::io::Write;

    let guid_str = format!(
        "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
        vendor_guid[3],
        vendor_guid[2],
        vendor_guid[1],
        vendor_guid[0],
        vendor_guid[5],
        vendor_guid[4],
        vendor_guid[7],
        vendor_guid[6],
        vendor_guid[8],
        vendor_guid[9],
        vendor_guid[10],
        vendor_guid[11],
        vendor_guid[12],
        vendor_guid[13],
        vendor_guid[14],
        vendor_guid[15],
    );
    let path = format!("{EFIVARS_DIR}/{name}-{guid_str}");

    // efivarfs expects one write of: attributes(u32 LE) || payload.
    let mut buf = Vec::with_capacity(4 + payload.len());
    buf.extend_from_slice(&AUTH_VAR_ATTRS.to_le_bytes());
    buf.extend_from_slice(payload);

    let mut file = std::fs::OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(false)
        .open(&path)
        .map_err(|e| anyhow!("failed to open {path}: {e}"))?;
    file.write_all(&buf)
        .map_err(|e| anyhow!("failed to write {path}: {e}"))?;
    Ok(())
}

/// Enroll PK/KEK/db from a sbctl-style keydir. Enrolls db and KEK first, then
/// PK last (enrolling PK leaves Setup Mode). Replaces `sbctl enroll-keys`.
pub fn enroll_keys(keydir: &std::path::Path, owner_guid: &[u8; 16]) -> Result<()> {
    let read_pair = |kind: &str| -> Result<(PKey<Private>, X509)> {
        let dir = keydir.join(kind);
        let key_pem = std::fs::read_to_string(dir.join(format!("{kind}.key")))
            .map_err(|e| anyhow!("failed to read {kind} key: {e}"))?;
        let cert_pem = std::fs::read_to_string(dir.join(format!("{kind}.pem")))
            .map_err(|e| anyhow!("failed to read {kind} certificate: {e}"))?;
        load_key_and_cert(&key_pem, &cert_pem)
    };

    let (pk_key, pk_cert) = read_pair("PK")?;
    let (kek_key, kek_cert) = read_pair("KEK")?;
    let (_db_key, db_cert) = read_pair("db")?;

    let cert_der = |c: &X509| -> Result<Vec<u8>> { Ok(c.to_der()?) };

    // db: payload db cert, signed by KEK, stored under the image security db GUID.
    let esl = signature_list(&cert_der(&db_cert)?, owner_guid);
    let payload = authenticated_payload(
        "db",
        &EFI_IMAGE_SECURITY_DATABASE_GUID,
        &esl,
        &kek_key,
        &kek_cert,
    )?;
    write_auth_variable("db", &EFI_IMAGE_SECURITY_DATABASE_GUID, &payload)?;

    // KEK: payload KEK cert, signed by PK.
    let esl = signature_list(&cert_der(&kek_cert)?, owner_guid);
    let payload = authenticated_payload("KEK", &EFI_GLOBAL_VARIABLE_GUID, &esl, &pk_key, &pk_cert)?;
    write_auth_variable("KEK", &EFI_GLOBAL_VARIABLE_GUID, &payload)?;

    // PK: payload PK cert, self-signed; enrolling this leaves Setup Mode.
    let esl = signature_list(&cert_der(&pk_cert)?, owner_guid);
    let payload = authenticated_payload("PK", &EFI_GLOBAL_VARIABLE_GUID, &esl, &pk_key, &pk_cert)?;
    write_auth_variable("PK", &EFI_GLOBAL_VARIABLE_GUID, &payload)?;

    Ok(())
}

// --- On-target key material (replaces `sbctl create-keys` and key layout) ---

const SBCTL_KEY_PATHS: &[&str] = &[
    "var/lib/sbctl/keys/PK/PK.key",
    "var/lib/sbctl/keys/KEK/KEK.key",
    "var/lib/sbctl/keys/db/db.key",
];

fn target_sbctl_keys_exist() -> bool {
    let target = Path::new(TARGET_MOUNT);
    SBCTL_KEY_PATHS.iter().all(|p| target.join(p).is_file())
}

fn target_sbctl_dir() -> PathBuf {
    let path = Path::new(TARGET_MOUNT).join("var/lib/sbctl");
    let _ = std::fs::create_dir_all(&path);
    path
}

/// Directory holding the generated PK/KEK/db key pairs.
pub fn sbctl_keydir() -> PathBuf {
    target_sbctl_dir().join("keys")
}

/// Signature owner GUID (from the generated GUID file) in EFI byte order.
pub fn sbctl_owner_guid() -> [u8; 16] {
    let contents = std::fs::read_to_string(target_sbctl_dir().join("GUID")).unwrap_or_default();
    uuid::Uuid::parse_str(contents.trim())
        .unwrap_or(uuid::Uuid::nil())
        .to_bytes_le()
}

pub fn target_efi_binaries() -> Vec<PathBuf> {
    let esp = Path::new(TARGET_EFI_MOUNT);
    let mut paths: Vec<PathBuf> = Vec::new();
    for pattern in &["*.efi", "*.EFI"] {
        for entry in walkdir_like(esp, pattern) {
            if entry.is_file() {
                paths.push(entry);
            }
        }
    }
    paths.sort();
    paths
}

fn walkdir_like(root: &Path, glob: &str) -> Vec<PathBuf> {
    let mut results = Vec::new();
    let ext_lower = glob.trim_start_matches('*').to_lowercase();
    #[allow(clippy::items_after_statements)]
    fn recurse(dir: &Path, ext: &str, results: &mut Vec<PathBuf>) {
        if let Ok(entries) = std::fs::read_dir(dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_dir() {
                    recurse(&path, ext, results);
                } else if path
                    .extension()
                    .and_then(|e| e.to_str())
                    .is_some_and(|e| e.to_lowercase() == ext)
                {
                    results.push(path);
                }
            }
        }
    }
    recurse(root, &ext_lower, &mut results);
    results
}

fn random_uuid() -> Result<String> {
    let mut buf = [0u8; 16];
    openssl::rand::rand_bytes(&mut buf)?;
    Ok(uuid::Builder::from_random_bytes(buf)
        .into_uuid()
        .to_string())
}

/// Generate an RSA-2048 key and a self-signed X.509 certificate for a Secure
/// Boot key (PK/KEK/db), returning their PEM encodings. Replaces the keygen
/// side of `sbctl create-keys`.
pub(crate) fn generate_sb_key(common_name: &str) -> Result<(String, String)> {
    use openssl::asn1::Asn1Time;
    use openssl::bn::{BigNum, MsbOption};
    use openssl::rsa::Rsa;
    use openssl::x509::X509NameBuilder;

    let rsa = Rsa::generate(2048)?;
    let pkey = PKey::from_rsa(rsa)?;

    let mut name = X509NameBuilder::new()?;
    name.append_entry_by_text("CN", common_name)?;
    let name = name.build();

    let mut builder = X509::builder()?;
    builder.set_version(2)?; // X.509 v3
    let mut serial = BigNum::new()?;
    serial.rand(159, MsbOption::MAYBE_ZERO, false)?;
    let serial = serial.to_asn1_integer()?;
    builder.set_serial_number(&serial)?;
    builder.set_subject_name(&name)?;
    builder.set_issuer_name(&name)?;
    builder.set_pubkey(&pkey)?;
    let not_before = Asn1Time::days_from_now(0)?;
    let not_after = Asn1Time::days_from_now(3650)?;
    builder.set_not_before(&not_before)?;
    builder.set_not_after(&not_after)?;
    builder.sign(&pkey, MessageDigest::sha256())?;
    let cert = builder.build();

    let key_pem = String::from_utf8(pkey.private_key_to_pem_pkcs8()?)?;
    let cert_pem = String::from_utf8(cert.to_pem()?)?;
    Ok((key_pem, cert_pem))
}

pub fn ensure_sbctl_keys(log: &mut LogFn) -> Result<()> {
    if target_sbctl_keys_exist() {
        log("Secure Boot signing keys already exist; reusing them".to_string());
        return Ok(());
    }
    let keydir = target_sbctl_dir().join("keys");
    for kind in ["PK", "KEK", "db"] {
        let dir = keydir.join(kind);
        std::fs::create_dir_all(&dir)?;
        let (key_pem, cert_pem) = generate_sb_key(kind)?;
        let key_path = dir.join(format!("{kind}.key"));
        std::fs::write(&key_path, key_pem)?;
        std::fs::set_permissions(&key_path, PermissionsExt::from_mode(0o600))?;
        std::fs::write(dir.join(format!("{kind}.pem")), cert_pem)?;
    }
    let guid_path = target_sbctl_dir().join("GUID");
    if !guid_path.exists() {
        std::fs::write(&guid_path, format!("{}\n", random_uuid()?))?;
    }
    log("generated Secure Boot PK/KEK/db keys".to_string());
    Ok(())
}