vmgstool/
main.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#![expect(missing_docs)]
5
6mod storage_backend;
7mod uefi_nvram;
8mod vmgs_json;
9
10use anyhow::Result;
11use clap::Args;
12use clap::Parser;
13use disk_backend::Disk;
14use disk_vhd1::Vhd1Disk;
15use fs_err::File;
16use pal_async::DefaultPool;
17use std::io::prelude::*;
18use std::path::Path;
19use std::path::PathBuf;
20use thiserror::Error;
21use uefi_nvram::UefiNvramOperation;
22use vmgs::Error as VmgsError;
23use vmgs::Vmgs;
24use vmgs::vmgs_helpers::get_active_header;
25use vmgs::vmgs_helpers::read_headers;
26use vmgs::vmgs_helpers::validate_header;
27use vmgs_format::EncryptionAlgorithm;
28use vmgs_format::FileId;
29use vmgs_format::VMGS_BYTES_PER_BLOCK;
30use vmgs_format::VMGS_DEFAULT_CAPACITY;
31use vmgs_format::VMGS_ENCRYPTION_KEY_SIZE;
32use vmgs_format::VmgsHeader;
33
34const ONE_MEGA_BYTE: u64 = 1024 * 1024;
35const ONE_GIGA_BYTE: u64 = ONE_MEGA_BYTE * 1024;
36
37#[derive(Debug, Error)]
38enum Error {
39    #[error("VMGS file IO")]
40    VmgsFile(#[source] std::io::Error),
41    #[error("VHD file error")]
42    Vhd1(#[source] disk_vhd1::OpenError),
43    #[error("invalid disk")]
44    InvalidDisk(#[source] disk_backend::InvalidDisk),
45    #[error("VMGS format")]
46    Vmgs(#[from] vmgs::Error),
47    #[error("VMGS file already exists")]
48    FileExists,
49    #[cfg(with_encryption)]
50    #[error("Adding encryption key")]
51    EncryptionKey(#[source] vmgs::Error),
52    #[error("Data file / STDOUT IO")]
53    DataFile(#[source] std::io::Error),
54    #[error("The VMGS file has zero size")]
55    ZeroSize,
56    #[error("The VMGS file has a non zero size but the contents are empty")]
57    EmptyFile,
58    #[error("Invalid VMGS file size: {0} {1}")]
59    InvalidVmgsFileSize(u64, String),
60    #[error("Key file IO")]
61    KeyFile(#[source] std::io::Error),
62    #[error("Key must be {0} bytes long, is {1} bytes instead")]
63    InvalidKeySize(u64, u64),
64    #[error("File is not encrypted")]
65    NotEncrypted,
66    #[error("File is VMGSv1 format")]
67    V1Format,
68    #[error("VmgsStorageBackend")]
69    VmgsStorageBackend(#[from] storage_backend::EncryptionNotSupported),
70    #[error("NVRAM storage")]
71    NvramStorage(#[from] uefi_nvram_storage::NvramStorageError),
72    #[error("UEFI NVRAM variable parsing")]
73    NvramParsing(#[from] uefi_nvram_specvars::ParseError),
74    #[error("NVRAM entry not found: {0}")]
75    MissingNvramEntry(ucs2::Ucs2LeVec),
76    #[error("GUID parsing")]
77    Guid(#[from] guid::ParseError),
78    #[error("JSON parsing")]
79    SerdeJson(#[from] serde_json::Error),
80    #[error("Bad JSON contents: {0}")]
81    Json(String),
82    #[error("File ID {0:?} already exists. Use `--allow-overwrite` to ignore.")]
83    FileIdExists(FileId),
84    #[error("VMGS file is encrypted using GspById")]
85    GspByIdEncryption,
86    #[error("VMGS file is encrypted using an unknown encryption scheme")]
87    GspUnknown,
88    #[error("VMGS file is using an unknown encryption algorithm")]
89    EncryptionUnknown,
90}
91
92/// Automation requires certain exit codes to be guaranteed
93/// main matches Error enum to ExitCode
94///
95/// query-encryption must return ErrorNotEncrypted if file is not encrypted
96/// dump-headers must return ErrorEmpty when file is blank
97/// query-size must return ErrorNotFound when file id is uninitialized
98/// ExitCode::Error returned for all other errors
99#[derive(Debug, Clone, Copy)]
100#[repr(i32)]
101enum ExitCode {
102    Error = 1,
103    ErrorNotEncrypted = 2,
104    ErrorEmpty = 3,
105    ErrorNotFound = 4,
106    ErrorV1 = 5,
107    ErrorGspById = 6,
108}
109
110#[derive(Debug, Clone, Copy)]
111enum VmgsEncryptionScheme {
112    GspKey,
113    GspById,
114    None,
115}
116
117#[derive(Args)]
118struct FilePathArg {
119    /// VMGS file path
120    #[clap(short = 'f', long, alias = "filepath")]
121    file_path: PathBuf,
122}
123
124#[derive(Args)]
125struct KeyPathArg {
126    /// Encryption key file path. The file must contain a key that is 32 bytes long.
127    #[clap(short = 'k', long, alias = "keypath")]
128    key_path: Option<PathBuf>,
129}
130
131#[derive(Args)]
132struct FileIdArg {
133    /// VMGS File ID
134    #[clap(short = 'i', long, alias = "fileid", value_parser = parse_file_id)]
135    file_id: FileId,
136}
137
138#[derive(Parser)]
139#[clap(name = "vmgstool", about = "Tool to interact with VMGS files.")]
140enum Options {
141    /// Create and initialize `filepath` as a VMGS file of size `filesize`.
142    ///
143    /// `keypath` and `encryptionalgorithm` must both be specified if encrypted
144    /// guest state is required.
145    Create {
146        #[command(flatten)]
147        file_path: FilePathArg,
148        /// VMGS file size, default = 4194816 (~4MB)
149        #[clap(short = 's', long, alias = "filesize")]
150        file_size: Option<u64>,
151        /// Encryption key file path. The file must contain a key that is 32 bytes long.
152        ///
153        /// `encryptionalgorithm` must also be specified when using this flag.
154        #[clap(
155            short = 'k',
156            long,
157            alias = "keypath",
158            requires = "encryption_algorithm"
159        )]
160        key_path: Option<PathBuf>,
161        /// Encryption algorithm. Currently AES_GCM is the only algorithm supported.
162        ///
163        /// `keypath` must also be specified when using this flag.
164        #[clap(short = 'e', long, alias = "encryptionalgorithm", requires = "key_path", value_parser = parse_encryption_algorithm)]
165        encryption_algorithm: Option<EncryptionAlgorithm>,
166        /// Force creation of the VMGS file. If the VMGS filepath already exists,
167        /// this flag allows an existing file to be overwritten.
168        #[clap(long, alias = "forcecreate")]
169        force_create: bool,
170    },
171    /// Write data into the specified file ID of the VMGS file.
172    ///
173    /// The proper key file must be specified to write encrypted data.
174    Write {
175        #[command(flatten)]
176        file_path: FilePathArg,
177        /// Data file path to read
178        #[clap(short = 'd', long, alias = "datapath")]
179        data_path: PathBuf,
180        #[command(flatten)]
181        file_id: FileIdArg,
182        #[command(flatten)]
183        key_path: KeyPathArg,
184        /// Overwrite the VMGS data at `fileid`, even if it already exists with nonzero size
185        #[clap(long, alias = "allowoverwrite")]
186        allow_overwrite: bool,
187    },
188    /// Dump/read data from the specified file ID of the VMGS file.
189    ///
190    /// The proper key file must be specified to read encrypted data. If the data
191    /// is encrypted and no key is specified, the data will be dumped without
192    /// decrypting.
193    Dump {
194        #[command(flatten)]
195        file_path: FilePathArg,
196        /// Data file path to write
197        #[clap(short = 'd', long, alias = "datapath")]
198        data_path: Option<PathBuf>,
199        #[command(flatten)]
200        file_id: FileIdArg,
201        #[command(flatten)]
202        key_path: KeyPathArg,
203        /// When dumping to stdout, dump data as raw bytes instead of ASCII hex
204        #[clap(long, conflicts_with = "data_path")]
205        raw_stdout: bool,
206    },
207    /// Dump headers of the VMGS file at `filepath` to the console.
208    DumpHeaders {
209        #[command(flatten)]
210        file_path: FilePathArg,
211    },
212    /// Get the size of the specified `fileid` within the VMGS file
213    QuerySize {
214        #[command(flatten)]
215        file_path: FilePathArg,
216        #[command(flatten)]
217        file_id: FileIdArg,
218    },
219    /// Replace the current encryption key with a new provided key
220    ///
221    /// Both key files must contain a key that is 32 bytes long.
222    UpdateKey {
223        #[command(flatten)]
224        file_path: FilePathArg,
225        /// Current encryption key file path.
226        #[clap(short = 'k', long, alias = "keypath")]
227        key_path: PathBuf,
228        /// New encryption key file path.
229        #[clap(short = 'n', long, alias = "newkeypath")]
230        new_key_path: PathBuf,
231        /// Encryption algorithm. Currently AES_GCM is the only algorithm supported.
232        #[clap(short = 'e', long, alias = "encryptionalgorithm", value_parser = parse_encryption_algorithm)]
233        encryption_algorithm: EncryptionAlgorithm,
234    },
235    /// Encrypt an existing VMGS file
236    Encrypt {
237        #[command(flatten)]
238        file_path: FilePathArg,
239        /// Encryption key file path. The file must contain a key that is 32 bytes long.
240        #[clap(short = 'k', long, alias = "keypath")]
241        key_path: PathBuf,
242        /// Encryption algorithm. Currently AES_GCM is the only algorithm supported.
243        #[clap(short = 'e', long, alias = "encryptionalgorithm", value_parser = parse_encryption_algorithm)]
244        encryption_algorithm: EncryptionAlgorithm,
245    },
246    /// Query whether a VMGS file is encrypted
247    QueryEncryption {
248        #[command(flatten)]
249        file_path: FilePathArg,
250    },
251    /// UEFI NVRAM operations
252    UefiNvram {
253        #[clap(subcommand)]
254        operation: UefiNvramOperation,
255    },
256}
257
258fn parse_file_id(file_id: &str) -> Result<FileId, std::num::ParseIntError> {
259    Ok(match file_id {
260        "FILE_TABLE" => FileId::FILE_TABLE,
261        "BIOS_NVRAM" => FileId::BIOS_NVRAM,
262        "TPM_PPI" => FileId::TPM_PPI,
263        "TPM_NVRAM" => FileId::TPM_NVRAM,
264        "RTC_SKEW" => FileId::RTC_SKEW,
265        "ATTEST" => FileId::ATTEST,
266        "KEY_PROTECTOR" => FileId::KEY_PROTECTOR,
267        "VM_UNIQUE_ID" => FileId::VM_UNIQUE_ID,
268        "GUEST_FIRMWARE" => FileId::GUEST_FIRMWARE,
269        "CUSTOM_UEFI" => FileId::CUSTOM_UEFI,
270        "GUEST_WATCHDOG" => FileId::GUEST_WATCHDOG,
271        "HW_KEY_PROTECTOR" => FileId::HW_KEY_PROTECTOR,
272        "GUEST_SECRET_KEY" => FileId::GUEST_SECRET_KEY,
273        "EXTENDED_FILE_TABLE" => FileId::EXTENDED_FILE_TABLE,
274        v => FileId(v.parse::<u32>()?),
275    })
276}
277
278fn parse_encryption_algorithm(algorithm: &str) -> Result<EncryptionAlgorithm, &'static str> {
279    match algorithm {
280        "AES_GCM" => Ok(EncryptionAlgorithm::AES_GCM),
281        _ => Err("Encryption algorithm not supported"),
282    }
283}
284
285fn extract_version(ver: u32) -> String {
286    let major = (ver >> 16) & 0xFF;
287    let minor = ver & 0xFF;
288    format!("{major}.{minor}")
289}
290
291fn parse_legacy_args() -> Vec<String> {
292    use std::env;
293    let mut args: Vec<String> = env::args().collect();
294    if let Some(cmd) = args.get(1) {
295        let cmd_lower = cmd.to_ascii_lowercase();
296        let new_cmd = match &cmd_lower[..] {
297            "-c" | "-create" => Some("create"),
298            "-w" | "-write" => Some("write"),
299            "-r" | "-dump" => Some("dump"),
300            "-rh" | "-dumpheaders" => Some("dump-headers"),
301            "-qs" | "-querysize" => Some("query-size"),
302            "-uk" | "-updatekey" => Some("update-key"),
303            "-e" | "-encrypt" => Some("encrypt"),
304            _ => None,
305        };
306
307        if let Some(new_cmd) = new_cmd {
308            eprintln!("Warning: Using legacy arguments. Please migrate to the new syntax.");
309            args[1] = new_cmd.to_string();
310
311            let mut index = 2;
312            while let Some(arg) = args.get(index) {
313                let arg_lower = arg.to_ascii_lowercase();
314                if let Some(new_arg) = match &arg_lower[..] {
315                    "-f" | "-filepath" => Some("--file-path"),
316                    "-s" | "-filesize" => Some("--file-size"),
317                    "-i" | "-fileid" => Some("--file-id"),
318                    "-d" | "-datapath" => Some("--data-path"),
319                    "-ow" | "-allowoverwrite" => Some("--allow-overwrite"),
320                    "-k" | "-keypath" => Some("--key-path"),
321                    "-n" | "-newkeypath" => Some("--new-key-path"),
322                    "-ea" | "-encryptionalgorithm" => Some("--encryption-algorithm"),
323                    "-fc" | "-forcecreate" => Some("--force-create"),
324                    _ => None,
325                } {
326                    args[index] = new_arg.to_string();
327                }
328                index += 1;
329            }
330        }
331    }
332    args
333}
334
335fn main() {
336    DefaultPool::run_with(async |_| match do_main().await {
337        Ok(_) => eprintln!("The operation completed successfully."),
338        Err(e) => {
339            let exit_code = match e {
340                Error::NotEncrypted => ExitCode::ErrorNotEncrypted,
341                Error::EmptyFile => ExitCode::ErrorEmpty,
342                Error::ZeroSize => ExitCode::ErrorEmpty,
343                Error::Vmgs(VmgsError::FileInfoAllocated) => ExitCode::ErrorNotFound,
344                Error::V1Format => ExitCode::ErrorV1,
345                Error::GspByIdEncryption => ExitCode::ErrorGspById,
346                _ => ExitCode::Error,
347            };
348
349            eprintln!("EXIT CODE: {} ({:?})", exit_code as i32, exit_code);
350            eprintln!("ERROR: {}", e);
351            let mut error_source = std::error::Error::source(&e);
352            while let Some(e2) = error_source {
353                eprintln!("- {}", e2);
354                error_source = e2.source();
355            }
356
357            std::process::exit(exit_code as i32);
358        }
359    })
360}
361
362async fn do_main() -> Result<(), Error> {
363    let opt = Options::parse_from(parse_legacy_args());
364
365    match opt {
366        Options::Create {
367            file_path,
368            file_size,
369            key_path,
370            encryption_algorithm,
371            force_create,
372        } => {
373            let encryption_alg_key = encryption_algorithm.map(|x| (x, key_path.unwrap()));
374            vmgs_file_create(
375                file_path.file_path,
376                file_size,
377                force_create,
378                encryption_alg_key,
379            )
380            .await
381        }
382        Options::Dump {
383            file_path,
384            data_path,
385            file_id,
386            key_path,
387            raw_stdout,
388        } => {
389            vmgs_file_read(
390                file_path.file_path,
391                data_path,
392                file_id.file_id,
393                key_path.key_path,
394                raw_stdout,
395            )
396            .await
397        }
398        Options::Write {
399            file_path,
400            data_path,
401            file_id,
402            key_path,
403            allow_overwrite,
404        } => {
405            vmgs_file_write(
406                file_path.file_path,
407                data_path,
408                file_id.file_id,
409                key_path.key_path,
410                allow_overwrite,
411            )
412            .await
413        }
414        Options::DumpHeaders { file_path } => vmgs_file_dump_headers(file_path.file_path).await,
415        Options::QuerySize { file_path, file_id } => {
416            vmgs_file_query_file_size(file_path.file_path, file_id.file_id).await
417        }
418        Options::UpdateKey {
419            file_path,
420            key_path,
421            new_key_path,
422            encryption_algorithm,
423        } => {
424            vmgs_file_update_key(
425                file_path.file_path,
426                encryption_algorithm,
427                Some(key_path),
428                new_key_path,
429            )
430            .await
431        }
432        Options::Encrypt {
433            file_path,
434            key_path,
435            encryption_algorithm,
436        } => {
437            vmgs_file_update_key(
438                file_path.file_path,
439                encryption_algorithm,
440                None as Option<PathBuf>,
441                key_path,
442            )
443            .await
444        }
445        Options::QueryEncryption { file_path } => {
446            vmgs_file_query_encryption(file_path.file_path).await
447        }
448        Options::UefiNvram { operation } => uefi_nvram::do_command(operation).await,
449    }
450}
451
452async fn vmgs_file_update_key(
453    file_path: impl AsRef<Path>,
454    encryption_alg: EncryptionAlgorithm,
455    key_path: Option<impl AsRef<Path>>,
456    new_key_path: impl AsRef<Path>,
457) -> Result<(), Error> {
458    let new_encryption_key = read_key_path(new_key_path)?;
459    let mut vmgs = vmgs_file_open(file_path, key_path, OpenMode::ReadWrite).await?;
460
461    vmgs_update_key(&mut vmgs, encryption_alg, new_encryption_key.as_ref()).await
462}
463
464#[cfg_attr(not(with_encryption), expect(unused_variables))]
465async fn vmgs_update_key(
466    vmgs: &mut Vmgs,
467    encryption_alg: EncryptionAlgorithm,
468    new_encryption_key: &[u8],
469) -> Result<(), Error> {
470    #[cfg(not(with_encryption))]
471    unreachable!("encryption requires the encryption feature");
472    #[cfg(with_encryption)]
473    {
474        eprintln!("Updating encryption key");
475        vmgs.update_encryption_key(new_encryption_key, encryption_alg)
476            .await
477            .map_err(Error::EncryptionKey)?;
478
479        Ok(())
480    }
481}
482
483async fn vmgs_file_create(
484    path: impl AsRef<Path>,
485    file_size: Option<u64>,
486    force_create: bool,
487    encryption_alg_key: Option<(EncryptionAlgorithm, impl AsRef<Path>)>,
488) -> Result<(), Error> {
489    let disk = vhdfiledisk_create(path, file_size, force_create)?;
490
491    let encryption_key = encryption_alg_key
492        .as_ref()
493        .map(|(_, key_path)| read_key_path(key_path))
494        .transpose()?;
495    let encryption_alg_key =
496        encryption_alg_key.map(|(alg, _)| (alg, encryption_key.as_deref().unwrap()));
497
498    let _ = vmgs_create(disk, encryption_alg_key).await?;
499
500    Ok(())
501}
502
503fn vhdfiledisk_create(
504    path: impl AsRef<Path>,
505    req_file_size: Option<u64>,
506    force_create: bool,
507) -> Result<Disk, Error> {
508    const MIN_VMGS_FILE_SIZE: u64 = 4 * VMGS_BYTES_PER_BLOCK as u64;
509    const SECTOR_SIZE: u64 = 512;
510
511    let file_size = req_file_size.unwrap_or(VMGS_DEFAULT_CAPACITY);
512    if file_size < MIN_VMGS_FILE_SIZE || file_size % SECTOR_SIZE != 0 {
513        return Err(Error::InvalidVmgsFileSize(
514            file_size,
515            format!(
516                "Must be a multiple of {} and at least {}",
517                SECTOR_SIZE, MIN_VMGS_FILE_SIZE
518            ),
519        ));
520    }
521
522    if force_create && Path::new(path.as_ref()).exists() {
523        eprintln!(
524            "File already exists. Recreating the file {:?}",
525            path.as_ref()
526        );
527    }
528
529    eprintln!("Creating file: {}", path.as_ref().display());
530    let file = match fs_err::OpenOptions::new()
531        .read(true)
532        .write(true)
533        .create(true)
534        .create_new(!force_create)
535        .truncate(true)
536        .open(path.as_ref())
537    {
538        Ok(file) => file,
539        Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
540            return Err(Error::FileExists);
541        }
542        Err(err) => return Err(Error::VmgsFile(err)),
543    };
544
545    eprintln!(
546        "Setting file size to {}{}",
547        file_size,
548        if req_file_size.is_some() {
549            ""
550        } else {
551            " (default)"
552        }
553    );
554    file.set_len(file_size).map_err(Error::VmgsFile)?;
555
556    eprintln!("Formatting VHD");
557    Vhd1Disk::make_fixed(file.file()).map_err(Error::Vhd1)?;
558    let disk = Vhd1Disk::open_fixed(file.into(), false).map_err(Error::Vhd1)?;
559    Disk::new(disk).map_err(Error::InvalidDisk)
560}
561
562#[cfg_attr(not(with_encryption), expect(unused_mut), expect(unused_variables))]
563async fn vmgs_create(
564    disk: Disk,
565    encryption_alg_key: Option<(EncryptionAlgorithm, &[u8])>,
566) -> Result<Vmgs, Error> {
567    eprintln!("Formatting VMGS");
568    let mut vmgs = Vmgs::format_new(disk, None).await?;
569
570    if let Some((algorithm, encryption_key)) = encryption_alg_key {
571        eprintln!("Adding encryption key");
572        #[cfg(with_encryption)]
573        vmgs.update_encryption_key(encryption_key, algorithm)
574            .await
575            .map_err(Error::EncryptionKey)?;
576        #[cfg(not(with_encryption))]
577        unreachable!("Encryption requires the encryption feature");
578    }
579
580    Ok(vmgs)
581}
582
583async fn vmgs_file_write(
584    file_path: impl AsRef<Path>,
585    data_path: impl AsRef<Path>,
586    file_id: FileId,
587    key_path: Option<impl AsRef<Path>>,
588    allow_overwrite: bool,
589) -> Result<(), Error> {
590    eprintln!(
591        "Opening source (raw data file): {}",
592        data_path.as_ref().display()
593    );
594
595    let mut file = File::open(data_path.as_ref()).map_err(Error::DataFile)?;
596    let mut buf = Vec::new();
597
598    // manually allow, since we want to differentiate between the file not being
599    // accessible, and a read operation failing
600    file.read_to_end(&mut buf).map_err(Error::DataFile)?;
601
602    eprintln!("Read {} bytes", buf.len());
603
604    let encrypt = key_path.is_some();
605    let mut vmgs = vmgs_file_open(file_path, key_path, OpenMode::ReadWrite).await?;
606
607    vmgs_write(&mut vmgs, file_id, &buf, encrypt, allow_overwrite).await?;
608
609    Ok(())
610}
611
612async fn vmgs_write(
613    vmgs: &mut Vmgs,
614    file_id: FileId,
615    data: &[u8],
616    encrypt: bool,
617    allow_overwrite: bool,
618) -> Result<(), Error> {
619    eprintln!("Writing File ID {} ({:?})", file_id.0, file_id);
620
621    if let Ok(info) = vmgs.get_file_info(file_id) {
622        if !allow_overwrite && info.valid_bytes > 0 {
623            return Err(Error::FileIdExists(file_id));
624        }
625        if !encrypt && info.encrypted {
626            eprintln!("Warning: overwriting encrypted file with plaintext data")
627        }
628    }
629
630    if encrypt {
631        #[cfg(with_encryption)]
632        vmgs.write_file_encrypted(file_id, data).await?;
633        #[cfg(not(with_encryption))]
634        unreachable!("Encryption requires the encryption feature");
635    } else {
636        vmgs.write_file_allow_overwrite_encrypted(file_id, data)
637            .await?;
638    }
639
640    Ok(())
641}
642
643/// Get data from VMGS file, and write to `data_path`.
644async fn vmgs_file_read(
645    file_path: impl AsRef<Path>,
646    data_path: Option<impl AsRef<Path>>,
647    file_id: FileId,
648    key_path: Option<impl AsRef<Path>>,
649    raw_stdout: bool,
650) -> Result<(), Error> {
651    let decrypt = key_path.is_some();
652    let mut vmgs = vmgs_file_open(file_path, key_path, OpenMode::ReadOnly).await?;
653
654    let file_info = vmgs.get_file_info(file_id)?;
655    if !decrypt && file_info.encrypted {
656        eprintln!("Warning: Reading encrypted file without decrypting");
657    }
658
659    let buf = vmgs_read(&mut vmgs, file_id, decrypt).await?;
660
661    eprintln!("Read {} bytes", buf.len());
662    if buf.len() != file_info.valid_bytes as usize {
663        eprintln!("Warning: Bytes read from VMGS doesn't match file info");
664    }
665
666    if let Some(path) = data_path {
667        eprintln!("Writing contents to {}", path.as_ref().display());
668        let mut file = File::create(path.as_ref()).map_err(Error::DataFile)?;
669        file.write_all(&buf).map_err(Error::DataFile)?;
670    } else {
671        eprintln!("Writing contents to stdout");
672        if raw_stdout {
673            let mut stdout = std::io::stdout();
674            stdout.write_all(&buf).map_err(Error::DataFile)?;
675        } else {
676            for c in buf.chunks(16) {
677                for b in c {
678                    print!("0x{:02x},", b);
679                }
680                println!(
681                    "{:missing$}// {}",
682                    ' ',
683                    c.iter()
684                        .map(|c| if c.is_ascii_graphic() {
685                            *c as char
686                        } else {
687                            '.'
688                        })
689                        .collect::<String>(),
690                    missing = (16 - c.len()) * 5 + 1
691                );
692            }
693        }
694    }
695
696    Ok(())
697}
698
699async fn vmgs_read(vmgs: &mut Vmgs, file_id: FileId, decrypt: bool) -> Result<Vec<u8>, Error> {
700    eprintln!("Reading File ID {} ({:?})", file_id.0, file_id);
701    Ok(if decrypt {
702        vmgs.read_file(file_id).await?
703    } else {
704        vmgs.read_file_raw(file_id).await?
705    })
706}
707
708async fn vmgs_file_dump_headers(file_path: impl AsRef<Path>) -> Result<(), Error> {
709    let file = File::open(file_path.as_ref()).map_err(Error::VmgsFile)?;
710    let validate_result = vmgs_file_validate(&file);
711    let disk = Disk::new(Vhd1Disk::open_fixed(file.into(), true).map_err(Error::Vhd1)?)
712        .map_err(Error::InvalidDisk)?;
713
714    let headers_result = match read_headers(disk).await {
715        Ok((header1, header2)) => vmgs_dump_headers(&header1, &header2),
716        Err(e) => Err(e.into()),
717    };
718
719    if validate_result.is_err() {
720        validate_result
721    } else {
722        headers_result
723    }
724}
725
726fn vmgs_dump_headers(header1: &VmgsHeader, header2: &VmgsHeader) -> Result<(), Error> {
727    println!("FILE HEADERS");
728    println!("{0:<23} {1:^70} {2:^70}", "Field", "Header 1", "Header 2");
729    println!("{} {} {}", "-".repeat(23), "-".repeat(70), "-".repeat(70));
730
731    let signature1 = format!("{:#018x}", header1.signature);
732    let signature2 = format!("{:#018x}", header2.signature);
733    println!(
734        "{0:<23} {1:>70} {2:>70}",
735        "Signature:", signature1, signature2
736    );
737
738    println!(
739        "{0:<23} {1:>70} {2:>70}",
740        "Version:",
741        extract_version(header1.version),
742        extract_version(header2.version)
743    );
744    println!(
745        "{0:<23} {1:>70x} {2:>70x}",
746        "Checksum:", header1.checksum, header2.checksum
747    );
748    println!(
749        "{0:<23} {1:>70} {2:>70}",
750        "Sequence:", header1.sequence, header2.sequence
751    );
752    println!(
753        "{0:<23} {1:>70} {2:>70}",
754        "HeaderSize:", header1.header_size, header2.header_size
755    );
756
757    let file_table_offset1 = format!("{:#010x}", header1.file_table_offset);
758    let file_table_offset2 = format!("{:#010x}", header2.file_table_offset);
759    println!(
760        "{0:<23} {1:>70} {2:>70}",
761        "FileTableOffset:", file_table_offset1, file_table_offset2
762    );
763
764    println!(
765        "{0:<23} {1:>70} {2:>70}",
766        "FileTableSize:", header1.file_table_size, header2.file_table_size
767    );
768
769    let encryption_algorithm1 = format!("{:#06x}", header1.encryption_algorithm.0);
770    let encryption_algorithm2 = format!("{:#06x}", header2.encryption_algorithm.0);
771    println!(
772        "{0:<23} {1:>70} {2:>70}",
773        "EncryptionAlgorithm:", encryption_algorithm1, encryption_algorithm2
774    );
775
776    let reserved1 = format!("{:#06x}", header1.reserved);
777    let reserved2 = format!("{:#06x}", header2.reserved);
778
779    println!("{0:<23} {1:>70} {2:>70}", "Reserved:", reserved1, reserved2);
780
781    println!("{0:<23}", "MetadataKey1:");
782
783    let key1_nonce = format!("0x{}", hex::encode(header1.metadata_keys[0].nonce));
784    let key2_nonce = format!("0x{}", hex::encode(header2.metadata_keys[0].nonce));
785    println!(
786        "    {0:<19} {1:>70} {2:>70}",
787        "Nonce:", key1_nonce, key2_nonce
788    );
789
790    let key1_reserved = format!("{:#010x}", header1.metadata_keys[0].reserved);
791    let key2_reserved = format!("{:#010x}", header2.metadata_keys[0].reserved);
792    println!(
793        "    {0:<19} {1:>70} {2:>70}",
794        "Reserved:", key1_reserved, key2_reserved
795    );
796
797    let key1_auth_tag = format!(
798        "0x{}",
799        hex::encode(header1.metadata_keys[0].authentication_tag)
800    );
801    let key2_auth_tag = format!(
802        "0x{}",
803        hex::encode(header2.metadata_keys[0].authentication_tag)
804    );
805    println!(
806        "    {0:<19} {1:>70} {2:>70}",
807        "AuthenticationTag:", key1_auth_tag, key2_auth_tag
808    );
809
810    let key1_encryption_key = format!("0x{}", hex::encode(header1.metadata_keys[0].encryption_key));
811    let key2_encryption_key = format!("0x{}", hex::encode(header2.metadata_keys[0].encryption_key));
812    println!(
813        "    {0:<19} {1:>70} {2:>70}",
814        "EncryptionKey:", key1_encryption_key, key2_encryption_key
815    );
816
817    println!("{0:<23}", "MetadataKey2:");
818    let key1_nonce = format!("0x{}", hex::encode(header1.metadata_keys[1].nonce));
819    let key2_nonce = format!("0x{}", hex::encode(header2.metadata_keys[1].nonce));
820    println!(
821        "    {0:<19} {1:>70} {2:>70}",
822        "Nonce:", key1_nonce, key2_nonce
823    );
824
825    let key1_reserved = format!("0x{:#010x}", header1.metadata_keys[1].reserved);
826    let key2_reserved = format!("0x{:#010x}", header2.metadata_keys[1].reserved);
827    println!(
828        "    {0:<19} {1:>70} {2:>70}",
829        "Reserved:", key1_reserved, key2_reserved
830    );
831
832    let key1_auth_tag = format!(
833        "0x{}",
834        hex::encode(header1.metadata_keys[1].authentication_tag)
835    );
836    let key2_auth_tag = format!(
837        "0x{}",
838        hex::encode(header2.metadata_keys[1].authentication_tag)
839    );
840    println!(
841        "    {0:<19} {1:>70} {2:>70}",
842        "AuthenticationTag:", key1_auth_tag, key2_auth_tag
843    );
844
845    let key1_encryption_key = format!("0x{}", hex::encode(header1.metadata_keys[1].encryption_key));
846    let key2_encryption_key = format!("0x{}", hex::encode(header2.metadata_keys[1].encryption_key));
847    println!(
848        "    {0:<19} {1:>70} {2:>70}",
849        "EncryptionKey:", key1_encryption_key, key2_encryption_key
850    );
851
852    let key1_reserved1 = format!("0x{:#010x}", header1.reserved_1);
853    let key2_reserved1 = format!("0x{:#010x}", header2.reserved_1);
854    println!(
855        "{0:<23} {1:>70} {2:>70}",
856        "Reserved:", key1_reserved1, key2_reserved1
857    );
858
859    println!("{} {} {}\n", "-".repeat(23), "-".repeat(70), "-".repeat(70));
860
861    print!("Verifying header 1... ");
862    let header1_result = validate_header(header1);
863    match &header1_result {
864        Ok(_) => println!("[VALID]"),
865        Err(e) => println!("[INVALID] Error: {}", e),
866    }
867
868    print!("Verifying header 2... ");
869    let header2_result = validate_header(header2);
870    match &header2_result {
871        Ok(_) => println!("[VALID]"),
872        Err(e) => println!("[INVALID] Error: {}", e),
873    }
874
875    match get_active_header(header1_result, header2_result) {
876        Ok(active_index) => match active_index {
877            0 => println!("Active header is 1"),
878            1 => println!("Active header is 2"),
879            _ => unreachable!(),
880        },
881        Err(e) => {
882            println!("Unable to determine active header");
883            return Err(Error::Vmgs(e));
884        }
885    }
886
887    Ok(())
888}
889
890#[derive(Copy, Clone, Debug, PartialEq, Eq)]
891enum OpenMode {
892    ReadOnly,
893    ReadWrite,
894}
895
896async fn vmgs_file_open(
897    file_path: impl AsRef<Path>,
898    key_path: Option<impl AsRef<Path>>,
899    open_mode: OpenMode,
900) -> Result<Vmgs, Error> {
901    eprintln!("Opening VMGS File: {}", file_path.as_ref().display());
902    let file = fs_err::OpenOptions::new()
903        .read(true)
904        .write(open_mode == OpenMode::ReadWrite)
905        .open(file_path.as_ref())
906        .map_err(Error::VmgsFile)?;
907
908    vmgs_file_validate(&file)?;
909
910    let disk = Disk::new(
911        Vhd1Disk::open_fixed(file.into(), open_mode == OpenMode::ReadOnly).map_err(Error::Vhd1)?,
912    )
913    .map_err(Error::InvalidDisk)?;
914    let encryption_key = key_path.map(read_key_path).transpose()?;
915
916    let res = vmgs_open(disk, encryption_key.as_deref()).await;
917
918    if matches!(
919        res,
920        Err(Error::Vmgs(VmgsError::InvalidFormat(_)))
921            | Err(Error::Vmgs(VmgsError::CorruptFormat(_)))
922    ) {
923        eprintln!("VMGS is corrupted or invalid. Dumping headers.");
924        let _ = vmgs_file_dump_headers(file_path.as_ref()).await;
925    }
926
927    res
928}
929
930#[cfg_attr(not(with_encryption), expect(unused_mut), expect(unused_variables))]
931async fn vmgs_open(disk: Disk, encryption_key: Option<&[u8]>) -> Result<Vmgs, Error> {
932    let mut vmgs: Vmgs = Vmgs::open(disk, None).await?;
933
934    if let Some(encryption_key) = encryption_key {
935        #[cfg(with_encryption)]
936        if vmgs.is_encrypted() {
937            vmgs.unlock_with_encryption_key(encryption_key).await?;
938        } else {
939            return Err(Error::NotEncrypted);
940        }
941        #[cfg(not(with_encryption))]
942        unreachable!("Encryption requires the encryption feature");
943    }
944
945    Ok(vmgs)
946}
947
948fn read_key_path(path: impl AsRef<Path>) -> Result<Vec<u8>, Error> {
949    eprintln!("Reading encryption key: {}", path.as_ref().display());
950    let metadata = fs_err::metadata(&path).map_err(Error::KeyFile)?;
951    if metadata.len() != VMGS_ENCRYPTION_KEY_SIZE as u64 {
952        return Err(Error::InvalidKeySize(
953            VMGS_ENCRYPTION_KEY_SIZE as u64,
954            metadata.len(),
955        ));
956    }
957
958    let bytes = fs_err::read(&path).map_err(Error::KeyFile)?;
959    if bytes.len() != metadata.len() as usize {
960        return Err(Error::InvalidKeySize(
961            VMGS_ENCRYPTION_KEY_SIZE as u64,
962            bytes.len() as u64,
963        ));
964    }
965
966    Ok(bytes)
967}
968
969async fn vmgs_file_query_file_size(
970    file_path: impl AsRef<Path>,
971    file_id: FileId,
972) -> Result<(), Error> {
973    let vmgs = vmgs_file_open(file_path, None as Option<PathBuf>, OpenMode::ReadOnly).await?;
974
975    let file_size = vmgs_query_file_size(&vmgs, file_id)?;
976
977    println!(
978        "File ID {} ({:?}) has a size of {}",
979        file_id.0, file_id, file_size
980    );
981
982    Ok(())
983}
984
985fn vmgs_query_file_size(vmgs: &Vmgs, file_id: FileId) -> Result<u64, Error> {
986    Ok(vmgs.get_file_info(file_id)?.valid_bytes)
987}
988
989async fn vmgs_file_query_encryption(file_path: impl AsRef<Path>) -> Result<(), Error> {
990    print!("{} is ", file_path.as_ref().display());
991
992    let vmgs = vmgs_file_open(file_path, None as Option<PathBuf>, OpenMode::ReadOnly).await?;
993
994    match (
995        vmgs.get_encryption_algorithm(),
996        vmgs_get_encryption_scheme(&vmgs),
997    ) {
998        (EncryptionAlgorithm::NONE, scheme) => {
999            println!("not encrypted (encryption scheme: {scheme:?})");
1000            Err(Error::NotEncrypted)
1001        }
1002        (EncryptionAlgorithm::AES_GCM, VmgsEncryptionScheme::GspKey) => {
1003            println!("encrypted with AES GCM encryption algorithm using GspKey");
1004            Ok(())
1005        }
1006        (EncryptionAlgorithm::AES_GCM, VmgsEncryptionScheme::GspById) => {
1007            println!("encrypted with AES GCM encryption algorithm using GspById");
1008            Err(Error::GspByIdEncryption)
1009        }
1010        (EncryptionAlgorithm::AES_GCM, VmgsEncryptionScheme::None) => {
1011            println!(
1012                "encrypted with AES GCM encryption algorithm using an unknown encryption scheme"
1013            );
1014            Err(Error::GspUnknown)
1015        }
1016        (alg, scheme) => {
1017            println!(
1018                "using an unknown encryption algorithm: {alg:?} (encryption scheme: {scheme:?})"
1019            );
1020            Err(Error::EncryptionUnknown)
1021        }
1022    }
1023}
1024
1025fn vmgs_get_encryption_scheme(vmgs: &Vmgs) -> VmgsEncryptionScheme {
1026    if vmgs_query_file_size(vmgs, FileId::KEY_PROTECTOR).is_ok() {
1027        VmgsEncryptionScheme::GspKey
1028    } else if vmgs_query_file_size(vmgs, FileId::VM_UNIQUE_ID).is_ok() {
1029        VmgsEncryptionScheme::GspById
1030    } else {
1031        VmgsEncryptionScheme::None
1032    }
1033}
1034
1035fn vmgs_file_validate(file: &File) -> Result<(), Error> {
1036    vmgs_file_validate_not_empty(file)?;
1037    vmgs_file_validate_not_v1(file)?;
1038    Ok(())
1039}
1040
1041/// Validate if the VMGS file is empty. This is a special case for Azure and
1042/// we want to return an error code (ERROR_EMPTY) instead of ERROR_FILE_CORRUPT.
1043/// A file can be empty in the following 2 cases:
1044///     1) the size is zero
1045///     2) the size is non-zero but there is no content inside the file except the footer.
1046fn vmgs_file_validate_not_empty(mut file: &File) -> Result<(), Error> {
1047    const VHD_DISK_FOOTER_PACKED_SIZE: u64 = 512;
1048    const MAX_VMGS_FILE_SIZE: u64 = 4 * ONE_GIGA_BYTE;
1049
1050    let file_size = file.metadata().map_err(Error::VmgsFile)?.len();
1051
1052    if file_size > MAX_VMGS_FILE_SIZE {
1053        return Err(Error::InvalidVmgsFileSize(
1054            file_size,
1055            format!("Must be less than {}", MAX_VMGS_FILE_SIZE),
1056        ));
1057    }
1058
1059    if file_size == 0 {
1060        return Err(Error::ZeroSize);
1061    }
1062
1063    // Special case - check that the file has a non zero size but the contents are empty
1064    // except for the file footer which is ignored.
1065    // This is to differentiate between a file without any content for an Azure scenario.
1066    // The VMGS file received by the HostAgent team from DiskRP contains a footer that
1067    // should be ignored during the empty file comparison.
1068    if file_size < VHD_DISK_FOOTER_PACKED_SIZE {
1069        return Err(Error::InvalidVmgsFileSize(
1070            file_size,
1071            format!("Must be greater than {}", VHD_DISK_FOOTER_PACKED_SIZE),
1072        ));
1073    }
1074
1075    let bytes_to_compare = file_size - VHD_DISK_FOOTER_PACKED_SIZE;
1076    let mut bytes_read = 0;
1077    let mut empty_file = true;
1078    let mut buf = vec![0; 32 * ONE_MEGA_BYTE as usize];
1079
1080    // Fragment reads to 32 MB when checking that file contents are 0
1081    while bytes_read < bytes_to_compare {
1082        let bytes_to_read =
1083            std::cmp::min(32 * ONE_MEGA_BYTE, bytes_to_compare - bytes_read) as usize;
1084
1085        file.read(&mut buf[..bytes_to_read])
1086            .map_err(Error::VmgsFile)?;
1087
1088        if !buf[..bytes_to_read].iter().all(|&x| x == 0) {
1089            empty_file = false;
1090            break;
1091        }
1092
1093        bytes_read += buf.len() as u64;
1094    }
1095
1096    if empty_file {
1097        return Err(Error::EmptyFile);
1098    }
1099
1100    Ok(())
1101}
1102
1103/// Validate that this is not a VMGSv1 file
1104fn vmgs_file_validate_not_v1(mut file: &File) -> Result<(), Error> {
1105    const EFI_SIGNATURE: &[u8] = b"EFI PART";
1106    let mut maybe_efi_signature = [0; EFI_SIGNATURE.len()];
1107    file.seek(std::io::SeekFrom::Start(512))
1108        .map_err(Error::VmgsFile)?;
1109    file.read(&mut maybe_efi_signature)
1110        .map_err(Error::VmgsFile)?;
1111    if maybe_efi_signature == EFI_SIGNATURE {
1112        return Err(Error::V1Format);
1113    }
1114
1115    Ok(())
1116}
1117
1118#[cfg(test)]
1119mod tests {
1120    use super::*;
1121    use pal_async::async_test;
1122    use tempfile::tempdir;
1123
1124    async fn test_vmgs_create(
1125        path: impl AsRef<Path>,
1126        file_size: Option<u64>,
1127        force_create: bool,
1128        encryption_alg_key: Option<(EncryptionAlgorithm, &[u8])>,
1129    ) -> Result<(), Error> {
1130        let disk = vhdfiledisk_create(path, file_size, force_create)?;
1131        let _ = vmgs_create(disk, encryption_alg_key).await?;
1132        Ok(())
1133    }
1134
1135    async fn test_vmgs_open(
1136        path: impl AsRef<Path>,
1137        open_mode: OpenMode,
1138        encryption_key: Option<&[u8]>,
1139    ) -> Result<Vmgs, Error> {
1140        let file = fs_err::OpenOptions::new()
1141            .read(true)
1142            .write(open_mode == OpenMode::ReadWrite)
1143            .open(path.as_ref())
1144            .map_err(Error::VmgsFile)?;
1145        vmgs_file_validate(&file)?;
1146        let disk = Disk::new(
1147            Vhd1Disk::open_fixed(file.into(), open_mode == OpenMode::ReadOnly)
1148                .map_err(Error::Vhd1)?,
1149        )
1150        .unwrap();
1151        let vmgs = vmgs_open(disk, encryption_key).await?;
1152        Ok(vmgs)
1153    }
1154
1155    async fn test_vmgs_query_file_size(
1156        file_path: impl AsRef<Path>,
1157        file_id: FileId,
1158    ) -> Result<u64, Error> {
1159        let vmgs = vmgs_file_open(file_path, None as Option<PathBuf>, OpenMode::ReadOnly).await?;
1160
1161        vmgs_query_file_size(&vmgs, file_id)
1162    }
1163
1164    #[cfg(with_encryption)]
1165    async fn test_vmgs_query_encryption(
1166        file_path: impl AsRef<Path>,
1167    ) -> Result<EncryptionAlgorithm, Error> {
1168        let vmgs = vmgs_file_open(file_path, None as Option<PathBuf>, OpenMode::ReadOnly).await?;
1169
1170        Ok(vmgs.get_encryption_algorithm())
1171    }
1172
1173    #[cfg(with_encryption)]
1174    async fn test_vmgs_update_key(
1175        file_path: impl AsRef<Path>,
1176        encryption_alg: EncryptionAlgorithm,
1177        encryption_key: Option<&[u8]>,
1178        new_encryption_key: &[u8],
1179    ) -> Result<(), Error> {
1180        let mut vmgs = test_vmgs_open(file_path, OpenMode::ReadWrite, encryption_key).await?;
1181
1182        vmgs_update_key(&mut vmgs, encryption_alg, new_encryption_key).await
1183    }
1184
1185    // Create a new test file path.
1186    fn new_path() -> (tempfile::TempDir, PathBuf) {
1187        let dir = tempdir().unwrap();
1188        let file_path = dir.path().join("test.vmgs");
1189        (dir, file_path)
1190    }
1191
1192    #[async_test]
1193    async fn read_invalid_file() {
1194        let (_dir, path) = new_path();
1195
1196        let result = test_vmgs_open(path, OpenMode::ReadOnly, None).await;
1197
1198        assert!(result.is_err());
1199    }
1200
1201    #[async_test]
1202    async fn read_empty_file() {
1203        let (_dir, path) = new_path();
1204
1205        test_vmgs_create(&path, None, false, None).await.unwrap();
1206
1207        let mut vmgs = test_vmgs_open(path, OpenMode::ReadOnly, None)
1208            .await
1209            .unwrap();
1210        let result = vmgs_read(&mut vmgs, FileId::FILE_TABLE, false).await;
1211        assert!(result.is_err());
1212    }
1213
1214    #[async_test]
1215    async fn read_write_file() {
1216        let (_dir, path) = new_path();
1217        let buf = b"Plain text data".to_vec();
1218
1219        test_vmgs_create(&path, None, false, None).await.unwrap();
1220
1221        let mut vmgs = test_vmgs_open(path, OpenMode::ReadWrite, None)
1222            .await
1223            .unwrap();
1224
1225        vmgs_write(&mut vmgs, FileId::ATTEST, &buf, false, false)
1226            .await
1227            .unwrap();
1228        let read_buf = vmgs_read(&mut vmgs, FileId::ATTEST, false).await.unwrap();
1229
1230        assert_eq!(buf, read_buf);
1231    }
1232
1233    #[async_test]
1234    async fn multiple_write_file() {
1235        let (_dir, path) = new_path();
1236        let buf_1 = b"Random super sensitive data".to_vec();
1237        let buf_2 = b"Other super secret data".to_vec();
1238        let buf_3 = b"I'm storing so much data".to_vec();
1239
1240        test_vmgs_create(&path, None, false, None).await.unwrap();
1241
1242        let mut vmgs = test_vmgs_open(path, OpenMode::ReadWrite, None)
1243            .await
1244            .unwrap();
1245
1246        vmgs_write(&mut vmgs, FileId::BIOS_NVRAM, &buf_1, false, false)
1247            .await
1248            .unwrap();
1249        let read_buf_1 = vmgs_read(&mut vmgs, FileId::BIOS_NVRAM, false)
1250            .await
1251            .unwrap();
1252
1253        assert_eq!(buf_1, read_buf_1);
1254
1255        vmgs_write(&mut vmgs, FileId::TPM_PPI, &buf_2, false, false)
1256            .await
1257            .unwrap();
1258        let read_buf_2 = vmgs_read(&mut vmgs, FileId::TPM_PPI, false).await.unwrap();
1259
1260        assert_eq!(buf_2, read_buf_2);
1261
1262        let result = vmgs_write(&mut vmgs, FileId::BIOS_NVRAM, &buf_3, false, false).await;
1263        assert!(result.is_err());
1264
1265        vmgs_write(&mut vmgs, FileId::BIOS_NVRAM, &buf_3, false, true)
1266            .await
1267            .unwrap();
1268        let read_buf_3 = vmgs_read(&mut vmgs, FileId::BIOS_NVRAM, false)
1269            .await
1270            .unwrap();
1271
1272        assert_eq!(buf_2, read_buf_2);
1273        assert_eq!(buf_3, read_buf_3);
1274    }
1275
1276    #[cfg(with_encryption)]
1277    #[async_test]
1278    async fn read_write_encrypted_file() {
1279        let (_dir, path) = new_path();
1280        let encryption_key = vec![5; 32];
1281        let buf_1 = b"123".to_vec();
1282
1283        test_vmgs_create(
1284            &path,
1285            None,
1286            false,
1287            Some((EncryptionAlgorithm::AES_GCM, &encryption_key)),
1288        )
1289        .await
1290        .unwrap();
1291
1292        let mut vmgs = test_vmgs_open(path, OpenMode::ReadWrite, Some(&encryption_key))
1293            .await
1294            .unwrap();
1295
1296        vmgs_write(&mut vmgs, FileId::BIOS_NVRAM, &buf_1, true, false)
1297            .await
1298            .unwrap();
1299        let read_buf = vmgs_read(&mut vmgs, FileId::BIOS_NVRAM, true)
1300            .await
1301            .unwrap();
1302
1303        assert!(read_buf == buf_1);
1304
1305        // try to normal write encrypted VMGs
1306        vmgs_write(&mut vmgs, FileId::TPM_PPI, &buf_1, false, false)
1307            .await
1308            .unwrap();
1309
1310        // try to normal read encrypted FileId
1311        let _encrypted_read = vmgs_read(&mut vmgs, FileId::BIOS_NVRAM, false)
1312            .await
1313            .unwrap();
1314    }
1315
1316    #[cfg(with_encryption)]
1317    #[async_test]
1318    async fn encrypted_read_write_plain_file() {
1319        // You shouldn't be able to use encryption if you create the VMGS
1320        // file without encryption.
1321        let (_dir, path) = new_path();
1322        let encryption_key = vec![5; VMGS_ENCRYPTION_KEY_SIZE];
1323
1324        test_vmgs_create(&path, None, false, None).await.unwrap();
1325
1326        let result = test_vmgs_open(path, OpenMode::ReadWrite, Some(&encryption_key)).await;
1327
1328        assert!(result.is_err());
1329    }
1330
1331    #[cfg(with_encryption)]
1332    #[async_test]
1333    async fn plain_read_write_encrypted_file() {
1334        let (_dir, path) = new_path();
1335        let encryption_key = vec![5; 32];
1336        let buf_1 = b"123".to_vec();
1337
1338        test_vmgs_create(
1339            &path,
1340            None,
1341            false,
1342            Some((EncryptionAlgorithm::AES_GCM, &encryption_key)),
1343        )
1344        .await
1345        .unwrap();
1346
1347        let mut vmgs = test_vmgs_open(path, OpenMode::ReadWrite, None)
1348            .await
1349            .unwrap();
1350
1351        vmgs_write(&mut vmgs, FileId::VM_UNIQUE_ID, &buf_1, false, false)
1352            .await
1353            .unwrap();
1354        let read_buf = vmgs_read(&mut vmgs, FileId::VM_UNIQUE_ID, false)
1355            .await
1356            .unwrap();
1357
1358        assert!(read_buf == buf_1);
1359    }
1360
1361    #[async_test]
1362    async fn query_size() {
1363        let (_dir, path) = new_path();
1364        let buf = b"Plain text data".to_vec();
1365
1366        test_vmgs_create(&path, None, false, None).await.unwrap();
1367
1368        {
1369            let mut vmgs = test_vmgs_open(&path, OpenMode::ReadWrite, None)
1370                .await
1371                .unwrap();
1372
1373            vmgs_write(&mut vmgs, FileId::ATTEST, &buf, false, false)
1374                .await
1375                .unwrap();
1376        }
1377
1378        let file_size = test_vmgs_query_file_size(&path, FileId::ATTEST)
1379            .await
1380            .unwrap();
1381        assert_eq!(file_size, buf.len() as u64);
1382    }
1383
1384    #[cfg(with_encryption)]
1385    #[async_test]
1386    async fn query_encrypted_file() {
1387        let (_dir, path) = new_path();
1388        let encryption_key = vec![5; 32];
1389        let buf_1 = b"123".to_vec();
1390
1391        test_vmgs_create(
1392            &path,
1393            None,
1394            false,
1395            Some((EncryptionAlgorithm::AES_GCM, &encryption_key)),
1396        )
1397        .await
1398        .unwrap();
1399
1400        {
1401            let mut vmgs = test_vmgs_open(&path, OpenMode::ReadWrite, Some(&encryption_key))
1402                .await
1403                .unwrap();
1404
1405            vmgs_write(&mut vmgs, FileId::BIOS_NVRAM, &buf_1, true, false)
1406                .await
1407                .unwrap();
1408        }
1409
1410        let file_size = test_vmgs_query_file_size(&path, FileId::BIOS_NVRAM)
1411            .await
1412            .unwrap();
1413        assert_eq!(file_size, buf_1.len() as u64);
1414    }
1415
1416    #[async_test]
1417    async fn test_validate_vmgs_file_not_empty() {
1418        let buf: Vec<u8> = (0..255).collect();
1419        let (_dir, path) = new_path();
1420
1421        test_vmgs_create(&path, None, false, None).await.unwrap();
1422
1423        let mut file = fs_err::OpenOptions::new()
1424            .read(true)
1425            .write(true)
1426            .open(path)
1427            .unwrap();
1428
1429        let result = vmgs_file_validate_not_empty(&file);
1430        matches!(result, Err(Error::ZeroSize));
1431
1432        file.seek(std::io::SeekFrom::Start(1024)).unwrap();
1433        file.write_all(&buf).unwrap();
1434        let result = vmgs_file_validate_not_empty(&file);
1435        matches!(result, Err(Error::VmgsFile(_)));
1436    }
1437
1438    #[async_test]
1439    async fn test_misaligned_size() {
1440        let (_dir, path) = new_path();
1441        //File size must be % 512 to be valid, should produce error and file should not be created
1442        let result = test_vmgs_create(&path, Some(65537), false, None).await;
1443        assert!(result.is_err());
1444        assert!(!path.exists());
1445    }
1446
1447    #[async_test]
1448    async fn test_forcecreate() {
1449        let (_dir, path) = new_path();
1450        let result = test_vmgs_create(&path, Some(4194304), false, None).await;
1451        assert!(result.is_ok());
1452        // Recreating file should fail without force create flag
1453        let result = test_vmgs_create(&path, Some(4194304), false, None).await;
1454        assert!(result.is_err());
1455        // Should be able to resize the file when force create is passed in
1456        let result = test_vmgs_create(&path, Some(8388608), true, None).await;
1457        assert!(result.is_ok());
1458    }
1459
1460    #[cfg(with_encryption)]
1461    #[async_test]
1462    async fn test_update_encryption_key() {
1463        let (_dir, path) = new_path();
1464        let encryption_key = vec![5; 32];
1465        let new_encryption_key = vec![6; 32];
1466        let buf_1 = b"123".to_vec();
1467
1468        test_vmgs_create(
1469            &path,
1470            None,
1471            false,
1472            Some((EncryptionAlgorithm::AES_GCM, &encryption_key)),
1473        )
1474        .await
1475        .unwrap();
1476
1477        {
1478            let mut vmgs = test_vmgs_open(&path, OpenMode::ReadWrite, Some(&encryption_key))
1479                .await
1480                .unwrap();
1481
1482            vmgs_write(&mut vmgs, FileId::BIOS_NVRAM, &buf_1, true, false)
1483                .await
1484                .unwrap();
1485        }
1486
1487        test_vmgs_update_key(
1488            &path,
1489            EncryptionAlgorithm::AES_GCM,
1490            Some(&encryption_key),
1491            &new_encryption_key,
1492        )
1493        .await
1494        .unwrap();
1495
1496        {
1497            let mut vmgs = test_vmgs_open(&path, OpenMode::ReadOnly, Some(&new_encryption_key))
1498                .await
1499                .unwrap();
1500
1501            let read_buf = vmgs_read(&mut vmgs, FileId::BIOS_NVRAM, true)
1502                .await
1503                .unwrap();
1504            assert!(read_buf == buf_1);
1505        }
1506
1507        // Old key should no longer work
1508        let result = test_vmgs_open(&path, OpenMode::ReadOnly, Some(&encryption_key)).await;
1509        assert!(result.is_err());
1510    }
1511
1512    #[cfg(with_encryption)]
1513    #[async_test]
1514    async fn test_add_encryption_key() {
1515        let (_dir, path) = new_path();
1516        let encryption_key = vec![5; 32];
1517        let buf_1 = b"123".to_vec();
1518
1519        test_vmgs_create(&path, None, false, None).await.unwrap();
1520
1521        test_vmgs_update_key(&path, EncryptionAlgorithm::AES_GCM, None, &encryption_key)
1522            .await
1523            .unwrap();
1524
1525        let mut vmgs = test_vmgs_open(&path, OpenMode::ReadWrite, Some(&encryption_key))
1526            .await
1527            .unwrap();
1528
1529        vmgs_write(&mut vmgs, FileId::BIOS_NVRAM, &buf_1, true, false)
1530            .await
1531            .unwrap();
1532
1533        let read_buf = vmgs_read(&mut vmgs, FileId::BIOS_NVRAM, true)
1534            .await
1535            .unwrap();
1536
1537        assert!(read_buf == buf_1);
1538    }
1539
1540    #[cfg(with_encryption)]
1541    #[async_test]
1542    async fn test_query_encryption_update() {
1543        let (_dir, path) = new_path();
1544        let encryption_key = vec![5; 32];
1545
1546        test_vmgs_create(&path, None, false, None).await.unwrap();
1547
1548        let encryption_algorithm = test_vmgs_query_encryption(&path).await.unwrap();
1549        assert_eq!(encryption_algorithm, EncryptionAlgorithm::NONE);
1550
1551        test_vmgs_update_key(&path, EncryptionAlgorithm::AES_GCM, None, &encryption_key)
1552            .await
1553            .unwrap();
1554
1555        let encryption_algorithm = test_vmgs_query_encryption(&path).await.unwrap();
1556        assert_eq!(encryption_algorithm, EncryptionAlgorithm::AES_GCM);
1557    }
1558
1559    #[cfg(with_encryption)]
1560    #[async_test]
1561    async fn test_query_encryption_new() {
1562        let (_dir, path) = new_path();
1563        let encryption_key = vec![5; 32];
1564
1565        test_vmgs_create(
1566            &path,
1567            None,
1568            false,
1569            Some((EncryptionAlgorithm::AES_GCM, &encryption_key)),
1570        )
1571        .await
1572        .unwrap();
1573
1574        let encryption_algorithm = test_vmgs_query_encryption(&path).await.unwrap();
1575        assert_eq!(encryption_algorithm, EncryptionAlgorithm::AES_GCM);
1576    }
1577}