Skip to content
Closed
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
9 changes: 9 additions & 0 deletions docs/app-key-epochs.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ image or restoring a compatible header backup. App rotation isolates future app
keys from an exposed old derived key, but cannot recover from compromise of the
KMS root or revoke already disclosed secrets.

Certificates issued by `SignCert` carry the root and app epoch they were issued
under in the `PHALA_RATLS_KEY_EPOCHS` extension (OID `1.3.6.1.4.1.62397.1.10`, a
MessagePack map with `root_epoch` and `app_epoch`), which `CertExt::get_key_epochs`
reads. There is no revocation list: certificates issued before a rotation stay
valid until they expire, and the requester chooses their `not_after`. A verifier
that reads the current epochs from the policy can reject a peer certificate from
an older epoch. Certificates without the extension come from a local CA or a KMS
predating it, so a verifier enforcing epochs must reject them as well.

## SDK selection

Both selectors are optional; specify zero explicitly to recover an old key.
Expand Down
4 changes: 2 additions & 2 deletions dstack/cert-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl CertRequestClient {
CertRequestClient::Local { ca } => {
let app_info = local_ca_app_info(&csr.attestation);
let cert = ca
.sign_csr(csr, app_info.as_ref(), "app:custom")
.sign_csr(csr, app_info.as_ref(), None, "app:custom")
.context("Failed to sign certificate")?;
Ok(vec![cert.pem(), ca.pem_cert.clone()])
}
Expand Down Expand Up @@ -480,7 +480,7 @@ mod tests {
let mut csr = csr_with(undecodable_attestation(), key.public_key_der());
csr.config.subject_alt_names = vec![domain.to_string()];
let leaf = app_ca
.sign_csr(&csr, Some(&app_info_for(app_id)), "app:custom")
.sign_csr(&csr, Some(&app_info_for(app_id)), None, "app:custom")
.expect("sign csr");
(
format!("{}\n{}\n{}", leaf.pem(), app_ca.pem_cert, root.pem_cert),
Expand Down
8 changes: 6 additions & 2 deletions dstack/kms/src/main_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use p256::pkcs8::DecodePrivateKey;
use ra_rpc::{CallContext, RemoteEndpoint, RpcCall};
use ra_tls::{
attestation::{resolve_vm_config, AttestationVerifier, TeeVariant, VerifiedAttestation},
cert::{CaCert, CertRequest, CertSigningRequestV1, CertSigningRequestV2, Csr},
cert::{CaCert, CertRequest, CertSigningRequestV1, CertSigningRequestV2, Csr, KeyEpochs},
kdf,
rcgen::KeyPair,
};
Expand Down Expand Up @@ -1160,8 +1160,12 @@ impl KmsRpc for RpcHandler {
let cert_app_info = attestation
.decode_app_info_ex(false, &request.vm_config)
.context("Failed to decode the verified app info")?;
let key_epochs = KeyEpochs {
root_epoch: app_info.root_epoch,
app_epoch: app_info.app_epoch,
};
let cert = app_ca
.sign_csr(&csr, Some(&cert_app_info), "app:custom")
.sign_csr(&csr, Some(&cert_app_info), Some(&key_epochs), "app:custom")
.context("Failed to sign certificate")?;
self.audit_key_release(
KeyRelease::AppCert,
Expand Down
44 changes: 42 additions & 2 deletions dstack/ra-tls/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@ use ring::signature::{
EcdsaKeyPair, UnparsedPublicKey, ECDSA_P256_SHA256_ASN1, ECDSA_P256_SHA256_ASN1_SIGNING,
};
use scale::{Decode, Encode};
use serde::{Deserialize, Serialize};
use x509_parser::der_parser::Oid;
use x509_parser::prelude::{FromDer as _, X509Certificate};
use x509_parser::public_key::PublicKey;
use x509_parser::x509::SubjectPublicKeyInfo;

use crate::oids::{
PHALA_RATLS_APP_ID, PHALA_RATLS_APP_INFO, PHALA_RATLS_ATTESTATION, PHALA_RATLS_CERT_USAGE,
PHALA_RATLS_KEY_EPOCHS,
};
use crate::traits::CertExt;
#[cfg(feature = "quote")]
use dstack_attest::attestation::QuoteContentType;
use dstack_attest::attestation::{AppInfo, Attestation, VersionedAttestation};

/// The KMS key epochs a certificate was issued under.
///
/// Certificates do not expire when an epoch is rotated, so a verifier that knows the
/// current epochs rejects older ones with this.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeyEpochs {
/// The KMS root epoch.
pub root_epoch: u32,
/// The app epoch.
pub app_epoch: u32,
}

/// A CA certificate and private key.
pub struct CaCert {
/// The original PEM certificate.
Expand Down Expand Up @@ -94,6 +108,7 @@ impl CaCert {
&self,
csr: &CertSigningRequestV2,
app_info: Option<&AppInfo>,
key_epochs: Option<&KeyEpochs>,
usage: &str,
) -> Result<Certificate> {
let pki = rcgen::SubjectPublicKeyInfo::from_der(&csr.pubkey)
Expand All @@ -118,6 +133,7 @@ impl CaCert {
.maybe_attestation(attestation)
.maybe_app_id(app_id)
.maybe_app_info(ext_app_info)
.maybe_key_epochs(key_epochs)
.special_usage(usage)
.maybe_not_before(
cfg.not_before
Expand Down Expand Up @@ -362,6 +378,7 @@ pub struct CertRequest<'a, Key> {
ca_level: Option<u8>,
app_id: Option<&'a [u8]>,
app_info: Option<&'a AppInfo>,
key_epochs: Option<&'a KeyEpochs>,
special_usage: Option<&'a str>,
attestation: Option<&'a VersionedAttestation>,
not_before: Option<SystemTime>,
Expand Down Expand Up @@ -405,6 +422,11 @@ impl<Key> CertRequest<'_, Key> {
rmp_serde::to_vec_named(&app_info).context("failed to serialize app info")?;
add_ext(&mut params, PHALA_RATLS_APP_INFO, app_info_bytes);
}
if let Some(key_epochs) = self.key_epochs {
let key_epochs_bytes =
rmp_serde::to_vec_named(key_epochs).context("failed to serialize key epochs")?;
add_ext(&mut params, PHALA_RATLS_KEY_EPOCHS, key_epochs_bytes);
}
if let Some(usage) = self.special_usage {
add_ext(&mut params, PHALA_RATLS_CERT_USAGE, usage);
}
Expand Down Expand Up @@ -797,7 +819,7 @@ mod tests {
.into_versioned(),
};

let error = match ca.sign_csr(&csr, None, "app:custom") {
let error = match ca.sign_csr(&csr, None, None, "app:custom") {
Ok(_) => panic!("an unrepresentable not_after must be rejected"),
Err(error) => error,
};
Expand Down Expand Up @@ -1004,6 +1026,24 @@ mod tests {
assert_eq!(decoded.key_provider_info, app_info.key_provider_info);
assert_eq!(decoded.init_script_hashes, app_info.init_script_hashes);
}

#[test]
fn key_epochs_survive_a_certificate_round_trip() {
let key = KeyPair::generate_for(&PKCS_ECDSA_P256_SHA256).unwrap();
let key_epochs = KeyEpochs {
root_epoch: 3,
app_epoch: 7,
};
let cert = CertRequest::builder()
.key(&key)
.subject("test.example.com")
.key_epochs(&key_epochs)
.build()
.self_signed()
.unwrap();

assert_eq!(cert.get_key_epochs().unwrap(), Some(key_epochs));
}
}

#[test]
Expand Down Expand Up @@ -1043,7 +1083,7 @@ mod tests {
}
.into_versioned(),
};
let Err(err) = CaCert::from_parts(ca_key, ca_cert).sign_csr(&csr, None, "app:custom")
let Err(err) = CaCert::from_parts(ca_key, ca_cert).sign_csr(&csr, None, None, "app:custom")
else {
panic!("app info must not be invented");
};
Expand Down
2 changes: 2 additions & 0 deletions dstack/ra-tls/src/oids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ pub const PHALA_RATLS_CERT_USAGE: &[u64] = &[1, 3, 6, 1, 4, 1, 62397, 1, 4];
pub const PHALA_RATLS_ATTESTATION: &[u64] = &[1, 3, 6, 1, 4, 1, 62397, 1, 8];
/// OID for app info
pub const PHALA_RATLS_APP_INFO: &[u64] = &[1, 3, 6, 1, 4, 1, 62397, 1, 9];
/// OID for the KMS key epochs a certificate was issued under
pub const PHALA_RATLS_KEY_EPOCHS: &[u64] = &[1, 3, 6, 1, 4, 1, 62397, 1, 10];
16 changes: 15 additions & 1 deletion dstack/ra-tls/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use anyhow::{Context, Result};
use dstack_attest::attestation::AppInfo;

use crate::oids::{PHALA_RATLS_APP_ID, PHALA_RATLS_APP_INFO, PHALA_RATLS_CERT_USAGE};
use crate::cert::KeyEpochs;
use crate::oids::{
PHALA_RATLS_APP_ID, PHALA_RATLS_APP_INFO, PHALA_RATLS_CERT_USAGE, PHALA_RATLS_KEY_EPOCHS,
};

/// Types that can get custom cert extensions from.
pub trait CertExt {
Expand Down Expand Up @@ -47,4 +50,15 @@ pub trait CertExt {
.context("failed to decode app info as msgpack")?;
Ok(app_info)
}

/// Get the KMS key epochs the certificate was issued under. `None` means it was
/// issued without them, by a local CA or a KMS predating this extension.
fn get_key_epochs(&self) -> Result<Option<KeyEpochs>> {
let Some(bytes) = self.get_extension_bytes(PHALA_RATLS_KEY_EPOCHS)? else {
return Ok(None);
};
let key_epochs =
rmp_serde::from_slice(&bytes).context("failed to decode key epochs as msgpack")?;
Ok(Some(key_epochs))
}
}
Loading