Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEV generation string handling #133

Merged
merged 5 commits into from
Feb 16, 2024
Merged
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
75 changes: 74 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,19 @@ use certs::sev::sev;
#[cfg(all(feature = "sev", feature = "openssl"))]
use certs::sev::ca::{Certificate, Chain as CertSevCaChain};

#[cfg(all(not(feature = "sev"), feature = "snp", feature = "openssl"))]
use certs::snp::ca::Chain as CertSnpCaChain;

#[cfg(all(feature = "sev", feature = "openssl"))]
use certs::sev::builtin as SevBuiltin;

#[cfg(all(not(feature = "sev"), feature = "snp", feature = "openssl"))]
use certs::snp::builtin as SnpBuiltin;

#[cfg(feature = "sev")]
use crate::{certs::sev::sev::Certificate as SevCertificate, error::Indeterminate, launch::sev::*};

#[cfg(all(feature = "sev", feature = "openssl"))]
#[cfg(any(feature = "sev", feature = "snp"))]
use std::convert::TryFrom;

use std::io::{Read, Write};
Expand Down Expand Up @@ -282,6 +288,24 @@ impl From<Generation> for CertSevCaChain {
}
}

#[cfg(all(not(feature = "sev"), feature = "snp", feature = "openssl"))]
impl From<Generation> for CertSnpCaChain {
fn from(gen: Generation) -> CertSnpCaChain {
let (ark, ask) = match gen {
Generation::Milan => (
SnpBuiltin::milan::ark().unwrap(),
SnpBuiltin::milan::ask().unwrap(),
),
Generation::Genoa => (
SnpBuiltin::genoa::ark().unwrap(),
SnpBuiltin::genoa::ask().unwrap(),
),
};

CertSnpCaChain { ark, ask }
}
}

#[cfg(all(feature = "sev", feature = "openssl"))]
impl TryFrom<&sev::Chain> for Generation {
type Error = ();
Expand All @@ -308,6 +332,55 @@ impl TryFrom<&sev::Chain> for Generation {
}
}

#[cfg(any(feature = "sev", feature = "snp"))]
impl TryFrom<String> for Generation {
type Error = ();

fn try_from(val: String) -> Result<Self, Self::Error> {
match &val.to_lowercase()[..] {
#[cfg(feature = "sev")]
"naples" => Ok(Self::Naples),

#[cfg(feature = "sev")]
"rome" => Ok(Self::Rome),

#[cfg(any(feature = "sev", feature = "snp"))]
"milan" => Ok(Self::Milan),

#[cfg(any(feature = "sev", feature = "snp"))]
"genoa" => Ok(Self::Genoa),

#[cfg(any(feature = "sev", feature = "snp"))]
"bergamo" => Ok(Self::Genoa),

#[cfg(any(feature = "sev", feature = "snp"))]
"siena" => Ok(Self::Genoa),

_ => Err(()),
}
}
}

#[cfg(any(feature = "sev", feature = "snp"))]
impl Generation {
/// Create a title-cased string identifying the SEV generation.
pub fn titlecase(&self) -> String {
match self {
#[cfg(feature = "sev")]
Self::Naples => "Naples".to_string(),

#[cfg(feature = "sev")]
Self::Rome => "Rome".to_string(),

#[cfg(any(feature = "sev", feature = "snp"))]
Self::Milan => "Milan".to_string(),

#[cfg(any(feature = "sev", feature = "snp"))]
Self::Genoa => "Genoa".to_string(),
}
}
}

// The C FFI interface to the library.

#[cfg(feature = "sev")]
Expand Down
2 changes: 1 addition & 1 deletion src/measurement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Everything one needs to calculate a launch measurement for a SEV encrypted confidential guest.
//! This includes, GCTX, SEV-HASHES, VMSA and OVMF pages.

#[cfg(all(any(feature = "sev", feature = "snp"), feature = "openssl"))]
#[cfg(all(feature = "snp", feature = "openssl"))]
pub mod gctx;

#[cfg(any(feature = "sev", feature = "snp"))]
Expand Down
Loading