-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clean-up attestation handler
- Remove redundant clones - Clean-up error handling and setup logging to debug level as part of it - Reorganize `use` statements according to Enarx guidelines Signed-off-by: Roman Volosatovs <[email protected]>
- Loading branch information
1 parent
e7840a4
commit 0685b67
Showing
2 changed files
with
160 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
// SPDX-FileCopyrightText: 2022 Profian Inc. <[email protected]> | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use anyhow::{anyhow, bail, Result}; | ||
use anyhow::{anyhow, Result}; | ||
use der::{asn1::BitStringRef, Encode}; | ||
use pkcs8::PrivateKeyInfo; | ||
use x509::ext::Extension; | ||
use x509::request::{CertReq, CertReqInfo, ExtensionReq}; | ||
use x509::request::{CertReq, CertReqInfo}; | ||
|
||
use super::{PrivateKeyInfoExt, SubjectPublicKeyInfoExt}; | ||
use crate::ext::{kvm::Kvm, sgx::Sgx, snp::Snp, ExtVerifier}; | ||
|
||
use const_oid::db::rfc5912::ID_EXTENSION_REQ; | ||
use x509::Certificate; | ||
|
||
pub trait CertReqExt<'a> { | ||
/// Verifies that a certification request is sane. | ||
|
@@ -38,15 +33,12 @@ impl<'a> CertReqExt<'a> for CertReq<'a> { | |
} | ||
} | ||
|
||
pub trait CertReqInfoExt<'a> { | ||
pub trait CertReqInfoExt { | ||
/// Signs the `CertReqInfo` with the specified `PrivateKeyInfo` | ||
fn sign(self, pki: &PrivateKeyInfo<'_>) -> Result<Vec<u8>>; | ||
|
||
/// Check that the `CertReqInfo` | ||
fn attest(&self, issuer: &Certificate<'_>) -> Result<Vec<Extension<'a>>>; | ||
} | ||
|
||
impl<'a> CertReqInfoExt<'a> for CertReqInfo<'a> { | ||
impl<'a> CertReqInfoExt for CertReqInfo<'a> { | ||
fn sign(self, pki: &PrivateKeyInfo<'_>) -> Result<Vec<u8>> { | ||
let algo = pki.signs_with()?; | ||
let body = self.to_vec()?; | ||
|
@@ -60,42 +52,4 @@ impl<'a> CertReqInfoExt<'a> for CertReqInfo<'a> { | |
|
||
Ok(rval.to_vec()?) | ||
} | ||
|
||
fn attest(&self, issuer: &Certificate<'_>) -> Result<Vec<Extension<'a>>> { | ||
let mut extensions = Vec::new(); | ||
let mut attested = false; | ||
for attr in self.attributes.iter() { | ||
if attr.oid != ID_EXTENSION_REQ { | ||
bail!("invalid extension"); | ||
} | ||
|
||
for any in attr.values.iter() { | ||
let ereq: ExtensionReq<'_> = any.decode_into().or_else(|e| bail!(e))?; | ||
for ext in Vec::from(ereq) { | ||
// If the issuer is self-signed, we are in debug mode. | ||
let iss = &issuer.tbs_certificate; | ||
let dbg = iss.issuer_unique_id == iss.subject_unique_id; | ||
let dbg = dbg && iss.issuer == iss.subject; | ||
|
||
// Validate the extension. | ||
let (copy, att) = match ext.extn_id { | ||
Kvm::OID => (Kvm::default().verify(self, &ext, dbg), Kvm::ATT), | ||
Sgx::OID => (Sgx::default().verify(self, &ext, dbg), Sgx::ATT), | ||
Snp::OID => (Snp::default().verify(self, &ext, dbg), Snp::ATT), | ||
_ => bail!("unsupported extension"), | ||
}; | ||
|
||
// Save results. | ||
attested |= att; | ||
if copy.or_else(|e| bail!(e))? { | ||
extensions.push(ext); | ||
} | ||
} | ||
} | ||
} | ||
if !attested { | ||
bail!("attestation failed"); | ||
} | ||
Ok(extensions) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters