Commit 6c90adec authored by Johannes Hayeß's avatar Johannes Hayeß
Browse files

Add crypto routine for sending OB Megolm session keys

parent 578b3512
Pipeline #70429 failed with stages
in 2 minutes and 15 seconds
......@@ -11,6 +11,7 @@ serde_json = "1.0"
serde_derive = "1.0"
indexmap = "1.0"
ring = "0.14.6"
[dev-dependencies]
base64 = "0.10"
......@@ -14,11 +14,26 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::collections::BTreeMap;
use std::sync::mpsc::Sender;
use std::time::SystemTime;
use olm_rs::{
account::OlmAccount,
inbound_group_session::OlmInboundGroupSession,
outbound_group_session::OlmOutboundGroupSession,
session::{OlmMessageType, OlmSession},
};
use ring::rand::{SecureRandom, SystemRandom};
use serde_json;
use crate::blocker::BlockerManager;
use crate::json_objects::{
event::{
MegolmEncrypted, MegolmEncryptedPayload, OlmEncrypted, OlmPlaintext, RoomEncrypted,
RoomEncryptedContent, RoomKey,
CiphertextInfo, MegolmEncrypted, MegolmEncryptedPayload, OlmEncrypted, OlmPayload,
OlmPlaintext, RoomEncrypted, RoomEncryptedContent, RoomKey,
},
misc::MegolmPlaintextPacketBody,
olm::OlmIdentityKeys,
......@@ -27,20 +42,10 @@ use crate::json_objects::{
transport::C2MEncryptEventBody,
};
use crate::packet::{
ActionType, M2CPacket, M2CPacketType, ModuleInfoReq, SelfInfoType, StorageDesc,
ActionType, HTTPReqType, M2CPacket, M2CPacketType, ModuleInfoReq, SelfInfoType, StorageDesc,
};
use crate::session_cache::OutboundGroupSessionWithInfo;
use crate::session_cache::{InboundMegolmSessionIdentifier, SessionCache};
use olm_rs::{
account::OlmAccount,
inbound_group_session::OlmInboundGroupSession,
outbound_group_session::OlmOutboundGroupSession,
session::{OlmMessageType, OlmSession},
};
use serde_json;
use std::collections::BTreeMap;
use std::sync::mpsc::Sender;
use std::time::SystemTime;
pub enum OlmDecryptResult {
Plaintext(OlmPlaintext),
......@@ -742,3 +747,76 @@ pub fn handle_room_key(
None
}
}
/// Encrypting the Megolm session information
/// and sending it to the specified device.
///
/// Specifically for sending outbound Megolm
/// session keys.
pub fn megolm_key_encrypt_and_send(
session: &OlmSession,
olm_payload: &OlmPayload,
recipient_ed25519: String,
sender_key: String,
room_id: String,
sender: Sender<M2CPacket>,
) {
let encrypt_message_type = session.encrypt_message_type();
let olm_encrypted = session.encrypt(&serde_json::to_string(olm_payload).unwrap());
let mut ciphertext_map = serde_json::map::Map::new();
ciphertext_map.insert(
recipient_ed25519,
serde_json::to_string(&CiphertextInfo {
body: olm_encrypted,
r#type: match encrypt_message_type {
OlmMessageType::PreKey => 0,
OlmMessageType::Message => 1,
},
})
.unwrap()
.into(),
);
let encrypted_event = RoomEncrypted {
content: RoomEncryptedContent {
algorithm: "m.olm.v1.curve25519-aes-sha2".to_string(),
ciphertext: serde_json::value::Value::Object(ciphertext_map.into()),
sender_key,
device_id: None,
session_id: None,
},
r#type: "m.room.encrypted".to_string(),
room_id,
};
// send encrypted event
let mut random_buf: Vec<u8> = vec![0; 32];
{
let rng = SystemRandom::new();
rng.fill(random_buf.as_mut_slice()).unwrap();
}
// generate random TxnID
// TODO: get TxnIDs from the client, this is just
// supposed to be a dirty hack for now
let mut txn_id = "m_".to_string(); // have "m_" as a prefix
for num in random_buf {
txn_id.push_str(&format!("{:X?}", num));
}
sender
.send(M2CPacket {
header: M2CPacketType::HTTPReq(
HTTPReqType::Put,
format!(
"/_matrix/client/r0/sendToDevice/m.room.encrypted/{}",
&txn_id
),
),
body: serde_json::to_string(&encrypted_event).unwrap(),
blocker_id: None,
})
.unwrap();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment