@@ -67,6 +67,16 @@ pub trait CommunicationBus {
6767
6868 /// Check the health of the communication bus
6969 async fn check_health ( & self ) -> Result < ComponentHealth , CommunicationError > ;
70+
71+ /// Create a properly signed internal message with real crypto
72+ fn create_internal_message (
73+ & self ,
74+ sender : AgentId ,
75+ recipient : AgentId ,
76+ payload_data : bytes:: Bytes ,
77+ message_type : MessageType ,
78+ ttl : std:: time:: Duration ,
79+ ) -> SecureMessage ;
7080}
7181
7282/// Communication bus configuration
@@ -504,33 +514,13 @@ impl DefaultCommunicationBus {
504514 request_payload : bytes:: Bytes ,
505515 timeout_duration : Duration ,
506516 ) -> Result < SecureMessage , CommunicationError > {
507- // Generate proper nonce
508- let nonce = Self :: generate_nonce ( ) ;
509-
510- // Create encrypted payload
511- let payload = EncryptedPayload {
512- data : request_payload,
513- nonce,
514- encryption_algorithm : EncryptionAlgorithm :: Aes256Gcm ,
515- } ;
516-
517- // Create a message to sign (we'll sign the payload data)
518- let message_data_to_sign = [ payload. data . as_ref ( ) , & payload. nonce ] . concat ( ) ;
519-
520- // Generate signature
521- let signature = self . sign_message_data ( & message_data_to_sign) ;
522-
523- Ok ( SecureMessage {
524- id : MessageId :: new ( ) ,
525- sender : self . system_agent_id ,
526- recipient : Some ( target_agent) ,
527- topic : None ,
528- message_type : MessageType :: Request ( request_id) ,
529- payload,
530- signature,
531- ttl : timeout_duration,
532- timestamp : SystemTime :: now ( ) ,
533- } )
517+ Ok ( self . create_internal_message (
518+ self . system_agent_id ,
519+ target_agent,
520+ request_payload,
521+ MessageType :: Request ( request_id) ,
522+ timeout_duration,
523+ ) )
534524 }
535525}
536526
@@ -758,6 +748,39 @@ impl CommunicationBus for DefaultCommunicationBus {
758748 . with_metric ( "dead_letters" . to_string ( ) , dead_letter_count. to_string ( ) )
759749 . with_metric ( "message_trackers" . to_string ( ) , tracker_count. to_string ( ) ) )
760750 }
751+
752+ fn create_internal_message (
753+ & self ,
754+ sender : AgentId ,
755+ recipient : AgentId ,
756+ payload_data : bytes:: Bytes ,
757+ message_type : MessageType ,
758+ ttl : Duration ,
759+ ) -> SecureMessage {
760+ let nonce = Self :: generate_nonce ( ) ;
761+
762+ let payload = EncryptedPayload {
763+ data : payload_data,
764+ nonce,
765+ encryption_algorithm : EncryptionAlgorithm :: Aes256Gcm ,
766+ } ;
767+
768+ // Sign the payload data concatenated with the nonce
769+ let message_data_to_sign = [ payload. data . as_ref ( ) , & payload. nonce ] . concat ( ) ;
770+ let signature = self . sign_message_data ( & message_data_to_sign) ;
771+
772+ SecureMessage {
773+ id : MessageId :: new ( ) ,
774+ sender,
775+ recipient : Some ( recipient) ,
776+ topic : None ,
777+ message_type,
778+ payload,
779+ signature,
780+ ttl,
781+ timestamp : SystemTime :: now ( ) ,
782+ }
783+ }
761784}
762785
763786/// Message queue for an agent
@@ -892,21 +915,35 @@ mod tests {
892915
893916 fn create_test_message ( sender : AgentId , recipient : AgentId ) -> SecureMessage {
894917 use crate :: types:: RequestId ;
918+ use aes_gcm:: { aead:: AeadCore , Aes256Gcm } ;
919+ use ed25519_dalek:: Signer ;
920+
921+ let mut secret_bytes = [ 0u8 ; 32 ] ;
922+ OsRng . fill_bytes ( & mut secret_bytes) ;
923+ let signing_key = SigningKey :: from_bytes ( & secret_bytes) ;
924+ let verifying_key = signing_key. verifying_key ( ) ;
925+
926+ let nonce = Aes256Gcm :: generate_nonce ( & mut OsRng ) . to_vec ( ) ;
927+ let data: bytes:: Bytes = b"test message" . to_vec ( ) . into ( ) ;
928+
929+ let message_data_to_sign = [ data. as_ref ( ) , & nonce] . concat ( ) ;
930+ let signature = signing_key. sign ( & message_data_to_sign) ;
931+
895932 SecureMessage {
896933 id : MessageId :: new ( ) ,
897934 sender,
898935 recipient : Some ( recipient) ,
899936 message_type : MessageType :: Request ( RequestId :: new ( ) ) ,
900937 topic : Some ( "test" . to_string ( ) ) ,
901938 payload : EncryptedPayload {
902- data : b"test message" . to_vec ( ) . into ( ) ,
903- nonce : [ 0u8 ; 12 ] . to_vec ( ) ,
939+ data,
940+ nonce,
904941 encryption_algorithm : EncryptionAlgorithm :: Aes256Gcm ,
905942 } ,
906943 signature : MessageSignature {
907- signature : vec ! [ 0u8 ; 64 ] ,
944+ signature : signature . to_bytes ( ) . to_vec ( ) ,
908945 algorithm : SignatureAlgorithm :: Ed25519 ,
909- public_key : vec ! [ 0u8 ; 32 ] ,
946+ public_key : verifying_key . to_bytes ( ) . to_vec ( ) ,
910947 } ,
911948 ttl : Duration :: from_secs ( 3600 ) ,
912949 timestamp : SystemTime :: now ( ) ,
@@ -1108,27 +1145,15 @@ mod tests {
11081145 assert_eq ! ( messages. len( ) , 1 ) ;
11091146 assert ! ( matches!( messages[ 0 ] . message_type, MessageType :: Request ( _) ) ) ;
11101147
1111- // Extract request ID and send response
1148+ // Extract request ID and send response using create_internal_message
11121149 if let MessageType :: Request ( request_id) = & messages[ 0 ] . message_type {
1113- let response_message = SecureMessage {
1114- id : MessageId :: new ( ) ,
1115- sender : responder,
1116- recipient : Some ( requester) ,
1117- topic : None ,
1118- message_type : MessageType :: Response ( * request_id) ,
1119- payload : EncryptedPayload {
1120- data : response_payload. clone ( ) ,
1121- nonce : vec ! [ 0u8 ; 12 ] ,
1122- encryption_algorithm : EncryptionAlgorithm :: Aes256Gcm ,
1123- } ,
1124- signature : MessageSignature {
1125- signature : vec ! [ 0u8 ; 64 ] ,
1126- algorithm : SignatureAlgorithm :: Ed25519 ,
1127- public_key : vec ! [ 0u8 ; 32 ] ,
1128- } ,
1129- ttl : Duration :: from_secs ( 3600 ) ,
1130- timestamp : SystemTime :: now ( ) ,
1131- } ;
1150+ let response_message = bus_clone. create_internal_message (
1151+ responder,
1152+ requester,
1153+ response_payload. clone ( ) ,
1154+ MessageType :: Response ( * request_id) ,
1155+ Duration :: from_secs ( 3600 ) ,
1156+ ) ;
11321157
11331158 bus_clone. send_message ( response_message) . await . unwrap ( ) ;
11341159 }
0 commit comments