Skip to content

Commit 1b70779

Browse files
committed
refactor(cert): remove attestation override from shared flow
1 parent 5ef09a9 commit 1b70779

4 files changed

Lines changed: 58 additions & 56 deletions

File tree

cert-client/src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,24 @@ impl CertRequestClient {
9393
}
9494
}
9595

96-
pub async fn request_cert(
96+
pub async fn request_cert(&self, key: &KeyPair, config: CertConfigV2) -> Result<Vec<String>> {
97+
let pubkey = key.public_key_der();
98+
let report_data = QuoteContentType::RaTlsCert.to_report_data(&pubkey);
99+
let attestation = ra_rpc::Attestation::quote(&report_data)
100+
.context("Failed to get quote for cert pubkey")?
101+
.into_versioned();
102+
self.sign_cert_with_attestation(key, config, attestation).await
103+
}
104+
105+
pub async fn sign_cert_with_attestation(
97106
&self,
98107
key: &KeyPair,
99108
config: CertConfigV2,
100-
attestation_override: Option<VersionedAttestation>,
109+
mut attestation: VersionedAttestation,
101110
) -> Result<Vec<String>> {
102111
let pubkey = key.public_key_der();
103112
let report_data = QuoteContentType::RaTlsCert.to_report_data(&pubkey);
104-
let attestation = match attestation_override {
105-
Some(mut attestation) => {
106-
attestation.set_report_data(report_data);
107-
attestation
108-
}
109-
None => ra_rpc::Attestation::quote(&report_data)
110-
.context("Failed to get quote for cert pubkey")?
111-
.into_versioned(),
112-
};
113+
attestation.set_report_data(report_data);
113114

114115
let csr = CertSigningRequestV2 {
115116
confirm: "please sign cert:".to_string(),

guest-agent-simulator/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use anyhow::{bail, Context, Result};
88
use clap::Parser;
99
use dstack_guest_agent::{
1010
backend::{
11-
load_versioned_attestation, simulated_attest_response, simulated_info_attestation,
11+
load_versioned_attestation, simulated_attest_response,
12+
simulated_certificate_attestation, simulated_info_attestation,
1213
simulated_quote_response, PlatformBackend,
1314
},
1415
config::{self, Config},
@@ -61,8 +62,8 @@ impl PlatformBackend for SimulatorPlatform {
6162
Ok(Some(simulated_info_attestation(&self.attestation)))
6263
}
6364

64-
fn attestation_override(&self) -> Result<Option<VersionedAttestation>> {
65-
Ok(Some(self.attestation.clone()))
65+
fn certificate_attestation(&self, pubkey: &[u8]) -> Result<VersionedAttestation> {
66+
Ok(simulated_certificate_attestation(&self.attestation, pubkey))
6667
}
6768

6869
fn quote_response(&self, report_data: [u8; 64], vm_config: &str) -> Result<GetQuoteResponse> {
@@ -120,9 +121,10 @@ mod tests {
120121
}
121122

122123
#[test]
123-
fn simulator_provides_attestation_override() {
124+
fn simulator_provides_certificate_attestation() {
124125
let platform = load_fixture_platform();
125-
assert!(platform.attestation_override().unwrap().is_some());
126+
let cert_attestation = platform.certificate_attestation(b"test-public-key").unwrap();
127+
assert!(cert_attestation.decode_app_info(false).is_ok());
126128
assert!(platform.attestation_for_info().unwrap().is_some());
127129
}
128130
}

guest-agent/src/backend.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ use dstack_attest::emit_runtime_event;
99
use dstack_guest_agent_rpc::{AttestResponse, GetQuoteResponse};
1010
use fs_err as fs;
1111
use ra_rpc::Attestation;
12-
use ra_tls::attestation::{VersionedAttestation, TDX_QUOTE_REPORT_DATA_RANGE};
12+
use ra_tls::attestation::{
13+
QuoteContentType, VersionedAttestation, TDX_QUOTE_REPORT_DATA_RANGE,
14+
};
1315

1416
pub trait PlatformBackend: Send + Sync {
1517
fn attestation_for_info(&self) -> Result<Option<Attestation>>;
16-
fn attestation_override(&self) -> Result<Option<VersionedAttestation>>;
18+
fn certificate_attestation(&self, pubkey: &[u8]) -> Result<VersionedAttestation>;
1719
fn quote_response(&self, report_data: [u8; 64], vm_config: &str) -> Result<GetQuoteResponse>;
1820
fn attest_response(&self, report_data: [u8; 64]) -> Result<AttestResponse>;
1921
fn emit_event(&self, event: &str, payload: &[u8]) -> Result<()>;
@@ -27,8 +29,11 @@ impl PlatformBackend for RealPlatform {
2729
Ok(Attestation::local().ok())
2830
}
2931

30-
fn attestation_override(&self) -> Result<Option<VersionedAttestation>> {
31-
Ok(None)
32+
fn certificate_attestation(&self, pubkey: &[u8]) -> Result<VersionedAttestation> {
33+
let report_data = QuoteContentType::RaTlsCert.to_report_data(pubkey);
34+
Ok(Attestation::quote(&report_data)
35+
.context("Failed to get quote for cert pubkey")?
36+
.into_versioned())
3237
}
3338

3439
fn quote_response(&self, report_data: [u8; 64], vm_config: &str) -> Result<GetQuoteResponse> {
@@ -97,3 +102,13 @@ pub fn simulated_attest_response(attestation: &VersionedAttestation) -> AttestRe
97102
pub fn simulated_info_attestation(attestation: &VersionedAttestation) -> Attestation {
98103
attestation.clone().into_inner()
99104
}
105+
106+
pub fn simulated_certificate_attestation(
107+
attestation: &VersionedAttestation,
108+
pubkey: &[u8],
109+
) -> VersionedAttestation {
110+
let mut attestation = attestation.clone();
111+
let report_data = QuoteContentType::RaTlsCert.to_report_data(pubkey);
112+
attestation.set_report_data(report_data);
113+
attestation
114+
}

guest-agent/src/rpc_service.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use k256::ecdsa::SigningKey;
2626
use or_panic::ResultOrPanic;
2727
use ra_rpc::{CallContext, RpcCall};
2828
use ra_tls::{
29-
attestation::{QuoteContentType, VersionedAttestation, DEFAULT_HASH_ALGORITHM},
29+
attestation::{QuoteContentType, DEFAULT_HASH_ALGORITHM},
3030
cert::CertConfigV2,
3131
kdf::{derive_key, derive_p256_key_pair_from_bytes},
3232
};
@@ -62,20 +62,25 @@ struct AppStateInner {
6262
}
6363

6464
impl AppStateInner {
65-
fn attestation_override(&self) -> Result<Option<VersionedAttestation>> {
66-
self.platform.attestation_override()
67-
}
68-
6965
fn info_attestation(&self) -> Result<Option<ra_rpc::Attestation>> {
7066
self.platform.attestation_for_info()
7167
}
7268

69+
async fn issue_cert(&self, key: &KeyPair, config: CertConfigV2) -> Result<Vec<String>> {
70+
let attestation = self
71+
.platform
72+
.certificate_attestation(&key.public_key_der())
73+
.context("Failed to get certificate attestation")?;
74+
self.cert_client
75+
.sign_cert_with_attestation(key, config, attestation)
76+
.await
77+
.context("Failed to sign the CSR")
78+
}
79+
7380
async fn request_demo_cert(&self) -> Result<String> {
7481
let key = KeyPair::generate().context("Failed to generate demo key")?;
75-
let attestation_override = self.attestation_override()?;
7682
let demo_cert = self
77-
.cert_client
78-
.request_cert(
83+
.issue_cert(
7984
&key,
8085
CertConfigV2 {
8186
org_name: None,
@@ -88,7 +93,6 @@ impl AppStateInner {
8893
not_after: None,
8994
not_before: None,
9095
},
91-
attestation_override,
9296
)
9397
.await
9498
.context("Failed to get app cert")?
@@ -260,18 +264,7 @@ impl DstackGuestRpc for InternalRpcHandler {
260264
not_after: request.not_after,
261265
not_before: request.not_before,
262266
};
263-
let attestation_override = self
264-
.state
265-
.inner
266-
.attestation_override()
267-
.context("Failed to load platform attestation override")?;
268-
let certificate_chain = self
269-
.state
270-
.inner
271-
.cert_client
272-
.request_cert(&derived_key, config, attestation_override)
273-
.await
274-
.context("Failed to sign the CSR")?;
267+
let certificate_chain = self.state.inner.issue_cert(&derived_key, config).await?;
275268
Ok(GetTlsKeyResponse {
276269
key: derived_key.serialize_pem(),
277270
certificate_chain,
@@ -497,18 +490,7 @@ impl TappdRpc for InternalRpcHandlerV0 {
497490
not_before: None,
498491
not_after: None,
499492
};
500-
let attestation_override = self
501-
.state
502-
.inner
503-
.attestation_override()
504-
.context("Failed to load platform attestation override")?;
505-
let certificate_chain = self
506-
.state
507-
.inner
508-
.cert_client
509-
.request_cert(&derived_key, config, attestation_override)
510-
.await
511-
.context("Failed to sign the CSR")?;
493+
let certificate_chain = self.state.inner.issue_cert(&derived_key, config).await?;
512494
Ok(GetTlsKeyResponse {
513495
key: derived_key.serialize_pem(),
514496
certificate_chain,
@@ -671,12 +653,14 @@ mod tests {
671653
use super::*;
672654
use crate::{
673655
backend::{
674-
load_versioned_attestation, simulated_attest_response, simulated_info_attestation,
656+
load_versioned_attestation, simulated_attest_response,
657+
simulated_certificate_attestation, simulated_info_attestation,
675658
simulated_quote_response, PlatformBackend,
676659
},
677660
config::{AppComposeWrapper, Config},
678661
};
679662
use dstack_guest_agent_rpc::{GetAttestationForAppKeyRequest, SignRequest};
663+
use ra_tls::attestation::VersionedAttestation;
680664
use dstack_types::{AppCompose, AppKeys, KeyProvider};
681665
use ed25519_dalek::ed25519::signature::hazmat::PrehashVerifier;
682666
use ed25519_dalek::{
@@ -827,8 +811,8 @@ pNs85uhOZE8z2jr8Pg==
827811
Ok(Some(simulated_info_attestation(&self.attestation)))
828812
}
829813

830-
fn attestation_override(&self) -> Result<Option<VersionedAttestation>> {
831-
Ok(Some(self.attestation.clone()))
814+
fn certificate_attestation(&self, pubkey: &[u8]) -> Result<VersionedAttestation> {
815+
Ok(simulated_certificate_attestation(&self.attestation, pubkey))
832816
}
833817

834818
fn quote_response(

0 commit comments

Comments
 (0)