Skip to content

Commit

Permalink
Merge pull request virtee#160 from larrydewey/cert-sorting
Browse files Browse the repository at this point in the history
Certificates: Adding Ord and PartialOrd
  • Loading branch information
larrydewey authored Feb 29, 2024
2 parents 2444598 + 5eb3969 commit ce8dd75
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sev"
version = "3.0.0"
version = "3.1.0"
authors = [
"Nathaniel McCallum <npmccallum@redhat.com>",
"The VirTee Project Developers",
Expand Down
104 changes: 104 additions & 0 deletions src/firmware/host/types/snp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,38 @@ impl TryFrom<&uuid::Uuid> for CertType {
}
}

impl Ord for CertType {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) {
(Self::ARK, Self::ARK)
| (Self::ASK, Self::ASK)
| (Self::VCEK, Self::VCEK)
| (Self::VLEK, Self::VLEK)
| (Self::CRL, Self::CRL)
| (Self::Empty, Self::Empty) => std::cmp::Ordering::Equal,
(Self::OTHER(left), Self::OTHER(right)) => left.cmp(right),
(Self::Empty, _) => std::cmp::Ordering::Greater,
(_, Self::Empty) => std::cmp::Ordering::Less,
(Self::OTHER(_), _) => std::cmp::Ordering::Greater,
(_, Self::OTHER(_)) => std::cmp::Ordering::Less,
(Self::CRL, _) => std::cmp::Ordering::Greater,
(_, Self::CRL) => std::cmp::Ordering::Less,
(Self::ASK, _) => std::cmp::Ordering::Greater,
(_, Self::ASK) => std::cmp::Ordering::Less,
(Self::VLEK, _) => std::cmp::Ordering::Greater,
(_, Self::VLEK) => std::cmp::Ordering::Less,
(Self::VCEK, _) => std::cmp::Ordering::Greater,
(_, Self::VCEK) => std::cmp::Ordering::Less,
}
}
}

impl PartialOrd for CertType {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[repr(C)]
/// An entry with information regarding a specific certificate.
Expand Down Expand Up @@ -153,6 +185,18 @@ impl CertTableEntry {
}
}

impl Ord for CertTableEntry {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.cert_type.cmp(&other.cert_type)
}
}

impl PartialOrd for CertTableEntry {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cert_type.cmp(&other.cert_type))
}
}

/// Information regarding the SEV-SNP platform's TCB version.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub struct TcbStatus {
Expand Down Expand Up @@ -348,3 +392,63 @@ impl Display for MaskId {
)
}
}

#[cfg(test)]
mod tests {
use super::CertType;
use uuid::Uuid;

#[test]
fn test_cert_type_sort_vcek() {
let mut certs: Vec<CertType> = vec![
CertType::Empty,
CertType::CRL,
CertType::OTHER(Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap()),
CertType::OTHER(Uuid::parse_str("33333333-3333-3333-3333-333333333333").unwrap()),
CertType::OTHER(Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap()),
CertType::ARK,
CertType::ASK,
CertType::VCEK,
];

let sorted_certs: Vec<CertType> = vec![
CertType::ARK,
CertType::VCEK,
CertType::ASK,
CertType::CRL,
CertType::OTHER(Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap()),
CertType::OTHER(Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap()),
CertType::OTHER(Uuid::parse_str("33333333-3333-3333-3333-333333333333").unwrap()),
CertType::Empty,
];
certs.sort();
assert_eq!(certs, sorted_certs);
}

#[test]
fn test_cert_type_sort_vlek() {
let mut certs: Vec<CertType> = vec![
CertType::Empty,
CertType::CRL,
CertType::OTHER(Uuid::parse_str("33333333-3333-3333-3333-333333333333").unwrap()),
CertType::OTHER(Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap()),
CertType::OTHER(Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap()),
CertType::ARK,
CertType::ASK,
CertType::VLEK,
];

let sorted_certs: Vec<CertType> = vec![
CertType::ARK,
CertType::VLEK,
CertType::ASK,
CertType::CRL,
CertType::OTHER(Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap()),
CertType::OTHER(Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap()),
CertType::OTHER(Uuid::parse_str("33333333-3333-3333-3333-333333333333").unwrap()),
CertType::Empty,
];
certs.sort();
assert_eq!(certs, sorted_certs);
}
}

0 comments on commit ce8dd75

Please sign in to comment.