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
// Copyright 2021 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use serde::{Deserialize, Serialize};
use super::{message::MegolmMessage, ratchet::Ratchet, session_keys::SessionKey};
use crate::{
cipher::Cipher,
types::Ed25519Keypair,
utilities::{pickle, unpickle},
PickleError,
};
/// A Megolm group session represents a single sending participant in an
/// encrypted group communication context containing multiple receiving parties.
///
/// A group session consists of a ratchet, used for encryption, and an Ed25519
/// signing key pair, used for authenticity.
///
/// A group session containing the signing key pair is also known as an
/// "outbound" group session. We differentiate this from an *inbound* group
/// session where this key pair has been removed and which can be used solely
/// for receipt and decryption of messages.
///
/// Such an inbound group session is typically sent by the outbound group
/// session owner to each of the receiving parties via a secure peer-to-peer
/// channel (e.g. an Olm channel).
pub struct GroupSession {
ratchet: Ratchet,
signing_key: Ed25519Keypair,
}
impl Default for GroupSession {
fn default() -> Self {
Self::new()
}
}
impl GroupSession {
/// Construct a new group session, with a random ratchet state and signing
/// key pair.
pub fn new() -> Self {
let signing_key = Ed25519Keypair::new();
Self { signing_key, ratchet: Ratchet::new() }
}
/// Returns the globally unique session ID, in base64-encoded form.
///
/// A session ID is the public part of the Ed25519 key pair associated with
/// the group session. Due to the construction, every session ID is
/// (probabilistically) globally unique.
pub fn session_id(&self) -> String {
self.signing_key.public_key().to_base64()
}
/// Return the current message index.
///
/// The message index is incremented each time a message is encrypted with
/// the group session.
pub fn message_index(&self) -> u32 {
self.ratchet.index()
}
/// Encrypt the `plaintext` with the group session.
///
/// The resulting ciphertext is MAC-ed, then signed with the group session's
/// Ed25519 key pair and finally base64-encoded.
pub fn encrypt(&mut self, plaintext: &str) -> MegolmMessage {
let cipher = Cipher::new_megolm(self.ratchet.as_bytes());
let message = MegolmMessage::encrypt_private(
self.message_index(),
&cipher,
&self.signing_key,
plaintext.as_ref(),
);
self.ratchet.advance();
message
}
/// Export the group session into a session key.
///
/// The session key contains the key version constant, the current message
/// index, the ratchet state and the *public* part of the signing key pair.
/// It is signed by the signing key pair for authenticity.
///
/// The session key is in a portable format, suitable for sending over the
/// network. It is typically sent to other group participants so that they
/// can reconstruct an inbound group session in order to decrypt messages
/// sent by this group session.
pub fn session_key(&self) -> SessionKey {
let mut session_key = SessionKey::new(&self.ratchet, self.signing_key.public_key());
let signature = self.signing_key.sign(&session_key.to_signature_bytes());
session_key.signature = signature;
session_key
}
/// Convert the group session into a struct which implements
/// [`serde::Serialize`] and [`serde::Deserialize`].
pub fn pickle(&self) -> GroupSessionPickle {
GroupSessionPickle { ratchet: self.ratchet.clone(), signing_key: self.signing_key.clone() }
}
/// Restore a [`GroupSession`] from a previously saved
/// [`GroupSessionPickle`].
pub fn from_pickle(pickle: GroupSessionPickle) -> Self {
pickle.into()
}
}
/// A format suitable for serialization which implements [`serde::Serialize`]
/// and [`serde::Deserialize`]. Obtainable by calling [`GroupSession::pickle`].
#[derive(Serialize, Deserialize)]
pub struct GroupSessionPickle {
ratchet: Ratchet,
signing_key: Ed25519Keypair,
}
impl GroupSessionPickle {
/// Serialize and encrypt the pickle using the given key.
///
/// This is the inverse of [`GroupSessionPickle::from_encrypted`].
pub fn encrypt(self, pickle_key: &[u8; 32]) -> String {
pickle(&self, pickle_key)
}
/// Obtain a pickle from a ciphertext by decrypting and deserializing using
/// the given key.
///
/// This is the inverse of [`GroupSessionPickle::encrypt`].
pub fn from_encrypted(ciphertext: &str, pickle_key: &[u8; 32]) -> Result<Self, PickleError> {
unpickle(ciphertext, pickle_key)
}
}
impl From<GroupSessionPickle> for GroupSession {
fn from(pickle: GroupSessionPickle) -> Self {
Self { ratchet: pickle.ratchet, signing_key: pickle.signing_key }
}
}