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

feat: handle many cert requests #55

Merged
merged 2 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 50 additions & 4 deletions src/crypto/certreq.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com>
// SPDX-License-Identifier: AGPL-3.0-only

use anyhow::{anyhow, Result};
use anyhow::{anyhow, bail, Result};
use der::{asn1::BitStringRef, Encode};
use pkcs8::PrivateKeyInfo;
use x509::request::{CertReq, CertReqInfo};
use x509::ext::Extension;
use x509::request::{CertReq, CertReqInfo, ExtensionReq};

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;
rvolosatovs marked this conversation as resolved.
Show resolved Hide resolved

pub trait CertReqExt<'a> {
/// Verifies that a certification request is sane.
Expand All @@ -33,12 +38,15 @@ impl<'a> CertReqExt<'a> for CertReq<'a> {
}
}

pub trait CertReqInfoExt {
pub trait CertReqInfoExt<'a> {
/// Signs the `CertReqInfo` with the specified `PrivateKeyInfo`
fn sign(self, pki: &PrivateKeyInfo<'_>) -> Result<Vec<u8>>;

/// Check that the `CertReqInfo`
rvolosatovs marked this conversation as resolved.
Show resolved Hide resolved
fn attest(&self, issuer: &Certificate<'_>) -> Result<Vec<Extension<'a>>>;
}

impl<'a> CertReqInfoExt for CertReqInfo<'a> {
impl<'a> CertReqInfoExt<'a> for CertReqInfo<'a> {
fn sign(self, pki: &PrivateKeyInfo<'_>) -> Result<Vec<u8>> {
let algo = pki.signs_with()?;
let body = self.to_vec()?;
Expand All @@ -52,4 +60,42 @@ impl<'a> CertReqInfoExt 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)
}
}
Loading