diff --git a/docs/app-key-epochs.md b/docs/app-key-epochs.md index a358c79fe..92d3024d4 100644 --- a/docs/app-key-epochs.md +++ b/docs/app-key-epochs.md @@ -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. diff --git a/dstack/cert-client/src/lib.rs b/dstack/cert-client/src/lib.rs index d5055016d..9b55810ac 100644 --- a/dstack/cert-client/src/lib.rs +++ b/dstack/cert-client/src/lib.rs @@ -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()]) } @@ -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), diff --git a/dstack/kms/src/main_service.rs b/dstack/kms/src/main_service.rs index 8295d20d8..2d6f218f9 100644 --- a/dstack/kms/src/main_service.rs +++ b/dstack/kms/src/main_service.rs @@ -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, }; @@ -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, diff --git a/dstack/ra-tls/src/cert.rs b/dstack/ra-tls/src/cert.rs index 5c748cad5..b7524ea78 100644 --- a/dstack/ra-tls/src/cert.rs +++ b/dstack/ra-tls/src/cert.rs @@ -18,6 +18,7 @@ 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; @@ -25,12 +26,25 @@ 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. @@ -94,6 +108,7 @@ impl CaCert { &self, csr: &CertSigningRequestV2, app_info: Option<&AppInfo>, + key_epochs: Option<&KeyEpochs>, usage: &str, ) -> Result { let pki = rcgen::SubjectPublicKeyInfo::from_der(&csr.pubkey) @@ -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 @@ -362,6 +378,7 @@ pub struct CertRequest<'a, Key> { ca_level: Option, 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, @@ -405,6 +422,11 @@ impl 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); } @@ -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, }; @@ -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] @@ -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"); }; diff --git a/dstack/ra-tls/src/oids.rs b/dstack/ra-tls/src/oids.rs index 3535ac1fb..d9e777d4c 100644 --- a/dstack/ra-tls/src/oids.rs +++ b/dstack/ra-tls/src/oids.rs @@ -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]; diff --git a/dstack/ra-tls/src/traits.rs b/dstack/ra-tls/src/traits.rs index 0916039c1..ed4acc7b7 100644 --- a/dstack/ra-tls/src/traits.rs +++ b/dstack/ra-tls/src/traits.rs @@ -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 { @@ -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> { + 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)) + } }