-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathmain_service.rs
More file actions
450 lines (412 loc) · 15.1 KB
/
main_service.rs
File metadata and controls
450 lines (412 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// SPDX-FileCopyrightText: © 2024-2025 Phala Network <dstack@phala.network>
//
// SPDX-License-Identifier: Apache-2.0
use std::{path::PathBuf, sync::Arc};
use anyhow::{bail, Context, Result};
use dstack_kms_rpc::{
kms_server::{KmsRpc, KmsServer},
AppId, AppKeyResponse, ClearImageCacheRequest, GetAppKeyRequest, GetKmsKeyRequest,
GetMetaResponse, GetTempCaCertResponse, KmsKeyResponse, KmsKeys, PublicKeyResponse,
SignCertRequest, SignCertResponse,
};
use dstack_verifier::{CvmVerifier, VerificationDetails};
use fs_err as fs;
use k256::ecdsa::SigningKey;
use ra_rpc::{CallContext, RpcCall};
use ra_tls::{
attestation::VerifiedAttestation,
cert::{CaCert, CertRequest, CertSigningRequestV1, CertSigningRequestV2, Csr},
kdf,
};
use scale::Decode;
use sha2::Digest;
use tokio::sync::OnceCell;
use tracing::info;
use upgrade_authority::{build_boot_info, local_kms_boot_info, BootInfo};
use crate::{
config::KmsConfig,
crypto::{derive_k256_key, sign_message, sign_message_with_timestamp},
};
pub(crate) mod upgrade_authority;
#[derive(Clone)]
pub struct KmsState {
inner: Arc<KmsStateInner>,
}
impl std::ops::Deref for KmsState {
type Target = KmsStateInner;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
pub struct KmsStateInner {
config: KmsConfig,
root_ca: CaCert,
k256_key: SigningKey,
temp_ca_cert: String,
temp_ca_key: String,
verifier: CvmVerifier,
self_boot_info: OnceCell<BootInfo>,
}
impl KmsState {
pub fn new(config: KmsConfig) -> Result<Self> {
let root_ca = CaCert::load(config.root_ca_cert(), config.root_ca_key())
.context("Failed to load root CA certificate")?;
let key_bytes = fs::read(config.k256_key()).context("Failed to read ECDSA root key")?;
let k256_key =
SigningKey::from_slice(&key_bytes).context("Failed to load ECDSA root key")?;
let temp_ca_key =
fs::read_to_string(config.tmp_ca_key()).context("Faeild to read temp ca key")?;
let temp_ca_cert =
fs::read_to_string(config.tmp_ca_cert()).context("Faeild to read temp ca cert")?;
let verifier = CvmVerifier::new(
config.image.cache_dir.display().to_string(),
config.image.download_url.clone(),
config.image.download_timeout,
config.pccs_url.clone(),
);
Ok(Self {
inner: Arc::new(KmsStateInner {
config,
root_ca,
k256_key,
temp_ca_cert,
temp_ca_key,
verifier,
self_boot_info: OnceCell::new(),
}),
})
}
}
pub struct RpcHandler {
state: KmsState,
attestation: Option<VerifiedAttestation>,
}
struct BootConfig {
boot_info: BootInfo,
gateway_app_id: String,
}
impl RpcHandler {
async fn ensure_self_allowed(&self) -> Result<()> {
if !self.state.config.onboard.quote_enabled {
return Ok(());
}
let boot_info = self
.state
.self_boot_info
.get_or_try_init(|| local_kms_boot_info(self.state.config.pccs_url.as_deref()))
.await
.context("Failed to load cached self boot info")?;
let response = self
.state
.config
.auth_api
.is_app_allowed(boot_info, true)
.await
.context("Failed to call self KMS auth check")?;
if !response.is_allowed {
bail!("KMS is not allowed: {}", response.reason);
}
Ok(())
}
fn ensure_attested(&self) -> Result<&VerifiedAttestation> {
let Some(attestation) = &self.attestation else {
bail!("No attestation provided");
};
Ok(attestation)
}
async fn ensure_kms_allowed(&self, vm_config: &str) -> Result<BootInfo> {
let att = self.ensure_attested()?;
self.ensure_app_attestation_allowed(att, true, false, vm_config)
.await
.map(|c| c.boot_info)
}
async fn ensure_app_boot_allowed(&self, vm_config: &str) -> Result<BootConfig> {
let att = self.ensure_attested()?;
self.ensure_app_attestation_allowed(att, false, false, vm_config)
.await
}
fn image_cache_dir(&self) -> PathBuf {
self.state.config.image.cache_dir.join("images")
}
fn remove_cache(&self, parent_dir: &PathBuf, sub_dir: &str) -> Result<()> {
if sub_dir.is_empty() {
return Ok(());
}
if sub_dir == "all" {
fs::remove_dir_all(parent_dir)?;
} else {
let path = parent_dir.join(sub_dir);
if path.is_dir() {
fs::remove_dir_all(path)?;
} else {
fs::remove_file(path)?;
}
}
Ok(())
}
fn ensure_admin(&self, token: &str) -> Result<()> {
let token_hash = sha2::Sha256::new_with_prefix(token).finalize();
if token_hash.as_slice() != self.state.config.admin_token_hash.as_slice() {
bail!("Invalid token");
}
Ok(())
}
async fn verify_os_image_hash(
&self,
vm_config: String,
report: &VerifiedAttestation,
) -> Result<()> {
if !self.state.config.image.verify {
info!("Image verification is disabled");
return Ok(());
}
let mut detail = VerificationDetails::default();
self.state
.verifier
.verify_os_image_hash(vm_config, report, false, &mut detail)
.await
.context("Failed to verify os image hash")?;
Ok(())
}
async fn ensure_app_attestation_allowed(
&self,
att: &VerifiedAttestation,
is_kms: bool,
use_boottime_mr: bool,
vm_config_str: &str,
) -> Result<BootConfig> {
let boot_info = build_boot_info(att, use_boottime_mr, vm_config_str)?;
let response = self
.state
.config
.auth_api
.is_app_allowed(&boot_info, is_kms)
.await?;
if !response.is_allowed {
bail!("Boot denied: {}", response.reason);
}
self.verify_os_image_hash(vm_config_str.into(), att)
.await
.context("Failed to verify os image hash")?;
Ok(BootConfig {
boot_info,
gateway_app_id: response.gateway_app_id,
})
}
fn derive_app_ca(&self, app_id: &[u8]) -> Result<CaCert> {
let context_data = vec![app_id, b"app-ca"];
let app_key = kdf::derive_p256_key_pair(&self.state.root_ca.key, &context_data)
.context("Failed to derive app disk key")?;
let req = CertRequest::builder()
.key(&app_key)
.org_name("Dstack")
.subject("Dstack App CA")
.ca_level(0)
.app_id(app_id)
.special_usage("app:ca")
.build();
let app_ca = self
.state
.root_ca
.sign(req)
.context("Failed to sign App CA")?;
Ok(CaCert::from_parts(app_key, app_ca))
}
}
impl KmsRpc for RpcHandler {
async fn get_app_key(self, request: GetAppKeyRequest) -> Result<AppKeyResponse> {
if request.api_version > 1 {
bail!("Unsupported API version: {}", request.api_version);
}
self.ensure_self_allowed()
.await
.context("KMS self authorization failed")?;
let BootConfig {
boot_info,
gateway_app_id,
} = self
.ensure_app_boot_allowed(&request.vm_config)
.await
.context("App not allowed")?;
let app_id = boot_info.app_id;
let instance_id = boot_info.instance_id;
let context_data = vec![&app_id[..], &instance_id[..], b"app-disk-crypt-key"];
let app_disk_key = kdf::derive_dh_secret(&self.state.root_ca.key, &context_data)
.context("Failed to derive app disk key")?;
let env_crypt_key = {
let secret =
kdf::derive_dh_secret(&self.state.root_ca.key, &[&app_id[..], b"env-encrypt-key"])
.context("Failed to derive env encrypt key")?;
let secret = x25519_dalek::StaticSecret::from(secret);
secret.to_bytes()
};
let (k256_key, k256_signature) = {
let (k256_app_key, signature) = derive_k256_key(&self.state.k256_key, &app_id)
.context("Failed to derive app ecdsa key")?;
(k256_app_key.to_bytes().to_vec(), signature)
};
Ok(AppKeyResponse {
ca_cert: self.state.root_ca.pem_cert.clone(),
disk_crypt_key: app_disk_key.to_vec(),
env_crypt_key: env_crypt_key.to_vec(),
k256_key,
k256_signature,
tproxy_app_id: gateway_app_id.clone(),
gateway_app_id,
os_image_hash: boot_info.os_image_hash,
})
}
async fn get_app_env_encrypt_pub_key(self, request: AppId) -> Result<PublicKeyResponse> {
self.ensure_self_allowed()
.await
.context("KMS self authorization failed")?;
let secret = kdf::derive_dh_secret(
&self.state.root_ca.key,
&[&request.app_id[..], "env-encrypt-key".as_bytes()],
)
.context("Failed to derive env encrypt key")?;
let secret = x25519_dalek::StaticSecret::from(secret);
let pubkey = x25519_dalek::PublicKey::from(&secret);
let public_key = pubkey.to_bytes().to_vec();
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.context("System time before UNIX epoch")?
.as_secs();
// Legacy signature (without timestamp) for backward compatibility
let signature = sign_message(
&self.state.k256_key,
b"dstack-env-encrypt-pubkey",
&request.app_id,
&public_key,
)
.context("Failed to sign the public key")?;
// New signature with timestamp to prevent replay attacks
let signature_v1 = sign_message_with_timestamp(
&self.state.k256_key,
b"dstack-env-encrypt-pubkey",
&request.app_id,
timestamp,
&public_key,
)
.context("Failed to sign the public key with timestamp")?;
Ok(PublicKeyResponse {
public_key,
signature,
timestamp,
signature_v1,
})
}
async fn get_meta(self) -> Result<GetMetaResponse> {
let bootstrap_info = fs::read_to_string(self.state.config.bootstrap_info())
.ok()
.and_then(|s| serde_json::from_str(&s).ok());
let info = self.state.config.auth_api.get_info().await?;
Ok(GetMetaResponse {
ca_cert: self.state.inner.root_ca.pem_cert.clone(),
allow_any_upgrade: self.state.inner.config.auth_api.is_dev(),
k256_pubkey: self
.state
.inner
.k256_key
.verifying_key()
.to_sec1_bytes()
.to_vec(),
bootstrap_info,
is_dev: self.state.config.auth_api.is_dev(),
kms_contract_address: info.kms_contract_address,
chain_id: info.chain_id,
gateway_app_id: info.gateway_app_id,
app_auth_implementation: info.app_implementation,
})
}
async fn get_kms_key(self, request: GetKmsKeyRequest) -> Result<KmsKeyResponse> {
self.ensure_self_allowed()
.await
.context("KMS self authorization failed")?;
if self.state.config.onboard.quote_enabled {
let _info = self.ensure_kms_allowed(&request.vm_config).await?;
}
Ok(KmsKeyResponse {
temp_ca_key: self.state.inner.temp_ca_key.clone(),
keys: vec![KmsKeys {
ca_key: self.state.inner.root_ca.key.serialize_pem(),
k256_key: self.state.inner.k256_key.to_bytes().to_vec(),
}],
})
}
async fn get_temp_ca_cert(self) -> Result<GetTempCaCertResponse> {
self.ensure_self_allowed()
.await
.context("KMS self authorization failed")?;
Ok(GetTempCaCertResponse {
temp_ca_cert: self.state.inner.temp_ca_cert.clone(),
temp_ca_key: self.state.inner.temp_ca_key.clone(),
ca_cert: self.state.inner.root_ca.pem_cert.clone(),
})
}
async fn sign_cert(self, request: SignCertRequest) -> Result<SignCertResponse> {
self.ensure_self_allowed()
.await
.context("KMS self authorization failed")?;
let csr = match request.api_version {
1 => {
let csr = CertSigningRequestV1::decode(&mut &request.csr[..])
.context("Failed to parse csr")?;
csr.verify(&request.signature)
.context("Failed to verify csr signature")?;
csr.try_into().context("Failed to upgrade csr v1 to v2")?
}
2 => {
let csr = CertSigningRequestV2::decode(&mut &request.csr[..])
.context("Failed to parse csr")?;
csr.verify(&request.signature)
.context("Failed to verify csr signature")?;
csr
}
_ => bail!("Unsupported API version: {}", request.api_version),
};
let attestation = csr
.attestation
.clone()
.into_inner()
.verify_with_ra_pubkey(&csr.pubkey, self.state.config.pccs_url.as_deref())
.await
.context("Quote verification failed")?;
let app_info = self
.ensure_app_attestation_allowed(&attestation, false, true, &request.vm_config)
.await?;
let app_ca = self.derive_app_ca(&app_info.boot_info.app_id)?;
let cert = app_ca
.sign_csr(&csr, Some(&app_info.boot_info.app_id), "app:custom")
.context("Failed to sign certificate")?;
Ok(SignCertResponse {
certificate_chain: vec![
cert.pem(),
app_ca.pem_cert.clone(),
self.state.root_ca.pem_cert.clone(),
],
})
}
async fn clear_image_cache(self, request: ClearImageCacheRequest) -> Result<()> {
self.ensure_admin(&request.token)?;
self.remove_cache(&self.image_cache_dir(), &request.image_hash)
.context("Failed to clear image cache")?;
// Clear measurement cache (now handled by verifier's cache in measurements/ dir)
let mr_cache_dir = self.state.config.image.cache_dir.join("measurements");
self.remove_cache(&mr_cache_dir, &request.config_hash)
.context("Failed to clear measurement cache")?;
Ok(())
}
}
impl RpcCall<KmsState> for RpcHandler {
type PrpcService = KmsServer<Self>;
fn construct(context: CallContext<'_, KmsState>) -> Result<Self> {
Ok(RpcHandler {
state: context.state.clone(),
attestation: context.attestation,
})
}
}
pub fn rpc_methods() -> &'static [&'static str] {
<KmsServer<RpcHandler>>::supported_methods()
}