Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Rustdoc

on: [push, pull_request]

permissions:
contents: read


jobs:
rdme:
name: cargo rdme
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
- run: cargo install cargo-rdme
- run: cargo rdme --check

rustdoc:
name: cargo doc
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
toolchain: 1.86.0
- run: sudo apt-get update && sudo apt-get install -y pkg-config libtss2-dev
- run: cargo doc --no-deps --all-features
513 changes: 153 additions & 360 deletions README.md

Large diffs are not rendered by default.

77 changes: 69 additions & 8 deletions src/certs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
// SPDX-License-Identifier: Apache-2.0
// This file contains code related to managing certificates. It defines a structure for managing certificate paths (`CertPaths`) and functions for obtaining extended certificates from the AMD Secure Processor.

//! Requests certificates from Host.
//!
//! This module provides the `certificate` subcommand which retrieves certificates
//! cached in hypervisor memory via the AMD Secure Processor (`SNP_GET_EXT_REPORT` ioctl),
//! converting certificate formats (PEM/DER).
//!
//! ## `certificates`
//!
//! ```bash
//! snpguest report $ATT_REPORT_PATH $REQUEST_FILE [OPTIONS]
//! ```
//!
//! The output depends on which certificates are cached in the hypervisor memory.
//! Before executing this command, the host (platform owner) must fetch certificates
//! from AMD KDS and load them into the extended configuration.
//!
//! ## Note
//!
//! In principle, the host can cache any certificate. However, in practice, it
//! typically caches only the leaf certificate (VCEK or VLEK) or the entire
//! certificate chain.
//!
//! Note that this feature is *not* supported in the current upstream kernels.
//! Actual behavior also depends on the kernel version. For example, behavior
//! when executed without loading certificates.
//!
//! ## Arguments
//!
//! | Argument | Description | Default |
//! | :-- | :-- | :-- |
//! | `$ENCODING` | The certificate encoding to store the certificates in (PEM or DER). All certificates will be in the same encoding. | *required* |
//! | `$CERTS_DIR` | The directory to store the certificates in. If certificates already exist in the provided directory, they will be overwritten. | *required* |
//!
//! ## Example
//!
//! ```bash
//! snpguest certificates pem ./certs
//! ```

use crate::fetch::Endorsement;

Expand All @@ -17,18 +55,25 @@ use sev::{
firmware::{guest::Firmware, host::CertType},
};

/// Paths to the three certificates that form an SNP certificate chain:
/// ARK (AMD Root Key), ASK (AMD SEV Key) or ASVK (AMD SEV-VLEK Key),
/// and VCEK (Versioned Chip Endorsement Key) or VLEK (Versioned Loaded Endorsement Key).
pub struct CertPaths {
/// Path to the AMD Root Key certificate.
pub ark_path: PathBuf,
/// Path to the AMD SEV Key (or ASVK for VLEK chains) certificate.
pub ask_path: PathBuf,
/// Path to the VCEK or VLEK certificate.
pub vek_path: PathBuf,
}

/// Supported certificate encoding formats.
#[derive(ValueEnum, Clone, Copy)]
pub enum CertFormat {
/// Certificates are encoded in PEM format.
/// PEM (Privacy-Enhanced Mail) encoding.
Pem,

/// Certificates are encoded in DER format.
/// DER (Distinguished Encoding Rules) binary encoding.
Der,
}

Expand All @@ -52,7 +97,10 @@ impl FromStr for CertFormat {
}
}

// Function that will convert a cert path into a snp Certificate
/// Read a certificate file and convert it into an SNP [`Certificate`].
///
/// If `cert_path` is empty, falls back to looking in `./certs/` for
/// `{cert_type}.pem` or `{cert_type}.der`.
pub fn convert_path_to_cert(
cert_path: &PathBuf,
cert_type: &str,
Expand Down Expand Up @@ -88,7 +136,7 @@ pub fn convert_path_to_cert(
Ok(Certificate::from_bytes(&buf)?)
}

// Tryfrom function that takes in 3 certificate paths returns a snp Certificate Chain
/// Build an SNP [`Chain`] from three certificate file paths (ARK, ASK/ASVK, VCEK/VLEK).
impl TryFrom<CertPaths> for Chain {
type Error = anyhow::Error;
fn try_from(content: CertPaths) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -117,7 +165,11 @@ impl TryFrom<CertPaths> for Chain {
}
}

// Function used to write provided cert into desired directory.
/// Write a certificate to a file in the specified encoding format.
///
/// The filename is derived from the certificate type and encoding
/// (e.g., `vcek.pem`, `ark.der`). For VLEK endorsement with an ASK
/// cert type, the file is named `asvk`.
pub fn write_cert(
path: &Path,
cert_type: &CertType,
Expand Down Expand Up @@ -164,17 +216,26 @@ pub fn write_cert(
Ok(())
}

/// CLI arguments for the `certificates` subcommand.
///
/// Requests certificates from the hypervisor extended memory via the AMD
/// Secure Processor and stores them in the specified directory.
#[derive(Parser)]
pub struct CertificatesArgs {
/// Specify encoding to use for certificates.
/// Certificate encoding format (PEM or DER).
#[arg(value_name = "encoding", required = true, ignore_case = true)]
pub encoding: CertFormat,

/// Directory to store certificates in. Required if requesting an extended-report.
/// Directory to store the certificates in. Created if it does not exist.
#[arg(value_name = "certs-dir", required = true)]
pub certs_dir: PathBuf,
}

/// Request extended certificates from the AMD Secure Processor and write them to disk.
///
/// Uses `SNP_GET_EXT_REPORT` to obtain certificates cached in the hypervisor
/// extended memory. Each certificate is written to the specified directory in the
/// chosen encoding format.
pub fn get_ext_certs(args: CertificatesArgs) -> Result<()> {
let mut sev_fw: Firmware = Firmware::open().context("failed to open SEV firmware device.")?;

Expand Down
18 changes: 17 additions & 1 deletion src/clparser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
// This file contains custom u32/u64 parsers for supporting decimal, hexadecimal and binary formats.

//! Custom integer parsers for CLI arguments supporting multiple radix formats.
//!
//! Provides a clap-compatible value parser that accepts integers in:
//! - Decimal (e.g., `63`)
//! - Hexadecimal with `0x` prefix (e.g., `0x3f`)
//! - Binary with `0b` prefix (e.g., `0b111111`)

use std::num::ParseIntError;

/// Parse an integer string with automatic radix detection.
///
/// Supported formats:
/// - `0x...` — hexadecimal
/// - `0b...` — binary
/// - anything else — decimal
pub fn parse_int_auto_radix<T>(s: &str) -> Result<T, ParseIntError>
where
T: FromStrRadix,
Expand All @@ -16,7 +28,11 @@ where
}
}

/// Trait for parsing integers from a string with a given radix.
///
/// Implemented for [`u32`] and [`u64`].
pub trait FromStrRadix: Sized {
/// Parse `src` as an integer in the given `radix` (2, 10, or 16).
fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>;
}

Expand Down
66 changes: 58 additions & 8 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,67 @@
// SPDX-License-Identifier: Apache-2.0
// This file contains the subcommands for displaying attestation reports and derived keys.

//! Prints attestation reports andderived keys.
//!
//! This module provides the following subcommands, which display artifacts
//! in human-readable form.
//!
//! - `display report` — Print an attestation report's contents to the terminal.
//! - `display key` — Print a derived key in hex format to the terminal.
//!
//! ## `display report`
//!
//! ```bash
//! snpguest display report $ATT_REPORT_PATH
//! ```
//!
//! Prints the attestation report contents into the terminal.
//!
//! ### Arguments
//!
//! | Argument | Description | Default |
//! | :-- | :-- | :-- |
//! | `$ATT_REPORT_PATH` | The path of the stored attestation report to display. | *required* |
//!
//! ### Example
//!
//! ```bash
//! snpguest display report report.bin
//! ```
//!
//! ## `display key`
//!
//! ```bash
//! snpguest display key $KEY_PATH
//! ```
//!
//! Prints the derived key in hex format into the terminal.
//!
//! ### Arguments
//!
//! | Argument | Description | Default |
//! | :-- | :-- | :-- |
//! | `$KEY_PATH` | The path of the stored derived key to display. | *required* |
//!
//! ### Example
//!
//! ```bash
//! snpguest display key derived-key.bin
//! ```

use super::*;
use std::path::PathBuf;

/// Subcommands for displaying attestation data in human-readable form.
#[derive(Subcommand)]
pub enum DisplayCmd {
/// Display an attestation report in console.
/// Print the contents of an attestation report to the terminal.
Report(report_display::Args),

/// Display the derived key in console.
/// Print a derived key in hex format to the terminal.
Key(key_display::Args),
}

/// Dispatch to the appropriate display subcommand handler.
pub fn cmd(cmd: DisplayCmd, quiet: bool) -> Result<()> {
match cmd {
DisplayCmd::Report(args) => report_display::display_attestation_report(args, quiet),
Expand All @@ -22,14 +71,15 @@ pub fn cmd(cmd: DisplayCmd, quiet: bool) -> Result<()> {
mod report_display {
use super::*;

/// CLI arguments for `display report`.
#[derive(Parser)]
pub struct Args {
/// Path to attestation report to display.
/// Path to the attestation report file to display.
#[arg(value_name = "att-report-path", required = true)]
pub att_report_path: PathBuf,
}

// Print attestation report in console
/// Read and print an attestation report to the console.
pub fn display_attestation_report(args: Args, quiet: bool) -> Result<()> {
let att_report = report::read_report(args.att_report_path)
.context("Could not open attestation report")?;
Expand All @@ -45,14 +95,15 @@ mod report_display {
mod key_display {
use super::*;

/// CLI arguments for `display key`.
#[derive(Parser)]
pub struct Args {
/// Path of key to be displayed.
/// Path to the derived key file to display.
#[arg(value_name = "key-path", required = true)]
pub key_path: PathBuf,
}

// Print derived key in console
/// Read and print a derived key in hex format (16 bytes per line).
pub fn display_derived_key(args: Args, quiet: bool) -> Result<()> {
let key_report = key::read_key(args.key_path).context("Could not open key")?;

Expand All @@ -62,7 +113,6 @@ mod key_display {
if (i % 16) == 0 {
keydata.push('\n');
}
//Displaying key in Hex format
keydata.push_str(&format!("{byte:02x} "));
}
keydata.push('\n');
Expand Down
Loading
Loading