@@ -490,47 +490,38 @@ async fn main() -> Result<()> {
490490 ( None , None )
491491 } ;
492492
493- // Generate ephemeral RSA key pair for secure transmission of u, v keys.
493+ // Load or generate RSA key pair for secure transmission of u, v keys.
494494 // The u, v keys are two halves of the key used to decrypt the workload after
495495 // the Identity and Integrity Quotes sent by the agent are validated
496496 // by the Tenant and Cloud Verifier, respectively.
497- debug ! ( "Generating ephemeral RSA key pair for payload mechanism" ) ;
498- let ( payload_pub_key, payload_priv_key) =
499- crypto:: rsa_generate_pair ( 2048 ) ?;
500-
501- // Generate mTLS key pair (separate from payload keys)
502- let ( mtls_pub, mtls_priv) = match config. server_key . as_ref ( ) {
503- "" => {
504- debug ! (
505- "The server_key option was not set in the configuration file"
506- ) ;
507- debug ! ( "Generating new mTLS key pair" ) ;
508- crypto:: rsa_generate_pair ( 2048 ) ?
509- }
510- path => {
511- let key_path = Path :: new ( & path) ;
512- if key_path. exists ( ) {
513- debug ! (
514- "Loading existing mTLS key pair from {}" ,
515- key_path. display( )
516- ) ;
517- crypto:: load_key_pair (
518- key_path,
519- Some ( config. server_key_password . as_ref ( ) ) ,
520- ) ?
521- } else {
522- debug ! ( "Generating new mTLS key pair" ) ;
523- let ( public, private) = crypto:: rsa_generate_pair ( 2048 ) ?;
524- // Write the generated key to the file
525- crypto:: write_key_pair (
526- & private,
527- key_path,
528- Some ( config. server_key_password . as_ref ( ) ) ,
529- ) ;
530- ( public, private)
531- }
532- }
533- } ;
497+ // The payload key is always persistent, stored at the configured path.
498+ let key_path = Path :: new ( & config. payload_key ) ;
499+ let ( payload_pub_key, payload_priv_key) = crypto:: load_or_generate_key (
500+ key_path,
501+ Some ( config. payload_key_password . as_ref ( ) ) ,
502+ keylime:: algorithms:: EncryptionAlgorithm :: Rsa2048 ,
503+ true , // Validate that loaded keys are RSA 2048
504+ )
505+ . map_err ( |e| {
506+ error ! (
507+ "Failed to load or generate payload key from {}: {e}" ,
508+ key_path. display( )
509+ ) ;
510+ Error :: Configuration ( config:: KeylimeConfigError :: Generic ( format ! (
511+ "Failed to load or generate payload key from {}: {e}" ,
512+ key_path. display( )
513+ ) ) )
514+ } ) ?;
515+
516+ // Load or generate mTLS key pair (separate from payload keys)
517+ // The mTLS key is always persistent, stored at the configured path.
518+ let key_path = Path :: new ( & config. server_key ) ;
519+ let ( mtls_pub, mtls_priv) = crypto:: load_or_generate_key (
520+ key_path,
521+ Some ( config. server_key_password . as_ref ( ) ) ,
522+ keylime:: algorithms:: EncryptionAlgorithm :: Rsa2048 ,
523+ false , // Don't validate algorithm for mTLS keys (for backward compatibility)
524+ ) ?;
534525
535526 let cert: X509 ;
536527 let mtls_cert;
@@ -1000,7 +991,7 @@ mod testing {
1000991 let ( mtls_pub, mtls_priv) =
1001992 crypto:: testing:: rsa_import_pair ( rsa_key_path. clone ( ) ) ?;
1002993
1003- // Generate separate ephemeral payload keys for testing
994+ // Generate ephemeral payload keys for testing
1004995 debug ! ( "Generating ephemeral RSA key pair for payload mechanism" ) ;
1005996 let ( payload_pub_key, payload_priv_key) =
1006997 crypto:: rsa_generate_pair ( 2048 ) ?;
0 commit comments