@@ -22,8 +22,9 @@ use ra_tls::{
2222} ;
2323use scale:: Decode ;
2424use sha2:: Digest ;
25+ use tokio:: sync:: OnceCell ;
2526use tracing:: info;
26- use upgrade_authority:: { build_boot_info, BootInfo } ;
27+ use upgrade_authority:: { build_boot_info, local_kms_boot_info , BootInfo } ;
2728
2829use crate :: {
2930 config:: KmsConfig ,
@@ -52,6 +53,7 @@ pub struct KmsStateInner {
5253 temp_ca_cert : String ,
5354 temp_ca_key : String ,
5455 verifier : CvmVerifier ,
56+ self_boot_info : OnceCell < BootInfo > ,
5557}
5658
5759impl KmsState {
@@ -79,6 +81,7 @@ impl KmsState {
7981 temp_ca_cert,
8082 temp_ca_key,
8183 verifier,
84+ self_boot_info : OnceCell :: new ( ) ,
8285 } ) ,
8386 } )
8487 }
@@ -95,6 +98,31 @@ struct BootConfig {
9598}
9699
97100impl RpcHandler {
101+ async fn ensure_self_allowed ( & self ) -> Result < ( ) > {
102+ if !self . state . config . onboard . quote_enabled {
103+ return Ok ( ( ) ) ;
104+ }
105+ let boot_info = self
106+ . state
107+ . self_boot_info
108+ . get_or_try_init ( || async {
109+ local_kms_boot_info ( self . state . config . pccs_url . as_deref ( ) ) . await
110+ } )
111+ . await
112+ . context ( "Failed to load cached self boot info" ) ?;
113+ let response = self
114+ . state
115+ . config
116+ . auth_api
117+ . is_app_allowed ( boot_info, true )
118+ . await
119+ . context ( "Failed to call self KMS auth check" ) ?;
120+ if !response. is_allowed {
121+ bail ! ( "KMS is not allowed: {}" , response. reason) ;
122+ }
123+ Ok ( ( ) )
124+ }
125+
98126 fn ensure_attested ( & self ) -> Result < & VerifiedAttestation > {
99127 let Some ( attestation) = & self . attestation else {
100128 bail ! ( "No attestation provided" ) ;
@@ -214,6 +242,9 @@ impl KmsRpc for RpcHandler {
214242 if request. api_version > 1 {
215243 bail ! ( "Unsupported API version: {}" , request. api_version) ;
216244 }
245+ self . ensure_self_allowed ( )
246+ . await
247+ . context ( "KMS self authorization failed" ) ?;
217248 let BootConfig {
218249 boot_info,
219250 gateway_app_id,
@@ -254,6 +285,9 @@ impl KmsRpc for RpcHandler {
254285 }
255286
256287 async fn get_app_env_encrypt_pub_key ( self , request : AppId ) -> Result < PublicKeyResponse > {
288+ self . ensure_self_allowed ( )
289+ . await
290+ . context ( "KMS self authorization failed" ) ?;
257291 let secret = kdf:: derive_dh_secret (
258292 & self . state . root_ca . key ,
259293 & [ & request. app_id [ ..] , "env-encrypt-key" . as_bytes ( ) ] ,
@@ -320,6 +354,9 @@ impl KmsRpc for RpcHandler {
320354 }
321355
322356 async fn get_kms_key ( self , request : GetKmsKeyRequest ) -> Result < KmsKeyResponse > {
357+ self . ensure_self_allowed ( )
358+ . await
359+ . context ( "KMS self authorization failed" ) ?;
323360 if self . state . config . onboard . quote_enabled {
324361 let _info = self . ensure_kms_allowed ( & request. vm_config ) . await ?;
325362 }
@@ -333,6 +370,9 @@ impl KmsRpc for RpcHandler {
333370 }
334371
335372 async fn get_temp_ca_cert ( self ) -> Result < GetTempCaCertResponse > {
373+ self . ensure_self_allowed ( )
374+ . await
375+ . context ( "KMS self authorization failed" ) ?;
336376 Ok ( GetTempCaCertResponse {
337377 temp_ca_cert : self . state . inner . temp_ca_cert . clone ( ) ,
338378 temp_ca_key : self . state . inner . temp_ca_key . clone ( ) ,
@@ -341,6 +381,9 @@ impl KmsRpc for RpcHandler {
341381 }
342382
343383 async fn sign_cert ( self , request : SignCertRequest ) -> Result < SignCertResponse > {
384+ self . ensure_self_allowed ( )
385+ . await
386+ . context ( "KMS self authorization failed" ) ?;
344387 let csr = match request. api_version {
345388 1 => {
346389 let csr = CertSigningRequestV1 :: decode ( & mut & request. csr [ ..] )
0 commit comments